Fixed failing hunks.
[rsync/rsync-patches.git] / backup-dir-dels.diff
1 This patches creates two new command line options as follows:
2         --backup-dir-rm
3         --backup-suffix-rm=SUFFIX
4
5 The backup-dir-rm and backup-suffix-rm 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       2005-02-14 02:45:09
17 +++ backup.c    2004-09-22 02:36:06
18 @@ -22,11 +22,17 @@
19  
20  extern int verbose;
21  extern int backup_suffix_len;
22 +extern int backup_suffix_rm_len;
23  extern int backup_dir_len;
24 +extern int backup_dir_rm_len;
25  extern unsigned int backup_dir_remainder;
26 +extern unsigned int backup_dir_rm_remainder;
27  extern char backup_dir_buf[MAXPATHLEN];
28 +extern char backup_dir_rm_buf[MAXPATHLEN];
29  extern char *backup_suffix;
30 +extern char *backup_suffix_rm;
31  extern char *backup_dir;
32 +extern char *backup_dir_rm;
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,10 +60,27 @@ char *get_backup_name(char *fname)
46         return NULL;
47  }
48  
49 +static char *get_delete_name(char *fname)
50 +{
51 +       if (backup_dir_rm) {
52 +               if (stringjoin(backup_dir_rm_buf + backup_dir_rm_len, backup_dir_rm_remainder,
53 +                              fname, backup_suffix_rm, NULL) < backup_dir_rm_remainder)
54 +                       return backup_dir_rm_buf;
55 +       } else {
56 +               if (stringjoin(backup_dir_rm_buf, MAXPATHLEN,
57 +                              fname, backup_suffix_rm, NULL) < MAXPATHLEN)
58 +                       return backup_dir_rm_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 -       char *fnamebak = get_backup_name(fname);
69 +       char *fnamebak = deleting ? get_delete_name(fname)
70 +                                 : get_backup_name(fname);
71  
72         if (!fnamebak)
73                 return 0;
74 @@ -83,7 +108,8 @@ path
75  static int make_bak_dir(char *fullpath)
76  {
77         STRUCT_STAT st;
78 -       char *rel = fullpath + backup_dir_len;
79 +       int dir_len = deleting ? backup_dir_rm_len : backup_dir_len;
80 +       char *rel = fullpath + dir_len;
81         char *end = rel + strlen(rel);
82         char *p = end;
83  
84 @@ -170,7 +196,8 @@ static int keep_backup(char *fname)
85         if (!(file = make_file(fname, NULL, NO_FILTERS)))
86                 return 1; /* the file could have disappeared */
87  
88 -       if (!(buf = get_backup_name(fname)))
89 +       buf = deleting ? get_delete_name(fname) : get_backup_name(fname);
90 +       if (!buf)
91                 return 0;
92  
93         /* Check to see if this is a device file, or link */
94 @@ -263,3 +290,13 @@ int make_backup(char *fname)
95                 return keep_backup(fname);
96         return make_simple_backup(fname);
97  }
98 +
99 +/* backup switch routine called only when backing-up removed file */
100 +int safe_delete(char *fname)
101 +{
102 +       int ret;
103 +       deleting = 1;
104 +       ret = make_backup(fname);
105 +       deleting = 0;
106 +       return ret;
107 +}
108 --- orig/flist.c        2005-02-21 10:51:51
109 +++ flist.c     2005-02-21 11:00:19
110 @@ -67,6 +67,9 @@ extern char *log_format;
111  extern char *backup_dir;
112  extern char *backup_suffix;
113  extern int backup_suffix_len;
114 +extern char *backup_dir_rm;
115 +extern char *backup_suffix_rm;
116 +extern int backup_suffix_rm_len;
117  
118  extern char curr_dir[MAXPATHLEN];
119  
120 @@ -1773,10 +1776,14 @@ struct file_list *get_dirlist(const char
121  
122  static int deletion_count = 0; /* used to implement --max-delete */
123  
124 +/* Function now checks if file matches backup- or delete-suffix patterns. */
125  static int is_backup_file(char *fn)
126  {
127         int k = strlen(fn) - backup_suffix_len;
128 -       return k > 0 && strcmp(fn+k, backup_suffix) == 0;
129 +       if (k > 0 && strcmp(fn+k, backup_suffix) == 0)
130 +               return 1;
131 +       k += backup_suffix_len - backup_suffix_rm_len;
132 +       return k > 0 && strcmp(fn+k, backup_suffix_rm) == 0;
133  }
134  
135  
136 @@ -1850,8 +1857,8 @@ int delete_file(char *fname, int mode, i
137         if (max_delete && deletion_count >= max_delete)
138                 return -1;
139  
140 -       if (make_backups && !backup_dir && !is_backup_file(fname))
141 -               ok = make_backup(fname);
142 +       if (make_backups && !backup_dir_rm && !is_backup_file(fname))
143 +               ok = safe_delete(fname);
144         else
145                 ok = do_rmdir(fname) == 0;
146         if (!ok && errno != ENOENT) {
147 --- orig/options.c      2005-02-21 10:51:52
148 +++ options.c   2005-02-21 11:02:45
149 @@ -126,10 +126,14 @@ int no_detach
150  int write_batch = 0;
151  int read_batch = 0;
152  int backup_dir_len = 0;
153 +int backup_dir_rm_len = 0;     
154  int backup_suffix_len;
155 +int backup_suffix_rm_len;
156  unsigned int backup_dir_remainder;
157 +unsigned int backup_dir_rm_remainder;
158  
159  char *backup_suffix = NULL;
160 +char *backup_suffix_rm = NULL;
161  char *tmpdir = NULL;
162  char *partial_dir = NULL;
163  char *basis_dir[MAX_BASIS_DIRS+1];
164 @@ -139,7 +143,9 @@ char *log_format = NULL;
165  char *password_file = NULL;
166  char *rsync_path = RSYNC_PATH;
167  char *backup_dir = NULL;
168 +char *backup_dir_rm = NULL;
169  char backup_dir_buf[MAXPATHLEN];
170 +char backup_dir_rm_buf[MAXPATHLEN];
171  int rsync_port = 0;
172  int compare_dest = 0;
173  int copy_dest = 0;
174 @@ -270,6 +276,8 @@ void usage(enum logcode F)
175    rprintf(F," -b, --backup                make backups (see --suffix & --backup-dir)\n");
176    rprintf(F,"     --backup-dir=DIR        make backups into hierarchy based in DIR\n");
177    rprintf(F,"     --suffix=SUFFIX         set backup suffix (default %s w/o --backup-dir)\n",BACKUP_SUFFIX);
178 +  rprintf(F,"     --backup-dir-rm         make backups of removed files into current dir\n");
179 +  rprintf(F,"     --backup-suffix-rm=SUF  set removed-files suffix (defaults to --suffix)\n");
180    rprintf(F," -u, --update                skip files that are newer on the receiver\n");
181    rprintf(F,"     --inplace               update destination files in-place (SEE MAN PAGE)\n");
182    rprintf(F," -d, --dirs                  transfer directories without recursing\n");
183 @@ -365,6 +373,7 @@ static struct poptOption long_options[] 
184    /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
185    {"version",          0,  POPT_ARG_NONE,   0, OPT_VERSION, 0, 0},
186    {"suffix",           0,  POPT_ARG_STRING, &backup_suffix, 0, 0, 0 },
187 +  {"backup-suffix-rm", 0,  POPT_ARG_STRING, &backup_suffix_rm, 0, 0, 0 },
188    {"rsync-path",       0,  POPT_ARG_STRING, &rsync_path, 0, 0, 0 },
189    {"password-file",    0,  POPT_ARG_STRING, &password_file, 0, 0, 0 },
190    {"ignore-times",    'I', POPT_ARG_NONE,   &ignore_times, 0, 0, 0 },
191 @@ -445,6 +454,7 @@ static struct poptOption long_options[] 
192    {"itemize-changes", 'i', POPT_ARG_NONE,   &itemize_changes, 0, 0, 0 },
193    {"bwlimit",          0,  POPT_ARG_INT,    &bwlimit, 0, 0, 0 },
194    {"backup-dir",       0,  POPT_ARG_STRING, &backup_dir, 0, 0, 0 },
195 +  {"backup-dir-rm",    0,  POPT_ARG_STRING, &backup_dir_rm, 0, 0, 0 },
196    {"hard-links",      'H', POPT_ARG_NONE,   &preserve_hard_links, 0, 0, 0 },
197    {"read-batch",       0,  POPT_ARG_STRING, &batch_name, OPT_READ_BATCH, 0, 0 },
198    {"write-batch",      0,  POPT_ARG_STRING, &batch_name, OPT_WRITE_BATCH, 0, 0 },
199 @@ -1013,6 +1023,8 @@ int parse_arguments(int *argc, const cha
200                         partial_dir = sanitize_path(NULL, partial_dir, NULL, 0);
201                 if (backup_dir)
202                         backup_dir = sanitize_path(NULL, backup_dir, NULL, 0);
203 +               if (backup_dir_rm)                                                      
204 +                       backup_dir_rm = sanitize_path(NULL, backup_dir_rm, NULL, 0);
205                 if (files_from)
206                         files_from = sanitize_path(NULL, files_from, NULL, 0);
207         }
208 @@ -1045,6 +1057,12 @@ int parse_arguments(int *argc, const cha
209                         if (check_filter(elp, backup_dir, 1) < 0)
210                                 goto options_rejected;
211                 }
212 +               /* Clean backup_dir_rm same as for backup_dir */
213 +               if (backup_dir_rm) {
214 +                       clean_fname(backup_dir_rm, 1);
215 +                       if (check_filter(elp, backup_dir_rm, 1) < 0)
216 +                               goto options_rejected;
217 +               }
218         }
219         if (server_filter_list.head && files_from) {
220                 if (!*files_from)
221 @@ -1067,6 +1085,16 @@ int parse_arguments(int *argc, const cha
222                         backup_suffix);
223                 return 0;
224         }
225 +       /* if suffix_rm not supplied, default to backup_suffix */
226 +       if (!backup_suffix_rm)
227 +               backup_suffix_rm = backup_dir_rm ? "" : backup_suffix;
228 +       backup_suffix_rm_len = strlen(backup_suffix_rm);
229 +       if (strchr(backup_suffix_rm, '/') != NULL) {
230 +               snprintf(err_buf, sizeof err_buf,
231 +                       "--backup-suffix-rm cannot contain slashes: %s\n",
232 +                       backup_suffix_rm);      
233 +               return 0;
234 +       }
235         if (backup_dir) {
236                 backup_dir_len = strlcpy(backup_dir_buf, backup_dir, sizeof backup_dir_buf);
237                 backup_dir_remainder = sizeof backup_dir_buf - backup_dir_len;
238 @@ -1088,6 +1116,31 @@ int parse_arguments(int *argc, const cha
239                         "--suffix cannot be a null string without --backup-dir\n");
240                 return 0;
241         }
242 +       /* If backup_dir_rm not supplied default to backup_dir if it has been supplied */
243 +       if (backup_dir && !backup_dir_rm) {
244 +               backup_dir_rm = backup_dir;
245 +               backup_dir_rm_len = backup_dir_len;
246 +               backup_dir_rm_remainder = backup_dir_remainder;
247 +               strlcpy(backup_dir_rm_buf, backup_dir_buf, sizeof backup_dir_buf);
248 +       } else if (backup_dir_rm) {
249 +               backup_dir_rm_len = strlcpy(backup_dir_rm_buf, backup_dir_rm, sizeof backup_dir_rm_buf);
250 +               backup_dir_rm_remainder = sizeof backup_dir_rm_buf - backup_dir_rm_len;
251 +               if (backup_dir_rm_remainder < 32) {
252 +                       snprintf(err_buf, sizeof err_buf,
253 +                               "the --backup-dir-rm path is WAY too long.\n");
254 +                       return 0;
255 +               }
256 +               if (backup_dir_rm_buf[backup_dir_rm_len - 1] != '/') {
257 +                       backup_dir_rm_buf[backup_dir_rm_len++] = '/';
258 +                       backup_dir_rm_buf[backup_dir_rm_len] = '\0';
259 +               }
260 +               if (verbose > 1 && !am_sender)
261 +                       rprintf(FINFO, "backup_dir_rm is %s\n", backup_dir_rm_buf);
262 +       } else if (!backup_suffix_rm_len && (!am_server || !am_sender)) {
263 +               snprintf(err_buf, sizeof err_buf,
264 +                       "--backup-suffix-rm cannot be a null string without --backup-dir-rm\n");
265 +               return 0;
266 +       }
267         if (make_backups && !backup_dir)
268                 omit_dir_times = 1;
269  
270 @@ -1353,6 +1406,10 @@ void server_options(char **args,int *arg
271                 args[ac++] = "--backup-dir";
272                 args[ac++] = backup_dir;
273         }
274 +       if (backup_dir_rm) {
275 +               args[ac++] = "--backup-dir-rm";
276 +               args[ac++] = backup_dir_rm;
277 +       }
278  
279         /* Only send --suffix if it specifies a non-default value. */
280         if (strcmp(backup_suffix, backup_dir ? "" : BACKUP_SUFFIX) != 0) {
281 @@ -1361,7 +1418,13 @@ void server_options(char **args,int *arg
282                         goto oom;
283                 args[ac++] = arg;
284         }
285 -
286 +       /* Only send --backup-suffix-rm if it specifies a non-default value. */
287 +       if (strcmp(backup_suffix_rm, backup_dir_rm ? "" : BACKUP_SUFFIX) != 0) {
288 +               /* We use the following syntax to avoid weirdness with '~'. */
289 +               if (asprintf(&arg, "--backup-suffix-rm=%s", backup_suffix_rm) < 0)
290 +                       goto oom;
291 +               args[ac++] = arg;
292 +       }
293         if (am_sender) {
294                 if (delete_excluded)
295                         args[ac++] = "--delete-excluded";