]> wimlib.net Git - wimlib/blob - src/hardlink.c
Win32: Add untested support for encryted capture
[wimlib] / src / hardlink.c
1 /*
2  * hardlink.c
3  *
4  * Code to deal with hard links in WIMs.
5  */
6
7 /*
8  * Copyright (C) 2012, 2013 Eric Biggers
9  *
10  * This file is part of wimlib, a library for working with WIM files.
11  *
12  * wimlib is free software; you can redistribute it and/or modify it under the
13  * terms of the GNU General Public License as published by the Free
14  * Software Foundation; either version 3 of the License, or (at your option)
15  * any later version.
16  *
17  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
18  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19  * A PARTICULAR PURPOSE. See the GNU General Public License for more
20  * details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with wimlib; if not, see http://www.gnu.org/licenses/.
24  */
25
26 #include "wimlib_internal.h"
27 #include "dentry.h"
28 #include "list.h"
29 #include "lookup_table.h"
30
31 /*                             NULL        NULL
32  *                              ^           ^
33  *         dentry               |           |
34  *        /     \          -----------  -----------
35  *        |      dentry<---|  struct  | |  struct  |---> dentry
36  *        \     /          | wim_inode| | wim_inode|
37  *         dentry          ------------ ------------
38  *                              ^           ^
39  *                              |           |
40  *                              |           |                   dentry
41  *                         -----------  -----------            /      \
42  *               dentry<---|  struct  | |  struct  |---> dentry        dentry
43  *              /          | wim_inode| | wim_inode|           \      /
44  *         dentry          ------------ ------------            dentry
45  *                              ^           ^
46  *                              |           |
47  *                            -----------------
48  *    wim_inode_table->array  | idx 0 | idx 1 |
49  *                            -----------------
50  */
51
52
53 int
54 init_inode_table(struct wim_inode_table *table, size_t capacity)
55 {
56         table->array = CALLOC(capacity, sizeof(table->array[0]));
57         if (!table->array) {
58                 ERROR("Cannot initalize inode table: out of memory");
59                 return WIMLIB_ERR_NOMEM;
60         }
61         table->num_entries  = 0;
62         table->capacity     = capacity;
63         INIT_LIST_HEAD(&table->extra_inodes);
64         return 0;
65 }
66
67 static inline size_t
68 inode_link_count(const struct wim_inode *inode)
69 {
70         const struct list_head *cur;
71         size_t size = 0;
72         list_for_each(cur, &inode->i_dentry)
73                 size++;
74         return size;
75 }
76
77 /* Insert a dentry into the inode table based on the inode number of the
78  * attached inode (which came from the hard link group ID field of the on-disk
79  * WIM dentry) */
80 static int
81 inode_table_insert(struct wim_dentry *dentry, void *_table)
82 {
83         struct wim_inode_table *table = _table;
84         struct wim_inode *d_inode = dentry->d_inode;
85
86         if (d_inode->i_ino == 0) {
87                 /* A dentry with a hard link group ID of 0 indicates that it's
88                  * in a hard link group by itself.  Add it to the list of extra
89                  * inodes rather than inserting it into the hash lists. */
90                 list_add_tail(&d_inode->i_list, &table->extra_inodes);
91
92                 wimlib_assert(d_inode->i_dentry.next == &dentry->d_alias);
93                 wimlib_assert(d_inode->i_dentry.prev == &dentry->d_alias);
94                 wimlib_assert(d_inode->i_nlink == 1);
95         } else {
96                 size_t pos;
97                 struct wim_inode *inode;
98                 struct hlist_node *cur;
99
100                 /* Try adding this dentry to an existing inode */
101                 pos = d_inode->i_ino % table->capacity;
102                 hlist_for_each_entry(inode, cur, &table->array[pos], i_hlist) {
103                         if (inode->i_ino == d_inode->i_ino) {
104                                 inode_add_dentry(dentry, inode);
105                                 inode->i_nlink++;
106                                 return 0;
107                         }
108                 }
109
110                 /* No inode in the table has the same number as this one, so add
111                  * it to the table. */
112                 hlist_add_head(&d_inode->i_hlist, &table->array[pos]);
113
114                 wimlib_assert(d_inode->i_dentry.next == &dentry->d_alias);
115                 wimlib_assert(d_inode->i_dentry.prev == &dentry->d_alias);
116                 wimlib_assert(d_inode->i_nlink == 1);
117
118                 /* XXX Make the table grow when too many entries have been
119                  * inserted. */
120                 table->num_entries++;
121         }
122         return 0;
123 }
124
125 static struct wim_inode *
126 inode_table_get_inode(struct wim_inode_table *table, u64 ino, u64 devno)
127 {
128         u64 hash = hash_u64(hash_u64(ino) + hash_u64(devno));
129         size_t pos = hash % table->capacity;
130         struct wim_inode *inode;
131         struct hlist_node *cur;
132
133         hlist_for_each_entry(inode, cur, &table->array[pos], i_hlist) {
134                 if (inode->i_ino == ino && inode->i_devno == devno) {
135                         DEBUG("Using existing inode {devno=%"PRIu64", ino=%"PRIu64"}",
136                               devno, ino);
137                         inode->i_nlink++;
138                         return inode;
139                 }
140         }
141         inode = new_timeless_inode();
142         if (inode) {
143                 inode->i_ino = ino;
144                 inode->i_devno = devno;
145                 hlist_add_head(&inode->i_hlist, &table->array[pos]);
146                 table->num_entries++;
147         }
148         return inode;
149 }
150
151 void
152 inode_ref_streams(struct wim_inode *inode)
153 {
154         for (unsigned i = 0; i <= inode->i_num_ads; i++) {
155                 struct wim_lookup_table_entry *lte;
156                 lte = inode_stream_lte_resolved(inode, i);
157                 if (lte)
158                         lte->refcnt++;
159         }
160 }
161
162 void
163 inode_add_link(struct wim_inode *inode, struct wim_dentry *dentry)
164 {
165 }
166
167 /* Given a directory entry with the name @name for the file with the inode
168  * number @ino and device number @devno, create a new WIM dentry with an
169  * associated inode, where the inode is shared if an inode with the same @ino
170  * and @devno has already been created.  On success, the new WIM dentry is
171  * written to *dentry_ret, and its inode has i_nlink > 1 if a previously
172  * existing inode was used.
173  */
174 int
175 inode_table_new_dentry(struct wim_inode_table *table, const tchar *name,
176                        u64 ino, u64 devno, struct wim_dentry **dentry_ret)
177 {
178         struct wim_dentry *dentry;
179         struct wim_inode *inode;
180         int ret;
181
182         ret = new_dentry(name, &dentry);
183         if (ret)
184                 return ret;
185
186         inode = inode_table_get_inode(table, ino, devno);
187         if (!inode) {
188                 free_dentry(dentry);
189                 return WIMLIB_ERR_NOMEM;
190         }
191         if (inode->i_nlink > 1)
192                 inode_ref_streams(inode);
193         dentry->d_inode = inode;
194         inode_add_dentry(dentry, inode);
195         *dentry_ret = dentry;
196         return 0;
197 }
198
199 #if defined(ENABLE_ERROR_MESSAGES) || defined(ENABLE_DEBUG)
200 static void
201 print_inode_dentries(const struct wim_inode *inode)
202 {
203         struct wim_dentry *dentry;
204         inode_for_each_dentry(dentry, inode)
205                 tprintf(T("%"TS), dentry_full_path(dentry));
206 }
207 #endif
208
209 static void
210 inconsistent_inode(const struct wim_inode *inode)
211 {
212 #ifdef ENABLE_ERROR_MESSAGES
213         ERROR("An inconsistent hard link group that cannot be corrected has "
214               "been detected");
215         ERROR("The dentries are located at the following paths:");
216         print_inode_dentries(inode);
217 #endif
218 }
219
220 static bool
221 ref_inodes_consistent(const struct wim_inode * restrict ref_inode_1,
222                       const struct wim_inode * restrict ref_inode_2)
223 {
224         wimlib_assert(ref_inode_1 != ref_inode_2);
225
226         if (ref_inode_1->i_num_ads != ref_inode_2->i_num_ads)
227                 return false;
228         if (ref_inode_1->i_security_id != ref_inode_2->i_security_id
229             || ref_inode_1->i_attributes != ref_inode_2->i_attributes)
230                 return false;
231         for (unsigned i = 0; i <= ref_inode_1->i_num_ads; i++) {
232                 const u8 *ref_1_hash, *ref_2_hash;
233                 ref_1_hash = inode_stream_hash(ref_inode_1, i);
234                 ref_2_hash = inode_stream_hash(ref_inode_2, i);
235                 if (!hashes_equal(ref_1_hash, ref_2_hash))
236                         return false;
237                 if (i && !ads_entries_have_same_name(&ref_inode_1->i_ads_entries[i - 1],
238                                                      &ref_inode_2->i_ads_entries[i - 1]))
239                         return false;
240
241         }
242         return true;
243 }
244
245 static bool
246 inodes_consistent(const struct wim_inode * restrict ref_inode,
247                   const struct wim_inode * restrict inode)
248 {
249         wimlib_assert(ref_inode != inode);
250
251         if (ref_inode->i_num_ads != inode->i_num_ads &&
252             inode->i_num_ads != 0)
253                 return false;
254         if (ref_inode->i_security_id != inode->i_security_id
255             || ref_inode->i_attributes != inode->i_attributes)
256                 return false;
257         for (unsigned i = 0; i <= min(ref_inode->i_num_ads, inode->i_num_ads); i++) {
258                 const u8 *ref_hash, *hash;
259                 ref_hash = inode_stream_hash(ref_inode, i);
260                 hash = inode_stream_hash(inode, i);
261                 if (!hashes_equal(ref_hash, hash) && !is_zero_hash(hash))
262                         return false;
263                 if (i && !ads_entries_have_same_name(&ref_inode->i_ads_entries[i - 1],
264                                                      &inode->i_ads_entries[i - 1]))
265                         return false;
266         }
267         return true;
268 }
269
270 /* Fix up a "true" inode and check for inconsistencies */
271 static int
272 fix_true_inode(struct wim_inode *inode, struct list_head *inode_list)
273 {
274         struct wim_dentry *dentry;
275         struct wim_dentry *ref_dentry = NULL;
276         struct wim_inode *ref_inode;
277         u64 last_ctime = 0;
278         u64 last_mtime = 0;
279         u64 last_atime = 0;
280
281         inode_for_each_dentry(dentry, inode) {
282                 if (!ref_dentry || dentry->d_inode->i_num_ads > ref_dentry->d_inode->i_num_ads)
283                         ref_dentry = dentry;
284                 if (dentry->d_inode->i_creation_time > last_ctime)
285                         last_ctime = dentry->d_inode->i_creation_time;
286                 if (dentry->d_inode->i_last_write_time > last_mtime)
287                         last_mtime = dentry->d_inode->i_last_write_time;
288                 if (dentry->d_inode->i_last_access_time > last_atime)
289                         last_atime = dentry->d_inode->i_last_access_time;
290         }
291
292         ref_inode = ref_dentry->d_inode;
293         ref_inode->i_nlink = 1;
294         list_add_tail(&ref_inode->i_list, inode_list);
295
296         list_del(&inode->i_dentry);
297         list_add(&ref_inode->i_dentry, &ref_dentry->d_alias);
298
299         inode_for_each_dentry(dentry, ref_inode) {
300                 if (dentry != ref_dentry) {
301                         if (!inodes_consistent(ref_inode, dentry->d_inode)) {
302                                 inconsistent_inode(ref_inode);
303                                 return WIMLIB_ERR_INVALID_DENTRY;
304                         }
305                         /* Free the unneeded `struct wim_inode'. */
306                         dentry->d_inode->i_hlist.next = NULL;
307                         dentry->d_inode->i_hlist.pprev = NULL;
308                         free_inode(dentry->d_inode);
309                         dentry->d_inode = ref_inode;
310                         ref_inode->i_nlink++;
311                 }
312         }
313         ref_inode->i_creation_time = last_ctime;
314         ref_inode->i_last_write_time = last_mtime;
315         ref_inode->i_last_access_time = last_atime;
316         wimlib_assert(inode_link_count(ref_inode) == ref_inode->i_nlink);
317         return 0;
318 }
319
320 /*
321  * Fixes up a nominal inode.
322  *
323  * By a nominal inode we mean a group of two or more dentries that share the
324  * same hard link group ID.
325  *
326  * If dentries in the inode are found to be inconsistent, we may split the inode
327  * into several "true" inodes.
328  *
329  * After splitting up each nominal inode into the "true" inodes we will
330  * canonicalize the link group by getting rid of all the unnecessary `struct
331  * wim_inode's.  There will be just one `struct wim_inode' for each hard link
332  * group remaining.
333  */
334 static int
335 fix_nominal_inode(struct wim_inode *inode, struct list_head *inode_list,
336                   bool *ino_changes_needed)
337 {
338         struct wim_dentry *dentry;
339         struct hlist_node *cur, *tmp;
340         int ret;
341         size_t num_true_inodes;
342
343         wimlib_assert(inode->i_nlink == inode_link_count(inode));
344
345         LIST_HEAD(dentries_with_data_streams);
346         LIST_HEAD(dentries_with_no_data_streams);
347         HLIST_HEAD(true_inodes);
348
349         /* Create a list of dentries in the nominal inode that have at
350          * least one data stream with a non-zero hash, and another list that
351          * contains the dentries that have a zero hash for all data streams. */
352         inode_for_each_dentry(dentry, inode) {
353                 for (unsigned i = 0; i <= dentry->d_inode->i_num_ads; i++) {
354                         const u8 *hash;
355                         hash = inode_stream_hash(dentry->d_inode, i);
356                         if (!is_zero_hash(hash)) {
357                                 list_add(&dentry->tmp_list,
358                                          &dentries_with_data_streams);
359                                 goto next_dentry;
360                         }
361                 }
362                 list_add(&dentry->tmp_list,
363                          &dentries_with_no_data_streams);
364         next_dentry:
365                 ;
366         }
367
368         /* If there are no dentries with data streams, we require the nominal
369          * inode to be a true inode */
370         if (list_empty(&dentries_with_data_streams)) {
371         #ifdef ENABLE_DEBUG
372                 if (inode->i_nlink > 1) {
373                         DEBUG("Found link group of size %u without "
374                               "any data streams:", inode->i_nlink);
375                         print_inode_dentries(inode);
376                         DEBUG("We are going to interpret it as true "
377                               "link group, provided that the dentries "
378                               "are consistent.");
379                 }
380         #endif
381                 return fix_true_inode(inode, inode_list);
382         }
383
384         /* One or more dentries had data streams specified.  We check each of
385          * these dentries for consistency with the others to form a set of true
386          * inodes. */
387         num_true_inodes = 0;
388         list_for_each_entry(dentry, &dentries_with_data_streams, tmp_list) {
389                 /* Look for a true inode that is consistent with this dentry and
390                  * add this dentry to it.  Or, if none of the true inodes are
391                  * consistent with this dentry, add a new one (if that happens,
392                  * we have split the hard link group). */
393                 hlist_for_each_entry(inode, cur, &true_inodes, i_hlist) {
394                         if (ref_inodes_consistent(inode, dentry->d_inode)) {
395                                 inode_add_dentry(dentry, inode);
396                                 goto next_dentry_2;
397                         }
398                 }
399                 num_true_inodes++;
400                 INIT_LIST_HEAD(&dentry->d_inode->i_dentry);
401                 inode_add_dentry(dentry, dentry->d_inode);
402                 hlist_add_head(&dentry->d_inode->i_hlist, &true_inodes);
403 next_dentry_2:
404                 ;
405         }
406
407         wimlib_assert(num_true_inodes != 0);
408
409         /* If there were dentries with no data streams, we require there to only
410          * be one true inode so that we know which inode to assign the
411          * streamless dentries to. */
412         if (!list_empty(&dentries_with_no_data_streams)) {
413                 if (num_true_inodes != 1) {
414                         ERROR("Hard inode ambiguity detected!");
415                         ERROR("We split up inode 0x%"PRIx64" due to "
416                               "inconsistencies,", inode->i_ino);
417                         ERROR("but dentries with no stream information remained. "
418                               "We don't know which inode");
419                         ERROR("to assign them to.");
420                         return WIMLIB_ERR_INVALID_DENTRY;
421                 }
422                 inode = container_of(true_inodes.first, struct wim_inode, i_hlist);
423                 /* Assign the streamless dentries to the one and only true
424                  * inode. */
425                 list_for_each_entry(dentry, &dentries_with_no_data_streams, tmp_list)
426                         inode_add_dentry(dentry, inode);
427         }
428         if (num_true_inodes != 1) {
429         #ifdef ENABLE_DEBUG
430                 inode = container_of(true_inodes.first, struct wim_inode, i_hlist);
431
432                 tprintf(T("Split nominal inode 0x%"PRIx64" into %zu "
433                           "inodes:\n"), inode->i_ino, num_true_inodes);
434                 tputs(T("----------------------------------------------------"
435                         "--------------------------"));
436                 size_t i = 1;
437                 hlist_for_each_entry(inode, cur, &true_inodes, i_hlist) {
438                         tprintf(T("[Split inode %zu]\n"), i++);
439                         print_inode_dentries(inode);
440                         tputchar(T('\n'));
441                 }
442                 tputs(T("----------------------------------------------------"
443                         "--------------------------"));
444         #endif
445                 *ino_changes_needed = true;
446         }
447
448         hlist_for_each_entry_safe(inode, cur, tmp, &true_inodes, i_hlist) {
449                 ret = fix_true_inode(inode, inode_list);
450                 if (ret)
451                         return ret;
452         }
453         return 0;
454 }
455
456 static int
457 fix_inodes(struct wim_inode_table *table, struct list_head *inode_list,
458            bool *ino_changes_needed)
459 {
460         struct wim_inode *inode;
461         struct hlist_node *cur, *tmp;
462         int ret;
463         INIT_LIST_HEAD(inode_list);
464         for (u64 i = 0; i < table->capacity; i++) {
465                 hlist_for_each_entry_safe(inode, cur, tmp, &table->array[i], i_hlist) {
466                         ret = fix_nominal_inode(inode, inode_list, ino_changes_needed);
467                         if (ret)
468                                 return ret;
469                 }
470         }
471         list_splice_tail(&table->extra_inodes, inode_list);
472         return 0;
473 }
474
475 /*
476  * dentry_tree_fix_inodes():
477  *
478  * This function takes as input a tree of WIM dentries that initially has a
479  * different inode associated with each dentry.  Sets of dentries that should
480  * share the same inode (a.k.a. hard link groups) are built using the i_ino
481  * field of each inode, then the link count and alias list for one inode in each
482  * set is set correctly and the unnecessary struct wim_inode's freed.  The
483  * effect is to correctly associate exactly one struct wim_inode with each
484  * original inode, regardless of how many dentries are aliases for that inode.
485  *
486  * The special inode number of 0 indicates that the dentry is in a hard link
487  * group by itself, and therefore has a 'struct wim_inode' with i_nlink=1 to
488  * itself.
489  *
490  * This function also checks the dentries in each hard link group for
491  * consistency.  In some WIMs, such as install.wim for some versions of Windows
492  * 7, dentries can share the same hard link group ID but not actually be hard
493  * linked to each other (based on conflicting information, such as file
494  * contents).  This should be an error, but this case needs be handled.  So,
495  * each "nominal" inode (the inode based on the inode numbers provided in the
496  * WIM) is examined for consistency and may be split into multiple "true" inodes
497  * that are maximally sized consistent sets of dentries.
498  *
499  * Return 0 on success; WIMLIB_ERR_NOMEM or WIMLIB_ERR_INVALID_DENTRY on
500  * failure.  On success, the list of "true" inodes, linked by the i_hlist field,
501  * is returned in the hlist @inode_list.
502  */
503 int
504 dentry_tree_fix_inodes(struct wim_dentry *root, struct list_head *inode_list)
505 {
506         struct wim_inode_table inode_tab;
507         int ret;
508         bool ino_changes_needed;
509
510         DEBUG("Inserting dentries into inode table");
511         ret = init_inode_table(&inode_tab, 9001);
512         if (ret)
513                 return ret;
514
515         for_dentry_in_tree(root, inode_table_insert, &inode_tab);
516
517         DEBUG("Cleaning up the hard link groups");
518         ino_changes_needed = false;
519         ret = fix_inodes(&inode_tab, inode_list, &ino_changes_needed);
520         destroy_inode_table(&inode_tab);
521
522         if (ret == 0 && ino_changes_needed) {
523                 u64 cur_ino = 1;
524                 struct wim_inode *inode;
525
526                 WARNING("Re-assigning inode numbers due to inode inconsistencies");
527                 list_for_each_entry(inode, inode_list, i_list) {
528                         if (inode->i_nlink > 1)
529                                 inode->i_ino = cur_ino++;
530                         else
531                                 inode->i_ino = 0;
532                 }
533         }
534         return ret;
535 }
536
537 /* Assign consecutive inode numbers to the inodes in the inode table, and move
538  * the inodes to a single list @head. */
539 void
540 inode_table_prepare_inode_list(struct wim_inode_table *table,
541                                struct list_head *head)
542 {
543         struct wim_inode *inode;
544         struct hlist_node *cur, *tmp;
545         u64 cur_ino = 1;
546
547         INIT_LIST_HEAD(head);
548         for (size_t i = 0; i < table->capacity; i++) {
549                 hlist_for_each_entry_safe(inode, cur, tmp, &table->array[i], i_hlist)
550                 {
551                         if (inode->i_nlink > 1)
552                                 inode->i_ino = cur_ino++;
553                         else
554                                 inode->i_ino = 0;
555                         list_add_tail(&inode->i_list, head);
556                 }
557                 INIT_HLIST_HEAD(&table->array[i]);
558         }
559         table->num_entries = 0;
560 }