added -r option to remove extra files.
[unix-history] / usr / src / usr.bin / rdist / docmd.c
index d4c0615..f9c4b73 100644 (file)
@@ -1,5 +1,5 @@
 #ifndef lint
 #ifndef lint
-static char *sccsid = "@(#)docmd.c     4.2 (Berkeley) 83/09/27";
+static char *sccsid = "@(#)docmd.c     4.5 (Berkeley) 83/10/20";
 #endif
 
 #include "defs.h"
 #endif
 
 #include "defs.h"
@@ -13,7 +13,7 @@ dohcmds(files, hosts, cmds)
        struct block *files, *hosts, *cmds;
 {
        register struct block *h, *f, *c;
        struct block *files, *hosts, *cmds;
 {
        register struct block *h, *f, *c;
-       register char *cp, **cpp;
+       register char **cpp;
        int n, ddir;
 
        if (debug)
        int n, ddir;
 
        if (debug)
@@ -51,16 +51,17 @@ dohcmds(files, hosts, cmds)
                        }
                found:
                        n = 0;
                        }
                found:
                        n = 0;
-                       for (c = cmds; c != NULL; c = c->b_next)
-                               if (c->b_type == INSTALL) {
-                                       install(f->b_name, c->b_name, ddir, 0);
-                                       n++;
-                               } else if (c->b_type == VERIFY) {
-                                       install(f->b_name, c->b_name, ddir, 1);
-                                       n++;
-                               }
+                       for (c = cmds; c != NULL; c = c->b_next) {
+                               if (c->b_type != INSTALL)
+                                       continue;
+                               n++;
+                               if (c->b_name == NULL)
+                                       install(f->b_name, f->b_name, 0, c->b_options);
+                               else
+                                       install(f->b_name, c->b_name, ddir, c->b_options);
+                       }
                        if (n == 0)
                        if (n == 0)
-                               install(f->b_name, f->b_name, 0, 0);
+                               install(f->b_name, f->b_name, 0, options);
                }
                if (!nflag) {
                        /* signal end of connection */
                }
                if (!nflag) {
                        /* signal end of connection */
@@ -70,7 +71,7 @@ dohcmds(files, hosts, cmds)
                }
                for (c = cmds; c != NULL; c = c->b_next)
                        if (c->b_type == NOTIFY)
                }
                for (c = cmds; c != NULL; c = c->b_next)
                        if (c->b_type == NOTIFY)
-                               notify(tmpfile, h->b_name, c->b_args);
+                               notify(tmpfile, h->b_name, c->b_args, 0);
        }
        if (!nflag)
                (void) unlink(tmpfile);
        }
        if (!nflag)
                (void) unlink(tmpfile);
@@ -85,9 +86,8 @@ makeconn(rhost)
        register char *ruser;
        extern char user[];
 
        register char *ruser;
        extern char user[];
 
-       (void) sprintf(buf, "/usr/local/rdist -Server%s%s%s%s%s",
-               vflag ? " -v" : "", qflag ? " -q" : "", nflag ? " -n" : "",
-               yflag ? " -y" : "", debug ? " -D" : "");
+       (void) sprintf(buf, "/usr/local/rdist -Server%s%s",
+               nflag ? " -n" : "", qflag ? " -q" : "");
 
        ruser = rindex(rhost, '.');
        if (ruser != NULL) {
 
        ruser = rindex(rhost, '.');
        if (ruser != NULL) {
@@ -112,23 +112,27 @@ makeconn(rhost)
        return(1);
 }
 
        return(1);
 }
 
-extern char target[], *tp;
-
 /*
  * Update the file(s) if they are different.
  * destdir = 1 if destination should be a directory
  * (i.e., more than one source is being copied to the same destination).
  */
 /*
  * Update the file(s) if they are different.
  * destdir = 1 if destination should be a directory
  * (i.e., more than one source is being copied to the same destination).
  */
-install(src, dest, destdir, verify)
+install(src, dest, destdir, opts)
        char *src, *dest;
        char *src, *dest;
-       int destdir, verify;
+       int destdir, opts;
 {
 {
+       register char *cp;
+
        if (exclude(src))
                return;
 
        if (exclude(src))
                return;
 
-       if (nflag) {
-               printf("%s %s %s\n", verify ? "verify" : "install", src, dest);
-               return;
+       if (nflag || debug) {
+               printf("%s%s%s%s %s %s\n", opts & VERIFY ? "verify":"install",
+                       opts & WHOLE ? " -w" : "",
+                       opts & YOUNGER ? " -y" : "",
+                       opts & REMOVE ? " -r" : "", src, dest);
+               if (nflag)
+                       return;
        }
        /*
         * Pass the destination file/directory name to remote.
        }
        /*
         * Pass the destination file/directory name to remote.
@@ -137,8 +141,14 @@ install(src, dest, destdir, verify)
        if (debug)
                printf("buf = %s", buf);
        (void) write(rem, buf, strlen(buf));
        if (debug)
                printf("buf = %s", buf);
        (void) write(rem, buf, strlen(buf));
-       tp = NULL;
-       sendf(src, verify);
+
+       if (!destdir && (opts & WHOLE))
+               opts |= STRIP;
+       if (opts & REMOVE) {
+               opts &= ~REMOVE;
+               rmchk(src, NULL, opts);
+       }
+       sendf(src, NULL, opts);
 }
 
 struct tstamp {
 }
 
 struct tstamp {
@@ -148,6 +158,8 @@ struct tstamp {
 
 int    nstamps;
 
 
 int    nstamps;
 
+extern char target[], *tp;
+
 /*
  * Process commands for comparing files to time stamp files.
  */
 /*
  * Process commands for comparing files to time stamp files.
  */
@@ -157,15 +169,16 @@ dofcmds(files, stamps, cmds)
        register struct block *b;
        register struct tstamp *t;
        register char **cpp;
        register struct block *b;
        register struct tstamp *t;
        register char **cpp;
+       struct timeval tv[2];
+       struct timezone tz;
        struct stat stb;
        extern char *tmpinc;
        struct stat stb;
        extern char *tmpinc;
-       int n;
 
        if (debug)
                printf("dofcmds()\n");
 
        files = expand(files, 0);
 
        if (debug)
                printf("dofcmds()\n");
 
        files = expand(files, 0);
-       stamps = expand(stamps, 1);
+       stamps = expand(stamps, 0);
        if (files == NULL)
                fatal("no files to be updated\n");
        if (stamps == NULL)
        if (files == NULL)
                fatal("no files to be updated\n");
        if (stamps == NULL)
@@ -184,7 +197,10 @@ dofcmds(files, stamps, cmds)
                if (debug)
                        printf("%s: %d\n", b->b_name, stb.st_mtime);
                t->lastmod = stb.st_mtime;
                if (debug)
                        printf("%s: %d\n", b->b_name, stb.st_mtime);
                t->lastmod = stb.st_mtime;
-               if (!nflag && !vflag) {
+               (void) gettimeofday(&tv[0], &tz);
+               tv[1] = tv[0];
+               (void) utimes(b->b_name, tv);
+               if (!nflag && !(options & VERIFY)) {
                        if ((t->tfp = fopen(tmpfile, "w")) == NULL)
                                error("%s: %s\n", b->b_name, sys_errlist[errno]);
                        (*tmpinc)++;
                        if ((t->tfp = fopen(tmpfile, "w")) == NULL)
                                error("%s: %s\n", b->b_name, sys_errlist[errno]);
                        (*tmpinc)++;
@@ -192,6 +208,7 @@ dofcmds(files, stamps, cmds)
                        t->tfp = NULL;
                t++;
        }
                        t->tfp = NULL;
                t++;
        }
+
        for (b = files; b != NULL; b = b->b_next) {
                if (filec) {
                        for (cpp = filev; *cpp; cpp++)
        for (b = files; b != NULL; b = b->b_next) {
                if (filec) {
                        for (cpp = filev; *cpp; cpp++)
@@ -203,16 +220,15 @@ dofcmds(files, stamps, cmds)
                tp = NULL;
                cmptime(b->b_name);
        }
                tp = NULL;
                cmptime(b->b_name);
        }
-       if (!nflag && !vflag)
-               for (t = ts; t < &ts[n]; t++)
-                       if (t->tfp != NULL)
-                               (void) fclose(t->tfp);
+
        *tmpinc = 'A';
        *tmpinc = 'A';
-       while (n--) {
+       for (t = ts; t < &ts[nstamps]; t++) {
+               if (t->tfp != NULL)
+                       (void) fclose(t->tfp);
                for (b = cmds; b != NULL; b = b->b_next)
                        if (b->b_type == NOTIFY)
                for (b = cmds; b != NULL; b = b->b_next)
                        if (b->b_type == NOTIFY)
-                               notify(tmpfile, NULL, b->b_args);
-               if (!nflag && !vflag)
+                               notify(tmpfile, NULL, b->b_args, t->lastmod);
+               if (!nflag && !(options & VERIFY))
                        (void) unlink(tmpfile);
                (*tmpinc)++;
        }
                        (void) unlink(tmpfile);
                (*tmpinc)++;
        }
@@ -261,9 +277,8 @@ cmptime(name)
        }
 
        for (t = ts; t < &ts[nstamps]; t++) {
        }
 
        for (t = ts; t < &ts[nstamps]; t++) {
-               if (stb.st_mtime <= t->lastmod)
-                       return;
-               log(t->tfp, "updating: %s\n", name);
+               if (stb.st_mtime > t->lastmod)
+                       log(t->tfp, "new: %s\n", name);
        }
 }
 
        }
 }
 
@@ -307,21 +322,24 @@ rcmptime(st)
 
 /*
  * Notify the list of people the changes that were made.
 
 /*
  * Notify the list of people the changes that were made.
+ * rhost == NULL if we are mailing a list of changes compared to at time
+ * stamp file.
  */
  */
-notify(file, host, to)
-       char *file, *host;
+notify(file, rhost, to, lmod)
+       char *file, *rhost;
        register struct block *to;
        register struct block *to;
+       time_t lmod;
 {
        register int fd, len;
        FILE *pf, *popen();
        struct stat stb;
 
 {
        register int fd, len;
        FILE *pf, *popen();
        struct stat stb;
 
-       if (vflag)
+       if (options & VERIFY)
                return;
        if (!qflag) {
                printf("notify ");
                return;
        if (!qflag) {
                printf("notify ");
-               if (host)
-                       printf("@%s ", host);
+               if (rhost)
+                       printf("@%s ", rhost);
                prnames(to);
        }
        if (nflag)
                prnames(to);
        }
        if (nflag)
@@ -344,22 +362,34 @@ notify(file, host, to)
         * Create a pipe to mailling program.
         */
        pf = popen(MAILCMD, "w");
         * Create a pipe to mailling program.
         */
        pf = popen(MAILCMD, "w");
-       if (pf == NULL)
-               fatal("notify: \"%s\" failed\n", MAILCMD);
+       if (pf == NULL) {
+               error("notify: \"%s\" failed\n", MAILCMD);
+               (void) close(fd);
+               return;
+       }
        /*
         * Output the proper header information.
         */
        fprintf(pf, "From: rdist (Remote distribution program)\n");
        fprintf(pf, "To:");
        /*
         * Output the proper header information.
         */
        fprintf(pf, "From: rdist (Remote distribution program)\n");
        fprintf(pf, "To:");
+       if (!any('@', to->b_name) && host != NULL)
+               fprintf(pf, " %s@%s", to->b_name, rhost);
+       else
+               fprintf(pf, " %s", to->b_name);
+       to = to->b_next;
        while (to != NULL) {
        while (to != NULL) {
-               if (!any('@', to->b_name))
-                       fprintf(pf, " %s@%s", to->b_name, host);
+               if (!any('@', to->b_name) && host != NULL)
+                       fprintf(pf, ", %s@%s", to->b_name, rhost);
                else
                else
-                       fprintf(pf, " %s", to->b_name);
+                       fprintf(pf, ", %s", to->b_name);
                to = to->b_next;
        }
        putc('\n', pf);
                to = to->b_next;
        }
        putc('\n', pf);
-       fprintf(pf, "Subject: files updated by rdist\n");
+       if (rhost != NULL)
+               fprintf(pf, "Subject: files updated by rdist from %s to %s\n",
+                       host, rhost);
+       else
+               fprintf(pf, "Subject: files updated after %s\n", ctime(&lmod));
        putc('\n', pf);
 
        while ((len = read(fd, buf, BUFSIZ)) > 0)
        putc('\n', pf);
 
        while ((len = read(fd, buf, BUFSIZ)) > 0)