blob: 97b120492655437a4bdcf4f18b8f0d50c0a135ae [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;
Wayne Davison16cc9ca2004-07-21 23:59:37 +000031extern int read_batch;
32extern int batch_gen_fd;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000033extern int am_server;
34extern int relative_paths;
Wayne Davison566fce32004-06-11 07:40:48 +000035extern int keep_dirlinks;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000036extern int preserve_hard_links;
Wayne Davison92b9eb92004-03-23 16:50:40 +000037extern int preserve_perms;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000038extern int cvs_exclude;
39extern int io_error;
40extern char *tmpdir;
David Dykstra375a4551998-10-26 21:42:38 +000041extern char *compare_dest;
David Dykstra53dd3131998-11-24 19:01:24 +000042extern int make_backups;
Wayne Davison16417f82003-07-08 16:49:10 +000043extern int do_progress;
Wayne Davisond74a2e32003-08-01 07:58:47 +000044extern char *backup_dir;
David Dykstra53dd3131998-11-24 19:01:24 +000045extern char *backup_suffix;
Wayne Davisond74a2e32003-08-01 07:58:47 +000046extern int backup_suffix_len;
Wayne Davison925c5172004-01-02 08:38:35 +000047extern int cleanup_got_literal;
Wayne Davison92b9eb92004-03-23 16:50:40 +000048extern int module_id;
49extern int ignore_errors;
50extern int orig_umask;
Wayne Davison55e50d82004-05-13 07:08:22 +000051extern int keep_partial;
Wayne Davisonba582f72004-05-21 08:27:04 +000052extern int checksum_seed;
Wayne Davisona3221d22004-07-16 20:06:24 +000053extern int inplace;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000054
Wayne Davison5ebab6c2004-07-19 16:37:30 +000055extern struct exclude_list_struct server_exclude_list;
56
57
Wayne Davisond74a2e32003-08-01 07:58:47 +000058static void delete_one(char *fn, int is_dir)
Andrew Tridgell2f03f951998-07-25 02:25:22 +000059{
Wayne Davisond74a2e32003-08-01 07:58:47 +000060 if (!is_dir) {
61 if (robust_unlink(fn) != 0) {
Wayne Davisond62bcc12004-05-15 19:31:10 +000062 rsyserr(FERROR, errno, "delete_one: unlink %s failed",
63 full_fname(fn));
Wayne Davisond2a918b2004-07-02 18:23:57 +000064 } else if (verbose)
Wayne Davisonecc81fc2004-07-26 16:36:59 +000065 rprintf(FINFO, "deleting %s\n", safe_fname(fn));
Wayne Davison17fadf72003-12-15 08:14:27 +000066 } else {
Wayne Davisond74a2e32003-08-01 07:58:47 +000067 if (do_rmdir(fn) != 0) {
Wayne Davisoneb84a832004-06-12 21:30:07 +000068 if (errno == ENOTDIR && keep_dirlinks) {
69 delete_one(fn, 0);
70 return;
71 }
Wayne Davisond74a2e32003-08-01 07:58:47 +000072 if (errno != ENOTEMPTY && errno != EEXIST) {
Wayne Davisond62bcc12004-05-15 19:31:10 +000073 rsyserr(FERROR, errno,
74 "delete_one: rmdir %s failed",
75 full_fname(fn));
Wayne Davisond74a2e32003-08-01 07:58:47 +000076 }
Wayne Davisonecc81fc2004-07-26 16:36:59 +000077 } else if (verbose) {
78 rprintf(FINFO, "deleting directory %s\n",
79 safe_fname(fn));
80 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +000081 }
82}
83
84
Wayne Davisond74a2e32003-08-01 07:58:47 +000085static int is_backup_file(char *fn)
86{
87 int k = strlen(fn) - backup_suffix_len;
88 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
89}
Andrew Tridgell2f03f951998-07-25 02:25:22 +000090
91
Wayne Davison1e82e2c2004-03-23 16:36:00 +000092/* This deletes any files on the receiving side that are not present
93 * on the sending side. */
Andrew Tridgell6957ae32000-01-24 11:20:25 +000094void delete_files(struct file_list *flist)
Andrew Tridgell2f03f951998-07-25 02:25:22 +000095{
96 struct file_list *local_file_list;
97 int i, j;
Wayne Davisona24639b2004-01-22 04:40:33 +000098 char *argv[1], fbuf[MAXPATHLEN];
Andrew Tridgell0b73ca12000-01-23 11:43:04 +000099 static int deletion_count;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000100
101 if (cvs_exclude)
102 add_cvs_excludes();
103
Andrew Tridgellef55c682000-03-21 04:06:04 +0000104 if (io_error && !(lp_ignore_errors(module_id) || ignore_errors)) {
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000105 rprintf(FINFO,"IO error encountered - skipping file deletion\n");
106 return;
107 }
108
Wayne Davison1e82e2c2004-03-23 16:36:00 +0000109 for (j = 0; j < flist->count; j++) {
Wayne Davisona5342642004-01-27 01:05:12 +0000110 if (!(flist->files[j]->flags & FLAG_TOP_DIR)
111 || !S_ISDIR(flist->files[j]->mode))
112 continue;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000113
Wayne Davisona24639b2004-01-22 04:40:33 +0000114 argv[0] = f_name_to(flist->files[j], fbuf);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000115
Wayne Davisona24639b2004-01-22 04:40:33 +0000116 if (!(local_file_list = send_file_list(-1, 1, argv)))
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000117 continue;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000118
119 if (verbose > 1)
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000120 rprintf(FINFO, "deleting in %s\n", safe_fname(fbuf));
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000121
Wayne Davison446e2392004-01-02 08:18:53 +0000122 for (i = local_file_list->count-1; i >= 0; i--) {
Wayne Davison92b9eb92004-03-23 16:50:40 +0000123 if (max_delete && deletion_count > max_delete)
124 break;
125 if (!local_file_list->files[i]->basename)
126 continue;
127 if (flist_find(flist,local_file_list->files[i]) < 0) {
David Dykstra53dd3131998-11-24 19:01:24 +0000128 char *f = f_name(local_file_list->files[i]);
Wayne Davisond74a2e32003-08-01 07:58:47 +0000129 if (make_backups && (backup_dir || !is_backup_file(f))) {
Wayne Davisond2a918b2004-07-02 18:23:57 +0000130 make_backup(f);
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000131 if (verbose) {
132 rprintf(FINFO, "deleting %s\n",
133 safe_fname(f));
134 }
David Dykstra53dd3131998-11-24 19:01:24 +0000135 } else {
Wayne Davisond74a2e32003-08-01 07:58:47 +0000136 int mode = local_file_list->files[i]->mode;
137 delete_one(f, S_ISDIR(mode) != 0);
David Dykstra53dd3131998-11-24 19:01:24 +0000138 }
Wayne Davison31f3b682003-08-01 08:20:53 +0000139 deletion_count++;
David Dykstra53dd3131998-11-24 19:01:24 +0000140 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000141 }
142 flist_free(local_file_list);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000143 }
144}
145
146
J.W. Schultz52d3e102003-03-26 11:04:14 +0000147/*
148 * get_tmpname() - create a tmp filename for a given filename
149 *
150 * If a tmpdir is defined, use that as the directory to
151 * put it in. Otherwise, the tmp filename is in the same
152 * directory as the given name. Note that there may be no
153 * directory at all in the given name!
Wayne Davison17fadf72003-12-15 08:14:27 +0000154 *
J.W. Schultz52d3e102003-03-26 11:04:14 +0000155 * The tmp filename is basically the given filename with a
156 * dot prepended, and .XXXXXX appended (for mkstemp() to
157 * put its unique gunk in). Take care to not exceed
158 * either the MAXPATHLEN or NAME_MAX, esp. the last, as
159 * the basename basically becomes 8 chars longer. In that
160 * case, the original name is shortened sufficiently to
161 * make it all fit.
Wayne Davison17fadf72003-12-15 08:14:27 +0000162 *
J.W. Schultz52d3e102003-03-26 11:04:14 +0000163 * Of course, there's no real reason for the tmp name to
164 * look like the original, except to satisfy us humans.
165 * As long as it's unique, rsync will work.
166 */
167
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000168static int get_tmpname(char *fnametmp, char *fname)
169{
170 char *f;
J.W. Schultz52d3e102003-03-26 11:04:14 +0000171 int length = 0;
172 int maxname;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000173
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000174 if (tmpdir) {
Wayne Davisona24639b2004-01-22 04:40:33 +0000175 /* Note: this can't overflow, so the return value is safe */
J.W. Schultzb7cee942004-01-20 03:37:04 +0000176 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
J.W. Schultz52d3e102003-03-26 11:04:14 +0000177 fnametmp[length++] = '/';
178 fnametmp[length] = '\0'; /* always NULL terminated */
Wayne Davison0c2ef5f2003-08-04 21:00:57 +0000179 }
J.W. Schultz52d3e102003-03-26 11:04:14 +0000180
Wayne Davison0c2ef5f2003-08-04 21:00:57 +0000181 if ((f = strrchr(fname, '/')) != NULL) {
J.W. Schultz52d3e102003-03-26 11:04:14 +0000182 ++f;
183 if (!tmpdir) {
184 length = f - fname;
Wayne Davison0c2ef5f2003-08-04 21:00:57 +0000185 /* copy up to and including the slash */
J.W. Schultz52d3e102003-03-26 11:04:14 +0000186 strlcpy(fnametmp, fname, length + 1);
Wayne Davison0c2ef5f2003-08-04 21:00:57 +0000187 }
Wayne Davison446e2392004-01-02 08:18:53 +0000188 } else
J.W. Schultz52d3e102003-03-26 11:04:14 +0000189 f = fname;
J.W. Schultz52d3e102003-03-26 11:04:14 +0000190 fnametmp[length++] = '.';
191 fnametmp[length] = '\0'; /* always NULL terminated */
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000192
J.W. Schultz52d3e102003-03-26 11:04:14 +0000193 maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000194
Wayne Davison0c2ef5f2003-08-04 21:00:57 +0000195 if (maxname < 1) {
J.W. Schultz52d3e102003-03-26 11:04:14 +0000196 rprintf(FERROR, "temporary filename too long: %s\n", fname);
197 fnametmp[0] = '\0';
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000198 return 0;
199 }
200
Wayne Davison17fadf72003-12-15 08:14:27 +0000201 strlcpy(fnametmp + length, f, maxname);
J.W. Schultz52d3e102003-03-26 11:04:14 +0000202 strcat(fnametmp + length, ".XXXXXX");
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000203
204 return 1;
205}
206
207
Wayne Davison7e5fa372004-07-20 21:35:58 +0000208static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
209 char *fname, int fd, OFF_T total_size)
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000210{
Wayne Davison7e5fa372004-07-20 21:35:58 +0000211 static char file_sum1[MD4_SUM_LENGTH];
212 static char file_sum2[MD4_SUM_LENGTH];
213 struct map_struct *mapbuf;
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000214 struct sum_struct sum;
215 unsigned int len;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000216 OFF_T offset = 0;
217 OFF_T offset2;
218 char *data;
Wayne Davison7e5fa372004-07-20 21:35:58 +0000219 int i;
Wayne Davisone1f67412004-06-30 07:27:30 +0000220 char *map = NULL;
Wayne Davison17fadf72003-12-15 08:14:27 +0000221
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000222 read_sum_head(f_in, &sum);
Wayne Davison17fadf72003-12-15 08:14:27 +0000223
Wayne Davison7e5fa372004-07-20 21:35:58 +0000224 if (fd_r >= 0 && size_r > 0) {
225 mapbuf = map_file(fd_r, size_r, sum.blength);
226 if (verbose > 2) {
227 rprintf(FINFO, "recv mapped %s of size %.0f\n",
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000228 safe_fname(fname_r), (double)size_r);
Wayne Davison7e5fa372004-07-20 21:35:58 +0000229 }
230 } else
231 mapbuf = NULL;
232
Wayne Davisonba582f72004-05-21 08:27:04 +0000233 sum_init(checksum_seed);
Wayne Davison17fadf72003-12-15 08:14:27 +0000234
Wayne Davison3f55bd52004-01-08 00:45:41 +0000235 while ((i = recv_token(f_in, &data)) != 0) {
Wayne Davison16417f82003-07-08 16:49:10 +0000236 if (do_progress)
237 show_progress(offset, total_size);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000238
239 if (i > 0) {
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000240 if (verbose > 3) {
Andrew Tridgell5f808df2000-01-23 12:30:34 +0000241 rprintf(FINFO,"data recv %d at %.0f\n",
242 i,(double)offset);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000243 }
244
245 stats.literal_data += i;
246 cleanup_got_literal = 1;
Wayne Davison17fadf72003-12-15 08:14:27 +0000247
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000248 sum_update(data,i);
249
250 if (fd != -1 && write_file(fd,data,i) != i) {
Wayne Davisond62bcc12004-05-15 19:31:10 +0000251 rsyserr(FERROR, errno, "write failed on %s",
252 full_fname(fname));
Andrew Tridgell65417571998-11-03 07:08:27 +0000253 exit_cleanup(RERR_FILEIO);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000254 }
255 offset += i;
256 continue;
Wayne Davison17fadf72003-12-15 08:14:27 +0000257 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000258
259 i = -(i+1);
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000260 offset2 = i*(OFF_T)sum.blength;
261 len = sum.blength;
Wayne Davisond2a918b2004-07-02 18:23:57 +0000262 if (i == (int)sum.count-1 && sum.remainder != 0)
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000263 len = sum.remainder;
Wayne Davison17fadf72003-12-15 08:14:27 +0000264
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000265 stats.matched_data += len;
Wayne Davison17fadf72003-12-15 08:14:27 +0000266
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000267 if (verbose > 3)
Andrew Tridgell5f808df2000-01-23 12:30:34 +0000268 rprintf(FINFO,"chunk[%d] of size %d at %.0f offset=%.0f\n",
269 i,len,(double)offset2,(double)offset);
Wayne Davison17fadf72003-12-15 08:14:27 +0000270
Wayne Davison446e2392004-01-02 08:18:53 +0000271 if (mapbuf) {
272 map = map_ptr(mapbuf,offset2,len);
Wayne Davison17fadf72003-12-15 08:14:27 +0000273
Andrew Tridgellc55f7022000-01-24 11:41:08 +0000274 see_token(map, len);
275 sum_update(map,len);
276 }
Wayne Davison17fadf72003-12-15 08:14:27 +0000277
Wayne Davisona3221d22004-07-16 20:06:24 +0000278 if (!inplace || offset != offset2) {
279 if (fd != -1 && write_file(fd, map, len) != (int)len) {
280 rsyserr(FERROR, errno, "write failed on %s",
281 full_fname(fname));
282 exit_cleanup(RERR_FILEIO);
283 }
284 } else {
285 flush_write_file(fd);
286 if (do_lseek(fd,(OFF_T)len,SEEK_CUR) != offset+len) {
287 rprintf(FERROR, "lseek failed on %s: %s, %lli, %lli, %i\n",
Wayne Davison7e5fa372004-07-20 21:35:58 +0000288 full_fname(fname), strerror(errno),
289 do_lseek(fd, 0, SEEK_CUR),
290 offset + len, i);
Wayne Davisona3221d22004-07-16 20:06:24 +0000291 exit_cleanup(RERR_FILEIO);
292 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000293 }
294 offset += len;
295 }
296
Wayne Davison76c21942004-01-02 08:29:49 +0000297 flush_write_file(fd);
298
Wayne Davisona3221d22004-07-16 20:06:24 +0000299#ifdef HAVE_FTRUNCATE
300 if (inplace)
301 ftruncate(fd, offset);
302#endif
303
Wayne Davison16417f82003-07-08 16:49:10 +0000304 if (do_progress)
305 end_progress(total_size);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000306
307 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
Wayne Davisond62bcc12004-05-15 19:31:10 +0000308 rsyserr(FERROR, errno, "write failed on %s",
309 full_fname(fname));
Andrew Tridgell65417571998-11-03 07:08:27 +0000310 exit_cleanup(RERR_FILEIO);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000311 }
312
313 sum_end(file_sum1);
314
Wayne Davison7e5fa372004-07-20 21:35:58 +0000315 if (mapbuf)
316 unmap_file(mapbuf);
317
J.W. Schultzbc63ae32003-03-31 17:28:34 +0000318 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
Wayne Davisond2a918b2004-07-02 18:23:57 +0000319 if (verbose > 2)
J.W. Schultzbc63ae32003-03-31 17:28:34 +0000320 rprintf(FINFO,"got file_sum\n");
Wayne Davisond2a918b2004-07-02 18:23:57 +0000321 if (fd != -1 && memcmp(file_sum1, file_sum2, MD4_SUM_LENGTH) != 0)
J.W. Schultzbc63ae32003-03-31 17:28:34 +0000322 return 0;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000323 return 1;
324}
325
326
Wayne Davison8ed9d842004-07-19 17:05:01 +0000327static void discard_receive_data(int f_in, OFF_T length)
328{
Wayne Davison7e5fa372004-07-20 21:35:58 +0000329 receive_data(f_in, NULL, -1, 0, NULL, -1, length);
Wayne Davison8ed9d842004-07-19 17:05:01 +0000330}
331
332
Martin Poolb35d0d82002-04-08 04:10:20 +0000333/**
334 * main routine for receiver process.
335 *
336 * Receiver process runs on the same host as the generator process. */
Wayne Davisond3979b02004-07-14 16:39:08 +0000337int recv_files(int f_in, struct file_list *flist, char *local_name)
Wayne Davison17fadf72003-12-15 08:14:27 +0000338{
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000339 int next_gen_i = -1;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000340 int fd1,fd2;
341 STRUCT_STAT st;
Wayne Davison446e2392004-01-02 08:18:53 +0000342 char *fname, fbuf[MAXPATHLEN];
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000343 char template[MAXPATHLEN];
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000344 char fnametmp[MAXPATHLEN];
David Dykstra375a4551998-10-26 21:42:38 +0000345 char *fnamecmp;
346 char fnamecmpbuf[MAXPATHLEN];
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000347 struct file_struct *file;
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000348 struct stats initial_stats;
Wayne Davison4e834af2004-06-14 15:09:36 +0000349 int save_make_backups = make_backups;
350 int i, recv_ok, phase = 0;
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000351
Wayne Davisond2a918b2004-07-02 18:23:57 +0000352 if (verbose > 2)
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000353 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000354
Wayne Davisoncb869c22004-02-10 17:28:59 +0000355 if (flist->hlink_pool) {
J.W. Schultz99350662004-02-10 03:23:37 +0000356 pool_destroy(flist->hlink_pool);
357 flist->hlink_pool = NULL;
358 }
359
Wayne Davison17fadf72003-12-15 08:14:27 +0000360 while (1) {
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000361 cleanup_disable();
362
363 i = read_int(f_in);
364 if (i == -1) {
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000365 if (read_batch) {
366 if (next_gen_i != flist->count)
367 while (read_int(batch_gen_fd) != -1) {}
368 next_gen_i = -1;
369 }
370
Wayne Davison4e834af2004-06-14 15:09:36 +0000371 if (phase)
372 break;
Wayne Davison5ebab6c2004-07-19 16:37:30 +0000373
Wayne Davison4e834af2004-06-14 15:09:36 +0000374 phase = 1;
375 csum_length = SUM_LENGTH;
376 if (verbose > 2)
377 rprintf(FINFO, "recv_files phase=%d\n", phase);
378 send_msg(MSG_DONE, "", 0);
379 if (keep_partial)
380 make_backups = 0; /* prevents double backup */
381 continue;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000382 }
383
384 if (i < 0 || i >= flist->count) {
Wayne Davison17fadf72003-12-15 08:14:27 +0000385 rprintf(FERROR,"Invalid file index %d in recv_files (count=%d)\n",
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000386 i, flist->count);
Andrew Tridgell65417571998-11-03 07:08:27 +0000387 exit_cleanup(RERR_PROTOCOL);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000388 }
389
390 file = flist->files[i];
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000391
Wayne Davison97feb552004-01-13 18:22:43 +0000392 stats.current_file_index = i;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000393 stats.num_transferred_files++;
394 stats.total_transferred_size += file->length;
Wayne Davison925c5172004-01-02 08:38:35 +0000395 cleanup_got_literal = 0;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000396
397 if (local_name)
398 fname = local_name;
Wayne Davison446e2392004-01-02 08:18:53 +0000399 else
Wayne Davison3fef5362004-01-22 04:38:18 +0000400 fname = f_name_to(file, fbuf);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000401
402 if (dry_run) {
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000403 if (!am_server && verbose) /* log the transfer */
404 rprintf(FINFO, "%s\n", safe_fname(fname));
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000405 continue;
406 }
407
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000408 initial_stats = stats;
409
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000410 if (verbose > 2)
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000411 rprintf(FINFO, "recv_files(%s)\n", safe_fname(fname));
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000412
David Dykstra375a4551998-10-26 21:42:38 +0000413 fnamecmp = fname;
414
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000415 if (read_batch) {
416 while (i > next_gen_i) {
417 next_gen_i = read_int(batch_gen_fd);
418 if (next_gen_i == -1)
419 next_gen_i = flist->count;
420 }
421 if (i < next_gen_i) {
422 rprintf(FINFO, "skipping update for \"%s\"\n",
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000423 safe_fname(fname));
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000424 discard_receive_data(f_in, file->length);
425 continue;
426 }
427 }
428
Wayne Davison5ebab6c2004-07-19 16:37:30 +0000429 if (server_exclude_list.head
430 && check_exclude(&server_exclude_list, fname,
431 S_ISDIR(file->mode)) < 0) {
432 if (verbose) {
433 rprintf(FINFO,
434 "skipping server-excluded update for \"%s\"\n",
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000435 safe_fname(fname));
Wayne Davison5ebab6c2004-07-19 16:37:30 +0000436 }
Wayne Davison8ed9d842004-07-19 17:05:01 +0000437 discard_receive_data(f_in, file->length);
Wayne Davison5ebab6c2004-07-19 16:37:30 +0000438 continue;
439 }
440
Wayne Davison17fadf72003-12-15 08:14:27 +0000441 /* open the file */
Andrew Tridgell8c9fd201999-10-25 22:04:09 +0000442 fd1 = do_open(fnamecmp, O_RDONLY, 0);
David Dykstra375a4551998-10-26 21:42:38 +0000443
Wayne Davisonc3384602004-02-27 08:03:49 +0000444 if (fd1 == -1 && compare_dest != NULL) {
David Dykstra375a4551998-10-26 21:42:38 +0000445 /* try the file at compare_dest instead */
Wayne Davisonf91e01d2004-01-24 22:12:58 +0000446 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
447 compare_dest, fname);
David Dykstra375a4551998-10-26 21:42:38 +0000448 fnamecmp = fnamecmpbuf;
Andrew Tridgell8c9fd201999-10-25 22:04:09 +0000449 fd1 = do_open(fnamecmp, O_RDONLY, 0);
David Dykstra375a4551998-10-26 21:42:38 +0000450 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000451
452 if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
Wayne Davisond62bcc12004-05-15 19:31:10 +0000453 rsyserr(FERROR, errno, "fstat %s failed",
454 full_fname(fnamecmp));
Wayne Davison8ed9d842004-07-19 17:05:01 +0000455 discard_receive_data(f_in, file->length);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000456 close(fd1);
457 continue;
458 }
459
J.W. Schultz350e4e42003-09-04 05:49:50 +0000460 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
461 /* this special handling for directories
462 * wouldn't be necessary if robust_rename()
463 * and the underlying robust_unlink could cope
464 * with directories
465 */
Wayne Davisonea425412003-09-11 04:53:05 +0000466 rprintf(FERROR,"recv_files: %s is a directory\n",
467 full_fname(fnamecmp));
Wayne Davison8ed9d842004-07-19 17:05:01 +0000468 discard_receive_data(f_in, file->length);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000469 close(fd1);
470 continue;
471 }
472
J.W. Schultz350e4e42003-09-04 05:49:50 +0000473 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
474 close(fd1);
475 fd1 = -1;
J.W. Schultz350e4e42003-09-04 05:49:50 +0000476 }
477
Andrew Tridgell4df9f361999-10-31 04:28:03 +0000478 if (fd1 != -1 && !preserve_perms) {
J.W. Schultz85ed0aa2003-03-21 07:27:31 +0000479 /* if the file exists already and we aren't preserving
Wayne Davison17fadf72003-12-15 08:14:27 +0000480 * permissions then act as though the remote end sent
481 * us the file permissions we already have */
Andrew Tridgell4df9f361999-10-31 04:28:03 +0000482 file->mode = st.st_mode;
483 }
484
Wayne Davisona3221d22004-07-16 20:06:24 +0000485 /* We now check to see if we are writing file "inplace" */
486 if (inplace) {
487 fd2 = do_open(fnamecmp, O_WRONLY|O_CREAT, 0);
488 if (fd2 == -1) {
489 rsyserr(FERROR, errno, "open %s failed",
490 full_fname(fnamecmp));
Wayne Davison8ed9d842004-07-19 17:05:01 +0000491 discard_receive_data(f_in, file->length);
Wayne Davisona3221d22004-07-16 20:06:24 +0000492 if (fd1 != -1)
493 close(fd1);
494 continue;
495 }
496 } else {
497 if (!get_tmpname(fnametmp,fname)) {
Wayne Davison8ed9d842004-07-19 17:05:01 +0000498 discard_receive_data(f_in, file->length);
Wayne Davisona3221d22004-07-16 20:06:24 +0000499 if (fd1 != -1)
500 close(fd1);
501 continue;
502 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000503
Wayne Davisona3221d22004-07-16 20:06:24 +0000504 strlcpy(template, fnametmp, sizeof template);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000505
Wayne Davisona3221d22004-07-16 20:06:24 +0000506 /* we initially set the perms without the
507 * setuid/setgid bits to ensure that there is no race
508 * condition. They are then correctly updated after
509 * the lchown. Thanks to snabb@epipe.fi for pointing
510 * this out. We also set it initially without group
511 * access because of a similar race condition. */
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000512 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
Wayne Davison17fadf72003-12-15 08:14:27 +0000513
Wayne Davisona3221d22004-07-16 20:06:24 +0000514 /* in most cases parent directories will already exist
515 * because their information should have been previously
516 * transferred, but that may not be the case with -R */
517 if (fd2 == -1 && relative_paths && errno == ENOENT
518 && create_directory_path(fnametmp, orig_umask) == 0) {
519 strlcpy(fnametmp, template, sizeof fnametmp);
520 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
521 }
522 if (fd2 == -1) {
523 rsyserr(FERROR, errno, "mkstemp %s failed",
524 full_fname(fnametmp));
Wayne Davison8ed9d842004-07-19 17:05:01 +0000525 discard_receive_data(f_in, file->length);
Wayne Davisona3221d22004-07-16 20:06:24 +0000526 if (fd1 != -1)
527 close(fd1);
528 continue;
529 }
530
Wayne Davison7e5fa372004-07-20 21:35:58 +0000531 cleanup_set(fnametmp, fname, file, fd1, fd2);
Wayne Davisona3221d22004-07-16 20:06:24 +0000532 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000533
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000534 if (!am_server && verbose) /* log the transfer */
535 rprintf(FINFO, "%s\n", safe_fname(fname));
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000536
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000537 /* recv file data */
Wayne Davison7e5fa372004-07-20 21:35:58 +0000538 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
539 fname, fd2, file->length);
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000540
541 log_recv(file, &initial_stats);
Wayne Davison17fadf72003-12-15 08:14:27 +0000542
Wayne Davisond2a918b2004-07-02 18:23:57 +0000543 if (fd1 != -1)
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000544 close(fd1);
Wayne Davison9f27cd82004-04-27 19:51:33 +0000545 if (close(fd2) < 0) {
Wayne Davisond62bcc12004-05-15 19:31:10 +0000546 rsyserr(FERROR, errno, "close failed on %s",
547 full_fname(fnametmp));
Wayne Davison9f27cd82004-04-27 19:51:33 +0000548 exit_cleanup(RERR_FILEIO);
549 }
Wayne Davison17fadf72003-12-15 08:14:27 +0000550
Wayne Davison9bccfc42004-07-22 15:31:06 +0000551 if (recv_ok || keep_partial || inplace)
Wayne Davison55e50d82004-05-13 07:08:22 +0000552 finish_transfer(fname, fnametmp, file, recv_ok);
553 else
554 do_unlink(fnametmp);
Andrew Tridgellc6b81a91998-09-09 06:23:27 +0000555
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000556 cleanup_disable();
Andrew Tridgell554e0a82000-01-23 07:36:56 +0000557
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000558 if (!recv_ok) {
Wayne Davison007e3c02004-07-22 08:16:35 +0000559 int msgtype = csum_length == SUM_LENGTH || read_batch ?
560 FERROR : FINFO;
561 if (msgtype == FERROR || verbose) {
562 char *errstr, *redostr;
563 char *keptstr = keep_partial || inplace ?
564 "retain" : "discard";
565 if (msgtype == FERROR) {
566 errstr = "ERROR";
567 redostr = "";
568 } else {
569 errstr = "WARNING";
570 redostr = " (will try again)";
571 }
572 rprintf(msgtype,
573 "%s: %s failed verification -- update %sed%s.\n",
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000574 errstr, safe_fname(fname),
575 keptstr, redostr);
Wayne Davison007e3c02004-07-22 08:16:35 +0000576 }
Wayne Davisone2bc4122004-07-22 04:15:18 +0000577 if (csum_length != SUM_LENGTH) {
Wayne Davison0569a132004-01-15 07:42:25 +0000578 char buf[4];
Wayne Davison0569a132004-01-15 07:42:25 +0000579 SIVAL(buf, 0, i);
580 send_msg(MSG_REDO, buf, 4);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000581 }
582 }
583 }
Wayne Davison4e834af2004-06-14 15:09:36 +0000584 make_backups = save_make_backups;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000585
Wayne Davisone76ca142004-07-22 02:52:57 +0000586 if (delete_after && recurse && !local_name && flist->count > 0)
Wayne Davison3f55bd52004-01-08 00:45:41 +0000587 delete_files(flist);
Andrew Tridgell57df1711999-11-08 13:03:05 +0000588
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000589 if (verbose > 2)
590 rprintf(FINFO,"recv_files finished\n");
Wayne Davison17fadf72003-12-15 08:14:27 +0000591
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000592 return 0;
593}