4 * This file implements mounting of WIM files using FUSE, which stands for
5 * Filesystem in Userspace. FUSE allows a filesystem to be implemented in a
6 * userspace process by implementing the filesystem primitives--- read(),
7 * write(), readdir(), etc.
11 * Copyright (C) 2012, 2013, 2014 Eric Biggers
13 * This file is part of wimlib, a library for working with WIM files.
15 * wimlib is free software; you can redistribute it and/or modify it under the
16 * terms of the GNU General Public License as published by the Free
17 * Software Foundation; either version 3 of the License, or (at your option)
20 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
21 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
22 * A PARTICULAR PURPOSE. See the GNU General Public License for more
25 * You should have received a copy of the GNU General Public License
26 * along with wimlib; if not, see http://www.gnu.org/licenses/.
38 # error "FUSE mount not supported on Windows! Please configure --without-fuse"
41 #include "wimlib/dentry.h"
42 #include "wimlib/encoding.h"
43 #include "wimlib/metadata.h"
44 #include "wimlib/paths.h"
45 #include "wimlib/progress.h"
46 #include "wimlib/reparse.h"
47 #include "wimlib/timestamp.h"
48 #include "wimlib/unix_data.h"
49 #include "wimlib/write.h"
50 #include "wimlib/xml.h"
61 #include <sys/types.h>
65 #define FUSE_USE_VERSION 26
67 #include <attr/xattr.h>
70 # define O_NOFOLLOW 0 /* Security only... */
73 #define WIMFS_MQUEUE_NAME_LEN 32
75 #define WIMLIB_UNMOUNT_FLAG_SEND_PROGRESS 0x80000000
77 struct wimfs_unmount_info {
78 unsigned unmount_flags;
79 char mq_name[WIMFS_MQUEUE_NAME_LEN + 1];
82 struct commit_progress_report {
83 enum wimlib_progress_msg msg;
84 union wimlib_progress_info info;
87 /* Description of an open file on a mounted WIM image. Actually, this
88 * represents the open state of a particular data stream of an inode, rather
89 * than the inode itself. (An inode might have multiple named data streams in
90 * addition to the default, unnamed data stream.) At a given time, an inode in
91 * the WIM image might have multiple file descriptors open to it, each to any
92 * one of its data streams. */
95 /* Pointer to the inode of this open file.
96 * 'i_num_opened_fds' of the inode tracks the number of file descriptors
97 * that reference it. */
98 struct wim_inode *f_inode;
100 /* Pointer to the lookup table entry for the data stream that has been
101 * opened. 'num_opened_fds' of the lookup table entry tracks the number
102 * of file descriptors that reference it. Or, this value may be NULL,
103 * which indicates that the opened stream is empty and consequently does
104 * not have a lookup table entry. */
105 struct wim_lookup_table_entry *f_lte;
107 /* If valid (filedes_valid(&f_staging_fd)), this contains the
108 * corresponding native file descriptor for the staging file that has
109 * been created for reading from and/or writing to this open stream. A
110 * single staging file might have multiple file descriptors open to it
111 * simultaneously, each used by a different 'struct wimfs_fd'.
113 * Or, if invalid (!filedes_valid(&f_staging_fd)), this 'struct
114 * wimfs_fd' is not associated with a staging file. This is permissible
115 * only if this 'struct wimfs_fd' was opened read-only and the stream
116 * has not yet been extracted to a staging file. */
117 struct filedes f_staging_fd;
119 /* 0-based index of this file descriptor in the file descriptor table of
123 /* Unique ID of the opened stream in the inode. This will stay the same
124 * even if the indices of the inode's alternate data streams are changed
129 #define WIMFS_FD(fi) ((struct wimfs_fd *)(uintptr_t)((fi)->fh))
131 /* Context structure for a mounted WIM image. */
132 struct wimfs_context {
133 /* The WIMStruct containing the mounted image. The mounted image is the
134 * currently selected image (wim->current_image). */
137 /* Flags passed to wimlib_mount_image() (WIMLIB_MOUNT_FLAG_*). */
140 /* Default flags for path lookup in the WIM image. */
141 int default_lookup_flags;
143 /* Information about the user who has mounted the WIM image */
147 /* Information about the staging directory for a read-write mount. */
150 char *staging_dir_name;
152 /* For read-write mounts, the inode number to be assigned to the next
153 * created file. Note: since this isn't a persistent filesystem and we
154 * can re-assign the inode numbers just before mounting the image, it's
155 * good enough to just generate inode numbers sequentially. */
158 /* Number of file descriptors open to the mounted WIM image. */
159 unsigned long num_open_fds;
161 /* Original list of single-instance streams in the mounted image, linked
162 * by 'struct wim_lookup_table_entry'.orig_stream_list. */
163 struct list_head orig_stream_list;
166 #define WIMFS_CTX(fuse_ctx) ((struct wimfs_context*)(fuse_ctx)->private_data)
168 /* Retrieve the context structure for the currently mounted WIM image.
170 * Note: this is a per-thread variable. It is possible for different threads to
171 * mount different images at the same time in the same process, although they
172 * must use different WIMStructs! */
173 static inline struct wimfs_context *
174 wimfs_get_context(void)
176 return WIMFS_CTX(fuse_get_context());
180 wimfs_inc_num_open_fds(void)
182 wimfs_get_context()->num_open_fds++;
186 wimfs_dec_num_open_fds(void)
188 wimfs_get_context()->num_open_fds--;
191 /* Retrieve the WIMStruct for the currently mounted WIM image. */
192 static inline WIMStruct *
193 wimfs_get_WIMStruct(void)
195 return wimfs_get_context()->wim;
199 get_lookup_flags(const struct wimfs_context *ctx)
201 return ctx->default_lookup_flags;
204 /* Is write permission requested on the file? */
206 flags_writable(int open_flags)
208 int accmode = (open_flags & O_ACCMODE);
209 return (accmode == O_RDWR || accmode == O_WRONLY);
213 fuse_mask_mode(mode_t mode, const struct fuse_context *fuse_ctx)
215 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 8)
216 mode &= ~fuse_ctx->umask;
222 * Allocate a file descriptor to a data stream in the mounted WIM image.
225 * A pointer to the inode containing the stream being opened.
227 * The ID of the data stream being opened within the inode.
229 * A pointer to the lookup table entry for the stream data. Or, for a
230 * 0-byte stream, this may be NULL.
232 * On success, a pointer to the new file descriptor will be stored here.
234 * Returns 0 or a -errno code.
237 alloc_wimfs_fd(struct wim_inode *inode,
239 struct wim_lookup_table_entry *lte,
240 struct wimfs_fd **fd_ret)
242 static const u16 min_fds_per_alloc = 8;
243 static const u16 max_fds = 0xffff;
247 if (inode->i_num_opened_fds == inode->i_num_allocated_fds) {
249 struct wimfs_fd **fds;
251 /* Expand this inode's file descriptor table. */
253 num_new_fds = max(min_fds_per_alloc,
254 inode->i_num_allocated_fds / 4);
256 num_new_fds = min(num_new_fds,
257 max_fds - inode->i_num_allocated_fds);
259 if (num_new_fds == 0)
262 fds = REALLOC(inode->i_fds,
263 (inode->i_num_allocated_fds + num_new_fds) *
268 memset(&fds[inode->i_num_allocated_fds], 0,
269 num_new_fds * sizeof(fds[0]));
271 inode->i_num_allocated_fds += num_new_fds;
272 inode->i_next_fd = inode->i_num_opened_fds;
275 /* Allocate the file descriptor in the first available space in the
276 * inode's file descriptor table.
278 * i_next_fd is the lower bound on the next open slot. */
279 for (i = inode->i_next_fd; inode->i_fds[i]; i++)
282 fd = MALLOC(sizeof(*fd));
288 filedes_invalidate(&fd->f_staging_fd);
290 fd->f_stream_id = stream_id;
292 inode->i_fds[i] = fd;
293 inode->i_num_opened_fds++;
295 lte->num_opened_fds++;
296 wimfs_inc_num_open_fds();
297 inode->i_next_fd = i + 1;
302 * Close a file descriptor to a data stream in the mounted WIM image.
304 * Returns 0 or a -errno code. The file descriptor is always closed.
307 close_wimfs_fd(struct wimfs_fd *fd)
310 struct wim_inode *inode;
312 /* Close the staging file if open. */
313 if (filedes_valid(&fd->f_staging_fd))
314 if (filedes_close(&fd->f_staging_fd))
317 /* Release this file descriptor from its lookup table entry. */
319 lte_decrement_num_opened_fds(fd->f_lte);
321 wimfs_dec_num_open_fds();
323 /* Release this file descriptor from its inode. */
325 inode->i_fds[fd->f_idx] = NULL;
326 if (fd->f_idx < inode->i_next_fd)
327 inode->i_next_fd = fd->f_idx;
329 if (--inode->i_num_opened_fds == 0) {
330 /* The last file descriptor to this inode was closed. */
333 inode->i_num_allocated_fds = 0;
334 if (inode->i_nlink == 0)
335 /* No links to this inode remain. Get rid of it. */
342 * Translate a path into the corresponding inode in the mounted WIM image.
344 * See get_dentry() for more information.
346 * Returns a pointer to the resulting inode, or NULL with errno set.
348 static struct wim_inode *
349 wim_pathname_to_inode(WIMStruct *wim, const char *path)
351 struct wim_dentry *dentry;
353 dentry = get_dentry(wim, path, WIMLIB_CASE_SENSITIVE);
356 return dentry->d_inode;
360 * Create a new file in the mounted WIM image.
363 * The FUSE context for the mounted image.
365 * The path at which to create the first link to the new file. If a file
366 * already exists at this path, -EEXIST is returned.
368 * The UNIX mode for the new file. This is only honored if
369 * WIMLIB_MOUNT_FLAG_UNIX_DATA was passed to wimlib_mount_image().
371 * The device ID for the new file, encoding the major and minor device
372 * numbers. This is only honored if WIMLIB_MOUNT_FLAG_UNIX_DATA was passed
373 * to wimlib_mount_image().
375 * Windows file attributes to use for the new file.
377 * On success, a pointer to the new dentry is returned here. Its d_inode
378 * member will point to the new inode that was created for it and added to
379 * the mounted WIM image.
381 * Returns 0 or a -errno code.
384 create_dentry(struct fuse_context *fuse_ctx, const char *path,
385 mode_t mode, dev_t rdev, u32 attributes,
386 struct wim_dentry **dentry_ret)
388 struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
389 struct wim_dentry *parent;
390 const char *basename;
391 struct wim_dentry *new_dentry;
392 struct wim_inode *new_inode;
394 parent = get_parent_dentry(wimfs_ctx->wim, path, WIMLIB_CASE_SENSITIVE);
398 if (!dentry_is_directory(parent))
401 basename = path_basename(path);
403 if (get_dentry_child_with_name(parent, basename, WIMLIB_CASE_SENSITIVE))
406 if (new_dentry_with_inode(basename, &new_dentry))
409 new_inode = new_dentry->d_inode;
411 new_inode->i_resolved = 1;
412 new_inode->i_ino = wimfs_ctx->next_ino++;
413 new_inode->i_attributes = attributes;
415 if (wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA) {
416 struct wimlib_unix_data unix_data;
418 unix_data.uid = fuse_ctx->uid;
419 unix_data.gid = fuse_ctx->gid;
420 unix_data.mode = fuse_mask_mode(mode, fuse_ctx);
421 unix_data.rdev = rdev;
422 if (!inode_set_unix_data(new_inode, &unix_data, UNIX_DATA_ALL))
424 free_dentry(new_dentry);
429 list_add_tail(&new_inode->i_list,
430 &wim_get_current_image_metadata(wimfs_ctx->wim)->inode_list);
432 dentry_add_child(parent, new_dentry);
435 *dentry_ret = new_dentry;
440 * Remove a dentry from the mounted WIM image; i.e. remove an alias for an
444 remove_dentry(struct wim_dentry *dentry,
445 struct wim_lookup_table *lookup_table)
447 /* Drop the reference to each stream the inode contains. */
448 inode_unref_streams(dentry->d_inode, lookup_table);
450 /* Unlink the dentry from the image's dentry tree. */
451 unlink_dentry(dentry);
453 /* Delete the dentry. This will also decrement the link count of the
454 * corresponding inode, and possibly cause it to be deleted as well. */
458 /* Generate UNIX filetype mode bits for the specified WIM inode, based on its
459 * Windows file attributes. */
461 inode_unix_file_type(const struct wim_inode *inode)
463 if (inode_is_symlink(inode))
465 else if (inode_is_directory(inode))
471 /* Generate a default UNIX mode for the specified WIM inode. */
473 inode_default_unix_mode(const struct wim_inode *inode)
475 return inode_unix_file_type(inode) | 0777;
479 * Retrieve standard UNIX metadata ('struct stat') for a WIM inode.
481 * @lte specifies the stream of the inode that is being queried. We mostly
482 * return the same information for all streams, but st_size and st_blocks may be
483 * different for different streams.
485 * This always returns 0.
488 inode_to_stbuf(const struct wim_inode *inode,
489 const struct wim_lookup_table_entry *lte,
492 const struct wimfs_context *ctx = wimfs_get_context();
493 struct wimlib_unix_data unix_data;
495 memset(stbuf, 0, sizeof(struct stat));
496 if ((ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA) &&
497 inode_get_unix_data(inode, &unix_data))
499 /* Use the user ID, group ID, mode, and device ID from the
500 * inode's extra UNIX metadata information. */
501 stbuf->st_uid = unix_data.uid;
502 stbuf->st_gid = unix_data.gid;
503 stbuf->st_mode = unix_data.mode;
504 stbuf->st_rdev = unix_data.rdev;
506 /* Generate default values for the user ID, group ID, and mode.
508 * Note: in the case of an allow_other mount, fuse_context.uid
509 * may not be the same as wimfs_context.owner_uid! */
510 stbuf->st_uid = ctx->owner_uid;
511 stbuf->st_gid = ctx->owner_gid;
512 stbuf->st_mode = inode_default_unix_mode(inode);
514 stbuf->st_ino = inode->i_ino;
515 stbuf->st_nlink = inode->i_nlink;
517 stbuf->st_size = lte->size;
518 #ifdef HAVE_STAT_NANOSECOND_PRECISION
519 stbuf->st_atim = wim_timestamp_to_timespec(inode->i_last_access_time);
520 stbuf->st_mtim = wim_timestamp_to_timespec(inode->i_last_write_time);
521 stbuf->st_ctim = stbuf->st_mtim;
523 stbuf->st_atime = wim_timestamp_to_unix(inode->i_last_access_time);
524 stbuf->st_mtime = wim_timestamp_to_unix(inode->i_last_write_time);
525 stbuf->st_ctime = stbuf->st_mtime;
527 stbuf->st_blocks = (stbuf->st_size + 511) / 512;
531 /* Update the last access and last write timestamps of a WIM inode. */
533 touch_inode(struct wim_inode *inode)
535 u64 now = get_wim_timestamp();
536 inode->i_last_access_time = now;
537 inode->i_last_write_time = now;
541 touch_parent(struct wim_dentry *dentry)
543 touch_inode(dentry->d_parent->d_inode);
547 * Create a new file in the staging directory for a read-write mounted image.
549 * On success, returns the file descriptor for the new staging file, opened for
550 * writing. In addition, stores the allocated name of the staging file in
553 * On failure, returns -1 and sets errno.
556 create_staging_file(const struct wimfs_context *ctx, char **name_ret)
559 static const size_t STAGING_FILE_NAME_LEN = 20;
563 name = MALLOC(STAGING_FILE_NAME_LEN + 1);
566 name[STAGING_FILE_NAME_LEN] = '\0';
569 randomize_char_array_with_alnum(name, STAGING_FILE_NAME_LEN);
570 fd = openat(ctx->staging_dir_fd, name,
571 O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, 0600);
572 if (unlikely(fd < 0)) {
573 if (unlikely(errno == EEXIST))
574 /* Try again with another name. */
584 * Extract a WIM resource to the staging directory.
585 * This is necessary if a stream using the resource is being opened for writing.
588 * The inode containing the stream being opened for writing.
591 * The index of the stream in @inode being opened for writing.
594 * *lte_ptr is the lookup table entry for the stream being extracted, or
595 * NULL if the stream does not have a lookup table entry (which is possible
596 * if the stream is empty). On success, *lte_ptr will be set to point to a
597 * lookup table entry that represents the resource in its new location in a
598 * staging file. This may be the same as the old entry in the case that it
599 * was reused, or it may be a new entry.
602 * Number of bytes of the stream to extract and include in the staging file
603 * resource. It may be less than the actual stream length, in which case
604 * only a prefix of the resource will be extracted. It may also be more
605 * than the actual stream length, in which case the extra space will be
608 * Returns 0 or a -errno code.
611 extract_resource_to_staging_dir(struct wim_inode *inode,
613 struct wim_lookup_table_entry **lte_ptr,
615 const struct wimfs_context *ctx)
617 struct wim_lookup_table_entry *old_lte;
618 struct wim_lookup_table_entry *new_lte;
619 char *staging_file_name;
628 /* Create the staging file. */
629 staging_fd = create_staging_file(ctx, &staging_file_name);
630 if (unlikely(staging_fd < 0))
633 /* Extract the stream to the staging file (possibly truncated). */
637 filedes_init(&fd, staging_fd);
639 extract_size = min(old_lte->size, size);
640 result = extract_stream_to_fd(old_lte, &fd, extract_size);
646 /* In the case of truncate() to more than the file length, extend the
647 * staging file with zeroes by calling ftruncate(). */
648 if (!result && size > extract_size)
649 result = ftruncate(staging_fd, size);
651 /* Close the staging file. */
652 if (close(staging_fd))
655 /* If an error occurred, unlink the staging file. */
656 if (unlikely(result)) {
657 /* extract_stream_to_fd() should set errno, but if it didn't,
658 * set a default value. */
659 ret = errno ? -errno : -EIO;
660 goto out_delete_staging_file;
663 /* Now deal with the lookup table entries. We may be able to re-use the
664 * existing entry, but we may have to create a new one instead. */
666 stream_id = inode_stream_idx_to_id(inode, stream_idx);
668 if (old_lte && inode->i_nlink == old_lte->refcnt) {
669 /* The reference count of the existing lookup table entry is the
670 * same as the link count of the inode that contains the stream
671 * we're opening. Therefore, all the references to the lookup
672 * table entry correspond to the stream we're trying to extract,
673 * so the lookup table entry can be re-used. */
674 lookup_table_unlink(ctx->wim->lookup_table, old_lte);
675 lte_put_resource(old_lte);
678 /* We need to split the old lookup table entry because it also
679 * has other references. Or, there was no old lookup table
680 * entry, so we need to create a new one anyway. */
682 new_lte = new_lookup_table_entry();
683 if (unlikely(!new_lte)) {
685 goto out_delete_staging_file;
688 /* There may already be open file descriptors to this stream if
689 * it's previously been opened read-only, but just now we're
690 * opening it read-write. Identify those file descriptors and
691 * change their lookup table entry pointers to point to the new
692 * lookup table entry, and open staging file descriptors for
695 * At the same time, we need to count the number of these opened
696 * file descriptors to the new lookup table entry. If there's
697 * an old lookup table entry, this number needs to be subtracted
698 * from the fd's opened to the old entry. */
699 for (u16 i = 0, j = 0; j < inode->i_num_opened_fds; i++) {
703 fd = inode->i_fds[i];
709 if (fd->f_stream_id != stream_id)
712 /* This is a readonly fd for the same stream. */
714 new_lte->num_opened_fds++;
715 raw_fd = openat(ctx->staging_dir_fd, staging_file_name,
716 O_RDONLY | O_NOFOLLOW);
717 if (unlikely(raw_fd < 0)) {
719 goto out_revert_fd_changes;
721 filedes_init(&fd->f_staging_fd, raw_fd);
724 old_lte->num_opened_fds -= new_lte->num_opened_fds;
725 old_lte->refcnt -= inode->i_nlink;
729 new_lte->refcnt = inode->i_nlink;
730 new_lte->resource_location = RESOURCE_IN_STAGING_FILE;
731 new_lte->staging_file_name = staging_file_name;
732 new_lte->staging_dir_fd = ctx->staging_dir_fd;
733 new_lte->size = size;
735 add_unhashed_stream(new_lte, inode, stream_id,
736 &wim_get_current_image_metadata(ctx->wim)->unhashed_streams);
738 inode->i_lte = new_lte;
740 inode->i_ads_entries[stream_idx - 1].lte = new_lte;
744 out_revert_fd_changes:
745 for (u16 i = 0; new_lte->num_opened_fds; i++) {
746 struct wimfs_fd *fd = inode->i_fds[i];
747 if (fd && fd->f_stream_id == stream_id) {
749 if (filedes_valid(&fd->f_staging_fd)) {
750 filedes_close(&fd->f_staging_fd);
751 filedes_invalidate(&fd->f_staging_fd);
753 new_lte->num_opened_fds--;
756 free_lookup_table_entry(new_lte);
757 out_delete_staging_file:
758 unlinkat(ctx->staging_dir_fd, staging_file_name, 0);
759 FREE(staging_file_name);
764 * Create the staging directory for the WIM file.
766 * The staging directory will be created in the directory specified by the open
767 * file descriptor @parent_dir_fd. It will be given a randomly generated name
768 * based on @wim_basename, the name of the WIM file.
770 * On success, returns a file descriptor to the open staging directory with
771 * O_RDONLY access. In addition, stores the allocated name of the staging
772 * directory (relative to @parent_dir_fd) in @staging_dir_name_ret.
773 * On failure, returns -1 and sets errno.
776 make_staging_dir_at(int parent_dir_fd, const char *wim_basename,
777 char **staging_dir_name_ret)
779 static const char common_suffix[8] = ".staging";
780 static const size_t random_suffix_len = 10;
781 size_t wim_basename_len;
782 size_t staging_dir_name_len;
783 char *staging_dir_name;
787 wim_basename_len = strlen(wim_basename);
788 staging_dir_name_len = wim_basename_len + sizeof(common_suffix) +
790 staging_dir_name = MALLOC(staging_dir_name_len + 1);
791 if (!staging_dir_name)
794 p = staging_dir_name;
795 p = mempcpy(p, wim_basename, wim_basename_len);
796 p = mempcpy(p, common_suffix, sizeof(common_suffix));
797 randomize_char_array_with_alnum(p, random_suffix_len);
798 p += random_suffix_len;
801 if (mkdirat(parent_dir_fd, staging_dir_name, 0700))
804 fd = openat(parent_dir_fd, staging_dir_name,
805 O_RDONLY | O_DIRECTORY | O_NOFOLLOW);
809 *staging_dir_name_ret = staging_dir_name;
813 unlinkat(parent_dir_fd, staging_dir_name, AT_REMOVEDIR);
815 FREE(staging_dir_name);
820 * Create the staging directory and set ctx->staging_dir_fd,
821 * ctx->staging_dir_name, and ctx->parent_dir_fd.
824 make_staging_dir(struct wimfs_context *ctx, const char *parent_dir_path)
826 const char *wim_basename;
830 wim_basename = path_basename(ctx->wim->filename);
832 if (!parent_dir_path) {
833 /* The user did not specify a directory. Default to creating
834 * the staging directory alongside the WIM file. */
835 if (wim_basename > ctx->wim->filename) {
836 parent_dir_path = ctx->wim->filename;
837 end = (char *)(wim_basename - 1);
838 /* *end must be a slash. Temporarily overwrite it so we
839 * can open the parent directory. */
842 parent_dir_path = ".";
846 /* Open the parent directory (in which we'll create our staging
848 ctx->parent_dir_fd = open(parent_dir_path, O_RDONLY | O_DIRECTORY);
849 if (ctx->parent_dir_fd < 0) {
850 ERROR_WITH_ERRNO("Can't open directory \"%s\"",
852 ret = WIMLIB_ERR_OPENDIR;
853 goto out_restore_wim_filename;
856 ctx->staging_dir_fd = make_staging_dir_at(ctx->parent_dir_fd,
858 &ctx->staging_dir_name);
859 if (ctx->staging_dir_fd < 0) {
860 ERROR_WITH_ERRNO("Can't create staging directory in \"%s\"",
862 close(ctx->parent_dir_fd);
863 ret = WIMLIB_ERR_MKDIR;
864 goto out_restore_wim_filename;
867 out_restore_wim_filename:
873 /* Deletes the staging directory, undoing the effects of a succesful call to
874 * make_staging_dir(). */
876 delete_staging_dir(struct wimfs_context *ctx)
881 dir = fdopendir(ctx->staging_dir_fd);
883 while ((ent = readdir(dir)))
884 unlinkat(ctx->staging_dir_fd, ent->d_name, 0);
887 close(ctx->staging_dir_fd);
889 if (unlinkat(ctx->parent_dir_fd, ctx->staging_dir_name, AT_REMOVEDIR))
890 WARNING_WITH_ERRNO("Could not delete staging directory");
891 FREE(ctx->staging_dir_name);
892 close(ctx->parent_dir_fd);
896 reassign_inode_numbers(struct wimfs_context *ctx)
898 struct wim_image_metadata *imd;
899 struct wim_inode *inode;
902 imd = wim_get_current_image_metadata(ctx->wim);
903 image_for_each_inode(inode, imd)
904 inode->i_ino = ctx->next_ino++;
908 release_extra_refcnts(struct wimfs_context *ctx)
910 struct list_head *list = &ctx->orig_stream_list;
911 struct wim_lookup_table *lookup_table = ctx->wim->lookup_table;
912 struct wim_lookup_table_entry *lte, *tmp;
914 list_for_each_entry_safe(lte, tmp, list, orig_stream_list) {
915 u32 n = lte->out_refcnt;
917 lte_decrement_refcnt(lte, lookup_table);
922 delete_empty_streams(struct wimfs_context *ctx)
924 struct wim_lookup_table_entry *lte, *tmp;
925 struct wim_image_metadata *imd;
927 imd = wim_get_current_image_metadata(ctx->wim);
929 image_for_each_unhashed_stream_safe(lte, tmp, imd) {
931 *retrieve_lte_pointer(lte) = NULL;
932 list_del(<e->unhashed_list);
933 free_lookup_table_entry(lte);
939 inode_close_fds(struct wim_inode *inode)
941 u16 num_open_fds = inode->i_num_opened_fds;
942 for (u16 i = 0; num_open_fds; i++) {
943 if (inode->i_fds[i]) {
944 close_wimfs_fd(inode->i_fds[i]);
951 close_all_fds(struct wimfs_context *ctx)
953 struct wim_inode *inode, *tmp;
954 struct wim_image_metadata *imd;
956 imd = wim_get_current_image_metadata(ctx->wim);
958 list_for_each_entry_safe(inode, tmp, &imd->inode_list, i_list)
959 inode_close_fds(inode);
962 /* Moves the currently selected image, which may have been modified, to a new
963 * index, and sets the original index to refer to a reset (unmodified) copy of
966 renew_current_image(struct wimfs_context *ctx)
968 WIMStruct *wim = ctx->wim;
969 int idx = wim->current_image - 1;
970 struct wim_image_metadata *imd = wim->image_metadata[idx];
971 struct wim_image_metadata *replace_imd;
972 struct wim_lookup_table_entry *new_lte;
975 /* Create 'replace_imd' structure to use for the reset original,
976 * unmodified image. */
977 ret = WIMLIB_ERR_NOMEM;
978 replace_imd = new_image_metadata();
982 /* Create new stream reference for the modified image's metadata
983 * resource, which doesn't exist yet. */
984 ret = WIMLIB_ERR_NOMEM;
985 new_lte = new_lookup_table_entry();
987 goto err_put_replace_imd;
988 new_lte->flags = WIM_RESHDR_FLAG_METADATA;
989 new_lte->unhashed = 1;
991 /* Make the image being moved available at a new index. Increments the
992 * WIM's image count, but does not increment the reference count of the
993 * 'struct image_metadata'. */
994 ret = append_image_metadata(wim, imd);
996 goto err_free_new_lte;
998 ret = xml_add_image(wim, T(""));
1000 goto err_undo_append;
1002 replace_imd->metadata_lte = imd->metadata_lte;
1003 imd->metadata_lte = new_lte;
1004 wim->image_metadata[idx] = replace_imd;
1005 wim->current_image = wim->hdr.image_count;
1009 wim->hdr.image_count--;
1011 free_lookup_table_entry(new_lte);
1012 err_put_replace_imd:
1013 put_image_metadata(replace_imd, NULL);
1018 static enum wimlib_progress_status
1019 commit_progress_func(enum wimlib_progress_msg msg,
1020 union wimlib_progress_info *info, void *progctx)
1022 mqd_t mq = *(mqd_t *)progctx;
1023 struct commit_progress_report report;
1025 memset(&report, 0, sizeof(report));
1028 report.info = *info;
1029 mq_send(mq, (const char *)&report, sizeof(report), 1);
1030 return WIMLIB_PROGRESS_STATUS_CONTINUE;
1033 /* Commit the mounted image to the underlying WIM file. */
1035 commit_image(struct wimfs_context *ctx, int unmount_flags, mqd_t mq)
1039 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_SEND_PROGRESS)
1040 wimlib_register_progress_function(ctx->wim,
1041 commit_progress_func, &mq);
1043 wimlib_register_progress_function(ctx->wim, NULL, NULL);
1045 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_NEW_IMAGE) {
1046 int ret = renew_current_image(ctx);
1050 release_extra_refcnts(ctx);
1052 INIT_LIST_HEAD(&ctx->orig_stream_list);
1053 delete_empty_streams(ctx);
1054 xml_update_image_info(ctx->wim, ctx->wim->current_image);
1057 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY)
1058 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1059 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_REBUILD)
1060 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1061 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_RECOMPRESS)
1062 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
1063 return wimlib_overwrite(ctx->wim, write_flags, 0);
1067 unmount_wimfs(const struct wimfs_unmount_info *info)
1069 struct fuse_context *fuse_ctx = fuse_get_context();
1070 struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
1071 int unmount_flags = info->unmount_flags;
1072 mqd_t mq = (mqd_t)-1;
1075 if (fuse_ctx->uid != wimfs_ctx->owner_uid &&
1079 if (info->mq_name[0]) {
1080 mq = mq_open(info->mq_name, O_WRONLY | O_NONBLOCK);
1081 if (mq == (mqd_t)-1)
1085 /* Ignore COMMIT if the image is mounted read-only. */
1086 if (!(wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE))
1087 unmount_flags &= ~WIMLIB_UNMOUNT_FLAG_COMMIT;
1089 if (wimfs_ctx->num_open_fds) {
1090 if ((unmount_flags & (WIMLIB_UNMOUNT_FLAG_COMMIT |
1091 WIMLIB_UNMOUNT_FLAG_FORCE))
1092 == WIMLIB_UNMOUNT_FLAG_COMMIT)
1094 status = WIMLIB_ERR_MOUNTED_IMAGE_IS_BUSY;
1095 goto out_send_status;
1097 close_all_fds(wimfs_ctx);
1100 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_COMMIT)
1101 status = commit_image(wimfs_ctx, unmount_flags, mq);
1104 fuse_exit(fuse_ctx->fuse);
1106 if (mq != (mqd_t)-1) {
1107 mq_send(mq, (const char *)&status, sizeof(int), 1);
1114 wimfs_chmod(const char *path, mode_t mask)
1116 const struct wimfs_context *ctx = wimfs_get_context();
1117 struct wim_inode *inode;
1118 struct wimlib_unix_data unix_data;
1120 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA))
1123 inode = wim_pathname_to_inode(ctx->wim, path);
1127 unix_data.uid = ctx->owner_uid;
1128 unix_data.gid = ctx->owner_gid;
1129 unix_data.mode = mask;
1132 if (!inode_set_unix_data(inode, &unix_data, UNIX_DATA_MODE))
1139 wimfs_chown(const char *path, uid_t uid, gid_t gid)
1141 const struct wimfs_context *ctx = wimfs_get_context();
1142 struct wim_inode *inode;
1143 struct wimlib_unix_data unix_data;
1146 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA))
1149 inode = wim_pathname_to_inode(ctx->wim, path);
1155 if (uid != (uid_t)-1)
1156 which |= UNIX_DATA_UID;
1158 uid = ctx->owner_uid;
1160 if (gid != (gid_t)-1)
1161 which |= UNIX_DATA_GID;
1163 gid = ctx->owner_gid;
1165 unix_data.uid = uid;
1166 unix_data.gid = gid;
1167 unix_data.mode = inode_default_unix_mode(inode);
1170 if (!inode_set_unix_data(inode, &unix_data, which))
1177 wimfs_fgetattr(const char *path, struct stat *stbuf, struct fuse_file_info *fi)
1179 struct wimfs_fd *fd = WIMFS_FD(fi);
1180 return inode_to_stbuf(fd->f_inode, fd->f_lte, stbuf);
1184 wimfs_ftruncate(const char *path, off_t size, struct fuse_file_info *fi)
1186 struct wimfs_fd *fd = WIMFS_FD(fi);
1187 if (ftruncate(fd->f_staging_fd.fd, size))
1189 touch_inode(fd->f_inode);
1190 fd->f_lte->size = size;
1195 wimfs_getattr(const char *path, struct stat *stbuf)
1197 const struct wimfs_context *ctx = wimfs_get_context();
1198 struct wim_dentry *dentry;
1199 struct wim_lookup_table_entry *lte;
1202 ret = wim_pathname_to_stream(ctx->wim, path,
1203 get_lookup_flags(ctx) |
1204 LOOKUP_FLAG_DIRECTORY_OK,
1205 &dentry, <e, NULL);
1208 return inode_to_stbuf(dentry->d_inode, lte, stbuf);
1212 copy_xattr(char *dest, size_t destsize, const void *src, size_t srcsize)
1216 if (destsize < srcsize)
1218 memcpy(dest, src, srcsize);
1223 wimfs_getxattr(const char *path, const char *name, char *value,
1226 struct fuse_context *fuse_ctx = fuse_get_context();
1227 const struct wimfs_context *ctx = WIMFS_CTX(fuse_ctx);
1228 struct wim_inode *inode;
1229 struct wim_ads_entry *ads_entry;
1230 struct wim_lookup_table_entry *lte;
1232 if (!strncmp(name, "wimfs.", 6)) {
1233 /* Handle some magical extended attributes. These really should
1234 * be ioctls, but directory ioctls aren't supported until
1235 * libfuse 2.9, and even then they are broken. */
1237 if (!strcmp(name, "wim_filename")) {
1238 return copy_xattr(value, size, ctx->wim->filename,
1239 strlen(ctx->wim->filename));
1241 if (!strcmp(name, "wim_info")) {
1242 struct wimlib_wim_info info;
1244 wimlib_get_wim_info(ctx->wim, &info);
1246 return copy_xattr(value, size, &info, sizeof(info));
1248 if (!strcmp(name, "mounted_image")) {
1249 return copy_xattr(value, size,
1250 &ctx->wim->current_image, sizeof(int));
1252 if (!strcmp(name, "mount_flags")) {
1253 return copy_xattr(value, size,
1254 &ctx->mount_flags, sizeof(int));
1256 if (!strcmp(name, "unmount")) {
1257 struct wimfs_unmount_info info;
1258 memset(&info, 0, sizeof(info));
1259 return unmount_wimfs(&info);
1264 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1267 if (strncmp(name, "user.", 5))
1271 /* Querying a named data stream */
1273 inode = wim_pathname_to_inode(ctx->wim, path);
1277 ads_entry = inode_get_ads_entry(inode, name);
1279 return (errno == ENOENT) ? -ENOATTR : -errno;
1281 lte = ads_entry->lte;
1285 if (unlikely(lte->size > INT_MAX))
1289 if (lte->size > size)
1292 if (read_full_stream_into_buf(lte, value))
1299 wimfs_link(const char *existing_path, const char *new_path)
1301 WIMStruct *wim = wimfs_get_WIMStruct();
1302 const char *new_name;
1303 struct wim_inode *inode;
1304 struct wim_dentry *dir;
1305 struct wim_dentry *new_alias;
1307 inode = wim_pathname_to_inode(wim, existing_path);
1311 if (inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
1312 FILE_ATTRIBUTE_REPARSE_POINT))
1315 new_name = path_basename(new_path);
1317 dir = get_parent_dentry(wim, new_path, WIMLIB_CASE_SENSITIVE);
1321 if (!dentry_is_directory(dir))
1324 if (get_dentry_child_with_name(dir, new_name, WIMLIB_CASE_SENSITIVE))
1327 if (new_dentry(new_name, &new_alias))
1330 new_alias->d_inode = inode;
1331 inode_add_dentry(new_alias, inode);
1332 dentry_add_child(dir, new_alias);
1333 touch_inode(dir->d_inode);
1335 inode_ref_streams(inode);
1340 wimfs_listxattr(const char *path, char *list, size_t size)
1342 const struct wimfs_context *ctx = wimfs_get_context();
1343 const struct wim_inode *inode;
1345 char *end = list + size;
1348 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1351 /* List named data streams, or get the list size. We report each named
1352 * data stream "X" as an extended attribute "user.X". */
1354 inode = wim_pathname_to_inode(ctx->wim, path);
1358 for (u16 i = 0; i < inode->i_num_ads; i++) {
1359 const struct wim_ads_entry *entry;
1360 char *stream_name_mbs;
1361 size_t stream_name_mbs_nbytes;
1363 entry = &inode->i_ads_entries[i];
1365 if (!entry->stream_name_nbytes)
1368 if (utf16le_to_tstr(entry->stream_name,
1369 entry->stream_name_nbytes,
1371 &stream_name_mbs_nbytes))
1374 if (unlikely(INT_MAX - total_size < stream_name_mbs_nbytes + 6)) {
1375 FREE(stream_name_mbs);
1379 total_size += stream_name_mbs_nbytes + 6;
1381 if (end - p < stream_name_mbs_nbytes + 6) {
1382 FREE(stream_name_mbs);
1385 p = mempcpy(p, "user.", 5);
1386 p = mempcpy(p, stream_name_mbs, stream_name_mbs_nbytes);
1389 FREE(stream_name_mbs);
1395 wimfs_mkdir(const char *path, mode_t mode)
1397 struct wim_dentry *dentry;
1400 /* Note: according to fuse.h, mode may not include S_IFDIR */
1401 ret = create_dentry(fuse_get_context(), path, mode | S_IFDIR, 0,
1402 FILE_ATTRIBUTE_DIRECTORY, &dentry);
1405 touch_parent(dentry);
1410 wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
1412 struct fuse_context *fuse_ctx = fuse_get_context();
1413 struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
1414 const char *stream_name;
1416 if ((wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
1417 && (stream_name = path_stream_name(path)))
1419 struct wim_ads_entry *old_entry;
1420 struct wim_ads_entry *new_entry;
1421 struct wim_inode *inode;
1424 /* Create a named data stream. */
1429 p = (char *)stream_name - 1;
1432 inode = wim_pathname_to_inode(wimfs_ctx->wim, path);
1437 old_entry = inode_get_ads_entry(inode, stream_name);
1440 if (errno != ENOENT)
1443 new_entry = inode_add_ads(inode, stream_name);
1448 /* Create a regular file, device node, named pipe, or socket.
1450 struct wim_dentry *dentry;
1453 if (!S_ISREG(mode) &&
1454 !(wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA))
1457 /* Note: we still use FILE_ATTRIBUTE_NORMAL for device nodes,
1458 * named pipes, and sockets. The real mode is in the UNIX
1460 ret = create_dentry(fuse_ctx, path, mode, rdev,
1461 FILE_ATTRIBUTE_NORMAL, &dentry);
1464 touch_parent(dentry);
1470 wimfs_open(const char *path, struct fuse_file_info *fi)
1472 struct wimfs_context *ctx = wimfs_get_context();
1473 struct wim_dentry *dentry;
1474 struct wim_inode *inode;
1475 struct wim_lookup_table_entry *lte;
1477 struct wimfs_fd *fd;
1480 ret = wim_pathname_to_stream(ctx->wim, path, get_lookup_flags(ctx),
1481 &dentry, <e, &stream_idx);
1485 inode = dentry->d_inode;
1487 /* The file resource may be in the staging directory (read-write mounts
1488 * only) or in the WIM. If it's in the staging directory, we need to
1489 * open a native file descriptor for the corresponding file. Otherwise,
1490 * we can read the file resource directly from the WIM file if we are
1491 * opening it read-only, but we need to extract the resource to the
1492 * staging directory if we are opening it writable. */
1494 if (flags_writable(fi->flags) &&
1495 (!lte || lte->resource_location != RESOURCE_IN_STAGING_FILE)) {
1496 ret = extract_resource_to_staging_dir(inode,
1499 lte ? lte->size : 0,
1505 ret = alloc_wimfs_fd(inode, inode_stream_idx_to_id(inode, stream_idx),
1510 if (lte && lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1513 raw_fd = openat(lte->staging_dir_fd, lte->staging_file_name,
1514 (fi->flags & O_ACCMODE) | O_NOFOLLOW);
1519 filedes_init(&fd->f_staging_fd, raw_fd);
1521 fi->fh = (uintptr_t)fd;
1526 wimfs_opendir(const char *path, struct fuse_file_info *fi)
1528 WIMStruct *wim = wimfs_get_WIMStruct();
1529 struct wim_inode *inode;
1530 struct wimfs_fd *fd;
1533 inode = wim_pathname_to_inode(wim, path);
1536 if (!inode_is_directory(inode))
1538 ret = alloc_wimfs_fd(inode, 0, NULL, &fd);
1541 fi->fh = (uintptr_t)fd;
1546 wimfs_read(const char *path, char *buf, size_t size,
1547 off_t offset, struct fuse_file_info *fi)
1549 struct wimfs_fd *fd = WIMFS_FD(fi);
1550 const struct wim_lookup_table_entry *lte;
1557 if (offset >= lte->size)
1560 if (size > lte->size - offset)
1561 size = lte->size - offset;
1566 switch (lte->resource_location) {
1567 case RESOURCE_IN_WIM:
1568 if (read_partial_wim_stream_into_buf(lte, size, offset, buf))
1573 case RESOURCE_IN_STAGING_FILE:
1574 ret = raw_pread(&fd->f_staging_fd, buf, size, offset);
1578 case RESOURCE_IN_ATTACHED_BUFFER:
1579 memcpy(buf, lte->attached_buffer + offset, size);
1590 wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
1591 off_t offset, struct fuse_file_info *fi)
1593 struct wimfs_fd *fd = WIMFS_FD(fi);
1594 const struct wim_inode *inode;
1595 const struct wim_dentry *child;
1598 inode = fd->f_inode;
1600 ret = filler(buf, ".", NULL, 0);
1603 ret = filler(buf, "..", NULL, 0);
1607 for_inode_child(child, inode) {
1608 char *file_name_mbs;
1609 size_t file_name_mbs_nbytes;
1611 ret = utf16le_to_tstr(child->file_name,
1612 child->file_name_nbytes,
1614 &file_name_mbs_nbytes);
1618 ret = filler(buf, file_name_mbs, NULL, 0);
1619 FREE(file_name_mbs);
1627 wimfs_readlink(const char *path, char *buf, size_t buf_len)
1629 WIMStruct *wim = wimfs_get_WIMStruct();
1630 const struct wim_inode *inode;
1633 inode = wim_pathname_to_inode(wim, path);
1636 if (!inode_is_symlink(inode))
1640 ret = wim_inode_readlink(inode, buf, buf_len - 1, NULL);
1644 } else if (ret == -ENAMETOOLONG) {
1645 buf[buf_len - 1] = '\0';
1650 /* We use this for both release() and releasedir(), since in both cases we
1651 * simply need to close the file descriptor. */
1653 wimfs_release(const char *path, struct fuse_file_info *fi)
1655 return close_wimfs_fd(WIMFS_FD(fi));
1659 wimfs_removexattr(const char *path, const char *name)
1661 struct wimfs_context *ctx = wimfs_get_context();
1662 struct wim_inode *inode;
1663 struct wim_ads_entry *ads_entry;
1665 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1668 if (strncmp(name, "user.", 5))
1672 /* Removing a named data stream. */
1674 inode = wim_pathname_to_inode(ctx->wim, path);
1678 ads_entry = inode_get_ads_entry(inode, name);
1680 return (errno == ENOENT) ? -ENOATTR : -errno;
1682 inode_remove_ads(inode, ads_entry, ctx->wim->lookup_table);
1687 wimfs_rename(const char *from, const char *to)
1689 return rename_wim_path(wimfs_get_WIMStruct(), from, to,
1690 WIMLIB_CASE_SENSITIVE, NULL);
1694 wimfs_rmdir(const char *path)
1696 WIMStruct *wim = wimfs_get_WIMStruct();
1697 struct wim_dentry *dentry;
1699 dentry = get_dentry(wim, path, WIMLIB_CASE_SENSITIVE);
1703 if (!dentry_is_directory(dentry))
1706 if (dentry_has_children(dentry))
1709 touch_parent(dentry);
1710 remove_dentry(dentry, wim->lookup_table);
1715 wimfs_setxattr(const char *path, const char *name,
1716 const char *value, size_t size, int flags)
1718 struct wimfs_context *ctx = wimfs_get_context();
1719 struct wim_inode *inode;
1720 struct wim_ads_entry *existing_entry;
1722 if (!strncmp(name, "wimfs.", 6)) {
1723 /* Handle some magical extended attributes. These really should
1724 * be ioctls, but directory ioctls aren't supported until
1725 * libfuse 2.9, and even then they are broken. */
1727 if (!strcmp(name, "unmount")) {
1728 if (size < sizeof(struct wimfs_unmount_info))
1730 return unmount_wimfs((const void *)value);
1735 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1738 if (strncmp(name, "user.", 5))
1742 /* Setting the contents of a named data stream. */
1744 inode = wim_pathname_to_inode(ctx->wim, path);
1748 existing_entry = inode_get_ads_entry(inode, name);
1749 if (existing_entry) {
1750 if (flags & XATTR_CREATE)
1753 if (errno != ENOENT)
1755 if (flags & XATTR_REPLACE)
1759 if (!inode_add_ads_with_data(inode, name, value,
1760 size, ctx->wim->lookup_table))
1763 inode_remove_ads(inode, existing_entry, ctx->wim->lookup_table);
1768 wimfs_symlink(const char *to, const char *from)
1770 struct fuse_context *fuse_ctx = fuse_get_context();
1771 struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
1772 struct wim_dentry *dentry;
1775 ret = create_dentry(fuse_ctx, from, S_IFLNK | 0777, 0,
1776 FILE_ATTRIBUTE_REPARSE_POINT, &dentry);
1779 dentry->d_inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
1780 ret = wim_inode_set_symlink(dentry->d_inode, to,
1781 wimfs_ctx->wim->lookup_table);
1783 remove_dentry(dentry, wimfs_ctx->wim->lookup_table);
1784 if (ret == WIMLIB_ERR_NOMEM)
1789 touch_parent(dentry);
1795 wimfs_truncate(const char *path, off_t size)
1797 const struct wimfs_context *ctx = wimfs_get_context();
1798 struct wim_dentry *dentry;
1799 struct wim_lookup_table_entry *lte;
1804 ret = wim_pathname_to_stream(ctx->wim, path, get_lookup_flags(ctx),
1805 &dentry, <e, &stream_idx);
1813 if (!lte || lte->resource_location != RESOURCE_IN_STAGING_FILE) {
1814 return extract_resource_to_staging_dir(dentry->d_inode,
1819 /* Truncate the staging file. */
1820 fd = openat(lte->staging_dir_fd, lte->staging_file_name,
1821 O_WRONLY | O_NOFOLLOW);
1824 ret = ftruncate(fd, size);
1825 if (close(fd) || ret)
1832 wimfs_unlink(const char *path)
1834 const struct wimfs_context *ctx = wimfs_get_context();
1835 struct wim_dentry *dentry;
1839 ret = wim_pathname_to_stream(ctx->wim, path, get_lookup_flags(ctx),
1840 &dentry, NULL, &stream_idx);
1845 if (inode_stream_name_nbytes(dentry->d_inode, stream_idx) == 0) {
1846 touch_parent(dentry);
1847 remove_dentry(dentry, ctx->wim->lookup_table);
1849 inode_remove_ads(dentry->d_inode,
1850 &dentry->d_inode->i_ads_entries[stream_idx - 1],
1851 ctx->wim->lookup_table);
1856 #ifdef HAVE_UTIMENSAT
1858 * Change the timestamp on a file dentry.
1860 * Note that alternate data streams do not have their own timestamps.
1863 wimfs_utimens(const char *path, const struct timespec tv[2])
1865 WIMStruct *wim = wimfs_get_WIMStruct();
1866 struct wim_inode *inode;
1868 inode = wim_pathname_to_inode(wim, path);
1872 if (tv[0].tv_nsec != UTIME_OMIT) {
1873 if (tv[0].tv_nsec == UTIME_NOW)
1874 inode->i_last_access_time = get_wim_timestamp();
1876 inode->i_last_access_time = timespec_to_wim_timestamp(tv[0]);
1878 if (tv[1].tv_nsec != UTIME_OMIT) {
1879 if (tv[1].tv_nsec == UTIME_NOW)
1880 inode->i_last_write_time = get_wim_timestamp();
1882 inode->i_last_write_time = timespec_to_wim_timestamp(tv[1]);
1886 #else /* HAVE_UTIMENSAT */
1888 wimfs_utime(const char *path, struct utimbuf *times)
1890 WIMStruct *wim = wimfs_get_WIMStruct();
1891 struct wim_inode *inode;
1893 inode = wim_pathname_to_inode(wim, path);
1897 inode->i_last_access_time = unix_timestamp_to_wim(times->actime);
1898 inode->i_last_write_time = unix_timestamp_to_wim(times->modtime);
1901 #endif /* !HAVE_UTIMENSAT */
1904 wimfs_write(const char *path, const char *buf, size_t size,
1905 off_t offset, struct fuse_file_info *fi)
1907 struct wimfs_fd *fd = WIMFS_FD(fi);
1910 ret = raw_pwrite(&fd->f_staging_fd, buf, size, offset);
1914 if (offset + size > fd->f_lte->size)
1915 fd->f_lte->size = offset + size;
1917 touch_inode(fd->f_inode);
1921 static struct fuse_operations wimfs_operations = {
1922 .chmod = wimfs_chmod,
1923 .chown = wimfs_chown,
1924 .fgetattr = wimfs_fgetattr,
1925 .ftruncate = wimfs_ftruncate,
1926 .getattr = wimfs_getattr,
1927 .getxattr = wimfs_getxattr,
1929 .listxattr = wimfs_listxattr,
1930 .mkdir = wimfs_mkdir,
1931 .mknod = wimfs_mknod,
1933 .opendir = wimfs_opendir,
1935 .readdir = wimfs_readdir,
1936 .readlink = wimfs_readlink,
1937 .release = wimfs_release,
1938 .releasedir = wimfs_release,
1939 .removexattr = wimfs_removexattr,
1940 .rename = wimfs_rename,
1941 .rmdir = wimfs_rmdir,
1942 .setxattr = wimfs_setxattr,
1943 .symlink = wimfs_symlink,
1944 .truncate = wimfs_truncate,
1945 .unlink = wimfs_unlink,
1946 #ifdef HAVE_UTIMENSAT
1947 .utimens = wimfs_utimens,
1949 .utime = wimfs_utime,
1951 .write = wimfs_write,
1953 /* We keep track of file descriptor structures (struct wimfs_fd), so
1954 * there is no need to have the file path provided on operations such as
1956 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 8)
1957 .flag_nullpath_ok = 1,
1959 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 9)
1961 .flag_utime_omit_ok = 1,
1965 /* API function documented in wimlib.h */
1967 wimlib_mount_image(WIMStruct *wim, int image, const char *dir,
1968 int mount_flags, const char *staging_dir)
1971 struct wim_image_metadata *imd;
1972 struct wimfs_context ctx;
1973 char *fuse_argv[16];
1976 if (!wim || !dir || !*dir)
1977 return WIMLIB_ERR_INVALID_PARAM;
1979 if (mount_flags & ~(WIMLIB_MOUNT_FLAG_READWRITE |
1980 WIMLIB_MOUNT_FLAG_DEBUG |
1981 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
1982 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
1983 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS |
1984 WIMLIB_MOUNT_FLAG_UNIX_DATA |
1985 WIMLIB_MOUNT_FLAG_ALLOW_OTHER))
1986 return WIMLIB_ERR_INVALID_PARAM;
1988 /* For read-write mount, check for write access to the WIM. */
1989 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1990 ret = can_delete_from_wim(wim);
1995 /* Select the image to mount. */
1996 ret = select_wim_image(wim, image);
2000 /* Get the metadata for the image to mount. */
2001 imd = wim_get_current_image_metadata(wim);
2003 if (imd->modified) {
2004 /* To avoid complicating things, we don't support mounting
2005 * images to which in-memory modifications have already been
2007 ERROR("Cannot mount a modified WIM image!");
2008 return WIMLIB_ERR_INVALID_PARAM;
2011 ret = lock_wim_for_append(wim, wim->in_fd.fd);
2015 /* If the user did not specify an interface for accessing named
2016 * data streams, use the default (extended attributes). */
2017 if (!(mount_flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
2018 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
2019 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
2020 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
2022 /* Start initializing the wimfs_context. */
2023 memset(&ctx, 0, sizeof(struct wimfs_context));
2025 ctx.mount_flags = mount_flags;
2026 if (mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
2027 ctx.default_lookup_flags = LOOKUP_FLAG_ADS_OK;
2028 /* For read-write mount, create the staging directory. */
2029 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2030 ret = make_staging_dir(&ctx, staging_dir);
2034 ctx.owner_uid = getuid();
2035 ctx.owner_gid = getgid();
2037 /* Add each stream referenced by files in the image to a list and
2038 * preemptively double the number of references to each. The latter is
2039 * done to allow implementing the WIMLIB_UNMOUNT_FLAG_NEW_IMAGE
2041 INIT_LIST_HEAD(&ctx.orig_stream_list);
2042 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2044 struct wim_inode *inode;
2045 struct wim_lookup_table_entry *lte;
2047 image_for_each_inode(inode, imd) {
2048 for (i = 0; i <= inode->i_num_ads; i++) {
2049 lte = inode_stream_lte(inode, i,
2052 lte->out_refcnt = 0;
2056 image_for_each_inode(inode, imd) {
2057 for (i = 0; i <= inode->i_num_ads; i++) {
2058 lte = inode_stream_lte(inode, i,
2061 if (lte->out_refcnt == 0)
2062 list_add(<e->orig_stream_list,
2063 &ctx.orig_stream_list);
2064 lte->out_refcnt += inode->i_nlink;
2065 lte->refcnt += inode->i_nlink;
2071 /* Assign new inode numbers. */
2072 reassign_inode_numbers(&ctx);
2074 /* If a read-write mount, mark the image as modified. */
2075 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2078 /* Build the FUSE command line. */
2081 fuse_argv[fuse_argc++] = "wimlib";
2082 fuse_argv[fuse_argc++] = (char *)dir;
2084 /* Disable multi-threaded operation. */
2085 fuse_argv[fuse_argc++] = "-s";
2087 /* Enable FUSE debug mode (don't fork) if requested by the user. */
2088 if (mount_flags & WIMLIB_MOUNT_FLAG_DEBUG)
2089 fuse_argv[fuse_argc++] = "-d";
2092 * Build the FUSE mount options:
2095 * FUSE will use the inode numbers we provide. We want this,
2096 * because we have inodes and will number them ourselves.
2099 * Name for our filesystem (main type is "fuse").
2102 * If an open file is unlinked, unlink it for real rather than
2103 * renaming it to a hidden file. Our code supports this; an
2104 * unlinked inode is retained until all its file descriptors have
2107 * default_permissions
2108 * FUSE will perform permission checking. Useful when
2109 * WIMLIB_MOUNT_FLAG_UNIX_DATA is provided and the WIM image
2110 * contains the UNIX permissions for each file.
2113 * Cache the contents of files. This will speed up repeated access
2114 * to files on a mounted WIM image, since they won't need to be
2115 * decompressed repeatedly. This option is valid because data in
2116 * the WIM image should never be changed externally. (Although, if
2117 * someone really wanted to they could modify the WIM file or mess
2118 * with the staging directory; but then they're asking for
2121 * entry_timeout=1000000000
2122 * Cache positive name lookups indefinitely, since names can only
2123 * be added, removed, or modified through the mounted filesystem
2126 * negative_timeout=1000000000
2127 * Cache negative name lookups indefinitely, since names can only
2128 * be added, removed, or modified through the mounted filesystem
2132 * Don't cache file/directory attributes. This is needed as a
2133 * workaround for the fact that when caching attributes, the high
2134 * level interface to libfuse considers a file which has several
2135 * hard-linked names as several different files. (Otherwise, we
2136 * could cache our file/directory attributes indefinitely, since
2137 * they can only be changed through the mounted filesystem itself.)
2139 char optstring[256] =
2144 ",default_permissions"
2146 ",entry_timeout=1000000000"
2147 ",negative_timeout=1000000000"
2150 fuse_argv[fuse_argc++] = "-o";
2151 fuse_argv[fuse_argc++] = optstring;
2152 if (!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE))
2153 strcat(optstring, ",ro");
2154 if (mount_flags & WIMLIB_MOUNT_FLAG_ALLOW_OTHER)
2155 strcat(optstring, ",allow_other");
2156 fuse_argv[fuse_argc] = NULL;
2158 /* Mount our filesystem. */
2159 ret = fuse_main(fuse_argc, fuse_argv, &wimfs_operations, &ctx);
2161 /* Cleanup and return. */
2163 ret = WIMLIB_ERR_FUSE;
2164 release_extra_refcnts(&ctx);
2165 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2166 delete_staging_dir(&ctx);
2168 unlock_wim_for_append(wim, wim->in_fd.fd);
2172 struct commit_progress_thread_args {
2174 wimlib_progress_func_t progfunc;
2180 commit_progress_thread_proc(void *_args)
2182 struct commit_progress_thread_args *args = _args;
2183 struct commit_progress_report report;
2186 args->status = WIMLIB_ERR_NOT_A_MOUNTPOINT;
2188 ret = mq_receive(args->mq,
2189 (char *)&report, sizeof(report), NULL);
2195 if (ret == sizeof(int)) {
2196 args->status = *(int *)&report;
2199 if (ret < sizeof(report))
2201 call_progress(args->progfunc, report.msg,
2202 &report.info, args->progctx);
2208 generate_message_queue_name(char name[WIMFS_MQUEUE_NAME_LEN + 1])
2211 memcpy(name + 1, "wimfs-", 6);
2212 randomize_char_array_with_alnum(name + 7, WIMFS_MQUEUE_NAME_LEN - 7);
2213 name[WIMFS_MQUEUE_NAME_LEN] = '\0';
2217 create_message_queue(const char *name, bool have_progfunc)
2219 bool am_root = (getuid() == 0);
2220 mode_t umask_save = 0;
2222 struct mq_attr attr;
2225 memset(&attr, 0, sizeof(attr));
2228 attr.mq_msgsize = sizeof(struct commit_progress_report);
2230 attr.mq_msgsize = sizeof(int);
2233 /* Filesystem mounted as normal user with --allow-other should
2234 * be able to send messages to root user, if they're doing the
2236 umask_save = umask(0);
2239 mq = mq_open(name, O_RDWR | O_CREAT | O_EXCL, mode, &attr);
2245 /* Unmount a read-write mounted WIM image, committing the changes. */
2247 do_unmount_commit(const char *dir, int unmount_flags,
2248 wimlib_progress_func_t progfunc, void *progctx)
2250 struct wimfs_unmount_info unmount_info;
2252 struct commit_progress_thread_args args;
2253 pthread_t commit_progress_tid;
2256 memset(&unmount_info, 0, sizeof(unmount_info));
2257 unmount_info.unmount_flags = unmount_flags;
2258 generate_message_queue_name(unmount_info.mq_name);
2260 mq = create_message_queue(unmount_info.mq_name, progfunc != NULL);
2261 if (mq == (mqd_t)-1) {
2262 ERROR_WITH_ERRNO("Can't create POSIX message queue");
2263 return WIMLIB_ERR_MQUEUE;
2266 /* The current thread will be stuck in setxattr() until the image is
2267 * committed. Create a thread to handle the progress messages. */
2270 args.progfunc = progfunc;
2271 args.progctx = progctx;
2272 ret = pthread_create(&commit_progress_tid, NULL,
2273 commit_progress_thread_proc, &args);
2276 ERROR_WITH_ERRNO("Can't create thread");
2277 ret = WIMLIB_ERR_NOMEM;
2280 unmount_info.unmount_flags |= WIMLIB_UNMOUNT_FLAG_SEND_PROGRESS;
2283 if (!setxattr(dir, "wimfs.unmount",
2284 (const char *)&unmount_info, sizeof(unmount_info), 0))
2286 else if (errno == EACCES || errno == EPERM)
2287 ret = WIMLIB_ERR_NOT_PERMITTED_TO_UNMOUNT;
2289 ret = WIMLIB_ERR_NOT_A_MOUNTPOINT;
2292 /* Terminate the progress thread and retrieve final unmount
2296 mq_send(mq, (const char *)&tmp, sizeof(int), 1);
2298 pthread_join(commit_progress_tid, NULL);
2299 if (!ret && args.status != -1)
2302 /* Retrieve the final unmount status. */
2307 mq_send(mq, (const char *)&tmp, sizeof(int), 1);
2308 len = mq_receive(mq, (char *)&tmp, sizeof(int), NULL);
2310 if (len == 4 && tmp != -1)
2313 ret = WIMLIB_ERR_NOT_A_MOUNTPOINT;
2317 mq_unlink(unmount_info.mq_name);
2321 /* Unmount a read-only or read-write mounted WIM image, discarding any changes.
2324 do_unmount_discard(const char *dir)
2326 if (!getxattr(dir, "wimfs.unmount", NULL, 0))
2328 else if (errno == EACCES || errno == EPERM)
2329 return WIMLIB_ERR_NOT_PERMITTED_TO_UNMOUNT;
2331 return WIMLIB_ERR_NOT_A_MOUNTPOINT;
2335 begin_unmount(const char *dir, int unmount_flags, int *mount_flags_ret,
2336 wimlib_progress_func_t progfunc, void *progctx)
2340 int wim_filename_len;
2341 union wimlib_progress_info progress;
2343 if (getxattr(dir, "wimfs.mount_flags",
2344 &mount_flags, sizeof(int)) != sizeof(int))
2345 return WIMLIB_ERR_NOT_A_MOUNTPOINT;
2347 *mount_flags_ret = mount_flags;
2352 if (getxattr(dir, "wimfs.mounted_image",
2353 &mounted_image, sizeof(int)) != sizeof(int))
2354 return WIMLIB_ERR_NOT_A_MOUNTPOINT;
2356 wim_filename_len = getxattr(dir, "wimfs.wim_filename", NULL, 0);
2357 if (wim_filename_len < 0)
2358 return WIMLIB_ERR_NOT_A_MOUNTPOINT;
2360 char wim_filename[wim_filename_len + 1];
2361 if (getxattr(dir, "wimfs.wim_filename",
2362 wim_filename, wim_filename_len) != wim_filename_len)
2363 return WIMLIB_ERR_NOT_A_MOUNTPOINT;
2364 wim_filename[wim_filename_len] = '\0';
2366 progress.unmount.mountpoint = dir;
2367 progress.unmount.mounted_wim = wim_filename;
2368 progress.unmount.mounted_image = mounted_image;
2369 progress.unmount.mount_flags = mount_flags;
2370 progress.unmount.unmount_flags = unmount_flags;
2372 return call_progress(progfunc, WIMLIB_PROGRESS_MSG_UNMOUNT_BEGIN,
2373 &progress, progctx);
2376 /* API function documented in wimlib.h */
2378 wimlib_unmount_image_with_progress(const char *dir, int unmount_flags,
2379 wimlib_progress_func_t progfunc, void *progctx)
2384 wimlib_global_init(WIMLIB_INIT_FLAG_ASSUME_UTF8);
2386 if (unmount_flags & ~(WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY |
2387 WIMLIB_UNMOUNT_FLAG_COMMIT |
2388 WIMLIB_UNMOUNT_FLAG_REBUILD |
2389 WIMLIB_UNMOUNT_FLAG_RECOMPRESS |
2390 WIMLIB_UNMOUNT_FLAG_FORCE |
2391 WIMLIB_UNMOUNT_FLAG_NEW_IMAGE))
2392 return WIMLIB_ERR_INVALID_PARAM;
2394 ret = begin_unmount(dir, unmount_flags, &mount_flags,
2399 if ((unmount_flags & WIMLIB_UNMOUNT_FLAG_COMMIT) &&
2400 (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE))
2401 return do_unmount_commit(dir, unmount_flags,
2404 return do_unmount_discard(dir);
2407 #else /* WITH_FUSE */
2411 mount_unsupported_error(void)
2413 #if defined(__WIN32__)
2414 ERROR("Sorry-- Mounting WIM images is not supported on Windows!");
2416 ERROR("wimlib was compiled with --without-fuse, which disables support "
2417 "for mounting WIMs.");
2419 return WIMLIB_ERR_UNSUPPORTED;
2423 wimlib_unmount_image_with_progress(const tchar *dir, int unmount_flags,
2424 wimlib_progress_func_t progfunc, void *progctx)
2426 return mount_unsupported_error();
2430 wimlib_mount_image(WIMStruct *wim, int image, const tchar *dir,
2431 int mount_flags, const tchar *staging_dir)
2433 return mount_unsupported_error();
2436 #endif /* !WITH_FUSE */
2439 wimlib_unmount_image(const tchar *dir, int unmount_flags)
2441 return wimlib_unmount_image_with_progress(dir, unmount_flags, NULL, NULL);