blob: 4ac407fa75f2b26cc59c93adeb84b7c71b5cd58c [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 *
Wayne Davisone7c67062006-04-25 23:51:12 +000018 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
Wayne Davison0f78b812006-04-25 20:23:34 +000021 */
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_server;
28extern int do_progress;
Wayne Davison910e89b2005-02-16 08:10:45 +000029extern int log_before_transfer;
Wayne Davison2fedf3d2006-05-29 22:57:01 +000030extern int stdout_format_has_i;
Wayne Davisonecc76232006-05-09 18:31:06 +000031extern int logfile_format_has_i;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000032extern int csum_length;
Wayne Davison16cc9ca2004-07-21 23:59:37 +000033extern int read_batch;
Wayne Davisona0009fc2005-04-09 18:59:55 +000034extern int write_batch;
Wayne Davison16cc9ca2004-07-21 23:59:37 +000035extern int batch_gen_fd;
Wayne Davison41cfde62004-11-03 20:30:31 +000036extern int protocol_version;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000037extern int relative_paths;
38extern int preserve_hard_links;
Wayne Davison92b9eb92004-03-23 16:50:40 +000039extern int preserve_perms;
Wayne Davisonc56595d2005-01-15 21:14:27 +000040extern int basis_dir_cnt;
David Dykstra53dd3131998-11-24 19:01:24 +000041extern int make_backups;
Wayne Davison925c5172004-01-02 08:38:35 +000042extern int cleanup_got_literal;
Wayne Davison47c11972006-06-30 15:52:52 +000043extern int remove_source_files;
Wayne Davison6cc11982005-07-28 01:46:12 +000044extern int append_mode;
Wayne Davisone7ee91d2005-08-30 02:58:42 +000045extern int sparse_files;
Wayne Davison55e50d82004-05-13 07:08:22 +000046extern int keep_partial;
Wayne Davisonba582f72004-05-21 08:27:04 +000047extern int checksum_seed;
Wayne Davisona3221d22004-07-16 20:06:24 +000048extern int inplace;
Wayne Davison48e1c8c2005-01-27 22:46:36 +000049extern int delay_updates;
Wayne Davison8715db22005-02-20 00:16:23 +000050extern struct stats stats;
Wayne Davison2fedf3d2006-05-29 22:57:01 +000051extern char *stdout_format;
Wayne Davison8715db22005-02-20 00:16:23 +000052extern char *tmpdir;
53extern char *partial_dir;
54extern char *basis_dir[];
Wayne Davison154cdaa2005-03-29 19:49:40 +000055extern struct file_list *the_file_list;
Wayne Davison78424182005-01-25 10:39:14 +000056extern struct filter_list_struct server_filter_list;
Wayne Davison5ebab6c2004-07-19 16:37:30 +000057
Wayne Davison10f994a2006-02-07 18:15:51 +000058static struct bitbag *delayed_bits = NULL;
Wayne Davison1ed91a02005-03-29 22:05:33 +000059static int phase = 0;
Wayne Davison48459ba2006-09-17 23:28:19 +000060/* We're either updating the basis file or an identical copy: */
61static int updating_basis;
Wayne Davison3e7934a2005-02-20 20:55:24 +000062
Andrew Tridgell2f03f951998-07-25 02:25:22 +000063
J.W. Schultz52d3e102003-03-26 11:04:14 +000064/*
65 * get_tmpname() - create a tmp filename for a given filename
66 *
67 * If a tmpdir is defined, use that as the directory to
68 * put it in. Otherwise, the tmp filename is in the same
69 * directory as the given name. Note that there may be no
70 * directory at all in the given name!
Wayne Davison17fadf72003-12-15 08:14:27 +000071 *
J.W. Schultz52d3e102003-03-26 11:04:14 +000072 * The tmp filename is basically the given filename with a
73 * dot prepended, and .XXXXXX appended (for mkstemp() to
74 * put its unique gunk in). Take care to not exceed
75 * either the MAXPATHLEN or NAME_MAX, esp. the last, as
76 * the basename basically becomes 8 chars longer. In that
77 * case, the original name is shortened sufficiently to
78 * make it all fit.
Wayne Davison17fadf72003-12-15 08:14:27 +000079 *
J.W. Schultz52d3e102003-03-26 11:04:14 +000080 * Of course, there's no real reason for the tmp name to
81 * look like the original, except to satisfy us humans.
82 * As long as it's unique, rsync will work.
83 */
84
Andrew Tridgell2f03f951998-07-25 02:25:22 +000085static int get_tmpname(char *fnametmp, char *fname)
86{
Wayne Davisond5dcb6f2006-10-13 07:18:29 +000087 int maxname, added, length = 0;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000088 char *f;
89
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++] = '/';
Wayne Davison0c2ef5f2003-08-04 21:00:57 +000094 }
J.W. Schultz52d3e102003-03-26 11:04:14 +000095
Wayne Davison0c2ef5f2003-08-04 21:00:57 +000096 if ((f = strrchr(fname, '/')) != NULL) {
J.W. Schultz52d3e102003-03-26 11:04:14 +000097 ++f;
98 if (!tmpdir) {
99 length = f - fname;
Wayne Davison0c2ef5f2003-08-04 21:00:57 +0000100 /* copy up to and including the slash */
J.W. Schultz52d3e102003-03-26 11:04:14 +0000101 strlcpy(fnametmp, fname, length + 1);
Wayne Davison0c2ef5f2003-08-04 21:00:57 +0000102 }
Wayne Davison446e2392004-01-02 08:18:53 +0000103 } else
J.W. Schultz52d3e102003-03-26 11:04:14 +0000104 f = fname;
J.W. Schultz52d3e102003-03-26 11:04:14 +0000105 fnametmp[length++] = '.';
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000106
Wayne Davisond5dcb6f2006-10-13 07:18:29 +0000107 /* The maxname value is bufsize, and includes space for the '\0'.
108 * (Note that NAME_MAX get -8 for the leading '.' above.) */
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 Davisond5dcb6f2006-10-13 07:18:29 +0000117 added = strlcpy(fnametmp + length, f, maxname);
118 if (added >= maxname)
119 added = maxname - 1;
120 memcpy(fnametmp + length + added, ".XXXXXX", 8);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000121
122 return 1;
123}
124
125
Wayne Davison7e5fa372004-07-20 21:35:58 +0000126static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
127 char *fname, int fd, OFF_T total_size)
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000128{
Wayne Davison7e5fa372004-07-20 21:35:58 +0000129 static char file_sum1[MD4_SUM_LENGTH];
130 static char file_sum2[MD4_SUM_LENGTH];
131 struct map_struct *mapbuf;
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000132 struct sum_struct sum;
Wayne Davison6c495e02005-01-01 21:08:17 +0000133 int32 len;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000134 OFF_T offset = 0;
Wayne Davison89e540e2004-07-29 06:59:30 +0000135 OFF_T offset2;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000136 char *data;
Wayne Davison6c495e02005-01-01 21:08:17 +0000137 int32 i;
Wayne Davisone1f67412004-06-30 07:27:30 +0000138 char *map = NULL;
Wayne Davison17fadf72003-12-15 08:14:27 +0000139
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000140 read_sum_head(f_in, &sum);
Wayne Davison17fadf72003-12-15 08:14:27 +0000141
Wayne Davison7e5fa372004-07-20 21:35:58 +0000142 if (fd_r >= 0 && size_r > 0) {
Wayne Davison80264052005-01-17 22:51:27 +0000143 int32 read_size = MAX(sum.blength * 2, 16*1024);
144 mapbuf = map_file(fd_r, size_r, read_size, sum.blength);
Wayne Davison7e5fa372004-07-20 21:35:58 +0000145 if (verbose > 2) {
146 rprintf(FINFO, "recv mapped %s of size %.0f\n",
Wayne Davison45c49b52006-01-13 21:17:09 +0000147 fname_r, (double)size_r);
Wayne Davison7e5fa372004-07-20 21:35:58 +0000148 }
149 } else
150 mapbuf = NULL;
151
Wayne Davisonba582f72004-05-21 08:27:04 +0000152 sum_init(checksum_seed);
Wayne Davison17fadf72003-12-15 08:14:27 +0000153
Wayne Davison6cc11982005-07-28 01:46:12 +0000154 if (append_mode) {
155 OFF_T j;
156 sum.flength = (OFF_T)sum.count * sum.blength;
157 if (sum.remainder)
158 sum.flength -= sum.blength - sum.remainder;
159 for (j = CHUNK_SIZE; j < sum.flength; j += CHUNK_SIZE) {
Wayne Davison97894c62005-07-30 16:14:22 +0000160 if (do_progress)
161 show_progress(offset, total_size);
Wayne Davison6cc11982005-07-28 01:46:12 +0000162 sum_update(map_ptr(mapbuf, offset, CHUNK_SIZE),
163 CHUNK_SIZE);
164 offset = j;
165 }
166 if (offset < sum.flength) {
167 int32 len = sum.flength - offset;
Wayne Davison97894c62005-07-30 16:14:22 +0000168 if (do_progress)
169 show_progress(offset, total_size);
Wayne Davison6cc11982005-07-28 01:46:12 +0000170 sum_update(map_ptr(mapbuf, offset, len), len);
171 offset = sum.flength;
172 }
Wayne Davisonffa8ab82006-05-18 17:04:40 +0000173 if (fd != -1 && (j = do_lseek(fd, offset, SEEK_SET)) != offset) {
174 rsyserr(FERROR, errno, "lseek of %s returned %.0f, not %.0f",
175 full_fname(fname), (double)j, (double)offset);
Wayne Davison6cc11982005-07-28 01:46:12 +0000176 exit_cleanup(RERR_FILEIO);
177 }
178 }
179
Wayne Davison3f55bd52004-01-08 00:45:41 +0000180 while ((i = recv_token(f_in, &data)) != 0) {
Wayne Davison16417f82003-07-08 16:49:10 +0000181 if (do_progress)
182 show_progress(offset, total_size);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000183
184 if (i > 0) {
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000185 if (verbose > 3) {
Andrew Tridgell5f808df2000-01-23 12:30:34 +0000186 rprintf(FINFO,"data recv %d at %.0f\n",
187 i,(double)offset);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000188 }
189
190 stats.literal_data += i;
191 cleanup_got_literal = 1;
Wayne Davison17fadf72003-12-15 08:14:27 +0000192
Wayne Davison6c495e02005-01-01 21:08:17 +0000193 sum_update(data, i);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000194
Wayne Davisonc52461f2004-07-29 07:37:27 +0000195 if (fd != -1 && write_file(fd,data,i) != i)
196 goto report_write_error;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000197 offset += i;
198 continue;
Wayne Davison17fadf72003-12-15 08:14:27 +0000199 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000200
201 i = -(i+1);
Wayne Davison6c495e02005-01-01 21:08:17 +0000202 offset2 = i * (OFF_T)sum.blength;
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000203 len = sum.blength;
Wayne Davisond2a918b2004-07-02 18:23:57 +0000204 if (i == (int)sum.count-1 && sum.remainder != 0)
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000205 len = sum.remainder;
Wayne Davison17fadf72003-12-15 08:14:27 +0000206
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000207 stats.matched_data += len;
Wayne Davison17fadf72003-12-15 08:14:27 +0000208
Wayne Davison6c495e02005-01-01 21:08:17 +0000209 if (verbose > 3) {
210 rprintf(FINFO,
211 "chunk[%d] of size %ld at %.0f offset=%.0f\n",
212 i, (long)len, (double)offset2, (double)offset);
213 }
Wayne Davison17fadf72003-12-15 08:14:27 +0000214
Wayne Davison446e2392004-01-02 08:18:53 +0000215 if (mapbuf) {
216 map = map_ptr(mapbuf,offset2,len);
Wayne Davison17fadf72003-12-15 08:14:27 +0000217
Andrew Tridgellc55f7022000-01-24 11:41:08 +0000218 see_token(map, len);
Wayne Davison6c495e02005-01-01 21:08:17 +0000219 sum_update(map, len);
Andrew Tridgellc55f7022000-01-24 11:41:08 +0000220 }
Wayne Davison17fadf72003-12-15 08:14:27 +0000221
Wayne Davison48459ba2006-09-17 23:28:19 +0000222 if (updating_basis) {
Wayne Davison89e540e2004-07-29 06:59:30 +0000223 if (offset == offset2 && fd != -1) {
Wayne Davisonffa8ab82006-05-18 17:04:40 +0000224 OFF_T pos;
Wayne Davisonc52461f2004-07-29 07:37:27 +0000225 if (flush_write_file(fd) < 0)
226 goto report_write_error;
Wayne Davison89e540e2004-07-29 06:59:30 +0000227 offset += len;
Wayne Davisonffa8ab82006-05-18 17:04:40 +0000228 if ((pos = do_lseek(fd, len, SEEK_CUR)) != offset) {
Wayne Davisonfab65a52004-07-29 06:40:26 +0000229 rsyserr(FERROR, errno,
Wayne Davisonffa8ab82006-05-18 17:04:40 +0000230 "lseek of %s returned %.0f, not %.0f",
231 full_fname(fname),
232 (double)pos, (double)offset);
Wayne Davisonfab65a52004-07-29 06:40:26 +0000233 exit_cleanup(RERR_FILEIO);
234 }
Wayne Davison89e540e2004-07-29 06:59:30 +0000235 continue;
Wayne Davisona3221d22004-07-16 20:06:24 +0000236 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000237 }
Wayne Davison1ed91a02005-03-29 22:05:33 +0000238 if (fd != -1 && map && write_file(fd, map, len) != (int)len)
Wayne Davisonc52461f2004-07-29 07:37:27 +0000239 goto report_write_error;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000240 offset += len;
241 }
242
Wayne Davison85f14172004-12-02 17:16:19 +0000243 if (flush_write_file(fd) < 0)
244 goto report_write_error;
Wayne Davison76c21942004-01-02 08:29:49 +0000245
Wayne Davison4f5b0752005-02-14 00:53:43 +0000246#ifdef HAVE_FTRUNCATE
Wayne Davisonfab65a52004-07-29 06:40:26 +0000247 if (inplace && fd != -1)
Wayne Davisona3221d22004-07-16 20:06:24 +0000248 ftruncate(fd, offset);
249#endif
250
Wayne Davison16417f82003-07-08 16:49:10 +0000251 if (do_progress)
252 end_progress(total_size);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000253
254 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
Wayne Davisonc52461f2004-07-29 07:37:27 +0000255 report_write_error:
Wayne Davisond62bcc12004-05-15 19:31:10 +0000256 rsyserr(FERROR, errno, "write failed on %s",
257 full_fname(fname));
Andrew Tridgell65417571998-11-03 07:08:27 +0000258 exit_cleanup(RERR_FILEIO);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000259 }
260
261 sum_end(file_sum1);
262
Wayne Davison7e5fa372004-07-20 21:35:58 +0000263 if (mapbuf)
264 unmap_file(mapbuf);
265
J.W. Schultzbc63ae32003-03-31 17:28:34 +0000266 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
Wayne Davisond2a918b2004-07-02 18:23:57 +0000267 if (verbose > 2)
J.W. Schultzbc63ae32003-03-31 17:28:34 +0000268 rprintf(FINFO,"got file_sum\n");
Wayne Davisond2a918b2004-07-02 18:23:57 +0000269 if (fd != -1 && memcmp(file_sum1, file_sum2, MD4_SUM_LENGTH) != 0)
J.W. Schultzbc63ae32003-03-31 17:28:34 +0000270 return 0;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000271 return 1;
272}
273
274
Wayne Davison8ed9d842004-07-19 17:05:01 +0000275static void discard_receive_data(int f_in, OFF_T length)
276{
Wayne Davison7e5fa372004-07-20 21:35:58 +0000277 receive_data(f_in, NULL, -1, 0, NULL, -1, length);
Wayne Davison8ed9d842004-07-19 17:05:01 +0000278}
279
Wayne Davison42be5322005-03-15 17:30:52 +0000280static void handle_delayed_updates(struct file_list *flist, char *local_name)
281{
282 char *fname, *partialptr, numbuf[4];
283 int i;
284
Wayne Davison10f994a2006-02-07 18:15:51 +0000285 for (i = -1; (i = bitbag_next_bit(delayed_bits, i)) >= 0; ) {
Wayne Davison42be5322005-03-15 17:30:52 +0000286 struct file_struct *file = flist->files[i];
Wayne Davison6cbde572006-01-14 20:26:23 +0000287 fname = local_name ? local_name : f_name(file, NULL);
Wayne Davison42be5322005-03-15 17:30:52 +0000288 if ((partialptr = partial_dir_fname(fname)) != NULL) {
289 if (make_backups && !make_backup(fname))
290 continue;
291 if (verbose > 2) {
292 rprintf(FINFO, "renaming %s to %s\n",
Wayne Davison45c49b52006-01-13 21:17:09 +0000293 partialptr, fname);
Wayne Davison42be5322005-03-15 17:30:52 +0000294 }
Wayne Davison6a94c582005-09-15 22:25:55 +0000295 /* We don't use robust_rename() here because the
296 * partial-dir must be on the same drive. */
Wayne Davison42be5322005-03-15 17:30:52 +0000297 if (do_rename(partialptr, fname) < 0) {
298 rsyserr(FERROR, errno,
299 "rename failed for %s (from %s)",
Wayne Davison45c49b52006-01-13 21:17:09 +0000300 full_fname(fname), partialptr);
Wayne Davison42be5322005-03-15 17:30:52 +0000301 } else {
Wayne Davison47c11972006-06-30 15:52:52 +0000302 if (remove_source_files
Wayne Davison42be5322005-03-15 17:30:52 +0000303 || (preserve_hard_links
304 && file->link_u.links)) {
305 SIVAL(numbuf, 0, i);
306 send_msg(MSG_SUCCESS,numbuf,4);
307 }
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 Davison154cdaa2005-03-29 19:49:40 +0000314static int get_next_gen_i(int batch_gen_fd, int next_gen_i, int desired_i)
315{
316 while (next_gen_i < desired_i) {
317 if (next_gen_i >= 0) {
318 rprintf(FINFO,
Wayne Davison1ed91a02005-03-29 22:05:33 +0000319 "(No batched update for%s \"%s\")\n",
320 phase ? " resend of" : "",
Wayne Davison6cbde572006-01-14 20:26:23 +0000321 f_name(the_file_list->files[next_gen_i], NULL));
Wayne Davison154cdaa2005-03-29 19:49:40 +0000322 }
323 next_gen_i = read_int(batch_gen_fd);
324 if (next_gen_i == -1)
325 next_gen_i = the_file_list->count;
326 }
327 return next_gen_i;
328}
329
Wayne Davison8ed9d842004-07-19 17:05:01 +0000330
Martin Poolb35d0d82002-04-08 04:10:20 +0000331/**
332 * main routine for receiver process.
333 *
334 * Receiver process runs on the same host as the generator process. */
Wayne Davison9e4a8d22005-03-09 18:54:16 +0000335int recv_files(int f_in, struct file_list *flist, char *local_name)
Wayne Davison17fadf72003-12-15 08:14:27 +0000336{
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000337 int next_gen_i = -1;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000338 int fd1,fd2;
339 STRUCT_STAT st;
Wayne Davisonb9232d42005-03-10 00:06:01 +0000340 int iflags, xlen;
Wayne Davison446e2392004-01-02 08:18:53 +0000341 char *fname, fbuf[MAXPATHLEN];
Wayne Davisonb9232d42005-03-10 00:06:01 +0000342 char xname[MAXPATHLEN];
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000343 char fnametmp[MAXPATHLEN];
Wayne Davison3e870a62005-02-20 01:04:51 +0000344 char *fnamecmp, *partialptr, numbuf[4];
David Dykstra375a4551998-10-26 21:42:38 +0000345 char fnamecmpbuf[MAXPATHLEN];
Wayne Davison9e4a8d22005-03-09 18:54:16 +0000346 uchar fnamecmp_type;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000347 struct file_struct *file;
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000348 struct stats initial_stats;
Wayne Davison4e834af2004-06-14 15:09:36 +0000349 int save_make_backups = make_backups;
Wayne Davison2fedf3d2006-05-29 22:57:01 +0000350 int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i;
351 enum logcode log_code = log_before_transfer ? FLOG : FINFO;
Wayne Davison1ed91a02005-03-29 22:05:33 +0000352 int max_phase = protocol_version >= 29 ? 2 : 1;
Wayne Davisonac3f7b82005-03-15 19:19:41 +0000353 int i, recv_ok;
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000354
Wayne Davisond2a918b2004-07-02 18:23:57 +0000355 if (verbose > 2)
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000356 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000357
Wayne Davisoncb869c22004-02-10 17:28:59 +0000358 if (flist->hlink_pool) {
J.W. Schultz99350662004-02-10 03:23:37 +0000359 pool_destroy(flist->hlink_pool);
360 flist->hlink_pool = NULL;
361 }
362
Wayne Davison3e7934a2005-02-20 20:55:24 +0000363 if (delay_updates)
Wayne Davison10f994a2006-02-07 18:15:51 +0000364 delayed_bits = bitbag_create(flist->count);
Wayne Davison48e1c8c2005-01-27 22:46:36 +0000365
Wayne Davison48459ba2006-09-17 23:28:19 +0000366 updating_basis = inplace;
367
Wayne Davison17fadf72003-12-15 08:14:27 +0000368 while (1) {
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000369 cleanup_disable();
370
371 i = read_int(f_in);
372 if (i == -1) {
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000373 if (read_batch) {
Wayne Davison154cdaa2005-03-29 19:49:40 +0000374 get_next_gen_i(batch_gen_fd, next_gen_i,
375 flist->count);
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000376 next_gen_i = -1;
377 }
Wayne Davisonac3f7b82005-03-15 19:19:41 +0000378 if (++phase > max_phase)
Wayne Davison4e834af2004-06-14 15:09:36 +0000379 break;
Wayne Davison4e834af2004-06-14 15:09:36 +0000380 csum_length = SUM_LENGTH;
381 if (verbose > 2)
382 rprintf(FINFO, "recv_files phase=%d\n", phase);
Wayne Davisonac3f7b82005-03-15 19:19:41 +0000383 if (phase == 2 && delay_updates)
Wayne Davison42be5322005-03-15 17:30:52 +0000384 handle_delayed_updates(flist, local_name);
Wayne Davison4e834af2004-06-14 15:09:36 +0000385 send_msg(MSG_DONE, "", 0);
Wayne Davisonaec6b9f2005-01-10 10:03:10 +0000386 if (keep_partial && !partial_dir)
Wayne Davison4e834af2004-06-14 15:09:36 +0000387 make_backups = 0; /* prevents double backup */
Wayne Davisone7ee91d2005-08-30 02:58:42 +0000388 if (append_mode) {
389 append_mode = 0;
390 sparse_files = 0;
391 }
Wayne Davison4e834af2004-06-14 15:09:36 +0000392 continue;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000393 }
394
Wayne Davisonb9232d42005-03-10 00:06:01 +0000395 iflags = read_item_attrs(f_in, -1, i, &fnamecmp_type,
396 xname, &xlen);
Wayne Davison58a14ed2005-03-04 16:54:08 +0000397 if (iflags == ITEM_IS_NEW) /* no-op packet */
398 continue;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000399
400 file = flist->files[i];
Wayne Davison6cbde572006-01-14 20:26:23 +0000401 fname = local_name ? local_name : f_name(file, fbuf);
Wayne Davison227a9c42005-02-19 22:16:47 +0000402
403 if (verbose > 2)
Wayne Davison45c49b52006-01-13 21:17:09 +0000404 rprintf(FINFO, "recv_files(%s)\n", fname);
Wayne Davison910e89b2005-02-16 08:10:45 +0000405
Wayne Davison22907b62005-03-05 00:21:56 +0000406 if (!(iflags & ITEM_TRANSFER)) {
Wayne Davisonb9232d42005-03-10 00:06:01 +0000407 maybe_log_item(file, iflags, itemizing, xname);
Wayne Davison58a14ed2005-03-04 16:54:08 +0000408 continue;
409 }
Wayne Davisonac3f7b82005-03-15 19:19:41 +0000410 if (phase == 2) {
411 rprintf(FERROR,
412 "got transfer request in phase 2 [%s]\n",
413 who_am_i());
414 exit_cleanup(RERR_PROTOCOL);
415 }
Wayne Davison910e89b2005-02-16 08:10:45 +0000416
Wayne Davison97feb552004-01-13 18:22:43 +0000417 stats.current_file_index = i;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000418 stats.num_transferred_files++;
419 stats.total_transferred_size += file->length;
Wayne Davison925c5172004-01-02 08:38:35 +0000420 cleanup_got_literal = 0;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000421
Wayne Davison8c2ffaf2005-02-03 00:19:40 +0000422 if (server_filter_list.head
423 && check_filter(&server_filter_list, fname, 0) < 0) {
424 rprintf(FERROR, "attempt to hack rsync failed.\n");
425 exit_cleanup(RERR_PROTOCOL);
426 }
427
Wayne Davisona0009fc2005-04-09 18:59:55 +0000428 if (!do_xfers) { /* log the transfer */
Wayne Davison85909932006-06-01 08:04:50 +0000429 log_item(FCLIENT, file, &stats, iflags, NULL);
Wayne Davisonf957e8f2005-03-24 16:38:34 +0000430 if (read_batch)
431 discard_receive_data(f_in, file->length);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000432 continue;
433 }
Wayne Davisona0009fc2005-04-09 18:59:55 +0000434 if (write_batch < 0) {
Wayne Davison2fedf3d2006-05-29 22:57:01 +0000435 log_item(FINFO, file, &stats, iflags, NULL);
Wayne Davisonb10917a2005-04-14 01:42:13 +0000436 if (!am_server)
437 discard_receive_data(f_in, file->length);
Wayne Davisona0009fc2005-04-09 18:59:55 +0000438 continue;
439 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000440
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000441 if (read_batch) {
Wayne Davison154cdaa2005-03-29 19:49:40 +0000442 next_gen_i = get_next_gen_i(batch_gen_fd, next_gen_i, i);
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000443 if (i < next_gen_i) {
Wayne Davison45c49b52006-01-13 21:17:09 +0000444 rprintf(FINFO,
445 "(Skipping batched update for \"%s\")\n",
446 fname);
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000447 discard_receive_data(f_in, file->length);
448 continue;
449 }
Wayne Davison41cfde62004-11-03 20:30:31 +0000450 next_gen_i = -1;
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000451 }
452
Wayne Davison41cfde62004-11-03 20:30:31 +0000453 partialptr = partial_dir ? partial_dir_fname(fname) : fname;
Wayne Davisona7260c42004-07-29 16:06:38 +0000454
Wayne Davison9e4a8d22005-03-09 18:54:16 +0000455 if (protocol_version >= 29) {
456 switch (fnamecmp_type) {
Wayne Davison41cfde62004-11-03 20:30:31 +0000457 case FNAMECMP_FNAME:
458 fnamecmp = fname;
459 break;
460 case FNAMECMP_PARTIAL_DIR:
Wayne Davison9e4a8d22005-03-09 18:54:16 +0000461 fnamecmp = partialptr;
Wayne Davison41cfde62004-11-03 20:30:31 +0000462 break;
463 case FNAMECMP_BACKUP:
464 fnamecmp = get_backup_name(fname);
465 break;
Wayne Davison73273072005-02-14 02:41:30 +0000466 case FNAMECMP_FUZZY:
Wayne Davison48459ba2006-09-17 23:28:19 +0000467 updating_basis = 0;
Wayne Davisonb9232d42005-03-10 00:06:01 +0000468 if (file->dirname) {
469 pathjoin(fnamecmpbuf, MAXPATHLEN,
470 file->dirname, xname);
471 fnamecmp = fnamecmpbuf;
472 } else
473 fnamecmp = xname;
Wayne Davison73273072005-02-14 02:41:30 +0000474 break;
Wayne Davison41cfde62004-11-03 20:30:31 +0000475 default:
Wayne Davison48459ba2006-09-17 23:28:19 +0000476 updating_basis = 0;
Wayne Davison9e4a8d22005-03-09 18:54:16 +0000477 if (fnamecmp_type >= basis_dir_cnt) {
Wayne Davisonc56595d2005-01-15 21:14:27 +0000478 rprintf(FERROR,
479 "invalid basis_dir index: %d.\n",
Wayne Davison9e4a8d22005-03-09 18:54:16 +0000480 fnamecmp_type);
Wayne Davisonc56595d2005-01-15 21:14:27 +0000481 exit_cleanup(RERR_PROTOCOL);
482 }
Wayne Davison41cfde62004-11-03 20:30:31 +0000483 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
Wayne Davison9e4a8d22005-03-09 18:54:16 +0000484 basis_dir[fnamecmp_type], fname);
Wayne Davison41cfde62004-11-03 20:30:31 +0000485 fnamecmp = fnamecmpbuf;
486 break;
487 }
Wayne Davison9e4a8d22005-03-09 18:54:16 +0000488 if (!fnamecmp || (server_filter_list.head
489 && check_filter(&server_filter_list, fname, 0) < 0))
490 fnamecmp = fname;
491 } else {
492 /* Reminder: --inplace && --partial-dir are never
493 * enabled at the same time. */
494 if (inplace && make_backups) {
495 if (!(fnamecmp = get_backup_name(fname)))
496 fnamecmp = fname;
497 } else if (partial_dir && partialptr)
498 fnamecmp = partialptr;
499 else
500 fnamecmp = fname;
501 }
Wayne Davisondc55d7b2004-09-07 21:34:26 +0000502
Wayne Davison910e89b2005-02-16 08:10:45 +0000503 initial_stats = stats;
504
Wayne Davison17fadf72003-12-15 08:14:27 +0000505 /* open the file */
Andrew Tridgell8c9fd201999-10-25 22:04:09 +0000506 fd1 = do_open(fnamecmp, O_RDONLY, 0);
David Dykstra375a4551998-10-26 21:42:38 +0000507
Wayne Davison9e4a8d22005-03-09 18:54:16 +0000508 if (fd1 == -1 && protocol_version < 29) {
509 if (fnamecmp != fname) {
510 fnamecmp = fname;
511 fd1 = do_open(fnamecmp, O_RDONLY, 0);
512 }
513
514 if (fd1 == -1 && basis_dir[0]) {
515 /* pre-29 allowed only one alternate basis */
516 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
517 basis_dir[0], fname);
518 fnamecmp = fnamecmpbuf;
519 fd1 = do_open(fnamecmp, O_RDONLY, 0);
520 }
521 }
522
Wayne Davisond5dcb6f2006-10-13 07:18:29 +0000523 if (fd1 == -1) {
524 st.st_mode = 0;
525 st.st_size = 0;
526 } else if (do_fstat(fd1,&st) != 0) {
Wayne Davisond62bcc12004-05-15 19:31:10 +0000527 rsyserr(FERROR, errno, "fstat %s failed",
528 full_fname(fnamecmp));
Wayne Davison8ed9d842004-07-19 17:05:01 +0000529 discard_receive_data(f_in, file->length);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000530 close(fd1);
531 continue;
532 }
533
J.W. Schultz350e4e42003-09-04 05:49:50 +0000534 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
535 /* this special handling for directories
536 * wouldn't be necessary if robust_rename()
537 * and the underlying robust_unlink could cope
538 * with directories
539 */
Wayne Davisonea425412003-09-11 04:53:05 +0000540 rprintf(FERROR,"recv_files: %s is a directory\n",
541 full_fname(fnamecmp));
Wayne Davison8ed9d842004-07-19 17:05:01 +0000542 discard_receive_data(f_in, file->length);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000543 close(fd1);
544 continue;
545 }
546
J.W. Schultz350e4e42003-09-04 05:49:50 +0000547 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
548 close(fd1);
549 fd1 = -1;
J.W. Schultz350e4e42003-09-04 05:49:50 +0000550 }
551
Wayne Davisona9d6e6f2006-01-31 02:30:09 +0000552 /* If we're not preserving permissions, change the file-list's
553 * mode based on the local permissions and some heuristics. */
554 if (!preserve_perms) {
555 int exists = fd1 != -1;
556 file->mode = dest_mode(file->mode, st.st_mode, exists);
Andrew Tridgell4df9f361999-10-31 04:28:03 +0000557 }
558
Wayne Davison48459ba2006-09-17 23:28:19 +0000559 /* We now check to see if we are writing the file "inplace" */
Wayne Davisona3221d22004-07-16 20:06:24 +0000560 if (inplace) {
Wayne Davison97894c62005-07-30 16:14:22 +0000561 fd2 = do_open(fname, O_WRONLY|O_CREAT, 0600);
Wayne Davisona3221d22004-07-16 20:06:24 +0000562 if (fd2 == -1) {
563 rsyserr(FERROR, errno, "open %s failed",
Wayne Davisondc55d7b2004-09-07 21:34:26 +0000564 full_fname(fname));
Wayne Davison8ed9d842004-07-19 17:05:01 +0000565 discard_receive_data(f_in, file->length);
Wayne Davisona3221d22004-07-16 20:06:24 +0000566 if (fd1 != -1)
567 close(fd1);
568 continue;
569 }
570 } else {
571 if (!get_tmpname(fnametmp,fname)) {
Wayne Davison8ed9d842004-07-19 17:05:01 +0000572 discard_receive_data(f_in, file->length);
Wayne Davisona3221d22004-07-16 20:06:24 +0000573 if (fd1 != -1)
574 close(fd1);
575 continue;
576 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000577
Wayne Davisona3221d22004-07-16 20:06:24 +0000578 /* we initially set the perms without the
579 * setuid/setgid bits to ensure that there is no race
580 * condition. They are then correctly updated after
581 * the lchown. Thanks to snabb@epipe.fi for pointing
582 * this out. We also set it initially without group
583 * access because of a similar race condition. */
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000584 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
Wayne Davison17fadf72003-12-15 08:14:27 +0000585
Wayne Davisona3221d22004-07-16 20:06:24 +0000586 /* in most cases parent directories will already exist
587 * because their information should have been previously
588 * transferred, but that may not be the case with -R */
589 if (fd2 == -1 && relative_paths && errno == ENOENT
Wayne Davison904e5af2006-02-24 01:56:21 +0000590 && create_directory_path(fnametmp) == 0) {
Wayne Davisonb9232d42005-03-10 00:06:01 +0000591 /* Get back to name with XXXXXX in it. */
592 get_tmpname(fnametmp, fname);
Wayne Davisona3221d22004-07-16 20:06:24 +0000593 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
594 }
595 if (fd2 == -1) {
596 rsyserr(FERROR, errno, "mkstemp %s failed",
597 full_fname(fnametmp));
Wayne Davison8ed9d842004-07-19 17:05:01 +0000598 discard_receive_data(f_in, file->length);
Wayne Davisona3221d22004-07-16 20:06:24 +0000599 if (fd1 != -1)
600 close(fd1);
601 continue;
602 }
603
Wayne Davison95ae5222006-05-18 18:53:24 +0000604 cleanup_set(fnametmp, partialptr, file, fd1, fd2);
Wayne Davisona3221d22004-07-16 20:06:24 +0000605 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000606
Wayne Davisona8ed4952005-02-15 07:20:04 +0000607 /* log the transfer */
Wayne Davison910e89b2005-02-16 08:10:45 +0000608 if (log_before_transfer)
Wayne Davison85909932006-06-01 08:04:50 +0000609 log_item(FCLIENT, file, &initial_stats, iflags, NULL);
Wayne Davison4bb0af72005-02-16 17:02:13 +0000610 else if (!am_server && verbose && do_progress)
Wayne Davison45c49b52006-01-13 21:17:09 +0000611 rprintf(FINFO, "%s\n", fname);
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000612
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000613 /* recv file data */
Wayne Davison7e5fa372004-07-20 21:35:58 +0000614 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
615 fname, fd2, file->length);
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000616
Wayne Davison2fedf3d2006-05-29 22:57:01 +0000617 log_item(log_code, file, &initial_stats, iflags, NULL);
Wayne Davison17fadf72003-12-15 08:14:27 +0000618
Wayne Davisond2a918b2004-07-02 18:23:57 +0000619 if (fd1 != -1)
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000620 close(fd1);
Wayne Davison9f27cd82004-04-27 19:51:33 +0000621 if (close(fd2) < 0) {
Wayne Davisond62bcc12004-05-15 19:31:10 +0000622 rsyserr(FERROR, errno, "close failed on %s",
623 full_fname(fnametmp));
Wayne Davison9f27cd82004-04-27 19:51:33 +0000624 exit_cleanup(RERR_FILEIO);
625 }
Wayne Davison17fadf72003-12-15 08:14:27 +0000626
Wayne Davison68795c62005-02-11 09:56:28 +0000627 if ((recv_ok && (!delay_updates || !partialptr)) || inplace) {
Wayne Davison01d124d2006-05-09 17:38:47 +0000628 char *temp_copy_name;
629 if (partialptr == fname)
630 partialptr = temp_copy_name = NULL;
631 else if (*partial_dir == '/')
632 temp_copy_name = NULL;
633 else
634 temp_copy_name = partialptr;
635 finish_transfer(fname, fnametmp, temp_copy_name,
Wayne Davison4a4d2b12006-01-30 07:18:10 +0000636 file, recv_ok, 1);
637 if (fnamecmp == partialptr) {
Wayne Davisonaec6b9f2005-01-10 10:03:10 +0000638 do_unlink(partialptr);
639 handle_partial_dir(partialptr, PDIR_DELETE);
640 }
641 } else if (keep_partial && partialptr
642 && handle_partial_dir(partialptr, PDIR_CREATE)) {
Wayne Davison4a4d2b12006-01-30 07:18:10 +0000643 finish_transfer(partialptr, fnametmp, NULL,
644 file, recv_ok, !partial_dir);
Wayne Davison3e7934a2005-02-20 20:55:24 +0000645 if (delay_updates && recv_ok) {
Wayne Davison10f994a2006-02-07 18:15:51 +0000646 bitbag_set_bit(delayed_bits, i);
Wayne Davison3e7934a2005-02-20 20:55:24 +0000647 recv_ok = -1;
648 }
Wayne Davisonaec6b9f2005-01-10 10:03:10 +0000649 } else {
Wayne Davisona7260c42004-07-29 16:06:38 +0000650 partialptr = NULL;
Wayne Davison55e50d82004-05-13 07:08:22 +0000651 do_unlink(fnametmp);
Wayne Davisona7260c42004-07-29 16:06:38 +0000652 }
653
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000654 cleanup_disable();
Andrew Tridgell554e0a82000-01-23 07:36:56 +0000655
Wayne Davison22907b62005-03-05 00:21:56 +0000656 if (recv_ok > 0) {
Wayne Davison47c11972006-06-30 15:52:52 +0000657 if (remove_source_files
Wayne Davison22907b62005-03-05 00:21:56 +0000658 || (preserve_hard_links && file->link_u.links)) {
Wayne Davison3e870a62005-02-20 01:04:51 +0000659 SIVAL(numbuf, 0, i);
660 send_msg(MSG_SUCCESS, numbuf, 4);
661 }
Wayne Davison22907b62005-03-05 00:21:56 +0000662 } else if (!recv_ok) {
Wayne Davisoned7e7952005-03-05 16:42:52 +0000663 int msgtype = phase || read_batch ? FERROR : FINFO;
Wayne Davison007e3c02004-07-22 08:16:35 +0000664 if (msgtype == FERROR || verbose) {
Wayne Davisona7260c42004-07-29 16:06:38 +0000665 char *errstr, *redostr, *keptstr;
666 if (!(keep_partial && partialptr) && !inplace)
667 keptstr = "discarded";
668 else if (partial_dir)
669 keptstr = "put into partial-dir";
670 else
671 keptstr = "retained";
Wayne Davison007e3c02004-07-22 08:16:35 +0000672 if (msgtype == FERROR) {
673 errstr = "ERROR";
674 redostr = "";
675 } else {
676 errstr = "WARNING";
677 redostr = " (will try again)";
678 }
679 rprintf(msgtype,
Wayne Davisona7260c42004-07-29 16:06:38 +0000680 "%s: %s failed verification -- update %s%s.\n",
Wayne Davison45c49b52006-01-13 21:17:09 +0000681 errstr, fname, keptstr, redostr);
Wayne Davison007e3c02004-07-22 08:16:35 +0000682 }
Wayne Davisoned7e7952005-03-05 16:42:52 +0000683 if (!phase) {
Wayne Davison3e870a62005-02-20 01:04:51 +0000684 SIVAL(numbuf, 0, i);
685 send_msg(MSG_REDO, numbuf, 4);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000686 }
687 }
688 }
Wayne Davison4e834af2004-06-14 15:09:36 +0000689 make_backups = save_make_backups;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000690
Wayne Davisonac3f7b82005-03-15 19:19:41 +0000691 if (phase == 2 && delay_updates) /* for protocol_version < 29 */
692 handle_delayed_updates(flist, local_name);
Wayne Davison48e1c8c2005-01-27 22:46:36 +0000693
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000694 if (verbose > 2)
695 rprintf(FINFO,"recv_files finished\n");
Wayne Davison17fadf72003-12-15 08:14:27 +0000696
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000697 return 0;
698}