Updated the opening comments to mention how to apply the patch
[rsync/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 --- old/backup.c
23 +++ new/backup.c
24 @@ -23,10 +23,15 @@
25  
26  extern int verbose;
27  extern int backup_dir_len;
28 +extern int backup_dir_dels_len;
29  extern unsigned int backup_dir_remainder;
30 +extern unsigned int backup_dir_dels_remainder;
31  extern char backup_dir_buf[MAXPATHLEN];
32 +extern char backup_dir_dels_buf[MAXPATHLEN];
33  extern char *backup_suffix;
34 +extern char *backup_suffix_dels;
35  extern char *backup_dir;
36 +extern char *backup_dir_dels;
37  
38  extern int am_root;
39  extern int preserve_devices;
40 @@ -34,6 +39,8 @@ extern int preserve_specials;
41  extern int preserve_links;
42  extern int safe_symlinks;
43  
44 +static int deleting;
45 +
46  /* make a complete pathname for backup file */
47  char *get_backup_name(char *fname)
48  {
49 @@ -51,11 +58,28 @@ char *get_backup_name(char *fname)
50         return NULL;
51  }
52  
53 +static char *get_delete_name(char *fname)
54 +{
55 +       if (backup_dir_dels) {
56 +               if (stringjoin(backup_dir_dels_buf + backup_dir_dels_len, backup_dir_dels_remainder,
57 +                              fname, backup_suffix_dels, NULL) < backup_dir_dels_remainder)
58 +                       return backup_dir_dels_buf;
59 +       } else {
60 +               if (stringjoin(backup_dir_dels_buf, MAXPATHLEN,
61 +                              fname, backup_suffix_dels, NULL) < MAXPATHLEN)
62 +                       return backup_dir_dels_buf;
63 +       }
64 +
65 +       rprintf(FERROR, "delete filename too long\n");
66 +       return NULL;
67 +}
68 +
69  /* simple backup creates a backup with a suffix in the same directory */
70  static int make_simple_backup(char *fname)
71  {
72         int rename_errno;
73 -       char *fnamebak = get_backup_name(fname);
74 +       char *fnamebak = deleting ? get_delete_name(fname)
75 +                                 : get_backup_name(fname);
76  
77         if (!fnamebak)
78                 return 0;
79 @@ -95,7 +119,8 @@ path
80  static int make_bak_dir(char *fullpath)
81  {
82         STRUCT_STAT st;
83 -       char *rel = fullpath + backup_dir_len;
84 +       int dir_len = deleting ? backup_dir_dels_len : backup_dir_len;
85 +       char *rel = fullpath + dir_len;
86         char *end = rel + strlen(rel);
87         char *p = end;
88  
89 @@ -183,7 +208,8 @@ static int keep_backup(char *fname)
90         if (!(file = make_file(fname, NULL, NULL, 0, NO_FILTERS)))
91                 return 1; /* the file could have disappeared */
92  
93 -       if (!(buf = get_backup_name(fname)))
94 +       buf = deleting ? get_delete_name(fname) : get_backup_name(fname);
95 +       if (!buf)
96                 return 0;
97  
98         /* Check to see if this is a device file, or link */
99 @@ -278,3 +304,13 @@ int make_backup(char *fname)
100                 return keep_backup(fname);
101         return make_simple_backup(fname);
102  }
103 +
104 +/* backup switch routine called only when backing-up removed file */
105 +int safe_delete(char *fname)
106 +{
107 +       int ret;
108 +       deleting = 1;
109 +       ret = make_backup(fname);
110 +       deleting = 0;
111 +       return ret;
112 +}
113 --- old/generator.c
114 +++ new/generator.c
115 @@ -90,6 +90,9 @@ extern dev_t filesystem_dev;
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 *the_file_list;
123  extern struct filter_list_struct server_filter_list;
124  
125 @@ -100,10 +103,14 @@ static int deletion_count = 0; /* used t
126  #define DEL_TERSE              (1<<3)
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  
141 @@ -124,8 +131,8 @@ static int delete_item(char *fname, int 
142         if (!S_ISDIR(mode)) {
143                 if (max_delete && ++deletion_count > max_delete)
144                         return 0;
145 -               if (make_backups && (backup_dir || !is_backup_file(fname)))
146 -                       ok = make_backup(fname);
147 +               if (make_backups && (backup_dir_dels || !is_backup_file(fname)))
148 +                       ok = safe_delete(fname);
149                 else
150                         ok = robust_unlink(fname) == 0;
151                 if (ok) {
152 @@ -147,9 +154,9 @@ static int delete_item(char *fname, int 
153             || (dry_run && zap_dir)) {
154                 ok = 0;
155                 errno = ENOTEMPTY;
156 -       } else if (make_backups && !backup_dir && !is_backup_file(fname)
157 +       } else if (make_backups && !backup_dir_dels && !is_backup_file(fname)
158             && !(flags & DEL_FORCE_RECURSE))
159 -               ok = make_backup(fname);
160 +               ok = safe_delete(fname);
161         else
162                 ok = do_rmdir(fname) == 0;
163         if (ok) {
164 --- old/options.c
165 +++ new/options.c
166 @@ -138,10 +138,14 @@ int no_detach
167  int write_batch = 0;
168  int read_batch = 0;
169  int backup_dir_len = 0;
170 +int backup_dir_dels_len = 0;   
171  int backup_suffix_len;
172 +int backup_suffix_dels_len;
173  unsigned int backup_dir_remainder;
174 +unsigned int backup_dir_dels_remainder;
175  
176  char *backup_suffix = NULL;
177 +char *backup_suffix_dels = NULL;
178  char *tmpdir = NULL;
179  char *partial_dir = NULL;
180  char *basis_dir[MAX_BASIS_DIRS+1];
181 @@ -153,7 +157,9 @@ char *stdout_format = NULL;
182  char *password_file = NULL;
183  char *rsync_path = RSYNC_PATH;
184  char *backup_dir = NULL;
185 +char *backup_dir_dels = NULL;
186  char backup_dir_buf[MAXPATHLEN];
187 +char backup_dir_dels_buf[MAXPATHLEN];
188  char *sockopts = NULL;
189  int rsync_port = 0;
190  int compare_dest = 0;
191 @@ -292,6 +298,8 @@ void usage(enum logcode F)
192    rprintf(F," -b, --backup                make backups (see --suffix & --backup-dir)\n");
193    rprintf(F,"     --backup-dir=DIR        make backups into hierarchy based in DIR\n");
194    rprintf(F,"     --suffix=SUFFIX         set backup suffix (default %s w/o --backup-dir)\n",BACKUP_SUFFIX);
195 +  rprintf(F,"     --backup-dir-dels       make backups of removed files into current dir\n");
196 +  rprintf(F,"     --suffix-dels=SUFFIX    set removed-files suffix (defaults to --suffix)\n");
197    rprintf(F," -u, --update                skip files that are newer on the receiver\n");
198    rprintf(F,"     --inplace               update destination files in-place (SEE MAN PAGE)\n");
199    rprintf(F,"     --append                append data onto shorter files\n");
200 @@ -518,7 +526,9 @@ static struct poptOption long_options[] 
201    {"bwlimit",          0,  POPT_ARG_INT,    &bwlimit, 0, 0, 0 },
202    {"backup",          'b', POPT_ARG_NONE,   &make_backups, 0, 0, 0 },
203    {"backup-dir",       0,  POPT_ARG_STRING, &backup_dir, 0, 0, 0 },
204 +  {"backup-dir-dels",  0,  POPT_ARG_STRING, &backup_dir_dels, 0, 0, 0 },
205    {"suffix",           0,  POPT_ARG_STRING, &backup_suffix, 0, 0, 0 },
206 +  {"suffix-dels",      0,  POPT_ARG_STRING, &backup_suffix_dels, 0, 0, 0 },
207    {"list-only",        0,  POPT_ARG_VAL,    &list_only, 2, 0, 0 },
208    {"read-batch",       0,  POPT_ARG_STRING, &batch_name, OPT_READ_BATCH, 0, 0 },
209    {"write-batch",      0,  POPT_ARG_STRING, &batch_name, OPT_WRITE_BATCH, 0, 0 },
210 @@ -1232,6 +1242,8 @@ int parse_arguments(int *argc, const cha
211                         tmpdir = sanitize_path(NULL, tmpdir, NULL, 0, NULL);
212                 if (backup_dir)
213                         backup_dir = sanitize_path(NULL, backup_dir, NULL, 0, NULL);
214 +               if (backup_dir_dels)
215 +                       backup_dir_dels = sanitize_path(NULL, backup_dir_dels, NULL, 0, NULL);
216         }
217         if (server_filter_list.head && !am_sender) {
218                 struct filter_list_struct *elp = &server_filter_list;
219 @@ -1253,6 +1265,14 @@ int parse_arguments(int *argc, const cha
220                                 return 0;
221                         }
222                 }
223 +               /* Clean backup_dir_dels same as for backup_dir */
224 +               if (backup_dir_dels) {
225 +                       if (!*backup_dir_dels)
226 +                               goto options_rejected;
227 +                       clean_fname(backup_dir_dels, 1);
228 +                       if (check_filter(elp, backup_dir_dels, 1) < 0)
229 +                               goto options_rejected;
230 +               }
231         }
232  
233         if (!backup_suffix)
234 @@ -1264,6 +1284,16 @@ int parse_arguments(int *argc, const cha
235                         backup_suffix);
236                 return 0;
237         }
238 +       /* if backup_suffix_dels not supplied, default to backup_suffix */
239 +       if (!backup_suffix_dels)
240 +               backup_suffix_dels = backup_dir_dels ? "" : backup_suffix;
241 +       backup_suffix_dels_len = strlen(backup_suffix_dels);
242 +       if (strchr(backup_suffix_dels, '/') != NULL) {
243 +               snprintf(err_buf, sizeof err_buf,
244 +                       "--suffix-dels cannot contain slashes: %s\n",
245 +                       backup_suffix_dels);    
246 +               return 0;
247 +       }
248         if (backup_dir) {
249                 backup_dir_len = strlcpy(backup_dir_buf, backup_dir, sizeof backup_dir_buf);
250                 backup_dir_remainder = sizeof backup_dir_buf - backup_dir_len;
251 @@ -1287,6 +1317,31 @@ int parse_arguments(int *argc, const cha
252                         "P *%s", backup_suffix);
253                 parse_rule(&filter_list, backup_dir_buf, 0, 0);
254         }
255 +       /* If backup_dir_dels not supplied default to backup_dir if it has been supplied */
256 +       if (backup_dir && !backup_dir_dels) {
257 +               backup_dir_dels = backup_dir;
258 +               backup_dir_dels_len = backup_dir_len;
259 +               backup_dir_dels_remainder = backup_dir_remainder;
260 +               strlcpy(backup_dir_dels_buf, backup_dir_buf, sizeof backup_dir_buf);
261 +       } else if (backup_dir_dels) {
262 +               backup_dir_dels_len = strlcpy(backup_dir_dels_buf, backup_dir_dels, sizeof backup_dir_dels_buf);
263 +               backup_dir_dels_remainder = sizeof backup_dir_dels_buf - backup_dir_dels_len;
264 +               if (backup_dir_dels_remainder < 32) {
265 +                       snprintf(err_buf, sizeof err_buf,
266 +                               "the --backup-dir-dels path is WAY too long.\n");
267 +                       return 0;
268 +               }
269 +               if (backup_dir_dels_buf[backup_dir_dels_len - 1] != '/') {
270 +                       backup_dir_dels_buf[backup_dir_dels_len++] = '/';
271 +                       backup_dir_dels_buf[backup_dir_dels_len] = '\0';
272 +               }
273 +               if (verbose > 1 && !am_sender)
274 +                       rprintf(FINFO, "backup_dir_dels is %s\n", backup_dir_dels_buf);
275 +       } else if (!backup_suffix_dels_len && (!am_server || !am_sender)) {
276 +               snprintf(err_buf, sizeof err_buf,
277 +                       "--suffix-dels cannot be a null string without --backup-dir-dels\n");
278 +               return 0;
279 +       }
280         if (make_backups && !backup_dir)
281                 omit_dir_times = 1;
282  
283 @@ -1646,6 +1701,10 @@ void server_options(char **args,int *arg
284                 args[ac++] = "--backup-dir";
285                 args[ac++] = backup_dir;
286         }
287 +       if (backup_dir_dels && backup_dir_dels != backup_dir) {
288 +               args[ac++] = "--backup-dir-dels";
289 +               args[ac++] = backup_dir_dels;
290 +       }
291  
292         /* Only send --suffix if it specifies a non-default value. */
293         if (strcmp(backup_suffix, backup_dir ? "" : BACKUP_SUFFIX) != 0) {
294 @@ -1654,7 +1713,13 @@ void server_options(char **args,int *arg
295                         goto oom;
296                 args[ac++] = arg;
297         }
298 -
299 +       /* Only send --suffix-dels if it specifies a non-default value. */
300 +       if (strcmp(backup_suffix_dels, backup_dir_dels ? "" : BACKUP_SUFFIX) != 0) {
301 +               /* We use the following syntax to avoid weirdness with '~'. */
302 +               if (asprintf(&arg, "--suffix-dels=%s", backup_suffix_dels) < 0)
303 +                       goto oom;
304 +               args[ac++] = arg;
305 +       }
306         if (am_sender) {
307                 if (delete_excluded)
308                         args[ac++] = "--delete-excluded";