XBPS Library API 20240111
The X Binary Package System
cb_util.c
1/*-
2 * Copyright (c) 2011-2013 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#include <stdio.h>
27#include <stdbool.h>
28#include <stdlib.h>
29#include <string.h>
30#include <stdarg.h>
31#include <errno.h>
32
33#include "xbps_api_impl.h"
34
35#ifdef __clang__
36#pragma clang diagnostic ignored "-Wformat-nonliteral"
37#endif
38
39void HIDDEN
40xbps_set_cb_fetch(struct xbps_handle *xhp,
41 off_t file_size,
42 off_t file_offset,
43 off_t file_dloaded,
44 const char *file_name,
45 bool cb_start,
46 bool cb_update,
47 bool cb_end)
48{
49 struct xbps_fetch_cb_data xfcd;
50
51 if (xhp->fetch_cb == NULL)
52 return;
53
54 xfcd.xhp = xhp;
55 xfcd.file_size = file_size;
56 xfcd.file_offset = file_offset;
57 xfcd.file_dloaded = file_dloaded;
58 xfcd.file_name = file_name;
59 xfcd.cb_start = cb_start;
60 xfcd.cb_update = cb_update;
61 xfcd.cb_end = cb_end;
62 (*xhp->fetch_cb)(&xfcd, xhp->fetch_cb_data);
63}
64
65int HIDDEN
66xbps_set_cb_state(struct xbps_handle *xhp,
67 xbps_state_t state,
68 int err,
69 const char *arg,
70 const char *fmt,
71 ...)
72{
73 struct xbps_state_cb_data xscd;
74 char *buf = NULL;
75 va_list va;
76 int retval;
77
78 if (xhp->state_cb == NULL)
79 return 0;
80
81 xscd.xhp = xhp;
82 xscd.state = state;
83 xscd.err = err;
84 xscd.arg = arg;
85 if (fmt != NULL) {
86 va_start(va, fmt);
87 retval = vasprintf(&buf, fmt, va);
88 va_end(va);
89 if (retval <= 0)
90 xscd.desc = NULL;
91 else
92 xscd.desc = buf;
93 }
94 retval = (*xhp->state_cb)(&xscd, xhp->state_cb_data);
95 if (buf != NULL)
96 free(buf);
97
98 return retval;
99}
struct xbps_handle * xhp
Definition xbps.h:439
off_t file_dloaded
Definition xbps.h:457
const char * file_name
Definition xbps.h:463
void * fetch_cb_data
Definition xbps.h:619
int(* state_cb)(const struct xbps_state_cb_data *, void *)
Definition xbps.h:588
void * state_cb_data
Definition xbps.h:595
const char * arg
Definition xbps.h:408
off_t file_size
Definition xbps.h:445
void(* fetch_cb)(const struct xbps_fetch_cb_data *, void *)
Definition xbps.h:612
xbps_state_t state
Definition xbps.h:420
off_t file_offset
Definition xbps.h:451
Structure to be passed as argument to the state function callback. All members are read-only and set ...
Definition xbps.h:389
Generic XBPS structure handler for initialization.
Definition xbps.h:550
Structure to be passed to the fetch function callback.
Definition xbps.h:433
xbps_state_t
Definition xbps.h:329