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