4 * Functions that operate on WIM inodes.
6 * See dentry.c for a description of the relationship between WIM dentries and
11 * Copyright (C) 2012, 2013, 2014 Eric Biggers
13 * This file is free software; you can redistribute it and/or modify it under
14 * the terms of the GNU Lesser General Public License as published by the Free
15 * Software Foundation; either version 3 of the License, or (at your option) any
18 * This file is distributed in the hope that it will be useful, but WITHOUT
19 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
23 * You should have received a copy of the GNU Lesser General Public License
24 * along with this file; if not, see http://www.gnu.org/licenses/.
31 #include "wimlib/assert.h"
32 #include "wimlib/dentry.h" /* Only for dentry_full_path(). Otherwise the code
33 in this file doesn't care about file names/paths.
35 #include "wimlib/encoding.h"
36 #include "wimlib/endianness.h"
37 #include "wimlib/error.h"
38 #include "wimlib/inode.h"
39 #include "wimlib/inode_table.h"
40 #include "wimlib/lookup_table.h"
41 #include "wimlib/security.h"
42 #include "wimlib/timestamp.h"
46 /* Allocate a new inode. Set the timestamps to the current time. */
50 struct wim_inode *inode = new_timeless_inode();
52 u64 now = get_wim_timestamp();
53 inode->i_creation_time = now;
54 inode->i_last_access_time = now;
55 inode->i_last_write_time = now;
60 /* Allocate a new inode. Leave the timestamps zeroed out. */
62 new_timeless_inode(void)
64 struct wim_inode *inode = CALLOC(1, sizeof(struct wim_inode));
66 inode->i_security_id = -1;
68 inode->i_next_stream_id = 1;
69 inode->i_not_rpfixed = 1;
70 inode->i_canonical_streams = 1;
71 INIT_LIST_HEAD(&inode->i_list);
72 INIT_LIST_HEAD(&inode->i_dentry);
77 /* Decrement link count on an inode. */
79 put_inode(struct wim_inode *inode)
81 wimlib_assert(inode->i_nlink != 0);
82 if (--inode->i_nlink == 0) {
83 /* If FUSE mounts are enabled, we must keep a unlinked inode
84 * around until all file descriptors to it have been closed. */
86 if (inode->i_num_opened_fds == 0)
92 /* Free memory allocated within an alternate data stream entry. */
94 destroy_ads_entry(struct wim_ads_entry *ads_entry)
96 FREE(ads_entry->stream_name);
99 /* Free an inode. Only use this if there can't be other links to the inode or
100 * if it doesn't matter if there are. */
102 free_inode(struct wim_inode *inode)
104 if (unlikely(!inode))
107 if (unlikely(inode->i_ads_entries)) {
108 for (u16 i = 0; i < inode->i_num_ads; i++)
109 destroy_ads_entry(&inode->i_ads_entries[i]);
110 FREE(inode->i_ads_entries);
112 if (unlikely(inode->i_extra))
113 FREE(inode->i_extra);
114 /* HACK: This may instead delete the inode from i_list, but hlist_del()
115 * behaves the same as list_del(). */
116 if (!hlist_unhashed(&inode->i_hlist))
117 hlist_del(&inode->i_hlist);
121 /* Return %true iff the alternate data stream entry @entry has the UTF-16LE
122 * stream name @name that has length @name_nbytes bytes. */
124 ads_entry_has_name(const struct wim_ads_entry *entry,
125 const utf16lechar *name, size_t name_nbytes,
128 return 0 == cmp_utf16le_strings(name,
131 entry->stream_name_nbytes / 2,
136 * Returns the alternate data stream entry belonging to @inode that has the
137 * stream name @stream_name, or NULL if the inode has no alternate data stream
140 * If @p stream_name is the empty string, NULL is returned --- that is, this
141 * function will not return "unnamed" alternate data stream entries.
143 * If NULL is returned, errno is set.
145 struct wim_ads_entry *
146 inode_get_ads_entry(struct wim_inode *inode, const tchar *stream_name)
149 const utf16lechar *stream_name_utf16le;
150 size_t stream_name_utf16le_nbytes;
152 struct wim_ads_entry *result;
154 if (inode->i_num_ads == 0) {
159 if (stream_name[0] == T('\0')) {
164 ret = tstr_get_utf16le_and_len(stream_name, &stream_name_utf16le,
165 &stream_name_utf16le_nbytes);
172 if (ads_entry_has_name(&inode->i_ads_entries[i],
174 stream_name_utf16le_nbytes,
175 default_ignore_case))
177 result = &inode->i_ads_entries[i];
180 } while (++i != inode->i_num_ads);
182 tstr_put_utf16le(stream_name_utf16le);
189 static struct wim_ads_entry *
190 do_inode_add_ads(struct wim_inode *inode,
191 utf16lechar *stream_name, size_t stream_name_nbytes)
194 struct wim_ads_entry *ads_entries;
195 struct wim_ads_entry *new_entry;
197 if (inode->i_num_ads >= 0xfffe) {
198 ERROR("File \"%"TS"\" has too many alternate data streams!",
199 inode_first_full_path(inode));
203 num_ads = inode->i_num_ads + 1;
204 ads_entries = REALLOC(inode->i_ads_entries,
205 num_ads * sizeof(inode->i_ads_entries[0]));
209 inode->i_ads_entries = ads_entries;
211 new_entry = &inode->i_ads_entries[num_ads - 1];
213 memset(new_entry, 0, sizeof(struct wim_ads_entry));
214 new_entry->stream_name = stream_name;
215 new_entry->stream_name_nbytes = stream_name_nbytes;
216 new_entry->stream_id = inode->i_next_stream_id++;
217 inode->i_num_ads = num_ads;
222 * Add an alternate data stream entry to a WIM inode (UTF-16LE version). On
223 * success, returns a pointer to the new entry. Note that this pointer might
224 * become invalid if another ADS entry is added to the inode. On failure,
225 * returns NULL and sets errno.
227 struct wim_ads_entry *
228 inode_add_ads_utf16le(struct wim_inode *inode,
229 const utf16lechar *stream_name, size_t stream_name_nbytes)
231 utf16lechar *dup = NULL;
232 struct wim_ads_entry *result;
234 if (stream_name_nbytes) {
235 dup = utf16le_dupz(stream_name, stream_name_nbytes);
240 result = do_inode_add_ads(inode, dup, stream_name_nbytes);
247 * Add an alternate data stream entry to a WIM inode (tchar version). On
248 * success, returns a pointer to the new entry. Note that this pointer might
249 * become invalid if another ADS entry is added to the inode. On failure,
250 * returns NULL and sets errno.
252 struct wim_ads_entry *
253 inode_add_ads(struct wim_inode *inode, const tchar *stream_name)
255 utf16lechar *stream_name_utf16le = NULL;
256 size_t stream_name_utf16le_nbytes = 0;
257 struct wim_ads_entry *result;
259 if (stream_name && *stream_name)
260 if (tstr_to_utf16le(stream_name,
261 tstrlen(stream_name) * sizeof(tchar),
262 &stream_name_utf16le,
263 &stream_name_utf16le_nbytes))
266 result = do_inode_add_ads(inode, stream_name_utf16le,
267 stream_name_utf16le_nbytes);
269 FREE(stream_name_utf16le);
274 * Add an data alternate stream entry to a WIM inode, where the contents of the
275 * new stream are specified in a data buffer. The inode must be resolved.
277 * On success, returns a pointer to the new alternate data stream entry. Note
278 * that this pointer might become invalid if another ADS entry is added to the
279 * inode. On failure, returns NULL and sets errno.
281 struct wim_ads_entry *
282 inode_add_ads_with_data(struct wim_inode *inode, const tchar *name,
283 const void *value, size_t size,
284 struct wim_lookup_table *lookup_table)
286 struct wim_ads_entry *new_entry;
288 wimlib_assert(inode->i_resolved);
290 new_entry = inode_add_ads(inode, name);
294 new_entry->lte = new_stream_from_data_buffer(value, size, lookup_table);
295 if (!new_entry->lte) {
296 inode_remove_ads(inode, new_entry, NULL);
303 * Does the inode have any named data streams?
306 inode_has_named_stream(const struct wim_inode *inode)
308 for (u16 i = 0; i < inode->i_num_ads; i++)
309 if (ads_entry_is_named_stream(&inode->i_ads_entries[i]))
314 /* Set the unnamed stream of a WIM inode, given a data buffer containing the
315 * stream contents. The inode must be resolved and cannot have an unnamed
316 * stream specified already. */
318 inode_set_unnamed_stream(struct wim_inode *inode, const void *data, size_t len,
319 struct wim_lookup_table *lookup_table)
321 wimlib_assert(inode->i_resolved);
322 wimlib_assert(!inode->i_lte);
323 inode->i_lte = new_stream_from_data_buffer(data, len, lookup_table);
325 return WIMLIB_ERR_NOMEM;
329 /* Remove an alternate data stream from a WIM inode. */
331 inode_remove_ads(struct wim_inode *inode, struct wim_ads_entry *entry,
332 struct wim_lookup_table *lookup_table)
334 struct wim_lookup_table_entry *lte;
335 unsigned idx = entry - inode->i_ads_entries;
337 wimlib_assert(idx < inode->i_num_ads);
338 wimlib_assert(inode->i_resolved);
342 lte_decrement_refcnt(lte, lookup_table);
344 destroy_ads_entry(entry);
346 memmove(&inode->i_ads_entries[idx],
347 &inode->i_ads_entries[idx + 1],
348 (inode->i_num_ads - idx - 1) * sizeof(inode->i_ads_entries[0]));
353 * Resolve an inode's single-instance streams.
355 * This takes each SHA-1 message digest stored in the inode or one of its ADS
356 * entries and replaces it with a pointer directly to the appropriate 'struct
357 * wim_lookup_table_entry' currently inserted into @table to represent the
358 * single-instance stream having that SHA-1 message digest.
360 * If @force is %false:
361 * If any of the needed single-instance streams do not exist in @table,
362 * return WIMLIB_ERR_RESOURCE_NOT_FOUND and leave the inode unmodified.
363 * If @force is %true:
364 * If any of the needed single-instance streams do not exist in @table,
365 * allocate new entries for them and insert them into @table. This does
366 * not, of course, cause these streams to magically exist, but this is
367 * needed by the code for extraction from a pipe.
369 * If the inode is already resolved, this function does nothing.
371 * Returns 0 on success; WIMLIB_ERR_NOMEM if out of memory; or
372 * WIMLIB_ERR_RESOURCE_NOT_FOUND if @force is %false and at least one
373 * single-instance stream referenced by the inode was missing.
376 inode_resolve_streams(struct wim_inode *inode, struct wim_lookup_table *table,
380 struct wim_lookup_table_entry *lte, *ads_lte;
382 if (inode->i_resolved)
385 struct wim_lookup_table_entry *ads_ltes[inode->i_num_ads];
387 /* Resolve the default data stream */
389 hash = inode->i_hash;
390 if (!is_zero_hash(hash)) {
391 lte = lookup_stream(table, hash);
394 lte = new_lookup_table_entry();
396 return WIMLIB_ERR_NOMEM;
397 copy_hash(lte->hash, hash);
398 lookup_table_insert(table, lte);
400 goto stream_not_found;
405 /* Resolve the alternate data streams */
406 for (u16 i = 0; i < inode->i_num_ads; i++) {
407 struct wim_ads_entry *cur_entry;
410 cur_entry = &inode->i_ads_entries[i];
411 hash = cur_entry->hash;
412 if (!is_zero_hash(hash)) {
413 ads_lte = lookup_stream(table, hash);
416 ads_lte = new_lookup_table_entry();
418 return WIMLIB_ERR_NOMEM;
419 copy_hash(ads_lte->hash, hash);
420 lookup_table_insert(table, ads_lte);
422 goto stream_not_found;
426 ads_ltes[i] = ads_lte;
429 for (u16 i = 0; i < inode->i_num_ads; i++)
430 inode->i_ads_entries[i].lte = ads_ltes[i];
431 inode->i_resolved = 1;
435 return stream_not_found_error(inode, hash);
439 * Undo the effects of inode_resolve_streams().
441 * If the inode is not resolved, this function does nothing.
444 inode_unresolve_streams(struct wim_inode *inode)
446 if (!inode->i_resolved)
450 copy_hash(inode->i_hash, inode->i_lte->hash);
452 zero_out_hash(inode->i_hash);
454 for (u16 i = 0; i < inode->i_num_ads; i++) {
455 if (inode->i_ads_entries[i].lte)
456 copy_hash(inode->i_ads_entries[i].hash,
457 inode->i_ads_entries[i].lte->hash);
459 zero_out_hash(inode->i_ads_entries[i].hash);
461 inode->i_resolved = 0;
465 stream_not_found_error(const struct wim_inode *inode, const u8 *hash)
467 if (wimlib_print_errors) {
468 tchar hashstr[SHA1_HASH_SIZE * 2 + 1];
470 sprint_hash(hash, hashstr);
472 ERROR("\"%"TS"\": stream not found\n"
473 " SHA-1 message digest of missing stream:\n"
475 inode_first_full_path(inode), hashstr);
477 return WIMLIB_ERR_RESOURCE_NOT_FOUND;
481 * Return the lookup table entry for stream @stream_idx of the inode, where
482 * stream_idx = 0 means the default un-named file stream, and stream_idx >= 1
483 * corresponds to an alternate data stream.
485 * This works for both resolved and un-resolved inodes.
487 struct wim_lookup_table_entry *
488 inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
489 const struct wim_lookup_table *table)
491 if (inode->i_resolved)
492 return inode_stream_lte_resolved(inode, stream_idx);
494 return inode_stream_lte_unresolved(inode, stream_idx, table);
498 * Return the lookup table entry for the unnamed data stream of a *resolved*
499 * inode, or NULL if there is none. Also return the 0-based index of the
500 * corresponding stream in *stream_idx_ret.
502 struct wim_lookup_table_entry *
503 inode_unnamed_stream_resolved(const struct wim_inode *inode, u16 *stream_idx_ret)
505 wimlib_assert(inode->i_resolved);
506 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
507 if (inode_stream_name_nbytes(inode, i) == 0 &&
508 !is_zero_hash(inode_stream_hash_resolved(inode, i)))
511 return inode_stream_lte_resolved(inode, i);
519 * Return the lookup table entry for the unnamed data stream of a *resolved*
520 * inode, or NULL if there is none.
522 struct wim_lookup_table_entry *
523 inode_unnamed_lte_resolved(const struct wim_inode *inode)
526 return inode_unnamed_stream_resolved(inode, &stream_idx);
530 * Return the lookup table entry for the unnamed data stream of an *unresolved*
531 * inode, or NULL if there is none.
533 struct wim_lookup_table_entry *
534 inode_unnamed_lte_unresolved(const struct wim_inode *inode,
535 const struct wim_lookup_table *table)
537 wimlib_assert(!inode->i_resolved);
538 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
539 if (inode_stream_name_nbytes(inode, i) == 0 &&
540 !is_zero_hash(inode_stream_hash_unresolved(inode, i)))
542 return inode_stream_lte_unresolved(inode, i, table);
549 * Return the lookup table entry for the unnamed data stream of an inode, or
550 * NULL if there is none.
552 * You'd think this would be easier than it actually is, since the unnamed data
553 * stream should be the one referenced from the inode itself. Alas, if there
554 * are named data streams, Microsoft's "imagex.exe" program will put the unnamed
555 * data stream in one of the alternate data streams instead of inside the WIM
556 * dentry itself. So we need to check the alternate data streams too.
558 * Also, note that a dentry may appear to have more than one unnamed stream, but
559 * if the SHA-1 message digest is all 0's then the corresponding stream does not
560 * really "count" (this is the case for the inode's own file stream when the
561 * file stream that should be there is actually in one of the alternate stream
562 * entries.). This is despite the fact that we may need to extract such a
563 * missing entry as an empty file or empty named data stream.
565 struct wim_lookup_table_entry *
566 inode_unnamed_lte(const struct wim_inode *inode,
567 const struct wim_lookup_table *table)
569 if (inode->i_resolved)
570 return inode_unnamed_lte_resolved(inode);
572 return inode_unnamed_lte_unresolved(inode, table);
576 * Return the SHA-1 message digest of the unnamed data stream of a WIM inode.
578 * If inode does not have an unnamed data stream, this returns a void SHA-1
579 * message digest containing all zero bytes.
582 inode_unnamed_stream_hash(const struct wim_inode *inode)
586 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
587 if (inode_stream_name_nbytes(inode, i) == 0) {
588 hash = inode_stream_hash(inode, i);
589 if (!is_zero_hash(hash))
597 * Translate a single-instance stream entry into the pointer contained in the
598 * inode (or ads entry of an inode) that references it.
600 * This is only possible for "unhashed" streams, which are guaranteed to have
601 * only one reference, and that reference is guaranteed to be in a resolved
602 * inode. (It can't be in an unresolved inode, since that would imply the hash
605 struct wim_lookup_table_entry **
606 retrieve_lte_pointer(struct wim_lookup_table_entry *lte)
608 wimlib_assert(lte->unhashed);
609 struct wim_inode *inode = lte->back_inode;
610 u32 stream_id = lte->back_stream_id;
612 return &inode->i_lte;
614 for (u16 i = 0; i < inode->i_num_ads; i++)
615 if (inode->i_ads_entries[i].stream_id == stream_id)
616 return &inode->i_ads_entries[i].lte;
622 * Read the alternate data stream entries of a WIM dentry.
625 * Pointer to buffer that starts with the first alternate stream entry.
628 * Inode to load the alternate data streams into. @inode->i_num_ads must
629 * have been set to the number of alternate data streams that are expected.
631 * @nbytes_remaining_p:
632 * Number of bytes of data remaining in the buffer pointed to by @p.
633 * On success this will be updated to point just past the ADS entries.
635 * On success, inode->i_ads_entries is set to an array of `struct
636 * wim_ads_entry's of length inode->i_num_ads. On failure, @inode is not
640 * WIMLIB_ERR_SUCCESS (0)
641 * WIMLIB_ERR_INVALID_METADATA_RESOURCE
645 read_ads_entries(const u8 * restrict p, struct wim_inode * restrict inode,
646 size_t *nbytes_remaining_p)
648 size_t nbytes_remaining = *nbytes_remaining_p;
650 struct wim_ads_entry *ads_entries;
653 BUILD_BUG_ON(sizeof(struct wim_ads_entry_on_disk) != WIM_ADS_ENTRY_DISK_SIZE);
655 /* Allocate an array for our in-memory representation of the alternate
656 * data stream entries. */
657 num_ads = inode->i_num_ads;
658 ads_entries = CALLOC(num_ads, sizeof(inode->i_ads_entries[0]));
662 /* Read the entries into our newly allocated buffer. */
663 for (u16 i = 0; i < num_ads; i++) {
665 struct wim_ads_entry *cur_entry;
666 const struct wim_ads_entry_on_disk *disk_entry =
667 (const struct wim_ads_entry_on_disk*)p;
669 cur_entry = &ads_entries[i];
670 ads_entries[i].stream_id = i + 1;
672 /* Do we have at least the size of the fixed-length data we know
674 if (nbytes_remaining < sizeof(struct wim_ads_entry_on_disk))
677 /* Read the length field */
678 length = le64_to_cpu(disk_entry->length);
680 /* Make sure the length field is neither so small it doesn't
681 * include all the fixed-length data nor so large it overflows
682 * the metadata resource buffer. */
683 if (length < sizeof(struct wim_ads_entry_on_disk) ||
684 length > nbytes_remaining)
687 /* Read the rest of the fixed-length data. */
689 cur_entry->reserved = le64_to_cpu(disk_entry->reserved);
690 copy_hash(cur_entry->hash, disk_entry->hash);
691 cur_entry->stream_name_nbytes = le16_to_cpu(disk_entry->stream_name_nbytes);
693 /* If stream_name_nbytes != 0, this is a named stream.
694 * Otherwise this is an unnamed stream, or in some cases (bugs
695 * in Microsoft's software I guess) a meaningless entry
696 * distinguished from the real unnamed stream entry, if any, by
697 * the fact that the real unnamed stream entry has a nonzero
699 if (cur_entry->stream_name_nbytes) {
700 /* The name is encoded in UTF16-LE, which uses 2-byte
701 * coding units, so the length of the name had better be
702 * an even number of bytes... */
703 if (cur_entry->stream_name_nbytes & 1)
706 /* Add the length of the stream name to get the length
707 * we actually need to read. Make sure this isn't more
708 * than the specified length of the entry. */
709 if (sizeof(struct wim_ads_entry_on_disk) +
710 cur_entry->stream_name_nbytes > length)
713 cur_entry->stream_name = utf16le_dupz(disk_entry->stream_name,
714 cur_entry->stream_name_nbytes);
715 if (!cur_entry->stream_name)
718 /* Mark inode as having weird stream entries. */
719 inode->i_canonical_streams = 0;
722 /* It's expected that the size of every ADS entry is a multiple
723 * of 8. However, to be safe, I'm allowing the possibility of
724 * an ADS entry at the very end of the metadata resource ending
725 * un-aligned. So although we still need to increment the input
726 * pointer by @length to reach the next ADS entry, it's possible
727 * that less than @length is actually remaining in the metadata
728 * resource. We should set the remaining bytes to 0 if this
730 length = (length + 7) & ~7;
732 if (nbytes_remaining < length)
733 nbytes_remaining = 0;
735 nbytes_remaining -= length;
737 inode->i_ads_entries = ads_entries;
738 inode->i_next_stream_id = inode->i_num_ads + 1;
739 *nbytes_remaining_p = nbytes_remaining;
743 ret = WIMLIB_ERR_NOMEM;
744 goto out_free_ads_entries;
746 ERROR("An alternate data stream entry is invalid");
747 ret = WIMLIB_ERR_INVALID_METADATA_RESOURCE;
748 out_free_ads_entries:
750 for (u16 i = 0; i < num_ads; i++)
751 destroy_ads_entry(&ads_entries[i]);
758 /* Check a WIM inode for unusual field values. */
760 check_inode(struct wim_inode *inode, const struct wim_security_data *sd)
762 /* Check the security ID. -1 is valid and means "no security
763 * descriptor". Anything else has to be a valid index into the WIM
764 * image's security descriptors table. */
765 if (inode->i_security_id < -1 ||
766 (inode->i_security_id >= 0 &&
767 inode->i_security_id >= sd->num_entries))
769 WARNING("\"%"TS"\" has an invalid security ID (%d)",
770 inode_first_full_path(inode), inode->i_security_id);
771 inode->i_security_id = -1;
774 /* Make sure there is only one unnamed data stream. */
775 unsigned num_unnamed_streams = 0;
776 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
778 hash = inode_stream_hash(inode, i);
779 if (inode_stream_name_nbytes(inode, i) == 0 && !is_zero_hash(hash))
780 num_unnamed_streams++;
782 if (num_unnamed_streams > 1) {
783 WARNING("\"%"TS"\" has multiple (%u) un-named streams",
784 inode_first_full_path(inode), num_unnamed_streams);
785 /* We currently don't treat this as an error and will just end
786 * up using the first unnamed data stream in the inode. */
790 /* Acquire another reference to each single-instance stream referenced by this
791 * inode. This is necessary when creating a hard link to this inode.
793 * The inode must be resolved. */
795 inode_ref_streams(struct wim_inode *inode)
797 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
798 struct wim_lookup_table_entry *lte;
800 lte = inode_stream_lte_resolved(inode, i);
806 /* Drop a reference to each single-instance stream referenced by this inode.
807 * This is necessary when deleting a hard link to this inode. */
809 inode_unref_streams(struct wim_inode *inode,
810 struct wim_lookup_table *lookup_table)
812 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
813 struct wim_lookup_table_entry *lte;
815 lte = inode_stream_lte(inode, i, lookup_table);
817 lte_decrement_refcnt(lte, lookup_table);
821 /* Initialize a hash table for hard link detection. */
823 init_inode_table(struct wim_inode_table *table, size_t capacity)
825 table->array = CALLOC(capacity, sizeof(table->array[0]));
827 return WIMLIB_ERR_NOMEM;
828 table->num_entries = 0;
829 table->capacity = capacity;
830 INIT_LIST_HEAD(&table->extra_inodes);
834 /* Free the memory allocated by init_inode_table(). */
836 destroy_inode_table(struct wim_inode_table *table)
841 static struct wim_inode *
842 inode_table_get_inode(struct wim_inode_table *table, u64 ino, u64 devno)
844 u64 hash = hash_u64(hash_u64(ino) + hash_u64(devno));
845 size_t pos = hash % table->capacity;
846 struct wim_inode *inode;
847 struct hlist_node *cur;
849 /* Search for an existing inode having the same inode number and device
851 hlist_for_each_entry(inode, cur, &table->array[pos], i_hlist) {
852 if (inode->i_ino == ino && inode->i_devno == devno) {
853 /* Found; use the existing inode. */
859 /* Create a new inode and insert it into the table. */
860 inode = new_timeless_inode();
863 inode->i_devno = devno;
864 hlist_add_head(&inode->i_hlist, &table->array[pos]);
865 table->num_entries++;
871 * Allocate a new dentry, with hard link detection.
874 * The inode table being used for the current directory scan operation. It
875 * will contain the mapping from (ino, devno) pairs to inodes.
878 * The name to give the new dentry.
881 * The inode number of the file, read from the filesystem.
884 * The device number of the file, read from the filesystem. Proper setting
885 * of this parameter prevents cross-device hardlinks from being created.
886 * If this is not a problem (perhaps because the current directory scan
887 * operation is guaranteed to never traverse a filesystem boundary), then
888 * this parameter can just be a fixed value such as 0.
891 * If %true, the new dentry will not be hard linked to any existing inode,
892 * regardless of the values of @ino and @devno. If %false, normal hard
893 * link detection will be done.
896 * On success, a pointer to the new dentry will be returned in this
897 * location. If i_nlink of the dentry's inode is greater than 1, then this
898 * function created a hard link to an existing inode rather than creating a
901 * On success, returns 0. On failure, returns WIMLIB_ERR_NOMEM or an error code
902 * resulting from a failed string conversion.
905 inode_table_new_dentry(struct wim_inode_table *table, const tchar *name,
906 u64 ino, u64 devno, bool noshare,
907 struct wim_dentry **dentry_ret)
909 struct wim_dentry *dentry;
910 struct wim_inode *inode;
914 /* File that cannot be hardlinked--- Return a new inode with its
915 * inode and device numbers left at 0. */
916 ret = new_dentry_with_timeless_inode(name, &dentry);
919 list_add_tail(&dentry->d_inode->i_list, &table->extra_inodes);
921 /* File that can be hardlinked--- search the table for an
922 * existing inode matching the inode number and device;
923 * otherwise create a new inode. */
924 ret = new_dentry(name, &dentry);
927 inode = inode_table_get_inode(table, ino, devno);
930 return WIMLIB_ERR_NOMEM;
932 /* If using an existing inode, we need to gain a reference to
933 * each of its streams. */
934 if (inode->i_nlink > 1)
935 inode_ref_streams(inode);
936 dentry->d_inode = inode;
937 inode_add_dentry(dentry, inode);
939 *dentry_ret = dentry;
946 * Following the allocation of dentries with hard link detection using
947 * inode_table_new_dentry(), this function will assign consecutive inode numbers
948 * to the new set of inodes. It will also append the list of new inodes to the
949 * list @head, which must contain any inodes already existing in the WIM image.
952 inode_table_prepare_inode_list(struct wim_inode_table *table,
953 struct list_head *head)
955 struct wim_inode *inode, *tmp_inode;
956 struct hlist_node *cur, *tmp;
959 /* Re-assign inode numbers in the existing list to avoid duplicates. */
960 list_for_each_entry(inode, head, i_list)
961 inode->i_ino = cur_ino++;
963 /* Assign inode numbers to the new inodes and move them to the image's
965 for (size_t i = 0; i < table->capacity; i++) {
966 hlist_for_each_entry_safe(inode, cur, tmp, &table->array[i], i_hlist)
968 inode->i_ino = cur_ino++;
970 list_add_tail(&inode->i_list, head);
972 INIT_HLIST_HEAD(&table->array[i]);
974 list_for_each_entry_safe(inode, tmp_inode, &table->extra_inodes, i_list)
976 inode->i_ino = cur_ino++;
978 list_add_tail(&inode->i_list, head);
980 INIT_LIST_HEAD(&table->extra_inodes);
981 table->num_entries = 0;