A slight improvement to the logic in try_dests_non().
[rsync/rsync.git] / generator.c
index fb79b2d..3eef9e5 100644 (file)
@@ -94,8 +94,6 @@ extern struct file_list *the_file_list;
 extern struct filter_list_struct server_filter_list;
 
 static int deletion_count = 0; /* used to implement --max-delete */
-static int can_link_symlinks = 1; /* start out optimistic */
-static int can_link_devices = 1;
 
 /* For calling delete_file() */
 #define DEL_FORCE_RECURSE      (1<<1) /* recurse even w/o --force */
@@ -702,34 +700,47 @@ static int try_dests_reg(struct file_struct *file, char *fname, int ndx,
 /* This is only called for non-regular files.  We return -2 if we've finished
  * handling the file, or -1 if no dest-linking occurred. */
 static int try_dests_non(struct file_struct *file, char *fname, int ndx,
-                        int itemizing, int *possible_ptr,
-                        int maybe_ATTRS_REPORT, enum logcode code)
+                        int itemizing, int maybe_ATTRS_REPORT,
+                        enum logcode code)
 {
-       char fnamebuf[MAXPATHLEN], lnk[MAXPATHLEN];
+       char fnamebuf[MAXPATHLEN];
        STRUCT_STAT st;
-       int len, i = 0;
+       int i = 0;
 
        do {
                pathjoin(fnamebuf, MAXPATHLEN, basis_dir[i], fname);
                if (link_stat(fnamebuf, &st, 0) < 0 || S_ISDIR(st.st_mode)
                 || !unchanged_attrs(file, &st))
                        continue;
-               if (S_ISLNK(file->mode)) {
+               if (IS_DEVICE(file->mode)) {
+                       if (!IS_DEVICE(st.st_mode) || st.st_rdev != file->u.rdev)
+                               continue;
+               } else if (IS_SPECIAL(file->mode)) {
+                       if (!IS_SPECIAL(st.st_mode) || st.st_rdev != file->u.rdev)
+                               continue;
+#ifdef CAN_HARDLINK_SYMLINK
+               } else if (S_ISLNK(file->mode)) {
 #ifdef SUPPORT_LINKS
+                       char lnk[MAXPATHLEN];
+                       int len;
                        if ((len = readlink(fnamebuf, lnk, MAXPATHLEN-1)) <= 0)
                                continue;
                        lnk[len] = '\0';
                        if (strcmp(lnk, file->u.link) != 0)
 #endif
                                continue;
+#endif
                } else {
-                       if (!IS_DEVICE(st.st_mode) || st.st_rdev != file->u.rdev)
-                               continue;
+                       rprintf(FERROR,
+                               "internal: try_dests_non() called with invalid mode (%o)\n",
+                               file->mode);
+                       exit_cleanup(RERR_UNSUPPORTED);
                }
                if (link_dest) {
                        if (do_link(fnamebuf, fname) < 0) {
-                               /* TODO improve this to be based on errno? */
-                               *possible_ptr = 0;
+                               rsyserr(FERROR, errno,
+                                       "failed to hard-link %s with %s",
+                                       fnamebuf, fname);
                                break;
                        }
                        if (preserve_hard_links && file->link_u.links)
@@ -960,14 +971,15 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
                                return;
                        if (!S_ISLNK(st.st_mode))
                                statret = -1;
-               } else if (basis_dir[0] != NULL && can_link_symlinks) {
+#ifdef CAN_HARDLINK_SYMLINK
+               } else if (basis_dir[0] != NULL) {
                        if (try_dests_non(file, fname, ndx, itemizing,
-                                         &can_link_symlinks,
                                          maybe_ATTRS_REPORT, code) == -2) {
                                if (!copy_dest)
                                        return;
                                itemizing = code = 0;
                        }
+#endif
                }
                if (preserve_hard_links && file->link_u.links
                    && hard_link_check(file, ndx, fname, -1, &st,
@@ -1000,16 +1012,16 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
 
        if ((am_root && preserve_devices && IS_DEVICE(file->mode))
         || (preserve_specials && IS_SPECIAL(file->mode))) {
-               if (statret != 0
-                && (basis_dir[0] != NULL && can_link_devices)) {
+#ifdef CAN_HARDLINK_SPECIAL
+               if (statret != 0 && basis_dir[0] != NULL) {
                        if (try_dests_non(file, fname, ndx, itemizing,
-                                         &can_link_devices,
                                          maybe_ATTRS_REPORT, code) == -2) {
                                if (!copy_dest)
                                        return;
                                itemizing = code = 0;
                        }
                }
+#endif
                if (statret != 0
                 || (st.st_mode & ~CHMOD_BITS) != (file->mode & ~CHMOD_BITS)
                 || st.st_rdev != file->u.rdev) {