Updated patches to work with the current trunk.
[rsync/rsync-patches.git] / backup-dir-dels.diff
... / ...
CommitLineData
1This patches creates two new command line options as follows:
2 --backup-dir-dels=DIR
3 --suffix-dels=SUFFIX
4
5The backup-dir-dels and suffix-dels options give the ability to store
6backup of removed files on the receiver in different directories or with
7different suffix than the backup of files that have been changed but that
8are still on the source drive. Both commands can be combined.
9
10The default behaviour if one or both of the options are not specified
11is the previous behaviour, both backups use the same directory or
12suffix.
13
14Marc St-Onge
15
16To use this patch, run these commands for a successful build:
17
18 patch -p1 <patches/backup-dir-dels.diff
19 ./configure (optional if already run)
20 make
21
22based-on: 24079e988fc31af4eba56cd2701fdc5a4154980d
23diff --git a/backup.c b/backup.c
24--- a/backup.c
25+++ b/backup.c
26@@ -29,29 +29,36 @@ extern int preserve_specials;
27 extern int preserve_links;
28 extern int safe_symlinks;
29 extern int backup_dir_len;
30+extern int backup_dir_dels_len;
31 extern unsigned int backup_dir_remainder;
32+extern unsigned int backup_dir_dels_remainder;
33 extern char backup_dir_buf[MAXPATHLEN];
34+extern char backup_dir_dels_buf[MAXPATHLEN];
35 extern char *backup_suffix;
36+extern char *backup_suffix_dels;
37 extern char *backup_dir;
38+extern char *backup_dir_dels;
39+
40+static BOOL deleting;
41
42 /* Create a backup path from the given fname, putting the result into
43 * backup_dir_buf. Any new directories (compared to the prior backup
44 * path) are ensured to exist as directories, replacing anything else
45 * that may be in the way (e.g. a symlink). */
46-static BOOL copy_valid_path(const char *fname)
47+static BOOL copy_valid_path(const char *fname, char *buf, int prefix_len, unsigned int remainder, const char *suffix)
48 {
49 const char *f;
50 int flags;
51 BOOL ret = True;
52 stat_x sx;
53- char *b, *rel = backup_dir_buf + backup_dir_len, *name = rel;
54+ char *b, *rel = buf + prefix_len, *name = rel;
55
56 for (f = fname, b = rel; *f && *f == *b; f++, b++) {
57 if (*b == '/')
58 name = b + 1;
59 }
60
61- if (stringjoin(rel, backup_dir_remainder, fname, backup_suffix, NULL) >= backup_dir_remainder) {
62+ if (stringjoin(rel, remainder, fname, suffix, NULL) >= remainder) {
63 rprintf(FERROR, "backup filename too long\n");
64 *name = '\0';
65 return False;
66@@ -62,16 +69,16 @@ static BOOL copy_valid_path(const char *fname)
67 return True;
68 *b = '\0';
69
70- if (do_lstat(backup_dir_buf, &sx.st) < 0) {
71+ if (do_lstat(buf, &sx.st) < 0) {
72 if (errno == ENOENT)
73 break;
74- rsyserr(FERROR, errno, "backup lstat %s failed", backup_dir_buf);
75+ rsyserr(FERROR, errno, "backup lstat %s failed", buf);
76 *name = '\0';
77 return False;
78 }
79 if (!S_ISDIR(sx.st.st_mode)) {
80 flags = get_del_for_flag(sx.st.st_mode) | DEL_FOR_BACKUP | DEL_RECURSE;
81- if (delete_item(backup_dir_buf, sx.st.st_mode, flags) == 0)
82+ if (delete_item(buf, sx.st.st_mode, flags) == 0)
83 break;
84 *name = '\0';
85 return False;
86@@ -85,8 +92,8 @@ static BOOL copy_valid_path(const char *fname)
87 for ( ; b; name = b + 1, b = strchr(name, '/')) {
88 *b = '\0';
89
90- if (mkdir_defmode(backup_dir_buf) < 0) {
91- rsyserr(FERROR, errno, "backup mkdir %s failed", backup_dir_buf);
92+ if (mkdir_defmode(buf) < 0) {
93+ rsyserr(FERROR, errno, "backup mkdir %s failed", buf);
94 *name = '\0';
95 ret = False;
96 break;
97@@ -114,7 +121,7 @@ static BOOL copy_valid_path(const char *fname)
98 free_xattr(&sx);
99 }
100 #endif
101- set_file_attrs(backup_dir_buf, file, NULL, NULL, 0);
102+ set_file_attrs(buf, file, NULL, NULL, 0);
103 unmake_file(file);
104 }
105
106@@ -134,15 +141,20 @@ static BOOL copy_valid_path(const char *fname)
107 /* Make a complete pathname for backup file and verify any new path elements. */
108 char *get_backup_name(const char *fname)
109 {
110+ char *buf = deleting ? backup_dir_dels_buf : backup_dir_buf;
111+ char *suffix = deleting ? backup_suffix_dels : backup_suffix;
112+
113 if (backup_dir) {
114+ int prefix_len = deleting ? backup_dir_dels_len : backup_dir_len;
115+ unsigned int remainder = deleting ? backup_dir_dels_remainder : backup_dir_remainder;
116 /* copy fname into backup_dir_buf while validating the dirs. */
117- if (copy_valid_path(fname))
118- return backup_dir_buf;
119+ if (copy_valid_path(fname, buf, prefix_len, remainder, suffix))
120+ return buf;
121 return NULL;
122 } else {
123 if (stringjoin(backup_dir_buf, MAXPATHLEN,
124- fname, backup_suffix, NULL) < MAXPATHLEN)
125- return backup_dir_buf;
126+ fname, suffix, NULL) < MAXPATHLEN)
127+ return buf;
128 }
129
130 rprintf(FERROR, "backup filename too long\n");
131@@ -317,3 +329,13 @@ int make_backup(const char *fname, BOOL prefer_rename)
132 rprintf(FINFO, "backed up %s to %s\n", fname, buf);
133 return ret;
134 }
135+
136+/* backup switch routine called only when backing-up removed file */
137+int safe_delete(const char *fname)
138+{
139+ int ret;
140+ deleting = 1;
141+ ret = make_backup(fname, True);
142+ deleting = 0;
143+ return ret;
144+}
145diff --git a/delete.c b/delete.c
146--- a/delete.c
147+++ b/delete.c
148@@ -28,6 +28,9 @@ extern int max_delete;
149 extern char *backup_dir;
150 extern char *backup_suffix;
151 extern int backup_suffix_len;
152+extern char *backup_dir_dels;
153+extern char *backup_suffix_dels;
154+extern int backup_suffix_dels_len;
155 extern uid_t our_uid;
156 extern struct stats stats;
157
158@@ -35,10 +38,14 @@ int ignore_perishable = 0;
159 int non_perishable_cnt = 0;
160 int skipped_deletes = 0;
161
162+/* Function now compares both backup_suffix and backup_suffix_dels. */
163 static inline int is_backup_file(char *fn)
164 {
165 int k = strlen(fn) - backup_suffix_len;
166- return k > 0 && strcmp(fn+k, backup_suffix) == 0;
167+ if (k > 0 && strcmp(fn+k, backup_suffix) == 0)
168+ return 1;
169+ k += backup_suffix_len - backup_suffix_dels_len;
170+ return k > 0 && strcmp(fn+k, backup_suffix_dels) == 0;
171 }
172
173 /* The directory is about to be deleted: if DEL_RECURSE is given, delete all
174@@ -170,9 +177,9 @@ enum delret delete_item(char *fbuf, uint16 mode, uint16 flags)
175 what = "rmdir";
176 ok = do_rmdir(fbuf) == 0;
177 } else {
178- if (make_backups > 0 && !(flags & DEL_FOR_BACKUP) && (backup_dir || !is_backup_file(fbuf))) {
179+ if (make_backups > 0 && !(flags & DEL_FOR_BACKUP) && (backup_dir_dels || !is_backup_file(fbuf))) {
180 what = "make_backup";
181- ok = make_backup(fbuf, True);
182+ ok = safe_delete(fbuf);
183 if (ok == 2) {
184 what = "unlink";
185 ok = robust_unlink(fbuf) == 0;
186diff --git a/options.c b/options.c
187--- a/options.c
188+++ b/options.c
189@@ -151,10 +151,14 @@ int no_detach
190 int write_batch = 0;
191 int read_batch = 0;
192 int backup_dir_len = 0;
193+int backup_dir_dels_len = 0;
194 int backup_suffix_len;
195+int backup_suffix_dels_len;
196 unsigned int backup_dir_remainder;
197+unsigned int backup_dir_dels_remainder;
198
199 char *backup_suffix = NULL;
200+char *backup_suffix_dels = NULL;
201 char *tmpdir = NULL;
202 char *partial_dir = NULL;
203 char *basis_dir[MAX_BASIS_DIRS+1];
204@@ -166,7 +170,9 @@ char *stdout_format = NULL;
205 char *password_file = NULL;
206 char *rsync_path = RSYNC_PATH;
207 char *backup_dir = NULL;
208+char *backup_dir_dels = NULL;
209 char backup_dir_buf[MAXPATHLEN];
210+char backup_dir_dels_buf[MAXPATHLEN];
211 char *sockopts = NULL;
212 char *usermap = NULL;
213 char *groupmap = NULL;
214@@ -670,6 +676,8 @@ void usage(enum logcode F)
215 rprintf(F," -b, --backup make backups (see --suffix & --backup-dir)\n");
216 rprintf(F," --backup-dir=DIR make backups into hierarchy based in DIR\n");
217 rprintf(F," --suffix=SUFFIX set backup suffix (default %s w/o --backup-dir)\n",BACKUP_SUFFIX);
218+ rprintf(F," --backup-dir-dels=DIR backup removed files into hierarchy based in DIR\n");
219+ rprintf(F," --suffix-dels=SUFFIX set removed-files suffix (def. --suffix w/o b-d-d)\n");
220 rprintf(F," -u, --update skip files that are newer on the receiver\n");
221 rprintf(F," --inplace update destination files in-place (SEE MAN PAGE)\n");
222 rprintf(F," --append append data onto shorter files\n");
223@@ -969,7 +977,9 @@ static struct poptOption long_options[] = {
224 {"backup", 'b', POPT_ARG_VAL, &make_backups, 1, 0, 0 },
225 {"no-backup", 0, POPT_ARG_VAL, &make_backups, 0, 0, 0 },
226 {"backup-dir", 0, POPT_ARG_STRING, &backup_dir, 0, 0, 0 },
227+ {"backup-dir-dels", 0, POPT_ARG_STRING, &backup_dir_dels, 0, 0, 0 },
228 {"suffix", 0, POPT_ARG_STRING, &backup_suffix, 0, 0, 0 },
229+ {"suffix-dels", 0, POPT_ARG_STRING, &backup_suffix_dels, 0, 0, 0 },
230 {"list-only", 0, POPT_ARG_VAL, &list_only, 2, 0, 0 },
231 {"read-batch", 0, POPT_ARG_STRING, &batch_name, OPT_READ_BATCH, 0, 0 },
232 {"write-batch", 0, POPT_ARG_STRING, &batch_name, OPT_WRITE_BATCH, 0, 0 },
233@@ -1991,6 +2001,8 @@ int parse_arguments(int *argc_p, const char ***argv_p)
234 tmpdir = sanitize_path(NULL, tmpdir, NULL, 0, SP_DEFAULT);
235 if (backup_dir)
236 backup_dir = sanitize_path(NULL, backup_dir, NULL, 0, SP_DEFAULT);
237+ if (backup_dir_dels)
238+ backup_dir_dels = sanitize_path(NULL, backup_dir_dels, NULL, 0, SP_DEFAULT);
239 }
240 if (daemon_filter_list.head && !am_sender) {
241 filter_rule_list *elp = &daemon_filter_list;
242@@ -2012,6 +2024,14 @@ int parse_arguments(int *argc_p, const char ***argv_p)
243 if (check_filter(elp, FLOG, dir, 1) < 0)
244 goto options_rejected;
245 }
246+ /* Clean backup_dir_dels same as for backup_dir */
247+ if (backup_dir_dels) {
248+ if (!*backup_dir_dels)
249+ goto options_rejected;
250+ clean_fname(backup_dir_dels, 1);
251+ if (check_filter(elp, FLOG, backup_dir_dels, 1) < 0)
252+ goto options_rejected;
253+ }
254 }
255
256 if (!backup_suffix)
257@@ -2023,6 +2043,20 @@ int parse_arguments(int *argc_p, const char ***argv_p)
258 backup_suffix);
259 return 0;
260 }
261+ /* --suffix-dels defaults to --suffix, or empty for a client given an
262+ * explicit --backup-dir-dels (just as --suffix defaults to empty when
263+ * a --backup-dir is given). The second case does not apply to the
264+ * server for consistency with server_options, which sends --suffix-dels
265+ * to the server iff it differs from --suffix. */
266+ if (!backup_suffix_dels)
267+ backup_suffix_dels = backup_dir_dels && !am_server ? "" : backup_suffix;
268+ backup_suffix_dels_len = strlen(backup_suffix_dels);
269+ if (strchr(backup_suffix_dels, '/') != NULL) {
270+ snprintf(err_buf, sizeof err_buf,
271+ "--suffix-dels cannot contain slashes: %s\n",
272+ backup_suffix_dels);
273+ return 0;
274+ }
275 if (backup_dir) {
276 while (*backup_dir == '.' && backup_dir[1] == '/')
277 backup_dir += 2;
278@@ -2056,6 +2090,34 @@ int parse_arguments(int *argc_p, const char ***argv_p)
279 "P *%s", backup_suffix);
280 parse_filter_str(&filter_list, backup_dir_buf, rule_template(0), 0);
281 }
282+ if (backup_dir_dels) {
283+ backup_dir_dels_len = strlcpy(backup_dir_dels_buf, backup_dir_dels, sizeof backup_dir_dels_buf);
284+ backup_dir_dels_remainder = sizeof backup_dir_dels_buf - backup_dir_dels_len;
285+ if (backup_dir_dels_remainder < 32) {
286+ snprintf(err_buf, sizeof err_buf,
287+ "the --backup-dir-dels path is WAY too long.\n");
288+ return 0;
289+ }
290+ if (backup_dir_dels_buf[backup_dir_dels_len - 1] != '/') {
291+ backup_dir_dels_buf[backup_dir_dels_len++] = '/';
292+ backup_dir_dels_buf[backup_dir_dels_len] = '\0';
293+ }
294+ if (INFO_GTE(BACKUP, 1) && !am_sender)
295+ rprintf(FINFO, "backup_dir_dels is %s\n", backup_dir_dels_buf);
296+ } else if (backup_dir) {
297+ backup_dir_dels = backup_dir;
298+ backup_dir_dels_len = backup_dir_len;
299+ backup_dir_dels_remainder = backup_dir_remainder;
300+ strlcpy(backup_dir_dels_buf, backup_dir_buf, sizeof backup_dir_buf);
301+ } else if (!backup_suffix_dels_len && (!am_server || !am_sender)) {
302+ snprintf(err_buf, sizeof err_buf,
303+ "--suffix-dels cannot be a null string without --backup-dir-dels\n");
304+ return 0;
305+ } else if (make_backups && delete_mode && !delete_excluded && !am_server) {
306+ snprintf(backup_dir_dels_buf, sizeof backup_dir_dels_buf,
307+ "P *%s", backup_suffix_dels);
308+ parse_rule(&filter_list, backup_dir_dels_buf, 0, 0);
309+ }
310
311 if (make_backups && !backup_dir) {
312 omit_dir_times = 0; /* Implied, so avoid -O to sender. */
313@@ -2469,6 +2531,10 @@ void server_options(char **args, int *argc_p)
314 args[ac++] = "--backup-dir";
315 args[ac++] = backup_dir;
316 }
317+ if (backup_dir_dels && backup_dir_dels != backup_dir) {
318+ args[ac++] = "--backup-dir-dels";
319+ args[ac++] = backup_dir_dels;
320+ }
321
322 /* Only send --suffix if it specifies a non-default value. */
323 if (strcmp(backup_suffix, backup_dir ? "" : BACKUP_SUFFIX) != 0) {
324@@ -2477,7 +2543,14 @@ void server_options(char **args, int *argc_p)
325 goto oom;
326 args[ac++] = arg;
327 }
328-
329+ /* Only send --suffix-dels if it specifies a value different from the
330+ * --suffix value, which would normally be used for deletions too. */
331+ if (strcmp(backup_suffix_dels, backup_suffix) != 0) {
332+ /* We use the following syntax to avoid weirdness with '~'. */
333+ if (asprintf(&arg, "--suffix-dels=%s", backup_suffix_dels) < 0)
334+ goto oom;
335+ args[ac++] = arg;
336+ }
337 if (am_sender) {
338 if (max_delete > 0) {
339 if (asprintf(&arg, "--max-delete=%d", max_delete) < 0)