*
* Copyright (C) 1996-2000 Andrew Tridgell
* Copyright (C) 1996 Paul Mackerras
- * Copyright (C) 2003-2013 Wayne Davison
+ * Copyright (C) 2003-2022 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 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;
extern int relative_paths;
extern int preserve_hard_links;
extern int preserve_perms;
+extern int write_devices;
extern int preserve_xattrs;
+extern int do_fsync;
extern int basis_dir_cnt;
extern int make_backups;
extern int cleanup_got_literal;
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 inplace_partial;
extern int allowed_lull;
extern int delay_updates;
+extern BOOL want_progress_now;
extern mode_t orig_umask;
extern struct stats stats;
extern char *tmpdir;
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;
+
+extern struct name_num_item *xfer_sum_nni;
+extern int xfer_sum_len;
static struct bitbag *delayed_bits = NULL;
static int phase = 0, redoing = 0;
static flist_ndx_list batch_redo_list;
-/* We're either updating the basis file or an identical copy: */
+/* This is non-0 when we are updating the basis file or an identical copy: */
static int updating_basis_or_equiv;
#define TMPNAME_SUFFIX ".XXXXXX"
}
} 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. */
}
static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
- const char *fname, int fd, OFF_T total_size)
+ const char *fname, int fd, struct file_struct *file, int inplace_sizing)
{
static char file_sum1[MAX_DIGEST_LEN];
struct map_struct *mapbuf;
struct sum_struct sum;
int32 len;
+ OFF_T total_size = F_LENGTH(file);
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
- if (preallocate_files && fd != -1 && total_size > 0 && (!inplace || total_size > size_r)) {
+#ifdef SUPPORT_PREALLOCATION
+ if (preallocate_files && fd != -1 && total_size > 0 && (!inplace_sizing || 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_sizing) {
+#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);
} else
mapbuf = NULL;
- sum_init(checksum_seed);
+ sum_init(xfer_sum_nni, checksum_seed);
if (append_mode > 0) {
OFF_T j;
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;
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) {
- rsyserr(FERROR_XFER, errno, "ftruncate failed on %s",
- full_fname(fname));
+ if ((inplace_sizing || preallocated_len > offset) && fd != -1 && !IS_DEVICE(file->mode)) {
+ if (do_ftruncate(fd, offset) < 0)
+ rsyserr(FERROR_XFER, errno, "ftruncate failed on %s", full_fname(fname));
}
#endif
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));
+ sum_end(file_sum1);
+
+ if (do_fsync && fd != -1 && fsync(fd) != 0) {
+ rsyserr(FERROR, errno, "fsync failed on %s", full_fname(fname));
exit_cleanup(RERR_FILEIO);
}
- if (sum_end(file_sum1) != checksum_len)
- overflow_exit("checksum_len"); /* Impossible... */
-
if (mapbuf)
unmap_file(mapbuf);
- read_buf(f_in, sender_file_sum, checksum_len);
+ read_buf(f_in, sender_file_sum, xfer_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, xfer_sum_len) != 0)
return 0;
return 1;
}
-static void discard_receive_data(int f_in, OFF_T length)
+static void discard_receive_data(int f_in, struct file_struct *file)
{
- receive_data(f_in, NULL, -1, 0, NULL, -1, length);
+ receive_data(f_in, NULL, -1, 0, NULL, -1, file, 0);
}
static void handle_delayed_updates(char *local_name)
"rename failed for %s (from %s)",
full_fname(fname), partialptr);
} else {
- if (remove_source_files
- || (preserve_hard_links && F_IS_HLINKED(file)))
- send_msg_int(MSG_SUCCESS, ndx);
+ if (remove_source_files || (preserve_hard_links && F_IS_HLINKED(file)))
+ send_msg_success(fname, ndx);
handle_partial_dir(partialptr, PDIR_DELETE);
}
}
int iflags, xlen;
char *fname, fbuf[MAXPATHLEN];
char xname[MAXPATHLEN];
- char fnametmp[MAXPATHLEN];
+ char *fnametmp, fnametmpbuf[MAXPATHLEN];
char *fnamecmp, *partialptr;
char fnamecmpbuf[MAXPATHLEN];
uchar fnamecmp_type;
#ifdef SUPPORT_ACLS
const char *parent_dirname = "";
#endif
- int ndx, recv_ok;
+ int ndx, recv_ok, one_inplace;
if (DEBUG_GTE(RECV, 1))
rprintf(FINFO, "recv_files(%d) starting\n", cur_flist->used);
if (delay_updates)
delayed_bits = bitbag_create(cur_flist->used + 1);
+ if (whole_file < 0)
+ whole_file = 0;
+
+ progress_init();
+
while (1) {
cleanup_disable();
ndx = read_ndx_and_attrs(f_in, f_out, &iflags, &fnamecmp_type,
xname, &xlen);
if (ndx == NDX_DONE) {
- if (!am_server && INFO_GTE(PROGRESS, 2) && cur_flist) {
+ if (!am_server && cur_flist) {
set_current_file_index(NULL, 0);
- end_progress(0);
+ if (INFO_GTE(PROGRESS, 2))
+ end_progress(0);
}
if (inc_recurse && first_flist) {
if (read_batch) {
if (DEBUG_GTE(RECV, 1))
rprintf(FINFO, "recv_files(%s)\n", fname);
+ if (daemon_filter_list.head && (*fname != '.' || fname[1] != '\0')) {
+ int filt_flags = S_ISDIR(file->mode) ? NAME_IS_DIR : NAME_IS_FILE;
+ if (check_filter(&daemon_filter_list, FLOG, fname, filt_flags) < 0) {
+ rprintf(FERROR, "ERROR: rejecting file transfer request for daemon excluded file: %s\n",
+ fname);
+ exit_cleanup(RERR_PROTOCOL);
+ }
+ }
+
#ifdef SUPPORT_XATTRS
- if (preserve_xattrs && 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) {
stats.created_files++;
}
- if (!am_server && INFO_GTE(PROGRESS, 1))
+ if (!am_server)
set_current_file_index(file, ndx);
stats.xferred_files++;
stats.total_transferred_size += F_LENGTH(file);
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)
"(Skipping batched update for%s \"%s\")\n",
redoing ? " resend of" : "",
fname);
- discard_receive_data(f_in, F_LENGTH(file));
+ discard_receive_data(f_in, file);
file->flags |= FLAG_FILE_SENT;
continue;
}
}
- if (!log_before_transfer)
- remember_initial_stats();
+ remember_initial_stats();
if (!do_xfers) { /* log the transfer */
log_item(FCLIENT, file, iflags, NULL);
if (read_batch)
- discard_receive_data(f_in, F_LENGTH(file));
+ discard_receive_data(f_in, file);
continue;
}
if (write_batch < 0) {
log_item(FCLIENT, file, iflags, NULL);
if (!am_server)
- discard_receive_data(f_in, F_LENGTH(file));
+ discard_receive_data(f_in, file);
if (inc_recurse)
- send_msg_int(MSG_SUCCESS, ndx);
+ send_msg_success(fname, ndx);
continue;
}
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;
}
if (fd1 == -1 && protocol_version < 29) {
if (fnamecmp != fname) {
fnamecmp = fname;
+ fnamecmp_type = FNAMECMP_FNAME;
fd1 = do_open(fnamecmp, O_RDONLY, 0);
}
pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
basis_dir[0], fname);
fnamecmp = fnamecmpbuf;
+ fnamecmp_type = FNAMECMP_BASIS_DIR_LOW;
fd1 = do_open(fnamecmp, O_RDONLY, 0);
}
}
- updating_basis_or_equiv = inplace
- && (fnamecmp == fname || fnamecmp_type == FNAMECMP_BACKUP);
+ one_inplace = inplace_partial && fnamecmp_type == FNAMECMP_PARTIAL_DIR;
+ updating_basis_or_equiv = one_inplace
+ || (inplace && (fnamecmp == fname || fnamecmp_type == FNAMECMP_BACKUP));
if (fd1 == -1) {
st.st_mode = 0;
} else if (do_fstat(fd1,&st) != 0) {
rsyserr(FERROR_XFER, errno, "fstat %s failed",
full_fname(fnamecmp));
- discard_receive_data(f_in, F_LENGTH(file));
+ discard_receive_data(f_in, file);
close(fd1);
if (inc_recurse)
send_msg_int(MSG_NO_SEND, ndx);
*/
rprintf(FERROR_XFER, "recv_files: %s is a directory\n",
full_fname(fnamecmp));
- discard_receive_data(f_in, F_LENGTH(file));
+ discard_receive_data(f_in, file);
close(fd1);
if (inc_recurse)
send_msg_int(MSG_NO_SEND, ndx);
continue;
}
- if (fd1 != -1 && !S_ISREG(st.st_mode)) {
+ if (write_devices && IS_DEVICE(st.st_mode)) {
+ if (fd1 != -1 && st.st_size == 0)
+ st.st_size = get_device_size(fd1, fname);
+ /* Mark the file entry as a device so that we don't try to truncate it later on. */
+ file->mode = S_IFBLK | (file->mode & ACCESSPERMS);
+ } else if (fd1 != -1 && !(S_ISREG(st.st_mode))) {
close(fd1);
fd1 = -1;
}
parent_dirname = dn;
}
#endif
- file->mode = dest_mode(file->mode, st.st_mode,
- dflt_perms, exists);
+ file->mode = dest_mode(file->mode, st.st_mode, dflt_perms, exists);
}
/* We now check to see if we are writing the file "inplace" */
- if (inplace) {
- fd2 = do_open(fname, O_WRONLY|O_CREAT, 0600);
+ if (inplace || one_inplace) {
+ fnametmp = one_inplace ? partialptr : fname;
+ fd2 = do_open(fnametmp, O_WRONLY|O_CREAT, 0600);
+#ifdef linux
+ if (fd2 == -1 && errno == EACCES) {
+ /* Maybe the error was due to protected_regular setting? */
+ fd2 = do_open(fname, O_WRONLY, 0600);
+ }
+#endif
if (fd2 == -1) {
rsyserr(FERROR_XFER, errno, "open %s failed",
- full_fname(fname));
+ full_fname(fnametmp));
} else if (updating_basis_or_equiv)
cleanup_set(NULL, NULL, file, fd1, fd2);
} else {
+ fnametmp = fnametmpbuf;
fd2 = open_tmpfile(fnametmp, fname, file);
if (fd2 != -1)
cleanup_set(fnametmp, partialptr, file, fd1, fd2);
}
if (fd2 == -1) {
- discard_receive_data(f_in, F_LENGTH(file));
+ discard_receive_data(f_in, file);
if (fd1 != -1)
close(fd1);
if (inc_recurse)
rprintf(FINFO, "%s\n", fname);
/* recv file data */
- recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
- fname, fd2, F_LENGTH(file));
+ recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size, fname, fd2, file, inplace || one_inplace);
log_item(log_code, file, iflags, NULL);
+ if (want_progress_now)
+ instant_progress(fname);
if (fd1 != -1)
close(fd1);
if ((recv_ok && (!delay_updates || !partialptr)) || inplace) {
if (partialptr == fname)
partialptr = NULL;
- if (!finish_transfer(fname, fnametmp, fnamecmp,
- partialptr, file, recv_ok, 1))
+ if (!finish_transfer(fname, fnametmp, fnamecmp, partialptr, file, recv_ok, 1))
recv_ok = -1;
else if (fnamecmp == partialptr) {
- do_unlink(partialptr);
+ if (!one_inplace)
+ do_unlink(partialptr);
handle_partial_dir(partialptr, PDIR_DELETE);
}
- } else if (keep_partial && partialptr) {
+ } else if (keep_partial && partialptr && (!one_inplace || delay_updates)) {
if (!handle_partial_dir(partialptr, PDIR_CREATE)) {
rprintf(FERROR,
- "Unable to create partial-dir for %s -- discarding %s.\n",
- local_name ? local_name : f_name(file, NULL),
- recv_ok ? "completed file" : "partial file");
+ "Unable to create partial-dir for %s -- discarding %s.\n",
+ local_name ? local_name : f_name(file, NULL),
+ recv_ok ? "completed file" : "partial file");
do_unlink(fnametmp);
recv_ok = -1;
} else if (!finish_transfer(partialptr, fnametmp, fnamecmp, NULL,
recv_ok = 2;
} else
partialptr = NULL;
- } else
+ } else if (!one_inplace)
do_unlink(fnametmp);
cleanup_disable();
case 2:
break;
case 1:
- if (remove_source_files || inc_recurse
- || (preserve_hard_links && F_IS_HLINKED(file)))
- send_msg_int(MSG_SUCCESS, ndx);
+ if (remove_source_files || inc_recurse || (preserve_hard_links && F_IS_HLINKED(file)))
+ send_msg_success(fname, ndx);
break;
case 0: {
enum logcode msgtype = redoing ? FERROR_XFER : FWARNING;
- if (msgtype == FERROR_XFER || INFO_GTE(NAME, 1)) {
+ if (msgtype == FERROR_XFER || INFO_GTE(NAME, 1) || stdout_format_has_i) {
char *errstr, *redostr, *keptstr;
if (!(keep_partial && partialptr) && !inplace)
keptstr = "discarded";
} else if (inc_recurse)
send_msg_int(MSG_NO_SEND, ndx);
break;
- }
+ }
case -1:
if (inc_recurse)
send_msg_int(MSG_NO_SEND, ndx);