One more tweak to the code in the backup.c file.
[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       2004-09-20 19:50:13
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 @@ -173,7 +199,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  #ifdef HAVE_MKNOD
95 @@ -266,3 +293,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/options.c      2004-09-20 05:10:48
110 +++ options.c   2004-09-22 02:25:03
111 @@ -113,10 +113,14 @@ int no_detach = 0;
112  int write_batch = 0;
113  int read_batch = 0;
114  int backup_dir_len = 0;
115 +int delete_dir_len = 0;        
116  int backup_suffix_len;
117 +int delete_suffix_len;
118  unsigned int backup_dir_remainder;
119 +unsigned int delete_dir_remainder;
120  
121  char *backup_suffix = NULL;
122 +char *delete_suffix = NULL;
123  char *tmpdir = NULL;
124  char *partial_dir = NULL;
125  char *compare_dest = NULL;
126 @@ -126,7 +130,9 @@ char *log_format = NULL;
127  char *password_file = NULL;
128  char *rsync_path = RSYNC_PATH;
129  char *backup_dir = NULL;
130 +char *delete_dir = NULL;
131  char backup_dir_buf[MAXPATHLEN];
132 +char delete_dir_buf[MAXPATHLEN];
133  int rsync_port = RSYNC_PORT;
134  int link_dest = 0;
135  
136 @@ -239,7 +245,9 @@ void usage(enum logcode F)
137    rprintf(F,"     --no-implied-dirs       don't send implied dirs with -R\n");
138    rprintf(F," -b, --backup                make backups (see --suffix & --backup-dir)\n");
139    rprintf(F,"     --backup-dir            make backups into this directory\n");
140 +  rprintf(F,"     --delete-dir            make backups of deleted files into this directory\n");
141    rprintf(F,"     --suffix=SUFFIX         backup suffix (default %s w/o --backup-dir)\n",BACKUP_SUFFIX);
142 +  rprintf(F,"     --delete-suffix=SUFFIX  deleted files suffix (default %s w/o --backup-dir)\n",BACKUP_SUFFIX);
143    rprintf(F," -u, --update                update only (don't overwrite newer files)\n");
144    rprintf(F,"     --inplace               update destination files inplace (SEE MAN PAGE)\n");
145    rprintf(F," -K, --keep-dirlinks         treat symlinked dir on receiver as dir\n");
146 @@ -326,6 +334,7 @@ static struct poptOption long_options[] 
147    /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
148    {"version",          0,  POPT_ARG_NONE,   0,              OPT_VERSION, 0, 0},
149    {"suffix",           0,  POPT_ARG_STRING, &backup_suffix, 0, 0, 0 },
150 +  {"delete-suffix",    0,  POPT_ARG_STRING, &delete_suffix, 0, 0, 0 },
151    {"rsync-path",       0,  POPT_ARG_STRING, &rsync_path, 0, 0, 0 },
152    {"password-file",    0,  POPT_ARG_STRING, &password_file, 0, 0, 0 },
153    {"ignore-times",    'I', POPT_ARG_NONE,   &ignore_times, 0, 0, 0 },
154 @@ -396,6 +405,7 @@ static struct poptOption long_options[] 
155    {"bwlimit",          0,  POPT_ARG_INT,    &bwlimit, 0, 0, 0 },
156    {"address",          0,  POPT_ARG_STRING, &bind_address, 0, 0, 0 },
157    {"backup-dir",       0,  POPT_ARG_STRING, &backup_dir, 0, 0, 0 },
158 +  {"delete-dir",       0,  POPT_ARG_STRING, &delete_dir, 0, 0, 0 },
159    {"hard-links",      'H', POPT_ARG_NONE,   &preserve_hard_links, 0, 0, 0 },
160    {"read-batch",       0,  POPT_ARG_STRING, &batch_name,  OPT_READ_BATCH, 0, 0 },
161    {"write-batch",      0,  POPT_ARG_STRING, &batch_name,  OPT_WRITE_BATCH, 0, 0 },
162 @@ -733,6 +743,8 @@ int parse_arguments(int *argc, const cha
163                         compare_dest = sanitize_path(NULL, compare_dest, NULL, 0);
164                 if (backup_dir)
165                         backup_dir = sanitize_path(NULL, backup_dir, NULL, 0);
166 +               if (delete_dir)                                                 
167 +                       delete_dir = sanitize_path(NULL, delete_dir, NULL, 0);
168                 if (files_from)
169                         files_from = sanitize_path(NULL, files_from, NULL, 0);
170         }
171 @@ -758,6 +770,12 @@ int parse_arguments(int *argc, const cha
172                         if (check_exclude(elp, backup_dir, 1) < 0)
173                                 goto options_rejected;
174                 }
175 +               /* Clean delete_dir same as for backup_dir */
176 +               if (delete_dir) {
177 +                       clean_fname(delete_dir, 1);
178 +                       if (check_exclude(elp, delete_dir, 1) < 0)
179 +                               goto options_rejected;
180 +               }
181         }
182         if (server_exclude_list.head && files_from) {
183                 clean_fname(files_from, 1);
184 @@ -784,6 +802,16 @@ int parse_arguments(int *argc, const cha
185                         backup_suffix);
186                 return 0;
187         }
188 +       /* if deleted_suffix not supplied, default to backup_suffix */
189 +       if (!delete_suffix)
190 +               delete_suffix = delete_dir ? "" : backup_suffix;
191 +       delete_suffix_len = strlen(delete_suffix);
192 +       if (strchr(delete_suffix, '/') != NULL) {
193 +               snprintf(err_buf, sizeof err_buf,
194 +                       "--delete-suffix cannot contain slashes: %s\n",
195 +                       delete_suffix); 
196 +               return 0;
197 +       }
198         if (backup_dir) {
199                 backup_dir_len = strlcpy(backup_dir_buf, backup_dir, sizeof backup_dir_buf);
200                 backup_dir_remainder = sizeof backup_dir_buf - backup_dir_len;
201 @@ -803,6 +831,31 @@ int parse_arguments(int *argc, const cha
202                         "--suffix cannot be a null string without --backup-dir\n");
203                 return 0;
204         }
205 +       /* If delete_dir not supplied default to backup_dir if it has been supplied */
206 +       if (backup_dir && !delete_dir) {
207 +               delete_dir = backup_dir;
208 +               delete_dir_len = backup_dir_len;
209 +               delete_dir_remainder = backup_dir_remainder;
210 +               strlcpy(delete_dir_buf, backup_dir_buf, sizeof backup_dir_buf);
211 +       } else if (delete_dir) {
212 +               delete_dir_len = strlcpy(delete_dir_buf, delete_dir, sizeof delete_dir_buf);
213 +               delete_dir_remainder = sizeof delete_dir_buf - delete_dir_len;
214 +               if (delete_dir_remainder < 32) {
215 +                       snprintf(err_buf, sizeof err_buf,
216 +                               "the --delete-dir path is WAY too long.\n");
217 +                       return 0;
218 +               }
219 +               if (delete_dir_buf[delete_dir_len - 1] != '/') {
220 +                       delete_dir_buf[delete_dir_len++] = '/';
221 +                       delete_dir_buf[delete_dir_len] = '\0';
222 +               }
223 +               if (verbose > 1 && !am_sender)
224 +                       rprintf(FINFO, "delete_dir is %s\n", delete_dir_buf);
225 +       } else if (!delete_suffix_len && (!am_server || !am_sender)) {
226 +               snprintf(err_buf, sizeof err_buf,
227 +                       "--delete-suffix cannot be a null string without --delete-dir\n");
228 +               return 0;
229 +       }
230  
231         if (do_progress && !verbose)
232                 verbose = 1;
233 @@ -1005,6 +1058,10 @@ void server_options(char **args,int *arg
234                 args[ac++] = "--backup-dir";
235                 args[ac++] = backup_dir;
236         }
237 +       if (delete_dir) {
238 +               args[ac++] = "--delete-dir";
239 +               args[ac++] = delete_dir;
240 +       }
241  
242         /* Only send --suffix if it specifies a non-default value. */
243         if (strcmp(backup_suffix, backup_dir ? "" : BACKUP_SUFFIX) != 0) {
244 @@ -1013,7 +1070,13 @@ void server_options(char **args,int *arg
245                         goto oom;
246                 args[ac++] = arg;
247         }
248 -
249 +       /* Only send --delete-suffix if it specifies a non-default value. */
250 +       if (strcmp(delete_suffix, delete_dir ? "" : BACKUP_SUFFIX) != 0) {
251 +               /* We use the following syntax to avoid weirdness with '~'. */
252 +               if (asprintf(&arg, "--delete-suffix=%s", delete_suffix) < 0)
253 +                       goto oom;
254 +               args[ac++] = arg;
255 +       }
256         if (am_sender) {
257                 if (delete_excluded)
258                         args[ac++] = "--delete-excluded";
259 --- orig/receiver.c     2004-09-21 09:40:27
260 +++ receiver.c  2004-09-22 02:09:20
261 @@ -42,8 +42,11 @@ extern char *compare_dest;
262  extern int make_backups;
263  extern int do_progress;
264  extern char *backup_dir;
265 +extern char *delete_dir;
266  extern char *backup_suffix;
267 +extern char *delete_suffix;
268  extern int backup_suffix_len;
269 +extern int delete_suffix_len;
270  extern int cleanup_got_literal;
271  extern int module_id;
272  extern int ignore_errors;
273 @@ -77,11 +80,14 @@ static void delete_one(char *fn, int is_
274         }
275  }
276  
277 -
278 +/* Function now checks if file matches backup or delete suffix patterns */
279  static int is_backup_file(char *fn)
280  {
281         int k = strlen(fn) - backup_suffix_len;
282 -       return k > 0 && strcmp(fn+k, backup_suffix) == 0;
283 +       if (k > 0 && strcmp(fn+k, backup_suffix) == 0)
284 +               return 1;
285 +       k = strlen(fn) - delete_suffix_len;
286 +       return k > 0 && strcmp(fn+k, delete_suffix) == 0;
287  }
288  
289  
290 @@ -122,10 +128,11 @@ void delete_files(struct file_list *flis
291                                 continue;
292                         if (flist_find(flist,local_file_list->files[i]) < 0) {
293                                 char *f = f_name(local_file_list->files[i]);
294 -                               if (make_backups && (backup_dir || !is_backup_file(f))) {
295 -                                       make_backup(f);
296 +                               int backup_file = is_backup_file(f);
297 +                               if (make_backups && (delete_dir || !backup_file)) {
298 +                                       safe_delete(f);
299                                         if (verbose) {
300 -                                               rprintf(FINFO, "deleting %s\n",
301 +                                               rprintf(FINFO, "safe-deleting %s\n",
302                                                         safe_fname(f));
303                                         }
304                                 } else {