1 /* -*- c-file-style: "linux" -*-
3 Copyright (C) 1996-2000 by Andrew Tridgell
4 Copyright (C) Paul Mackerras 1996
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.
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.
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.
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;
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;
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;
47 extern int ignore_errors;
48 extern int orig_umask;
49 extern int keep_partial;
50 extern int checksum_seed;
53 extern struct exclude_list_struct server_exclude_list;
56 static void delete_one(char *fn, int is_dir)
59 if (robust_unlink(fn) != 0) {
60 rsyserr(FERROR, errno, "delete_one: unlink %s failed",
63 rprintf(FINFO, "deleting %s\n", fn);
65 if (do_rmdir(fn) != 0) {
66 if (errno == ENOTDIR && keep_dirlinks) {
70 if (errno != ENOTEMPTY && errno != EEXIST) {
71 rsyserr(FERROR, errno,
72 "delete_one: rmdir %s failed",
76 rprintf(FINFO, "deleting directory %s\n", fn);
81 static int is_backup_file(char *fn)
83 int k = strlen(fn) - backup_suffix_len;
84 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
88 /* This deletes any files on the receiving side that are not present
89 * on the sending side. */
90 void delete_files(struct file_list *flist)
92 struct file_list *local_file_list;
94 char *argv[1], fbuf[MAXPATHLEN];
95 static int deletion_count;
100 if (io_error && !(lp_ignore_errors(module_id) || ignore_errors)) {
101 rprintf(FINFO,"IO error encountered - skipping file deletion\n");
105 for (j = 0; j < flist->count; j++) {
106 if (!(flist->files[j]->flags & FLAG_TOP_DIR)
107 || !S_ISDIR(flist->files[j]->mode))
110 argv[0] = f_name_to(flist->files[j], fbuf);
112 if (!(local_file_list = send_file_list(-1, 1, argv)))
116 rprintf(FINFO, "deleting in %s\n", fbuf);
118 for (i = local_file_list->count-1; i >= 0; i--) {
119 if (max_delete && deletion_count > max_delete)
121 if (!local_file_list->files[i]->basename)
123 if (flist_find(flist,local_file_list->files[i]) < 0) {
124 char *f = f_name(local_file_list->files[i]);
125 if (make_backups && (backup_dir || !is_backup_file(f))) {
128 rprintf(FINFO, "deleting %s\n", f);
130 int mode = local_file_list->files[i]->mode;
131 delete_one(f, S_ISDIR(mode) != 0);
136 flist_free(local_file_list);
142 * get_tmpname() - create a tmp filename for a given filename
144 * If a tmpdir is defined, use that as the directory to
145 * put it in. Otherwise, the tmp filename is in the same
146 * directory as the given name. Note that there may be no
147 * directory at all in the given name!
149 * The tmp filename is basically the given filename with a
150 * dot prepended, and .XXXXXX appended (for mkstemp() to
151 * put its unique gunk in). Take care to not exceed
152 * either the MAXPATHLEN or NAME_MAX, esp. the last, as
153 * the basename basically becomes 8 chars longer. In that
154 * case, the original name is shortened sufficiently to
157 * Of course, there's no real reason for the tmp name to
158 * look like the original, except to satisfy us humans.
159 * As long as it's unique, rsync will work.
162 static int get_tmpname(char *fnametmp, char *fname)
169 /* Note: this can't overflow, so the return value is safe */
170 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
171 fnametmp[length++] = '/';
172 fnametmp[length] = '\0'; /* always NULL terminated */
175 if ((f = strrchr(fname, '/')) != NULL) {
179 /* copy up to and including the slash */
180 strlcpy(fnametmp, fname, length + 1);
184 fnametmp[length++] = '.';
185 fnametmp[length] = '\0'; /* always NULL terminated */
187 maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8);
190 rprintf(FERROR, "temporary filename too long: %s\n", fname);
195 strlcpy(fnametmp + length, f, maxname);
196 strcat(fnametmp + length, ".XXXXXX");
202 static int receive_data(int f_in,struct map_struct *mapbuf,int fd,char *fname,
206 struct sum_struct sum;
211 static char file_sum1[MD4_SUM_LENGTH];
212 static char file_sum2[MD4_SUM_LENGTH];
215 read_sum_head(f_in, &sum);
217 sum_init(checksum_seed);
219 while ((i = recv_token(f_in, &data)) != 0) {
221 show_progress(offset, total_size);
225 rprintf(FINFO,"data recv %d at %.0f\n",
229 stats.literal_data += i;
230 cleanup_got_literal = 1;
234 if (fd != -1 && write_file(fd,data,i) != i) {
235 rsyserr(FERROR, errno, "write failed on %s",
237 exit_cleanup(RERR_FILEIO);
244 offset2 = i*(OFF_T)sum.blength;
246 if (i == (int)sum.count-1 && sum.remainder != 0)
249 stats.matched_data += len;
252 rprintf(FINFO,"chunk[%d] of size %d at %.0f offset=%.0f\n",
253 i,len,(double)offset2,(double)offset);
256 map = map_ptr(mapbuf,offset2,len);
262 if (!inplace || offset != offset2) {
263 if (fd != -1 && write_file(fd, map, len) != (int)len) {
264 rsyserr(FERROR, errno, "write failed on %s",
266 exit_cleanup(RERR_FILEIO);
269 flush_write_file(fd);
270 if (do_lseek(fd,(OFF_T)len,SEEK_CUR) != offset+len) {
271 rprintf(FERROR, "lseek failed on %s: %s, %lli, %lli, %i\n",
272 full_fname(fname), strerror(errno), do_lseek(fd,0,SEEK_CUR), (offset+len), i);
273 exit_cleanup(RERR_FILEIO);
279 flush_write_file(fd);
281 #ifdef HAVE_FTRUNCATE
283 ftruncate(fd, offset);
287 end_progress(total_size);
289 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
290 rsyserr(FERROR, errno, "write failed on %s",
292 exit_cleanup(RERR_FILEIO);
297 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
299 rprintf(FINFO,"got file_sum\n");
300 if (fd != -1 && memcmp(file_sum1, file_sum2, MD4_SUM_LENGTH) != 0)
306 static void discard_receive_data(int f_in, OFF_T length)
308 receive_data(f_in, NULL, -1, NULL, length);
313 * main routine for receiver process.
315 * Receiver process runs on the same host as the generator process. */
316 int recv_files(int f_in, struct file_list *flist, char *local_name)
320 char *fname, fbuf[MAXPATHLEN];
321 char template[MAXPATHLEN];
322 char fnametmp[MAXPATHLEN];
324 char fnamecmpbuf[MAXPATHLEN];
325 struct map_struct *mapbuf;
326 struct file_struct *file;
327 struct stats initial_stats;
328 int save_make_backups = make_backups;
329 int i, recv_ok, phase = 0;
332 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
334 if (flist->hlink_pool) {
335 pool_destroy(flist->hlink_pool);
336 flist->hlink_pool = NULL;
348 csum_length = SUM_LENGTH;
350 rprintf(FINFO, "recv_files phase=%d\n", phase);
351 send_msg(MSG_DONE, "", 0);
353 make_backups = 0; /* prevents double backup */
357 if (i < 0 || i >= flist->count) {
358 rprintf(FERROR,"Invalid file index %d in recv_files (count=%d)\n",
360 exit_cleanup(RERR_PROTOCOL);
363 file = flist->files[i];
365 stats.current_file_index = i;
366 stats.num_transferred_files++;
367 stats.total_transferred_size += file->length;
368 cleanup_got_literal = 0;
373 fname = f_name_to(file, fbuf);
376 if (!am_server && verbose)
377 rprintf(FINFO, "%s\n", fname);
381 initial_stats = stats;
384 rprintf(FINFO,"recv_files(%s)\n",fname);
388 if (server_exclude_list.head
389 && check_exclude(&server_exclude_list, fname,
390 S_ISDIR(file->mode)) < 0) {
393 "skipping server-excluded update for \"%s\"\n",
396 discard_receive_data(f_in, file->length);
401 fd1 = do_open(fnamecmp, O_RDONLY, 0);
403 if (fd1 == -1 && compare_dest != NULL) {
404 /* try the file at compare_dest instead */
405 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
406 compare_dest, fname);
407 fnamecmp = fnamecmpbuf;
408 fd1 = do_open(fnamecmp, O_RDONLY, 0);
411 if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
412 rsyserr(FERROR, errno, "fstat %s failed",
413 full_fname(fnamecmp));
414 discard_receive_data(f_in, file->length);
419 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
420 /* this special handling for directories
421 * wouldn't be necessary if robust_rename()
422 * and the underlying robust_unlink could cope
425 rprintf(FERROR,"recv_files: %s is a directory\n",
426 full_fname(fnamecmp));
427 discard_receive_data(f_in, file->length);
432 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
438 if (fd1 != -1 && !preserve_perms) {
439 /* if the file exists already and we aren't preserving
440 * permissions then act as though the remote end sent
441 * us the file permissions we already have */
442 file->mode = st.st_mode;
445 if (fd1 != -1 && st.st_size > 0) {
446 mapbuf = map_file(fd1,st.st_size);
448 rprintf(FINFO, "recv mapped %s of size %.0f\n",
449 fnamecmp, (double)st.st_size);
454 /* We now check to see if we are writing file "inplace" */
456 fd2 = do_open(fnamecmp, O_WRONLY|O_CREAT, 0);
458 rsyserr(FERROR, errno, "open %s failed",
459 full_fname(fnamecmp));
460 discard_receive_data(f_in, file->length);
468 if (!get_tmpname(fnametmp,fname)) {
469 discard_receive_data(f_in, file->length);
477 strlcpy(template, fnametmp, sizeof template);
479 /* we initially set the perms without the
480 * setuid/setgid bits to ensure that there is no race
481 * condition. They are then correctly updated after
482 * the lchown. Thanks to snabb@epipe.fi for pointing
483 * this out. We also set it initially without group
484 * access because of a similar race condition. */
485 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
487 /* in most cases parent directories will already exist
488 * because their information should have been previously
489 * transferred, but that may not be the case with -R */
490 if (fd2 == -1 && relative_paths && errno == ENOENT
491 && create_directory_path(fnametmp, orig_umask) == 0) {
492 strlcpy(fnametmp, template, sizeof fnametmp);
493 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
496 rsyserr(FERROR, errno, "mkstemp %s failed",
497 full_fname(fnametmp));
498 discard_receive_data(f_in, file->length);
506 cleanup_set(fnametmp, fname, file, mapbuf, fd1, fd2);
509 if (!am_server && verbose)
510 rprintf(FINFO, "%s\n", fname);
513 recv_ok = receive_data(f_in,mapbuf,fd2,fname,file->length);
515 log_recv(file, &initial_stats);
521 if (close(fd2) < 0) {
522 rsyserr(FERROR, errno, "close failed on %s",
523 full_fname(fnametmp));
524 exit_cleanup(RERR_FILEIO);
527 if (recv_ok || keep_partial)
528 finish_transfer(fname, fnametmp, file, recv_ok);
535 if (csum_length == SUM_LENGTH) {
536 rprintf(FERROR,"ERROR: file corruption in %s. File changed during transfer?\n",
541 rprintf(FINFO,"redoing %s(%d)\n",fname,i);
543 send_msg(MSG_REDO, buf, 4);
547 make_backups = save_make_backups;
549 if (delete_after && recurse && delete_mode && !local_name
554 rprintf(FINFO,"recv_files finished\n");