-
- if (always_checksum)
- read_buf(f,file->sum,SUM_LENGTH);
-
- last_mode = file->mode;
- last_dev = file->dev;
- last_uid = file->uid;
- last_gid = file->gid;
- p = strrchr(file->name,'/');
- if (p) {
- int l = (int)(p - file->name) + 1;
- strncpy(lastdir,file->name,l);
- lastdir[l] = 0;
- } else {
- strcpy(lastdir,"");
- }
-}
-
-
-static struct file_struct *make_file(int recurse,char *fname)
-{
- static struct file_struct file;
- struct stat st;
- char sum[SUM_LENGTH];
-
- bzero(sum,SUM_LENGTH);
-
- if (lstat(fname,&st) != 0) {
- fprintf(stderr,"%s: %s\n",
- fname,strerror(errno));
- return NULL;
- }
-
- if (S_ISDIR(st.st_mode) && !recurse) {
- fprintf(stderr,"skipping directory %s\n",fname);
- return NULL;
- }
-
- if (one_file_system && st.st_dev != filesystem_dev)
- return NULL;
-
- if (!match_file_name(fname,&st))
- return NULL;
-
- if (verbose > 2)
- fprintf(stderr,"make_file(%s)\n",fname);
-
- file.name = strdup(fname);
- 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;
-#ifdef HAVE_ST_RDEV
- file.dev = st.st_rdev;
+
+ if (always_checksum) {
+ char *sum;
+ if (S_ISREG(mode)) {
+ sum = file->u.sum = new_array(char, MD4_SUM_LENGTH);
+ if (!sum)
+ out_of_memory("md4 sum");
+ } else if (protocol_version < 28) {
+ /* Prior to 28, we get a useless set of nulls. */
+ sum = empty_sum;
+ } else
+ sum = NULL;
+ if (sum) {
+ read_buf(f, sum, protocol_version < 21? 2
+ : MD4_SUM_LENGTH);
+ }
+ }
+
+ if (!preserve_perms) {
+ extern int orig_umask;
+ /* set an appropriate set of permissions based on original
+ * permissions and umask. This emulates what GNU cp does */
+ file->mode &= ~orig_umask;
+ }
+}
+
+
+#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 (IS_DEVICE(st.st_mode))
+ file->u.rdev = st.st_rdev;