Avoid leaving a file open on error return.
[rsync.git] / receiver.c
index 1e064d9dd65a02da9946992fb1ced98683a86d1c..d6a48f14a66772eda9a3c15c349ccec7f641d6d2 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright (C) 1996-2000 Andrew Tridgell
  * Copyright (C) 1996 Paul Mackerras
- * Copyright (C) 2003-2013 Wayne Davison
+ * Copyright (C) 2003-2018 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
@@ -30,6 +30,7 @@ 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;
@@ -47,11 +48,12 @@ 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;
@@ -60,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;
@@ -113,9 +116,12 @@ int get_tmpname(char *fnametmp, const char *fname, BOOL make_unique)
                }
        } else
                f = fname;
-       if (*f == '.') /* avoid an extra leading dot for OS X's sake */
-               f++;
-       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. */
@@ -230,28 +236,32 @@ 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;
        char *data;
        int32 i;
        char *map = NULL;
-#ifdef SUPPORT_PREALLOCATION
-#ifdef PREALLOCATE_NEEDS_TRUNCATE
-       OFF_T preallocated_len = 0;
-#endif
 
+#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 (do_fallocate(fd, 0, total_size) == 0) {
-#ifdef PREALLOCATE_NEEDS_TRUNCATE
-                       preallocated_len = total_size;
-#endif
-               } else
+               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);
 
@@ -265,7 +275,7 @@ 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;
@@ -313,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;
@@ -343,37 +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
        /* 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
-#ifdef PREALLOCATE_NEEDS_TRUNCATE
-         || preallocated_len > offset
-#endif
-         ) && fd != -1 && do_ftruncate(fd, offset) < 0) {
+       if ((inplace || preallocated_len > offset) && fd != -1 && do_ftruncate(fd, offset) < 0) {
                rsyserr(FERROR_XFER, errno, "ftruncate failed on %s",
                        full_fname(fname));
        }
@@ -382,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, offset) != 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;
 }
@@ -579,9 +577,15 @@ int recv_files(int f_in, int f_out, char *local_name)
                if (DEBUG_GTE(RECV, 1))
                        rprintf(FINFO, "recv_files(%s)\n", fname);
 
+               if (daemon_filter_list.head && (*fname != '.' || fname[1] != '\0')
+                && check_filter(&daemon_filter_list, FLOG, fname, 0) < 0) {
+                       rprintf(FERROR, "attempt to hack rsync failed.\n");
+                       exit_cleanup(RERR_PROTOCOL);
+               }
+
 #ifdef SUPPORT_XATTRS
                if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers
-                && (protocol_version < 31 || !BITS_SET(iflags, ITEM_XNAME_FOLLOWS|ITEM_LOCAL_CHANGE)))
+                && !(want_xattr_optim && BITS_SET(iflags, ITEM_XNAME_FOLLOWS|ITEM_LOCAL_CHANGE)))
                        recv_xattr_request(file, f_in);
 #endif
 
@@ -647,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)
@@ -668,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);
@@ -725,7 +722,7 @@ int recv_files(int f_in, int f_out, char *local_name)
                                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;
                        }