2 * Copyright (C) 2012 Eric Biggers
4 * This file is part of wimlib, a library for working with WIM files.
6 * wimlib is free software; you can redistribute it and/or modify it under the
7 * terms of the GNU Lesser General Public License as published by the Free
8 * Software Foundation; either version 2.1 of the License, or (at your option)
11 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with wimlib; if not, see http://www.gnu.org/licenses/.
20 #include "wimlib_internal.h"
23 #include "lookup_table.h"
25 /* Hard link group; it's identified by its hard link group ID and consists of a
26 * circularly linked list of dentries. */
29 struct link_group *next;
30 struct list_head *dentry_list;
33 /* Hash table to find hard link groups, identified by their hard link group ID.
35 struct link_group_table {
36 struct link_group **array;
39 struct link_group *singles;
42 /* Returns pointer to a new link group table having the specified capacity */
43 struct link_group_table *new_link_group_table(u64 capacity)
45 struct link_group_table *table;
46 struct link_group **array;
48 table = MALLOC(sizeof(struct link_group_table));
51 array = CALLOC(capacity, sizeof(array[0]));
56 table->num_entries = 0;
57 table->capacity = capacity;
59 table->singles = NULL;
62 ERROR("Failed to allocate memory for link group table with capacity %zu",
68 * Insert a dentry into the hard link group table based on its hard link group
71 * If there is already a dentry in the table having the same hard link group ID,
72 * we link the dentries together in a circular list.
74 * If the hard link group ID is 0, this indicates a dentry that's in a hard link
75 * group by itself (has a link count of 1). We can't insert it into the hash
76 * table itself because we don't know what hard link group IDs are available to
77 * give it (this could be kept track of but would be more difficult). Instead
78 * we keep a linked list of the single dentries, and assign them hard link group
81 int link_group_table_insert(struct dentry *dentry, void *__table)
83 struct link_group_table *table = __table;
85 struct link_group *group;
87 if (dentry->hard_link == 0) {
88 /* Single group--- Add to the singles list (we can't put it in
89 * the table itself because all the singles have a link group ID
91 group = MALLOC(sizeof(struct link_group));
93 return WIMLIB_ERR_NOMEM;
94 group->link_group_id = 0;
95 group->next = table->singles;
96 table->singles = group;
97 INIT_LIST_HEAD(&dentry->link_group_list);
98 group->dentry_list = &dentry->link_group_list;
100 /* Hard link group that may should multiple dentries (the code
101 * will work even if the group actually contains only 1 dentry
104 /* Try adding to existing hard link group */
105 pos = dentry->hard_link % table->capacity;
106 group = table->array[pos];
108 if (group->link_group_id == dentry->hard_link) {
109 list_add(&dentry->link_group_list,
116 /* Add new hard link group to the table */
118 group = MALLOC(sizeof(struct link_group));
120 return WIMLIB_ERR_NOMEM;
121 group->link_group_id = dentry->hard_link;
122 group->next = table->array[pos];
123 INIT_LIST_HEAD(&dentry->link_group_list);
124 group->dentry_list = &dentry->link_group_list;
125 table->array[pos] = group;
127 /* XXX Make the table grow when too many entries have been
129 table->num_entries++;
134 /* Frees a link group table. */
135 void free_link_group_table(struct link_group_table *table)
137 struct link_group *single, *next;
142 for (u64 i = 0; i < table->capacity; i++) {
143 struct link_group *group = table->array[i];
144 struct link_group *next;
153 single = table->singles;
163 /* Assign the link group IDs to dentries in a link group table, and return the
164 * next available link group ID. */
165 u64 assign_link_groups(struct link_group_table *table)
167 DEBUG("Assigning link groups");
169 /* Assign consecutive link group IDs to each link group in the hash
172 for (u64 i = 0; i < table->capacity; i++) {
173 struct link_group *group = table->array[i];
174 struct link_group *next_group;
175 struct dentry *dentry;
176 struct list_head *cur;
178 cur = group->dentry_list;
180 dentry = container_of(cur,
183 dentry->hard_link = id;
185 } while (cur != group->dentry_list);
190 /* Assign link group IDs to the link groups that previously had link
191 * group IDs of 0, and insert them into the hash table */
192 struct link_group *single = table->singles;
194 struct dentry *dentry;
195 struct link_group *next_single;
198 next_single = single->next;
200 dentry = container_of(single->dentry_list, struct dentry,
202 dentry->hard_link = id;
204 pos = id % table->capacity;
205 single->next = table->array[pos];
206 table->array[pos] = single;
208 single = next_single;
211 table->singles = NULL;
215 static bool dentries_have_same_ads(const struct dentry *d1,
216 const struct dentry *d2)
218 /* Verify stream names and hashes are the same */
219 for (u16 i = 0; i < d1->num_ads; i++) {
220 if (strcmp(d1->ads_entries[i].stream_name_utf8,
221 d2->ads_entries[i].stream_name_utf8) != 0)
223 if (!hashes_equal(d1->ads_entries[i].hash,
224 d2->ads_entries[i].hash))
230 /* Share the alternate stream entries between hard-linked dentries. */
231 static int share_dentry_ads(struct dentry *owner, struct dentry *user)
233 const char *mismatch_type;
234 wimlib_assert(owner->num_ads == 0 ||
235 owner->ads_entries != user->ads_entries);
236 if (owner->attributes != user->attributes) {
237 mismatch_type = "attributes";
240 if (owner->attributes & FILE_ATTRIBUTE_DIRECTORY) {
241 WARNING("`%s' is hard-linked to `%s', which is a directory ",
242 user->full_path_utf8, owner->full_path_utf8);
243 return WIMLIB_ERR_INVALID_DENTRY;
245 if (owner->security_id != user->security_id) {
246 mismatch_type = "security ID";
249 if (!hashes_equal(owner->hash, user->hash)) {
250 mismatch_type = "main file resource";
253 if (!dentries_have_same_ads(owner, user)) {
254 mismatch_type = "Alternate Stream Entries";
257 dentry_free_ads_entries(user);
258 user->ads_entries = owner->ads_entries;
259 user->ads_entries_status = ADS_ENTRIES_USER;
262 WARNING("Dentries `%s' and `%s' in the same hard-link group but "
263 "do not share the same %s",
264 owner->full_path_utf8, user->full_path_utf8,
266 return WIMLIB_ERR_INVALID_DENTRY;
269 static int link_group_free_duplicate_data(struct link_group *group,
270 struct link_group **bad_links)
272 struct dentry *owner, *user, *tmp;
274 owner = container_of(group->dentry_list, struct dentry,
276 owner->ads_entries_status = ADS_ENTRIES_OWNER;
278 list_for_each_entry_safe(user, tmp, group->dentry_list,
281 /* I would like it to be an error if two dentries are in the
282 * same hard link group but have irreconcilable differences such
283 * as different file permissions, but unfortunately some of M$'s
284 * WIMs contain many instances of this error. This problem is
285 * worked around here by splitting each offending dentry off
286 * into its own hard link group. */
287 if (share_dentry_ads(owner, user) != 0) {
288 struct link_group *single;
289 single = MALLOC(sizeof(struct link_group));
291 return WIMLIB_ERR_NOMEM;
292 list_del(&user->link_group_list);
293 INIT_LIST_HEAD(&user->link_group_list);
294 single->link_group_id = 0;
295 single->next = *bad_links;
296 single->dentry_list = &user->link_group_list;
298 user->ads_entries_status = ADS_ENTRIES_OWNER;
305 * Goes through each link group and shares the ads_entries (Alternate Data
306 * Stream entries) field of each dentry between members of a hard link group.
308 * In the process, the dentries in each link group are checked for consistency.
309 * If they contain data features that indicate they cannot really be in the same
310 * hard link group, this should be an error, but in reality this case needs to
311 * be handled, so we split the dentries into different hard link groups.
313 * One of the dentries in the group is arbitrarily assigned the role of "owner"
314 * (ADS_ENTRIES_OWNER), while the others are "users" (ADS_ENTRIES_USER).
316 int link_groups_free_duplicate_data(struct link_group_table *table)
318 for (u64 i = 0; i < table->capacity; i++) {
319 struct link_group *group = table->array[i];
322 ret = link_group_free_duplicate_data(group,