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);
63 u64 now = now_as_wim_timestamp();
64 inode->i_creation_time = now;
65 inode->i_last_access_time = now;
66 inode->i_last_write_time = now;
68 d_associate(dentry, inode);
73 destroy_stream(struct wim_inode_stream *strm)
75 if (strm->stream_name != NO_STREAM_NAME)
76 FREE(strm->stream_name);
80 free_inode(struct wim_inode *inode)
82 for (unsigned i = 0; i < inode->i_num_streams; i++)
83 destroy_stream(&inode->i_streams[i]);
84 if (inode->i_streams != inode->i_embedded_streams)
85 FREE(inode->i_streams);
88 /* HACK: This may instead delete the inode from i_list, but hlist_del()
89 * behaves the same as list_del(). */
90 if (!hlist_unhashed(&inode->i_hlist))
91 hlist_del(&inode->i_hlist);
96 free_inode_if_unneeded(struct wim_inode *inode)
101 if (inode->i_num_opened_fds)
107 /* Associate a dentry with the specified inode. */
109 d_associate(struct wim_dentry *dentry, struct wim_inode *inode)
111 wimlib_assert(!dentry->d_inode);
113 list_add_tail(&dentry->d_alias, &inode->i_dentry);
114 dentry->d_inode = inode;
118 /* Disassociate a dentry from its inode, if any. Following this, free the inode
119 * if it is no longer in use. */
121 d_disassociate(struct wim_dentry *dentry)
123 struct wim_inode *inode = dentry->d_inode;
125 if (unlikely(!inode))
128 wimlib_assert(inode->i_nlink > 0);
130 list_del(&dentry->d_alias);
131 dentry->d_inode = NULL;
134 free_inode_if_unneeded(inode);
139 inode_dec_num_opened_fds(struct wim_inode *inode)
141 wimlib_assert(inode->i_num_opened_fds > 0);
143 if (--inode->i_num_opened_fds == 0) {
144 /* The last file descriptor to this inode was closed. */
147 inode->i_num_allocated_fds = 0;
149 free_inode_if_unneeded(inode);
155 * Retrieve a stream of an inode.
158 * The inode from which the stream is desired
160 * The type of the stream desired
162 * The name of the stream desired as a null-terminated UTF-16LE string, or
163 * NO_STREAM_NAME if an unnamed stream is desired
165 * Returns a pointer to the stream if found, otherwise NULL.
167 struct wim_inode_stream *
168 inode_get_stream(const struct wim_inode *inode, int stream_type,
169 const utf16lechar *stream_name)
171 if (stream_name == NO_STREAM_NAME) /* Optimization */
172 return inode_get_unnamed_stream(inode, stream_type);
174 for (unsigned i = 0; i < inode->i_num_streams; i++) {
175 struct wim_inode_stream *strm = &inode->i_streams[i];
176 if (strm->stream_type == stream_type &&
177 !cmp_utf16le_strings_z(strm->stream_name, stream_name,
178 default_ignore_case))
187 * This is equivalent to inode_get_stream(inode, stream_type, NO_STREAM_NAME),
188 * but this optimizes for the unnamed case by not doing full string comparisons.
190 struct wim_inode_stream *
191 inode_get_unnamed_stream(const struct wim_inode *inode, int stream_type)
193 for (unsigned i = 0; i < inode->i_num_streams; i++) {
194 struct wim_inode_stream *strm = &inode->i_streams[i];
195 if (strm->stream_type == stream_type &&
196 strm->stream_name == NO_STREAM_NAME)
206 inode_set_stream_blob(struct wim_inode *inode, struct wim_inode_stream *strm,
207 struct blob_descriptor *new_blob)
209 strm->_stream_blob = new_blob;
210 strm->stream_resolved = 1;
212 new_blob->refcnt += inode->i_nlink;
216 inode_unset_stream_blob(struct wim_inode *inode, struct wim_inode_stream *strm,
217 struct blob_table *blob_table)
219 struct blob_descriptor *old_blob;
221 old_blob = stream_blob(strm, blob_table);
223 blob_subtract_refcnt(old_blob, blob_table, inode->i_nlink);
224 strm->_stream_blob = NULL;
225 strm->stream_resolved = 1;
229 * Replace the blob associated with the specified stream.
232 * The inode containing @strm
234 * The stream whose data needs to be replaced
236 * The new blob descriptor to assign
238 * Pointer to the blob table in which data blobs are being indexed
241 inode_replace_stream_blob(struct wim_inode *inode,
242 struct wim_inode_stream *strm,
243 struct blob_descriptor *new_blob,
244 struct blob_table *blob_table)
246 inode_unset_stream_blob(inode, strm, blob_table);
247 inode_set_stream_blob(inode, strm, new_blob);
251 * Add a new stream to the specified inode.
254 * The inode to which to add the stream
256 * The type of the stream being added
258 * The name of the stream being added as a null-terminated UTF-16LE string,
259 * or NO_STREAM_NAME if the stream is unnamed
261 * The blob that the new stream will initially reference, or NULL
263 * Returns a pointer to the new stream, or NULL with errno set if it could not
266 struct wim_inode_stream *
267 inode_add_stream(struct wim_inode *inode, int stream_type,
268 const utf16lechar *stream_name, struct blob_descriptor *blob)
270 if (inode->i_num_streams >= 0xFFFF) {
271 ERROR("Inode has too many streams! Path=\"%"TS"\"",
272 inode_first_full_path(inode));
277 struct wim_inode_stream *streams;
278 struct wim_inode_stream *new_strm;
280 if (inode->i_streams == inode->i_embedded_streams) {
281 if (inode->i_num_streams < ARRAY_LEN(inode->i_embedded_streams)) {
282 streams = inode->i_embedded_streams;
284 streams = MALLOC((inode->i_num_streams + 1) *
285 sizeof(inode->i_streams[0]));
288 memcpy(streams, inode->i_streams,
289 (inode->i_num_streams *
290 sizeof(inode->i_streams[0])));
291 inode->i_streams = streams;
294 streams = REALLOC(inode->i_streams,
295 (inode->i_num_streams + 1) *
296 sizeof(inode->i_streams[0]));
299 inode->i_streams = streams;
301 new_strm = &streams[inode->i_num_streams];
303 memset(new_strm, 0, sizeof(*new_strm));
305 new_strm->stream_type = stream_type;
308 new_strm->stream_name = (utf16lechar *)NO_STREAM_NAME;
311 new_strm->stream_name = utf16le_dup(stream_name);
312 if (!new_strm->stream_name)
316 new_strm->stream_id = inode->i_next_stream_id++;
318 inode_set_stream_blob(inode, new_strm, blob);
320 inode->i_num_streams++;
326 * Replace the data of the specified stream.
329 * The inode containing @strm
331 * The stream whose data needs to be replaced
333 * The buffer of data to assign to the stream
335 * Size of the @data buffer, in bytes
337 * Pointer to the blob table in which data blobs are being indexed
339 * Returns true if successful; false with errno set if unsuccessful.
342 inode_replace_stream_data(struct wim_inode *inode,
343 struct wim_inode_stream *strm,
344 const void *data, size_t size,
345 struct blob_table *blob_table)
347 struct blob_descriptor *new_blob;
349 new_blob = new_blob_from_data_buffer(data, size, blob_table);
353 inode_replace_stream_blob(inode, strm, new_blob, blob_table);
358 * Add a new stream to the specified inode and assign it the specified data.
361 * The inode to which to add the stream
363 * The type of the stream being added
365 * The name of the stream being added as a null-terminated UTF-16LE string,
366 * or NO_STREAM_NAME if the stream is unnamed
368 * The buffer of data to assign to the new stream
370 * Size of the @data buffer, in bytes
372 * Pointer to the blob table in which data blobs are being indexed
374 * Returns true if successful; false with errno set if unsuccessful.
377 inode_add_stream_with_data(struct wim_inode *inode,
378 int stream_type, const utf16lechar *stream_name,
379 const void *data, size_t size,
380 struct blob_table *blob_table)
382 struct wim_inode_stream *strm;
383 struct blob_descriptor *blob;
385 strm = inode_add_stream(inode, stream_type, stream_name, NULL);
389 blob = new_blob_from_data_buffer(data, size, blob_table);
391 inode_remove_stream(inode, strm, blob_table);
395 inode_set_stream_blob(inode, strm, blob);
400 * Remove a stream from the specified inode.
402 * This handles releasing the references to the blob descriptor, if any.
405 inode_remove_stream(struct wim_inode *inode, struct wim_inode_stream *strm,
406 struct blob_table *blob_table)
408 unsigned idx = strm - inode->i_streams;
410 wimlib_assert(idx < inode->i_num_streams);
412 inode_unset_stream_blob(inode, strm, blob_table);
414 destroy_stream(strm);
416 memmove(strm, strm + 1,
417 (inode->i_num_streams - idx - 1) * sizeof(inode->i_streams[0]));
418 inode->i_num_streams--;
421 /* Returns true iff the specified inode has at least one named data stream. */
423 inode_has_named_data_stream(const struct wim_inode *inode)
425 for (unsigned i = 0; i < inode->i_num_streams; i++)
426 if (stream_is_named_data_stream(&inode->i_streams[i]))
432 * Resolve an inode's streams.
434 * For each stream, this replaces the SHA-1 message digest of the blob data with
435 * a pointer to the 'struct blob_descriptor' for the blob. Blob descriptors are
436 * looked up in @table.
438 * If @force is %false:
439 * If any of the needed blobs do not exist in @table, return
440 * WIMLIB_ERR_RESOURCE_NOT_FOUND and leave the inode unmodified.
441 * If @force is %true:
442 * If any of the needed blobs do not exist in @table, allocate new blob
443 * descriptors for them and insert them into @table. This does not, of
444 * course, cause the data of these blobs to magically exist, but this is
445 * needed by the code for extraction from a pipe.
447 * Returns 0 on success; WIMLIB_ERR_NOMEM if out of memory; or
448 * WIMLIB_ERR_RESOURCE_NOT_FOUND if @force is %false and at least one blob
449 * referenced by the inode was missing.
452 inode_resolve_streams(struct wim_inode *inode, struct blob_table *table,
455 struct blob_descriptor *blobs[inode->i_num_streams];
457 for (unsigned i = 0; i < inode->i_num_streams; i++) {
459 if (inode->i_streams[i].stream_resolved)
462 const u8 *hash = stream_hash(&inode->i_streams[i]);
463 struct blob_descriptor *blob = NULL;
465 if (!is_zero_hash(hash)) {
466 blob = lookup_blob(table, hash);
469 return blob_not_found_error(inode, hash);
470 blob = new_blob_descriptor();
472 return WIMLIB_ERR_NOMEM;
473 copy_hash(blob->hash, hash);
474 blob_table_insert(table, blob);
480 for (unsigned i = 0; i < inode->i_num_streams; i++) {
481 if (!inode->i_streams[i].stream_resolved) {
482 inode->i_streams[i]._stream_blob = blobs[i];
483 inode->i_streams[i].stream_resolved = 1;
489 /* Undo the effects of inode_resolve_streams(). */
491 inode_unresolve_streams(struct wim_inode *inode)
493 for (unsigned i = 0; i < inode->i_num_streams; i++) {
495 if (!inode->i_streams[i].stream_resolved)
498 copy_hash(inode->i_streams[i]._stream_hash,
499 stream_hash(&inode->i_streams[i]));
500 inode->i_streams[i].stream_resolved = 0;
505 blob_not_found_error(const struct wim_inode *inode, const u8 *hash)
507 if (wimlib_print_errors) {
508 tchar hashstr[SHA1_HASH_SIZE * 2 + 1];
510 sprint_hash(hash, hashstr);
512 ERROR("\"%"TS"\": blob not found\n"
513 " SHA-1 message digest of missing blob:\n"
515 inode_first_full_path(inode), hashstr);
517 return WIMLIB_ERR_RESOURCE_NOT_FOUND;
521 * Return the blob descriptor for the specified stream, or NULL if the blob for
522 * the stream is empty or not available.
524 struct blob_descriptor *
525 stream_blob(const struct wim_inode_stream *strm, const struct blob_table *table)
527 if (strm->stream_resolved)
528 return strm->_stream_blob;
530 return lookup_blob(table, strm->_stream_hash);
533 /* Return the SHA-1 message digest of the data of the specified stream, or a
534 * void SHA-1 of all zeroes if the specified stream is empty. */
536 stream_hash(const struct wim_inode_stream *strm)
538 if (strm->stream_resolved)
539 return strm->_stream_blob ? strm->_stream_blob->hash : zero_hash;
541 return strm->_stream_hash;
545 * Return the blob descriptor for the unnamed data stream of the inode, or NULL
546 * if the inode does not have an unnamed data stream, the blob for the inode's
547 * unnamed data stream is empty, or the blob for the inode's unnamed data stream
548 * is not available in @blob_table.
550 struct blob_descriptor *
551 inode_get_blob_for_unnamed_data_stream(const struct wim_inode *inode,
552 const struct blob_table *blob_table)
554 const struct wim_inode_stream *strm;
556 strm = inode_get_unnamed_stream(inode, STREAM_TYPE_DATA);
560 return stream_blob(strm, blob_table);
563 /* Like inode_get_blob_for_unnamed_data_stream(), but assumes the unnamed data
564 * stream is resolved. */
565 struct blob_descriptor *
566 inode_get_blob_for_unnamed_data_stream_resolved(const struct wim_inode *inode)
568 const struct wim_inode_stream *strm;
570 strm = inode_get_unnamed_stream(inode, STREAM_TYPE_DATA);
574 return stream_blob_resolved(strm);
578 * Return the SHA-1 message digest of the unnamed data stream of the inode, or a
579 * void SHA-1 of all zeroes if the inode does not have an unnamed data stream or
580 * if the inode's unnamed data stream is empty.
583 inode_get_hash_of_unnamed_data_stream(const struct wim_inode *inode)
585 const struct wim_inode_stream *strm;
587 strm = inode_get_unnamed_stream(inode, STREAM_TYPE_DATA);
591 return stream_hash(strm);
594 /* Acquire another reference to each blob referenced by this inode. This is
595 * necessary when creating a hard link to this inode.
597 * All streams of the inode must be resolved. */
599 inode_ref_blobs(struct wim_inode *inode)
601 for (unsigned i = 0; i < inode->i_num_streams; i++) {
602 struct blob_descriptor *blob;
604 blob = stream_blob_resolved(&inode->i_streams[i]);
610 /* Release a reference to each blob referenced by this inode. This is necessary
611 * when deleting a hard link to this inode. */
613 inode_unref_blobs(struct wim_inode *inode, struct blob_table *blob_table)
615 for (unsigned i = 0; i < inode->i_num_streams; i++) {
616 struct blob_descriptor *blob;
618 blob = stream_blob(&inode->i_streams[i], blob_table);
620 blob_decrement_refcnt(blob, blob_table);
625 * Given a blob descriptor, return a pointer to the pointer contained in the
626 * stream that references it.
628 * This is only possible for "unhashed" blobs, which are guaranteed to have only
629 * one referencing stream, and that reference is guaranteed to be in a resolved
630 * stream. (It can't be in an unresolved stream, since that would imply the
633 struct blob_descriptor **
634 retrieve_pointer_to_unhashed_blob(struct blob_descriptor *blob)
636 wimlib_assert(blob->unhashed);
638 struct wim_inode *inode = blob->back_inode;
639 for (unsigned i = 0; i < inode->i_num_streams; i++) {
640 if (inode->i_streams[i].stream_id == blob->back_stream_id) {
641 wimlib_assert(inode->i_streams[i]._stream_blob == blob);
642 return &inode->i_streams[i]._stream_blob;