Check daemon filter against fnamecmp in recv_files().
[rsync.git] / receiver.c
index e8fa3d16bcb1c8fcf5663059bbcb8246d2b1183a..9c46242e013c4d447b17b00bbb705a96a5c92ed1 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright (C) 1996-2000 Andrew Tridgell
  * Copyright (C) 1996 Paul Mackerras
- * Copyright (C) 2003-2009 Wayne Davison
+ * Copyright (C) 2003-2015 Wayne Davison
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 
 extern int dry_run;
 extern int do_xfers;
+extern int am_root;
 extern int am_server;
 extern int inc_recurse;
 extern int log_before_transfer;
 extern int stdout_format_has_i;
 extern int logfile_format_has_i;
+extern int want_xattr_optim;
 extern int csum_length;
 extern int read_batch;
 extern int write_batch;
@@ -44,11 +46,14 @@ extern int cleanup_got_literal;
 extern int remove_source_files;
 extern int append_mode;
 extern int sparse_files;
+extern int preallocate_files;
 extern int keep_partial;
-extern int checksum_len;
 extern int checksum_seed;
+extern int whole_file;
 extern int inplace;
+extern int allowed_lull;
 extern int delay_updates;
+extern int xfersum_type;
 extern mode_t orig_umask;
 extern struct stats stats;
 extern char *tmpdir;
@@ -57,6 +62,7 @@ extern char *basis_dir[MAX_BASIS_DIRS+1];
 extern char sender_file_sum[MAX_DIGEST_LEN];
 extern struct file_list *cur_flist, *first_flist, *dir_flist;
 extern filter_rule_list daemon_filter_list;
+extern OFF_T preallocated_len;
 
 static struct bitbag *delayed_bits = NULL;
 static int phase = 0, redoing = 0;
@@ -91,7 +97,7 @@ static int updating_basis_or_equiv;
  * transfer is in progress. */
 int get_tmpname(char *fnametmp, const char *fname, BOOL make_unique)
 {
-       int maxname, added, length = 0;
+       int maxname, length = 0;
        const char *f;
        char *suf;
 
@@ -110,23 +116,42 @@ int get_tmpname(char *fnametmp, const char *fname, BOOL make_unique)
                }
        } else
                f = fname;
-       fnametmp[length++] = '.';
+
+       if (!tmpdir) { /* using a tmpdir avoids the leading dot on our temp names */
+               if (*f == '.') /* avoid an extra leading dot for OS X's sake */
+                       f++;
+               fnametmp[length++] = '.';
+       }
 
        /* The maxname value is bufsize, and includes space for the '\0'.
         * NAME_MAX needs an extra -1 for the name's leading dot. */
        maxname = MIN(MAXPATHLEN - length - TMPNAME_SUFFIX_LEN,
                      NAME_MAX - 1 - TMPNAME_SUFFIX_LEN);
 
-       if (maxname < 1) {
+       if (maxname < 0) {
                rprintf(FERROR_XFER, "temporary filename too long: %s\n", fname);
                fnametmp[0] = '\0';
                return 0;
        }
 
-       added = strlcpy(fnametmp + length, f, maxname);
-       if (added >= maxname)
-               added = maxname - 1;
-       suf = fnametmp + length + added;
+       if (maxname) {
+               int added = strlcpy(fnametmp + length, f, maxname);
+               if (added >= maxname)
+                       added = maxname - 1;
+               suf = fnametmp + length + added;
+
+               /* Trim any dangling high-bit chars if the first-trimmed char (if any) is
+                * also a high-bit char, just in case we cut into a multi-byte sequence.
+                * We are guaranteed to stop because of the leading '.' we added. */
+               if ((int)f[added] & 0x80) {
+                       while ((int)suf[-1] & 0x80)
+                               suf--;
+               }
+               /* trim one trailing dot before our suffix's dot */
+               if (suf[-1] == '.')
+                       suf--;
+       } else
+               suf = fnametmp + length - 1; /* overwrite the leading dot with suffix's dot */
 
        if (make_unique) {
                static unsigned counter_limit;
@@ -164,15 +189,25 @@ int get_tmpname(char *fnametmp, const char *fname, BOOL make_unique)
 int open_tmpfile(char *fnametmp, const char *fname, struct file_struct *file)
 {
        int fd;
+       mode_t added_perms;
 
        if (!get_tmpname(fnametmp, fname, False))
                return -1;
 
+       if (am_root < 0) {
+               /* For --fake-super, the file must be useable by the copying
+                * user, just like it would be for root. */
+               added_perms = S_IRUSR|S_IWUSR;
+       } else {
+               /* For a normal copy, we need to be able to tweak things like xattrs. */
+               added_perms = S_IWUSR;
+       }
+
        /* We initially set the perms without the setuid/setgid bits or group
         * access to ensure that there is no race condition.  They will be
         * correctly updated after the right owner and group info is set.
         * (Thanks to snabb@epipe.fi for pointing this out.) */
-       fd = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
+       fd = do_mkstemp(fnametmp, (file->mode|added_perms) & INITACCESSPERMS);
 
 #if 0
        /* In most cases parent directories will already exist because their
@@ -182,7 +217,7 @@ int open_tmpfile(char *fnametmp, const char *fname, struct file_struct *file)
         && make_path(fnametmp, MKP_SKIP_SLASH | MKP_DROP_NAME) == 0) {
                /* Get back to name with XXXXXX in it. */
                get_tmpname(fnametmp, fname, False);
-               fd = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
+               fd = do_mkstemp(fnametmp, (file->mode|added_perms) & INITACCESSPERMS);
        }
 #endif
 
@@ -201,6 +236,7 @@ static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
        static char file_sum1[MAX_DIGEST_LEN];
        struct map_struct *mapbuf;
        struct sum_struct sum;
+       int sum_len;
        int32 len;
        OFF_T offset = 0;
        OFF_T offset2;
@@ -208,6 +244,25 @@ static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
        int32 i;
        char *map = NULL;
 
+#ifdef SUPPORT_PREALLOCATION
+       if (preallocate_files && fd != -1 && total_size > 0 && (!inplace || total_size > size_r)) {
+               /* Try to preallocate enough space for file's eventual length.  Can
+                * reduce fragmentation on filesystems like ext4, xfs, and NTFS. */
+               if ((preallocated_len = do_fallocate(fd, 0, total_size)) < 0)
+                       rsyserr(FWARNING, errno, "do_fallocate %s", full_fname(fname));
+       } else
+#endif
+       if (inplace) {
+#ifdef HAVE_FTRUNCATE
+               /* The most compatible way to create a sparse file is to start with no length. */
+               if (sparse_files > 0 && whole_file && fd >= 0 && do_ftruncate(fd, 0) == 0)
+                       preallocated_len = 0;
+               else
+#endif
+                       preallocated_len = size_r;
+       } else
+               preallocated_len = 0;
+
        read_sum_head(f_in, &sum);
 
        if (fd_r >= 0 && size_r > 0) {
@@ -220,14 +275,14 @@ static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
        } else
                mapbuf = NULL;
 
-       sum_init(checksum_seed);
+       sum_init(xfersum_type, checksum_seed);
 
        if (append_mode > 0) {
                OFF_T j;
                sum.flength = (OFF_T)sum.count * sum.blength;
                if (sum.remainder)
                        sum.flength -= sum.blength - sum.remainder;
-               if (append_mode == 2) {
+               if (append_mode == 2 && mapbuf) {
                        for (j = CHUNK_SIZE; j < sum.flength; j += CHUNK_SIZE) {
                                if (INFO_GTE(PROGRESS, 1))
                                        show_progress(offset, total_size);
@@ -254,6 +309,9 @@ static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
                if (INFO_GTE(PROGRESS, 1))
                        show_progress(offset, total_size);
 
+               if (allowed_lull)
+                       maybe_send_keepalive(time(NULL), MSK_ALLOW_FLUSH | MSK_ACTIVE_RECEIVER);
+
                if (i > 0) {
                        if (DEBUG_GTE(DELTASUM, 3)) {
                                rprintf(FINFO,"data recv %d at %s\n",
@@ -265,7 +323,7 @@ static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
 
                        sum_update(data, i);
 
-                       if (fd != -1 && write_file(fd,data,i) != i)
+                       if (fd != -1 && write_file(fd, 0, offset, data, i) != i)
                                goto report_write_error;
                        offset += i;
                        continue;
@@ -281,8 +339,9 @@ static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
 
                if (DEBUG_GTE(DELTASUM, 3)) {
                        rprintf(FINFO,
-                               "chunk[%d] of size %ld at %s offset=%s\n",
-                               i, (long)len, big_num(offset2), big_num(offset));
+                               "chunk[%d] of size %ld at %s offset=%s%s\n",
+                               i, (long)len, big_num(offset2), big_num(offset),
+                               updating_basis_or_equiv && offset == offset2 ? " (seek)" : "");
                }
 
                if (mapbuf) {
@@ -294,31 +353,33 @@ static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
 
                if (updating_basis_or_equiv) {
                        if (offset == offset2 && fd != -1) {
-                               OFF_T pos;
-                               if (flush_write_file(fd) < 0)
+                               if (skip_matched(fd, offset, map, len) < 0)
                                        goto report_write_error;
                                offset += len;
-                               if ((pos = do_lseek(fd, len, SEEK_CUR)) != offset) {
-                                       rsyserr(FERROR_XFER, errno,
-                                               "lseek of %s returned %s, not %s",
-                                               full_fname(fname),
-                                               big_num(pos), big_num(offset));
-                                       exit_cleanup(RERR_FILEIO);
-                               }
                                continue;
                        }
                }
-               if (fd != -1 && map && write_file(fd, map, len) != (int)len)
+               if (fd != -1 && map && write_file(fd, 0, offset, map, len) != (int)len)
                        goto report_write_error;
                offset += len;
        }
 
-       if (flush_write_file(fd) < 0)
-               goto report_write_error;
+       if (fd != -1 && offset > 0) {
+               if (sparse_files > 0) {
+                       if (sparse_end(fd, offset) != 0)
+                               goto report_write_error;
+               } else if (flush_write_file(fd) < 0) {
+                   report_write_error:
+                       rsyserr(FERROR_XFER, errno, "write failed on %s", full_fname(fname));
+                       exit_cleanup(RERR_FILEIO);
+               }
+       }
 
 #ifdef HAVE_FTRUNCATE
-       if (inplace && fd != -1
-        && ftruncate(fd, offset) < 0) {
+       /* inplace: New data could be shorter than old data.
+        * preallocate_files: total_size could have been an overestimate.
+        *     Cut off any extra preallocated zeros from dest file. */
+       if ((inplace || preallocated_len > offset) && fd != -1 && do_ftruncate(fd, offset) < 0) {
                rsyserr(FERROR_XFER, errno, "ftruncate failed on %s",
                        full_fname(fname));
        }
@@ -327,23 +388,15 @@ static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
        if (INFO_GTE(PROGRESS, 1))
                end_progress(total_size);
 
-       if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
-           report_write_error:
-               rsyserr(FERROR_XFER, errno, "write failed on %s",
-                       full_fname(fname));
-               exit_cleanup(RERR_FILEIO);
-       }
-
-       if (sum_end(file_sum1) != checksum_len)
-               overflow_exit("checksum_len"); /* Impossible... */
+       sum_len = sum_end(file_sum1);
 
        if (mapbuf)
                unmap_file(mapbuf);
 
-       read_buf(f_in, sender_file_sum, checksum_len);
+       read_buf(f_in, sender_file_sum, sum_len);
        if (DEBUG_GTE(DELTASUM, 2))
                rprintf(FINFO,"got file_sum\n");
-       if (fd != -1 && memcmp(file_sum1, sender_file_sum, checksum_len) != 0)
+       if (fd != -1 && memcmp(file_sum1, sender_file_sum, sum_len) != 0)
                return 0;
        return 1;
 }
@@ -521,18 +574,26 @@ int recv_files(int f_in, int f_out, char *local_name)
                        file = dir_flist->files[cur_flist->parent_ndx];
                fname = local_name ? local_name : f_name(file, fbuf);
 
+               if (daemon_filter_list.head
+                   && check_filter(&daemon_filter_list, FLOG, fname, 0) < 0) {
+                       rprintf(FERROR, "attempt to hack rsync failed.\n");
+                       exit_cleanup(RERR_PROTOCOL);
+               }
+
                if (DEBUG_GTE(RECV, 1))
                        rprintf(FINFO, "recv_files(%s)\n", fname);
 
 #ifdef SUPPORT_XATTRS
-               if (iflags & ITEM_REPORT_XATTR && do_xfers)
+               if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers
+                && !(want_xattr_optim && BITS_SET(iflags, ITEM_XNAME_FOLLOWS|ITEM_LOCAL_CHANGE)))
                        recv_xattr_request(file, f_in);
 #endif
 
                if (!(iflags & ITEM_TRANSFER)) {
                        maybe_log_item(file, iflags, itemizing, xname);
 #ifdef SUPPORT_XATTRS
-                       if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers)
+                       if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers
+                        && !BITS_SET(iflags, ITEM_XNAME_FOLLOWS|ITEM_LOCAL_CHANGE))
                                set_file_attrs(fname, file, NULL, fname, 0);
 #endif
                        if (iflags & ITEM_IS_NEW) {
@@ -590,12 +651,6 @@ int recv_files(int f_in, int f_out, char *local_name)
 
                cleanup_got_literal = 0;
 
-               if (daemon_filter_list.head
-                   && check_filter(&daemon_filter_list, FLOG, fname, 0) < 0) {
-                       rprintf(FERROR, "attempt to hack rsync failed.\n");
-                       exit_cleanup(RERR_PROTOCOL);
-               }
-
                if (read_batch) {
                        int wanted = redoing
                                   ? we_want_redo(ndx)
@@ -611,8 +666,7 @@ int recv_files(int f_in, int f_out, char *local_name)
                        }
                }
 
-               if (!log_before_transfer)
-                       remember_initial_stats();
+               remember_initial_stats();
 
                if (!do_xfers) { /* log the transfer */
                        log_item(FCLIENT, file, iflags, NULL);
@@ -624,6 +678,8 @@ int recv_files(int f_in, int f_out, char *local_name)
                        log_item(FCLIENT, file, iflags, NULL);
                        if (!am_server)
                                discard_receive_data(f_in, F_LENGTH(file));
+                       if (inc_recurse)
+                               send_msg_int(MSG_SUCCESS, ndx);
                        continue;
                }
 
@@ -642,26 +698,31 @@ int recv_files(int f_in, int f_out, char *local_name)
                                break;
                        case FNAMECMP_FUZZY:
                                if (file->dirname) {
-                                       pathjoin(fnamecmpbuf, MAXPATHLEN,
-                                                file->dirname, xname);
+                                       pathjoin(fnamecmpbuf, sizeof fnamecmpbuf, file->dirname, xname);
                                        fnamecmp = fnamecmpbuf;
                                } else
                                        fnamecmp = xname;
                                break;
                        default:
-                               if (fnamecmp_type >= basis_dir_cnt) {
+                               if (fnamecmp_type > FNAMECMP_FUZZY && fnamecmp_type-FNAMECMP_FUZZY <= basis_dir_cnt) {
+                                       fnamecmp_type -= FNAMECMP_FUZZY + 1;
+                                       if (file->dirname) {
+                                               stringjoin(fnamecmpbuf, sizeof fnamecmpbuf,
+                                                          basis_dir[fnamecmp_type], "/", file->dirname, "/", xname, NULL);
+                                       } else
+                                               pathjoin(fnamecmpbuf, sizeof fnamecmpbuf, basis_dir[fnamecmp_type], xname);
+                               } else if (fnamecmp_type >= basis_dir_cnt) {
                                        rprintf(FERROR,
                                                "invalid basis_dir index: %d.\n",
                                                fnamecmp_type);
                                        exit_cleanup(RERR_PROTOCOL);
-                               }
-                               pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
-                                        basis_dir[fnamecmp_type], fname);
+                               } else
+                                       pathjoin(fnamecmpbuf, sizeof fnamecmpbuf, basis_dir[fnamecmp_type], fname);
                                fnamecmp = fnamecmpbuf;
                                break;
                        }
                        if (!fnamecmp || (daemon_filter_list.head
-                         && check_filter(&daemon_filter_list, FLOG, fname, 0) < 0)) {
+                         && check_filter(&daemon_filter_list, FLOG, fnamecmp, 0) < 0)) {
                                fnamecmp = fname;
                                fnamecmp_type = FNAMECMP_FNAME;
                        }
@@ -755,7 +816,8 @@ int recv_files(int f_in, int f_out, char *local_name)
                        if (fd2 == -1) {
                                rsyserr(FERROR_XFER, errno, "open %s failed",
                                        full_fname(fname));
-                       }
+                       } else if (updating_basis_or_equiv)
+                               cleanup_set(NULL, NULL, file, fd1, fd2);
                } else {
                        fd2 = open_tmpfile(fnametmp, fname, file);
                        if (fd2 != -1)