tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

ftp(1) check for file existence only (PATCH included)



This has not tested with other options but this does what I need.

From my changed man page:


     -h          Check for file and do not download.  For FTP it does an ls.
                 For HTTP it does a HEAD request.

Maybe renamed to -c?


I thought I needed this so I can check for existence of pkg_summary 
file(s).

Now I realize I didn't need this, I want to download pkg_summary so I was 
going to test for existence for different pkgsrc branches (directories) 
but now I realize I could have just downloaded and if that errored then 
try an older branch location ... anyways I still have the following 
patch. What do you think?

curl uses -I switch to do this.



Index: usr.bin/ftp/fetch.c
===================================================================
RCS file: /cvsroot/src/usr.bin/ftp/fetch.c,v
retrieving revision 1.183
diff -u -r1.183 fetch.c
--- usr.bin/ftp/fetch.c 5 Dec 2007 03:46:33 -0000       1.183
+++ usr.bin/ftp/fetch.c 14 Mar 2008 01:40:03 -0000
@@ -753,7 +753,8 @@
                 * Construct and send the request.
                 */
                if (verbose)
-                       fprintf(ttyout, "Requesting %s\n", url);
+                       fprintf(ttyout, "%s %s\n",
+                               checkonly ? "Checking" : "Requesting", url);
                leading = "  (";
                hasleading = 0;
                if (isproxy) {
@@ -763,11 +764,13 @@
                                leading = ", ";
                                hasleading++;
                        }
-                       fprintf(fin, "GET %s HTTP/1.0\r\n", path);
+                       fprintf(fin, "%s %s HTTP/1.0\r\n",
+                               checkonly ? "HEAD" : "GET", path);
                        if (flushcache)
                                fprintf(fin, "Pragma: no-cache\r\n");
                } else {
-                       fprintf(fin, "GET %s HTTP/1.1\r\n", path);
+                       fprintf(fin, "%s %s HTTP/1.1\r\n",
+                               checkonly ? "HEAD" : "GET", path);
                        if (strchr(host, ':')) {
                                char *h, *p;
 
@@ -1083,14 +1086,21 @@
 #endif
                default:
                        if (message)
-                               warnx("Error retrieving file `%s'", message);
+                               warnx("Error %s file `%s'",
+                                       checkonly ? "checking" : "retrieving",
+                                       message);
                        else
-                               warnx("Unknown error retrieving file");
+                               warnx("Unknown error %s file",
+                                       checkonly ? "checking" : "retrieving");
                        goto cleanup_fetch_url;
                }
        }               /* end of ftp:// or http:// specific setup */
 
-                       /* Open the output file. */
+       if (checkonly)  {
+               rval=0;
+               goto cleanup_fetch_url;
+       }
+       /* Open the output file. */
        if (strcmp(savefile, "-") == 0) {
                fout = stdout;
        } else if (*savefile == '|') {
@@ -1103,7 +1113,7 @@
                closefunc = pclose;
        } else {
                if ((rangeend != -1 && rangeend <= restart_point) ||
-                   (rangestart == -1 && filesize != -1 && filesize <= 
restart_point)) {
+               (rangestart == -1 && filesize != -1 && filesize <= 
restart_point)) {
                        /* already done */
                        if (verbose)
                                fprintf(ttyout, "already done\n");
@@ -1629,6 +1639,9 @@
        xargv[0] = "get";
        xargv[1] = file;
        xargv[2] = NULL;
+       if (checkonly) {
+               ls(xargc, xargv);
+       } else
        if (dirhasglob || filehasglob) {
                int ointeractive;
 
Index: usr.bin/ftp/ftp.1
===================================================================
RCS file: /cvsroot/src/usr.bin/ftp/ftp.1,v
retrieving revision 1.123
diff -u -r1.123 ftp.1
--- usr.bin/ftp/ftp.1   8 Jan 2008 15:28:31 -0000       1.123
+++ usr.bin/ftp/ftp.1   14 Mar 2008 01:40:04 -0000
@@ -217,6 +217,17 @@
 proxies.
 .It Fl g
 Disables file name globbing.
+.It Fl h
+Check for file and do not download.
+For
+.Tn FTP
+it does an
+.Ic ls .
+For
+.Tn HTTP
+it does a
+.Ic HEAD
+request.
 .It Fl i
 Turns off interactive prompting during
 multiple file transfers.
Index: usr.bin/ftp/ftp_var.h
===================================================================
RCS file: /cvsroot/src/usr.bin/ftp/ftp_var.h,v
retrieving revision 1.77
diff -u -r1.77 ftp_var.h
--- usr.bin/ftp/ftp_var.h       5 Dec 2007 03:46:34 -0000       1.77
+++ usr.bin/ftp/ftp_var.h       14 Mar 2008 01:40:04 -0000
@@ -213,6 +213,7 @@
 GLOBAL int     ftp_debug;      /* debugging level */
 GLOBAL int     bell;           /* ring bell on cmd completion */
 GLOBAL int     doglob;         /* glob local file names */
+GLOBAL int     checkonly;      /* ls for ftp and HEAD for http */
 GLOBAL int     autologin;      /* establish user account on connection */
 GLOBAL int     proxy;          /* proxy server connection active */
 GLOBAL int     proxflag;       /* proxy connection exists */
Index: usr.bin/ftp/main.c
===================================================================
RCS file: /cvsroot/src/usr.bin/ftp/main.c,v
retrieving revision 1.106
diff -u -r1.106 main.c
--- usr.bin/ftp/main.c  2 Dec 2007 19:41:53 -0000       1.106
+++ usr.bin/ftp/main.c  14 Mar 2008 01:40:04 -0000
@@ -163,6 +163,7 @@
        else
                gateport = "ftpgate";
        doglob = 1;
+       checkonly = 0;
        interactive = 1;
        autologin = 1;
        passivemode = 1;
@@ -290,7 +291,7 @@
                }
        }
 
-       while ((ch = getopt(argc, argv, "46AadefginN:o:pP:q:r:Rs:tT:u:vV")) != 
-1) {
+       while ((ch = getopt(argc, argv, "46AadefghinN:o:pP:q:r:Rs:tT:u:vV")) != 
-1) {
                switch (ch) {
                case '4':
                        family = AF_INET;
@@ -332,6 +333,10 @@
                        doglob = 0;
                        break;
 
+               case 'h':
+                       checkonly = 1;  /* ls for ftp and HEAD for http */
+                       break;
+
                case 'i':
                        interactive = 0;
                        break;

  Jeremy C. Reed


Home | Main Index | Thread Index | Old Index