Fix failing hunks.
[rsync/rsync-patches.git] / partial-dir.diff
1 You must run "make proto" after applying this patch.
2
3 --- orig/cleanup.c      2004-07-20 21:36:07
4 +++ cleanup.c   2004-07-27 23:32:17
5 @@ -111,7 +111,8 @@ void _exit_cleanup(int code, const char 
6                 }
7         }
8  
9 -       if (cleanup_got_literal && cleanup_fname && keep_partial) {
10 +       if (cleanup_got_literal && cleanup_fname && keep_partial
11 +           && handle_partial_dir(cleanup_new_fname, PDIR_CREATE)) {
12                 char *fname = cleanup_fname;
13                 cleanup_fname = NULL;
14                 if (cleanup_fd_r != -1)
15 --- orig/generator.c    2004-07-28 10:05:29
16 +++ generator.c 2004-07-28 09:58:28
17 @@ -42,6 +42,7 @@ extern int size_only;
18  extern int io_timeout;
19  extern int protocol_version;
20  extern int always_checksum;
21 +extern char *partial_dir;
22  extern char *compare_dest;
23  extern int link_dest;
24  extern int whole_file;
25 @@ -496,6 +497,16 @@ static void recv_generator(char *fname, 
26                 return;
27         }
28  
29 +       if (partial_dir) {
30 +               STRUCT_STAT st2;
31 +               char *partialptr = partial_dir_fname(fname);
32 +               if (partialptr && link_stat(partialptr, &st2, 0) == 0
33 +                   && S_ISREG(st2.st_mode)) {
34 +                       st = st2;
35 +                       fnamecmp = partialptr;
36 +               }
37 +       }
38 +
39         /* open the file */
40         fd = do_open(fnamecmp, O_RDONLY, 0);
41  
42 --- orig/options.c      2004-07-23 17:16:13
43 +++ options.c   2004-07-26 16:43:48
44 @@ -118,6 +118,7 @@ unsigned int backup_dir_remainder;
45  
46  char *backup_suffix = NULL;
47  char *tmpdir = NULL;
48 +char *partial_dir = NULL;
49  char *compare_dest = NULL;
50  char *config_file = NULL;
51  char *shell_cmd = NULL;
52 @@ -268,6 +269,7 @@ void usage(enum logcode F)
53    rprintf(F,"     --ignore-errors         delete even if there are I/O errors\n");
54    rprintf(F,"     --max-delete=NUM        don't delete more than NUM files\n");
55    rprintf(F,"     --partial               keep partially transferred files\n");
56 +  rprintf(F,"     --partial-dir=DIR       put a partially transferred file into DIR\n");
57    rprintf(F,"     --force                 force deletion of directories even if not empty\n");
58    rprintf(F,"     --numeric-ids           don't map uid/gid values by user/group name\n");
59    rprintf(F,"     --timeout=TIME          set I/O timeout in seconds\n");
60 @@ -383,6 +385,7 @@ static struct poptOption long_options[] 
61    {"stats",            0,  POPT_ARG_NONE,   &do_stats, 0, 0, 0 },
62    {"progress",         0,  POPT_ARG_NONE,   &do_progress, 0, 0, 0 },
63    {"partial",          0,  POPT_ARG_NONE,   &keep_partial, 0, 0, 0 },
64 +  {"partial-dir",      0,  POPT_ARG_STRING, &partial_dir, 0, 0, 0 },
65    {"ignore-errors",    0,  POPT_ARG_NONE,   &ignore_errors, 0, 0, 0 },
66    {"blocking-io",      0,  POPT_ARG_VAL,    &blocking_io, 1, 0, 0 },
67    {"no-blocking-io",   0,  POPT_ARG_VAL,    &blocking_io, 0, 0, 0 },
68 @@ -718,6 +721,8 @@ int parse_arguments(int *argc, const cha
69                         (*argv)[i] = alloc_sanitize_path((*argv)[i], NULL);
70                 if (tmpdir)
71                         tmpdir = alloc_sanitize_path(tmpdir, curr_dir);
72 +               if (partial_dir)
73 +                       partial_dir = alloc_sanitize_path(partial_dir, curr_dir);
74                 if (compare_dest)
75                         compare_dest = alloc_sanitize_path(compare_dest, curr_dir);
76                 if (backup_dir)
77 @@ -770,6 +775,11 @@ int parse_arguments(int *argc, const cha
78  
79         if (inplace) {
80  #if HAVE_FTRUNCATE
81 +               if (partial_dir) {
82 +                       snprintf(err_buf, sizeof err_buf,
83 +                                "--inplace cannot be used with --partial-dir\n");
84 +                       return 0;
85 +               }
86                 keep_partial = 0;
87  #else
88                 snprintf(err_buf, sizeof err_buf,
89 @@ -777,7 +787,8 @@ int parse_arguments(int *argc, const cha
90                          am_server ? "server" : "client");
91                 return 0;
92  #endif
93 -       }
94 +       } else if (partial_dir)
95 +               keep_partial = 1;
96  
97         if (files_from) {
98                 char *colon;
99 @@ -969,7 +980,10 @@ void server_options(char **args,int *arg
100                 args[ac++] = arg;
101         }
102  
103 -       if (keep_partial)
104 +       if (partial_dir && am_sender) {
105 +               args[ac++] = "--partial-dir";
106 +               args[ac++] = partial_dir;
107 +       } else if (keep_partial)
108                 args[ac++] = "--partial";
109  
110         if (force_delete)
111 --- orig/receiver.c     2004-07-26 16:20:00
112 +++ receiver.c  2004-07-27 23:26:20
113 @@ -38,6 +38,7 @@ extern int preserve_perms;
114  extern int cvs_exclude;
115  extern int io_error;
116  extern char *tmpdir;
117 +extern char *partial_dir;
118  extern char *compare_dest;
119  extern int make_backups;
120  extern int do_progress;
121 @@ -342,7 +343,7 @@ int recv_files(int f_in, struct file_lis
122         char *fname, fbuf[MAXPATHLEN];
123         char template[MAXPATHLEN];
124         char fnametmp[MAXPATHLEN];
125 -       char *fnamecmp;
126 +       char *fnamecmp, *partialptr;
127         char fnamecmpbuf[MAXPATHLEN];
128         struct file_struct *file;
129         struct stats initial_stats;
130 @@ -410,8 +411,6 @@ int recv_files(int f_in, struct file_lis
131                 if (verbose > 2)
132                         rprintf(FINFO, "recv_files(%s)\n", safe_fname(fname));
133  
134 -               fnamecmp = fname;
135 -
136                 if (read_batch) {
137                         while (i > next_gen_i) {
138                                 next_gen_i = read_int(batch_gen_fd);
139 @@ -438,9 +437,22 @@ int recv_files(int f_in, struct file_lis
140                         continue;
141                 }
142  
143 +               if (partial_dir) {
144 +                       if ((partialptr = partial_dir_fname(fname)) != NULL)
145 +                               fnamecmp = partialptr;
146 +                       else
147 +                               fnamecmp = fname;
148 +               } else
149 +                       fnamecmp = partialptr = fname;
150 +
151                 /* open the file */
152                 fd1 = do_open(fnamecmp, O_RDONLY, 0);
153  
154 +               if (fd1 == -1 && fnamecmp != fname) {
155 +                       fnamecmp = fname;
156 +                       fd1 = do_open(fnamecmp, O_RDONLY, 0);
157 +               }
158 +
159                 if (fd1 == -1 && compare_dest != NULL) {
160                         /* try the file at compare_dest instead */
161                         pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
162 @@ -528,7 +540,8 @@ int recv_files(int f_in, struct file_lis
163                                 continue;
164                         }
165  
166 -                       cleanup_set(fnametmp, fname, file, fd1, fd2);
167 +                       if (partialptr)
168 +                               cleanup_set(fnametmp, partialptr, file, fd1, fd2);
169                 }
170  
171                 if (!am_server && verbose) /* log the transfer */
172 @@ -548,10 +561,20 @@ int recv_files(int f_in, struct file_lis
173                         exit_cleanup(RERR_FILEIO);
174                 }
175  
176 -               if (recv_ok || keep_partial || inplace)
177 +               if (recv_ok || inplace)
178                         finish_transfer(fname, fnametmp, file, recv_ok);
179 -               else
180 +               else if (keep_partial && partialptr
181 +                   && handle_partial_dir(partialptr, PDIR_CREATE))
182 +                       finish_transfer(partialptr, fnametmp, file, 0);
183 +               else {
184 +                       partialptr = NULL;
185                         do_unlink(fnametmp);
186 +               }
187 +
188 +               if (partialptr != fname && fnamecmp == partialptr && recv_ok) {
189 +                       do_unlink(partialptr);
190 +                       handle_partial_dir(partialptr, PDIR_DELETE);
191 +               }
192  
193                 cleanup_disable();
194  
195 @@ -559,9 +582,13 @@ int recv_files(int f_in, struct file_lis
196                         int msgtype = csum_length == SUM_LENGTH || read_batch ?
197                                 FERROR : FINFO;
198                         if (msgtype == FERROR || verbose) {
199 -                               char *errstr, *redostr;
200 -                               char *keptstr = keep_partial || inplace ?
201 -                                       "retain" : "discard";
202 +                               char *errstr, *redostr, *keptstr;
203 +                               if (!(keep_partial && partialptr) && !inplace)
204 +                                       keptstr = "discarded";
205 +                               else if (partial_dir)
206 +                                       keptstr = "put into partial-dir";
207 +                               else
208 +                                       keptstr = "retained";
209                                 if (msgtype == FERROR) {
210                                         errstr = "ERROR";
211                                         redostr = "";
212 @@ -570,7 +597,7 @@ int recv_files(int f_in, struct file_lis
213                                         redostr = " (will try again)";
214                                 }
215                                 rprintf(msgtype,
216 -                                       "%s: %s failed verification -- update %sed%s.\n",
217 +                                       "%s: %s failed verification -- update %s%s.\n",
218                                         errstr, safe_fname(fname),
219                                         keptstr, redostr);
220                         }
221 --- orig/rsync.h        2004-07-23 17:16:13
222 +++ rsync.h     2004-07-27 23:23:54
223 @@ -115,6 +115,9 @@
224  #define FULL_FLUSH     1
225  #define NORMAL_FLUSH   0
226  
227 +#define PDIR_CREATE    1
228 +#define PDIR_DELETE    0
229 +
230  
231  /* Log-message categories.  FLOG is only used on the daemon side to
232   * output messages to the log file. */
233 --- orig/rsync.yo       2004-07-24 16:52:10
234 +++ rsync.yo    2004-07-28 02:26:19
235 @@ -317,6 +317,7 @@ verb(
236       --ignore-errors         delete even if there are I/O errors
237       --max-delete=NUM        don't delete more than NUM files
238       --partial               keep partially transferred files
239 +     --partial-dir=DIR       put a partially transferred file into DIR
240       --force                 force deletion of dirs even if not empty
241       --numeric-ids           don't map uid/gid values by user/group name
242       --timeout=TIME          set I/O timeout in seconds
243 @@ -865,6 +866,29 @@ it is more desirable to keep partially t
244  --partial option tells rsync to keep the partial file which should
245  make a subsequent transfer of the rest of the file much faster.
246  
247 +dit(bf(--partial-dir=DIR)) Turns on --partial mode, but tells rsync to
248 +put a partially transferred file into DIR instead of writing out the
249 +file to the destination dir.  Rsync will also use a file found in this
250 +dir as data to speed up the transfer (i.e. when you redo the send after
251 +rsync creates a partial file) and delete such a file after it has served
252 +its purpose.
253 +
254 +Rsync will create the dir if it is missing (just the last dir -- not the
255 +whole path).  This makes it easy to use a relative path (such as
256 +"--partial-dir=.rsync-partial") to have rsync create the partial-directory
257 +in the destination file's directory (rsync will also try to remove the DIR
258 +if a partial file was found to exist at the start of the transfer and the
259 +DIR was specified as a relative path).
260 +
261 +If you are deleting files on the destination and your partial-dir is
262 +inside the destination hierarchy, make sure you specify an exclude to
263 +prevent the partial file from being deleted (it could get deleted at the
264 +end of the transfer when using --delete-after, or at the beginning of the
265 +transfer when using --delete).  E.g. "--exclude=.rsync-partial/".
266 +
267 +IMPORTANT: the --partial-dir should not be writable by other users to
268 +avoid a security risk.  E.g. AVOID "/tmp".
269 +
270  dit(bf(--progress)) This option tells rsync to print information
271  showing the progress of the transfer. This gives a bored user
272  something to watch.
273 --- orig/t_stub.c       2004-05-15 20:10:13
274 +++ t_stub.c    2004-07-24 17:00:35
275 @@ -28,6 +28,7 @@
276  
277  int modify_window = 0;
278  int module_id = -1;
279 +char *partial_dir;
280  struct exclude_list_struct server_exclude_list;
281  
282   void rprintf(UNUSED(enum logcode code), const char *format, ...)
283 --- orig/util.c 2004-07-26 16:33:24
284 +++ util.c      2004-07-27 23:25:20
285 @@ -31,6 +31,7 @@ extern int verbose;
286  extern int dry_run;
287  extern int module_id;
288  extern int modify_window;
289 +extern char *partial_dir;
290  extern struct exclude_list_struct server_exclude_list;
291  
292  int sanitize_paths = 0;
293 @@ -970,6 +971,66 @@ char *full_fname(const char *fn)
294         return result;
295  }
296  
297 +static char partial_fname[MAXPATHLEN];
298 +
299 +char *partial_dir_fname(const char *fname)
300 +{
301 +       char *t = partial_fname;
302 +       int sz = sizeof partial_fname;
303 +       const char *fn;
304 +
305 +       if ((fn = strrchr(fname, '/')) != NULL) {
306 +               fn++;
307 +               if (*partial_dir != '/') {
308 +                       int len = fn - fname;
309 +                       strncpy(t, fname, len); /* safe */
310 +                       t += len;
311 +                       sz -= len;
312 +               }
313 +       } else
314 +               fn = fname;
315 +       if ((int)pathjoin(t, sz, partial_dir, fn) >= sz)
316 +               return NULL;
317 +
318 +       return partial_fname;
319 +}
320 +
321 +/* If no --partial-dir option was specified, we don't need to do anything
322 + * (the partial-dir is essentially '.'), so just return success. */
323 +int handle_partial_dir(const char *fname, int create)
324 +{
325 +       char *fn, *dir;
326 +
327 +       if (fname != partial_fname)
328 +               return 1;
329 +       if (!create && *partial_dir == '/')
330 +               return 1;
331 +       if (!(fn = strrchr(partial_fname, '/')))
332 +               return 1;
333 +
334 +       *fn = '\0';
335 +       dir = partial_fname;
336 +       if (create) {
337 +               STRUCT_STAT st;
338 +#if SUPPORT_LINKS
339 +               int statret = do_lstat(dir, &st);
340 +#else
341 +               int statret = do_stat(dir, &st);
342 +#endif
343 +               if (statret == 0 && !S_ISDIR(st.st_mode)) {
344 +                       if (do_unlink(dir) < 0)
345 +                               return 0;
346 +                       statret = -1;
347 +               }
348 +               if (statret < 0 && do_mkdir(dir, 0700) < 0)
349 +                       return 0;
350 +       } else
351 +               do_rmdir(dir);
352 +       *fn = '/';
353 +
354 +       return 1;
355 +}
356 +
357  /** We need to supply our own strcmp function for file list comparisons
358     to ensure that signed/unsigned usage is consistent between machines. */
359  int u_strcmp(const char *cs1, const char *cs2)