fetch-1.2:

Add -i option for conditional fetch, e.g. only fetch the file again, if
the remote version is newer.
This commit is contained in:
joerg 2009-02-05 17:14:06 +00:00
parent 5af00eebb3
commit f89a759330
4 changed files with 39 additions and 7 deletions

View file

@ -1,7 +1,7 @@
# $NetBSD: Makefile,v 1.2 2009/02/04 21:29:40 joerg Exp $ # $NetBSD: Makefile,v 1.3 2009/02/05 17:14:06 joerg Exp $
# #
DISTNAME= fetch-1.1 DISTNAME= fetch-1.2
CATEGORIES= net CATEGORIES= net
MASTER_SITES= # empty MASTER_SITES= # empty
DISTFILES= # empty DISTFILES= # empty

View file

@ -37,7 +37,7 @@
.Nd retrieve a file by Uniform Resource Locator .Nd retrieve a file by Uniform Resource Locator
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm .Nm
.Op Fl 146AFMRUadlmnqrsv .Op Fl 146AFMRUadilmnqrsv
.Op Fl B Ar bytes .Op Fl B Ar bytes
.Op Fl S Ar bytes .Op Fl S Ar bytes
.Op Fl T Ar seconds .Op Fl T Ar seconds
@ -110,6 +110,10 @@ The file to retrieve is located on the host
.Ar host . .Ar host .
This option is deprecated and is provided for backward compatibility This option is deprecated and is provided for backward compatibility
only. only.
.It Fl i
Only fetch if it the output file is older than the referenced URL.
This option is overriden by
.Fl o Ar - .
.It Fl l .It Fl l
If the target is a file-scheme URL, make a symbolic link to the target If the target is a file-scheme URL, make a symbolic link to the target
rather than trying to copy it. rather than trying to copy it.

View file

@ -90,6 +90,7 @@ int d_flag; /* -d: direct connection */
int F_flag; /* -F: restart without checking mtime */ int F_flag; /* -F: restart without checking mtime */
char *f_filename; /* -f: file to fetch */ char *f_filename; /* -f: file to fetch */
char *h_hostname; /* -h: host to fetch from */ char *h_hostname; /* -h: host to fetch from */
int i_flag; /* -i: fetch file if modified */
int l_flag; /* -l: link rather than copy file: URLs */ int l_flag; /* -l: link rather than copy file: URLs */
int m_flag; /* -[Mm]: mirror mode */ int m_flag; /* -[Mm]: mirror mode */
char *N_filename; /* -N: netrc file name */ char *N_filename; /* -N: netrc file name */
@ -393,6 +394,17 @@ fetch(char *URL, const char *path)
break; break;
} }
/* Protocol independent flags */
if (i_flag) {
if (stat(path, &sb) == 0) {
url->last_modified = sb.st_mtime;
strcat(flags, "i");
} else if (errno != ENOENT) {
warn("%s: stat()", path);
goto failure;
}
}
/* FTP specific flags */ /* FTP specific flags */
if (strcmp(url->scheme, SCHEME_FTP) == 0) { if (strcmp(url->scheme, SCHEME_FTP) == 0) {
if (d_flag) if (d_flag)
@ -474,6 +486,12 @@ fetch(char *URL, const char *path)
alarm(0); alarm(0);
if (sigalrm || sigint) if (sigalrm || sigint)
goto signal; goto signal;
if (f == NULL && i_flag && fetchLastErrCode == FETCH_UNCHANGED) {
/* URL was not modified, return OK. */
printf("%s: not modified\n", URL);
r = 0;
goto done;
}
if (f == NULL) { if (f == NULL) {
warnx("%s: %s", URL, fetchLastErrString); warnx("%s: %s", URL, fetchLastErrString);
goto failure; goto failure;
@ -747,7 +765,7 @@ static void
usage(void) usage(void)
{ {
fprintf(stderr, "%s\n%s\n%s\n", fprintf(stderr, "%s\n%s\n%s\n",
"usage: fetch [-146AFMPRUadlmnpqrsv] [-N netrc] [-o outputfile]", "usage: fetch [-146AFMPRUadilmnpqrsv] [-N netrc] [-o outputfile]",
" [-S bytes] [-B bytes] [-T seconds] [-w seconds]", " [-S bytes] [-B bytes] [-T seconds] [-w seconds]",
" [-h host -f file [-c dir] | URL ...]"); " [-h host -f file [-c dir] | URL ...]");
} }
@ -766,7 +784,7 @@ main(int argc, char *argv[])
int c, e, r; int c, e, r;
while ((c = getopt(argc, argv, while ((c = getopt(argc, argv,
"146AaB:bc:dFf:Hh:lMmN:no:qRrS:sT:tUvw:")) != -1) "146AaB:bc:dFf:Hh:ilMmN:no:qRrS:sT:tUvw:")) != -1)
switch (c) { switch (c) {
case '1': case '1':
once_flag = 1; once_flag = 1;
@ -811,6 +829,9 @@ main(int argc, char *argv[])
case 'h': case 'h':
h_hostname = optarg; h_hostname = optarg;
break; break;
case 'i':
i_flag = 1;
break;
case 'l': case 'l':
l_flag = 1; l_flag = 1;
break; break;
@ -934,6 +955,10 @@ main(int argc, char *argv[])
if (o_flag) { if (o_flag) {
if (strcmp(o_filename, "-") == 0) { if (strcmp(o_filename, "-") == 0) {
o_stdout = 1; o_stdout = 1;
if (i_flag) {
warnx("-i and -o - are incompatible, dropping -i");
i_flag = 0;
}
} else if (stat(o_filename, &sb) == -1) { } else if (stat(o_filename, &sb) == -1) {
if (errno == ENOENT) { if (errno == ENOENT) {
if (argc > 1) if (argc > 1)

View file

@ -4,7 +4,7 @@ NNAAMMEE
ffeettcchh -- retrieve a file by Uniform Resource Locator ffeettcchh -- retrieve a file by Uniform Resource Locator
SSYYNNOOPPSSIISS SSYYNNOOPPSSIISS
ffeettcchh [--114466AAFFMMRRUUaaddllmmnnqqrrssvv] [--BB _b_y_t_e_s] [--SS _b_y_t_e_s] [--TT _s_e_c_o_n_d_s] [--NN _f_i_l_e] ffeettcchh [--114466AAFFMMRRUUaaddiillmmnnqqrrssvv] [--BB _b_y_t_e_s] [--SS _b_y_t_e_s] [--TT _s_e_c_o_n_d_s] [--NN _f_i_l_e]
[--oo _f_i_l_e] [--ww _s_e_c_o_n_d_s] [--hh _h_o_s_t] [--cc _d_i_r] [--ff _f_i_l_e] [_U_R_L _._._.] [--oo _f_i_l_e] [--ww _s_e_c_o_n_d_s] [--hh _h_o_s_t] [--cc _d_i_r] [--ff _f_i_l_e] [_U_R_L _._._.]
DDEESSCCRRIIPPTTIIOONN DDEESSCCRRIIPPTTIIOONN
@ -50,6 +50,9 @@ DDEESSCCRRIIPPTTIIOONN
option is deprecated and is provided for backward compatibil- option is deprecated and is provided for backward compatibil-
ity only. ity only.
--ii Only fetch if it the output file is older than the referenced
URL. This option is overriden by --oo _-.
--ll If the target is a file-scheme URL, make a symbolic link to --ll If the target is a file-scheme URL, make a symbolic link to
the target rather than trying to copy it. the target rather than trying to copy it.
@ -150,4 +153,4 @@ NNOOTTEESS
One cannot both use the --hh, --cc and --ff options and specify URLs on the One cannot both use the --hh, --cc and --ff options and specify URLs on the
command line. command line.
NetBSD 4.0 March 11, 2003 NetBSD 4.0 NetBSD 5.0 March 11, 2003 NetBSD 5.0