Merge pull request #32 from Gottox/master

Adding xbps_fetch_file_dest and support custom file output names for xbps-uhelper fetch
This commit is contained in:
Juan RP 2014-03-04 14:29:56 +01:00
commit 01dfc5b0ac
3 changed files with 42 additions and 15 deletions

View File

@ -49,7 +49,7 @@ usage(void)
" Action arguments:\n"
" cmpver\t\t<instver> <reqver>\n"
" digest\t\t<file> <file1+N>\n"
" fetch\t\t<URL> <URL1+N>\n"
" fetch\t\t<URL[::filename]> <URL1+N[::filename]>\n"
" getpkgdepname\t<string>\n"
" getpkgdepversion\t<string>\n"
" getpkgname\t\t<string>\n"
@ -87,7 +87,7 @@ main(int argc, char **argv)
struct xbps_handle xh;
struct xferstat xfer;
const char *version, *rootdir = NULL, *confdir = NULL;
char *pkgname, *hash;
char *pkgname, *hash, *sep;
int flags = 0, c, rv = 0;
while ((c = getopt(argc, argv, "C:dr:V")) != -1) {
@ -248,13 +248,19 @@ main(int argc, char **argv)
usage();
for (int i = 1; i < argc; i++) {
rv = xbps_fetch_file(&xh, argv[i], "v");
if( (sep = strrchr(argv[i], '>')) ) {
*sep = '\0';
rv = xbps_fetch_file_dest(&xh, argv[i], sep+1, "v");
} else {
rv = xbps_fetch_file(&xh, argv[i], "v");
}
if (rv == -1) {
printf("%s: %s\n", argv[1],
printf("%s: %s\n", argv[i],
xbps_fetch_error_string());
} else if (rv == 0) {
printf("%s: file is identical than remote.\n",
argv[1]);
argv[i]);
} else
rv = 0;
}

View File

@ -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().
*

View File

@ -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);
}