rsync.c 18 KB

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