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