| 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-2007 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 version 2 as |
| 11 | * published by the Free Software Foundation. |
| 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, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. |
| 21 | */ |
| 22 | |
| 23 | #include "rsync.h" |
| 24 | |
| 25 | extern int verbose; |
| 26 | extern int dry_run; |
| 27 | extern int do_xfers; |
| 28 | extern int link_dest; |
| 29 | extern int make_backups; |
| 30 | extern int protocol_version; |
| 31 | extern int remove_source_files; |
| 32 | extern int stdout_format_has_i; |
| 33 | extern int maybe_ATTRS_REPORT; |
| 34 | extern char *basis_dir[]; |
| 35 | extern struct file_list *cur_flist; |
| 36 | |
| 37 | #ifdef SUPPORT_HARD_LINKS |
| 38 | |
| 39 | #define HASH_LOAD_LIMIT(size) ((size)*3/4) |
| 40 | #define FPTR(i) (cur_flist->files[i]) |
| 41 | |
| 42 | struct ihash_table { |
| 43 | int32 size; |
| 44 | int32 entries; |
| 45 | struct idev_node *buckets; |
| 46 | } *dev_tbl; |
| 47 | |
| 48 | static struct idev_node *ihash_node(struct ihash_table *tbl, int64 key); |
| 49 | |
| 50 | /* Starting with protocol 30, we use a simple hashtable on the sending side |
| 51 | * for hashing the st_dev and st_ino info. The receiving side gets told |
| 52 | * (via flags and a "group index") which items are hard-linked together, so |
| 53 | * we can avoid the pool of dev+inode data. */ |
| 54 | |
| 55 | static struct ihash_table *ihash_create(int size) |
| 56 | { |
| 57 | struct ihash_table *tbl; |
| 58 | |
| 59 | /* Pick a power of 2 that can hold the requested size. */ |
| 60 | if (size & (size-1) || size < 16) { |
| 61 | int req = size; |
| 62 | size = 16; |
| 63 | while (size < req) |
| 64 | size *= 2; |
| 65 | } |
| 66 | |
| 67 | if (!(tbl = new(struct ihash_table)) |
| 68 | || !(tbl->buckets = new_array(struct idev_node, size))) |
| 69 | out_of_memory("ihash_create"); |
| 70 | memset(tbl->buckets, 0, size * sizeof tbl->buckets[0]); |
| 71 | tbl->size = size; |
| 72 | tbl->entries = 0; |
| 73 | |
| 74 | return tbl; |
| 75 | } |
| 76 | |
| 77 | static void ihash_destroy(struct ihash_table *tbl) |
| 78 | { |
| 79 | free(tbl->buckets); |
| 80 | free(tbl); |
| 81 | } |
| 82 | |
| 83 | void init_hard_links(void) |
| 84 | { |
| 85 | dev_tbl = ihash_create(16); |
| 86 | } |
| 87 | |
| 88 | static void expand_ihash(struct ihash_table *tbl) |
| 89 | { |
| 90 | struct idev_node *old_buckets = tbl->buckets; |
| 91 | int size = tbl->size * 2; |
| 92 | int i; |
| 93 | |
| 94 | if (!(tbl->buckets = new_array(struct idev_node, size))) |
| 95 | out_of_memory("ihash_create"); |
| 96 | memset(tbl->buckets, 0, size * sizeof (struct idev_node)); |
| 97 | tbl->size = size; |
| 98 | tbl->entries = 0; |
| 99 | |
| 100 | for (i = size / 2; i-- > 0; ) { |
| 101 | int64 key = old_buckets[i].key; |
| 102 | if (key == 0) |
| 103 | continue; |
| 104 | ihash_node(tbl, key)->data = old_buckets[i].data; |
| 105 | } |
| 106 | |
| 107 | free(old_buckets); |
| 108 | } |
| 109 | |
| 110 | /* This returns the node for the indicated key, either newly created, |
| 111 | * or already existing. */ |
| 112 | static struct idev_node *ihash_node(struct ihash_table *tbl, int64 key) |
| 113 | { |
| 114 | uint32 bkt; |
| 115 | |
| 116 | if (tbl->entries > HASH_LOAD_LIMIT(tbl->size)) |
| 117 | expand_ihash(tbl); |
| 118 | |
| 119 | #if SIZEOF_INT64 < 8 |
| 120 | /* Based on Jenkins One-at-a-time hash. */ |
| 121 | { |
| 122 | uchar *keyp = (uchar*)&key; |
| 123 | int i; |
| 124 | |
| 125 | for (bkt = 0, i = 0; i < SIZEOF_INT64; i++) { |
| 126 | bkt += keyp[i]; |
| 127 | bkt += (bkt << 10); |
| 128 | bkt ^= (bkt >> 6); |
| 129 | } |
| 130 | bkt += (bkt << 3); |
| 131 | bkt ^= (bkt >> 11); |
| 132 | bkt += (bkt << 15); |
| 133 | } |
| 134 | #else |
| 135 | #define rot(x,k) (((x)<<(k)) ^ ((x)>>(32-(k)))) |
| 136 | /* Based on Jenkins hashword() from lookup3.c. */ |
| 137 | { |
| 138 | uint32 a, b, c; |
| 139 | |
| 140 | /* Set up the internal state */ |
| 141 | a = b = c = 0xdeadbeef + (8 << 2); |
| 142 | |
| 143 | b += (uint32)(key >> 32); |
| 144 | a += (uint32)key; |
| 145 | c ^= b; c -= rot(b, 14); |
| 146 | a ^= c; a -= rot(c, 11); |
| 147 | b ^= a; b -= rot(a, 25); |
| 148 | c ^= b; c -= rot(b, 16); |
| 149 | a ^= c; a -= rot(c, 4); |
| 150 | b ^= a; b -= rot(a, 14); |
| 151 | c ^= b; c -= rot(b, 24); |
| 152 | bkt = c; |
| 153 | } |
| 154 | #endif |
| 155 | |
| 156 | /* If it already exists, return it. */ |
| 157 | while (1) { |
| 158 | bkt &= tbl->size - 1; |
| 159 | if (tbl->buckets[bkt].key == key) |
| 160 | return &tbl->buckets[bkt]; |
| 161 | if (tbl->buckets[bkt].key == 0) |
| 162 | break; |
| 163 | bkt++; |
| 164 | } |
| 165 | |
| 166 | /* Otherwise, take over this empty spot and then return it. */ |
| 167 | tbl->buckets[bkt].key = key; |
| 168 | tbl->entries++; |
| 169 | return &tbl->buckets[bkt]; |
| 170 | } |
| 171 | |
| 172 | struct idev_node *idev_node(int64 dev, int64 ino) |
| 173 | { |
| 174 | static struct idev_node *dev_node = NULL; |
| 175 | struct ihash_table *tbl; |
| 176 | |
| 177 | if (!dev_node || dev_node->key != dev) { |
| 178 | /* We keep a separate hash table of inodes for every device. */ |
| 179 | dev_node = ihash_node(dev_tbl, dev); |
| 180 | if (!(tbl = dev_node->data)) |
| 181 | tbl = dev_node->data = ihash_create(512); |
| 182 | } else |
| 183 | tbl = dev_node->data; |
| 184 | |
| 185 | return ihash_node(tbl, ino); |
| 186 | } |
| 187 | |
| 188 | void idev_destroy(void) |
| 189 | { |
| 190 | int i; |
| 191 | |
| 192 | for (i = 0; i < dev_tbl->size; i++) { |
| 193 | if (dev_tbl->buckets[i].data) |
| 194 | ihash_destroy(dev_tbl->buckets[i].data); |
| 195 | } |
| 196 | |
| 197 | ihash_destroy(dev_tbl); |
| 198 | } |
| 199 | |
| 200 | static int hlink_compare_gnum(int *int1, int *int2) |
| 201 | { |
| 202 | struct file_struct *f1 = FPTR(*int1); |
| 203 | struct file_struct *f2 = FPTR(*int2); |
| 204 | int32 gnum1 = F_HL_GNUM(f1); |
| 205 | int32 gnum2 = F_HL_GNUM(f2); |
| 206 | |
| 207 | if (gnum1 != gnum2) |
| 208 | return gnum1 > gnum2 ? 1 : -1; |
| 209 | |
| 210 | return *int1 > *int2 ? 1 : -1; |
| 211 | } |
| 212 | |
| 213 | static void match_gnums(int32 *ndx_list, int ndx_count) |
| 214 | { |
| 215 | int32 from, prev; |
| 216 | struct file_struct *file, *file_next; |
| 217 | int32 gnum, gnum_next; |
| 218 | |
| 219 | qsort(ndx_list, ndx_count, sizeof ndx_list[0], |
| 220 | (int (*)()) hlink_compare_gnum); |
| 221 | |
| 222 | for (from = 0; from < ndx_count; from++) { |
| 223 | for (file = FPTR(ndx_list[from]), gnum = F_HL_GNUM(file), prev = -1; |
| 224 | from < ndx_count-1; |
| 225 | file = file_next, gnum = gnum_next, prev = ndx_list[from++]) |
| 226 | { |
| 227 | file_next = FPTR(ndx_list[from+1]); |
| 228 | gnum_next = F_HL_GNUM(file_next); |
| 229 | if (gnum != gnum_next) |
| 230 | break; |
| 231 | if (prev < 0) |
| 232 | file->flags |= FLAG_HLINK_FIRST; |
| 233 | F_HL_PREV(file) = prev; |
| 234 | } |
| 235 | if (prev < 0) |
| 236 | file->flags &= ~FLAG_HLINKED; |
| 237 | else { |
| 238 | file->flags |= FLAG_HLINK_LAST; |
| 239 | F_HL_PREV(file) = prev; |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | /* Analyze the hard-links in the file-list by creating a list of all the |
| 245 | * items that have hlink data, sorting them, and matching up identical |
| 246 | * values into clusters. These will be a single linked list from last |
| 247 | * to first when we're done. */ |
| 248 | void match_hard_links(void) |
| 249 | { |
| 250 | int i, ndx_count = 0; |
| 251 | int32 *ndx_list; |
| 252 | |
| 253 | if (!(ndx_list = new_array(int32, cur_flist->count))) |
| 254 | out_of_memory("match_hard_links"); |
| 255 | |
| 256 | for (i = 0; i < cur_flist->count; i++) { |
| 257 | if (F_IS_HLINKED(FPTR(i))) |
| 258 | ndx_list[ndx_count++] = i; |
| 259 | } |
| 260 | |
| 261 | if (ndx_count) |
| 262 | match_gnums(ndx_list, ndx_count); |
| 263 | |
| 264 | free(ndx_list); |
| 265 | if (protocol_version < 30) |
| 266 | idev_destroy(); |
| 267 | } |
| 268 | |
| 269 | static int maybe_hard_link(struct file_struct *file, int ndx, |
| 270 | const char *fname, int statret, STRUCT_STAT *stp, |
| 271 | const char *oldname, STRUCT_STAT *old_stp, |
| 272 | const char *realname, int itemizing, enum logcode code) |
| 273 | { |
| 274 | if (statret == 0) { |
| 275 | if (stp->st_dev == old_stp->st_dev |
| 276 | && stp->st_ino == old_stp->st_ino) { |
| 277 | if (itemizing) { |
| 278 | itemize(file, ndx, statret, stp, |
| 279 | ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS, |
| 280 | 0, ""); |
| 281 | } |
| 282 | if (verbose > 1 && maybe_ATTRS_REPORT) |
| 283 | rprintf(FCLIENT, "%s is uptodate\n", fname); |
| 284 | file->flags |= FLAG_HLINK_DONE; |
| 285 | return 0; |
| 286 | } |
| 287 | if (make_backups > 0) { |
| 288 | if (!make_backup(fname)) |
| 289 | return -1; |
| 290 | } else if (robust_unlink(fname)) { |
| 291 | rsyserr(FERROR, errno, "unlink %s failed", |
| 292 | full_fname(fname)); |
| 293 | return -1; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | if (hard_link_one(file, fname, oldname, 0)) { |
| 298 | if (itemizing) { |
| 299 | itemize(file, ndx, statret, stp, |
| 300 | ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS, 0, |
| 301 | realname); |
| 302 | } |
| 303 | if (code != FNONE && verbose) |
| 304 | rprintf(code, "%s => %s\n", fname, realname); |
| 305 | return 0; |
| 306 | } |
| 307 | return -1; |
| 308 | } |
| 309 | |
| 310 | /* Only called if FLAG_HLINKED is set and FLAG_HLINK_FIRST is not. Returns: |
| 311 | * 0 = process the file, 1 = skip the file, -1 = error occurred. */ |
| 312 | int hard_link_check(struct file_struct *file, int ndx, const char *fname, |
| 313 | int statret, STRUCT_STAT *stp, int itemizing, |
| 314 | enum logcode code) |
| 315 | { |
| 316 | STRUCT_STAT prev_st; |
| 317 | char prev_name[MAXPATHLEN], altbuf[MAXPATHLEN], *realname; |
| 318 | int alt_dest, prev_ndx = F_HL_PREV(file); |
| 319 | struct file_struct *prev_file = FPTR(prev_ndx); |
| 320 | |
| 321 | /* Is the previous link is not complete yet? */ |
| 322 | if (!(prev_file->flags & FLAG_HLINK_DONE)) { |
| 323 | /* Is the previous link being transferred? */ |
| 324 | if (prev_file->flags & FLAG_FILE_SENT) { |
| 325 | /* Add ourselves to the list of files that will be |
| 326 | * updated when the transfer completes, and mark |
| 327 | * ourself as waiting for the transfer. */ |
| 328 | F_HL_PREV(file) = F_HL_PREV(prev_file); |
| 329 | F_HL_PREV(prev_file) = ndx; |
| 330 | file->flags |= FLAG_FILE_SENT; |
| 331 | return 1; |
| 332 | } |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | /* There is a finished file to link with! */ |
| 337 | if (!(prev_file->flags & FLAG_HLINK_FIRST)) { |
| 338 | /* The previous previous will be marked with FIRST. */ |
| 339 | prev_ndx = F_HL_PREV(prev_file); |
| 340 | prev_file = FPTR(prev_ndx); |
| 341 | /* Update our previous pointer to point to the first. */ |
| 342 | F_HL_PREV(file) = prev_ndx; |
| 343 | } |
| 344 | alt_dest = F_HL_PREV(prev_file); /* alternate value when DONE && FIRST */ |
| 345 | if (alt_dest >= 0 && dry_run) { |
| 346 | pathjoin(prev_name, MAXPATHLEN, basis_dir[alt_dest], |
| 347 | f_name(prev_file, NULL)); |
| 348 | f_name(prev_file, altbuf); |
| 349 | realname = altbuf; |
| 350 | } else { |
| 351 | f_name(prev_file, prev_name); |
| 352 | realname = prev_name; |
| 353 | } |
| 354 | |
| 355 | if (link_stat(prev_name, &prev_st, 0) < 0) { |
| 356 | rsyserr(FERROR, errno, "stat %s failed", |
| 357 | full_fname(prev_name)); |
| 358 | return -1; |
| 359 | } |
| 360 | |
| 361 | if (statret < 0 && basis_dir[0] != NULL) { |
| 362 | /* If we match an alt-dest item, we don't output this as a change. */ |
| 363 | char cmpbuf[MAXPATHLEN]; |
| 364 | STRUCT_STAT alt_st; |
| 365 | int j = 0; |
| 366 | do { |
| 367 | pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname); |
| 368 | if (link_stat(cmpbuf, &alt_st, 0) < 0) |
| 369 | continue; |
| 370 | if (link_dest) { |
| 371 | if (prev_st.st_dev != alt_st.st_dev |
| 372 | || prev_st.st_ino != alt_st.st_ino) |
| 373 | continue; |
| 374 | statret = 1; |
| 375 | *stp = alt_st; |
| 376 | if (verbose < 2 || !stdout_format_has_i) { |
| 377 | itemizing = 0; |
| 378 | code = FNONE; |
| 379 | if (verbose > 1 && maybe_ATTRS_REPORT) |
| 380 | rprintf(FCLIENT, "%s is uptodate\n", fname); |
| 381 | } |
| 382 | break; |
| 383 | } |
| 384 | if (!unchanged_file(cmpbuf, file, &alt_st)) |
| 385 | continue; |
| 386 | statret = 1; |
| 387 | *stp = alt_st; |
| 388 | if (unchanged_attrs(file, &alt_st)) |
| 389 | break; |
| 390 | } while (basis_dir[++j] != NULL); |
| 391 | } |
| 392 | |
| 393 | if (maybe_hard_link(file, ndx, fname, statret, stp, prev_name, &prev_st, |
| 394 | realname, itemizing, code) < 0) |
| 395 | return -1; |
| 396 | |
| 397 | if (remove_source_files == 1 && do_xfers) |
| 398 | send_msg_int(MSG_SUCCESS, ndx); |
| 399 | |
| 400 | return 1; |
| 401 | } |
| 402 | |
| 403 | int hard_link_one(struct file_struct *file, const char *fname, |
| 404 | const char *oldname, int terse) |
| 405 | { |
| 406 | if (do_link(oldname, fname) < 0) { |
| 407 | enum logcode code; |
| 408 | if (terse) { |
| 409 | if (!verbose) |
| 410 | return -1; |
| 411 | code = FINFO; |
| 412 | } else |
| 413 | code = FERROR; |
| 414 | rsyserr(code, errno, "link %s => %s failed", |
| 415 | full_fname(fname), oldname); |
| 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | file->flags |= FLAG_HLINK_DONE; |
| 420 | |
| 421 | return 1; |
| 422 | } |
| 423 | |
| 424 | void finish_hard_link(struct file_struct *file, const char *fname, |
| 425 | STRUCT_STAT *stp, int itemizing, enum logcode code, |
| 426 | int alt_dest) |
| 427 | { |
| 428 | STRUCT_STAT st, prev_st; |
| 429 | char alt_name[MAXPATHLEN], *prev_name; |
| 430 | const char *our_name; |
| 431 | int prev_statret, ndx, prev_ndx = F_HL_PREV(file); |
| 432 | |
| 433 | if (stp == NULL && prev_ndx >= 0) { |
| 434 | if (link_stat(fname, &st, 0) < 0) { |
| 435 | rsyserr(FERROR, errno, "stat %s failed", |
| 436 | full_fname(fname)); |
| 437 | return; |
| 438 | } |
| 439 | stp = &st; |
| 440 | } |
| 441 | |
| 442 | /* FIRST combined with DONE means we were the first to get done. */ |
| 443 | file->flags |= FLAG_HLINK_FIRST | FLAG_HLINK_DONE; |
| 444 | F_HL_PREV(file) = alt_dest; |
| 445 | if (alt_dest >= 0 && dry_run) { |
| 446 | pathjoin(alt_name, MAXPATHLEN, basis_dir[alt_dest], |
| 447 | f_name(file, NULL)); |
| 448 | our_name = alt_name; |
| 449 | } else |
| 450 | our_name = fname; |
| 451 | |
| 452 | while ((ndx = prev_ndx) >= 0) { |
| 453 | file = FPTR(ndx); |
| 454 | file->flags = (file->flags & ~FLAG_HLINK_FIRST) | FLAG_HLINK_DONE; |
| 455 | prev_ndx = F_HL_PREV(file); |
| 456 | prev_name = f_name(file, NULL); |
| 457 | prev_statret = link_stat(prev_name, &prev_st, 0); |
| 458 | if (maybe_hard_link(file, ndx, prev_name, prev_statret, &prev_st, |
| 459 | our_name, stp, fname, itemizing, code) < 0) |
| 460 | continue; |
| 461 | if (remove_source_files == 1 && do_xfers) |
| 462 | send_msg_int(MSG_SUCCESS, ndx); |
| 463 | } |
| 464 | } |
| 465 | #endif |