5dc442fac8517f39d8bab0ce77ad4b060c67d6ad
[rsync/rsync-patches.git] / remove-sent-files.diff
1 After applying this patch and running configure, you MUST run this
2 command before "make":
3
4     make proto
5
6
7 --- orig/io.c   2005-01-28 06:51:59
8 +++ io.c        2005-01-10 10:49:17
9 @@ -244,6 +244,14 @@ static void read_msg_fd(void)
10                 read_loop(fd, buf, 4);
11                 redo_list_add(IVAL(buf,0));
12                 break;
13 +       case MSG_SUCCESS:
14 +               if (len != 4) {
15 +                       rprintf(FERROR, "invalid message %d:%d\n", tag, len);
16 +                       exit_cleanup(RERR_STREAMIO);
17 +               }
18 +               read_loop(fd, buf, len);
19 +               io_multiplex_write(MSG_SUCCESS, buf, len);
20 +               break;
21         case MSG_INFO:
22         case MSG_ERROR:
23         case MSG_LOG:
24 @@ -677,6 +685,16 @@ static int readfd_unbuffered(int fd, cha
25                         read_loop(fd, iobuf_in, remaining);
26                         iobuf_in_ndx = 0;
27                         break;
28 +               case MSG_SUCCESS:
29 +                       if (remaining != 4) {
30 +                               rprintf(FERROR, "invalid multi-message %d:%ld\n",
31 +                                       tag, (long)remaining);
32 +                               exit_cleanup(RERR_STREAMIO);
33 +                       }
34 +                       read_loop(fd, line, remaining);
35 +                       successful_send(IVAL(line, 0));
36 +                       remaining = 0;
37 +                       break;
38                 case MSG_INFO:
39                 case MSG_ERROR:
40                         if (remaining >= sizeof line) {
41 --- orig/main.c 2005-01-27 22:25:43
42 +++ main.c      2005-01-28 17:13:25
43 @@ -36,6 +36,7 @@ extern int delete_mode;
44  extern int delete_before;
45  extern int delete_after;
46  extern int delete_excluded;
47 +extern int delete_sent_files;
48  extern int daemon_over_rsh;
49  extern int do_stats;
50  extern int dry_run;
51 @@ -43,6 +44,7 @@ extern int list_only;
52  extern int local_server;
53  extern int log_got_error;
54  extern int module_id;
55 +extern int need_messages_from_generator;
56  extern int orig_umask;
57  extern int copy_links;
58  extern int keep_dirlinks;
59 @@ -437,6 +439,12 @@ static void do_server_sender(int f_in, i
60                 exit_cleanup(RERR_SYNTAX);
61                 return;
62         }
63 +       if (am_daemon && lp_read_only(module_id) && delete_sent_files) {
64 +               rprintf(FERROR,
65 +                   "ERROR: --delete-sent-files cannot be used with a read-only module\n");
66 +               exit_cleanup(RERR_SYNTAX);
67 +               return;
68 +       }
69  
70         if (!relative_paths && !push_dir(dir)) {
71                 rsyserr(FERROR, errno, "push_dir#3 %s failed",
72 @@ -671,6 +679,8 @@ void start_server(int f_in, int f_out, i
73  
74         if (am_sender) {
75                 keep_dirlinks = 0; /* Must be disabled on the sender. */
76 +               if (need_messages_from_generator)
77 +                       io_start_multiplex_in();
78  
79                 recv_filter_list(f_in);
80                 if (cvs_exclude)
81 @@ -753,6 +763,9 @@ int client_run(int f_in, int f_out, pid_
82                 exit_cleanup(status);
83         }
84  
85 +       if (need_messages_from_generator && !read_batch)
86 +               io_start_multiplex_out();
87 +
88         if (argc == 0)
89                 list_only |= 1;
90  
91 --- orig/options.c      2005-01-28 17:12:13
92 +++ options.c   2005-01-23 07:30:51
93 @@ -59,6 +59,7 @@ int delete_during = 0;
94  int delete_before = 0;
95  int delete_after = 0;
96  int delete_excluded = 0;
97 +int delete_sent_files = 0;
98  int one_file_system = 0;
99  int protocol_version = PROTOCOL_VERSION;
100  int sparse_files = 0;
101 @@ -93,6 +94,7 @@ int bwlimit = 0;
102  size_t bwlimit_writemax = 0;
103  int only_existing = 0;
104  int opt_ignore_existing = 0;
105 +int need_messages_from_generator = 0;
106  int max_delete = 0;
107  OFF_T max_size = 0;
108  int ignore_errors = 0;
109 @@ -287,6 +289,7 @@ void usage(enum logcode F)
110    rprintf(F,"     --delete-during         receiver deletes during transfer, not before\n");
111    rprintf(F,"     --delete-after          receiver deletes after transfer, not before\n");
112    rprintf(F,"     --delete-excluded       also delete excluded files on the receiving side\n");
113 +  rprintf(F,"     --delete-sent-files     updated/sent files are removed from sending side\n");
114    rprintf(F,"     --ignore-errors         delete even if there are I/O errors\n");
115    rprintf(F,"     --force                 force deletion of directories even if not empty\n");
116    rprintf(F,"     --max-delete=NUM        don't delete more than NUM files\n");
117 @@ -361,6 +364,7 @@ static struct poptOption long_options[] 
118    {"delete-during",    0,  POPT_ARG_NONE,   &delete_during, 0, 0, 0 },
119    {"delete-after",     0,  POPT_ARG_NONE,   &delete_after, 0, 0, 0 },
120    {"delete-excluded",  0,  POPT_ARG_NONE,   &delete_excluded, 0, 0, 0 },
121 +  {"delete-sent-files",0,  POPT_ARG_NONE,   &delete_sent_files, 0, 0, 0 },
122    {"force",            0,  POPT_ARG_NONE,   &force_delete, 0, 0, 0 },
123    {"numeric-ids",      0,  POPT_ARG_NONE,   &numeric_ids, 0, 0, 0 },
124    {"filter",          'f', POPT_ARG_STRING, 0, OPT_FILTER, 0, 0 },
125 @@ -919,6 +923,9 @@ int parse_arguments(int *argc, const cha
126         else if (delete_mode || delete_excluded)
127                 delete_mode = delete_before = 1;
128  
129 +       if (delete_sent_files)
130 +               need_messages_from_generator = 1;
131 +
132         *argv = poptGetArgs(pc);
133         *argc = count_args(*argv);
134  
135 @@ -1315,6 +1322,9 @@ void server_options(char **args,int *arg
136         if (!implied_dirs && !am_sender)
137                 args[ac++] = "--no-implied-dirs";
138  
139 +       if (delete_sent_files)
140 +               args[ac++] = "--delete-sent-files";
141 +
142         *argc = ac;
143         return;
144  
145 --- orig/receiver.c     2005-01-27 23:57:45
146 +++ receiver.c  2004-08-13 08:38:51
147 @@ -42,6 +42,7 @@ extern int basis_dir_cnt;
148  extern int make_backups;
149  extern int do_progress;
150  extern int cleanup_got_literal;
151 +extern int delete_sent_files;
152  extern int module_id;
153  extern int ignore_errors;
154  extern int orig_umask;
155 @@ -271,7 +272,7 @@ int recv_files(int f_in, struct file_lis
156         char *fname, fbuf[MAXPATHLEN];
157         char template[MAXPATHLEN];
158         char fnametmp[MAXPATHLEN];
159 -       char *fnamecmp, *partialptr;
160 +       char *fnamecmp, *partialptr, numbuf[4];
161         char fnamecmpbuf[MAXPATHLEN];
162         uchar *delayed_bits = NULL;
163         struct file_struct *file;
164 @@ -527,7 +528,12 @@ int recv_files(int f_in, struct file_lis
165  
166                 cleanup_disable();
167  
168 -               if (!recv_ok) {
169 +               if (recv_ok) {
170 +                       if (delete_sent_files) {
171 +                               SIVAL(numbuf, 0, i);
172 +                               send_msg(MSG_SUCCESS, numbuf, 4);
173 +                       }
174 +               } else {
175                         int msgtype = csum_length == SUM_LENGTH || read_batch ?
176                                 FERROR : FINFO;
177                         if (msgtype == FERROR || verbose) {
178 @@ -551,9 +557,8 @@ int recv_files(int f_in, struct file_lis
179                                         keptstr, redostr);
180                         }
181                         if (csum_length != SUM_LENGTH) {
182 -                               char buf[4];
183 -                               SIVAL(buf, 0, i);
184 -                               send_msg(MSG_REDO, buf, 4);
185 +                               SIVAL(numbuf, 0, i);
186 +                               send_msg(MSG_REDO, numbuf, 4);
187                         }
188                 }
189         }
190 --- orig/rsync.h        2005-01-28 17:12:14
191 +++ rsync.h     2004-07-03 20:17:10
192 @@ -60,6 +60,7 @@
193  #define FLAG_DEL_START (1<<0)
194  #define FLAG_HLINK_EOL (1<<1)  /* generator only */
195  #define FLAG_MOUNT_POINT (1<<2)        /* sender only */
196 +#define FLAG_SENT (1<<7)       /* sender only */
197  
198  /* update this if you make incompatible changes */
199  #define PROTOCOL_VERSION 29
200 @@ -145,6 +146,7 @@ enum logcode { FERROR=1, FINFO=2, FLOG=3
201  /* Messages types that are sent over the message channel.  The logcode
202   * values must all be present here with identical numbers. */
203  enum msgcode {
204 +       MSG_SUCCESS=6,  /* successfully updated indicated flist index */
205         MSG_DONE=5,     /* current phase is done */
206         MSG_REDO=4,     /* reprocess indicated flist index */
207         MSG_ERROR=FERROR, MSG_INFO=FINFO, MSG_LOG=FLOG, /* remote logging */
208 --- orig/rsync.yo       2005-01-28 17:12:14
209 +++ rsync.yo    2005-01-23 07:31:03
210 @@ -349,6 +349,7 @@ verb(
211       --delete-during         receiver deletes during xfer, not before
212       --delete-after          receiver deletes after transfer, not before
213       --delete-excluded       also delete excluded files on receiver
214 +     --delete-sent-files     updated/sent files are removed from sender
215       --ignore-errors         delete even if there are I/O errors
216       --force                 force deletion of dirs even if not empty
217       --max-delete=NUM        don't delete more than NUM files
218 @@ -738,6 +739,11 @@ receiving side that are not on the sendi
219  delete any files on the receiving side that are excluded (see --exclude).
220  See --delete (which is implied) for more details on file-deletion.
221  
222 +dit(bf(--delete-sent-files)) This tells rsync to remove the source files
223 +on the sending side that are successfully transferred to the receiving
224 +side.  Directories are not removed, nor are files that are identical on
225 +both systems.
226 +
227  dit(bf(--ignore-errors)) Tells --delete to go ahead and delete files
228  even when there are I/O errors.
229  
230 --- orig/sender.c       2005-01-25 00:00:31
231 +++ sender.c    2004-07-26 16:49:19
232 @@ -26,6 +26,7 @@ extern int io_error;
233  extern int dry_run;
234  extern int am_server;
235  extern int am_daemon;
236 +extern int delete_sent_files;
237  extern int protocol_version;
238  extern int updating_basis_file;
239  extern int make_backups;
240 @@ -94,7 +95,29 @@ static struct sum_struct *receive_sums(i
241         return s;
242  }
243  
244 +static struct file_list *the_flist;
245  
246 +void successful_send(int i)
247 +{
248 +       char fname[MAXPATHLEN];
249 +       struct file_struct *file;
250 +       unsigned int offset;
251 +
252 +       if (!the_flist || i < 0 || i >= the_flist->count)
253 +               return;
254 +
255 +       file = the_flist->files[i];
256 +       if (!(file->flags & FLAG_SENT))
257 +               return; /* We didn't send it -- impossible! */
258 +       if (file->basedir) {
259 +               offset = stringjoin(fname, sizeof fname,
260 +                                   file->basedir, "/", NULL);
261 +       } else
262 +               offset = 0;
263 +       f_name_to(file, fname + offset);
264 +       if (delete_sent_files && do_unlink(fname) == 0 && verbose > 0)
265 +               rprintf(FINFO, "sender removed %s\n", fname + offset);
266 +}
267  
268  void send_files(struct file_list *flist, int f_out, int f_in)
269  {
270 @@ -113,6 +136,8 @@ void send_files(struct file_list *flist,
271         if (verbose > 2)
272                 rprintf(FINFO, "send_files starting\n");
273  
274 +       the_flist = flist;
275 +
276         while (1) {
277                 unsigned int offset;
278  
279 @@ -249,6 +274,9 @@ void send_files(struct file_list *flist,
280                         rprintf(FINFO, "sender finished %s\n",
281                                 safe_fname(fname));
282                 }
283 +
284 +               /* Flag that we actually sent this entry. */
285 +               file->flags |= FLAG_SENT;
286         }
287         make_backups = save_make_backups;
288