Fixed some failing hunks.
[rsync/rsync-patches.git] / backup-dir-dels.diff
1 This patches creates two new command line options as follows:
2         --delete-dir
3         --delete-suffix
4
5 The delete-dir and delete-suffix options give the ability to store 
6 backup of deleted files on the receiver in different directories
7 or with different suffix than the backup of files that have been
8 changed but that are still on the source drive.  Both commands can
9 be combined.
10
11 The default behaviour if one or both of the options are not specified
12 is the previous behaviour, both backups use the same directory or
13 suffix.
14
15 Marc St-Onge
16
17 --- orig/backup.c       2005-01-19 20:11:10
18 +++ backup.c    2004-09-22 02:36:06
19 @@ -22,11 +22,17 @@
20  
21  extern int verbose;
22  extern int backup_suffix_len;
23 +extern int delete_suffix_len;
24  extern int backup_dir_len;
25 +extern int delete_dir_len;
26  extern unsigned int backup_dir_remainder;
27 +extern unsigned int delete_dir_remainder;
28  extern char backup_dir_buf[MAXPATHLEN];
29 +extern char delete_dir_buf[MAXPATHLEN];
30  extern char *backup_suffix;
31 +extern char *delete_suffix;
32  extern char *backup_dir;
33 +extern char *delete_dir;
34  
35  extern int am_root;
36  extern int preserve_devices;
37 @@ -35,6 +41,8 @@ extern int preserve_hard_links;
38  extern int orig_umask;
39  extern int safe_symlinks;
40  
41 +static int deleting;
42 +
43  /* make a complete pathname for backup file */
44  char *get_backup_name(char *fname)
45  {
46 @@ -52,10 +60,27 @@ char *get_backup_name(char *fname)
47         return NULL;
48  }
49  
50 +static char *get_delete_name(char *fname)
51 +{
52 +       if (delete_dir) {
53 +               if (stringjoin(delete_dir_buf + delete_dir_len, delete_dir_remainder,
54 +                              fname, delete_suffix, NULL) < delete_dir_remainder)
55 +                       return delete_dir_buf;
56 +       } else {
57 +               if (stringjoin(delete_dir_buf, MAXPATHLEN,
58 +                              fname, delete_suffix, NULL) < MAXPATHLEN)
59 +                       return delete_dir_buf;
60 +       }
61 +
62 +       rprintf(FERROR, "delete filename too long\n");
63 +       return NULL;
64 +}
65 +
66  /* simple backup creates a backup with a suffix in the same directory */
67  static int make_simple_backup(char *fname)
68  {
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 @@ -81,7 +106,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 ? delete_dir_len : backup_dir_len;
81 +       char *rel = fullpath + dir_len;
82         char *end = rel + strlen(rel);
83         char *p = end;
84  
85 @@ -168,7 +194,8 @@ static int keep_backup(char *fname)
86         if (!(file = make_file(fname, NULL, NO_EXCLUDES)))
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 @@ -259,3 +286,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 deleted 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/flist.c        2005-01-24 01:43:09
110 +++ flist.c     2005-01-24 02:21:33
111 @@ -48,6 +48,8 @@ extern int xfer_dirs;
112  extern char curr_dir[MAXPATHLEN];
113  extern char *backup_dir;
114  extern char *backup_suffix;
115 +extern char *delete_dir;
116 +extern char *delete_suffix;
117  extern int filesfrom_fd;
118  
119  extern int one_file_system;
120 @@ -62,6 +64,7 @@ extern int relative_paths;
121  extern int implied_dirs;
122  extern int make_backups;
123  extern int backup_suffix_len;
124 +extern int delete_suffix_len;
125  extern int copy_links;
126  extern int copy_unsafe_links;
127  extern int protocol_version;
128 @@ -1670,10 +1673,14 @@ char *f_name(struct file_struct *f)
129         return f_name_to(f, names[n]);
130  }
131  
132 +/* Function now checks if file matches backup or delete suffix patterns */
133  static int is_backup_file(char *fn)
134  {
135         int k = strlen(fn) - backup_suffix_len;
136 -       return k > 0 && strcmp(fn+k, backup_suffix) == 0;
137 +       if (k > 0 && strcmp(fn+k, backup_suffix) == 0)
138 +               return 1;
139 +       k += backup_suffix_len - delete_suffix_len;
140 +       return k > 0 && strcmp(fn+k, delete_suffix) == 0;
141  }
142  
143  void delete_in_dir(struct file_list *flist, char *fname)
144 @@ -1727,11 +1734,12 @@ void delete_in_dir(struct file_list *fli
145                     || (delete_during && S_ISDIR(mode)
146                      && !S_ISDIR(flist->files[j]->mode))) {
147                         char *f = f_name(del_flist->files[i]);
148 -                       if (make_backups && (backup_dir || !is_backup_file(f))
149 +                       int backup_file = is_backup_file(f);
150 +                       if (make_backups && (delete_dir || !backup_file)
151                           && !S_ISDIR(mode)) {
152 -                               make_backup(f);
153 +                               safe_delete(f);
154                                 if (verbose) {
155 -                                       rprintf(FINFO, "deleting %s\n",
156 +                                       rprintf(FINFO, "safe-deleting %s\n",
157                                                 safe_fname(f));
158                                 }
159                         } else {
160 --- orig/options.c      2005-01-24 01:43:10
161 +++ options.c   2004-11-27 18:37:18
162 @@ -119,10 +119,14 @@ int no_detach = 0;
163  int write_batch = 0;
164  int read_batch = 0;
165  int backup_dir_len = 0;
166 +int delete_dir_len = 0;        
167  int backup_suffix_len;
168 +int delete_suffix_len;
169  unsigned int backup_dir_remainder;
170 +unsigned int delete_dir_remainder;
171  
172  char *backup_suffix = NULL;
173 +char *delete_suffix = NULL;
174  char *tmpdir = NULL;
175  char *partial_dir = NULL;
176  char *basis_dir[MAX_BASIS_DIRS+1];
177 @@ -132,7 +136,9 @@ char *log_format = NULL;
178  char *password_file = NULL;
179  char *rsync_path = RSYNC_PATH;
180  char *backup_dir = NULL;
181 +char *delete_dir = NULL;
182  char backup_dir_buf[MAXPATHLEN];
183 +char delete_dir_buf[MAXPATHLEN];
184  int rsync_port = 0;
185  int compare_dest = 0;
186  int copy_dest = 0;
187 @@ -250,7 +256,9 @@ void usage(enum logcode F)
188    rprintf(F,"     --no-implied-dirs       don't send implied dirs with -R\n");
189    rprintf(F," -b, --backup                make backups (see --suffix & --backup-dir)\n");
190    rprintf(F,"     --backup-dir            make backups into this directory\n");
191 +  rprintf(F,"     --delete-dir            make backups of deleted files into this directory\n");
192    rprintf(F,"     --suffix=SUFFIX         backup suffix (default %s w/o --backup-dir)\n",BACKUP_SUFFIX);
193 +  rprintf(F,"     --delete-suffix=SUFFIX  deleted files suffix (default %s w/o --backup-dir)\n",BACKUP_SUFFIX);
194    rprintf(F," -u, --update                update only (don't overwrite newer files)\n");
195    rprintf(F,"     --inplace               update destination files in-place (SEE MAN PAGE)\n");
196    rprintf(F," -d, --dirs                  transfer directories without recursing\n");
197 @@ -337,6 +345,7 @@ static struct poptOption long_options[] 
198    /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
199    {"version",          0,  POPT_ARG_NONE,   0, OPT_VERSION, 0, 0},
200    {"suffix",           0,  POPT_ARG_STRING, &backup_suffix, 0, 0, 0 },
201 +  {"delete-suffix",    0,  POPT_ARG_STRING, &delete_suffix, 0, 0, 0 },
202    {"rsync-path",       0,  POPT_ARG_STRING, &rsync_path, 0, 0, 0 },
203    {"password-file",    0,  POPT_ARG_STRING, &password_file, 0, 0, 0 },
204    {"ignore-times",    'I', POPT_ARG_NONE,   &ignore_times, 0, 0, 0 },
205 @@ -409,6 +418,7 @@ static struct poptOption long_options[] 
206    {"log-format",       0,  POPT_ARG_STRING, &log_format, 0, 0, 0 },
207    {"bwlimit",          0,  POPT_ARG_INT,    &bwlimit, 0, 0, 0 },
208    {"backup-dir",       0,  POPT_ARG_STRING, &backup_dir, 0, 0, 0 },
209 +  {"delete-dir",       0,  POPT_ARG_STRING, &delete_dir, 0, 0, 0 },
210    {"hard-links",      'H', POPT_ARG_NONE,   &preserve_hard_links, 0, 0, 0 },
211    {"read-batch",       0,  POPT_ARG_STRING, &batch_name, OPT_READ_BATCH, 0, 0 },
212    {"write-batch",      0,  POPT_ARG_STRING, &batch_name, OPT_WRITE_BATCH, 0, 0 },
213 @@ -878,6 +888,8 @@ int parse_arguments(int *argc, const cha
214                         partial_dir = sanitize_path(NULL, partial_dir, NULL, 0);
215                 if (backup_dir)
216                         backup_dir = sanitize_path(NULL, backup_dir, NULL, 0);
217 +               if (delete_dir)                                                 
218 +                       delete_dir = sanitize_path(NULL, delete_dir, NULL, 0);
219                 if (files_from)
220                         files_from = sanitize_path(NULL, files_from, NULL, 0);
221         }
222 @@ -904,6 +916,12 @@ int parse_arguments(int *argc, const cha
223                         if (check_exclude(elp, backup_dir, 1) < 0)
224                                 goto options_rejected;
225                 }
226 +               /* Clean delete_dir same as for backup_dir */
227 +               if (delete_dir) {
228 +                       clean_fname(delete_dir, 1);
229 +                       if (check_exclude(elp, delete_dir, 1) < 0)
230 +                               goto options_rejected;
231 +               }
232         }
233         if (server_exclude_list.head && files_from) {
234                 clean_fname(files_from, 1);
235 @@ -924,6 +942,16 @@ int parse_arguments(int *argc, const cha
236                         backup_suffix);
237                 return 0;
238         }
239 +       /* if deleted_suffix not supplied, default to backup_suffix */
240 +       if (!delete_suffix)
241 +               delete_suffix = delete_dir ? "" : backup_suffix;
242 +       delete_suffix_len = strlen(delete_suffix);
243 +       if (strchr(delete_suffix, '/') != NULL) {
244 +               snprintf(err_buf, sizeof err_buf,
245 +                       "--delete-suffix cannot contain slashes: %s\n",
246 +                       delete_suffix); 
247 +               return 0;
248 +       }
249         if (backup_dir) {
250                 backup_dir_len = strlcpy(backup_dir_buf, backup_dir, sizeof backup_dir_buf);
251                 backup_dir_remainder = sizeof backup_dir_buf - backup_dir_len;
252 @@ -943,6 +971,31 @@ int parse_arguments(int *argc, const cha
253                         "--suffix cannot be a null string without --backup-dir\n");
254                 return 0;
255         }
256 +       /* If delete_dir not supplied default to backup_dir if it has been supplied */
257 +       if (backup_dir && !delete_dir) {
258 +               delete_dir = backup_dir;
259 +               delete_dir_len = backup_dir_len;
260 +               delete_dir_remainder = backup_dir_remainder;
261 +               strlcpy(delete_dir_buf, backup_dir_buf, sizeof backup_dir_buf);
262 +       } else if (delete_dir) {
263 +               delete_dir_len = strlcpy(delete_dir_buf, delete_dir, sizeof delete_dir_buf);
264 +               delete_dir_remainder = sizeof delete_dir_buf - delete_dir_len;
265 +               if (delete_dir_remainder < 32) {
266 +                       snprintf(err_buf, sizeof err_buf,
267 +                               "the --delete-dir path is WAY too long.\n");
268 +                       return 0;
269 +               }
270 +               if (delete_dir_buf[delete_dir_len - 1] != '/') {
271 +                       delete_dir_buf[delete_dir_len++] = '/';
272 +                       delete_dir_buf[delete_dir_len] = '\0';
273 +               }
274 +               if (verbose > 1 && !am_sender)
275 +                       rprintf(FINFO, "delete_dir is %s\n", delete_dir_buf);
276 +       } else if (!delete_suffix_len && (!am_server || !am_sender)) {
277 +               snprintf(err_buf, sizeof err_buf,
278 +                       "--delete-suffix cannot be a null string without --delete-dir\n");
279 +               return 0;
280 +       }
281  
282         if (do_progress && !verbose)
283                 verbose = 1;
284 @@ -1156,6 +1209,10 @@ void server_options(char **args,int *arg
285                 args[ac++] = "--backup-dir";
286                 args[ac++] = backup_dir;
287         }
288 +       if (delete_dir) {
289 +               args[ac++] = "--delete-dir";
290 +               args[ac++] = delete_dir;
291 +       }
292  
293         /* Only send --suffix if it specifies a non-default value. */
294         if (strcmp(backup_suffix, backup_dir ? "" : BACKUP_SUFFIX) != 0) {
295 @@ -1164,7 +1221,13 @@ void server_options(char **args,int *arg
296                         goto oom;
297                 args[ac++] = arg;
298         }
299 -
300 +       /* Only send --delete-suffix if it specifies a non-default value. */
301 +       if (strcmp(delete_suffix, delete_dir ? "" : BACKUP_SUFFIX) != 0) {
302 +               /* We use the following syntax to avoid weirdness with '~'. */
303 +               if (asprintf(&arg, "--delete-suffix=%s", delete_suffix) < 0)
304 +                       goto oom;
305 +               args[ac++] = arg;
306 +       }
307         if (am_sender) {
308                 if (delete_excluded)
309                         args[ac++] = "--delete-excluded";