Some changes to know if a file was downloaded successfully or not.

Improved xbps-repo sync/add and xbps-fetch to print informative
messages when a transfer was not necessary because local/remote size
and/or mtime matched.

--HG--
extra : convert_revision : xtraeme%40gmail.com-20091124050326-owez7140fdxwepuk
This commit is contained in:
Juan RP
2009-11-24 05:03:26 +00:00
parent 83f71f3d2b
commit c676622869
5 changed files with 124 additions and 85 deletions

View File

@@ -183,6 +183,10 @@ print_time(time_t *t)
}
#endif
/*
* Returns -1 on error, 0 if not download (because local/remote
* size and/or mtime match) and 1 if downloaded successfully.
*/
int SYMEXPORT
xbps_fetch_file(const char *uri, const char *outputdir, bool refetch,
const char *flags)
@@ -207,15 +211,17 @@ xbps_fetch_file(const char *uri, const char *outputdir, bool refetch,
* Get the filename specified in URI argument.
*/
filename = strrchr(uri, '/');
if (filename == NULL)
return EINVAL;
if (filename == NULL) {
errno = EINVAL;
return -1;
}
filename++;
/*
* Compute destination file path.
*/
destfile = xbps_xasprintf("%s/%s", outputdir, filename);
if (destfile == NULL) {
rv = errno;
rv = -1;
goto out;
}
/*
@@ -227,7 +233,7 @@ xbps_fetch_file(const char *uri, const char *outputdir, bool refetch,
restart = true;
} else {
if (errno != ENOENT) {
rv = errno;
rv = -1;
goto out;
}
}
@@ -235,7 +241,7 @@ xbps_fetch_file(const char *uri, const char *outputdir, bool refetch,
* Prepare stuff for libfetch.
*/
if ((url = fetchParseURL(uri)) == NULL) {
rv = fetchLastErrCode;
rv = -1;
goto out;
}
@@ -246,10 +252,9 @@ xbps_fetch_file(const char *uri, const char *outputdir, bool refetch,
/*
* Issue a HEAD request to know size and mtime.
*/
if (fetchStat(url, &url_st, NULL) == -1) {
rv = fetchLastErrCode;
if ((rv = fetchStat(url, &url_st, NULL)) == -1)
goto out;
}
/*
* If mtime and size match do nothing.
*/
@@ -268,7 +273,7 @@ xbps_fetch_file(const char *uri, const char *outputdir, bool refetch,
* Remove current file (if exists).
*/
if (restart && remove(destfile) == -1) {
rv = errno;
rv = -1;
goto out;
}
restart = false;
@@ -313,22 +318,23 @@ xbps_fetch_file(const char *uri, const char *outputdir, bool refetch,
if (url->length == 0)
goto out;
}
rv = fetchLastErrCode;
rv = -1;
goto out;
}
if (url_st.size == -1) {
printf("Remote file size is unknown!\n");
rv = EINVAL;
errno = EINVAL;
rv = -1;
goto out;
} else if (st.st_size > url_st.size) {
printf("Local file %s is greater than remote file!\n",
filename);
rv = EFBIG;
errno = EFBIG;
rv = -1;
goto out;
} else if (restart && url_st.mtime && url_st.size &&
url_st.size == st.st_size && url_st.mtime == st.st_mtime) {
/* Local and remote size/mtime match, do nothing. */
rv = 0;
goto out;
}
fprintf(stderr, "Connected to %s.\n", url->host);
@@ -342,7 +348,7 @@ xbps_fetch_file(const char *uri, const char *outputdir, bool refetch,
fd = open(destfile, O_WRONLY|O_CREAT|O_TRUNC, 0644);
if (fd == -1) {
rv = errno;
rv = -1;
goto out;
}
@@ -354,7 +360,7 @@ xbps_fetch_file(const char *uri, const char *outputdir, bool refetch,
bytes_written = write(fd, buf, (size_t)bytes_read);
if (bytes_written != bytes_read) {
fprintf(stderr, "Couldn't write to %s!\n", destfile);
rv = errno;
rv = -1;
goto out;
}
bytes_dld += bytes_read;
@@ -363,7 +369,8 @@ xbps_fetch_file(const char *uri, const char *outputdir, bool refetch,
if (bytes_read == -1) {
fprintf(stderr, "IO error while fetching %s: %s\n", filename,
fetchLastErrString);
rv = EINVAL;
errno = EIO;
rv = -1;
goto out;
}
stat_end(&xs);
@@ -379,7 +386,11 @@ xbps_fetch_file(const char *uri, const char *outputdir, bool refetch,
tv[1].tv_sec = url_st.mtime;
tv[0].tv_usec = tv[1].tv_usec = 0;
if (utimes(destfile, tv) == -1)
rv = errno;
rv = -1;
else {
/* File downloaded successfully */
rv = 1;
}
out:
if (fd != -1)

View File

@@ -68,6 +68,10 @@ xbps_get_remote_repo_string(const char *uri)
return p;
}
/*
* Returns -1 on error, 0 if transfer was not necessary (local/remote
* size and/or mtime match) and 1 if downloaded successfully.
*/
int SYMEXPORT
xbps_sync_repository_pkg_index(const char *uri)
{
@@ -84,15 +88,15 @@ xbps_sync_repository_pkg_index(const char *uri)
metadir = tmp_metafile = lrepofile = NULL;
if (uname(&un) == -1)
return errno;
return -1;
if ((url = fetchParseURL(uri)) == NULL)
return errno;
return -1;
uri_fixedp = xbps_get_remote_repo_string(uri);
if (uri_fixedp == NULL) {
fetchFreeURL(url);
return errno;
return -1;
}
/*
@@ -101,17 +105,18 @@ xbps_sync_repository_pkg_index(const char *uri)
metadir = xbps_xasprintf("%s/%s", xbps_get_rootdir(),
XBPS_META_PATH);
if (metadir == NULL) {
rv = errno;
rv = -1;
goto out;
}
rv = stat(metadir, &st);
if (rv == -1 && errno == ENOENT) {
if (mkpath(metadir, 0755) == -1) {
rv = errno;
rv = -1;
goto out;
}
} else if (rv == 0 && !S_ISDIR(st.st_mode)) {
rv = ENOTDIR;
errno = ENOTDIR;
rv = -1;
goto out;
}
@@ -120,7 +125,7 @@ xbps_sync_repository_pkg_index(const char *uri)
*/
rpidx = xbps_xasprintf("%s/%s/%s", uri, un.machine, XBPS_PKGINDEX);
if (rpidx == NULL) {
rv = errno;
rv = -1;
goto out;
}
/*
@@ -129,7 +134,7 @@ xbps_sync_repository_pkg_index(const char *uri)
*/
tmp_metafile = xbps_xasprintf("%s/%s", metadir, XBPS_PKGINDEX);
if (tmp_metafile == NULL) {
rv = errno;
rv = -1;
goto out;
}
/*
@@ -138,7 +143,7 @@ xbps_sync_repository_pkg_index(const char *uri)
lrepodir = xbps_xasprintf("%s/%s/%s/%s",
xbps_get_rootdir(), XBPS_META_PATH, uri_fixedp, un.machine);
if (lrepodir == NULL) {
rv = errno;
rv = -1;
goto out;
}
/*
@@ -156,7 +161,7 @@ xbps_sync_repository_pkg_index(const char *uri)
* Download pkg-index.plist file from repository.
*/
if ((rv = xbps_fetch_file(rpidx, fetch_outputdir,
true, NULL)) != 0) {
true, NULL)) == -1) {
(void)remove(tmp_metafile);
goto out;
}
@@ -171,11 +176,12 @@ xbps_sync_repository_pkg_index(const char *uri)
rv = stat(lrepodir, &st);
if (rv == -1 && errno == ENOENT) {
if (mkpath(lrepodir, 0755) == -1) {
rv = errno;
rv = -1;
goto out;
}
} else if (rv == 0 && !S_ISDIR(st.st_mode)) {
rv = ENOTDIR;
errno = ENOTDIR;
rv = -1;
goto out;
}
/*
@@ -186,31 +192,34 @@ xbps_sync_repository_pkg_index(const char *uri)
dir = xbps_xasprintf("%s/%s/%s/noarch",
xbps_get_rootdir(), XBPS_META_PATH, uri_fixedp);
if (dir == NULL) {
rv = errno;
rv = -1;
goto out;
}
rv = stat(dir, &st);
if (rv == -1 && errno == ENOENT) {
if (mkpath(dir, 0755) == -1) {
free(dir);
rv = errno;
rv = -1;
goto out;
}
} else if (rv == 0 && !S_ISDIR(st.st_mode)) {
free(dir);
rv = ENOTDIR;
errno = ENOTDIR;
rv = -1;
goto out;
}
free(dir);
lrepofile = xbps_xasprintf("%s/%s", lrepodir, XBPS_PKGINDEX);
if (lrepofile == NULL) {
rv = errno;
rv = -1;
goto out;
}
/*
* Rename to destination file now it has been fetched successfully.
*/
rv = rename(tmp_metafile, lrepofile);
if ((rv = rename(tmp_metafile, lrepofile)) == 0)
rv = 1;
out:
if (rpidx)
free(rpidx);