]> wimlib.net Git - wimlib/blob - src/inode.c
Limit exposure of dentry and inode creation
[wimlib] / src / inode.c
1 /*
2  * inode.c
3  *
4  * Functions that operate on WIM inodes.
5  *
6  * See dentry.c for a description of the relationship between WIM dentries and
7  * WIM inodes.
8  */
9
10 /*
11  * Copyright (C) 2012, 2013, 2014, 2015 Eric Biggers
12  *
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
16  * later version.
17  *
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
21  * details.
22  *
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/.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 #  include "config.h"
29 #endif
30
31 #include <errno.h>
32
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"
40
41 /*
42  * The 'stream_name' field of unnamed streams always points to this array, which
43  * is an empty UTF-16 string.
44  */
45 const utf16lechar NO_STREAM_NAME[1];
46
47 /* Allocate a new inode and associate the specified dentry with it.  */
48 struct wim_inode *
49 new_inode(struct wim_dentry *dentry, bool set_timestamps)
50 {
51         struct wim_inode *inode;
52
53         inode = CALLOC(1, sizeof(struct wim_inode));
54         if (!inode)
55                 return NULL;
56
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         if (set_timestamps) {
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;
67         }
68         d_associate(dentry, inode);
69         return inode;
70 }
71
72 static inline void
73 destroy_stream(struct wim_inode_stream *strm)
74 {
75         if (strm->stream_name != NO_STREAM_NAME)
76                 FREE(strm->stream_name);
77 }
78
79 static void
80 free_inode(struct wim_inode *inode)
81 {
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);
86         if (inode->i_extra)
87                 FREE(inode->i_extra);
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);
92         FREE(inode);
93 }
94
95 static inline void
96 free_inode_if_unneeded(struct wim_inode *inode)
97 {
98         if (inode->i_nlink)
99                 return;
100 #ifdef WITH_FUSE
101         if (inode->i_num_opened_fds)
102                 return;
103 #endif
104         free_inode(inode);
105 }
106
107 /* Associate a dentry with the specified inode.  */
108 void
109 d_associate(struct wim_dentry *dentry, struct wim_inode *inode)
110 {
111         wimlib_assert(!dentry->d_inode);
112
113         list_add_tail(&dentry->d_alias, &inode->i_dentry);
114         dentry->d_inode = inode;
115         inode->i_nlink++;
116 }
117
118 /* Disassociate a dentry from its inode, if any.  Following this, free the inode
119  * if it is no longer in use.  */
120 void
121 d_disassociate(struct wim_dentry *dentry)
122 {
123         struct wim_inode *inode = dentry->d_inode;
124
125         if (unlikely(!inode))
126                 return;
127
128         wimlib_assert(inode->i_nlink > 0);
129
130         list_del(&dentry->d_alias);
131         dentry->d_inode = NULL;
132         inode->i_nlink--;
133
134         free_inode_if_unneeded(inode);
135 }
136
137 #ifdef WITH_FUSE
138 void
139 inode_dec_num_opened_fds(struct wim_inode *inode)
140 {
141         wimlib_assert(inode->i_num_opened_fds > 0);
142
143         if (--inode->i_num_opened_fds == 0) {
144                 /* The last file descriptor to this inode was closed.  */
145                 FREE(inode->i_fds);
146                 inode->i_fds = NULL;
147                 inode->i_num_allocated_fds = 0;
148
149                 free_inode_if_unneeded(inode);
150         }
151 }
152 #endif
153
154 /*
155  * Retrieve a stream of an inode.
156  *
157  * @inode
158  *      The inode from which the stream is desired
159  * @stream_type
160  *      The type of the stream desired
161  * @stream_name
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
164  *
165  * Returns a pointer to the stream if found, otherwise NULL.
166  */
167 struct wim_inode_stream *
168 inode_get_stream(const struct wim_inode *inode, int stream_type,
169                  const utf16lechar *stream_name)
170 {
171         if (stream_name == NO_STREAM_NAME)  /* Optimization  */
172                 return inode_get_unnamed_stream(inode, stream_type);
173
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))
179                 {
180                         return strm;
181                 }
182         }
183         return NULL;
184 }
185
186 /*
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.
189  */
190 struct wim_inode_stream *
191 inode_get_unnamed_stream(const struct wim_inode *inode, int stream_type)
192 {
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)
197                 {
198                         return strm;
199                 }
200         }
201         return NULL;
202 }
203
204 /*
205  * Add a new stream to the specified inode.
206  *
207  * @inode
208  *      The inode to which to add the stream
209  * @stream_type
210  *      The type of the stream being added
211  * @stream_name
212  *      The name of the stream being added as a null-terminated UTF-16LE string,
213  *      or NO_STREAM_NAME if the stream is unnamed
214  * @blob
215  *      The blob that the new stream will initially reference, or NULL
216  *
217  * Returns a pointer to the new stream, or NULL with errno set if it could not
218  * be added.
219  */
220 struct wim_inode_stream *
221 inode_add_stream(struct wim_inode *inode, int stream_type,
222                  const utf16lechar *stream_name, struct blob_descriptor *blob)
223 {
224         if (inode->i_num_streams >= 0xFFFF) {
225                 ERROR("Inode has too many streams! Path=\"%"TS"\"",
226                       inode_first_full_path(inode));
227                 errno = EFBIG;
228                 return NULL;
229         }
230
231         struct wim_inode_stream *streams;
232         struct wim_inode_stream *new_strm;
233
234         if (inode->i_streams == inode->i_embedded_streams) {
235                 if (inode->i_num_streams < ARRAY_LEN(inode->i_embedded_streams)) {
236                         streams = inode->i_embedded_streams;
237                 } else {
238                         streams = MALLOC((inode->i_num_streams + 1) *
239                                                 sizeof(inode->i_streams[0]));
240                         if (!streams)
241                                 return NULL;
242                         memcpy(streams, inode->i_streams,
243                                (inode->i_num_streams *
244                                         sizeof(inode->i_streams[0])));
245                         inode->i_streams = streams;
246                 }
247         } else {
248                 streams = REALLOC(inode->i_streams,
249                                   (inode->i_num_streams + 1) *
250                                         sizeof(inode->i_streams[0]));
251                 if (!streams)
252                         return NULL;
253                 inode->i_streams = streams;
254         }
255         new_strm = &streams[inode->i_num_streams];
256
257         memset(new_strm, 0, sizeof(*new_strm));
258
259         new_strm->stream_type = stream_type;
260         if (!*stream_name) {
261                 /* Unnamed stream  */
262                 new_strm->stream_name = (utf16lechar *)NO_STREAM_NAME;
263         } else {
264                 /* Named stream  */
265                 new_strm->stream_name = utf16le_dup(stream_name);
266                 if (!new_strm->stream_name)
267                         return NULL;
268         }
269         new_strm->stream_id = inode->i_next_stream_id++;
270
271         stream_set_blob(new_strm, blob);
272
273         inode->i_num_streams++;
274
275         return new_strm;
276 }
277
278 /*
279  * Create a new blob descriptor for the specified data buffer or use an existing
280  * blob descriptor in @blob_table for an identical blob, then add a stream of
281  * the specified type and name to the specified inode and set it to initially
282  * reference the blob.
283  *
284  * @inode
285  *      The inode to which to add the stream
286  * @stream_type
287  *      The type of the stream being added
288  * @stream_name
289  *      The name of the stream being added as a null-terminated UTF-16LE string,
290  *      or NO_STREAM_NAME if the stream is unnamed
291  * @data
292  *      The uncompressed data of the blob
293  * @size
294  *      The size, in bytes, of the blob data
295  * @blob_table
296  *      Pointer to the blob table in which the blob needs to be indexed.
297  *
298  * Returns a pointer to the new stream if successfully added, otherwise NULL
299  * with errno set.
300  */
301 struct wim_inode_stream *
302 inode_add_stream_with_data(struct wim_inode *inode,
303                            int stream_type, const utf16lechar *stream_name,
304                            const void *data, size_t size,
305                            struct blob_table *blob_table)
306 {
307         struct blob_descriptor *blob;
308         struct wim_inode_stream *strm;
309
310         blob = new_blob_from_data_buffer(data, size, blob_table);
311         if (!blob)
312                 return NULL;
313         strm = inode_add_stream(inode, stream_type, stream_name, blob);
314         if (!strm)
315                 blob_decrement_refcnt(blob, blob_table);
316         return strm;
317 }
318
319 /*
320  * Remove a stream from the specified inode and release the reference to the
321  * blob descriptor, if any.
322  */
323 void
324 inode_remove_stream(struct wim_inode *inode, struct wim_inode_stream *strm,
325                     struct blob_table *blob_table)
326 {
327         struct blob_descriptor *blob;
328         unsigned idx = strm - inode->i_streams;
329
330         wimlib_assert(idx < inode->i_num_streams);
331
332         blob = stream_blob(strm, blob_table);
333         if (blob)
334                 blob_decrement_refcnt(blob, blob_table);
335
336         destroy_stream(strm);
337
338         memmove(&inode->i_streams[idx],
339                 &inode->i_streams[idx + 1],
340                 (inode->i_num_streams - idx - 1) * sizeof(inode->i_streams[0]));
341         inode->i_num_streams--;
342 }
343
344 /* Returns true iff the specified inode has at least one named data stream.  */
345 bool
346 inode_has_named_data_stream(const struct wim_inode *inode)
347 {
348         for (unsigned i = 0; i < inode->i_num_streams; i++)
349                 if (stream_is_named_data_stream(&inode->i_streams[i]))
350                         return true;
351         return false;
352 }
353
354 /*
355  * Resolve an inode's streams.
356  *
357  * For each stream, this replaces the SHA-1 message digest of the blob data with
358  * a pointer to the 'struct blob_descriptor' for the blob.  Blob descriptors are
359  * looked up in @table.
360  *
361  * If @force is %false:
362  *      If any of the needed blobs do not exist in @table, return
363  *      WIMLIB_ERR_RESOURCE_NOT_FOUND and leave the inode unmodified.
364  * If @force is %true:
365  *      If any of the needed blobs do not exist in @table, allocate new blob
366  *      descriptors for them and insert them into @table.  This does not, of
367  *      course, cause the data of these blobs to magically exist, but this is
368  *      needed by the code for extraction from a pipe.
369  *
370  * Returns 0 on success; WIMLIB_ERR_NOMEM if out of memory; or
371  * WIMLIB_ERR_RESOURCE_NOT_FOUND if @force is %false and at least one blob
372  * referenced by the inode was missing.
373  */
374 int
375 inode_resolve_streams(struct wim_inode *inode, struct blob_table *table,
376                       bool force)
377 {
378         struct blob_descriptor *blobs[inode->i_num_streams];
379
380         for (unsigned i = 0; i < inode->i_num_streams; i++) {
381
382                 if (inode->i_streams[i].stream_resolved)
383                         continue;
384
385                 const u8 *hash = stream_hash(&inode->i_streams[i]);
386                 struct blob_descriptor *blob = NULL;
387
388                 if (!is_zero_hash(hash)) {
389                         blob = lookup_blob(table, hash);
390                         if (!blob) {
391                                 if (!force)
392                                         return blob_not_found_error(inode, hash);
393                                 blob = new_blob_descriptor();
394                                 if (!blob)
395                                         return WIMLIB_ERR_NOMEM;
396                                 copy_hash(blob->hash, hash);
397                                 blob_table_insert(table, blob);
398                         }
399                 }
400                 blobs[i] = blob;
401         }
402
403         for (unsigned i = 0; i < inode->i_num_streams; i++)
404                 if (!inode->i_streams[i].stream_resolved)
405                         stream_set_blob(&inode->i_streams[i], blobs[i]);
406         return 0;
407 }
408
409 /* Undo the effects of inode_resolve_streams().  */
410 void
411 inode_unresolve_streams(struct wim_inode *inode)
412 {
413         for (unsigned i = 0; i < inode->i_num_streams; i++) {
414
415                 if (!inode->i_streams[i].stream_resolved)
416                         continue;
417
418                 copy_hash(inode->i_streams[i]._stream_hash,
419                           stream_hash(&inode->i_streams[i]));
420                 inode->i_streams[i].stream_resolved = 0;
421         }
422 }
423
424 int
425 blob_not_found_error(const struct wim_inode *inode, const u8 *hash)
426 {
427         if (wimlib_print_errors) {
428                 tchar hashstr[SHA1_HASH_SIZE * 2 + 1];
429
430                 sprint_hash(hash, hashstr);
431
432                 ERROR("\"%"TS"\": blob not found\n"
433                       "        SHA-1 message digest of missing blob:\n"
434                       "        %"TS"",
435                       inode_first_full_path(inode), hashstr);
436         }
437         return WIMLIB_ERR_RESOURCE_NOT_FOUND;
438 }
439
440 /*
441  * Return the blob descriptor for the specified stream, or NULL if the blob for
442  * the stream is empty or not available.
443  */
444 struct blob_descriptor *
445 stream_blob(const struct wim_inode_stream *strm, const struct blob_table *table)
446 {
447         if (strm->stream_resolved)
448                 return strm->_stream_blob;
449         else
450                 return lookup_blob(table, strm->_stream_hash);
451 }
452
453 /* Return the SHA-1 message digest of the data of the specified stream, or a
454  * void SHA-1 of all zeroes if the specified stream is empty.   */
455 const u8 *
456 stream_hash(const struct wim_inode_stream *strm)
457 {
458         if (strm->stream_resolved)
459                 return strm->_stream_blob ? strm->_stream_blob->hash : zero_hash;
460         else
461                 return strm->_stream_hash;
462 }
463
464 /*
465  * Return the blob descriptor for the unnamed data stream of the inode, or NULL
466  * if the inode does not have an unnamed data stream, the blob for the inode's
467  * unnamed data stream is empty, or the blob for the inode's unnamed data stream
468  * is not available in @blob_table.
469  */
470 struct blob_descriptor *
471 inode_get_blob_for_unnamed_data_stream(const struct wim_inode *inode,
472                                        const struct blob_table *blob_table)
473 {
474         const struct wim_inode_stream *strm;
475
476         strm = inode_get_unnamed_stream(inode, STREAM_TYPE_DATA);
477         if (!strm)
478                 return NULL;
479
480         return stream_blob(strm, blob_table);
481 }
482
483 /* Like inode_get_blob_for_unnamed_data_stream(), but assumes the unnamed data
484  * stream is resolved.  */
485 struct blob_descriptor *
486 inode_get_blob_for_unnamed_data_stream_resolved(const struct wim_inode *inode)
487 {
488         const struct wim_inode_stream *strm;
489
490         strm = inode_get_unnamed_stream(inode, STREAM_TYPE_DATA);
491         if (!strm)
492                 return NULL;
493
494         return stream_blob_resolved(strm);
495 }
496
497 /*
498  * Return the SHA-1 message digest of the unnamed data stream of the inode, or a
499  * void SHA-1 of all zeroes if the inode does not have an unnamed data stream or
500  * if the inode's unnamed data stream is empty.
501  */
502 const u8 *
503 inode_get_hash_of_unnamed_data_stream(const struct wim_inode *inode)
504 {
505         const struct wim_inode_stream *strm;
506
507         strm = inode_get_unnamed_stream(inode, STREAM_TYPE_DATA);
508         if (!strm)
509                 return zero_hash;
510
511         return stream_hash(strm);
512 }
513
514 /* Acquire another reference to each blob referenced by this inode.  This is
515  * necessary when creating a hard link to this inode.
516  *
517  * All streams of the inode must be resolved.  */
518 void
519 inode_ref_blobs(struct wim_inode *inode)
520 {
521         for (unsigned i = 0; i < inode->i_num_streams; i++) {
522                 struct blob_descriptor *blob;
523
524                 blob = stream_blob_resolved(&inode->i_streams[i]);
525                 if (blob)
526                         blob->refcnt++;
527         }
528 }
529
530 /* Release a reference to each blob referenced by this inode.  This is necessary
531  * when deleting a hard link to this inode.  */
532 void
533 inode_unref_blobs(struct wim_inode *inode, struct blob_table *blob_table)
534 {
535         for (unsigned i = 0; i < inode->i_num_streams; i++) {
536                 struct blob_descriptor *blob;
537
538                 blob = stream_blob(&inode->i_streams[i], blob_table);
539                 if (blob)
540                         blob_decrement_refcnt(blob, blob_table);
541         }
542 }
543
544 /*
545  * Given a blob descriptor, return a pointer to the pointer contained in the
546  * stream that references it.
547  *
548  * This is only possible for "unhashed" blobs, which are guaranteed to have only
549  * one referencing stream, and that reference is guaranteed to be in a resolved
550  * stream.  (It can't be in an unresolved stream, since that would imply the
551  * hash is known!)
552  */
553 struct blob_descriptor **
554 retrieve_pointer_to_unhashed_blob(struct blob_descriptor *blob)
555 {
556         wimlib_assert(blob->unhashed);
557
558         struct wim_inode *inode = blob->back_inode;
559         for (unsigned i = 0; i < inode->i_num_streams; i++) {
560                 if (inode->i_streams[i].stream_id == blob->back_stream_id) {
561                         wimlib_assert(inode->i_streams[i]._stream_blob == blob);
562                         return &inode->i_streams[i]._stream_blob;
563                 }
564         }
565
566         wimlib_assert(0);
567         return NULL;
568 }