Updated to apply to latest source.
[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 @@ -28,10 +28,17 @@ extern int preserve_specials;
25  extern int preserve_links;
26  extern int safe_symlinks;
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 +static int deleting;
39  
40  /* make a complete pathname for backup file */
41  char *get_backup_name(const char *fname)
42 @@ -50,11 +57,28 @@ char *get_backup_name(const char *fname)
43         return NULL;
44  }
45  
46 +static char *get_delete_name(const char *fname)
47 +{
48 +       if (backup_dir_dels) {
49 +               if (stringjoin(backup_dir_dels_buf + backup_dir_dels_len, backup_dir_dels_remainder,
50 +                              fname, backup_suffix_dels, NULL) < backup_dir_dels_remainder)
51 +                       return backup_dir_dels_buf;
52 +       } else {
53 +               if (stringjoin(backup_dir_dels_buf, MAXPATHLEN,
54 +                              fname, backup_suffix_dels, NULL) < MAXPATHLEN)
55 +                       return backup_dir_dels_buf;
56 +       }
57 +
58 +       rprintf(FERROR, "delete filename too long\n");
59 +       return NULL;
60 +}
61 +
62  /* simple backup creates a backup with a suffix in the same directory */
63  static int make_simple_backup(const char *fname)
64  {
65         int rename_errno;
66 -       const char *fnamebak = get_backup_name(fname);
67 +       const char *fnamebak = deleting ? get_delete_name(fname)
68 +                                       : get_backup_name(fname);
69  
70         if (!fnamebak)
71                 return 0;
72 @@ -94,7 +118,8 @@ path
73  static int make_bak_dir(char *fullpath)
74  {
75         STRUCT_STAT st;
76 -       char *rel = fullpath + backup_dir_len;
77 +       int dir_len = deleting ? backup_dir_dels_len : backup_dir_len;
78 +       char *rel = fullpath + dir_len;
79         char *end = rel + strlen(rel);
80         char *p = end;
81  
82 @@ -184,7 +209,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 @@ -284,3 +310,13 @@ int make_backup(const char *fname)
93                 return keep_backup(fname);
94         return make_simple_backup(fname);
95  }
96 +
97 +/* backup switch routine called only when backing-up removed file */
98 +int safe_delete(char *fname)
99 +{
100 +       int ret;
101 +       deleting = 1;
102 +       ret = make_backup(fname);
103 +       deleting = 0;
104 +       return ret;
105 +}
106 --- old/generator.c
107 +++ new/generator.c
108 @@ -93,6 +93,9 @@ extern dev_t filesystem_dev;
109  extern char *backup_dir;
110  extern char *backup_suffix;
111  extern int backup_suffix_len;
112 +extern char *backup_dir_dels;
113 +extern char *backup_suffix_dels;
114 +extern int backup_suffix_dels_len;
115  extern struct file_list *cur_flist, *first_flist, *dir_flist;
116  extern struct filter_list_struct server_filter_list;
117  
118 @@ -123,10 +126,14 @@ enum delret {
119  static enum delret delete_dir_contents(char *fname, int flags);
120  
121  
122 +/* Function now compares both backup_suffix and backup_suffix_dels. */
123  static int is_backup_file(char *fn)
124  {
125         int k = strlen(fn) - backup_suffix_len;
126 -       return k > 0 && strcmp(fn+k, backup_suffix) == 0;
127 +       if (k > 0 && strcmp(fn+k, backup_suffix) == 0)
128 +               return 1;
129 +       k += backup_suffix_len - backup_suffix_dels_len;
130 +       return k > 0 && strcmp(fn+k, backup_suffix_dels) == 0;
131  }
132  
133  /* Delete a file or directory.  If DEL_RECURSE is set in the flags, this will
134 @@ -162,9 +169,9 @@ static enum delret delete_item(char *fbu
135         if (S_ISDIR(mode)) {
136                 what = "rmdir";
137                 ok = do_rmdir(fbuf) == 0;
138 -       } else if (make_backups > 0 && (backup_dir || !is_backup_file(fbuf))) {
139 +       } else if (make_backups > 0 && (backup_dir_dels || !is_backup_file(fbuf))) {
140                 what = "make_backup";
141 -               ok = make_backup(fbuf);
142 +               ok = safe_delete(fbuf);
143         } else {
144                 what = "unlink";
145                 ok = robust_unlink(fbuf) == 0;
146 --- old/options.c
147 +++ new/options.c
148 @@ -137,10 +137,14 @@ int no_detach
149  int write_batch = 0;
150  int read_batch = 0;
151  int backup_dir_len = 0;
152 +int backup_dir_dels_len = 0;   
153  int backup_suffix_len;
154 +int backup_suffix_dels_len;
155  unsigned int backup_dir_remainder;
156 +unsigned int backup_dir_dels_remainder;
157  
158  char *backup_suffix = NULL;
159 +char *backup_suffix_dels = NULL;
160  char *tmpdir = NULL;
161  char *partial_dir = NULL;
162  char *basis_dir[MAX_BASIS_DIRS+1];
163 @@ -152,7 +156,9 @@ char *stdout_format = NULL;
164  char *password_file = NULL;
165  char *rsync_path = RSYNC_PATH;
166  char *backup_dir = NULL;
167 +char *backup_dir_dels = NULL;
168  char backup_dir_buf[MAXPATHLEN];
169 +char backup_dir_dels_buf[MAXPATHLEN];
170  char *sockopts = NULL;
171  int rsync_port = 0;
172  int compare_dest = 0;
173 @@ -288,6 +294,8 @@ void usage(enum logcode F)
174    rprintf(F," -b, --backup                make backups (see --suffix & --backup-dir)\n");
175    rprintf(F,"     --backup-dir=DIR        make backups into hierarchy based in DIR\n");
176    rprintf(F,"     --suffix=SUFFIX         set backup suffix (default %s w/o --backup-dir)\n",BACKUP_SUFFIX);
177 +  rprintf(F,"     --backup-dir-dels       make backups of removed files into current dir\n");
178 +  rprintf(F,"     --suffix-dels=SUFFIX    set removed-files suffix (defaults to --suffix)\n");
179    rprintf(F," -u, --update                skip files that are newer on the receiver\n");
180    rprintf(F,"     --inplace               update destination files in-place (SEE MAN PAGE)\n");
181    rprintf(F,"     --append                append data onto shorter files\n");
182 @@ -516,7 +524,9 @@ static struct poptOption long_options[] 
183    {"bwlimit",          0,  POPT_ARG_INT,    &bwlimit, 0, 0, 0 },
184    {"backup",          'b', POPT_ARG_NONE,   &make_backups, 0, 0, 0 },
185    {"backup-dir",       0,  POPT_ARG_STRING, &backup_dir, 0, 0, 0 },
186 +  {"backup-dir-dels",  0,  POPT_ARG_STRING, &backup_dir_dels, 0, 0, 0 },
187    {"suffix",           0,  POPT_ARG_STRING, &backup_suffix, 0, 0, 0 },
188 +  {"suffix-dels",      0,  POPT_ARG_STRING, &backup_suffix_dels, 0, 0, 0 },
189    {"list-only",        0,  POPT_ARG_VAL,    &list_only, 2, 0, 0 },
190    {"read-batch",       0,  POPT_ARG_STRING, &batch_name, OPT_READ_BATCH, 0, 0 },
191    {"write-batch",      0,  POPT_ARG_STRING, &batch_name, OPT_WRITE_BATCH, 0, 0 },
192 @@ -1242,6 +1252,8 @@ int parse_arguments(int *argc, const cha
193                         tmpdir = sanitize_path(NULL, tmpdir, NULL, 0, NULL);
194                 if (backup_dir)
195                         backup_dir = sanitize_path(NULL, backup_dir, NULL, 0, NULL);
196 +               if (backup_dir_dels)
197 +                       backup_dir_dels = sanitize_path(NULL, backup_dir_dels, NULL, 0, NULL);
198         }
199         if (server_filter_list.head && !am_sender) {
200                 struct filter_list_struct *elp = &server_filter_list;
201 @@ -1259,6 +1271,14 @@ int parse_arguments(int *argc, const cha
202                         if (check_filter(elp, backup_dir, 1) < 0)
203                                 goto options_rejected;
204                 }
205 +               /* Clean backup_dir_dels same as for backup_dir */
206 +               if (backup_dir_dels) {
207 +                       if (!*backup_dir_dels)
208 +                               goto options_rejected;
209 +                       clean_fname(backup_dir_dels, 1);
210 +                       if (check_filter(elp, backup_dir_dels, 1) < 0)
211 +                               goto options_rejected;
212 +               }
213         }
214  
215         if (!backup_suffix)
216 @@ -1270,6 +1290,16 @@ int parse_arguments(int *argc, const cha
217                         backup_suffix);
218                 return 0;
219         }
220 +       /* if backup_suffix_dels not supplied, default to backup_suffix */
221 +       if (!backup_suffix_dels)
222 +               backup_suffix_dels = backup_dir_dels ? "" : backup_suffix;
223 +       backup_suffix_dels_len = strlen(backup_suffix_dels);
224 +       if (strchr(backup_suffix_dels, '/') != NULL) {
225 +               snprintf(err_buf, sizeof err_buf,
226 +                       "--suffix-dels cannot contain slashes: %s\n",
227 +                       backup_suffix_dels);    
228 +               return 0;
229 +       }
230         if (backup_dir) {
231                 backup_dir_len = strlcpy(backup_dir_buf, backup_dir, sizeof backup_dir_buf);
232                 backup_dir_remainder = sizeof backup_dir_buf - backup_dir_len;
233 @@ -1293,6 +1323,31 @@ int parse_arguments(int *argc, const cha
234                         "P *%s", backup_suffix);
235                 parse_rule(&filter_list, backup_dir_buf, 0, 0);
236         }
237 +       /* If backup_dir_dels not supplied default to backup_dir if it has been supplied */
238 +       if (backup_dir && !backup_dir_dels) {
239 +               backup_dir_dels = backup_dir;
240 +               backup_dir_dels_len = backup_dir_len;
241 +               backup_dir_dels_remainder = backup_dir_remainder;
242 +               strlcpy(backup_dir_dels_buf, backup_dir_buf, sizeof backup_dir_buf);
243 +       } else if (backup_dir_dels) {
244 +               backup_dir_dels_len = strlcpy(backup_dir_dels_buf, backup_dir_dels, sizeof backup_dir_dels_buf);
245 +               backup_dir_dels_remainder = sizeof backup_dir_dels_buf - backup_dir_dels_len;
246 +               if (backup_dir_dels_remainder < 32) {
247 +                       snprintf(err_buf, sizeof err_buf,
248 +                               "the --backup-dir-dels path is WAY too long.\n");
249 +                       return 0;
250 +               }
251 +               if (backup_dir_dels_buf[backup_dir_dels_len - 1] != '/') {
252 +                       backup_dir_dels_buf[backup_dir_dels_len++] = '/';
253 +                       backup_dir_dels_buf[backup_dir_dels_len] = '\0';
254 +               }
255 +               if (verbose > 1 && !am_sender)
256 +                       rprintf(FINFO, "backup_dir_dels is %s\n", backup_dir_dels_buf);
257 +       } else if (!backup_suffix_dels_len && (!am_server || !am_sender)) {
258 +               snprintf(err_buf, sizeof err_buf,
259 +                       "--suffix-dels cannot be a null string without --backup-dir-dels\n");
260 +               return 0;
261 +       }
262         if (make_backups && !backup_dir)
263                 omit_dir_times = 1;
264  
265 @@ -1660,6 +1715,10 @@ void server_options(char **args,int *arg
266                 args[ac++] = "--backup-dir";
267                 args[ac++] = backup_dir;
268         }
269 +       if (backup_dir_dels && backup_dir_dels != backup_dir) {
270 +               args[ac++] = "--backup-dir-dels";
271 +               args[ac++] = backup_dir_dels;
272 +       }
273  
274         /* Only send --suffix if it specifies a non-default value. */
275         if (strcmp(backup_suffix, backup_dir ? "" : BACKUP_SUFFIX) != 0) {
276 @@ -1668,7 +1727,13 @@ void server_options(char **args,int *arg
277                         goto oom;
278                 args[ac++] = arg;
279         }
280 -
281 +       /* Only send --suffix-dels if it specifies a non-default value. */
282 +       if (strcmp(backup_suffix_dels, backup_dir_dels ? "" : BACKUP_SUFFIX) != 0) {
283 +               /* We use the following syntax to avoid weirdness with '~'. */
284 +               if (asprintf(&arg, "--suffix-dels=%s", backup_suffix_dels) < 0)
285 +                       goto oom;
286 +               args[ac++] = arg;
287 +       }
288         if (am_sender) {
289                 if (delete_before)
290                         args[ac++] = "--delete-before";