Decided on a better option name --itemize-changes (-i).
[rsync/rsync.git] / generator.c
index 8f3177c..aecf892 100644 (file)
@@ -25,6 +25,7 @@
 
 extern int verbose;
 extern int dry_run;
+extern int itemize_changes;
 extern int relative_paths;
 extern int keep_dirlinks;
 extern int preserve_links;
@@ -47,6 +48,7 @@ extern int size_only;
 extern OFF_T max_size;
 extern int io_timeout;
 extern int protocol_version;
+extern int fuzzy_basis;
 extern int always_checksum;
 extern char *partial_dir;
 extern char *basis_dir[];
@@ -78,6 +80,57 @@ static int unchanged_attrs(struct file_struct *file, STRUCT_STAT *st)
        return 1;
 }
 
+
+#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)
+
+static void showchg(const char *fname, struct file_struct *file, int statret,
+                   STRUCT_STAT *st, int flags)
+{
+       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");
+       }
+}
+
+
 /* Perform our quick-check heuristic for determining if a file is unchanged. */
 static int unchanged_file(char *fn, struct file_struct *file, STRUCT_STAT *st)
 {
@@ -186,7 +239,7 @@ static void sum_sizes_sqroot(struct sum_struct *sum, int64 len)
  */
 static void generate_and_send_sums(int fd, OFF_T len, int f_out, int f_copy)
 {
-       size_t i;
+       int32 i;
        struct map_struct *mapbuf;
        struct sum_struct sum;
        OFF_T offset = 0;
@@ -228,6 +281,60 @@ static void generate_and_send_sums(int fd, OFF_T len, int f_out, int f_copy)
 }
 
 
+/* Try to find a filename in the same dir as "fname" with a similar name. */
+static int find_fuzzy(struct file_struct *file, struct file_list *dirlist)
+{
+       int fname_len, fname_suf_len;
+       const char *fname_suf, *fname = file->basename;
+       uint32 lowest_dist = 0x7FFFFFFF;
+       int j, lowest_j = -1;
+
+       fname_len = strlen(fname);
+       fname_suf = find_filename_suffix(fname, fname_len, &fname_suf_len);
+
+       for (j = 0; j < dirlist->count; j++) {
+               struct file_struct *fp = dirlist->files[j];
+               const char *suf, *name;
+               int len, suf_len;
+               uint32 dist;
+
+               if (!S_ISREG(fp->mode) || !fp->length
+                   || fp->flags & FLAG_NO_FUZZY)
+                       continue;
+
+               name = fp->basename;
+
+               if (fp->length == file->length
+                   && fp->modtime == file->modtime) {
+                       if (verbose > 4) {
+                               rprintf(FINFO,
+                                       "fuzzy size/modtime match for %s\n",
+                                       name);
+                       }
+                       return j;
+               }
+
+               len = strlen(name);
+               suf = find_filename_suffix(name, len, &suf_len);
+
+               dist = fuzzy_distance(name, len, fname, fname_len);
+               /* Add some extra weight to how well the suffixes match. */
+               dist += fuzzy_distance(suf, suf_len, fname_suf, fname_suf_len)
+                     * 10;
+               if (verbose > 4) {
+                       rprintf(FINFO, "fuzzy distance for %s = %d.%05d\n",
+                               name, (int)(dist>>16), (int)(dist&0xFFFF));
+               }
+               if (dist <= lowest_dist) {
+                       lowest_dist = dist;
+                       lowest_j = j;
+               }
+       }
+
+       return lowest_j;
+}
+
+
 /* Acts on flist->file's ndx'th item, whose name is fname.  If a directory,
  * make sure it exists, and has the right permissions/timestamp info.  For
  * all other non-regular files (symlinks, etc.) we create them here.  For
@@ -240,6 +347,10 @@ static void recv_generator(char *fname, struct file_list *flist,
                           struct file_struct *file, int ndx,
                           int f_out, int f_out_name)
 {
+       static int missing_below = -1;
+       static char *fuzzy_dirname = NULL;
+       static struct file_list *fuzzy_dirlist = NULL;
+       struct file_struct *fuzzy_file = NULL;
        int fd = -1, f_copy = -1;
        STRUCT_STAT st, partial_st;
        struct file_struct *back_file = NULL;
@@ -251,6 +362,19 @@ static void recv_generator(char *fname, struct file_list *flist,
        if (list_only)
                return;
 
+       if (!fname) {
+               if (fuzzy_dirlist) {
+                       flist_free(fuzzy_dirlist);
+                       fuzzy_dirlist = NULL;
+                       fuzzy_dirname = NULL;
+               }
+               if (missing_below >= 0) {
+                       dry_run--;
+                       missing_below = -1;
+               }
+               return;
+       }
+
        if (verbose > 2) {
                rprintf(FINFO, "recv_generator(%s,%d)\n",
                        safe_fname(fname), ndx);
@@ -266,10 +390,26 @@ static void recv_generator(char *fname, struct file_list *flist,
                return;
        }
 
+       if (missing_below >= 0 && file->dir.depth <= missing_below) {
+               dry_run--;
+               missing_below = -1;
+       }
        if (dry_run > 1) {
                statret = -1;
                stat_errno = ENOENT;
        } else {
+               if (fuzzy_basis && S_ISREG(file->mode)) {
+                       char *dn = file->dirname ? file->dirname : ".";
+                       /* Yes, identical dirnames are guaranteed to have
+                        * identical pointers at this point. */
+                       if (fuzzy_dirname != dn) {
+                               if (fuzzy_dirlist)
+                                       flist_free(fuzzy_dirlist);
+                               fuzzy_dirname = dn;
+                               fuzzy_dirlist = get_dirlist(fuzzy_dirname, 1);
+                       }
+               }
+
                statret = link_stat(fname, &st,
                                    keep_dirlinks && S_ISDIR(file->mode));
                stat_errno = errno;
@@ -303,6 +443,12 @@ static void recv_generator(char *fname, struct file_list *flist,
                        delete_file(fname, DEL_TERSE);
                        statret = -1;
                }
+               if (dry_run && statret != 0 && missing_below < 0) {
+                       missing_below = file->dir.depth;
+                       dry_run++;
+               }
+               if (itemize_changes && f_out != -1)
+                       showchg(fname, file, statret, &st, 0);
                if (statret != 0 && do_mkdir(fname,file->mode) != 0 && errno != EEXIST) {
                        if (!relative_paths || errno != ENOENT
                            || create_directory_path(fname, orig_umask) < 0
@@ -313,13 +459,15 @@ static void recv_generator(char *fname, struct file_list *flist,
                        }
                }
                if (set_perms(fname, file, statret ? NULL : &st, 0)
-                   && verbose && f_out != -1)
+                   && verbose && f_out != -1 && !itemize_changes)
                        rprintf(FINFO, "%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;
-       } else if (max_size && file->length > max_size) {
+       }
+       
+       if (max_size && file->length > max_size) {
                if (verbose > 1) {
                        rprintf(FINFO, "%s is over max-size\n",
                                safe_fname(fname));
@@ -328,7 +476,7 @@ static void recv_generator(char *fname, struct file_list *flist,
        }
 
        if (preserve_links && S_ISLNK(file->mode)) {
-#if SUPPORT_LINKS
+#ifdef SUPPORT_LINKS
                if (safe_symlinks && unsafe_symlink(file->u.link, fname)) {
                        if (verbose) {
                                rprintf(FINFO,
@@ -350,6 +498,8 @@ static void recv_generator(char *fname, struct file_list *flist,
                                 * right place -- no further action
                                 * required. */
                                if (strcmp(lnk, file->u.link) == 0) {
+                                       if (itemize_changes)
+                                               showchg(fname, file, 0, &st, 0);
                                        set_perms(fname, file, &st,
                                                  PERMS_REPORT);
                                        return;
@@ -364,9 +514,15 @@ static void recv_generator(char *fname, struct file_list *flist,
                                full_fname(fname), safe_fname(file->u.link));
                } else {
                        set_perms(fname,file,NULL,0);
+                       if (itemize_changes) {
+                               showchg(fname, file, statret, &st,
+                                       SC_SYMLINK_CHANGED
+                                       | (verbose ? SC_NO_NL : 0));
+                       }
                        if (verbose) {
-                               rprintf(FINFO, "%s -> %s\n", safe_fname(fname),
-                                       safe_fname(file->u.link));
+                               rprintf(FINFO, "%s -> %s\n",
+                                   itemize_changes ? "" : safe_fname(fname),
+                                   safe_fname(file->u.link));
                        }
                }
 #endif
@@ -374,6 +530,8 @@ static void recv_generator(char *fname, struct file_list *flist,
        }
 
        if (am_root && preserve_devices && IS_DEVICE(file->mode)) {
+               if (itemize_changes)
+                       showchg(fname, file, statret, &st, 0);
                if (statret != 0 ||
                    st.st_mode != file->mode ||
                    st.st_rdev != file->u.rdev) {
@@ -389,7 +547,7 @@ static void recv_generator(char *fname, struct file_list *flist,
                                        full_fname(fname));
                        } else {
                                set_perms(fname,file,NULL,0);
-                               if (verbose) {
+                               if (verbose && !itemize_changes) {
                                        rprintf(FINFO, "%s\n",
                                                safe_fname(fname));
                                }
@@ -412,7 +570,7 @@ static void recv_generator(char *fname, struct file_list *flist,
        fnamecmp = fname;
        fnamecmp_type = FNAMECMP_FNAME;
 
-       if (statret == -1 && basis_dir[0] != NULL) {
+       if (statret != 0 && basis_dir[0] != NULL) {
                int fallback_match = -1;
                int match_level = 0;
                int i = 0;
@@ -446,7 +604,7 @@ static void recv_generator(char *fname, struct file_list *flist,
                                pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
                                         basis_dir[i], fname);
                        }
-#if HAVE_LINK
+#ifdef HAVE_LINK
                        if (link_dest && match_level == 3 && !dry_run) {
                                if (do_link(fnamecmpbuf, fname) < 0) {
                                        if (verbose) {
@@ -478,12 +636,30 @@ static void recv_generator(char *fname, struct file_list *flist,
        if (partial_dir && (partialptr = partial_dir_fname(fname)) != NULL
            && link_stat(partialptr, &partial_st, 0) == 0
            && S_ISREG(partial_st.st_mode)) {
-               if (statret == -1)
+               if (statret != 0)
                        goto prepare_to_open;
        } else
                partialptr = NULL;
 
-       if (statret == -1) {
+       if (statret != 0 && fuzzy_basis && dry_run <= 1) {
+               int j = find_fuzzy(file, fuzzy_dirlist);
+               if (j >= 0) {
+                       fuzzy_file = fuzzy_dirlist->files[j];
+                       f_name_to(fuzzy_file, fnamecmpbuf);
+                       if (verbose > 2) {
+                               rprintf(FINFO, "fuzzy basis selected for %s: %s\n",
+                                       safe_fname(fname), safe_fname(fnamecmpbuf));
+                       }
+                       st.st_mode = fuzzy_file->mode;
+                       st.st_size = fuzzy_file->length;
+                       st.st_mtime = fuzzy_file->modtime;
+                       statret = 0;
+                       fnamecmp = fnamecmpbuf;
+                       fnamecmp_type = FNAMECMP_FUZZY;
+               }
+       }
+
+       if (statret != 0) {
                if (preserve_hard_links && hard_link_check(file, HL_SKIP))
                        return;
                if (stat_errno == ENOENT)
@@ -511,7 +687,14 @@ static void recv_generator(char *fname, struct file_list *flist,
 
        if (!compare_dest && fnamecmp_type <= FNAMECMP_BASIS_DIR_HIGH)
                ;
+       else if (fnamecmp_type == FNAMECMP_FUZZY)
+               ;
        else if (unchanged_file(fnamecmp, file, &st)) {
+               if (itemize_changes) {
+                       showchg(fname, file, statret, &st,
+                               fnamecmp_type == FNAMECMP_FNAME ? 0
+                               : SC_NO_BASIS);
+               }
                if (fnamecmp_type == FNAMECMP_FNAME)
                        set_perms(fname, file, &st, PERMS_REPORT);
                return;
@@ -525,12 +708,19 @@ prepare_to_open:
                statret = 0;
        }
 
-       if (dry_run || whole_file > 0) {
-               statret = -1;
+       if (dry_run || read_batch)
                goto notify_others;
-       }
-       if (read_batch)
+       if (whole_file > 0) {
+               if (statret == 0)
+                       statret = 1;
                goto notify_others;
+       }
+
+       if (fuzzy_basis) {
+               int j = flist_find(fuzzy_dirlist, file);
+               if (j >= 0) /* don't use changing file as future fuzzy basis */
+                       fuzzy_dirlist->files[j]->flags |= FLAG_NO_FUZZY;
+       }
 
        /* open the file */
        fd = do_open(fnamecmp, O_RDONLY, 0);
@@ -585,8 +775,29 @@ notify_others:
        write_int(f_out, ndx);
        if (protocol_version >= 29 && inplace && !read_batch)
                write_byte(f_out, fnamecmp_type);
-       if (f_out_name >= 0)
+       if (f_out_name >= 0) {
                write_byte(f_out_name, fnamecmp_type);
+               if (fnamecmp_type == FNAMECMP_FUZZY) {
+                       uchar lenbuf[3], *lb = lenbuf;
+                       int len = strlen(fuzzy_file->basename);
+                       if (len > 0x7F) {
+#if MAXPATHLEN > 0x7FFF
+                               *lb++ = len / 0x10000 + 0x80;
+                               *lb++ = len / 0x100;
+#else
+                               *lb++ = len / 0x100 + 0x80;
+#endif
+                       }
+                       *lb = len;
+                       write_buf(f_out_name, lenbuf, lb - lenbuf + 1);
+                       write_buf(f_out_name, fuzzy_file->basename, len);
+               }
+       }
+       if (itemize_changes) {
+               showchg(fname, file, statret, &st,
+                       (always_checksum ? SC_CHECKSUM_CHANGED : 0)
+                       | SC_SENDING_FILE);
+       }
 
        if (dry_run || read_batch)
                return;
@@ -657,6 +868,7 @@ 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);
        }
+       recv_generator(NULL, NULL, NULL, 0, -1, -1);
        if (delete_during)
                delete_in_dir(NULL, NULL, NULL);
 
@@ -709,6 +921,7 @@ void generate_files(int f_out, struct file_list *flist, char *local_name,
                                       flist, file, i, -1, -1);
                }
        }
+       recv_generator(NULL, NULL, NULL, 0, -1, -1);
 
        if (verbose > 2)
                rprintf(FINFO,"generate_files finished\n");