hlink.c 14 KB

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