rsync.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. /*
  2. * Routines common to more than one of the rsync processes.
  3. *
  4. * Copyright (C) 1996 Andrew Tridgell
  5. * Copyright (C) 1996 Paul Mackerras
  6. * Copyright (C) 2003-2008 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. #include "ifuncs.h"
  23. #if defined HAVE_LIBCHARSET_H && defined HAVE_LOCALE_CHARSET
  24. #include <libcharset.h>
  25. #elif defined HAVE_LANGINFO_H && defined HAVE_NL_LANGINFO
  26. #include <langinfo.h>
  27. #endif
  28. extern int verbose;
  29. extern int dry_run;
  30. extern int preserve_acls;
  31. extern int preserve_xattrs;
  32. extern int preserve_perms;
  33. extern int preserve_executability;
  34. extern int preserve_times;
  35. extern int am_root;
  36. extern int am_server;
  37. extern int am_sender;
  38. extern int am_generator;
  39. extern int am_starting_up;
  40. extern int allow_8bit_chars;
  41. extern int protocol_version;
  42. extern int uid_ndx;
  43. extern int gid_ndx;
  44. extern int inc_recurse;
  45. extern int inplace;
  46. extern int flist_eof;
  47. extern int keep_dirlinks;
  48. extern int make_backups;
  49. extern struct file_list *cur_flist, *first_flist, *dir_flist;
  50. extern struct chmod_mode_struct *daemon_chmod_modes;
  51. #ifdef ICONV_OPTION
  52. extern char *iconv_opt;
  53. #endif
  54. #ifdef ICONV_CONST
  55. iconv_t ic_chck = (iconv_t)-1;
  56. # ifdef ICONV_OPTION
  57. iconv_t ic_send = (iconv_t)-1, ic_recv = (iconv_t)-1;
  58. # endif
  59. static const char *default_charset(void)
  60. {
  61. # if defined HAVE_LIBCHARSET_H && defined HAVE_LOCALE_CHARSET
  62. return locale_charset();
  63. # elif defined HAVE_LANGINFO_H && defined HAVE_NL_LANGINFO
  64. return nl_langinfo(CODESET);
  65. # else
  66. return ""; /* Works with (at the very least) gnu iconv... */
  67. # endif
  68. }
  69. void setup_iconv(void)
  70. {
  71. const char *defset = default_charset();
  72. # ifdef ICONV_OPTION
  73. const char *charset;
  74. char *cp;
  75. # endif
  76. if (!am_server && !allow_8bit_chars) {
  77. /* It's OK if this fails... */
  78. ic_chck = iconv_open(defset, defset);
  79. if (verbose > 3) {
  80. if (ic_chck == (iconv_t)-1) {
  81. rprintf(FINFO,
  82. "note: iconv_open(\"%s\", \"%s\") failed (%d)"
  83. " -- using isprint() instead of iconv().\n",
  84. defset, defset, errno);
  85. } else {
  86. rprintf(FINFO,
  87. "note: iconv_open(\"%s\", \"%s\") succeeded.\n",
  88. defset, defset);
  89. }
  90. }
  91. }
  92. # ifdef ICONV_OPTION
  93. if (!iconv_opt)
  94. return;
  95. if ((cp = strchr(iconv_opt, ',')) != NULL) {
  96. if (am_server) /* A local transfer needs this. */
  97. iconv_opt = cp + 1;
  98. else
  99. *cp = '\0';
  100. }
  101. if (!*iconv_opt || (*iconv_opt == '.' && iconv_opt[1] == '\0'))
  102. charset = defset;
  103. else
  104. charset = iconv_opt;
  105. if ((ic_send = iconv_open(UTF8_CHARSET, charset)) == (iconv_t)-1) {
  106. rprintf(FERROR, "iconv_open(\"%s\", \"%s\") failed\n",
  107. UTF8_CHARSET, charset);
  108. exit_cleanup(RERR_UNSUPPORTED);
  109. }
  110. if ((ic_recv = iconv_open(charset, UTF8_CHARSET)) == (iconv_t)-1) {
  111. rprintf(FERROR, "iconv_open(\"%s\", \"%s\") failed\n",
  112. charset, UTF8_CHARSET);
  113. exit_cleanup(RERR_UNSUPPORTED);
  114. }
  115. if (verbose > 1) {
  116. rprintf(FINFO, "%s charset: %s\n",
  117. am_server ? "server" : "client",
  118. *charset ? charset : "[LOCALE]");
  119. }
  120. # endif
  121. }
  122. /* This function converts the characters in the "in" xbuf into characters
  123. * in the "out" xbuf. The "len" of the "in" xbuf is used starting from its
  124. * "pos". The "size" of the "out" xbuf restricts how many characters can be
  125. * stored, starting at its "pos+len" position. Note that the last byte of
  126. * the buffer is never used, which reserves space for a terminating '\0'.
  127. * We return a 0 on success or a -1 on error. An error also sets errno to
  128. * E2BIG, EILSEQ, or EINVAL (see below); otherwise errno will be set to 0.
  129. * The "in" xbuf is altered to update "pos" and "len". The "out" xbuf has
  130. * data appended, and its "len" incremented. If ICB_EXPAND_OUT is set in
  131. * "flags", the "out" xbuf will also be allocated if empty, and expanded if
  132. * too small (so E2BIG will not be returned). If ICB_INCLUDE_BAD is set in
  133. * "flags", any badly-encoded chars are included verbatim in the "out" xbuf,
  134. * so EILSEQ will not be returned. Likewise for ICB_INCLUDE_INCOMPLETE with
  135. * respect to an incomplete multi-byte char at the end, which ensures that
  136. * EINVAL is not returned. Anytime "in.pos" is 0 we will reset the iconv()
  137. * state prior to processing the characters. */
  138. int iconvbufs(iconv_t ic, xbuf *in, xbuf *out, int flags)
  139. {
  140. ICONV_CONST char *ibuf;
  141. size_t icnt, ocnt;
  142. char *obuf;
  143. if (!out->size && flags & ICB_EXPAND_OUT)
  144. alloc_xbuf(out, 1024);
  145. if (!in->pos)
  146. iconv(ic, NULL, 0, NULL, 0);
  147. ibuf = in->buf + in->pos;
  148. icnt = in->len;
  149. obuf = out->buf + (out->pos + out->len);
  150. ocnt = out->size - (out->pos + out->len) - 1;
  151. while (icnt) {
  152. while (iconv(ic, &ibuf, &icnt, &obuf, &ocnt) == (size_t)-1) {
  153. if (errno == EINTR)
  154. continue;
  155. if (errno == EINVAL) {
  156. if (!(flags & ICB_INCLUDE_INCOMPLETE))
  157. goto finish;
  158. } else if (errno == EILSEQ) {
  159. if (!(flags & ICB_INCLUDE_BAD))
  160. goto finish;
  161. } else {
  162. size_t opos = obuf - out->buf;
  163. if (!(flags & ICB_EXPAND_OUT)) {
  164. errno = E2BIG;
  165. goto finish;
  166. }
  167. realloc_xbuf(out, out->size + 1024);
  168. obuf = out->buf + opos;
  169. ocnt += 1024;
  170. continue;
  171. }
  172. *obuf++ = *ibuf++;
  173. ocnt--, icnt--;
  174. }
  175. }
  176. errno = 0;
  177. finish:
  178. in->len = icnt;
  179. in->pos = ibuf - in->buf;
  180. out->len = obuf - out->buf - out->pos;
  181. return errno ? -1 : 0;
  182. }
  183. #endif
  184. void send_protected_args(int fd, char *args[])
  185. {
  186. int i;
  187. #ifdef ICONV_OPTION
  188. int convert = ic_send != (iconv_t)-1;
  189. xbuf outbuf, inbuf;
  190. if (convert)
  191. alloc_xbuf(&outbuf, 1024);
  192. #endif
  193. for (i = 0; args[i]; i++) {} /* find first NULL */
  194. args[i] = "rsync"; /* set a new arg0 */
  195. if (verbose > 1)
  196. print_child_argv("protected args:", args + i + 1);
  197. do {
  198. #ifdef ICONV_OPTION
  199. if (convert) {
  200. INIT_XBUF_STRLEN(inbuf, args[i]);
  201. iconvbufs(ic_send, &inbuf, &outbuf,
  202. ICB_EXPAND_OUT | ICB_INCLUDE_BAD | ICB_INCLUDE_INCOMPLETE);
  203. outbuf.buf[outbuf.len] = '\0';
  204. write_buf(fd, outbuf.buf, outbuf.len + 1);
  205. outbuf.len = 0;
  206. } else
  207. #endif
  208. write_buf(fd, args[i], strlen(args[i]) + 1);
  209. } while (args[++i]);
  210. write_byte(fd, 0);
  211. #ifdef ICONV_OPTION
  212. if (convert)
  213. free(outbuf.buf);
  214. #endif
  215. }
  216. int read_ndx_and_attrs(int f_in, int *iflag_ptr, uchar *type_ptr,
  217. char *buf, int *len_ptr)
  218. {
  219. int len, iflags = 0;
  220. struct file_list *flist;
  221. uchar fnamecmp_type = FNAMECMP_FNAME;
  222. int ndx, save_verbose = verbose;
  223. read_loop:
  224. while (1) {
  225. ndx = read_ndx(f_in);
  226. if (ndx >= 0)
  227. break;
  228. if (ndx == NDX_DONE)
  229. return ndx;
  230. if (!inc_recurse || am_sender) {
  231. int last;
  232. if (first_flist)
  233. last = first_flist->prev->ndx_start + first_flist->prev->used - 1;
  234. else
  235. last = -1;
  236. rprintf(FERROR,
  237. "Invalid file index: %d (%d - %d) [%s]\n",
  238. ndx, NDX_DONE, last, who_am_i());
  239. exit_cleanup(RERR_PROTOCOL);
  240. }
  241. if (ndx == NDX_FLIST_EOF) {
  242. flist_eof = 1;
  243. send_msg(MSG_FLIST_EOF, "", 0, 0);
  244. continue;
  245. }
  246. ndx = NDX_FLIST_OFFSET - ndx;
  247. if (ndx < 0 || ndx >= dir_flist->used) {
  248. ndx = NDX_FLIST_OFFSET - ndx;
  249. rprintf(FERROR,
  250. "Invalid dir index: %d (%d - %d) [%s]\n",
  251. ndx, NDX_FLIST_OFFSET,
  252. NDX_FLIST_OFFSET - dir_flist->used + 1,
  253. who_am_i());
  254. exit_cleanup(RERR_PROTOCOL);
  255. }
  256. /* Send everything read from f_in to msg_fd_out. */
  257. if (verbose > 3) {
  258. rprintf(FINFO, "[%s] receiving flist for dir %d\n",
  259. who_am_i(), ndx);
  260. }
  261. verbose = 0;
  262. send_msg_int(MSG_FLIST, ndx);
  263. start_flist_forward(f_in);
  264. flist = recv_file_list(f_in);
  265. flist->parent_ndx = ndx;
  266. stop_flist_forward();
  267. verbose = save_verbose;
  268. }
  269. iflags = protocol_version >= 29 ? read_shortint(f_in)
  270. : ITEM_TRANSFER | ITEM_MISSING_DATA;
  271. /* Honor the old-style keep-alive indicator. */
  272. if (protocol_version < 30
  273. && ndx == cur_flist->used && iflags == ITEM_IS_NEW) {
  274. if (am_sender)
  275. maybe_send_keepalive();
  276. goto read_loop;
  277. }
  278. cur_flist = flist_for_ndx(ndx, "read_ndx_and_attrs");
  279. if (iflags & ITEM_BASIS_TYPE_FOLLOWS)
  280. fnamecmp_type = read_byte(f_in);
  281. *type_ptr = fnamecmp_type;
  282. if (iflags & ITEM_XNAME_FOLLOWS) {
  283. if ((len = read_vstring(f_in, buf, MAXPATHLEN)) < 0)
  284. exit_cleanup(RERR_PROTOCOL);
  285. } else {
  286. *buf = '\0';
  287. len = -1;
  288. }
  289. *len_ptr = len;
  290. if (iflags & ITEM_TRANSFER) {
  291. int i = ndx - cur_flist->ndx_start;
  292. if (i < 0 || !S_ISREG(cur_flist->files[i]->mode)) {
  293. rprintf(FERROR,
  294. "received request to transfer non-regular file: %d [%s]\n",
  295. ndx, who_am_i());
  296. exit_cleanup(RERR_PROTOCOL);
  297. }
  298. }
  299. *iflag_ptr = iflags;
  300. return ndx;
  301. }
  302. /*
  303. free a sums struct
  304. */
  305. void free_sums(struct sum_struct *s)
  306. {
  307. if (s->sums) free(s->sums);
  308. free(s);
  309. }
  310. /* This is only called when we aren't preserving permissions. Figure out what
  311. * the permissions should be and return them merged back into the mode. */
  312. mode_t dest_mode(mode_t flist_mode, mode_t stat_mode, int dflt_perms,
  313. int exists)
  314. {
  315. int new_mode;
  316. /* If the file already exists, we'll return the local permissions,
  317. * possibly tweaked by the --executability option. */
  318. if (exists) {
  319. new_mode = (flist_mode & ~CHMOD_BITS) | (stat_mode & CHMOD_BITS);
  320. if (preserve_executability && S_ISREG(flist_mode)) {
  321. /* If the source file is executable, grant execute
  322. * rights to everyone who can read, but ONLY if the
  323. * file isn't already executable. */
  324. if (!(flist_mode & 0111))
  325. new_mode &= ~0111;
  326. else if (!(stat_mode & 0111))
  327. new_mode |= (new_mode & 0444) >> 2;
  328. }
  329. } else {
  330. /* Apply destination default permissions and turn
  331. * off special permissions. */
  332. new_mode = flist_mode & (~CHMOD_BITS | dflt_perms);
  333. }
  334. return new_mode;
  335. }
  336. int set_file_attrs(const char *fname, struct file_struct *file, stat_x *sxp,
  337. const char *fnamecmp, int flags)
  338. {
  339. int updated = 0;
  340. stat_x sx2;
  341. int change_uid, change_gid;
  342. mode_t new_mode = file->mode;
  343. int inherit;
  344. if (!sxp) {
  345. if (dry_run)
  346. return 1;
  347. if (link_stat(fname, &sx2.st, 0) < 0) {
  348. rsyserr(FERROR_XFER, errno, "stat %s failed",
  349. full_fname(fname));
  350. return 0;
  351. }
  352. #ifdef SUPPORT_ACLS
  353. sx2.acc_acl = sx2.def_acl = NULL;
  354. #endif
  355. #ifdef SUPPORT_XATTRS
  356. sx2.xattr = NULL;
  357. #endif
  358. sxp = &sx2;
  359. inherit = !preserve_perms;
  360. } else
  361. inherit = !preserve_perms && file->flags & FLAG_DIR_CREATED;
  362. if (inherit && S_ISDIR(new_mode) && sxp->st.st_mode & S_ISGID) {
  363. /* We just created this directory and its setgid
  364. * bit is on, so make sure it stays on. */
  365. new_mode |= S_ISGID;
  366. }
  367. if (daemon_chmod_modes && !S_ISLNK(new_mode))
  368. new_mode = tweak_mode(new_mode, daemon_chmod_modes);
  369. #ifdef SUPPORT_ACLS
  370. if (preserve_acls && !S_ISLNK(file->mode) && !ACL_READY(*sxp))
  371. get_acl(fname, sxp);
  372. #endif
  373. #ifdef SUPPORT_XATTRS
  374. if (am_root < 0)
  375. set_stat_xattr(fname, file, new_mode);
  376. if (preserve_xattrs && fnamecmp)
  377. set_xattr(fname, file, fnamecmp, sxp);
  378. #endif
  379. if (!preserve_times || (S_ISDIR(sxp->st.st_mode) && preserve_times == 1))
  380. flags |= ATTRS_SKIP_MTIME;
  381. if (!(flags & ATTRS_SKIP_MTIME)
  382. && cmp_time(sxp->st.st_mtime, file->modtime) != 0) {
  383. int ret = set_modtime(fname, file->modtime, sxp->st.st_mode);
  384. if (ret < 0) {
  385. rsyserr(FERROR_XFER, errno, "failed to set times on %s",
  386. full_fname(fname));
  387. goto cleanup;
  388. }
  389. if (ret == 0) /* ret == 1 if symlink could not be set */
  390. updated = 1;
  391. else
  392. file->flags |= FLAG_TIME_FAILED;
  393. }
  394. change_uid = am_root && uid_ndx && sxp->st.st_uid != (uid_t)F_OWNER(file);
  395. change_gid = gid_ndx && !(file->flags & FLAG_SKIP_GROUP)
  396. && sxp->st.st_gid != (gid_t)F_GROUP(file);
  397. #if !defined HAVE_LCHOWN && !defined CHOWN_MODIFIES_SYMLINK
  398. if (S_ISLNK(sxp->st.st_mode)) {
  399. ;
  400. } else
  401. #endif
  402. if (change_uid || change_gid) {
  403. if (verbose > 2) {
  404. if (change_uid) {
  405. rprintf(FINFO,
  406. "set uid of %s from %u to %u\n",
  407. fname, (unsigned)sxp->st.st_uid, F_OWNER(file));
  408. }
  409. if (change_gid) {
  410. rprintf(FINFO,
  411. "set gid of %s from %u to %u\n",
  412. fname, (unsigned)sxp->st.st_gid, F_GROUP(file));
  413. }
  414. }
  415. if (am_root >= 0) {
  416. if (do_lchown(fname,
  417. change_uid ? (uid_t)F_OWNER(file) : sxp->st.st_uid,
  418. change_gid ? (gid_t)F_GROUP(file) : sxp->st.st_gid) != 0) {
  419. /* We shouldn't have attempted to change uid
  420. * or gid unless have the privilege. */
  421. rsyserr(FERROR_XFER, errno, "%s %s failed",
  422. change_uid ? "chown" : "chgrp",
  423. full_fname(fname));
  424. goto cleanup;
  425. }
  426. /* A lchown had been done, so we need to re-stat if
  427. * the destination had the setuid or setgid bits set
  428. * (due to the side effect of the chown call). */
  429. if (sxp->st.st_mode & (S_ISUID | S_ISGID)) {
  430. link_stat(fname, &sxp->st,
  431. keep_dirlinks && S_ISDIR(sxp->st.st_mode));
  432. }
  433. }
  434. updated = 1;
  435. }
  436. #ifdef SUPPORT_ACLS
  437. /* It's OK to call set_acl() now, even for a dir, as the generator
  438. * will enable owner-writability using chmod, if necessary.
  439. *
  440. * If set_acl() changes permission bits in the process of setting
  441. * an access ACL, it changes sxp->st.st_mode so we know whether we
  442. * need to chmod(). */
  443. if (preserve_acls && !S_ISLNK(new_mode) && set_acl(fname, file, sxp) == 0)
  444. updated = 1;
  445. #endif
  446. #ifdef HAVE_CHMOD
  447. if (!BITS_EQUAL(sxp->st.st_mode, new_mode, CHMOD_BITS)) {
  448. int ret = am_root < 0 ? 0 : do_chmod(fname, new_mode);
  449. if (ret < 0) {
  450. rsyserr(FERROR_XFER, errno,
  451. "failed to set permissions on %s",
  452. full_fname(fname));
  453. goto cleanup;
  454. }
  455. if (ret == 0) /* ret == 1 if symlink could not be set */
  456. updated = 1;
  457. }
  458. #endif
  459. if (verbose > 1 && flags & ATTRS_REPORT) {
  460. if (updated)
  461. rprintf(FCLIENT, "%s\n", fname);
  462. else
  463. rprintf(FCLIENT, "%s is uptodate\n", fname);
  464. }
  465. cleanup:
  466. if (sxp == &sx2) {
  467. #ifdef SUPPORT_ACLS
  468. if (preserve_acls)
  469. free_acl(&sx2);
  470. #endif
  471. #ifdef SUPPORT_XATTRS
  472. if (preserve_xattrs)
  473. free_xattr(&sx2);
  474. #endif
  475. }
  476. return updated;
  477. }
  478. RETSIGTYPE sig_int(UNUSED(int val))
  479. {
  480. /* KLUGE: if the user hits Ctrl-C while ssh is prompting
  481. * for a password, then our cleanup's sending of a SIGUSR1
  482. * signal to all our children may kill ssh before it has a
  483. * chance to restore the tty settings (i.e. turn echo back
  484. * on). By sleeping for a short time, ssh gets a bigger
  485. * chance to do the right thing. If child processes are
  486. * not ssh waiting for a password, then this tiny delay
  487. * shouldn't hurt anything. */
  488. msleep(400);
  489. exit_cleanup(RERR_SIGNAL);
  490. }
  491. /* Finish off a file transfer: renaming the file and setting the file's
  492. * attributes (e.g. permissions, ownership, etc.). If the robust_rename()
  493. * call is forced to copy the temp file and partialptr is both non-NULL and
  494. * not an absolute path, we stage the file into the partial-dir and then
  495. * rename it into place. This returns 1 on succcess or 0 on failure. */
  496. int finish_transfer(const char *fname, const char *fnametmp,
  497. const char *fnamecmp, const char *partialptr,
  498. struct file_struct *file, int ok_to_set_time,
  499. int overwriting_basis)
  500. {
  501. int ret;
  502. const char *temp_copy_name = partialptr && *partialptr != '/' ? partialptr : NULL;
  503. if (inplace) {
  504. if (verbose > 2)
  505. rprintf(FINFO, "finishing %s\n", fname);
  506. fnametmp = fname;
  507. goto do_set_file_attrs;
  508. }
  509. if (make_backups > 0 && overwriting_basis) {
  510. if (!make_backup(fname))
  511. return 1;
  512. if (fnamecmp == fname)
  513. fnamecmp = get_backup_name(fname);
  514. }
  515. /* Change permissions before putting the file into place. */
  516. set_file_attrs(fnametmp, file, NULL, fnamecmp,
  517. ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
  518. /* move tmp file over real file */
  519. if (verbose > 2)
  520. rprintf(FINFO, "renaming %s to %s\n", fnametmp, fname);
  521. ret = robust_rename(fnametmp, fname, temp_copy_name,
  522. file->mode & INITACCESSPERMS);
  523. if (ret < 0) {
  524. rsyserr(FERROR_XFER, errno, "%s %s -> \"%s\"",
  525. ret == -2 ? "copy" : "rename",
  526. full_fname(fnametmp), fname);
  527. if (!partialptr || (ret == -2 && temp_copy_name)
  528. || robust_rename(fnametmp, partialptr, NULL,
  529. file->mode & INITACCESSPERMS) < 0)
  530. do_unlink(fnametmp);
  531. return 0;
  532. }
  533. if (ret == 0) {
  534. /* The file was moved into place (not copied), so it's done. */
  535. return 1;
  536. }
  537. /* The file was copied, so tweak the perms of the copied file. If it
  538. * was copied to partialptr, move it into its final destination. */
  539. fnametmp = temp_copy_name ? temp_copy_name : fname;
  540. do_set_file_attrs:
  541. set_file_attrs(fnametmp, file, NULL, fnamecmp,
  542. ok_to_set_time ? 0 : ATTRS_SKIP_MTIME);
  543. if (temp_copy_name) {
  544. if (do_rename(fnametmp, fname) < 0) {
  545. rsyserr(FERROR_XFER, errno, "rename %s -> \"%s\"",
  546. full_fname(fnametmp), fname);
  547. return 0;
  548. }
  549. handle_partial_dir(temp_copy_name, PDIR_DELETE);
  550. }
  551. return 1;
  552. }
  553. struct file_list *flist_for_ndx(int ndx, const char *fatal_error_loc)
  554. {
  555. struct file_list *flist = cur_flist;
  556. if (!flist && !(flist = first_flist))
  557. goto not_found;
  558. while (ndx < flist->ndx_start-1) {
  559. if (flist == first_flist)
  560. goto not_found;
  561. flist = flist->prev;
  562. }
  563. while (ndx >= flist->ndx_start + flist->used) {
  564. if (!(flist = flist->next))
  565. goto not_found;
  566. }
  567. return flist;
  568. not_found:
  569. if (fatal_error_loc) {
  570. int first, last;
  571. if (first_flist) {
  572. first = first_flist->ndx_start - 1;
  573. last = first_flist->prev->ndx_start + first_flist->prev->used - 1;
  574. } else {
  575. first = 0;
  576. last = -1;
  577. }
  578. rprintf(FERROR,
  579. "File-list index %d not in %d - %d (%s) [%s]\n",
  580. ndx, first, last, fatal_error_loc, who_am_i());
  581. exit_cleanup(RERR_PROTOCOL);
  582. }
  583. return NULL;
  584. }
  585. const char *who_am_i(void)
  586. {
  587. if (am_starting_up)
  588. return am_server ? "server" : "client";
  589. return am_sender ? "sender" : am_generator ? "generator" : "receiver";
  590. }