5dec3084f9784f8d9d73b823baf5b49e13a7ddf8
[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-dir-dels.diff
19     ./configure                                 (optional if already run)
20     make
21
22 diff --git a/backup.c b/backup.c
23 --- a/backup.c
24 +++ b/backup.c
25 @@ -29,10 +29,17 @@ extern int preserve_specials;
26  extern int preserve_links;
27  extern int safe_symlinks;
28  extern int backup_dir_len;
29 +extern int backup_dir_dels_len;
30  extern unsigned int backup_dir_remainder;
31 +extern unsigned int backup_dir_dels_remainder;
32  extern char backup_dir_buf[MAXPATHLEN];
33 +extern char backup_dir_dels_buf[MAXPATHLEN];
34  extern char *backup_suffix;
35 +extern char *backup_suffix_dels;
36  extern char *backup_dir;
37 +extern char *backup_dir_dels;
38 +
39 +static int deleting;
40  
41  /* make a complete pathname for backup file */
42  char *get_backup_name(const char *fname)
43 @@ -51,11 +58,28 @@ char *get_backup_name(const char *fname)
44         return NULL;
45  }
46  
47 +static char *get_delete_name(const char *fname)
48 +{
49 +       if (backup_dir_dels) {
50 +               if (stringjoin(backup_dir_dels_buf + backup_dir_dels_len, backup_dir_dels_remainder,
51 +                              fname, backup_suffix_dels, NULL) < backup_dir_dels_remainder)
52 +                       return backup_dir_dels_buf;
53 +       } else {
54 +               if (stringjoin(backup_dir_dels_buf, MAXPATHLEN,
55 +                              fname, backup_suffix_dels, NULL) < MAXPATHLEN)
56 +                       return backup_dir_dels_buf;
57 +       }
58 +
59 +       rprintf(FERROR, "delete filename too long\n");
60 +       return NULL;
61 +}
62 +
63  /* simple backup creates a backup with a suffix in the same directory */
64  static int make_simple_backup(const char *fname)
65  {
66         int rename_errno;
67 -       const char *fnamebak = get_backup_name(fname);
68 +       const char *fnamebak = deleting ? get_delete_name(fname)
69 +                                       : get_backup_name(fname);
70  
71         if (!fnamebak)
72                 return 0;
73 @@ -96,7 +120,7 @@ int make_bak_dir(const char *fullpath)
74  {
75         char fbuf[MAXPATHLEN], *rel, *end, *p;
76         struct file_struct *file;
77 -       int len = backup_dir_len;
78 +       int len = deleting ? backup_dir_dels_len : backup_dir_len;
79         stat_x sx;
80  
81         while (*fullpath == '.' && fullpath[1] == '/') {
82 @@ -221,7 +245,8 @@ static int keep_backup(const char *fname)
83         if (!(file = make_file(fname, NULL, NULL, 0, NO_FILTERS)))
84                 return 1; /* the file could have disappeared */
85  
86 -       if (!(buf = get_backup_name(fname))) {
87 +       buf = deleting ? get_delete_name(fname) : get_backup_name(fname);
88 +       if (!buf) {
89                 unmake_file(file);
90                 return 0;
91         }
92 @@ -356,7 +381,17 @@ static int keep_backup(const char *fname)
93  /* main backup switch routine */
94  int make_backup(const char *fname)
95  {
96 -       if (backup_dir)
97 +       if (deleting ? backup_dir_dels : backup_dir)
98                 return keep_backup(fname);
99         return make_simple_backup(fname);
100  }
101 +
102 +/* backup switch routine called only when backing-up removed file */
103 +int safe_delete(char *fname)
104 +{
105 +       int ret;
106 +       deleting = 1;
107 +       ret = make_backup(fname);
108 +       deleting = 0;
109 +       return ret;
110 +}
111 diff --git a/generator.c b/generator.c
112 --- a/generator.c
113 +++ b/generator.c
114 @@ -96,6 +96,9 @@ extern uid_t our_uid;
115  extern char *backup_dir;
116  extern char *backup_suffix;
117  extern int backup_suffix_len;
118 +extern char *backup_dir_dels;
119 +extern char *backup_suffix_dels;
120 +extern int backup_suffix_dels_len;
121  extern struct file_list *cur_flist, *first_flist, *dir_flist;
122  extern struct filter_list_struct daemon_filter_list;
123  
124 @@ -142,10 +145,15 @@ static void handle_skipped_hlink(struct file_struct *file, int itemizing,
125                                  enum logcode code, int f_out);
126  #endif
127  
128 +
129 +/* Function now compares both backup_suffix and backup_suffix_dels. */
130  static int is_backup_file(char *fn)
131  {
132         int k = strlen(fn) - backup_suffix_len;
133 -       return k > 0 && strcmp(fn+k, backup_suffix) == 0;
134 +       if (k > 0 && strcmp(fn+k, backup_suffix) == 0)
135 +               return 1;
136 +       k += backup_suffix_len - backup_suffix_dels_len;
137 +       return k > 0 && strcmp(fn+k, backup_suffix_dels) == 0;
138  }
139  
140  /* Delete a file or directory.  If DEL_RECURSE is set in the flags, this will
141 @@ -193,9 +201,9 @@ static enum delret delete_item(char *fbuf, uint16 mode, uint16 flags)
142         if (S_ISDIR(mode)) {
143                 what = "rmdir";
144                 ok = do_rmdir(fbuf) == 0;
145 -       } else if (make_backups > 0 && (backup_dir || !is_backup_file(fbuf))) {
146 +       } else if (make_backups > 0 && (backup_dir_dels || !is_backup_file(fbuf))) {
147                 what = "make_backup";
148 -               ok = make_backup(fbuf);
149 +               ok = safe_delete(fbuf);
150         } else {
151                 what = "unlink";
152                 ok = robust_unlink(fbuf) == 0;
153 diff --git a/options.c b/options.c
154 --- a/options.c
155 +++ b/options.c
156 @@ -150,10 +150,14 @@ int no_detach
157  int write_batch = 0;
158  int read_batch = 0;
159  int backup_dir_len = 0;
160 +int backup_dir_dels_len = 0;
161  int backup_suffix_len;
162 +int backup_suffix_dels_len;
163  unsigned int backup_dir_remainder;
164 +unsigned int backup_dir_dels_remainder;
165  
166  char *backup_suffix = NULL;
167 +char *backup_suffix_dels = NULL;
168  char *tmpdir = NULL;
169  char *partial_dir = NULL;
170  char *basis_dir[MAX_BASIS_DIRS+1];
171 @@ -165,7 +169,9 @@ char *stdout_format = NULL;
172  char *password_file = NULL;
173  char *rsync_path = RSYNC_PATH;
174  char *backup_dir = NULL;
175 +char *backup_dir_dels = NULL;
176  char backup_dir_buf[MAXPATHLEN];
177 +char backup_dir_dels_buf[MAXPATHLEN];
178  char *sockopts = NULL;
179  int rsync_port = 0;
180  int compare_dest = 0;
181 @@ -325,6 +331,8 @@ void usage(enum logcode F)
182    rprintf(F," -b, --backup                make backups (see --suffix & --backup-dir)\n");
183    rprintf(F,"     --backup-dir=DIR        make backups into hierarchy based in DIR\n");
184    rprintf(F,"     --suffix=SUFFIX         set backup suffix (default %s w/o --backup-dir)\n",BACKUP_SUFFIX);
185 +  rprintf(F,"     --backup-dir-dels=DIR   backup removed files into hierarchy based in DIR\n");
186 +  rprintf(F,"     --suffix-dels=SUFFIX    set removed-files suffix (def. --suffix w/o b-d-d)\n");
187    rprintf(F," -u, --update                skip files that are newer on the receiver\n");
188    rprintf(F,"     --inplace               update destination files in-place (SEE MAN PAGE)\n");
189    rprintf(F,"     --append                append data onto shorter files\n");
190 @@ -609,7 +617,9 @@ static struct poptOption long_options[] = {
191    {"backup",          'b', POPT_ARG_VAL,    &make_backups, 1, 0, 0 },
192    {"no-backup",        0,  POPT_ARG_VAL,    &make_backups, 0, 0, 0 },
193    {"backup-dir",       0,  POPT_ARG_STRING, &backup_dir, 0, 0, 0 },
194 +  {"backup-dir-dels",  0,  POPT_ARG_STRING, &backup_dir_dels, 0, 0, 0 },
195    {"suffix",           0,  POPT_ARG_STRING, &backup_suffix, 0, 0, 0 },
196 +  {"suffix-dels",      0,  POPT_ARG_STRING, &backup_suffix_dels, 0, 0, 0 },
197    {"list-only",        0,  POPT_ARG_VAL,    &list_only, 2, 0, 0 },
198    {"read-batch",       0,  POPT_ARG_STRING, &batch_name, OPT_READ_BATCH, 0, 0 },
199    {"write-batch",      0,  POPT_ARG_STRING, &batch_name, OPT_WRITE_BATCH, 0, 0 },
200 @@ -1453,6 +1463,8 @@ int parse_arguments(int *argc_p, const char ***argv_p)
201                         tmpdir = sanitize_path(NULL, tmpdir, NULL, 0, SP_DEFAULT);
202                 if (backup_dir)
203                         backup_dir = sanitize_path(NULL, backup_dir, NULL, 0, SP_DEFAULT);
204 +               if (backup_dir_dels)
205 +                       backup_dir_dels = sanitize_path(NULL, backup_dir_dels, NULL, 0, SP_DEFAULT);
206         }
207         if (daemon_filter_list.head && !am_sender) {
208                 struct filter_list_struct *elp = &daemon_filter_list;
209 @@ -1474,6 +1486,14 @@ int parse_arguments(int *argc_p, const char ***argv_p)
210                         if (check_filter(elp, FLOG, dir, 1) < 0)
211                                 goto options_rejected;
212                 }
213 +               /* Clean backup_dir_dels same as for backup_dir */
214 +               if (backup_dir_dels) {
215 +                       if (!*backup_dir_dels)
216 +                               goto options_rejected;
217 +                       clean_fname(backup_dir_dels, 1);
218 +                       if (check_filter(elp, FLOG, backup_dir_dels, 1) < 0)
219 +                               goto options_rejected;
220 +               }
221         }
222  
223         if (!backup_suffix)
224 @@ -1485,6 +1505,20 @@ int parse_arguments(int *argc_p, const char ***argv_p)
225                         backup_suffix);
226                 return 0;
227         }
228 +       /* --suffix-dels defaults to --suffix, or empty for a client given an
229 +        * explicit --backup-dir-dels (just as --suffix defaults to empty when
230 +        * a --backup-dir is given).  The second case does not apply to the
231 +        * server for consistency with server_options, which sends --suffix-dels
232 +        * to the server iff it differs from --suffix. */
233 +       if (!backup_suffix_dels)
234 +               backup_suffix_dels = backup_dir_dels && !am_server ? "" : backup_suffix;
235 +       backup_suffix_dels_len = strlen(backup_suffix_dels);
236 +       if (strchr(backup_suffix_dels, '/') != NULL) {
237 +               snprintf(err_buf, sizeof err_buf,
238 +                       "--suffix-dels cannot contain slashes: %s\n",
239 +                       backup_suffix_dels);
240 +               return 0;
241 +       }
242         if (backup_dir) {
243                 backup_dir_len = strlcpy(backup_dir_buf, backup_dir, sizeof backup_dir_buf);
244                 backup_dir_remainder = sizeof backup_dir_buf - backup_dir_len;
245 @@ -1508,6 +1542,34 @@ int parse_arguments(int *argc_p, const char ***argv_p)
246                         "P *%s", backup_suffix);
247                 parse_rule(&filter_list, backup_dir_buf, 0, 0);
248         }
249 +       if (backup_dir_dels) {
250 +               backup_dir_dels_len = strlcpy(backup_dir_dels_buf, backup_dir_dels, sizeof backup_dir_dels_buf);
251 +               backup_dir_dels_remainder = sizeof backup_dir_dels_buf - backup_dir_dels_len;
252 +               if (backup_dir_dels_remainder < 32) {
253 +                       snprintf(err_buf, sizeof err_buf,
254 +                               "the --backup-dir-dels path is WAY too long.\n");
255 +                       return 0;
256 +               }
257 +               if (backup_dir_dels_buf[backup_dir_dels_len - 1] != '/') {
258 +                       backup_dir_dels_buf[backup_dir_dels_len++] = '/';
259 +                       backup_dir_dels_buf[backup_dir_dels_len] = '\0';
260 +               }
261 +               if (verbose > 1 && !am_sender)
262 +                       rprintf(FINFO, "backup_dir_dels is %s\n", backup_dir_dels_buf);
263 +       } else if (backup_dir) {
264 +               backup_dir_dels = backup_dir;
265 +               backup_dir_dels_len = backup_dir_len;
266 +               backup_dir_dels_remainder = backup_dir_remainder;
267 +               strlcpy(backup_dir_dels_buf, backup_dir_buf, sizeof backup_dir_buf);
268 +       } else if (!backup_suffix_dels_len && (!am_server || !am_sender)) {
269 +               snprintf(err_buf, sizeof err_buf,
270 +                       "--suffix-dels cannot be a null string without --backup-dir-dels\n");
271 +               return 0;
272 +       } else if (make_backups && delete_mode && !delete_excluded && !am_server) {
273 +               snprintf(backup_dir_dels_buf, sizeof backup_dir_dels_buf,
274 +                       "P *%s", backup_suffix_dels);
275 +               parse_rule(&filter_list, backup_dir_dels_buf, 0, 0);
276 +       }
277  
278         if (make_backups && !backup_dir) {
279                 omit_dir_times = 0; /* Implied, so avoid -O to sender. */
280 @@ -1915,6 +1977,10 @@ void server_options(char **args, int *argc_p)
281                 args[ac++] = "--backup-dir";
282                 args[ac++] = backup_dir;
283         }
284 +       if (backup_dir_dels && backup_dir_dels != backup_dir) {
285 +               args[ac++] = "--backup-dir-dels";
286 +               args[ac++] = backup_dir_dels;
287 +       }
288  
289         /* Only send --suffix if it specifies a non-default value. */
290         if (strcmp(backup_suffix, backup_dir ? "" : BACKUP_SUFFIX) != 0) {
291 @@ -1923,7 +1989,14 @@ void server_options(char **args, int *argc_p)
292                         goto oom;
293                 args[ac++] = arg;
294         }
295 -
296 +       /* Only send --suffix-dels if it specifies a value different from the
297 +        * --suffix value, which would normally be used for deletions too. */
298 +       if (strcmp(backup_suffix_dels, backup_suffix) != 0) {
299 +               /* We use the following syntax to avoid weirdness with '~'. */
300 +               if (asprintf(&arg, "--suffix-dels=%s", backup_suffix_dels) < 0)
301 +                       goto oom;
302 +               args[ac++] = arg;
303 +       }
304         if (am_sender) {
305                 if (max_delete > 0) {
306                         if (asprintf(&arg, "--max-delete=%d", max_delete) < 0)