Introduce struct xbps_handle and use it for xbps_init().
This structure sets up function callbacks for fetching files and unpacking binary packages, as well as setting the debug boolean. This way the affected functions (xbps_fetch_file() and xbps_unpack_binary_pkg()) do not need to accept the fn cb pointers and data as arguments. Bump XBPS_RELVER.
This commit is contained in:
@@ -89,10 +89,9 @@ int
|
||||
xbps_fetch_file(const char *uri,
|
||||
const char *outputdir,
|
||||
bool refetch,
|
||||
const char *flags,
|
||||
void (*progress_cb)(void *),
|
||||
struct xbps_fetch_progress_data *xfpd)
|
||||
const char *flags)
|
||||
{
|
||||
struct xbps_handle *xhp;
|
||||
struct stat st;
|
||||
struct url *url = NULL;
|
||||
struct url_stat url_st;
|
||||
@@ -110,6 +109,7 @@ xbps_fetch_file(const char *uri,
|
||||
fetchLastErrCode = 0;
|
||||
fetchTimeout = 30; /* 30 seconds of timeout */
|
||||
|
||||
xhp = xbps_handle_get();
|
||||
/*
|
||||
* Get the filename specified in URI argument.
|
||||
*/
|
||||
@@ -257,15 +257,15 @@ xbps_fetch_file(const char *uri,
|
||||
* and let the user know that the transfer is going to start
|
||||
* immediately.
|
||||
*/
|
||||
if (progress_cb != NULL && xfpd != NULL) {
|
||||
xfpd->file_name = filename;
|
||||
xfpd->file_size = url_st.size;
|
||||
xfpd->file_offset = url->offset;
|
||||
xfpd->file_dloaded = -1;
|
||||
xfpd->cb_start = true;
|
||||
xfpd->cb_update = false;
|
||||
xfpd->cb_end = false;
|
||||
progress_cb(xfpd);
|
||||
if (xhp != NULL && xhp->xbps_fetch_cb != NULL && xhp->xfpd != NULL) {
|
||||
xhp->xfpd->file_name = filename;
|
||||
xhp->xfpd->file_size = url_st.size;
|
||||
xhp->xfpd->file_offset = url->offset;
|
||||
xhp->xfpd->file_dloaded = -1;
|
||||
xhp->xfpd->cb_start = true;
|
||||
xhp->xfpd->cb_update = false;
|
||||
xhp->xfpd->cb_end = false;
|
||||
xhp->xbps_fetch_cb(xhp->xfpd);
|
||||
}
|
||||
/*
|
||||
* Start fetching requested file.
|
||||
@@ -282,11 +282,12 @@ xbps_fetch_file(const char *uri,
|
||||
* Let the fetch progress callback know that
|
||||
* we are sucking more bytes from it.
|
||||
*/
|
||||
if (progress_cb != NULL && xfpd != NULL) {
|
||||
xfpd->file_dloaded = bytes_dload;
|
||||
xfpd->cb_start = false;
|
||||
xfpd->cb_update = true;
|
||||
progress_cb(xfpd);
|
||||
if (xhp != NULL && xhp->xbps_fetch_cb != NULL &&
|
||||
xhp->xfpd != NULL) {
|
||||
xhp->xfpd->file_dloaded = bytes_dload;
|
||||
xhp->xfpd->cb_start = false;
|
||||
xhp->xfpd->cb_update = true;
|
||||
xhp->xbps_fetch_cb(xhp->xfpd);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -294,10 +295,10 @@ xbps_fetch_file(const char *uri,
|
||||
* Let the fetch progress callback know that the file
|
||||
* has been fetched.
|
||||
*/
|
||||
if (progress_cb != NULL && xfpd != NULL) {
|
||||
xfpd->cb_update = false;
|
||||
xfpd->cb_end = true;
|
||||
progress_cb(xfpd);
|
||||
if (xhp != NULL && xhp->xbps_fetch_cb != NULL && xhp->xfpd != NULL) {
|
||||
xhp->xfpd->cb_update = false;
|
||||
xhp->xfpd->cb_end = true;
|
||||
xhp->xbps_fetch_cb(xhp->xfpd);
|
||||
}
|
||||
if (bytes_read == -1) {
|
||||
xbps_error_printf("IO error while fetching %s: %s\n", filename,
|
||||
|
||||
@@ -40,14 +40,18 @@
|
||||
* Use these functions to initialize some parameters before starting
|
||||
* using libxbps and finalize usage to release resources at the end.
|
||||
*/
|
||||
static bool with_debug;
|
||||
static bool debug;
|
||||
static struct xbps_handle *xhp;
|
||||
|
||||
void
|
||||
xbps_init(bool debug)
|
||||
xbps_init(struct xbps_handle *xh)
|
||||
{
|
||||
assert(xh != NULL);
|
||||
|
||||
xhp = xh;
|
||||
debug = xhp->with_debug;
|
||||
xbps_fetch_set_cache_connection(XBPS_FETCH_CACHECONN,
|
||||
XBPS_FETCH_CACHECONN_HOST);
|
||||
with_debug = debug;
|
||||
}
|
||||
|
||||
void
|
||||
@@ -56,6 +60,13 @@ xbps_end(void)
|
||||
xbps_regpkgdb_dictionary_release();
|
||||
xbps_repository_pool_release();
|
||||
xbps_fetch_unset_cache_connection();
|
||||
xhp = NULL;
|
||||
}
|
||||
|
||||
struct xbps_handle HIDDEN *
|
||||
xbps_handle_get(void)
|
||||
{
|
||||
return xhp;
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -72,7 +83,7 @@ xbps_dbg_printf_append(const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
if (!with_debug)
|
||||
if (!debug)
|
||||
return;
|
||||
|
||||
va_start(ap, fmt);
|
||||
@@ -85,7 +96,7 @@ xbps_dbg_printf(const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
if (!with_debug)
|
||||
if (!debug)
|
||||
return;
|
||||
|
||||
va_start(ap, fmt);
|
||||
|
||||
@@ -149,10 +149,10 @@ remove_metafile(const char *file, const char *pkgname, const char *version)
|
||||
* archive_read_set_progress_callback() from libarchive(3) cannot be used
|
||||
* here because sometimes it misses some entries by unknown reasons.
|
||||
*/
|
||||
#define RUN_PROGRESS_CB() \
|
||||
do { \
|
||||
if (progress_cb != NULL && xupd != NULL) \
|
||||
(*progress_cb)(xupd); \
|
||||
#define RUN_PROGRESS_CB() \
|
||||
do { \
|
||||
if (xhp != NULL && xhp->xbps_unpack_cb != NULL && xhp->xupd != NULL) \
|
||||
(*xhp->xbps_unpack_cb)(xhp->xupd); \
|
||||
} while (0)
|
||||
|
||||
static int
|
||||
@@ -160,8 +160,7 @@ unpack_archive(prop_dictionary_t pkg_repod,
|
||||
struct archive *ar,
|
||||
const char *pkgname,
|
||||
const char *version,
|
||||
void (*progress_cb)(void *),
|
||||
struct xbps_unpack_progress_data *xupd)
|
||||
struct xbps_handle *xhp)
|
||||
{
|
||||
prop_dictionary_t propsd = NULL, filesd = NULL, old_filesd = NULL;
|
||||
prop_array_t array;
|
||||
@@ -212,11 +211,12 @@ unpack_archive(prop_dictionary_t pkg_repod,
|
||||
while (archive_read_next_header(ar, &entry) == ARCHIVE_OK) {
|
||||
entry_pname = archive_entry_pathname(entry);
|
||||
set_extract_flags(&flags, update);
|
||||
if (progress_cb != NULL && xupd != NULL) {
|
||||
xupd->entry = entry_pname;
|
||||
xupd->entry_size = archive_entry_size(entry);
|
||||
xupd->entry_is_metadata = false;
|
||||
xupd->entry_is_conf = false;
|
||||
if (xhp != NULL && xhp->xbps_unpack_cb != NULL &&
|
||||
xhp->xupd != NULL) {
|
||||
xhp->xupd->entry = entry_pname;
|
||||
xhp->xupd->entry_size = archive_entry_size(entry);
|
||||
xhp->xupd->entry_is_metadata = false;
|
||||
xhp->xupd->entry_is_conf = false;
|
||||
}
|
||||
|
||||
if (strcmp("./INSTALL", entry_pname) == 0) {
|
||||
@@ -246,9 +246,9 @@ unpack_archive(prop_dictionary_t pkg_repod,
|
||||
goto out;
|
||||
}
|
||||
nmetadata++;
|
||||
if (xupd != NULL) {
|
||||
xupd->entry_is_metadata = true;
|
||||
xupd->entry_extract_count++;
|
||||
if (xhp->xupd != NULL) {
|
||||
xhp->xupd->entry_is_metadata = true;
|
||||
xhp->xupd->entry_extract_count++;
|
||||
}
|
||||
RUN_PROGRESS_CB();
|
||||
continue;
|
||||
@@ -260,9 +260,9 @@ unpack_archive(prop_dictionary_t pkg_repod,
|
||||
goto out;
|
||||
|
||||
nmetadata++;
|
||||
if (xupd != NULL) {
|
||||
xupd->entry_is_metadata = true;
|
||||
xupd->entry_extract_count++;
|
||||
if (xhp->xupd != NULL) {
|
||||
xhp->xupd->entry_is_metadata = true;
|
||||
xhp->xupd->entry_extract_count++;
|
||||
}
|
||||
RUN_PROGRESS_CB();
|
||||
continue;
|
||||
@@ -279,9 +279,9 @@ unpack_archive(prop_dictionary_t pkg_repod,
|
||||
goto out;
|
||||
}
|
||||
nmetadata++;
|
||||
if (xupd != NULL) {
|
||||
xupd->entry_is_metadata = true;
|
||||
xupd->entry_extract_count++;
|
||||
if (xhp->xupd != NULL) {
|
||||
xhp->xupd->entry_is_metadata = true;
|
||||
xhp->xupd->entry_extract_count++;
|
||||
}
|
||||
RUN_PROGRESS_CB();
|
||||
continue;
|
||||
@@ -299,9 +299,9 @@ unpack_archive(prop_dictionary_t pkg_repod,
|
||||
goto out;
|
||||
}
|
||||
nmetadata++;
|
||||
if (xupd != NULL) {
|
||||
xupd->entry_is_metadata = true;
|
||||
xupd->entry_extract_count++;
|
||||
if (xhp->xupd != NULL) {
|
||||
xhp->xupd->entry_is_metadata = true;
|
||||
xhp->xupd->entry_extract_count++;
|
||||
}
|
||||
RUN_PROGRESS_CB();
|
||||
continue;
|
||||
@@ -330,16 +330,16 @@ unpack_archive(prop_dictionary_t pkg_repod,
|
||||
* Compute total entries in progress data, if set.
|
||||
* total_entries = metadata + files + conf_files + links.
|
||||
*/
|
||||
if (xupd != NULL) {
|
||||
xupd->entry_total_count = nmetadata;
|
||||
if (xhp->xupd != NULL) {
|
||||
xhp->xupd->entry_total_count = nmetadata;
|
||||
array = prop_dictionary_get(filesd, "files");
|
||||
xupd->entry_total_count +=
|
||||
xhp->xupd->entry_total_count +=
|
||||
(ssize_t)prop_array_count(array);
|
||||
array = prop_dictionary_get(filesd, "conf_files");
|
||||
xupd->entry_total_count +=
|
||||
xhp->xupd->entry_total_count +=
|
||||
(ssize_t)prop_array_count(array);
|
||||
array = prop_dictionary_get(filesd, "links");
|
||||
xupd->entry_total_count +=
|
||||
xhp->xupd->entry_total_count +=
|
||||
(ssize_t)prop_array_count(array);
|
||||
}
|
||||
|
||||
@@ -354,8 +354,8 @@ unpack_archive(prop_dictionary_t pkg_repod,
|
||||
/* error */
|
||||
goto out;
|
||||
} else if (rv == 1) {
|
||||
if (xupd != NULL)
|
||||
xupd->entry_is_conf = true;
|
||||
if (xhp->xupd != NULL)
|
||||
xhp->xupd->entry_is_conf = true;
|
||||
|
||||
rv = xbps_entry_install_conf_file(filesd,
|
||||
entry, entry_pname, pkgname, version);
|
||||
@@ -397,8 +397,8 @@ unpack_archive(prop_dictionary_t pkg_repod,
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (xupd != NULL)
|
||||
xupd->entry_extract_count++;
|
||||
if (xhp->xupd != NULL)
|
||||
xhp->xupd->entry_extract_count++;
|
||||
|
||||
RUN_PROGRESS_CB();
|
||||
}
|
||||
@@ -461,10 +461,9 @@ out:
|
||||
#undef RUN_PROGRESS_CB
|
||||
|
||||
int
|
||||
xbps_unpack_binary_pkg(prop_dictionary_t pkg_repod,
|
||||
void (*progress_cb)(void *),
|
||||
struct xbps_unpack_progress_data *xupd)
|
||||
xbps_unpack_binary_pkg(prop_dictionary_t pkg_repod)
|
||||
{
|
||||
struct xbps_handle *xhp;
|
||||
struct archive *ar;
|
||||
const char *pkgname, *version, *repoloc;
|
||||
char *bpkg;
|
||||
@@ -503,15 +502,15 @@ xbps_unpack_binary_pkg(prop_dictionary_t pkg_repod,
|
||||
/*
|
||||
* Set extract progress callback if specified.
|
||||
*/
|
||||
if (progress_cb != NULL && xupd != NULL) {
|
||||
xupd->entry_extract_count = 0;
|
||||
xupd->entry_total_count = 0;
|
||||
xhp = xbps_handle_get();
|
||||
if (xhp != NULL && xhp->xbps_unpack_cb != NULL && xhp->xupd != NULL) {
|
||||
xhp->xupd->entry_extract_count = 0;
|
||||
xhp->xupd->entry_total_count = 0;
|
||||
}
|
||||
/*
|
||||
* Extract archive files.
|
||||
*/
|
||||
rv = unpack_archive(pkg_repod, ar, pkgname, version,
|
||||
progress_cb, xupd);
|
||||
rv = unpack_archive(pkg_repod, ar, pkgname, version, xhp);
|
||||
if (rv != 0) {
|
||||
xbps_error_printf("failed to unpack `%s' binpkg: %s\n",
|
||||
bpkg, strerror(rv));
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*-
|
||||
* Copyright (c) 2009 Juan Romero Pardines.
|
||||
* Copyright (c) 2009-2011 Juan Romero Pardines.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -82,9 +82,7 @@ xbps_get_remote_repo_string(const char *uri)
|
||||
* size and/or mtime match) and 1 if downloaded successfully.
|
||||
*/
|
||||
int
|
||||
xbps_repository_sync_pkg_index(const char *uri,
|
||||
void (*progress_cb)(void *),
|
||||
struct xbps_fetch_progress_data *xfpd)
|
||||
xbps_repository_sync_pkg_index(const char *uri)
|
||||
{
|
||||
struct url *url = NULL;
|
||||
struct utsname un;
|
||||
@@ -163,8 +161,7 @@ xbps_repository_sync_pkg_index(const char *uri,
|
||||
/*
|
||||
* Download pkg-index.plist file from repository.
|
||||
*/
|
||||
rv = xbps_fetch_file(rpidx, fetch_outputdir, true, NULL,
|
||||
progress_cb, xfpd);
|
||||
rv = xbps_fetch_file(rpidx, fetch_outputdir, true, NULL);
|
||||
if (rv == -1) {
|
||||
(void)remove(tmp_metafile);
|
||||
goto out;
|
||||
|
||||
Reference in New Issue
Block a user