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;
52 static void delete_one(char *fn, int is_dir)
55 if (robust_unlink(fn) != 0) {
56 rsyserr(FERROR, errno, "delete_one: unlink %s failed",
59 rprintf(FINFO, "deleting %s\n", fn);
62 if (do_rmdir(fn) != 0) {
63 if (errno == ENOTDIR && keep_dirlinks) {
67 if (errno != ENOTEMPTY && errno != EEXIST) {
68 rsyserr(FERROR, errno,
69 "delete_one: rmdir %s failed",
73 rprintf(FINFO, "deleting directory %s\n", fn);
79 static int is_backup_file(char *fn)
81 int k = strlen(fn) - backup_suffix_len;
82 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
86 /* This deletes any files on the receiving side that are not present
87 * on the sending side. */
88 void delete_files(struct file_list *flist)
90 struct file_list *local_file_list;
92 char *argv[1], fbuf[MAXPATHLEN];
93 static int deletion_count;
98 if (io_error && !(lp_ignore_errors(module_id) || ignore_errors)) {
99 rprintf(FINFO,"IO error encountered - skipping file deletion\n");
103 for (j = 0; j < flist->count; j++) {
104 if (!(flist->files[j]->flags & FLAG_TOP_DIR)
105 || !S_ISDIR(flist->files[j]->mode))
108 argv[0] = f_name_to(flist->files[j], fbuf);
110 if (!(local_file_list = send_file_list(-1, 1, argv)))
114 rprintf(FINFO, "deleting in %s\n", fbuf);
116 for (i = local_file_list->count-1; i >= 0; i--) {
117 if (max_delete && deletion_count > max_delete)
119 if (!local_file_list->files[i]->basename)
121 if (flist_find(flist,local_file_list->files[i]) < 0) {
122 char *f = f_name(local_file_list->files[i]);
123 if (make_backups && (backup_dir || !is_backup_file(f))) {
124 (void) make_backup(f);
126 rprintf(FINFO, "deleting %s\n", f);
128 int mode = local_file_list->files[i]->mode;
129 delete_one(f, S_ISDIR(mode) != 0);
134 flist_free(local_file_list);
140 * get_tmpname() - create a tmp filename for a given filename
142 * If a tmpdir is defined, use that as the directory to
143 * put it in. Otherwise, the tmp filename is in the same
144 * directory as the given name. Note that there may be no
145 * directory at all in the given name!
147 * The tmp filename is basically the given filename with a
148 * dot prepended, and .XXXXXX appended (for mkstemp() to
149 * put its unique gunk in). Take care to not exceed
150 * either the MAXPATHLEN or NAME_MAX, esp. the last, as
151 * the basename basically becomes 8 chars longer. In that
152 * case, the original name is shortened sufficiently to
155 * Of course, there's no real reason for the tmp name to
156 * look like the original, except to satisfy us humans.
157 * As long as it's unique, rsync will work.
160 static int get_tmpname(char *fnametmp, char *fname)
167 /* Note: this can't overflow, so the return value is safe */
168 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
169 fnametmp[length++] = '/';
170 fnametmp[length] = '\0'; /* always NULL terminated */
173 if ((f = strrchr(fname, '/')) != NULL) {
177 /* copy up to and including the slash */
178 strlcpy(fnametmp, fname, length + 1);
182 fnametmp[length++] = '.';
183 fnametmp[length] = '\0'; /* always NULL terminated */
185 maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8);
188 rprintf(FERROR, "temporary filename too long: %s\n", fname);
193 strlcpy(fnametmp + length, f, maxname);
194 strcat(fnametmp + length, ".XXXXXX");
200 static int receive_data(int f_in,struct map_struct *mapbuf,int fd,char *fname,
204 struct sum_struct sum;
209 static char file_sum1[MD4_SUM_LENGTH];
210 static char file_sum2[MD4_SUM_LENGTH];
213 read_sum_head(f_in, &sum);
215 sum_init(checksum_seed);
217 while ((i = recv_token(f_in, &data)) != 0) {
219 show_progress(offset, total_size);
223 rprintf(FINFO,"data recv %d at %.0f\n",
227 stats.literal_data += i;
228 cleanup_got_literal = 1;
232 if (fd != -1 && write_file(fd,data,i) != i) {
233 rsyserr(FERROR, errno, "write failed on %s",
235 exit_cleanup(RERR_FILEIO);
242 offset2 = i*(OFF_T)sum.blength;
244 if (i == (int) sum.count-1 && sum.remainder != 0)
247 stats.matched_data += len;
250 rprintf(FINFO,"chunk[%d] of size %d at %.0f offset=%.0f\n",
251 i,len,(double)offset2,(double)offset);
254 map = map_ptr(mapbuf,offset2,len);
260 if (fd != -1 && write_file(fd,map,len) != (int) len) {
261 rsyserr(FERROR, errno, "write failed on %s",
263 exit_cleanup(RERR_FILEIO);
268 flush_write_file(fd);
271 end_progress(total_size);
273 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
274 rsyserr(FERROR, errno, "write failed on %s",
276 exit_cleanup(RERR_FILEIO);
281 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
283 rprintf(FINFO,"got file_sum\n");
285 if (fd != -1 && memcmp(file_sum1,file_sum2,MD4_SUM_LENGTH) != 0) {
293 * main routine for receiver process.
295 * Receiver process runs on the same host as the generator process. */
296 int recv_files(int f_in,struct file_list *flist,char *local_name)
300 char *fname, fbuf[MAXPATHLEN];
301 char template[MAXPATHLEN];
302 char fnametmp[MAXPATHLEN];
304 char fnamecmpbuf[MAXPATHLEN];
305 struct map_struct *mapbuf;
306 struct file_struct *file;
307 struct stats initial_stats;
308 int save_make_backups = make_backups;
309 int i, recv_ok, phase = 0;
312 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
315 if (flist->hlink_pool) {
316 pool_destroy(flist->hlink_pool);
317 flist->hlink_pool = NULL;
328 csum_length = SUM_LENGTH;
330 rprintf(FINFO, "recv_files phase=%d\n", phase);
331 send_msg(MSG_DONE, "", 0);
333 make_backups = 0; /* prevents double backup */
337 if (i < 0 || i >= flist->count) {
338 rprintf(FERROR,"Invalid file index %d in recv_files (count=%d)\n",
340 exit_cleanup(RERR_PROTOCOL);
343 file = flist->files[i];
345 stats.current_file_index = i;
346 stats.num_transferred_files++;
347 stats.total_transferred_size += file->length;
348 cleanup_got_literal = 0;
353 fname = f_name_to(file, fbuf);
356 if (!am_server && verbose) { /* log transfer */
357 rprintf(FINFO, "%s\n", fname);
362 initial_stats = stats;
365 rprintf(FINFO,"recv_files(%s)\n",fname);
370 fd1 = do_open(fnamecmp, O_RDONLY, 0);
372 if (fd1 == -1 && compare_dest != NULL) {
373 /* try the file at compare_dest instead */
374 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
375 compare_dest, fname);
376 fnamecmp = fnamecmpbuf;
377 fd1 = do_open(fnamecmp, O_RDONLY, 0);
380 if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
381 rsyserr(FERROR, errno, "fstat %s failed",
382 full_fname(fnamecmp));
383 receive_data(f_in,NULL,-1,NULL,file->length);
388 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
389 /* this special handling for directories
390 * wouldn't be necessary if robust_rename()
391 * and the underlying robust_unlink could cope
394 rprintf(FERROR,"recv_files: %s is a directory\n",
395 full_fname(fnamecmp));
396 receive_data(f_in, NULL, -1, NULL, file->length);
401 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
407 if (fd1 != -1 && !preserve_perms) {
408 /* if the file exists already and we aren't preserving
409 * permissions then act as though the remote end sent
410 * us the file permissions we already have */
411 file->mode = st.st_mode;
414 if (fd1 != -1 && st.st_size > 0) {
415 mapbuf = map_file(fd1,st.st_size);
417 rprintf(FINFO,"recv mapped %s of size %.0f\n",fnamecmp,(double)st.st_size);
421 if (!get_tmpname(fnametmp,fname)) {
422 if (mapbuf) unmap_file(mapbuf);
423 if (fd1 != -1) close(fd1);
427 strlcpy(template, fnametmp, sizeof template);
429 /* we initially set the perms without the
430 * setuid/setgid bits to ensure that there is no race
431 * condition. They are then correctly updated after
432 * the lchown. Thanks to snabb@epipe.fi for pointing
433 * this out. We also set it initially without group
434 * access because of a similar race condition. */
435 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
437 /* in most cases parent directories will already exist
438 * because their information should have been previously
439 * transferred, but that may not be the case with -R */
440 if (fd2 == -1 && relative_paths && errno == ENOENT &&
441 create_directory_path(fnametmp, orig_umask) == 0) {
442 strlcpy(fnametmp, template, sizeof fnametmp);
443 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
446 rsyserr(FERROR, errno, "mkstemp %s failed",
447 full_fname(fnametmp));
448 receive_data(f_in,mapbuf,-1,NULL,file->length);
449 if (mapbuf) unmap_file(mapbuf);
450 if (fd1 != -1) close(fd1);
454 cleanup_set(fnametmp, fname, file, mapbuf, fd1, fd2);
456 if (!am_server && verbose) { /* log transfer */
457 rprintf(FINFO, "%s\n", fname);
461 recv_ok = receive_data(f_in,mapbuf,fd2,fname,file->length);
463 log_recv(file, &initial_stats);
465 if (mapbuf) unmap_file(mapbuf);
469 if (close(fd2) < 0) {
470 rsyserr(FERROR, errno, "close failed on %s",
471 full_fname(fnametmp));
472 exit_cleanup(RERR_FILEIO);
476 rprintf(FINFO,"renaming %s to %s\n",fnametmp,fname);
478 if (recv_ok || keep_partial)
479 finish_transfer(fname, fnametmp, file, recv_ok);
486 if (csum_length == SUM_LENGTH) {
487 rprintf(FERROR,"ERROR: file corruption in %s. File changed during transfer?\n",
492 rprintf(FINFO,"redoing %s(%d)\n",fname,i);
494 send_msg(MSG_REDO, buf, 4);
498 make_backups = save_make_backups;
500 if (delete_after && recurse && delete_mode && !local_name
505 rprintf(FINFO,"recv_files finished\n");