Make idev, hlink and file_struct + strings use allocation
[rsync/rsync.git] / receiver.c
... / ...
CommitLineData
1/* -*- c-file-style: "linux" -*-
2
3 Copyright (C) 1996-2000 by Andrew Tridgell
4 Copyright (C) Paul Mackerras 1996
5
6 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.
10
11 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.
15
16 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;
26extern int csum_length;
27extern struct stats stats;
28extern int dry_run;
29extern int am_server;
30extern int relative_paths;
31extern int preserve_hard_links;
32extern int cvs_exclude;
33extern int io_error;
34extern char *tmpdir;
35extern char *compare_dest;
36extern int make_backups;
37extern int do_progress;
38extern char *backup_dir;
39extern char *backup_suffix;
40extern int backup_suffix_len;
41extern int cleanup_got_literal;
42
43static void delete_one(char *fn, int is_dir)
44{
45 if (!is_dir) {
46 if (robust_unlink(fn) != 0) {
47 rprintf(FERROR, "delete_one: unlink %s failed: %s\n",
48 full_fname(fn), strerror(errno));
49 } else if (verbose) {
50 rprintf(FINFO, "deleting %s\n", fn);
51 }
52 } else {
53 if (do_rmdir(fn) != 0) {
54 if (errno != ENOTEMPTY && errno != EEXIST) {
55 rprintf(FERROR, "delete_one: rmdir %s failed: %s\n",
56 full_fname(fn), strerror(errno));
57 }
58 } else if (verbose) {
59 rprintf(FINFO, "deleting directory %s\n", fn);
60 }
61 }
62}
63
64
65static int is_backup_file(char *fn)
66{
67 int k = strlen(fn) - backup_suffix_len;
68 return k > 0 && strcmp(fn+k, backup_suffix) == 0;
69}
70
71
72/* this deletes any files on the receiving side that are not present
73 * on the sending side. For version 1.6.4 I have changed the behaviour
74 * to match more closely what most people seem to expect of this option */
75void delete_files(struct file_list *flist)
76{
77 struct file_list *local_file_list;
78 int i, j;
79 char *argv[1], fbuf[MAXPATHLEN];
80 extern int module_id;
81 extern int ignore_errors;
82 extern int max_delete;
83 static int deletion_count;
84
85 if (cvs_exclude)
86 add_cvs_excludes();
87
88 if (io_error && !(lp_ignore_errors(module_id) || ignore_errors)) {
89 rprintf(FINFO,"IO error encountered - skipping file deletion\n");
90 return;
91 }
92
93 for (j = 0;j < flist->count; j++) {
94 if (!(flist->files[j]->flags & FLAG_TOP_DIR)
95 || !S_ISDIR(flist->files[j]->mode))
96 continue;
97
98 argv[0] = f_name_to(flist->files[j], fbuf);
99
100 if (!(local_file_list = send_file_list(-1, 1, argv)))
101 continue;
102
103 if (verbose > 1)
104 rprintf(FINFO,"deleting in %s\n", fbuf);
105
106 for (i = local_file_list->count-1; i >= 0; i--) {
107 if (max_delete && deletion_count > max_delete) break;
108 if (!local_file_list->files[i]->basename) continue;
109 if (-1 == flist_find(flist,local_file_list->files[i])) {
110 char *f = f_name(local_file_list->files[i]);
111 if (make_backups && (backup_dir || !is_backup_file(f))) {
112 (void) make_backup(f);
113 if (verbose)
114 rprintf(FINFO, "deleting %s\n", f);
115 } else {
116 int mode = local_file_list->files[i]->mode;
117 delete_one(f, S_ISDIR(mode) != 0);
118 }
119 deletion_count++;
120 }
121 }
122 flist_free(local_file_list);
123 }
124}
125
126
127/*
128 * get_tmpname() - create a tmp filename for a given filename
129 *
130 * If a tmpdir is defined, use that as the directory to
131 * put it in. Otherwise, the tmp filename is in the same
132 * directory as the given name. Note that there may be no
133 * directory at all in the given name!
134 *
135 * The tmp filename is basically the given filename with a
136 * dot prepended, and .XXXXXX appended (for mkstemp() to
137 * put its unique gunk in). Take care to not exceed
138 * either the MAXPATHLEN or NAME_MAX, esp. the last, as
139 * the basename basically becomes 8 chars longer. In that
140 * case, the original name is shortened sufficiently to
141 * make it all fit.
142 *
143 * Of course, there's no real reason for the tmp name to
144 * look like the original, except to satisfy us humans.
145 * As long as it's unique, rsync will work.
146 */
147
148static int get_tmpname(char *fnametmp, char *fname)
149{
150 char *f;
151 int length = 0;
152 int maxname;
153
154 if (tmpdir) {
155 /* Note: this can't overflow, so the return value is safe */
156 length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
157 fnametmp[length++] = '/';
158 fnametmp[length] = '\0'; /* always NULL terminated */
159 }
160
161 if ((f = strrchr(fname, '/')) != NULL) {
162 ++f;
163 if (!tmpdir) {
164 length = f - fname;
165 /* copy up to and including the slash */
166 strlcpy(fnametmp, fname, length + 1);
167 }
168 } else
169 f = fname;
170 fnametmp[length++] = '.';
171 fnametmp[length] = '\0'; /* always NULL terminated */
172
173 maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8);
174
175 if (maxname < 1) {
176 rprintf(FERROR, "temporary filename too long: %s\n", fname);
177 fnametmp[0] = '\0';
178 return 0;
179 }
180
181 strlcpy(fnametmp + length, f, maxname);
182 strcat(fnametmp + length, ".XXXXXX");
183
184 return 1;
185}
186
187
188static int receive_data(int f_in,struct map_struct *mapbuf,int fd,char *fname,
189 OFF_T total_size)
190{
191 int i;
192 struct sum_struct sum;
193 unsigned int len;
194 OFF_T offset = 0;
195 OFF_T offset2;
196 char *data;
197 static char file_sum1[MD4_SUM_LENGTH];
198 static char file_sum2[MD4_SUM_LENGTH];
199 char *map=NULL;
200
201 read_sum_head(f_in, &sum);
202
203 sum_init();
204
205 while ((i = recv_token(f_in, &data)) != 0) {
206 if (do_progress)
207 show_progress(offset, total_size);
208
209 if (i > 0) {
210 if (verbose > 3) {
211 rprintf(FINFO,"data recv %d at %.0f\n",
212 i,(double)offset);
213 }
214
215 stats.literal_data += i;
216 cleanup_got_literal = 1;
217
218 sum_update(data,i);
219
220 if (fd != -1 && write_file(fd,data,i) != i) {
221 rprintf(FERROR, "write failed on %s: %s\n",
222 full_fname(fname), strerror(errno));
223 exit_cleanup(RERR_FILEIO);
224 }
225 offset += i;
226 continue;
227 }
228
229 i = -(i+1);
230 offset2 = i*(OFF_T)sum.blength;
231 len = sum.blength;
232 if (i == (int) sum.count-1 && sum.remainder != 0)
233 len = sum.remainder;
234
235 stats.matched_data += len;
236
237 if (verbose > 3)
238 rprintf(FINFO,"chunk[%d] of size %d at %.0f offset=%.0f\n",
239 i,len,(double)offset2,(double)offset);
240
241 if (mapbuf) {
242 map = map_ptr(mapbuf,offset2,len);
243
244 see_token(map, len);
245 sum_update(map,len);
246 }
247
248 if (fd != -1 && write_file(fd,map,len) != (int) len) {
249 rprintf(FERROR, "write failed on %s: %s\n",
250 full_fname(fname), strerror(errno));
251 exit_cleanup(RERR_FILEIO);
252 }
253 offset += len;
254 }
255
256 flush_write_file(fd);
257
258 if (do_progress)
259 end_progress(total_size);
260
261 if (fd != -1 && offset > 0 && sparse_end(fd) != 0) {
262 rprintf(FERROR, "write failed on %s: %s\n",
263 full_fname(fname), strerror(errno));
264 exit_cleanup(RERR_FILEIO);
265 }
266
267 sum_end(file_sum1);
268
269 read_buf(f_in,file_sum2,MD4_SUM_LENGTH);
270 if (verbose > 2) {
271 rprintf(FINFO,"got file_sum\n");
272 }
273 if (fd != -1 && memcmp(file_sum1,file_sum2,MD4_SUM_LENGTH) != 0) {
274 return 0;
275 }
276 return 1;
277}
278
279
280/**
281 * main routine for receiver process.
282 *
283 * Receiver process runs on the same host as the generator process. */
284int recv_files(int f_in,struct file_list *flist,char *local_name)
285{
286 int fd1,fd2;
287 STRUCT_STAT st;
288 char *fname, fbuf[MAXPATHLEN];
289 char template[MAXPATHLEN];
290 char fnametmp[MAXPATHLEN];
291 char *fnamecmp;
292 char fnamecmpbuf[MAXPATHLEN];
293 struct map_struct *mapbuf;
294 int i;
295 struct file_struct *file;
296 int phase=0;
297 int recv_ok;
298 extern struct stats stats;
299 extern int preserve_perms;
300 extern int delete_after;
301 extern int orig_umask;
302 struct stats initial_stats;
303
304 if (verbose > 2) {
305 rprintf(FINFO,"recv_files(%d) starting\n",flist->count);
306 }
307
308 if (flist->hlink_pool)
309 {
310 pool_destroy(flist->hlink_pool);
311 flist->hlink_pool = NULL;
312 }
313
314 while (1) {
315 cleanup_disable();
316
317 i = read_int(f_in);
318 if (i == -1) {
319 if (phase == 0) {
320 phase++;
321 csum_length = SUM_LENGTH;
322 if (verbose > 2)
323 rprintf(FINFO,"recv_files phase=%d\n",phase);
324 send_msg(MSG_DONE, "", 0);
325 continue;
326 }
327 break;
328 }
329
330 if (i < 0 || i >= flist->count) {
331 rprintf(FERROR,"Invalid file index %d in recv_files (count=%d)\n",
332 i, flist->count);
333 exit_cleanup(RERR_PROTOCOL);
334 }
335
336 file = flist->files[i];
337
338 stats.current_file_index = i;
339 stats.num_transferred_files++;
340 stats.total_transferred_size += file->length;
341 cleanup_got_literal = 0;
342
343 if (local_name)
344 fname = local_name;
345 else
346 fname = f_name_to(file, fbuf);
347
348 if (dry_run) {
349 if (!am_server && verbose) { /* log transfer */
350 rprintf(FINFO, "%s\n", fname);
351 }
352 continue;
353 }
354
355 initial_stats = stats;
356
357 if (verbose > 2)
358 rprintf(FINFO,"recv_files(%s)\n",fname);
359
360 fnamecmp = fname;
361
362 /* open the file */
363 fd1 = do_open(fnamecmp, O_RDONLY, 0);
364
365 if ((fd1 == -1) && (compare_dest != NULL)) {
366 /* try the file at compare_dest instead */
367 pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
368 compare_dest, fname);
369 fnamecmp = fnamecmpbuf;
370 fd1 = do_open(fnamecmp, O_RDONLY, 0);
371 }
372
373 if (fd1 != -1 && do_fstat(fd1,&st) != 0) {
374 rprintf(FERROR, "fstat %s failed: %s\n",
375 full_fname(fnamecmp), strerror(errno));
376 receive_data(f_in,NULL,-1,NULL,file->length);
377 close(fd1);
378 continue;
379 }
380
381 if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
382 /* this special handling for directories
383 * wouldn't be necessary if robust_rename()
384 * and the underlying robust_unlink could cope
385 * with directories
386 */
387 rprintf(FERROR,"recv_files: %s is a directory\n",
388 full_fname(fnamecmp));
389 receive_data(f_in, NULL, -1, NULL, file->length);
390 close(fd1);
391 continue;
392 }
393
394 if (fd1 != -1 && !S_ISREG(st.st_mode)) {
395 close(fd1);
396 fd1 = -1;
397 mapbuf = NULL;
398 }
399
400 if (fd1 != -1 && !preserve_perms) {
401 /* if the file exists already and we aren't preserving
402 * permissions then act as though the remote end sent
403 * us the file permissions we already have */
404 file->mode = st.st_mode;
405 }
406
407 if (fd1 != -1 && st.st_size > 0) {
408 mapbuf = map_file(fd1,st.st_size);
409 if (verbose > 2)
410 rprintf(FINFO,"recv mapped %s of size %.0f\n",fnamecmp,(double)st.st_size);
411 } else
412 mapbuf = NULL;
413
414 if (!get_tmpname(fnametmp,fname)) {
415 if (mapbuf) unmap_file(mapbuf);
416 if (fd1 != -1) close(fd1);
417 continue;
418 }
419
420 strlcpy(template, fnametmp, sizeof template);
421
422 /* we initially set the perms without the
423 * setuid/setgid bits to ensure that there is no race
424 * condition. They are then correctly updated after
425 * the lchown. Thanks to snabb@epipe.fi for pointing
426 * this out. We also set it initially without group
427 * access because of a similar race condition. */
428 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
429
430 /* in most cases parent directories will already exist
431 * because their information should have been previously
432 * transferred, but that may not be the case with -R */
433 if (fd2 == -1 && relative_paths && errno == ENOENT &&
434 create_directory_path(fnametmp, orig_umask) == 0) {
435 strlcpy(fnametmp, template, sizeof fnametmp);
436 fd2 = do_mkstemp(fnametmp, file->mode & INITACCESSPERMS);
437 }
438 if (fd2 == -1) {
439 rprintf(FERROR, "mkstemp %s failed: %s\n",
440 full_fname(fnametmp), strerror(errno));
441 receive_data(f_in,mapbuf,-1,NULL,file->length);
442 if (mapbuf) unmap_file(mapbuf);
443 if (fd1 != -1) close(fd1);
444 continue;
445 }
446
447 cleanup_set(fnametmp, fname, file, mapbuf, fd1, fd2);
448
449 if (!am_server && verbose) { /* log transfer */
450 rprintf(FINFO, "%s\n", fname);
451 }
452
453 /* recv file data */
454 recv_ok = receive_data(f_in,mapbuf,fd2,fname,file->length);
455
456 log_recv(file, &initial_stats);
457
458 if (mapbuf) unmap_file(mapbuf);
459 if (fd1 != -1) {
460 close(fd1);
461 }
462 close(fd2);
463
464 if (verbose > 2)
465 rprintf(FINFO,"renaming %s to %s\n",fnametmp,fname);
466
467 finish_transfer(fname, fnametmp, file);
468
469 cleanup_disable();
470
471 if (!recv_ok) {
472 if (csum_length == SUM_LENGTH) {
473 rprintf(FERROR,"ERROR: file corruption in %s. File changed during transfer?\n",
474 full_fname(fname));
475 } else {
476 char buf[4];
477 if (verbose > 1)
478 rprintf(FINFO,"redoing %s(%d)\n",fname,i);
479 SIVAL(buf, 0, i);
480 send_msg(MSG_REDO, buf, 4);
481 }
482 }
483 }
484
485 if (delete_after && recurse && delete_mode && !local_name
486 && flist->count > 0)
487 delete_files(flist);
488
489 if (verbose > 2)
490 rprintf(FINFO,"recv_files finished\n");
491
492 return 0;
493}