blob: 6595c496d47408aeba239451ed5b3d9238386636 [file] [log] [blame]
Martin Poole327ace2000-11-08 09:32:11 +00001/* -*- c-file-style: "linux" -*-
Wayne Davison17fadf72003-12-15 08:14:27 +00002
Martin Poole327ace2000-11-08 09:32:11 +00003 Copyright (C) 1996-2000 by Andrew Tridgell
Andrew Tridgell2f03f951998-07-25 02:25:22 +00004 Copyright (C) Paul Mackerras 1996
Wayne Davison17fadf72003-12-15 08:14:27 +00005
Andrew Tridgell2f03f951998-07-25 02:25:22 +00006 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.
Wayne Davison17fadf72003-12-15 08:14:27 +000010
Andrew Tridgell2f03f951998-07-25 02:25:22 +000011 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.
Wayne Davison17fadf72003-12-15 08:14:27 +000015
Andrew Tridgell2f03f951998-07-25 02:25:22 +000016 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
23extern int verbose;
24extern int recurse;
25extern int delete_mode;
Wayne Davison92b9eb92004-03-23 16:50:40 +000026extern int delete_after;
27extern int max_delete;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000028extern int csum_length;
29extern struct stats stats;
30extern int dry_run;
31extern int am_server;
32extern int relative_paths;
Wayne Davison566fce32004-06-11 07:40:48 +000033extern int keep_dirlinks;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000034extern int preserve_hard_links;
Wayne Davison92b9eb92004-03-23 16:50:40 +000035extern int preserve_perms;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000036extern int cvs_exclude;
37extern int io_error;
38extern char *tmpdir;
David Dykstra375a4551998-10-26 21:42:38 +000039extern char *compare_dest;
David Dykstra53dd3131998-11-24 19:01:24 +000040extern int make_backups;
Wayne Davison16417f82003-07-08 16:49:10 +000041extern int do_progress;
Wayne Davisond74a2e32003-08-01 07:58:47 +000042extern char *backup_dir;
David Dykstra53dd3131998-11-24 19:01:24 +000043extern char *backup_suffix;
Wayne Davisond74a2e32003-08-01 07:58:47 +000044extern int backup_suffix_len;
Wayne Davison925c5172004-01-02 08:38:35 +000045extern int cleanup_got_literal;
Wayne Davison92b9eb92004-03-23 16:50:40 +000046extern int module_id;
47extern int ignore_errors;
48extern int orig_umask;
Wayne Davison55e50d82004-05-13 07:08:22 +000049extern int keep_partial;
Wayne Davisonba582f72004-05-21 08:27:04 +000050extern int checksum_seed;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000051
Wayne Davisond74a2e32003-08-01 07:58:47 +000052static void delete_one(char *fn, int is_dir)
Andrew Tridgell2f03f951998-07-25 02:25:22 +000053{
Wayne Davisond74a2e32003-08-01 07:58:47 +000054 if (!is_dir) {
55 if (robust_unlink(fn) != 0) {
Wayne Davisond62bcc12004-05-15 19:31:10 +000056 rsyserr(FERROR, errno, "delete_one: unlink %s failed",
57 full_fname(fn));
Andrew Tridgell2f03f951998-07-25 02:25:22 +000058 } else if (verbose) {
Wayne Davisond74a2e32003-08-01 07:58:47 +000059 rprintf(FINFO, "deleting %s\n", fn);
Andrew Tridgell2f03f951998-07-25 02:25:22 +000060 }
Wayne Davison17fadf72003-12-15 08:14:27 +000061 } else {
Wayne Davisond74a2e32003-08-01 07:58:47 +000062 if (do_rmdir(fn) != 0) {
Wayne Davisoneb84a832004-06-12 21:30:07 +000063 if (errno == ENOTDIR && keep_dirlinks) {
64 delete_one(fn, 0);
65 return;
66 }
Wayne Davisond74a2e32003-08-01 07:58:47 +000067 if (errno != ENOTEMPTY && errno != EEXIST) {
Wayne Davisond62bcc12004-05-15 19:31:10 +000068 rsyserr(FERROR, errno,
69 "delete_one: rmdir %s failed",
70 full_fname(fn));
Wayne Davisond74a2e32003-08-01 07:58:47 +000071 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +000072 } else if (verbose) {
Wayne Davisond74a2e32003-08-01 07:58:47 +000073 rprintf(FINFO, "deleting directory %s\n", fn);
Andrew Tridgell2f03f951998-07-25 02:25:22 +000074 }
75 }
76}
77
78
Wayne Davisond74a2e32003-08-01 07:58:47 +000079static int is_backup_file(char *fn)
80{
81 int k = strlen(fn) - backup_suffix_len;
82 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
83}
Andrew Tridgell2f03f951998-07-25 02:25:22 +000084
85
Wayne Davison1e82e2c2004-03-23 16:36:00 +000086/* This deletes any files on the receiving side that are not present
87 * on the sending side. */
Andrew Tridgell6957ae32000-01-24 11:20:25 +000088void delete_files(struct file_list *flist)
Andrew Tridgell2f03f951998-07-25 02:25:22 +000089{
90 struct file_list *local_file_list;
91 int i, j;
Wayne Davisona24639b2004-01-22 04:40:33 +000092 char *argv[1], fbuf[MAXPATHLEN];
Andrew Tridgell0b73ca12000-01-23 11:43:04 +000093 static int deletion_count;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000094
95 if (cvs_exclude)
96 add_cvs_excludes();
97
Andrew Tridgellef55c682000-03-21 04:06:04 +000098 if (io_error && !(lp_ignore_errors(module_id) || ignore_errors)) {
Andrew Tridgell2f03f951998-07-25 02:25:22 +000099 rprintf(FINFO,"IO error encountered - skipping file deletion\n");
100 return;
101 }
102
Wayne Davison1e82e2c2004-03-23 16:36:00 +0000103 for (j = 0; j < flist->count; j++) {
Wayne Davisona5342642004-01-27 01:05:12 +0000104 if (!(flist->files[j]->flags & FLAG_TOP_DIR)
105 || !S_ISDIR(flist->files[j]->mode))
106 continue;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000107
Wayne Davisona24639b2004-01-22 04:40:33 +0000108 argv[0] = f_name_to(flist->files[j], fbuf);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000109
Wayne Davisona24639b2004-01-22 04:40:33 +0000110 if (!(local_file_list = send_file_list(-1, 1, argv)))
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000111 continue;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000112
113 if (verbose > 1)
Wayne Davison92b9eb92004-03-23 16:50:40 +0000114 rprintf(FINFO, "deleting in %s\n", fbuf);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000115
Wayne Davison446e2392004-01-02 08:18:53 +0000116 for (i = local_file_list->count-1; i >= 0; i--) {
Wayne Davison92b9eb92004-03-23 16:50:40 +0000117 if (max_delete && deletion_count > max_delete)
118 break;
119 if (!local_file_list->files[i]->basename)
120 continue;
121 if (flist_find(flist,local_file_list->files[i]) < 0) {
David Dykstra53dd3131998-11-24 19:01:24 +0000122 char *f = f_name(local_file_list->files[i]);
Wayne Davisond74a2e32003-08-01 07:58:47 +0000123 if (make_backups && (backup_dir || !is_backup_file(f))) {
David Dykstra53dd3131998-11-24 19:01:24 +0000124 (void) make_backup(f);
Wayne Davisond74a2e32003-08-01 07:58:47 +0000125 if (verbose)
126 rprintf(FINFO, "deleting %s\n", f);
David Dykstra53dd3131998-11-24 19:01:24 +0000127 } else {
Wayne Davisond74a2e32003-08-01 07:58:47 +0000128 int mode = local_file_list->files[i]->mode;
129 delete_one(f, S_ISDIR(mode) != 0);
David Dykstra53dd3131998-11-24 19:01:24 +0000130 }
Wayne Davison31f3b682003-08-01 08:20:53 +0000131 deletion_count++;
David Dykstra53dd3131998-11-24 19:01:24 +0000132 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000133 }
134 flist_free(local_file_list);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000135 }
136}
137
138
J.W. Schultz52d3e102003-03-26 11:04:14 +0000139/*
140 * get_tmpname() - create a tmp filename for a given filename
141 *
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!
Wayne Davison17fadf72003-12-15 08:14:27 +0000146 *
J.W. Schultz52d3e102003-03-26 11:04:14 +0000147 * 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
153 * make it all fit.
Wayne Davison17fadf72003-12-15 08:14:27 +0000154 *
J.W. Schultz52d3e102003-03-26 11:04:14 +0000155 * 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.
158 */
159
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000160static int get_tmpname(char *fnametmp, char *fname)
161{
162 char *f;
J.W. Schultz52d3e102003-03-26 11:04:14 +0000163 int length = 0;
164 int maxname;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000165
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000166 if (tmpdir) {
Wayne Davisona24639b2004-01-22 04:40:33 +0000167 /* Note: this can't overflow, so the return value is safe */
J.W. Schultzb7cee942004-01-20 03:37:04 +0000168 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
J.W. Schultz52d3e102003-03-26 11:04:14 +0000169 fnametmp[length++] = '/';
170 fnametmp[length] = '\0'; /* always NULL terminated */
Wayne Davison0c2ef5f2003-08-04 21:00:57 +0000171 }
J.W. Schultz52d3e102003-03-26 11:04:14 +0000172
Wayne Davison0c2ef5f2003-08-04 21:00:57 +0000173 if ((f = strrchr(fname, '/')) != NULL) {
J.W. Schultz52d3e102003-03-26 11:04:14 +0000174 ++f;
175 if (!tmpdir) {
176 length = f - fname;
Wayne Davison0c2ef5f2003-08-04 21:00:57 +0000177 /* copy up to and including the slash */
J.W. Schultz52d3e102003-03-26 11:04:14 +0000178 strlcpy(fnametmp, fname, length + 1);
Wayne Davison0c2ef5f2003-08-04 21:00:57 +0000179 }
Wayne Davison446e2392004-01-02 08:18:53 +0000180 } else
J.W. Schultz52d3e102003-03-26 11:04:14 +0000181 f = fname;
J.W. Schultz52d3e102003-03-26 11:04:14 +0000182 fnametmp[length++] = '.';
183 fnametmp[length] = '\0'; /* always NULL terminated */
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000184
J.W. Schultz52d3e102003-03-26 11:04:14 +0000185 maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000186
Wayne Davison0c2ef5f2003-08-04 21:00:57 +0000187 if (maxname < 1) {
J.W. Schultz52d3e102003-03-26 11:04:14 +0000188 rprintf(FERROR, "temporary filename too long: %s\n", fname);
189 fnametmp[0] = '\0';
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000190 return 0;
191 }
192
Wayne Davison17fadf72003-12-15 08:14:27 +0000193 strlcpy(fnametmp + length, f, maxname);
J.W. Schultz52d3e102003-03-26 11:04:14 +0000194 strcat(fnametmp + length, ".XXXXXX");
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000195
196 return 1;
197}
198
199
Wayne Davison446e2392004-01-02 08:18:53 +0000200static int receive_data(int f_in,struct map_struct *mapbuf,int fd,char *fname,
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000201 OFF_T total_size)
202{
Martin Pool9dd891b2002-01-23 04:57:18 +0000203 int i;
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000204 struct sum_struct sum;
205 unsigned int len;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000206 OFF_T offset = 0;
207 OFF_T offset2;
208 char *data;
209 static char file_sum1[MD4_SUM_LENGTH];
210 static char file_sum2[MD4_SUM_LENGTH];
211 char *map=NULL;
Wayne Davison17fadf72003-12-15 08:14:27 +0000212
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000213 read_sum_head(f_in, &sum);
Wayne Davison17fadf72003-12-15 08:14:27 +0000214
Wayne Davisonba582f72004-05-21 08:27:04 +0000215 sum_init(checksum_seed);
Wayne Davison17fadf72003-12-15 08:14:27 +0000216
Wayne Davison3f55bd52004-01-08 00:45:41 +0000217 while ((i = recv_token(f_in, &data)) != 0) {
Wayne Davison16417f82003-07-08 16:49:10 +0000218 if (do_progress)
219 show_progress(offset, total_size);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000220
221 if (i > 0) {
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000222 if (verbose > 3) {
Andrew Tridgell5f808df2000-01-23 12:30:34 +0000223 rprintf(FINFO,"data recv %d at %.0f\n",
224 i,(double)offset);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000225 }
226
227 stats.literal_data += i;
228 cleanup_got_literal = 1;
Wayne Davison17fadf72003-12-15 08:14:27 +0000229
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000230 sum_update(data,i);
231
232 if (fd != -1 && write_file(fd,data,i) != i) {
Wayne Davisond62bcc12004-05-15 19:31:10 +0000233 rsyserr(FERROR, errno, "write failed on %s",
234 full_fname(fname));
Andrew Tridgell65417571998-11-03 07:08:27 +0000235 exit_cleanup(RERR_FILEIO);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000236 }
237 offset += i;
238 continue;
Wayne Davison17fadf72003-12-15 08:14:27 +0000239 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000240
241 i = -(i+1);
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000242 offset2 = i*(OFF_T)sum.blength;
243 len = sum.blength;
244 if (i == (int) sum.count-1 && sum.remainder != 0)
245 len = sum.remainder;
Wayne Davison17fadf72003-12-15 08:14:27 +0000246
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000247 stats.matched_data += len;
Wayne Davison17fadf72003-12-15 08:14:27 +0000248
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000249 if (verbose > 3)
Andrew Tridgell5f808df2000-01-23 12:30:34 +0000250 rprintf(FINFO,"chunk[%d] of size %d at %.0f offset=%.0f\n",
251 i,len,(double)offset2,(double)offset);
Wayne Davison17fadf72003-12-15 08:14:27 +0000252
Wayne Davison446e2392004-01-02 08:18:53 +0000253 if (mapbuf) {
254 map = map_ptr(mapbuf,offset2,len);
Wayne Davison17fadf72003-12-15 08:14:27 +0000255
Andrew Tridgellc55f7022000-01-24 11:41:08 +0000256 see_token(map, len);
257 sum_update(map,len);
258 }
Wayne Davison17fadf72003-12-15 08:14:27 +0000259
Martin Poola2619892002-01-25 23:07:33 +0000260 if (fd != -1 && write_file(fd,map,len) != (int) len) {
Wayne Davisond62bcc12004-05-15 19:31:10 +0000261 rsyserr(FERROR, errno, "write failed on %s",
262 full_fname(fname));
Andrew Tridgell65417571998-11-03 07:08:27 +0000263 exit_cleanup(RERR_FILEIO);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000264 }
265 offset += len;
266 }
267
Wayne Davison76c21942004-01-02 08:29:49 +0000268 flush_write_file(fd);
269
Wayne Davison16417f82003-07-08 16:49:10 +0000270 if (do_progress)
271 end_progress(total_size);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000272
273 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
Wayne Davisond62bcc12004-05-15 19:31:10 +0000274 rsyserr(FERROR, errno, "write failed on %s",
275 full_fname(fname));
Andrew Tridgell65417571998-11-03 07:08:27 +0000276 exit_cleanup(RERR_FILEIO);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000277 }
278
279 sum_end(file_sum1);
280
J.W. Schultzbc63ae32003-03-31 17:28:34 +0000281 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
282 if (verbose > 2) {
283 rprintf(FINFO,"got file_sum\n");
284 }
Wayne Davison17fadf72003-12-15 08:14:27 +0000285 if (fd != -1 && memcmp(file_sum1,file_sum2,MD4_SUM_LENGTH) != 0) {
J.W. Schultzbc63ae32003-03-31 17:28:34 +0000286 return 0;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000287 }
288 return 1;
289}
290
291
Martin Poolb35d0d82002-04-08 04:10:20 +0000292/**
293 * main routine for receiver process.
294 *
295 * Receiver process runs on the same host as the generator process. */
Wayne Davison0569a132004-01-15 07:42:25 +0000296int recv_files(int f_in,struct file_list *flist,char *local_name)
Wayne Davison17fadf72003-12-15 08:14:27 +0000297{
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000298 int fd1,fd2;
299 STRUCT_STAT st;
Wayne Davison446e2392004-01-02 08:18:53 +0000300 char *fname, fbuf[MAXPATHLEN];
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000301 char template[MAXPATHLEN];
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000302 char fnametmp[MAXPATHLEN];
David Dykstra375a4551998-10-26 21:42:38 +0000303 char *fnamecmp;
304 char fnamecmpbuf[MAXPATHLEN];
Wayne Davison446e2392004-01-02 08:18:53 +0000305 struct map_struct *mapbuf;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000306 struct file_struct *file;
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000307 struct stats initial_stats;
Wayne Davison4e834af2004-06-14 15:09:36 +0000308 int save_make_backups = make_backups;
309 int i, recv_ok, phase = 0;
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000310
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000311 if (verbose > 2) {
312 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
313 }
314
Wayne Davisoncb869c22004-02-10 17:28:59 +0000315 if (flist->hlink_pool) {
J.W. Schultz99350662004-02-10 03:23:37 +0000316 pool_destroy(flist->hlink_pool);
317 flist->hlink_pool = NULL;
318 }
319
Wayne Davison17fadf72003-12-15 08:14:27 +0000320 while (1) {
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000321 cleanup_disable();
322
323 i = read_int(f_in);
324 if (i == -1) {
Wayne Davison4e834af2004-06-14 15:09:36 +0000325 if (phase)
326 break;
327 phase = 1;
328 csum_length = SUM_LENGTH;
329 if (verbose > 2)
330 rprintf(FINFO, "recv_files phase=%d\n", phase);
331 send_msg(MSG_DONE, "", 0);
332 if (keep_partial)
333 make_backups = 0; /* prevents double backup */
334 continue;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000335 }
336
337 if (i < 0 || i >= flist->count) {
Wayne Davison17fadf72003-12-15 08:14:27 +0000338 rprintf(FERROR,"Invalid file index %d in recv_files (count=%d)\n",
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000339 i, flist->count);
Andrew Tridgell65417571998-11-03 07:08:27 +0000340 exit_cleanup(RERR_PROTOCOL);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000341 }
342
343 file = flist->files[i];
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000344
Wayne Davison97feb552004-01-13 18:22:43 +0000345 stats.current_file_index = i;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000346 stats.num_transferred_files++;
347 stats.total_transferred_size += file->length;
Wayne Davison925c5172004-01-02 08:38:35 +0000348 cleanup_got_literal = 0;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000349
350 if (local_name)
351 fname = local_name;
Wayne Davison446e2392004-01-02 08:18:53 +0000352 else
Wayne Davison3fef5362004-01-22 04:38:18 +0000353 fname = f_name_to(file, fbuf);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000354
355 if (dry_run) {
J.W. Schultz42d4edc2003-03-25 02:28:54 +0000356 if (!am_server && verbose) { /* log transfer */
357 rprintf(FINFO, "%s\n", fname);
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000358 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000359 continue;
360 }
361
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000362 initial_stats = stats;
363
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000364 if (verbose > 2)
365 rprintf(FINFO,"recv_files(%s)\n",fname);
366
David Dykstra375a4551998-10-26 21:42:38 +0000367 fnamecmp = fname;
368
Wayne Davison17fadf72003-12-15 08:14:27 +0000369 /* open the file */
Andrew Tridgell8c9fd201999-10-25 22:04:09 +0000370 fd1 = do_open(fnamecmp, O_RDONLY, 0);
David Dykstra375a4551998-10-26 21:42:38 +0000371
Wayne Davisonc3384602004-02-27 08:03:49 +0000372 if (fd1 == -1 && compare_dest != NULL) {
David Dykstra375a4551998-10-26 21:42:38 +0000373 /* try the file at compare_dest instead */
Wayne Davisonf91e01d2004-01-24 22:12:58 +0000374 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
375 compare_dest, fname);
David Dykstra375a4551998-10-26 21:42:38 +0000376 fnamecmp = fnamecmpbuf;
Andrew Tridgell8c9fd201999-10-25 22:04:09 +0000377 fd1 = do_open(fnamecmp, O_RDONLY, 0);
David Dykstra375a4551998-10-26 21:42:38 +0000378 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000379
380 if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
Wayne Davisond62bcc12004-05-15 19:31:10 +0000381 rsyserr(FERROR, errno, "fstat %s failed",
382 full_fname(fnamecmp));
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000383 receive_data(f_in,NULL,-1,NULL,file->length);
384 close(fd1);
385 continue;
386 }
387
J.W. Schultz350e4e42003-09-04 05:49:50 +0000388 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
392 * with directories
393 */
Wayne Davisonea425412003-09-11 04:53:05 +0000394 rprintf(FERROR,"recv_files: %s is a directory\n",
395 full_fname(fnamecmp));
J.W. Schultz350e4e42003-09-04 05:49:50 +0000396 receive_data(f_in, NULL, -1, NULL, file->length);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000397 close(fd1);
398 continue;
399 }
400
J.W. Schultz350e4e42003-09-04 05:49:50 +0000401 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
402 close(fd1);
403 fd1 = -1;
Wayne Davison446e2392004-01-02 08:18:53 +0000404 mapbuf = NULL;
J.W. Schultz350e4e42003-09-04 05:49:50 +0000405 }
406
Andrew Tridgell4df9f361999-10-31 04:28:03 +0000407 if (fd1 != -1 && !preserve_perms) {
J.W. Schultz85ed0aa2003-03-21 07:27:31 +0000408 /* if the file exists already and we aren't preserving
Wayne Davison17fadf72003-12-15 08:14:27 +0000409 * permissions then act as though the remote end sent
410 * us the file permissions we already have */
Andrew Tridgell4df9f361999-10-31 04:28:03 +0000411 file->mode = st.st_mode;
412 }
413
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000414 if (fd1 != -1 && st.st_size > 0) {
Wayne Davison446e2392004-01-02 08:18:53 +0000415 mapbuf = map_file(fd1,st.st_size);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000416 if (verbose > 2)
Andrew Tridgell5f808df2000-01-23 12:30:34 +0000417 rprintf(FINFO,"recv mapped %s of size %.0f\n",fnamecmp,(double)st.st_size);
Wayne Davison446e2392004-01-02 08:18:53 +0000418 } else
419 mapbuf = NULL;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000420
421 if (!get_tmpname(fnametmp,fname)) {
Wayne Davison446e2392004-01-02 08:18:53 +0000422 if (mapbuf) unmap_file(mapbuf);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000423 if (fd1 != -1) close(fd1);
424 continue;
425 }
426
J.W. Schultzbd6abc42004-02-03 03:42:49 +0000427 strlcpy(template, fnametmp, sizeof template);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000428
429 /* we initially set the perms without the
Wayne Davison17fadf72003-12-15 08:14:27 +0000430 * 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. */
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000435 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000436
David Dykstra752eaba1999-03-24 19:28:03 +0000437 /* in most cases parent directories will already exist
Wayne Davison17fadf72003-12-15 08:14:27 +0000438 * 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 &&
Martin Poolb35d0d82002-04-08 04:10:20 +0000441 create_directory_path(fnametmp, orig_umask) == 0) {
J.W. Schultzbd6abc42004-02-03 03:42:49 +0000442 strlcpy(fnametmp, template, sizeof fnametmp);
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000443 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000444 }
445 if (fd2 == -1) {
Wayne Davisond62bcc12004-05-15 19:31:10 +0000446 rsyserr(FERROR, errno, "mkstemp %s failed",
447 full_fname(fnametmp));
Wayne Davison446e2392004-01-02 08:18:53 +0000448 receive_data(f_in,mapbuf,-1,NULL,file->length);
449 if (mapbuf) unmap_file(mapbuf);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000450 if (fd1 != -1) close(fd1);
451 continue;
452 }
Wayne Davison17fadf72003-12-15 08:14:27 +0000453
Wayne Davison446e2392004-01-02 08:18:53 +0000454 cleanup_set(fnametmp, fname, file, mapbuf, fd1, fd2);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000455
J.W. Schultz42d4edc2003-03-25 02:28:54 +0000456 if (!am_server && verbose) { /* log transfer */
457 rprintf(FINFO, "%s\n", fname);
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000458 }
459
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000460 /* recv file data */
Wayne Davison446e2392004-01-02 08:18:53 +0000461 recv_ok = receive_data(f_in,mapbuf,fd2,fname,file->length);
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000462
463 log_recv(file, &initial_stats);
Wayne Davison17fadf72003-12-15 08:14:27 +0000464
Wayne Davison446e2392004-01-02 08:18:53 +0000465 if (mapbuf) unmap_file(mapbuf);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000466 if (fd1 != -1) {
467 close(fd1);
468 }
Wayne Davison9f27cd82004-04-27 19:51:33 +0000469 if (close(fd2) < 0) {
Wayne Davisond62bcc12004-05-15 19:31:10 +0000470 rsyserr(FERROR, errno, "close failed on %s",
471 full_fname(fnametmp));
Wayne Davison9f27cd82004-04-27 19:51:33 +0000472 exit_cleanup(RERR_FILEIO);
473 }
Wayne Davison17fadf72003-12-15 08:14:27 +0000474
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000475 if (verbose > 2)
476 rprintf(FINFO,"renaming %s to %s\n",fnametmp,fname);
477
Wayne Davison55e50d82004-05-13 07:08:22 +0000478 if (recv_ok || keep_partial)
479 finish_transfer(fname, fnametmp, file, recv_ok);
480 else
481 do_unlink(fnametmp);
Andrew Tridgellc6b81a91998-09-09 06:23:27 +0000482
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000483 cleanup_disable();
Andrew Tridgell554e0a82000-01-23 07:36:56 +0000484
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000485 if (!recv_ok) {
486 if (csum_length == SUM_LENGTH) {
487 rprintf(FERROR,"ERROR: file corruption in %s. File changed during transfer?\n",
Wayne Davisonea425412003-09-11 04:53:05 +0000488 full_fname(fname));
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000489 } else {
Wayne Davison0569a132004-01-15 07:42:25 +0000490 char buf[4];
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000491 if (verbose > 1)
492 rprintf(FINFO,"redoing %s(%d)\n",fname,i);
Wayne Davison0569a132004-01-15 07:42:25 +0000493 SIVAL(buf, 0, i);
494 send_msg(MSG_REDO, buf, 4);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000495 }
496 }
497 }
Wayne Davison4e834af2004-06-14 15:09:36 +0000498 make_backups = save_make_backups;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000499
Wayne Davison3f55bd52004-01-08 00:45:41 +0000500 if (delete_after && recurse && delete_mode && !local_name
501 && flist->count > 0)
502 delete_files(flist);
Andrew Tridgell57df1711999-11-08 13:03:05 +0000503
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000504 if (verbose > 2)
505 rprintf(FINFO,"recv_files finished\n");
Wayne Davison17fadf72003-12-15 08:14:27 +0000506
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000507 return 0;
508}