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, 2015 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/.
33 #include "wimlib/assert.h"
34 #include "wimlib/blob_table.h"
35 #include "wimlib/dentry.h"
36 #include "wimlib/encoding.h"
37 #include "wimlib/error.h"
38 #include "wimlib/inode.h"
39 #include "wimlib/timestamp.h"
42 * The 'stream_name' field of unnamed streams always points to this array, which
43 * is an empty UTF-16 string.
45 const utf16lechar NO_STREAM_NAME[1];
47 /* Allocate a new inode and associate the specified dentry with it. */
49 new_inode(struct wim_dentry *dentry, bool set_timestamps)
51 struct wim_inode *inode;
53 inode = CALLOC(1, sizeof(struct wim_inode));
57 inode->i_security_id = -1;
58 /*inode->i_nlink = 0;*/
59 inode->i_not_rpfixed = 1;
60 INIT_LIST_HEAD(&inode->i_list);
61 INIT_LIST_HEAD(&inode->i_dentry);
62 inode->i_streams = inode->i_embedded_streams;
64 u64 now = now_as_wim_timestamp();
65 inode->i_creation_time = now;
66 inode->i_last_access_time = now;
67 inode->i_last_write_time = now;
69 d_associate(dentry, inode);
74 destroy_stream(struct wim_inode_stream *strm)
76 if (strm->stream_name != NO_STREAM_NAME)
77 FREE(strm->stream_name);
81 free_inode(struct wim_inode *inode)
83 for (unsigned i = 0; i < inode->i_num_streams; i++)
84 destroy_stream(&inode->i_streams[i]);
85 if (inode->i_streams != inode->i_embedded_streams)
86 FREE(inode->i_streams);
89 /* HACK: This may instead delete the inode from i_list, but hlist_del()
90 * behaves the same as list_del(). */
91 if (!hlist_unhashed(&inode->i_hlist))
92 hlist_del(&inode->i_hlist);
97 free_inode_if_unneeded(struct wim_inode *inode)
102 if (inode->i_num_opened_fds)
108 /* Associate a dentry with the specified inode. */
110 d_associate(struct wim_dentry *dentry, struct wim_inode *inode)
112 wimlib_assert(!dentry->d_inode);
114 list_add_tail(&dentry->d_alias, &inode->i_dentry);
115 dentry->d_inode = inode;
119 /* Disassociate a dentry from its inode, if any. Following this, free the inode
120 * if it is no longer in use. */
122 d_disassociate(struct wim_dentry *dentry)
124 struct wim_inode *inode = dentry->d_inode;
126 if (unlikely(!inode))
129 wimlib_assert(inode->i_nlink > 0);
131 list_del(&dentry->d_alias);
132 dentry->d_inode = NULL;
135 free_inode_if_unneeded(inode);
140 inode_dec_num_opened_fds(struct wim_inode *inode)
142 wimlib_assert(inode->i_num_opened_fds > 0);
144 if (--inode->i_num_opened_fds == 0) {
145 /* The last file descriptor to this inode was closed. */
148 inode->i_num_allocated_fds = 0;
150 free_inode_if_unneeded(inode);
156 * Retrieve a stream of an inode.
159 * The inode from which the stream is desired
161 * The type of the stream desired
163 * The name of the stream desired as a null-terminated UTF-16LE string, or
164 * NO_STREAM_NAME if an unnamed stream is desired
166 * Returns a pointer to the stream if found, otherwise NULL.
168 struct wim_inode_stream *
169 inode_get_stream(const struct wim_inode *inode, int stream_type,
170 const utf16lechar *stream_name)
172 if (stream_name == NO_STREAM_NAME) /* Optimization */
173 return inode_get_unnamed_stream(inode, stream_type);
175 for (unsigned i = 0; i < inode->i_num_streams; i++) {
176 struct wim_inode_stream *strm = &inode->i_streams[i];
177 if (strm->stream_type == stream_type &&
178 !cmp_utf16le_strings_z(strm->stream_name, stream_name,
179 default_ignore_case))
188 * This is equivalent to inode_get_stream(inode, stream_type, NO_STREAM_NAME),
189 * but this optimizes for the unnamed case by not doing full string comparisons.
191 struct wim_inode_stream *
192 inode_get_unnamed_stream(const struct wim_inode *inode, int stream_type)
194 for (unsigned i = 0; i < inode->i_num_streams; i++) {
195 struct wim_inode_stream *strm = &inode->i_streams[i];
196 if (strm->stream_type == stream_type &&
197 strm->stream_name == NO_STREAM_NAME)
207 inode_set_stream_blob(struct wim_inode *inode, struct wim_inode_stream *strm,
208 struct blob_descriptor *new_blob)
210 strm->_stream_blob = new_blob;
211 strm->stream_resolved = 1;
213 new_blob->refcnt += inode->i_nlink;
217 inode_unset_stream_blob(struct wim_inode *inode, struct wim_inode_stream *strm,
218 struct blob_table *blob_table)
220 struct blob_descriptor *old_blob;
222 old_blob = stream_blob(strm, blob_table);
224 blob_subtract_refcnt(old_blob, blob_table, inode->i_nlink);
225 strm->_stream_blob = NULL;
226 strm->stream_resolved = 1;
230 * Replace the blob associated with the specified stream.
233 * The inode containing @strm
235 * The stream whose data needs to be replaced
237 * The new blob descriptor to assign
239 * Pointer to the blob table in which data blobs are being indexed
242 inode_replace_stream_blob(struct wim_inode *inode,
243 struct wim_inode_stream *strm,
244 struct blob_descriptor *new_blob,
245 struct blob_table *blob_table)
247 inode_unset_stream_blob(inode, strm, blob_table);
248 inode_set_stream_blob(inode, strm, new_blob);
252 * Add a new stream to the specified inode.
255 * The inode to which to add the stream
257 * The type of the stream being added
259 * The name of the stream being added as a null-terminated UTF-16LE string,
260 * or NO_STREAM_NAME if the stream is unnamed
262 * The blob that the new stream will initially reference, or NULL
264 * Returns a pointer to the new stream, or NULL with errno set if it could not
267 struct wim_inode_stream *
268 inode_add_stream(struct wim_inode *inode, int stream_type,
269 const utf16lechar *stream_name, struct blob_descriptor *blob)
271 if (inode->i_num_streams >= 0xFFFF) {
272 ERROR("Inode has too many streams! Path=\"%"TS"\"",
273 inode_first_full_path(inode));
278 struct wim_inode_stream *streams;
279 struct wim_inode_stream *new_strm;
281 if (inode->i_streams == inode->i_embedded_streams) {
282 if (inode->i_num_streams < ARRAY_LEN(inode->i_embedded_streams)) {
283 streams = inode->i_embedded_streams;
285 streams = MALLOC((inode->i_num_streams + 1) *
286 sizeof(inode->i_streams[0]));
289 memcpy(streams, inode->i_streams,
290 (inode->i_num_streams *
291 sizeof(inode->i_streams[0])));
292 inode->i_streams = streams;
295 streams = REALLOC(inode->i_streams,
296 (inode->i_num_streams + 1) *
297 sizeof(inode->i_streams[0]));
300 inode->i_streams = streams;
302 new_strm = &streams[inode->i_num_streams];
304 memset(new_strm, 0, sizeof(*new_strm));
306 new_strm->stream_type = stream_type;
309 new_strm->stream_name = (utf16lechar *)NO_STREAM_NAME;
312 new_strm->stream_name = utf16le_dup(stream_name);
313 if (!new_strm->stream_name)
317 new_strm->stream_id = inode->i_next_stream_id++;
319 inode_set_stream_blob(inode, new_strm, blob);
321 inode->i_num_streams++;
327 * Replace the data of the specified stream.
330 * The inode containing @strm
332 * The stream whose data needs to be replaced
334 * The buffer of data to assign to the stream
336 * Size of the @data buffer, in bytes
338 * Pointer to the blob table in which data blobs are being indexed
340 * Returns true if successful; false with errno set if unsuccessful.
343 inode_replace_stream_data(struct wim_inode *inode,
344 struct wim_inode_stream *strm,
345 const void *data, size_t size,
346 struct blob_table *blob_table)
348 struct blob_descriptor *new_blob;
350 new_blob = new_blob_from_data_buffer(data, size, blob_table);
354 inode_replace_stream_blob(inode, strm, new_blob, blob_table);
359 * Add a new stream to the specified inode and assign it the specified data.
362 * The inode to which to add the stream
364 * The type of the stream being added
366 * The name of the stream being added as a null-terminated UTF-16LE string,
367 * or NO_STREAM_NAME if the stream is unnamed
369 * The buffer of data to assign to the new stream
371 * Size of the @data buffer, in bytes
373 * Pointer to the blob table in which data blobs are being indexed
375 * Returns true if successful; false with errno set if unsuccessful.
378 inode_add_stream_with_data(struct wim_inode *inode,
379 int stream_type, const utf16lechar *stream_name,
380 const void *data, size_t size,
381 struct blob_table *blob_table)
383 struct wim_inode_stream *strm;
384 struct blob_descriptor *blob;
386 strm = inode_add_stream(inode, stream_type, stream_name, NULL);
390 blob = new_blob_from_data_buffer(data, size, blob_table);
392 inode_remove_stream(inode, strm, blob_table);
396 inode_set_stream_blob(inode, strm, blob);
401 * Remove a stream from the specified inode.
403 * This handles releasing the references to the blob descriptor, if any.
406 inode_remove_stream(struct wim_inode *inode, struct wim_inode_stream *strm,
407 struct blob_table *blob_table)
409 unsigned idx = strm - inode->i_streams;
411 wimlib_assert(idx < inode->i_num_streams);
413 inode_unset_stream_blob(inode, strm, blob_table);
415 destroy_stream(strm);
417 memmove(strm, strm + 1,
418 (inode->i_num_streams - idx - 1) * sizeof(inode->i_streams[0]));
419 inode->i_num_streams--;
422 /* Returns true iff the specified inode has at least one named data stream. */
424 inode_has_named_data_stream(const struct wim_inode *inode)
426 for (unsigned i = 0; i < inode->i_num_streams; i++)
427 if (stream_is_named_data_stream(&inode->i_streams[i]))
433 * Resolve an inode's streams.
435 * For each stream, this replaces the SHA-1 message digest of the blob data with
436 * a pointer to the 'struct blob_descriptor' for the blob. Blob descriptors are
437 * looked up in @table.
439 * If @force is %false:
440 * If any of the needed blobs do not exist in @table, return
441 * WIMLIB_ERR_RESOURCE_NOT_FOUND and leave the inode unmodified.
442 * If @force is %true:
443 * If any of the needed blobs do not exist in @table, allocate new blob
444 * descriptors for them and insert them into @table. This does not, of
445 * course, cause the data of these blobs to magically exist, but this is
446 * needed by the code for extraction from a pipe.
448 * Returns 0 on success; WIMLIB_ERR_NOMEM if out of memory; or
449 * WIMLIB_ERR_RESOURCE_NOT_FOUND if @force is %false and at least one blob
450 * referenced by the inode was missing.
453 inode_resolve_streams(struct wim_inode *inode, struct blob_table *table,
456 struct blob_descriptor *blobs[inode->i_num_streams];
458 for (unsigned i = 0; i < inode->i_num_streams; i++) {
460 if (inode->i_streams[i].stream_resolved)
463 const u8 *hash = stream_hash(&inode->i_streams[i]);
464 struct blob_descriptor *blob = NULL;
466 if (!is_zero_hash(hash)) {
467 blob = lookup_blob(table, hash);
470 return blob_not_found_error(inode, hash);
471 blob = new_blob_descriptor();
473 return WIMLIB_ERR_NOMEM;
474 copy_hash(blob->hash, hash);
475 blob_table_insert(table, blob);
481 for (unsigned i = 0; i < inode->i_num_streams; i++) {
482 if (!inode->i_streams[i].stream_resolved) {
483 inode->i_streams[i]._stream_blob = blobs[i];
484 inode->i_streams[i].stream_resolved = 1;
490 /* Undo the effects of inode_resolve_streams(). */
492 inode_unresolve_streams(struct wim_inode *inode)
494 for (unsigned i = 0; i < inode->i_num_streams; i++) {
496 if (!inode->i_streams[i].stream_resolved)
499 copy_hash(inode->i_streams[i]._stream_hash,
500 stream_hash(&inode->i_streams[i]));
501 inode->i_streams[i].stream_resolved = 0;
506 blob_not_found_error(const struct wim_inode *inode, const u8 *hash)
508 if (wimlib_print_errors) {
509 tchar hashstr[SHA1_HASH_SIZE * 2 + 1];
511 sprint_hash(hash, hashstr);
513 ERROR("\"%"TS"\": blob not found\n"
514 " SHA-1 message digest of missing blob:\n"
516 inode_first_full_path(inode), hashstr);
518 return WIMLIB_ERR_RESOURCE_NOT_FOUND;
522 * Return the blob descriptor for the specified stream, or NULL if the blob for
523 * the stream is empty or not available.
525 struct blob_descriptor *
526 stream_blob(const struct wim_inode_stream *strm, const struct blob_table *table)
528 if (strm->stream_resolved)
529 return strm->_stream_blob;
531 return lookup_blob(table, strm->_stream_hash);
534 /* Return the SHA-1 message digest of the data of the specified stream, or a
535 * void SHA-1 of all zeroes if the specified stream is empty. */
537 stream_hash(const struct wim_inode_stream *strm)
539 if (strm->stream_resolved)
540 return strm->_stream_blob ? strm->_stream_blob->hash : zero_hash;
542 return strm->_stream_hash;
546 * Return the blob descriptor for the unnamed data stream of the inode, or NULL
547 * if the inode does not have an unnamed data stream, the blob for the inode's
548 * unnamed data stream is empty, or the blob for the inode's unnamed data stream
549 * is not available in @blob_table.
551 struct blob_descriptor *
552 inode_get_blob_for_unnamed_data_stream(const struct wim_inode *inode,
553 const struct blob_table *blob_table)
555 const struct wim_inode_stream *strm;
557 strm = inode_get_unnamed_stream(inode, STREAM_TYPE_DATA);
561 return stream_blob(strm, blob_table);
564 /* Like inode_get_blob_for_unnamed_data_stream(), but assumes the unnamed data
565 * stream is resolved. */
566 struct blob_descriptor *
567 inode_get_blob_for_unnamed_data_stream_resolved(const struct wim_inode *inode)
569 const struct wim_inode_stream *strm;
571 strm = inode_get_unnamed_stream(inode, STREAM_TYPE_DATA);
575 return stream_blob_resolved(strm);
579 * Return the SHA-1 message digest of the unnamed data stream of the inode, or a
580 * void SHA-1 of all zeroes if the inode does not have an unnamed data stream or
581 * if the inode's unnamed data stream is empty.
584 inode_get_hash_of_unnamed_data_stream(const struct wim_inode *inode)
586 const struct wim_inode_stream *strm;
588 strm = inode_get_unnamed_stream(inode, STREAM_TYPE_DATA);
592 return stream_hash(strm);
595 /* Acquire another reference to each blob referenced by this inode. This is
596 * necessary when creating a hard link to this inode.
598 * All streams of the inode must be resolved. */
600 inode_ref_blobs(struct wim_inode *inode)
602 for (unsigned i = 0; i < inode->i_num_streams; i++) {
603 struct blob_descriptor *blob;
605 blob = stream_blob_resolved(&inode->i_streams[i]);
611 /* Release a reference to each blob referenced by this inode. This is necessary
612 * when deleting a hard link to this inode. */
614 inode_unref_blobs(struct wim_inode *inode, struct blob_table *blob_table)
616 for (unsigned i = 0; i < inode->i_num_streams; i++) {
617 struct blob_descriptor *blob;
619 blob = stream_blob(&inode->i_streams[i], blob_table);
621 blob_decrement_refcnt(blob, blob_table);
626 * Given a blob descriptor, return a pointer to the pointer contained in the
627 * stream that references it.
629 * This is only possible for "unhashed" blobs, which are guaranteed to have only
630 * one referencing stream, and that reference is guaranteed to be in a resolved
631 * stream. (It can't be in an unresolved stream, since that would imply the
634 struct blob_descriptor **
635 retrieve_pointer_to_unhashed_blob(struct blob_descriptor *blob)
637 wimlib_assert(blob->unhashed);
639 struct wim_inode *inode = blob->back_inode;
640 for (unsigned i = 0; i < inode->i_num_streams; i++) {
641 if (inode->i_streams[i].stream_id == blob->back_stream_id) {
642 wimlib_assert(inode->i_streams[i]._stream_blob == blob);
643 return &inode->i_streams[i]._stream_blob;