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
;
49 static void delete_one(char *fn
, int is_dir
)
52 if (robust_unlink(fn
) != 0) {
53 rprintf(FERROR
, "delete_one: unlink %s failed: %s\n",
54 full_fname(fn
), strerror(errno
));
56 rprintf(FINFO
, "deleting %s\n", fn
);
59 if (do_rmdir(fn
) != 0) {
60 if (errno
!= ENOTEMPTY
&& errno
!= EEXIST
) {
61 rprintf(FERROR
, "delete_one: rmdir %s failed: %s\n",
62 full_fname(fn
), strerror(errno
));
65 rprintf(FINFO
, "deleting directory %s\n", fn
);
71 static int is_backup_file(char *fn
)
73 int k
= strlen(fn
) - backup_suffix_len
;
74 return k
> 0 && strcmp(fn
+k
, backup_suffix
) == 0;
78 /* This deletes any files on the receiving side that are not present
79 * on the sending side. */
80 void delete_files(struct file_list
*flist
)
82 struct file_list
*local_file_list
;
84 char *argv
[1], fbuf
[MAXPATHLEN
];
85 static int deletion_count
;
90 if (io_error
&& !(lp_ignore_errors(module_id
) || ignore_errors
)) {
91 rprintf(FINFO
,"IO error encountered - skipping file deletion\n");
95 for (j
= 0; j
< flist
->count
; j
++) {
96 if (!(flist
->files
[j
]->flags
& FLAG_TOP_DIR
)
97 || !S_ISDIR(flist
->files
[j
]->mode
))
100 argv
[0] = f_name_to(flist
->files
[j
], fbuf
);
102 if (!(local_file_list
= send_file_list(-1, 1, argv
)))
106 rprintf(FINFO
, "deleting in %s\n", fbuf
);
108 for (i
= local_file_list
->count
-1; i
>= 0; i
--) {
109 if (max_delete
&& deletion_count
> max_delete
)
111 if (!local_file_list
->files
[i
]->basename
)
113 if (flist_find(flist
,local_file_list
->files
[i
]) < 0) {
114 char *f
= f_name(local_file_list
->files
[i
]);
115 if (make_backups
&& (backup_dir
|| !is_backup_file(f
))) {
116 (void) make_backup(f
);
118 rprintf(FINFO
, "deleting %s\n", f
);
120 int mode
= local_file_list
->files
[i
]->mode
;
121 delete_one(f
, S_ISDIR(mode
) != 0);
126 flist_free(local_file_list
);
132 * get_tmpname() - create a tmp filename for a given filename
134 * If a tmpdir is defined, use that as the directory to
135 * put it in. Otherwise, the tmp filename is in the same
136 * directory as the given name. Note that there may be no
137 * directory at all in the given name!
139 * The tmp filename is basically the given filename with a
140 * dot prepended, and .XXXXXX appended (for mkstemp() to
141 * put its unique gunk in). Take care to not exceed
142 * either the MAXPATHLEN or NAME_MAX, esp. the last, as
143 * the basename basically becomes 8 chars longer. In that
144 * case, the original name is shortened sufficiently to
147 * Of course, there's no real reason for the tmp name to
148 * look like the original, except to satisfy us humans.
149 * As long as it's unique, rsync will work.
152 static int get_tmpname(char *fnametmp
, char *fname
)
159 /* Note: this can't overflow, so the return value is safe */
160 length
= strlcpy(fnametmp
, tmpdir
, MAXPATHLEN
- 2);
161 fnametmp
[length
++] = '/';
162 fnametmp
[length
] = '\0'; /* always NULL terminated */
165 if ((f
= strrchr(fname
, '/')) != NULL
) {
169 /* copy up to and including the slash */
170 strlcpy(fnametmp
, fname
, length
+ 1);
174 fnametmp
[length
++] = '.';
175 fnametmp
[length
] = '\0'; /* always NULL terminated */
177 maxname
= MIN(MAXPATHLEN
- 7 - length
, NAME_MAX
- 8);
180 rprintf(FERROR
, "temporary filename too long: %s\n", fname
);
185 strlcpy(fnametmp
+ length
, f
, maxname
);
186 strcat(fnametmp
+ length
, ".XXXXXX");
192 static int receive_data(int f_in
,struct map_struct
*mapbuf
,int fd
,char *fname
,
196 struct sum_struct sum
;
201 static char file_sum1
[MD4_SUM_LENGTH
];
202 static char file_sum2
[MD4_SUM_LENGTH
];
205 read_sum_head(f_in
, &sum
);
209 while ((i
= recv_token(f_in
, &data
)) != 0) {
211 show_progress(offset
, total_size
);
215 rprintf(FINFO
,"data recv %d at %.0f\n",
219 stats
.literal_data
+= i
;
220 cleanup_got_literal
= 1;
224 if (fd
!= -1 && write_file(fd
,data
,i
) != i
) {
225 rprintf(FERROR
, "write failed on %s: %s\n",
226 full_fname(fname
), strerror(errno
));
227 exit_cleanup(RERR_FILEIO
);
234 offset2
= i
*(OFF_T
)sum
.blength
;
236 if (i
== (int) sum
.count
-1 && sum
.remainder
!= 0)
239 stats
.matched_data
+= len
;
242 rprintf(FINFO
,"chunk[%d] of size %d at %.0f offset=%.0f\n",
243 i
,len
,(double)offset2
,(double)offset
);
246 map
= map_ptr(mapbuf
,offset2
,len
);
252 if (fd
!= -1 && write_file(fd
,map
,len
) != (int) len
) {
253 rprintf(FERROR
, "write failed on %s: %s\n",
254 full_fname(fname
), strerror(errno
));
255 exit_cleanup(RERR_FILEIO
);
260 flush_write_file(fd
);
263 end_progress(total_size
);
265 if (fd
!= -1 && offset
> 0 && sparse_end(fd
) != 0) {
266 rprintf(FERROR
, "write failed on %s: %s\n",
267 full_fname(fname
), strerror(errno
));
268 exit_cleanup(RERR_FILEIO
);
273 read_buf(f_in
,file_sum2
,MD4_SUM_LENGTH
);
275 rprintf(FINFO
,"got file_sum\n");
277 if (fd
!= -1 && memcmp(file_sum1
,file_sum2
,MD4_SUM_LENGTH
) != 0) {
285 * main routine for receiver process.
287 * Receiver process runs on the same host as the generator process. */
288 int recv_files(int f_in
,struct file_list
*flist
,char *local_name
)
292 char *fname
, fbuf
[MAXPATHLEN
];
293 char template[MAXPATHLEN
];
294 char fnametmp
[MAXPATHLEN
];
296 char fnamecmpbuf
[MAXPATHLEN
];
297 struct map_struct
*mapbuf
;
299 struct file_struct
*file
;
302 struct stats initial_stats
;
305 rprintf(FINFO
,"recv_files(%d) starting\n",flist
->count
);
308 if (flist
->hlink_pool
) {
309 pool_destroy(flist
->hlink_pool
);
310 flist
->hlink_pool
= NULL
;
320 csum_length
= SUM_LENGTH
;
322 rprintf(FINFO
,"recv_files phase=%d\n",phase
);
323 send_msg(MSG_DONE
, "", 0);
329 if (i
< 0 || i
>= flist
->count
) {
330 rprintf(FERROR
,"Invalid file index %d in recv_files (count=%d)\n",
332 exit_cleanup(RERR_PROTOCOL
);
335 file
= flist
->files
[i
];
337 stats
.current_file_index
= i
;
338 stats
.num_transferred_files
++;
339 stats
.total_transferred_size
+= file
->length
;
340 cleanup_got_literal
= 0;
345 fname
= f_name_to(file
, fbuf
);
348 if (!am_server
&& verbose
) { /* log transfer */
349 rprintf(FINFO
, "%s\n", fname
);
354 initial_stats
= stats
;
357 rprintf(FINFO
,"recv_files(%s)\n",fname
);
362 fd1
= do_open(fnamecmp
, O_RDONLY
, 0);
364 if (fd1
== -1 && compare_dest
!= NULL
) {
365 /* try the file at compare_dest instead */
366 pathjoin(fnamecmpbuf
, sizeof fnamecmpbuf
,
367 compare_dest
, fname
);
368 fnamecmp
= fnamecmpbuf
;
369 fd1
= do_open(fnamecmp
, O_RDONLY
, 0);
372 if (fd1
!= -1 && do_fstat(fd1
,&st
) != 0) {
373 rprintf(FERROR
, "fstat %s failed: %s\n",
374 full_fname(fnamecmp
), strerror(errno
));
375 receive_data(f_in
,NULL
,-1,NULL
,file
->length
);
380 if (fd1
!= -1 && S_ISDIR(st
.st_mode
) && fnamecmp
== fname
) {
381 /* this special handling for directories
382 * wouldn't be necessary if robust_rename()
383 * and the underlying robust_unlink could cope
386 rprintf(FERROR
,"recv_files: %s is a directory\n",
387 full_fname(fnamecmp
));
388 receive_data(f_in
, NULL
, -1, NULL
, file
->length
);
393 if (fd1
!= -1 && !S_ISREG(st
.st_mode
)) {
399 if (fd1
!= -1 && !preserve_perms
) {
400 /* if the file exists already and we aren't preserving
401 * permissions then act as though the remote end sent
402 * us the file permissions we already have */
403 file
->mode
= st
.st_mode
;
406 if (fd1
!= -1 && st
.st_size
> 0) {
407 mapbuf
= map_file(fd1
,st
.st_size
);
409 rprintf(FINFO
,"recv mapped %s of size %.0f\n",fnamecmp
,(double)st
.st_size
);
413 if (!get_tmpname(fnametmp
,fname
)) {
414 if (mapbuf
) unmap_file(mapbuf
);
415 if (fd1
!= -1) close(fd1
);
419 strlcpy(template, fnametmp
, sizeof template);
421 /* we initially set the perms without the
422 * setuid/setgid bits to ensure that there is no race
423 * condition. They are then correctly updated after
424 * the lchown. Thanks to snabb@epipe.fi for pointing
425 * this out. We also set it initially without group
426 * access because of a similar race condition. */
427 fd2
= do_mkstemp(fnametmp
, file
->mode
& INITACCESSPERMS
);
429 /* in most cases parent directories will already exist
430 * because their information should have been previously
431 * transferred, but that may not be the case with -R */
432 if (fd2
== -1 && relative_paths
&& errno
== ENOENT
&&
433 create_directory_path(fnametmp
, orig_umask
) == 0) {
434 strlcpy(fnametmp
, template, sizeof fnametmp
);
435 fd2
= do_mkstemp(fnametmp
, file
->mode
& INITACCESSPERMS
);
438 rprintf(FERROR
, "mkstemp %s failed: %s\n",
439 full_fname(fnametmp
), strerror(errno
));
440 receive_data(f_in
,mapbuf
,-1,NULL
,file
->length
);
441 if (mapbuf
) unmap_file(mapbuf
);
442 if (fd1
!= -1) close(fd1
);
446 cleanup_set(fnametmp
, fname
, file
, mapbuf
, fd1
, fd2
);
448 if (!am_server
&& verbose
) { /* log transfer */
449 rprintf(FINFO
, "%s\n", fname
);
453 recv_ok
= receive_data(f_in
,mapbuf
,fd2
,fname
,file
->length
);
455 log_recv(file
, &initial_stats
);
457 if (mapbuf
) unmap_file(mapbuf
);
464 rprintf(FINFO
,"renaming %s to %s\n",fnametmp
,fname
);
466 finish_transfer(fname
, fnametmp
, file
);
471 if (csum_length
== SUM_LENGTH
) {
472 rprintf(FERROR
,"ERROR: file corruption in %s. File changed during transfer?\n",
477 rprintf(FINFO
,"redoing %s(%d)\n",fname
,i
);
479 send_msg(MSG_REDO
, buf
, 4);
484 if (delete_after
&& recurse
&& delete_mode
&& !local_name
489 rprintf(FINFO
,"recv_files finished\n");