-static void clean_fname(char *name)
-{
- char *p;
- int l;
- int modified = 1;
-
- if (!name) return;
-
- while (modified) {
- modified = 0;
-
- if ((p=strstr(name,"/./"))) {
- modified = 1;
- while (*p) {
- p[0] = p[2];
- p++;
- }
- }
-
- if ((p=strstr(name,"//"))) {
- modified = 1;
- while (*p) {
- p[0] = p[1];
- p++;
- }
- }
-
- if (strncmp(p=name,"./",2) == 0) {
- modified = 1;
- do {
- p[0] = p[2];
- } while (*p++);
- }
-
- l = strlen(p=name);
- if (l > 1 && p[l-1] == '/') {
- modified = 1;
- p[l-1] = 0;
- }
- }
-}
-
-
-
-void send_file_entry(struct file_struct *file,int f,unsigned base_flags)
-{
- unsigned char flags;
- static time_t last_time;
- static mode_t last_mode;
- static dev_t last_rdev;
- static uid_t last_uid;
- static gid_t last_gid;
- static char lastname[MAXPATHLEN];
- char *fname;
- int l1,l2;
-
- if (f == -1) return;
-
- if (!file) {
- write_byte(f,0);
- return;
- }
-
- fname = f_name(file);
-
- flags = base_flags;
-
- if (file->mode == last_mode) flags |= SAME_MODE;
- if (file->rdev == last_rdev) flags |= SAME_RDEV;
- if (file->uid == last_uid) flags |= SAME_UID;
- if (file->gid == last_gid) flags |= SAME_GID;
- if (file->modtime == last_time) flags |= SAME_TIME;
-
- for (l1=0;lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);l1++) ;
- l2 = strlen(fname) - l1;
-
- if (l1 > 0) flags |= SAME_NAME;
- if (l2 > 255) flags |= LONG_NAME;
-
- write_byte(f,flags);
- if (flags & SAME_NAME)
- write_byte(f,l1);
- if (flags & LONG_NAME)
- write_int(f,l2);
- else
- write_byte(f,l2);
- write_buf(f,fname+l1,l2);
-
- write_longint(f,file->length);
- if (!(flags & SAME_TIME))
- write_int(f,(int)file->modtime);
- if (!(flags & SAME_MODE))
- write_int(f,(int)file->mode);
- if (preserve_uid && !(flags & SAME_UID)) {
- add_uid(file->uid);
- write_int(f,(int)file->uid);
- }
- if (preserve_gid && !(flags & SAME_GID)) {
- add_gid(file->gid);
- write_int(f,(int)file->gid);
- }
- if (preserve_devices && IS_DEVICE(file->mode) && !(flags & SAME_RDEV))
- write_int(f,(int)file->rdev);
-
-#if SUPPORT_LINKS
- if (preserve_links && S_ISLNK(file->mode)) {
- write_int(f,strlen(file->link));
- write_buf(f,file->link,strlen(file->link));
- }
+#ifdef SUPPORT_LINKS
+ if (preserve_links && S_ISLNK(mode)) {
+ int len = strlen(file->u.link);
+ write_int(f, len);
+ write_buf(f, file->u.link, len);
+ }
+#endif
+
+#ifdef SUPPORT_HARD_LINKS
+ if (flags & XMIT_HAS_IDEV_DATA) {
+ if (protocol_version < 26) {
+ /* 32-bit dev_t and ino_t */
+ write_int(f, dev);
+ write_int(f, file->F_INODE);
+ } else {
+ /* 64-bit dev_t and ino_t */
+ if (!(flags & XMIT_SAME_DEV))
+ write_longint(f, dev);
+ write_longint(f, file->F_INODE);
+ }
+ }
+#endif
+
+ if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
+ char *sum;
+ if (S_ISREG(mode))
+ sum = file->u.sum;
+ else {
+ /* Prior to 28, we sent a useless set of nulls. */
+ sum = empty_sum;
+ }
+ write_buf(f, sum, checksum_len);
+ }
+
+ strlcpy(lastname, fname, MAXPATHLEN);
+}
+
+static struct file_struct *receive_file_entry(struct file_list *flist,
+ unsigned short flags, int f)
+{
+ static time_t modtime;
+ static mode_t mode;
+ static int64 dev;
+ static dev_t rdev;
+ static uint32 rdev_major;
+ static uid_t uid;
+ static gid_t gid;
+ static char lastname[MAXPATHLEN], *lastdir;
+ static int lastdir_depth, lastdir_len = -1;
+ static unsigned int del_hier_name_len = 0;
+ static int in_del_hier = 0;
+ char thisname[MAXPATHLEN];
+ unsigned int l1 = 0, l2 = 0;
+ int alloc_len, basename_len, dirname_len, linkname_len, sum_len;
+ OFF_T file_length;
+ char *basename, *dirname, *bp;
+ struct file_struct *file;
+
+ if (!flist) {
+ modtime = 0, mode = 0;
+ dev = 0, rdev = makedev(0, 0);
+ rdev_major = 0;
+ uid = 0, gid = 0;
+ *lastname = '\0';
+ lastdir_len = -1;
+ in_del_hier = 0;
+ return NULL;
+ }
+
+ if (flags & XMIT_SAME_NAME)
+ l1 = read_byte(f);
+
+ if (flags & XMIT_LONG_NAME)
+ l2 = read_int(f);
+ else
+ l2 = read_byte(f);
+
+ if (l2 >= MAXPATHLEN - l1) {
+ rprintf(FERROR,
+ "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
+ flags, l1, l2, lastname);
+ overflow_exit("receive_file_entry");
+ }
+
+ strlcpy(thisname, lastname, l1 + 1);
+ read_sbuf(f, &thisname[l1], l2);
+ thisname[l1 + l2] = 0;
+
+ strlcpy(lastname, thisname, MAXPATHLEN);
+
+ clean_fname(thisname, 0);
+
+ if (sanitize_paths)
+ sanitize_path(thisname, thisname, "", 0);
+
+ if ((basename = strrchr(thisname, '/')) != NULL) {
+ dirname_len = ++basename - thisname; /* counts future '\0' */
+ if (lastdir_len == dirname_len - 1
+ && strncmp(thisname, lastdir, lastdir_len) == 0) {
+ dirname = lastdir;
+ dirname_len = 0; /* indicates no copy is needed */
+ } else
+ dirname = thisname;
+ } else {
+ basename = thisname;
+ dirname = NULL;
+ dirname_len = 0;
+ }
+ basename_len = strlen(basename) + 1; /* count the '\0' */
+
+ file_length = read_longint(f);
+ if (!(flags & XMIT_SAME_TIME))
+ modtime = (time_t)read_int(f);
+ if (!(flags & XMIT_SAME_MODE))
+ mode = from_wire_mode(read_int(f));
+
+ if (chmod_modes && !S_ISLNK(mode))
+ mode = tweak_mode(mode, chmod_modes);
+
+ if (preserve_uid && !(flags & XMIT_SAME_UID))
+ uid = (uid_t)read_int(f);
+ if (preserve_gid && !(flags & XMIT_SAME_GID))
+ gid = (gid_t)read_int(f);
+
+ if ((preserve_devices && IS_DEVICE(mode))
+ || (preserve_specials && IS_SPECIAL(mode))) {
+ if (protocol_version < 28) {
+ if (!(flags & XMIT_SAME_RDEV_pre28))
+ rdev = (dev_t)read_int(f);
+ } else {
+ uint32 rdev_minor;
+ if (!(flags & XMIT_SAME_RDEV_MAJOR))
+ rdev_major = read_int(f);
+ if (flags & XMIT_RDEV_MINOR_IS_SMALL)
+ rdev_minor = read_byte(f);
+ else
+ rdev_minor = read_int(f);
+ rdev = makedev(rdev_major, rdev_minor);
+ }
+ } else if (protocol_version < 28)
+ rdev = makedev(0, 0);
+
+#ifdef SUPPORT_LINKS
+ if (preserve_links && S_ISLNK(mode)) {
+ linkname_len = read_int(f) + 1; /* count the '\0' */
+ if (linkname_len <= 0 || linkname_len > MAXPATHLEN) {
+ rprintf(FERROR, "overflow: linkname_len=%d\n",
+ linkname_len - 1);
+ overflow_exit("receive_file_entry");
+ }
+ }
+ else