Fixed failing hunks and added preallocate.diff.
[rsync/rsync-patches.git] / preallocate.diff
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.
4
5 To use this patch, run these commands for a successful build:
6
7     patch -p1 <patches/preallocate.diff
8     ./prepare-source
9     ./configure
10     make
11
12
13 TODO:  update docs
14
15 --- old/configure.in
16 +++ new/configure.in
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)
23  
24  AC_CHECK_FUNCS(getpgrp tcgetpgrp)
25  if test $ac_cv_func_getpgrp = yes; then
26 --- old/options.c
27 +++ new/options.c
28 @@ -69,6 +69,7 @@ int remove_source_files = 0;
29  int one_file_system = 0;
30  int protocol_version = PROTOCOL_VERSION;
31  int sparse_files = 0;
32 +int preallocate_files = 0;
33  int do_compression = 0;
34  int def_compress_level = Z_DEFAULT_COMPRESSION;
35  int am_root = 0;
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 ";
41         STRUCT_STAT *dumstat;
42  
43  #ifdef HAVE_SOCKETPAIR
44 @@ -223,6 +225,10 @@ static void print_rsync_version(enum log
45         ipv6 = "";
46  #endif
47  
48 +#ifdef SUPPORT_PREALLOCATION
49 +       preallocation = "";
50 +#endif
51 +
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",
60 -               have_inplace);
61 +       rprintf(f, "    %sappend, %spreallocation\n",
62 +               have_inplace, preallocation);
63  
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");
72 +#endif
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
85         }
86  #endif
87  
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");
93 +               return 0;
94 +       }
95 +#endif
96 +
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";
103  
104 +       if (preallocate_files && am_sender)
105 +               args[ac++] = "--preallocate";
106 +
107         *argc = ac;
108         return;
109  
110 --- old/receiver.c
111 +++ new/receiver.c
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;
119  extern int inplace;
120 @@ -137,6 +138,19 @@ static int receive_data(int f_in, char *
121         int32 i;
122         char *map = NULL;
123  
124 +#ifdef SUPPORT_PREALLOCATION
125 +       int preallocated_len = 0;
126 +
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;
132 +               else
133 +                       rsyserr(FINFO, errno, "preallocate %s", full_fname(fname));
134 +       }
135 +#endif
136 +
137         read_sum_head(f_in, &sum);
138  
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;
142  
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. */
149 +       if ((inplace
150 +#ifdef SUPPORT_PREALLOCATION
151 +                       || preallocated_len > offset
152 +#endif
153 +               ) && fd != -1)
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));
158  #endif
159  
160         if (do_progress)
161 --- old/rsync.h
162 +++ new/rsync.h
163 @@ -493,6 +493,10 @@ struct idev {
164  #define IN_LOOPBACKNET 127
165  #endif
166  
167 +#if defined HAVE_FTRUNCATE && defined HAVE_POSIX_FALLOCATE
168 +#define SUPPORT_PREALLOCATION 1
169 +#endif
170 +
171  #define GID_NONE ((gid_t)-1)
172  
173  #define HL_CHECK_MASTER        0
174 --- old/t_stub.c
175 +++ new/t_stub.c
176 @@ -23,6 +23,7 @@
177  #include "rsync.h"
178  
179  int modify_window = 0;
180 +int preallocate_files = 0;
181  int module_id = -1;
182  int relative_paths = 0;
183  int human_readable = 0;
184 --- old/util.c
185 +++ new/util.c
186 @@ -25,6 +25,7 @@
187  
188  extern int verbose;
189  extern int dry_run;
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 
195         int ofd;
196         char buf[1024 * 8];
197         int len;   /* Number of bytes read into `buf'. */
198 +#ifdef SUPPORT_PREALLOCATION
199 +       int preallocated_len = 0;
200 +       int offset = 0;
201 +#endif
202  
203         ifd = do_open(source, O_RDONLY, 0);
204         if (ifd == -1) {
205 @@ -289,7 +294,27 @@ int copy_file(const char *source, const 
206                 return -1;
207         }
208  
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. */
213 +               STRUCT_STAT srcst;
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;
218 +                               else
219 +                                       rsyserr(FINFO, errno, "posix_fallocate %s", full_fname(dest));
220 +                       }
221 +               } else
222 +                       rsyserr(FINFO, errno, "fstat %s", full_fname(source));
223 +       }
224 +#endif
225 +
226         while ((len = safe_read(ifd, buf, sizeof buf)) > 0) {
227 +#ifdef SUPPORT_PREALLOCATION
228 +               offset += len;
229 +#endif
230                 if (full_write(ofd, buf, len) < 0) {
231                         rsyserr(FERROR, errno, "write %s", full_fname(dest));
232                         close(ifd);
233 @@ -310,6 +335,16 @@ int copy_file(const char *source, const 
234                         full_fname(source));
235         }
236  
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));
245 +#endif
246 +
247         if (close(ofd) < 0) {
248                 rsyserr(FERROR, errno, "close failed on %s",
249                         full_fname(dest));