+ write_varlong30(f, F_LENGTH(file), 3);
+ if (!(flags & XMIT_SAME_TIME)) {
+ if (protocol_version >= 30)
+ write_varlong(f, modtime, 4);
+ else
+ write_int(f, modtime);
+ }
+ if (!(flags & XMIT_SAME_MODE))
+ write_int(f, to_wire_mode(mode));
+ if (preserve_uid && !(flags & XMIT_SAME_UID)) {
+ if (protocol_version < 30)
+ write_int(f, uid);
+ else {
+ write_varint(f, uid);
+ if (flags & XMIT_USER_NAME_FOLLOWS) {
+ int len = strlen(user_name);
+ write_byte(f, len);
+ write_buf(f, user_name, len);
+ }
+ }
+ }
+ if (preserve_gid && !(flags & XMIT_SAME_GID)) {
+ if (protocol_version < 30)
+ write_int(f, gid);
+ else {
+ write_varint(f, gid);
+ if (flags & XMIT_GROUP_NAME_FOLLOWS) {
+ int len = strlen(group_name);
+ write_byte(f, len);
+ write_buf(f, group_name, len);
+ }
+ }
+ }
+ if ((preserve_devices && IS_DEVICE(mode))
+ || (preserve_specials && IS_SPECIAL(mode))) {
+ if (protocol_version < 28) {
+ if (!(flags & XMIT_SAME_RDEV_pre28))
+ write_int(f, (int)rdev);
+ } else {
+ if (!(flags & XMIT_SAME_RDEV_MAJOR))
+ write_varint30(f, major(rdev));
+ if (protocol_version >= 30)
+ write_varint(f, minor(rdev));
+ else if (flags & XMIT_RDEV_MINOR_8_pre30)
+ write_byte(f, minor(rdev));
+ else
+ write_int(f, minor(rdev));
+ }
+ }
+
+#ifdef SUPPORT_LINKS
+ if (preserve_links && S_ISLNK(mode)) {
+ const char *sl = F_SYMLINK(file);
+ int len = strlen(sl);
+ write_varint30(f, len);
+ write_buf(f, sl, len);
+ }
+#endif
+
+#ifdef SUPPORT_HARD_LINKS
+ if (tmp_dev != 0 && protocol_version < 30) {
+ if (protocol_version < 26) {
+ /* 32-bit dev_t and ino_t */
+ write_int(f, (int32)dev);
+ write_int(f, (int32)tmp_ino);
+ } else {
+ /* 64-bit dev_t and ino_t */
+ if (!(flags & XMIT_SAME_DEV_pre30))
+ write_longint(f, dev);
+ write_longint(f, tmp_ino);
+ }
+ }
+#endif
+
+ if (always_checksum && (S_ISREG(mode) || protocol_version < 28)) {
+ const char *sum;
+ if (S_ISREG(mode))
+ sum = tmp_sum;
+ else {
+ /* Prior to 28, we sent a useless set of nulls. */
+ sum = empty_sum;
+ }
+ write_buf(f, sum, checksum_len);
+ }
+
+ the_end:
+ strlcpy(lastname, fname, MAXPATHLEN);
+
+ if (S_ISREG(mode) || S_ISLNK(mode))
+ stats.total_size += F_LENGTH(file);
+}
+
+static struct file_struct *recv_file_entry(struct file_list *flist,
+ int xflags, int f)
+{
+ static int64 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 uint16 gid_flags;
+ 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, linkname_len;
+ int extra_len = file_extra_cnt * EXTRA_LEN;
+ int first_hlink_ndx = -1;
+ OFF_T file_length;
+ const char *basename;
+ struct file_struct *file;
+ alloc_pool_t *pool;
+ char *bp;
+
+ if (xflags & XMIT_SAME_NAME)
+ l1 = read_byte(f);
+
+ if (xflags & XMIT_LONG_NAME)
+ l2 = read_varint30(f);
+ else
+ l2 = read_byte(f);
+
+ if (l2 >= MAXPATHLEN - l1) {
+ rprintf(FERROR,
+ "overflow: xflags=0x%x l1=%d l2=%d lastname=%s [%s]\n",
+ xflags, l1, l2, lastname, who_am_i());
+ overflow_exit("recv_file_entry");
+ }
+
+ strlcpy(thisname, lastname, l1 + 1);
+ read_sbuf(f, &thisname[l1], l2);
+ thisname[l1 + l2] = 0;
+
+ /* Abuse basename_len for a moment... */
+ basename_len = strlcpy(lastname, thisname, MAXPATHLEN);
+
+#ifdef ICONV_OPTION
+ if (ic_recv != (iconv_t)-1) {
+ char *obuf = thisname;
+ ICONV_CONST char *ibuf = (ICONV_CONST char *)lastname;
+ size_t ocnt = MAXPATHLEN, icnt = basename_len;
+
+ if (icnt >= MAXPATHLEN) {
+ errno = E2BIG;
+ goto convert_error;
+ }
+
+ iconv(ic_recv, NULL,0, NULL,0);
+ if (iconv(ic_recv, &ibuf,&icnt, &obuf,&ocnt) == (size_t)-1) {
+ convert_error:
+ io_error |= IOERR_GENERAL;
+ rprintf(FINFO,
+ "[%s] cannot convert filename: %s (%s)\n",
+ who_am_i(), lastname, strerror(errno));
+ obuf = thisname;
+ }
+ *obuf = '\0';
+ }
+#endif
+
+ clean_fname(thisname, 0);
+
+ if (sanitize_paths)
+ sanitize_path(thisname, thisname, "", 0, NULL);
+
+ if ((basename = strrchr(thisname, '/')) != NULL) {
+ int len = basename++ - thisname;
+ if (len != lastdir_len || memcmp(thisname, lastdir, len) != 0) {
+ lastdir = new_array(char, len + 1);
+ memcpy(lastdir, thisname, len);
+ lastdir[len] = '\0';
+ lastdir_len = len;
+ lastdir_depth = count_dir_elements(lastdir);
+ }
+ } else
+ basename = thisname;
+ basename_len = strlen(basename) + 1; /* count the '\0' */
+
+#ifdef SUPPORT_HARD_LINKS
+ if (protocol_version >= 30
+ && BITS_SETnUNSET(xflags, XMIT_HLINKED, XMIT_HLINK_FIRST)) {
+ struct file_struct *first;
+ first_hlink_ndx = read_varint30(f);
+ if (first_hlink_ndx < 0 || first_hlink_ndx >= flist->count) {
+ rprintf(FERROR,
+ "hard-link reference out of range: %d (%d)\n",
+ first_hlink_ndx, flist->count);
+ exit_cleanup(RERR_PROTOCOL);