Compute the right checksum length for the current protocol_version
[rsync/rsync.git] / flist.c
diff --git a/flist.c b/flist.c
index 4f40da8..3e7481f 100644 (file)
--- a/flist.c
+++ b/flist.c
@@ -62,10 +62,13 @@ extern struct file_list *the_file_list;
 
 extern char curr_dir[MAXPATHLEN];
 
+extern struct chmod_mode_struct *chmod_modes;
+
 extern struct filter_list_struct filter_list;
 extern struct filter_list_struct server_filter_list;
 
 int io_error;
+int checksum_len;
 dev_t filesystem_dev; /* used to implement -x */
 
 static char empty_sum[MD4_SUM_LENGTH];
@@ -82,9 +85,9 @@ void init_flist(void)
 
        /* Figure out how big the file_struct is without trailing padding */
        file_struct_len = offsetof(struct file_struct, flags) + sizeof f.flags;
+       checksum_len = protocol_version < 21 ? 2 : MD4_SUM_LENGTH;
 }
 
-
 static int show_filelist_p(void)
 {
        return verbose && xfer_dirs && !am_server;
@@ -98,20 +101,17 @@ static void start_filelist_progress(char *kind)
        rflush(FINFO);
 }
 
-
 static void emit_filelist_progress(int count)
 {
        rprintf(FINFO, " %d files...\r", count);
 }
 
-
 static void maybe_emit_filelist_progress(int count)
 {
        if (do_progress && show_filelist_p() && (count % 100) == 0)
                emit_filelist_progress(count);
 }
 
-
 static void finish_filelist_progress(const struct file_list *flist)
 {
        if (do_progress) {
@@ -127,7 +127,6 @@ void show_flist_stats(void)
        /* Nothing yet */
 }
 
-
 static void list_file_entry(struct file_struct *f)
 {
        char perms[11];
@@ -155,7 +154,6 @@ static void list_file_entry(struct file_struct *f)
        }
 }
 
-
 /**
  * Stat either a symlink or its referent, depending on the settings of
  * copy_links, copy_unsafe_links, etc.
@@ -261,7 +259,6 @@ static mode_t from_wire_mode(int mode)
        return (mode_t)mode;
 }
 
-
 static void send_directory(int f, struct file_list *flist,
                           char *fbuf, int len);
 
@@ -414,10 +411,8 @@ void send_file_entry(struct file_struct *file, int f, unsigned short base_flags)
                } else
                        write_byte(f, flags);
        } else {
-               if (!(flags & 0xFF) && !S_ISDIR(mode))
-                       flags |= XMIT_TOP_DIR;
                if (!(flags & 0xFF))
-                       flags |= XMIT_LONG_NAME;
+                       flags |= S_ISDIR(mode) ? XMIT_LONG_NAME : XMIT_TOP_DIR;
                write_byte(f, flags);
        }
        if (flags & XMIT_SAME_NAME)
@@ -480,19 +475,15 @@ void send_file_entry(struct file_struct *file, int f, unsigned short base_flags)
        }
 #endif
 
-       if (always_checksum) {
+       if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
                char *sum;
                if (S_ISREG(mode))
                        sum = file->u.sum;
-               else if (protocol_version < 28) {
+               else {
                        /* Prior to 28, we sent a useless set of nulls. */
                        sum = empty_sum;
-               } else
-                       sum = NULL;
-               if (sum) {
-                       write_buf(f, sum,
-                           protocol_version < 21 ? 2 : MD4_SUM_LENGTH);
                }
+               write_buf(f, sum, checksum_len);
        }
 
        strlcpy(lastname, fname, MAXPATHLEN);
@@ -500,8 +491,6 @@ void send_file_entry(struct file_struct *file, int f, unsigned short base_flags)
        io_write_phase = "unknown";
 }
 
-
-
 static struct file_struct *receive_file_entry(struct file_list *flist,
                                              unsigned short flags, int f)
 {
@@ -653,7 +642,7 @@ static struct file_struct *receive_file_entry(struct file_list *flist,
                if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */
                        file->dir.depth--;
                if (flags & XMIT_TOP_DIR) {
-                       in_del_hier = 1;
+                       in_del_hier = recurse;
                        del_hier_name_len = file->dir.depth == 0 ? 0 : l1 + l2;
                        if (relative_paths && del_hier_name_len > 2
                            && basename_len == 1+1 && *basename == '.')
@@ -708,20 +697,16 @@ static struct file_struct *receive_file_entry(struct file_list *flist,
        }
 #endif
 
-       if (always_checksum) {
+       if (always_checksum && (sum_len || protocol_version < 28)) {
                char *sum;
                if (sum_len) {
                        file->u.sum = sum = bp;
                        /*bp += sum_len;*/
-               } else if (protocol_version < 28) {
+               } else {
                        /* Prior to 28, we get a useless set of nulls. */
                        sum = empty_sum;
-               } else
-                       sum = NULL;
-               if (sum) {
-                       read_buf(f, sum,
-                           protocol_version < 21 ? 2 : MD4_SUM_LENGTH);
                }
+               read_buf(f, sum, checksum_len);
        }
 
        if (!preserve_perms) {
@@ -733,7 +718,6 @@ static struct file_struct *receive_file_entry(struct file_list *flist,
        return file;
 }
 
-
 /**
  * Create a file_struct for a named file by reading its stat()
  * information and performing extensive checks against global
@@ -883,7 +867,10 @@ skip_filters:
        file->flags = flags;
        file->modtime = st.st_mtime;
        file->length = st.st_size;
-       file->mode = st.st_mode;
+       if (chmod_modes && am_sender && (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode)))
+               file->mode = tweak_mode(st.st_mode, chmod_modes);
+       else
+               file->mode = st.st_mode;
        file->uid = st.st_uid;
        file->gid = st.st_gid;
 
@@ -965,7 +952,6 @@ skip_filters:
        return file;
 }
 
-
 static struct file_struct *send_file_name(int f, struct file_list *flist,
                                          char *fname, unsigned short base_flags)
 {
@@ -987,9 +973,10 @@ static struct file_struct *send_file_name(int f, struct file_list *flist,
 }
 
 static void send_if_directory(int f, struct file_list *flist,
-                             struct file_struct *file)
+                             struct file_struct *file,
+                             char *fbuf, unsigned int ol)
 {
-       char fbuf[MAXPATHLEN];
+       char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/');
 
        if (S_ISDIR(file->mode)
            && !(file->flags & FLAG_MOUNT_POINT) && f_name_to(file, fbuf)) {
@@ -1006,10 +993,12 @@ static void send_if_directory(int f, struct file_list *flist,
                save_filters = push_local_filters(fbuf, len);
                send_directory(f, flist, fbuf, len);
                pop_local_filters(save_filters);
+               fbuf[ol] = '\0';
+               if (is_dot_dir)
+                       fbuf[ol-1] = '.';
        }
 }
 
-
 /* This function is normally called by the sender, but the receiving side also
  * calls it from get_dirlist() with f set to -1 so that we just construct the
  * file list in memory without sending it over the wire.  Also, get_dirlist()
@@ -1063,14 +1052,13 @@ static void send_directory(int f, struct file_list *flist,
        if (recurse) {
                int i, end = flist->count - 1;
                for (i = start; i <= end; i++)
-                       send_if_directory(f, flist, flist->files[i]);
+                       send_if_directory(f, flist, flist->files[i], fbuf, len);
        }
 }
 
-
 struct file_list *send_file_list(int f, int argc, char *argv[])
 {
-       int l;
+       int len;
        STRUCT_STAT st;
        char *p, *dir, olddir[sizeof curr_dir];
        char lastpath[MAXPATHLEN] = "";
@@ -1098,58 +1086,57 @@ struct file_list *send_file_list(int f, int argc, char *argv[])
        }
 
        while (1) {
-               struct file_struct *file;
-               char fname2[MAXPATHLEN];
-               char *fname = fname2;
+               char fbuf[MAXPATHLEN];
+               char *fn;
                int is_dot_dir;
 
                if (use_ff_fd) {
-                       if (read_filesfrom_line(filesfrom_fd, fname) == 0)
+                       if (read_filesfrom_line(filesfrom_fd, fbuf) == 0)
                                break;
-                       sanitize_path(fname, fname, "", 0);
+                       sanitize_path(fbuf, fbuf, "", 0);
                } else {
                        if (argc-- == 0)
                                break;
-                       strlcpy(fname, *argv++, MAXPATHLEN);
+                       strlcpy(fbuf, *argv++, MAXPATHLEN);
                        if (sanitize_paths)
-                               sanitize_path(fname, fname, "", 0);
+                               sanitize_path(fbuf, fbuf, "", 0);
                }
 
-               l = strlen(fname);
-               if (!l || fname[l - 1] == '/') {
-                       if (l == 2 && fname[0] == '.') {
+               len = strlen(fbuf);
+               if (!len || fbuf[len - 1] == '/') {
+                       if (len == 2 && fbuf[0] == '.') {
                                /* Turn "./" into just "." rather than "./." */
-                               fname[1] = '\0';
+                               fbuf[1] = '\0';
                        } else {
-                               if (l + 1 >= MAXPATHLEN)
+                               if (len + 1 >= MAXPATHLEN)
                                        overflow_exit("send_file_list");
-                               fname[l++] = '.';
-                               fname[l] = '\0';
+                               fbuf[len++] = '.';
+                               fbuf[len] = '\0';
                        }
                        is_dot_dir = 1;
-               } else if (l > 1 && fname[l-1] == '.' && fname[l-2] == '.'
-                   && (l == 2 || fname[l-3] == '/')) {
-                       if (l + 2 >= MAXPATHLEN)
+               } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.'
+                   && (len == 2 || fbuf[len-3] == '/')) {
+                       if (len + 2 >= MAXPATHLEN)
                                overflow_exit("send_file_list");
-                       fname[l++] = '/';
-                       fname[l++] = '.';
-                       fname[l] = '\0';
+                       fbuf[len++] = '/';
+                       fbuf[len++] = '.';
+                       fbuf[len] = '\0';
                        is_dot_dir = 1;
                } else {
-                       is_dot_dir = fname[l-1] == '.'
-                                  && (l == 1 || fname[l-2] == '/');
+                       is_dot_dir = fbuf[len-1] == '.'
+                                  && (len == 1 || fbuf[len-2] == '/');
                }
 
-               if (link_stat(fname, &st, keep_dirlinks) != 0) {
+               if (link_stat(fbuf, &st, keep_dirlinks) != 0) {
                        io_error |= IOERR_GENERAL;
                        rsyserr(FERROR, errno, "link_stat %s failed",
-                               full_fname(fname));
+                               full_fname(fbuf));
                        continue;
                }
 
                if (S_ISDIR(st.st_mode) && !xfer_dirs) {
                        rprintf(FINFO, "skipping directory %s\n",
-                               safe_fname(fname));
+                               safe_fname(fbuf));
                        continue;
                }
 
@@ -1157,32 +1144,38 @@ struct file_list *send_file_list(int f, int argc, char *argv[])
                olddir[0] = '\0';
 
                if (!relative_paths) {
-                       p = strrchr(fname, '/');
+                       p = strrchr(fbuf, '/');
                        if (p) {
                                *p = '\0';
-                               if (p == fname)
+                               if (p == fbuf)
                                        dir = "/";
                                else
-                                       dir = fname;
-                               fname = p + 1;
-                       }
-               } else if ((p = strstr(fname, "/./")) != NULL) {
+                                       dir = fbuf;
+                               len -= p - fbuf + 1;
+                               fn = p + 1;
+                       } else
+                               fn = fbuf;
+               } else if ((p = strstr(fbuf, "/./")) != NULL) {
                        *p = '\0';
-                       if (p == fname)
+                       if (p == fbuf)
                                dir = "/";
                        else
-                               dir = fname;
-                       fname = p + 3;
-               }
+                               dir = fbuf;
+                       len -= p - fbuf + 3;
+                       fn = p + 3;
+               } else
+                       fn = fbuf;
 
-               if (!*fname)
-                       fname = ".";
+               if (!*fn) {
+                       len = 1;
+                       fn = ".";
+               }
 
                if (dir && *dir) {
                        static char *lastdir;
                        static int lastdir_len;
 
-                       strcpy(olddir, curr_dir); /* can't overflow */
+                       strlcpy(olddir, curr_dir, sizeof olddir);
 
                        if (!push_dir(dir)) {
                                io_error |= IOERR_GENERAL;
@@ -1200,17 +1193,19 @@ struct file_list *send_file_list(int f, int argc, char *argv[])
                        }
                }
 
-               if (implied_dirs && (p=strrchr(fname,'/')) && p != fname) {
+               if (fn != fbuf)
+                       memmove(fbuf, fn, len + 1);
+
+               if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) {
                        /* Send the implied directories at the start of the
                         * source spec, so we get their permissions right. */
-                       char *lp = lastpath, *fn = fname, *slash = fname;
+                       char *lp = lastpath, *slash = fbuf;
                        *p = '\0';
                        /* Skip any initial directories in our path that we
                         * have in common with lastpath. */
-                       while (*fn && *lp == *fn) {
+                       for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) {
                                if (*fn == '/')
                                        slash = fn;
-                               lp++, fn++;
                        }
                        *p = '/';
                        if (fn != p || (*lp && *lp != '/')) {
@@ -1220,13 +1215,13 @@ struct file_list *send_file_list(int f, int argc, char *argv[])
                                xfer_dirs = 1;
                                while ((slash = strchr(slash+1, '/')) != 0) {
                                        *slash = '\0';
-                                       send_file_name(f, flist, fname, 0);
+                                       send_file_name(f, flist, fbuf, 0);
                                        *slash = '/';
                                }
                                copy_links = save_copy_links;
                                xfer_dirs = save_xfer_dirs;
                                *p = '\0';
-                               strlcpy(lastpath, fname, sizeof lastpath);
+                               strlcpy(lastpath, fbuf, sizeof lastpath);
                                *p = '/';
                        }
                }
@@ -1234,17 +1229,19 @@ struct file_list *send_file_list(int f, int argc, char *argv[])
                if (one_file_system)
                        filesystem_dev = st.st_dev;
 
-               if ((file = send_file_name(f, flist, fname, XMIT_TOP_DIR))) {
-                       if (recurse || (xfer_dirs && is_dot_dir))
-                               send_if_directory(f, flist, file);
-               }
+               if (recurse || (xfer_dirs && is_dot_dir)) {
+                       struct file_struct *file;
+                       if ((file = send_file_name(f, flist, fbuf, XMIT_TOP_DIR)))
+                               send_if_directory(f, flist, file, fbuf, len);
+               } else
+                       send_file_name(f, flist, fbuf, 0);
 
                if (olddir[0]) {
                        flist_dir = NULL;
                        flist_dir_len = 0;
                        if (!pop_dir(olddir)) {
                                rsyserr(FERROR, errno, "pop_dir %s failed",
-                                       full_fname(dir));
+                                       full_fname(olddir));
                                exit_cleanup(RERR_FILESELECT);
                        }
                }
@@ -1297,7 +1294,6 @@ struct file_list *send_file_list(int f, int argc, char *argv[])
        return flist;
 }
 
-
 struct file_list *recv_file_list(int f)
 {
        struct file_list *flist;
@@ -1383,13 +1379,11 @@ oom:
        return NULL;            /* not reached */
 }
 
-
 static int file_compare(struct file_struct **file1, struct file_struct **file2)
 {
        return f_name_cmp(*file1, *file2);
 }
 
-
 /* Search for an identically-named item in the file list.  Note that the
  * items must agree in their directory-ness, or no match is returned. */
 int flist_find(struct file_list *flist, struct file_struct *f)
@@ -1419,7 +1413,6 @@ int flist_find(struct file_list *flist, struct file_struct *f)
        return -1;
 }
 
-
 /*
  * Free up any resources a file_struct has allocated
  * and clear the file.
@@ -1431,7 +1424,6 @@ void clear_file(int i, struct file_list *flist)
        memset(flist->files[i], 0, file_struct_len);
 }
 
-
 /*
  * allocate a new file list
  */
@@ -1471,7 +1463,6 @@ void flist_free(struct file_list *flist)
        free(flist);
 }
 
-
 /*
  * This routine ensures we don't have any duplicate names in our file list.
  * duplicate names can cause corruption because of the pipelining
@@ -1576,7 +1567,6 @@ static void clean_flist(struct file_list *flist, int strip_root, int no_dups)
        }
 }
 
-
 static void output_flist(struct file_list *flist)
 {
        char uidbuf[16], gidbuf[16], depthbuf[16];
@@ -1605,7 +1595,6 @@ static void output_flist(struct file_list *flist)
        }
 }
 
-
 enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING };
 enum fnc_type { t_PATH, t_ITEM };
 
@@ -1752,7 +1741,6 @@ int f_name_cmp(struct file_struct *f1, struct file_struct *f2)
        return dif;
 }
 
-
 /* Return a copy of the full filename of a flist entry, using the indicated
  * buffer.  No size-checking is done because we checked the size when creating
  * the file_struct entry.
@@ -1772,7 +1760,6 @@ char *f_name_to(struct file_struct *f, char *fbuf)
        return fbuf;
 }
 
-
 /* Like f_name_to(), but we rotate through 5 static buffers of our own. */
 char *f_name(struct file_struct *f)
 {
@@ -1784,7 +1771,6 @@ char *f_name(struct file_struct *f)
        return f_name_to(f, names[n]);
 }
 
-
 /* Do a non-recursive scan of the named directory, possibly ignoring all
  * exclude rules except for the daemon's.  If "dlen" is >=0, it is the length
  * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN