- Typical tranfers now save 12-20 bytes per file because several vars
[rsync/rsync.git] / main.c
diff --git a/main.c b/main.c
index ed0ce0a..2833435 100644 (file)
--- a/main.c
+++ b/main.c
@@ -72,6 +72,7 @@ extern char curr_dir[MAXPATHLEN];
 extern struct filter_list_struct server_filter_list;
 
 int local_server = 0;
+int new_root_dir = 0;
 mode_t orig_umask = 0;
 struct file_list *the_file_list;
 
@@ -237,7 +238,8 @@ static void output_summary(void)
                        human_num(stats.literal_data));
                rprintf(FINFO,"Matched data: %s bytes\n",
                        human_num(stats.matched_data));
-               rprintf(FINFO,"File list size: %d\n", stats.flist_size);
+               rprintf(FINFO,"File list size: %s\n",
+                       human_num(stats.flist_size));
                if (stats.flist_buildtime) {
                        rprintf(FINFO,
                                "File list generation time: %.3f seconds\n",
@@ -476,11 +478,9 @@ static char *get_local_name(struct file_list *flist, char *dest_path)
                return NULL;
 
        /* See what currently exists at the destination. */
-       if ((statret = safe_stat(dest_path, &st)) == 0) {
+       if ((statret = do_stat(dest_path, &st)) == 0) {
                /* If the destination is a dir, enter it and use mode 1. */
                if (S_ISDIR(st.st_mode)) {
-                       if (sanitize_paths)
-                               die_on_unsafe_path(dest_path, 0);
                        if (!push_dir(dest_path, 0)) {
                                rsyserr(FERROR, errno, "push_dir#1 %s failed",
                                        full_fname(dest_path));
@@ -488,8 +488,6 @@ static char *get_local_name(struct file_list *flist, char *dest_path)
                        }
                        return NULL;
                }
-               if (sanitize_paths && S_ISLNK(st.st_mode))
-                       die_on_unsafe_path(dest_path, 0);
                if (flist->count > 1) {
                        rprintf(FERROR,
                                "ERROR: destination must be a directory when"
@@ -532,6 +530,8 @@ static char *get_local_name(struct file_list *flist, char *dest_path)
                        exit_cleanup(RERR_FILEIO);
                }
 
+               new_root_dir = 1;
+
                if (verbose)
                        rprintf(FINFO, "created directory %s\n", dest_path);
 
@@ -540,8 +540,6 @@ static char *get_local_name(struct file_list *flist, char *dest_path)
                        dry_run++;
                }
 
-               if (sanitize_paths)
-                       die_on_unsafe_path(dest_path, 0);
                if (!push_dir(dest_path, dry_run > 1)) {
                        rsyserr(FERROR, errno, "push_dir#2 %s failed",
                                full_fname(dest_path));
@@ -562,8 +560,6 @@ static char *get_local_name(struct file_list *flist, char *dest_path)
                dest_path = "/";
 
        *cp = '\0';
-       if (sanitize_paths)
-               die_on_unsafe_path(dest_path, 0);
        if (!push_dir(dest_path, 0)) {
                rsyserr(FERROR, errno, "push_dir#3 %s failed",
                        full_fname(dest_path));
@@ -579,17 +575,30 @@ static char *get_local_name(struct file_list *flist, char *dest_path)
  * mode.  We'll fix dirs that can be relative to the non-existent dir. */
 static void fix_basis_dirs(void)
 {
-       char **dir, *new;
+       char **dir, *new, *slash;
        int len;
 
+       if (dry_run <= 1)
+               return;
+
+       slash = strrchr(curr_dir, '/');
+
        for (dir = basis_dir; *dir; dir++) {
                if (**dir == '/')
                        continue;
                len = curr_dir_len + 1 + strlen(*dir) + 1;
                if (!(new = new_array(char, len)))
                        out_of_memory("fix_basis_dirs");
-               pathjoin(new, len, curr_dir, *dir);
-               clean_fname(new, 1);
+               if (slash && strncmp(*dir, "../", 3) == 0) {
+                   /* We want to remove only one leading "../" prefix for
+                    * the directory we couldn't create in dry-run mode:
+                    * this ensures that any other ".." references get
+                    * evaluated the same as they would for a live copy. */
+                   *slash = '\0';
+                   pathjoin(new, len, curr_dir, *dir + 3);
+                   *slash = '/';
+               } else
+                   pathjoin(new, len, curr_dir, *dir);
                *dir = new;
        }
 }
@@ -610,7 +619,7 @@ static void read_final_goodbye(int f_in, int f_out)
                }
        }
 
-       if (i != -1) {
+       if (i != NDX_DONE) {
                rprintf(FERROR, "Invalid packet at end of run (%d) [%s]\n",
                        i, who_am_i());
                exit_cleanup(RERR_PROTOCOL);
@@ -642,8 +651,6 @@ static void do_server_sender(int f_in, int f_out, int argc, char *argv[])
        }
 
        if (!relative_paths) {
-               if (sanitize_paths)
-                       die_on_unsafe_path(dir, 0);
                if (!push_dir(dir, 0)) {
                        rsyserr(FERROR, errno, "push_dir#3 %s failed",
                                full_fname(dir));
@@ -761,7 +768,7 @@ static int do_recv(int f_in,int f_out,struct file_list *flist,char *local_name)
        io_flush(FULL_FLUSH);
        if (protocol_version >= 24) {
                /* send a final goodbye message */
-               write_int(f_out, -1);
+               write_int(f_out, NDX_DONE);
        }
        io_flush(FULL_FLUSH);
 
@@ -838,17 +845,12 @@ static void do_server_recv(int f_in, int f_out, int argc,char *argv[])
                char **dir;
                for (dir = basis_dir; *dir; dir++) {
                        *dir = sanitize_path(NULL, *dir, NULL, curr_dir_depth, NULL);
-                       die_on_unsafe_path(*dir, 0);
                }
                if (partial_dir) {
                        partial_dir = sanitize_path(NULL, partial_dir, NULL, curr_dir_depth, NULL);
-                       /* A relative path gets this checked at every dir change. */
-                       if (*partial_dir == '/')
-                               die_on_unsafe_path(partial_dir, 0);
                }
        }
-       if (dry_run > 1)
-               fix_basis_dirs();
+       fix_basis_dirs();
 
        if (server_filter_list.head) {
                char **dir;
@@ -997,8 +999,7 @@ int client_run(int f_in, int f_out, pid_t pid, int argc, char *argv[])
        if (flist && flist->count > 0) {
                local_name = get_local_name(flist, argv[0]);
 
-               if (dry_run > 1)
-                       fix_basis_dirs();
+               fix_basis_dirs();
 
                exit_code2 = do_recv(f_in, f_out, flist, local_name);
        } else {
@@ -1258,8 +1259,8 @@ static RETSIGTYPE rsync_panic_handler(UNUSED(int whatsig))
        char cmd_buf[300];
        int ret;
 
-       sprintf(cmd_buf, get_panic_action(),
-               getpid(), getpid());
+       snprintf(cmd_buf, sizeof cmd_buf, get_panic_action(),
+                getpid(), getpid());
 
        /* Unless we failed to execute gdb, we allow the process to
         * continue.  I'm not sure if that's right. */