First stable version.
[unix-history] / usr / src / usr.bin / rdist / docmd.c
index 248ce8e..d4c0615 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.2 (Berkeley) 83/09/27";
 #endif
 
 #include "defs.h"
 #endif
 
 #include "defs.h"
@@ -7,62 +7,70 @@ 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;
        register char *cp, **cpp;
        struct block *files, *hosts, *cmds;
 {
        register struct block *h, *f, *c;
        register char *cp, **cpp;
-       int n;
+       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;
                        for (c = cmds; c != NULL; c = c->b_next)
                                if (c->b_type == INSTALL) {
                                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);
+                                       install(f->b_name, c->b_name, ddir, 0);
                                        n++;
                                } else if (c->b_type == VERIFY) {
                                        n++;
                                } else if (c->b_type == VERIFY) {
-                                       install(f->b_name, c->b_name, 1);
+                                       install(f->b_name, c->b_name, ddir, 1);
                                        n++;
                                }
                        if (n == 0)
                                        n++;
                                }
                        if (n == 0)
-                               install(f->b_name, f->b_name, 0);
+                               install(f->b_name, f->b_name, 0, 0);
                }
                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);
        }
        if (!nflag)
                (void) unlink(tmpfile);
        }
        if (!nflag)
                (void) unlink(tmpfile);
@@ -79,7 +87,7 @@ makeconn(rhost)
 
        (void) sprintf(buf, "/usr/local/rdist -Server%s%s%s%s%s",
                vflag ? " -v" : "", qflag ? " -q" : "", nflag ? " -n" : "",
 
        (void) sprintf(buf, "/usr/local/rdist -Server%s%s%s%s%s",
                vflag ? " -v" : "", qflag ? " -q" : "", nflag ? " -n" : "",
-               yflag ? " -y" : "", debug ? " -d" : "");
+               yflag ? " -y" : "", debug ? " -D" : "");
 
        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);
@@ -103,38 +112,204 @@ makeconn(rhost)
        return(1);
 }
 
        return(1);
 }
 
+extern char target[], *tp;
+
 /*
  * 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, verify)
        char *src, *dest;
        char *src, *dest;
-       int verify;
+       int destdir, verify;
 {
 {
-       register char *cp;
-       extern char *tp;
-       char lbuf[BUFSIZ];
+       if (exclude(src))
+               return;
 
 
-       if (!qflag)
+       if (nflag) {
                printf("%s %s %s\n", verify ? "verify" : "install", src, dest);
                printf("%s %s %s\n", verify ? "verify" : "install", src, dest);
-       if (nflag)
                return;
                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));
        tp = NULL;
        if (debug)
                printf("buf = %s", buf);
        (void) write(rem, buf, strlen(buf));
        tp = NULL;
-       shexpand(lbuf, src);
-       sendf(lbuf, verify);
+       sendf(src, verify);
+}
+
+struct tstamp {
+       time_t  lastmod;
+       FILE    *tfp;
+} ts[NSTAMPS];
+
+int    nstamps;
+
+/*
+ * 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 stat stb;
+       extern char *tmpinc;
+       int n;
+
+       if (debug)
+               printf("dofcmds()\n");
+
+       files = expand(files, 0);
+       stamps = expand(stamps, 1);
+       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;
+               if (!nflag && !vflag) {
+                       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);
+       }
+       if (!nflag && !vflag)
+               for (t = ts; t < &ts[n]; t++)
+                       if (t->tfp != NULL)
+                               (void) fclose(t->tfp);
+       *tmpinc = 'A';
+       while (n--) {
+               for (b = cmds; b != NULL; b = b->b_next)
+                       if (b->b_type == NOTIFY)
+                               notify(tmpfile, NULL, b->b_args);
+               if (!nflag && !vflag)
+                       (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)
+                       return;
+               log(t->tfp, "updating: %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.
  */
-notify(host, to)
-       char *host;
+notify(file, host, to)
+       char *file, *host;
        register struct block *to;
 {
        register int fd, len;
        register struct block *to;
 {
        register int fd, len;
@@ -144,14 +319,25 @@ notify(host, to)
        if (vflag)
                return;
        if (!qflag) {
        if (vflag)
                return;
        if (!qflag) {
-               printf("notify @%s ", host);
+               printf("notify ");
+               if (host)
+                       printf("@%s ", host);
                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;
        }
        /*
                return;
        }
        /*
@@ -166,7 +352,10 @@ notify(host, to)
        fprintf(pf, "From: rdist (Remote distribution program)\n");
        fprintf(pf, "To:");
        while (to != NULL) {
        fprintf(pf, "From: rdist (Remote distribution program)\n");
        fprintf(pf, "To:");
        while (to != NULL) {
-               fprintf(pf, " %s@%s", to->b_name, host);
+               if (!any('@', to->b_name))
+                       fprintf(pf, " %s@%s", to->b_name, host);
+               else
+                       fprintf(pf, " %s", to->b_name);
                to = to->b_next;
        }
        putc('\n', pf);
                to = to->b_next;
        }
        putc('\n', pf);