hlink.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /*
  2. * Routines to support hard-linking.
  3. *
  4. * Copyright (C) 1996 Andrew Tridgell
  5. * Copyright (C) 1996 Paul Mackerras
  6. * Copyright (C) 2002 Martin Pool <mbp@samba.org>
  7. * Copyright (C) 2004-2009 Wayne Davison
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, visit the http://fsf.org website.
  21. */
  22. #include "rsync.h"
  23. extern int verbose;
  24. extern int dry_run;
  25. extern int list_only;
  26. extern int am_sender;
  27. extern int inc_recurse;
  28. extern int do_xfers;
  29. extern int link_dest;
  30. extern int preserve_acls;
  31. extern int preserve_xattrs;
  32. extern int make_backups;
  33. extern int protocol_version;
  34. extern int remove_source_files;
  35. extern int stdout_format_has_i;
  36. extern int maybe_ATTRS_REPORT;
  37. extern int unsort_ndx;
  38. extern char *basis_dir[MAX_BASIS_DIRS+1];
  39. extern struct file_list *cur_flist;
  40. #ifdef SUPPORT_HARD_LINKS
  41. /* Starting with protocol 30, we use a simple hashtable on the sending side
  42. * for hashing the st_dev and st_ino info. The receiving side gets told
  43. * (via flags and a "group index") which items are hard-linked together, so
  44. * we can avoid the pool of dev+inode data. For incremental recursion mode,
  45. * the receiver will use a ndx hash to remember old pathnames. */
  46. static struct hashtable *dev_tbl;
  47. static struct hashtable *prior_hlinks;
  48. static struct file_list *hlink_flist;
  49. void init_hard_links(void)
  50. {
  51. if (am_sender || protocol_version < 30)
  52. dev_tbl = hashtable_create(16, 1);
  53. else if (inc_recurse)
  54. prior_hlinks = hashtable_create(1024, 0);
  55. }
  56. struct ht_int64_node *idev_find(int64 dev, int64 ino)
  57. {
  58. static struct ht_int64_node *dev_node = NULL;
  59. struct hashtable *tbl;
  60. /* Note that some OSes have a dev == 0, so increment to avoid storing a 0. */
  61. if (!dev_node || dev_node->key != dev+1) {
  62. /* We keep a separate hash table of inodes for every device. */
  63. dev_node = hashtable_find(dev_tbl, dev+1, 1);
  64. if (!(tbl = dev_node->data))
  65. tbl = dev_node->data = hashtable_create(512, 1);
  66. } else
  67. tbl = dev_node->data;
  68. return hashtable_find(tbl, ino, 1);
  69. }
  70. void idev_destroy(void)
  71. {
  72. int i;
  73. for (i = 0; i < dev_tbl->size; i++) {
  74. struct ht_int32_node *node = HT_NODE(dev_tbl, dev_tbl->nodes, i);
  75. if (node->data)
  76. hashtable_destroy(node->data);
  77. }
  78. hashtable_destroy(dev_tbl);
  79. }
  80. static int hlink_compare_gnum(int *int1, int *int2)
  81. {
  82. struct file_struct *f1 = hlink_flist->sorted[*int1];
  83. struct file_struct *f2 = hlink_flist->sorted[*int2];
  84. int32 gnum1 = F_HL_GNUM(f1);
  85. int32 gnum2 = F_HL_GNUM(f2);
  86. if (gnum1 != gnum2)
  87. return gnum1 > gnum2 ? 1 : -1;
  88. return *int1 > *int2 ? 1 : -1;
  89. }
  90. static void match_gnums(int32 *ndx_list, int ndx_count)
  91. {
  92. int32 from, prev;
  93. struct file_struct *file, *file_next;
  94. struct ht_int32_node *node = NULL;
  95. int32 gnum, gnum_next;
  96. qsort(ndx_list, ndx_count, sizeof ndx_list[0],
  97. (int (*)()) hlink_compare_gnum);
  98. for (from = 0; from < ndx_count; from++) {
  99. file = hlink_flist->sorted[ndx_list[from]];
  100. gnum = F_HL_GNUM(file);
  101. if (inc_recurse) {
  102. node = hashtable_find(prior_hlinks, gnum, 1);
  103. if (!node->data) {
  104. if (!(node->data = new_array0(char, 5)))
  105. out_of_memory("match_gnums");
  106. assert(gnum >= hlink_flist->ndx_start);
  107. file->flags |= FLAG_HLINK_FIRST;
  108. prev = -1;
  109. } else if (CVAL(node->data, 0) == 0) {
  110. struct file_list *flist;
  111. prev = IVAL(node->data, 1);
  112. flist = flist_for_ndx(prev, NULL);
  113. if (flist)
  114. flist->files[prev - flist->ndx_start]->flags &= ~FLAG_HLINK_LAST;
  115. else {
  116. /* We skipped all prior files in this
  117. * group, so mark this as a "first". */
  118. file->flags |= FLAG_HLINK_FIRST;
  119. prev = -1;
  120. }
  121. } else
  122. prev = -1;
  123. } else {
  124. file->flags |= FLAG_HLINK_FIRST;
  125. prev = -1;
  126. }
  127. for ( ; from < ndx_count-1; file = file_next, gnum = gnum_next, from++) { /*SHARED ITERATOR*/
  128. file_next = hlink_flist->sorted[ndx_list[from+1]];
  129. gnum_next = F_HL_GNUM(file_next);
  130. if (gnum != gnum_next)
  131. break;
  132. F_HL_PREV(file) = prev;
  133. /* The linked list uses over-the-wire ndx values. */
  134. if (unsort_ndx)
  135. prev = F_NDX(file);
  136. else
  137. prev = ndx_list[from] + hlink_flist->ndx_start;
  138. }
  139. if (prev < 0 && !inc_recurse) {
  140. /* Disable hard-link bit and set DONE so that
  141. * HLINK_BUMP()-dependent values are unaffected. */
  142. file->flags &= ~(FLAG_HLINKED | FLAG_HLINK_FIRST);
  143. file->flags |= FLAG_HLINK_DONE;
  144. continue;
  145. }
  146. file->flags |= FLAG_HLINK_LAST;
  147. F_HL_PREV(file) = prev;
  148. if (inc_recurse && CVAL(node->data, 0) == 0) {
  149. if (unsort_ndx)
  150. prev = F_NDX(file);
  151. else
  152. prev = ndx_list[from] + hlink_flist->ndx_start;
  153. SIVAL(node->data, 1, prev);
  154. }
  155. }
  156. }
  157. /* Analyze the hard-links in the file-list by creating a list of all the
  158. * items that have hlink data, sorting them, and matching up identical
  159. * values into clusters. These will be a single linked list from last
  160. * to first when we're done. */
  161. void match_hard_links(struct file_list *flist)
  162. {
  163. if (!list_only && flist->used) {
  164. int i, ndx_count = 0;
  165. int32 *ndx_list;
  166. if (!(ndx_list = new_array(int32, flist->used)))
  167. out_of_memory("match_hard_links");
  168. for (i = 0; i < flist->used; i++) {
  169. if (F_IS_HLINKED(flist->sorted[i]))
  170. ndx_list[ndx_count++] = i;
  171. }
  172. hlink_flist = flist;
  173. if (ndx_count)
  174. match_gnums(ndx_list, ndx_count);
  175. free(ndx_list);
  176. }
  177. if (protocol_version < 30)
  178. idev_destroy();
  179. }
  180. static int maybe_hard_link(struct file_struct *file, int ndx,
  181. const char *fname, int statret, stat_x *sxp,
  182. const char *oldname, STRUCT_STAT *old_stp,
  183. const char *realname, int itemizing, enum logcode code)
  184. {
  185. if (statret == 0) {
  186. if (sxp->st.st_dev == old_stp->st_dev
  187. && sxp->st.st_ino == old_stp->st_ino) {
  188. if (itemizing) {
  189. itemize(fname, file, ndx, statret, sxp,
  190. ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
  191. 0, "");
  192. }
  193. if (verbose > 1 && maybe_ATTRS_REPORT)
  194. rprintf(FCLIENT, "%s is uptodate\n", fname);
  195. file->flags |= FLAG_HLINK_DONE;
  196. return 0;
  197. }
  198. if (make_backups > 0) {
  199. if (!make_backup(fname))
  200. return -1;
  201. } else if (robust_unlink(fname)) {
  202. rsyserr(FERROR_XFER, errno, "unlink %s failed",
  203. full_fname(fname));
  204. return -1;
  205. }
  206. }
  207. if (hard_link_one(file, fname, oldname, 0)) {
  208. if (itemizing) {
  209. itemize(fname, file, ndx, statret, sxp,
  210. ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS, 0,
  211. realname);
  212. }
  213. if (code != FNONE && verbose)
  214. rprintf(code, "%s => %s\n", fname, realname);
  215. return 0;
  216. }
  217. return -1;
  218. }
  219. /* Figure out if a prior entry is still there or if we just have a
  220. * cached name for it. */
  221. static char *check_prior(struct file_struct *file, int gnum,
  222. int *prev_ndx_p, struct file_list **flist_p)
  223. {
  224. struct file_struct *fp;
  225. struct ht_int32_node *node;
  226. int prev_ndx = F_HL_PREV(file);
  227. while (1) {
  228. struct file_list *flist;
  229. if (prev_ndx < 0
  230. || (flist = flist_for_ndx(prev_ndx, NULL)) == NULL)
  231. break;
  232. fp = flist->files[prev_ndx - flist->ndx_start];
  233. if (!(fp->flags & FLAG_SKIP_HLINK)) {
  234. *prev_ndx_p = prev_ndx;
  235. *flist_p = flist;
  236. return NULL;
  237. }
  238. F_HL_PREV(file) = prev_ndx = F_HL_PREV(fp);
  239. }
  240. if (inc_recurse
  241. && (node = hashtable_find(prior_hlinks, gnum, 0)) != NULL) {
  242. assert(node->data != NULL);
  243. if (CVAL(node->data, 0) != 0) {
  244. *prev_ndx_p = -1;
  245. *flist_p = NULL;
  246. return node->data;
  247. }
  248. /* The prior file must have been skipped. */
  249. F_HL_PREV(file) = -1;
  250. }
  251. *prev_ndx_p = -1;
  252. *flist_p = NULL;
  253. return NULL;
  254. }
  255. /* Only called if FLAG_HLINKED is set and FLAG_HLINK_FIRST is not. Returns:
  256. * 0 = process the file, 1 = skip the file, -1 = error occurred. */
  257. int hard_link_check(struct file_struct *file, int ndx, const char *fname,
  258. int statret, stat_x *sxp, int itemizing,
  259. enum logcode code)
  260. {
  261. STRUCT_STAT prev_st;
  262. char namebuf[MAXPATHLEN], altbuf[MAXPATHLEN];
  263. char *realname, *prev_name;
  264. struct file_list *flist;
  265. int gnum = inc_recurse ? F_HL_GNUM(file) : -1;
  266. int prev_ndx;
  267. prev_name = realname = check_prior(file, gnum, &prev_ndx, &flist);
  268. if (!prev_name) {
  269. struct file_struct *prev_file;
  270. if (!flist) {
  271. /* The previous file was skipped, so this one is
  272. * treated as if it were the first in its group. */
  273. return 0;
  274. }
  275. prev_file = flist->files[prev_ndx - flist->ndx_start];
  276. /* Is the previous link not complete yet? */
  277. if (!(prev_file->flags & FLAG_HLINK_DONE)) {
  278. /* Is the previous link being transferred? */
  279. if (prev_file->flags & FLAG_FILE_SENT) {
  280. /* Add ourselves to the list of files that will
  281. * be updated when the transfer completes, and
  282. * mark ourself as waiting for the transfer. */
  283. F_HL_PREV(file) = F_HL_PREV(prev_file);
  284. F_HL_PREV(prev_file) = ndx;
  285. file->flags |= FLAG_FILE_SENT;
  286. cur_flist->in_progress++;
  287. return 1;
  288. }
  289. return 0;
  290. }
  291. /* There is a finished file to link with! */
  292. if (!(prev_file->flags & FLAG_HLINK_FIRST)) {
  293. /* The previous previous is FIRST when prev is not. */
  294. prev_name = realname = check_prior(prev_file, gnum, &prev_ndx, &flist);
  295. assert(prev_name != NULL || flist != NULL);
  296. /* Update our previous pointer to point to the FIRST. */
  297. F_HL_PREV(file) = prev_ndx;
  298. }
  299. if (!prev_name) {
  300. int alt_dest;
  301. prev_file = flist->files[prev_ndx - flist->ndx_start];
  302. /* F_HL_PREV() is alt_dest value when DONE && FIRST. */
  303. alt_dest = F_HL_PREV(prev_file);
  304. if (alt_dest >= 0 && dry_run) {
  305. pathjoin(namebuf, MAXPATHLEN, basis_dir[alt_dest],
  306. f_name(prev_file, NULL));
  307. prev_name = namebuf;
  308. realname = f_name(prev_file, altbuf);
  309. } else {
  310. prev_name = f_name(prev_file, namebuf);
  311. realname = prev_name;
  312. }
  313. }
  314. }
  315. if (link_stat(prev_name, &prev_st, 0) < 0) {
  316. if (!dry_run || errno != ENOENT) {
  317. rsyserr(FERROR_XFER, errno, "stat %s failed", full_fname(prev_name));
  318. return -1;
  319. }
  320. /* A new hard-link will get a new dev & inode, so approximate
  321. * those values in dry-run mode by zeroing them. */
  322. memset(&prev_st, 0, sizeof prev_st);
  323. }
  324. if (statret < 0 && basis_dir[0] != NULL) {
  325. /* If we match an alt-dest item, we don't output this as a change. */
  326. char cmpbuf[MAXPATHLEN];
  327. stat_x alt_sx;
  328. int j = 0;
  329. #ifdef SUPPORT_ACLS
  330. alt_sx.acc_acl = alt_sx.def_acl = NULL;
  331. #endif
  332. #ifdef SUPPORT_XATTRS
  333. alt_sx.xattr = NULL;
  334. #endif
  335. do {
  336. pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
  337. if (link_stat(cmpbuf, &alt_sx.st, 0) < 0)
  338. continue;
  339. if (link_dest) {
  340. if (prev_st.st_dev != alt_sx.st.st_dev
  341. || prev_st.st_ino != alt_sx.st.st_ino)
  342. continue;
  343. statret = 1;
  344. if (stdout_format_has_i == 0
  345. || (verbose < 2 && stdout_format_has_i < 2)) {
  346. itemizing = 0;
  347. code = FNONE;
  348. if (verbose > 1 && maybe_ATTRS_REPORT)
  349. rprintf(FCLIENT, "%s is uptodate\n", fname);
  350. }
  351. break;
  352. }
  353. if (!unchanged_file(cmpbuf, file, &alt_sx.st))
  354. continue;
  355. statret = 1;
  356. if (unchanged_attrs(cmpbuf, file, &alt_sx))
  357. break;
  358. } while (basis_dir[++j] != NULL);
  359. if (statret == 1) {
  360. sxp->st = alt_sx.st;
  361. #ifdef SUPPORT_ACLS
  362. if (preserve_acls && !S_ISLNK(file->mode)) {
  363. free_acl(sxp);
  364. if (!ACL_READY(alt_sx))
  365. get_acl(cmpbuf, sxp);
  366. else {
  367. sxp->acc_acl = alt_sx.acc_acl;
  368. sxp->def_acl = alt_sx.def_acl;
  369. alt_sx.acc_acl = alt_sx.def_acl = NULL;
  370. }
  371. }
  372. #endif
  373. #ifdef SUPPORT_XATTRS
  374. if (preserve_xattrs) {
  375. free_xattr(sxp);
  376. if (!XATTR_READY(alt_sx))
  377. get_xattr(cmpbuf, sxp);
  378. else {
  379. sxp->xattr = alt_sx.xattr;
  380. alt_sx.xattr = NULL;
  381. }
  382. }
  383. #endif
  384. } else {
  385. #ifdef SUPPORT_ACLS
  386. if (preserve_acls)
  387. free_acl(&alt_sx);
  388. #endif
  389. #ifdef SUPPORT_XATTRS
  390. if (preserve_xattrs)
  391. free_xattr(&alt_sx);
  392. #endif
  393. }
  394. }
  395. if (maybe_hard_link(file, ndx, fname, statret, sxp, prev_name, &prev_st,
  396. realname, itemizing, code) < 0)
  397. return -1;
  398. if (remove_source_files == 1 && do_xfers)
  399. send_msg_int(MSG_SUCCESS, ndx);
  400. return 1;
  401. }
  402. int hard_link_one(struct file_struct *file, const char *fname,
  403. const char *oldname, int terse)
  404. {
  405. if (do_link(oldname, fname) < 0) {
  406. enum logcode code;
  407. if (terse) {
  408. if (!verbose)
  409. return 0;
  410. code = FINFO;
  411. } else
  412. code = FERROR_XFER;
  413. rsyserr(code, errno, "link %s => %s failed",
  414. full_fname(fname), oldname);
  415. return 0;
  416. }
  417. file->flags |= FLAG_HLINK_DONE;
  418. return 1;
  419. }
  420. void finish_hard_link(struct file_struct *file, const char *fname, int fin_ndx,
  421. STRUCT_STAT *stp, int itemizing, enum logcode code,
  422. int alt_dest)
  423. {
  424. stat_x prev_sx;
  425. STRUCT_STAT st;
  426. char prev_name[MAXPATHLEN], alt_name[MAXPATHLEN];
  427. const char *our_name;
  428. struct file_list *flist;
  429. int prev_statret, ndx, prev_ndx = F_HL_PREV(file);
  430. if (stp == NULL && prev_ndx >= 0) {
  431. if (link_stat(fname, &st, 0) < 0) {
  432. rsyserr(FERROR_XFER, errno, "stat %s failed",
  433. full_fname(fname));
  434. return;
  435. }
  436. stp = &st;
  437. }
  438. /* FIRST combined with DONE means we were the first to get done. */
  439. file->flags |= FLAG_HLINK_FIRST | FLAG_HLINK_DONE;
  440. F_HL_PREV(file) = alt_dest;
  441. if (alt_dest >= 0 && dry_run) {
  442. pathjoin(alt_name, MAXPATHLEN, basis_dir[alt_dest],
  443. f_name(file, NULL));
  444. our_name = alt_name;
  445. } else
  446. our_name = fname;
  447. #ifdef SUPPORT_ACLS
  448. prev_sx.acc_acl = prev_sx.def_acl = NULL;
  449. #endif
  450. #ifdef SUPPORT_XATTRS
  451. prev_sx.xattr = NULL;
  452. #endif
  453. while ((ndx = prev_ndx) >= 0) {
  454. int val;
  455. flist = flist_for_ndx(ndx, "finish_hard_link");
  456. file = flist->files[ndx - flist->ndx_start];
  457. file->flags = (file->flags & ~FLAG_HLINK_FIRST) | FLAG_HLINK_DONE;
  458. prev_ndx = F_HL_PREV(file);
  459. F_HL_PREV(file) = fin_ndx;
  460. prev_statret = link_stat(f_name(file, prev_name), &prev_sx.st, 0);
  461. val = maybe_hard_link(file, ndx, prev_name, prev_statret, &prev_sx,
  462. our_name, stp, fname, itemizing, code);
  463. flist->in_progress--;
  464. #ifdef SUPPORT_ACLS
  465. if (preserve_acls)
  466. free_acl(&prev_sx);
  467. #endif
  468. #ifdef SUPPORT_XATTRS
  469. if (preserve_xattrs)
  470. free_xattr(&prev_sx);
  471. #endif
  472. if (val < 0)
  473. continue;
  474. if (remove_source_files == 1 && do_xfers)
  475. send_msg_int(MSG_SUCCESS, ndx);
  476. }
  477. if (inc_recurse) {
  478. int gnum = F_HL_GNUM(file);
  479. struct ht_int32_node *node = hashtable_find(prior_hlinks, gnum, 0);
  480. if (node == NULL) {
  481. rprintf(FERROR, "Unable to find a hlink node for %d (%s)\n", gnum, f_name(file, prev_name));
  482. exit_cleanup(RERR_MESSAGEIO);
  483. }
  484. if (node->data == NULL) {
  485. rprintf(FERROR, "Hlink node data for %d is NULL (%s)\n", gnum, f_name(file, prev_name));
  486. exit_cleanup(RERR_MESSAGEIO);
  487. }
  488. if (CVAL(node->data, 0) != 0) {
  489. rprintf(FERROR, "Hlink node data for %d already has path=%s (%s)\n",
  490. gnum, (char*)node->data, f_name(file, prev_name));
  491. exit_cleanup(RERR_MESSAGEIO);
  492. }
  493. free(node->data);
  494. if (!(node->data = strdup(our_name)))
  495. out_of_memory("finish_hard_link");
  496. }
  497. }
  498. int skip_hard_link(struct file_struct *file, struct file_list **flist_p)
  499. {
  500. struct file_list *flist;
  501. int prev_ndx;
  502. file->flags |= FLAG_SKIP_HLINK;
  503. if (!(file->flags & FLAG_HLINK_LAST))
  504. return -1;
  505. check_prior(file, F_HL_GNUM(file), &prev_ndx, &flist);
  506. if (prev_ndx >= 0) {
  507. file = flist->files[prev_ndx - flist->ndx_start];
  508. if (file->flags & (FLAG_HLINK_DONE|FLAG_FILE_SENT))
  509. return -1;
  510. file->flags |= FLAG_HLINK_LAST;
  511. *flist_p = flist;
  512. }
  513. return prev_ndx;
  514. }
  515. #endif