]> wimlib.net Git - wimlib/blob - src/inode.c
Remove some dead assignments
[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  * @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 }