]> wimlib.net Git - wimlib/blob - src/hardlink.c
imagex capture and append consolidation
[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 General Public License as published by the Free
16  * Software Foundation; either version 3 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 General Public License for more
22  * details.
23  *
24  * You should have received a copy of the GNU 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 /*                             NULL        NULL
34  *                              ^           ^
35  *         dentry               |           |                
36  *        /     \          -----------  -----------           
37  *        |      dentry<---|  struct  | |  struct  |---> dentry
38  *        \     /          |link_group| |link_group|       
39  *         dentry          ------------ ------------
40  *                              ^           ^
41  *                              |           |
42  *                              |           |                   dentry
43  *                         -----------  -----------            /      \
44  *               dentry<---|  struct  | |  struct  |---> dentry        dentry
45  *              /          |link_group| |link_group|           \      /
46  *         dentry          ------------ ------------            dentry
47  *                              ^           ^
48  *                              |           |
49  *                            -----------------
50  *    link_group_table->array | idx 0 | idx 1 | 
51  *                            -----------------
52  */
53
54 /* Hard link group; it's identified by its hard link group ID and points to a
55  * circularly linked list of dentries. */
56 struct link_group {
57         u64 link_group_id;
58
59         /* Pointer to use to make a singly-linked list of link groups. */
60         struct link_group *next;
61
62         /* This is a pointer to the circle and not part of the circle itself.
63          * This makes it easy to iterate through other dentries hard-linked to a
64          * given dentry without having to find the "head" of the list first. */
65         struct list_head *dentry_list;
66 };
67
68 /* Hash table to find hard link groups, identified by their hard link group ID.
69  * */
70 struct link_group_table {
71         /* Fields for the hash table */
72         struct link_group **array;
73         u64 num_entries;
74         u64 capacity;
75
76         /* 
77          * Linked list of "extra" groups.  These may be:
78          *
79          * - Hard link groups of size 1, which are all allowed to have 0 for
80          *   their hard link group ID, meaning we cannot insert them into the
81          *   hash table before calling assign_link_group_ids().
82          *
83          * - Groups we create ourselves by splitting a nominal hard link group
84          *   due to inconsistencies in the dentries.  These groups will share a
85          *   hard link group ID with some other group until
86          *   assign_link_group_ids() is called.
87          */
88         struct link_group *extra_groups;
89 };
90
91 /* Returns pointer to a new link group table having the specified capacity */
92 struct link_group_table *new_link_group_table(size_t capacity)
93 {
94         struct link_group_table *table;
95         struct link_group **array;
96
97         table = MALLOC(sizeof(struct link_group_table));
98         if (!table)
99                 goto err;
100         array = CALLOC(capacity, sizeof(array[0]));
101         if (!array) {
102                 FREE(table);
103                 goto err;
104         }
105         table->num_entries  = 0;
106         table->capacity     = capacity;
107         table->array        = array;
108         table->extra_groups = NULL;
109         return table;
110 err:
111         ERROR("Failed to allocate memory for link group table with capacity %zu",
112               capacity);
113         return NULL;
114 }
115
116 /* 
117  * Insert a dentry into the hard link group table based on its hard link group
118  * ID.
119  *
120  * If there is already a dentry in the table having the same hard link group ID,
121  * and the hard link group ID is not 0, the dentry is added to the circular
122  * linked list for that hard link group.
123  *
124  * If the hard link group ID is 0, this indicates a dentry that's in a hard link
125  * group by itself (has a link count of 1).  We can't insert it into the hash
126  * table itself because we don't know what hard link group IDs are available to
127  * give it (this could be kept track of but would be more difficult).  Instead
128  * we keep a linked list of the single dentries, and assign them hard link group
129  * IDs later.
130  */
131 int link_group_table_insert(struct dentry *dentry, void *__table)
132 {
133         struct link_group_table *table = __table;
134         size_t pos;
135         struct link_group *group;
136
137         if (dentry->link_group_id == 0) {
138                 /* Single group--- Add to the list of extra groups (we can't put
139                  * it in the table itself because all the singles have a link
140                  * group ID of 0) */
141                 group = MALLOC(sizeof(struct link_group));
142                 if (!group)
143                         return WIMLIB_ERR_NOMEM;
144                 group->link_group_id = 0;
145                 group->next          = table->extra_groups;
146                 table->extra_groups  = group;
147                 INIT_LIST_HEAD(&dentry->link_group_list);
148                 group->dentry_list = &dentry->link_group_list;
149         } else {
150                 /* Hard link group that may contain multiple dentries (the code
151                  * will work even if the group actually contains only 1 dentry
152                  * though) */
153
154                 /* Try adding to existing hard link group */
155                 pos = dentry->link_group_id % table->capacity;
156                 group = table->array[pos];
157                 while (group) {
158                         if (group->link_group_id == dentry->link_group_id) {
159                                 list_add(&dentry->link_group_list,
160                                          group->dentry_list);
161                                 return 0;
162                         }
163                         group = group->next;
164                 }
165
166                 /* Add new hard link group to the table */
167
168                 group = MALLOC(sizeof(struct link_group));
169                 if (!group)
170                         return WIMLIB_ERR_NOMEM;
171                 group->link_group_id   = dentry->link_group_id;
172                 group->next            = table->array[pos];
173                 INIT_LIST_HEAD(&dentry->link_group_list);
174                 group->dentry_list = &dentry->link_group_list;
175                 table->array[pos]      = group;
176
177                 /* XXX Make the table grow when too many entries have been
178                  * inserted. */
179                 table->num_entries++;
180         }
181         return 0;
182 }
183
184 static void free_link_group_list(struct link_group *group)
185 {
186         struct link_group *next_group;
187         while (group) {
188                 next_group = group->next;
189                 FREE(group);
190                 group = next_group;
191         }
192 }
193
194 /* Frees a link group table. */
195 void free_link_group_table(struct link_group_table *table)
196 {
197         if (table) {
198                 if (table->array)
199                         for (size_t i = 0; i < table->capacity; i++)
200                                 free_link_group_list(table->array[i]);
201                 free_link_group_list(table->extra_groups);
202                 FREE(table);
203         }
204 }
205
206 static u64
207 assign_link_group_ids_to_list(struct link_group *group, u64 id,
208                               struct link_group **extra_groups)
209 {
210         struct dentry *dentry;
211         struct list_head *cur_head;
212         struct link_group *prev_group = NULL;
213         struct link_group *cur_group = group;
214         while (cur_group) {
215                 cur_head = cur_group->dentry_list;
216                 do {
217                         dentry = container_of(cur_head,
218                                               struct dentry,
219                                               link_group_list);
220                         dentry->link_group_id = id;
221                         cur_head = cur_head->next;
222                 } while (cur_head != cur_group->dentry_list);
223                 cur_group->link_group_id = id;
224                 id++;
225                 prev_group = cur_group;
226                 cur_group = cur_group->next;
227         }
228         if (group && extra_groups) {
229                 prev_group->next = *extra_groups;
230                 *extra_groups = group;
231         }
232         return id;
233 }
234
235 /* Insert the link groups in the `extra_groups' list into the hash table */
236 static void insert_extra_groups(struct link_group_table *table)
237 {
238         struct link_group *group, *next_group;
239         size_t pos;
240
241         group = table->extra_groups;
242         while (group) {
243                 next_group        = group->next;
244                 pos               = group->link_group_id % table->capacity;
245                 group->next       = table->array[pos];
246                 table->array[pos] = group;
247                 group             = next_group;
248         }
249         table->extra_groups = NULL;
250 }
251
252 /* Assign the link group IDs to dentries in a link group table, and return the
253  * next available link group ID. */
254 u64 assign_link_group_ids(struct link_group_table *table)
255 {
256         DEBUG("Assigning link groups");
257         struct link_group *extra_groups = table->extra_groups;
258
259         /* Assign consecutive link group IDs to each link group in the hash
260          * table */
261         u64 id = 1;
262         for (size_t i = 0; i < table->capacity; i++) {
263                 id = assign_link_group_ids_to_list(table->array[i], id,
264                                                    &table->extra_groups);
265                 table->array[i] = NULL;
266         }
267
268         /* Assign link group IDs to the "extra" link groups and insert them into
269          * the hash table */
270         id = assign_link_group_ids_to_list(extra_groups, id, NULL);
271         insert_extra_groups(table);
272         return id;
273 }
274
275
276
277 static void inconsistent_link_group(const struct dentry *first_dentry)
278 {
279         const struct dentry *dentry = first_dentry;
280
281         ERROR("An inconsistent hard link group that we cannot correct has been "
282               "detected");
283         ERROR("The dentries are located at the following paths:");
284         do {
285                 ERROR("`%s'", dentry->full_path_utf8);
286         } while ((dentry = container_of(dentry->link_group_list.next,
287                                         const struct dentry,
288                                         link_group_list)) != first_dentry);
289 }
290
291 static bool ref_dentries_consistent(const struct dentry * restrict ref_dentry_1,
292                                     const struct dentry * restrict ref_dentry_2)
293 {
294         wimlib_assert(ref_dentry_1 != ref_dentry_2);
295
296         if (ref_dentry_1->num_ads != ref_dentry_2->num_ads)
297                 return false;
298         if (ref_dentry_1->security_id != ref_dentry_2->security_id
299             || ref_dentry_1->attributes != ref_dentry_2->attributes)
300                 return false;
301         for (unsigned i = 0; i <= ref_dentry_1->num_ads; i++) {
302                 const u8 *ref_1_hash, *ref_2_hash;
303                 ref_1_hash = dentry_stream_hash(ref_dentry_1, i);
304                 ref_2_hash = dentry_stream_hash(ref_dentry_2, i);
305                 if (!hashes_equal(ref_1_hash, ref_2_hash))
306                         return false;
307                 if (i && !ads_entries_have_same_name(&ref_dentry_1->ads_entries[i - 1],
308                                                      &ref_dentry_2->ads_entries[i - 1]))
309                         return false;
310
311         }
312         return true;
313 }
314
315 static bool dentries_consistent(const struct dentry * restrict ref_dentry,
316                                 const struct dentry * restrict dentry)
317 {
318         wimlib_assert(ref_dentry != dentry);
319
320         if (ref_dentry->num_ads != dentry->num_ads && dentry->num_ads != 0)
321                 return false;
322         if (ref_dentry->security_id != dentry->security_id
323             || ref_dentry->attributes != dentry->attributes)
324                 return false;
325         for (unsigned i = 0; i <= min(ref_dentry->num_ads, dentry->num_ads); i++) {
326                 const u8 *ref_hash, *hash;
327                 ref_hash = dentry_stream_hash(ref_dentry, i);
328                 hash = dentry_stream_hash(dentry, i);
329                 if (!hashes_equal(ref_hash, hash) && !is_zero_hash(hash))
330                         return false;
331                 if (i && !ads_entries_have_same_name(&ref_dentry->ads_entries[i - 1],
332                                                      &dentry->ads_entries[i - 1]))
333                         return false;
334         }
335         return true;
336 }
337
338 #ifdef ENABLE_DEBUG
339 static void
340 print_dentry_list(const struct dentry *first_dentry)
341 {
342         const struct dentry *dentry = first_dentry;
343         do {
344                 printf("`%s'\n", dentry->full_path_utf8);
345         } while ((dentry = container_of(dentry->link_group_list.next,
346                                         struct dentry,
347                                         link_group_list)) != first_dentry);
348 }
349 #endif
350
351 /* Fix up a "true" link group and check for inconsistencies */
352 static int
353 fix_true_link_group(struct dentry *first_dentry)
354 {
355         struct dentry *dentry;
356         struct dentry *ref_dentry = NULL;
357         u64 last_ctime = 0;
358         u64 last_mtime = 0;
359         u64 last_atime = 0;
360         bool found_short_name = false;
361
362         dentry = first_dentry;
363         do {
364                 if (!ref_dentry || ref_dentry->num_ads == 0)
365                         ref_dentry = dentry;
366                 if (dentry->short_name_len) {
367                         if (found_short_name) {
368                                 ERROR("Multiple short names in hard link "
369                                       "group!");
370                                 inconsistent_link_group(first_dentry);
371                                 return WIMLIB_ERR_INVALID_DENTRY;
372                         } else {
373                                 found_short_name = true;
374                         }
375                 }
376                 if (dentry->creation_time > last_ctime)
377                         last_ctime = dentry->creation_time;
378                 if (dentry->last_write_time > last_mtime)
379                         last_mtime = dentry->last_write_time;
380                 if (dentry->last_access_time > last_atime)
381                         last_atime = dentry->last_access_time;
382         } while ((dentry = container_of(dentry->link_group_list.next,
383                                         struct dentry,
384                                         link_group_list)) != first_dentry);
385
386
387         ref_dentry->ads_entries_status = ADS_ENTRIES_OWNER;
388         dentry = first_dentry;
389         do {
390                 if (dentry != ref_dentry) {
391                         if (!dentries_consistent(ref_dentry, dentry)) {
392                                 inconsistent_link_group(first_dentry);
393                                 return WIMLIB_ERR_INVALID_DENTRY;
394                         }
395                         copy_hash(dentry->hash, ref_dentry->hash);
396                         dentry_free_ads_entries(dentry);
397                         dentry->num_ads            = ref_dentry->num_ads;
398                         dentry->ads_entries        = ref_dentry->ads_entries;
399                         dentry->ads_entries_status = ADS_ENTRIES_USER;
400                 }
401                 dentry->creation_time    = last_ctime;
402                 dentry->last_write_time  = last_mtime;
403                 dentry->last_access_time = last_atime;
404         } while ((dentry = container_of(dentry->link_group_list.next,
405                                         struct dentry,
406                                         link_group_list)) != first_dentry);
407         return 0;
408 }
409
410 /* 
411  * Fixes up a nominal link group.
412  *
413  * By a nominal link group we mean a group of two or more dentries that share
414  * the same hard link group ID.
415  *
416  * If dentries in the group are found to be inconsistent, we may split the group
417  * into several "true" hard link groups.  @new_groups points to a linked list of
418  * these split groups, and if we create any, they will be added to this list.
419  *
420  * After splitting up each nominal link group into the "true" link groups we
421  * will canonicalize the link groups.  To do this, we:
422  *
423  *      - Assign all the dentries in the link group the most recent timestamp
424  *      among all the corresponding timestamps in the link group, for each of
425  *      the three categories of time stamps.
426  *
427  *      - Make sure the dentry->hash field is valid in all the dentries, if
428  *      possible (this field may be all zeroes, and in the context of a hard
429  *      link group this must be interpreted as implicitly refering to the same
430  *      stream as another dentry in the hard link group that does NOT have all
431  *      zeroes for this field).
432  *
433  *      - Make sure dentry->num_ads is the same in all the dentries in the link
434  *      group.  In some cases, it's possible for it to be set to 0 when it
435  *      actually must be interpreted as being the same as the number of
436  *      alternate data streams in another dentry in the hard link group that has
437  *      a nonzero number of alternate data streams.
438  *
439  *      - Make sure only the dentry->ads_entries array is only allocated for one
440  *      dentry in the hard link group.  This dentry will have
441  *      dentry->ads_entries_status set to ADS_ENTRIES_OWNER, while the others
442  *      will have dentry->ads_entries_status set to ADS_ENTRIES_USER.
443  */
444 static int
445 fix_nominal_link_group(struct link_group *group,
446                        struct link_group **new_groups)
447 {
448         struct dentry *tmp, *dentry, *ref_dentry;
449         int ret;
450         size_t num_true_link_groups;
451         struct list_head *head;
452
453         LIST_HEAD(dentries_with_data_streams);
454         LIST_HEAD(dentries_with_no_data_streams);
455         LIST_HEAD(true_link_groups);
456
457         /* Create a list of dentries in the nominal hard link group that have at
458          * least one data stream with a non-zero hash, and another list that
459          * contains the dentries that have a zero hash for all data streams. */
460         dentry = container_of(group->dentry_list, struct dentry,
461                               link_group_list);
462         do {
463                 for (unsigned i = 0; i <= dentry->num_ads; i++) {
464                         const u8 *hash;
465                         hash = dentry_stream_hash(dentry, i);
466                         if (!is_zero_hash(hash)) {
467                                 list_add(&dentry->tmp_list,
468                                          &dentries_with_data_streams);
469                                 goto next_dentry;
470                         }
471                 }
472                 list_add(&dentry->tmp_list,
473                          &dentries_with_no_data_streams);
474         next_dentry:
475                 dentry = container_of(dentry->link_group_list.next,
476                                       struct dentry,
477                                       link_group_list);
478         } while (&dentry->link_group_list != group->dentry_list);
479
480         /* If there are no dentries with data streams, we require the nominal
481          * link group to be a true link group */
482         if (list_empty(&dentries_with_data_streams)) {
483         #ifdef ENABLE_DEBUG
484                 {
485                         size_t size = dentry_link_group_size(dentry);
486                         if (size > 1) {
487                                 DEBUG("Found link group of size %zu without "
488                                       "any data streams:", size);
489                                 print_dentry_list(dentry);
490                                 DEBUG("We are going to interpret it as true "
491                                       "link group, provided that the dentries "
492                                       "are consistent.");
493                         }
494                 }
495         #endif
496                 return fix_true_link_group(container_of(group->dentry_list,
497                                                         struct dentry,
498                                                         link_group_list));
499         }
500
501         /* One or more dentries had data streams specified.  We check each of
502          * these dentries for consistency with the others to form a set of true
503          * link groups. */
504         num_true_link_groups = 0;
505         list_for_each_entry_safe(dentry, tmp, &dentries_with_data_streams,
506                                  tmp_list)
507         {
508                 list_del(&dentry->tmp_list);
509
510                 /* Look for a true link group that is consistent with
511                  * this dentry and add this dentry to it.  Or, if none
512                  * of the true link groups are consistent with this
513                  * dentry, make a new one. */
514                 list_for_each_entry(ref_dentry, &true_link_groups, tmp_list) {
515                         if (ref_dentries_consistent(ref_dentry, dentry)) {
516                                 list_add(&dentry->link_group_list,
517                                          &ref_dentry->link_group_list);
518                                 goto next_dentry_2;
519                         }
520                 }
521                 num_true_link_groups++;
522                 list_add(&dentry->tmp_list, &true_link_groups);
523                 INIT_LIST_HEAD(&dentry->link_group_list);
524 next_dentry_2:
525                 ;
526         }
527
528         wimlib_assert(num_true_link_groups != 0);
529
530         /* If there were dentries with no data streams, we require there to only
531          * be one true link group so that we know which link group to assign the
532          * streamless dentries to. */
533         if (!list_empty(&dentries_with_no_data_streams)) {
534                 if (num_true_link_groups != 1) {
535                         ERROR("Hard link group ambiguity detected!");
536                         ERROR("We split up hard link group 0x%"PRIx64" due to "
537                               "inconsistencies,", group->link_group_id);
538                         ERROR("but dentries with no stream information remained. "
539                               "We don't know which true hard link");
540                         ERROR("group to assign them to.");
541                         return WIMLIB_ERR_INVALID_DENTRY;
542                 }
543                 /* Assign the streamless dentries to the one and only true link
544                  * group. */
545                 ref_dentry = container_of(true_link_groups.next,
546                                           struct dentry,
547                                           tmp_list);
548                 list_for_each_entry(dentry, &dentries_with_no_data_streams, tmp_list)
549                         list_add(&dentry->link_group_list, &ref_dentry->link_group_list);
550         }
551         if (num_true_link_groups != 1) {
552                 #ifdef ENABLE_DEBUG
553                 {
554                         printf("Split nominal link group 0x%"PRIx64" into %zu "
555                                "link groups:\n",
556                                group->link_group_id, num_true_link_groups);
557                         puts("------------------------------------------------------------------------------");
558                         size_t i = 1;
559                         list_for_each_entry(dentry, &true_link_groups, tmp_list) {
560                                 printf("[Split link group %zu]\n", i++);
561                                 print_dentry_list(dentry);
562                                 putchar('\n');
563                         }
564                         puts("------------------------------------------------------------------------------");
565                 }
566                 #endif
567         }
568
569         list_for_each_entry(dentry, &true_link_groups, tmp_list) {
570                 ret = fix_true_link_group(dentry);
571                 if (ret != 0)
572                         return ret;
573         }
574
575         /* Make new `struct link_group's for the new link groups */
576         for (head = true_link_groups.next->next;
577              head != &true_link_groups;
578              head = head->next)
579         {
580                 dentry = container_of(head, struct dentry, tmp_list);
581                 group = MALLOC(sizeof(*group));
582                 if (!group) {
583                         ERROR("Out of memory");
584                         return WIMLIB_ERR_NOMEM;
585                 }
586                 group->link_group_id = dentry->link_group_id;
587                 group->dentry_list = &dentry->link_group_list;
588                 group->next = *new_groups;
589                 *new_groups = group;
590         }
591         return 0;
592 }
593
594 /*
595  * Goes through each link group and shares the ads_entries (Alternate Data
596  * Stream entries) field of each dentry among members of a hard link group.
597  *
598  * In the process, the dentries in each link group are checked for consistency.
599  * If they contain data features that indicate they cannot really be in the same
600  * hard link group, this should be an error, but in reality this case needs to
601  * be handled, so we split the dentries into different hard link groups.
602  *
603  * One of the dentries in each hard link group group is arbitrarily assigned the
604  * role of "owner" of the memory pointed to by the @ads_entries field,
605  * (ADS_ENTRIES_OWNER), while the others are "users" (ADS_ENTRIES_USER) who are
606  * not allowed to free the memory.
607  */
608 int fix_link_groups(struct link_group_table *table)
609 {
610         for (u64 i = 0; i < table->capacity; i++) {
611                 struct link_group *group = table->array[i];
612                 while (group) {
613                         int ret;
614                         ret = fix_nominal_link_group(group, &table->extra_groups);
615                         if (ret != 0)
616                                 return ret;
617                         group = group->next;
618                 }
619         }
620         return 0;
621 }