]> wimlib.net Git - wimlib/blob - src/inode.c
Remove printf extension
[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 bool
330 inode_has_unix_data(const struct wim_inode *inode)
331 {
332         for (u16 i = 0; i < inode->i_num_ads; i++)
333                 if (ads_entry_is_unix_data(&inode->i_ads_entries[i]))
334                         return true;
335         return false;
336 }
337
338 #ifndef __WIN32__
339 int
340 inode_get_unix_data(const struct wim_inode *inode,
341                     struct wimlib_unix_data *unix_data,
342                     u16 *stream_idx_ret)
343 {
344         const struct wim_ads_entry *ads_entry;
345         const struct wim_lookup_table_entry *lte;
346         size_t size;
347         int ret;
348
349         wimlib_assert(inode->i_resolved);
350
351         ads_entry = inode_get_ads_entry((struct wim_inode*)inode,
352                                         WIMLIB_UNIX_DATA_TAG, NULL);
353         if (ads_entry == NULL)
354                 return NO_UNIX_DATA;
355
356         if (stream_idx_ret)
357                 *stream_idx_ret = ads_entry - inode->i_ads_entries;
358
359         lte = ads_entry->lte;
360         if (lte == NULL)
361                 return NO_UNIX_DATA;
362
363         size = lte->size;
364         if (size != sizeof(struct wimlib_unix_data))
365                 return BAD_UNIX_DATA;
366
367         ret = read_full_stream_into_buf(lte, unix_data);
368         if (ret)
369                 return ret;
370
371         if (unix_data->version != 0)
372                 return BAD_UNIX_DATA;
373         return 0;
374 }
375
376 int
377 inode_set_unix_data(struct wim_inode *inode, u16 uid, u16 gid, u16 mode,
378                     struct wim_lookup_table *lookup_table, int which)
379 {
380         struct wimlib_unix_data unix_data;
381         int ret;
382         bool have_good_unix_data = false;
383         bool have_unix_data = false;
384         u16 stream_idx;
385
386         if (!(which & UNIX_DATA_CREATE)) {
387                 ret = inode_get_unix_data(inode, &unix_data, &stream_idx);
388                 if (ret == 0 || ret == BAD_UNIX_DATA || ret > 0)
389                         have_unix_data = true;
390                 if (ret == 0)
391                         have_good_unix_data = true;
392         }
393         unix_data.version = 0;
394         if (which & UNIX_DATA_UID || !have_good_unix_data)
395                 unix_data.uid = uid;
396         if (which & UNIX_DATA_GID || !have_good_unix_data)
397                 unix_data.gid = gid;
398         if (which & UNIX_DATA_MODE || !have_good_unix_data)
399                 unix_data.mode = mode;
400         ret = inode_add_ads_with_data(inode, WIMLIB_UNIX_DATA_TAG,
401                                       &unix_data,
402                                       sizeof(struct wimlib_unix_data),
403                                       lookup_table);
404         if (ret == 0 && have_unix_data)
405                 inode_remove_ads(inode, stream_idx, lookup_table);
406         return ret;
407 }
408 #endif /* __WIN32__  */
409
410 /*
411  * Resolve an inode's lookup table entries.
412  *
413  * This replaces the SHA1 hash fields (which are used to lookup an entry in the
414  * lookup table) with pointers directly to the lookup table entries.
415  *
416  * If @force is %false:
417  *      If any needed SHA1 message digests are not found in the lookup table,
418  *      WIMLIB_ERR_RESOURCE_NOT_FOUND is returned and the inode is left
419  *      unmodified.
420  * If @force is %true:
421  *      If any needed SHA1 message digests are not found in the lookup table,
422  *      new entries are allocated and inserted into the lookup table.
423  */
424 int
425 inode_resolve_streams(struct wim_inode *inode, struct wim_lookup_table *table,
426                       bool force)
427 {
428         const u8 *hash;
429
430         if (!inode->i_resolved) {
431                 struct wim_lookup_table_entry *lte, *ads_lte;
432
433                 /* Resolve the default file stream */
434                 lte = NULL;
435                 hash = inode->i_hash;
436                 if (!is_zero_hash(hash)) {
437                         lte = lookup_stream(table, hash);
438                         if (!lte) {
439                                 if (force) {
440                                         lte = new_lookup_table_entry();
441                                         if (!lte)
442                                                 return WIMLIB_ERR_NOMEM;
443                                         copy_hash(lte->hash, hash);
444                                         lookup_table_insert(table, lte);
445                                 } else {
446                                         goto stream_not_found;
447                                 }
448                         }
449                 }
450
451                 /* Resolve the alternate data streams */
452                 struct wim_lookup_table_entry *ads_ltes[inode->i_num_ads];
453                 for (u16 i = 0; i < inode->i_num_ads; i++) {
454                         struct wim_ads_entry *cur_entry;
455
456                         ads_lte = NULL;
457                         cur_entry = &inode->i_ads_entries[i];
458                         hash = cur_entry->hash;
459                         if (!is_zero_hash(hash)) {
460                                 ads_lte = lookup_stream(table, hash);
461                                 if (!ads_lte) {
462                                         if (force) {
463                                                 ads_lte = new_lookup_table_entry();
464                                                 if (!ads_lte)
465                                                         return WIMLIB_ERR_NOMEM;
466                                                 copy_hash(ads_lte->hash, hash);
467                                                 lookup_table_insert(table, ads_lte);
468                                         } else {
469                                                 goto stream_not_found;
470                                         }
471                                 }
472                         }
473                         ads_ltes[i] = ads_lte;
474                 }
475                 inode->i_lte = lte;
476                 for (u16 i = 0; i < inode->i_num_ads; i++)
477                         inode->i_ads_entries[i].lte = ads_ltes[i];
478                 inode->i_resolved = 1;
479         }
480         return 0;
481
482 stream_not_found:
483         return stream_not_found_error(inode, hash);
484 }
485
486 void
487 inode_unresolve_streams(struct wim_inode *inode)
488 {
489         if (inode->i_resolved) {
490                 if (inode->i_lte)
491                         copy_hash(inode->i_hash, inode->i_lte->hash);
492                 else
493                         zero_out_hash(inode->i_hash);
494
495                 for (u16 i = 0; i < inode->i_num_ads; i++) {
496                         if (inode->i_ads_entries[i].lte)
497                                 copy_hash(inode->i_ads_entries[i].hash,
498                                           inode->i_ads_entries[i].lte->hash);
499                         else
500                                 zero_out_hash(inode->i_ads_entries[i].hash);
501                 }
502                 inode->i_resolved = 0;
503         }
504 }
505
506 /*
507  * Returns the lookup table entry for stream @stream_idx of the inode, where
508  * stream_idx = 0 means the default un-named file stream, and stream_idx >= 1
509  * corresponds to an alternate data stream.
510  *
511  * This works for both resolved and un-resolved inodes.
512  */
513 struct wim_lookup_table_entry *
514 inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
515                  const struct wim_lookup_table *table)
516 {
517         if (inode->i_resolved)
518                 return inode_stream_lte_resolved(inode, stream_idx);
519         else
520                 return inode_stream_lte_unresolved(inode, stream_idx, table);
521 }
522
523 struct wim_lookup_table_entry *
524 inode_unnamed_stream_resolved(const struct wim_inode *inode, u16 *stream_idx_ret)
525 {
526         wimlib_assert(inode->i_resolved);
527         for (unsigned i = 0; i <= inode->i_num_ads; i++) {
528                 if (inode_stream_name_nbytes(inode, i) == 0 &&
529                     !is_zero_hash(inode_stream_hash_resolved(inode, i)))
530                 {
531                         *stream_idx_ret = i;
532                         return inode_stream_lte_resolved(inode, i);
533                 }
534         }
535         *stream_idx_ret = 0;
536         return NULL;
537 }
538
539 struct wim_lookup_table_entry *
540 inode_unnamed_lte_resolved(const struct wim_inode *inode)
541 {
542         u16 stream_idx;
543         return inode_unnamed_stream_resolved(inode, &stream_idx);
544 }
545
546 struct wim_lookup_table_entry *
547 inode_unnamed_lte_unresolved(const struct wim_inode *inode,
548                              const struct wim_lookup_table *table)
549 {
550         wimlib_assert(!inode->i_resolved);
551         for (unsigned i = 0; i <= inode->i_num_ads; i++) {
552                 if (inode_stream_name_nbytes(inode, i) == 0 &&
553                     !is_zero_hash(inode_stream_hash_unresolved(inode, i)))
554                 {
555                         return inode_stream_lte_unresolved(inode, i, table);
556                 }
557         }
558         return NULL;
559 }
560
561 /* Return the lookup table entry for the unnamed data stream of an inode, or
562  * NULL if there is none.
563  *
564  * You'd think this would be easier than it actually is, since the unnamed data
565  * stream should be the one referenced from the inode itself.  Alas, if there
566  * are named data streams, Microsoft's "imagex.exe" program will put the unnamed
567  * data stream in one of the alternate data streams instead of inside the WIM
568  * dentry itself.  So we need to check the alternate data streams too.
569  *
570  * Also, note that a dentry may appear to have more than one unnamed stream, but
571  * if the SHA1 message digest is all 0's then the corresponding stream does not
572  * really "count" (this is the case for the inode's own file stream when the
573  * file stream that should be there is actually in one of the alternate stream
574  * entries.).  This is despite the fact that we may need to extract such a
575  * missing entry as an empty file or empty named data stream.
576  */
577 struct wim_lookup_table_entry *
578 inode_unnamed_lte(const struct wim_inode *inode,
579                   const struct wim_lookup_table *table)
580 {
581         if (inode->i_resolved)
582                 return inode_unnamed_lte_resolved(inode);
583         else
584                 return inode_unnamed_lte_unresolved(inode, table);
585 }
586
587 /* Returns the SHA1 message digest of the unnamed data stream of a WIM inode, or
588  * 'zero_hash' if the unnamed data stream is missing has all zeroes in its SHA1
589  * message digest field.  */
590 const u8 *
591 inode_unnamed_stream_hash(const struct wim_inode *inode)
592 {
593         const u8 *hash;
594
595         for (unsigned i = 0; i <= inode->i_num_ads; i++) {
596                 if (inode_stream_name_nbytes(inode, i) == 0) {
597                         hash = inode_stream_hash(inode, i);
598                         if (!is_zero_hash(hash))
599                                 return hash;
600                 }
601         }
602         return zero_hash;
603 }
604
605 /* Given an unhashed stream, get the pointer to it in an inode.
606  * As this is only for unhashed streams, there can only be one such pointer.  */
607 struct wim_lookup_table_entry **
608 retrieve_lte_pointer(struct wim_lookup_table_entry *lte)
609 {
610         wimlib_assert(lte->unhashed);
611         struct wim_inode *inode = lte->back_inode;
612         u32 stream_id = lte->back_stream_id;
613         if (stream_id == 0)
614                 return &inode->i_lte;
615         else
616                 for (u16 i = 0; i < inode->i_num_ads; i++)
617                         if (inode->i_ads_entries[i].stream_id == stream_id)
618                                 return &inode->i_ads_entries[i].lte;
619         wimlib_assert(0);
620         return NULL;
621 }
622
623 int
624 stream_not_found_error(const struct wim_inode *inode, const u8 *hash)
625 {
626         if (wimlib_print_errors) {
627                 ERROR("\"%"TS"\": stream not found", inode_first_full_path(inode));
628                 tfprintf(stderr, T("        SHA-1 message digest of missing stream:\n        "));
629                 print_hash(hash, stderr);
630                 tputc(T('\n'), stderr);
631         }
632         return WIMLIB_ERR_RESOURCE_NOT_FOUND;
633 }
634
635 /*
636  * Reads the alternate data stream entries of a WIM dentry.
637  *
638  * @p:
639  *      Pointer to buffer that starts with the first alternate stream entry.
640  *
641  * @inode:
642  *      Inode to load the alternate data streams into.  @inode->i_num_ads must
643  *      have been set to the number of alternate data streams that are expected.
644  *
645  * @remaining_size:
646  *      Number of bytes of data remaining in the buffer pointed to by @p.
647  *
648  * On success, inode->i_ads_entries is set to an array of `struct
649  * wim_ads_entry's of length inode->i_num_ads.  On failure, @inode is not
650  * modified.
651  *
652  * Return values:
653  *      WIMLIB_ERR_SUCCESS (0)
654  *      WIMLIB_ERR_INVALID_METADATA_RESOURCE
655  *      WIMLIB_ERR_NOMEM
656  */
657 int
658 read_ads_entries(const u8 * restrict p, struct wim_inode * restrict inode,
659                  size_t nbytes_remaining)
660 {
661         u16 num_ads;
662         struct wim_ads_entry *ads_entries;
663         int ret;
664
665         BUILD_BUG_ON(sizeof(struct wim_ads_entry_on_disk) != WIM_ADS_ENTRY_DISK_SIZE);
666
667         /* Allocate an array for our in-memory representation of the alternate
668          * data stream entries. */
669         num_ads = inode->i_num_ads;
670         ads_entries = CALLOC(num_ads, sizeof(inode->i_ads_entries[0]));
671         if (ads_entries == NULL)
672                 goto out_of_memory;
673
674         /* Read the entries into our newly allocated buffer. */
675         for (u16 i = 0; i < num_ads; i++) {
676                 u64 length;
677                 struct wim_ads_entry *cur_entry;
678                 const struct wim_ads_entry_on_disk *disk_entry =
679                         (const struct wim_ads_entry_on_disk*)p;
680
681                 cur_entry = &ads_entries[i];
682                 ads_entries[i].stream_id = i + 1;
683
684                 /* Do we have at least the size of the fixed-length data we know
685                  * need? */
686                 if (nbytes_remaining < sizeof(struct wim_ads_entry_on_disk))
687                         goto out_invalid;
688
689                 /* Read the length field */
690                 length = le64_to_cpu(disk_entry->length);
691
692                 /* Make sure the length field is neither so small it doesn't
693                  * include all the fixed-length data nor so large it overflows
694                  * the metadata resource buffer. */
695                 if (length < sizeof(struct wim_ads_entry_on_disk) ||
696                     length > nbytes_remaining)
697                         goto out_invalid;
698
699                 /* Read the rest of the fixed-length data. */
700
701                 cur_entry->reserved = le64_to_cpu(disk_entry->reserved);
702                 copy_hash(cur_entry->hash, disk_entry->hash);
703                 cur_entry->stream_name_nbytes = le16_to_cpu(disk_entry->stream_name_nbytes);
704
705                 /* If stream_name_nbytes != 0, this is a named stream.
706                  * Otherwise this is an unnamed stream, or in some cases (bugs
707                  * in Microsoft's software I guess) a meaningless entry
708                  * distinguished from the real unnamed stream entry, if any, by
709                  * the fact that the real unnamed stream entry has a nonzero
710                  * hash field. */
711                 if (cur_entry->stream_name_nbytes) {
712                         /* The name is encoded in UTF16-LE, which uses 2-byte
713                          * coding units, so the length of the name had better be
714                          * an even number of bytes... */
715                         if (cur_entry->stream_name_nbytes & 1)
716                                 goto out_invalid;
717
718                         /* Add the length of the stream name to get the length
719                          * we actually need to read.  Make sure this isn't more
720                          * than the specified length of the entry. */
721                         if (sizeof(struct wim_ads_entry_on_disk) +
722                             cur_entry->stream_name_nbytes > length)
723                                 goto out_invalid;
724
725                         cur_entry->stream_name = utf16le_dupz(disk_entry->stream_name,
726                                                               cur_entry->stream_name_nbytes);
727                         if (cur_entry->stream_name == NULL)
728                                 goto out_of_memory;
729                 } else {
730                         /* Mark inode as having weird stream entries.  */
731                         inode->i_canonical_streams = 0;
732                 }
733
734                 /* It's expected that the size of every ADS entry is a multiple
735                  * of 8.  However, to be safe, I'm allowing the possibility of
736                  * an ADS entry at the very end of the metadata resource ending
737                  * un-aligned.  So although we still need to increment the input
738                  * pointer by @length to reach the next ADS entry, it's possible
739                  * that less than @length is actually remaining in the metadata
740                  * resource. We should set the remaining bytes to 0 if this
741                  * happens. */
742                 length = (length + 7) & ~(u64)7;
743                 p += length;
744                 if (nbytes_remaining < length)
745                         nbytes_remaining = 0;
746                 else
747                         nbytes_remaining -= length;
748         }
749         inode->i_ads_entries = ads_entries;
750         inode->i_next_stream_id = inode->i_num_ads + 1;
751         ret = 0;
752         goto out;
753 out_of_memory:
754         ret = WIMLIB_ERR_NOMEM;
755         goto out_free_ads_entries;
756 out_invalid:
757         ERROR("An alternate data stream entry is invalid");
758         ret = WIMLIB_ERR_INVALID_METADATA_RESOURCE;
759 out_free_ads_entries:
760         if (ads_entries) {
761                 for (u16 i = 0; i < num_ads; i++)
762                         destroy_ads_entry(&ads_entries[i]);
763                 FREE(ads_entries);
764         }
765 out:
766         return ret;
767 }
768
769 /*
770  * Verify a WIM inode:
771  *
772  * - Check to make sure the security ID is valid
773  * - Check to make sure there is at most one unnamed stream
774  * - Check to make sure there is at most one DOS name.
775  *
776  * Return values:
777  *      WIMLIB_ERR_SUCCESS (0)
778  */
779 int
780 verify_inode(struct wim_inode *inode, const struct wim_security_data *sd)
781 {
782         /* Check the security ID.  -1 is valid and means "no security
783          * descriptor".  Anything else has to be a valid index into the WIM
784          * image's security descriptors table. */
785         if (inode->i_security_id < -1 ||
786             (inode->i_security_id >= 0 &&
787              inode->i_security_id >= sd->num_entries))
788         {
789                 WARNING("\"%"TS"\" has an invalid security ID (%d)",
790                         inode_first_full_path(inode), inode->i_security_id);
791                 inode->i_security_id = -1;
792         }
793
794         /* Make sure there is only one unnamed data stream. */
795         unsigned num_unnamed_streams = 0;
796         for (unsigned i = 0; i <= inode->i_num_ads; i++) {
797                 const u8 *hash;
798                 hash = inode_stream_hash(inode, i);
799                 if (inode_stream_name_nbytes(inode, i) == 0 && !is_zero_hash(hash))
800                         num_unnamed_streams++;
801         }
802         if (num_unnamed_streams > 1) {
803                 WARNING("\"%"TS"\" has multiple (%u) un-named streams",
804                         inode_first_full_path(inode), num_unnamed_streams);
805         }
806
807         return 0;
808 }
809
810 void
811 inode_ref_streams(struct wim_inode *inode)
812 {
813         for (unsigned i = 0; i <= inode->i_num_ads; i++) {
814                 struct wim_lookup_table_entry *lte;
815                 lte = inode_stream_lte_resolved(inode, i);
816                 if (lte)
817                         lte->refcnt++;
818         }
819 }
820
821 void
822 inode_unref_streams(struct wim_inode *inode,
823                     struct wim_lookup_table *lookup_table)
824 {
825         struct wim_lookup_table_entry *lte;
826         unsigned i;
827
828         for (i = 0; i <= inode->i_num_ads; i++) {
829                 lte = inode_stream_lte(inode, i, lookup_table);
830                 if (lte)
831                         lte_decrement_refcnt(lte, lookup_table);
832         }
833 }
834
835 int
836 init_inode_table(struct wim_inode_table *table, size_t capacity)
837 {
838         table->array = CALLOC(capacity, sizeof(table->array[0]));
839         if (table->array == NULL) {
840                 ERROR("Cannot initalize inode table: out of memory");
841                 return WIMLIB_ERR_NOMEM;
842         }
843         table->num_entries  = 0;
844         table->capacity  = capacity;
845         INIT_LIST_HEAD(&table->extra_inodes);
846         return 0;
847 }
848
849 void
850 destroy_inode_table(struct wim_inode_table *table)
851 {
852         FREE(table->array);
853 }
854
855 static struct wim_inode *
856 inode_table_get_inode(struct wim_inode_table *table, u64 ino, u64 devno)
857 {
858         u64 hash = hash_u64(hash_u64(ino) + hash_u64(devno));
859         size_t pos = hash % table->capacity;
860         struct wim_inode *inode;
861         struct hlist_node *cur;
862
863         hlist_for_each_entry(inode, cur, &table->array[pos], i_hlist) {
864                 if (inode->i_ino == ino && inode->i_devno == devno) {
865                         DEBUG("Using existing inode {devno=%"PRIu64", ino=%"PRIu64"}",
866                               devno, ino);
867                         inode->i_nlink++;
868                         return inode;
869                 }
870         }
871         inode = new_timeless_inode();
872         if (inode) {
873                 inode->i_ino = ino;
874                 inode->i_devno = devno;
875                 hlist_add_head(&inode->i_hlist, &table->array[pos]);
876                 table->num_entries++;
877         }
878         return inode;
879 }
880
881
882 /* Given a directory entry with the name @name for the file with the inode
883  * number @ino and device number @devno, create a new WIM dentry with an
884  * associated inode, where the inode is shared if an inode with the same @ino
885  * and @devno has already been created.  On success, the new WIM dentry is
886  * written to *dentry_ret, and its inode has i_nlink > 1 if a previously
887  * existing inode was used.
888  */
889 int
890 inode_table_new_dentry(struct wim_inode_table *table, const tchar *name,
891                        u64 ino, u64 devno, bool noshare,
892                        struct wim_dentry **dentry_ret)
893 {
894         struct wim_dentry *dentry;
895         struct wim_inode *inode;
896         int ret;
897
898         if (noshare) {
899                 /* File that cannot be hardlinked--- Return a new inode with its
900                  * inode and device numbers left at 0. */
901                 ret = new_dentry_with_timeless_inode(name, &dentry);
902                 if (ret)
903                         return ret;
904                 list_add_tail(&dentry->d_inode->i_list, &table->extra_inodes);
905         } else {
906                 /* File that can be hardlinked--- search the table for an
907                  * existing inode matching the inode number and device;
908                  * otherwise create a new inode. */
909                 ret = new_dentry(name, &dentry);
910                 if (ret)
911                         return ret;
912                 inode = inode_table_get_inode(table, ino, devno);
913                 if (!inode) {
914                         free_dentry(dentry);
915                         return WIMLIB_ERR_NOMEM;
916                 }
917                 /* If using an existing inode, we need to gain a reference to
918                  * each of its streams. */
919                 if (inode->i_nlink > 1)
920                         inode_ref_streams(inode);
921                 dentry->d_inode = inode;
922                 inode_add_dentry(dentry, inode);
923         }
924         *dentry_ret = dentry;
925         return 0;
926 }
927
928
929
930 /* Assign consecutive inode numbers to a new set of inodes from the inode table,
931  * and append the inodes to a single list @head that contains the inodes already
932  * existing in the WIM image.  */
933 void
934 inode_table_prepare_inode_list(struct wim_inode_table *table,
935                                struct list_head *head)
936 {
937         struct wim_inode *inode, *tmp_inode;
938         struct hlist_node *cur, *tmp;
939         u64 cur_ino = 1;
940
941         /* Re-assign inode numbers in the existing list to avoid duplicates. */
942         list_for_each_entry(inode, head, i_list)
943                 inode->i_ino = cur_ino++;
944
945         /* Assign inode numbers to the new inodes and move them to the image's
946          * inode list. */
947         for (size_t i = 0; i < table->capacity; i++) {
948                 hlist_for_each_entry_safe(inode, cur, tmp, &table->array[i], i_hlist)
949                 {
950                         inode->i_ino = cur_ino++;
951                         inode->i_devno = 0;
952                         list_add_tail(&inode->i_list, head);
953                 }
954                 INIT_HLIST_HEAD(&table->array[i]);
955         }
956         list_for_each_entry_safe(inode, tmp_inode, &table->extra_inodes, i_list)
957         {
958                 inode->i_ino = cur_ino++;
959                 inode->i_devno = 0;
960                 list_add_tail(&inode->i_list, head);
961         }
962         INIT_LIST_HEAD(&table->extra_inodes);
963         table->num_entries = 0;
964 }