]> wimlib.net Git - wimlib/blob - src/inode.c
Update mount implementation
[wimlib] / src / inode.c
1 /*
2  * inode.c
3  */
4
5 /*
6  * Copyright (C) 2012, 2013 Eric Biggers
7  *
8  * This file is part of wimlib, a library for working with WIM files.
9  *
10  * wimlib is free software; you can redistribute it and/or modify it under the
11  * terms of the GNU General Public License as published by the Free
12  * Software Foundation; either version 3 of the License, or (at your option)
13  * any later version.
14  *
15  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
16  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17  * A PARTICULAR PURPOSE. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with wimlib; if not, see http://www.gnu.org/licenses/.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include "wimlib/assert.h"
29 #include "wimlib/case.h"
30 #include "wimlib/dentry.h" /* Only for dentry_full_path().  Otherwise the code
31                               in this file doesn't care about file names/paths.
32                             */
33 #include "wimlib/encoding.h"
34 #include "wimlib/endianness.h"
35 #include "wimlib/error.h"
36 #include "wimlib/inode.h"
37 #include "wimlib/inode_table.h"
38 #include "wimlib/lookup_table.h"
39 #include "wimlib/security.h"
40 #include "wimlib/timestamp.h"
41
42 #include <errno.h>
43
44 /* Allocate a new inode.  Set the timestamps to the current time.  */
45 struct wim_inode *
46 new_inode(void)
47 {
48         struct wim_inode *inode = new_timeless_inode();
49         if (inode) {
50                 u64 now = get_wim_timestamp();
51                 inode->i_creation_time = now;
52                 inode->i_last_access_time = now;
53                 inode->i_last_write_time = now;
54         }
55         return inode;
56 }
57
58
59 /* Allocate a new inode.  Leave the timestamps zeroed out.  */
60 struct wim_inode *
61 new_timeless_inode(void)
62 {
63         struct wim_inode *inode = CALLOC(1, sizeof(struct wim_inode));
64         if (inode) {
65                 inode->i_security_id = -1;
66                 inode->i_nlink = 1;
67                 inode->i_next_stream_id = 1;
68                 inode->i_not_rpfixed = 1;
69                 inode->i_canonical_streams = 1;
70                 INIT_LIST_HEAD(&inode->i_list);
71                 INIT_LIST_HEAD(&inode->i_dentry);
72         }
73         return inode;
74 }
75
76 /* Decrement link count on an inode.  */
77 void
78 put_inode(struct wim_inode *inode)
79 {
80         wimlib_assert(inode->i_nlink != 0);
81         if (--inode->i_nlink == 0) {
82                 /* If FUSE mounts are enabled, we must keep a unlinked inode
83                  * around until all file descriptors to it have been closed.
84                  * inode_put_fd() in mount_image.c handles dropping a file
85                  * descriptor.  */
86         #ifdef WITH_FUSE
87                 if (inode->i_num_opened_fds == 0)
88         #endif
89                         free_inode(inode);
90         }
91 }
92
93 /* De-allocate memory for an alternate data stream entry.  */
94 static void
95 destroy_ads_entry(struct wim_ads_entry *ads_entry)
96 {
97         FREE(ads_entry->stream_name);
98 }
99
100 /* Free an inode.  Only use this if there can't be other links to the inode or
101  * if it doesn't matter if there are.  */
102 void
103 free_inode(struct wim_inode *inode)
104 {
105         if (inode == NULL)
106                 return;
107
108         if (inode->i_ads_entries) {
109                 for (u16 i = 0; i < inode->i_num_ads; i++)
110                         destroy_ads_entry(&inode->i_ads_entries[i]);
111                 FREE(inode->i_ads_entries);
112         }
113         if (inode->i_extra)
114                 FREE(inode->i_extra);
115         /* HACK: This may instead delete the inode from i_list, but hlist_del()
116          * behaves the same as list_del(). */
117         if (!hlist_unhashed(&inode->i_hlist))
118                 hlist_del(&inode->i_hlist);
119         FREE(inode);
120 }
121
122 /* Return %true iff the alternate data stream entry @entry has the UTF-16LE
123  * stream name @name that has length @name_nbytes bytes.  */
124 static inline bool
125 ads_entry_has_name(const struct wim_ads_entry *entry,
126                    const utf16lechar *name, size_t name_nbytes,
127                    bool ignore_case)
128 {
129         return 0 == cmp_utf16le_strings(name,
130                                         name_nbytes / 2,
131                                         entry->stream_name,
132                                         entry->stream_name_nbytes / 2,
133                                         ignore_case);
134 }
135
136 /*
137  * Returns the alternate data stream entry belonging to @inode that has the
138  * stream name @stream_name, or NULL if the inode has no alternate data stream
139  * with that name.
140  *
141  * If @p stream_name is the empty string, NULL is returned --- that is, this
142  * function will not return "unnamed" alternate data stream entries.
143  *
144  * If NULL is returned, errno is set.
145  */
146 struct wim_ads_entry *
147 inode_get_ads_entry(struct wim_inode *inode, const tchar *stream_name)
148 {
149         int ret;
150         const utf16lechar *stream_name_utf16le;
151         size_t stream_name_utf16le_nbytes;
152         u16 i;
153         struct wim_ads_entry *result;
154
155         if (inode->i_num_ads == 0) {
156                 errno = ENOENT;
157                 return NULL;
158         }
159
160         if (stream_name[0] == T('\0')) {
161                 errno = ENOENT;
162                 return NULL;
163         }
164
165         ret = tstr_get_utf16le_and_len(stream_name, &stream_name_utf16le,
166                                        &stream_name_utf16le_nbytes);
167         if (ret)
168                 return NULL;
169
170         i = 0;
171         result = NULL;
172         do {
173                 if (ads_entry_has_name(&inode->i_ads_entries[i],
174                                        stream_name_utf16le,
175                                        stream_name_utf16le_nbytes,
176                                        default_ignore_case))
177                 {
178                         result = &inode->i_ads_entries[i];
179                         break;
180                 }
181         } while (++i != inode->i_num_ads);
182
183         tstr_put_utf16le(stream_name_utf16le);
184
185         if (!result)
186                 errno = ENOENT;
187         return result;
188 }
189
190 static struct wim_ads_entry *
191 do_inode_add_ads(struct wim_inode *inode,
192                  utf16lechar *stream_name, size_t stream_name_nbytes)
193 {
194         u16 num_ads;
195         struct wim_ads_entry *ads_entries;
196         struct wim_ads_entry *new_entry;
197
198         if (inode->i_num_ads >= 0xfffe) {
199                 ERROR("Too many alternate data streams in one inode!");
200                 errno = EFBIG;
201                 return NULL;
202         }
203         num_ads = inode->i_num_ads + 1;
204         ads_entries = REALLOC(inode->i_ads_entries,
205                               num_ads * sizeof(inode->i_ads_entries[0]));
206         if (ads_entries == NULL) {
207                 ERROR("Failed to allocate memory for new alternate data stream");
208                 return NULL;
209         }
210         inode->i_ads_entries = ads_entries;
211
212         new_entry = &inode->i_ads_entries[num_ads - 1];
213
214         memset(new_entry, 0, sizeof(struct wim_ads_entry));
215         new_entry->stream_name = stream_name;
216         new_entry->stream_name_nbytes = stream_name_nbytes;
217         new_entry->stream_id = inode->i_next_stream_id++;
218         inode->i_num_ads = num_ads;
219         return new_entry;
220 }
221
222 struct wim_ads_entry *
223 inode_add_ads_utf16le(struct wim_inode *inode,
224                       const utf16lechar *stream_name, size_t stream_name_nbytes)
225 {
226         utf16lechar *dup = NULL;
227         struct wim_ads_entry *result;
228
229         if (stream_name_nbytes) {
230                 dup = utf16le_dupz(stream_name, stream_name_nbytes);
231                 if (!dup)
232                         return NULL;
233         }
234
235         result = do_inode_add_ads(inode, dup, stream_name_nbytes);
236         if (!result)
237                 FREE(dup);
238         return result;
239 }
240
241 /*
242  * Add an alternate stream entry to a WIM inode.  On success, returns a pointer
243  * to the new entry; on failure, returns NULL and sets errno.
244  */
245 struct wim_ads_entry *
246 inode_add_ads(struct wim_inode *inode, const tchar *stream_name)
247 {
248         utf16lechar *stream_name_utf16le = NULL;
249         size_t stream_name_utf16le_nbytes = 0;
250         struct wim_ads_entry *result;
251
252         if (stream_name && *stream_name)
253                 if (tstr_to_utf16le(stream_name,
254                                     tstrlen(stream_name) * sizeof(tchar),
255                                     &stream_name_utf16le,
256                                     &stream_name_utf16le_nbytes))
257                         return NULL;
258
259         result = do_inode_add_ads(inode, stream_name_utf16le,
260                                   stream_name_utf16le_nbytes);
261         if (!result)
262                 FREE(stream_name_utf16le);
263         return result;
264 }
265
266 struct wim_ads_entry *
267 inode_add_ads_with_data(struct wim_inode *inode, const tchar *name,
268                         const void *value, size_t size,
269                         struct wim_lookup_table *lookup_table)
270 {
271         struct wim_ads_entry *new_entry;
272
273         wimlib_assert(inode->i_resolved);
274
275         new_entry = inode_add_ads(inode, name);
276         if (!new_entry)
277                 return NULL;
278
279         new_entry->lte = new_stream_from_data_buffer(value, size, lookup_table);
280         if (!new_entry->lte) {
281                 inode_remove_ads(inode, new_entry, lookup_table);
282                 return NULL;
283         }
284         return new_entry;
285 }
286
287 bool
288 inode_has_named_stream(const struct wim_inode *inode)
289 {
290         for (u16 i = 0; i < inode->i_num_ads; i++)
291                 if (ads_entry_is_named_stream(&inode->i_ads_entries[i]))
292                         return true;
293         return false;
294 }
295
296 /* Set the unnamed stream of a WIM inode, given a data buffer containing the
297  * stream contents. */
298 int
299 inode_set_unnamed_stream(struct wim_inode *inode, const void *data, size_t len,
300                          struct wim_lookup_table *lookup_table)
301 {
302         wimlib_assert(inode->i_resolved);
303         inode->i_lte = new_stream_from_data_buffer(data, len, lookup_table);
304         if (inode->i_lte == NULL)
305                 return WIMLIB_ERR_NOMEM;
306         return 0;
307 }
308
309 /* Remove an alternate data stream from a WIM inode  */
310 void
311 inode_remove_ads(struct wim_inode *inode, struct wim_ads_entry *entry,
312                  struct wim_lookup_table *lookup_table)
313 {
314         struct wim_lookup_table_entry *lte;
315         unsigned idx = entry - inode->i_ads_entries;
316
317         wimlib_assert(idx < inode->i_num_ads);
318         wimlib_assert(inode->i_resolved);
319
320         lte = entry->lte;
321         if (lte)
322                 lte_decrement_refcnt(lte, lookup_table);
323
324         destroy_ads_entry(entry);
325
326         memmove(&inode->i_ads_entries[idx],
327                 &inode->i_ads_entries[idx + 1],
328                 (inode->i_num_ads - idx - 1) * sizeof(inode->i_ads_entries[0]));
329         inode->i_num_ads--;
330 }
331
332 /*
333  * Resolve an inode's lookup table entries.
334  *
335  * This replaces the SHA1 hash fields (which are used to lookup an entry in the
336  * lookup table) with pointers directly to the lookup table entries.
337  *
338  * If @force is %false:
339  *      If any needed SHA1 message digests are not found in the lookup table,
340  *      WIMLIB_ERR_RESOURCE_NOT_FOUND is returned and the inode is left
341  *      unmodified.
342  * If @force is %true:
343  *      If any needed SHA1 message digests are not found in the lookup table,
344  *      new entries are allocated and inserted into the lookup table.
345  */
346 int
347 inode_resolve_streams(struct wim_inode *inode, struct wim_lookup_table *table,
348                       bool force)
349 {
350         const u8 *hash;
351
352         if (!inode->i_resolved) {
353                 struct wim_lookup_table_entry *lte, *ads_lte;
354
355                 /* Resolve the default file stream */
356                 lte = NULL;
357                 hash = inode->i_hash;
358                 if (!is_zero_hash(hash)) {
359                         lte = lookup_stream(table, hash);
360                         if (!lte) {
361                                 if (force) {
362                                         lte = new_lookup_table_entry();
363                                         if (!lte)
364                                                 return WIMLIB_ERR_NOMEM;
365                                         copy_hash(lte->hash, hash);
366                                         lookup_table_insert(table, lte);
367                                 } else {
368                                         goto stream_not_found;
369                                 }
370                         }
371                 }
372
373                 /* Resolve the alternate data streams */
374                 struct wim_lookup_table_entry *ads_ltes[inode->i_num_ads];
375                 for (u16 i = 0; i < inode->i_num_ads; i++) {
376                         struct wim_ads_entry *cur_entry;
377
378                         ads_lte = NULL;
379                         cur_entry = &inode->i_ads_entries[i];
380                         hash = cur_entry->hash;
381                         if (!is_zero_hash(hash)) {
382                                 ads_lte = lookup_stream(table, hash);
383                                 if (!ads_lte) {
384                                         if (force) {
385                                                 ads_lte = new_lookup_table_entry();
386                                                 if (!ads_lte)
387                                                         return WIMLIB_ERR_NOMEM;
388                                                 copy_hash(ads_lte->hash, hash);
389                                                 lookup_table_insert(table, ads_lte);
390                                         } else {
391                                                 goto stream_not_found;
392                                         }
393                                 }
394                         }
395                         ads_ltes[i] = ads_lte;
396                 }
397                 inode->i_lte = lte;
398                 for (u16 i = 0; i < inode->i_num_ads; i++)
399                         inode->i_ads_entries[i].lte = ads_ltes[i];
400                 inode->i_resolved = 1;
401         }
402         return 0;
403
404 stream_not_found:
405         return stream_not_found_error(inode, hash);
406 }
407
408 void
409 inode_unresolve_streams(struct wim_inode *inode)
410 {
411         if (inode->i_resolved) {
412                 if (inode->i_lte)
413                         copy_hash(inode->i_hash, inode->i_lte->hash);
414                 else
415                         zero_out_hash(inode->i_hash);
416
417                 for (u16 i = 0; i < inode->i_num_ads; i++) {
418                         if (inode->i_ads_entries[i].lte)
419                                 copy_hash(inode->i_ads_entries[i].hash,
420                                           inode->i_ads_entries[i].lte->hash);
421                         else
422                                 zero_out_hash(inode->i_ads_entries[i].hash);
423                 }
424                 inode->i_resolved = 0;
425         }
426 }
427
428 /*
429  * Returns the lookup table entry for stream @stream_idx of the inode, where
430  * stream_idx = 0 means the default un-named file stream, and stream_idx >= 1
431  * corresponds to an alternate data stream.
432  *
433  * This works for both resolved and un-resolved inodes.
434  */
435 struct wim_lookup_table_entry *
436 inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
437                  const struct wim_lookup_table *table)
438 {
439         if (inode->i_resolved)
440                 return inode_stream_lte_resolved(inode, stream_idx);
441         else
442                 return inode_stream_lte_unresolved(inode, stream_idx, table);
443 }
444
445 struct wim_lookup_table_entry *
446 inode_unnamed_stream_resolved(const struct wim_inode *inode, u16 *stream_idx_ret)
447 {
448         wimlib_assert(inode->i_resolved);
449         for (unsigned i = 0; i <= inode->i_num_ads; i++) {
450                 if (inode_stream_name_nbytes(inode, i) == 0 &&
451                     !is_zero_hash(inode_stream_hash_resolved(inode, i)))
452                 {
453                         *stream_idx_ret = i;
454                         return inode_stream_lte_resolved(inode, i);
455                 }
456         }
457         *stream_idx_ret = 0;
458         return NULL;
459 }
460
461 struct wim_lookup_table_entry *
462 inode_unnamed_lte_resolved(const struct wim_inode *inode)
463 {
464         u16 stream_idx;
465         return inode_unnamed_stream_resolved(inode, &stream_idx);
466 }
467
468 struct wim_lookup_table_entry *
469 inode_unnamed_lte_unresolved(const struct wim_inode *inode,
470                              const struct wim_lookup_table *table)
471 {
472         wimlib_assert(!inode->i_resolved);
473         for (unsigned i = 0; i <= inode->i_num_ads; i++) {
474                 if (inode_stream_name_nbytes(inode, i) == 0 &&
475                     !is_zero_hash(inode_stream_hash_unresolved(inode, i)))
476                 {
477                         return inode_stream_lte_unresolved(inode, i, table);
478                 }
479         }
480         return NULL;
481 }
482
483 /* Return the lookup table entry for the unnamed data stream of an inode, or
484  * NULL if there is none.
485  *
486  * You'd think this would be easier than it actually is, since the unnamed data
487  * stream should be the one referenced from the inode itself.  Alas, if there
488  * are named data streams, Microsoft's "imagex.exe" program will put the unnamed
489  * data stream in one of the alternate data streams instead of inside the WIM
490  * dentry itself.  So we need to check the alternate data streams too.
491  *
492  * Also, note that a dentry may appear to have more than one unnamed stream, but
493  * if the SHA1 message digest is all 0's then the corresponding stream does not
494  * really "count" (this is the case for the inode's own file stream when the
495  * file stream that should be there is actually in one of the alternate stream
496  * entries.).  This is despite the fact that we may need to extract such a
497  * missing entry as an empty file or empty named data stream.
498  */
499 struct wim_lookup_table_entry *
500 inode_unnamed_lte(const struct wim_inode *inode,
501                   const struct wim_lookup_table *table)
502 {
503         if (inode->i_resolved)
504                 return inode_unnamed_lte_resolved(inode);
505         else
506                 return inode_unnamed_lte_unresolved(inode, table);
507 }
508
509 /* Returns the SHA1 message digest of the unnamed data stream of a WIM inode, or
510  * 'zero_hash' if the unnamed data stream is missing has all zeroes in its SHA1
511  * message digest field.  */
512 const u8 *
513 inode_unnamed_stream_hash(const struct wim_inode *inode)
514 {
515         const u8 *hash;
516
517         for (unsigned i = 0; i <= inode->i_num_ads; i++) {
518                 if (inode_stream_name_nbytes(inode, i) == 0) {
519                         hash = inode_stream_hash(inode, i);
520                         if (!is_zero_hash(hash))
521                                 return hash;
522                 }
523         }
524         return zero_hash;
525 }
526
527 /* Given an unhashed stream, get the pointer to it in an inode.
528  * As this is only for unhashed streams, there can only be one such pointer.  */
529 struct wim_lookup_table_entry **
530 retrieve_lte_pointer(struct wim_lookup_table_entry *lte)
531 {
532         wimlib_assert(lte->unhashed);
533         struct wim_inode *inode = lte->back_inode;
534         u32 stream_id = lte->back_stream_id;
535         if (stream_id == 0)
536                 return &inode->i_lte;
537         else
538                 for (u16 i = 0; i < inode->i_num_ads; i++)
539                         if (inode->i_ads_entries[i].stream_id == stream_id)
540                                 return &inode->i_ads_entries[i].lte;
541         wimlib_assert(0);
542         return NULL;
543 }
544
545 int
546 stream_not_found_error(const struct wim_inode *inode, const u8 *hash)
547 {
548         if (wimlib_print_errors) {
549                 ERROR("\"%"TS"\": stream not found", inode_first_full_path(inode));
550                 tfprintf(stderr, T("        SHA-1 message digest of missing stream:\n        "));
551                 print_hash(hash, stderr);
552                 tputc(T('\n'), stderr);
553         }
554         return WIMLIB_ERR_RESOURCE_NOT_FOUND;
555 }
556
557 /*
558  * Reads the alternate data stream entries of a WIM dentry.
559  *
560  * @p:
561  *      Pointer to buffer that starts with the first alternate stream entry.
562  *
563  * @inode:
564  *      Inode to load the alternate data streams into.  @inode->i_num_ads must
565  *      have been set to the number of alternate data streams that are expected.
566  *
567  * @remaining_size:
568  *      Number of bytes of data remaining in the buffer pointed to by @p.
569  *
570  * On success, inode->i_ads_entries is set to an array of `struct
571  * wim_ads_entry's of length inode->i_num_ads.  On failure, @inode is not
572  * modified.
573  *
574  * Return values:
575  *      WIMLIB_ERR_SUCCESS (0)
576  *      WIMLIB_ERR_INVALID_METADATA_RESOURCE
577  *      WIMLIB_ERR_NOMEM
578  */
579 int
580 read_ads_entries(const u8 * restrict p, struct wim_inode * restrict inode,
581                  size_t nbytes_remaining)
582 {
583         u16 num_ads;
584         struct wim_ads_entry *ads_entries;
585         int ret;
586
587         BUILD_BUG_ON(sizeof(struct wim_ads_entry_on_disk) != WIM_ADS_ENTRY_DISK_SIZE);
588
589         /* Allocate an array for our in-memory representation of the alternate
590          * data stream entries. */
591         num_ads = inode->i_num_ads;
592         ads_entries = CALLOC(num_ads, sizeof(inode->i_ads_entries[0]));
593         if (ads_entries == NULL)
594                 goto out_of_memory;
595
596         /* Read the entries into our newly allocated buffer. */
597         for (u16 i = 0; i < num_ads; i++) {
598                 u64 length;
599                 struct wim_ads_entry *cur_entry;
600                 const struct wim_ads_entry_on_disk *disk_entry =
601                         (const struct wim_ads_entry_on_disk*)p;
602
603                 cur_entry = &ads_entries[i];
604                 ads_entries[i].stream_id = i + 1;
605
606                 /* Do we have at least the size of the fixed-length data we know
607                  * need? */
608                 if (nbytes_remaining < sizeof(struct wim_ads_entry_on_disk))
609                         goto out_invalid;
610
611                 /* Read the length field */
612                 length = le64_to_cpu(disk_entry->length);
613
614                 /* Make sure the length field is neither so small it doesn't
615                  * include all the fixed-length data nor so large it overflows
616                  * the metadata resource buffer. */
617                 if (length < sizeof(struct wim_ads_entry_on_disk) ||
618                     length > nbytes_remaining)
619                         goto out_invalid;
620
621                 /* Read the rest of the fixed-length data. */
622
623                 cur_entry->reserved = le64_to_cpu(disk_entry->reserved);
624                 copy_hash(cur_entry->hash, disk_entry->hash);
625                 cur_entry->stream_name_nbytes = le16_to_cpu(disk_entry->stream_name_nbytes);
626
627                 /* If stream_name_nbytes != 0, this is a named stream.
628                  * Otherwise this is an unnamed stream, or in some cases (bugs
629                  * in Microsoft's software I guess) a meaningless entry
630                  * distinguished from the real unnamed stream entry, if any, by
631                  * the fact that the real unnamed stream entry has a nonzero
632                  * hash field. */
633                 if (cur_entry->stream_name_nbytes) {
634                         /* The name is encoded in UTF16-LE, which uses 2-byte
635                          * coding units, so the length of the name had better be
636                          * an even number of bytes... */
637                         if (cur_entry->stream_name_nbytes & 1)
638                                 goto out_invalid;
639
640                         /* Add the length of the stream name to get the length
641                          * we actually need to read.  Make sure this isn't more
642                          * than the specified length of the entry. */
643                         if (sizeof(struct wim_ads_entry_on_disk) +
644                             cur_entry->stream_name_nbytes > length)
645                                 goto out_invalid;
646
647                         cur_entry->stream_name = utf16le_dupz(disk_entry->stream_name,
648                                                               cur_entry->stream_name_nbytes);
649                         if (cur_entry->stream_name == NULL)
650                                 goto out_of_memory;
651                 } else {
652                         /* Mark inode as having weird stream entries.  */
653                         inode->i_canonical_streams = 0;
654                 }
655
656                 /* It's expected that the size of every ADS entry is a multiple
657                  * of 8.  However, to be safe, I'm allowing the possibility of
658                  * an ADS entry at the very end of the metadata resource ending
659                  * un-aligned.  So although we still need to increment the input
660                  * pointer by @length to reach the next ADS entry, it's possible
661                  * that less than @length is actually remaining in the metadata
662                  * resource. We should set the remaining bytes to 0 if this
663                  * happens. */
664                 length = (length + 7) & ~(u64)7;
665                 p += length;
666                 if (nbytes_remaining < length)
667                         nbytes_remaining = 0;
668                 else
669                         nbytes_remaining -= length;
670         }
671         inode->i_ads_entries = ads_entries;
672         inode->i_next_stream_id = inode->i_num_ads + 1;
673         ret = 0;
674         goto out;
675 out_of_memory:
676         ret = WIMLIB_ERR_NOMEM;
677         goto out_free_ads_entries;
678 out_invalid:
679         ERROR("An alternate data stream entry is invalid");
680         ret = WIMLIB_ERR_INVALID_METADATA_RESOURCE;
681 out_free_ads_entries:
682         if (ads_entries) {
683                 for (u16 i = 0; i < num_ads; i++)
684                         destroy_ads_entry(&ads_entries[i]);
685                 FREE(ads_entries);
686         }
687 out:
688         return ret;
689 }
690
691 /*
692  * Verify a WIM inode:
693  *
694  * - Check to make sure the security ID is valid
695  * - Check to make sure there is at most one unnamed stream
696  * - Check to make sure there is at most one DOS name.
697  *
698  * Return values:
699  *      WIMLIB_ERR_SUCCESS (0)
700  */
701 int
702 verify_inode(struct wim_inode *inode, const struct wim_security_data *sd)
703 {
704         /* Check the security ID.  -1 is valid and means "no security
705          * descriptor".  Anything else has to be a valid index into the WIM
706          * image's security descriptors table. */
707         if (inode->i_security_id < -1 ||
708             (inode->i_security_id >= 0 &&
709              inode->i_security_id >= sd->num_entries))
710         {
711                 WARNING("\"%"TS"\" has an invalid security ID (%d)",
712                         inode_first_full_path(inode), inode->i_security_id);
713                 inode->i_security_id = -1;
714         }
715
716         /* Make sure there is only one unnamed data stream. */
717         unsigned num_unnamed_streams = 0;
718         for (unsigned i = 0; i <= inode->i_num_ads; i++) {
719                 const u8 *hash;
720                 hash = inode_stream_hash(inode, i);
721                 if (inode_stream_name_nbytes(inode, i) == 0 && !is_zero_hash(hash))
722                         num_unnamed_streams++;
723         }
724         if (num_unnamed_streams > 1) {
725                 WARNING("\"%"TS"\" has multiple (%u) un-named streams",
726                         inode_first_full_path(inode), num_unnamed_streams);
727         }
728
729         return 0;
730 }
731
732 void
733 inode_ref_streams(struct wim_inode *inode)
734 {
735         for (unsigned i = 0; i <= inode->i_num_ads; i++) {
736                 struct wim_lookup_table_entry *lte;
737                 lte = inode_stream_lte_resolved(inode, i);
738                 if (lte)
739                         lte->refcnt++;
740         }
741 }
742
743 void
744 inode_unref_streams(struct wim_inode *inode,
745                     struct wim_lookup_table *lookup_table)
746 {
747         struct wim_lookup_table_entry *lte;
748         unsigned i;
749
750         for (i = 0; i <= inode->i_num_ads; i++) {
751                 lte = inode_stream_lte(inode, i, lookup_table);
752                 if (lte)
753                         lte_decrement_refcnt(lte, lookup_table);
754         }
755 }
756
757 int
758 init_inode_table(struct wim_inode_table *table, size_t capacity)
759 {
760         table->array = CALLOC(capacity, sizeof(table->array[0]));
761         if (table->array == NULL) {
762                 ERROR("Cannot initalize inode table: out of memory");
763                 return WIMLIB_ERR_NOMEM;
764         }
765         table->num_entries  = 0;
766         table->capacity  = capacity;
767         INIT_LIST_HEAD(&table->extra_inodes);
768         return 0;
769 }
770
771 void
772 destroy_inode_table(struct wim_inode_table *table)
773 {
774         FREE(table->array);
775 }
776
777 static struct wim_inode *
778 inode_table_get_inode(struct wim_inode_table *table, u64 ino, u64 devno)
779 {
780         u64 hash = hash_u64(hash_u64(ino) + hash_u64(devno));
781         size_t pos = hash % table->capacity;
782         struct wim_inode *inode;
783         struct hlist_node *cur;
784
785         hlist_for_each_entry(inode, cur, &table->array[pos], i_hlist) {
786                 if (inode->i_ino == ino && inode->i_devno == devno) {
787                         DEBUG("Using existing inode {devno=%"PRIu64", ino=%"PRIu64"}",
788                               devno, ino);
789                         inode->i_nlink++;
790                         return inode;
791                 }
792         }
793         inode = new_timeless_inode();
794         if (inode) {
795                 inode->i_ino = ino;
796                 inode->i_devno = devno;
797                 hlist_add_head(&inode->i_hlist, &table->array[pos]);
798                 table->num_entries++;
799         }
800         return inode;
801 }
802
803
804 /* Given a directory entry with the name @name for the file with the inode
805  * number @ino and device number @devno, create a new WIM dentry with an
806  * associated inode, where the inode is shared if an inode with the same @ino
807  * and @devno has already been created.  On success, the new WIM dentry is
808  * written to *dentry_ret, and its inode has i_nlink > 1 if a previously
809  * existing inode was used.
810  */
811 int
812 inode_table_new_dentry(struct wim_inode_table *table, const tchar *name,
813                        u64 ino, u64 devno, bool noshare,
814                        struct wim_dentry **dentry_ret)
815 {
816         struct wim_dentry *dentry;
817         struct wim_inode *inode;
818         int ret;
819
820         if (noshare) {
821                 /* File that cannot be hardlinked--- Return a new inode with its
822                  * inode and device numbers left at 0. */
823                 ret = new_dentry_with_timeless_inode(name, &dentry);
824                 if (ret)
825                         return ret;
826                 list_add_tail(&dentry->d_inode->i_list, &table->extra_inodes);
827         } else {
828                 /* File that can be hardlinked--- search the table for an
829                  * existing inode matching the inode number and device;
830                  * otherwise create a new inode. */
831                 ret = new_dentry(name, &dentry);
832                 if (ret)
833                         return ret;
834                 inode = inode_table_get_inode(table, ino, devno);
835                 if (!inode) {
836                         free_dentry(dentry);
837                         return WIMLIB_ERR_NOMEM;
838                 }
839                 /* If using an existing inode, we need to gain a reference to
840                  * each of its streams. */
841                 if (inode->i_nlink > 1)
842                         inode_ref_streams(inode);
843                 dentry->d_inode = inode;
844                 inode_add_dentry(dentry, inode);
845         }
846         *dentry_ret = dentry;
847         return 0;
848 }
849
850
851
852 /* Assign consecutive inode numbers to a new set of inodes from the inode table,
853  * and append the inodes to a single list @head that contains the inodes already
854  * existing in the WIM image.  */
855 void
856 inode_table_prepare_inode_list(struct wim_inode_table *table,
857                                struct list_head *head)
858 {
859         struct wim_inode *inode, *tmp_inode;
860         struct hlist_node *cur, *tmp;
861         u64 cur_ino = 1;
862
863         /* Re-assign inode numbers in the existing list to avoid duplicates. */
864         list_for_each_entry(inode, head, i_list)
865                 inode->i_ino = cur_ino++;
866
867         /* Assign inode numbers to the new inodes and move them to the image's
868          * inode list. */
869         for (size_t i = 0; i < table->capacity; i++) {
870                 hlist_for_each_entry_safe(inode, cur, tmp, &table->array[i], i_hlist)
871                 {
872                         inode->i_ino = cur_ino++;
873                         inode->i_devno = 0;
874                         list_add_tail(&inode->i_list, head);
875                 }
876                 INIT_HLIST_HEAD(&table->array[i]);
877         }
878         list_for_each_entry_safe(inode, tmp_inode, &table->extra_inodes, i_list)
879         {
880                 inode->i_ino = cur_ino++;
881                 inode->i_devno = 0;
882                 list_add_tail(&inode->i_list, head);
883         }
884         INIT_LIST_HEAD(&table->extra_inodes);
885         table->num_entries = 0;
886 }