+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 dev;
+ static uid_t uid;
+ static gid_t gid;
+ static char lastname[MAXPATHLEN];
+ char thisname[MAXPATHLEN];
+ unsigned int l1 = 0, l2 = 0;
+ char *p;
+ struct file_struct *file;
+
+ if (!fptr) {
+ modtime = 0, mode = 0;
+ rdev = 0, dev = 0;
+ uid = 0, gid = 0;
+ *lastname = '\0';
+ return;
+ }
+
+ if (flags & SAME_NAME)
+ l1 = read_byte(f);
+
+ if (flags & LONG_NAME)
+ l2 = read_int(f);
+ else
+ l2 = read_byte(f);
+
+ file = new(struct file_struct);
+ if (!file)
+ out_of_memory("receive_file_entry");
+ memset((char *) file, 0, sizeof(*file));
+ (*fptr) = file;
+
+ if (l2 >= MAXPATHLEN - l1) {
+ rprintf(FERROR,
+ "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
+ flags, l1, l2, lastname);
+ overflow("receive_file_entry");
+ }
+
+ strlcpy(thisname, lastname, l1 + 1);
+ read_sbuf(f, &thisname[l1], l2);
+ thisname[l1 + l2] = 0;
+
+ strlcpy(lastname, thisname, MAXPATHLEN);
+ lastname[MAXPATHLEN - 1] = 0;
+
+ clean_fname(thisname);
+
+ if (sanitize_paths) {
+ sanitize_path(thisname, NULL);
+ }
+
+ if ((p = strrchr(thisname, '/'))) {
+ static char *lastdir;
+ *p = 0;
+ if (lastdir && strcmp(thisname, lastdir) == 0)
+ file->dirname = lastdir;
+ else {
+ file->dirname = strdup(thisname);
+ lastdir = file->dirname;
+ }
+ file->basename = strdup(p + 1);
+ } else {
+ file->dirname = NULL;
+ file->basename = strdup(thisname);
+ }
+
+ if (!file->basename)
+ out_of_memory("receive_file_entry 1");
+
+ file->flags = flags;
+ file->length = read_longint(f);
+ if (!(flags & SAME_TIME))
+ modtime = (time_t)read_int(f);
+ file->modtime = modtime;
+ if (!(flags & SAME_MODE))
+ mode = from_wire_mode(read_int(f));
+ file->mode = mode;
+
+ if (preserve_uid) {
+ if (!(flags & SAME_UID))
+ uid = (uid_t)read_int(f);
+ file->uid = uid;
+ }
+ if (preserve_gid) {
+ if (!(flags & SAME_GID))
+ gid = (gid_t)read_int(f);
+ file->gid = gid;
+ }
+ if (preserve_devices) {
+ if (protocol_version < 28) {
+ if (IS_DEVICE(mode)) {
+ if (!(flags & SAME_RDEV_pre28))
+ rdev = (DEV64_T)read_int(f);
+ file->u.rdev = rdev;
+ } else
+ rdev = 0;
+ } else if (IS_DEVICE(mode)) {
+ if (!(flags & SAME_HIGH_RDEV)) {
+ file->u.rdev = (DEV64_T)read_int(f);
+ rdev = file->u.rdev & ~0xFF;
+ } else
+ file->u.rdev = rdev | (DEV64_T)read_byte(f);
+ }
+ }
+
+ if (preserve_links && S_ISLNK(mode)) {
+ int l = read_int(f);
+ if (l < 0) {
+ rprintf(FERROR, "overflow: l=%d\n", l);
+ overflow("receive_file_entry");
+ }
+ if (!(file->u.link = new_array(char, l + 1)))
+ out_of_memory("receive_file_entry 2");
+ read_sbuf(f, file->u.link, l);
+ if (sanitize_paths)
+ sanitize_path(file->u.link, file->dirname);
+ }