]> wimlib.net Git - wimlib/blob - src/hardlink.c
Hard link disambiguation
[wimlib] / src / hardlink.c
1 /*
2  * hardlink.c
3  *
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.
7  */
8
9 /*
10  * Copyright (C) 2012 Eric Biggers
11  *
12  * This file is part of wimlib, a library for working with WIM files.
13  *
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)
17  * any later version.
18  *
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
22  * details.
23  *
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/.
26  */
27
28 #include "wimlib_internal.h"
29 #include "dentry.h"
30 #include "list.h"
31 #include "lookup_table.h"
32
33 /* Hard link group; it's identified by its hard link group ID and consists of a
34  * circularly linked list of dentries. */
35 struct link_group {
36         u64 link_group_id;
37         struct link_group *next;
38         struct list_head *dentry_list;
39 };
40
41 /* Hash table to find hard link groups, identified by their hard link group ID.
42  * */
43 struct link_group_table {
44         /* Fields for the hash table */
45         struct link_group **array;
46         u64 num_entries;
47         u64 capacity;
48
49         /* 
50          * Linked list of "extra" groups.  These may be:
51          *
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
54          *   hash table
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.
59          */
60         struct link_group *extra_groups;
61 };
62
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)
65 {
66         struct link_group_table *table;
67         struct link_group **array;
68
69         table = MALLOC(sizeof(struct link_group_table));
70         if (!table)
71                 goto err;
72         array = CALLOC(capacity, sizeof(array[0]));
73         if (!array) {
74                 FREE(table);
75                 goto err;
76         }
77         table->num_entries  = 0;
78         table->capacity     = capacity;
79         table->array        = array;
80         table->extra_groups = NULL;
81         return table;
82 err:
83         ERROR("Failed to allocate memory for link group table with capacity %zu",
84               capacity);
85         return NULL;
86 }
87
88 /* 
89  * Insert a dentry into the hard link group table based on its hard link group
90  * ID.
91  *
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.
94  *
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
100  * IDs later.
101  */
102 int link_group_table_insert(struct dentry *dentry, void *__table)
103 {
104         struct link_group_table *table = __table;
105         size_t pos;
106         struct link_group *group;
107
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
111                  * group ID of 0) */
112                 group = MALLOC(sizeof(struct link_group));
113                 if (!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;
120         } else {
121                 /* Hard link group that may should multiple dentries (the code
122                  * will work even if the group actually contains only 1 dentry
123                  * though) */
124
125                 /* Try adding to existing hard link group */
126                 pos = dentry->hard_link % table->capacity;
127                 group = table->array[pos];
128                 while (group) {
129                         if (group->link_group_id == dentry->hard_link) {
130                                 list_add(&dentry->link_group_list,
131                                          group->dentry_list);
132                                 return 0;
133                         }
134                         group = group->next;
135                 }
136
137                 /* Add new hard link group to the table */
138
139                 group = MALLOC(sizeof(struct link_group));
140                 if (!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;
147
148                 /* XXX Make the table grow when too many entries have been
149                  * inserted. */
150                 table->num_entries++;
151         }
152         return 0;
153 }
154
155 static void free_link_group_list(struct link_group *group)
156 {
157         struct link_group *next_group;
158         while (group) {
159                 next_group = group->next;
160                 FREE(group);
161                 group = next_group;
162         }
163 }
164
165 /* Frees a link group table. */
166 void free_link_group_table(struct link_group_table *table)
167 {
168         struct link_group *single, *next;
169
170         if (!table)
171                 return;
172         if (table->array)
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);
176
177         FREE(table);
178 }
179
180 u64 assign_link_group_ids_to_list(struct link_group *group, u64 id)
181 {
182         struct dentry *dentry;
183         struct list_head *cur;
184         while (group) {
185                 cur = group->dentry_list;
186                 do {
187                         dentry = container_of(cur,
188                                               struct dentry,
189                                               link_group_list);
190                         dentry->hard_link = id;
191                         cur = cur->next;
192                 } while (cur != group->dentry_list);
193                 group->link_group_id = id;
194                 id++;
195                 group = group->next;
196         }
197         return id;
198 }
199
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)
202 {
203         struct link_group *group, *next_group;
204         size_t pos;
205
206         group = table->extra_groups;
207         while (group) {
208                 next_group = group->next;
209                 pos = group->link_group_id % table->capacity;
210                 group->next = table->array[pos];
211                 table->array[pos] = group;
212                 group = next_group;
213         }
214         table->extra_groups = NULL;
215 }
216
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)
220 {
221         DEBUG("Assigning link groups");
222
223         /* Assign consecutive link group IDs to each link group in the hash
224          * table */
225         u64 id = 1;
226         for (size_t i = 0; i < table->capacity; i++)
227                 id = assign_link_group_ids_to_list(table->array[i], id);
228
229         /* Assign link group IDs to the "extra" link groups and insert them into
230          * the hash table */
231         id = assign_link_group_ids_to_list(table->extra_groups, id);
232         insert_extra_groups(table);
233         return id;
234 }
235
236
237
238 static void inconsistent_link_group(const struct dentry *first_dentry)
239 {
240         const struct dentry *dentry = first_dentry;
241
242         ERROR("An inconsistent hard link group that we cannot correct has been "
243               "detected");
244         ERROR("The dentries are located at the following paths:");
245         do {
246                 ERROR("`%s'", dentry->full_path_utf8);
247         } while ((dentry = container_of(dentry->link_group_list.next,
248                                         struct dentry,
249                                         link_group_list)) != first_dentry);
250 }
251
252 static bool ref_dentries_consistent(const struct dentry * restrict ref_dentry_1,
253                                     const struct dentry * restrict ref_dentry_2)
254 {
255         wimlib_assert(ref_dentry_1 != ref_dentry_2);
256
257         if (ref_dentry_1->num_ads != ref_dentry_2->num_ads)
258                 return false;
259         if (ref_dentry_1->security_id != ref_dentry_2->security_id
260             || ref_dentry_1->attributes != ref_dentry_2->attributes)
261                 return false;
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))
267                         return false;
268                 if (i && !ads_entries_have_same_name(&ref_dentry_1->ads_entries[i - 1],
269                                                      &ref_dentry_2->ads_entries[i - 1]))
270                         return false;
271
272         }
273         return true;
274 }
275
276 static bool dentries_consistent(const struct dentry * restrict ref_dentry,
277                                 const struct dentry * restrict dentry)
278 {
279         wimlib_assert(ref_dentry != dentry);
280
281         if (ref_dentry->num_ads != dentry->num_ads && dentry->num_ads != 0)
282                 return false;
283         if (ref_dentry->security_id != dentry->security_id
284             || ref_dentry->attributes != dentry->attributes)
285                 return false;
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))
291                         return false;
292                 if (i && !ads_entries_have_same_name(&ref_dentry->ads_entries[i - 1],
293                                                      &dentry->ads_entries[i - 1]))
294                         return false;
295         }
296         return true;
297 }
298
299 static int
300 fix_true_link_group(struct dentry *first_dentry)
301 {
302         struct dentry *dentry;
303         struct dentry *ref_dentry = NULL;
304         u64 last_ctime = 0;
305         u64 last_mtime = 0;
306         u64 last_atime = 0;
307
308         dentry = first_dentry;
309         do {
310                 if (!ref_dentry || ref_dentry->num_ads == 0)
311                         ref_dentry = dentry;
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,
319                                         struct dentry,
320                                         link_group_list)) != first_dentry);
321
322
323         ref_dentry->ads_entries_status = ADS_ENTRIES_OWNER;
324         dentry = first_dentry;
325         do {
326                 if (dentry != ref_dentry) {
327                         if (!dentries_consistent(ref_dentry, dentry)) {
328                                 inconsistent_link_group(first_dentry);
329                                 return WIMLIB_ERR_INVALID_DENTRY;
330                         }
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;
336                 }
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,
341                                         struct dentry,
342                                         link_group_list)) != first_dentry);
343         return 0;
344 }
345
346 static int
347 fix_nominal_link_group(struct link_group *group,
348                        struct link_group **new_groups)
349 {
350         struct dentry *tmp, *dentry, *ref_dentry;
351         int ret;
352         size_t num_true_link_groups;
353         struct list_head *head;
354         u64 link_group_id;
355
356         LIST_HEAD(dentries_with_data_streams);
357         LIST_HEAD(dentries_with_no_data_streams);
358         LIST_HEAD(true_link_groups);
359
360         /* Create the lists @dentries_with_data_streams and
361          * @dentries_with_no_data_streams. */
362         dentry = container_of(group->dentry_list, struct dentry,
363                               link_group_list);
364         do {
365                 for (unsigned i = 0; i <= dentry->num_ads; i++) {
366                         const u8 *hash;
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);
371                                 goto next_dentry;
372                         }
373                 }
374                 list_add(&dentry->tmp_list,
375                          &dentries_with_no_data_streams);
376         next_dentry:
377                 dentry = container_of(dentry->link_group_list.next,
378                                       struct dentry,
379                                       link_group_list);
380         } while (&dentry->link_group_list != group->dentry_list);
381
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,
386                                                         struct dentry,
387                                                         link_group_list));
388
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
391          * groups. */
392         num_true_link_groups = 0;
393         list_for_each_entry_safe(dentry, tmp, &dentries_with_data_streams,
394                                  tmp_list)
395         {
396                 list_del(&dentry->tmp_list);
397
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);
406                                 goto next_dentry_2;
407                         }
408                 }
409                 num_true_link_groups++;
410                 list_add(&dentry->tmp_list, &true_link_groups);
411                 INIT_LIST_HEAD(&dentry->link_group_list);
412 next_dentry_2:
413                 ;
414         }
415
416         wimlib_assert(num_true_link_groups != 0);
417
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
420          *
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 "
426                               "inconsistencies,");
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;
431                 }
432                 ref_dentry = container_of(true_link_groups.next,
433                                           struct dentry,
434                                           tmp_list);
435                 list_for_each_entry(dentry, &dentries_with_no_data_streams,
436                                     tmp_list)
437                 {
438                         list_add(&dentry->link_group_list,
439                                  &ref_dentry->link_group_list);
440                 }
441         }
442
443         list_for_each_entry(dentry, &true_link_groups, tmp_list) {
444                 ret = fix_true_link_group(dentry);
445                 if (ret != 0)
446                         return ret;
447         }
448
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;
452              head = head->next)
453         {
454                 dentry = container_of(head, struct dentry, tmp_list);
455                 group = MALLOC(sizeof(*group));
456                 if (!group) {
457                         ERROR("Out of memory");
458                         return WIMLIB_ERR_NOMEM;
459                 }
460                 group->link_group_id = link_group_id;
461                 group->dentry_list = &dentry->link_group_list;
462                 group->next = *new_groups;
463                 *new_groups = group;
464         } while ((head = head->next) != &true_link_groups);
465         return 0;
466 }
467
468 /*
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.
471  *
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.
476  *
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).
479  */
480 int fix_link_groups(struct link_group_table *table)
481 {
482         for (u64 i = 0; i < table->capacity; i++) {
483                 struct link_group *group = table->array[i];
484                 while (group) {
485                         int ret;
486                         ret = fix_nominal_link_group(group, &table->extra_groups);
487                         if (ret != 0)
488                                 return ret;
489                         group = group->next;
490                 }
491         }
492         return 0;
493 }
494
495 #if 0
496 static bool dentries_have_same_ads(const struct dentry *d1,
497                                    const struct dentry *d2)
498 {
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)
504                         return false;
505                 if (!hashes_equal(d1->ads_entries[i].hash,
506                                   d2->ads_entries[i].hash))
507                         return false;
508         }
509         return true;
510 }
511
512 /* 
513  * Share the alternate stream entries between hard-linked dentries.
514  *
515  * Notes:
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
521  *   0's).
522  *
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.
528  *
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.
533  */
534 static int share_dentry_ads(struct dentry *owner, struct dentry *user)
535 {
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";
542                 goto mismatch;
543         }
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;
548         }
549         if (owner->security_id != user->security_id) {
550                 mismatch_type = "security ID";
551                 goto mismatch;
552         }
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);
557                 } else {
558                         mismatch_type = "main file resource";
559                         goto mismatch;
560                 }
561         }
562         if (data_streams_shared) {
563                 if (!dentries_have_same_ads(owner, user)) {
564                         mismatch_type = "Alternate Stream Entries";
565                         goto mismatch;
566                 }
567         }
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) {
571         }
572         dentry_free_ads_entries(user);
573         user->ads_entries = owner->ads_entries;
574         user->ads_entries_status = ADS_ENTRIES_USER;
575         return 0;
576 mismatch:
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,
580                 mismatch_type);
581         return WIMLIB_ERR_INVALID_DENTRY;
582 }
583 #endif