blob: 8c91ea6f06bf31ea8828d7240dd1d1c8106fd5a7 [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[];
David Dykstra53dd3131998-11-24 19:01:24 +000043extern int make_backups;
Wayne Davison16417f82003-07-08 16:49:10 +000044extern int do_progress;
Wayne Davisond74a2e32003-08-01 07:58:47 +000045extern char *backup_dir;
David Dykstra53dd3131998-11-24 19:01:24 +000046extern char *backup_suffix;
Wayne Davisond74a2e32003-08-01 07:58:47 +000047extern int backup_suffix_len;
Wayne Davison925c5172004-01-02 08:38:35 +000048extern int cleanup_got_literal;
Wayne Davison92b9eb92004-03-23 16:50:40 +000049extern int module_id;
50extern int ignore_errors;
51extern int orig_umask;
Wayne Davison55e50d82004-05-13 07:08:22 +000052extern int keep_partial;
Wayne Davisonba582f72004-05-21 08:27:04 +000053extern int checksum_seed;
Wayne Davisona3221d22004-07-16 20:06:24 +000054extern int inplace;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000055
Wayne Davison5ebab6c2004-07-19 16:37:30 +000056extern struct exclude_list_struct server_exclude_list;
57
58
Wayne Davisond74a2e32003-08-01 07:58:47 +000059static void delete_one(char *fn, int is_dir)
Andrew Tridgell2f03f951998-07-25 02:25:22 +000060{
Wayne Davisond74a2e32003-08-01 07:58:47 +000061 if (!is_dir) {
62 if (robust_unlink(fn) != 0) {
Wayne Davisond62bcc12004-05-15 19:31:10 +000063 rsyserr(FERROR, errno, "delete_one: unlink %s failed",
64 full_fname(fn));
Wayne Davisond2a918b2004-07-02 18:23:57 +000065 } else if (verbose)
Wayne Davisonecc81fc2004-07-26 16:36:59 +000066 rprintf(FINFO, "deleting %s\n", safe_fname(fn));
Wayne Davison17fadf72003-12-15 08:14:27 +000067 } else {
Wayne Davisond74a2e32003-08-01 07:58:47 +000068 if (do_rmdir(fn) != 0) {
69 if (errno != ENOTEMPTY && errno != EEXIST) {
Wayne Davisond62bcc12004-05-15 19:31:10 +000070 rsyserr(FERROR, errno,
71 "delete_one: rmdir %s failed",
72 full_fname(fn));
Wayne Davisond74a2e32003-08-01 07:58:47 +000073 }
Wayne Davisonecc81fc2004-07-26 16:36:59 +000074 } else if (verbose) {
75 rprintf(FINFO, "deleting directory %s\n",
76 safe_fname(fn));
77 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +000078 }
79}
80
81
Wayne Davisond74a2e32003-08-01 07:58:47 +000082static int is_backup_file(char *fn)
83{
84 int k = strlen(fn) - backup_suffix_len;
85 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
86}
Andrew Tridgell2f03f951998-07-25 02:25:22 +000087
88
Wayne Davison1e82e2c2004-03-23 16:36:00 +000089/* This deletes any files on the receiving side that are not present
90 * on the sending side. */
Andrew Tridgell6957ae32000-01-24 11:20:25 +000091void delete_files(struct file_list *flist)
Andrew Tridgell2f03f951998-07-25 02:25:22 +000092{
93 struct file_list *local_file_list;
94 int i, j;
Wayne Davisona24639b2004-01-22 04:40:33 +000095 char *argv[1], fbuf[MAXPATHLEN];
Andrew Tridgell0b73ca12000-01-23 11:43:04 +000096 static int deletion_count;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000097
98 if (cvs_exclude)
99 add_cvs_excludes();
100
Andrew Tridgellef55c682000-03-21 04:06:04 +0000101 if (io_error && !(lp_ignore_errors(module_id) || ignore_errors)) {
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000102 rprintf(FINFO,"IO error encountered - skipping file deletion\n");
103 return;
104 }
105
Wayne Davison1e82e2c2004-03-23 16:36:00 +0000106 for (j = 0; j < flist->count; j++) {
Wayne Davisona5342642004-01-27 01:05:12 +0000107 if (!(flist->files[j]->flags & FLAG_TOP_DIR)
108 || !S_ISDIR(flist->files[j]->mode))
109 continue;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000110
Wayne Davisona24639b2004-01-22 04:40:33 +0000111 argv[0] = f_name_to(flist->files[j], fbuf);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000112
Wayne Davisona24639b2004-01-22 04:40:33 +0000113 if (!(local_file_list = send_file_list(-1, 1, argv)))
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000114 continue;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000115
116 if (verbose > 1)
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000117 rprintf(FINFO, "deleting in %s\n", safe_fname(fbuf));
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000118
Wayne Davison446e2392004-01-02 08:18:53 +0000119 for (i = local_file_list->count-1; i >= 0; i--) {
Wayne Davison08b1b482004-10-18 20:41:57 +0000120 if (max_delete && deletion_count >= max_delete)
Wayne Davison92b9eb92004-03-23 16:50:40 +0000121 break;
122 if (!local_file_list->files[i]->basename)
123 continue;
124 if (flist_find(flist,local_file_list->files[i]) < 0) {
David Dykstra53dd3131998-11-24 19:01:24 +0000125 char *f = f_name(local_file_list->files[i]);
Wayne Davisonf80a8522004-10-27 06:34:13 +0000126 int mode = local_file_list->files[i]->mode;
127 if (make_backups && (backup_dir || !is_backup_file(f))
128 && !S_ISDIR(mode)) {
Wayne Davisond2a918b2004-07-02 18:23:57 +0000129 make_backup(f);
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000130 if (verbose) {
131 rprintf(FINFO, "deleting %s\n",
132 safe_fname(f));
133 }
Wayne Davisonf80a8522004-10-27 06:34:13 +0000134 } else
Wayne Davisond74a2e32003-08-01 07:58:47 +0000135 delete_one(f, S_ISDIR(mode) != 0);
Wayne Davison31f3b682003-08-01 08:20:53 +0000136 deletion_count++;
David Dykstra53dd3131998-11-24 19:01:24 +0000137 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000138 }
139 flist_free(local_file_list);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000140 }
141}
142
143
J.W. Schultz52d3e102003-03-26 11:04:14 +0000144/*
145 * get_tmpname() - create a tmp filename for a given filename
146 *
147 * If a tmpdir is defined, use that as the directory to
148 * put it in. Otherwise, the tmp filename is in the same
149 * directory as the given name. Note that there may be no
150 * directory at all in the given name!
Wayne Davison17fadf72003-12-15 08:14:27 +0000151 *
J.W. Schultz52d3e102003-03-26 11:04:14 +0000152 * The tmp filename is basically the given filename with a
153 * dot prepended, and .XXXXXX appended (for mkstemp() to
154 * put its unique gunk in). Take care to not exceed
155 * either the MAXPATHLEN or NAME_MAX, esp. the last, as
156 * the basename basically becomes 8 chars longer. In that
157 * case, the original name is shortened sufficiently to
158 * make it all fit.
Wayne Davison17fadf72003-12-15 08:14:27 +0000159 *
J.W. Schultz52d3e102003-03-26 11:04:14 +0000160 * Of course, there's no real reason for the tmp name to
161 * look like the original, except to satisfy us humans.
162 * As long as it's unique, rsync will work.
163 */
164
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000165static int get_tmpname(char *fnametmp, char *fname)
166{
167 char *f;
J.W. Schultz52d3e102003-03-26 11:04:14 +0000168 int length = 0;
169 int maxname;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000170
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000171 if (tmpdir) {
Wayne Davisona24639b2004-01-22 04:40:33 +0000172 /* Note: this can't overflow, so the return value is safe */
J.W. Schultzb7cee942004-01-20 03:37:04 +0000173 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
J.W. Schultz52d3e102003-03-26 11:04:14 +0000174 fnametmp[length++] = '/';
175 fnametmp[length] = '\0'; /* always NULL terminated */
Wayne Davison0c2ef5f2003-08-04 21:00:57 +0000176 }
J.W. Schultz52d3e102003-03-26 11:04:14 +0000177
Wayne Davison0c2ef5f2003-08-04 21:00:57 +0000178 if ((f = strrchr(fname, '/')) != NULL) {
J.W. Schultz52d3e102003-03-26 11:04:14 +0000179 ++f;
180 if (!tmpdir) {
181 length = f - fname;
Wayne Davison0c2ef5f2003-08-04 21:00:57 +0000182 /* copy up to and including the slash */
J.W. Schultz52d3e102003-03-26 11:04:14 +0000183 strlcpy(fnametmp, fname, length + 1);
Wayne Davison0c2ef5f2003-08-04 21:00:57 +0000184 }
Wayne Davison446e2392004-01-02 08:18:53 +0000185 } else
J.W. Schultz52d3e102003-03-26 11:04:14 +0000186 f = fname;
J.W. Schultz52d3e102003-03-26 11:04:14 +0000187 fnametmp[length++] = '.';
188 fnametmp[length] = '\0'; /* always NULL terminated */
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000189
J.W. Schultz52d3e102003-03-26 11:04:14 +0000190 maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000191
Wayne Davison0c2ef5f2003-08-04 21:00:57 +0000192 if (maxname < 1) {
J.W. Schultz52d3e102003-03-26 11:04:14 +0000193 rprintf(FERROR, "temporary filename too long: %s\n", fname);
194 fnametmp[0] = '\0';
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000195 return 0;
196 }
197
Wayne Davison17fadf72003-12-15 08:14:27 +0000198 strlcpy(fnametmp + length, f, maxname);
J.W. Schultz52d3e102003-03-26 11:04:14 +0000199 strcat(fnametmp + length, ".XXXXXX");
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000200
201 return 1;
202}
203
204
Wayne Davison7e5fa372004-07-20 21:35:58 +0000205static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
206 char *fname, int fd, OFF_T total_size)
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000207{
Wayne Davison7e5fa372004-07-20 21:35:58 +0000208 static char file_sum1[MD4_SUM_LENGTH];
209 static char file_sum2[MD4_SUM_LENGTH];
210 struct map_struct *mapbuf;
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000211 struct sum_struct sum;
Wayne Davison6c495e02005-01-01 21:08:17 +0000212 int32 len;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000213 OFF_T offset = 0;
Wayne Davison89e540e2004-07-29 06:59:30 +0000214 OFF_T offset2;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000215 char *data;
Wayne Davison6c495e02005-01-01 21:08:17 +0000216 int32 i;
Wayne Davisone1f67412004-06-30 07:27:30 +0000217 char *map = NULL;
Wayne Davison17fadf72003-12-15 08:14:27 +0000218
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000219 read_sum_head(f_in, &sum);
Wayne Davison17fadf72003-12-15 08:14:27 +0000220
Wayne Davison7e5fa372004-07-20 21:35:58 +0000221 if (fd_r >= 0 && size_r > 0) {
Wayne Davison6c495e02005-01-01 21:08:17 +0000222 OFF_T map_size = MAX((OFF_T)sum.blength * 2, 16*1024);
Wayne Davison96d910c2004-08-03 08:05:27 +0000223 mapbuf = map_file(fd_r, size_r, map_size, sum.blength);
Wayne Davison7e5fa372004-07-20 21:35:58 +0000224 if (verbose > 2) {
225 rprintf(FINFO, "recv mapped %s of size %.0f\n",
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000226 safe_fname(fname_r), (double)size_r);
Wayne Davison7e5fa372004-07-20 21:35:58 +0000227 }
228 } else
229 mapbuf = NULL;
230
Wayne Davisonba582f72004-05-21 08:27:04 +0000231 sum_init(checksum_seed);
Wayne Davison17fadf72003-12-15 08:14:27 +0000232
Wayne Davison3f55bd52004-01-08 00:45:41 +0000233 while ((i = recv_token(f_in, &data)) != 0) {
Wayne Davison16417f82003-07-08 16:49:10 +0000234 if (do_progress)
235 show_progress(offset, total_size);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000236
237 if (i > 0) {
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000238 if (verbose > 3) {
Andrew Tridgell5f808df2000-01-23 12:30:34 +0000239 rprintf(FINFO,"data recv %d at %.0f\n",
240 i,(double)offset);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000241 }
242
243 stats.literal_data += i;
244 cleanup_got_literal = 1;
Wayne Davison17fadf72003-12-15 08:14:27 +0000245
Wayne Davison6c495e02005-01-01 21:08:17 +0000246 sum_update(data, i);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000247
Wayne Davisonc52461f2004-07-29 07:37:27 +0000248 if (fd != -1 && write_file(fd,data,i) != i)
249 goto report_write_error;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000250 offset += i;
251 continue;
Wayne Davison17fadf72003-12-15 08:14:27 +0000252 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000253
254 i = -(i+1);
Wayne Davison6c495e02005-01-01 21:08:17 +0000255 offset2 = i * (OFF_T)sum.blength;
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000256 len = sum.blength;
Wayne Davisond2a918b2004-07-02 18:23:57 +0000257 if (i == (int)sum.count-1 && sum.remainder != 0)
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000258 len = sum.remainder;
Wayne Davison17fadf72003-12-15 08:14:27 +0000259
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000260 stats.matched_data += len;
Wayne Davison17fadf72003-12-15 08:14:27 +0000261
Wayne Davison6c495e02005-01-01 21:08:17 +0000262 if (verbose > 3) {
263 rprintf(FINFO,
264 "chunk[%d] of size %ld at %.0f offset=%.0f\n",
265 i, (long)len, (double)offset2, (double)offset);
266 }
Wayne Davison17fadf72003-12-15 08:14:27 +0000267
Wayne Davison446e2392004-01-02 08:18:53 +0000268 if (mapbuf) {
269 map = map_ptr(mapbuf,offset2,len);
Wayne Davison17fadf72003-12-15 08:14:27 +0000270
Andrew Tridgellc55f7022000-01-24 11:41:08 +0000271 see_token(map, len);
Wayne Davison6c495e02005-01-01 21:08:17 +0000272 sum_update(map, len);
Andrew Tridgellc55f7022000-01-24 11:41:08 +0000273 }
Wayne Davison17fadf72003-12-15 08:14:27 +0000274
Wayne Davisonfab65a52004-07-29 06:40:26 +0000275 if (inplace) {
Wayne Davison89e540e2004-07-29 06:59:30 +0000276 if (offset == offset2 && fd != -1) {
Wayne Davisonc52461f2004-07-29 07:37:27 +0000277 if (flush_write_file(fd) < 0)
278 goto report_write_error;
Wayne Davison89e540e2004-07-29 06:59:30 +0000279 offset += len;
280 if (do_lseek(fd, len, SEEK_CUR) != offset) {
Wayne Davisonfab65a52004-07-29 06:40:26 +0000281 rsyserr(FERROR, errno,
282 "lseek failed on %s",
283 full_fname(fname));
284 exit_cleanup(RERR_FILEIO);
285 }
Wayne Davison89e540e2004-07-29 06:59:30 +0000286 continue;
Wayne Davisona3221d22004-07-16 20:06:24 +0000287 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000288 }
Wayne Davisonc52461f2004-07-29 07:37:27 +0000289 if (fd != -1 && write_file(fd, map, len) != (int)len)
290 goto report_write_error;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000291 offset += len;
292 }
293
Wayne Davison85f14172004-12-02 17:16:19 +0000294 if (flush_write_file(fd) < 0)
295 goto report_write_error;
Wayne Davison76c21942004-01-02 08:29:49 +0000296
Wayne Davisona3221d22004-07-16 20:06:24 +0000297#ifdef HAVE_FTRUNCATE
Wayne Davisonfab65a52004-07-29 06:40:26 +0000298 if (inplace && fd != -1)
Wayne Davisona3221d22004-07-16 20:06:24 +0000299 ftruncate(fd, offset);
300#endif
301
Wayne Davison16417f82003-07-08 16:49:10 +0000302 if (do_progress)
303 end_progress(total_size);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000304
305 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
Wayne Davisonc52461f2004-07-29 07:37:27 +0000306 report_write_error:
Wayne Davisond62bcc12004-05-15 19:31:10 +0000307 rsyserr(FERROR, errno, "write failed on %s",
308 full_fname(fname));
Andrew Tridgell65417571998-11-03 07:08:27 +0000309 exit_cleanup(RERR_FILEIO);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000310 }
311
312 sum_end(file_sum1);
313
Wayne Davison7e5fa372004-07-20 21:35:58 +0000314 if (mapbuf)
315 unmap_file(mapbuf);
316
J.W. Schultzbc63ae32003-03-31 17:28:34 +0000317 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
Wayne Davisond2a918b2004-07-02 18:23:57 +0000318 if (verbose > 2)
J.W. Schultzbc63ae32003-03-31 17:28:34 +0000319 rprintf(FINFO,"got file_sum\n");
Wayne Davisond2a918b2004-07-02 18:23:57 +0000320 if (fd != -1 && memcmp(file_sum1, file_sum2, MD4_SUM_LENGTH) != 0)
J.W. Schultzbc63ae32003-03-31 17:28:34 +0000321 return 0;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000322 return 1;
323}
324
325
Wayne Davison8ed9d842004-07-19 17:05:01 +0000326static void discard_receive_data(int f_in, OFF_T length)
327{
Wayne Davison7e5fa372004-07-20 21:35:58 +0000328 receive_data(f_in, NULL, -1, 0, NULL, -1, length);
Wayne Davison8ed9d842004-07-19 17:05:01 +0000329}
330
331
Martin Poolb35d0d82002-04-08 04:10:20 +0000332/**
333 * main routine for receiver process.
334 *
335 * Receiver process runs on the same host as the generator process. */
Wayne Davison41cfde62004-11-03 20:30:31 +0000336int recv_files(int f_in, struct file_list *flist, char *local_name,
337 int f_in_name)
Wayne Davison17fadf72003-12-15 08:14:27 +0000338{
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000339 int next_gen_i = -1;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000340 int fd1,fd2;
341 STRUCT_STAT st;
Wayne Davison446e2392004-01-02 08:18:53 +0000342 char *fname, fbuf[MAXPATHLEN];
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000343 char template[MAXPATHLEN];
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000344 char fnametmp[MAXPATHLEN];
Wayne Davisona7260c42004-07-29 16:06:38 +0000345 char *fnamecmp, *partialptr;
David Dykstra375a4551998-10-26 21:42:38 +0000346 char fnamecmpbuf[MAXPATHLEN];
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;
350 int i, recv_ok, phase = 0;
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000351
Wayne Davisond2a918b2004-07-02 18:23:57 +0000352 if (verbose > 2)
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000353 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000354
Wayne Davisoncb869c22004-02-10 17:28:59 +0000355 if (flist->hlink_pool) {
J.W. Schultz99350662004-02-10 03:23:37 +0000356 pool_destroy(flist->hlink_pool);
357 flist->hlink_pool = NULL;
358 }
359
Wayne Davison17fadf72003-12-15 08:14:27 +0000360 while (1) {
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000361 cleanup_disable();
362
363 i = read_int(f_in);
364 if (i == -1) {
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000365 if (read_batch) {
Wayne Davison41cfde62004-11-03 20:30:31 +0000366 if (next_gen_i != flist->count) {
367 do {
368 if (f_in_name >= 0
369 && next_gen_i >= 0)
370 read_byte(f_in_name);
371 } while (read_int(batch_gen_fd) != -1);
372 }
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000373 next_gen_i = -1;
374 }
375
Wayne Davison4e834af2004-06-14 15:09:36 +0000376 if (phase)
377 break;
Wayne Davison5ebab6c2004-07-19 16:37:30 +0000378
Wayne Davison4e834af2004-06-14 15:09:36 +0000379 phase = 1;
380 csum_length = SUM_LENGTH;
381 if (verbose > 2)
382 rprintf(FINFO, "recv_files phase=%d\n", phase);
383 send_msg(MSG_DONE, "", 0);
Wayne Davisonaec6b9f2005-01-10 10:03:10 +0000384 if (keep_partial && !partial_dir)
Wayne Davison4e834af2004-06-14 15:09:36 +0000385 make_backups = 0; /* prevents double backup */
386 continue;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000387 }
388
389 if (i < 0 || i >= flist->count) {
Wayne Davison17fadf72003-12-15 08:14:27 +0000390 rprintf(FERROR,"Invalid file index %d in recv_files (count=%d)\n",
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000391 i, flist->count);
Andrew Tridgell65417571998-11-03 07:08:27 +0000392 exit_cleanup(RERR_PROTOCOL);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000393 }
394
395 file = flist->files[i];
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000396
Wayne Davison97feb552004-01-13 18:22:43 +0000397 stats.current_file_index = i;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000398 stats.num_transferred_files++;
399 stats.total_transferred_size += file->length;
Wayne Davison925c5172004-01-02 08:38:35 +0000400 cleanup_got_literal = 0;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000401
402 if (local_name)
403 fname = local_name;
Wayne Davison446e2392004-01-02 08:18:53 +0000404 else
Wayne Davison3fef5362004-01-22 04:38:18 +0000405 fname = f_name_to(file, fbuf);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000406
407 if (dry_run) {
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000408 if (!am_server && verbose) /* log the transfer */
409 rprintf(FINFO, "%s\n", safe_fname(fname));
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000410 continue;
411 }
412
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000413 initial_stats = stats;
414
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000415 if (verbose > 2)
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000416 rprintf(FINFO, "recv_files(%s)\n", safe_fname(fname));
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000417
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000418 if (read_batch) {
419 while (i > next_gen_i) {
Wayne Davison41cfde62004-11-03 20:30:31 +0000420 if (f_in_name >= 0 && next_gen_i >= 0)
421 read_byte(f_in_name);
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000422 next_gen_i = read_int(batch_gen_fd);
423 if (next_gen_i == -1)
424 next_gen_i = flist->count;
425 }
426 if (i < next_gen_i) {
427 rprintf(FINFO, "skipping update for \"%s\"\n",
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000428 safe_fname(fname));
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000429 discard_receive_data(f_in, file->length);
430 continue;
431 }
Wayne Davison41cfde62004-11-03 20:30:31 +0000432 next_gen_i = -1;
Wayne Davison16cc9ca2004-07-21 23:59:37 +0000433 }
434
Wayne Davison5ebab6c2004-07-19 16:37:30 +0000435 if (server_exclude_list.head
436 && check_exclude(&server_exclude_list, fname,
437 S_ISDIR(file->mode)) < 0) {
Wayne Davison33eff8b2004-07-30 07:02:37 +0000438 rprintf(FERROR, "attempt to hack rsync failed.\n");
439 exit_cleanup(RERR_PROTOCOL);
Wayne Davison5ebab6c2004-07-19 16:37:30 +0000440 }
441
Wayne Davison41cfde62004-11-03 20:30:31 +0000442 partialptr = partial_dir ? partial_dir_fname(fname) : fname;
Wayne Davisona7260c42004-07-29 16:06:38 +0000443
Wayne Davison41cfde62004-11-03 20:30:31 +0000444 if (f_in_name >= 0) {
Wayne Davisonee297522004-11-27 17:56:58 +0000445 uchar j;
446 switch (j = read_byte(f_in_name)) {
Wayne Davison41cfde62004-11-03 20:30:31 +0000447 case FNAMECMP_FNAME:
448 fnamecmp = fname;
449 break;
450 case FNAMECMP_PARTIAL_DIR:
451 fnamecmp = partialptr ? partialptr : fname;
452 break;
453 case FNAMECMP_BACKUP:
454 fnamecmp = get_backup_name(fname);
455 break;
Wayne Davisonee297522004-11-27 17:56:58 +0000456 case FNAMECMP_BASIS_DIR:
Wayne Davison41cfde62004-11-03 20:30:31 +0000457 default:
458 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
Wayne Davisonee297522004-11-27 17:56:58 +0000459 basis_dir[j], fname);
Wayne Davison41cfde62004-11-03 20:30:31 +0000460 fnamecmp = fnamecmpbuf;
461 break;
462 }
463 } else
464 fnamecmp = fname;
Wayne Davisondc55d7b2004-09-07 21:34:26 +0000465
Wayne Davison17fadf72003-12-15 08:14:27 +0000466 /* open the file */
Andrew Tridgell8c9fd201999-10-25 22:04:09 +0000467 fd1 = do_open(fnamecmp, O_RDONLY, 0);
David Dykstra375a4551998-10-26 21:42:38 +0000468
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000469 if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
Wayne Davisond62bcc12004-05-15 19:31:10 +0000470 rsyserr(FERROR, errno, "fstat %s failed",
471 full_fname(fnamecmp));
Wayne Davison8ed9d842004-07-19 17:05:01 +0000472 discard_receive_data(f_in, file->length);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000473 close(fd1);
474 continue;
475 }
476
J.W. Schultz350e4e42003-09-04 05:49:50 +0000477 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
478 /* this special handling for directories
479 * wouldn't be necessary if robust_rename()
480 * and the underlying robust_unlink could cope
481 * with directories
482 */
Wayne Davisonea425412003-09-11 04:53:05 +0000483 rprintf(FERROR,"recv_files: %s is a directory\n",
484 full_fname(fnamecmp));
Wayne Davison8ed9d842004-07-19 17:05:01 +0000485 discard_receive_data(f_in, file->length);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000486 close(fd1);
487 continue;
488 }
489
J.W. Schultz350e4e42003-09-04 05:49:50 +0000490 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
491 close(fd1);
492 fd1 = -1;
J.W. Schultz350e4e42003-09-04 05:49:50 +0000493 }
494
Andrew Tridgell4df9f361999-10-31 04:28:03 +0000495 if (fd1 != -1 && !preserve_perms) {
J.W. Schultz85ed0aa2003-03-21 07:27:31 +0000496 /* if the file exists already and we aren't preserving
Wayne Davison17fadf72003-12-15 08:14:27 +0000497 * permissions then act as though the remote end sent
498 * us the file permissions we already have */
Andrew Tridgell4df9f361999-10-31 04:28:03 +0000499 file->mode = st.st_mode;
500 }
501
Wayne Davisona3221d22004-07-16 20:06:24 +0000502 /* We now check to see if we are writing file "inplace" */
503 if (inplace) {
Wayne Davisondc55d7b2004-09-07 21:34:26 +0000504 fd2 = do_open(fname, O_WRONLY|O_CREAT, 0);
Wayne Davisona3221d22004-07-16 20:06:24 +0000505 if (fd2 == -1) {
506 rsyserr(FERROR, errno, "open %s failed",
Wayne Davisondc55d7b2004-09-07 21:34:26 +0000507 full_fname(fname));
Wayne Davison8ed9d842004-07-19 17:05:01 +0000508 discard_receive_data(f_in, file->length);
Wayne Davisona3221d22004-07-16 20:06:24 +0000509 if (fd1 != -1)
510 close(fd1);
511 continue;
512 }
513 } else {
514 if (!get_tmpname(fnametmp,fname)) {
Wayne Davison8ed9d842004-07-19 17:05:01 +0000515 discard_receive_data(f_in, file->length);
Wayne Davisona3221d22004-07-16 20:06:24 +0000516 if (fd1 != -1)
517 close(fd1);
518 continue;
519 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000520
Wayne Davisona3221d22004-07-16 20:06:24 +0000521 strlcpy(template, fnametmp, sizeof template);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000522
Wayne Davisona3221d22004-07-16 20:06:24 +0000523 /* we initially set the perms without the
524 * setuid/setgid bits to ensure that there is no race
525 * condition. They are then correctly updated after
526 * the lchown. Thanks to snabb@epipe.fi for pointing
527 * this out. We also set it initially without group
528 * access because of a similar race condition. */
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000529 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
Wayne Davison17fadf72003-12-15 08:14:27 +0000530
Wayne Davisona3221d22004-07-16 20:06:24 +0000531 /* in most cases parent directories will already exist
532 * because their information should have been previously
533 * transferred, but that may not be the case with -R */
534 if (fd2 == -1 && relative_paths && errno == ENOENT
535 && create_directory_path(fnametmp, orig_umask) == 0) {
536 strlcpy(fnametmp, template, sizeof fnametmp);
537 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
538 }
539 if (fd2 == -1) {
540 rsyserr(FERROR, errno, "mkstemp %s failed",
541 full_fname(fnametmp));
Wayne Davison8ed9d842004-07-19 17:05:01 +0000542 discard_receive_data(f_in, file->length);
Wayne Davisona3221d22004-07-16 20:06:24 +0000543 if (fd1 != -1)
544 close(fd1);
545 continue;
546 }
547
Wayne Davisona7260c42004-07-29 16:06:38 +0000548 if (partialptr)
549 cleanup_set(fnametmp, partialptr, file, fd1, fd2);
Wayne Davisona3221d22004-07-16 20:06:24 +0000550 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000551
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000552 if (!am_server && verbose) /* log the transfer */
553 rprintf(FINFO, "%s\n", safe_fname(fname));
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000554
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000555 /* recv file data */
Wayne Davison7e5fa372004-07-20 21:35:58 +0000556 recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
557 fname, fd2, file->length);
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000558
559 log_recv(file, &initial_stats);
Wayne Davison17fadf72003-12-15 08:14:27 +0000560
Wayne Davisond2a918b2004-07-02 18:23:57 +0000561 if (fd1 != -1)
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000562 close(fd1);
Wayne Davison9f27cd82004-04-27 19:51:33 +0000563 if (close(fd2) < 0) {
Wayne Davisond62bcc12004-05-15 19:31:10 +0000564 rsyserr(FERROR, errno, "close failed on %s",
565 full_fname(fnametmp));
Wayne Davison9f27cd82004-04-27 19:51:33 +0000566 exit_cleanup(RERR_FILEIO);
567 }
Wayne Davison17fadf72003-12-15 08:14:27 +0000568
Wayne Davisonaec6b9f2005-01-10 10:03:10 +0000569 if (recv_ok || inplace) {
570 finish_transfer(fname, fnametmp, file, recv_ok, 1);
571 if (partialptr != fname && fnamecmp == partialptr) {
572 do_unlink(partialptr);
573 handle_partial_dir(partialptr, PDIR_DELETE);
574 }
575 } else if (keep_partial && partialptr
576 && handle_partial_dir(partialptr, PDIR_CREATE)) {
577 finish_transfer(partialptr, fnametmp, file, recv_ok,
578 !partial_dir);
579 } else {
Wayne Davisona7260c42004-07-29 16:06:38 +0000580 partialptr = NULL;
Wayne Davison55e50d82004-05-13 07:08:22 +0000581 do_unlink(fnametmp);
Wayne Davisona7260c42004-07-29 16:06:38 +0000582 }
583
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000584 cleanup_disable();
Andrew Tridgell554e0a82000-01-23 07:36:56 +0000585
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000586 if (!recv_ok) {
Wayne Davison007e3c02004-07-22 08:16:35 +0000587 int msgtype = csum_length == SUM_LENGTH || read_batch ?
588 FERROR : FINFO;
589 if (msgtype == FERROR || verbose) {
Wayne Davisona7260c42004-07-29 16:06:38 +0000590 char *errstr, *redostr, *keptstr;
591 if (!(keep_partial && partialptr) && !inplace)
592 keptstr = "discarded";
593 else if (partial_dir)
594 keptstr = "put into partial-dir";
595 else
596 keptstr = "retained";
Wayne Davison007e3c02004-07-22 08:16:35 +0000597 if (msgtype == FERROR) {
598 errstr = "ERROR";
599 redostr = "";
600 } else {
601 errstr = "WARNING";
602 redostr = " (will try again)";
603 }
604 rprintf(msgtype,
Wayne Davisona7260c42004-07-29 16:06:38 +0000605 "%s: %s failed verification -- update %s%s.\n",
Wayne Davisonecc81fc2004-07-26 16:36:59 +0000606 errstr, safe_fname(fname),
607 keptstr, redostr);
Wayne Davison007e3c02004-07-22 08:16:35 +0000608 }
Wayne Davisone2bc4122004-07-22 04:15:18 +0000609 if (csum_length != SUM_LENGTH) {
Wayne Davison0569a132004-01-15 07:42:25 +0000610 char buf[4];
Wayne Davison0569a132004-01-15 07:42:25 +0000611 SIVAL(buf, 0, i);
612 send_msg(MSG_REDO, buf, 4);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000613 }
614 }
615 }
Wayne Davison4e834af2004-06-14 15:09:36 +0000616 make_backups = save_make_backups;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000617
Wayne Davisone76ca142004-07-22 02:52:57 +0000618 if (delete_after && recurse && !local_name && flist->count > 0)
Wayne Davison3f55bd52004-01-08 00:45:41 +0000619 delete_files(flist);
Andrew Tridgell57df1711999-11-08 13:03:05 +0000620
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000621 if (verbose > 2)
622 rprintf(FINFO,"recv_files finished\n");
Wayne Davison17fadf72003-12-15 08:14:27 +0000623
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000624 return 0;
625}