From 59ee6b6733f8e7ada7fe35d897943d96e6f07245 Mon Sep 17 00:00:00 2001 From: Enno Boland Date: Tue, 4 Mar 2014 14:19:52 +0100 Subject: [PATCH] Adding xbps_fetch_file_dest(). This function is similiar to xbps_fetch_file(). In contrast to xbps_fetch_file() xbps_fetch_file_dest has an extra paramenter which allow to define an output file for the request. --- include/xbps.h.in | 14 ++++++++++++++ lib/download.c | 27 +++++++++++++++++---------- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/include/xbps.h.in b/include/xbps.h.in index 05b7adc5..557d83b0 100644 --- a/include/xbps.h.in +++ b/include/xbps.h.in @@ -670,6 +670,20 @@ int xbps_configure_packages(struct xbps_handle *xhp, bool flush); int xbps_fetch_file(struct xbps_handle *xhp, const char *uri, const char *flags); +/** + * Download a file from a remote URL to current working directory. + * + * @param[in] xhp Pointer to an xbps_handle struct. + * @param[in] uri Remote URI string. + * @param[in] filename Local filename to safe the file + * @param[in] flags Flags passed to libfetch's fetchXget(). + * + * @return -1 on error, 0 if not downloaded (because local/remote size/mtime + * do not match) and 1 if downloaded successfully. + **/ +int xbps_fetch_file_dest(struct xbps_handle *xhp, const char *uri, + const char *filename, const char *flags); + /** * Returns last error string reported by xbps_fetch_file(). * diff --git a/lib/download.c b/lib/download.c index 6a3c9303..0ebfb70d 100644 --- a/lib/download.c +++ b/lib/download.c @@ -91,7 +91,7 @@ xbps_fetch_error_string(void) } int -xbps_fetch_file(struct xbps_handle *xhp, const char *uri, const char *flags) +xbps_fetch_file_dest(struct xbps_handle *xhp, const char *uri, const char *filename, const char *flags) { struct stat st, st_tmpfile, *stp; struct url *url = NULL; @@ -100,7 +100,7 @@ xbps_fetch_file(struct xbps_handle *xhp, const char *uri, const char *flags) struct timespec ts[2]; off_t bytes_dload = 0; ssize_t bytes_read = 0, bytes_written = 0; - char buf[4096], *filename, *tempfile = NULL; + char buf[4096], *tempfile = NULL; char fetch_flags[8]; int fd = -1, rv = 0; bool refetch = false, restart = false; @@ -117,14 +117,6 @@ xbps_fetch_file(struct xbps_handle *xhp, const char *uri, const char *flags) memset(&fetch_flags, 0, sizeof(fetch_flags)); if (flags != NULL) strlcpy(fetch_flags, flags, 7); - /* - * Get the filename specified in URI argument. - */ - filename = strrchr(uri, '/') + 1; - if (filename == NULL) { - rv = -1; - goto out; - } tempfile = xbps_xasprintf("%s.part", filename); /* @@ -298,3 +290,18 @@ out: return rv; } +int +xbps_fetch_file(struct xbps_handle *xhp, const char *uri, const char *flags) +{ + char *filename; + + /* + * Get the filename specified in URI argument. + */ + filename = strrchr(uri, '/') + 1; + if (filename == NULL) { + return -1; + } + + return xbps_fetch_file_dest(xhp, uri, filename, flags); +}