XBPS Library API  0.19
The X Binary Package System
cb_util.c
1 /*-
2  * Copyright (c) 2011-2012 Juan Romero Pardines.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #ifdef HAVE_VASPRINTF
27 # define _GNU_SOURCE /* for vasprintf(3) */
28 #endif
29 
30 #include <stdio.h>
31 #include <stdbool.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <stdarg.h>
35 #include <errno.h>
36 
37 #ifdef HAVE_CONFIG_H
38 #include "config.h"
39 #endif
40 
41 #include "xbps_api_impl.h"
42 
43 void HIDDEN
44 xbps_set_cb_fetch(struct xbps_handle *xhp,
45  off_t file_size,
46  off_t file_offset,
47  off_t file_dloaded,
48  const char *file_name,
49  bool cb_start,
50  bool cb_update,
51  bool cb_end)
52 {
53  struct xbps_fetch_cb_data xfcd;
54 
55  if (xhp->fetch_cb == NULL)
56  return;
57 
58  xfcd.xhp = xhp;
59  xfcd.file_size = file_size;
60  xfcd.file_offset = file_offset;
61  xfcd.file_dloaded = file_dloaded;
62  xfcd.file_name = file_name;
63  xfcd.cb_start = cb_start;
64  xfcd.cb_update = cb_update;
65  xfcd.cb_end = cb_end;
66  (*xhp->fetch_cb)(&xfcd, xhp->fetch_cb_data);
67 }
68 
69 void HIDDEN
70 xbps_set_cb_state(struct xbps_handle *xhp,
71  xbps_state_t state,
72  int err,
73  const char *arg0,
74  const char *arg1,
75  const char *fmt,
76  ...)
77 {
78  struct xbps_state_cb_data xscd;
79  char *buf = NULL;
80  va_list va;
81  int retval;
82 
83  if (xhp->state_cb == NULL)
84  return;
85 
86  xscd.xhp = xhp;
87  xscd.state = state;
88  xscd.err = err;
89  xscd.arg0 = arg0;
90  xscd.arg1 = arg1;
91  if (fmt != NULL) {
92  va_start(va, fmt);
93  retval = vasprintf(&buf, fmt, va);
94  va_end(va);
95  if (retval <= 0)
96  xscd.desc = NULL;
97  else
98  xscd.desc = buf;
99  }
100  (*xhp->state_cb)(&xscd, xhp->state_cb_data);
101  if (buf != NULL)
102  free(buf);
103 }