Fixed failing hunks.
[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 --- old/backup.c
17 +++ new/backup.c
18 @@ -22,10 +22,15 @@
19  
20  extern int verbose;
21  extern int backup_dir_len;
22 +extern int backup_dir_dels_len;
23  extern unsigned int backup_dir_remainder;
24 +extern unsigned int backup_dir_dels_remainder;
25  extern char backup_dir_buf[MAXPATHLEN];
26 +extern char backup_dir_dels_buf[MAXPATHLEN];
27  extern char *backup_suffix;
28 +extern char *backup_suffix_dels;
29  extern char *backup_dir;
30 +extern char *backup_dir_dels;
31  
32  extern int am_root;
33  extern int preserve_devices;
34 @@ -33,6 +38,8 @@ extern int preserve_specials;
35  extern int preserve_links;
36  extern int safe_symlinks;
37  
38 +static int deleting;
39 +
40  /* make a complete pathname for backup file */
41  char *get_backup_name(char *fname)
42  {
43 @@ -50,11 +57,28 @@ char *get_backup_name(char *fname)
44         return NULL;
45  }
46  
47 +static char *get_delete_name(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(char *fname)
65  {
66         int rename_errno;
67 -       char *fnamebak = get_backup_name(fname);
68 +       char *fnamebak = deleting ? get_delete_name(fname)
69 +                                 : get_backup_name(fname);
70  
71         if (!fnamebak)
72                 return 0;
73 @@ -94,7 +118,8 @@ path
74  static int make_bak_dir(char *fullpath)
75  {
76         STRUCT_STAT st;
77 -       char *rel = fullpath + backup_dir_len;
78 +       int dir_len = deleting ? backup_dir_dels_len : backup_dir_len;
79 +       char *rel = fullpath + dir_len;
80         char *end = rel + strlen(rel);
81         char *p = end;
82  
83 @@ -182,7 +207,8 @@ static int keep_backup(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                 return 0;
91  
92         /* Check to see if this is a device file, or link */
93 @@ -277,3 +303,13 @@ int make_backup(char *fname)
94                 return keep_backup(fname);
95         return make_simple_backup(fname);
96  }
97 +
98 +/* backup switch routine called only when backing-up removed file */
99 +int safe_delete(char *fname)
100 +{
101 +       int ret;
102 +       deleting = 1;
103 +       ret = make_backup(fname);
104 +       deleting = 0;
105 +       return ret;
106 +}
107 --- old/generator.c
108 +++ new/generator.c
109 @@ -90,6 +90,9 @@ extern dev_t filesystem_dev;
110  extern char *backup_dir;
111  extern char *backup_suffix;
112  extern int backup_suffix_len;
113 +extern char *backup_dir_dels;
114 +extern char *backup_suffix_dels;
115 +extern int backup_suffix_dels_len;
116  extern struct file_list *the_file_list;
117  extern struct filter_list_struct server_filter_list;
118  
119 @@ -102,10 +105,14 @@ static int can_link_devices = 1;
120  #define DEL_TERSE              (1<<3)
121  
122  
123 +/* Function now compares both backup_suffix and backup_suffix_dels. */
124  static int is_backup_file(char *fn)
125  {
126         int k = strlen(fn) - backup_suffix_len;
127 -       return k > 0 && strcmp(fn+k, backup_suffix) == 0;
128 +       if (k > 0 && strcmp(fn+k, backup_suffix) == 0)
129 +               return 1;
130 +       k += backup_suffix_len - backup_suffix_dels_len;
131 +       return k > 0 && strcmp(fn+k, backup_suffix_dels) == 0;
132  }
133  
134  
135 @@ -126,8 +133,8 @@ static int delete_item(char *fname, int 
136         if (!S_ISDIR(mode)) {
137                 if (max_delete && ++deletion_count > max_delete)
138                         return 0;
139 -               if (make_backups && (backup_dir || !is_backup_file(fname)))
140 -                       ok = make_backup(fname);
141 +               if (make_backups && (backup_dir_dels || !is_backup_file(fname)))
142 +                       ok = safe_delete(fname);
143                 else
144                         ok = robust_unlink(fname) == 0;
145                 if (ok) {
146 @@ -149,9 +156,9 @@ static int delete_item(char *fname, int 
147             || (dry_run && zap_dir)) {
148                 ok = 0;
149                 errno = ENOTEMPTY;
150 -       } else if (make_backups && !backup_dir && !is_backup_file(fname)
151 +       } else if (make_backups && !backup_dir_dels && !is_backup_file(fname)
152             && !(flags & DEL_FORCE_RECURSE))
153 -               ok = make_backup(fname);
154 +               ok = safe_delete(fname);
155         else
156                 ok = do_rmdir(fname) == 0;
157         if (ok) {
158 --- old/options.c
159 +++ new/options.c
160 @@ -138,10 +138,14 @@ int no_detach
161  int write_batch = 0;
162  int read_batch = 0;
163  int backup_dir_len = 0;
164 +int backup_dir_dels_len = 0;   
165  int backup_suffix_len;
166 +int backup_suffix_dels_len;
167  unsigned int backup_dir_remainder;
168 +unsigned int backup_dir_dels_remainder;
169  
170  char *backup_suffix = NULL;
171 +char *backup_suffix_dels = NULL;
172  char *tmpdir = NULL;
173  char *partial_dir = NULL;
174  char *basis_dir[MAX_BASIS_DIRS+1];
175 @@ -151,7 +155,9 @@ char *log_format = NULL;
176  char *password_file = NULL;
177  char *rsync_path = RSYNC_PATH;
178  char *backup_dir = NULL;
179 +char *backup_dir_dels = NULL;
180  char backup_dir_buf[MAXPATHLEN];
181 +char backup_dir_dels_buf[MAXPATHLEN];
182  char *sockopts = NULL;
183  int rsync_port = 0;
184  int compare_dest = 0;
185 @@ -282,6 +288,8 @@ void usage(enum logcode F)
186    rprintf(F," -b, --backup                make backups (see --suffix & --backup-dir)\n");
187    rprintf(F,"     --backup-dir=DIR        make backups into hierarchy based in DIR\n");
188    rprintf(F,"     --suffix=SUFFIX         set backup suffix (default %s w/o --backup-dir)\n",BACKUP_SUFFIX);
189 +  rprintf(F,"     --backup-dir-dels       make backups of removed files into current dir\n");
190 +  rprintf(F,"     --suffix-dels=SUFFIX    set removed-files suffix (defaults to --suffix)\n");
191    rprintf(F," -u, --update                skip files that are newer on the receiver\n");
192    rprintf(F,"     --inplace               update destination files in-place (SEE MAN PAGE)\n");
193    rprintf(F,"     --append                append data onto shorter files\n");
194 @@ -497,7 +505,9 @@ static struct poptOption long_options[] 
195    {"bwlimit",          0,  POPT_ARG_INT,    &bwlimit, 0, 0, 0 },
196    {"backup",          'b', POPT_ARG_NONE,   &make_backups, 0, 0, 0 },
197    {"backup-dir",       0,  POPT_ARG_STRING, &backup_dir, 0, 0, 0 },
198 +  {"backup-dir-dels",  0,  POPT_ARG_STRING, &backup_dir_dels, 0, 0, 0 },
199    {"suffix",           0,  POPT_ARG_STRING, &backup_suffix, 0, 0, 0 },
200 +  {"suffix-dels",      0,  POPT_ARG_STRING, &backup_suffix_dels, 0, 0, 0 },
201    {"list-only",        0,  POPT_ARG_VAL,    &list_only, 2, 0, 0 },
202    {"read-batch",       0,  POPT_ARG_STRING, &batch_name, OPT_READ_BATCH, 0, 0 },
203    {"write-batch",      0,  POPT_ARG_STRING, &batch_name, OPT_WRITE_BATCH, 0, 0 },
204 @@ -1212,6 +1222,8 @@ int parse_arguments(int *argc, const cha
205                         partial_dir = sanitize_path(NULL, partial_dir, NULL, 0);
206                 if (backup_dir)
207                         backup_dir = sanitize_path(NULL, backup_dir, NULL, 0);
208 +               if (backup_dir_dels)                                                    
209 +                       backup_dir_dels = sanitize_path(NULL, backup_dir_dels, NULL, 0);
210         }
211         if (server_filter_list.head && !am_sender) {
212                 struct filter_list_struct *elp = &server_filter_list;
213 @@ -1246,6 +1258,14 @@ int parse_arguments(int *argc, const cha
214                                 return 0;
215                         }
216                 }
217 +               /* Clean backup_dir_dels same as for backup_dir */
218 +               if (backup_dir_dels) {
219 +                       if (!*backup_dir_dels)
220 +                               goto options_rejected;
221 +                       clean_fname(backup_dir_dels, 1);
222 +                       if (check_filter(elp, backup_dir_dels, 1) < 0)
223 +                               goto options_rejected;
224 +               }
225         }
226  
227         if (!backup_suffix)
228 @@ -1257,6 +1277,16 @@ int parse_arguments(int *argc, const cha
229                         backup_suffix);
230                 return 0;
231         }
232 +       /* if backup_suffix_dels not supplied, default to backup_suffix */
233 +       if (!backup_suffix_dels)
234 +               backup_suffix_dels = backup_dir_dels ? "" : 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 @@ -1280,6 +1310,31 @@ int parse_arguments(int *argc, const cha
246                         "P *%s", backup_suffix);
247                 parse_rule(&filter_list, backup_dir_buf, 0, 0);
248         }
249 +       /* If backup_dir_dels not supplied default to backup_dir if it has been supplied */
250 +       if (backup_dir && !backup_dir_dels) {
251 +               backup_dir_dels = backup_dir;
252 +               backup_dir_dels_len = backup_dir_len;
253 +               backup_dir_dels_remainder = backup_dir_remainder;
254 +               strlcpy(backup_dir_dels_buf, backup_dir_buf, sizeof backup_dir_buf);
255 +       } else if (backup_dir_dels) {
256 +               backup_dir_dels_len = strlcpy(backup_dir_dels_buf, backup_dir_dels, sizeof backup_dir_dels_buf);
257 +               backup_dir_dels_remainder = sizeof backup_dir_dels_buf - backup_dir_dels_len;
258 +               if (backup_dir_dels_remainder < 32) {
259 +                       snprintf(err_buf, sizeof err_buf,
260 +                               "the --backup-dir-dels path is WAY too long.\n");
261 +                       return 0;
262 +               }
263 +               if (backup_dir_dels_buf[backup_dir_dels_len - 1] != '/') {
264 +                       backup_dir_dels_buf[backup_dir_dels_len++] = '/';
265 +                       backup_dir_dels_buf[backup_dir_dels_len] = '\0';
266 +               }
267 +               if (verbose > 1 && !am_sender)
268 +                       rprintf(FINFO, "backup_dir_dels is %s\n", backup_dir_dels_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 +       }
274         if (make_backups && !backup_dir)
275                 omit_dir_times = 1;
276  
277 @@ -1624,6 +1679,10 @@ void server_options(char **args,int *arg
278                 args[ac++] = "--backup-dir";
279                 args[ac++] = backup_dir;
280         }
281 +       if (backup_dir_dels && backup_dir_dels != backup_dir) {
282 +               args[ac++] = "--backup-dir-dels";
283 +               args[ac++] = backup_dir_dels;
284 +       }
285  
286         /* Only send --suffix if it specifies a non-default value. */
287         if (strcmp(backup_suffix, backup_dir ? "" : BACKUP_SUFFIX) != 0) {
288 @@ -1632,7 +1691,13 @@ void server_options(char **args,int *arg
289                         goto oom;
290                 args[ac++] = arg;
291         }
292 -
293 +       /* Only send --suffix-dels if it specifies a non-default value. */
294 +       if (strcmp(backup_suffix_dels, backup_dir_dels ? "" : BACKUP_SUFFIX) != 0) {
295 +               /* We use the following syntax to avoid weirdness with '~'. */
296 +               if (asprintf(&arg, "--suffix-dels=%s", backup_suffix_dels) < 0)
297 +                       goto oom;
298 +               args[ac++] = arg;
299 +       }
300         if (am_sender) {
301                 if (delete_excluded)
302                         args[ac++] = "--delete-excluded";