blob: ac19f9c7530a70e2e0f5c8dcfe23aaf121acfd0a [file] [log] [blame]
Wayne Davison0f78b812006-04-25 20:23:34 +00001/*
2 * Routines only used by the receiving process.
3 *
4 * Copyright (C) 1996-2000 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
Wayne Davisonba2133d2007-02-04 14:54:58 +00006 * Copyright (C) 2003-2007 Wayne Davison
Wayne Davison0f78b812006-04-25 20:23:34 +00007 *
8 * This program is free software; you can redistribute it and/or modify
Wayne Davison8e41b682007-07-10 13:55:49 +00009 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
Wayne Davison0f78b812006-04-25 20:23:34 +000012 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
Wayne Davisone7c67062006-04-25 23:51:12 +000018 * You should have received a copy of the GNU General Public License along
Wayne Davison4fd842f2007-07-07 05:33:14 +000019 * with this program; if not, visit the http://fsf.org website.
Wayne Davison0f78b812006-04-25 20:23:34 +000020 */
Andrew Tridgell2f03f951998-07-25 02:25:22 +000021
22#include "rsync.h"
23
24extern int verbose;
Wayne Davison16edf862007-04-07 17:22:25 +000025extern int dry_run;
Wayne Davisona0009fc2005-04-09 18:59:55 +000026extern int do_xfers;
Wayne Davison8715db22005-02-20 00:16:23 +000027extern int am_server;
28extern int do_progress;
Wayne Davison3ea6e0e2007-01-27 14:56:09 +000029extern int inc_recurse;
Wayne Davison910e89b2005-02-16 08:10:45 +000030extern int log_before_transfer;
Wayne Davison2fedf3d2006-05-29 22:57:01 +000031extern int stdout_format_has_i;
Wayne Davisonecc76232006-05-09 18:31:06 +000032extern int logfile_format_has_i;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000033extern int csum_length;
Wayne Davison16cc9ca2004-07-21 23:59:37 +000034extern int read_batch;
Wayne Davisona0009fc2005-04-09 18:59:55 +000035extern int write_batch;
Wayne Davison16cc9ca2004-07-21 23:59:37 +000036extern int batch_gen_fd;
Wayne Davison41cfde62004-11-03 20:30:31 +000037extern int protocol_version;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000038extern int relative_paths;
Wayne Davison744e63f2007-01-27 14:37:53 +000039extern int preserve_hard_links;
Wayne Davison92b9eb92004-03-23 16:50:40 +000040extern int preserve_perms;
Wayne Davison16edf862007-04-07 17:22:25 +000041extern int preserve_xattrs;
Wayne Davisonc56595d2005-01-15 21:14:27 +000042extern int basis_dir_cnt;
David Dykstra53dd3131998-11-24 19:01:24 +000043extern int make_backups;
Wayne Davison925c5172004-01-02 08:38:35 +000044extern int cleanup_got_literal;
Wayne Davison744e63f2007-01-27 14:37:53 +000045extern int remove_source_files;
Wayne Davison6cc11982005-07-28 01:46:12 +000046extern int append_mode;
Wayne Davisone7ee91d2005-08-30 02:58:42 +000047extern int sparse_files;
Wayne Davison55e50d82004-05-13 07:08:22 +000048extern int keep_partial;
Wayne Davisonba582f72004-05-21 08:27:04 +000049extern int checksum_seed;
Wayne Davisona3221d22004-07-16 20:06:24 +000050extern int inplace;
Wayne Davison48e1c8c2005-01-27 22:46:36 +000051extern int delay_updates;
Wayne Davison1c3344a2007-03-11 00:13:34 +000052extern mode_t orig_umask;
Wayne Davison8715db22005-02-20 00:16:23 +000053extern struct stats stats;
Wayne Davison8715db22005-02-20 00:16:23 +000054extern char *tmpdir;
55extern char *partial_dir;
56extern char *basis_dir[];
Wayne Davison6755a7d2007-07-08 06:25:47 +000057extern struct file_list *cur_flist, *first_flist, *dir_flist;
Wayne Davison78424182005-01-25 10:39:14 +000058extern struct filter_list_struct server_filter_list;
Wayne Davison5ebab6c2004-07-19 16:37:30 +000059
Wayne Davison10f994a2006-02-07 18:15:51 +000060static struct bitbag *delayed_bits = NULL;
Wayne Davisond0221f12007-01-21 14:51:52 +000061static int phase = 0, redoing = 0;
Wayne Davison48459ba2006-09-17 23:28:19 +000062/* We're either updating the basis file or an identical copy: */
63static int updating_basis;
Wayne Davison3e7934a2005-02-20 20:55:24 +000064
J.W. Schultz52d3e102003-03-26 11:04:14 +000065/*
66 * get_tmpname() - create a tmp filename for a given filename
67 *
68 * If a tmpdir is defined, use that as the directory to
69 * put it in. Otherwise, the tmp filename is in the same
70 * directory as the given name. Note that there may be no
71 * directory at all in the given name!
Wayne Davison17fadf72003-12-15 08:14:27 +000072 *
J.W. Schultz52d3e102003-03-26 11:04:14 +000073 * The tmp filename is basically the given filename with a
74 * dot prepended, and .XXXXXX appended (for mkstemp() to
75 * put its unique gunk in). Take care to not exceed
76 * either the MAXPATHLEN or NAME_MAX, esp. the last, as
77 * the basename basically becomes 8 chars longer. In that
78 * case, the original name is shortened sufficiently to
79 * make it all fit.
Wayne Davison17fadf72003-12-15 08:14:27 +000080 *
J.W. Schultz52d3e102003-03-26 11:04:14 +000081 * Of course, there's no real reason for the tmp name to
82 * look like the original, except to satisfy us humans.
83 * As long as it's unique, rsync will work.
84 */
85
Wayne Davisonb20830b2006-11-22 20:48:04 +000086int get_tmpname(char *fnametmp, char *fname)
Andrew Tridgell2f03f951998-07-25 02:25:22 +000087{
Wayne Davisond5dcb6f2006-10-13 07:18:29 +000088 int maxname, added, length = 0;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000089 char *f;
90
Andrew Tridgell2f03f951998-07-25 02:25:22 +000091 if (tmpdir) {
Wayne Davisona24639b2004-01-22 04:40:33 +000092 /* Note: this can't overflow, so the return value is safe */
J.W. Schultzb7cee942004-01-20 03:37:04 +000093 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
J.W. Schultz52d3e102003-03-26 11:04:14 +000094 fnametmp[length++] = '/';
Wayne Davison0c2ef5f2003-08-04 21:00:57 +000095 }
J.W. Schultz52d3e102003-03-26 11:04:14 +000096
Wayne Davison0c2ef5f2003-08-04 21:00:57 +000097 if ((f = strrchr(fname, '/')) != NULL) {
J.W. Schultz52d3e102003-03-26 11:04:14 +000098 ++f;
99 if (!tmpdir) {
100 length = f - fname;
Wayne Davison0c2ef5f2003-08-04 21:00:57 +0000101 /* copy up to and including the slash */
J.W. Schultz52d3e102003-03-26 11:04:14 +0000102 strlcpy(fnametmp, fname, length + 1);
Wayne Davison0c2ef5f2003-08-04 21:00:57 +0000103 }
Wayne Davison446e2392004-01-02 08:18:53 +0000104 } else
J.W. Schultz52d3e102003-03-26 11:04:14 +0000105 f = fname;
J.W. Schultz52d3e102003-03-26 11:04:14 +0000106 fnametmp[length++] = '.';
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000107
Wayne Davisond5dcb6f2006-10-13 07:18:29 +0000108 /* The maxname value is bufsize, and includes space for the '\0'.
109 * (Note that NAME_MAX get -8 for the leading '.' above.) */
J.W. Schultz52d3e102003-03-26 11:04:14 +0000110 maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000111
Wayne Davison0c2ef5f2003-08-04 21:00:57 +0000112 if (maxname < 1) {
Wayne Davison45c49b52006-01-13 21:17:09 +0000113 rprintf(FERROR, "temporary filename too long: %s\n", fname);
J.W. Schultz52d3e102003-03-26 11:04:14 +0000114 fnametmp[0] = '\0';
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000115 return 0;
116 }
117
Wayne Davisond5dcb6f2006-10-13 07:18:29 +0000118 added = strlcpy(fnametmp + length, f, maxname);
119 if (added >= maxname)
120 added = maxname - 1;
121 memcpy(fnametmp + length + added, ".XXXXXX", 8);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000122
123 return 1;
124}
125
126
Wayne Davison7e5fa372004-07-20 21:35:58 +0000127static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
Wayne Davison55edf182007-03-18 06:20:07 +0000128 const char *fname, int fd, OFF_T total_size)
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000129{
Wayne Davisona0456b92007-03-18 06:00:53 +0000130 static char file_sum1[MAX_DIGEST_LEN];
131 static char file_sum2[MAX_DIGEST_LEN];
Wayne Davison7e5fa372004-07-20 21:35:58 +0000132 struct map_struct *mapbuf;
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000133 struct sum_struct sum;
Wayne Davisona0456b92007-03-18 06:00:53 +0000134 int32 len, sum_len;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000135 OFF_T offset = 0;
Wayne Davison89e540e2004-07-29 06:59:30 +0000136 OFF_T offset2;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000137 char *data;
Wayne Davison6c495e02005-01-01 21:08:17 +0000138 int32 i;
Wayne Davisone1f67412004-06-30 07:27:30 +0000139 char *map = NULL;
Wayne Davison17fadf72003-12-15 08:14:27 +0000140
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000141 read_sum_head(f_in, &sum);
Wayne Davison17fadf72003-12-15 08:14:27 +0000142
Wayne Davison7e5fa372004-07-20 21:35:58 +0000143 if (fd_r >= 0 && size_r > 0) {
Wayne Davison80264052005-01-17 22:51:27 +0000144 int32 read_size = MAX(sum.blength * 2, 16*1024);
145 mapbuf = map_file(fd_r, size_r, read_size, sum.blength);
Wayne Davison7e5fa372004-07-20 21:35:58 +0000146 if (verbose > 2) {
147 rprintf(FINFO, "recv mapped %s of size %.0f\n",
Wayne Davison45c49b52006-01-13 21:17:09 +0000148 fname_r, (double)size_r);
Wayne Davison7e5fa372004-07-20 21:35:58 +0000149 }
150 } else
151 mapbuf = NULL;
152
Wayne Davisonba582f72004-05-21 08:27:04 +0000153 sum_init(checksum_seed);
Wayne Davison17fadf72003-12-15 08:14:27 +0000154
Wayne Davisonf3d6d482006-12-28 07:54:31 +0000155 if (append_mode > 0) {
Wayne Davison6cc11982005-07-28 01:46:12 +0000156 OFF_T j;
157 sum.flength = (OFF_T)sum.count * sum.blength;
158 if (sum.remainder)
159 sum.flength -= sum.blength - sum.remainder;
Wayne Davison936fa862007-09-01 16:38:00 +0000160 if (append_mode == 2) {
161 for (j = CHUNK_SIZE; j < sum.flength; j += CHUNK_SIZE) {
162 if (do_progress)
163 show_progress(offset, total_size);
164 sum_update(map_ptr(mapbuf, offset, CHUNK_SIZE),
165 CHUNK_SIZE);
166 offset = j;
167 }
168 if (offset < sum.flength) {
169 int32 len = (int32)(sum.flength - offset);
170 if (do_progress)
171 show_progress(offset, total_size);
172 sum_update(map_ptr(mapbuf, offset, len), len);
173 }
Wayne Davison6cc11982005-07-28 01:46:12 +0000174 }
Wayne Davison936fa862007-09-01 16:38:00 +0000175 offset = sum.flength;
Wayne Davisonffa8ab82006-05-18 17:04:40 +0000176 if (fd != -1 && (j = do_lseek(fd, offset, SEEK_SET)) != offset) {
177 rsyserr(FERROR, errno, "lseek of %s returned %.0f, not %.0f",
178 full_fname(fname), (double)j, (double)offset);
Wayne Davison6cc11982005-07-28 01:46:12 +0000179 exit_cleanup(RERR_FILEIO);
180 }
181 }
182
Wayne Davison3f55bd52004-01-08 00:45:41 +0000183 while ((i = recv_token(f_in, &data)) != 0) {
Wayne Davison16417f82003-07-08 16:49:10 +0000184 if (do_progress)
185 show_progress(offset, total_size);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000186
187 if (i > 0) {
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000188 if (verbose > 3) {
Andrew Tridgell5f808df2000-01-23 12:30:34 +0000189 rprintf(FINFO,"data recv %d at %.0f\n",
190 i,(double)offset);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000191 }
192
193 stats.literal_data += i;
194 cleanup_got_literal = 1;
Wayne Davison17fadf72003-12-15 08:14:27 +0000195
Wayne Davison6c495e02005-01-01 21:08:17 +0000196 sum_update(data, i);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000197
Wayne Davisonc52461f2004-07-29 07:37:27 +0000198 if (fd != -1 && write_file(fd,data,i) != i)
199 goto report_write_error;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000200 offset += i;
201 continue;
Wayne Davison17fadf72003-12-15 08:14:27 +0000202 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000203
204 i = -(i+1);
Wayne Davison6c495e02005-01-01 21:08:17 +0000205 offset2 = i * (OFF_T)sum.blength;
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000206 len = sum.blength;
Wayne Davisond2a918b2004-07-02 18:23:57 +0000207 if (i == (int)sum.count-1 && sum.remainder != 0)
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000208 len = sum.remainder;
Wayne Davison17fadf72003-12-15 08:14:27 +0000209
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000210 stats.matched_data += len;
Wayne Davison17fadf72003-12-15 08:14:27 +0000211
Wayne Davison6c495e02005-01-01 21:08:17 +0000212 if (verbose > 3) {
213 rprintf(FINFO,
214 "chunk[%d] of size %ld at %.0f offset=%.0f\n",
215 i, (long)len, (double)offset2, (double)offset);
216 }
Wayne Davison17fadf72003-12-15 08:14:27 +0000217
Wayne Davison446e2392004-01-02 08:18:53 +0000218 if (mapbuf) {
219 map = map_ptr(mapbuf,offset2,len);
Wayne Davison17fadf72003-12-15 08:14:27 +0000220
Andrew Tridgellc55f7022000-01-24 11:41:08 +0000221 see_token(map, len);
Wayne Davison6c495e02005-01-01 21:08:17 +0000222 sum_update(map, len);
Andrew Tridgellc55f7022000-01-24 11:41:08 +0000223 }
Wayne Davison17fadf72003-12-15 08:14:27 +0000224
Wayne Davison48459ba2006-09-17 23:28:19 +0000225 if (updating_basis) {
Wayne Davison89e540e2004-07-29 06:59:30 +0000226 if (offset == offset2 && fd != -1) {
Wayne Davisonffa8ab82006-05-18 17:04:40 +0000227 OFF_T pos;
Wayne Davisonc52461f2004-07-29 07:37:27 +0000228 if (flush_write_file(fd) < 0)
229 goto report_write_error;
Wayne Davison89e540e2004-07-29 06:59:30 +0000230 offset += len;
Wayne Davisonffa8ab82006-05-18 17:04:40 +0000231 if ((pos = do_lseek(fd, len, SEEK_CUR)) != offset) {
Wayne Davisonfab65a52004-07-29 06:40:26 +0000232 rsyserr(FERROR, errno,
Wayne Davisonffa8ab82006-05-18 17:04:40 +0000233 "lseek of %s returned %.0f, not %.0f",
234 full_fname(fname),
235 (double)pos, (double)offset);
Wayne Davisonfab65a52004-07-29 06:40:26 +0000236 exit_cleanup(RERR_FILEIO);
237 }
Wayne Davison89e540e2004-07-29 06:59:30 +0000238 continue;
Wayne Davisona3221d22004-07-16 20:06:24 +0000239 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000240 }
Wayne Davison1ed91a02005-03-29 22:05:33 +0000241 if (fd != -1 && map && write_file(fd, map, len) != (int)len)
Wayne Davisonc52461f2004-07-29 07:37:27 +0000242 goto report_write_error;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000243 offset += len;
244 }
245
Wayne Davison85f14172004-12-02 17:16:19 +0000246 if (flush_write_file(fd) < 0)
247 goto report_write_error;
Wayne Davison76c21942004-01-02 08:29:49 +0000248
Wayne Davison4f5b0752005-02-14 00:53:43 +0000249#ifdef HAVE_FTRUNCATE
Wayne Davisonfab65a52004-07-29 06:40:26 +0000250 if (inplace && fd != -1)
Wayne Davisona3221d22004-07-16 20:06:24 +0000251 ftruncate(fd, offset);
252#endif
253
Wayne Davison16417f82003-07-08 16:49:10 +0000254 if (do_progress)
255 end_progress(total_size);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000256
257 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
Wayne Davisonc52461f2004-07-29 07:37:27 +0000258 report_write_error:
Wayne Davisond62bcc12004-05-15 19:31:10 +0000259 rsyserr(FERROR, errno, "write failed on %s",
260 full_fname(fname));
Andrew Tridgell65417571998-11-03 07:08:27 +0000261 exit_cleanup(RERR_FILEIO);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000262 }
263
Wayne Davisona0456b92007-03-18 06:00:53 +0000264 sum_len = sum_end(file_sum1);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000265
Wayne Davison7e5fa372004-07-20 21:35:58 +0000266 if (mapbuf)
267 unmap_file(mapbuf);
268
Wayne Davisona0456b92007-03-18 06:00:53 +0000269 read_buf(f_in, file_sum2, sum_len);
Wayne Davisond2a918b2004-07-02 18:23:57 +0000270 if (verbose > 2)
J.W. Schultzbc63ae32003-03-31 17:28:34 +0000271 rprintf(FINFO,"got file_sum\n");
Wayne Davisona0456b92007-03-18 06:00:53 +0000272 if (fd != -1 && memcmp(file_sum1, file_sum2, sum_len) != 0)
J.W. Schultzbc63ae32003-03-31 17:28:34 +0000273 return 0;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000274 return 1;
275}
276
277
Wayne Davison8ed9d842004-07-19 17:05:01 +0000278static void discard_receive_data(int f_in, OFF_T length)
279{
Wayne Davison7e5fa372004-07-20 21:35:58 +0000280 receive_data(f_in, NULL, -1, 0, NULL, -1, length);
Wayne Davison8ed9d842004-07-19 17:05:01 +0000281}
282
Wayne Davisonf3d6d482006-12-28 07:54:31 +0000283static void handle_delayed_updates(char *local_name)
Wayne Davison42be5322005-03-15 17:30:52 +0000284{
Wayne Davison663b2852006-12-04 02:07:37 +0000285 char *fname, *partialptr;
Wayne Davison03951302006-12-06 00:55:33 +0000286 int ndx;
Wayne Davison42be5322005-03-15 17:30:52 +0000287
Wayne Davison03951302006-12-06 00:55:33 +0000288 for (ndx = -1; (ndx = bitbag_next_bit(delayed_bits, ndx)) >= 0; ) {
Wayne Davisonf3d6d482006-12-28 07:54:31 +0000289 struct file_struct *file = cur_flist->files[ndx];
Wayne Davison6cbde572006-01-14 20:26:23 +0000290 fname = local_name ? local_name : f_name(file, NULL);
Wayne Davison42be5322005-03-15 17:30:52 +0000291 if ((partialptr = partial_dir_fname(fname)) != NULL) {
Wayne Davisonf3d6d482006-12-28 07:54:31 +0000292 if (make_backups > 0 && !make_backup(fname))
Wayne Davison42be5322005-03-15 17:30:52 +0000293 continue;
294 if (verbose > 2) {
295 rprintf(FINFO, "renaming %s to %s\n",
Wayne Davison45c49b52006-01-13 21:17:09 +0000296 partialptr, fname);
Wayne Davison42be5322005-03-15 17:30:52 +0000297 }
Wayne Davison6a94c582005-09-15 22:25:55 +0000298 /* We don't use robust_rename() here because the
299 * partial-dir must be on the same drive. */
Wayne Davison42be5322005-03-15 17:30:52 +0000300 if (do_rename(partialptr, fname) < 0) {
301 rsyserr(FERROR, errno,
302 "rename failed for %s (from %s)",
Wayne Davison45c49b52006-01-13 21:17:09 +0000303 full_fname(fname), partialptr);
Wayne Davison42be5322005-03-15 17:30:52 +0000304 } else {
Wayne Davison47c11972006-06-30 15:52:52 +0000305 if (remove_source_files
Wayne Davison112d7282006-12-05 15:59:58 +0000306 || (preserve_hard_links && F_IS_HLINKED(file)))
Wayne Davison03951302006-12-06 00:55:33 +0000307 send_msg_int(MSG_SUCCESS, ndx);
Wayne Davison45c49b52006-01-13 21:17:09 +0000308 handle_partial_dir(partialptr, PDIR_DELETE);
Wayne Davison42be5322005-03-15 17:30:52 +0000309 }
310 }
311 }
312}
313
Wayne Davisonf3d6d482006-12-28 07:54:31 +0000314static int get_next_gen_ndx(int fd, int next_gen_ndx, int desired_ndx)
Wayne Davison154cdaa2005-03-29 19:49:40 +0000315{
Wayne Davison03951302006-12-06 00:55:33 +0000316 while (next_gen_ndx < desired_ndx) {
317 if (next_gen_ndx >= 0) {
Wayne Davison154cdaa2005-03-29 19:49:40 +0000318 rprintf(FINFO,
Wayne Davison1ed91a02005-03-29 22:05:33 +0000319 "(No batched update for%s \"%s\")\n",
Wayne Davisond0221f12007-01-21 14:51:52 +0000320 redoing ? " resend of" : "",
Wayne Davisonf3d6d482006-12-28 07:54:31 +0000321 f_name(cur_flist->files[next_gen_ndx], NULL));
Wayne Davison154cdaa2005-03-29 19:49:40 +0000322 }
Wayne Davisonf3d6d482006-12-28 07:54:31 +0000323 next_gen_ndx = read_int(fd);
324 if (next_gen_ndx == -1) {
Wayne Davison3ea6e0e2007-01-27 14:56:09 +0000325 if (inc_recurse)
Wayne Davison9decb4d2007-07-07 20:22:05 +0000326 next_gen_ndx = first_flist->prev->used + first_flist->prev->ndx_start;
Wayne Davisonf3d6d482006-12-28 07:54:31 +0000327 else
Wayne Davison9decb4d2007-07-07 20:22:05 +0000328 next_gen_ndx = cur_flist->used;
Wayne Davisonf3d6d482006-12-28 07:54:31 +0000329 }
Wayne Davison154cdaa2005-03-29 19:49:40 +0000330 }
Wayne Davison03951302006-12-06 00:55:33 +0000331 return next_gen_ndx;
Wayne Davison154cdaa2005-03-29 19:49:40 +0000332}
333
Martin Poolb35d0d82002-04-08 04:10:20 +0000334/**
335 * main routine for receiver process.
336 *
337 * Receiver process runs on the same host as the generator process. */
Wayne Davisonf3d6d482006-12-28 07:54:31 +0000338int recv_files(int f_in, char *local_name)
Wayne Davison17fadf72003-12-15 08:14:27 +0000339{
Wayne Davison03951302006-12-06 00:55:33 +0000340 int next_gen_ndx = -1;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000341 int fd1,fd2;
342 STRUCT_STAT st;
Wayne Davisonb9232d42005-03-10 00:06:01 +0000343 int iflags, xlen;
Wayne Davison446e2392004-01-02 08:18:53 +0000344 char *fname, fbuf[MAXPATHLEN];
Wayne Davisonb9232d42005-03-10 00:06:01 +0000345 char xname[MAXPATHLEN];
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000346 char fnametmp[MAXPATHLEN];
Wayne Davison663b2852006-12-04 02:07:37 +0000347 char *fnamecmp, *partialptr;
David Dykstra375a4551998-10-26 21:42:38 +0000348 char fnamecmpbuf[MAXPATHLEN];
Wayne Davison9e4a8d22005-03-09 18:54:16 +0000349 uchar fnamecmp_type;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000350 struct file_struct *file;
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000351 struct stats initial_stats;
Wayne Davison2fedf3d2006-05-29 22:57:01 +0000352 int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i;
353 enum logcode log_code = log_before_transfer ? FLOG : FINFO;
Wayne Davison1ed91a02005-03-29 22:05:33 +0000354 int max_phase = protocol_version >= 29 ? 2 : 1;
Wayne Davison1c3344a2007-03-11 00:13:34 +0000355 int dflt_perms = (ACCESSPERMS & ~orig_umask);
356#ifdef SUPPORT_ACLS
357 const char *parent_dirname = "";
358#endif
Wayne Davison03951302006-12-06 00:55:33 +0000359 int ndx, recv_ok;
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000360
Wayne Davisond2a918b2004-07-02 18:23:57 +0000361 if (verbose > 2)
Wayne Davison9decb4d2007-07-07 20:22:05 +0000362 rprintf(FINFO, "recv_files(%d) starting\n", cur_flist->used);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000363
Wayne Davison3e7934a2005-02-20 20:55:24 +0000364 if (delay_updates)
Wayne Davison9decb4d2007-07-07 20:22:05 +0000365 delayed_bits = bitbag_create(cur_flist->used + 1);
Wayne Davison48e1c8c2005-01-27 22:46:36 +0000366
Wayne Davison48459ba2006-09-17 23:28:19 +0000367 updating_basis = inplace;
368
Wayne Davison17fadf72003-12-15 08:14:27 +0000369 while (1) {
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000370 cleanup_disable();
371
Wayne Davisonf3d6d482006-12-28 07:54:31 +0000372 /* This call also sets cur_flist. */
Wayne Davison16edf862007-04-07 17:22:25 +0000373 ndx = read_ndx_and_attrs(f_in, &iflags, &fnamecmp_type,
374 xname, &xlen);
Wayne Davison03951302006-12-06 00:55:33 +0000375 if (ndx == NDX_DONE) {
Wayne Davison3ea6e0e2007-01-27 14:56:09 +0000376 if (inc_recurse && first_flist) {
Wayne Davisonf3d6d482006-12-28 07:54:31 +0000377 flist_free(first_flist);
378 if (first_flist)
379 continue;
380 }
381 if (read_batch && cur_flist) {
Wayne Davison3ea6e0e2007-01-27 14:56:09 +0000382 int high = inc_recurse
Wayne Davison9decb4d2007-07-07 20:22:05 +0000383 ? first_flist->prev->used + first_flist->prev->ndx_start
384 : cur_flist->used;
Wayne Davisonf3d6d482006-12-28 07:54:31 +0000385 get_next_gen_ndx(batch_gen_fd, next_gen_ndx, high);
Wayne Davison03951302006-12-06 00:55:33 +0000386 next_gen_ndx = -1;
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000387 }
Wayne Davisonac3f7b82005-03-15 19:19:41 +0000388 if (++phase > max_phase)
Wayne Davison4e834af2004-06-14 15:09:36 +0000389 break;
Wayne Davison4e834af2004-06-14 15:09:36 +0000390 if (verbose > 2)
391 rprintf(FINFO, "recv_files phase=%d\n", phase);
Wayne Davisonac3f7b82005-03-15 19:19:41 +0000392 if (phase == 2 && delay_updates)
Wayne Davisonf3d6d482006-12-28 07:54:31 +0000393 handle_delayed_updates(local_name);
Wayne Davison332cf6d2007-04-26 05:53:13 +0000394 send_msg(MSG_DONE, "", 0, 0);
Wayne Davison4e834af2004-06-14 15:09:36 +0000395 continue;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000396 }
397
Wayne Davison6755a7d2007-07-08 06:25:47 +0000398 if (ndx - cur_flist->ndx_start >= 0)
399 file = cur_flist->files[ndx - cur_flist->ndx_start];
400 else
401 file = dir_flist->files[cur_flist->parent_ndx];
Wayne Davison6cbde572006-01-14 20:26:23 +0000402 fname = local_name ? local_name : f_name(file, fbuf);
Wayne Davison227a9c42005-02-19 22:16:47 +0000403
404 if (verbose > 2)
Wayne Davison45c49b52006-01-13 21:17:09 +0000405 rprintf(FINFO, "recv_files(%s)\n", fname);
Wayne Davison910e89b2005-02-16 08:10:45 +0000406
Wayne Davison16edf862007-04-07 17:22:25 +0000407#ifdef SUPPORT_XATTRS
408 if (iflags & ITEM_REPORT_XATTR && !dry_run)
409 recv_xattr_request(file, f_in);
410#endif
411
Wayne Davison22907b62005-03-05 00:21:56 +0000412 if (!(iflags & ITEM_TRANSFER)) {
Wayne Davisonb9232d42005-03-10 00:06:01 +0000413 maybe_log_item(file, iflags, itemizing, xname);
Wayne Davison16edf862007-04-07 17:22:25 +0000414#ifdef SUPPORT_XATTRS
415 if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && !dry_run)
416 set_file_attrs(fname, file, NULL, fname, 0);
417#endif
Wayne Davison58a14ed2005-03-04 16:54:08 +0000418 continue;
419 }
Wayne Davisonac3f7b82005-03-15 19:19:41 +0000420 if (phase == 2) {
421 rprintf(FERROR,
422 "got transfer request in phase 2 [%s]\n",
423 who_am_i());
424 exit_cleanup(RERR_PROTOCOL);
425 }
Wayne Davison910e89b2005-02-16 08:10:45 +0000426
Wayne Davisonf3d6d482006-12-28 07:54:31 +0000427 if (file->flags & FLAG_FILE_SENT) {
428 if (csum_length == SHORT_SUM_LENGTH) {
429 if (keep_partial && !partial_dir)
430 make_backups = -make_backups; /* prevents double backup */
Wayne Davison936fa862007-09-01 16:38:00 +0000431 if (append_mode)
432 sparse_files = -sparse_files;
Wayne Davisonf3d6d482006-12-28 07:54:31 +0000433 append_mode = -append_mode;
Wayne Davisonf3d6d482006-12-28 07:54:31 +0000434 csum_length = SUM_LENGTH;
Wayne Davisond0221f12007-01-21 14:51:52 +0000435 redoing = 1;
Wayne Davisonf3d6d482006-12-28 07:54:31 +0000436 }
437 } else {
438 if (csum_length != SHORT_SUM_LENGTH) {
439 if (keep_partial && !partial_dir)
440 make_backups = -make_backups;
Wayne Davison936fa862007-09-01 16:38:00 +0000441 if (append_mode)
442 sparse_files = -sparse_files;
Wayne Davisonf3d6d482006-12-28 07:54:31 +0000443 append_mode = -append_mode;
Wayne Davisonf3d6d482006-12-28 07:54:31 +0000444 csum_length = SHORT_SUM_LENGTH;
Wayne Davisond0221f12007-01-21 14:51:52 +0000445 redoing = 0;
Wayne Davisonf3d6d482006-12-28 07:54:31 +0000446 }
447 }
448
Wayne Davison03951302006-12-06 00:55:33 +0000449 stats.current_file_index = ndx;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000450 stats.num_transferred_files++;
Wayne Davison112d7282006-12-05 15:59:58 +0000451 stats.total_transferred_size += F_LENGTH(file);
Wayne Davison925c5172004-01-02 08:38:35 +0000452 cleanup_got_literal = 0;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000453
Wayne Davison8c2ffaf2005-02-03 00:19:40 +0000454 if (server_filter_list.head
455 && check_filter(&server_filter_list, fname, 0) < 0) {
456 rprintf(FERROR, "attempt to hack rsync failed.\n");
457 exit_cleanup(RERR_PROTOCOL);
458 }
459
Wayne Davisona0009fc2005-04-09 18:59:55 +0000460 if (!do_xfers) { /* log the transfer */
Wayne Davison85909932006-06-01 08:04:50 +0000461 log_item(FCLIENT, file, &stats, iflags, NULL);
Wayne Davisonf957e8f2005-03-24 16:38:34 +0000462 if (read_batch)
Wayne Davison112d7282006-12-05 15:59:58 +0000463 discard_receive_data(f_in, F_LENGTH(file));
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000464 continue;
465 }
Wayne Davisona0009fc2005-04-09 18:59:55 +0000466 if (write_batch < 0) {
Wayne Davison2fedf3d2006-05-29 22:57:01 +0000467 log_item(FINFO, file, &stats, iflags, NULL);
Wayne Davisonb10917a2005-04-14 01:42:13 +0000468 if (!am_server)
Wayne Davison112d7282006-12-05 15:59:58 +0000469 discard_receive_data(f_in, F_LENGTH(file));
Wayne Davisona0009fc2005-04-09 18:59:55 +0000470 continue;
471 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000472
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000473 if (read_batch) {
Wayne Davison03951302006-12-06 00:55:33 +0000474 next_gen_ndx = get_next_gen_ndx(batch_gen_fd, next_gen_ndx, ndx);
475 if (ndx < next_gen_ndx) {
Wayne Davison45c49b52006-01-13 21:17:09 +0000476 rprintf(FINFO,
477 "(Skipping batched update for \"%s\")\n",
478 fname);
Wayne Davison112d7282006-12-05 15:59:58 +0000479 discard_receive_data(f_in, F_LENGTH(file));
Wayne Davisona4306912007-04-26 23:06:38 +0000480 if (inc_recurse)
481 send_msg_int(MSG_NO_SEND, ndx);
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000482 continue;
483 }
Wayne Davison03951302006-12-06 00:55:33 +0000484 next_gen_ndx = -1;
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000485 }
486
Wayne Davison41cfde62004-11-03 20:30:31 +0000487 partialptr = partial_dir ? partial_dir_fname(fname) : fname;
Wayne Davisona7260c42004-07-29 16:06:38 +0000488
Wayne Davison9e4a8d22005-03-09 18:54:16 +0000489 if (protocol_version >= 29) {
490 switch (fnamecmp_type) {
Wayne Davison41cfde62004-11-03 20:30:31 +0000491 case FNAMECMP_FNAME:
492 fnamecmp = fname;
493 break;
494 case FNAMECMP_PARTIAL_DIR:
Wayne Davison9e4a8d22005-03-09 18:54:16 +0000495 fnamecmp = partialptr;
Wayne Davison41cfde62004-11-03 20:30:31 +0000496 break;
497 case FNAMECMP_BACKUP:
498 fnamecmp = get_backup_name(fname);
499 break;
Wayne Davison73273072005-02-14 02:41:30 +0000500 case FNAMECMP_FUZZY:
Wayne Davison48459ba2006-09-17 23:28:19 +0000501 updating_basis = 0;
Wayne Davisonb9232d42005-03-10 00:06:01 +0000502 if (file->dirname) {
503 pathjoin(fnamecmpbuf, MAXPATHLEN,
504 file->dirname, xname);
505 fnamecmp = fnamecmpbuf;
506 } else
507 fnamecmp = xname;
Wayne Davison73273072005-02-14 02:41:30 +0000508 break;
Wayne Davison41cfde62004-11-03 20:30:31 +0000509 default:
Wayne Davison48459ba2006-09-17 23:28:19 +0000510 updating_basis = 0;
Wayne Davison9e4a8d22005-03-09 18:54:16 +0000511 if (fnamecmp_type >= basis_dir_cnt) {
Wayne Davisonc56595d2005-01-15 21:14:27 +0000512 rprintf(FERROR,
513 "invalid basis_dir index: %d.\n",
Wayne Davison9e4a8d22005-03-09 18:54:16 +0000514 fnamecmp_type);
Wayne Davisonc56595d2005-01-15 21:14:27 +0000515 exit_cleanup(RERR_PROTOCOL);
516 }
Wayne Davison41cfde62004-11-03 20:30:31 +0000517 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
Wayne Davison9e4a8d22005-03-09 18:54:16 +0000518 basis_dir[fnamecmp_type], fname);
Wayne Davison41cfde62004-11-03 20:30:31 +0000519 fnamecmp = fnamecmpbuf;
520 break;
521 }
Wayne Davison9e4a8d22005-03-09 18:54:16 +0000522 if (!fnamecmp || (server_filter_list.head
523 && check_filter(&server_filter_list, fname, 0) < 0))
524 fnamecmp = fname;
525 } else {
526 /* Reminder: --inplace && --partial-dir are never
527 * enabled at the same time. */
Wayne Davisonf3d6d482006-12-28 07:54:31 +0000528 if (inplace && make_backups > 0) {
Wayne Davison9e4a8d22005-03-09 18:54:16 +0000529 if (!(fnamecmp = get_backup_name(fname)))
530 fnamecmp = fname;
531 } else if (partial_dir && partialptr)
532 fnamecmp = partialptr;
533 else
534 fnamecmp = fname;
535 }
Wayne Davisondc55d7b2004-09-07 21:34:26 +0000536
Wayne Davison910e89b2005-02-16 08:10:45 +0000537 initial_stats = stats;
538
Wayne Davison17fadf72003-12-15 08:14:27 +0000539 /* open the file */
Andrew Tridgell8c9fd201999-10-25 22:04:09 +0000540 fd1 = do_open(fnamecmp, O_RDONLY, 0);
David Dykstra375a4551998-10-26 21:42:38 +0000541
Wayne Davison9e4a8d22005-03-09 18:54:16 +0000542 if (fd1 == -1 && protocol_version < 29) {
543 if (fnamecmp != fname) {
544 fnamecmp = fname;
545 fd1 = do_open(fnamecmp, O_RDONLY, 0);
546 }
547
548 if (fd1 == -1 && basis_dir[0]) {
549 /* pre-29 allowed only one alternate basis */
550 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
551 basis_dir[0], fname);
552 fnamecmp = fnamecmpbuf;
553 fd1 = do_open(fnamecmp, O_RDONLY, 0);
554 }
555 }
556
Wayne Davisond5dcb6f2006-10-13 07:18:29 +0000557 if (fd1 == -1) {
558 st.st_mode = 0;
559 st.st_size = 0;
560 } else if (do_fstat(fd1,&st) != 0) {
Wayne Davisond62bcc12004-05-15 19:31:10 +0000561 rsyserr(FERROR, errno, "fstat %s failed",
562 full_fname(fnamecmp));
Wayne Davison112d7282006-12-05 15:59:58 +0000563 discard_receive_data(f_in, F_LENGTH(file));
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000564 close(fd1);
Wayne Davisona4306912007-04-26 23:06:38 +0000565 if (inc_recurse)
566 send_msg_int(MSG_NO_SEND, ndx);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000567 continue;
568 }
569
J.W. Schultz350e4e42003-09-04 05:49:50 +0000570 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
571 /* this special handling for directories
572 * wouldn't be necessary if robust_rename()
573 * and the underlying robust_unlink could cope
574 * with directories
575 */
Wayne Davisonea425412003-09-11 04:53:05 +0000576 rprintf(FERROR,"recv_files: %s is a directory\n",
577 full_fname(fnamecmp));
Wayne Davison112d7282006-12-05 15:59:58 +0000578 discard_receive_data(f_in, F_LENGTH(file));
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000579 close(fd1);
Wayne Davisona4306912007-04-26 23:06:38 +0000580 if (inc_recurse)
581 send_msg_int(MSG_NO_SEND, ndx);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000582 continue;
583 }
584
J.W. Schultz350e4e42003-09-04 05:49:50 +0000585 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
586 close(fd1);
587 fd1 = -1;
J.W. Schultz350e4e42003-09-04 05:49:50 +0000588 }
589
Wayne Davisona9d6e6f2006-01-31 02:30:09 +0000590 /* If we're not preserving permissions, change the file-list's
591 * mode based on the local permissions and some heuristics. */
592 if (!preserve_perms) {
593 int exists = fd1 != -1;
Wayne Davison1c3344a2007-03-11 00:13:34 +0000594#ifdef SUPPORT_ACLS
595 const char *dn = file->dirname ? file->dirname : ".";
596 if (parent_dirname != dn
597 && strcmp(parent_dirname, dn) != 0) {
598 dflt_perms = default_perms_for_dir(dn);
599 parent_dirname = dn;
600 }
601#endif
602 file->mode = dest_mode(file->mode, st.st_mode,
603 dflt_perms, exists);
Andrew Tridgell4df9f361999-10-31 04:28:03 +0000604 }
605
Wayne Davison48459ba2006-09-17 23:28:19 +0000606 /* We now check to see if we are writing the file "inplace" */
Wayne Davisona3221d22004-07-16 20:06:24 +0000607 if (inplace) {
Wayne Davison97894c62005-07-30 16:14:22 +0000608 fd2 = do_open(fname, O_WRONLY|O_CREAT, 0600);
Wayne Davisona3221d22004-07-16 20:06:24 +0000609 if (fd2 == -1) {
610 rsyserr(FERROR, errno, "open %s failed",
Wayne Davisondc55d7b2004-09-07 21:34:26 +0000611 full_fname(fname));
Wayne Davison112d7282006-12-05 15:59:58 +0000612 discard_receive_data(f_in, F_LENGTH(file));
Wayne Davisona3221d22004-07-16 20:06:24 +0000613 if (fd1 != -1)
614 close(fd1);
Wayne Davisona4306912007-04-26 23:06:38 +0000615 if (inc_recurse)
616 send_msg_int(MSG_NO_SEND, ndx);
Wayne Davisona3221d22004-07-16 20:06:24 +0000617 continue;
618 }
619 } else {
620 if (!get_tmpname(fnametmp,fname)) {
Wayne Davison112d7282006-12-05 15:59:58 +0000621 discard_receive_data(f_in, F_LENGTH(file));
Wayne Davisona3221d22004-07-16 20:06:24 +0000622 if (fd1 != -1)
623 close(fd1);
Wayne Davisona4306912007-04-26 23:06:38 +0000624 if (inc_recurse)
625 send_msg_int(MSG_NO_SEND, ndx);
Wayne Davisona3221d22004-07-16 20:06:24 +0000626 continue;
627 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000628
Wayne Davisona3221d22004-07-16 20:06:24 +0000629 /* we initially set the perms without the
630 * setuid/setgid bits to ensure that there is no race
631 * condition. They are then correctly updated after
632 * the lchown. Thanks to snabb@epipe.fi for pointing
633 * this out. We also set it initially without group
634 * access because of a similar race condition. */
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000635 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
Wayne Davison17fadf72003-12-15 08:14:27 +0000636
Wayne Davisona3221d22004-07-16 20:06:24 +0000637 /* in most cases parent directories will already exist
638 * because their information should have been previously
639 * transferred, but that may not be the case with -R */
640 if (fd2 == -1 && relative_paths && errno == ENOENT
Wayne Davison904e5af2006-02-24 01:56:21 +0000641 && create_directory_path(fnametmp) == 0) {
Wayne Davisonb9232d42005-03-10 00:06:01 +0000642 /* Get back to name with XXXXXX in it. */
643 get_tmpname(fnametmp, fname);
Wayne Davisona3221d22004-07-16 20:06:24 +0000644 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
645 }
646 if (fd2 == -1) {
647 rsyserr(FERROR, errno, "mkstemp %s failed",
648 full_fname(fnametmp));
Wayne Davison112d7282006-12-05 15:59:58 +0000649 discard_receive_data(f_in, F_LENGTH(file));
Wayne Davisona3221d22004-07-16 20:06:24 +0000650 if (fd1 != -1)
651 close(fd1);
Wayne Davisona4306912007-04-26 23:06:38 +0000652 if (inc_recurse)
653 send_msg_int(MSG_NO_SEND, ndx);
Wayne Davisona3221d22004-07-16 20:06:24 +0000654 continue;
655 }
656
Wayne Davison95ae5222006-05-18 18:53:24 +0000657 cleanup_set(fnametmp, partialptr, file, fd1, fd2);
Wayne Davisona3221d22004-07-16 20:06:24 +0000658 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000659
Wayne Davisona8ed4952005-02-15 07:20:04 +0000660 /* log the transfer */
Wayne Davison910e89b2005-02-16 08:10:45 +0000661 if (log_before_transfer)
Wayne Davison85909932006-06-01 08:04:50 +0000662 log_item(FCLIENT, file, &initial_stats, iflags, NULL);
Wayne Davison4bb0af72005-02-16 17:02:13 +0000663 else if (!am_server && verbose && do_progress)
Wayne Davison45c49b52006-01-13 21:17:09 +0000664 rprintf(FINFO, "%s\n", fname);
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000665
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000666 /* recv file data */
Wayne Davison7e5fa372004-07-20 21:35:58 +0000667 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
Wayne Davison112d7282006-12-05 15:59:58 +0000668 fname, fd2, F_LENGTH(file));
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000669
Wayne Davison2fedf3d2006-05-29 22:57:01 +0000670 log_item(log_code, file, &initial_stats, iflags, NULL);
Wayne Davison17fadf72003-12-15 08:14:27 +0000671
Wayne Davisond2a918b2004-07-02 18:23:57 +0000672 if (fd1 != -1)
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000673 close(fd1);
Wayne Davison9f27cd82004-04-27 19:51:33 +0000674 if (close(fd2) < 0) {
Wayne Davisond62bcc12004-05-15 19:31:10 +0000675 rsyserr(FERROR, errno, "close failed on %s",
676 full_fname(fnametmp));
Wayne Davison9f27cd82004-04-27 19:51:33 +0000677 exit_cleanup(RERR_FILEIO);
678 }
Wayne Davison17fadf72003-12-15 08:14:27 +0000679
Wayne Davison68795c62005-02-11 09:56:28 +0000680 if ((recv_ok && (!delay_updates || !partialptr)) || inplace) {
Wayne Davison01d124d2006-05-09 17:38:47 +0000681 char *temp_copy_name;
682 if (partialptr == fname)
683 partialptr = temp_copy_name = NULL;
684 else if (*partial_dir == '/')
685 temp_copy_name = NULL;
686 else
687 temp_copy_name = partialptr;
Wayne Davison16edf862007-04-07 17:22:25 +0000688 finish_transfer(fname, fnametmp, fnamecmp,
689 temp_copy_name, file, recv_ok, 1);
Wayne Davison4a4d2b12006-01-30 07:18:10 +0000690 if (fnamecmp == partialptr) {
Wayne Davisonaec6b9f2005-01-10 10:03:10 +0000691 do_unlink(partialptr);
692 handle_partial_dir(partialptr, PDIR_DELETE);
693 }
694 } else if (keep_partial && partialptr
695 && handle_partial_dir(partialptr, PDIR_CREATE)) {
Wayne Davison16edf862007-04-07 17:22:25 +0000696 finish_transfer(partialptr, fnametmp, fnamecmp, NULL,
Wayne Davison4a4d2b12006-01-30 07:18:10 +0000697 file, recv_ok, !partial_dir);
Wayne Davison3e7934a2005-02-20 20:55:24 +0000698 if (delay_updates && recv_ok) {
Wayne Davison03951302006-12-06 00:55:33 +0000699 bitbag_set_bit(delayed_bits, ndx);
Wayne Davison3e7934a2005-02-20 20:55:24 +0000700 recv_ok = -1;
701 }
Wayne Davisonaec6b9f2005-01-10 10:03:10 +0000702 } else {
Wayne Davisona7260c42004-07-29 16:06:38 +0000703 partialptr = NULL;
Wayne Davison55e50d82004-05-13 07:08:22 +0000704 do_unlink(fnametmp);
Wayne Davisona7260c42004-07-29 16:06:38 +0000705 }
706
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000707 cleanup_disable();
Andrew Tridgell554e0a82000-01-23 07:36:56 +0000708
Wayne Davison22907b62005-03-05 00:21:56 +0000709 if (recv_ok > 0) {
Wayne Davison3ea6e0e2007-01-27 14:56:09 +0000710 if (remove_source_files || inc_recurse
Wayne Davison112d7282006-12-05 15:59:58 +0000711 || (preserve_hard_links && F_IS_HLINKED(file)))
Wayne Davison03951302006-12-06 00:55:33 +0000712 send_msg_int(MSG_SUCCESS, ndx);
Wayne Davison22907b62005-03-05 00:21:56 +0000713 } else if (!recv_ok) {
Wayne Davisond0221f12007-01-21 14:51:52 +0000714 enum logcode msgtype = redoing || read_batch ? FERROR : FINFO;
Wayne Davison007e3c02004-07-22 08:16:35 +0000715 if (msgtype == FERROR || verbose) {
Wayne Davisona7260c42004-07-29 16:06:38 +0000716 char *errstr, *redostr, *keptstr;
717 if (!(keep_partial && partialptr) && !inplace)
718 keptstr = "discarded";
719 else if (partial_dir)
720 keptstr = "put into partial-dir";
721 else
722 keptstr = "retained";
Wayne Davison007e3c02004-07-22 08:16:35 +0000723 if (msgtype == FERROR) {
724 errstr = "ERROR";
725 redostr = "";
726 } else {
727 errstr = "WARNING";
728 redostr = " (will try again)";
729 }
730 rprintf(msgtype,
Wayne Davisona7260c42004-07-29 16:06:38 +0000731 "%s: %s failed verification -- update %s%s.\n",
Wayne Davison45c49b52006-01-13 21:17:09 +0000732 errstr, fname, keptstr, redostr);
Wayne Davison007e3c02004-07-22 08:16:35 +0000733 }
Wayne Davisond0221f12007-01-21 14:51:52 +0000734 if (!redoing) {
Wayne Davison03951302006-12-06 00:55:33 +0000735 send_msg_int(MSG_REDO, ndx);
Wayne Davisonf3d6d482006-12-28 07:54:31 +0000736 file->flags |= FLAG_FILE_SENT;
Wayne Davison3ea6e0e2007-01-27 14:56:09 +0000737 } else if (inc_recurse)
Wayne Davisond0221f12007-01-21 14:51:52 +0000738 send_msg_int(MSG_NO_SEND, ndx);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000739 }
740 }
Wayne Davisonf3d6d482006-12-28 07:54:31 +0000741 if (make_backups < 0)
742 make_backups = -make_backups;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000743
Wayne Davisonac3f7b82005-03-15 19:19:41 +0000744 if (phase == 2 && delay_updates) /* for protocol_version < 29 */
Wayne Davisonf3d6d482006-12-28 07:54:31 +0000745 handle_delayed_updates(local_name);
Wayne Davison48e1c8c2005-01-27 22:46:36 +0000746
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000747 if (verbose > 2)
748 rprintf(FINFO,"recv_files finished\n");
Wayne Davison17fadf72003-12-15 08:14:27 +0000749
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000750 return 0;
751}