Fixed a failing hunk.
[rsync/rsync-patches.git] / delay-renames.diff
1 Delay the renaming of all the temp files until the end of the transfer.
2
3 --- options.c   1 Jan 2005 21:08:14 -0000       1.193
4 +++ options.c   10 Jan 2005 10:16:54 -0000
5 @@ -97,6 +97,7 @@ int modify_window = 0;
6  int blocking_io = -1;
7  int checksum_seed = 0;
8  int inplace = 0;
9 +int delay_renames = 0;
10  long block_size = 0; /* "long" because popt can't set an int32. */
11  
12  
13 @@ -278,6 +279,7 @@ void usage(enum logcode F)
14    rprintf(F,"     --max-size=SIZE         don't transfer any file larger than SIZE\n");
15    rprintf(F,"     --partial               keep partially transferred files\n");
16    rprintf(F,"     --partial-dir=DIR       put a partially transferred file into DIR\n");
17 +  rprintf(F,"     --delay-renames         renames transferred files into place at end\n");
18    rprintf(F,"     --force                 force deletion of directories even if not empty\n");
19    rprintf(F,"     --numeric-ids           don't map uid/gid values by user/group name\n");
20    rprintf(F,"     --timeout=TIME          set I/O timeout in seconds\n");
21 @@ -390,6 +392,7 @@ static struct poptOption long_options[] 
22    {"progress",         0,  POPT_ARG_NONE,   &do_progress, 0, 0, 0 },
23    {"partial",          0,  POPT_ARG_NONE,   &keep_partial, 0, 0, 0 },
24    {"partial-dir",      0,  POPT_ARG_STRING, &partial_dir, 0, 0, 0 },
25 +  {"delay-renames",    0,  POPT_ARG_NONE,   &delay_renames, 0, 0, 0 },
26    {"ignore-errors",    0,  POPT_ARG_NONE,   &ignore_errors, 0, 0, 0 },
27    {"blocking-io",      0,  POPT_ARG_VAL,    &blocking_io, 1, 0, 0 },
28    {"no-blocking-io",   0,  POPT_ARG_VAL,    &blocking_io, 0, 0, 0 },
29 @@ -944,11 +947,15 @@ int parse_arguments(int *argc, const cha
30                         bwlimit_writemax = 512;
31         }
32  
33 +       if (delay_renames && !partial_dir)
34 +               partial_dir = ".~tmp~";
35 +
36         if (inplace) {
37  #if HAVE_FTRUNCATE
38                 if (partial_dir) {
39                         snprintf(err_buf, sizeof err_buf,
40 -                                "--inplace cannot be used with --partial-dir\n");
41 +                                "--inplace cannot be used with --%s\n",
42 +                                delay_renames ? "delay-renames" : "partial-dir");
43                         return 0;
44                 }
45                 keep_partial = 0;
46 @@ -1185,6 +1192,8 @@ void server_options(char **args,int *arg
47         if (partial_dir && am_sender) {
48                 args[ac++] = "--partial-dir";
49                 args[ac++] = partial_dir;
50 +               if (delay_renames)
51 +                       args[ac++] = "--delay-renames";
52         } else if (keep_partial)
53                 args[ac++] = "--partial";
54  
55 --- receiver.c  10 Jan 2005 10:03:10 -0000      1.113
56 +++ receiver.c  10 Jan 2005 10:16:54 -0000
57 @@ -52,6 +52,7 @@ extern int orig_umask;
58  extern int keep_partial;
59  extern int checksum_seed;
60  extern int inplace;
61 +extern int delay_renames;
62  
63  extern struct exclude_list_struct server_exclude_list;
64  
65 @@ -344,6 +345,7 @@ int recv_files(int f_in, struct file_lis
66         char fnametmp[MAXPATHLEN];
67         char *fnamecmp, *partialptr;
68         char fnamecmpbuf[MAXPATHLEN];
69 +       uchar *delayed_bits = NULL;
70         struct file_struct *file;
71         struct stats initial_stats;
72         int save_make_backups = make_backups;
73 @@ -357,6 +359,12 @@ int recv_files(int f_in, struct file_lis
74                 flist->hlink_pool = NULL;
75         }
76  
77 +       if (delay_renames) {
78 +               if (!(delayed_bits = new_array(char, (flist->count + 7) / 8)))
79 +                       out_of_memory("recv_files");
80 +               memset(delayed_bits, 0, (flist->count + 7) / 8);
81 +       }
82 +
83         while (1) {
84                 cleanup_disable();
85  
86 @@ -566,7 +574,7 @@ int recv_files(int f_in, struct file_lis
87                         exit_cleanup(RERR_FILEIO);
88                 }
89  
90 -               if (recv_ok || inplace) {
91 +               if ((recv_ok && !delay_renames) || inplace) {
92                         finish_transfer(fname, fnametmp, file, recv_ok, 1);
93                         if (partialptr != fname && fnamecmp == partialptr) {
94                                 do_unlink(partialptr);
95 @@ -576,6 +584,8 @@ int recv_files(int f_in, struct file_lis
96                     && handle_partial_dir(partialptr, PDIR_CREATE)) {
97                         finish_transfer(partialptr, fnametmp, file, recv_ok,
98                                         !partial_dir);
99 +                       if (delay_renames && recv_ok)
100 +                               delayed_bits[i/8] |= 1 << (i % 8);
101                 } else {
102                         partialptr = NULL;
103                         do_unlink(fnametmp);
104 @@ -615,6 +625,33 @@ int recv_files(int f_in, struct file_lis
105         }
106         make_backups = save_make_backups;
107  
108 +       if (delay_renames) {
109 +               for (i = 0; i < flist->count; i++) {
110 +                       struct file_struct *file = flist->files[i];
111 +                       if (!file->basename
112 +                        || !(delayed_bits[i/8] & (1 << (i % 8))))
113 +                               continue;
114 +                       fname = local_name ? local_name : f_name(file);
115 +                       partialptr = partial_dir_fname(fname);
116 +                       if (partialptr) {
117 +                               if (make_backups && !make_backup(fname))
118 +                                       continue;
119 +                               if (verbose > 2) {
120 +                                       rprintf(FINFO, "renaming %s to %s\n",
121 +                                               partialptr, fname);
122 +                               }
123 +                               if (do_rename(partialptr, fname) < 0) {
124 +                                       rsyserr(FERROR, errno,
125 +                                               "rename failed for %s (from %s)",
126 +                                               fname, partialptr);
127 +                               } else {
128 +                                       handle_partial_dir(partialptr,
129 +                                                          PDIR_DELETE);
130 +                               }
131 +                       }
132 +               }
133 +       }
134 +
135         if (delete_after && recurse && !local_name && flist->count > 0)
136                 delete_files(flist);
137  
138 --- rsync.yo    8 Dec 2004 17:30:40 -0000       1.205
139 +++ rsync.yo    10 Jan 2005 10:16:56 -0000
140 @@ -348,6 +348,7 @@ verb(
141       --max-size=SIZE         don't transfer any file larger than SIZE
142       --partial               keep partially transferred files
143       --partial-dir=DIR       put a partially transferred file into DIR
144 +     --delay-renames         renames transferred files into place at end
145       --force                 force deletion of dirs even if not empty
146       --numeric-ids           don't map uid/gid values by user/group name
147       --timeout=TIME          set I/O timeout in seconds
148 @@ -544,9 +545,10 @@ or appended data, and also on systems th
149  bound.
150  
151  The option implies --partial (since an interrupted transfer does not delete
152 -the file), but conflicts with --partial-dir, --compare-dest, --copy-dest, and
153 ---link-dest (a future rsync version will hopefully update the protocol to
154 -remove some of these restrictions).
155 +the file), but conflicts with --partial-dir, --compare-dest, --copy-dest,
156 +--link-dest, and --delay-renames.
157 +(A future rsync version will hopefully update the protocol to
158 +remove some of these restrictions.)
159  
160  WARNING: The file's data will be in an inconsistent state during the
161  transfer (and possibly afterward if the transfer gets interrupted), so you
162 @@ -967,6 +969,17 @@ environment and then just use the -P opt
163  does not look for this environment value is when --inplace was also
164  specified (since --inplace conflicts with --partial-dir).
165  
166 +dit(bf(--delay-renames)) This option puts the temporary file from each
167 +updated file into the file's partial-dir (see above) until the end of the
168 +transfer, at which time all the files are renamed into place in rapid
169 +succession.  This attempts to make the updating of the files a little more
170 +atomic.  If you don't specify the --partial-dir option, this option will
171 +cause it to default to ".~tmp~" (RSYNC_PARTIAL_DIR is not consulted for
172 +this value).  Conflicts with --inplace.
173 +
174 +See also the "atomic-rsync" perl script in the "support" subdir for an
175 +update algorithm that is even more atomic (it uses --link-dest).
176 +
177  dit(bf(--progress)) This option tells rsync to print information
178  showing the progress of the transfer. This gives a bored user
179  something to watch.