blob: 663b1c07a70809fb3d111f49060f6e9a43740633 [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
6 * Copyright (C) 2003, 2004, 2005, 2006 Wayne Davison
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
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 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
Andrew Tridgell2f03f951998-07-25 02:25:22 +000022
23#include "rsync.h"
24
25extern int verbose;
Wayne Davisona0009fc2005-04-09 18:59:55 +000026extern int do_xfers;
Wayne Davison8715db22005-02-20 00:16:23 +000027extern int am_daemon;
28extern int am_server;
29extern int do_progress;
Wayne Davison910e89b2005-02-16 08:10:45 +000030extern int log_before_transfer;
Wayne Davison227a9c42005-02-19 22:16:47 +000031extern int log_format_has_i;
32extern int daemon_log_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;
39extern int preserve_hard_links;
Wayne Davison92b9eb92004-03-23 16:50:40 +000040extern int preserve_perms;
Wayne Davisonc56595d2005-01-15 21:14:27 +000041extern int basis_dir_cnt;
David Dykstra53dd3131998-11-24 19:01:24 +000042extern int make_backups;
Wayne Davison925c5172004-01-02 08:38:35 +000043extern int cleanup_got_literal;
Wayne Davison3e870a62005-02-20 01:04:51 +000044extern int remove_sent_files;
Wayne Davison6cc11982005-07-28 01:46:12 +000045extern int append_mode;
Wayne Davisone7ee91d2005-08-30 02:58:42 +000046extern int sparse_files;
Wayne Davison55e50d82004-05-13 07:08:22 +000047extern int keep_partial;
Wayne Davisonba582f72004-05-21 08:27:04 +000048extern int checksum_seed;
Wayne Davisona3221d22004-07-16 20:06:24 +000049extern int inplace;
Wayne Davison48e1c8c2005-01-27 22:46:36 +000050extern int delay_updates;
Wayne Davison8715db22005-02-20 00:16:23 +000051extern struct stats stats;
Wayne Davison910e89b2005-02-16 08:10:45 +000052extern char *log_format;
Wayne Davison8715db22005-02-20 00:16:23 +000053extern char *tmpdir;
54extern char *partial_dir;
55extern char *basis_dir[];
Wayne Davison154cdaa2005-03-29 19:49:40 +000056extern struct file_list *the_file_list;
Wayne Davison78424182005-01-25 10:39:14 +000057extern struct filter_list_struct server_filter_list;
Wayne Davison5ebab6c2004-07-19 16:37:30 +000058
Wayne Davison10f994a2006-02-07 18:15:51 +000059static struct bitbag *delayed_bits = NULL;
Wayne Davison1ed91a02005-03-29 22:05:33 +000060static int phase = 0;
Wayne Davison3e7934a2005-02-20 20:55:24 +000061
Andrew Tridgell2f03f951998-07-25 02:25:22 +000062
J.W. Schultz52d3e102003-03-26 11:04:14 +000063/*
64 * get_tmpname() - create a tmp filename for a given filename
65 *
66 * If a tmpdir is defined, use that as the directory to
67 * put it in. Otherwise, the tmp filename is in the same
68 * directory as the given name. Note that there may be no
69 * directory at all in the given name!
Wayne Davison17fadf72003-12-15 08:14:27 +000070 *
J.W. Schultz52d3e102003-03-26 11:04:14 +000071 * The tmp filename is basically the given filename with a
72 * dot prepended, and .XXXXXX appended (for mkstemp() to
73 * put its unique gunk in). Take care to not exceed
74 * either the MAXPATHLEN or NAME_MAX, esp. the last, as
75 * the basename basically becomes 8 chars longer. In that
76 * case, the original name is shortened sufficiently to
77 * make it all fit.
Wayne Davison17fadf72003-12-15 08:14:27 +000078 *
J.W. Schultz52d3e102003-03-26 11:04:14 +000079 * Of course, there's no real reason for the tmp name to
80 * look like the original, except to satisfy us humans.
81 * As long as it's unique, rsync will work.
82 */
83
Andrew Tridgell2f03f951998-07-25 02:25:22 +000084static int get_tmpname(char *fnametmp, char *fname)
85{
86 char *f;
J.W. Schultz52d3e102003-03-26 11:04:14 +000087 int length = 0;
88 int maxname;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000089
Andrew Tridgell2f03f951998-07-25 02:25:22 +000090 if (tmpdir) {
Wayne Davisona24639b2004-01-22 04:40:33 +000091 /* Note: this can't overflow, so the return value is safe */
J.W. Schultzb7cee942004-01-20 03:37:04 +000092 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
J.W. Schultz52d3e102003-03-26 11:04:14 +000093 fnametmp[length++] = '/';
94 fnametmp[length] = '\0'; /* always NULL terminated */
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++] = '.';
107 fnametmp[length] = '\0'; /* always NULL terminated */
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000108
J.W. Schultz52d3e102003-03-26 11:04:14 +0000109 maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000110
Wayne Davison0c2ef5f2003-08-04 21:00:57 +0000111 if (maxname < 1) {
Wayne Davison45c49b52006-01-13 21:17:09 +0000112 rprintf(FERROR, "temporary filename too long: %s\n", fname);
J.W. Schultz52d3e102003-03-26 11:04:14 +0000113 fnametmp[0] = '\0';
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000114 return 0;
115 }
116
Wayne Davison17fadf72003-12-15 08:14:27 +0000117 strlcpy(fnametmp + length, f, maxname);
J.W. Schultz52d3e102003-03-26 11:04:14 +0000118 strcat(fnametmp + length, ".XXXXXX");
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000119
120 return 1;
121}
122
123
Wayne Davison7e5fa372004-07-20 21:35:58 +0000124static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
125 char *fname, int fd, OFF_T total_size)
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000126{
Wayne Davison7e5fa372004-07-20 21:35:58 +0000127 static char file_sum1[MD4_SUM_LENGTH];
128 static char file_sum2[MD4_SUM_LENGTH];
129 struct map_struct *mapbuf;
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000130 struct sum_struct sum;
Wayne Davison6c495e02005-01-01 21:08:17 +0000131 int32 len;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000132 OFF_T offset = 0;
Wayne Davison89e540e2004-07-29 06:59:30 +0000133 OFF_T offset2;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000134 char *data;
Wayne Davison6c495e02005-01-01 21:08:17 +0000135 int32 i;
Wayne Davisone1f67412004-06-30 07:27:30 +0000136 char *map = NULL;
Wayne Davison17fadf72003-12-15 08:14:27 +0000137
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000138 read_sum_head(f_in, &sum);
Wayne Davison17fadf72003-12-15 08:14:27 +0000139
Wayne Davison7e5fa372004-07-20 21:35:58 +0000140 if (fd_r >= 0 && size_r > 0) {
Wayne Davison80264052005-01-17 22:51:27 +0000141 int32 read_size = MAX(sum.blength * 2, 16*1024);
142 mapbuf = map_file(fd_r, size_r, read_size, sum.blength);
Wayne Davison7e5fa372004-07-20 21:35:58 +0000143 if (verbose > 2) {
144 rprintf(FINFO, "recv mapped %s of size %.0f\n",
Wayne Davison45c49b52006-01-13 21:17:09 +0000145 fname_r, (double)size_r);
Wayne Davison7e5fa372004-07-20 21:35:58 +0000146 }
147 } else
148 mapbuf = NULL;
149
Wayne Davisonba582f72004-05-21 08:27:04 +0000150 sum_init(checksum_seed);
Wayne Davison17fadf72003-12-15 08:14:27 +0000151
Wayne Davison6cc11982005-07-28 01:46:12 +0000152 if (append_mode) {
153 OFF_T j;
154 sum.flength = (OFF_T)sum.count * sum.blength;
155 if (sum.remainder)
156 sum.flength -= sum.blength - sum.remainder;
157 for (j = CHUNK_SIZE; j < sum.flength; j += CHUNK_SIZE) {
Wayne Davison97894c62005-07-30 16:14:22 +0000158 if (do_progress)
159 show_progress(offset, total_size);
Wayne Davison6cc11982005-07-28 01:46:12 +0000160 sum_update(map_ptr(mapbuf, offset, CHUNK_SIZE),
161 CHUNK_SIZE);
162 offset = j;
163 }
164 if (offset < sum.flength) {
165 int32 len = sum.flength - offset;
Wayne Davison97894c62005-07-30 16:14:22 +0000166 if (do_progress)
167 show_progress(offset, total_size);
Wayne Davison6cc11982005-07-28 01:46:12 +0000168 sum_update(map_ptr(mapbuf, offset, len), len);
169 offset = sum.flength;
170 }
171 if (fd != -1 && do_lseek(fd, offset, SEEK_SET) != offset) {
172 rsyserr(FERROR, errno, "lseek failed on %s",
173 full_fname(fname));
174 exit_cleanup(RERR_FILEIO);
175 }
176 }
177
Wayne Davison3f55bd52004-01-08 00:45:41 +0000178 while ((i = recv_token(f_in, &data)) != 0) {
Wayne Davison16417f82003-07-08 16:49:10 +0000179 if (do_progress)
180 show_progress(offset, total_size);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000181
182 if (i > 0) {
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000183 if (verbose > 3) {
Andrew Tridgell5f808df2000-01-23 12:30:34 +0000184 rprintf(FINFO,"data recv %d at %.0f\n",
185 i,(double)offset);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000186 }
187
188 stats.literal_data += i;
189 cleanup_got_literal = 1;
Wayne Davison17fadf72003-12-15 08:14:27 +0000190
Wayne Davison6c495e02005-01-01 21:08:17 +0000191 sum_update(data, i);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000192
Wayne Davisonc52461f2004-07-29 07:37:27 +0000193 if (fd != -1 && write_file(fd,data,i) != i)
194 goto report_write_error;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000195 offset += i;
196 continue;
Wayne Davison17fadf72003-12-15 08:14:27 +0000197 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000198
199 i = -(i+1);
Wayne Davison6c495e02005-01-01 21:08:17 +0000200 offset2 = i * (OFF_T)sum.blength;
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000201 len = sum.blength;
Wayne Davisond2a918b2004-07-02 18:23:57 +0000202 if (i == (int)sum.count-1 && sum.remainder != 0)
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000203 len = sum.remainder;
Wayne Davison17fadf72003-12-15 08:14:27 +0000204
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000205 stats.matched_data += len;
Wayne Davison17fadf72003-12-15 08:14:27 +0000206
Wayne Davison6c495e02005-01-01 21:08:17 +0000207 if (verbose > 3) {
208 rprintf(FINFO,
209 "chunk[%d] of size %ld at %.0f offset=%.0f\n",
210 i, (long)len, (double)offset2, (double)offset);
211 }
Wayne Davison17fadf72003-12-15 08:14:27 +0000212
Wayne Davison446e2392004-01-02 08:18:53 +0000213 if (mapbuf) {
214 map = map_ptr(mapbuf,offset2,len);
Wayne Davison17fadf72003-12-15 08:14:27 +0000215
Andrew Tridgellc55f7022000-01-24 11:41:08 +0000216 see_token(map, len);
Wayne Davison6c495e02005-01-01 21:08:17 +0000217 sum_update(map, len);
Andrew Tridgellc55f7022000-01-24 11:41:08 +0000218 }
Wayne Davison17fadf72003-12-15 08:14:27 +0000219
Wayne Davisonfab65a52004-07-29 06:40:26 +0000220 if (inplace) {
Wayne Davison89e540e2004-07-29 06:59:30 +0000221 if (offset == offset2 && fd != -1) {
Wayne Davisonc52461f2004-07-29 07:37:27 +0000222 if (flush_write_file(fd) < 0)
223 goto report_write_error;
Wayne Davison89e540e2004-07-29 06:59:30 +0000224 offset += len;
225 if (do_lseek(fd, len, SEEK_CUR) != offset) {
Wayne Davisonfab65a52004-07-29 06:40:26 +0000226 rsyserr(FERROR, errno,
227 "lseek failed on %s",
228 full_fname(fname));
229 exit_cleanup(RERR_FILEIO);
230 }
Wayne Davison89e540e2004-07-29 06:59:30 +0000231 continue;
Wayne Davisona3221d22004-07-16 20:06:24 +0000232 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000233 }
Wayne Davison1ed91a02005-03-29 22:05:33 +0000234 if (fd != -1 && map && write_file(fd, map, len) != (int)len)
Wayne Davisonc52461f2004-07-29 07:37:27 +0000235 goto report_write_error;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000236 offset += len;
237 }
238
Wayne Davison85f14172004-12-02 17:16:19 +0000239 if (flush_write_file(fd) < 0)
240 goto report_write_error;
Wayne Davison76c21942004-01-02 08:29:49 +0000241
Wayne Davison4f5b0752005-02-14 00:53:43 +0000242#ifdef HAVE_FTRUNCATE
Wayne Davisonfab65a52004-07-29 06:40:26 +0000243 if (inplace && fd != -1)
Wayne Davisona3221d22004-07-16 20:06:24 +0000244 ftruncate(fd, offset);
245#endif
246
Wayne Davison16417f82003-07-08 16:49:10 +0000247 if (do_progress)
248 end_progress(total_size);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000249
250 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
Wayne Davisonc52461f2004-07-29 07:37:27 +0000251 report_write_error:
Wayne Davisond62bcc12004-05-15 19:31:10 +0000252 rsyserr(FERROR, errno, "write failed on %s",
253 full_fname(fname));
Andrew Tridgell65417571998-11-03 07:08:27 +0000254 exit_cleanup(RERR_FILEIO);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000255 }
256
257 sum_end(file_sum1);
258
Wayne Davison7e5fa372004-07-20 21:35:58 +0000259 if (mapbuf)
260 unmap_file(mapbuf);
261
J.W. Schultzbc63ae32003-03-31 17:28:34 +0000262 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
Wayne Davisond2a918b2004-07-02 18:23:57 +0000263 if (verbose > 2)
J.W. Schultzbc63ae32003-03-31 17:28:34 +0000264 rprintf(FINFO,"got file_sum\n");
Wayne Davisond2a918b2004-07-02 18:23:57 +0000265 if (fd != -1 && memcmp(file_sum1, file_sum2, MD4_SUM_LENGTH) != 0)
J.W. Schultzbc63ae32003-03-31 17:28:34 +0000266 return 0;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000267 return 1;
268}
269
270
Wayne Davison8ed9d842004-07-19 17:05:01 +0000271static void discard_receive_data(int f_in, OFF_T length)
272{
Wayne Davison7e5fa372004-07-20 21:35:58 +0000273 receive_data(f_in, NULL, -1, 0, NULL, -1, length);
Wayne Davison8ed9d842004-07-19 17:05:01 +0000274}
275
Wayne Davison42be5322005-03-15 17:30:52 +0000276static void handle_delayed_updates(struct file_list *flist, char *local_name)
277{
278 char *fname, *partialptr, numbuf[4];
279 int i;
280
Wayne Davison10f994a2006-02-07 18:15:51 +0000281 for (i = -1; (i = bitbag_next_bit(delayed_bits, i)) >= 0; ) {
Wayne Davison42be5322005-03-15 17:30:52 +0000282 struct file_struct *file = flist->files[i];
Wayne Davison6cbde572006-01-14 20:26:23 +0000283 fname = local_name ? local_name : f_name(file, NULL);
Wayne Davison42be5322005-03-15 17:30:52 +0000284 if ((partialptr = partial_dir_fname(fname)) != NULL) {
285 if (make_backups && !make_backup(fname))
286 continue;
287 if (verbose > 2) {
288 rprintf(FINFO, "renaming %s to %s\n",
Wayne Davison45c49b52006-01-13 21:17:09 +0000289 partialptr, fname);
Wayne Davison42be5322005-03-15 17:30:52 +0000290 }
Wayne Davison6a94c582005-09-15 22:25:55 +0000291 /* We don't use robust_rename() here because the
292 * partial-dir must be on the same drive. */
Wayne Davison42be5322005-03-15 17:30:52 +0000293 if (do_rename(partialptr, fname) < 0) {
294 rsyserr(FERROR, errno,
295 "rename failed for %s (from %s)",
Wayne Davison45c49b52006-01-13 21:17:09 +0000296 full_fname(fname), partialptr);
Wayne Davison42be5322005-03-15 17:30:52 +0000297 } else {
298 if (remove_sent_files
299 || (preserve_hard_links
300 && file->link_u.links)) {
301 SIVAL(numbuf, 0, i);
302 send_msg(MSG_SUCCESS,numbuf,4);
303 }
Wayne Davison45c49b52006-01-13 21:17:09 +0000304 handle_partial_dir(partialptr, PDIR_DELETE);
Wayne Davison42be5322005-03-15 17:30:52 +0000305 }
306 }
307 }
308}
309
Wayne Davison154cdaa2005-03-29 19:49:40 +0000310static int get_next_gen_i(int batch_gen_fd, int next_gen_i, int desired_i)
311{
312 while (next_gen_i < desired_i) {
313 if (next_gen_i >= 0) {
314 rprintf(FINFO,
Wayne Davison1ed91a02005-03-29 22:05:33 +0000315 "(No batched update for%s \"%s\")\n",
316 phase ? " resend of" : "",
Wayne Davison6cbde572006-01-14 20:26:23 +0000317 f_name(the_file_list->files[next_gen_i], NULL));
Wayne Davison154cdaa2005-03-29 19:49:40 +0000318 }
319 next_gen_i = read_int(batch_gen_fd);
320 if (next_gen_i == -1)
321 next_gen_i = the_file_list->count;
322 }
323 return next_gen_i;
324}
325
Wayne Davison8ed9d842004-07-19 17:05:01 +0000326
Martin Poolb35d0d82002-04-08 04:10:20 +0000327/**
328 * main routine for receiver process.
329 *
330 * Receiver process runs on the same host as the generator process. */
Wayne Davison9e4a8d22005-03-09 18:54:16 +0000331int recv_files(int f_in, struct file_list *flist, char *local_name)
Wayne Davison17fadf72003-12-15 08:14:27 +0000332{
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000333 int next_gen_i = -1;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000334 int fd1,fd2;
335 STRUCT_STAT st;
Wayne Davisonb9232d42005-03-10 00:06:01 +0000336 int iflags, xlen;
Wayne Davison446e2392004-01-02 08:18:53 +0000337 char *fname, fbuf[MAXPATHLEN];
Wayne Davisonb9232d42005-03-10 00:06:01 +0000338 char xname[MAXPATHLEN];
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000339 char fnametmp[MAXPATHLEN];
Wayne Davison3e870a62005-02-20 01:04:51 +0000340 char *fnamecmp, *partialptr, numbuf[4];
David Dykstra375a4551998-10-26 21:42:38 +0000341 char fnamecmpbuf[MAXPATHLEN];
Wayne Davison9e4a8d22005-03-09 18:54:16 +0000342 uchar fnamecmp_type;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000343 struct file_struct *file;
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000344 struct stats initial_stats;
Wayne Davison4e834af2004-06-14 15:09:36 +0000345 int save_make_backups = make_backups;
Wayne Davison227a9c42005-02-19 22:16:47 +0000346 int itemizing = am_daemon ? daemon_log_format_has_i
347 : !am_server && log_format_has_i;
Wayne Davison1ed91a02005-03-29 22:05:33 +0000348 int max_phase = protocol_version >= 29 ? 2 : 1;
Wayne Davisonac3f7b82005-03-15 19:19:41 +0000349 int i, recv_ok;
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000350
Wayne Davisond2a918b2004-07-02 18:23:57 +0000351 if (verbose > 2)
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000352 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000353
Wayne Davisoncb869c22004-02-10 17:28:59 +0000354 if (flist->hlink_pool) {
J.W. Schultz99350662004-02-10 03:23:37 +0000355 pool_destroy(flist->hlink_pool);
356 flist->hlink_pool = NULL;
357 }
358
Wayne Davison3e7934a2005-02-20 20:55:24 +0000359 if (delay_updates)
Wayne Davison10f994a2006-02-07 18:15:51 +0000360 delayed_bits = bitbag_create(flist->count);
Wayne Davison48e1c8c2005-01-27 22:46:36 +0000361
Wayne Davison17fadf72003-12-15 08:14:27 +0000362 while (1) {
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000363 cleanup_disable();
364
365 i = read_int(f_in);
366 if (i == -1) {
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000367 if (read_batch) {
Wayne Davison154cdaa2005-03-29 19:49:40 +0000368 get_next_gen_i(batch_gen_fd, next_gen_i,
369 flist->count);
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000370 next_gen_i = -1;
371 }
Wayne Davisonac3f7b82005-03-15 19:19:41 +0000372 if (++phase > max_phase)
Wayne Davison4e834af2004-06-14 15:09:36 +0000373 break;
Wayne Davison4e834af2004-06-14 15:09:36 +0000374 csum_length = SUM_LENGTH;
375 if (verbose > 2)
376 rprintf(FINFO, "recv_files phase=%d\n", phase);
Wayne Davisonac3f7b82005-03-15 19:19:41 +0000377 if (phase == 2 && delay_updates)
Wayne Davison42be5322005-03-15 17:30:52 +0000378 handle_delayed_updates(flist, local_name);
Wayne Davison4e834af2004-06-14 15:09:36 +0000379 send_msg(MSG_DONE, "", 0);
Wayne Davisonaec6b9f2005-01-10 10:03:10 +0000380 if (keep_partial && !partial_dir)
Wayne Davison4e834af2004-06-14 15:09:36 +0000381 make_backups = 0; /* prevents double backup */
Wayne Davisone7ee91d2005-08-30 02:58:42 +0000382 if (append_mode) {
383 append_mode = 0;
384 sparse_files = 0;
385 }
Wayne Davison4e834af2004-06-14 15:09:36 +0000386 continue;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000387 }
388
Wayne Davisonb9232d42005-03-10 00:06:01 +0000389 iflags = read_item_attrs(f_in, -1, i, &fnamecmp_type,
390 xname, &xlen);
Wayne Davison58a14ed2005-03-04 16:54:08 +0000391 if (iflags == ITEM_IS_NEW) /* no-op packet */
392 continue;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000393
394 file = flist->files[i];
Wayne Davison6cbde572006-01-14 20:26:23 +0000395 fname = local_name ? local_name : f_name(file, fbuf);
Wayne Davison227a9c42005-02-19 22:16:47 +0000396
397 if (verbose > 2)
Wayne Davison45c49b52006-01-13 21:17:09 +0000398 rprintf(FINFO, "recv_files(%s)\n", fname);
Wayne Davison910e89b2005-02-16 08:10:45 +0000399
Wayne Davison22907b62005-03-05 00:21:56 +0000400 if (!(iflags & ITEM_TRANSFER)) {
Wayne Davisonb9232d42005-03-10 00:06:01 +0000401 maybe_log_item(file, iflags, itemizing, xname);
Wayne Davison58a14ed2005-03-04 16:54:08 +0000402 continue;
403 }
Wayne Davisonac3f7b82005-03-15 19:19:41 +0000404 if (phase == 2) {
405 rprintf(FERROR,
406 "got transfer request in phase 2 [%s]\n",
407 who_am_i());
408 exit_cleanup(RERR_PROTOCOL);
409 }
Wayne Davison910e89b2005-02-16 08:10:45 +0000410
Wayne Davison97feb552004-01-13 18:22:43 +0000411 stats.current_file_index = i;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000412 stats.num_transferred_files++;
413 stats.total_transferred_size += file->length;
Wayne Davison925c5172004-01-02 08:38:35 +0000414 cleanup_got_literal = 0;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000415
Wayne Davison8c2ffaf2005-02-03 00:19:40 +0000416 if (server_filter_list.head
417 && check_filter(&server_filter_list, fname, 0) < 0) {
418 rprintf(FERROR, "attempt to hack rsync failed.\n");
419 exit_cleanup(RERR_PROTOCOL);
420 }
421
Wayne Davisona0009fc2005-04-09 18:59:55 +0000422 if (!do_xfers) { /* log the transfer */
Wayne Davison4bb0af72005-02-16 17:02:13 +0000423 if (!am_server && log_format)
Wayne Davison9497b0d2005-03-04 16:08:16 +0000424 log_item(file, &stats, iflags, NULL);
Wayne Davisonf957e8f2005-03-24 16:38:34 +0000425 if (read_batch)
426 discard_receive_data(f_in, file->length);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000427 continue;
428 }
Wayne Davisona0009fc2005-04-09 18:59:55 +0000429 if (write_batch < 0) {
430 log_item(file, &stats, iflags, NULL);
Wayne Davisonb10917a2005-04-14 01:42:13 +0000431 if (!am_server)
432 discard_receive_data(f_in, file->length);
Wayne Davisona0009fc2005-04-09 18:59:55 +0000433 continue;
434 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000435
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000436 if (read_batch) {
Wayne Davison154cdaa2005-03-29 19:49:40 +0000437 next_gen_i = get_next_gen_i(batch_gen_fd, next_gen_i, i);
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000438 if (i < next_gen_i) {
Wayne Davison45c49b52006-01-13 21:17:09 +0000439 rprintf(FINFO,
440 "(Skipping batched update for \"%s\")\n",
441 fname);
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000442 discard_receive_data(f_in, file->length);
443 continue;
444 }
Wayne Davison41cfde62004-11-03 20:30:31 +0000445 next_gen_i = -1;
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000446 }
447
Wayne Davison41cfde62004-11-03 20:30:31 +0000448 partialptr = partial_dir ? partial_dir_fname(fname) : fname;
Wayne Davisona7260c42004-07-29 16:06:38 +0000449
Wayne Davison9e4a8d22005-03-09 18:54:16 +0000450 if (protocol_version >= 29) {
451 switch (fnamecmp_type) {
Wayne Davison41cfde62004-11-03 20:30:31 +0000452 case FNAMECMP_FNAME:
453 fnamecmp = fname;
454 break;
455 case FNAMECMP_PARTIAL_DIR:
Wayne Davison9e4a8d22005-03-09 18:54:16 +0000456 fnamecmp = partialptr;
Wayne Davison41cfde62004-11-03 20:30:31 +0000457 break;
458 case FNAMECMP_BACKUP:
459 fnamecmp = get_backup_name(fname);
460 break;
Wayne Davison73273072005-02-14 02:41:30 +0000461 case FNAMECMP_FUZZY:
Wayne Davisonb9232d42005-03-10 00:06:01 +0000462 if (file->dirname) {
463 pathjoin(fnamecmpbuf, MAXPATHLEN,
464 file->dirname, xname);
465 fnamecmp = fnamecmpbuf;
466 } else
467 fnamecmp = xname;
Wayne Davison73273072005-02-14 02:41:30 +0000468 break;
Wayne Davison41cfde62004-11-03 20:30:31 +0000469 default:
Wayne Davison9e4a8d22005-03-09 18:54:16 +0000470 if (fnamecmp_type >= basis_dir_cnt) {
Wayne Davisonc56595d2005-01-15 21:14:27 +0000471 rprintf(FERROR,
472 "invalid basis_dir index: %d.\n",
Wayne Davison9e4a8d22005-03-09 18:54:16 +0000473 fnamecmp_type);
Wayne Davisonc56595d2005-01-15 21:14:27 +0000474 exit_cleanup(RERR_PROTOCOL);
475 }
Wayne Davison41cfde62004-11-03 20:30:31 +0000476 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
Wayne Davison9e4a8d22005-03-09 18:54:16 +0000477 basis_dir[fnamecmp_type], fname);
Wayne Davison41cfde62004-11-03 20:30:31 +0000478 fnamecmp = fnamecmpbuf;
479 break;
480 }
Wayne Davison9e4a8d22005-03-09 18:54:16 +0000481 if (!fnamecmp || (server_filter_list.head
482 && check_filter(&server_filter_list, fname, 0) < 0))
483 fnamecmp = fname;
484 } else {
485 /* Reminder: --inplace && --partial-dir are never
486 * enabled at the same time. */
487 if (inplace && make_backups) {
488 if (!(fnamecmp = get_backup_name(fname)))
489 fnamecmp = fname;
490 } else if (partial_dir && partialptr)
491 fnamecmp = partialptr;
492 else
493 fnamecmp = fname;
494 }
Wayne Davisondc55d7b2004-09-07 21:34:26 +0000495
Wayne Davison910e89b2005-02-16 08:10:45 +0000496 initial_stats = stats;
497
Wayne Davison17fadf72003-12-15 08:14:27 +0000498 /* open the file */
Andrew Tridgell8c9fd201999-10-25 22:04:09 +0000499 fd1 = do_open(fnamecmp, O_RDONLY, 0);
David Dykstra375a4551998-10-26 21:42:38 +0000500
Wayne Davison9e4a8d22005-03-09 18:54:16 +0000501 if (fd1 == -1 && protocol_version < 29) {
502 if (fnamecmp != fname) {
503 fnamecmp = fname;
504 fd1 = do_open(fnamecmp, O_RDONLY, 0);
505 }
506
507 if (fd1 == -1 && basis_dir[0]) {
508 /* pre-29 allowed only one alternate basis */
509 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
510 basis_dir[0], fname);
511 fnamecmp = fnamecmpbuf;
512 fd1 = do_open(fnamecmp, O_RDONLY, 0);
513 }
514 }
515
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000516 if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
Wayne Davisond62bcc12004-05-15 19:31:10 +0000517 rsyserr(FERROR, errno, "fstat %s failed",
518 full_fname(fnamecmp));
Wayne Davison8ed9d842004-07-19 17:05:01 +0000519 discard_receive_data(f_in, file->length);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000520 close(fd1);
521 continue;
522 }
523
J.W. Schultz350e4e42003-09-04 05:49:50 +0000524 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
525 /* this special handling for directories
526 * wouldn't be necessary if robust_rename()
527 * and the underlying robust_unlink could cope
528 * with directories
529 */
Wayne Davisonea425412003-09-11 04:53:05 +0000530 rprintf(FERROR,"recv_files: %s is a directory\n",
531 full_fname(fnamecmp));
Wayne Davison8ed9d842004-07-19 17:05:01 +0000532 discard_receive_data(f_in, file->length);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000533 close(fd1);
534 continue;
535 }
536
J.W. Schultz350e4e42003-09-04 05:49:50 +0000537 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
538 close(fd1);
539 fd1 = -1;
J.W. Schultz350e4e42003-09-04 05:49:50 +0000540 }
541
Wayne Davisona9d6e6f2006-01-31 02:30:09 +0000542 /* If we're not preserving permissions, change the file-list's
543 * mode based on the local permissions and some heuristics. */
544 if (!preserve_perms) {
545 int exists = fd1 != -1;
546 file->mode = dest_mode(file->mode, st.st_mode, exists);
Andrew Tridgell4df9f361999-10-31 04:28:03 +0000547 }
548
Wayne Davisona3221d22004-07-16 20:06:24 +0000549 /* We now check to see if we are writing file "inplace" */
550 if (inplace) {
Wayne Davison97894c62005-07-30 16:14:22 +0000551 fd2 = do_open(fname, O_WRONLY|O_CREAT, 0600);
Wayne Davisona3221d22004-07-16 20:06:24 +0000552 if (fd2 == -1) {
553 rsyserr(FERROR, errno, "open %s failed",
Wayne Davisondc55d7b2004-09-07 21:34:26 +0000554 full_fname(fname));
Wayne Davison8ed9d842004-07-19 17:05:01 +0000555 discard_receive_data(f_in, file->length);
Wayne Davisona3221d22004-07-16 20:06:24 +0000556 if (fd1 != -1)
557 close(fd1);
558 continue;
559 }
560 } else {
561 if (!get_tmpname(fnametmp,fname)) {
Wayne Davison8ed9d842004-07-19 17:05:01 +0000562 discard_receive_data(f_in, file->length);
Wayne Davisona3221d22004-07-16 20:06:24 +0000563 if (fd1 != -1)
564 close(fd1);
565 continue;
566 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000567
Wayne Davisona3221d22004-07-16 20:06:24 +0000568 /* we initially set the perms without the
569 * setuid/setgid bits to ensure that there is no race
570 * condition. They are then correctly updated after
571 * the lchown. Thanks to snabb@epipe.fi for pointing
572 * this out. We also set it initially without group
573 * access because of a similar race condition. */
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000574 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
Wayne Davison17fadf72003-12-15 08:14:27 +0000575
Wayne Davisona3221d22004-07-16 20:06:24 +0000576 /* in most cases parent directories will already exist
577 * because their information should have been previously
578 * transferred, but that may not be the case with -R */
579 if (fd2 == -1 && relative_paths && errno == ENOENT
Wayne Davison904e5af2006-02-24 01:56:21 +0000580 && create_directory_path(fnametmp) == 0) {
Wayne Davisonb9232d42005-03-10 00:06:01 +0000581 /* Get back to name with XXXXXX in it. */
582 get_tmpname(fnametmp, fname);
Wayne Davisona3221d22004-07-16 20:06:24 +0000583 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
584 }
585 if (fd2 == -1) {
586 rsyserr(FERROR, errno, "mkstemp %s failed",
587 full_fname(fnametmp));
Wayne Davison8ed9d842004-07-19 17:05:01 +0000588 discard_receive_data(f_in, file->length);
Wayne Davisona3221d22004-07-16 20:06:24 +0000589 if (fd1 != -1)
590 close(fd1);
591 continue;
592 }
593
Wayne Davisona7e60f02005-11-10 16:57:30 +0000594 if (keep_partial)
Wayne Davisona7260c42004-07-29 16:06:38 +0000595 cleanup_set(fnametmp, partialptr, file, fd1, fd2);
Wayne Davisona3221d22004-07-16 20:06:24 +0000596 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000597
Wayne Davisona8ed4952005-02-15 07:20:04 +0000598 /* log the transfer */
Wayne Davison910e89b2005-02-16 08:10:45 +0000599 if (log_before_transfer)
Wayne Davison9497b0d2005-03-04 16:08:16 +0000600 log_item(file, &initial_stats, iflags, NULL);
Wayne Davison4bb0af72005-02-16 17:02:13 +0000601 else if (!am_server && verbose && do_progress)
Wayne Davison45c49b52006-01-13 21:17:09 +0000602 rprintf(FINFO, "%s\n", fname);
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000603
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000604 /* recv file data */
Wayne Davison7e5fa372004-07-20 21:35:58 +0000605 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
606 fname, fd2, file->length);
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000607
Wayne Davison910e89b2005-02-16 08:10:45 +0000608 if (!log_before_transfer)
Wayne Davison9497b0d2005-03-04 16:08:16 +0000609 log_item(file, &initial_stats, iflags, NULL);
Wayne Davison17fadf72003-12-15 08:14:27 +0000610
Wayne Davisond2a918b2004-07-02 18:23:57 +0000611 if (fd1 != -1)
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000612 close(fd1);
Wayne Davison9f27cd82004-04-27 19:51:33 +0000613 if (close(fd2) < 0) {
Wayne Davisond62bcc12004-05-15 19:31:10 +0000614 rsyserr(FERROR, errno, "close failed on %s",
615 full_fname(fnametmp));
Wayne Davison9f27cd82004-04-27 19:51:33 +0000616 exit_cleanup(RERR_FILEIO);
617 }
Wayne Davison17fadf72003-12-15 08:14:27 +0000618
Wayne Davison68795c62005-02-11 09:56:28 +0000619 if ((recv_ok && (!delay_updates || !partialptr)) || inplace) {
Wayne Davison4a4d2b12006-01-30 07:18:10 +0000620 if (partialptr == fname || *partial_dir == '/')
621 partialptr = NULL;
622 finish_transfer(fname, fnametmp, partialptr,
623 file, recv_ok, 1);
624 if (fnamecmp == partialptr) {
Wayne Davisonaec6b9f2005-01-10 10:03:10 +0000625 do_unlink(partialptr);
626 handle_partial_dir(partialptr, PDIR_DELETE);
627 }
628 } else if (keep_partial && partialptr
629 && handle_partial_dir(partialptr, PDIR_CREATE)) {
Wayne Davison4a4d2b12006-01-30 07:18:10 +0000630 finish_transfer(partialptr, fnametmp, NULL,
631 file, recv_ok, !partial_dir);
Wayne Davison3e7934a2005-02-20 20:55:24 +0000632 if (delay_updates && recv_ok) {
Wayne Davison10f994a2006-02-07 18:15:51 +0000633 bitbag_set_bit(delayed_bits, i);
Wayne Davison3e7934a2005-02-20 20:55:24 +0000634 recv_ok = -1;
635 }
Wayne Davisonaec6b9f2005-01-10 10:03:10 +0000636 } else {
Wayne Davisona7260c42004-07-29 16:06:38 +0000637 partialptr = NULL;
Wayne Davison55e50d82004-05-13 07:08:22 +0000638 do_unlink(fnametmp);
Wayne Davisona7260c42004-07-29 16:06:38 +0000639 }
640
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000641 cleanup_disable();
Andrew Tridgell554e0a82000-01-23 07:36:56 +0000642
Wayne Davison22907b62005-03-05 00:21:56 +0000643 if (recv_ok > 0) {
644 if (remove_sent_files
645 || (preserve_hard_links && file->link_u.links)) {
Wayne Davison3e870a62005-02-20 01:04:51 +0000646 SIVAL(numbuf, 0, i);
647 send_msg(MSG_SUCCESS, numbuf, 4);
648 }
Wayne Davison22907b62005-03-05 00:21:56 +0000649 } else if (!recv_ok) {
Wayne Davisoned7e7952005-03-05 16:42:52 +0000650 int msgtype = phase || read_batch ? FERROR : FINFO;
Wayne Davison007e3c02004-07-22 08:16:35 +0000651 if (msgtype == FERROR || verbose) {
Wayne Davisona7260c42004-07-29 16:06:38 +0000652 char *errstr, *redostr, *keptstr;
653 if (!(keep_partial && partialptr) && !inplace)
654 keptstr = "discarded";
655 else if (partial_dir)
656 keptstr = "put into partial-dir";
657 else
658 keptstr = "retained";
Wayne Davison007e3c02004-07-22 08:16:35 +0000659 if (msgtype == FERROR) {
660 errstr = "ERROR";
661 redostr = "";
662 } else {
663 errstr = "WARNING";
664 redostr = " (will try again)";
665 }
666 rprintf(msgtype,
Wayne Davisona7260c42004-07-29 16:06:38 +0000667 "%s: %s failed verification -- update %s%s.\n",
Wayne Davison45c49b52006-01-13 21:17:09 +0000668 errstr, fname, keptstr, redostr);
Wayne Davison007e3c02004-07-22 08:16:35 +0000669 }
Wayne Davisoned7e7952005-03-05 16:42:52 +0000670 if (!phase) {
Wayne Davison3e870a62005-02-20 01:04:51 +0000671 SIVAL(numbuf, 0, i);
672 send_msg(MSG_REDO, numbuf, 4);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000673 }
674 }
675 }
Wayne Davison4e834af2004-06-14 15:09:36 +0000676 make_backups = save_make_backups;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000677
Wayne Davisonac3f7b82005-03-15 19:19:41 +0000678 if (phase == 2 && delay_updates) /* for protocol_version < 29 */
679 handle_delayed_updates(flist, local_name);
Wayne Davison48e1c8c2005-01-27 22:46:36 +0000680
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000681 if (verbose > 2)
682 rprintf(FINFO,"recv_files finished\n");
Wayne Davison17fadf72003-12-15 08:14:27 +0000683
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000684 return 0;
685}