XBPS Library API 20250813
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
32#include "xbps_api_impl.h"
33
34void HIDDEN
35xbps_set_cb_fetch(struct xbps_handle *xhp,
36 off_t file_size,
37 off_t file_offset,
38 off_t file_dloaded,
39 const char *file_name,
40 bool cb_start,
41 bool cb_update,
42 bool cb_end)
43{
44 struct xbps_fetch_cb_data xfcd;
45
46 if (xhp->fetch_cb == NULL)
47 return;
48
49 xfcd.xhp = xhp;
50 xfcd.file_size = file_size;
51 xfcd.file_offset = file_offset;
52 xfcd.file_dloaded = file_dloaded;
53 xfcd.file_name = file_name;
54 xfcd.cb_start = cb_start;
55 xfcd.cb_update = cb_update;
56 xfcd.cb_end = cb_end;
57 (*xhp->fetch_cb)(&xfcd, xhp->fetch_cb_data);
58}
59
60int HIDDEN PRINTF_LIKE(5, 6)
61xbps_set_cb_state(struct xbps_handle *xhp,
62 xbps_state_t state,
63 int err,
64 const char *arg,
65 const char *fmt,
66 ...)
67{
68 struct xbps_state_cb_data xscd;
69 char *buf = NULL;
70 va_list va;
71 int retval;
72
73 if (xhp->state_cb == NULL)
74 return 0;
75
76 xscd.xhp = xhp;
77 xscd.state = state;
78 xscd.err = err;
79 xscd.arg = arg;
80 if (fmt != NULL) {
81 va_start(va, fmt);
82 retval = vasprintf(&buf, fmt, va);
83 va_end(va);
84 if (retval <= 0)
85 xscd.desc = NULL;
86 else
87 xscd.desc = buf;
88 }
89 retval = (*xhp->state_cb)(&xscd, xhp->state_cb_data);
90 if (buf != NULL)
91 free(buf);
92
93 return retval;
94}
void * fetch_cb_data
Definition xbps.h:633
off_t file_size
Definition xbps.h:455
struct xbps_handle * xhp
Definition xbps.h:449
const char * file_name
Definition xbps.h:473
off_t file_offset
Definition xbps.h:461
int(* state_cb)(const struct xbps_state_cb_data *, void *)
Definition xbps.h:602
const char * arg
Definition xbps.h:418
void * state_cb_data
Definition xbps.h:609
void(* fetch_cb)(const struct xbps_fetch_cb_data *, void *)
Definition xbps.h:626
off_t file_dloaded
Definition xbps.h:467
Structure to be passed as argument to the state function callback. All members are read-only and set ...
Definition xbps.h:399
Generic XBPS structure handler for initialization.
Definition xbps.h:560
Structure to be passed to the fetch function callback.
Definition xbps.h:443
xbps_state_t
Definition xbps.h:341