Updated to apply to the latest source.
[rsync-patches.git] / backup-dir-dels.diff
1 This patches creates two new command line options as follows:
2         --backup-dir-dels=DIR
3         --suffix-dels=SUFFIX
4
5 The backup-dir-dels and suffix-dels options give the ability to store
6 backup of removed files on the receiver in different directories or with
7 different suffix than the backup of files that have been changed but that
8 are still on the source drive.  Both commands can be combined.
9
10 The default behaviour if one or both of the options are not specified
11 is the previous behaviour, both backups use the same directory or
12 suffix.
13
14 Marc St-Onge
15
16 To use this patch, run these commands for a successful build:
17
18     patch -p1 <patches/backup-deleted.diff
19     patch -p1 <patches/backup-dir-dels.diff
20     ./configure                                 (optional if already run)
21     make
22
23 based-on: patch/master/backup-deleted
24 diff --git a/backup.c b/backup.c
25 --- a/backup.c
26 +++ b/backup.c
27 @@ -29,25 +29,32 @@ extern int preserve_specials;
28  extern int preserve_links;
29  extern int safe_symlinks;
30  extern int backup_dir_len;
31 +extern int backup_dir_dels_len;
32  extern unsigned int backup_dir_remainder;
33 +extern unsigned int backup_dir_dels_remainder;
34  extern char backup_dir_buf[MAXPATHLEN];
35 +extern char backup_dir_dels_buf[MAXPATHLEN];
36  extern char *backup_suffix;
37 +extern char *backup_suffix_dels;
38  extern char *backup_dir;
39 +extern char *backup_dir_dels;
40 +
41 +static BOOL deleting;
42  
43  /* Returns -1 on error, 0 on missing dir, and 1 on present dir. */
44 -static int validate_backup_dir(void)
45 +static int validate_backup_dir(char *buf)
46  {
47         STRUCT_STAT st;
48  
49 -       if (do_lstat(backup_dir_buf, &st) < 0) {
50 +       if (do_lstat(buf, &st) < 0) {
51                 if (errno == ENOENT)
52                         return 0;
53 -               rsyserr(FERROR, errno, "backup lstat %s failed", backup_dir_buf);
54 +               rsyserr(FERROR, errno, "backup lstat %s failed", buf);
55                 return -1;
56         }
57         if (!S_ISDIR(st.st_mode)) {
58                 int flags = get_del_for_flag(st.st_mode) | DEL_FOR_BACKUP | DEL_RECURSE;
59 -               if (delete_item(backup_dir_buf, st.st_mode, flags) == 0)
60 +               if (delete_item(buf, st.st_mode, flags) == 0)
61                         return 0;
62                 return -1;
63         }
64 @@ -58,20 +65,20 @@ static int validate_backup_dir(void)
65   * backup_dir_buf.  Any new directories (compared to the prior backup
66   * path) are ensured to exist as directories, replacing anything else
67   * that may be in the way (e.g. a symlink). */
68 -static BOOL copy_valid_path(const char *fname)
69 +static BOOL copy_valid_path(const char *fname, char *buf, int prefix_len, unsigned int remainder, const char *suffix)
70  {
71         const char *f;
72         int val;
73         BOOL ret = True;
74         stat_x sx;
75 -       char *b, *rel = backup_dir_buf + backup_dir_len, *name = rel;
76 +       char *b, *rel = buf + prefix_len, *name = rel;
77  
78         for (f = fname, b = rel; *f && *f == *b; f++, b++) {
79                 if (*b == '/')
80                         name = b + 1;
81         }
82  
83 -       if (stringjoin(rel, backup_dir_remainder, fname, backup_suffix, NULL) >= backup_dir_remainder) {
84 +       if (stringjoin(rel, remainder, fname, suffix, NULL) >= remainder) {
85                 rprintf(FERROR, "backup filename too long\n");
86                 *name = '\0';
87                 return False;
88 @@ -82,7 +89,7 @@ static BOOL copy_valid_path(const char *fname)
89                         return True;
90                 *b = '\0';
91  
92 -               val = validate_backup_dir();
93 +               val = validate_backup_dir(buf);
94                 if (val == 0)
95                         break;
96                 if (val < 0) {
97 @@ -98,9 +105,9 @@ static BOOL copy_valid_path(const char *fname)
98         for ( ; b; name = b + 1, b = strchr(name, '/')) {
99                 *b = '\0';
100  
101 -               while (do_mkdir(backup_dir_buf, ACCESSPERMS) < 0) {
102 +               while (do_mkdir(buf, ACCESSPERMS) < 0) {
103                         if (errno == EEXIST) {
104 -                               val = validate_backup_dir();
105 +                               val = validate_backup_dir(buf);
106                                 if (val > 0)
107                                         break;
108                                 if (val == 0)
109 @@ -134,7 +141,7 @@ static BOOL copy_valid_path(const char *fname)
110                                 free_xattr(&sx);
111                         }
112  #endif
113 -                       set_file_attrs(backup_dir_buf, file, NULL, NULL, 0);
114 +                       set_file_attrs(buf, file, NULL, NULL, 0);
115                         unmake_file(file);
116                 }
117  
118 @@ -156,28 +163,34 @@ static BOOL copy_valid_path(const char *fname)
119  /* Make a complete pathname for backup file and verify any new path elements. */
120  char *get_backup_name(const char *fname)
121  {
122 +       char *buf = deleting ? backup_dir_dels_buf : backup_dir_buf;
123 +       char *suffix = deleting ? backup_suffix_dels : backup_suffix;
124 +
125         if (backup_dir) {
126                 static int initialized = 0;
127 -               if (!initialized) {
128 +               int prefix_len = deleting ? backup_dir_dels_len : backup_dir_len;
129 +               unsigned int remainder = deleting ? backup_dir_dels_remainder : backup_dir_remainder;
130 +               int init_bits = deleting ? 2 : 1;
131 +               if (!(initialized & init_bits)) {
132                         int ret;
133 -                       if (backup_dir_len > 1)
134 -                               backup_dir_buf[backup_dir_len-1] = '\0';
135 -                       ret = make_path(backup_dir_buf, 0);
136 -                       if (backup_dir_len > 1)
137 -                               backup_dir_buf[backup_dir_len-1] = '/';
138 +                       if (prefix_len > 1)
139 +                               buf[prefix_len-1] = '\0';
140 +                       ret = make_path(buf, 0);
141 +                       if (prefix_len > 1)
142 +                               buf[prefix_len-1] = '/';
143                         if (ret < 0)
144                                 return NULL;
145 -                       initialized = 1;
146 +                       initialized |= init_bits;
147                 }
148                 /* copy fname into backup_dir_buf while validating the dirs. */
149 -               if (copy_valid_path(fname))
150 -                       return backup_dir_buf;
151 +               if (copy_valid_path(fname, buf, prefix_len, remainder, suffix))
152 +                       return buf;
153                 /* copy_valid_path() has printed an error message. */
154                 return NULL;
155         }
156  
157 -       if (stringjoin(backup_dir_buf, MAXPATHLEN, fname, backup_suffix, NULL) < MAXPATHLEN)
158 -               return backup_dir_buf;
159 +       if (stringjoin(buf, MAXPATHLEN, fname, suffix, NULL) < MAXPATHLEN)
160 +               return buf;
161  
162         rprintf(FERROR, "backup filename too long\n");
163         return NULL;
164 @@ -352,3 +365,13 @@ int make_backup(const char *fname, BOOL prefer_rename)
165                 rprintf(FINFO, "backed up %s to %s\n", fname, buf);
166         return ret;
167  }
168 +
169 +/* backup switch routine called only when backing-up removed file */
170 +int safe_delete(const char *fname)
171 +{
172 +       int ret;
173 +       deleting = 1;
174 +       ret = make_backup(fname, True);
175 +       deleting = 0;
176 +       return ret;
177 +}
178 diff --git a/delete.c b/delete.c
179 --- a/delete.c
180 +++ b/delete.c
181 @@ -28,16 +28,23 @@ extern int max_delete;
182  extern char *backup_dir;
183  extern char *backup_suffix;
184  extern int backup_suffix_len;
185 +extern char *backup_dir_dels;
186 +extern char *backup_suffix_dels;
187 +extern int backup_suffix_dels_len;
188  extern struct stats stats;
189  
190  int ignore_perishable = 0;
191  int non_perishable_cnt = 0;
192  int skipped_deletes = 0;
193  
194 +/* Function now compares both backup_suffix and backup_suffix_dels. */
195  static inline int is_backup_file(char *fn)
196  {
197         int k = strlen(fn) - backup_suffix_len;
198 -       return k > 0 && strcmp(fn+k, backup_suffix) == 0;
199 +       if (k > 0 && strcmp(fn+k, backup_suffix) == 0)
200 +               return 1;
201 +       k += backup_suffix_len - backup_suffix_dels_len;
202 +       return k > 0 && strcmp(fn+k, backup_suffix_dels) == 0;
203  }
204  
205  /* The directory is about to be deleted: if DEL_RECURSE is given, delete all
206 @@ -162,9 +169,9 @@ enum delret delete_item(char *fbuf, uint16 mode, uint16 flags)
207                 what = "rmdir";
208                 ok = do_rmdir(fbuf) == 0;
209         } else {
210 -               if (make_backups > 0 && !(flags & DEL_FOR_BACKUP) && (backup_dir || !is_backup_file(fbuf))) {
211 +               if (make_backups > 0 && !(flags & DEL_FOR_BACKUP) && (backup_dir_dels || !is_backup_file(fbuf))) {
212                         what = "make_backup";
213 -                       ok = make_backup(fbuf, True);
214 +                       ok = safe_delete(fbuf);
215                         if (ok == 2) {
216                                 what = "unlink";
217                                 ok = robust_unlink(fbuf) == 0;
218 diff --git a/options.c b/options.c
219 --- a/options.c
220 +++ b/options.c
221 @@ -153,10 +153,14 @@ int no_detach
222  int write_batch = 0;
223  int read_batch = 0;
224  int backup_dir_len = 0;
225 +int backup_dir_dels_len = 0;
226  int backup_suffix_len;
227 +int backup_suffix_dels_len;
228  unsigned int backup_dir_remainder;
229 +unsigned int backup_dir_dels_remainder;
230  
231  char *backup_suffix = NULL;
232 +char *backup_suffix_dels = NULL;
233  char *tmpdir = NULL;
234  char *partial_dir = NULL;
235  char *basis_dir[MAX_BASIS_DIRS+1];
236 @@ -168,7 +172,9 @@ char *stdout_format = NULL;
237  char *password_file = NULL;
238  char *rsync_path = RSYNC_PATH;
239  char *backup_dir = NULL;
240 +char *backup_dir_dels = NULL;
241  char backup_dir_buf[MAXPATHLEN];
242 +char backup_dir_dels_buf[MAXPATHLEN];
243  char *sockopts = NULL;
244  char *usermap = NULL;
245  char *groupmap = NULL;
246 @@ -681,6 +687,8 @@ void usage(enum logcode F)
247    rprintf(F,"     --backup-deleted        make backups only of deleted files\n");
248    rprintf(F,"     --backup-dir=DIR        make backups into hierarchy based in DIR\n");
249    rprintf(F,"     --suffix=SUFFIX         set backup suffix (default %s w/o --backup-dir)\n",BACKUP_SUFFIX);
250 +  rprintf(F,"     --backup-dir-dels=DIR   backup removed files into hierarchy based in DIR\n");
251 +  rprintf(F,"     --suffix-dels=SUFFIX    set removed-files suffix (def. --suffix w/o b-d-d)\n");
252    rprintf(F," -u, --update                skip files that are newer on the receiver\n");
253    rprintf(F,"     --inplace               update destination files in-place (SEE MAN PAGE)\n");
254    rprintf(F,"     --append                append data onto shorter files\n");
255 @@ -999,7 +1007,9 @@ static struct poptOption long_options[] = {
256    {"backup-deleted",   0,  POPT_ARG_VAL,    &make_backups, 1, 0, 0 },
257    {"no-backup",        0,  POPT_ARG_VAL,    &make_backups, 0, 0, 0 },
258    {"backup-dir",       0,  POPT_ARG_STRING, &backup_dir, 0, 0, 0 },
259 +  {"backup-dir-dels",  0,  POPT_ARG_STRING, &backup_dir_dels, 0, 0, 0 },
260    {"suffix",           0,  POPT_ARG_STRING, &backup_suffix, 0, 0, 0 },
261 +  {"suffix-dels",      0,  POPT_ARG_STRING, &backup_suffix_dels, 0, 0, 0 },
262    {"list-only",        0,  POPT_ARG_VAL,    &list_only, 2, 0, 0 },
263    {"read-batch",       0,  POPT_ARG_STRING, &batch_name, OPT_READ_BATCH, 0, 0 },
264    {"write-batch",      0,  POPT_ARG_STRING, &batch_name, OPT_WRITE_BATCH, 0, 0 },
265 @@ -2101,6 +2111,8 @@ int parse_arguments(int *argc_p, const char ***argv_p)
266                         tmpdir = sanitize_path(NULL, tmpdir, NULL, 0, SP_DEFAULT);
267                 if (backup_dir)
268                         backup_dir = sanitize_path(NULL, backup_dir, NULL, 0, SP_DEFAULT);
269 +               if (backup_dir_dels)
270 +                       backup_dir_dels = sanitize_path(NULL, backup_dir_dels, NULL, 0, SP_DEFAULT);
271         }
272         if (daemon_filter_list.head && !am_sender) {
273                 filter_rule_list *elp = &daemon_filter_list;
274 @@ -2122,6 +2134,14 @@ int parse_arguments(int *argc_p, const char ***argv_p)
275                         if (check_filter(elp, FLOG, dir, 1) < 0)
276                                 goto options_rejected;
277                 }
278 +               /* Clean backup_dir_dels same as for backup_dir */
279 +               if (backup_dir_dels) {
280 +                       if (!*backup_dir_dels)
281 +                               goto options_rejected;
282 +                       clean_fname(backup_dir_dels, 1);
283 +                       if (check_filter(elp, FLOG, backup_dir_dels, 1) < 0)
284 +                               goto options_rejected;
285 +               }
286         }
287  
288         if (!backup_suffix)
289 @@ -2133,6 +2153,20 @@ int parse_arguments(int *argc_p, const char ***argv_p)
290                         backup_suffix);
291                 return 0;
292         }
293 +       /* --suffix-dels defaults to --suffix, or empty for a client given an
294 +        * explicit --backup-dir-dels (just as --suffix defaults to empty when
295 +        * a --backup-dir is given).  The second case does not apply to the
296 +        * server for consistency with server_options, which sends --suffix-dels
297 +        * to the server iff it differs from --suffix. */
298 +       if (!backup_suffix_dels)
299 +               backup_suffix_dels = backup_dir_dels && !am_server ? "" : backup_suffix;
300 +       backup_suffix_dels_len = strlen(backup_suffix_dels);
301 +       if (strchr(backup_suffix_dels, '/') != NULL) {
302 +               snprintf(err_buf, sizeof err_buf,
303 +                       "--suffix-dels cannot contain slashes: %s\n",
304 +                       backup_suffix_dels);
305 +               return 0;
306 +       }
307         if (backup_dir) {
308                 size_t len;
309                 while (*backup_dir == '.' && backup_dir[1] == '/')
310 @@ -2168,6 +2202,34 @@ int parse_arguments(int *argc_p, const char ***argv_p)
311                         "P *%s", backup_suffix);
312                 parse_filter_str(&filter_list, backup_dir_buf, rule_template(0), 0);
313         }
314 +       if (backup_dir_dels) {
315 +               backup_dir_dels_len = strlcpy(backup_dir_dels_buf, backup_dir_dels, sizeof backup_dir_dels_buf);
316 +               backup_dir_dels_remainder = sizeof backup_dir_dels_buf - backup_dir_dels_len;
317 +               if (backup_dir_dels_remainder < 32) {
318 +                       snprintf(err_buf, sizeof err_buf,
319 +                               "the --backup-dir-dels path is WAY too long.\n");
320 +                       return 0;
321 +               }
322 +               if (backup_dir_dels_buf[backup_dir_dels_len - 1] != '/') {
323 +                       backup_dir_dels_buf[backup_dir_dels_len++] = '/';
324 +                       backup_dir_dels_buf[backup_dir_dels_len] = '\0';
325 +               }
326 +               if (INFO_GTE(BACKUP, 1) && !am_sender)
327 +                       rprintf(FINFO, "backup_dir_dels is %s\n", backup_dir_dels_buf);
328 +       } else if (backup_dir) {
329 +               backup_dir_dels = backup_dir;
330 +               backup_dir_dels_len = backup_dir_len;
331 +               backup_dir_dels_remainder = backup_dir_remainder;
332 +               strlcpy(backup_dir_dels_buf, backup_dir_buf, sizeof backup_dir_buf);
333 +       } else if (!backup_suffix_dels_len && (!am_server || !am_sender)) {
334 +               snprintf(err_buf, sizeof err_buf,
335 +                       "--suffix-dels cannot be a null string without --backup-dir-dels\n");
336 +               return 0;
337 +       } else if (make_backups && delete_mode && !delete_excluded && !am_server) {
338 +               snprintf(backup_dir_dels_buf, sizeof backup_dir_dels_buf,
339 +                       "P *%s", backup_suffix_dels);
340 +               parse_filter_str(&filter_list, backup_dir_dels_buf, rule_template(0), 0);
341 +       }
342  
343         if (preserve_times) {
344                 preserve_times = PRESERVE_FILE_TIMES;
345 @@ -2594,6 +2656,10 @@ void server_options(char **args, int *argc_p)
346                 args[ac++] = "--backup-dir";
347                 args[ac++] = backup_dir;
348         }
349 +       if (backup_dir_dels && backup_dir_dels != backup_dir) {
350 +               args[ac++] = "--backup-dir-dels";
351 +               args[ac++] = backup_dir_dels;
352 +       }
353  
354         /* Only send --suffix if it specifies a non-default value. */
355         if (strcmp(backup_suffix, backup_dir ? "" : BACKUP_SUFFIX) != 0) {
356 @@ -2602,6 +2668,14 @@ void server_options(char **args, int *argc_p)
357                         goto oom;
358                 args[ac++] = arg;
359         }
360 +       /* Only send --suffix-dels if it specifies a value different from the
361 +        * --suffix value, which would normally be used for deletions too. */
362 +       if (strcmp(backup_suffix_dels, backup_suffix) != 0) {
363 +               /* We use the following syntax to avoid weirdness with '~'. */
364 +               if (asprintf(&arg, "--suffix-dels=%s", backup_suffix_dels) < 0)
365 +                       goto oom;
366 +               args[ac++] = arg;
367 +       }
368  
369         if (checksum_choice) {
370                 if (asprintf(&arg, "--checksum-choice=%s", checksum_choice) < 0)