blob: 6e2bd1c93b50568c11557e59104ad5550cc72067 [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;
Wayne Davisona0009fc2005-04-09 18:59:55 +000024extern int do_xfers;
Wayne Davison8715db22005-02-20 00:16:23 +000025extern int am_daemon;
26extern int am_server;
27extern int do_progress;
Wayne Davison910e89b2005-02-16 08:10:45 +000028extern int log_before_transfer;
Wayne Davison227a9c42005-02-19 22:16:47 +000029extern int log_format_has_i;
30extern int daemon_log_format_has_i;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000031extern int csum_length;
Wayne Davison16cc9ca2004-07-21 23:59:37 +000032extern int read_batch;
Wayne Davisona0009fc2005-04-09 18:59:55 +000033extern int write_batch;
Wayne Davison16cc9ca2004-07-21 23:59:37 +000034extern int batch_gen_fd;
Wayne Davison41cfde62004-11-03 20:30:31 +000035extern int protocol_version;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000036extern int relative_paths;
Wayne Davison566fce32004-06-11 07:40:48 +000037extern int keep_dirlinks;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000038extern int preserve_hard_links;
Wayne Davison92b9eb92004-03-23 16:50:40 +000039extern int preserve_perms;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000040extern int io_error;
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 Davison92b9eb92004-03-23 16:50:40 +000045extern int module_id;
46extern int ignore_errors;
47extern int orig_umask;
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 Davison8715db22005-02-20 00:16:23 +000052extern struct stats stats;
Wayne Davison910e89b2005-02-16 08:10:45 +000053extern char *log_format;
Wayne Davison8715db22005-02-20 00:16:23 +000054extern char *tmpdir;
55extern char *partial_dir;
56extern char *basis_dir[];
Wayne Davison154cdaa2005-03-29 19:49:40 +000057extern struct file_list *the_file_list;
Wayne Davison78424182005-01-25 10:39:14 +000058extern struct filter_list_struct server_filter_list;
Wayne Davison5ebab6c2004-07-19 16:37:30 +000059
Wayne Davison3e7934a2005-02-20 20:55:24 +000060#define SLOT_SIZE (16*1024) /* Desired size in bytes */
61#define PER_SLOT_BITS (SLOT_SIZE * 8) /* Number of bits per slot */
62#define PER_SLOT_INTS (SLOT_SIZE / 4) /* Number of int32s per slot */
63
64static uint32 **delayed_bits = NULL;
65static int delayed_slot_cnt = 0;
Wayne Davison1ed91a02005-03-29 22:05:33 +000066static int phase = 0;
Wayne Davison3e7934a2005-02-20 20:55:24 +000067
68static void init_delayed_bits(int max_ndx)
69{
70 delayed_slot_cnt = (max_ndx + PER_SLOT_BITS - 1) / PER_SLOT_BITS;
71
72 if (!(delayed_bits = (uint32**)calloc(delayed_slot_cnt, sizeof (uint32*))))
73 out_of_memory("set_delayed_bit");
74}
75
76static void set_delayed_bit(int ndx)
77{
78 int slot = ndx / PER_SLOT_BITS;
79 ndx %= PER_SLOT_BITS;
80
81 if (!delayed_bits[slot]) {
82 if (!(delayed_bits[slot] = (uint32*)calloc(PER_SLOT_INTS, 4)))
83 out_of_memory("set_delayed_bit");
84 }
85
86 delayed_bits[slot][ndx/32] |= 1u << (ndx % 32);
87}
88
89/* Call this with -1 to start checking from 0. Returns -1 at the end. */
90static int next_delayed_bit(int after)
91{
92 uint32 bits, mask;
93 int i, ndx = after + 1;
94 int slot = ndx / PER_SLOT_BITS;
95 ndx %= PER_SLOT_BITS;
96
97 mask = (1u << (ndx % 32)) - 1;
98 for (i = ndx / 32; slot < delayed_slot_cnt; slot++, i = mask = 0) {
99 if (!delayed_bits[slot])
100 continue;
101 for ( ; i < PER_SLOT_INTS; i++, mask = 0) {
102 if (!(bits = delayed_bits[slot][i] & ~mask))
103 continue;
104 /* The xor magic figures out the lowest enabled bit in
105 * bits, and the switch quickly computes log2(bit). */
106 switch (bits ^ (bits & (bits-1))) {
107#define LOG2(n) case 1u << n: return slot*PER_SLOT_BITS + i*32 + n
108 LOG2(0); LOG2(1); LOG2(2); LOG2(3);
109 LOG2(4); LOG2(5); LOG2(6); LOG2(7);
110 LOG2(8); LOG2(9); LOG2(10); LOG2(11);
111 LOG2(12); LOG2(13); LOG2(14); LOG2(15);
112 LOG2(16); LOG2(17); LOG2(18); LOG2(19);
113 LOG2(20); LOG2(21); LOG2(22); LOG2(23);
114 LOG2(24); LOG2(25); LOG2(26); LOG2(27);
115 LOG2(28); LOG2(29); LOG2(30); LOG2(31);
116 }
117 return -1; /* impossible... */
118 }
119 }
120
121 return -1;
122}
123
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000124
J.W. Schultz52d3e102003-03-26 11:04:14 +0000125/*
126 * get_tmpname() - create a tmp filename for a given filename
127 *
128 * If a tmpdir is defined, use that as the directory to
129 * put it in. Otherwise, the tmp filename is in the same
130 * directory as the given name. Note that there may be no
131 * directory at all in the given name!
Wayne Davison17fadf72003-12-15 08:14:27 +0000132 *
J.W. Schultz52d3e102003-03-26 11:04:14 +0000133 * The tmp filename is basically the given filename with a
134 * dot prepended, and .XXXXXX appended (for mkstemp() to
135 * put its unique gunk in). Take care to not exceed
136 * either the MAXPATHLEN or NAME_MAX, esp. the last, as
137 * the basename basically becomes 8 chars longer. In that
138 * case, the original name is shortened sufficiently to
139 * make it all fit.
Wayne Davison17fadf72003-12-15 08:14:27 +0000140 *
J.W. Schultz52d3e102003-03-26 11:04:14 +0000141 * Of course, there's no real reason for the tmp name to
142 * look like the original, except to satisfy us humans.
143 * As long as it's unique, rsync will work.
144 */
145
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000146static int get_tmpname(char *fnametmp, char *fname)
147{
148 char *f;
J.W. Schultz52d3e102003-03-26 11:04:14 +0000149 int length = 0;
150 int maxname;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000151
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000152 if (tmpdir) {
Wayne Davisona24639b2004-01-22 04:40:33 +0000153 /* Note: this can't overflow, so the return value is safe */
J.W. Schultzb7cee942004-01-20 03:37:04 +0000154 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
J.W. Schultz52d3e102003-03-26 11:04:14 +0000155 fnametmp[length++] = '/';
156 fnametmp[length] = '\0'; /* always NULL terminated */
Wayne Davison0c2ef5f2003-08-04 21:00:57 +0000157 }
J.W. Schultz52d3e102003-03-26 11:04:14 +0000158
Wayne Davison0c2ef5f2003-08-04 21:00:57 +0000159 if ((f = strrchr(fname, '/')) != NULL) {
J.W. Schultz52d3e102003-03-26 11:04:14 +0000160 ++f;
161 if (!tmpdir) {
162 length = f - fname;
Wayne Davison0c2ef5f2003-08-04 21:00:57 +0000163 /* copy up to and including the slash */
J.W. Schultz52d3e102003-03-26 11:04:14 +0000164 strlcpy(fnametmp, fname, length + 1);
Wayne Davison0c2ef5f2003-08-04 21:00:57 +0000165 }
Wayne Davison446e2392004-01-02 08:18:53 +0000166 } else
J.W. Schultz52d3e102003-03-26 11:04:14 +0000167 f = fname;
J.W. Schultz52d3e102003-03-26 11:04:14 +0000168 fnametmp[length++] = '.';
169 fnametmp[length] = '\0'; /* always NULL terminated */
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000170
J.W. Schultz52d3e102003-03-26 11:04:14 +0000171 maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000172
Wayne Davison0c2ef5f2003-08-04 21:00:57 +0000173 if (maxname < 1) {
Wayne Davison71903f62005-02-07 20:40:18 +0000174 rprintf(FERROR, "temporary filename too long: %s\n",
175 safe_fname(fname));
J.W. Schultz52d3e102003-03-26 11:04:14 +0000176 fnametmp[0] = '\0';
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000177 return 0;
178 }
179
Wayne Davison17fadf72003-12-15 08:14:27 +0000180 strlcpy(fnametmp + length, f, maxname);
J.W. Schultz52d3e102003-03-26 11:04:14 +0000181 strcat(fnametmp + length, ".XXXXXX");
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000182
183 return 1;
184}
185
186
Wayne Davison7e5fa372004-07-20 21:35:58 +0000187static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
188 char *fname, int fd, OFF_T total_size)
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000189{
Wayne Davison7e5fa372004-07-20 21:35:58 +0000190 static char file_sum1[MD4_SUM_LENGTH];
191 static char file_sum2[MD4_SUM_LENGTH];
192 struct map_struct *mapbuf;
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000193 struct sum_struct sum;
Wayne Davison6c495e02005-01-01 21:08:17 +0000194 int32 len;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000195 OFF_T offset = 0;
Wayne Davison89e540e2004-07-29 06:59:30 +0000196 OFF_T offset2;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000197 char *data;
Wayne Davison6c495e02005-01-01 21:08:17 +0000198 int32 i;
Wayne Davisone1f67412004-06-30 07:27:30 +0000199 char *map = NULL;
Wayne Davison17fadf72003-12-15 08:14:27 +0000200
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000201 read_sum_head(f_in, &sum);
Wayne Davison17fadf72003-12-15 08:14:27 +0000202
Wayne Davison7e5fa372004-07-20 21:35:58 +0000203 if (fd_r >= 0 && size_r > 0) {
Wayne Davison80264052005-01-17 22:51:27 +0000204 int32 read_size = MAX(sum.blength * 2, 16*1024);
205 mapbuf = map_file(fd_r, size_r, read_size, sum.blength);
Wayne Davison7e5fa372004-07-20 21:35:58 +0000206 if (verbose > 2) {
207 rprintf(FINFO, "recv mapped %s of size %.0f\n",
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000208 safe_fname(fname_r), (double)size_r);
Wayne Davison7e5fa372004-07-20 21:35:58 +0000209 }
210 } else
211 mapbuf = NULL;
212
Wayne Davisonba582f72004-05-21 08:27:04 +0000213 sum_init(checksum_seed);
Wayne Davison17fadf72003-12-15 08:14:27 +0000214
Wayne Davison3f55bd52004-01-08 00:45:41 +0000215 while ((i = recv_token(f_in, &data)) != 0) {
Wayne Davison16417f82003-07-08 16:49:10 +0000216 if (do_progress)
217 show_progress(offset, total_size);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000218
219 if (i > 0) {
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000220 if (verbose > 3) {
Andrew Tridgell5f808df2000-01-23 12:30:34 +0000221 rprintf(FINFO,"data recv %d at %.0f\n",
222 i,(double)offset);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000223 }
224
225 stats.literal_data += i;
226 cleanup_got_literal = 1;
Wayne Davison17fadf72003-12-15 08:14:27 +0000227
Wayne Davison6c495e02005-01-01 21:08:17 +0000228 sum_update(data, i);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000229
Wayne Davisonc52461f2004-07-29 07:37:27 +0000230 if (fd != -1 && write_file(fd,data,i) != i)
231 goto report_write_error;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000232 offset += i;
233 continue;
Wayne Davison17fadf72003-12-15 08:14:27 +0000234 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000235
236 i = -(i+1);
Wayne Davison6c495e02005-01-01 21:08:17 +0000237 offset2 = i * (OFF_T)sum.blength;
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000238 len = sum.blength;
Wayne Davisond2a918b2004-07-02 18:23:57 +0000239 if (i == (int)sum.count-1 && sum.remainder != 0)
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000240 len = sum.remainder;
Wayne Davison17fadf72003-12-15 08:14:27 +0000241
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000242 stats.matched_data += len;
Wayne Davison17fadf72003-12-15 08:14:27 +0000243
Wayne Davison6c495e02005-01-01 21:08:17 +0000244 if (verbose > 3) {
245 rprintf(FINFO,
246 "chunk[%d] of size %ld at %.0f offset=%.0f\n",
247 i, (long)len, (double)offset2, (double)offset);
248 }
Wayne Davison17fadf72003-12-15 08:14:27 +0000249
Wayne Davison446e2392004-01-02 08:18:53 +0000250 if (mapbuf) {
251 map = map_ptr(mapbuf,offset2,len);
Wayne Davison17fadf72003-12-15 08:14:27 +0000252
Andrew Tridgellc55f7022000-01-24 11:41:08 +0000253 see_token(map, len);
Wayne Davison6c495e02005-01-01 21:08:17 +0000254 sum_update(map, len);
Andrew Tridgellc55f7022000-01-24 11:41:08 +0000255 }
Wayne Davison17fadf72003-12-15 08:14:27 +0000256
Wayne Davisonfab65a52004-07-29 06:40:26 +0000257 if (inplace) {
Wayne Davison89e540e2004-07-29 06:59:30 +0000258 if (offset == offset2 && fd != -1) {
Wayne Davisonc52461f2004-07-29 07:37:27 +0000259 if (flush_write_file(fd) < 0)
260 goto report_write_error;
Wayne Davison89e540e2004-07-29 06:59:30 +0000261 offset += len;
262 if (do_lseek(fd, len, SEEK_CUR) != offset) {
Wayne Davisonfab65a52004-07-29 06:40:26 +0000263 rsyserr(FERROR, errno,
264 "lseek failed on %s",
265 full_fname(fname));
266 exit_cleanup(RERR_FILEIO);
267 }
Wayne Davison89e540e2004-07-29 06:59:30 +0000268 continue;
Wayne Davisona3221d22004-07-16 20:06:24 +0000269 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000270 }
Wayne Davison1ed91a02005-03-29 22:05:33 +0000271 if (fd != -1 && map && write_file(fd, map, len) != (int)len)
Wayne Davisonc52461f2004-07-29 07:37:27 +0000272 goto report_write_error;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000273 offset += len;
274 }
275
Wayne Davison85f14172004-12-02 17:16:19 +0000276 if (flush_write_file(fd) < 0)
277 goto report_write_error;
Wayne Davison76c21942004-01-02 08:29:49 +0000278
Wayne Davison4f5b0752005-02-14 00:53:43 +0000279#ifdef HAVE_FTRUNCATE
Wayne Davisonfab65a52004-07-29 06:40:26 +0000280 if (inplace && fd != -1)
Wayne Davisona3221d22004-07-16 20:06:24 +0000281 ftruncate(fd, offset);
282#endif
283
Wayne Davison16417f82003-07-08 16:49:10 +0000284 if (do_progress)
285 end_progress(total_size);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000286
287 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
Wayne Davisonc52461f2004-07-29 07:37:27 +0000288 report_write_error:
Wayne Davisond62bcc12004-05-15 19:31:10 +0000289 rsyserr(FERROR, errno, "write failed on %s",
290 full_fname(fname));
Andrew Tridgell65417571998-11-03 07:08:27 +0000291 exit_cleanup(RERR_FILEIO);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000292 }
293
294 sum_end(file_sum1);
295
Wayne Davison7e5fa372004-07-20 21:35:58 +0000296 if (mapbuf)
297 unmap_file(mapbuf);
298
J.W. Schultzbc63ae32003-03-31 17:28:34 +0000299 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
Wayne Davisond2a918b2004-07-02 18:23:57 +0000300 if (verbose > 2)
J.W. Schultzbc63ae32003-03-31 17:28:34 +0000301 rprintf(FINFO,"got file_sum\n");
Wayne Davisond2a918b2004-07-02 18:23:57 +0000302 if (fd != -1 && memcmp(file_sum1, file_sum2, MD4_SUM_LENGTH) != 0)
J.W. Schultzbc63ae32003-03-31 17:28:34 +0000303 return 0;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000304 return 1;
305}
306
307
Wayne Davison8ed9d842004-07-19 17:05:01 +0000308static void discard_receive_data(int f_in, OFF_T length)
309{
Wayne Davison7e5fa372004-07-20 21:35:58 +0000310 receive_data(f_in, NULL, -1, 0, NULL, -1, length);
Wayne Davison8ed9d842004-07-19 17:05:01 +0000311}
312
Wayne Davison42be5322005-03-15 17:30:52 +0000313static void handle_delayed_updates(struct file_list *flist, char *local_name)
314{
315 char *fname, *partialptr, numbuf[4];
316 int i;
317
318 for (i = -1; (i = next_delayed_bit(i)) >= 0; ) {
319 struct file_struct *file = flist->files[i];
320 fname = local_name ? local_name : f_name(file);
321 if ((partialptr = partial_dir_fname(fname)) != NULL) {
322 if (make_backups && !make_backup(fname))
323 continue;
324 if (verbose > 2) {
325 rprintf(FINFO, "renaming %s to %s\n",
326 safe_fname(partialptr),
327 safe_fname(fname));
328 }
329 if (do_rename(partialptr, fname) < 0) {
330 rsyserr(FERROR, errno,
331 "rename failed for %s (from %s)",
332 full_fname(fname),
333 safe_fname(partialptr));
334 } else {
335 if (remove_sent_files
336 || (preserve_hard_links
337 && file->link_u.links)) {
338 SIVAL(numbuf, 0, i);
339 send_msg(MSG_SUCCESS,numbuf,4);
340 }
341 handle_partial_dir(partialptr,
342 PDIR_DELETE);
343 }
344 }
345 }
346}
347
Wayne Davison154cdaa2005-03-29 19:49:40 +0000348static int get_next_gen_i(int batch_gen_fd, int next_gen_i, int desired_i)
349{
350 while (next_gen_i < desired_i) {
351 if (next_gen_i >= 0) {
352 rprintf(FINFO,
Wayne Davison1ed91a02005-03-29 22:05:33 +0000353 "(No batched update for%s \"%s\")\n",
354 phase ? " resend of" : "",
Wayne Davison154cdaa2005-03-29 19:49:40 +0000355 safe_fname(f_name(the_file_list->files[next_gen_i])));
356 }
357 next_gen_i = read_int(batch_gen_fd);
358 if (next_gen_i == -1)
359 next_gen_i = the_file_list->count;
360 }
361 return next_gen_i;
362}
363
Wayne Davison8ed9d842004-07-19 17:05:01 +0000364
Martin Poolb35d0d82002-04-08 04:10:20 +0000365/**
366 * main routine for receiver process.
367 *
368 * Receiver process runs on the same host as the generator process. */
Wayne Davison9e4a8d22005-03-09 18:54:16 +0000369int recv_files(int f_in, struct file_list *flist, char *local_name)
Wayne Davison17fadf72003-12-15 08:14:27 +0000370{
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000371 int next_gen_i = -1;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000372 int fd1,fd2;
373 STRUCT_STAT st;
Wayne Davisonb9232d42005-03-10 00:06:01 +0000374 int iflags, xlen;
Wayne Davison446e2392004-01-02 08:18:53 +0000375 char *fname, fbuf[MAXPATHLEN];
Wayne Davisonb9232d42005-03-10 00:06:01 +0000376 char xname[MAXPATHLEN];
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000377 char fnametmp[MAXPATHLEN];
Wayne Davison3e870a62005-02-20 01:04:51 +0000378 char *fnamecmp, *partialptr, numbuf[4];
David Dykstra375a4551998-10-26 21:42:38 +0000379 char fnamecmpbuf[MAXPATHLEN];
Wayne Davison9e4a8d22005-03-09 18:54:16 +0000380 uchar fnamecmp_type;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000381 struct file_struct *file;
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000382 struct stats initial_stats;
Wayne Davison4e834af2004-06-14 15:09:36 +0000383 int save_make_backups = make_backups;
Wayne Davison227a9c42005-02-19 22:16:47 +0000384 int itemizing = am_daemon ? daemon_log_format_has_i
385 : !am_server && log_format_has_i;
Wayne Davison1ed91a02005-03-29 22:05:33 +0000386 int max_phase = protocol_version >= 29 ? 2 : 1;
Wayne Davisonac3f7b82005-03-15 19:19:41 +0000387 int i, recv_ok;
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000388
Wayne Davisond2a918b2004-07-02 18:23:57 +0000389 if (verbose > 2)
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000390 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000391
Wayne Davisoncb869c22004-02-10 17:28:59 +0000392 if (flist->hlink_pool) {
J.W. Schultz99350662004-02-10 03:23:37 +0000393 pool_destroy(flist->hlink_pool);
394 flist->hlink_pool = NULL;
395 }
396
Wayne Davison3e7934a2005-02-20 20:55:24 +0000397 if (delay_updates)
398 init_delayed_bits(flist->count);
Wayne Davison48e1c8c2005-01-27 22:46:36 +0000399
Wayne Davison17fadf72003-12-15 08:14:27 +0000400 while (1) {
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000401 cleanup_disable();
402
403 i = read_int(f_in);
404 if (i == -1) {
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000405 if (read_batch) {
Wayne Davison154cdaa2005-03-29 19:49:40 +0000406 get_next_gen_i(batch_gen_fd, next_gen_i,
407 flist->count);
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000408 next_gen_i = -1;
409 }
Wayne Davisonac3f7b82005-03-15 19:19:41 +0000410 if (++phase > max_phase)
Wayne Davison4e834af2004-06-14 15:09:36 +0000411 break;
Wayne Davison4e834af2004-06-14 15:09:36 +0000412 csum_length = SUM_LENGTH;
413 if (verbose > 2)
414 rprintf(FINFO, "recv_files phase=%d\n", phase);
Wayne Davisonac3f7b82005-03-15 19:19:41 +0000415 if (phase == 2 && delay_updates)
Wayne Davison42be5322005-03-15 17:30:52 +0000416 handle_delayed_updates(flist, local_name);
Wayne Davison4e834af2004-06-14 15:09:36 +0000417 send_msg(MSG_DONE, "", 0);
Wayne Davisonaec6b9f2005-01-10 10:03:10 +0000418 if (keep_partial && !partial_dir)
Wayne Davison4e834af2004-06-14 15:09:36 +0000419 make_backups = 0; /* prevents double backup */
420 continue;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000421 }
422
Wayne Davisonb9232d42005-03-10 00:06:01 +0000423 iflags = read_item_attrs(f_in, -1, i, &fnamecmp_type,
424 xname, &xlen);
Wayne Davison58a14ed2005-03-04 16:54:08 +0000425 if (iflags == ITEM_IS_NEW) /* no-op packet */
426 continue;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000427
428 file = flist->files[i];
Wayne Davison227a9c42005-02-19 22:16:47 +0000429 fname = local_name ? local_name : f_name_to(file, fbuf);
430
431 if (verbose > 2)
432 rprintf(FINFO, "recv_files(%s)\n", safe_fname(fname));
Wayne Davison910e89b2005-02-16 08:10:45 +0000433
Wayne Davison22907b62005-03-05 00:21:56 +0000434 if (!(iflags & ITEM_TRANSFER)) {
Wayne Davisonb9232d42005-03-10 00:06:01 +0000435 maybe_log_item(file, iflags, itemizing, xname);
Wayne Davison58a14ed2005-03-04 16:54:08 +0000436 continue;
437 }
Wayne Davisonac3f7b82005-03-15 19:19:41 +0000438 if (phase == 2) {
439 rprintf(FERROR,
440 "got transfer request in phase 2 [%s]\n",
441 who_am_i());
442 exit_cleanup(RERR_PROTOCOL);
443 }
Wayne Davison910e89b2005-02-16 08:10:45 +0000444
Wayne Davison97feb552004-01-13 18:22:43 +0000445 stats.current_file_index = i;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000446 stats.num_transferred_files++;
447 stats.total_transferred_size += file->length;
Wayne Davison925c5172004-01-02 08:38:35 +0000448 cleanup_got_literal = 0;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000449
Wayne Davison8c2ffaf2005-02-03 00:19:40 +0000450 if (server_filter_list.head
451 && check_filter(&server_filter_list, fname, 0) < 0) {
452 rprintf(FERROR, "attempt to hack rsync failed.\n");
453 exit_cleanup(RERR_PROTOCOL);
454 }
455
Wayne Davisona0009fc2005-04-09 18:59:55 +0000456 if (!do_xfers) { /* log the transfer */
Wayne Davison4bb0af72005-02-16 17:02:13 +0000457 if (!am_server && log_format)
Wayne Davison9497b0d2005-03-04 16:08:16 +0000458 log_item(file, &stats, iflags, NULL);
Wayne Davisonf957e8f2005-03-24 16:38:34 +0000459 if (read_batch)
460 discard_receive_data(f_in, file->length);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000461 continue;
462 }
Wayne Davisona0009fc2005-04-09 18:59:55 +0000463 if (write_batch < 0) {
464 log_item(file, &stats, iflags, NULL);
465 discard_receive_data(f_in, file->length);
466 continue;
467 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000468
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000469 if (read_batch) {
Wayne Davison154cdaa2005-03-29 19:49:40 +0000470 next_gen_i = get_next_gen_i(batch_gen_fd, next_gen_i, i);
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000471 if (i < next_gen_i) {
Wayne Davison154cdaa2005-03-29 19:49:40 +0000472 rprintf(FINFO, "(Skipping batched update for \"%s\")\n",
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000473 safe_fname(fname));
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000474 discard_receive_data(f_in, file->length);
475 continue;
476 }
Wayne Davison41cfde62004-11-03 20:30:31 +0000477 next_gen_i = -1;
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000478 }
479
Wayne Davison41cfde62004-11-03 20:30:31 +0000480 partialptr = partial_dir ? partial_dir_fname(fname) : fname;
Wayne Davisona7260c42004-07-29 16:06:38 +0000481
Wayne Davison9e4a8d22005-03-09 18:54:16 +0000482 if (protocol_version >= 29) {
483 switch (fnamecmp_type) {
Wayne Davison41cfde62004-11-03 20:30:31 +0000484 case FNAMECMP_FNAME:
485 fnamecmp = fname;
486 break;
487 case FNAMECMP_PARTIAL_DIR:
Wayne Davison9e4a8d22005-03-09 18:54:16 +0000488 fnamecmp = partialptr;
Wayne Davison41cfde62004-11-03 20:30:31 +0000489 break;
490 case FNAMECMP_BACKUP:
491 fnamecmp = get_backup_name(fname);
492 break;
Wayne Davison73273072005-02-14 02:41:30 +0000493 case FNAMECMP_FUZZY:
Wayne Davisonb9232d42005-03-10 00:06:01 +0000494 if (file->dirname) {
495 pathjoin(fnamecmpbuf, MAXPATHLEN,
496 file->dirname, xname);
497 fnamecmp = fnamecmpbuf;
498 } else
499 fnamecmp = xname;
Wayne Davison73273072005-02-14 02:41:30 +0000500 break;
Wayne Davison41cfde62004-11-03 20:30:31 +0000501 default:
Wayne Davison9e4a8d22005-03-09 18:54:16 +0000502 if (fnamecmp_type >= basis_dir_cnt) {
Wayne Davisonc56595d2005-01-15 21:14:27 +0000503 rprintf(FERROR,
504 "invalid basis_dir index: %d.\n",
Wayne Davison9e4a8d22005-03-09 18:54:16 +0000505 fnamecmp_type);
Wayne Davisonc56595d2005-01-15 21:14:27 +0000506 exit_cleanup(RERR_PROTOCOL);
507 }
Wayne Davison41cfde62004-11-03 20:30:31 +0000508 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
Wayne Davison9e4a8d22005-03-09 18:54:16 +0000509 basis_dir[fnamecmp_type], fname);
Wayne Davison41cfde62004-11-03 20:30:31 +0000510 fnamecmp = fnamecmpbuf;
511 break;
512 }
Wayne Davison9e4a8d22005-03-09 18:54:16 +0000513 if (!fnamecmp || (server_filter_list.head
514 && check_filter(&server_filter_list, fname, 0) < 0))
515 fnamecmp = fname;
516 } else {
517 /* Reminder: --inplace && --partial-dir are never
518 * enabled at the same time. */
519 if (inplace && make_backups) {
520 if (!(fnamecmp = get_backup_name(fname)))
521 fnamecmp = fname;
522 } else if (partial_dir && partialptr)
523 fnamecmp = partialptr;
524 else
525 fnamecmp = fname;
526 }
Wayne Davisondc55d7b2004-09-07 21:34:26 +0000527
Wayne Davison910e89b2005-02-16 08:10:45 +0000528 initial_stats = stats;
529
Wayne Davison17fadf72003-12-15 08:14:27 +0000530 /* open the file */
Andrew Tridgell8c9fd201999-10-25 22:04:09 +0000531 fd1 = do_open(fnamecmp, O_RDONLY, 0);
David Dykstra375a4551998-10-26 21:42:38 +0000532
Wayne Davison9e4a8d22005-03-09 18:54:16 +0000533 if (fd1 == -1 && protocol_version < 29) {
534 if (fnamecmp != fname) {
535 fnamecmp = fname;
536 fd1 = do_open(fnamecmp, O_RDONLY, 0);
537 }
538
539 if (fd1 == -1 && basis_dir[0]) {
540 /* pre-29 allowed only one alternate basis */
541 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
542 basis_dir[0], fname);
543 fnamecmp = fnamecmpbuf;
544 fd1 = do_open(fnamecmp, O_RDONLY, 0);
545 }
546 }
547
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000548 if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
Wayne Davisond62bcc12004-05-15 19:31:10 +0000549 rsyserr(FERROR, errno, "fstat %s failed",
550 full_fname(fnamecmp));
Wayne Davison8ed9d842004-07-19 17:05:01 +0000551 discard_receive_data(f_in, file->length);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000552 close(fd1);
553 continue;
554 }
555
J.W. Schultz350e4e42003-09-04 05:49:50 +0000556 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
557 /* this special handling for directories
558 * wouldn't be necessary if robust_rename()
559 * and the underlying robust_unlink could cope
560 * with directories
561 */
Wayne Davisonea425412003-09-11 04:53:05 +0000562 rprintf(FERROR,"recv_files: %s is a directory\n",
563 full_fname(fnamecmp));
Wayne Davison8ed9d842004-07-19 17:05:01 +0000564 discard_receive_data(f_in, file->length);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000565 close(fd1);
566 continue;
567 }
568
J.W. Schultz350e4e42003-09-04 05:49:50 +0000569 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
570 close(fd1);
571 fd1 = -1;
J.W. Schultz350e4e42003-09-04 05:49:50 +0000572 }
573
Andrew Tridgell4df9f361999-10-31 04:28:03 +0000574 if (fd1 != -1 && !preserve_perms) {
J.W. Schultz85ed0aa2003-03-21 07:27:31 +0000575 /* if the file exists already and we aren't preserving
Wayne Davison17fadf72003-12-15 08:14:27 +0000576 * permissions then act as though the remote end sent
577 * us the file permissions we already have */
Andrew Tridgell4df9f361999-10-31 04:28:03 +0000578 file->mode = st.st_mode;
579 }
580
Wayne Davisona3221d22004-07-16 20:06:24 +0000581 /* We now check to see if we are writing file "inplace" */
582 if (inplace) {
Wayne Davisondc55d7b2004-09-07 21:34:26 +0000583 fd2 = do_open(fname, O_WRONLY|O_CREAT, 0);
Wayne Davisona3221d22004-07-16 20:06:24 +0000584 if (fd2 == -1) {
585 rsyserr(FERROR, errno, "open %s failed",
Wayne Davisondc55d7b2004-09-07 21:34:26 +0000586 full_fname(fname));
Wayne Davison8ed9d842004-07-19 17:05:01 +0000587 discard_receive_data(f_in, file->length);
Wayne Davisona3221d22004-07-16 20:06:24 +0000588 if (fd1 != -1)
589 close(fd1);
590 continue;
591 }
592 } else {
593 if (!get_tmpname(fnametmp,fname)) {
Wayne Davison8ed9d842004-07-19 17:05:01 +0000594 discard_receive_data(f_in, file->length);
Wayne Davisona3221d22004-07-16 20:06:24 +0000595 if (fd1 != -1)
596 close(fd1);
597 continue;
598 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000599
Wayne Davisona3221d22004-07-16 20:06:24 +0000600 /* we initially set the perms without the
601 * setuid/setgid bits to ensure that there is no race
602 * condition. They are then correctly updated after
603 * the lchown. Thanks to snabb@epipe.fi for pointing
604 * this out. We also set it initially without group
605 * access because of a similar race condition. */
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000606 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
Wayne Davison17fadf72003-12-15 08:14:27 +0000607
Wayne Davisona3221d22004-07-16 20:06:24 +0000608 /* in most cases parent directories will already exist
609 * because their information should have been previously
610 * transferred, but that may not be the case with -R */
611 if (fd2 == -1 && relative_paths && errno == ENOENT
612 && create_directory_path(fnametmp, orig_umask) == 0) {
Wayne Davisonb9232d42005-03-10 00:06:01 +0000613 /* Get back to name with XXXXXX in it. */
614 get_tmpname(fnametmp, fname);
Wayne Davisona3221d22004-07-16 20:06:24 +0000615 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
616 }
617 if (fd2 == -1) {
618 rsyserr(FERROR, errno, "mkstemp %s failed",
619 full_fname(fnametmp));
Wayne Davison8ed9d842004-07-19 17:05:01 +0000620 discard_receive_data(f_in, file->length);
Wayne Davisona3221d22004-07-16 20:06:24 +0000621 if (fd1 != -1)
622 close(fd1);
623 continue;
624 }
625
Wayne Davisona7260c42004-07-29 16:06:38 +0000626 if (partialptr)
627 cleanup_set(fnametmp, partialptr, file, fd1, fd2);
Wayne Davisona3221d22004-07-16 20:06:24 +0000628 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000629
Wayne Davisona8ed4952005-02-15 07:20:04 +0000630 /* log the transfer */
Wayne Davison910e89b2005-02-16 08:10:45 +0000631 if (log_before_transfer)
Wayne Davison9497b0d2005-03-04 16:08:16 +0000632 log_item(file, &initial_stats, iflags, NULL);
Wayne Davison4bb0af72005-02-16 17:02:13 +0000633 else if (!am_server && verbose && do_progress)
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000634 rprintf(FINFO, "%s\n", safe_fname(fname));
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000635
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000636 /* recv file data */
Wayne Davison7e5fa372004-07-20 21:35:58 +0000637 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
638 fname, fd2, file->length);
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000639
Wayne Davison910e89b2005-02-16 08:10:45 +0000640 if (!log_before_transfer)
Wayne Davison9497b0d2005-03-04 16:08:16 +0000641 log_item(file, &initial_stats, iflags, NULL);
Wayne Davison17fadf72003-12-15 08:14:27 +0000642
Wayne Davisond2a918b2004-07-02 18:23:57 +0000643 if (fd1 != -1)
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000644 close(fd1);
Wayne Davison9f27cd82004-04-27 19:51:33 +0000645 if (close(fd2) < 0) {
Wayne Davisond62bcc12004-05-15 19:31:10 +0000646 rsyserr(FERROR, errno, "close failed on %s",
647 full_fname(fnametmp));
Wayne Davison9f27cd82004-04-27 19:51:33 +0000648 exit_cleanup(RERR_FILEIO);
649 }
Wayne Davison17fadf72003-12-15 08:14:27 +0000650
Wayne Davison68795c62005-02-11 09:56:28 +0000651 if ((recv_ok && (!delay_updates || !partialptr)) || inplace) {
Wayne Davisonaec6b9f2005-01-10 10:03:10 +0000652 finish_transfer(fname, fnametmp, file, recv_ok, 1);
653 if (partialptr != fname && fnamecmp == partialptr) {
654 do_unlink(partialptr);
655 handle_partial_dir(partialptr, PDIR_DELETE);
656 }
657 } else if (keep_partial && partialptr
658 && handle_partial_dir(partialptr, PDIR_CREATE)) {
659 finish_transfer(partialptr, fnametmp, file, recv_ok,
660 !partial_dir);
Wayne Davison3e7934a2005-02-20 20:55:24 +0000661 if (delay_updates && recv_ok) {
662 set_delayed_bit(i);
663 recv_ok = -1;
664 }
Wayne Davisonaec6b9f2005-01-10 10:03:10 +0000665 } else {
Wayne Davisona7260c42004-07-29 16:06:38 +0000666 partialptr = NULL;
Wayne Davison55e50d82004-05-13 07:08:22 +0000667 do_unlink(fnametmp);
Wayne Davisona7260c42004-07-29 16:06:38 +0000668 }
669
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000670 cleanup_disable();
Andrew Tridgell554e0a82000-01-23 07:36:56 +0000671
Wayne Davison22907b62005-03-05 00:21:56 +0000672 if (recv_ok > 0) {
673 if (remove_sent_files
674 || (preserve_hard_links && file->link_u.links)) {
Wayne Davison3e870a62005-02-20 01:04:51 +0000675 SIVAL(numbuf, 0, i);
676 send_msg(MSG_SUCCESS, numbuf, 4);
677 }
Wayne Davison22907b62005-03-05 00:21:56 +0000678 } else if (!recv_ok) {
Wayne Davisoned7e7952005-03-05 16:42:52 +0000679 int msgtype = phase || read_batch ? FERROR : FINFO;
Wayne Davison007e3c02004-07-22 08:16:35 +0000680 if (msgtype == FERROR || verbose) {
Wayne Davisona7260c42004-07-29 16:06:38 +0000681 char *errstr, *redostr, *keptstr;
682 if (!(keep_partial && partialptr) && !inplace)
683 keptstr = "discarded";
684 else if (partial_dir)
685 keptstr = "put into partial-dir";
686 else
687 keptstr = "retained";
Wayne Davison007e3c02004-07-22 08:16:35 +0000688 if (msgtype == FERROR) {
689 errstr = "ERROR";
690 redostr = "";
691 } else {
692 errstr = "WARNING";
693 redostr = " (will try again)";
694 }
695 rprintf(msgtype,
Wayne Davisona7260c42004-07-29 16:06:38 +0000696 "%s: %s failed verification -- update %s%s.\n",
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000697 errstr, safe_fname(fname),
698 keptstr, redostr);
Wayne Davison007e3c02004-07-22 08:16:35 +0000699 }
Wayne Davisoned7e7952005-03-05 16:42:52 +0000700 if (!phase) {
Wayne Davison3e870a62005-02-20 01:04:51 +0000701 SIVAL(numbuf, 0, i);
702 send_msg(MSG_REDO, numbuf, 4);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000703 }
704 }
705 }
Wayne Davison4e834af2004-06-14 15:09:36 +0000706 make_backups = save_make_backups;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000707
Wayne Davisonac3f7b82005-03-15 19:19:41 +0000708 if (phase == 2 && delay_updates) /* for protocol_version < 29 */
709 handle_delayed_updates(flist, local_name);
Wayne Davison48e1c8c2005-01-27 22:46:36 +0000710
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000711 if (verbose > 2)
712 rprintf(FINFO,"recv_files finished\n");
Wayne Davison17fadf72003-12-15 08:14:27 +0000713
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000714 return 0;
715}