Mention the bugfix in option-parsing error-reporting from a daemon.
[rsync/rsync.git] / receiver.c
1 /* -*- c-file-style: "linux" -*-
2
3    Copyright (C) 1996-2000 by Andrew Tridgell
4    Copyright (C) Paul Mackerras 1996
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "rsync.h"
22
23 extern int verbose;
24 extern int recurse;
25 extern int delete_mode;
26 extern int delete_after;
27 extern int max_delete;
28 extern int csum_length;
29 extern struct stats stats;
30 extern int dry_run;
31 extern int am_server;
32 extern int relative_paths;
33 extern int keep_dirlinks;
34 extern int preserve_hard_links;
35 extern int preserve_perms;
36 extern int cvs_exclude;
37 extern int io_error;
38 extern char *tmpdir;
39 extern char *compare_dest;
40 extern int make_backups;
41 extern int do_progress;
42 extern char *backup_dir;
43 extern char *backup_suffix;
44 extern int backup_suffix_len;
45 extern int cleanup_got_literal;
46 extern int module_id;
47 extern int ignore_errors;
48 extern int orig_umask;
49 extern int keep_partial;
50 extern int checksum_seed;
51
52 static void delete_one(char *fn, int is_dir)
53 {
54         if (!is_dir) {
55                 if (robust_unlink(fn) != 0) {
56                         rsyserr(FERROR, errno, "delete_one: unlink %s failed",
57                                 full_fname(fn));
58                 } else if (verbose) {
59                         rprintf(FINFO, "deleting %s\n", fn);
60                 }
61         } else {
62                 if (do_rmdir(fn) != 0) {
63                         if (errno == ENOTDIR && keep_dirlinks)
64                                 return delete_one(fn, 0);
65                         if (errno != ENOTEMPTY && errno != EEXIST) {
66                                 rsyserr(FERROR, errno,
67                                         "delete_one: rmdir %s failed",
68                                         full_fname(fn));
69                         }
70                 } else if (verbose) {
71                         rprintf(FINFO, "deleting directory %s\n", fn);
72                 }
73         }
74 }
75
76
77 static int is_backup_file(char *fn)
78 {
79         int k = strlen(fn) - backup_suffix_len;
80         return k > 0 && strcmp(fn+k, backup_suffix) == 0;
81 }
82
83
84 /* This deletes any files on the receiving side that are not present
85  * on the sending side. */
86 void delete_files(struct file_list *flist)
87 {
88         struct file_list *local_file_list;
89         int i, j;
90         char *argv[1], fbuf[MAXPATHLEN];
91         static int deletion_count;
92
93         if (cvs_exclude)
94                 add_cvs_excludes();
95
96         if (io_error && !(lp_ignore_errors(module_id) || ignore_errors)) {
97                 rprintf(FINFO,"IO error encountered - skipping file deletion\n");
98                 return;
99         }
100
101         for (j = 0; j < flist->count; j++) {
102                 if (!(flist->files[j]->flags & FLAG_TOP_DIR)
103                     || !S_ISDIR(flist->files[j]->mode))
104                         continue;
105
106                 argv[0] = f_name_to(flist->files[j], fbuf);
107
108                 if (!(local_file_list = send_file_list(-1, 1, argv)))
109                         continue;
110
111                 if (verbose > 1)
112                         rprintf(FINFO, "deleting in %s\n", fbuf);
113
114                 for (i = local_file_list->count-1; i >= 0; i--) {
115                         if (max_delete && deletion_count > max_delete)
116                                 break;
117                         if (!local_file_list->files[i]->basename)
118                                 continue;
119                         if (flist_find(flist,local_file_list->files[i]) < 0) {
120                                 char *f = f_name(local_file_list->files[i]);
121                                 if (make_backups && (backup_dir || !is_backup_file(f))) {
122                                         (void) make_backup(f);
123                                         if (verbose)
124                                                 rprintf(FINFO, "deleting %s\n", f);
125                                 } else {
126                                         int mode = local_file_list->files[i]->mode;
127                                         delete_one(f, S_ISDIR(mode) != 0);
128                                 }
129                                 deletion_count++;
130                         }
131                 }
132                 flist_free(local_file_list);
133         }
134 }
135
136
137 /*
138  * get_tmpname() - create a tmp filename for a given filename
139  *
140  *   If a tmpdir is defined, use that as the directory to
141  *   put it in.  Otherwise, the tmp filename is in the same
142  *   directory as the given name.  Note that there may be no
143  *   directory at all in the given name!
144  *
145  *   The tmp filename is basically the given filename with a
146  *   dot prepended, and .XXXXXX appended (for mkstemp() to
147  *   put its unique gunk in).  Take care to not exceed
148  *   either the MAXPATHLEN or NAME_MAX, esp. the last, as
149  *   the basename basically becomes 8 chars longer. In that
150  *   case, the original name is shortened sufficiently to
151  *   make it all fit.
152  *
153  *   Of course, there's no real reason for the tmp name to
154  *   look like the original, except to satisfy us humans.
155  *   As long as it's unique, rsync will work.
156  */
157
158 static int get_tmpname(char *fnametmp, char *fname)
159 {
160         char *f;
161         int     length = 0;
162         int     maxname;
163
164         if (tmpdir) {
165                 /* Note: this can't overflow, so the return value is safe */
166                 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
167                 fnametmp[length++] = '/';
168                 fnametmp[length] = '\0';        /* always NULL terminated */
169         }
170
171         if ((f = strrchr(fname, '/')) != NULL) {
172                 ++f;
173                 if (!tmpdir) {
174                         length = f - fname;
175                         /* copy up to and including the slash */
176                         strlcpy(fnametmp, fname, length + 1);
177                 }
178         } else
179                 f = fname;
180         fnametmp[length++] = '.';
181         fnametmp[length] = '\0';                /* always NULL terminated */
182
183         maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8);
184
185         if (maxname < 1) {
186                 rprintf(FERROR, "temporary filename too long: %s\n", fname);
187                 fnametmp[0] = '\0';
188                 return 0;
189         }
190
191         strlcpy(fnametmp + length, f, maxname);
192         strcat(fnametmp + length, ".XXXXXX");
193
194         return 1;
195 }
196
197
198 static int receive_data(int f_in,struct map_struct *mapbuf,int fd,char *fname,
199                         OFF_T total_size)
200 {
201         int i;
202         struct sum_struct sum;
203         unsigned int len;
204         OFF_T offset = 0;
205         OFF_T offset2;
206         char *data;
207         static char file_sum1[MD4_SUM_LENGTH];
208         static char file_sum2[MD4_SUM_LENGTH];
209         char *map=NULL;
210
211         read_sum_head(f_in, &sum);
212
213         sum_init(checksum_seed);
214
215         while ((i = recv_token(f_in, &data)) != 0) {
216                 if (do_progress)
217                         show_progress(offset, total_size);
218
219                 if (i > 0) {
220                         if (verbose > 3) {
221                                 rprintf(FINFO,"data recv %d at %.0f\n",
222                                         i,(double)offset);
223                         }
224
225                         stats.literal_data += i;
226                         cleanup_got_literal = 1;
227
228                         sum_update(data,i);
229
230                         if (fd != -1 && write_file(fd,data,i) != i) {
231                                 rsyserr(FERROR, errno, "write failed on %s",
232                                         full_fname(fname));
233                                 exit_cleanup(RERR_FILEIO);
234                         }
235                         offset += i;
236                         continue;
237                 }
238
239                 i = -(i+1);
240                 offset2 = i*(OFF_T)sum.blength;
241                 len = sum.blength;
242                 if (i == (int) sum.count-1 && sum.remainder != 0)
243                         len = sum.remainder;
244
245                 stats.matched_data += len;
246
247                 if (verbose > 3)
248                         rprintf(FINFO,"chunk[%d] of size %d at %.0f offset=%.0f\n",
249                                 i,len,(double)offset2,(double)offset);
250
251                 if (mapbuf) {
252                         map = map_ptr(mapbuf,offset2,len);
253
254                         see_token(map, len);
255                         sum_update(map,len);
256                 }
257
258                 if (fd != -1 && write_file(fd,map,len) != (int) len) {
259                         rsyserr(FERROR, errno, "write failed on %s",
260                                 full_fname(fname));
261                         exit_cleanup(RERR_FILEIO);
262                 }
263                 offset += len;
264         }
265
266         flush_write_file(fd);
267
268         if (do_progress)
269                 end_progress(total_size);
270
271         if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
272                 rsyserr(FERROR, errno, "write failed on %s",
273                         full_fname(fname));
274                 exit_cleanup(RERR_FILEIO);
275         }
276
277         sum_end(file_sum1);
278
279         read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
280         if (verbose > 2) {
281                 rprintf(FINFO,"got file_sum\n");
282         }
283         if (fd != -1 && memcmp(file_sum1,file_sum2,MD4_SUM_LENGTH) != 0) {
284                 return 0;
285         }
286         return 1;
287 }
288
289
290 /**
291  * main routine for receiver process.
292  *
293  * Receiver process runs on the same host as the generator process. */
294 int recv_files(int f_in,struct file_list *flist,char *local_name)
295 {
296         int fd1,fd2;
297         STRUCT_STAT st;
298         char *fname, fbuf[MAXPATHLEN];
299         char template[MAXPATHLEN];
300         char fnametmp[MAXPATHLEN];
301         char *fnamecmp;
302         char fnamecmpbuf[MAXPATHLEN];
303         struct map_struct *mapbuf;
304         int i;
305         struct file_struct *file;
306         int phase=0;
307         int recv_ok;
308         struct stats initial_stats;
309
310         if (verbose > 2) {
311                 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
312         }
313
314         if (flist->hlink_pool) {
315                 pool_destroy(flist->hlink_pool);
316                 flist->hlink_pool = NULL;
317         }
318
319         while (1) {
320                 cleanup_disable();
321
322                 i = read_int(f_in);
323                 if (i == -1) {
324                         if (phase == 0) {
325                                 phase++;
326                                 csum_length = SUM_LENGTH;
327                                 if (verbose > 2)
328                                         rprintf(FINFO,"recv_files phase=%d\n",phase);
329                                 send_msg(MSG_DONE, "", 0);
330                                 continue;
331                         }
332                         break;
333                 }
334
335                 if (i < 0 || i >= flist->count) {
336                         rprintf(FERROR,"Invalid file index %d in recv_files (count=%d)\n",
337                                 i, flist->count);
338                         exit_cleanup(RERR_PROTOCOL);
339                 }
340
341                 file = flist->files[i];
342
343                 stats.current_file_index = i;
344                 stats.num_transferred_files++;
345                 stats.total_transferred_size += file->length;
346                 cleanup_got_literal = 0;
347
348                 if (local_name)
349                         fname = local_name;
350                 else
351                         fname = f_name_to(file, fbuf);
352
353                 if (dry_run) {
354                         if (!am_server && verbose) {    /* log transfer */
355                                 rprintf(FINFO, "%s\n", fname);
356                         }
357                         continue;
358                 }
359
360                 initial_stats = stats;
361
362                 if (verbose > 2)
363                         rprintf(FINFO,"recv_files(%s)\n",fname);
364
365                 fnamecmp = fname;
366
367                 /* open the file */
368                 fd1 = do_open(fnamecmp, O_RDONLY, 0);
369
370                 if (fd1 == -1 && compare_dest != NULL) {
371                         /* try the file at compare_dest instead */
372                         pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
373                                  compare_dest, fname);
374                         fnamecmp = fnamecmpbuf;
375                         fd1 = do_open(fnamecmp, O_RDONLY, 0);
376                 }
377
378                 if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
379                         rsyserr(FERROR, errno, "fstat %s failed",
380                                 full_fname(fnamecmp));
381                         receive_data(f_in,NULL,-1,NULL,file->length);
382                         close(fd1);
383                         continue;
384                 }
385
386                 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
387                         /* this special handling for directories
388                          * wouldn't be necessary if robust_rename()
389                          * and the underlying robust_unlink could cope
390                          * with directories
391                          */
392                         rprintf(FERROR,"recv_files: %s is a directory\n",
393                                 full_fname(fnamecmp));
394                         receive_data(f_in, NULL, -1, NULL, file->length);
395                         close(fd1);
396                         continue;
397                 }
398
399                 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
400                         close(fd1);
401                         fd1 = -1;
402                         mapbuf = NULL;
403                 }
404
405                 if (fd1 != -1 && !preserve_perms) {
406                         /* if the file exists already and we aren't preserving
407                          * permissions then act as though the remote end sent
408                          * us the file permissions we already have */
409                         file->mode = st.st_mode;
410                 }
411
412                 if (fd1 != -1 && st.st_size > 0) {
413                         mapbuf = map_file(fd1,st.st_size);
414                         if (verbose > 2)
415                                 rprintf(FINFO,"recv mapped %s of size %.0f\n",fnamecmp,(double)st.st_size);
416                 } else
417                         mapbuf = NULL;
418
419                 if (!get_tmpname(fnametmp,fname)) {
420                         if (mapbuf) unmap_file(mapbuf);
421                         if (fd1 != -1) close(fd1);
422                         continue;
423                 }
424
425                 strlcpy(template, fnametmp, sizeof template);
426
427                 /* we initially set the perms without the
428                  * setuid/setgid bits to ensure that there is no race
429                  * condition. They are then correctly updated after
430                  * the lchown. Thanks to snabb@epipe.fi for pointing
431                  * this out.  We also set it initially without group
432                  * access because of a similar race condition. */
433                 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
434
435                 /* in most cases parent directories will already exist
436                  * because their information should have been previously
437                  * transferred, but that may not be the case with -R */
438                 if (fd2 == -1 && relative_paths && errno == ENOENT &&
439                     create_directory_path(fnametmp, orig_umask) == 0) {
440                         strlcpy(fnametmp, template, sizeof fnametmp);
441                         fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
442                 }
443                 if (fd2 == -1) {
444                         rsyserr(FERROR, errno, "mkstemp %s failed",
445                                 full_fname(fnametmp));
446                         receive_data(f_in,mapbuf,-1,NULL,file->length);
447                         if (mapbuf) unmap_file(mapbuf);
448                         if (fd1 != -1) close(fd1);
449                         continue;
450                 }
451
452                 cleanup_set(fnametmp, fname, file, mapbuf, fd1, fd2);
453
454                 if (!am_server && verbose) {    /* log transfer */
455                         rprintf(FINFO, "%s\n", fname);
456                 }
457
458                 /* recv file data */
459                 recv_ok = receive_data(f_in,mapbuf,fd2,fname,file->length);
460
461                 log_recv(file, &initial_stats);
462
463                 if (mapbuf) unmap_file(mapbuf);
464                 if (fd1 != -1) {
465                         close(fd1);
466                 }
467                 if (close(fd2) < 0) {
468                         rsyserr(FERROR, errno, "close failed on %s",
469                                 full_fname(fnametmp));
470                         exit_cleanup(RERR_FILEIO);
471                 }
472
473                 if (verbose > 2)
474                         rprintf(FINFO,"renaming %s to %s\n",fnametmp,fname);
475
476                 if (recv_ok || keep_partial)
477                         finish_transfer(fname, fnametmp, file, recv_ok);
478                 else
479                         do_unlink(fnametmp);
480
481                 cleanup_disable();
482
483                 if (!recv_ok) {
484                         if (csum_length == SUM_LENGTH) {
485                                 rprintf(FERROR,"ERROR: file corruption in %s. File changed during transfer?\n",
486                                         full_fname(fname));
487                         } else {
488                                 char buf[4];
489                                 if (verbose > 1)
490                                         rprintf(FINFO,"redoing %s(%d)\n",fname,i);
491                                 SIVAL(buf, 0, i);
492                                 send_msg(MSG_REDO, buf, 4);
493                         }
494                 }
495         }
496
497         if (delete_after && recurse && delete_mode && !local_name
498             && flist->count > 0)
499                 delete_files(flist);
500
501         if (verbose > 2)
502                 rprintf(FINFO,"recv_files finished\n");
503
504         return 0;
505 }