blob: 1ab74f5d59fbef4aa09e3946068705aa56c143f6 [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;
25extern int delete_mode;
Wayne Davisond04e9c52003-09-09 15:58:48 +000026extern int protocol_version;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000027extern int csum_length;
28extern struct stats stats;
29extern int dry_run;
30extern int am_server;
31extern int relative_paths;
32extern int preserve_hard_links;
33extern int cvs_exclude;
34extern int io_error;
35extern char *tmpdir;
David Dykstra375a4551998-10-26 21:42:38 +000036extern char *compare_dest;
David Dykstra53dd3131998-11-24 19:01:24 +000037extern int make_backups;
Wayne Davison16417f82003-07-08 16:49:10 +000038extern int do_progress;
Wayne Davisond74a2e32003-08-01 07:58:47 +000039extern char *backup_dir;
David Dykstra53dd3131998-11-24 19:01:24 +000040extern char *backup_suffix;
Wayne Davisond74a2e32003-08-01 07:58:47 +000041extern int backup_suffix_len;
Wayne Davison925c5172004-01-02 08:38:35 +000042extern int cleanup_got_literal;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000043
Andrew Tridgell2f03f951998-07-25 02:25:22 +000044static struct delete_list {
Martin Pool6abd1932002-01-11 08:25:32 +000045 DEV64_T dev;
46 INO64_T inode;
Andrew Tridgell2f03f951998-07-25 02:25:22 +000047} *delete_list;
48static int dlist_len, dlist_alloc_len;
49
Andrew Tridgell2f03f951998-07-25 02:25:22 +000050/* yuck! This function wouldn't have been necessary if I had the sorting
Wayne Davison17fadf72003-12-15 08:14:27 +000051 * algorithm right. Unfortunately fixing the sorting algorithm would introduce
52 * a backward incompatibility as file list indexes are sent over the link.
53 */
Andrew Tridgell2f03f951998-07-25 02:25:22 +000054static int delete_already_done(struct file_list *flist,int j)
55{
56 int i;
57 STRUCT_STAT st;
58
59 if (link_stat(f_name(flist->files[j]), &st)) return 1;
60
Wayne Davison17fadf72003-12-15 08:14:27 +000061 for (i = 0; i < dlist_len; i++) {
Andrew Tridgell2f03f951998-07-25 02:25:22 +000062 if (st.st_ino == delete_list[i].inode &&
Wayne Davisond01350a2003-08-21 23:45:49 +000063 (DEV64_T)st.st_dev == delete_list[i].dev)
Andrew Tridgell2f03f951998-07-25 02:25:22 +000064 return 1;
65 }
66
67 return 0;
68}
69
70static void add_delete_entry(struct file_struct *file)
71{
72 if (dlist_len == dlist_alloc_len) {
73 dlist_alloc_len += 1024;
Wayne Davison58cadc82003-12-06 21:07:27 +000074 delete_list = realloc_array(delete_list, struct delete_list,
75 dlist_alloc_len);
Andrew Tridgell2f03f951998-07-25 02:25:22 +000076 if (!delete_list) out_of_memory("add_delete_entry");
77 }
78
79 delete_list[dlist_len].dev = file->dev;
80 delete_list[dlist_len].inode = file->inode;
81 dlist_len++;
82
83 if (verbose > 3)
84 rprintf(FINFO,"added %s to delete list\n", f_name(file));
85}
86
Wayne Davisond74a2e32003-08-01 07:58:47 +000087static void delete_one(char *fn, int is_dir)
Andrew Tridgell2f03f951998-07-25 02:25:22 +000088{
Wayne Davisond74a2e32003-08-01 07:58:47 +000089 if (!is_dir) {
90 if (robust_unlink(fn) != 0) {
Wayne Davisonea425412003-09-11 04:53:05 +000091 rprintf(FERROR, "delete_one: unlink %s failed: %s\n",
92 full_fname(fn), strerror(errno));
Andrew Tridgell2f03f951998-07-25 02:25:22 +000093 } else if (verbose) {
Wayne Davisond74a2e32003-08-01 07:58:47 +000094 rprintf(FINFO, "deleting %s\n", fn);
Andrew Tridgell2f03f951998-07-25 02:25:22 +000095 }
Wayne Davison17fadf72003-12-15 08:14:27 +000096 } else {
Wayne Davisond74a2e32003-08-01 07:58:47 +000097 if (do_rmdir(fn) != 0) {
98 if (errno != ENOTEMPTY && errno != EEXIST) {
Wayne Davisonea425412003-09-11 04:53:05 +000099 rprintf(FERROR, "delete_one: rmdir %s failed: %s\n",
Wayne Davison17fadf72003-12-15 08:14:27 +0000100 full_fname(fn), strerror(errno));
Wayne Davisond74a2e32003-08-01 07:58:47 +0000101 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000102 } else if (verbose) {
Wayne Davisond74a2e32003-08-01 07:58:47 +0000103 rprintf(FINFO, "deleting directory %s\n", fn);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000104 }
105 }
106}
107
108
Wayne Davisond74a2e32003-08-01 07:58:47 +0000109static int is_backup_file(char *fn)
110{
111 int k = strlen(fn) - backup_suffix_len;
112 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
113}
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000114
115
116/* this deletes any files on the receiving side that are not present
Wayne Davison17fadf72003-12-15 08:14:27 +0000117 * on the sending side. For version 1.6.4 I have changed the behaviour
118 * to match more closely what most people seem to expect of this option */
Andrew Tridgell6957ae32000-01-24 11:20:25 +0000119void delete_files(struct file_list *flist)
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000120{
121 struct file_list *local_file_list;
122 int i, j;
Wayne Davison446e2392004-01-02 08:18:53 +0000123 char *name, fbuf[MAXPATHLEN];
Andrew Tridgellcda2ae81999-10-31 02:19:24 +0000124 extern int module_id;
Andrew Tridgellef55c682000-03-21 04:06:04 +0000125 extern int ignore_errors;
Andrew Tridgell0b73ca12000-01-23 11:43:04 +0000126 extern int max_delete;
127 static int deletion_count;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000128
129 if (cvs_exclude)
130 add_cvs_excludes();
131
Andrew Tridgellef55c682000-03-21 04:06:04 +0000132 if (io_error && !(lp_ignore_errors(module_id) || ignore_errors)) {
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000133 rprintf(FINFO,"IO error encountered - skipping file deletion\n");
134 return;
135 }
136
Wayne Davison446e2392004-01-02 08:18:53 +0000137 for (j = 0;j < flist->count; j++) {
Wayne Davison17fadf72003-12-15 08:14:27 +0000138 if (!S_ISDIR(flist->files[j]->mode) ||
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000139 !(flist->files[j]->flags & FLAG_DELETE)) continue;
140
Wayne Davisond04e9c52003-09-09 15:58:48 +0000141 if (protocol_version < 19 &&
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000142 delete_already_done(flist, j)) continue;
143
Wayne Davison446e2392004-01-02 08:18:53 +0000144 name = f_name_to(flist->files[j], fbuf, sizeof fbuf);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000145
Wayne Davison446e2392004-01-02 08:18:53 +0000146 if (!(local_file_list = send_file_list(-1,1,&name)))
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000147 continue;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000148
149 if (verbose > 1)
150 rprintf(FINFO,"deleting in %s\n", name);
151
Wayne Davison446e2392004-01-02 08:18:53 +0000152 for (i = local_file_list->count-1; i >= 0; i--) {
Andrew Tridgell0b73ca12000-01-23 11:43:04 +0000153 if (max_delete && deletion_count > max_delete) break;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000154 if (!local_file_list->files[i]->basename) continue;
Wayne Davisond04e9c52003-09-09 15:58:48 +0000155 if (protocol_version < 19 &&
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000156 S_ISDIR(local_file_list->files[i]->mode))
157 add_delete_entry(local_file_list->files[i]);
158 if (-1 == flist_find(flist,local_file_list->files[i])) {
David Dykstra53dd3131998-11-24 19:01:24 +0000159 char *f = f_name(local_file_list->files[i]);
Wayne Davisond74a2e32003-08-01 07:58:47 +0000160 if (make_backups && (backup_dir || !is_backup_file(f))) {
David Dykstra53dd3131998-11-24 19:01:24 +0000161 (void) make_backup(f);
Wayne Davisond74a2e32003-08-01 07:58:47 +0000162 if (verbose)
163 rprintf(FINFO, "deleting %s\n", f);
David Dykstra53dd3131998-11-24 19:01:24 +0000164 } else {
Wayne Davisond74a2e32003-08-01 07:58:47 +0000165 int mode = local_file_list->files[i]->mode;
166 delete_one(f, S_ISDIR(mode) != 0);
David Dykstra53dd3131998-11-24 19:01:24 +0000167 }
Wayne Davison31f3b682003-08-01 08:20:53 +0000168 deletion_count++;
David Dykstra53dd3131998-11-24 19:01:24 +0000169 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000170 }
171 flist_free(local_file_list);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000172 }
173}
174
175
J.W. Schultz52d3e102003-03-26 11:04:14 +0000176/*
177 * get_tmpname() - create a tmp filename for a given filename
178 *
179 * If a tmpdir is defined, use that as the directory to
180 * put it in. Otherwise, the tmp filename is in the same
181 * directory as the given name. Note that there may be no
182 * directory at all in the given name!
Wayne Davison17fadf72003-12-15 08:14:27 +0000183 *
J.W. Schultz52d3e102003-03-26 11:04:14 +0000184 * The tmp filename is basically the given filename with a
185 * dot prepended, and .XXXXXX appended (for mkstemp() to
186 * put its unique gunk in). Take care to not exceed
187 * either the MAXPATHLEN or NAME_MAX, esp. the last, as
188 * the basename basically becomes 8 chars longer. In that
189 * case, the original name is shortened sufficiently to
190 * make it all fit.
Wayne Davison17fadf72003-12-15 08:14:27 +0000191 *
J.W. Schultz52d3e102003-03-26 11:04:14 +0000192 * Of course, there's no real reason for the tmp name to
193 * look like the original, except to satisfy us humans.
194 * As long as it's unique, rsync will work.
195 */
196
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000197static int get_tmpname(char *fnametmp, char *fname)
198{
199 char *f;
J.W. Schultz52d3e102003-03-26 11:04:14 +0000200 int length = 0;
201 int maxname;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000202
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000203 if (tmpdir) {
J.W. Schultz52d3e102003-03-26 11:04:14 +0000204 strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
205 length = strlen(fnametmp);
206 fnametmp[length++] = '/';
207 fnametmp[length] = '\0'; /* always NULL terminated */
Wayne Davison0c2ef5f2003-08-04 21:00:57 +0000208 }
J.W. Schultz52d3e102003-03-26 11:04:14 +0000209
Wayne Davison0c2ef5f2003-08-04 21:00:57 +0000210 if ((f = strrchr(fname, '/')) != NULL) {
J.W. Schultz52d3e102003-03-26 11:04:14 +0000211 ++f;
212 if (!tmpdir) {
213 length = f - fname;
Wayne Davison0c2ef5f2003-08-04 21:00:57 +0000214 /* copy up to and including the slash */
J.W. Schultz52d3e102003-03-26 11:04:14 +0000215 strlcpy(fnametmp, fname, length + 1);
Wayne Davison0c2ef5f2003-08-04 21:00:57 +0000216 }
Wayne Davison446e2392004-01-02 08:18:53 +0000217 } else
J.W. Schultz52d3e102003-03-26 11:04:14 +0000218 f = fname;
J.W. Schultz52d3e102003-03-26 11:04:14 +0000219 fnametmp[length++] = '.';
220 fnametmp[length] = '\0'; /* always NULL terminated */
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000221
J.W. Schultz52d3e102003-03-26 11:04:14 +0000222 maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000223
Wayne Davison0c2ef5f2003-08-04 21:00:57 +0000224 if (maxname < 1) {
J.W. Schultz52d3e102003-03-26 11:04:14 +0000225 rprintf(FERROR, "temporary filename too long: %s\n", fname);
226 fnametmp[0] = '\0';
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000227 return 0;
228 }
229
Wayne Davison17fadf72003-12-15 08:14:27 +0000230 strlcpy(fnametmp + length, f, maxname);
J.W. Schultz52d3e102003-03-26 11:04:14 +0000231 strcat(fnametmp + length, ".XXXXXX");
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000232
233 return 1;
234}
235
236
Wayne Davison446e2392004-01-02 08:18:53 +0000237static int receive_data(int f_in,struct map_struct *mapbuf,int fd,char *fname,
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000238 OFF_T total_size)
239{
Martin Pool9dd891b2002-01-23 04:57:18 +0000240 int i;
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000241 struct sum_struct sum;
242 unsigned int len;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000243 OFF_T offset = 0;
244 OFF_T offset2;
245 char *data;
246 static char file_sum1[MD4_SUM_LENGTH];
247 static char file_sum2[MD4_SUM_LENGTH];
248 char *map=NULL;
Wayne Davison17fadf72003-12-15 08:14:27 +0000249
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000250 read_sum_head(f_in, &sum);
Wayne Davison17fadf72003-12-15 08:14:27 +0000251
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000252 sum_init();
Wayne Davison17fadf72003-12-15 08:14:27 +0000253
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000254 for (i=recv_token(f_in,&data); i != 0; i=recv_token(f_in,&data)) {
Wayne Davison16417f82003-07-08 16:49:10 +0000255 if (do_progress)
256 show_progress(offset, total_size);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000257
258 if (i > 0) {
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000259 if (verbose > 3) {
Andrew Tridgell5f808df2000-01-23 12:30:34 +0000260 rprintf(FINFO,"data recv %d at %.0f\n",
261 i,(double)offset);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000262 }
263
264 stats.literal_data += i;
265 cleanup_got_literal = 1;
Wayne Davison17fadf72003-12-15 08:14:27 +0000266
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000267 sum_update(data,i);
268
269 if (fd != -1 && write_file(fd,data,i) != i) {
Wayne Davisonea425412003-09-11 04:53:05 +0000270 rprintf(FERROR, "write failed on %s: %s\n",
271 full_fname(fname), strerror(errno));
Andrew Tridgell65417571998-11-03 07:08:27 +0000272 exit_cleanup(RERR_FILEIO);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000273 }
274 offset += i;
275 continue;
Wayne Davison17fadf72003-12-15 08:14:27 +0000276 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000277
278 i = -(i+1);
J.W. Schultzfc0257c2003-04-10 01:13:30 +0000279 offset2 = i*(OFF_T)sum.blength;
280 len = sum.blength;
281 if (i == (int) sum.count-1 && sum.remainder != 0)
282 len = sum.remainder;
Wayne Davison17fadf72003-12-15 08:14:27 +0000283
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000284 stats.matched_data += len;
Wayne Davison17fadf72003-12-15 08:14:27 +0000285
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000286 if (verbose > 3)
Andrew Tridgell5f808df2000-01-23 12:30:34 +0000287 rprintf(FINFO,"chunk[%d] of size %d at %.0f offset=%.0f\n",
288 i,len,(double)offset2,(double)offset);
Wayne Davison17fadf72003-12-15 08:14:27 +0000289
Wayne Davison446e2392004-01-02 08:18:53 +0000290 if (mapbuf) {
291 map = map_ptr(mapbuf,offset2,len);
Wayne Davison17fadf72003-12-15 08:14:27 +0000292
Andrew Tridgellc55f7022000-01-24 11:41:08 +0000293 see_token(map, len);
294 sum_update(map,len);
295 }
Wayne Davison17fadf72003-12-15 08:14:27 +0000296
Martin Poola2619892002-01-25 23:07:33 +0000297 if (fd != -1 && write_file(fd,map,len) != (int) len) {
Wayne Davisonea425412003-09-11 04:53:05 +0000298 rprintf(FERROR, "write failed on %s: %s\n",
299 full_fname(fname), strerror(errno));
Andrew Tridgell65417571998-11-03 07:08:27 +0000300 exit_cleanup(RERR_FILEIO);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000301 }
302 offset += len;
303 }
304
Wayne Davison76c21942004-01-02 08:29:49 +0000305 flush_write_file(fd);
306
Wayne Davison16417f82003-07-08 16:49:10 +0000307 if (do_progress)
308 end_progress(total_size);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000309
310 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
Wayne Davisonea425412003-09-11 04:53:05 +0000311 rprintf(FERROR, "write failed on %s: %s\n",
312 full_fname(fname), strerror(errno));
Andrew Tridgell65417571998-11-03 07:08:27 +0000313 exit_cleanup(RERR_FILEIO);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000314 }
315
316 sum_end(file_sum1);
317
J.W. Schultzbc63ae32003-03-31 17:28:34 +0000318 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
319 if (verbose > 2) {
320 rprintf(FINFO,"got file_sum\n");
321 }
Wayne Davison17fadf72003-12-15 08:14:27 +0000322 if (fd != -1 && memcmp(file_sum1,file_sum2,MD4_SUM_LENGTH) != 0) {
J.W. Schultzbc63ae32003-03-31 17:28:34 +0000323 return 0;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000324 }
325 return 1;
326}
327
328
Martin Poolb35d0d82002-04-08 04:10:20 +0000329/**
330 * main routine for receiver process.
331 *
332 * Receiver process runs on the same host as the generator process. */
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000333int recv_files(int f_in,struct file_list *flist,char *local_name,int f_gen)
Wayne Davison17fadf72003-12-15 08:14:27 +0000334{
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000335 int fd1,fd2;
336 STRUCT_STAT st;
Wayne Davison446e2392004-01-02 08:18:53 +0000337 char *fname, fbuf[MAXPATHLEN];
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000338 char template[MAXPATHLEN];
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000339 char fnametmp[MAXPATHLEN];
David Dykstra375a4551998-10-26 21:42:38 +0000340 char *fnamecmp;
341 char fnamecmpbuf[MAXPATHLEN];
Wayne Davison446e2392004-01-02 08:18:53 +0000342 struct map_struct *mapbuf;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000343 int i;
344 struct file_struct *file;
345 int phase=0;
346 int recv_ok;
Wayne Davison17fadf72003-12-15 08:14:27 +0000347 extern struct stats stats;
Andrew Tridgell4df9f361999-10-31 04:28:03 +0000348 extern int preserve_perms;
Andrew Tridgell57df1711999-11-08 13:03:05 +0000349 extern int delete_after;
Martin Poolb35d0d82002-04-08 04:10:20 +0000350 extern int orig_umask;
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000351 struct stats initial_stats;
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000352
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000353 if (verbose > 2) {
354 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
355 }
356
Wayne Davison17fadf72003-12-15 08:14:27 +0000357 while (1) {
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000358 cleanup_disable();
359
360 i = read_int(f_in);
361 if (i == -1) {
J.W. Schultzbc63ae32003-03-31 17:28:34 +0000362 if (phase==0) {
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000363 phase++;
364 csum_length = SUM_LENGTH;
365 if (verbose > 2)
366 rprintf(FINFO,"recv_files phase=%d\n",phase);
367 write_int(f_gen,-1);
368 continue;
369 }
370 break;
371 }
372
373 if (i < 0 || i >= flist->count) {
Wayne Davison17fadf72003-12-15 08:14:27 +0000374 rprintf(FERROR,"Invalid file index %d in recv_files (count=%d)\n",
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000375 i, flist->count);
Andrew Tridgell65417571998-11-03 07:08:27 +0000376 exit_cleanup(RERR_PROTOCOL);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000377 }
378
379 file = flist->files[i];
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000380
381 stats.num_transferred_files++;
382 stats.total_transferred_size += file->length;
Wayne Davison925c5172004-01-02 08:38:35 +0000383 cleanup_got_literal = 0;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000384
385 if (local_name)
386 fname = local_name;
Wayne Davison446e2392004-01-02 08:18:53 +0000387 else
388 fname = f_name_to(file, fbuf, sizeof fbuf);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000389
390 if (dry_run) {
J.W. Schultz42d4edc2003-03-25 02:28:54 +0000391 if (!am_server && verbose) { /* log transfer */
392 rprintf(FINFO, "%s\n", fname);
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000393 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000394 continue;
395 }
396
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000397 initial_stats = stats;
398
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000399 if (verbose > 2)
400 rprintf(FINFO,"recv_files(%s)\n",fname);
401
David Dykstra375a4551998-10-26 21:42:38 +0000402 fnamecmp = fname;
403
Wayne Davison17fadf72003-12-15 08:14:27 +0000404 /* open the file */
Andrew Tridgell8c9fd201999-10-25 22:04:09 +0000405 fd1 = do_open(fnamecmp, O_RDONLY, 0);
David Dykstra375a4551998-10-26 21:42:38 +0000406
407 if ((fd1 == -1) && (compare_dest != NULL)) {
408 /* try the file at compare_dest instead */
Andrew Tridgell8950ac02001-05-07 06:59:37 +0000409 snprintf(fnamecmpbuf,MAXPATHLEN,"%s/%s",
David Dykstra375a4551998-10-26 21:42:38 +0000410 compare_dest,fname);
411 fnamecmp = fnamecmpbuf;
Andrew Tridgell8c9fd201999-10-25 22:04:09 +0000412 fd1 = do_open(fnamecmp, O_RDONLY, 0);
David Dykstra375a4551998-10-26 21:42:38 +0000413 }
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000414
415 if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
Wayne Davisonea425412003-09-11 04:53:05 +0000416 rprintf(FERROR, "fstat %s failed: %s\n",
417 full_fname(fnamecmp), strerror(errno));
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000418 receive_data(f_in,NULL,-1,NULL,file->length);
419 close(fd1);
420 continue;
421 }
422
J.W. Schultz350e4e42003-09-04 05:49:50 +0000423 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
424 /* this special handling for directories
425 * wouldn't be necessary if robust_rename()
426 * and the underlying robust_unlink could cope
427 * with directories
428 */
Wayne Davisonea425412003-09-11 04:53:05 +0000429 rprintf(FERROR,"recv_files: %s is a directory\n",
430 full_fname(fnamecmp));
J.W. Schultz350e4e42003-09-04 05:49:50 +0000431 receive_data(f_in, NULL, -1, NULL, file->length);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000432 close(fd1);
433 continue;
434 }
435
J.W. Schultz350e4e42003-09-04 05:49:50 +0000436 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
437 close(fd1);
438 fd1 = -1;
Wayne Davison446e2392004-01-02 08:18:53 +0000439 mapbuf = NULL;
J.W. Schultz350e4e42003-09-04 05:49:50 +0000440 }
441
Andrew Tridgell4df9f361999-10-31 04:28:03 +0000442 if (fd1 != -1 && !preserve_perms) {
J.W. Schultz85ed0aa2003-03-21 07:27:31 +0000443 /* if the file exists already and we aren't preserving
Wayne Davison17fadf72003-12-15 08:14:27 +0000444 * permissions then act as though the remote end sent
445 * us the file permissions we already have */
Andrew Tridgell4df9f361999-10-31 04:28:03 +0000446 file->mode = st.st_mode;
447 }
448
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000449 if (fd1 != -1 && st.st_size > 0) {
Wayne Davison446e2392004-01-02 08:18:53 +0000450 mapbuf = map_file(fd1,st.st_size);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000451 if (verbose > 2)
Andrew Tridgell5f808df2000-01-23 12:30:34 +0000452 rprintf(FINFO,"recv mapped %s of size %.0f\n",fnamecmp,(double)st.st_size);
Wayne Davison446e2392004-01-02 08:18:53 +0000453 } else
454 mapbuf = NULL;
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000455
456 if (!get_tmpname(fnametmp,fname)) {
Wayne Davison446e2392004-01-02 08:18:53 +0000457 if (mapbuf) unmap_file(mapbuf);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000458 if (fd1 != -1) close(fd1);
459 continue;
460 }
461
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000462 strlcpy(template, fnametmp, sizeof(template));
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000463
464 /* we initially set the perms without the
Wayne Davison17fadf72003-12-15 08:14:27 +0000465 * setuid/setgid bits to ensure that there is no race
466 * condition. They are then correctly updated after
467 * the lchown. Thanks to snabb@epipe.fi for pointing
468 * this out. We also set it initially without group
469 * access because of a similar race condition. */
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000470 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000471
David Dykstra752eaba1999-03-24 19:28:03 +0000472 /* in most cases parent directories will already exist
Wayne Davison17fadf72003-12-15 08:14:27 +0000473 * because their information should have been previously
474 * transferred, but that may not be the case with -R */
475 if (fd2 == -1 && relative_paths && errno == ENOENT &&
Martin Poolb35d0d82002-04-08 04:10:20 +0000476 create_directory_path(fnametmp, orig_umask) == 0) {
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000477 strlcpy(fnametmp, template, sizeof(fnametmp));
478 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000479 }
480 if (fd2 == -1) {
Wayne Davisonea425412003-09-11 04:53:05 +0000481 rprintf(FERROR, "mkstemp %s failed: %s\n",
482 full_fname(fnametmp), strerror(errno));
Wayne Davison446e2392004-01-02 08:18:53 +0000483 receive_data(f_in,mapbuf,-1,NULL,file->length);
484 if (mapbuf) unmap_file(mapbuf);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000485 if (fd1 != -1) close(fd1);
486 continue;
487 }
Wayne Davison17fadf72003-12-15 08:14:27 +0000488
Wayne Davison446e2392004-01-02 08:18:53 +0000489 cleanup_set(fnametmp, fname, file, mapbuf, fd1, fd2);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000490
J.W. Schultz42d4edc2003-03-25 02:28:54 +0000491 if (!am_server && verbose) { /* log transfer */
492 rprintf(FINFO, "%s\n", fname);
Andrew Tridgell11a5a3c1998-10-28 03:28:30 +0000493 }
494
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000495 /* recv file data */
Wayne Davison446e2392004-01-02 08:18:53 +0000496 recv_ok = receive_data(f_in,mapbuf,fd2,fname,file->length);
Andrew Tridgell1b7c47c1998-11-02 04:17:56 +0000497
498 log_recv(file, &initial_stats);
Wayne Davison17fadf72003-12-15 08:14:27 +0000499
Wayne Davison446e2392004-01-02 08:18:53 +0000500 if (mapbuf) unmap_file(mapbuf);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000501 if (fd1 != -1) {
502 close(fd1);
503 }
504 close(fd2);
Wayne Davison17fadf72003-12-15 08:14:27 +0000505
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000506 if (verbose > 2)
507 rprintf(FINFO,"renaming %s to %s\n",fnametmp,fname);
508
509 finish_transfer(fname, fnametmp, file);
Andrew Tridgellc6b81a91998-09-09 06:23:27 +0000510
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000511 cleanup_disable();
Andrew Tridgell554e0a82000-01-23 07:36:56 +0000512
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000513 if (!recv_ok) {
514 if (csum_length == SUM_LENGTH) {
515 rprintf(FERROR,"ERROR: file corruption in %s. File changed during transfer?\n",
Wayne Davisonea425412003-09-11 04:53:05 +0000516 full_fname(fname));
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000517 } else {
518 if (verbose > 1)
519 rprintf(FINFO,"redoing %s(%d)\n",fname,i);
520 write_int(f_gen,i);
521 }
522 }
523 }
524
Andrew Tridgell57df1711999-11-08 13:03:05 +0000525 if (delete_after) {
526 if (recurse && delete_mode && !local_name && flist->count>0) {
527 delete_files(flist);
528 }
529 }
530
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000531 if (preserve_hard_links)
Martin Poolfae5bb32002-01-23 07:42:30 +0000532 do_hard_links();
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000533
Wayne Davison17fadf72003-12-15 08:14:27 +0000534 /* now we need to fix any directory permissions that were
535 * modified during the transfer */
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000536 for (i = 0; i < flist->count; i++) {
537 file = flist->files[i];
538 if (!file->basename || !S_ISDIR(file->mode)) continue;
Wayne Davison446e2392004-01-02 08:18:53 +0000539 recv_generator(local_name? local_name
540 : f_name_to(file,fbuf,sizeof fbuf), flist, i, -1);
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000541 }
542
543 if (verbose > 2)
544 rprintf(FINFO,"recv_files finished\n");
Wayne Davison17fadf72003-12-15 08:14:27 +0000545
Andrew Tridgell2f03f951998-07-25 02:25:22 +0000546 return 0;
547}