added -r option to remove extra files.
[unix-history] / usr / src / usr.bin / rdist / docmd.c
index 248ce8e..f9c4b73 100644 (file)
@@ -1,5 +1,5 @@
 #ifndef lint
 #ifndef lint
-static char *sccsid = "@(#)docmd.c     4.1 (Berkeley) 83/09/07";
+static char *sccsid = "@(#)docmd.c     4.5 (Berkeley) 83/10/20";
 #endif
 
 #include "defs.h"
 #endif
 
 #include "defs.h"
@@ -7,62 +7,71 @@ static        char *sccsid = "@(#)docmd.c     4.1 (Berkeley) 83/09/07";
 FILE   *lfp;           /* log file for recording files updated */
 
 /*
 FILE   *lfp;           /* log file for recording files updated */
 
 /*
- * Routines to process commands.
+ * Process commands for sending files to other machines.
  */
  */
-docmd(files, hosts, cmds)
+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;
-       int n;
+       register char **cpp;
+       int n, ddir;
 
        if (debug)
 
        if (debug)
-               printf("docmd()\n");
+               printf("dohcmds(%x, %x, %x)\n", files, hosts, cmds);
 
 
-       files = expand(files);
-       hosts = expand(hosts);
+       files = expand(files, 0);
+       hosts = expand(hosts, 1);
        if (files == NULL)
                fatal("no files to be updated\n");
        if (hosts == NULL)
                fatal("empty list of hosts to be updated\n");
        except = cmds;
        if (files == NULL)
                fatal("no files to be updated\n");
        if (hosts == NULL)
                fatal("empty list of hosts to be updated\n");
        except = cmds;
+       ddir = files->b_next != NULL;
 
        for (h = hosts; h != NULL; h = h->b_next) {
 
        for (h = hosts; h != NULL; h = h->b_next) {
+               if (!qflag)
+                       printf("updating host %s\n", h->b_name);
                if (!nflag) {
                if (!nflag) {
+                       if (!makeconn(h->b_name))
+                               continue;
                        if ((lfp = fopen(tmpfile, "w")) == NULL) {
                                fatal("cannot open %s\n", tmpfile);
                                exit(1);
                        }
                        if ((lfp = fopen(tmpfile, "w")) == NULL) {
                                fatal("cannot open %s\n", tmpfile);
                                exit(1);
                        }
-                       if (!makeconn(h->b_name))
-                               continue;
                }
                for (f = files; f != NULL; f = f->b_next) {
                        if (filec) {
                                for (cpp = filev; *cpp; cpp++)
                                        if (!strcmp(f->b_name, *cpp))
                                                goto found;
                }
                for (f = files; f != NULL; f = f->b_next) {
                        if (filec) {
                                for (cpp = filev; *cpp; cpp++)
                                        if (!strcmp(f->b_name, *cpp))
                                                goto found;
+                               if (!nflag) {
+                                       (void) fclose(lfp);
+                               }
                                continue;
                        }
                found:
                        n = 0;
                                continue;
                        }
                found:
                        n = 0;
-                       for (c = cmds; c != NULL; c = c->b_next)
-                               if (c->b_type == INSTALL) {
-                                       install(f->b_name, c->b_name, 0);
-                                       n++;
-                               } else if (c->b_type == VERIFY) {
-                                       install(f->b_name, c->b_name, 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);
+                               install(f->b_name, f->b_name, 0, options);
                }
                if (!nflag) {
                }
                if (!nflag) {
-                       (void) fclose(lfp);
+                       /* signal end of connection */
+                       (void) write(rem, "\2\n", 2);
                        (void) close(rem);
                        (void) close(rem);
+                       (void) fclose(lfp);
                }
                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(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);
@@ -77,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) {
@@ -95,6 +103,7 @@ makeconn(rhost)
                printf("buf = %s\n", buf);
        }
 
                printf("buf = %s\n", buf);
        }
 
+       fflush(stdout);
        rem = rcmd(&rhost, IPPORT_CMDSERVER, user, ruser, buf, 0);
        if (rem < 0)
                return(0);
        rem = rcmd(&rhost, IPPORT_CMDSERVER, user, ruser, buf, 0);
        if (rem < 0)
                return(0);
@@ -105,72 +114,282 @@ makeconn(rhost)
 
 /*
  * Update the file(s) if they are different.
 
 /*
  * 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, verify)
+install(src, dest, destdir, opts)
        char *src, *dest;
        char *src, *dest;
-       int verify;
+       int destdir, opts;
 {
        register char *cp;
 {
        register char *cp;
-       extern char *tp;
-       char lbuf[BUFSIZ];
 
 
-       if (!qflag)
-               printf("%s %s %s\n", verify ? "verify" : "install", src, dest);
-       if (nflag)
+       if (exclude(src))
                return;
                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.
         */
-       (void) sprintf(buf, "T%s\n", dest);
+       (void) sprintf(buf, "%c%s\n", destdir ? 'T' : 't', dest);
        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;
-       shexpand(lbuf, src);
-       sendf(lbuf, verify);
+
+       if (!destdir && (opts & WHOLE))
+               opts |= STRIP;
+       if (opts & REMOVE) {
+               opts &= ~REMOVE;
+               rmchk(src, NULL, opts);
+       }
+       sendf(src, NULL, opts);
+}
+
+struct tstamp {
+       time_t  lastmod;
+       FILE    *tfp;
+} ts[NSTAMPS];
+
+int    nstamps;
+
+extern char target[], *tp;
+
+/*
+ * Process commands for comparing files to time stamp files.
+ */
+dofcmds(files, stamps, cmds)
+       struct block *files, *stamps, *cmds;
+{
+       register struct block *b;
+       register struct tstamp *t;
+       register char **cpp;
+       struct timeval tv[2];
+       struct timezone tz;
+       struct stat stb;
+       extern char *tmpinc;
+
+       if (debug)
+               printf("dofcmds()\n");
+
+       files = expand(files, 0);
+       stamps = expand(stamps, 0);
+       if (files == NULL)
+               fatal("no files to be updated\n");
+       if (stamps == NULL)
+               fatal("empty time stamp file list\n");
+       except = cmds;
+
+       t = ts;
+       nstamps = 0;
+       for (b = stamps; b != NULL; b = b->b_next) {
+               if (stat(b->b_name, &stb) < 0) {
+                       error("%s: %s\n", b->b_name, sys_errlist[errno]);
+                       continue;
+               }
+               if (++nstamps > NSTAMPS)
+                       fatal("too many time stamp files in one command\n");
+               if (debug)
+                       printf("%s: %d\n", b->b_name, stb.st_mtime);
+               t->lastmod = stb.st_mtime;
+               (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)++;
+               } else
+                       t->tfp = NULL;
+               t++;
+       }
+
+       for (b = files; b != NULL; b = b->b_next) {
+               if (filec) {
+                       for (cpp = filev; *cpp; cpp++)
+                               if (!strcmp(b->b_name, *cpp))
+                                       goto found;
+                       continue;
+               }
+       found:
+               tp = NULL;
+               cmptime(b->b_name);
+       }
+
+       *tmpinc = 'A';
+       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)
+                               notify(tmpfile, NULL, b->b_args, t->lastmod);
+               if (!nflag && !(options & VERIFY))
+                       (void) unlink(tmpfile);
+               (*tmpinc)++;
+       }
+}
+
+/*
+ * Compare the mtime of file to the list of time stamps.
+ */
+cmptime(name)
+       char *name;
+{
+       register struct tstamp *t;
+       struct stat stb;
+
+       if (debug)
+               printf("cmptime(%s)\n", name);
+
+       if (exclude(name))
+               return;
+
+       /*
+        * first time cmptime() is called?
+        */
+       if (tp == NULL) {
+               exptilde(target, name);
+               tp = name = target;
+               while (*tp)
+                       tp++;
+       }
+       if (access(name, 4) < 0 || stat(name, &stb) < 0) {
+               error("%s: %s\n", name, sys_errlist[errno]);
+               return;
+       }
+
+       switch (stb.st_mode & S_IFMT) {
+       case S_IFREG:
+               break;
+
+       case S_IFDIR:
+               rcmptime(&stb);
+               return;
+
+       default:
+               error("%s: not a plain file\n", name);
+               return;
+       }
+
+       for (t = ts; t < &ts[nstamps]; t++) {
+               if (stb.st_mtime > t->lastmod)
+                       log(t->tfp, "new: %s\n", name);
+       }
+}
+
+rcmptime(st)
+       struct stat *st;
+{
+       register DIR *d;
+       register struct direct *dp;
+       register char *cp;
+       char *otp;
+       int len;
+
+       if (debug)
+               printf("rcmptime(%x)\n", st);
+
+       if ((d = opendir(target)) == NULL) {
+               error("%s: %s\n", target, sys_errlist[errno]);
+               return;
+       }
+       otp = tp;
+       len = tp - target;
+       while (dp = readdir(d)) {
+               if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
+                       continue;
+               if (len + 1 + strlen(dp->d_name) >= BUFSIZ - 1) {
+                       error("%s/%s: Name too long\n", target, dp->d_name);
+                       continue;
+               }
+               tp = otp;
+               *tp++ = '/';
+               cp = dp->d_name;
+               while (*tp++ = *cp++)
+                       ;
+               tp--;
+               cmptime(target);
+       }
+       closedir(d);
+       tp = otp;
+       *tp = '\0';
 }
 
 /*
  * 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(host, to)
-       char *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) {
                return;
        if (!qflag) {
-               printf("notify @%s ", host);
+               printf("notify ");
+               if (rhost)
+                       printf("@%s ", rhost);
                prnames(to);
        }
        if (nflag)
                return;
 
                prnames(to);
        }
        if (nflag)
                return;
 
-       if ((fd = open(tmpfile, 0)) < 0) {
-               error("%s: %s\n", tmpfile, sys_errlist[errno]);
+       if ((fd = open(file, 0)) < 0) {
+               error("%s: %s\n", file, sys_errlist[errno]);
+               return;
+       }
+       if (fstat(fd, &stb) < 0) {
+               error("%s: %s\n", file, sys_errlist[errno]);
+               (void) close(fd);
+               return;
+       }
+       if (stb.st_size == 0) {
+               (void) close(fd);
                return;
        }
        /*
         * Create a pipe to mailling program.
         */
        pf = popen(MAILCMD, "w");
                return;
        }
        /*
         * 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) {
-               fprintf(pf, " %s@%s", to->b_name, host);
+               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;
        }
        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)