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