blob: 01f7925256c2bac784b9b61443f29257e883d7fb [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 Davisonb98f0402005-01-23 07:27:01 +000024extern int recurse;
Wayne Davison92b9eb92004-03-23 16:50:40 +000025extern int delete_after;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000026extern int csum_length;
27extern struct stats stats;
28extern int dry_run;
Wayne Davison16cc9ca2004-07-21 23:59:37 +000029extern int read_batch;
30extern int batch_gen_fd;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000031extern int am_server;
Wayne Davison41cfde62004-11-03 20:30:31 +000032extern int protocol_version;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000033extern int relative_paths;
Wayne Davison566fce32004-06-11 07:40:48 +000034extern int keep_dirlinks;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000035extern int preserve_hard_links;
Wayne Davison92b9eb92004-03-23 16:50:40 +000036extern int preserve_perms;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000037extern int io_error;
38extern char *tmpdir;
Wayne Davisona7260c42004-07-29 16:06:38 +000039extern char *partial_dir;
Wayne Davisonee297522004-11-27 17:56:58 +000040extern char *basis_dir[];
Wayne Davisonc56595d2005-01-15 21:14:27 +000041extern int basis_dir_cnt;
David Dykstra53dd3131998-11-24 19:01:24 +000042extern int make_backups;
Wayne Davison16417f82003-07-08 16:49:10 +000043extern int do_progress;
Wayne Davison925c5172004-01-02 08:38:35 +000044extern int cleanup_got_literal;
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;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000051
Wayne Davison78424182005-01-25 10:39:14 +000052extern struct filter_list_struct server_filter_list;
Wayne Davison5ebab6c2004-07-19 16:37:30 +000053
54
Wayne Davison3ab56a22005-01-24 01:41:45 +000055/* This deletes any files on the receiving side that are not present on the
56 * sending side. This is used by --delete-before and --delete-after. */
Andrew Tridgell6957ae32000-01-24 11:20:25 +000057void delete_files(struct file_list *flist)
Andrew Tridgell2f03f951998-07-25 02:25:22 +000058{
Wayne Davison3ab56a22005-01-24 01:41:45 +000059 char fbuf[MAXPATHLEN];
60 int j;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000061
Wayne Davison1e82e2c2004-03-23 16:36:00 +000062 for (j = 0; j < flist->count; j++) {
Wayne Davison71e27c42005-01-21 00:34:37 +000063 if (!(flist->files[j]->flags & FLAG_DEL_START)
Wayne Davisona5342642004-01-27 01:05:12 +000064 || !S_ISDIR(flist->files[j]->mode))
65 continue;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000066
Wayne Davison3ab56a22005-01-24 01:41:45 +000067 delete_in_dir(flist, f_name_to(flist->files[j], fbuf));
Andrew Tridgell2f03f951998-07-25 02:25:22 +000068 }
69}
70
71
J.W. Schultz52d3e102003-03-26 11:04:14 +000072/*
73 * get_tmpname() - create a tmp filename for a given filename
74 *
75 * If a tmpdir is defined, use that as the directory to
76 * put it in. Otherwise, the tmp filename is in the same
77 * directory as the given name. Note that there may be no
78 * directory at all in the given name!
Wayne Davison17fadf72003-12-15 08:14:27 +000079 *
J.W. Schultz52d3e102003-03-26 11:04:14 +000080 * The tmp filename is basically the given filename with a
81 * dot prepended, and .XXXXXX appended (for mkstemp() to
82 * put its unique gunk in). Take care to not exceed
83 * either the MAXPATHLEN or NAME_MAX, esp. the last, as
84 * the basename basically becomes 8 chars longer. In that
85 * case, the original name is shortened sufficiently to
86 * make it all fit.
Wayne Davison17fadf72003-12-15 08:14:27 +000087 *
J.W. Schultz52d3e102003-03-26 11:04:14 +000088 * Of course, there's no real reason for the tmp name to
89 * look like the original, except to satisfy us humans.
90 * As long as it's unique, rsync will work.
91 */
92
Andrew Tridgell2f03f951998-07-25 02:25:22 +000093static int get_tmpname(char *fnametmp, char *fname)
94{
95 char *f;
J.W. Schultz52d3e102003-03-26 11:04:14 +000096 int length = 0;
97 int maxname;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000098
Andrew Tridgell2f03f951998-07-25 02:25:22 +000099 if (tmpdir) {
Wayne Davisona24639b2004-01-22 04:40:33 +0000100 /* Note: this can't overflow, so the return value is safe */
J.W. Schultzb7cee942004-01-20 03:37:04 +0000101 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
J.W. Schultz52d3e102003-03-26 11:04:14 +0000102 fnametmp[length++] = '/';
103 fnametmp[length] = '\0'; /* always NULL terminated */
Wayne Davison0c2ef5f2003-08-04 21:00:57 +0000104 }
J.W. Schultz52d3e102003-03-26 11:04:14 +0000105
Wayne Davison0c2ef5f2003-08-04 21:00:57 +0000106 if ((f = strrchr(fname, '/')) != NULL) {
J.W. Schultz52d3e102003-03-26 11:04:14 +0000107 ++f;
108 if (!tmpdir) {
109 length = f - fname;
Wayne Davison0c2ef5f2003-08-04 21:00:57 +0000110 /* copy up to and including the slash */
J.W. Schultz52d3e102003-03-26 11:04:14 +0000111 strlcpy(fnametmp, fname, length + 1);
Wayne Davison0c2ef5f2003-08-04 21:00:57 +0000112 }
Wayne Davison446e2392004-01-02 08:18:53 +0000113 } else
J.W. Schultz52d3e102003-03-26 11:04:14 +0000114 f = fname;
J.W. Schultz52d3e102003-03-26 11:04:14 +0000115 fnametmp[length++] = '.';
116 fnametmp[length] = '\0'; /* always NULL terminated */
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000117
J.W. Schultz52d3e102003-03-26 11:04:14 +0000118 maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000119
Wayne Davison0c2ef5f2003-08-04 21:00:57 +0000120 if (maxname < 1) {
J.W. Schultz52d3e102003-03-26 11:04:14 +0000121 rprintf(FERROR, "temporary filename too long: %s\n", fname);
122 fnametmp[0] = '\0';
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000123 return 0;
124 }
125
Wayne Davison17fadf72003-12-15 08:14:27 +0000126 strlcpy(fnametmp + length, f, maxname);
J.W. Schultz52d3e102003-03-26 11:04:14 +0000127 strcat(fnametmp + length, ".XXXXXX");
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000128
129 return 1;
130}
131
132
Wayne Davison7e5fa372004-07-20 21:35:58 +0000133static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
134 char *fname, int fd, OFF_T total_size)
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000135{
Wayne Davison7e5fa372004-07-20 21:35:58 +0000136 static char file_sum1[MD4_SUM_LENGTH];
137 static char file_sum2[MD4_SUM_LENGTH];
138 struct map_struct *mapbuf;
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000139 struct sum_struct sum;
Wayne Davison6c495e02005-01-01 21:08:17 +0000140 int32 len;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000141 OFF_T offset = 0;
Wayne Davison89e540e2004-07-29 06:59:30 +0000142 OFF_T offset2;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000143 char *data;
Wayne Davison6c495e02005-01-01 21:08:17 +0000144 int32 i;
Wayne Davisone1f67412004-06-30 07:27:30 +0000145 char *map = NULL;
Wayne Davison17fadf72003-12-15 08:14:27 +0000146
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000147 read_sum_head(f_in, &sum);
Wayne Davison17fadf72003-12-15 08:14:27 +0000148
Wayne Davison7e5fa372004-07-20 21:35:58 +0000149 if (fd_r >= 0 && size_r > 0) {
Wayne Davison80264052005-01-17 22:51:27 +0000150 int32 read_size = MAX(sum.blength * 2, 16*1024);
151 mapbuf = map_file(fd_r, size_r, read_size, sum.blength);
Wayne Davison7e5fa372004-07-20 21:35:58 +0000152 if (verbose > 2) {
153 rprintf(FINFO, "recv mapped %s of size %.0f\n",
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000154 safe_fname(fname_r), (double)size_r);
Wayne Davison7e5fa372004-07-20 21:35:58 +0000155 }
156 } else
157 mapbuf = NULL;
158
Wayne Davisonba582f72004-05-21 08:27:04 +0000159 sum_init(checksum_seed);
Wayne Davison17fadf72003-12-15 08:14:27 +0000160
Wayne Davison3f55bd52004-01-08 00:45:41 +0000161 while ((i = recv_token(f_in, &data)) != 0) {
Wayne Davison16417f82003-07-08 16:49:10 +0000162 if (do_progress)
163 show_progress(offset, total_size);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000164
165 if (i > 0) {
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000166 if (verbose > 3) {
Andrew Tridgell5f808df2000-01-23 12:30:34 +0000167 rprintf(FINFO,"data recv %d at %.0f\n",
168 i,(double)offset);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000169 }
170
171 stats.literal_data += i;
172 cleanup_got_literal = 1;
Wayne Davison17fadf72003-12-15 08:14:27 +0000173
Wayne Davison6c495e02005-01-01 21:08:17 +0000174 sum_update(data, i);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000175
Wayne Davisonc52461f2004-07-29 07:37:27 +0000176 if (fd != -1 && write_file(fd,data,i) != i)
177 goto report_write_error;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000178 offset += i;
179 continue;
Wayne Davison17fadf72003-12-15 08:14:27 +0000180 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000181
182 i = -(i+1);
Wayne Davison6c495e02005-01-01 21:08:17 +0000183 offset2 = i * (OFF_T)sum.blength;
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000184 len = sum.blength;
Wayne Davisond2a918b2004-07-02 18:23:57 +0000185 if (i == (int)sum.count-1 && sum.remainder != 0)
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000186 len = sum.remainder;
Wayne Davison17fadf72003-12-15 08:14:27 +0000187
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000188 stats.matched_data += len;
Wayne Davison17fadf72003-12-15 08:14:27 +0000189
Wayne Davison6c495e02005-01-01 21:08:17 +0000190 if (verbose > 3) {
191 rprintf(FINFO,
192 "chunk[%d] of size %ld at %.0f offset=%.0f\n",
193 i, (long)len, (double)offset2, (double)offset);
194 }
Wayne Davison17fadf72003-12-15 08:14:27 +0000195
Wayne Davison446e2392004-01-02 08:18:53 +0000196 if (mapbuf) {
197 map = map_ptr(mapbuf,offset2,len);
Wayne Davison17fadf72003-12-15 08:14:27 +0000198
Andrew Tridgellc55f7022000-01-24 11:41:08 +0000199 see_token(map, len);
Wayne Davison6c495e02005-01-01 21:08:17 +0000200 sum_update(map, len);
Andrew Tridgellc55f7022000-01-24 11:41:08 +0000201 }
Wayne Davison17fadf72003-12-15 08:14:27 +0000202
Wayne Davisonfab65a52004-07-29 06:40:26 +0000203 if (inplace) {
Wayne Davison89e540e2004-07-29 06:59:30 +0000204 if (offset == offset2 && fd != -1) {
Wayne Davisonc52461f2004-07-29 07:37:27 +0000205 if (flush_write_file(fd) < 0)
206 goto report_write_error;
Wayne Davison89e540e2004-07-29 06:59:30 +0000207 offset += len;
208 if (do_lseek(fd, len, SEEK_CUR) != offset) {
Wayne Davisonfab65a52004-07-29 06:40:26 +0000209 rsyserr(FERROR, errno,
210 "lseek failed on %s",
211 full_fname(fname));
212 exit_cleanup(RERR_FILEIO);
213 }
Wayne Davison89e540e2004-07-29 06:59:30 +0000214 continue;
Wayne Davisona3221d22004-07-16 20:06:24 +0000215 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000216 }
Wayne Davisonc52461f2004-07-29 07:37:27 +0000217 if (fd != -1 && write_file(fd, map, len) != (int)len)
218 goto report_write_error;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000219 offset += len;
220 }
221
Wayne Davison85f14172004-12-02 17:16:19 +0000222 if (flush_write_file(fd) < 0)
223 goto report_write_error;
Wayne Davison76c21942004-01-02 08:29:49 +0000224
Wayne Davisona3221d22004-07-16 20:06:24 +0000225#ifdef HAVE_FTRUNCATE
Wayne Davisonfab65a52004-07-29 06:40:26 +0000226 if (inplace && fd != -1)
Wayne Davisona3221d22004-07-16 20:06:24 +0000227 ftruncate(fd, offset);
228#endif
229
Wayne Davison16417f82003-07-08 16:49:10 +0000230 if (do_progress)
231 end_progress(total_size);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000232
233 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
Wayne Davisonc52461f2004-07-29 07:37:27 +0000234 report_write_error:
Wayne Davisond62bcc12004-05-15 19:31:10 +0000235 rsyserr(FERROR, errno, "write failed on %s",
236 full_fname(fname));
Andrew Tridgell65417571998-11-03 07:08:27 +0000237 exit_cleanup(RERR_FILEIO);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000238 }
239
240 sum_end(file_sum1);
241
Wayne Davison7e5fa372004-07-20 21:35:58 +0000242 if (mapbuf)
243 unmap_file(mapbuf);
244
J.W. Schultzbc63ae32003-03-31 17:28:34 +0000245 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
Wayne Davisond2a918b2004-07-02 18:23:57 +0000246 if (verbose > 2)
J.W. Schultzbc63ae32003-03-31 17:28:34 +0000247 rprintf(FINFO,"got file_sum\n");
Wayne Davisond2a918b2004-07-02 18:23:57 +0000248 if (fd != -1 && memcmp(file_sum1, file_sum2, MD4_SUM_LENGTH) != 0)
J.W. Schultzbc63ae32003-03-31 17:28:34 +0000249 return 0;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000250 return 1;
251}
252
253
Wayne Davison8ed9d842004-07-19 17:05:01 +0000254static void discard_receive_data(int f_in, OFF_T length)
255{
Wayne Davison7e5fa372004-07-20 21:35:58 +0000256 receive_data(f_in, NULL, -1, 0, NULL, -1, length);
Wayne Davison8ed9d842004-07-19 17:05:01 +0000257}
258
259
Martin Poolb35d0d82002-04-08 04:10:20 +0000260/**
261 * main routine for receiver process.
262 *
263 * Receiver process runs on the same host as the generator process. */
Wayne Davison41cfde62004-11-03 20:30:31 +0000264int recv_files(int f_in, struct file_list *flist, char *local_name,
265 int f_in_name)
Wayne Davison17fadf72003-12-15 08:14:27 +0000266{
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000267 int next_gen_i = -1;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000268 int fd1,fd2;
269 STRUCT_STAT st;
Wayne Davison446e2392004-01-02 08:18:53 +0000270 char *fname, fbuf[MAXPATHLEN];
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000271 char template[MAXPATHLEN];
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000272 char fnametmp[MAXPATHLEN];
Wayne Davisona7260c42004-07-29 16:06:38 +0000273 char *fnamecmp, *partialptr;
David Dykstra375a4551998-10-26 21:42:38 +0000274 char fnamecmpbuf[MAXPATHLEN];
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000275 struct file_struct *file;
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000276 struct stats initial_stats;
Wayne Davison4e834af2004-06-14 15:09:36 +0000277 int save_make_backups = make_backups;
278 int i, recv_ok, phase = 0;
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000279
Wayne Davisond2a918b2004-07-02 18:23:57 +0000280 if (verbose > 2)
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000281 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000282
Wayne Davisoncb869c22004-02-10 17:28:59 +0000283 if (flist->hlink_pool) {
J.W. Schultz99350662004-02-10 03:23:37 +0000284 pool_destroy(flist->hlink_pool);
285 flist->hlink_pool = NULL;
286 }
287
Wayne Davison17fadf72003-12-15 08:14:27 +0000288 while (1) {
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000289 cleanup_disable();
290
291 i = read_int(f_in);
292 if (i == -1) {
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000293 if (read_batch) {
Wayne Davison41cfde62004-11-03 20:30:31 +0000294 if (next_gen_i != flist->count) {
295 do {
296 if (f_in_name >= 0
297 && next_gen_i >= 0)
298 read_byte(f_in_name);
299 } while (read_int(batch_gen_fd) != -1);
300 }
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000301 next_gen_i = -1;
302 }
303
Wayne Davison4e834af2004-06-14 15:09:36 +0000304 if (phase)
305 break;
Wayne Davison5ebab6c2004-07-19 16:37:30 +0000306
Wayne Davison4e834af2004-06-14 15:09:36 +0000307 phase = 1;
308 csum_length = SUM_LENGTH;
309 if (verbose > 2)
310 rprintf(FINFO, "recv_files phase=%d\n", phase);
311 send_msg(MSG_DONE, "", 0);
Wayne Davisonaec6b9f2005-01-10 10:03:10 +0000312 if (keep_partial && !partial_dir)
Wayne Davison4e834af2004-06-14 15:09:36 +0000313 make_backups = 0; /* prevents double backup */
314 continue;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000315 }
316
317 if (i < 0 || i >= flist->count) {
Wayne Davison17fadf72003-12-15 08:14:27 +0000318 rprintf(FERROR,"Invalid file index %d in recv_files (count=%d)\n",
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000319 i, flist->count);
Andrew Tridgell65417571998-11-03 07:08:27 +0000320 exit_cleanup(RERR_PROTOCOL);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000321 }
322
323 file = flist->files[i];
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000324
Wayne Davison97feb552004-01-13 18:22:43 +0000325 stats.current_file_index = i;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000326 stats.num_transferred_files++;
327 stats.total_transferred_size += file->length;
Wayne Davison925c5172004-01-02 08:38:35 +0000328 cleanup_got_literal = 0;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000329
330 if (local_name)
331 fname = local_name;
Wayne Davison446e2392004-01-02 08:18:53 +0000332 else
Wayne Davison3fef5362004-01-22 04:38:18 +0000333 fname = f_name_to(file, fbuf);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000334
335 if (dry_run) {
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000336 if (!am_server && verbose) /* log the transfer */
337 rprintf(FINFO, "%s\n", safe_fname(fname));
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000338 continue;
339 }
340
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000341 initial_stats = stats;
342
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000343 if (verbose > 2)
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000344 rprintf(FINFO, "recv_files(%s)\n", safe_fname(fname));
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000345
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000346 if (read_batch) {
347 while (i > next_gen_i) {
Wayne Davison41cfde62004-11-03 20:30:31 +0000348 if (f_in_name >= 0 && next_gen_i >= 0)
349 read_byte(f_in_name);
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000350 next_gen_i = read_int(batch_gen_fd);
351 if (next_gen_i == -1)
352 next_gen_i = flist->count;
353 }
354 if (i < next_gen_i) {
355 rprintf(FINFO, "skipping update for \"%s\"\n",
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000356 safe_fname(fname));
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000357 discard_receive_data(f_in, file->length);
358 continue;
359 }
Wayne Davison41cfde62004-11-03 20:30:31 +0000360 next_gen_i = -1;
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000361 }
362
Wayne Davison78424182005-01-25 10:39:14 +0000363 if (server_filter_list.head
364 && check_filter(&server_filter_list, fname,
Wayne Davison5ebab6c2004-07-19 16:37:30 +0000365 S_ISDIR(file->mode)) < 0) {
Wayne Davison33eff8b2004-07-30 07:02:37 +0000366 rprintf(FERROR, "attempt to hack rsync failed.\n");
367 exit_cleanup(RERR_PROTOCOL);
Wayne Davison5ebab6c2004-07-19 16:37:30 +0000368 }
369
Wayne Davison41cfde62004-11-03 20:30:31 +0000370 partialptr = partial_dir ? partial_dir_fname(fname) : fname;
Wayne Davisona7260c42004-07-29 16:06:38 +0000371
Wayne Davison41cfde62004-11-03 20:30:31 +0000372 if (f_in_name >= 0) {
Wayne Davisonee297522004-11-27 17:56:58 +0000373 uchar j;
374 switch (j = read_byte(f_in_name)) {
Wayne Davison41cfde62004-11-03 20:30:31 +0000375 case FNAMECMP_FNAME:
376 fnamecmp = fname;
377 break;
378 case FNAMECMP_PARTIAL_DIR:
379 fnamecmp = partialptr ? partialptr : fname;
380 break;
381 case FNAMECMP_BACKUP:
382 fnamecmp = get_backup_name(fname);
383 break;
Wayne Davison41cfde62004-11-03 20:30:31 +0000384 default:
Wayne Davisonc56595d2005-01-15 21:14:27 +0000385 if (j >= basis_dir_cnt) {
386 rprintf(FERROR,
387 "invalid basis_dir index: %d.\n",
388 j);
389 exit_cleanup(RERR_PROTOCOL);
390 }
Wayne Davison41cfde62004-11-03 20:30:31 +0000391 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
Wayne Davisonee297522004-11-27 17:56:58 +0000392 basis_dir[j], fname);
Wayne Davison41cfde62004-11-03 20:30:31 +0000393 fnamecmp = fnamecmpbuf;
394 break;
395 }
396 } else
397 fnamecmp = fname;
Wayne Davisondc55d7b2004-09-07 21:34:26 +0000398
Wayne Davison17fadf72003-12-15 08:14:27 +0000399 /* open the file */
Andrew Tridgell8c9fd201999-10-25 22:04:09 +0000400 fd1 = do_open(fnamecmp, O_RDONLY, 0);
David Dykstra375a4551998-10-26 21:42:38 +0000401
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000402 if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
Wayne Davisond62bcc12004-05-15 19:31:10 +0000403 rsyserr(FERROR, errno, "fstat %s failed",
404 full_fname(fnamecmp));
Wayne Davison8ed9d842004-07-19 17:05:01 +0000405 discard_receive_data(f_in, file->length);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000406 close(fd1);
407 continue;
408 }
409
J.W. Schultz350e4e42003-09-04 05:49:50 +0000410 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
411 /* this special handling for directories
412 * wouldn't be necessary if robust_rename()
413 * and the underlying robust_unlink could cope
414 * with directories
415 */
Wayne Davisonea425412003-09-11 04:53:05 +0000416 rprintf(FERROR,"recv_files: %s is a directory\n",
417 full_fname(fnamecmp));
Wayne Davison8ed9d842004-07-19 17:05:01 +0000418 discard_receive_data(f_in, file->length);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000419 close(fd1);
420 continue;
421 }
422
J.W. Schultz350e4e42003-09-04 05:49:50 +0000423 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
424 close(fd1);
425 fd1 = -1;
J.W. Schultz350e4e42003-09-04 05:49:50 +0000426 }
427
Andrew Tridgell4df9f361999-10-31 04:28:03 +0000428 if (fd1 != -1 && !preserve_perms) {
J.W. Schultz85ed0aa2003-03-21 07:27:31 +0000429 /* if the file exists already and we aren't preserving
Wayne Davison17fadf72003-12-15 08:14:27 +0000430 * permissions then act as though the remote end sent
431 * us the file permissions we already have */
Andrew Tridgell4df9f361999-10-31 04:28:03 +0000432 file->mode = st.st_mode;
433 }
434
Wayne Davisona3221d22004-07-16 20:06:24 +0000435 /* We now check to see if we are writing file "inplace" */
436 if (inplace) {
Wayne Davisondc55d7b2004-09-07 21:34:26 +0000437 fd2 = do_open(fname, O_WRONLY|O_CREAT, 0);
Wayne Davisona3221d22004-07-16 20:06:24 +0000438 if (fd2 == -1) {
439 rsyserr(FERROR, errno, "open %s failed",
Wayne Davisondc55d7b2004-09-07 21:34:26 +0000440 full_fname(fname));
Wayne Davison8ed9d842004-07-19 17:05:01 +0000441 discard_receive_data(f_in, file->length);
Wayne Davisona3221d22004-07-16 20:06:24 +0000442 if (fd1 != -1)
443 close(fd1);
444 continue;
445 }
446 } else {
447 if (!get_tmpname(fnametmp,fname)) {
Wayne Davison8ed9d842004-07-19 17:05:01 +0000448 discard_receive_data(f_in, file->length);
Wayne Davisona3221d22004-07-16 20:06:24 +0000449 if (fd1 != -1)
450 close(fd1);
451 continue;
452 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000453
Wayne Davisona3221d22004-07-16 20:06:24 +0000454 strlcpy(template, fnametmp, sizeof template);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000455
Wayne Davisona3221d22004-07-16 20:06:24 +0000456 /* we initially set the perms without the
457 * setuid/setgid bits to ensure that there is no race
458 * condition. They are then correctly updated after
459 * the lchown. Thanks to snabb@epipe.fi for pointing
460 * this out. We also set it initially without group
461 * access because of a similar race condition. */
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000462 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
Wayne Davison17fadf72003-12-15 08:14:27 +0000463
Wayne Davisona3221d22004-07-16 20:06:24 +0000464 /* in most cases parent directories will already exist
465 * because their information should have been previously
466 * transferred, but that may not be the case with -R */
467 if (fd2 == -1 && relative_paths && errno == ENOENT
468 && create_directory_path(fnametmp, orig_umask) == 0) {
469 strlcpy(fnametmp, template, sizeof fnametmp);
470 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
471 }
472 if (fd2 == -1) {
473 rsyserr(FERROR, errno, "mkstemp %s failed",
474 full_fname(fnametmp));
Wayne Davison8ed9d842004-07-19 17:05:01 +0000475 discard_receive_data(f_in, file->length);
Wayne Davisona3221d22004-07-16 20:06:24 +0000476 if (fd1 != -1)
477 close(fd1);
478 continue;
479 }
480
Wayne Davisona7260c42004-07-29 16:06:38 +0000481 if (partialptr)
482 cleanup_set(fnametmp, partialptr, file, fd1, fd2);
Wayne Davisona3221d22004-07-16 20:06:24 +0000483 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000484
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000485 if (!am_server && verbose) /* log the transfer */
486 rprintf(FINFO, "%s\n", safe_fname(fname));
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000487
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000488 /* recv file data */
Wayne Davison7e5fa372004-07-20 21:35:58 +0000489 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
490 fname, fd2, file->length);
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000491
492 log_recv(file, &initial_stats);
Wayne Davison17fadf72003-12-15 08:14:27 +0000493
Wayne Davisond2a918b2004-07-02 18:23:57 +0000494 if (fd1 != -1)
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000495 close(fd1);
Wayne Davison9f27cd82004-04-27 19:51:33 +0000496 if (close(fd2) < 0) {
Wayne Davisond62bcc12004-05-15 19:31:10 +0000497 rsyserr(FERROR, errno, "close failed on %s",
498 full_fname(fnametmp));
Wayne Davison9f27cd82004-04-27 19:51:33 +0000499 exit_cleanup(RERR_FILEIO);
500 }
Wayne Davison17fadf72003-12-15 08:14:27 +0000501
Wayne Davisonaec6b9f2005-01-10 10:03:10 +0000502 if (recv_ok || inplace) {
503 finish_transfer(fname, fnametmp, file, recv_ok, 1);
504 if (partialptr != fname && fnamecmp == partialptr) {
505 do_unlink(partialptr);
506 handle_partial_dir(partialptr, PDIR_DELETE);
507 }
508 } else if (keep_partial && partialptr
509 && handle_partial_dir(partialptr, PDIR_CREATE)) {
510 finish_transfer(partialptr, fnametmp, file, recv_ok,
511 !partial_dir);
512 } else {
Wayne Davisona7260c42004-07-29 16:06:38 +0000513 partialptr = NULL;
Wayne Davison55e50d82004-05-13 07:08:22 +0000514 do_unlink(fnametmp);
Wayne Davisona7260c42004-07-29 16:06:38 +0000515 }
516
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000517 cleanup_disable();
Andrew Tridgell554e0a82000-01-23 07:36:56 +0000518
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000519 if (!recv_ok) {
Wayne Davison007e3c02004-07-22 08:16:35 +0000520 int msgtype = csum_length == SUM_LENGTH || read_batch ?
521 FERROR : FINFO;
522 if (msgtype == FERROR || verbose) {
Wayne Davisona7260c42004-07-29 16:06:38 +0000523 char *errstr, *redostr, *keptstr;
524 if (!(keep_partial && partialptr) && !inplace)
525 keptstr = "discarded";
526 else if (partial_dir)
527 keptstr = "put into partial-dir";
528 else
529 keptstr = "retained";
Wayne Davison007e3c02004-07-22 08:16:35 +0000530 if (msgtype == FERROR) {
531 errstr = "ERROR";
532 redostr = "";
533 } else {
534 errstr = "WARNING";
535 redostr = " (will try again)";
536 }
537 rprintf(msgtype,
Wayne Davisona7260c42004-07-29 16:06:38 +0000538 "%s: %s failed verification -- update %s%s.\n",
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000539 errstr, safe_fname(fname),
540 keptstr, redostr);
Wayne Davison007e3c02004-07-22 08:16:35 +0000541 }
Wayne Davisone2bc4122004-07-22 04:15:18 +0000542 if (csum_length != SUM_LENGTH) {
Wayne Davison0569a132004-01-15 07:42:25 +0000543 char buf[4];
Wayne Davison0569a132004-01-15 07:42:25 +0000544 SIVAL(buf, 0, i);
545 send_msg(MSG_REDO, buf, 4);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000546 }
547 }
548 }
Wayne Davison4e834af2004-06-14 15:09:36 +0000549 make_backups = save_make_backups;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000550
Wayne Davisonb98f0402005-01-23 07:27:01 +0000551 if (delete_after && recurse && !local_name && flist->count > 0)
Wayne Davison3f55bd52004-01-08 00:45:41 +0000552 delete_files(flist);
Andrew Tridgell57df1711999-11-08 13:03:05 +0000553
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000554 if (verbose > 2)
555 rprintf(FINFO,"recv_files finished\n");
Wayne Davison17fadf72003-12-15 08:14:27 +0000556
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000557 return 0;
558}