Updated to the latest source.
[rsync-patches.git] / backup-deleted.diff
1 This patches adds the --backup-deleted option, as proposed by Jonathan
2 Kames in bug 7889.
3
4 To use this patch, run these commands for a successful build:
5
6     patch -p1 <patches/backup-deleted.diff
7     ./configure                                 (optional if already run)
8     make
9
10 based-on: a3377921ebe651cb7d2b969853cb9fe0e135ff75
11 diff --git a/generator.c b/generator.c
12 --- a/generator.c
13 +++ b/generator.c
14 @@ -1813,7 +1813,7 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
15                 goto notify_others;
16  
17         if (read_batch || whole_file) {
18 -               if (inplace && make_backups > 0 && fnamecmp_type == FNAMECMP_FNAME) {
19 +               if (inplace && make_backups > 1 && fnamecmp_type == FNAMECMP_FNAME) {
20                         if (!(backupptr = get_backup_name(fname)))
21                                 goto cleanup;
22                         if (!(back_file = make_file(fname, NULL, NULL, 0, NO_FILTERS)))
23 @@ -1849,7 +1849,7 @@ static void recv_generator(char *fname, struct file_struct *file, int ndx,
24                 goto notify_others;
25         }
26  
27 -       if (inplace && make_backups > 0 && fnamecmp_type == FNAMECMP_FNAME) {
28 +       if (inplace && make_backups > 1 && fnamecmp_type == FNAMECMP_FNAME) {
29                 if (!(backupptr = get_backup_name(fname))) {
30                         close(fd);
31                         goto cleanup;
32 @@ -1973,7 +1973,7 @@ int atomic_create(struct file_struct *file, char *fname, const char *slnk, const
33                 skip_atomic = 0;
34  
35         if (del_for_flag) {
36 -               if (make_backups > 0 && !dir_in_the_way) {
37 +               if (make_backups > 1 && !dir_in_the_way) {
38                         if (!make_backup(fname, skip_atomic))
39                                 return 0;
40                 } else if (skip_atomic) {
41 diff --git a/options.c b/options.c
42 --- a/options.c
43 +++ b/options.c
44 @@ -741,6 +741,7 @@ void usage(enum logcode F)
45    rprintf(F," -R, --relative              use relative path names\n");
46    rprintf(F,"     --no-implied-dirs       don't send implied dirs with --relative\n");
47    rprintf(F," -b, --backup                make backups (see --suffix & --backup-dir)\n");
48 +  rprintf(F,"     --backup-deleted        make backups only of deleted files\n");
49    rprintf(F,"     --backup-dir=DIR        make backups into hierarchy based in DIR\n");
50    rprintf(F,"     --suffix=SUFFIX         set backup suffix (default %s w/o --backup-dir)\n",BACKUP_SUFFIX);
51    rprintf(F," -u, --update                skip files that are newer on the receiver\n");
52 @@ -1073,7 +1074,8 @@ static struct poptOption long_options[] = {
53    {"no-i",             0,  POPT_ARG_VAL,    &itemize_changes, 0, 0, 0 },
54    {"bwlimit",          0,  POPT_ARG_STRING, &bwlimit_arg, OPT_BWLIMIT, 0, 0 },
55    {"no-bwlimit",       0,  POPT_ARG_VAL,    &bwlimit, 0, 0, 0 },
56 -  {"backup",          'b', POPT_ARG_VAL,    &make_backups, 1, 0, 0 },
57 +  {"backup",          'b', POPT_ARG_VAL,    &make_backups, 2, 0, 0 },
58 +  {"backup-deleted",   0,  POPT_ARG_VAL,    &make_backups, 1, 0, 0 },
59    {"no-backup",        0,  POPT_ARG_VAL,    &make_backups, 0, 0, 0 },
60    {"backup-dir",       0,  POPT_ARG_STRING, &backup_dir, 0, 0, 0 },
61    {"suffix",           0,  POPT_ARG_STRING, &backup_suffix, 0, 0, 0 },
62 @@ -2817,6 +2819,10 @@ void server_options(char **args, int *argc_p)
63         }
64  
65         if (am_sender) {
66 +               /* A remote sender just needs the above -b option.
67 +                * A remote receiver will override that with this option. */
68 +               if (make_backups == 1)
69 +                       args[ac++] = "--backup-deleted";
70                 if (max_delete > 0) {
71                         if (asprintf(&arg, "--max-delete=%d", max_delete) < 0)
72                                 goto oom;
73 diff --git a/receiver.c b/receiver.c
74 --- a/receiver.c
75 +++ b/receiver.c
76 @@ -420,7 +420,7 @@ static void handle_delayed_updates(char *local_name)
77                 struct file_struct *file = cur_flist->files[ndx];
78                 fname = local_name ? local_name : f_name(file, NULL);
79                 if ((partialptr = partial_dir_fname(fname)) != NULL) {
80 -                       if (make_backups > 0 && !make_backup(fname, False))
81 +                       if (make_backups > 1 && !make_backup(fname, False))
82                                 continue;
83                         if (DEBUG_GTE(RECV, 1)) {
84                                 rprintf(FINFO, "renaming %s to %s\n",
85 @@ -736,7 +736,7 @@ int recv_files(int f_in, int f_out, char *local_name)
86                 } else {
87                         /* Reminder: --inplace && --partial-dir are never
88                          * enabled at the same time. */
89 -                       if (inplace && make_backups > 0) {
90 +                       if (inplace && make_backups > 1) {
91                                 if (!(fnamecmp = get_backup_name(fname)))
92                                         fnamecmp = fname;
93                                 else
94 diff --git a/rsync.1.md b/rsync.1.md
95 --- a/rsync.1.md
96 +++ b/rsync.1.md
97 @@ -341,6 +341,7 @@ detailed description below for a complete description.
98  --relative, -R              use relative path names
99  --no-implied-dirs           don't send implied dirs with --relative
100  --backup, -b                make backups (see --suffix & --backup-dir)
101 +--backup-deleted            make backups only of deleted files
102  --backup-dir=DIR            make backups into hierarchy based in DIR
103  --suffix=SUFFIX             backup suffix (default ~ w/o --backup-dir)
104  --update, -u                skip files that are newer on the receiver
105 @@ -831,6 +832,13 @@ your home directory (remove the '=' for that).
106      trailing inclusion/exclusion of `*`, the auto-added rule would never be
107      reached).
108  
109 +0.  --backup-deleted
110 +
111 +    With this option, deleted destination files are renamed, while modified
112 +    destination files are not. Otherwise, this option behaves the same as
113 +    `--backup`, described above.  Note that if `--backup` is also specified,
114 +    whichever option is specified last takes precedence.
115 +
116  0.  `--backup-dir=DIR`
117  
118      In combination with the `--backup` option, this tells rsync to store all
119 diff --git a/rsync.c b/rsync.c
120 --- a/rsync.c
121 +++ b/rsync.c
122 @@ -699,7 +699,7 @@ int finish_transfer(const char *fname, const char *fnametmp,
123                 goto do_set_file_attrs;
124         }
125  
126 -       if (make_backups > 0 && overwriting_basis) {
127 +       if (make_backups > 1 && overwriting_basis) {
128                 int ok = make_backup(fname, False);
129                 if (!ok)
130                         exit_cleanup(RERR_FILEIO);