- Moved file-deletion code into generator.c.
[rsync/rsync.git] / generator.c
index 1777ef2..0349b35 100644 (file)
 
 extern int verbose;
 extern int dry_run;
-extern int what_has_changed;
+extern int log_format_has_i;
+extern int log_format_has_o_or_i;
+extern int daemon_log_format_has_i;
+extern int am_root;
+extern int am_server;
+extern int am_daemon;
 extern int relative_paths;
 extern int keep_dirlinks;
 extern int preserve_links;
-extern int am_root;
 extern int preserve_devices;
 extern int preserve_hard_links;
 extern int preserve_perms;
@@ -38,6 +42,7 @@ extern int preserve_gid;
 extern int preserve_times;
 extern int omit_dir_times;
 extern int delete_during;
+extern int remove_sent_files;
 extern int update_only;
 extern int opt_ignore_existing;
 extern int inplace;
@@ -47,6 +52,7 @@ extern int ignore_times;
 extern int size_only;
 extern OFF_T max_size;
 extern int io_timeout;
+extern int ignore_timeout;
 extern int protocol_version;
 extern int fuzzy_basis;
 extern int always_checksum;
@@ -62,6 +68,7 @@ extern int only_existing;
 extern int orig_umask;
 extern int safe_symlinks;
 extern long block_size; /* "long" because popt can't set an int32. */
+extern struct stats stats;
 
 extern struct filter_list_struct server_filter_list;
 
@@ -81,52 +88,46 @@ static int unchanged_attrs(struct file_struct *file, STRUCT_STAT *st)
 }
 
 
-#define SC_CHECKSUM_CHANGED (1<<0)
-#define SC_SYMLINK_CHANGED (1<<1)
-#define SC_SENDING_FILE (1<<2)
-#define SC_NO_BASIS (1<<3)
-#define SC_NO_NL (1<<4)
+#define SID_UPDATING             ITEM_UPDATING
+#define SID_REPORT_CHECKSUM      ITEM_REPORT_CHECKSUM
+#define SID_USING_ALT_BASIS      ITEM_USING_ALT_BASIS
+/* This flag doesn't get sent, so it must be outside 0xffff. */
+#define SID_NO_DEST_AND_NO_UPDATE (1<<16)
 
-static void showchg(const char *fname, struct file_struct *file, int statret,
-                   STRUCT_STAT *st, int flags)
+static void itemize(struct file_struct *file, int statret, STRUCT_STAT *st,
+                   int32 sflags, int f_out, int ndx)
 {
-       static char ch[] = "*XcstpogDL";
-       int keep_time;
-       char *s;
-
-       ch[0] = flags & SC_SENDING_FILE ? '*' : ' ';
-       ch[1] = S_ISDIR(file->mode) ? 'd' : IS_DEVICE(file->mode) ? 'D'
-             : S_ISLNK(file->mode) ? 'L' : 'f';
-
-       if (statret < 0) {
-               for (s = ch + 2; *s; ) *s++ = '+';
-               goto print_it;
-       }
-
-       keep_time = !preserve_times ? 0
-               : S_ISDIR(file->mode) ? !omit_dir_times : !S_ISLNK(file->mode);
-
-       ch[2] = !(flags & SC_CHECKSUM_CHANGED) ? '-' : 'c';
-       ch[3] = !S_ISREG(file->mode) || file->length == st->st_size ? '-' : 's';
-       ch[4] = flags & SC_SENDING_FILE && !keep_time ? 'T'
-           : !keep_time || file->modtime == st->st_mtime ? '-' : 't';
-       ch[5] = !preserve_perms || file->mode == st->st_mode ? '-' : 'p';
-       ch[6] = !am_root || !preserve_uid || file->uid == st->st_uid ? '-' : 'o';
-       ch[7] = preserve_gid && file->gid != GID_NONE && st->st_gid != file->gid  ? 'g' : '-';
-       ch[8] = IS_DEVICE(file->mode) && file->u.rdev != st->st_rdev ? 'D' : '-';
-       ch[9] = flags & SC_SYMLINK_CHANGED ? 'L' : '-';
-
-       if (flags & SC_NO_BASIS)
-           ch[4] = ch[5] = ch[6] = ch[7] = '-';
-
-       s = ch + 2;
-       if (!(flags & SC_SENDING_FILE))
-               while (*s == '-') s++;
-       if (*s) {
-           print_it:
-               rprintf(FINFO, "%s %s%s%s", ch, fname,
-                       ch[1] == 'd' ? "/" : "",
-                       flags & SC_NO_NL ? "" : "\n");
+       int iflags = sflags & 0xffff;
+
+       if (statret >= 0) {
+               if (S_ISREG(file->mode) && file->length != st->st_size)
+                       iflags |= ITEM_REPORT_SIZE;
+               if (!(sflags & SID_NO_DEST_AND_NO_UPDATE)) {
+                       int keep_time = !preserve_times ? 0
+                           : S_ISDIR(file->mode) ? !omit_dir_times
+                           : !S_ISLNK(file->mode);
+
+                       if ((iflags & ITEM_UPDATING && !keep_time)
+                           || (keep_time && file->modtime != st->st_mtime))
+                               iflags |= ITEM_REPORT_TIME;
+                       if (preserve_perms && file->mode != st->st_mode)
+                               iflags |= ITEM_REPORT_PERMS;
+                       if (preserve_uid && am_root && file->uid != st->st_uid)
+                               iflags |= ITEM_REPORT_OWNER;
+                       if (preserve_gid && file->gid != GID_NONE
+                           && st->st_gid != file->gid)
+                               iflags |= ITEM_REPORT_GROUP;
+               }
+       } else
+               iflags |= ITEM_IS_NEW | ITEM_UPDATING;
+
+       if ((iflags || verbose > 1) && !read_batch) {
+               if (protocol_version >= 29) {
+                       if (ndx >= 0)
+                               write_int(f_out, ndx);
+                       write_shortint(f_out, iflags);
+               } else if (ndx >= 0)
+                       log_recv(file, &stats, iflags);
        }
 }
 
@@ -345,9 +346,11 @@ static int find_fuzzy(struct file_struct *file, struct file_list *dirlist)
  * modification-time repair. */
 static void recv_generator(char *fname, struct file_list *flist,
                           struct file_struct *file, int ndx,
+                          int itemizing, int maybe_PERMS_REPORT,
+                          enum logcode code,
                           int f_out, int f_out_name)
 {
-       static int missing_below = -1;
+       static int missing_below = -1, excluded_below = -1;
        static char *fuzzy_dirname = NULL;
        static struct file_list *fuzzy_dirlist = NULL;
        struct file_struct *fuzzy_file = NULL;
@@ -380,14 +383,24 @@ static void recv_generator(char *fname, struct file_list *flist,
                        safe_fname(fname), ndx);
        }
 
-       if (server_filter_list.head
-           && check_filter(&server_filter_list, fname,
-                           S_ISDIR(file->mode)) < 0) {
-               if (verbose) {
-                       rprintf(FINFO, "skipping server-excluded file \"%s\"\n",
-                               safe_fname(fname));
+       if (server_filter_list.head) {
+               if (excluded_below >= 0) {
+                       if (file->dir.depth > excluded_below)
+                               goto skipping;
+                       excluded_below = -1;
+               }
+               if (check_filter(&server_filter_list, fname,
+                                S_ISDIR(file->mode)) < 0) {
+                       if (S_ISDIR(file->mode))
+                               excluded_below = file->dir.depth;
+                   skipping:
+                       if (verbose) {
+                               rprintf(FINFO,
+                                       "skipping server-excluded file \"%s\"\n",
+                                       safe_fname(fname));
+                       }
+                       return;
                }
-               return;
        }
 
        if (missing_below >= 0 && file->dir.depth <= missing_below) {
@@ -440,15 +453,15 @@ static void recv_generator(char *fname, struct file_list *flist,
                 * we need to delete it.  If it doesn't exist, then
                 * (perhaps recursively) create it. */
                if (statret == 0 && !S_ISDIR(st.st_mode)) {
-                       delete_file(fname, DEL_TERSE);
+                       delete_file(fname, st.st_mode, DEL_TERSE);
                        statret = -1;
                }
                if (dry_run && statret != 0 && missing_below < 0) {
                        missing_below = file->dir.depth;
                        dry_run++;
                }
-               if (what_has_changed && f_out != -1)
-                       showchg(fname, file, statret, &st, 0);
+               if (itemizing && f_out != -1)
+                       itemize(file, statret, &st, 0, f_out, ndx);
                if (statret != 0 && do_mkdir(fname,file->mode) != 0 && errno != EEXIST) {
                        if (!relative_paths || errno != ENOENT
                            || create_directory_path(fname, orig_umask) < 0
@@ -459,14 +472,14 @@ static void recv_generator(char *fname, struct file_list *flist,
                        }
                }
                if (set_perms(fname, file, statret ? NULL : &st, 0)
-                   && verbose && f_out != -1 && !what_has_changed)
-                       rprintf(FINFO, "%s/\n", safe_fname(fname));
+                   && verbose && code && f_out != -1)
+                       rprintf(code, "%s/\n", safe_fname(fname));
                if (delete_during && f_out != -1 && csum_length != SUM_LENGTH
                    && (file->flags & FLAG_DEL_HERE))
                        delete_in_dir(flist, fname, file);
                return;
        }
-       
+
        if (max_size && file->length > max_size) {
                if (verbose > 1) {
                        rprintf(FINFO, "%s is over max-size\n",
@@ -487,42 +500,51 @@ static void recv_generator(char *fname, struct file_list *flist,
                        return;
                }
                if (statret == 0) {
-                       int dflag = S_ISDIR(st.st_mode) ? DEL_DIR : 0;
                        char lnk[MAXPATHLEN];
                        int len;
 
-                       if (!dflag
+                       if (!S_ISDIR(st.st_mode)
                            && (len = readlink(fname, lnk, MAXPATHLEN-1)) > 0) {
                                lnk[len] = 0;
                                /* A link already pointing to the
                                 * right place -- no further action
                                 * required. */
                                if (strcmp(lnk, file->u.link) == 0) {
-                                       if (what_has_changed)
-                                               showchg(fname, file, 0, &st, 0);
+                                       if (itemizing) {
+                                               itemize(file, 0, &st, 0,
+                                                       f_out, ndx);
+                                       }
                                        set_perms(fname, file, &st,
-                                                 PERMS_REPORT);
+                                                 maybe_PERMS_REPORT);
                                        return;
                                }
                        }
                        /* Not the right symlink (or not a symlink), so
                         * delete it. */
-                       delete_file(fname, dflag | DEL_TERSE);
+                       if (S_ISLNK(st.st_mode))
+                               delete_file(fname, st.st_mode, DEL_TERSE);
+                       else {
+                               delete_file(fname, st.st_mode, DEL_TERSE);
+                               statret = -1;
+                       }
                }
                if (do_symlink(file->u.link,fname) != 0) {
                        rsyserr(FERROR, errno, "symlink %s -> \"%s\" failed",
                                full_fname(fname), safe_fname(file->u.link));
                } else {
                        set_perms(fname,file,NULL,0);
-                       if (what_has_changed) {
-                               showchg(fname, file, statret, &st,
-                                       SC_SYMLINK_CHANGED
-                                       | (verbose ? SC_NO_NL : 0));
+                       if (itemizing) {
+                               itemize(file, statret, &st, SID_UPDATING,
+                                       f_out, ndx);
                        }
-                       if (verbose) {
-                               rprintf(FINFO, "%s -> %s\n",
-                                   what_has_changed ? "" : safe_fname(fname),
-                                   safe_fname(file->u.link));
+                       if (code && verbose) {
+                               rprintf(code, "%s -> %s\n", safe_fname(fname),
+                                       safe_fname(file->u.link));
+                       }
+                       if (remove_sent_files && !dry_run) {
+                               char numbuf[4];
+                               SIVAL(numbuf, 0, ndx);
+                               send_msg(MSG_SUCCESS, numbuf, 4);
                        }
                }
 #endif
@@ -530,13 +552,15 @@ static void recv_generator(char *fname, struct file_list *flist,
        }
 
        if (am_root && preserve_devices && IS_DEVICE(file->mode)) {
-               if (what_has_changed)
-                       showchg(fname, file, statret, &st, 0);
                if (statret != 0 ||
                    st.st_mode != file->mode ||
                    st.st_rdev != file->u.rdev) {
-                       int dflag = S_ISDIR(st.st_mode) ? DEL_DIR : 0;
-                       delete_file(fname, dflag | DEL_TERSE);
+                       if (IS_DEVICE(st.st_mode))
+                               delete_file(fname, st.st_mode, DEL_TERSE);
+                       else {
+                               delete_file(fname, st.st_mode, DEL_TERSE);
+                               statret = -1;
+                       }
                        if (verbose > 2) {
                                rprintf(FINFO,"mknod(%s,0%o,0x%x)\n",
                                        safe_fname(fname),
@@ -547,13 +571,21 @@ static void recv_generator(char *fname, struct file_list *flist,
                                        full_fname(fname));
                        } else {
                                set_perms(fname,file,NULL,0);
-                               if (verbose && !what_has_changed) {
-                                       rprintf(FINFO, "%s\n",
+                               if (itemizing) {
+                                       itemize(file, statret, &st, SID_UPDATING,
+                                               f_out, ndx);
+                               }
+                               if (code && verbose) {
+                                       rprintf(code, "%s\n",
                                                safe_fname(fname));
                                }
                        }
                } else {
-                       set_perms(fname, file, &st, PERMS_REPORT);
+                       if (itemizing) {
+                               itemize(file, statret, &st, 0,
+                                       f_out, ndx);
+                       }
+                       set_perms(fname, file, &st, maybe_PERMS_REPORT);
                }
                return;
        }
@@ -626,8 +658,7 @@ static void recv_generator(char *fname, struct file_list *flist,
        }
 
        if (statret == 0 && !S_ISREG(st.st_mode)) {
-               int dflag = S_ISDIR(st.st_mode) ? DEL_DIR : 0;
-               if (delete_file(fname, dflag | DEL_TERSE) != 0)
+               if (delete_file(fname, st.st_mode, DEL_TERSE) != 0)
                        return;
                statret = -1;
                stat_errno = ENOENT;
@@ -690,13 +721,14 @@ static void recv_generator(char *fname, struct file_list *flist,
        else if (fnamecmp_type == FNAMECMP_FUZZY)
                ;
        else if (unchanged_file(fnamecmp, file, &st)) {
-               if (what_has_changed) {
-                       showchg(fname, file, statret, &st,
-                               fnamecmp_type == FNAMECMP_FNAME ? 0
-                               : SC_NO_BASIS);
+               if (itemizing) {
+                       itemize(file, statret, &st,
+                               fnamecmp_type == FNAMECMP_FNAME
+                                              ? 0 : SID_NO_DEST_AND_NO_UPDATE,
+                               f_out, ndx);
                }
                if (fnamecmp_type == FNAMECMP_FNAME)
-                       set_perms(fname, file, &st, PERMS_REPORT);
+                       set_perms(fname, file, &st, maybe_PERMS_REPORT);
                return;
        }
 
@@ -773,8 +805,14 @@ prepare_to_open:
 
 notify_others:
        write_int(f_out, ndx);
-       if (protocol_version >= 29 && inplace && !read_batch)
-               write_byte(f_out, fnamecmp_type);
+       if (itemizing) {
+               int iflags = SID_UPDATING;
+               if (always_checksum)
+                       iflags |= SID_REPORT_CHECKSUM;
+               if (fnamecmp_type != FNAMECMP_FNAME)
+                       iflags |= SID_USING_ALT_BASIS;
+               itemize(file, statret, &st, iflags, f_out, -1);
+       }
        if (f_out_name >= 0) {
                write_byte(f_out_name, fnamecmp_type);
                if (fnamecmp_type == FNAMECMP_FUZZY) {
@@ -789,15 +827,10 @@ notify_others:
 #endif
                        }
                        *lb = len;
-                       write_buf(f_out_name, lenbuf, lb - lenbuf + 1);
+                       write_buf(f_out_name, (char*)lenbuf, lb - lenbuf + 1);
                        write_buf(f_out_name, fuzzy_file->basename, len);
                }
        }
-       if (what_has_changed) {
-               showchg(fname, file, statret, &st,
-                       (always_checksum ? SC_CHECKSUM_CHANGED : 0)
-                       | SC_SENDING_FILE);
-       }
 
        if (dry_run || read_batch)
                return;
@@ -827,10 +860,31 @@ void generate_files(int f_out, struct file_list *flist, char *local_name,
        int i;
        int phase = 0;
        char fbuf[MAXPATHLEN];
+       int itemizing, maybe_PERMS_REPORT;
+       enum logcode code;
        int need_retouch_dir_times = preserve_times && !omit_dir_times;
        int need_retouch_dir_perms = 0;
        int save_only_existing = only_existing;
        int save_opt_ignore_existing = opt_ignore_existing;
+       int allowed_lull = io_timeout / 2;
+
+       if (protocol_version >= 29) {
+               itemizing = 1;
+               maybe_PERMS_REPORT = log_format_has_i ? 0 : PERMS_REPORT;
+               code = daemon_log_format_has_i ? 0 : FLOG;
+       } else if (am_daemon) {
+               itemizing = daemon_log_format_has_i && !dry_run;
+               maybe_PERMS_REPORT = PERMS_REPORT;
+               code = itemizing || dry_run ? FCLIENT : FINFO;
+       } else if (!am_server) {
+               itemizing = log_format_has_i;
+               maybe_PERMS_REPORT = log_format_has_i ? 0 : PERMS_REPORT;
+               code = itemizing ? 0 : FINFO;
+       } else {
+               itemizing = 0;
+               maybe_PERMS_REPORT = PERMS_REPORT;
+               code = FINFO;
+       }
 
        if (verbose > 2) {
                rprintf(FINFO, "generator starting pid=%ld count=%d\n",
@@ -838,15 +892,14 @@ void generate_files(int f_out, struct file_list *flist, char *local_name,
        }
 
        if (verbose >= 2) {
-               rprintf(FINFO,
+               rprintf(FINFO, "delta-transmission %s\n",
                        whole_file > 0
-                       ? "delta-transmission disabled for local transfer or --whole-file\n"
-                       : "delta transmission enabled\n");
+                       ? "disabled for local transfer or --whole-file"
+                       : "enabled");
        }
 
-       /* We expect to just sit around now, so don't exit on a timeout.
-        * If we really get a timeout then the other process should exit. */
-       io_timeout = 0;
+       if (protocol_version < 29)
+               ignore_timeout = 1;
 
        for (i = 0; i < flist->count; i++) {
                struct file_struct *file = flist->files[i];
@@ -866,9 +919,13 @@ void generate_files(int f_out, struct file_list *flist, char *local_name,
                }
 
                recv_generator(local_name ? local_name : f_name_to(file, fbuf),
-                              flist, file, i, f_out, f_out_name);
+                              flist, file, i, itemizing, maybe_PERMS_REPORT,
+                              code, f_out, f_out_name);
+
+               if (allowed_lull && !(i % 100))
+                       maybe_send_keepalive(allowed_lull, flist->count);
        }
-       recv_generator(NULL, NULL, NULL, 0, -1, -1);
+       recv_generator(NULL, NULL, NULL, 0, 0, 0, code, -1, -1);
        if (delete_during)
                delete_in_dir(NULL, NULL, NULL);
 
@@ -878,6 +935,10 @@ void generate_files(int f_out, struct file_list *flist, char *local_name,
        update_only = always_checksum = size_only = 0;
        ignore_times = 1;
 
+       /* We expect to just sit around now, so don't exit on a timeout.
+        * If we really get a timeout then the other process should exit. */
+       ignore_timeout = 1;
+
        if (verbose > 2)
                rprintf(FINFO,"generate_files phase=%d\n",phase);
 
@@ -888,7 +949,10 @@ void generate_files(int f_out, struct file_list *flist, char *local_name,
        while ((i = get_redo_num()) != -1) {
                struct file_struct *file = flist->files[i];
                recv_generator(local_name ? local_name : f_name_to(file, fbuf),
-                              flist, file, i, f_out, f_out_name);
+                              flist, file, i, itemizing, maybe_PERMS_REPORT,
+                              code, f_out, f_out_name);
+               if (allowed_lull)
+                       maybe_send_keepalive(allowed_lull, flist->count);
        }
 
        phase++;
@@ -918,10 +982,11 @@ void generate_files(int f_out, struct file_list *flist, char *local_name,
                        if (!need_retouch_dir_times && file->mode & S_IWUSR)
                                continue;
                        recv_generator(local_name ? local_name : f_name(file),
-                                      flist, file, i, -1, -1);
+                                      flist, file, i, itemizing,
+                                      maybe_PERMS_REPORT, code, -1, -1);
                }
        }
-       recv_generator(NULL, NULL, NULL, 0, -1, -1);
+       recv_generator(NULL, NULL, NULL, 0, 0, 0, code, -1, -1);
 
        if (verbose > 2)
                rprintf(FINFO,"generate_files finished\n");