blob: 3e72768b3a07d49aed29ef31310ba121c9076e56 [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;
24extern int recurse;
Wayne Davison92b9eb92004-03-23 16:50:40 +000025extern int delete_after;
26extern int max_delete;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000027extern int csum_length;
28extern struct stats stats;
29extern int dry_run;
Wayne Davison16cc9ca2004-07-21 23:59:37 +000030extern int read_batch;
31extern int batch_gen_fd;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000032extern int am_server;
Wayne Davison41cfde62004-11-03 20:30:31 +000033extern int protocol_version;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000034extern int relative_paths;
Wayne Davison566fce32004-06-11 07:40:48 +000035extern int keep_dirlinks;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000036extern int preserve_hard_links;
Wayne Davison92b9eb92004-03-23 16:50:40 +000037extern int preserve_perms;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000038extern int cvs_exclude;
39extern int io_error;
40extern char *tmpdir;
Wayne Davisona7260c42004-07-29 16:06:38 +000041extern char *partial_dir;
Wayne Davisonee297522004-11-27 17:56:58 +000042extern char *basis_dir[];
Wayne Davisonc56595d2005-01-15 21:14:27 +000043extern int basis_dir_cnt;
David Dykstra53dd3131998-11-24 19:01:24 +000044extern int make_backups;
Wayne Davison16417f82003-07-08 16:49:10 +000045extern int do_progress;
Wayne Davisond74a2e32003-08-01 07:58:47 +000046extern char *backup_dir;
David Dykstra53dd3131998-11-24 19:01:24 +000047extern char *backup_suffix;
Wayne Davisond74a2e32003-08-01 07:58:47 +000048extern int backup_suffix_len;
Wayne Davison925c5172004-01-02 08:38:35 +000049extern int cleanup_got_literal;
Wayne Davison92b9eb92004-03-23 16:50:40 +000050extern int module_id;
51extern int ignore_errors;
52extern int orig_umask;
Wayne Davison55e50d82004-05-13 07:08:22 +000053extern int keep_partial;
Wayne Davisonba582f72004-05-21 08:27:04 +000054extern int checksum_seed;
Wayne Davisona3221d22004-07-16 20:06:24 +000055extern int inplace;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000056
Wayne Davison5ebab6c2004-07-19 16:37:30 +000057extern struct exclude_list_struct server_exclude_list;
58
59
Wayne Davisond74a2e32003-08-01 07:58:47 +000060static void delete_one(char *fn, int is_dir)
Andrew Tridgell2f03f951998-07-25 02:25:22 +000061{
Wayne Davisond74a2e32003-08-01 07:58:47 +000062 if (!is_dir) {
63 if (robust_unlink(fn) != 0) {
Wayne Davisond62bcc12004-05-15 19:31:10 +000064 rsyserr(FERROR, errno, "delete_one: unlink %s failed",
65 full_fname(fn));
Wayne Davisond2a918b2004-07-02 18:23:57 +000066 } else if (verbose)
Wayne Davisonecc81fc2004-07-26 16:36:59 +000067 rprintf(FINFO, "deleting %s\n", safe_fname(fn));
Wayne Davison17fadf72003-12-15 08:14:27 +000068 } else {
Wayne Davisond74a2e32003-08-01 07:58:47 +000069 if (do_rmdir(fn) != 0) {
70 if (errno != ENOTEMPTY && errno != EEXIST) {
Wayne Davisond62bcc12004-05-15 19:31:10 +000071 rsyserr(FERROR, errno,
72 "delete_one: rmdir %s failed",
73 full_fname(fn));
Wayne Davisond74a2e32003-08-01 07:58:47 +000074 }
Wayne Davisonecc81fc2004-07-26 16:36:59 +000075 } else if (verbose) {
76 rprintf(FINFO, "deleting directory %s\n",
77 safe_fname(fn));
78 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +000079 }
80}
81
82
Wayne Davisond74a2e32003-08-01 07:58:47 +000083static int is_backup_file(char *fn)
84{
85 int k = strlen(fn) - backup_suffix_len;
86 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
87}
Andrew Tridgell2f03f951998-07-25 02:25:22 +000088
89
Wayne Davison1e82e2c2004-03-23 16:36:00 +000090/* This deletes any files on the receiving side that are not present
91 * on the sending side. */
Andrew Tridgell6957ae32000-01-24 11:20:25 +000092void delete_files(struct file_list *flist)
Andrew Tridgell2f03f951998-07-25 02:25:22 +000093{
94 struct file_list *local_file_list;
95 int i, j;
Wayne Davisona24639b2004-01-22 04:40:33 +000096 char *argv[1], fbuf[MAXPATHLEN];
Andrew Tridgell0b73ca12000-01-23 11:43:04 +000097 static int deletion_count;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000098
99 if (cvs_exclude)
100 add_cvs_excludes();
101
Andrew Tridgellef55c682000-03-21 04:06:04 +0000102 if (io_error && !(lp_ignore_errors(module_id) || ignore_errors)) {
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000103 rprintf(FINFO,"IO error encountered - skipping file deletion\n");
104 return;
105 }
106
Wayne Davison1e82e2c2004-03-23 16:36:00 +0000107 for (j = 0; j < flist->count; j++) {
Wayne Davisona5342642004-01-27 01:05:12 +0000108 if (!(flist->files[j]->flags & FLAG_TOP_DIR)
109 || !S_ISDIR(flist->files[j]->mode))
110 continue;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000111
Wayne Davisona24639b2004-01-22 04:40:33 +0000112 argv[0] = f_name_to(flist->files[j], fbuf);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000113
Wayne Davisona24639b2004-01-22 04:40:33 +0000114 if (!(local_file_list = send_file_list(-1, 1, argv)))
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000115 continue;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000116
117 if (verbose > 1)
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000118 rprintf(FINFO, "deleting in %s\n", safe_fname(fbuf));
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000119
Wayne Davison446e2392004-01-02 08:18:53 +0000120 for (i = local_file_list->count-1; i >= 0; i--) {
Wayne Davison08b1b482004-10-18 20:41:57 +0000121 if (max_delete && deletion_count >= max_delete)
Wayne Davison92b9eb92004-03-23 16:50:40 +0000122 break;
123 if (!local_file_list->files[i]->basename)
124 continue;
125 if (flist_find(flist,local_file_list->files[i]) < 0) {
David Dykstra53dd3131998-11-24 19:01:24 +0000126 char *f = f_name(local_file_list->files[i]);
Wayne Davisonf80a8522004-10-27 06:34:13 +0000127 int mode = local_file_list->files[i]->mode;
128 if (make_backups && (backup_dir || !is_backup_file(f))
129 && !S_ISDIR(mode)) {
Wayne Davisond2a918b2004-07-02 18:23:57 +0000130 make_backup(f);
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000131 if (verbose) {
132 rprintf(FINFO, "deleting %s\n",
133 safe_fname(f));
134 }
Wayne Davisonf80a8522004-10-27 06:34:13 +0000135 } else
Wayne Davisond74a2e32003-08-01 07:58:47 +0000136 delete_one(f, S_ISDIR(mode) != 0);
Wayne Davison31f3b682003-08-01 08:20:53 +0000137 deletion_count++;
David Dykstra53dd3131998-11-24 19:01:24 +0000138 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000139 }
140 flist_free(local_file_list);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000141 }
142}
143
144
J.W. Schultz52d3e102003-03-26 11:04:14 +0000145/*
146 * get_tmpname() - create a tmp filename for a given filename
147 *
148 * If a tmpdir is defined, use that as the directory to
149 * put it in. Otherwise, the tmp filename is in the same
150 * directory as the given name. Note that there may be no
151 * directory at all in the given name!
Wayne Davison17fadf72003-12-15 08:14:27 +0000152 *
J.W. Schultz52d3e102003-03-26 11:04:14 +0000153 * The tmp filename is basically the given filename with a
154 * dot prepended, and .XXXXXX appended (for mkstemp() to
155 * put its unique gunk in). Take care to not exceed
156 * either the MAXPATHLEN or NAME_MAX, esp. the last, as
157 * the basename basically becomes 8 chars longer. In that
158 * case, the original name is shortened sufficiently to
159 * make it all fit.
Wayne Davison17fadf72003-12-15 08:14:27 +0000160 *
J.W. Schultz52d3e102003-03-26 11:04:14 +0000161 * Of course, there's no real reason for the tmp name to
162 * look like the original, except to satisfy us humans.
163 * As long as it's unique, rsync will work.
164 */
165
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000166static int get_tmpname(char *fnametmp, char *fname)
167{
168 char *f;
J.W. Schultz52d3e102003-03-26 11:04:14 +0000169 int length = 0;
170 int maxname;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000171
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000172 if (tmpdir) {
Wayne Davisona24639b2004-01-22 04:40:33 +0000173 /* Note: this can't overflow, so the return value is safe */
J.W. Schultzb7cee942004-01-20 03:37:04 +0000174 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
J.W. Schultz52d3e102003-03-26 11:04:14 +0000175 fnametmp[length++] = '/';
176 fnametmp[length] = '\0'; /* always NULL terminated */
Wayne Davison0c2ef5f2003-08-04 21:00:57 +0000177 }
J.W. Schultz52d3e102003-03-26 11:04:14 +0000178
Wayne Davison0c2ef5f2003-08-04 21:00:57 +0000179 if ((f = strrchr(fname, '/')) != NULL) {
J.W. Schultz52d3e102003-03-26 11:04:14 +0000180 ++f;
181 if (!tmpdir) {
182 length = f - fname;
Wayne Davison0c2ef5f2003-08-04 21:00:57 +0000183 /* copy up to and including the slash */
J.W. Schultz52d3e102003-03-26 11:04:14 +0000184 strlcpy(fnametmp, fname, length + 1);
Wayne Davison0c2ef5f2003-08-04 21:00:57 +0000185 }
Wayne Davison446e2392004-01-02 08:18:53 +0000186 } else
J.W. Schultz52d3e102003-03-26 11:04:14 +0000187 f = fname;
J.W. Schultz52d3e102003-03-26 11:04:14 +0000188 fnametmp[length++] = '.';
189 fnametmp[length] = '\0'; /* always NULL terminated */
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000190
J.W. Schultz52d3e102003-03-26 11:04:14 +0000191 maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000192
Wayne Davison0c2ef5f2003-08-04 21:00:57 +0000193 if (maxname < 1) {
J.W. Schultz52d3e102003-03-26 11:04:14 +0000194 rprintf(FERROR, "temporary filename too long: %s\n", fname);
195 fnametmp[0] = '\0';
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000196 return 0;
197 }
198
Wayne Davison17fadf72003-12-15 08:14:27 +0000199 strlcpy(fnametmp + length, f, maxname);
J.W. Schultz52d3e102003-03-26 11:04:14 +0000200 strcat(fnametmp + length, ".XXXXXX");
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000201
202 return 1;
203}
204
205
Wayne Davison7e5fa372004-07-20 21:35:58 +0000206static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
207 char *fname, int fd, OFF_T total_size)
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000208{
Wayne Davison7e5fa372004-07-20 21:35:58 +0000209 static char file_sum1[MD4_SUM_LENGTH];
210 static char file_sum2[MD4_SUM_LENGTH];
211 struct map_struct *mapbuf;
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000212 struct sum_struct sum;
Wayne Davison6c495e02005-01-01 21:08:17 +0000213 int32 len;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000214 OFF_T offset = 0;
Wayne Davison89e540e2004-07-29 06:59:30 +0000215 OFF_T offset2;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000216 char *data;
Wayne Davison6c495e02005-01-01 21:08:17 +0000217 int32 i;
Wayne Davisone1f67412004-06-30 07:27:30 +0000218 char *map = NULL;
Wayne Davison17fadf72003-12-15 08:14:27 +0000219
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000220 read_sum_head(f_in, &sum);
Wayne Davison17fadf72003-12-15 08:14:27 +0000221
Wayne Davison7e5fa372004-07-20 21:35:58 +0000222 if (fd_r >= 0 && size_r > 0) {
Wayne Davison6c495e02005-01-01 21:08:17 +0000223 OFF_T map_size = MAX((OFF_T)sum.blength * 2, 16*1024);
Wayne Davison96d910c2004-08-03 08:05:27 +0000224 mapbuf = map_file(fd_r, size_r, map_size, sum.blength);
Wayne Davison7e5fa372004-07-20 21:35:58 +0000225 if (verbose > 2) {
226 rprintf(FINFO, "recv mapped %s of size %.0f\n",
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000227 safe_fname(fname_r), (double)size_r);
Wayne Davison7e5fa372004-07-20 21:35:58 +0000228 }
229 } else
230 mapbuf = NULL;
231
Wayne Davisonba582f72004-05-21 08:27:04 +0000232 sum_init(checksum_seed);
Wayne Davison17fadf72003-12-15 08:14:27 +0000233
Wayne Davison3f55bd52004-01-08 00:45:41 +0000234 while ((i = recv_token(f_in, &data)) != 0) {
Wayne Davison16417f82003-07-08 16:49:10 +0000235 if (do_progress)
236 show_progress(offset, total_size);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000237
238 if (i > 0) {
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000239 if (verbose > 3) {
Andrew Tridgell5f808df2000-01-23 12:30:34 +0000240 rprintf(FINFO,"data recv %d at %.0f\n",
241 i,(double)offset);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000242 }
243
244 stats.literal_data += i;
245 cleanup_got_literal = 1;
Wayne Davison17fadf72003-12-15 08:14:27 +0000246
Wayne Davison6c495e02005-01-01 21:08:17 +0000247 sum_update(data, i);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000248
Wayne Davisonc52461f2004-07-29 07:37:27 +0000249 if (fd != -1 && write_file(fd,data,i) != i)
250 goto report_write_error;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000251 offset += i;
252 continue;
Wayne Davison17fadf72003-12-15 08:14:27 +0000253 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000254
255 i = -(i+1);
Wayne Davison6c495e02005-01-01 21:08:17 +0000256 offset2 = i * (OFF_T)sum.blength;
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000257 len = sum.blength;
Wayne Davisond2a918b2004-07-02 18:23:57 +0000258 if (i == (int)sum.count-1 && sum.remainder != 0)
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000259 len = sum.remainder;
Wayne Davison17fadf72003-12-15 08:14:27 +0000260
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000261 stats.matched_data += len;
Wayne Davison17fadf72003-12-15 08:14:27 +0000262
Wayne Davison6c495e02005-01-01 21:08:17 +0000263 if (verbose > 3) {
264 rprintf(FINFO,
265 "chunk[%d] of size %ld at %.0f offset=%.0f\n",
266 i, (long)len, (double)offset2, (double)offset);
267 }
Wayne Davison17fadf72003-12-15 08:14:27 +0000268
Wayne Davison446e2392004-01-02 08:18:53 +0000269 if (mapbuf) {
270 map = map_ptr(mapbuf,offset2,len);
Wayne Davison17fadf72003-12-15 08:14:27 +0000271
Andrew Tridgellc55f7022000-01-24 11:41:08 +0000272 see_token(map, len);
Wayne Davison6c495e02005-01-01 21:08:17 +0000273 sum_update(map, len);
Andrew Tridgellc55f7022000-01-24 11:41:08 +0000274 }
Wayne Davison17fadf72003-12-15 08:14:27 +0000275
Wayne Davisonfab65a52004-07-29 06:40:26 +0000276 if (inplace) {
Wayne Davison89e540e2004-07-29 06:59:30 +0000277 if (offset == offset2 && fd != -1) {
Wayne Davisonc52461f2004-07-29 07:37:27 +0000278 if (flush_write_file(fd) < 0)
279 goto report_write_error;
Wayne Davison89e540e2004-07-29 06:59:30 +0000280 offset += len;
281 if (do_lseek(fd, len, SEEK_CUR) != offset) {
Wayne Davisonfab65a52004-07-29 06:40:26 +0000282 rsyserr(FERROR, errno,
283 "lseek failed on %s",
284 full_fname(fname));
285 exit_cleanup(RERR_FILEIO);
286 }
Wayne Davison89e540e2004-07-29 06:59:30 +0000287 continue;
Wayne Davisona3221d22004-07-16 20:06:24 +0000288 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000289 }
Wayne Davisonc52461f2004-07-29 07:37:27 +0000290 if (fd != -1 && write_file(fd, map, len) != (int)len)
291 goto report_write_error;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000292 offset += len;
293 }
294
Wayne Davison85f14172004-12-02 17:16:19 +0000295 if (flush_write_file(fd) < 0)
296 goto report_write_error;
Wayne Davison76c21942004-01-02 08:29:49 +0000297
Wayne Davisona3221d22004-07-16 20:06:24 +0000298#ifdef HAVE_FTRUNCATE
Wayne Davisonfab65a52004-07-29 06:40:26 +0000299 if (inplace && fd != -1)
Wayne Davisona3221d22004-07-16 20:06:24 +0000300 ftruncate(fd, offset);
301#endif
302
Wayne Davison16417f82003-07-08 16:49:10 +0000303 if (do_progress)
304 end_progress(total_size);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000305
306 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
Wayne Davisonc52461f2004-07-29 07:37:27 +0000307 report_write_error:
Wayne Davisond62bcc12004-05-15 19:31:10 +0000308 rsyserr(FERROR, errno, "write failed on %s",
309 full_fname(fname));
Andrew Tridgell65417571998-11-03 07:08:27 +0000310 exit_cleanup(RERR_FILEIO);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000311 }
312
313 sum_end(file_sum1);
314
Wayne Davison7e5fa372004-07-20 21:35:58 +0000315 if (mapbuf)
316 unmap_file(mapbuf);
317
J.W. Schultzbc63ae32003-03-31 17:28:34 +0000318 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
Wayne Davisond2a918b2004-07-02 18:23:57 +0000319 if (verbose > 2)
J.W. Schultzbc63ae32003-03-31 17:28:34 +0000320 rprintf(FINFO,"got file_sum\n");
Wayne Davisond2a918b2004-07-02 18:23:57 +0000321 if (fd != -1 && memcmp(file_sum1, file_sum2, MD4_SUM_LENGTH) != 0)
J.W. Schultzbc63ae32003-03-31 17:28:34 +0000322 return 0;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000323 return 1;
324}
325
326
Wayne Davison8ed9d842004-07-19 17:05:01 +0000327static void discard_receive_data(int f_in, OFF_T length)
328{
Wayne Davison7e5fa372004-07-20 21:35:58 +0000329 receive_data(f_in, NULL, -1, 0, NULL, -1, length);
Wayne Davison8ed9d842004-07-19 17:05:01 +0000330}
331
332
Martin Poolb35d0d82002-04-08 04:10:20 +0000333/**
334 * main routine for receiver process.
335 *
336 * Receiver process runs on the same host as the generator process. */
Wayne Davison41cfde62004-11-03 20:30:31 +0000337int recv_files(int f_in, struct file_list *flist, char *local_name,
338 int f_in_name)
Wayne Davison17fadf72003-12-15 08:14:27 +0000339{
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000340 int next_gen_i = -1;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000341 int fd1,fd2;
342 STRUCT_STAT st;
Wayne Davison446e2392004-01-02 08:18:53 +0000343 char *fname, fbuf[MAXPATHLEN];
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000344 char template[MAXPATHLEN];
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000345 char fnametmp[MAXPATHLEN];
Wayne Davisona7260c42004-07-29 16:06:38 +0000346 char *fnamecmp, *partialptr;
David Dykstra375a4551998-10-26 21:42:38 +0000347 char fnamecmpbuf[MAXPATHLEN];
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000348 struct file_struct *file;
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000349 struct stats initial_stats;
Wayne Davison4e834af2004-06-14 15:09:36 +0000350 int save_make_backups = make_backups;
351 int i, recv_ok, phase = 0;
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000352
Wayne Davisond2a918b2004-07-02 18:23:57 +0000353 if (verbose > 2)
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000354 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000355
Wayne Davisoncb869c22004-02-10 17:28:59 +0000356 if (flist->hlink_pool) {
J.W. Schultz99350662004-02-10 03:23:37 +0000357 pool_destroy(flist->hlink_pool);
358 flist->hlink_pool = NULL;
359 }
360
Wayne Davison17fadf72003-12-15 08:14:27 +0000361 while (1) {
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000362 cleanup_disable();
363
364 i = read_int(f_in);
365 if (i == -1) {
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000366 if (read_batch) {
Wayne Davison41cfde62004-11-03 20:30:31 +0000367 if (next_gen_i != flist->count) {
368 do {
369 if (f_in_name >= 0
370 && next_gen_i >= 0)
371 read_byte(f_in_name);
372 } while (read_int(batch_gen_fd) != -1);
373 }
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000374 next_gen_i = -1;
375 }
376
Wayne Davison4e834af2004-06-14 15:09:36 +0000377 if (phase)
378 break;
Wayne Davison5ebab6c2004-07-19 16:37:30 +0000379
Wayne Davison4e834af2004-06-14 15:09:36 +0000380 phase = 1;
381 csum_length = SUM_LENGTH;
382 if (verbose > 2)
383 rprintf(FINFO, "recv_files phase=%d\n", phase);
384 send_msg(MSG_DONE, "", 0);
Wayne Davisonaec6b9f2005-01-10 10:03:10 +0000385 if (keep_partial && !partial_dir)
Wayne Davison4e834af2004-06-14 15:09:36 +0000386 make_backups = 0; /* prevents double backup */
387 continue;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000388 }
389
390 if (i < 0 || i >= flist->count) {
Wayne Davison17fadf72003-12-15 08:14:27 +0000391 rprintf(FERROR,"Invalid file index %d in recv_files (count=%d)\n",
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000392 i, flist->count);
Andrew Tridgell65417571998-11-03 07:08:27 +0000393 exit_cleanup(RERR_PROTOCOL);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000394 }
395
396 file = flist->files[i];
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000397
Wayne Davison97feb552004-01-13 18:22:43 +0000398 stats.current_file_index = i;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000399 stats.num_transferred_files++;
400 stats.total_transferred_size += file->length;
Wayne Davison925c5172004-01-02 08:38:35 +0000401 cleanup_got_literal = 0;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000402
403 if (local_name)
404 fname = local_name;
Wayne Davison446e2392004-01-02 08:18:53 +0000405 else
Wayne Davison3fef5362004-01-22 04:38:18 +0000406 fname = f_name_to(file, fbuf);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000407
408 if (dry_run) {
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000409 if (!am_server && verbose) /* log the transfer */
410 rprintf(FINFO, "%s\n", safe_fname(fname));
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000411 continue;
412 }
413
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000414 initial_stats = stats;
415
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000416 if (verbose > 2)
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000417 rprintf(FINFO, "recv_files(%s)\n", safe_fname(fname));
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000418
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000419 if (read_batch) {
420 while (i > next_gen_i) {
Wayne Davison41cfde62004-11-03 20:30:31 +0000421 if (f_in_name >= 0 && next_gen_i >= 0)
422 read_byte(f_in_name);
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000423 next_gen_i = read_int(batch_gen_fd);
424 if (next_gen_i == -1)
425 next_gen_i = flist->count;
426 }
427 if (i < next_gen_i) {
428 rprintf(FINFO, "skipping update for \"%s\"\n",
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000429 safe_fname(fname));
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000430 discard_receive_data(f_in, file->length);
431 continue;
432 }
Wayne Davison41cfde62004-11-03 20:30:31 +0000433 next_gen_i = -1;
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000434 }
435
Wayne Davison5ebab6c2004-07-19 16:37:30 +0000436 if (server_exclude_list.head
437 && check_exclude(&server_exclude_list, fname,
438 S_ISDIR(file->mode)) < 0) {
Wayne Davison33eff8b2004-07-30 07:02:37 +0000439 rprintf(FERROR, "attempt to hack rsync failed.\n");
440 exit_cleanup(RERR_PROTOCOL);
Wayne Davison5ebab6c2004-07-19 16:37:30 +0000441 }
442
Wayne Davison41cfde62004-11-03 20:30:31 +0000443 partialptr = partial_dir ? partial_dir_fname(fname) : fname;
Wayne Davisona7260c42004-07-29 16:06:38 +0000444
Wayne Davison41cfde62004-11-03 20:30:31 +0000445 if (f_in_name >= 0) {
Wayne Davisonee297522004-11-27 17:56:58 +0000446 uchar j;
447 switch (j = read_byte(f_in_name)) {
Wayne Davison41cfde62004-11-03 20:30:31 +0000448 case FNAMECMP_FNAME:
449 fnamecmp = fname;
450 break;
451 case FNAMECMP_PARTIAL_DIR:
452 fnamecmp = partialptr ? partialptr : fname;
453 break;
454 case FNAMECMP_BACKUP:
455 fnamecmp = get_backup_name(fname);
456 break;
Wayne Davison41cfde62004-11-03 20:30:31 +0000457 default:
Wayne Davisonc56595d2005-01-15 21:14:27 +0000458 if (j >= basis_dir_cnt) {
459 rprintf(FERROR,
460 "invalid basis_dir index: %d.\n",
461 j);
462 exit_cleanup(RERR_PROTOCOL);
463 }
Wayne Davison41cfde62004-11-03 20:30:31 +0000464 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
Wayne Davisonee297522004-11-27 17:56:58 +0000465 basis_dir[j], fname);
Wayne Davison41cfde62004-11-03 20:30:31 +0000466 fnamecmp = fnamecmpbuf;
467 break;
468 }
469 } else
470 fnamecmp = fname;
Wayne Davisondc55d7b2004-09-07 21:34:26 +0000471
Wayne Davison17fadf72003-12-15 08:14:27 +0000472 /* open the file */
Andrew Tridgell8c9fd201999-10-25 22:04:09 +0000473 fd1 = do_open(fnamecmp, O_RDONLY, 0);
David Dykstra375a4551998-10-26 21:42:38 +0000474
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000475 if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
Wayne Davisond62bcc12004-05-15 19:31:10 +0000476 rsyserr(FERROR, errno, "fstat %s failed",
477 full_fname(fnamecmp));
Wayne Davison8ed9d842004-07-19 17:05:01 +0000478 discard_receive_data(f_in, file->length);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000479 close(fd1);
480 continue;
481 }
482
J.W. Schultz350e4e42003-09-04 05:49:50 +0000483 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
484 /* this special handling for directories
485 * wouldn't be necessary if robust_rename()
486 * and the underlying robust_unlink could cope
487 * with directories
488 */
Wayne Davisonea425412003-09-11 04:53:05 +0000489 rprintf(FERROR,"recv_files: %s is a directory\n",
490 full_fname(fnamecmp));
Wayne Davison8ed9d842004-07-19 17:05:01 +0000491 discard_receive_data(f_in, file->length);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000492 close(fd1);
493 continue;
494 }
495
J.W. Schultz350e4e42003-09-04 05:49:50 +0000496 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
497 close(fd1);
498 fd1 = -1;
J.W. Schultz350e4e42003-09-04 05:49:50 +0000499 }
500
Andrew Tridgell4df9f361999-10-31 04:28:03 +0000501 if (fd1 != -1 && !preserve_perms) {
J.W. Schultz85ed0aa2003-03-21 07:27:31 +0000502 /* if the file exists already and we aren't preserving
Wayne Davison17fadf72003-12-15 08:14:27 +0000503 * permissions then act as though the remote end sent
504 * us the file permissions we already have */
Andrew Tridgell4df9f361999-10-31 04:28:03 +0000505 file->mode = st.st_mode;
506 }
507
Wayne Davisona3221d22004-07-16 20:06:24 +0000508 /* We now check to see if we are writing file "inplace" */
509 if (inplace) {
Wayne Davisondc55d7b2004-09-07 21:34:26 +0000510 fd2 = do_open(fname, O_WRONLY|O_CREAT, 0);
Wayne Davisona3221d22004-07-16 20:06:24 +0000511 if (fd2 == -1) {
512 rsyserr(FERROR, errno, "open %s failed",
Wayne Davisondc55d7b2004-09-07 21:34:26 +0000513 full_fname(fname));
Wayne Davison8ed9d842004-07-19 17:05:01 +0000514 discard_receive_data(f_in, file->length);
Wayne Davisona3221d22004-07-16 20:06:24 +0000515 if (fd1 != -1)
516 close(fd1);
517 continue;
518 }
519 } else {
520 if (!get_tmpname(fnametmp,fname)) {
Wayne Davison8ed9d842004-07-19 17:05:01 +0000521 discard_receive_data(f_in, file->length);
Wayne Davisona3221d22004-07-16 20:06:24 +0000522 if (fd1 != -1)
523 close(fd1);
524 continue;
525 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000526
Wayne Davisona3221d22004-07-16 20:06:24 +0000527 strlcpy(template, fnametmp, sizeof template);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000528
Wayne Davisona3221d22004-07-16 20:06:24 +0000529 /* we initially set the perms without the
530 * setuid/setgid bits to ensure that there is no race
531 * condition. They are then correctly updated after
532 * the lchown. Thanks to snabb@epipe.fi for pointing
533 * this out. We also set it initially without group
534 * access because of a similar race condition. */
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000535 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
Wayne Davison17fadf72003-12-15 08:14:27 +0000536
Wayne Davisona3221d22004-07-16 20:06:24 +0000537 /* in most cases parent directories will already exist
538 * because their information should have been previously
539 * transferred, but that may not be the case with -R */
540 if (fd2 == -1 && relative_paths && errno == ENOENT
541 && create_directory_path(fnametmp, orig_umask) == 0) {
542 strlcpy(fnametmp, template, sizeof fnametmp);
543 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
544 }
545 if (fd2 == -1) {
546 rsyserr(FERROR, errno, "mkstemp %s failed",
547 full_fname(fnametmp));
Wayne Davison8ed9d842004-07-19 17:05:01 +0000548 discard_receive_data(f_in, file->length);
Wayne Davisona3221d22004-07-16 20:06:24 +0000549 if (fd1 != -1)
550 close(fd1);
551 continue;
552 }
553
Wayne Davisona7260c42004-07-29 16:06:38 +0000554 if (partialptr)
555 cleanup_set(fnametmp, partialptr, file, fd1, fd2);
Wayne Davisona3221d22004-07-16 20:06:24 +0000556 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000557
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000558 if (!am_server && verbose) /* log the transfer */
559 rprintf(FINFO, "%s\n", safe_fname(fname));
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000560
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000561 /* recv file data */
Wayne Davison7e5fa372004-07-20 21:35:58 +0000562 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
563 fname, fd2, file->length);
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000564
565 log_recv(file, &initial_stats);
Wayne Davison17fadf72003-12-15 08:14:27 +0000566
Wayne Davisond2a918b2004-07-02 18:23:57 +0000567 if (fd1 != -1)
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000568 close(fd1);
Wayne Davison9f27cd82004-04-27 19:51:33 +0000569 if (close(fd2) < 0) {
Wayne Davisond62bcc12004-05-15 19:31:10 +0000570 rsyserr(FERROR, errno, "close failed on %s",
571 full_fname(fnametmp));
Wayne Davison9f27cd82004-04-27 19:51:33 +0000572 exit_cleanup(RERR_FILEIO);
573 }
Wayne Davison17fadf72003-12-15 08:14:27 +0000574
Wayne Davisonaec6b9f2005-01-10 10:03:10 +0000575 if (recv_ok || inplace) {
576 finish_transfer(fname, fnametmp, file, recv_ok, 1);
577 if (partialptr != fname && fnamecmp == partialptr) {
578 do_unlink(partialptr);
579 handle_partial_dir(partialptr, PDIR_DELETE);
580 }
581 } else if (keep_partial && partialptr
582 && handle_partial_dir(partialptr, PDIR_CREATE)) {
583 finish_transfer(partialptr, fnametmp, file, recv_ok,
584 !partial_dir);
585 } else {
Wayne Davisona7260c42004-07-29 16:06:38 +0000586 partialptr = NULL;
Wayne Davison55e50d82004-05-13 07:08:22 +0000587 do_unlink(fnametmp);
Wayne Davisona7260c42004-07-29 16:06:38 +0000588 }
589
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000590 cleanup_disable();
Andrew Tridgell554e0a82000-01-23 07:36:56 +0000591
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000592 if (!recv_ok) {
Wayne Davison007e3c02004-07-22 08:16:35 +0000593 int msgtype = csum_length == SUM_LENGTH || read_batch ?
594 FERROR : FINFO;
595 if (msgtype == FERROR || verbose) {
Wayne Davisona7260c42004-07-29 16:06:38 +0000596 char *errstr, *redostr, *keptstr;
597 if (!(keep_partial && partialptr) && !inplace)
598 keptstr = "discarded";
599 else if (partial_dir)
600 keptstr = "put into partial-dir";
601 else
602 keptstr = "retained";
Wayne Davison007e3c02004-07-22 08:16:35 +0000603 if (msgtype == FERROR) {
604 errstr = "ERROR";
605 redostr = "";
606 } else {
607 errstr = "WARNING";
608 redostr = " (will try again)";
609 }
610 rprintf(msgtype,
Wayne Davisona7260c42004-07-29 16:06:38 +0000611 "%s: %s failed verification -- update %s%s.\n",
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000612 errstr, safe_fname(fname),
613 keptstr, redostr);
Wayne Davison007e3c02004-07-22 08:16:35 +0000614 }
Wayne Davisone2bc4122004-07-22 04:15:18 +0000615 if (csum_length != SUM_LENGTH) {
Wayne Davison0569a132004-01-15 07:42:25 +0000616 char buf[4];
Wayne Davison0569a132004-01-15 07:42:25 +0000617 SIVAL(buf, 0, i);
618 send_msg(MSG_REDO, buf, 4);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000619 }
620 }
621 }
Wayne Davison4e834af2004-06-14 15:09:36 +0000622 make_backups = save_make_backups;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000623
Wayne Davisone76ca142004-07-22 02:52:57 +0000624 if (delete_after && recurse && !local_name && flist->count > 0)
Wayne Davison3f55bd52004-01-08 00:45:41 +0000625 delete_files(flist);
Andrew Tridgell57df1711999-11-08 13:03:05 +0000626
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000627 if (verbose > 2)
628 rprintf(FINFO,"recv_files finished\n");
Wayne Davison17fadf72003-12-15 08:14:27 +0000629
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000630 return 0;
631}