1 This patches creates two new command line options as follows:
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.
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
16 To use this patch, run these commands for a successful build:
18 patch -p1 <patches/backup-dir-dels.diff
19 ./configure (optional if already run)
24 @@ -29,10 +29,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;
40 /* make a complete pathname for backup file */
41 char *get_backup_name(const char *fname)
42 @@ -51,11 +58,28 @@ char *get_backup_name(const char *fname)
46 +static char *get_delete_name(const char *fname)
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;
53 + if (stringjoin(backup_dir_dels_buf, MAXPATHLEN,
54 + fname, backup_suffix_dels, NULL) < MAXPATHLEN)
55 + return backup_dir_dels_buf;
58 + rprintf(FERROR, "delete filename too long\n");
62 /* simple backup creates a backup with a suffix in the same directory */
63 static int make_simple_backup(const char *fname)
66 - const char *fnamebak = get_backup_name(fname);
67 + const char *fnamebak = deleting ? get_delete_name(fname)
68 + : get_backup_name(fname);
72 @@ -96,7 +120,8 @@ static int make_bak_dir(char *fullpath)
75 struct file_struct *file;
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);
82 @@ -212,7 +237,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 */
86 - if (!(buf = get_backup_name(fname))) {
87 + buf = deleting ? get_delete_name(fname) : get_backup_name(fname);
92 @@ -327,3 +353,13 @@ int make_backup(const char *fname)
93 return keep_backup(fname);
94 return make_simple_backup(fname);
97 +/* backup switch routine called only when backing-up removed file */
98 +int safe_delete(char *fname)
102 + ret = make_backup(fname);
108 @@ -94,6 +94,9 @@ extern mode_t orig_umask;
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;
118 @@ -131,10 +134,14 @@ enum delret {
119 static enum delret delete_dir_contents(char *fname, int flags);
122 +/* Function now compares both backup_suffix and backup_suffix_dels. */
123 static int is_backup_file(char *fn)
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)
129 + k += backup_suffix_len - backup_suffix_dels_len;
130 + return k > 0 && strcmp(fn+k, backup_suffix_dels) == 0;
133 /* Delete a file or directory. If DEL_RECURSE is set in the flags, this will
134 @@ -170,9 +177,9 @@ static enum delret delete_item(char *fbu
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);
145 ok = robust_unlink(fbuf) == 0;
148 @@ -145,10 +145,14 @@ int no_detach
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;
158 char *backup_suffix = NULL;
159 +char *backup_suffix_dels = NULL;
161 char *partial_dir = NULL;
162 char *basis_dir[MAX_BASIS_DIRS+1];
163 @@ -160,7 +164,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;
172 int compare_dest = 0;
173 @@ -316,6 +322,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 @@ -583,7 +591,9 @@ static struct poptOption long_options[]
183 {"backup", 'b', POPT_ARG_VAL, &make_backups, 1, 0, 0 },
184 {"no-backup", 0, POPT_ARG_VAL, &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 @@ -1378,6 +1388,8 @@ int parse_arguments(int *argc, const cha
193 tmpdir = sanitize_path(NULL, tmpdir, NULL, 0, NULL);
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);
199 if (server_filter_list.head && !am_sender) {
200 struct filter_list_struct *elp = &server_filter_list;
201 @@ -1395,6 +1407,14 @@ int parse_arguments(int *argc, const cha
202 if (check_filter(elp, backup_dir, 1) < 0)
203 goto options_rejected;
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;
216 @@ -1406,6 +1426,16 @@ int parse_arguments(int *argc, const cha
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);
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 @@ -1429,6 +1459,31 @@ int parse_arguments(int *argc, const cha
234 "P *%s", backup_suffix);
235 parse_rule(&filter_list, backup_dir_buf, 0, 0);
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");
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';
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");
263 if (make_backups && !backup_dir) {
264 omit_dir_times = 0; /* Implied, so avoid -O to sender. */
265 @@ -1816,6 +1871,10 @@ void server_options(char **args,int *arg
266 args[ac++] = "--backup-dir";
267 args[ac++] = backup_dir;
269 + if (backup_dir_dels && backup_dir_dels != backup_dir) {
270 + args[ac++] = "--backup-dir-dels";
271 + args[ac++] = backup_dir_dels;
274 /* Only send --suffix if it specifies a non-default value. */
275 if (strcmp(backup_suffix, backup_dir ? "" : BACKUP_SUFFIX) != 0) {
276 @@ -1824,7 +1883,13 @@ void server_options(char **args,int *arg
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)
289 if (max_delete > 0) {
290 if (asprintf(&arg, "--max-delete=%d", max_delete) < 0)