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