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 preserve_hard_links;
34 extern int preserve_perms;
35 extern int cvs_exclude;
38 extern char *compare_dest;
39 extern int make_backups;
40 extern int do_progress;
41 extern char *backup_dir;
42 extern char *backup_suffix;
43 extern int backup_suffix_len;
44 extern int cleanup_got_literal;
46 extern int ignore_errors;
47 extern int orig_umask;
48 extern int keep_partial;
49 extern int checksum_seed;
51 static void delete_one(char *fn, int is_dir)
54 if (robust_unlink(fn) != 0) {
55 rsyserr(FERROR, errno, "delete_one: unlink %s failed",
58 rprintf(FINFO, "deleting %s\n", fn);
61 if (do_rmdir(fn) != 0) {
62 if (errno != ENOTEMPTY && errno != EEXIST) {
63 rsyserr(FERROR, errno,
64 "delete_one: rmdir %s failed",
68 rprintf(FINFO, "deleting directory %s\n", fn);
74 static int is_backup_file(char *fn)
76 int k = strlen(fn) - backup_suffix_len;
77 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
81 /* This deletes any files on the receiving side that are not present
82 * on the sending side. */
83 void delete_files(struct file_list *flist)
85 struct file_list *local_file_list;
87 char *argv[1], fbuf[MAXPATHLEN];
88 static int deletion_count;
93 if (io_error && !(lp_ignore_errors(module_id) || ignore_errors)) {
94 rprintf(FINFO,"IO error encountered - skipping file deletion\n");
98 for (j = 0; j < flist->count; j++) {
99 if (!(flist->files[j]->flags & FLAG_TOP_DIR)
100 || !S_ISDIR(flist->files[j]->mode))
103 argv[0] = f_name_to(flist->files[j], fbuf);
105 if (!(local_file_list = send_file_list(-1, 1, argv)))
109 rprintf(FINFO, "deleting in %s\n", fbuf);
111 for (i = local_file_list->count-1; i >= 0; i--) {
112 if (max_delete && deletion_count > max_delete)
114 if (!local_file_list->files[i]->basename)
116 if (flist_find(flist,local_file_list->files[i]) < 0) {
117 char *f = f_name(local_file_list->files[i]);
118 if (make_backups && (backup_dir || !is_backup_file(f))) {
119 (void) make_backup(f);
121 rprintf(FINFO, "deleting %s\n", f);
123 int mode = local_file_list->files[i]->mode;
124 delete_one(f, S_ISDIR(mode) != 0);
129 flist_free(local_file_list);
135 * get_tmpname() - create a tmp filename for a given filename
137 * If a tmpdir is defined, use that as the directory to
138 * put it in. Otherwise, the tmp filename is in the same
139 * directory as the given name. Note that there may be no
140 * directory at all in the given name!
142 * The tmp filename is basically the given filename with a
143 * dot prepended, and .XXXXXX appended (for mkstemp() to
144 * put its unique gunk in). Take care to not exceed
145 * either the MAXPATHLEN or NAME_MAX, esp. the last, as
146 * the basename basically becomes 8 chars longer. In that
147 * case, the original name is shortened sufficiently to
150 * Of course, there's no real reason for the tmp name to
151 * look like the original, except to satisfy us humans.
152 * As long as it's unique, rsync will work.
155 static int get_tmpname(char *fnametmp, char *fname)
162 /* Note: this can't overflow, so the return value is safe */
163 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
164 fnametmp[length++] = '/';
165 fnametmp[length] = '\0'; /* always NULL terminated */
168 if ((f = strrchr(fname, '/')) != NULL) {
172 /* copy up to and including the slash */
173 strlcpy(fnametmp, fname, length + 1);
177 fnametmp[length++] = '.';
178 fnametmp[length] = '\0'; /* always NULL terminated */
180 maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8);
183 rprintf(FERROR, "temporary filename too long: %s\n", fname);
188 strlcpy(fnametmp + length, f, maxname);
189 strcat(fnametmp + length, ".XXXXXX");
195 static int receive_data(int f_in,struct map_struct *mapbuf,int fd,char *fname,
199 struct sum_struct sum;
204 static char file_sum1[MD4_SUM_LENGTH];
205 static char file_sum2[MD4_SUM_LENGTH];
208 read_sum_head(f_in, &sum);
210 sum_init(checksum_seed);
212 while ((i = recv_token(f_in, &data)) != 0) {
214 show_progress(offset, total_size);
218 rprintf(FINFO,"data recv %d at %.0f\n",
222 stats.literal_data += i;
223 cleanup_got_literal = 1;
227 if (fd != -1 && write_file(fd,data,i) != i) {
228 rsyserr(FERROR, errno, "write failed on %s",
230 exit_cleanup(RERR_FILEIO);
237 offset2 = i*(OFF_T)sum.blength;
239 if (i == (int) sum.count-1 && sum.remainder != 0)
242 stats.matched_data += len;
245 rprintf(FINFO,"chunk[%d] of size %d at %.0f offset=%.0f\n",
246 i,len,(double)offset2,(double)offset);
249 map = map_ptr(mapbuf,offset2,len);
255 if (fd != -1 && write_file(fd,map,len) != (int) len) {
256 rsyserr(FERROR, errno, "write failed on %s",
258 exit_cleanup(RERR_FILEIO);
263 flush_write_file(fd);
266 end_progress(total_size);
268 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
269 rsyserr(FERROR, errno, "write failed on %s",
271 exit_cleanup(RERR_FILEIO);
276 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
278 rprintf(FINFO,"got file_sum\n");
280 if (fd != -1 && memcmp(file_sum1,file_sum2,MD4_SUM_LENGTH) != 0) {
288 * main routine for receiver process.
290 * Receiver process runs on the same host as the generator process. */
291 int recv_files(int f_in,struct file_list *flist,char *local_name)
295 char *fname, fbuf[MAXPATHLEN];
296 char template[MAXPATHLEN];
297 char fnametmp[MAXPATHLEN];
299 char fnamecmpbuf[MAXPATHLEN];
300 struct map_struct *mapbuf;
302 struct file_struct *file;
305 struct stats initial_stats;
308 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
311 if (flist->hlink_pool) {
312 pool_destroy(flist->hlink_pool);
313 flist->hlink_pool = NULL;
323 csum_length = SUM_LENGTH;
325 rprintf(FINFO,"recv_files phase=%d\n",phase);
326 send_msg(MSG_DONE, "", 0);
332 if (i < 0 || i >= flist->count) {
333 rprintf(FERROR,"Invalid file index %d in recv_files (count=%d)\n",
335 exit_cleanup(RERR_PROTOCOL);
338 file = flist->files[i];
340 stats.current_file_index = i;
341 stats.num_transferred_files++;
342 stats.total_transferred_size += file->length;
343 cleanup_got_literal = 0;
348 fname = f_name_to(file, fbuf);
351 if (!am_server && verbose) { /* log transfer */
352 rprintf(FINFO, "%s\n", fname);
357 initial_stats = stats;
360 rprintf(FINFO,"recv_files(%s)\n",fname);
365 fd1 = do_open(fnamecmp, O_RDONLY, 0);
367 if (fd1 == -1 && compare_dest != NULL) {
368 /* try the file at compare_dest instead */
369 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
370 compare_dest, fname);
371 fnamecmp = fnamecmpbuf;
372 fd1 = do_open(fnamecmp, O_RDONLY, 0);
375 if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
376 rsyserr(FERROR, errno, "fstat %s failed",
377 full_fname(fnamecmp));
378 receive_data(f_in,NULL,-1,NULL,file->length);
383 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
384 /* this special handling for directories
385 * wouldn't be necessary if robust_rename()
386 * and the underlying robust_unlink could cope
389 rprintf(FERROR,"recv_files: %s is a directory\n",
390 full_fname(fnamecmp));
391 receive_data(f_in, NULL, -1, NULL, file->length);
396 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
402 if (fd1 != -1 && !preserve_perms) {
403 /* if the file exists already and we aren't preserving
404 * permissions then act as though the remote end sent
405 * us the file permissions we already have */
406 file->mode = st.st_mode;
409 if (fd1 != -1 && st.st_size > 0) {
410 mapbuf = map_file(fd1,st.st_size);
412 rprintf(FINFO,"recv mapped %s of size %.0f\n",fnamecmp,(double)st.st_size);
416 if (!get_tmpname(fnametmp,fname)) {
417 if (mapbuf) unmap_file(mapbuf);
418 if (fd1 != -1) close(fd1);
422 strlcpy(template, fnametmp, sizeof template);
424 /* we initially set the perms without the
425 * setuid/setgid bits to ensure that there is no race
426 * condition. They are then correctly updated after
427 * the lchown. Thanks to snabb@epipe.fi for pointing
428 * this out. We also set it initially without group
429 * access because of a similar race condition. */
430 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
432 /* in most cases parent directories will already exist
433 * because their information should have been previously
434 * transferred, but that may not be the case with -R */
435 if (fd2 == -1 && relative_paths && errno == ENOENT &&
436 create_directory_path(fnametmp, orig_umask) == 0) {
437 strlcpy(fnametmp, template, sizeof fnametmp);
438 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
441 rsyserr(FERROR, errno, "mkstemp %s failed",
442 full_fname(fnametmp));
443 receive_data(f_in,mapbuf,-1,NULL,file->length);
444 if (mapbuf) unmap_file(mapbuf);
445 if (fd1 != -1) close(fd1);
449 cleanup_set(fnametmp, fname, file, mapbuf, fd1, fd2);
451 if (!am_server && verbose) { /* log transfer */
452 rprintf(FINFO, "%s\n", fname);
456 recv_ok = receive_data(f_in,mapbuf,fd2,fname,file->length);
458 log_recv(file, &initial_stats);
460 if (mapbuf) unmap_file(mapbuf);
464 if (close(fd2) < 0) {
465 rsyserr(FERROR, errno, "close failed on %s",
466 full_fname(fnametmp));
467 exit_cleanup(RERR_FILEIO);
471 rprintf(FINFO,"renaming %s to %s\n",fnametmp,fname);
473 if (recv_ok || keep_partial)
474 finish_transfer(fname, fnametmp, file, recv_ok);
481 if (csum_length == SUM_LENGTH) {
482 rprintf(FERROR,"ERROR: file corruption in %s. File changed during transfer?\n",
487 rprintf(FINFO,"redoing %s(%d)\n",fname,i);
489 send_msg(MSG_REDO, buf, 4);
494 if (delete_after && recurse && delete_mode && !local_name
499 rprintf(FINFO,"recv_files finished\n");