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