1 #include "wimlib_internal.h"
4 #include "lookup_table.h"
6 /* Hard link group; it's identified by its hard link group ID and consists of a
7 * circularly linked list of dentries. */
10 struct link_group *next;
11 struct list_head *dentry_list;
14 /* Hash table to find hard link groups, identified by their hard link group ID.
16 struct link_group_table {
17 struct link_group **array;
20 struct link_group *singles;
23 /* Returns pointer to a new link group table having the specified capacity */
24 struct link_group_table *new_link_group_table(u64 capacity)
26 struct link_group_table *table;
27 struct link_group **array;
29 table = MALLOC(sizeof(struct link_group_table));
32 array = CALLOC(capacity, sizeof(array[0]));
37 table->num_entries = 0;
38 table->capacity = capacity;
40 table->singles = NULL;
43 ERROR("Failed to allocate memory for link group table with capacity %zu",
49 * Insert a dentry into the hard link group table based on its hard link group
52 * If there is already a dentry in the table having the same hard link group ID,
53 * we link the dentries together in a circular list.
55 * If the hard link group ID is 0, this indicates a dentry that's in a hard link
56 * group by itself (has a link count of 1). We can't insert it into the hash
57 * table itself because we don't know what hard link group IDs are available to
58 * give it (this could be kept track of but would be more difficult). Instead
59 * we keep a linked list of the single dentries, and assign them hard link group
62 int link_group_table_insert(struct dentry *dentry, struct link_group_table *table)
65 struct link_group *group;
67 if (dentry->hard_link == 0) {
68 /* Single group--- Add to the singles list (we can't put it in
69 * the table itself because all the singles have a link group ID
71 group = MALLOC(sizeof(struct link_group));
73 return WIMLIB_ERR_NOMEM;
74 group->link_group_id = 0;
75 group->next = table->singles;
76 table->singles = group;
77 INIT_LIST_HEAD(&dentry->link_group_list);
78 group->dentry_list = &dentry->link_group_list;
80 /* Hard link group that may should multiple dentries (the code
81 * will work even if the group actually contains only 1 dentry
84 /* Try adding to existing hard link group */
85 pos = dentry->hard_link % table->capacity;
86 group = table->array[pos];
88 if (group->link_group_id == dentry->hard_link) {
89 list_add(&dentry->link_group_list,
96 /* Add new hard link group to the table */
98 group = MALLOC(sizeof(struct link_group));
100 return WIMLIB_ERR_NOMEM;
101 group->link_group_id = dentry->hard_link;
102 group->next = table->array[pos];
103 INIT_LIST_HEAD(&dentry->link_group_list);
104 group->dentry_list = &dentry->link_group_list;
105 table->array[pos] = group;
107 /* XXX Make the table grow when too many entries have been
109 table->num_entries++;
114 /* Frees a link group table. */
115 void free_link_group_table(struct link_group_table *table)
117 struct link_group *single, *next;
122 for (u64 i = 0; i < table->capacity; i++) {
123 struct link_group *group = table->array[i];
124 struct link_group *next;
133 single = table->singles;
143 /* Assign the link group IDs to dentries in a link group table, and return the
144 * next available link group ID. */
145 u64 assign_link_groups(struct link_group_table *table)
147 DEBUG("Assigning link groups");
149 /* Assign consecutive link group IDs to each link group in the hash
152 for (u64 i = 0; i < table->capacity; i++) {
153 struct link_group *group = table->array[i];
154 struct link_group *next_group;
155 struct dentry *dentry;
156 struct list_head *cur;
158 cur = group->dentry_list;
160 dentry = container_of(cur,
163 dentry->hard_link = id;
165 } while (cur != group->dentry_list);
170 /* Assign link group IDs to the link groups that previously had link
171 * group IDs of 0, and insert them into the hash table */
172 struct link_group *single = table->singles;
174 struct dentry *dentry;
175 struct link_group *next_single;
178 next_single = single->next;
180 dentry = container_of(single->dentry_list, struct dentry,
182 dentry->hard_link = id;
184 pos = id % table->capacity;
185 single->next = table->array[pos];
186 table->array[pos] = single;
188 single = next_single;
191 table->singles = NULL;
195 static bool dentries_have_same_ads(const struct dentry *d1,
196 const struct dentry *d2)
198 /* Verify stream names and hashes are the same */
199 for (u16 i = 0; i < d1->num_ads; i++) {
200 if (strcmp(d1->ads_entries[i].stream_name_utf8,
201 d2->ads_entries[i].stream_name_utf8) != 0)
203 if (memcmp(d1->ads_entries[i].hash,
204 d2->ads_entries[i].hash,
211 /* Share the alternate stream entries between hard-linked dentries. */
212 static int share_dentry_ads(struct dentry *owner, struct dentry *user)
214 const char *mismatch_type;
215 wimlib_assert(owner->num_ads == 0 ||
216 owner->ads_entries != user->ads_entries);
217 if (owner->attributes != user->attributes) {
218 mismatch_type = "attributes";
221 if (owner->attributes & FILE_ATTRIBUTE_DIRECTORY) {
222 WARNING("`%s' is hard-linked to `%s', which is a directory ",
223 user->full_path_utf8, owner->full_path_utf8);
224 return WIMLIB_ERR_INVALID_DENTRY;
226 if (owner->security_id != user->security_id) {
227 mismatch_type = "security ID";
230 if (memcmp(owner->hash, user->hash, WIM_HASH_SIZE) != 0) {
231 mismatch_type = "main file resource";
234 if (!dentries_have_same_ads(owner, user)) {
235 mismatch_type = "Alternate Stream Entries";
238 dentry_free_ads_entries(user);
239 user->ads_entries = owner->ads_entries;
240 user->ads_entries_status = ADS_ENTRIES_USER;
243 WARNING("Dentries `%s' and `%s' in the same hard-link group but "
244 "do not share the same %s",
245 owner->full_path_utf8, user->full_path_utf8,
247 return WIMLIB_ERR_INVALID_DENTRY;
250 static int link_group_free_duplicate_data(struct link_group *group,
251 struct link_group **bad_links)
253 struct dentry *owner, *user, *tmp;
255 owner = container_of(group->dentry_list, struct dentry,
257 owner->ads_entries_status = ADS_ENTRIES_OWNER;
259 list_for_each_entry_safe(user, tmp, group->dentry_list,
262 /* I would like it to be an error if two dentries are in the
263 * same hard link group but have irreconcilable differences such
264 * as different file permissions, but unfortunately some of M$'s
265 * WIMs contain many instances of this error. This problem is
266 * worked around here by splitting each offending dentry off
267 * into its own hard link group. */
268 if (share_dentry_ads(owner, user) != 0) {
269 struct link_group *single;
270 single = MALLOC(sizeof(struct link_group));
272 return WIMLIB_ERR_NOMEM;
273 list_del(&user->link_group_list);
274 INIT_LIST_HEAD(&user->link_group_list);
275 single->link_group_id = 0;
276 single->next = *bad_links;
277 single->dentry_list = &user->link_group_list;
279 user->ads_entries_status = ADS_ENTRIES_OWNER;
286 * Goes through each link group and shares the ads_entries (Alternate Data
287 * Stream entries) field of each dentry between members of a hard link group.
289 * In the process, the dentries in each link group are checked for consistency.
290 * If they contain data features that indicate they cannot really be in the same
291 * hard link group, this should be an error, but in reality this case needs to
292 * be handled, so we split the dentries into different hard link groups.
294 * One of the dentries in the group is arbitrarily assigned the role of "owner"
295 * (ADS_ENTRIES_OWNER), while the others are "users" (ADS_ENTRIES_USER).
297 int link_groups_free_duplicate_data(struct link_group_table *table)
299 for (u64 i = 0; i < table->capacity; i++) {
300 struct link_group *group = table->array[i];
303 ret = link_group_free_duplicate_data(group,