make repeated --fuzzy option look into alt-dest dirs.
[rsync.git] / main.c
diff --git a/main.c b/main.c
index 664129161baba58ed4be9bee7fd650e57e5cad82..93cd50d3938df666e004dfedc48af54bcb2d0060 100644 (file)
--- a/main.c
+++ b/main.c
@@ -70,11 +70,13 @@ extern int sock_f_out;
 extern int filesfrom_fd;
 extern int connect_timeout;
 extern int send_msgs_to_gen;
+extern dev_t filesystem_dev;
 extern pid_t cleanup_child_pid;
 extern size_t bwlimit_writemax;
 extern unsigned int module_dirlen;
 extern BOOL flist_receiving_enabled;
 extern BOOL shutting_down;
+extern int basis_dir_cnt;
 extern struct stats stats;
 extern char *stdout_format;
 extern char *logfile_format;
@@ -130,7 +132,7 @@ static void show_malloc_stats(void);
 pid_t wait_process(pid_t pid, int *status_ptr, int flags)
 {
        pid_t waited_pid;
-       
+
        do {
                waited_pid = waitpid(pid, status_ptr, flags);
        } while (waited_pid == -1 && errno == EINTR);
@@ -363,7 +365,7 @@ static void show_malloc_stats(void)
 
        rprintf(FCLIENT, "\n");
        rprintf(FINFO, RSYNC_NAME "[%d] (%s%s%s) heap statistics:\n",
-               getpid(), am_server ? "server " : "",
+               (int)getpid(), am_server ? "server " : "",
                am_daemon ? "daemon " : "", who_am_i());
        rprintf(FINFO, "  arena:     %10ld   (bytes from sbrk)\n",
                (long)mi.arena);
@@ -493,7 +495,11 @@ static pid_t do_cmd(char *cmd, char *machine, char *user, char **remote_argv, in
                                rprintf(FERROR, "internal: args[] overflowed in do_cmd()\n");
                                exit_cleanup(RERR_SYNTAX);
                        }
-                       args[argc++] = *remote_argv++;
+                       if (**remote_argv == '-') {
+                               if (asprintf(args + argc++, "./%s", *remote_argv++) < 0)
+                                       out_of_memory("do_cmd");
+                       } else
+                               args[argc++] = *remote_argv++;
                        remote_argc--;
                }
        }
@@ -576,6 +582,10 @@ static char *get_local_name(struct file_list *flist, char *dest_path)
        if (!dest_path || list_only)
                return NULL;
 
+       /* Treat an empty string as a copy into the current directory. */
+       if (!*dest_path)
+           dest_path = ".";
+
        if (daemon_filter_list.head) {
                char *slash = strrchr(dest_path, '/');
                if (slash && (slash[1] == '\0' || (slash[1] == '.' && slash[2] == '\0')))
@@ -585,7 +595,7 @@ static char *get_local_name(struct file_list *flist, char *dest_path)
                if ((*dest_path != '.' || dest_path[1] != '\0')
                 && (check_filter(&daemon_filter_list, FLOG, dest_path, 0) < 0
                  || check_filter(&daemon_filter_list, FLOG, dest_path, 1) < 0)) {
-                       rprintf(FERROR, "skipping daemon-excluded destination \"%s\"\n",
+                       rprintf(FERROR, "ERROR: daemon has excluded destination \"%s\"\n",
                                dest_path);
                        exit_cleanup(RERR_FILESELECT);
                }
@@ -602,6 +612,7 @@ static char *get_local_name(struct file_list *flist, char *dest_path)
                                        full_fname(dest_path));
                                exit_cleanup(RERR_FILESELECT);
                        }
+                       filesystem_dev = st.st_dev; /* ensures --force works right w/-x */
                        return NULL;
                }
                if (file_total > 1) {
@@ -639,7 +650,7 @@ static char *get_local_name(struct file_list *flist, char *dest_path)
                        exit_cleanup(RERR_SYNTAX);
                }
 
-               if (mkdir_defmode(dest_path) != 0) {
+               if (do_mkdir(dest_path, ACCESSPERMS) != 0) {
                        rsyserr(FERROR, errno, "mkdir %s failed",
                                full_fname(dest_path));
                        exit_cleanup(RERR_FILEIO);
@@ -695,33 +706,35 @@ static char *get_local_name(struct file_list *flist, char *dest_path)
 static void check_alt_basis_dirs(void)
 {
        STRUCT_STAT st;
-       char **dir_p, *slash = strrchr(curr_dir, '/');
-
-       for (dir_p = basis_dir; *dir_p; dir_p++) {
-               if (dry_run > 1 && **dir_p != '/') {
-                       int len = curr_dir_len + 1 + strlen(*dir_p) + 1;
+       char *slash = strrchr(curr_dir, '/');
+       int j;
+
+       for (j = 0; j < basis_dir_cnt; j++) {
+               char *bdir = basis_dir[j];
+               int bd_len = strlen(bdir);
+               if (bd_len > 1 && bdir[bd_len-1] == '/')
+                       bdir[--bd_len] = '\0';
+               if (dry_run > 1 && *bdir != '/') {
+                       int len = curr_dir_len + 1 + bd_len + 1;
                        char *new = new_array(char, len);
                        if (!new)
                                out_of_memory("check_alt_basis_dirs");
-                       if (slash && strncmp(*dir_p, "../", 3) == 0) {
+                       if (slash && strncmp(bdir, "../", 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_p + 3);
+                           pathjoin(new, len, curr_dir, bdir + 3);
                            *slash = '/';
                        } else
-                           pathjoin(new, len, curr_dir, *dir_p);
-                       *dir_p = new;
-               }
-               if (do_stat(*dir_p, &st) < 0) {
-                       rprintf(FWARNING, "%s arg does not exist: %s\n",
-                               dest_option, *dir_p);
-               } else if (!S_ISDIR(st.st_mode)) {
-                       rprintf(FWARNING, "%s arg is not a dir: %s\n",
-                               dest_option, *dir_p);
+                           pathjoin(new, len, curr_dir, bdir);
+                       basis_dir[j] = bdir = new;
                }
+               if (do_stat(bdir, &st) < 0)
+                       rprintf(FWARNING, "%s arg does not exist: %s\n", dest_option, bdir);
+               else if (!S_ISDIR(st.st_mode))
+                       rprintf(FWARNING, "%s arg is not a dir: %s\n", dest_option, bdir);
        }
 }
 
@@ -764,10 +777,8 @@ static void do_server_sender(int f_in, int f_out, int argc, char *argv[])
        struct file_list *flist;
        char *dir = argv[0];
 
-       if (DEBUG_GTE(SEND, 1)) {
-               rprintf(FINFO, "server_sender starting pid=%ld\n",
-                       (long)getpid());
-       }
+       if (DEBUG_GTE(SEND, 1))
+               rprintf(FINFO, "server_sender starting pid=%d\n", (int)getpid());
 
        if (am_daemon && lp_write_only(module_id)) {
                rprintf(FERROR, "ERROR: module is write only\n");
@@ -799,8 +810,12 @@ static void do_server_sender(int f_in, int f_out, int argc, char *argv[])
        }
 
        flist = send_file_list(f_out,argc,argv);
-       if (!flist || flist->used == 0)
+       if (!flist || flist->used == 0) {
+               /* Make sure input buffering is off so we can't hang in noop_io_until_death(). */
+               io_end_buffering_in(0);
+               /* TODO:  we should really exit in a more controlled manner. */
                exit_cleanup(0);
+       }
 
        io_start_buffering_in(f_in);
 
@@ -958,10 +973,8 @@ static void do_server_recv(int f_in, int f_out, int argc, char *argv[])
        } else
                negated_levels = 0;
 
-       if (DEBUG_GTE(RECV, 1)) {
-               rprintf(FINFO, "server_recv(%d) starting pid=%ld\n",
-                       argc, (long)getpid());
-       }
+       if (DEBUG_GTE(RECV, 1))
+               rprintf(FINFO, "server_recv(%d) starting pid=%d\n", argc, (int)getpid());
 
        if (am_daemon && read_only) {
                rprintf(FERROR,"ERROR: module is read only\n");
@@ -1303,6 +1316,9 @@ static int start_client(int argc, char *argv[])
                remote_argc = argc = 1;
        }
 
+       if (!rsync_port && remote_argc && !**remote_argv) /* Turn an empty arg into a dot dir. */
+               *remote_argv = ".";
+
        if (am_sender) {
                char *dummy_host;
                int dummy_port = rsync_port;
@@ -1338,6 +1354,8 @@ static int start_client(int argc, char *argv[])
                                        rprintf(FERROR, "All source args must use the same port number.\n");
                                exit_cleanup(RERR_SYNTAX);
                        }
+                       if (!rsync_port && !*arg) /* Turn an empty arg into a dot dir. */
+                               arg = ".";
                        remote_argv[i] = arg;
                }
        }
@@ -1472,10 +1490,9 @@ const char *get_panic_action(void)
 static RETSIGTYPE rsync_panic_handler(UNUSED(int whatsig))
 {
        char cmd_buf[300];
-       int ret;
+       int ret, pid_int = getpid();
 
-       snprintf(cmd_buf, sizeof cmd_buf, get_panic_action(),
-                getpid(), getpid());
+       snprintf(cmd_buf, sizeof cmd_buf, get_panic_action(), pid_int, pid_int);
 
        /* Unless we failed to execute gdb, we allow the process to
         * continue.  I'm not sure if that's right. */