4 * This file implements mounting of WIM images using FUSE
5 * (Filesystem in Userspace). See http://fuse.sourceforge.net/.
7 * Currently it is only expected to work on Linux.
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/.
32 #include "wimlib/error.h"
37 # error "FUSE mount not supported on Windows! Please configure --without-fuse"
40 #define FUSE_USE_VERSION 26
42 #include <attr/xattr.h>
53 #include <sys/types.h>
57 #include "wimlib/blob_table.h"
58 #include "wimlib/dentry.h"
59 #include "wimlib/encoding.h"
60 #include "wimlib/metadata.h"
61 #include "wimlib/paths.h"
62 #include "wimlib/progress.h"
63 #include "wimlib/reparse.h"
64 #include "wimlib/timestamp.h"
65 #include "wimlib/unix_data.h"
66 #include "wimlib/write.h"
67 #include "wimlib/xml.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 blob descriptor for the data stream that has been
101 * opened. 'num_opened_fds' of the blob descriptor tracks the number of
102 * 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 blob descriptor. */
105 struct blob_descriptor *f_blob;
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 streams are changed by a deletion.
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 blobs in the mounted image, linked by
162 * 'struct blob_descriptor'.orig_blob_list. */
163 struct list_head orig_blob_list;
165 /* Parameters for unmounting the image (can be set via extended
166 * attribute "wimfs.unmount_info"). */
167 struct wimfs_unmount_info unmount_info;
170 #define WIMFS_CTX(fuse_ctx) ((struct wimfs_context*)(fuse_ctx)->private_data)
172 /* Retrieve the context structure for the currently mounted WIM image.
174 * Note: this is a per-thread variable. It is possible for different threads to
175 * mount different images at the same time in the same process, although they
176 * must use different WIMStructs! */
177 static inline struct wimfs_context *
178 wimfs_get_context(void)
180 return WIMFS_CTX(fuse_get_context());
184 wimfs_inc_num_open_fds(void)
186 wimfs_get_context()->num_open_fds++;
190 wimfs_dec_num_open_fds(void)
192 wimfs_get_context()->num_open_fds--;
195 /* Retrieve the WIMStruct for the currently mounted WIM image. */
196 static inline WIMStruct *
197 wimfs_get_WIMStruct(void)
199 return wimfs_get_context()->wim;
202 /* Is write permission requested on the file? */
204 flags_writable(int open_flags)
206 int accmode = (open_flags & O_ACCMODE);
207 return (accmode == O_RDWR || accmode == O_WRONLY);
211 fuse_mask_mode(mode_t mode, const struct fuse_context *fuse_ctx)
213 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 8)
214 mode &= ~fuse_ctx->umask;
220 * Allocate a file descriptor to a data stream in the mounted WIM image.
223 * The inode containing the stream being opened
225 * The stream of the inode being opened
227 * On success, a pointer to the new file descriptor will be stored here.
229 * Returns 0 or a -errno code.
232 alloc_wimfs_fd(struct wim_inode *inode,
233 struct wim_inode_stream *strm,
234 struct wimfs_fd **fd_ret)
236 static const u16 min_fds_per_alloc = 8;
237 static const u16 max_fds = 0xffff;
241 if (inode->i_num_opened_fds == inode->i_num_allocated_fds) {
243 struct wimfs_fd **fds;
245 /* Expand this inode's file descriptor table. */
247 num_new_fds = max(min_fds_per_alloc,
248 inode->i_num_allocated_fds / 4);
250 num_new_fds = min(num_new_fds,
251 max_fds - inode->i_num_allocated_fds);
253 if (num_new_fds == 0)
256 fds = REALLOC(inode->i_fds,
257 (inode->i_num_allocated_fds + num_new_fds) *
262 memset(&fds[inode->i_num_allocated_fds], 0,
263 num_new_fds * sizeof(fds[0]));
265 inode->i_num_allocated_fds += num_new_fds;
266 inode->i_next_fd = inode->i_num_opened_fds;
269 /* Allocate the file descriptor in the first available space in the
270 * inode's file descriptor table.
272 * i_next_fd is the lower bound on the next open slot. */
273 for (i = inode->i_next_fd; inode->i_fds[i]; i++)
276 fd = MALLOC(sizeof(*fd));
281 fd->f_blob = stream_blob_resolved(strm);
282 filedes_invalidate(&fd->f_staging_fd);
284 fd->f_stream_id = strm->stream_id;
286 inode->i_fds[i] = fd;
287 inode->i_num_opened_fds++;
289 fd->f_blob->num_opened_fds++;
290 wimfs_inc_num_open_fds();
291 inode->i_next_fd = i + 1;
296 * Close a file descriptor to a data stream in the mounted WIM image.
298 * Returns 0 or a -errno code. The file descriptor is always closed.
301 close_wimfs_fd(struct wimfs_fd *fd)
304 struct wim_inode *inode;
306 /* Close the staging file if open. */
307 if (filedes_valid(&fd->f_staging_fd))
308 if (filedes_close(&fd->f_staging_fd))
311 /* Release this file descriptor from its blob descriptor. */
313 blob_decrement_num_opened_fds(fd->f_blob);
315 wimfs_dec_num_open_fds();
317 /* Release this file descriptor from its inode. */
319 inode->i_fds[fd->f_idx] = NULL;
320 if (fd->f_idx < inode->i_next_fd)
321 inode->i_next_fd = fd->f_idx;
323 inode_dec_num_opened_fds(inode);
328 * Translate a path into the corresponding inode in the mounted WIM image.
330 * See get_dentry() for more information.
332 * Returns a pointer to the resulting inode, or NULL with errno set.
334 static struct wim_inode *
335 wim_pathname_to_inode(WIMStruct *wim, const char *path)
337 struct wim_dentry *dentry;
339 dentry = get_dentry(wim, path, WIMLIB_CASE_SENSITIVE);
342 return dentry->d_inode;
345 /* Can look up named data stream with colon syntax */
346 #define LOOKUP_FLAG_ADS_OK 0x01
348 /* Can look up directory (otherwise get -ENOTDIR) */
349 #define LOOKUP_FLAG_DIRECTORY_OK 0x02
351 /* Get the data stream of the specified name from the specified inode. Returns
352 * NULL with errno set if not found. */
353 static struct wim_inode_stream *
354 inode_get_data_stream_tstr(const struct wim_inode *inode,
355 const char *stream_name)
357 struct wim_inode_stream *strm;
359 if (!stream_name || !*stream_name) {
360 strm = inode_get_unnamed_data_stream(inode);
362 const utf16lechar *uname;
364 if (tstr_get_utf16le(stream_name, &uname))
366 strm = inode_get_stream(inode, STREAM_TYPE_DATA, uname);
367 tstr_put_utf16le(uname);
375 * Translate a path into the corresponding dentry and stream in the mounted WIM
378 * Returns 0 or a -errno code. @dentry_ret and @strm_ret are both optional.
381 wim_pathname_to_stream(const struct wimfs_context *ctx,
384 struct wim_dentry **dentry_ret,
385 struct wim_inode_stream **strm_ret)
387 WIMStruct *wim = ctx->wim;
388 struct wim_dentry *dentry;
389 struct wim_inode *inode;
390 struct wim_inode_stream *strm;
391 const char *stream_name = NULL;
394 lookup_flags |= ctx->default_lookup_flags;
396 if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
397 stream_name = path_stream_name(path);
399 p = (char *)stream_name - 1;
404 dentry = get_dentry(wim, path, WIMLIB_CASE_SENSITIVE);
410 inode = dentry->d_inode;
412 if (inode_resolve_streams(inode, wim->blob_table, false))
415 if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
416 && inode_is_directory(inode))
419 strm = inode_get_data_stream_tstr(inode, stream_name);
421 /* Force creation of an unnamed data stream */
423 strm = inode_add_stream(inode, STREAM_TYPE_DATA,
424 NO_STREAM_NAME, NULL);
430 *dentry_ret = dentry;
437 * Create a new file in the mounted WIM image.
440 * The FUSE context for the mounted image.
442 * The path at which to create the first link to the new file. If a file
443 * already exists at this path, -EEXIST is returned.
445 * The UNIX mode for the new file. This is only honored if
446 * WIMLIB_MOUNT_FLAG_UNIX_DATA was passed to wimlib_mount_image().
448 * The device ID for the new file, encoding the major and minor device
449 * numbers. This is only honored if WIMLIB_MOUNT_FLAG_UNIX_DATA was passed
450 * to wimlib_mount_image().
452 * Windows file attributes to use for the new file.
454 * On success, a pointer to the new dentry is returned here. Its d_inode
455 * member will point to the new inode that was created for it and added to
456 * the mounted WIM image.
458 * Returns 0 or a -errno code.
461 create_file(struct fuse_context *fuse_ctx, const char *path,
462 mode_t mode, dev_t rdev, u32 attributes,
463 struct wim_dentry **dentry_ret)
465 struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
466 struct wim_dentry *parent;
467 const char *basename;
468 struct wim_dentry *new_dentry;
469 struct wim_inode *new_inode;
471 parent = get_parent_dentry(wimfs_ctx->wim, path, WIMLIB_CASE_SENSITIVE);
475 if (!dentry_is_directory(parent))
478 basename = path_basename(path);
480 if (get_dentry_child_with_name(parent, basename, WIMLIB_CASE_SENSITIVE))
483 if (new_dentry_with_new_inode(basename, true, &new_dentry))
486 new_inode = new_dentry->d_inode;
488 new_inode->i_ino = wimfs_ctx->next_ino++;
489 new_inode->i_attributes = attributes;
491 if (wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA) {
492 struct wimlib_unix_data unix_data;
494 unix_data.uid = fuse_ctx->uid;
495 unix_data.gid = fuse_ctx->gid;
496 unix_data.mode = fuse_mask_mode(mode, fuse_ctx);
497 unix_data.rdev = rdev;
498 if (!inode_set_unix_data(new_inode, &unix_data, UNIX_DATA_ALL))
500 free_dentry(new_dentry);
505 hlist_add_head(&new_inode->i_hlist,
506 &wim_get_current_image_metadata(wimfs_ctx->wim)->inode_list);
508 dentry_add_child(parent, new_dentry);
510 *dentry_ret = new_dentry;
515 * Remove a dentry from the mounted WIM image; i.e. remove an alias for an
519 remove_dentry(struct wim_dentry *dentry, struct blob_table *blob_table)
521 /* Drop blob references. */
522 inode_unref_blobs(dentry->d_inode, blob_table);
524 /* Unlink the dentry from the image's dentry tree. */
525 unlink_dentry(dentry);
527 /* Delete the dentry. This will also decrement the link count of the
528 * corresponding inode, and possibly cause it to be deleted as well. */
532 /* Generate UNIX filetype mode bits for the specified WIM inode, based on its
533 * Windows file attributes. */
535 inode_unix_file_type(const struct wim_inode *inode)
537 if (inode_is_symlink(inode))
539 else if (inode_is_directory(inode))
545 /* Generate a default UNIX mode for the specified WIM inode. */
547 inode_default_unix_mode(const struct wim_inode *inode)
549 return inode_unix_file_type(inode) | 0777;
553 blob_size(const struct blob_descriptor *blob)
561 blob_stored_size(const struct blob_descriptor *blob)
565 if (blob->blob_location == BLOB_IN_WIM &&
566 blob->size == blob->rdesc->uncompressed_size)
567 return blob->rdesc->size_in_wim;
572 * Retrieve standard UNIX metadata ('struct stat') for a WIM inode.
574 * @blob is the blob descriptor for the stream of the inode that is being
575 * queried, or NULL. We mostly return the same information for all streams, but
576 * st_size and st_blocks may be different for different streams.
578 * This always returns 0.
581 inode_to_stbuf(const struct wim_inode *inode,
582 const struct blob_descriptor *blob, struct stat *stbuf)
584 const struct wimfs_context *ctx = wimfs_get_context();
585 struct wimlib_unix_data unix_data;
587 memset(stbuf, 0, sizeof(struct stat));
588 if ((ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA) &&
589 inode_get_unix_data(inode, &unix_data))
591 /* Use the user ID, group ID, mode, and device ID from the
592 * inode's extra UNIX metadata information. */
593 stbuf->st_uid = unix_data.uid;
594 stbuf->st_gid = unix_data.gid;
595 stbuf->st_mode = unix_data.mode;
596 stbuf->st_rdev = unix_data.rdev;
598 /* Generate default values for the user ID, group ID, and mode.
600 * Note: in the case of an allow_other mount, fuse_context.uid
601 * may not be the same as wimfs_context.owner_uid! */
602 stbuf->st_uid = ctx->owner_uid;
603 stbuf->st_gid = ctx->owner_gid;
604 stbuf->st_mode = inode_default_unix_mode(inode);
606 stbuf->st_ino = inode->i_ino;
607 stbuf->st_nlink = inode->i_nlink;
608 stbuf->st_size = blob_size(blob);
609 #ifdef HAVE_STAT_NANOSECOND_PRECISION
610 stbuf->st_atim = wim_timestamp_to_timespec(inode->i_last_access_time);
611 stbuf->st_mtim = wim_timestamp_to_timespec(inode->i_last_write_time);
612 stbuf->st_ctim = stbuf->st_mtim;
614 stbuf->st_atime = wim_timestamp_to_time_t(inode->i_last_access_time);
615 stbuf->st_mtime = wim_timestamp_to_time_t(inode->i_last_write_time);
616 stbuf->st_ctime = stbuf->st_mtime;
618 stbuf->st_blocks = DIV_ROUND_UP(blob_stored_size(blob), 512);
622 /* Update the last access and last write timestamps of a WIM inode. */
624 touch_inode(struct wim_inode *inode)
626 u64 now = now_as_wim_timestamp();
627 inode->i_last_access_time = now;
628 inode->i_last_write_time = now;
632 touch_parent(struct wim_dentry *dentry)
634 touch_inode(dentry->d_parent->d_inode);
638 * Create a new file in the staging directory for a read-write mounted image.
640 * On success, returns the file descriptor for the new staging file, opened for
641 * writing. In addition, stores the allocated name of the staging file in
644 * On failure, returns -1 and sets errno.
647 create_staging_file(const struct wimfs_context *ctx, char **name_ret)
650 static const size_t STAGING_FILE_NAME_LEN = 20;
654 name = MALLOC(STAGING_FILE_NAME_LEN + 1);
657 name[STAGING_FILE_NAME_LEN] = '\0';
660 randomize_char_array_with_alnum(name, STAGING_FILE_NAME_LEN);
661 fd = openat(ctx->staging_dir_fd, name,
662 O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, 0600);
663 if (unlikely(fd < 0)) {
664 if (unlikely(errno == EEXIST))
665 /* Try again with another name. */
675 * Extract a blob to the staging directory. This is necessary when a stream
676 * using the blob is being opened for writing and the blob has not already been
677 * extracted to the staging directory.
680 * The inode containing the stream being opened for writing.
682 * The stream being opened for writing. The blob descriptor to which the
683 * stream refers will be changed by this function.
685 * Number of bytes of the blob to extract and include in the staging file.
686 * It may be less than the actual blob length, in which case only a prefix
687 * of the blob will be extracted. It may also be more than the actual blob
688 * length, in which case the extra space will be zero-filled.
690 * Returns 0 or a -errno code.
693 extract_blob_to_staging_dir(struct wim_inode *inode,
694 struct wim_inode_stream *strm,
695 off_t size, const struct wimfs_context *ctx)
697 struct blob_descriptor *old_blob;
698 struct blob_descriptor *new_blob;
699 char *staging_file_name;
705 old_blob = stream_blob_resolved(strm);
707 /* Create the staging file. */
708 staging_fd = create_staging_file(ctx, &staging_file_name);
709 if (unlikely(staging_fd < 0))
712 /* Extract the stream to the staging file (possibly truncated). */
716 filedes_init(&fd, staging_fd);
718 extract_size = min(old_blob->size, size);
719 result = extract_blob_to_fd(old_blob, &fd, extract_size);
725 /* In the case of truncate() to more than the file length, extend the
726 * staging file with zeroes by calling ftruncate(). */
727 if (!result && size > extract_size)
728 result = ftruncate(staging_fd, size);
730 /* Close the staging file. */
731 if (close(staging_fd))
734 /* If an error occurred, unlink the staging file. */
735 if (unlikely(result)) {
736 /* extract_blob_to_fd() should set errno, but if it didn't,
737 * set a default value. */
738 ret = errno ? -errno : -EIO;
739 goto out_delete_staging_file;
742 /* Create a blob descriptor for the staging file. */
743 new_blob = new_blob_descriptor();
744 if (unlikely(!new_blob)) {
746 goto out_delete_staging_file;
749 /* There may already be open file descriptors to this stream if it's
750 * previously been opened read-only, but just now we're opening it
751 * read-write. Identify those file descriptors, update them to use the
752 * new blob descriptor, and open staging file descriptors for them. */
753 for (u16 i = 0, j = 0; j < inode->i_num_opened_fds; i++) {
757 fd = inode->i_fds[i];
763 if (fd->f_stream_id != strm->stream_id)
766 /* This is a readonly fd for the same stream. */
767 fd->f_blob = new_blob;
768 new_blob->num_opened_fds++;
769 raw_fd = openat(ctx->staging_dir_fd, staging_file_name,
770 O_RDONLY | O_NOFOLLOW);
771 if (unlikely(raw_fd < 0)) {
773 goto out_revert_fd_changes;
775 filedes_init(&fd->f_staging_fd, raw_fd);
779 old_blob->num_opened_fds -= new_blob->num_opened_fds;
781 new_blob->blob_location = BLOB_IN_STAGING_FILE;
782 new_blob->staging_file_name = staging_file_name;
783 new_blob->staging_dir_fd = ctx->staging_dir_fd;
784 new_blob->size = size;
786 prepare_unhashed_blob(new_blob, inode, strm->stream_id,
787 &wim_get_current_image_metadata(ctx->wim)->unhashed_blobs);
788 inode_replace_stream_blob(inode, strm, new_blob, ctx->wim->blob_table);
791 out_revert_fd_changes:
792 for (u16 i = 0; new_blob->num_opened_fds; i++) {
793 struct wimfs_fd *fd = inode->i_fds[i];
794 if (fd && fd->f_stream_id == strm->stream_id) {
795 fd->f_blob = old_blob;
796 if (filedes_valid(&fd->f_staging_fd)) {
797 filedes_close(&fd->f_staging_fd);
798 filedes_invalidate(&fd->f_staging_fd);
800 new_blob->num_opened_fds--;
803 free_blob_descriptor(new_blob);
804 out_delete_staging_file:
805 unlinkat(ctx->staging_dir_fd, staging_file_name, 0);
806 FREE(staging_file_name);
811 * Create the staging directory for the WIM file.
813 * The staging directory will be created in the directory specified by the open
814 * file descriptor @parent_dir_fd. It will be given a randomly generated name
815 * based on @wim_basename, the name of the WIM file.
817 * On success, returns a file descriptor to the open staging directory with
818 * O_RDONLY access. In addition, stores the allocated name of the staging
819 * directory (relative to @parent_dir_fd) in @staging_dir_name_ret.
820 * On failure, returns -1 and sets errno.
823 make_staging_dir_at(int parent_dir_fd, const char *wim_basename,
824 char **staging_dir_name_ret)
826 static const char common_suffix[8] = ".staging";
827 static const size_t random_suffix_len = 10;
828 size_t wim_basename_len;
829 size_t staging_dir_name_len;
830 char *staging_dir_name;
834 wim_basename_len = strlen(wim_basename);
835 staging_dir_name_len = wim_basename_len + sizeof(common_suffix) +
837 staging_dir_name = MALLOC(staging_dir_name_len + 1);
838 if (!staging_dir_name)
841 p = staging_dir_name;
842 p = mempcpy(p, wim_basename, wim_basename_len);
843 p = mempcpy(p, common_suffix, sizeof(common_suffix));
844 randomize_char_array_with_alnum(p, random_suffix_len);
845 p += random_suffix_len;
848 if (mkdirat(parent_dir_fd, staging_dir_name, 0700))
851 fd = openat(parent_dir_fd, staging_dir_name,
852 O_RDONLY | O_DIRECTORY | O_NOFOLLOW);
856 *staging_dir_name_ret = staging_dir_name;
860 unlinkat(parent_dir_fd, staging_dir_name, AT_REMOVEDIR);
862 FREE(staging_dir_name);
867 * Create the staging directory and set ctx->staging_dir_fd,
868 * ctx->staging_dir_name, and ctx->parent_dir_fd.
871 make_staging_dir(struct wimfs_context *ctx, const char *parent_dir_path)
873 const char *wim_basename;
877 wim_basename = path_basename(ctx->wim->filename);
879 if (!parent_dir_path) {
880 /* The user did not specify a directory. Default to creating
881 * the staging directory alongside the WIM file. */
882 if (wim_basename > ctx->wim->filename) {
883 parent_dir_path = ctx->wim->filename;
884 end = (char *)(wim_basename - 1);
885 /* *end must be a slash. Temporarily overwrite it so we
886 * can open the parent directory. */
889 parent_dir_path = ".";
893 /* Open the parent directory (in which we'll create our staging
895 ctx->parent_dir_fd = open(parent_dir_path, O_RDONLY | O_DIRECTORY);
896 if (ctx->parent_dir_fd < 0) {
897 ERROR_WITH_ERRNO("Can't open directory \"%s\"",
899 ret = WIMLIB_ERR_OPENDIR;
900 goto out_restore_wim_filename;
903 ctx->staging_dir_fd = make_staging_dir_at(ctx->parent_dir_fd,
905 &ctx->staging_dir_name);
906 if (ctx->staging_dir_fd < 0) {
907 ERROR_WITH_ERRNO("Can't create staging directory in \"%s\"",
909 close(ctx->parent_dir_fd);
910 ret = WIMLIB_ERR_MKDIR;
911 goto out_restore_wim_filename;
914 out_restore_wim_filename:
920 /* Deletes the staging directory, undoing the effects of a successful call to
921 * make_staging_dir(). */
923 delete_staging_dir(struct wimfs_context *ctx)
928 dir = fdopendir(ctx->staging_dir_fd);
930 while ((ent = readdir(dir)))
931 unlinkat(ctx->staging_dir_fd, ent->d_name, 0);
934 close(ctx->staging_dir_fd);
936 if (unlinkat(ctx->parent_dir_fd, ctx->staging_dir_name, AT_REMOVEDIR))
937 WARNING_WITH_ERRNO("Could not delete staging directory");
938 FREE(ctx->staging_dir_name);
939 close(ctx->parent_dir_fd);
943 prepare_inodes(struct wimfs_context *ctx)
945 struct wim_image_metadata *imd;
946 struct wim_inode *inode;
949 imd = wim_get_current_image_metadata(ctx->wim);
950 image_for_each_inode(inode, imd) {
951 inode->i_ino = ctx->next_ino++;
952 inode->i_num_opened_fds = 0;
953 inode->i_num_allocated_fds = 0;
959 release_extra_refcnts(struct wimfs_context *ctx)
961 struct list_head *list = &ctx->orig_blob_list;
962 struct blob_table *blob_table = ctx->wim->blob_table;
963 struct blob_descriptor *blob, *tmp;
965 list_for_each_entry_safe(blob, tmp, list, orig_blob_list)
966 blob_subtract_refcnt(blob, blob_table, blob->out_refcnt);
969 /* Delete the 'struct blob_descriptor' for any stream that was modified
970 * or created in the read-write mounted image and had a final size of 0. */
972 delete_empty_blobs(struct wimfs_context *ctx)
974 struct blob_descriptor *blob, *tmp;
975 struct wim_image_metadata *imd;
977 imd = wim_get_current_image_metadata(ctx->wim);
979 image_for_each_unhashed_blob_safe(blob, tmp, imd) {
981 *retrieve_pointer_to_unhashed_blob(blob) = NULL;
982 list_del(&blob->unhashed_list);
983 free_blob_descriptor(blob);
988 /* Close all file descriptors open to the specified inode.
990 * Note: closing the last file descriptor might free the inode. */
992 inode_close_fds(struct wim_inode *inode)
994 u16 num_open_fds = inode->i_num_opened_fds;
995 for (u16 i = 0; num_open_fds; i++) {
996 if (inode->i_fds[i]) {
997 close_wimfs_fd(inode->i_fds[i]);
1003 /* Close all file descriptors open to the mounted image. */
1005 close_all_fds(struct wimfs_context *ctx)
1007 struct wim_inode *inode;
1008 struct hlist_node *tmp;
1009 struct wim_image_metadata *imd;
1011 imd = wim_get_current_image_metadata(ctx->wim);
1013 image_for_each_inode_safe(inode, tmp, imd)
1014 inode_close_fds(inode);
1017 /* Moves the currently selected image, which may have been modified, to a new
1018 * index, and sets the original index to refer to a reset (unmodified) copy of
1021 renew_current_image(struct wimfs_context *ctx)
1023 WIMStruct *wim = ctx->wim;
1024 int idx = wim->current_image - 1;
1025 struct wim_image_metadata *imd = wim->image_metadata[idx];
1026 struct wim_image_metadata *replace_imd;
1027 struct blob_descriptor *new_blob;
1030 /* Create 'replace_imd' structure to use for the reset original,
1031 * unmodified image. */
1032 ret = WIMLIB_ERR_NOMEM;
1033 replace_imd = new_image_metadata();
1037 /* Create new blob descriptor for the modified image's metadata
1038 * resource, which doesn't exist yet. */
1039 ret = WIMLIB_ERR_NOMEM;
1040 new_blob = new_blob_descriptor();
1042 goto err_put_replace_imd;
1044 new_blob->refcnt = 1;
1045 new_blob->unhashed = 1;
1046 new_blob->is_metadata = 1;
1048 /* Make the image being moved available at a new index. Increments the
1049 * WIM's image count, but does not increment the reference count of the
1050 * 'struct image_metadata'. */
1051 ret = append_image_metadata(wim, imd);
1053 goto err_free_new_blob;
1055 ret = xml_add_image(wim, "");
1057 goto err_undo_append;
1059 replace_imd->metadata_blob = imd->metadata_blob;
1060 imd->metadata_blob = new_blob;
1061 wim->image_metadata[idx] = replace_imd;
1062 wim->current_image = wim->hdr.image_count;
1066 wim->hdr.image_count--;
1068 free_blob_descriptor(new_blob);
1069 err_put_replace_imd:
1070 put_image_metadata(replace_imd, NULL);
1075 static enum wimlib_progress_status
1076 commit_progress_func(enum wimlib_progress_msg msg,
1077 union wimlib_progress_info *info, void *progctx)
1079 mqd_t mq = *(mqd_t *)progctx;
1080 struct commit_progress_report report;
1082 memset(&report, 0, sizeof(report));
1085 report.info = *info;
1086 mq_send(mq, (const char *)&report, sizeof(report), 1);
1087 return WIMLIB_PROGRESS_STATUS_CONTINUE;
1090 /* Commit the mounted image to the underlying WIM file. */
1092 commit_image(struct wimfs_context *ctx, int unmount_flags, mqd_t mq)
1096 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_SEND_PROGRESS)
1097 wimlib_register_progress_function(ctx->wim,
1098 commit_progress_func, &mq);
1100 wimlib_register_progress_function(ctx->wim, NULL, NULL);
1102 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_NEW_IMAGE) {
1103 int ret = renew_current_image(ctx);
1107 release_extra_refcnts(ctx);
1109 INIT_LIST_HEAD(&ctx->orig_blob_list);
1110 delete_empty_blobs(ctx);
1111 xml_update_image_info(ctx->wim, ctx->wim->current_image);
1115 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY)
1116 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1118 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_REBUILD)
1119 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1121 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_RECOMPRESS)
1122 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
1124 return wimlib_overwrite(ctx->wim, write_flags, 0);
1127 /* In the case of an allow_other mount, only the mount owner and root are
1128 * allowed to unmount the filesystem. */
1130 may_unmount_wimfs(void)
1132 const struct fuse_context *fuse_ctx = fuse_get_context();
1133 const struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
1135 return (fuse_ctx->uid == wimfs_ctx->owner_uid ||
1136 fuse_ctx->uid == 0);
1139 /* Unmount the mounted image, called from the daemon process. */
1143 struct fuse_context *fuse_ctx = fuse_get_context();
1144 struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
1145 const struct wimfs_unmount_info *info = &wimfs_ctx->unmount_info;
1146 int unmount_flags = info->unmount_flags;
1147 mqd_t mq = (mqd_t)-1;
1150 /* Ignore COMMIT if the image is mounted read-only. */
1151 if (!(wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE))
1152 unmount_flags &= ~WIMLIB_UNMOUNT_FLAG_COMMIT;
1154 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_SEND_PROGRESS) {
1155 mq = mq_open(info->mq_name, O_WRONLY | O_NONBLOCK);
1156 if (mq == (mqd_t)-1) {
1157 ret = WIMLIB_ERR_MQUEUE;
1162 if (wimfs_ctx->num_open_fds) {
1164 /* There are still open file descriptors to the image. */
1166 /* With COMMIT, refuse to unmount unless FORCE is also
1168 if ((unmount_flags & (WIMLIB_UNMOUNT_FLAG_COMMIT |
1169 WIMLIB_UNMOUNT_FLAG_FORCE))
1170 == WIMLIB_UNMOUNT_FLAG_COMMIT)
1172 ret = WIMLIB_ERR_MOUNTED_IMAGE_IS_BUSY;
1176 /* Force-close all file descriptors. */
1177 close_all_fds(wimfs_ctx);
1180 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_COMMIT)
1181 ret = commit_image(wimfs_ctx, unmount_flags, mq);
1183 ret = 0; /* Read-only mount, or discarding changes to
1184 a read-write mount */
1187 /* Leave the image mounted if commit failed, unless this is a
1188 * forced unmount. The user can retry without COMMIT if they
1190 if (!ret || (unmount_flags & WIMLIB_UNMOUNT_FLAG_FORCE)) {
1191 unlock_wim_for_append(wimfs_ctx->wim);
1192 fuse_exit(fuse_ctx->fuse);
1194 if (mq != (mqd_t)-1)
1200 wimfs_chmod(const char *path, mode_t mask)
1202 const struct wimfs_context *ctx = wimfs_get_context();
1203 struct wim_inode *inode;
1204 struct wimlib_unix_data unix_data;
1206 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA))
1209 inode = wim_pathname_to_inode(ctx->wim, path);
1213 unix_data.uid = ctx->owner_uid;
1214 unix_data.gid = ctx->owner_gid;
1215 unix_data.mode = mask;
1218 if (!inode_set_unix_data(inode, &unix_data, UNIX_DATA_MODE))
1225 wimfs_chown(const char *path, uid_t uid, gid_t gid)
1227 const struct wimfs_context *ctx = wimfs_get_context();
1228 struct wim_inode *inode;
1229 struct wimlib_unix_data unix_data;
1232 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA))
1235 inode = wim_pathname_to_inode(ctx->wim, path);
1241 if (uid != (uid_t)-1)
1242 which |= UNIX_DATA_UID;
1244 uid = ctx->owner_uid;
1246 if (gid != (gid_t)-1)
1247 which |= UNIX_DATA_GID;
1249 gid = ctx->owner_gid;
1251 unix_data.uid = uid;
1252 unix_data.gid = gid;
1253 unix_data.mode = inode_default_unix_mode(inode);
1256 if (!inode_set_unix_data(inode, &unix_data, which))
1263 wimfs_fgetattr(const char *path, struct stat *stbuf, struct fuse_file_info *fi)
1265 struct wimfs_fd *fd = WIMFS_FD(fi);
1266 return inode_to_stbuf(fd->f_inode, fd->f_blob, stbuf);
1270 wimfs_ftruncate(const char *path, off_t size, struct fuse_file_info *fi)
1272 struct wimfs_fd *fd = WIMFS_FD(fi);
1273 if (ftruncate(fd->f_staging_fd.fd, size))
1275 touch_inode(fd->f_inode);
1276 fd->f_blob->size = size;
1281 wimfs_getattr(const char *path, struct stat *stbuf)
1283 const struct wimfs_context *ctx = wimfs_get_context();
1284 struct wim_dentry *dentry;
1285 struct wim_inode_stream *strm;
1288 ret = wim_pathname_to_stream(ctx, path, LOOKUP_FLAG_DIRECTORY_OK,
1293 return inode_to_stbuf(dentry->d_inode,
1294 stream_blob_resolved(strm), stbuf);
1298 copy_xattr(char *dest, size_t destsize, const void *src, size_t srcsize)
1301 if (destsize < srcsize)
1303 memcpy(dest, src, srcsize);
1309 wimfs_getxattr(const char *path, const char *name, char *value,
1312 const struct wimfs_context *ctx = wimfs_get_context();
1313 struct wim_inode *inode;
1314 struct wim_inode_stream *strm;
1315 struct blob_descriptor *blob;
1317 if (!strncmp(name, "wimfs.", 6)) {
1318 /* Handle some magical extended attributes. These really should
1319 * be ioctls, but directory ioctls aren't supported until
1320 * libfuse 2.9, and even then they are broken. */
1322 if (!strcmp(name, "wim_filename")) {
1323 return copy_xattr(value, size, ctx->wim->filename,
1324 strlen(ctx->wim->filename));
1326 if (!strcmp(name, "wim_info")) {
1327 struct wimlib_wim_info info;
1329 wimlib_get_wim_info(ctx->wim, &info);
1331 return copy_xattr(value, size, &info, sizeof(info));
1333 if (!strcmp(name, "mounted_image")) {
1334 return copy_xattr(value, size,
1335 &ctx->wim->current_image, sizeof(int));
1337 if (!strcmp(name, "mount_flags")) {
1338 return copy_xattr(value, size,
1339 &ctx->mount_flags, sizeof(int));
1341 if (!strcmp(name, "unmount")) {
1342 if (!may_unmount_wimfs())
1347 if (size < sizeof(int))
1349 status = unmount_wimfs();
1350 memcpy(value, &status, sizeof(int));
1357 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1360 if (strncmp(name, "user.", 5))
1367 /* Querying a named data stream */
1369 inode = wim_pathname_to_inode(ctx->wim, path);
1373 strm = inode_get_data_stream_tstr(inode, name);
1375 return (errno == ENOENT) ? -ENOATTR : -errno;
1377 blob = stream_blob_resolved(strm);
1381 if (unlikely(blob->size > INT_MAX))
1385 if (size < blob->size)
1388 if (read_full_blob_into_buf(blob, value))
1389 return errno ? -errno : -EIO;
1395 wimfs_link(const char *existing_path, const char *new_path)
1397 WIMStruct *wim = wimfs_get_WIMStruct();
1398 const char *new_name;
1399 struct wim_inode *inode;
1400 struct wim_dentry *dir;
1401 struct wim_dentry *new_alias;
1403 inode = wim_pathname_to_inode(wim, existing_path);
1407 if (inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
1408 FILE_ATTRIBUTE_REPARSE_POINT))
1411 new_name = path_basename(new_path);
1413 dir = get_parent_dentry(wim, new_path, WIMLIB_CASE_SENSITIVE);
1417 if (!dentry_is_directory(dir))
1420 if (get_dentry_child_with_name(dir, new_name, WIMLIB_CASE_SENSITIVE))
1423 if (new_dentry_with_existing_inode(new_name, inode, &new_alias))
1426 dentry_add_child(dir, new_alias);
1427 touch_inode(dir->d_inode);
1432 wimfs_listxattr(const char *path, char *list, size_t size)
1434 const struct wimfs_context *ctx = wimfs_get_context();
1435 const struct wim_inode *inode;
1437 char *end = list + size;
1440 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1443 /* List named data streams, or get the list size. We report each named
1444 * data stream "X" as an extended attribute "user.X". */
1446 inode = wim_pathname_to_inode(ctx->wim, path);
1450 for (unsigned i = 0; i < inode->i_num_streams; i++) {
1451 const struct wim_inode_stream *strm;
1452 char *stream_name_mbs;
1453 size_t stream_name_mbs_nbytes;
1455 strm = &inode->i_streams[i];
1457 if (!stream_is_named_data_stream(strm))
1460 if (utf16le_to_tstr(strm->stream_name,
1461 utf16le_len_bytes(strm->stream_name),
1463 &stream_name_mbs_nbytes))
1466 if (unlikely(INT_MAX - total_size < stream_name_mbs_nbytes + 6)) {
1467 FREE(stream_name_mbs);
1471 total_size += stream_name_mbs_nbytes + 6;
1473 if (end - p < stream_name_mbs_nbytes + 6) {
1474 FREE(stream_name_mbs);
1477 p = mempcpy(p, "user.", 5);
1478 p = mempcpy(p, stream_name_mbs, stream_name_mbs_nbytes);
1481 FREE(stream_name_mbs);
1487 wimfs_mkdir(const char *path, mode_t mode)
1489 struct wim_dentry *dentry;
1492 /* Note: according to fuse.h, mode may not include S_IFDIR */
1493 ret = create_file(fuse_get_context(), path, mode | S_IFDIR, 0,
1494 FILE_ATTRIBUTE_DIRECTORY, &dentry);
1497 touch_parent(dentry);
1502 wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
1504 struct fuse_context *fuse_ctx = fuse_get_context();
1505 struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
1506 const char *stream_name;
1508 if ((wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
1509 && (stream_name = path_stream_name(path)))
1511 struct wim_inode *inode;
1512 struct wim_inode_stream *existing_strm;
1513 struct wim_inode_stream *new_strm;
1515 const utf16lechar *uname;
1517 /* Create a named data stream. */
1522 p = (char *)stream_name - 1;
1525 inode = wim_pathname_to_inode(wimfs_ctx->wim, path);
1530 if (tstr_get_utf16le(stream_name, &uname))
1533 existing_strm = inode_get_stream(inode, STREAM_TYPE_DATA, uname);
1534 if (existing_strm) {
1535 tstr_put_utf16le(uname);
1539 new_strm = inode_add_stream(inode, STREAM_TYPE_DATA, uname, NULL);
1541 tstr_put_utf16le(uname);
1547 /* Create a regular file, device node, named pipe, or socket.
1549 struct wim_dentry *dentry;
1552 if (!S_ISREG(mode) &&
1553 !(wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA))
1556 /* Note: we still use FILE_ATTRIBUTE_NORMAL for device nodes,
1557 * named pipes, and sockets. The real mode is in the UNIX
1559 ret = create_file(fuse_ctx, path, mode, rdev,
1560 FILE_ATTRIBUTE_NORMAL, &dentry);
1563 touch_parent(dentry);
1569 wimfs_open(const char *path, struct fuse_file_info *fi)
1571 struct wimfs_context *ctx = wimfs_get_context();
1572 struct wim_dentry *dentry;
1573 struct wim_inode *inode;
1574 struct wim_inode_stream *strm;
1575 struct blob_descriptor *blob;
1576 struct wimfs_fd *fd;
1579 ret = wim_pathname_to_stream(ctx, path, 0, &dentry, &strm);
1583 inode = dentry->d_inode;
1584 blob = stream_blob_resolved(strm);
1586 /* The data of the file being opened may be in the staging directory
1587 * (read-write mounts only) or in the WIM. If it's in the staging
1588 * directory, we need to open a native file descriptor for the
1589 * corresponding file. Otherwise, we can read the file data directly
1590 * from the WIM file if we are opening it read-only, but we need to
1591 * extract the data to the staging directory if we are opening it
1594 if (flags_writable(fi->flags) &&
1595 (!blob || blob->blob_location != BLOB_IN_STAGING_FILE)) {
1596 ret = extract_blob_to_staging_dir(inode,
1602 blob = stream_blob_resolved(strm);
1605 ret = alloc_wimfs_fd(inode, strm, &fd);
1609 if (blob && blob->blob_location == BLOB_IN_STAGING_FILE) {
1612 raw_fd = openat(blob->staging_dir_fd, blob->staging_file_name,
1613 (fi->flags & O_ACCMODE) | O_NOFOLLOW);
1618 filedes_init(&fd->f_staging_fd, raw_fd);
1620 fi->fh = (uintptr_t)fd;
1625 wimfs_opendir(const char *path, struct fuse_file_info *fi)
1627 WIMStruct *wim = wimfs_get_WIMStruct();
1628 struct wim_inode *inode;
1629 struct wim_inode_stream *strm;
1630 struct wimfs_fd *fd;
1633 inode = wim_pathname_to_inode(wim, path);
1636 if (!inode_is_directory(inode))
1638 strm = inode_get_unnamed_data_stream(inode);
1641 ret = alloc_wimfs_fd(inode, strm, &fd);
1644 fi->fh = (uintptr_t)fd;
1649 wimfs_read(const char *path, char *buf, size_t size,
1650 off_t offset, struct fuse_file_info *fi)
1652 struct wimfs_fd *fd = WIMFS_FD(fi);
1653 const struct blob_descriptor *blob;
1660 if (offset >= blob->size)
1663 if (size > blob->size - offset)
1664 size = blob->size - offset;
1669 switch (blob->blob_location) {
1671 if (read_partial_wim_blob_into_buf(blob, size, offset, buf))
1672 ret = errno ? -errno : -EIO;
1676 case BLOB_IN_STAGING_FILE:
1677 ret = raw_pread(&fd->f_staging_fd, buf, size, offset);
1681 case BLOB_IN_ATTACHED_BUFFER:
1682 memcpy(buf, blob->attached_buffer + offset, size);
1693 wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
1694 off_t offset, struct fuse_file_info *fi)
1696 struct wimfs_fd *fd = WIMFS_FD(fi);
1697 const struct wim_inode *inode;
1698 const struct wim_dentry *child;
1701 inode = fd->f_inode;
1703 ret = filler(buf, ".", NULL, 0);
1706 ret = filler(buf, "..", NULL, 0);
1710 for_inode_child(child, inode) {
1711 char *file_name_mbs;
1712 size_t file_name_mbs_nbytes;
1714 ret = utf16le_to_tstr(child->file_name,
1715 child->file_name_nbytes,
1717 &file_name_mbs_nbytes);
1721 ret = filler(buf, file_name_mbs, NULL, 0);
1722 FREE(file_name_mbs);
1730 wimfs_readlink(const char *path, char *buf, size_t buf_len)
1732 WIMStruct *wim = wimfs_get_WIMStruct();
1733 const struct wim_inode *inode;
1736 inode = wim_pathname_to_inode(wim, path);
1739 if (!inode_is_symlink(inode))
1743 ret = wim_inode_readlink(inode, buf, buf_len - 1, NULL);
1747 } else if (ret == -ENAMETOOLONG) {
1748 buf[buf_len - 1] = '\0';
1753 /* We use this for both release() and releasedir(), since in both cases we
1754 * simply need to close the file descriptor. */
1756 wimfs_release(const char *path, struct fuse_file_info *fi)
1758 return close_wimfs_fd(WIMFS_FD(fi));
1762 wimfs_removexattr(const char *path, const char *name)
1764 struct wimfs_context *ctx = wimfs_get_context();
1765 struct wim_inode *inode;
1766 struct wim_inode_stream *strm;
1768 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1771 if (strncmp(name, "user.", 5))
1778 /* Removing a named data stream. */
1780 inode = wim_pathname_to_inode(ctx->wim, path);
1784 strm = inode_get_data_stream_tstr(inode, name);
1786 return (errno == ENOENT) ? -ENOATTR : -errno;
1788 inode_remove_stream(inode, strm, ctx->wim->blob_table);
1793 wimfs_rename(const char *from, const char *to)
1795 return rename_wim_path(wimfs_get_WIMStruct(), from, to,
1796 WIMLIB_CASE_SENSITIVE, NULL);
1800 wimfs_rmdir(const char *path)
1802 WIMStruct *wim = wimfs_get_WIMStruct();
1803 struct wim_dentry *dentry;
1805 dentry = get_dentry(wim, path, WIMLIB_CASE_SENSITIVE);
1809 if (!dentry_is_directory(dentry))
1812 if (dentry_has_children(dentry))
1815 touch_parent(dentry);
1816 remove_dentry(dentry, wim->blob_table);
1821 wimfs_setxattr(const char *path, const char *name,
1822 const char *value, size_t size, int flags)
1824 struct wimfs_context *ctx = wimfs_get_context();
1825 struct wim_inode *inode;
1826 struct wim_inode_stream *strm;
1827 const utf16lechar *uname;
1830 if (!strncmp(name, "wimfs.", 6)) {
1831 /* Handle some magical extended attributes. These really should
1832 * be ioctls, but directory ioctls aren't supported until
1833 * libfuse 2.9, and even then they are broken. [Fixed by
1834 * libfuse commit e3b7d4c278a26520be63d99d6ea84b26906fe73d] */
1836 if (!strcmp(name, "unmount_info")) {
1837 if (!may_unmount_wimfs())
1839 if (size < sizeof(struct wimfs_unmount_info))
1841 memcpy(&ctx->unmount_info, value,
1842 sizeof(struct wimfs_unmount_info));
1848 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1851 if (strncmp(name, "user.", 5))
1858 /* Setting the contents of a named data stream. */
1860 inode = wim_pathname_to_inode(ctx->wim, path);
1864 ret = tstr_get_utf16le(name, &uname);
1868 strm = inode_get_stream(inode, STREAM_TYPE_DATA, uname);
1871 if (flags & XATTR_CREATE)
1875 if (flags & XATTR_REPLACE)
1880 if (!inode_replace_stream_data(inode, strm, value, size,
1881 ctx->wim->blob_table))
1887 if (!inode_add_stream_with_data(inode, STREAM_TYPE_DATA, uname,
1888 value, size, ctx->wim->blob_table))
1897 tstr_put_utf16le(uname);
1902 wimfs_symlink(const char *to, const char *from)
1904 struct fuse_context *fuse_ctx = fuse_get_context();
1905 struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
1906 struct wim_dentry *dentry;
1909 ret = create_file(fuse_ctx, from, S_IFLNK | 0777, 0,
1910 FILE_ATTRIBUTE_REPARSE_POINT, &dentry);
1913 dentry->d_inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
1914 ret = wim_inode_set_symlink(dentry->d_inode, to,
1915 wimfs_ctx->wim->blob_table);
1917 remove_dentry(dentry, wimfs_ctx->wim->blob_table);
1918 if (ret == WIMLIB_ERR_NOMEM)
1923 touch_parent(dentry);
1929 wimfs_truncate(const char *path, off_t size)
1931 const struct wimfs_context *ctx = wimfs_get_context();
1932 struct wim_dentry *dentry;
1933 struct wim_inode_stream *strm;
1934 struct blob_descriptor *blob;
1938 ret = wim_pathname_to_stream(ctx, path, 0, &dentry, &strm);
1942 blob = stream_blob_resolved(strm);
1947 if (!blob || blob->blob_location != BLOB_IN_STAGING_FILE) {
1948 return extract_blob_to_staging_dir(dentry->d_inode,
1952 /* Truncate the staging file. */
1953 fd = openat(blob->staging_dir_fd, blob->staging_file_name,
1954 O_WRONLY | O_NOFOLLOW);
1957 ret = ftruncate(fd, size);
1958 if (close(fd) || ret)
1961 touch_inode(dentry->d_inode);
1966 wimfs_unlink(const char *path)
1968 const struct wimfs_context *ctx = wimfs_get_context();
1969 struct wim_dentry *dentry;
1970 struct wim_inode_stream *strm;
1973 ret = wim_pathname_to_stream(ctx, path, 0, &dentry, &strm);
1977 if (stream_is_named(strm)) {
1978 inode_remove_stream(dentry->d_inode, strm,
1979 ctx->wim->blob_table);
1981 touch_parent(dentry);
1982 remove_dentry(dentry, ctx->wim->blob_table);
1987 #ifdef HAVE_UTIMENSAT
1989 * Change the timestamp on a file dentry.
1991 * Note that alternate data streams do not have their own timestamps.
1994 wimfs_utimens(const char *path, const struct timespec tv[2])
1996 WIMStruct *wim = wimfs_get_WIMStruct();
1997 struct wim_inode *inode;
1999 inode = wim_pathname_to_inode(wim, path);
2003 if (tv[0].tv_nsec != UTIME_OMIT) {
2004 if (tv[0].tv_nsec == UTIME_NOW)
2005 inode->i_last_access_time = now_as_wim_timestamp();
2007 inode->i_last_access_time = timespec_to_wim_timestamp(&tv[0]);
2009 if (tv[1].tv_nsec != UTIME_OMIT) {
2010 if (tv[1].tv_nsec == UTIME_NOW)
2011 inode->i_last_write_time = now_as_wim_timestamp();
2013 inode->i_last_write_time = timespec_to_wim_timestamp(&tv[1]);
2017 #else /* HAVE_UTIMENSAT */
2019 wimfs_utime(const char *path, struct utimbuf *times)
2021 WIMStruct *wim = wimfs_get_WIMStruct();
2022 struct wim_inode *inode;
2024 inode = wim_pathname_to_inode(wim, path);
2028 inode->i_last_access_time = time_t_to_wim_timestamp(times->actime);
2029 inode->i_last_write_time = time_t_to_wim_timestamp(times->modtime);
2032 #endif /* !HAVE_UTIMENSAT */
2035 wimfs_write(const char *path, const char *buf, size_t size,
2036 off_t offset, struct fuse_file_info *fi)
2038 struct wimfs_fd *fd = WIMFS_FD(fi);
2041 ret = raw_pwrite(&fd->f_staging_fd, buf, size, offset);
2045 if (offset + size > fd->f_blob->size)
2046 fd->f_blob->size = offset + size;
2048 touch_inode(fd->f_inode);
2052 static struct fuse_operations wimfs_operations = {
2053 .chmod = wimfs_chmod,
2054 .chown = wimfs_chown,
2055 .fgetattr = wimfs_fgetattr,
2056 .ftruncate = wimfs_ftruncate,
2057 .getattr = wimfs_getattr,
2058 .getxattr = wimfs_getxattr,
2060 .listxattr = wimfs_listxattr,
2061 .mkdir = wimfs_mkdir,
2062 .mknod = wimfs_mknod,
2064 .opendir = wimfs_opendir,
2066 .readdir = wimfs_readdir,
2067 .readlink = wimfs_readlink,
2068 .release = wimfs_release,
2069 .releasedir = wimfs_release,
2070 .removexattr = wimfs_removexattr,
2071 .rename = wimfs_rename,
2072 .rmdir = wimfs_rmdir,
2073 .setxattr = wimfs_setxattr,
2074 .symlink = wimfs_symlink,
2075 .truncate = wimfs_truncate,
2076 .unlink = wimfs_unlink,
2077 #ifdef HAVE_UTIMENSAT
2078 .utimens = wimfs_utimens,
2080 .utime = wimfs_utime,
2082 .write = wimfs_write,
2084 /* We keep track of file descriptor structures (struct wimfs_fd), so
2085 * there is no need to have the file path provided on operations such as
2087 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 8)
2088 .flag_nullpath_ok = 1,
2090 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 9)
2092 .flag_utime_omit_ok = 1,
2096 /* API function documented in wimlib.h */
2098 wimlib_mount_image(WIMStruct *wim, int image, const char *dir,
2099 int mount_flags, const char *staging_dir)
2102 struct wim_image_metadata *imd;
2103 struct wimfs_context ctx;
2104 char *fuse_argv[16];
2107 if (!wim || !dir || !*dir)
2108 return WIMLIB_ERR_INVALID_PARAM;
2110 if (mount_flags & ~(WIMLIB_MOUNT_FLAG_READWRITE |
2111 WIMLIB_MOUNT_FLAG_DEBUG |
2112 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
2113 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
2114 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS |
2115 WIMLIB_MOUNT_FLAG_UNIX_DATA |
2116 WIMLIB_MOUNT_FLAG_ALLOW_OTHER))
2117 return WIMLIB_ERR_INVALID_PARAM;
2119 /* For read-write mount, check for write access to the WIM. */
2120 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2122 return WIMLIB_ERR_NO_FILENAME;
2123 ret = can_modify_wim(wim);
2128 /* Select the image to mount. */
2129 ret = select_wim_image(wim, image);
2133 /* Get the metadata for the image to mount. */
2134 imd = wim_get_current_image_metadata(wim);
2136 if (imd->modified) {
2137 /* To avoid complicating things, we don't support mounting
2138 * images to which in-memory modifications have already been
2140 ERROR("Cannot mount a modified WIM image!");
2141 return WIMLIB_ERR_INVALID_PARAM;
2144 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2145 ret = lock_wim_for_append(wim);
2150 if (wim_has_solid_resources(wim)) {
2151 WARNING("Mounting a WIM file containing solid-compressed data; "
2152 "file access may be slow.");
2155 /* If the user did not specify an interface for accessing named
2156 * data streams, use the default (extended attributes). */
2157 if (!(mount_flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
2158 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
2159 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
2160 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
2162 /* Start initializing the wimfs_context. */
2163 memset(&ctx, 0, sizeof(struct wimfs_context));
2165 ctx.mount_flags = mount_flags;
2166 if (mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
2167 ctx.default_lookup_flags = LOOKUP_FLAG_ADS_OK;
2168 /* For read-write mount, create the staging directory. */
2169 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2170 ret = make_staging_dir(&ctx, staging_dir);
2174 ctx.owner_uid = getuid();
2175 ctx.owner_gid = getgid();
2177 /* Add each blob referenced by files in the image to a list and
2178 * preemptively double the number of references to each. This is done
2179 * to allow implementing the WIMLIB_UNMOUNT_FLAG_NEW_IMAGE semantics.
2181 INIT_LIST_HEAD(&ctx.orig_blob_list);
2182 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2184 struct wim_inode *inode;
2185 struct blob_descriptor *blob;
2187 image_for_each_inode(inode, imd) {
2188 for (i = 0; i < inode->i_num_streams; i++) {
2189 blob = stream_blob(&inode->i_streams[i],
2192 blob->out_refcnt = 0;
2196 image_for_each_inode(inode, imd) {
2197 for (i = 0; i < inode->i_num_streams; i++) {
2198 blob = stream_blob(&inode->i_streams[i],
2201 if (blob->out_refcnt == 0)
2202 list_add(&blob->orig_blob_list,
2203 &ctx.orig_blob_list);
2204 blob->out_refcnt += inode->i_nlink;
2205 blob->refcnt += inode->i_nlink;
2211 /* Number the inodes in the mounted image sequentially and initialize
2212 * the file descriptor arrays */
2213 prepare_inodes(&ctx);
2215 /* If a read-write mount, mark the image as modified. */
2216 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2219 /* Build the FUSE command line. */
2222 fuse_argv[fuse_argc++] = "wimlib";
2223 fuse_argv[fuse_argc++] = (char *)dir;
2225 /* Disable multi-threaded operation. */
2226 fuse_argv[fuse_argc++] = "-s";
2228 /* Enable FUSE debug mode (don't fork) if requested by the user. */
2229 if (mount_flags & WIMLIB_MOUNT_FLAG_DEBUG)
2230 fuse_argv[fuse_argc++] = "-d";
2233 * Build the FUSE mount options:
2236 * FUSE will use the inode numbers we provide. We want this,
2237 * because we have inodes and will number them ourselves.
2240 * Name for our filesystem (main type is "fuse").
2243 * If an open file is unlinked, unlink it for real rather than
2244 * renaming it to a hidden file. Our code supports this; an
2245 * unlinked inode is retained until all its file descriptors have
2248 * default_permissions
2249 * FUSE will perform permission checking. Useful when
2250 * WIMLIB_MOUNT_FLAG_UNIX_DATA is provided and the WIM image
2251 * contains the UNIX permissions for each file.
2254 * Cache the contents of files. This will speed up repeated access
2255 * to files on a mounted WIM image, since they won't need to be
2256 * decompressed repeatedly. This option is valid because data in
2257 * the WIM image should never be changed externally. (Although, if
2258 * someone really wanted to they could modify the WIM file or mess
2259 * with the staging directory; but then they're asking for
2262 * entry_timeout=1000000000
2263 * Cache positive name lookups indefinitely, since names can only
2264 * be added, removed, or modified through the mounted filesystem
2267 * negative_timeout=1000000000
2268 * Cache negative name lookups indefinitely, since names can only
2269 * be added, removed, or modified through the mounted filesystem
2273 * Don't cache file/directory attributes. This is needed as a
2274 * workaround for the fact that when caching attributes, the high
2275 * level interface to libfuse considers a file which has several
2276 * hard-linked names as several different files. (Otherwise, we
2277 * could cache our file/directory attributes indefinitely, since
2278 * they can only be changed through the mounted filesystem itself.)
2280 char optstring[256] =
2284 ",default_permissions"
2286 ",entry_timeout=1000000000"
2287 ",negative_timeout=1000000000"
2290 fuse_argv[fuse_argc++] = "-o";
2291 fuse_argv[fuse_argc++] = optstring;
2292 if (!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE))
2293 strcat(optstring, ",ro");
2294 if (mount_flags & WIMLIB_MOUNT_FLAG_ALLOW_OTHER)
2295 strcat(optstring, ",allow_other");
2296 fuse_argv[fuse_argc] = NULL;
2298 /* Mount our filesystem. */
2299 ret = fuse_main(fuse_argc, fuse_argv, &wimfs_operations, &ctx);
2301 /* Cleanup and return. */
2303 ret = WIMLIB_ERR_FUSE;
2304 release_extra_refcnts(&ctx);
2305 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2306 delete_staging_dir(&ctx);
2308 unlock_wim_for_append(wim);
2312 struct commit_progress_thread_args {
2314 wimlib_progress_func_t progfunc;
2319 commit_progress_thread_proc(void *_args)
2321 struct commit_progress_thread_args *args = _args;
2322 struct commit_progress_report report;
2326 ret = mq_receive(args->mq,
2327 (char *)&report, sizeof(report), NULL);
2328 if (ret == sizeof(report)) {
2329 call_progress(args->progfunc, report.msg,
2330 &report.info, args->progctx);
2332 if (ret == 0 || (ret < 0 && errno != EINTR))
2340 generate_message_queue_name(char name[WIMFS_MQUEUE_NAME_LEN + 1])
2343 memcpy(name + 1, "wimfs-", 6);
2344 randomize_char_array_with_alnum(name + 7, WIMFS_MQUEUE_NAME_LEN - 7);
2345 name[WIMFS_MQUEUE_NAME_LEN] = '\0';
2349 create_message_queue(const char *name)
2354 struct mq_attr attr;
2357 memset(&attr, 0, sizeof(attr));
2359 attr.mq_msgsize = sizeof(struct commit_progress_report);
2361 am_root = (geteuid() == 0);
2363 /* Filesystem mounted as normal user with --allow-other should
2364 * be able to send messages to root user, if they're doing the
2366 umask_save = umask(0);
2371 mq = mq_open(name, O_RDWR | O_CREAT | O_EXCL, mode, &attr);
2377 /* Unmount a read-only or read-write mounted WIM image. */
2379 do_unmount(const char *dir)
2384 len = getxattr(dir, "wimfs.unmount", &status, sizeof(int));
2385 if (len == sizeof(int))
2387 else if (len < 0 && (errno == EACCES || errno == EPERM))
2388 return WIMLIB_ERR_NOT_PERMITTED_TO_UNMOUNT;
2390 return WIMLIB_ERR_NOT_A_MOUNTPOINT;
2394 set_unmount_info(const char *dir, const struct wimfs_unmount_info *unmount_info)
2396 if (!setxattr(dir, "wimfs.unmount_info",
2397 unmount_info, sizeof(struct wimfs_unmount_info), 0))
2399 else if (errno == EROFS)
2401 else if (errno == EACCES || errno == EPERM)
2402 return WIMLIB_ERR_NOT_PERMITTED_TO_UNMOUNT;
2404 return WIMLIB_ERR_NOT_A_MOUNTPOINT;
2408 do_unmount_discard(const char *dir)
2411 struct wimfs_unmount_info unmount_info;
2413 memset(&unmount_info, 0, sizeof(unmount_info));
2415 ret = set_unmount_info(dir, &unmount_info);
2418 return do_unmount(dir);
2421 /* Unmount a read-write mounted WIM image, committing the changes. */
2423 do_unmount_commit(const char *dir, int unmount_flags,
2424 wimlib_progress_func_t progfunc, void *progctx)
2426 struct wimfs_unmount_info unmount_info;
2428 struct commit_progress_thread_args args;
2429 pthread_t commit_progress_tid;
2432 memset(&unmount_info, 0, sizeof(unmount_info));
2433 unmount_info.unmount_flags = unmount_flags;
2435 /* The current thread will be stuck in getxattr() until the image is
2436 * committed. Create a thread to handle the progress messages. */
2438 generate_message_queue_name(unmount_info.mq_name);
2440 mq = create_message_queue(unmount_info.mq_name);
2441 if (mq == (mqd_t)-1) {
2442 ERROR_WITH_ERRNO("Can't create POSIX message queue");
2443 return WIMLIB_ERR_MQUEUE;
2446 args.progfunc = progfunc;
2447 args.progctx = progctx;
2448 ret = pthread_create(&commit_progress_tid, NULL,
2449 commit_progress_thread_proc, &args);
2452 ERROR_WITH_ERRNO("Can't create thread");
2453 ret = WIMLIB_ERR_NOMEM;
2456 unmount_info.unmount_flags |= WIMLIB_UNMOUNT_FLAG_SEND_PROGRESS;
2459 ret = set_unmount_info(dir, &unmount_info);
2461 ret = do_unmount(dir);
2463 /* Terminate the progress thread. */
2465 mq_send(mq, empty, 0, 1);
2466 pthread_join(commit_progress_tid, NULL);
2471 mq_unlink(unmount_info.mq_name);
2477 begin_unmount(const char *dir, int unmount_flags, int *mount_flags_ret,
2478 wimlib_progress_func_t progfunc, void *progctx)
2482 int wim_filename_len;
2483 union wimlib_progress_info progress;
2485 if (getxattr(dir, "wimfs.mount_flags",
2486 &mount_flags, sizeof(int)) != sizeof(int))
2487 return WIMLIB_ERR_NOT_A_MOUNTPOINT;
2489 *mount_flags_ret = mount_flags;
2494 if (getxattr(dir, "wimfs.mounted_image",
2495 &mounted_image, sizeof(int)) != sizeof(int))
2496 return WIMLIB_ERR_NOT_A_MOUNTPOINT;
2498 wim_filename_len = getxattr(dir, "wimfs.wim_filename", NULL, 0);
2499 if (wim_filename_len < 0)
2500 return WIMLIB_ERR_NOT_A_MOUNTPOINT;
2502 char wim_filename[wim_filename_len + 1];
2503 if (getxattr(dir, "wimfs.wim_filename",
2504 wim_filename, wim_filename_len) != wim_filename_len)
2505 return WIMLIB_ERR_NOT_A_MOUNTPOINT;
2506 wim_filename[wim_filename_len] = '\0';
2508 progress.unmount.mountpoint = dir;
2509 progress.unmount.mounted_wim = wim_filename;
2510 progress.unmount.mounted_image = mounted_image;
2511 progress.unmount.mount_flags = mount_flags;
2512 progress.unmount.unmount_flags = unmount_flags;
2514 return call_progress(progfunc, WIMLIB_PROGRESS_MSG_UNMOUNT_BEGIN,
2515 &progress, progctx);
2518 /* API function documented in wimlib.h */
2520 wimlib_unmount_image_with_progress(const char *dir, int unmount_flags,
2521 wimlib_progress_func_t progfunc, void *progctx)
2526 ret = wimlib_global_init(WIMLIB_INIT_FLAG_ASSUME_UTF8);
2530 if (unmount_flags & ~(WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY |
2531 WIMLIB_UNMOUNT_FLAG_COMMIT |
2532 WIMLIB_UNMOUNT_FLAG_REBUILD |
2533 WIMLIB_UNMOUNT_FLAG_RECOMPRESS |
2534 WIMLIB_UNMOUNT_FLAG_FORCE |
2535 WIMLIB_UNMOUNT_FLAG_NEW_IMAGE))
2536 return WIMLIB_ERR_INVALID_PARAM;
2538 ret = begin_unmount(dir, unmount_flags, &mount_flags,
2543 if ((unmount_flags & WIMLIB_UNMOUNT_FLAG_COMMIT) &&
2544 (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE))
2545 return do_unmount_commit(dir, unmount_flags,
2548 return do_unmount_discard(dir);
2551 #else /* WITH_FUSE */
2555 mount_unsupported_error(void)
2557 #if defined(__WIN32__)
2558 ERROR("Sorry-- Mounting WIM images is not supported on Windows!");
2560 ERROR("wimlib was compiled with --without-fuse, which disables support "
2561 "for mounting WIMs.");
2563 return WIMLIB_ERR_UNSUPPORTED;
2567 wimlib_unmount_image_with_progress(const tchar *dir, int unmount_flags,
2568 wimlib_progress_func_t progfunc, void *progctx)
2570 return mount_unsupported_error();
2574 wimlib_mount_image(WIMStruct *wim, int image, const tchar *dir,
2575 int mount_flags, const tchar *staging_dir)
2577 return mount_unsupported_error();
2580 #endif /* !WITH_FUSE */
2583 wimlib_unmount_image(const tchar *dir, int unmount_flags)
2585 return wimlib_unmount_image_with_progress(dir, unmount_flags, NULL, NULL);