1 This patch adds the --preallocate option that asks rsync to preallocate the
2 copied files. This slows down the copy, but should reduce fragmentation on
3 systems that need that.
5 To use this patch, run these commands for a successful build:
7 patch -p1 <patches/preallocate.diff
17 @@ -550,7 +550,7 @@ AC_CHECK_FUNCS(waitpid wait4 getcwd strd
18 strlcat strlcpy strtol mallinfo getgroups setgroups geteuid getegid \
19 setlocale setmode open64 lseek64 mkstemp64 mtrace va_copy __va_copy \
20 strerror putenv iconv_open locale_charset nl_langinfo \
21 - sigaction sigprocmask)
22 + sigaction sigprocmask posix_fallocate)
24 AC_CHECK_FUNCS(getpgrp tcgetpgrp)
25 if test $ac_cv_func_getpgrp = yes; then
28 @@ -69,6 +69,7 @@ int remove_source_files = 0;
29 int one_file_system = 0;
30 int protocol_version = PROTOCOL_VERSION;
32 +int preallocate_files = 0;
33 int do_compression = 0;
34 int def_compress_level = Z_DEFAULT_COMPRESSION;
36 @@ -201,6 +202,7 @@ static void print_rsync_version(enum log
37 char const *hardlinks = "no ";
38 char const *links = "no ";
39 char const *ipv6 = "no ";
40 + char const *preallocation = "no ";
43 #ifdef HAVE_SOCKETPAIR
44 @@ -223,6 +225,10 @@ static void print_rsync_version(enum log
48 +#ifdef SUPPORT_PREALLOCATION
52 rprintf(f, "%s version %s protocol version %d\n",
53 RSYNC_NAME, RSYNC_VERSION, PROTOCOL_VERSION);
54 rprintf(f, "Copyright (C) 1996-2006 by Andrew Tridgell, Wayne Davison, and others.\n");
55 @@ -233,8 +239,8 @@ static void print_rsync_version(enum log
56 (int)(sizeof (int64) * 8));
57 rprintf(f, " %ssocketpairs, %shardlinks, %ssymlinks, %sIPv6, batchfiles, %sinplace,\n",
58 got_socketpair, hardlinks, links, ipv6, have_inplace);
59 - rprintf(f, " %sappend\n",
61 + rprintf(f, " %sappend, %spreallocation\n",
62 + have_inplace, preallocation);
64 #ifdef MAINTAINER_MODE
65 rprintf(f, "Panic Action: \"%s\"\n", get_panic_action());
66 @@ -311,6 +317,9 @@ void usage(enum logcode F)
67 rprintf(F," -O, --omit-dir-times omit directories when preserving times\n");
68 rprintf(F," --super receiver attempts super-user activities\n");
69 rprintf(F," -S, --sparse handle sparse files efficiently\n");
70 +#ifdef SUPPORT_PREALLOCATION
71 + rprintf(F," --preallocate posix_fallocate dest files before writing them\n");
73 rprintf(F," -n, --dry-run show what would have been transferred\n");
74 rprintf(F," -W, --whole-file copy files whole (without rsync algorithm)\n");
75 rprintf(F," -x, --one-file-system don't cross filesystem boundaries\n");
76 @@ -468,6 +477,7 @@ static struct poptOption long_options[]
77 {"max-size", 0, POPT_ARG_STRING, &max_size_arg, OPT_MAX_SIZE, 0, 0 },
78 {"min-size", 0, POPT_ARG_STRING, &min_size_arg, OPT_MIN_SIZE, 0, 0 },
79 {"sparse", 'S', POPT_ARG_NONE, &sparse_files, 0, 0, 0 },
80 + {"preallocate", 0, POPT_ARG_NONE, &preallocate_files, 0, 0, 0},
81 {"inplace", 0, POPT_ARG_NONE, &inplace, 0, 0, 0 },
82 {"append", 0, POPT_ARG_VAL, &append_mode, 1, 0, 0 },
83 {"del", 0, POPT_ARG_NONE, &delete_during, 0, 0, 0 },
84 @@ -1126,6 +1136,15 @@ int parse_arguments(int *argc, const cha
88 +#ifndef SUPPORT_PREALLOCATION
89 + if (preallocate_files && !am_sender) {
90 + snprintf(err_buf, sizeof err_buf,
91 + "preallocation is not supported on this %s\n",
92 + am_server ? "server" : "client");
97 if (write_batch && read_batch) {
98 snprintf(err_buf, sizeof err_buf,
99 "--write-batch and --read-batch can not be used together\n");
100 @@ -1764,6 +1783,9 @@ void server_options(char **args,int *arg
101 else if (remove_source_files)
102 args[ac++] = "--remove-sent-files";
104 + if (preallocate_files && am_sender)
105 + args[ac++] = "--preallocate";
112 @@ -43,6 +43,7 @@ extern int cleanup_got_literal;
113 extern int remove_source_files;
114 extern int append_mode;
115 extern int sparse_files;
116 +extern int preallocate_files;
117 extern int keep_partial;
118 extern int checksum_seed;
120 @@ -137,6 +138,19 @@ static int receive_data(int f_in, char *
124 +#ifdef SUPPORT_PREALLOCATION
125 + int preallocated_len = 0;
127 + if (preallocate_files && fd != -1 && total_size > 0) {
128 + /* Preallocate enough space for file's eventual length if
129 + * possible; seems to reduce fragmentation on Windows. */
130 + if (posix_fallocate(fd, 0, total_size) == 0)
131 + preallocated_len = total_size;
133 + rsyserr(FINFO, errno, "preallocate %s", full_fname(fname));
137 read_sum_head(f_in, &sum);
139 if (fd_r >= 0 && size_r > 0) {
140 @@ -244,8 +258,18 @@ static int receive_data(int f_in, char *
141 goto report_write_error;
143 #ifdef HAVE_FTRUNCATE
144 - if (inplace && fd != -1)
145 - ftruncate(fd, offset);
146 + /* inplace: New data could be shorter than old data.
147 + * preallocate_files: total_size could have been an overestimate.
148 + * Cut off any extra preallocated zeros from dest file. */
150 +#ifdef SUPPORT_PREALLOCATION
151 + || preallocated_len > offset
154 + if (ftruncate(fd, offset) < 0)
155 + /* If we fail to truncate, the dest file may be wrong, so we
156 + * must trigger the "partial transfer" error. */
157 + rsyserr(FERROR, errno, "ftruncate %s", full_fname(fname));
163 @@ -493,6 +493,10 @@ struct idev {
164 #define IN_LOOPBACKNET 127
167 +#if defined HAVE_FTRUNCATE && defined HAVE_POSIX_FALLOCATE
168 +#define SUPPORT_PREALLOCATION 1
171 #define GID_NONE ((gid_t)-1)
173 #define HL_CHECK_MASTER 0
179 int modify_window = 0;
180 +int preallocate_files = 0;
182 int relative_paths = 0;
183 int human_readable = 0;
190 +extern int preallocate_files;
191 extern int module_id;
192 extern int modify_window;
193 extern int relative_paths;
194 @@ -270,6 +271,10 @@ int copy_file(const char *source, const
197 int len; /* Number of bytes read into `buf'. */
198 +#ifdef SUPPORT_PREALLOCATION
199 + int preallocated_len = 0;
203 ifd = do_open(source, O_RDONLY, 0);
205 @@ -289,7 +294,27 @@ int copy_file(const char *source, const
209 +#ifdef SUPPORT_PREALLOCATION
210 + if (preallocate_files) {
211 + /* Preallocate enough space for file's eventual length if
212 + * possible; seems to reduce fragmentation on Windows. */
214 + if (do_fstat(ifd, &srcst) == 0) {
215 + if (srcst.st_size > 0) {
216 + if (posix_fallocate(ofd, 0, srcst.st_size) == 0)
217 + preallocated_len = srcst.st_size;
219 + rsyserr(FINFO, errno, "posix_fallocate %s", full_fname(dest));
222 + rsyserr(FINFO, errno, "fstat %s", full_fname(source));
226 while ((len = safe_read(ifd, buf, sizeof buf)) > 0) {
227 +#ifdef SUPPORT_PREALLOCATION
230 if (full_write(ofd, buf, len) < 0) {
231 rsyserr(FERROR, errno, "write %s", full_fname(dest));
233 @@ -310,6 +335,16 @@ int copy_file(const char *source, const
237 +#ifdef SUPPORT_PREALLOCATION
238 + /* Source file might have shrunk since we fstatted it.
239 + * Cut off any extra preallocated zeros from dest file. */
240 + if (preallocated_len > offset)
241 + if (ftruncate(ofd, offset) < 0)
242 + /* If we fail to truncate, the dest file may be wrong, so we
243 + * must trigger the "partial transfer" error. */
244 + rsyserr(FERROR, errno, "ftruncate %s", full_fname(dest));
247 if (close(ofd) < 0) {
248 rsyserr(FERROR, errno, "close failed on %s",