The patches for 3.2.3.
[rsync-patches.git] / source-backup.diff
1 This patch adds a --source-backup option that backs up source files
2 removed due to --remove-source-files.
3
4 To use this patch, run these commands for a successful build:
5
6     patch -p1 <patches/source-backup.diff
7     ./configure                         (optional if already run)
8     make
9
10 -- Matt McCutchen <hashproduct@gmail.com>
11
12 based-on: e94bad1c156fc3910f24e2b3b71a81b0b0bdeb70
13 diff --git a/options.c b/options.c
14 --- a/options.c
15 +++ b/options.c
16 @@ -32,6 +32,7 @@ extern filter_rule_list filter_list;
17  extern filter_rule_list daemon_filter_list;
18  
19  int make_backups = 0;
20 +int make_source_backups = 0;
21  
22  /**
23   * If 1, send the whole file as literal data rather than trying to
24 @@ -767,6 +768,7 @@ static struct poptOption long_options[] = {
25    {"bwlimit",          0,  POPT_ARG_STRING, &bwlimit_arg, OPT_BWLIMIT, 0, 0 },
26    {"no-bwlimit",       0,  POPT_ARG_VAL,    &bwlimit, 0, 0, 0 },
27    {"backup",          'b', POPT_ARG_VAL,    &make_backups, 1, 0, 0 },
28 +  {"source-backup",    0,  POPT_ARG_NONE,   &make_source_backups, 0, 0, 0},
29    {"no-backup",        0,  POPT_ARG_VAL,    &make_backups, 0, 0, 0 },
30    {"backup-dir",       0,  POPT_ARG_STRING, &backup_dir, 0, 0, 0 },
31    {"suffix",           0,  POPT_ARG_STRING, &backup_suffix, 0, 0, 0 },
32 @@ -2768,6 +2770,8 @@ void server_options(char **args, int *argc_p)
33                                 goto oom;
34                         args[ac++] = arg;
35                 }
36 +               if (make_source_backups)
37 +                       args[ac++] = "--source-backup";
38         }
39  
40         if (max_alloc_arg && max_alloc != DEFAULT_MAX_ALLOC) {
41 diff --git a/rsync.1.md b/rsync.1.md
42 --- a/rsync.1.md
43 +++ b/rsync.1.md
44 @@ -391,6 +391,7 @@ detailed description below for a complete description.
45  --existing               skip creating new files on receiver
46  --ignore-existing        skip updating files that exist on receiver
47  --remove-source-files    sender removes synchronized files (non-dir)
48 +--source-backup          ... and backs up those files
49  --del                    an alias for --delete-during
50  --delete                 delete extraneous files from dest dirs
51  --delete-before          receiver deletes before xfer, not during
52 @@ -1620,6 +1621,16 @@ your home directory (remove the '=' for that).
53      Starting with 3.1.0, rsync will skip the sender-side removal (and output an
54      error) if the file's size or modify time has not stayed unchanged.
55  
56 +0.  `--source-backup`
57 +
58 +    Makes the sender back up the source files it removes due to
59 +    `--remove-source-files`.  This option is independent of `--backup` but uses
60 +    the same `--backup-dir` and `--suffix` settings, if any.  With
61 +    `--backup-dir`, rsync looks for each file's backup dir relative to the
62 +    source argument the file came from.  Consequently, if the `--backup-dir`
63 +    path is relative, each source argument gets a separate backup dir at that
64 +    path relative to the argument.
65 +
66  0.  `--delete`
67  
68      This tells rsync to delete extraneous files from the receiving side (ones
69 diff --git a/sender.c b/sender.c
70 --- a/sender.c
71 +++ b/sender.c
72 @@ -41,6 +41,7 @@ extern int protocol_version;
73  extern int remove_source_files;
74  extern int updating_basis_file;
75  extern int make_backups;
76 +extern int make_source_backups;
77  extern int inplace;
78  extern int inplace_partial;
79  extern int batch_fd;
80 @@ -127,6 +128,7 @@ void successful_send(int ndx)
81         struct file_struct *file;
82         struct file_list *flist;
83         STRUCT_STAT st;
84 +       int result;
85  
86         if (!remove_source_files)
87                 return;
88 @@ -151,7 +153,11 @@ void successful_send(int ndx)
89                 return;
90         }
91  
92 -       if (do_unlink(fname) < 0) {
93 +       if (make_source_backups)
94 +               result = !make_backup(fname, True);
95 +       else
96 +               result = do_unlink(fname);
97 +       if (result < 0) {
98                 failed_op = "remove";
99           failed:
100                 if (errno == ENOENT)