6 * Copyright (C) 2012, 2013 Eric Biggers
8 * This file is part of wimlib, a library for working with WIM files.
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)
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
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/.
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.
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"
42 /* Allocate a new inode. Set the timestamps to the current time. */
46 struct wim_inode *inode = new_timeless_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;
57 /* Allocate a new inode. Leave the timestamps zeroed out. */
59 new_timeless_inode(void)
61 struct wim_inode *inode = CALLOC(1, sizeof(struct wim_inode));
63 inode->i_security_id = -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);
74 /* Decrement link count on an inode. */
76 put_inode(struct wim_inode *inode)
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
85 if (inode->i_num_opened_fds == 0)
91 /* De-allocate memory for an alternate data stream entry. */
93 destroy_ads_entry(struct wim_ads_entry *ads_entry)
95 FREE(ads_entry->stream_name);
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. */
101 free_inode(struct wim_inode *inode)
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);
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);
118 /* Return %true iff the alternate data stream entry @entry has the UTF-16LE
119 * stream name @name that has length @name_nbytes bytes. */
121 ads_entry_has_name(const struct wim_ads_entry *entry,
122 const utf16lechar *name, size_t name_nbytes,
125 return 0 == cmp_utf16le_strings(name,
128 entry->stream_name_nbytes / 2,
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
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.
140 struct wim_ads_entry *
141 inode_get_ads_entry(struct wim_inode *inode, const tchar *stream_name,
145 const utf16lechar *stream_name_utf16le;
146 size_t stream_name_utf16le_nbytes;
148 struct wim_ads_entry *result;
150 if (inode->i_num_ads == 0)
153 if (stream_name[0] == T('\0'))
156 ret = tstr_get_utf16le_and_len(stream_name, &stream_name_utf16le,
157 &stream_name_utf16le_nbytes);
164 if (ads_entry_has_name(&inode->i_ads_entries[i],
166 stream_name_utf16le_nbytes,
167 default_ignore_case))
171 result = &inode->i_ads_entries[i];
174 } while (++i != inode->i_num_ads);
176 tstr_put_utf16le(stream_name_utf16le);
181 static struct wim_ads_entry *
182 do_inode_add_ads(struct wim_inode *inode,
183 utf16lechar *stream_name, size_t stream_name_nbytes)
186 struct wim_ads_entry *ads_entries;
187 struct wim_ads_entry *new_entry;
189 if (inode->i_num_ads >= 0xfffe) {
190 ERROR("Too many alternate data streams in one inode!");
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");
200 inode->i_ads_entries = ads_entries;
202 new_entry = &inode->i_ads_entries[num_ads - 1];
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;
212 struct wim_ads_entry *
213 inode_add_ads_utf16le(struct wim_inode *inode,
214 const utf16lechar *stream_name, size_t stream_name_nbytes)
216 utf16lechar *dup = NULL;
217 struct wim_ads_entry *result;
219 if (stream_name_nbytes) {
220 dup = utf16le_dupz(stream_name, stream_name_nbytes);
225 result = do_inode_add_ads(inode, dup, stream_name_nbytes);
232 * Add an alternate stream entry to a WIM inode. On success, returns a pointer
233 * to the new entry; on failure, returns NULL.
235 struct wim_ads_entry *
236 inode_add_ads(struct wim_inode *inode, const tchar *stream_name)
238 utf16lechar *stream_name_utf16le = NULL;
239 size_t stream_name_utf16le_nbytes = 0;
241 struct wim_ads_entry *result;
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);
252 result = do_inode_add_ads(inode, stream_name_utf16le,
253 stream_name_utf16le_nbytes);
255 FREE(stream_name_utf16le);
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)
264 struct wim_ads_entry *new_ads_entry;
266 wimlib_assert(inode->i_resolved);
268 new_ads_entry = inode_add_ads(inode, name);
269 if (new_ads_entry == NULL)
270 return WIMLIB_ERR_NOMEM;
272 new_ads_entry->lte = new_stream_from_data_buffer(value, size,
274 if (new_ads_entry->lte == NULL) {
275 inode_remove_ads(inode, new_ads_entry - inode->i_ads_entries,
277 return WIMLIB_ERR_NOMEM;
283 inode_has_named_stream(const struct wim_inode *inode)
285 for (u16 i = 0; i < inode->i_num_ads; i++)
286 if (ads_entry_is_named_stream(&inode->i_ads_entries[i]))
291 /* Set the unnamed stream of a WIM inode, given a data buffer containing the
292 * stream contents. */
294 inode_set_unnamed_stream(struct wim_inode *inode, const void *data, size_t len,
295 struct wim_lookup_table *lookup_table)
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;
304 /* Remove an alternate data stream from a WIM inode */
306 inode_remove_ads(struct wim_inode *inode, u16 idx,
307 struct wim_lookup_table *lookup_table)
309 struct wim_ads_entry *ads_entry;
310 struct wim_lookup_table_entry *lte;
312 wimlib_assert(idx < inode->i_num_ads);
313 wimlib_assert(inode->i_resolved);
315 ads_entry = &inode->i_ads_entries[idx];
317 lte = ads_entry->lte;
319 lte_decrement_refcnt(lte, lookup_table);
321 destroy_ads_entry(ads_entry);
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]));
330 inode_has_unix_data(const struct wim_inode *inode)
332 for (u16 i = 0; i < inode->i_num_ads; i++)
333 if (ads_entry_is_unix_data(&inode->i_ads_entries[i]))
340 inode_get_unix_data(const struct wim_inode *inode,
341 struct wimlib_unix_data *unix_data,
344 const struct wim_ads_entry *ads_entry;
345 const struct wim_lookup_table_entry *lte;
349 wimlib_assert(inode->i_resolved);
351 ads_entry = inode_get_ads_entry((struct wim_inode*)inode,
352 WIMLIB_UNIX_DATA_TAG, NULL);
353 if (ads_entry == NULL)
357 *stream_idx_ret = ads_entry - inode->i_ads_entries;
359 lte = ads_entry->lte;
364 if (size != sizeof(struct wimlib_unix_data))
365 return BAD_UNIX_DATA;
367 ret = read_full_stream_into_buf(lte, unix_data);
371 if (unix_data->version != 0)
372 return BAD_UNIX_DATA;
377 inode_set_unix_data(struct wim_inode *inode, u16 uid, u16 gid, u16 mode,
378 struct wim_lookup_table *lookup_table, int which)
380 struct wimlib_unix_data unix_data;
382 bool have_good_unix_data = false;
383 bool have_unix_data = false;
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;
391 have_good_unix_data = true;
393 unix_data.version = 0;
394 if (which & UNIX_DATA_UID || !have_good_unix_data)
396 if (which & UNIX_DATA_GID || !have_good_unix_data)
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,
402 sizeof(struct wimlib_unix_data),
404 if (ret == 0 && have_unix_data)
405 inode_remove_ads(inode, stream_idx, lookup_table);
408 #endif /* __WIN32__ */
411 * Resolve an inode's lookup table entries.
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.
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
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.
425 inode_resolve_streams(struct wim_inode *inode, struct wim_lookup_table *table,
430 if (!inode->i_resolved) {
431 struct wim_lookup_table_entry *lte, *ads_lte;
433 /* Resolve the default file stream */
435 hash = inode->i_hash;
436 if (!is_zero_hash(hash)) {
437 lte = lookup_stream(table, hash);
440 lte = new_lookup_table_entry();
442 return WIMLIB_ERR_NOMEM;
443 copy_hash(lte->hash, hash);
444 lookup_table_insert(table, lte);
446 goto stream_not_found;
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;
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);
463 ads_lte = new_lookup_table_entry();
465 return WIMLIB_ERR_NOMEM;
466 copy_hash(ads_lte->hash, hash);
467 lookup_table_insert(table, ads_lte);
469 goto stream_not_found;
473 ads_ltes[i] = ads_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;
483 return stream_not_found_error(inode, hash);
487 inode_unresolve_streams(struct wim_inode *inode)
489 if (inode->i_resolved) {
491 copy_hash(inode->i_hash, inode->i_lte->hash);
493 zero_out_hash(inode->i_hash);
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);
500 zero_out_hash(inode->i_ads_entries[i].hash);
502 inode->i_resolved = 0;
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.
511 * This works for both resolved and un-resolved inodes.
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)
517 if (inode->i_resolved)
518 return inode_stream_lte_resolved(inode, stream_idx);
520 return inode_stream_lte_unresolved(inode, stream_idx, table);
523 struct wim_lookup_table_entry *
524 inode_unnamed_stream_resolved(const struct wim_inode *inode, u16 *stream_idx_ret)
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)))
532 return inode_stream_lte_resolved(inode, i);
539 struct wim_lookup_table_entry *
540 inode_unnamed_lte_resolved(const struct wim_inode *inode)
543 return inode_unnamed_stream_resolved(inode, &stream_idx);
546 struct wim_lookup_table_entry *
547 inode_unnamed_lte_unresolved(const struct wim_inode *inode,
548 const struct wim_lookup_table *table)
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)))
555 return inode_stream_lte_unresolved(inode, i, table);
561 /* Return the lookup table entry for the unnamed data stream of an inode, or
562 * NULL if there is none.
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.
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.
577 struct wim_lookup_table_entry *
578 inode_unnamed_lte(const struct wim_inode *inode,
579 const struct wim_lookup_table *table)
581 if (inode->i_resolved)
582 return inode_unnamed_lte_resolved(inode);
584 return inode_unnamed_lte_unresolved(inode, table);
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. */
591 inode_unnamed_stream_hash(const struct wim_inode *inode)
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))
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)
610 wimlib_assert(lte->unhashed);
611 struct wim_inode *inode = lte->back_inode;
612 u32 stream_id = lte->back_stream_id;
614 return &inode->i_lte;
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;
624 stream_not_found_error(const struct wim_inode *inode, const u8 *hash)
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);
632 return WIMLIB_ERR_RESOURCE_NOT_FOUND;
636 * Reads the alternate data stream entries of a WIM dentry.
639 * Pointer to buffer that starts with the first alternate stream entry.
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.
646 * Number of bytes of data remaining in the buffer pointed to by @p.
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
653 * WIMLIB_ERR_SUCCESS (0)
654 * WIMLIB_ERR_INVALID_METADATA_RESOURCE
658 read_ads_entries(const u8 * restrict p, struct wim_inode * restrict inode,
659 size_t nbytes_remaining)
662 struct wim_ads_entry *ads_entries;
665 BUILD_BUG_ON(sizeof(struct wim_ads_entry_on_disk) != WIM_ADS_ENTRY_DISK_SIZE);
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)
674 /* Read the entries into our newly allocated buffer. */
675 for (u16 i = 0; i < num_ads; i++) {
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;
681 cur_entry = &ads_entries[i];
682 ads_entries[i].stream_id = i + 1;
684 /* Do we have at least the size of the fixed-length data we know
686 if (nbytes_remaining < sizeof(struct wim_ads_entry_on_disk))
689 /* Read the length field */
690 length = le64_to_cpu(disk_entry->length);
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)
699 /* Read the rest of the fixed-length data. */
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);
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
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)
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)
725 cur_entry->stream_name = utf16le_dupz(disk_entry->stream_name,
726 cur_entry->stream_name_nbytes);
727 if (cur_entry->stream_name == NULL)
730 /* Mark inode as having weird stream entries. */
731 inode->i_canonical_streams = 0;
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
742 length = (length + 7) & ~(u64)7;
744 if (nbytes_remaining < length)
745 nbytes_remaining = 0;
747 nbytes_remaining -= length;
749 inode->i_ads_entries = ads_entries;
750 inode->i_next_stream_id = inode->i_num_ads + 1;
754 ret = WIMLIB_ERR_NOMEM;
755 goto out_free_ads_entries;
757 ERROR("An alternate data stream entry is invalid");
758 ret = WIMLIB_ERR_INVALID_METADATA_RESOURCE;
759 out_free_ads_entries:
761 for (u16 i = 0; i < num_ads; i++)
762 destroy_ads_entry(&ads_entries[i]);
770 * Verify a WIM inode:
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.
777 * WIMLIB_ERR_SUCCESS (0)
780 verify_inode(struct wim_inode *inode, const struct wim_security_data *sd)
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))
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;
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++) {
798 hash = inode_stream_hash(inode, i);
799 if (inode_stream_name_nbytes(inode, i) == 0 && !is_zero_hash(hash))
800 num_unnamed_streams++;
802 if (num_unnamed_streams > 1) {
803 WARNING("\"%"TS"\" has multiple (%u) un-named streams",
804 inode_first_full_path(inode), num_unnamed_streams);
811 inode_ref_streams(struct wim_inode *inode)
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);
822 inode_unref_streams(struct wim_inode *inode,
823 struct wim_lookup_table *lookup_table)
825 struct wim_lookup_table_entry *lte;
828 for (i = 0; i <= inode->i_num_ads; i++) {
829 lte = inode_stream_lte(inode, i, lookup_table);
831 lte_decrement_refcnt(lte, lookup_table);
836 init_inode_table(struct wim_inode_table *table, size_t capacity)
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;
843 table->num_entries = 0;
844 table->capacity = capacity;
845 INIT_LIST_HEAD(&table->extra_inodes);
850 destroy_inode_table(struct wim_inode_table *table)
855 static struct wim_inode *
856 inode_table_get_inode(struct wim_inode_table *table, u64 ino, u64 devno)
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;
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"}",
871 inode = new_timeless_inode();
874 inode->i_devno = devno;
875 hlist_add_head(&inode->i_hlist, &table->array[pos]);
876 table->num_entries++;
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.
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)
894 struct wim_dentry *dentry;
895 struct wim_inode *inode;
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);
904 list_add_tail(&dentry->d_inode->i_list, &table->extra_inodes);
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);
912 inode = inode_table_get_inode(table, ino, devno);
915 return WIMLIB_ERR_NOMEM;
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);
924 *dentry_ret = dentry;
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. */
934 inode_table_prepare_inode_list(struct wim_inode_table *table,
935 struct list_head *head)
937 struct wim_inode *inode, *tmp_inode;
938 struct hlist_node *cur, *tmp;
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++;
945 /* Assign inode numbers to the new inodes and move them to the image's
947 for (size_t i = 0; i < table->capacity; i++) {
948 hlist_for_each_entry_safe(inode, cur, tmp, &table->array[i], i_hlist)
950 inode->i_ino = cur_ino++;
952 list_add_tail(&inode->i_list, head);
954 INIT_HLIST_HEAD(&table->array[i]);
956 list_for_each_entry_safe(inode, tmp_inode, &table->extra_inodes, i_list)
958 inode->i_ino = cur_ino++;
960 list_add_tail(&inode->i_list, head);
962 INIT_LIST_HEAD(&table->extra_inodes);
963 table->num_entries = 0;