4 * Code to deal with hard links in WIMs. Essentially, the WIM dentries are put
5 * into a hash table indexed by the hard link group ID field, then for each hard
6 * link group, a linked list is made to connect the dentries.
10 * Copyright (C) 2012 Eric Biggers
12 * This file is part of wimlib, a library for working with WIM files.
14 * wimlib is free software; you can redistribute it and/or modify it under the
15 * terms of the GNU Lesser General Public License as published by the Free
16 * Software Foundation; either version 2.1 of the License, or (at your option)
19 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
20 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
21 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
24 * You should have received a copy of the GNU Lesser General Public License
25 * along with wimlib; if not, see http://www.gnu.org/licenses/.
28 #include "wimlib_internal.h"
31 #include "lookup_table.h"
33 /* Hard link group; it's identified by its hard link group ID and consists of a
34 * circularly linked list of dentries. */
37 struct link_group *next;
38 struct list_head *dentry_list;
41 /* Hash table to find hard link groups, identified by their hard link group ID.
43 struct link_group_table {
44 /* Fields for the hash table */
45 struct link_group **array;
50 * Linked list of "extra" groups. These may be:
52 * - Hard link groups of size 1, which are all allowed to have 0 for
53 * their hard link group ID, meaning we cannot insert them into the
55 * - Groups we create ourselves by splitting a nominal hard link group
56 * due to inconsistencies in the dentries. These groups will have a
57 * hard link group ID duplicated with some other group until
58 * assign_link_group_ids() is called.
60 struct link_group *extra_groups;
63 /* Returns pointer to a new link group table having the specified capacity */
64 struct link_group_table *new_link_group_table(size_t capacity)
66 struct link_group_table *table;
67 struct link_group **array;
69 table = MALLOC(sizeof(struct link_group_table));
72 array = CALLOC(capacity, sizeof(array[0]));
77 table->num_entries = 0;
78 table->capacity = capacity;
80 table->extra_groups = NULL;
83 ERROR("Failed to allocate memory for link group table with capacity %zu",
89 * Insert a dentry into the hard link group table based on its hard link group
92 * If there is already a dentry in the table having the same hard link group ID,
93 * we link the dentries together in a circular list.
95 * If the hard link group ID is 0, this indicates a dentry that's in a hard link
96 * group by itself (has a link count of 1). We can't insert it into the hash
97 * table itself because we don't know what hard link group IDs are available to
98 * give it (this could be kept track of but would be more difficult). Instead
99 * we keep a linked list of the single dentries, and assign them hard link group
102 int link_group_table_insert(struct dentry *dentry, void *__table)
104 struct link_group_table *table = __table;
106 struct link_group *group;
108 if (dentry->hard_link == 0) {
109 /* Single group--- Add to the list of extra groups (we can't put
110 * it in the table itself because all the singles have a link
112 group = MALLOC(sizeof(struct link_group));
114 return WIMLIB_ERR_NOMEM;
115 group->link_group_id = 0;
116 group->next = table->extra_groups;
117 table->extra_groups = group;
118 INIT_LIST_HEAD(&dentry->link_group_list);
119 group->dentry_list = &dentry->link_group_list;
121 /* Hard link group that may should multiple dentries (the code
122 * will work even if the group actually contains only 1 dentry
125 /* Try adding to existing hard link group */
126 pos = dentry->hard_link % table->capacity;
127 group = table->array[pos];
129 if (group->link_group_id == dentry->hard_link) {
130 list_add(&dentry->link_group_list,
137 /* Add new hard link group to the table */
139 group = MALLOC(sizeof(struct link_group));
141 return WIMLIB_ERR_NOMEM;
142 group->link_group_id = dentry->hard_link;
143 group->next = table->array[pos];
144 INIT_LIST_HEAD(&dentry->link_group_list);
145 group->dentry_list = &dentry->link_group_list;
146 table->array[pos] = group;
148 /* XXX Make the table grow when too many entries have been
150 table->num_entries++;
155 static void free_link_group_list(struct link_group *group)
157 struct link_group *next_group;
159 next_group = group->next;
165 /* Frees a link group table. */
166 void free_link_group_table(struct link_group_table *table)
168 struct link_group *single, *next;
173 for (size_t i = 0; i < table->capacity; i++)
174 free_link_group_list(table->array[i]);
175 free_link_group_list(table->extra_groups);
180 u64 assign_link_group_ids_to_list(struct link_group *group, u64 id)
182 struct dentry *dentry;
183 struct list_head *cur;
185 cur = group->dentry_list;
187 dentry = container_of(cur,
190 dentry->hard_link = id;
192 } while (cur != group->dentry_list);
193 group->link_group_id = id;
200 /* Insert the link groups in the `extra_groups' list into the hash table */
201 static void insert_extra_groups(struct link_group_table *table)
203 struct link_group *group, *next_group;
206 group = table->extra_groups;
208 next_group = group->next;
209 pos = group->link_group_id % table->capacity;
210 group->next = table->array[pos];
211 table->array[pos] = group;
214 table->extra_groups = NULL;
217 /* Assign the link group IDs to dentries in a link group table, and return the
218 * next available link group ID. */
219 u64 assign_link_group_ids(struct link_group_table *table)
221 DEBUG("Assigning link groups");
223 /* Assign consecutive link group IDs to each link group in the hash
226 for (size_t i = 0; i < table->capacity; i++)
227 id = assign_link_group_ids_to_list(table->array[i], id);
229 /* Assign link group IDs to the "extra" link groups and insert them into
231 id = assign_link_group_ids_to_list(table->extra_groups, id);
232 insert_extra_groups(table);
238 static void inconsistent_link_group(const struct dentry *first_dentry)
240 const struct dentry *dentry = first_dentry;
242 ERROR("An inconsistent hard link group that we cannot correct has been "
244 ERROR("The dentries are located at the following paths:");
246 ERROR("`%s'", dentry->full_path_utf8);
247 } while ((dentry = container_of(dentry->link_group_list.next,
249 link_group_list)) != first_dentry);
252 static bool ref_dentries_consistent(const struct dentry * restrict ref_dentry_1,
253 const struct dentry * restrict ref_dentry_2)
255 wimlib_assert(ref_dentry_1 != ref_dentry_2);
257 if (ref_dentry_1->num_ads != ref_dentry_2->num_ads)
259 if (ref_dentry_1->security_id != ref_dentry_2->security_id
260 || ref_dentry_1->attributes != ref_dentry_2->attributes)
262 for (unsigned i = 0; i <= ref_dentry_1->num_ads; i++) {
263 const u8 *ref_1_hash, *ref_2_hash;
264 ref_1_hash = dentry_stream_hash_unresolved(ref_dentry_1, i);
265 ref_2_hash = dentry_stream_hash_unresolved(ref_dentry_2, i);
266 if (!hashes_equal(ref_1_hash, ref_2_hash))
268 if (i && !ads_entries_have_same_name(&ref_dentry_1->ads_entries[i - 1],
269 &ref_dentry_2->ads_entries[i - 1]))
276 static bool dentries_consistent(const struct dentry * restrict ref_dentry,
277 const struct dentry * restrict dentry)
279 wimlib_assert(ref_dentry != dentry);
281 if (ref_dentry->num_ads != dentry->num_ads && dentry->num_ads != 0)
283 if (ref_dentry->security_id != dentry->security_id
284 || ref_dentry->attributes != dentry->attributes)
286 for (unsigned i = 0; i <= min(ref_dentry->num_ads, dentry->num_ads); i++) {
287 const u8 *ref_hash, *hash;
288 ref_hash = dentry_stream_hash_unresolved(ref_dentry, i);
289 hash = dentry_stream_hash_unresolved(dentry, i);
290 if (!hashes_equal(ref_hash, hash) && !is_zero_hash(hash))
292 if (i && !ads_entries_have_same_name(&ref_dentry->ads_entries[i - 1],
293 &dentry->ads_entries[i - 1]))
300 fix_true_link_group(struct dentry *first_dentry)
302 struct dentry *dentry;
303 struct dentry *ref_dentry = NULL;
308 dentry = first_dentry;
310 if (!ref_dentry || ref_dentry->num_ads == 0)
312 if (dentry->creation_time > last_ctime)
313 last_ctime = dentry->creation_time;
314 if (dentry->last_write_time > last_mtime)
315 last_mtime = dentry->last_write_time;
316 if (dentry->last_access_time > last_atime)
317 last_atime = dentry->last_access_time;
318 } while ((dentry = container_of(dentry->link_group_list.next,
320 link_group_list)) != first_dentry);
323 ref_dentry->ads_entries_status = ADS_ENTRIES_OWNER;
324 dentry = first_dentry;
326 if (dentry != ref_dentry) {
327 if (!dentries_consistent(ref_dentry, dentry)) {
328 inconsistent_link_group(first_dentry);
329 return WIMLIB_ERR_INVALID_DENTRY;
331 copy_hash(dentry->hash, ref_dentry->hash);
332 dentry_free_ads_entries(dentry);
333 dentry->num_ads = ref_dentry->num_ads;
334 dentry->ads_entries = ref_dentry->ads_entries;
335 dentry->ads_entries_status = ADS_ENTRIES_USER;
337 dentry->creation_time = last_ctime;
338 dentry->last_write_time = last_mtime;
339 dentry->last_access_time = last_atime;
340 } while ((dentry = container_of(dentry->link_group_list.next,
342 link_group_list)) != first_dentry);
347 fix_nominal_link_group(struct link_group *group,
348 struct link_group **new_groups)
350 struct dentry *tmp, *dentry, *ref_dentry;
352 size_t num_true_link_groups;
353 struct list_head *head;
356 LIST_HEAD(dentries_with_data_streams);
357 LIST_HEAD(dentries_with_no_data_streams);
358 LIST_HEAD(true_link_groups);
360 /* Create the lists @dentries_with_data_streams and
361 * @dentries_with_no_data_streams. */
362 dentry = container_of(group->dentry_list, struct dentry,
365 for (unsigned i = 0; i <= dentry->num_ads; i++) {
367 hash = dentry_stream_hash_unresolved(dentry, i);
368 if (!is_zero_hash(hash)) {
369 list_add(&dentry->tmp_list,
370 &dentries_with_data_streams);
374 list_add(&dentry->tmp_list,
375 &dentries_with_no_data_streams);
377 dentry = container_of(dentry->link_group_list.next,
380 } while (&dentry->link_group_list != group->dentry_list);
382 /* If there are no dentries with data streams, we require the nominal
383 * link group to be a true link group */
384 if (list_empty(&dentries_with_data_streams))
385 return fix_true_link_group(container_of(group->dentry_list,
389 /* One or more dentries had data streams. We check each of these
390 * dentries for consistency with the others to form a set of true link
392 num_true_link_groups = 0;
393 list_for_each_entry_safe(dentry, tmp, &dentries_with_data_streams,
396 list_del(&dentry->tmp_list);
398 /* Look for a true link group that is consistent with
399 * this dentry and add this dentry to it. Or, if none
400 * of the true link groups are consistent with this
401 * dentry, make a new one. */
402 list_for_each_entry(ref_dentry, &true_link_groups, tmp_list) {
403 if (ref_dentries_consistent(ref_dentry, dentry)) {
404 list_add(&dentry->link_group_list,
405 &ref_dentry->link_group_list);
409 num_true_link_groups++;
410 list_add(&dentry->tmp_list, &true_link_groups);
411 INIT_LIST_HEAD(&dentry->link_group_list);
416 wimlib_assert(num_true_link_groups != 0);
418 /* If there were dentries with no data streams, we require there to only
419 * be one true link group so that we know which link group to assign the
421 * streamless dentries to. */
422 if (!list_empty(&dentries_with_no_data_streams)) {
423 if (num_true_link_groups != 1) {
424 ERROR("Hard link group ambiguity detected!");
425 ERROR("We split up hard link group 0x%"PRIx64" due to "
427 ERROR("but dentries with no stream information remained. "
428 "We don't know which true hard link");
429 ERROR("group to assign them to.");
430 return WIMLIB_ERR_INVALID_DENTRY;
432 ref_dentry = container_of(true_link_groups.next,
435 list_for_each_entry(dentry, &dentries_with_no_data_streams,
438 list_add(&dentry->link_group_list,
439 &ref_dentry->link_group_list);
443 list_for_each_entry(dentry, &true_link_groups, tmp_list) {
444 ret = fix_true_link_group(dentry);
449 /* Make new `struct link_group's for the new link groups */
450 for (head = true_link_groups.next->next;
451 head != &true_link_groups;
454 dentry = container_of(head, struct dentry, tmp_list);
455 group = MALLOC(sizeof(*group));
457 ERROR("Out of memory");
458 return WIMLIB_ERR_NOMEM;
460 group->link_group_id = link_group_id;
461 group->dentry_list = &dentry->link_group_list;
462 group->next = *new_groups;
464 } while ((head = head->next) != &true_link_groups);
469 * Goes through each link group and shares the ads_entries (Alternate Data
470 * Stream entries) field of each dentry between members of a hard link group.
472 * In the process, the dentries in each link group are checked for consistency.
473 * If they contain data features that indicate they cannot really be in the same
474 * hard link group, this should be an error, but in reality this case needs to
475 * be handled, so we split the dentries into different hard link groups.
477 * One of the dentries in the group is arbitrarily assigned the role of "owner"
478 * (ADS_ENTRIES_OWNER), while the others are "users" (ADS_ENTRIES_USER).
480 int fix_link_groups(struct link_group_table *table)
482 for (u64 i = 0; i < table->capacity; i++) {
483 struct link_group *group = table->array[i];
486 ret = fix_nominal_link_group(group, &table->extra_groups);
496 static bool dentries_have_same_ads(const struct dentry *d1,
497 const struct dentry *d2)
499 wimlib_assert(d1->num_ads == d2->num_ads);
500 /* Verify stream names and hashes are the same */
501 for (u16 i = 0; i < d1->num_ads; i++) {
502 if (strcmp(d1->ads_entries[i].stream_name_utf8,
503 d2->ads_entries[i].stream_name_utf8) != 0)
505 if (!hashes_equal(d1->ads_entries[i].hash,
506 d2->ads_entries[i].hash))
513 * Share the alternate stream entries between hard-linked dentries.
516 * - If you use 'imagex.exe' (version 6.1.7600.16385) to create a WIM containing
517 * hard-linked files, only one dentry in the hard link set will refer to data
518 * streams, including all alternate data streams. The rest of the dentries in
519 * the hard link set will be marked as having 0 alternate data streams and
520 * will not refer to any main file stream (the SHA1 message digest will be all
523 * - However, if you look at the WIM's that Microsoft actually distributes (e.g.
524 * Windows 7/8 boot.wim, install.wim), it's not the same as above. The
525 * dentries in hard link sets will have stream information duplicated. I
526 * can't say anything about the alternate data streams because these WIMs do
527 * not contain alternate data streams.
529 * - Windows 7 'install.wim' contains hard link sets containing dentries with
530 * inconsistent streams and other inconsistent information such as security
531 * ID. The only way I can think to handle these is to treat the hard link
532 * grouping as erroneous and split up the hard link group.
534 static int share_dentry_ads(struct dentry *owner, struct dentry *user)
536 const char *mismatch_type;
537 bool data_streams_shared = true;
538 wimlib_assert(owner->num_ads == 0 ||
539 owner->ads_entries != user->ads_entries);
540 if (owner->attributes != user->attributes) {
541 mismatch_type = "attributes";
544 if (owner->attributes & FILE_ATTRIBUTE_DIRECTORY) {
545 WARNING("`%s' is hard-linked to `%s', which is a directory ",
546 user->full_path_utf8, owner->full_path_utf8);
547 return WIMLIB_ERR_INVALID_DENTRY;
549 if (owner->security_id != user->security_id) {
550 mismatch_type = "security ID";
553 if (!hashes_equal(owner->hash, user->hash)) {
554 if (is_zero_hash(user->hash)) {
555 data_streams_shared = false;
556 copy_hash(user->hash, owner->hash);
558 mismatch_type = "main file resource";
562 if (data_streams_shared) {
563 if (!dentries_have_same_ads(owner, user)) {
564 mismatch_type = "Alternate Stream Entries";
568 if (owner->last_access_time != user->last_access_time
569 || owner->last_write_time != user->last_write_time
570 || owner->creation_time != user->creation_time) {
572 dentry_free_ads_entries(user);
573 user->ads_entries = owner->ads_entries;
574 user->ads_entries_status = ADS_ENTRIES_USER;
577 WARNING("Dentries `%s' and `%s' are supposedly in the same hard-link "
578 "group but do not share the same %s",
579 owner->full_path_utf8, user->full_path_utf8,
581 return WIMLIB_ERR_INVALID_DENTRY;