+#define STRDUP(ap, p) (ap ? string_area_strdup(ap, p) : strdup(p))
+/* IRIX cc cares that the operands to the ternary have the same type. */
+#define MALLOC(ap, i) (ap ? (void*) string_area_malloc(ap, i) : malloc(i))
+
+/**
+ * Create a file_struct for a named file by reading its stat()
+ * information and performing extensive checks against global
+ * options.
+ *
+ * @return the new file, or NULL if there was an error or this file
+ * should be excluded.
+ *
+ * @todo There is a small optimization opportunity here to avoid
+ * stat()ing the file in some circumstances, which has a certain cost.
+ * We are called immediately after doing readdir(), and so we may
+ * already know the d_type of the file. We could for example avoid
+ * statting directories if we're not recursing, but this is not a very
+ * important case. Some systems may not have d_type.
+ **/
+struct file_struct *make_file(char *fname, struct string_area **ap,
+ int exclude_level)
+{
+ struct file_struct *file;
+ STRUCT_STAT st;
+ char sum[SUM_LENGTH];
+ char *p;
+ char thisname[MAXPATHLEN];
+ char linkname[MAXPATHLEN];
+ unsigned short flags = 0;
+
+ if (strlcpy(thisname, fname, sizeof thisname)
+ >= sizeof thisname - flist_dir_len) {
+ rprintf(FINFO, "skipping overly long name: %s\n", fname);
+ return NULL;
+ }
+ clean_fname(thisname);
+ if (sanitize_paths)
+ sanitize_path(thisname, NULL);
+
+ memset(sum, 0, SUM_LENGTH);
+
+ 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
+ * was removed during rsync run; see if excluded
+ * before reporting an error */
+ if (check_exclude_file(thisname, 0, exclude_level)) {
+ /* file is excluded anyway, ignore silently */
+ return NULL;
+ }
+ }
+ io_error |= IOERR_GENERAL;
+ rprintf(FERROR, "readlink %s failed: %s\n",
+ full_fname(thisname), strerror(save_errno));
+ return NULL;
+ }
+
+ /* backup.c calls us with exclude_level set to NO_EXCLUDES. */
+ if (exclude_level == NO_EXCLUDES)
+ goto skip_excludes;
+
+ if (S_ISDIR(st.st_mode) && !recurse && !files_from) {
+ rprintf(FINFO, "skipping directory %s\n", thisname);
+ return NULL;
+ }
+
+ if (one_file_system && st.st_dev != filesystem_dev) {
+ /* We allow a directory though to preserve the mount point.
+ * However, flag it so that we don't recurse. */
+ if (!S_ISDIR(st.st_mode))
+ return NULL;
+ flags |= FLAG_MOUNT_POINT;
+ }
+
+ if (check_exclude_file(thisname, S_ISDIR(st.st_mode) != 0, exclude_level))
+ return NULL;
+
+ if (lp_ignore_nonreadable(module_id) && access(thisname, R_OK) != 0)
+ return NULL;
+
+ skip_excludes:
+
+ if (verbose > 2) {
+ rprintf(FINFO, "[%s] make_file(%s,*,%d)\n",
+ who_am_i(), thisname, exclude_level);
+ }
+
+ file = new(struct file_struct);
+ if (!file)
+ out_of_memory("make_file");
+ memset((char *) file, 0, sizeof(*file));
+ file->flags = flags;
+
+ 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(ap, p + 1);
+ *p = '/';
+ } else {
+ file->dirname = NULL;
+ file->basename = STRDUP(ap, thisname);
+ }
+
+ file->modtime = st.st_mtime;
+ file->length = st.st_size;
+ file->mode = st.st_mode;
+ file->uid = st.st_uid;
+ file->gid = st.st_gid;
+ if (preserve_hard_links) {
+ if (protocol_version < 28 ? S_ISREG(st.st_mode)
+ : !S_ISDIR(st.st_mode) && st.st_nlink > 1) {
+ if (!(file->link_u.idev = new(struct idev)))
+ out_of_memory("file inode data");
+ file->F_DEV = st.st_dev;
+ file->F_INODE = st.st_ino;
+ }
+ }
+#ifdef HAVE_STRUCT_STAT_ST_RDEV
+ 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, linkname);
+#endif
+
+ if (always_checksum && S_ISREG(st.st_mode)) {
+ if (!(file->u.sum = (char*)MALLOC(ap, MD4_SUM_LENGTH)))
+ out_of_memory("md4 sum");
+ file_checksum(thisname, file->u.sum, st.st_size);
+ }
+
+ file->basedir = flist_dir;
+
+ if (!S_ISDIR(st.st_mode))
+ stats.total_size += st.st_size;
+
+ return file;
+}
+