Changed the rdev code to have both an "rdev" variable (which always
[rsync/rsync.git] / flist.c
diff --git a/flist.c b/flist.c
index c4459b1..69594cc 100644 (file)
--- a/flist.c
+++ b/flist.c
@@ -361,7 +361,7 @@ void send_file_entry(struct file_struct *file, int f, unsigned short base_flags)
        unsigned short flags;
        static time_t modtime;
        static mode_t mode;
-       static DEV64_T rdev;    /* just high bytes in p28 onward */
+       static DEV64_T rdev, rdev_high;
        static DEV64_T dev;
        static uid_t uid;
        static gid_t gid;
@@ -375,7 +375,7 @@ void send_file_entry(struct file_struct *file, int f, unsigned short base_flags)
        if (!file) {
                write_byte(f, 0);
                modtime = 0, mode = 0;
-               rdev = 0, dev = 0;
+               rdev = 0, rdev_high = 0, dev = 0;
                uid = 0, gid = 0;
                *lastname = '\0';
                return;
@@ -404,10 +404,12 @@ void send_file_entry(struct file_struct *file, int f, unsigned short base_flags)
                        } else
                                rdev = 0;
                } else if (IS_DEVICE(mode)) {
-                       if ((file->u.rdev & ~0xFF) == rdev)
+                       if ((file->u.rdev & ~0xFF) == rdev_high)
                                flags |= XMIT_SAME_HIGH_RDEV;
-                       else
-                               rdev = file->u.rdev & ~0xFF;
+                       else {
+                               rdev = file->u.rdev;
+                               rdev_high = rdev & ~0xFF;
+                       }
                }
        }
        if (file->uid == uid)
@@ -441,19 +443,23 @@ void send_file_entry(struct file_struct *file, int f, unsigned short base_flags)
        if (l2 > 255)
                flags |= XMIT_LONG_NAME;
 
-       /* We must make sure we don't send a zero flags byte or
-        * the other end will terminate the flist transfer. */
-       if (flags == 0 && !S_ISDIR(mode))
-               flags |= XMIT_TOP_DIR; /* NOTE: no meaning for non-dir */
+       /* We must make sure we don't send a zero flag byte or the
+        * other end will terminate the flist transfer.  Note that
+        * the use of XMIT_TOP_DIR on a non-dir has no meaning, so
+        * it's harmless way to add a bit to the first flag byte. */
        if (protocol_version >= 28) {
-               if ((flags & 0xFF00) || flags == 0) {
+               if (!flags && !S_ISDIR(mode))
+                       flags |= XMIT_TOP_DIR;
+               if ((flags & 0xFF00) || !flags) {
                        flags |= XMIT_EXTENDED_FLAGS;
                        write_byte(f, flags);
                        write_byte(f, flags >> 8);
                } else
                        write_byte(f, flags);
        } else {
-               if (flags == 0)
+               if (!(flags & 0xFF) && !S_ISDIR(mode))
+                       flags |= XMIT_TOP_DIR;
+               if (!(flags & 0xFF))
                        flags |= XMIT_LONG_NAME;
                write_byte(f, flags);
        }
@@ -480,18 +486,18 @@ void send_file_entry(struct file_struct *file, int f, unsigned short base_flags)
        }
        if (preserve_devices && IS_DEVICE(mode)) {
                /* If XMIT_SAME_HIGH_RDEV is off, XMIT_SAME_RDEV_pre28 is
-                * also off.  Also, avoid using "rdev" because it may be
-                * incomplete. */
+                * also off. */
                if (!(flags & XMIT_SAME_HIGH_RDEV))
-                       write_int(f, file->u.rdev);
+                       write_int(f, rdev);
                else if (protocol_version >= 28)
-                       write_byte(f, file->u.rdev);
+                       write_byte(f, rdev);
        }
 
 #if SUPPORT_LINKS
        if (preserve_links && S_ISLNK(mode)) {
-               write_int(f, strlen(file->u.link));
-               write_buf(f, file->u.link, strlen(file->u.link));
+               int len = strlen(file->u.link);
+               write_int(f, len);
+               write_buf(f, file->u.link, len);
        }
 #endif
 
@@ -536,7 +542,7 @@ void receive_file_entry(struct file_struct **fptr, unsigned short flags, int f)
 {
        static time_t modtime;
        static mode_t mode;
-       static DEV64_T rdev;    /* just high bytes in p28 onward */
+       static DEV64_T rdev, rdev_high;
        static DEV64_T dev;
        static uid_t uid;
        static gid_t gid;
@@ -548,7 +554,7 @@ void receive_file_entry(struct file_struct **fptr, unsigned short flags, int f)
 
        if (!fptr) {
                modtime = 0, mode = 0;
-               rdev = 0, dev = 0;
+               rdev = 0, rdev_high = 0, dev = 0;
                uid = 0, gid = 0;
                *lastname = '\0';
                return;
@@ -635,22 +641,23 @@ void receive_file_entry(struct file_struct **fptr, unsigned short flags, int f)
                                rdev = 0;
                } else if (IS_DEVICE(mode)) {
                        if (!(flags & XMIT_SAME_HIGH_RDEV)) {
-                               file->u.rdev = (DEV64_T)read_int(f);
-                               rdev = file->u.rdev & ~0xFF;
+                               rdev = (DEV64_T)read_int(f);
+                               rdev_high = rdev & ~0xFF;
                        } else
-                               file->u.rdev = rdev | (DEV64_T)read_byte(f);
+                               rdev = rdev_high | (DEV64_T)read_byte(f);
+                       file->u.rdev = rdev;
                }
        }
 
        if (preserve_links && S_ISLNK(mode)) {
-               int l = read_int(f);
-               if (l < 0) {
-                       rprintf(FERROR, "overflow: l=%d\n", l);
+               int len = read_int(f);
+               if (len < 0 || len >= MAXPATHLEN) {
+                       rprintf(FERROR, "overflow: len=%d\n", len);
                        overflow("receive_file_entry");
                }
-               if (!(file->u.link = new_array(char, l + 1)))
+               if (!(file->u.link = new_array(char, len + 1)))
                        out_of_memory("receive_file_entry 2");
-               read_sbuf(f, file->u.link, l);
+               read_sbuf(f, file->u.link, len);
                if (sanitize_paths)
                        sanitize_path(file->u.link, file->dirname);
        }
@@ -725,7 +732,7 @@ struct file_struct *make_file(char *fname, struct string_area **ap,
        char sum[SUM_LENGTH];
        char *p;
        char thisname[MAXPATHLEN];
-       char linkbuf[MAXPATHLEN];
+       char linkname[MAXPATHLEN];
        unsigned short flags = 0;
 
        if (strlcpy(thisname, fname, sizeof thisname)
@@ -739,7 +746,7 @@ struct file_struct *make_file(char *fname, struct string_area **ap,
 
        memset(sum, 0, SUM_LENGTH);
 
-       if (readlink_stat(thisname, &st, linkbuf) != 0) {
+       if (readlink_stat(thisname, &st, linkname) != 0) {
                int save_errno = errno;
                if (errno == ENOENT && exclude_level != NO_EXCLUDES) {
                        /* either symlink pointing nowhere or file that
@@ -823,13 +830,13 @@ struct file_struct *make_file(char *fname, struct string_area **ap,
                }
        }
 #ifdef HAVE_STRUCT_STAT_ST_RDEV
-       if (IS_DEVICE(st.st_mode))
+       if (preserve_devices && IS_DEVICE(st.st_mode))
                file->u.rdev = st.st_rdev;
 #endif
 
 #if SUPPORT_LINKS
        if (S_ISLNK(st.st_mode))
-               file->u.link = STRDUP(ap, linkbuf);
+               file->u.link = STRDUP(ap, linkname);
 #endif
 
        if (always_checksum && S_ISREG(st.st_mode)) {