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