]> wimlib.net Git - wimlib/blob - src/inode.c
wimlib: Allow custom error file
[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(wimlib_error_file,
551                          T("        SHA-1 message digest of missing stream:\n        "));
552                 print_hash(hash, wimlib_error_file);
553                 tputc(T('\n'), wimlib_error_file);
554         }
555         return WIMLIB_ERR_RESOURCE_NOT_FOUND;
556 }
557
558 /*
559  * Reads the alternate data stream entries of a WIM dentry.
560  *
561  * @p:
562  *      Pointer to buffer that starts with the first alternate stream entry.
563  *
564  * @inode:
565  *      Inode to load the alternate data streams into.  @inode->i_num_ads must
566  *      have been set to the number of alternate data streams that are expected.
567  *
568  * @nbytes_remaining_p:
569  *      Number of bytes of data remaining in the buffer pointed to by @p.
570  *      On success this will be updated to point just past the ADS entries.
571  *
572  * On success, inode->i_ads_entries is set to an array of `struct
573  * wim_ads_entry's of length inode->i_num_ads.  On failure, @inode is not
574  * modified.
575  *
576  * Return values:
577  *      WIMLIB_ERR_SUCCESS (0)
578  *      WIMLIB_ERR_INVALID_METADATA_RESOURCE
579  *      WIMLIB_ERR_NOMEM
580  */
581 int
582 read_ads_entries(const u8 * restrict p, struct wim_inode * restrict inode,
583                  size_t *nbytes_remaining_p)
584 {
585         size_t nbytes_remaining = *nbytes_remaining_p;
586         u16 num_ads;
587         struct wim_ads_entry *ads_entries;
588         int ret;
589
590         BUILD_BUG_ON(sizeof(struct wim_ads_entry_on_disk) != WIM_ADS_ENTRY_DISK_SIZE);
591
592         /* Allocate an array for our in-memory representation of the alternate
593          * data stream entries. */
594         num_ads = inode->i_num_ads;
595         ads_entries = CALLOC(num_ads, sizeof(inode->i_ads_entries[0]));
596         if (ads_entries == NULL)
597                 goto out_of_memory;
598
599         /* Read the entries into our newly allocated buffer. */
600         for (u16 i = 0; i < num_ads; i++) {
601                 u64 length;
602                 struct wim_ads_entry *cur_entry;
603                 const struct wim_ads_entry_on_disk *disk_entry =
604                         (const struct wim_ads_entry_on_disk*)p;
605
606                 cur_entry = &ads_entries[i];
607                 ads_entries[i].stream_id = i + 1;
608
609                 /* Do we have at least the size of the fixed-length data we know
610                  * need? */
611                 if (nbytes_remaining < sizeof(struct wim_ads_entry_on_disk))
612                         goto out_invalid;
613
614                 /* Read the length field */
615                 length = le64_to_cpu(disk_entry->length);
616
617                 /* Make sure the length field is neither so small it doesn't
618                  * include all the fixed-length data nor so large it overflows
619                  * the metadata resource buffer. */
620                 if (length < sizeof(struct wim_ads_entry_on_disk) ||
621                     length > nbytes_remaining)
622                         goto out_invalid;
623
624                 /* Read the rest of the fixed-length data. */
625
626                 cur_entry->reserved = le64_to_cpu(disk_entry->reserved);
627                 copy_hash(cur_entry->hash, disk_entry->hash);
628                 cur_entry->stream_name_nbytes = le16_to_cpu(disk_entry->stream_name_nbytes);
629
630                 /* If stream_name_nbytes != 0, this is a named stream.
631                  * Otherwise this is an unnamed stream, or in some cases (bugs
632                  * in Microsoft's software I guess) a meaningless entry
633                  * distinguished from the real unnamed stream entry, if any, by
634                  * the fact that the real unnamed stream entry has a nonzero
635                  * hash field. */
636                 if (cur_entry->stream_name_nbytes) {
637                         /* The name is encoded in UTF16-LE, which uses 2-byte
638                          * coding units, so the length of the name had better be
639                          * an even number of bytes... */
640                         if (cur_entry->stream_name_nbytes & 1)
641                                 goto out_invalid;
642
643                         /* Add the length of the stream name to get the length
644                          * we actually need to read.  Make sure this isn't more
645                          * than the specified length of the entry. */
646                         if (sizeof(struct wim_ads_entry_on_disk) +
647                             cur_entry->stream_name_nbytes > length)
648                                 goto out_invalid;
649
650                         cur_entry->stream_name = utf16le_dupz(disk_entry->stream_name,
651                                                               cur_entry->stream_name_nbytes);
652                         if (cur_entry->stream_name == NULL)
653                                 goto out_of_memory;
654                 } else {
655                         /* Mark inode as having weird stream entries.  */
656                         inode->i_canonical_streams = 0;
657                 }
658
659                 /* It's expected that the size of every ADS entry is a multiple
660                  * of 8.  However, to be safe, I'm allowing the possibility of
661                  * an ADS entry at the very end of the metadata resource ending
662                  * un-aligned.  So although we still need to increment the input
663                  * pointer by @length to reach the next ADS entry, it's possible
664                  * that less than @length is actually remaining in the metadata
665                  * resource. We should set the remaining bytes to 0 if this
666                  * happens. */
667                 length = (length + 7) & ~7;
668                 p += length;
669                 if (nbytes_remaining < length)
670                         nbytes_remaining = 0;
671                 else
672                         nbytes_remaining -= length;
673         }
674         inode->i_ads_entries = ads_entries;
675         inode->i_next_stream_id = inode->i_num_ads + 1;
676         *nbytes_remaining_p = nbytes_remaining;
677         ret = 0;
678         goto out;
679 out_of_memory:
680         ret = WIMLIB_ERR_NOMEM;
681         goto out_free_ads_entries;
682 out_invalid:
683         ERROR("An alternate data stream entry is invalid");
684         ret = WIMLIB_ERR_INVALID_METADATA_RESOURCE;
685 out_free_ads_entries:
686         if (ads_entries) {
687                 for (u16 i = 0; i < num_ads; i++)
688                         destroy_ads_entry(&ads_entries[i]);
689                 FREE(ads_entries);
690         }
691 out:
692         return ret;
693 }
694
695 /*
696  * Verify a WIM inode:
697  *
698  * - Check to make sure the security ID is valid
699  * - Check to make sure there is at most one unnamed stream
700  * - Check to make sure there is at most one DOS name.
701  *
702  * Return values:
703  *      WIMLIB_ERR_SUCCESS (0)
704  */
705 int
706 verify_inode(struct wim_inode *inode, const struct wim_security_data *sd)
707 {
708         /* Check the security ID.  -1 is valid and means "no security
709          * descriptor".  Anything else has to be a valid index into the WIM
710          * image's security descriptors table. */
711         if (inode->i_security_id < -1 ||
712             (inode->i_security_id >= 0 &&
713              inode->i_security_id >= sd->num_entries))
714         {
715                 WARNING("\"%"TS"\" has an invalid security ID (%d)",
716                         inode_first_full_path(inode), inode->i_security_id);
717                 inode->i_security_id = -1;
718         }
719
720         /* Make sure there is only one unnamed data stream. */
721         unsigned num_unnamed_streams = 0;
722         for (unsigned i = 0; i <= inode->i_num_ads; i++) {
723                 const u8 *hash;
724                 hash = inode_stream_hash(inode, i);
725                 if (inode_stream_name_nbytes(inode, i) == 0 && !is_zero_hash(hash))
726                         num_unnamed_streams++;
727         }
728         if (num_unnamed_streams > 1) {
729                 WARNING("\"%"TS"\" has multiple (%u) un-named streams",
730                         inode_first_full_path(inode), num_unnamed_streams);
731         }
732
733         return 0;
734 }
735
736 void
737 inode_ref_streams(struct wim_inode *inode)
738 {
739         for (unsigned i = 0; i <= inode->i_num_ads; i++) {
740                 struct wim_lookup_table_entry *lte;
741                 lte = inode_stream_lte_resolved(inode, i);
742                 if (lte)
743                         lte->refcnt++;
744         }
745 }
746
747 void
748 inode_unref_streams(struct wim_inode *inode,
749                     struct wim_lookup_table *lookup_table)
750 {
751         struct wim_lookup_table_entry *lte;
752         unsigned i;
753
754         for (i = 0; i <= inode->i_num_ads; i++) {
755                 lte = inode_stream_lte(inode, i, lookup_table);
756                 if (lte)
757                         lte_decrement_refcnt(lte, lookup_table);
758         }
759 }
760
761 int
762 init_inode_table(struct wim_inode_table *table, size_t capacity)
763 {
764         table->array = CALLOC(capacity, sizeof(table->array[0]));
765         if (table->array == NULL) {
766                 ERROR("Cannot initalize inode table: out of memory");
767                 return WIMLIB_ERR_NOMEM;
768         }
769         table->num_entries  = 0;
770         table->capacity  = capacity;
771         INIT_LIST_HEAD(&table->extra_inodes);
772         return 0;
773 }
774
775 void
776 destroy_inode_table(struct wim_inode_table *table)
777 {
778         FREE(table->array);
779 }
780
781 static struct wim_inode *
782 inode_table_get_inode(struct wim_inode_table *table, u64 ino, u64 devno)
783 {
784         u64 hash = hash_u64(hash_u64(ino) + hash_u64(devno));
785         size_t pos = hash % table->capacity;
786         struct wim_inode *inode;
787         struct hlist_node *cur;
788
789         hlist_for_each_entry(inode, cur, &table->array[pos], i_hlist) {
790                 if (inode->i_ino == ino && inode->i_devno == devno) {
791                         DEBUG("Using existing inode {devno=%"PRIu64", ino=%"PRIu64"}",
792                               devno, ino);
793                         inode->i_nlink++;
794                         return inode;
795                 }
796         }
797         inode = new_timeless_inode();
798         if (inode) {
799                 inode->i_ino = ino;
800                 inode->i_devno = devno;
801                 hlist_add_head(&inode->i_hlist, &table->array[pos]);
802                 table->num_entries++;
803         }
804         return inode;
805 }
806
807
808 /* Given a directory entry with the name @name for the file with the inode
809  * number @ino and device number @devno, create a new WIM dentry with an
810  * associated inode, where the inode is shared if an inode with the same @ino
811  * and @devno has already been created.  On success, the new WIM dentry is
812  * written to *dentry_ret, and its inode has i_nlink > 1 if a previously
813  * existing inode was used.
814  */
815 int
816 inode_table_new_dentry(struct wim_inode_table *table, const tchar *name,
817                        u64 ino, u64 devno, bool noshare,
818                        struct wim_dentry **dentry_ret)
819 {
820         struct wim_dentry *dentry;
821         struct wim_inode *inode;
822         int ret;
823
824         if (noshare) {
825                 /* File that cannot be hardlinked--- Return a new inode with its
826                  * inode and device numbers left at 0. */
827                 ret = new_dentry_with_timeless_inode(name, &dentry);
828                 if (ret)
829                         return ret;
830                 list_add_tail(&dentry->d_inode->i_list, &table->extra_inodes);
831         } else {
832                 /* File that can be hardlinked--- search the table for an
833                  * existing inode matching the inode number and device;
834                  * otherwise create a new inode. */
835                 ret = new_dentry(name, &dentry);
836                 if (ret)
837                         return ret;
838                 inode = inode_table_get_inode(table, ino, devno);
839                 if (!inode) {
840                         free_dentry(dentry);
841                         return WIMLIB_ERR_NOMEM;
842                 }
843                 /* If using an existing inode, we need to gain a reference to
844                  * each of its streams. */
845                 if (inode->i_nlink > 1)
846                         inode_ref_streams(inode);
847                 dentry->d_inode = inode;
848                 inode_add_dentry(dentry, inode);
849         }
850         *dentry_ret = dentry;
851         return 0;
852 }
853
854
855
856 /* Assign consecutive inode numbers to a new set of inodes from the inode table,
857  * and append the inodes to a single list @head that contains the inodes already
858  * existing in the WIM image.  */
859 void
860 inode_table_prepare_inode_list(struct wim_inode_table *table,
861                                struct list_head *head)
862 {
863         struct wim_inode *inode, *tmp_inode;
864         struct hlist_node *cur, *tmp;
865         u64 cur_ino = 1;
866
867         /* Re-assign inode numbers in the existing list to avoid duplicates. */
868         list_for_each_entry(inode, head, i_list)
869                 inode->i_ino = cur_ino++;
870
871         /* Assign inode numbers to the new inodes and move them to the image's
872          * inode list. */
873         for (size_t i = 0; i < table->capacity; i++) {
874                 hlist_for_each_entry_safe(inode, cur, tmp, &table->array[i], i_hlist)
875                 {
876                         inode->i_ino = cur_ino++;
877                         inode->i_devno = 0;
878                         list_add_tail(&inode->i_list, head);
879                 }
880                 INIT_HLIST_HEAD(&table->array[i]);
881         }
882         list_for_each_entry_safe(inode, tmp_inode, &table->extra_inodes, i_list)
883         {
884                 inode->i_ino = cur_ino++;
885                 inode->i_devno = 0;
886                 list_add_tail(&inode->i_list, head);
887         }
888         INIT_LIST_HEAD(&table->extra_inodes);
889         table->num_entries = 0;
890 }