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