Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F178104570
D61993.1795974526.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
6 KB
Referenced Files
None
Subscribers
None
D61993.1795974526.diff
View Options
diff --git a/usr.bin/fetch/fetch.1 b/usr.bin/fetch/fetch.1
--- a/usr.bin/fetch/fetch.1
+++ b/usr.bin/fetch/fetch.1
@@ -28,7 +28,7 @@
.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
-.Dd June 27, 2026
+.Dd November 9, 2026
.Dt FETCH 1
.Os
.Sh NAME
@@ -110,6 +110,11 @@
error when the requested object does not exist.
.It Fl a , -retry
Automatically retry the transfer upon soft failures.
+Each retry resumes where the previous attempt stopped.
+Over HTTP, when the server identifies the file with an entity tag, a
+retry resumes only if the file is unchanged, and starts over otherwise.
+A transfer which keeps being cut short is retried for as long as each
+attempt gets further than the ones before it.
.It Fl B Ar bytes , Fl -buffer-size= Ns Ar bytes
Specify the read buffer size in bytes.
The default is 16,384 bytes.
diff --git a/usr.bin/fetch/fetch.c b/usr.bin/fetch/fetch.c
--- a/usr.bin/fetch/fetch.c
+++ b/usr.bin/fetch/fetch.c
@@ -420,27 +420,28 @@
static int
fetch(char *URL, const char *path, int *is_http)
{
- struct url *url;
- struct url_stat us;
- struct stat sb, nsb;
+ struct stat nsb, sb;
struct xferstat xs;
+ struct url_stat us;
+ off_t count, kept, size_prev;
+ char *etag, *ptr, *tmppath;
FILE *f, *of;
- size_t size, readcnt, wr;
- off_t count, size_prev;
char flags[8];
+ size_t readcnt, size, wr;
const char *slash;
- char *tmppath;
- int r, tries;
- unsigned timeout;
+ struct url *url;
+ int changed, r, restarted;
int timedout = 0;
- char *ptr;
+ unsigned timeout;
f = of = NULL;
- tmppath = NULL;
+ etag = tmppath = NULL;
timeout = 0;
*flags = 0;
- count = 0;
+ count = kept = 0;
+ size_prev = -1;
+ restarted = 0;
/* set verbosity level */
if (v_level > 1)
@@ -528,8 +529,6 @@
goto success;
}
- tries = 1;
-again:
r = 0;
/*
* If the -r flag was specified, we have to compare the local
@@ -547,7 +546,7 @@
sb.st_size = -1;
if (!o_stdout) {
r = stat(path, &sb);
- if (r == 0 && (r_flag || tries > 1) && S_ISREG(sb.st_mode)) {
+ if (r == 0 && r_flag && S_ISREG(sb.st_mode)) {
url->offset = sb.st_size;
} else if (r == -1 || !S_ISREG(sb.st_mode)) {
/*
@@ -562,10 +561,24 @@
goto failure;
}
}
- size_prev = sb.st_size;
+again:
/* start the transfer */
+ free(etag);
+ etag = NULL;
+ if (url->validator != NULL &&
+ (etag = strdup(url->validator)) == NULL) {
+ warnx("%s", strerror(ENOMEM));
+ goto failure;
+ }
f = fetchXGet(url, &us, flags);
+ changed = (f == NULL && fetchLastErrCode == FETCH_RESTART);
+ if (changed) {
+ /* the server will not resume from our copy: start over */
+ warnx("%s: %s", URL, fetchLastErrString);
+ url->offset = 0;
+ f = fetchXGet(url, &us, flags);
+ }
if (f == NULL) {
if (i_flag && *is_http && fetchLastErrCode == FETCH_OK &&
strcmp(fetchLastErrString, "Not Modified") == 0) {
@@ -614,14 +627,49 @@
}
/* open output file */
- if (o_stdout) {
+ if (of != NULL) {
+ /* retrying */
+ if (etag != NULL && url->validator != NULL &&
+ strcmp(etag, url->validator) != 0)
+ changed = 1;
+ if (changed && !restarted) {
+ /* a new version of the file gets one fresh start */
+ restarted = 1;
+ size_prev = url->offset;
+ }
+ if (url->offset < kept) {
+ /*
+ * The server did not resume from the file we
+ * found here, which we may not overwrite: treat
+ * it as one that does not match.
+ */
+ if (R_flag) {
+ warnx("%s: local file no longer matches "
+ "remote", path);
+ goto failure_keep;
+ }
+ fclose(of);
+ of = NULL;
+ kept = 0;
+ /* picked up again later */
+ } else {
+ /* drop what the server did not resume */
+ if (fflush(of) != 0 ||
+ ftruncate(fileno(of), url->offset) != 0 ||
+ fseeko(of, url->offset, SEEK_SET) != 0) {
+ warn("%s", tmppath ? tmppath : path);
+ goto failure;
+ }
+ }
+ } else if (o_stdout) {
/* output to stdout */
of = stdout;
} else if (r_flag && sb.st_size != -1) {
/* resume mode, local file exists */
- if (!F_flag && us.mtime && sb.st_mtime != us.mtime && tries == 1) {
+ if (!F_flag && us.mtime && sb.st_mtime != us.mtime) {
/* no match! have to refetch */
fclose(f);
+ f = NULL;
/* if precious, warn the user and give up */
if (R_flag) {
warnx("%s: local modification time "
@@ -673,6 +721,9 @@
of = NULL;
/* picked up again later */
}
+ /* what was here before that offset is not ours to discard */
+ if (of != NULL)
+ kept = url->offset;
} else if (m_flag && sb.st_size != -1) {
/* mirror mode, local file exists */
if (sb.st_size == us.size && sb.st_mtime == us.mtime)
@@ -686,12 +737,14 @@
* remote files didn't match.
*/
- if (url->offset > 0) {
+ if (f == NULL || url->offset > 0) {
/*
* We tried to restart a transfer, but for
* some reason gave up - so we have to restart
* from scratch if we want the whole file
*/
+ if (f != NULL)
+ fclose(f);
url->offset = 0;
if ((f = fetchXGet(url, &us, flags)) == NULL) {
warnx("%s: %s", URL, fetchLastErrString);
@@ -729,6 +782,8 @@
}
}
count = url->offset;
+ if (size_prev == -1)
+ size_prev = count;
/* start the counter */
stat_start(&xs, path, us.size, count);
@@ -812,23 +867,26 @@
/* check the status of our files */
if (ferror(f))
warn("%s", URL);
- if (ferror(of))
+ if (ferror(of)) {
warn("%s", path);
- if (ferror(f) || ferror(of))
goto failure;
+ }
+ if (ferror(f)) {
+ if (!o_stdout && a_flag && count > size_prev)
+ goto retry;
+ goto failure;
+ }
}
/* did the transfer complete normally? */
if (us.size != -1 && count < us.size) {
warnx("%s appears to be truncated: %jd/%jd bytes",
path, (intmax_t)count, (intmax_t)us.size);
- if(!o_stdout && a_flag && count > size_prev) {
- fclose(f);
- if (w_secs)
- sleep(w_secs);
- tries++;
- goto again;
- }
+ if (!o_stdout && a_flag && count > size_prev)
+ goto retry;
+ /* nothing can resume a partial temporary file */
+ if (tmppath != NULL)
+ goto failure;
goto failure_keep;
}
@@ -849,7 +907,7 @@
}
goto done;
failure:
- if (of && of != stdout && !R_flag && !r_flag)
+ if (of && of != stdout && !R_flag && (!r_flag || tmppath != NULL))
if (stat(path, &sb) != -1 && (sb.st_mode & S_IFREG))
unlink(tmppath ? tmppath : path);
if (R_flag && tmppath != NULL && sb.st_size == -1)
@@ -857,6 +915,14 @@
failure_keep:
r = -1;
goto done;
+ retry:
+ /* resume from what we have written, as long as we get further */
+ fclose(f);
+ f = NULL;
+ url->offset = size_prev = count;
+ if (w_secs)
+ sleep(w_secs);
+ goto again;
done:
if (f)
fclose(f);
@@ -864,6 +930,7 @@
fclose(of);
if (url)
fetchFreeURL(url);
+ free(etag);
if (tmppath != NULL)
free(tmppath);
return (r);
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sun, Nov 29, 5:48 PM (16 h, 39 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
42093728
Default Alt Text
D61993.1795974526.diff (6 KB)
Attached To
Mode
D61993: fetch: Resume -a retries from the bytes written
Attached
Detach File
Event Timeline