| 1 | /* |
| 2 | Copyright (C) Andrew Tridgell 1996 |
| 3 | Copyright (C) Paul Mackerras 1996 |
| 4 | Copyright (C) 2002 by Martin Pool <mbp@samba.org> |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program; if not, write to the Free Software |
| 18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | */ |
| 20 | |
| 21 | #include "rsync.h" |
| 22 | |
| 23 | extern int dry_run; |
| 24 | extern int verbose; |
| 25 | |
| 26 | #if SUPPORT_HARD_LINKS |
| 27 | static int hlink_compare(struct file_struct **file1, struct file_struct **file2) |
| 28 | { |
| 29 | struct file_struct *f1 = *file1; |
| 30 | struct file_struct *f2 = *file2; |
| 31 | |
| 32 | if (f1->F_DEV != f2->F_DEV) |
| 33 | return (int) (f1->F_DEV > f2->F_DEV ? 1 : -1); |
| 34 | |
| 35 | if (f1->F_INODE != f2->F_INODE) |
| 36 | return (int) (f1->F_INODE > f2->F_INODE ? 1 : -1); |
| 37 | |
| 38 | return file_compare(file1, file2); |
| 39 | } |
| 40 | |
| 41 | static struct file_struct **hlink_list; |
| 42 | static int hlink_count; |
| 43 | #endif |
| 44 | |
| 45 | void init_hard_links(struct file_list *flist) |
| 46 | { |
| 47 | #if SUPPORT_HARD_LINKS |
| 48 | int i; |
| 49 | |
| 50 | if (flist->count < 2) |
| 51 | return; |
| 52 | |
| 53 | if (hlink_list) |
| 54 | free(hlink_list); |
| 55 | |
| 56 | if (!(hlink_list = new_array(struct file_struct *, flist->count))) |
| 57 | out_of_memory("init_hard_links"); |
| 58 | |
| 59 | /* we'll want to restore the memcpy when we purge the |
| 60 | * hlink list after the sort. |
| 61 | * memcpy(hlink_list, flist->files, sizeof(hlink_list[0]) * flist->count); |
| 62 | */ |
| 63 | hlink_count = 0; |
| 64 | for (i = 0; i < flist->count; i++) { |
| 65 | if (flist->files[i]->link_u.idev) |
| 66 | hlink_list[hlink_count++] = flist->files[i]; |
| 67 | } |
| 68 | |
| 69 | qsort(hlink_list, hlink_count, |
| 70 | sizeof(hlink_list[0]), (int (*)()) hlink_compare); |
| 71 | |
| 72 | if (!hlink_count) { |
| 73 | free(hlink_list); |
| 74 | hlink_list = NULL; |
| 75 | } else if (!(hlink_list = realloc_array(hlink_list, |
| 76 | struct file_struct *, hlink_count))) |
| 77 | out_of_memory("init_hard_links"); |
| 78 | #endif |
| 79 | } |
| 80 | |
| 81 | /* check if a file should be skipped because it is the same as an |
| 82 | earlier hard link */ |
| 83 | int check_hard_link(struct file_struct *file) |
| 84 | { |
| 85 | #if SUPPORT_HARD_LINKS |
| 86 | int low = 0, high = hlink_count - 1; |
| 87 | int ret = 0; |
| 88 | |
| 89 | if (!hlink_list || !file->link_u.idev) |
| 90 | return 0; |
| 91 | |
| 92 | while (low != high) { |
| 93 | int mid = (low + high) / 2; |
| 94 | ret = hlink_compare(&hlink_list[mid], &file); |
| 95 | if (ret == 0) { |
| 96 | low = mid; |
| 97 | break; |
| 98 | } |
| 99 | if (ret > 0) |
| 100 | high = mid; |
| 101 | else |
| 102 | low = mid + 1; |
| 103 | } |
| 104 | |
| 105 | /* Check if we ended up finding the file struct or not. */ |
| 106 | if (hlink_compare(&hlink_list[low], &file) != 0) |
| 107 | return 0; |
| 108 | |
| 109 | /* Now check if the previous item shares the current one's device |
| 110 | * and inode. If so, we're not the "master", so return 1. */ |
| 111 | if (low > 0 && |
| 112 | file->F_DEV == hlink_list[low - 1]->F_DEV && |
| 113 | file->F_INODE == hlink_list[low - 1]->F_INODE) { |
| 114 | if (verbose >= 2) { |
| 115 | rprintf(FINFO, "check_hard_link: \"%s\" is a hard link to file %d, \"%s\"\n", |
| 116 | f_name(file), low-1, f_name(hlink_list[low-1])); |
| 117 | } |
| 118 | return 1; |
| 119 | } |
| 120 | #endif |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | |
| 126 | #if SUPPORT_HARD_LINKS |
| 127 | static void hard_link_one(int i) |
| 128 | { |
| 129 | STRUCT_STAT st1, st2; |
| 130 | char *hlink2, *hlink1 = f_name(hlink_list[i - 1]); |
| 131 | |
| 132 | if (link_stat(hlink1, &st1) != 0) |
| 133 | return; |
| 134 | |
| 135 | hlink2 = f_name(hlink_list[i]); |
| 136 | if (link_stat(hlink2, &st2) != 0) { |
| 137 | if (do_link(hlink1, hlink2)) { |
| 138 | if (verbose > 0) { |
| 139 | rprintf(FINFO, "link %s => %s : %s\n", |
| 140 | hlink2, hlink1, strerror(errno)); |
| 141 | } |
| 142 | return; |
| 143 | } |
| 144 | } else { |
| 145 | if (st2.st_dev == st1.st_dev && st2.st_ino == st1.st_ino) |
| 146 | return; |
| 147 | |
| 148 | if (robust_unlink(hlink2) || do_link(hlink1, hlink2)) { |
| 149 | if (verbose > 0) { |
| 150 | rprintf(FINFO, "link %s => %s : %s\n", |
| 151 | hlink2, hlink1, strerror(errno)); |
| 152 | } |
| 153 | return; |
| 154 | } |
| 155 | } |
| 156 | if (verbose > 0) |
| 157 | rprintf(FINFO, "%s => %s\n", hlink2, hlink1); |
| 158 | } |
| 159 | #endif |
| 160 | |
| 161 | |
| 162 | |
| 163 | /** |
| 164 | * Create any hard links in the global hlink_list. They were put |
| 165 | * there by running init_hard_links on the filelist. |
| 166 | **/ |
| 167 | void do_hard_links(void) |
| 168 | { |
| 169 | #if SUPPORT_HARD_LINKS |
| 170 | int i; |
| 171 | |
| 172 | if (!hlink_list) |
| 173 | return; |
| 174 | |
| 175 | for (i = 1; i < hlink_count; i++) { |
| 176 | if (hlink_list[i]->basename && hlink_list[i - 1]->basename && |
| 177 | hlink_list[i]->F_DEV == hlink_list[i - 1]->F_DEV && |
| 178 | hlink_list[i]->F_INODE == hlink_list[i - 1]->F_INODE) { |
| 179 | hard_link_one(i); |
| 180 | } |
| 181 | } |
| 182 | #endif |
| 183 | } |