receiver.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. /*
  2. * Routines only used by the receiving process.
  3. *
  4. * Copyright (C) 1996-2000 Andrew Tridgell
  5. * Copyright (C) 1996 Paul Mackerras
  6. * Copyright (C) 2003-2009 Wayne Davison
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, visit the http://fsf.org website.
  20. */
  21. #include "rsync.h"
  22. extern int verbose;
  23. extern int dry_run;
  24. extern int do_xfers;
  25. extern int am_root;
  26. extern int am_server;
  27. extern int do_progress;
  28. extern int inc_recurse;
  29. extern int log_before_transfer;
  30. extern int stdout_format_has_i;
  31. extern int logfile_format_has_i;
  32. extern int csum_length;
  33. extern int read_batch;
  34. extern int write_batch;
  35. extern int batch_gen_fd;
  36. extern int protocol_version;
  37. extern int relative_paths;
  38. extern int preserve_hard_links;
  39. extern int preserve_perms;
  40. extern int preserve_xattrs;
  41. extern int basis_dir_cnt;
  42. extern int make_backups;
  43. extern int cleanup_got_literal;
  44. extern int remove_source_files;
  45. extern int append_mode;
  46. extern int sparse_files;
  47. extern int keep_partial;
  48. extern int checksum_seed;
  49. extern int inplace;
  50. extern int delay_updates;
  51. extern mode_t orig_umask;
  52. extern struct stats stats;
  53. extern char *tmpdir;
  54. extern char *partial_dir;
  55. extern char *basis_dir[MAX_BASIS_DIRS+1];
  56. extern struct file_list *cur_flist, *first_flist, *dir_flist;
  57. extern struct filter_list_struct daemon_filter_list;
  58. static struct bitbag *delayed_bits = NULL;
  59. static int phase = 0, redoing = 0;
  60. static flist_ndx_list batch_redo_list;
  61. /* We're either updating the basis file or an identical copy: */
  62. static int updating_basis_or_equiv;
  63. #define TMPNAME_SUFFIX ".XXXXXX"
  64. #define TMPNAME_SUFFIX_LEN ((int)sizeof TMPNAME_SUFFIX - 1)
  65. /* get_tmpname() - create a tmp filename for a given filename
  66. *
  67. * If a tmpdir is defined, use that as the directory to put it in. Otherwise,
  68. * the tmp filename is in the same directory as the given name. Note that
  69. * there may be no directory at all in the given name!
  70. *
  71. * The tmp filename is basically the given filename with a dot prepended, and
  72. * .XXXXXX appended (for mkstemp() to put its unique gunk in). We take care
  73. * to not exceed either the MAXPATHLEN or NAME_MAX, especially the last, as
  74. * the basename basically becomes 8 characters longer. In such a case, the
  75. * original name is shortened sufficiently to make it all fit.
  76. *
  77. * Of course, the only reason the file is based on the original name is to
  78. * make it easier to figure out what purpose a temp file is serving when a
  79. * transfer is in progress. */
  80. int get_tmpname(char *fnametmp, const char *fname)
  81. {
  82. int maxname, added, length = 0;
  83. const char *f;
  84. char *suf;
  85. if (tmpdir) {
  86. /* Note: this can't overflow, so the return value is safe */
  87. length = strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
  88. fnametmp[length++] = '/';
  89. }
  90. if ((f = strrchr(fname, '/')) != NULL) {
  91. ++f;
  92. if (!tmpdir) {
  93. length = f - fname;
  94. /* copy up to and including the slash */
  95. strlcpy(fnametmp, fname, length + 1);
  96. }
  97. } else
  98. f = fname;
  99. fnametmp[length++] = '.';
  100. /* The maxname value is bufsize, and includes space for the '\0'.
  101. * NAME_MAX needs an extra -1 for the name's leading dot. */
  102. maxname = MIN(MAXPATHLEN - length - TMPNAME_SUFFIX_LEN,
  103. NAME_MAX - 1 - TMPNAME_SUFFIX_LEN);
  104. if (maxname < 1) {
  105. rprintf(FERROR_XFER, "temporary filename too long: %s\n", fname);
  106. fnametmp[0] = '\0';
  107. return 0;
  108. }
  109. added = strlcpy(fnametmp + length, f, maxname);
  110. if (added >= maxname)
  111. added = maxname - 1;
  112. suf = fnametmp + length + added;
  113. /* Trim any dangling high-bit chars if the first-trimmed char (if any) is
  114. * also a high-bit char, just in case we cut into a multi-byte sequence.
  115. * We are guaranteed to stop because of the leading '.' we added. */
  116. if ((int)f[added] & 0x80) {
  117. while ((int)suf[-1] & 0x80)
  118. suf--;
  119. }
  120. memcpy(suf, TMPNAME_SUFFIX, TMPNAME_SUFFIX_LEN+1);
  121. return 1;
  122. }
  123. /* Opens a temporary file for writing.
  124. * Success: Writes name into fnametmp, returns fd.
  125. * Failure: Clobbers fnametmp, returns -1.
  126. * Calling cleanup_set() is the caller's job. */
  127. int open_tmpfile(char *fnametmp, const char *fname, struct file_struct *file)
  128. {
  129. int fd;
  130. mode_t added_perms;
  131. if (!get_tmpname(fnametmp, fname))
  132. return -1;
  133. if (am_root < 0) {
  134. /* For --fake-super, the file must be useable by the copying
  135. * user, just like it would be for root. */
  136. added_perms = S_IRUSR|S_IWUSR;
  137. } else {
  138. /* For a normal copy, we need to be able to tweak things like xattrs. */
  139. added_perms = S_IWUSR;
  140. }
  141. /* We initially set the perms without the setuid/setgid bits or group
  142. * access to ensure that there is no race condition. They will be
  143. * correctly updated after the right owner and group info is set.
  144. * (Thanks to snabb@epipe.fi for pointing this out.) */
  145. fd = do_mkstemp(fnametmp, (file->mode|added_perms) & INITACCESSPERMS);
  146. #if 0
  147. /* In most cases parent directories will already exist because their
  148. * information should have been previously transferred, but that may
  149. * not be the case with -R */
  150. if (fd == -1 && relative_paths && errno == ENOENT
  151. && create_directory_path(fnametmp) == 0) {
  152. /* Get back to name with XXXXXX in it. */
  153. get_tmpname(fnametmp, fname);
  154. fd = do_mkstemp(fnametmp, (file->mode|added_perms) & INITACCESSPERMS);
  155. }
  156. #endif
  157. if (fd == -1) {
  158. rsyserr(FERROR_XFER, errno, "mkstemp %s failed",
  159. full_fname(fnametmp));
  160. return -1;
  161. }
  162. return fd;
  163. }
  164. static int receive_data(int f_in, char *fname_r, int fd_r, OFF_T size_r,
  165. const char *fname, int fd, OFF_T total_size)
  166. {
  167. static char file_sum1[MAX_DIGEST_LEN];
  168. static char file_sum2[MAX_DIGEST_LEN];
  169. struct map_struct *mapbuf;
  170. struct sum_struct sum;
  171. int32 len, sum_len;
  172. OFF_T offset = 0;
  173. OFF_T offset2;
  174. char *data;
  175. int32 i;
  176. char *map = NULL;
  177. read_sum_head(f_in, &sum);
  178. if (fd_r >= 0 && size_r > 0) {
  179. int32 read_size = MAX(sum.blength * 2, 16*1024);
  180. mapbuf = map_file(fd_r, size_r, read_size, sum.blength);
  181. if (verbose > 2) {
  182. rprintf(FINFO, "recv mapped %s of size %.0f\n",
  183. fname_r, (double)size_r);
  184. }
  185. } else
  186. mapbuf = NULL;
  187. sum_init(checksum_seed);
  188. if (append_mode > 0) {
  189. OFF_T j;
  190. sum.flength = (OFF_T)sum.count * sum.blength;
  191. if (sum.remainder)
  192. sum.flength -= sum.blength - sum.remainder;
  193. if (append_mode == 2) {
  194. for (j = CHUNK_SIZE; j < sum.flength; j += CHUNK_SIZE) {
  195. if (do_progress)
  196. show_progress(offset, total_size);
  197. sum_update(map_ptr(mapbuf, offset, CHUNK_SIZE),
  198. CHUNK_SIZE);
  199. offset = j;
  200. }
  201. if (offset < sum.flength) {
  202. int32 len = (int32)(sum.flength - offset);
  203. if (do_progress)
  204. show_progress(offset, total_size);
  205. sum_update(map_ptr(mapbuf, offset, len), len);
  206. }
  207. }
  208. offset = sum.flength;
  209. if (fd != -1 && (j = do_lseek(fd, offset, SEEK_SET)) != offset) {
  210. rsyserr(FERROR_XFER, errno, "lseek of %s returned %.0f, not %.0f",
  211. full_fname(fname), (double)j, (double)offset);
  212. exit_cleanup(RERR_FILEIO);
  213. }
  214. }
  215. while ((i = recv_token(f_in, &data)) != 0) {
  216. if (do_progress)
  217. show_progress(offset, total_size);
  218. if (i > 0) {
  219. if (verbose > 3) {
  220. rprintf(FINFO,"data recv %d at %.0f\n",
  221. i,(double)offset);
  222. }
  223. stats.literal_data += i;
  224. cleanup_got_literal = 1;
  225. sum_update(data, i);
  226. if (fd != -1 && write_file(fd,data,i) != i)
  227. goto report_write_error;
  228. offset += i;
  229. continue;
  230. }
  231. i = -(i+1);
  232. offset2 = i * (OFF_T)sum.blength;
  233. len = sum.blength;
  234. if (i == (int)sum.count-1 && sum.remainder != 0)
  235. len = sum.remainder;
  236. stats.matched_data += len;
  237. if (verbose > 3) {
  238. rprintf(FINFO,
  239. "chunk[%d] of size %ld at %.0f offset=%.0f%s\n",
  240. i, (long)len, (double)offset2, (double)offset,
  241. updating_basis_or_equiv && offset == offset2 ? " (seek)" : "");
  242. }
  243. if (mapbuf) {
  244. map = map_ptr(mapbuf,offset2,len);
  245. see_token(map, len);
  246. sum_update(map, len);
  247. }
  248. if (updating_basis_or_equiv) {
  249. if (offset == offset2 && fd != -1) {
  250. OFF_T pos;
  251. if (flush_write_file(fd) < 0)
  252. goto report_write_error;
  253. offset += len;
  254. if ((pos = do_lseek(fd, len, SEEK_CUR)) != offset) {
  255. rsyserr(FERROR_XFER, errno,
  256. "lseek of %s returned %.0f, not %.0f",
  257. full_fname(fname),
  258. (double)pos, (double)offset);
  259. exit_cleanup(RERR_FILEIO);
  260. }
  261. continue;
  262. }
  263. }
  264. if (fd != -1 && map && write_file(fd, map, len) != (int)len)
  265. goto report_write_error;
  266. offset += len;
  267. }
  268. if (flush_write_file(fd) < 0)
  269. goto report_write_error;
  270. #ifdef HAVE_FTRUNCATE
  271. if (inplace && fd != -1 && do_ftruncate(fd, offset) < 0) {
  272. rsyserr(FERROR_XFER, errno, "ftruncate failed on %s",
  273. full_fname(fname));
  274. }
  275. #endif
  276. if (do_progress)
  277. end_progress(total_size);
  278. if (fd != -1 && offset > 0 && sparse_end(fd, offset) != 0) {
  279. report_write_error:
  280. rsyserr(FERROR_XFER, errno, "write failed on %s",
  281. full_fname(fname));
  282. exit_cleanup(RERR_FILEIO);
  283. }
  284. sum_len = sum_end(file_sum1);
  285. if (mapbuf)
  286. unmap_file(mapbuf);
  287. read_buf(f_in, file_sum2, sum_len);
  288. if (verbose > 2)
  289. rprintf(FINFO,"got file_sum\n");
  290. if (fd != -1 && memcmp(file_sum1, file_sum2, sum_len) != 0)
  291. return 0;
  292. return 1;
  293. }
  294. static void discard_receive_data(int f_in, OFF_T length)
  295. {
  296. receive_data(f_in, NULL, -1, 0, NULL, -1, length);
  297. }
  298. static void handle_delayed_updates(char *local_name)
  299. {
  300. char *fname, *partialptr;
  301. int ndx;
  302. for (ndx = -1; (ndx = bitbag_next_bit(delayed_bits, ndx)) >= 0; ) {
  303. struct file_struct *file = cur_flist->files[ndx];
  304. fname = local_name ? local_name : f_name(file, NULL);
  305. if ((partialptr = partial_dir_fname(fname)) != NULL) {
  306. if (make_backups > 0 && !make_backup(fname))
  307. continue;
  308. if (verbose > 2) {
  309. rprintf(FINFO, "renaming %s to %s\n",
  310. partialptr, fname);
  311. }
  312. /* We don't use robust_rename() here because the
  313. * partial-dir must be on the same drive. */
  314. if (do_rename(partialptr, fname) < 0) {
  315. rsyserr(FERROR_XFER, errno,
  316. "rename failed for %s (from %s)",
  317. full_fname(fname), partialptr);
  318. } else {
  319. if (remove_source_files
  320. || (preserve_hard_links && F_IS_HLINKED(file)))
  321. send_msg_int(MSG_SUCCESS, ndx);
  322. handle_partial_dir(partialptr, PDIR_DELETE);
  323. }
  324. }
  325. }
  326. }
  327. static void no_batched_update(int ndx, BOOL is_redo)
  328. {
  329. struct file_list *flist = flist_for_ndx(ndx, "no_batched_update");
  330. struct file_struct *file = flist->files[ndx - flist->ndx_start];
  331. rprintf(FERROR_XFER, "(No batched update for%s \"%s\")\n",
  332. is_redo ? " resend of" : "", f_name(file, NULL));
  333. if (inc_recurse && !dry_run)
  334. send_msg_int(MSG_NO_SEND, ndx);
  335. }
  336. static int we_want_redo(int desired_ndx)
  337. {
  338. static int redo_ndx = -1;
  339. while (redo_ndx < desired_ndx) {
  340. if (redo_ndx >= 0)
  341. no_batched_update(redo_ndx, True);
  342. if ((redo_ndx = flist_ndx_pop(&batch_redo_list)) < 0)
  343. return 0;
  344. }
  345. if (redo_ndx == desired_ndx) {
  346. redo_ndx = -1;
  347. return 1;
  348. }
  349. return 0;
  350. }
  351. static int gen_wants_ndx(int desired_ndx)
  352. {
  353. static int next_ndx = -1;
  354. static int done_cnt = 0;
  355. static BOOL got_eof = False;
  356. int flist_num = first_flist->flist_num;
  357. if (got_eof)
  358. return 0;
  359. while (next_ndx < desired_ndx) {
  360. if (inc_recurse && flist_num <= done_cnt)
  361. return 0;
  362. if (next_ndx >= 0)
  363. no_batched_update(next_ndx, False);
  364. if ((next_ndx = read_int(batch_gen_fd)) < 0) {
  365. if (inc_recurse) {
  366. done_cnt++;
  367. continue;
  368. }
  369. got_eof = True;
  370. return 0;
  371. }
  372. }
  373. if (next_ndx == desired_ndx) {
  374. next_ndx = -1;
  375. return 1;
  376. }
  377. return 0;
  378. }
  379. /**
  380. * main routine for receiver process.
  381. *
  382. * Receiver process runs on the same host as the generator process. */
  383. int recv_files(int f_in, char *local_name)
  384. {
  385. int fd1,fd2;
  386. STRUCT_STAT st;
  387. int iflags, xlen;
  388. char *fname, fbuf[MAXPATHLEN];
  389. char xname[MAXPATHLEN];
  390. char fnametmp[MAXPATHLEN];
  391. char *fnamecmp, *partialptr;
  392. char fnamecmpbuf[MAXPATHLEN];
  393. uchar fnamecmp_type;
  394. struct file_struct *file;
  395. struct stats initial_stats;
  396. int itemizing = am_server ? logfile_format_has_i : stdout_format_has_i;
  397. enum logcode log_code = log_before_transfer ? FLOG : FINFO;
  398. int max_phase = protocol_version >= 29 ? 2 : 1;
  399. int dflt_perms = (ACCESSPERMS & ~orig_umask);
  400. #ifdef SUPPORT_ACLS
  401. const char *parent_dirname = "";
  402. #endif
  403. int ndx, recv_ok;
  404. if (verbose > 2)
  405. rprintf(FINFO, "recv_files(%d) starting\n", cur_flist->used);
  406. if (delay_updates)
  407. delayed_bits = bitbag_create(cur_flist->used + 1);
  408. while (1) {
  409. cleanup_disable();
  410. /* This call also sets cur_flist. */
  411. ndx = read_ndx_and_attrs(f_in, &iflags, &fnamecmp_type,
  412. xname, &xlen);
  413. if (ndx == NDX_DONE) {
  414. if (inc_recurse && first_flist) {
  415. if (read_batch)
  416. gen_wants_ndx(first_flist->used + first_flist->ndx_start);
  417. flist_free(first_flist);
  418. if (first_flist)
  419. continue;
  420. } else if (read_batch && first_flist)
  421. gen_wants_ndx(first_flist->used);
  422. if (++phase > max_phase)
  423. break;
  424. if (verbose > 2)
  425. rprintf(FINFO, "recv_files phase=%d\n", phase);
  426. if (phase == 2 && delay_updates)
  427. handle_delayed_updates(local_name);
  428. send_msg(MSG_DONE, "", 0, 0);
  429. continue;
  430. }
  431. if (ndx - cur_flist->ndx_start >= 0)
  432. file = cur_flist->files[ndx - cur_flist->ndx_start];
  433. else
  434. file = dir_flist->files[cur_flist->parent_ndx];
  435. fname = local_name ? local_name : f_name(file, fbuf);
  436. if (verbose > 2)
  437. rprintf(FINFO, "recv_files(%s)\n", fname);
  438. #ifdef SUPPORT_XATTRS
  439. if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers)
  440. recv_xattr_request(file, f_in);
  441. #endif
  442. if (!(iflags & ITEM_TRANSFER)) {
  443. maybe_log_item(file, iflags, itemizing, xname);
  444. #ifdef SUPPORT_XATTRS
  445. if (preserve_xattrs && iflags & ITEM_REPORT_XATTR && do_xfers
  446. && !BITS_SET(iflags, ITEM_XNAME_FOLLOWS|ITEM_LOCAL_CHANGE))
  447. set_file_attrs(fname, file, NULL, fname, 0);
  448. #endif
  449. continue;
  450. }
  451. if (phase == 2) {
  452. rprintf(FERROR,
  453. "got transfer request in phase 2 [%s]\n",
  454. who_am_i());
  455. exit_cleanup(RERR_PROTOCOL);
  456. }
  457. if (file->flags & FLAG_FILE_SENT) {
  458. if (csum_length == SHORT_SUM_LENGTH) {
  459. if (keep_partial && !partial_dir)
  460. make_backups = -make_backups; /* prevents double backup */
  461. if (append_mode)
  462. sparse_files = -sparse_files;
  463. append_mode = -append_mode;
  464. csum_length = SUM_LENGTH;
  465. redoing = 1;
  466. }
  467. } else {
  468. if (csum_length != SHORT_SUM_LENGTH) {
  469. if (keep_partial && !partial_dir)
  470. make_backups = -make_backups;
  471. if (append_mode)
  472. sparse_files = -sparse_files;
  473. append_mode = -append_mode;
  474. csum_length = SHORT_SUM_LENGTH;
  475. redoing = 0;
  476. }
  477. }
  478. if (!am_server && do_progress)
  479. set_current_file_index(file, ndx);
  480. stats.num_transferred_files++;
  481. stats.total_transferred_size += F_LENGTH(file);
  482. cleanup_got_literal = 0;
  483. if (daemon_filter_list.head
  484. && check_filter(&daemon_filter_list, FLOG, fname, 0) < 0) {
  485. rprintf(FERROR, "attempt to hack rsync failed.\n");
  486. exit_cleanup(RERR_PROTOCOL);
  487. }
  488. if (read_batch) {
  489. int wanted = redoing
  490. ? we_want_redo(ndx)
  491. : gen_wants_ndx(ndx);
  492. if (!wanted) {
  493. rprintf(FINFO,
  494. "(Skipping batched update for%s \"%s\")\n",
  495. redoing ? " resend of" : "",
  496. fname);
  497. discard_receive_data(f_in, F_LENGTH(file));
  498. file->flags |= FLAG_FILE_SENT;
  499. continue;
  500. }
  501. }
  502. if (!do_xfers) { /* log the transfer */
  503. log_item(FCLIENT, file, &stats, iflags, NULL);
  504. if (read_batch)
  505. discard_receive_data(f_in, F_LENGTH(file));
  506. continue;
  507. }
  508. if (write_batch < 0) {
  509. log_item(FCLIENT, file, &stats, iflags, NULL);
  510. if (!am_server)
  511. discard_receive_data(f_in, F_LENGTH(file));
  512. continue;
  513. }
  514. partialptr = partial_dir ? partial_dir_fname(fname) : fname;
  515. if (protocol_version >= 29) {
  516. switch (fnamecmp_type) {
  517. case FNAMECMP_FNAME:
  518. fnamecmp = fname;
  519. break;
  520. case FNAMECMP_PARTIAL_DIR:
  521. fnamecmp = partialptr;
  522. break;
  523. case FNAMECMP_BACKUP:
  524. fnamecmp = get_backup_name(fname);
  525. break;
  526. case FNAMECMP_FUZZY:
  527. if (file->dirname) {
  528. pathjoin(fnamecmpbuf, MAXPATHLEN,
  529. file->dirname, xname);
  530. fnamecmp = fnamecmpbuf;
  531. } else
  532. fnamecmp = xname;
  533. break;
  534. default:
  535. if (fnamecmp_type >= basis_dir_cnt) {
  536. rprintf(FERROR,
  537. "invalid basis_dir index: %d.\n",
  538. fnamecmp_type);
  539. exit_cleanup(RERR_PROTOCOL);
  540. }
  541. pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
  542. basis_dir[fnamecmp_type], fname);
  543. fnamecmp = fnamecmpbuf;
  544. break;
  545. }
  546. if (!fnamecmp || (daemon_filter_list.head
  547. && check_filter(&daemon_filter_list, FLOG, fname, 0) < 0)) {
  548. fnamecmp = fname;
  549. fnamecmp_type = FNAMECMP_FNAME;
  550. }
  551. } else {
  552. /* Reminder: --inplace && --partial-dir are never
  553. * enabled at the same time. */
  554. if (inplace && make_backups > 0) {
  555. if (!(fnamecmp = get_backup_name(fname)))
  556. fnamecmp = fname;
  557. else
  558. fnamecmp_type = FNAMECMP_BACKUP;
  559. } else if (partial_dir && partialptr)
  560. fnamecmp = partialptr;
  561. else
  562. fnamecmp = fname;
  563. }
  564. initial_stats = stats;
  565. /* open the file */
  566. fd1 = do_open(fnamecmp, O_RDONLY, 0);
  567. if (fd1 == -1 && protocol_version < 29) {
  568. if (fnamecmp != fname) {
  569. fnamecmp = fname;
  570. fd1 = do_open(fnamecmp, O_RDONLY, 0);
  571. }
  572. if (fd1 == -1 && basis_dir[0]) {
  573. /* pre-29 allowed only one alternate basis */
  574. pathjoin(fnamecmpbuf, sizeof fnamecmpbuf,
  575. basis_dir[0], fname);
  576. fnamecmp = fnamecmpbuf;
  577. fd1 = do_open(fnamecmp, O_RDONLY, 0);
  578. }
  579. }
  580. updating_basis_or_equiv = inplace
  581. && (fnamecmp == fname || fnamecmp_type == FNAMECMP_BACKUP);
  582. if (fd1 == -1) {
  583. st.st_mode = 0;
  584. st.st_size = 0;
  585. } else if (do_fstat(fd1,&st) != 0) {
  586. rsyserr(FERROR_XFER, errno, "fstat %s failed",
  587. full_fname(fnamecmp));
  588. discard_receive_data(f_in, F_LENGTH(file));
  589. close(fd1);
  590. if (inc_recurse)
  591. send_msg_int(MSG_NO_SEND, ndx);
  592. continue;
  593. }
  594. if (fd1 != -1 && S_ISDIR(st.st_mode) && fnamecmp == fname) {
  595. /* this special handling for directories
  596. * wouldn't be necessary if robust_rename()
  597. * and the underlying robust_unlink could cope
  598. * with directories
  599. */
  600. rprintf(FERROR_XFER, "recv_files: %s is a directory\n",
  601. full_fname(fnamecmp));
  602. discard_receive_data(f_in, F_LENGTH(file));
  603. close(fd1);
  604. if (inc_recurse)
  605. send_msg_int(MSG_NO_SEND, ndx);
  606. continue;
  607. }
  608. if (fd1 != -1 && !S_ISREG(st.st_mode)) {
  609. close(fd1);
  610. fd1 = -1;
  611. }
  612. /* If we're not preserving permissions, change the file-list's
  613. * mode based on the local permissions and some heuristics. */
  614. if (!preserve_perms) {
  615. int exists = fd1 != -1;
  616. #ifdef SUPPORT_ACLS
  617. const char *dn = file->dirname ? file->dirname : ".";
  618. if (parent_dirname != dn
  619. && strcmp(parent_dirname, dn) != 0) {
  620. dflt_perms = default_perms_for_dir(dn);
  621. parent_dirname = dn;
  622. }
  623. #endif
  624. file->mode = dest_mode(file->mode, st.st_mode,
  625. dflt_perms, exists);
  626. }
  627. /* We now check to see if we are writing the file "inplace" */
  628. if (inplace) {
  629. fd2 = do_open(fname, O_WRONLY|O_CREAT, 0600);
  630. if (fd2 == -1) {
  631. rsyserr(FERROR_XFER, errno, "open %s failed",
  632. full_fname(fname));
  633. }
  634. } else {
  635. fd2 = open_tmpfile(fnametmp, fname, file);
  636. if (fd2 != -1)
  637. cleanup_set(fnametmp, partialptr, file, fd1, fd2);
  638. }
  639. if (fd2 == -1) {
  640. discard_receive_data(f_in, F_LENGTH(file));
  641. if (fd1 != -1)
  642. close(fd1);
  643. if (inc_recurse)
  644. send_msg_int(MSG_NO_SEND, ndx);
  645. continue;
  646. }
  647. /* log the transfer */
  648. if (log_before_transfer)
  649. log_item(FCLIENT, file, &initial_stats, iflags, NULL);
  650. else if (!am_server && verbose && do_progress)
  651. rprintf(FINFO, "%s\n", fname);
  652. /* recv file data */
  653. recv_ok = receive_data(f_in, fnamecmp, fd1, st.st_size,
  654. fname, fd2, F_LENGTH(file));
  655. log_item(log_code, file, &initial_stats, iflags, NULL);
  656. if (fd1 != -1)
  657. close(fd1);
  658. if (close(fd2) < 0) {
  659. rsyserr(FERROR, errno, "close failed on %s",
  660. full_fname(fnametmp));
  661. exit_cleanup(RERR_FILEIO);
  662. }
  663. if ((recv_ok && (!delay_updates || !partialptr)) || inplace) {
  664. if (partialptr == fname)
  665. partialptr = NULL;
  666. if (!finish_transfer(fname, fnametmp, fnamecmp,
  667. partialptr, file, recv_ok, 1))
  668. recv_ok = -1;
  669. else if (fnamecmp == partialptr) {
  670. do_unlink(partialptr);
  671. handle_partial_dir(partialptr, PDIR_DELETE);
  672. }
  673. } else if (keep_partial && partialptr) {
  674. if (!handle_partial_dir(partialptr, PDIR_CREATE)) {
  675. rprintf(FERROR,
  676. "Unable to create partial-dir for %s -- discarding %s.\n",
  677. local_name ? local_name : f_name(file, NULL),
  678. recv_ok ? "completed file" : "partial file");
  679. do_unlink(fnametmp);
  680. recv_ok = -1;
  681. } else if (!finish_transfer(partialptr, fnametmp, fnamecmp, NULL,
  682. file, recv_ok, !partial_dir))
  683. recv_ok = -1;
  684. else if (delay_updates && recv_ok) {
  685. bitbag_set_bit(delayed_bits, ndx);
  686. recv_ok = 2;
  687. } else
  688. partialptr = NULL;
  689. } else
  690. do_unlink(fnametmp);
  691. cleanup_disable();
  692. if (read_batch)
  693. file->flags |= FLAG_FILE_SENT;
  694. switch (recv_ok) {
  695. case 2:
  696. break;
  697. case 1:
  698. if (remove_source_files || inc_recurse
  699. || (preserve_hard_links && F_IS_HLINKED(file)))
  700. send_msg_int(MSG_SUCCESS, ndx);
  701. break;
  702. case 0: {
  703. enum logcode msgtype = redoing ? FERROR_XFER : FWARNING;
  704. if (msgtype == FERROR_XFER || verbose) {
  705. char *errstr, *redostr, *keptstr;
  706. if (!(keep_partial && partialptr) && !inplace)
  707. keptstr = "discarded";
  708. else if (partial_dir)
  709. keptstr = "put into partial-dir";
  710. else
  711. keptstr = "retained";
  712. if (msgtype == FERROR_XFER) {
  713. errstr = "ERROR";
  714. redostr = "";
  715. } else {
  716. errstr = "WARNING";
  717. redostr = read_batch ? " (may try again)"
  718. : " (will try again)";
  719. }
  720. rprintf(msgtype,
  721. "%s: %s failed verification -- update %s%s.\n",
  722. errstr, local_name ? f_name(file, NULL) : fname,
  723. keptstr, redostr);
  724. }
  725. if (!redoing) {
  726. if (read_batch)
  727. flist_ndx_push(&batch_redo_list, ndx);
  728. send_msg_int(MSG_REDO, ndx);
  729. file->flags |= FLAG_FILE_SENT;
  730. } else if (inc_recurse)
  731. send_msg_int(MSG_NO_SEND, ndx);
  732. break;
  733. }
  734. case -1:
  735. if (inc_recurse)
  736. send_msg_int(MSG_NO_SEND, ndx);
  737. break;
  738. }
  739. }
  740. if (make_backups < 0)
  741. make_backups = -make_backups;
  742. if (phase == 2 && delay_updates) /* for protocol_version < 29 */
  743. handle_delayed_updates(local_name);
  744. if (verbose > 2)
  745. rprintf(FINFO,"recv_files finished\n");
  746. return 0;
  747. }