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 <sys/types.h> /* sometimes required before <attr/xattr.h> */
44 #include <attr/xattr.h>
58 #include "wimlib/blob_table.h"
59 #include "wimlib/dentry.h"
60 #include "wimlib/encoding.h"
61 #include "wimlib/metadata.h"
62 #include "wimlib/paths.h"
63 #include "wimlib/progress.h"
64 #include "wimlib/reparse.h"
65 #include "wimlib/timestamp.h"
66 #include "wimlib/unix_data.h"
67 #include "wimlib/write.h"
68 #include "wimlib/xml.h"
71 # define O_NOFOLLOW 0 /* Security only... */
74 #define WIMFS_MQUEUE_NAME_LEN 32
76 #define WIMLIB_UNMOUNT_FLAG_SEND_PROGRESS 0x80000000
78 struct wimfs_unmount_info {
79 unsigned unmount_flags;
80 char mq_name[WIMFS_MQUEUE_NAME_LEN + 1];
83 struct commit_progress_report {
84 enum wimlib_progress_msg msg;
85 union wimlib_progress_info info;
88 /* Description of an open file on a mounted WIM image. Actually, this
89 * represents the open state of a particular data stream of an inode, rather
90 * than the inode itself. (An inode might have multiple named data streams in
91 * addition to the default, unnamed data stream.) At a given time, an inode in
92 * the WIM image might have multiple file descriptors open to it, each to any
93 * one of its data streams. */
96 /* Pointer to the inode of this open file.
97 * 'i_num_opened_fds' of the inode tracks the number of file descriptors
98 * that reference it. */
99 struct wim_inode *f_inode;
101 /* Pointer to the blob descriptor for the data stream that has been
102 * opened. 'num_opened_fds' of the blob descriptor tracks the number of
103 * file descriptors that reference it. Or, this value may be NULL,
104 * which indicates that the opened stream is empty and consequently does
105 * not have a blob descriptor. */
106 struct blob_descriptor *f_blob;
108 /* If valid (filedes_valid(&f_staging_fd)), this contains the
109 * corresponding native file descriptor for the staging file that has
110 * been created for reading from and/or writing to this open stream. A
111 * single staging file might have multiple file descriptors open to it
112 * simultaneously, each used by a different 'struct wimfs_fd'.
114 * Or, if invalid (!filedes_valid(&f_staging_fd)), this 'struct
115 * wimfs_fd' is not associated with a staging file. This is permissible
116 * only if this 'struct wimfs_fd' was opened read-only and the stream
117 * has not yet been extracted to a staging file. */
118 struct filedes f_staging_fd;
120 /* 0-based index of this file descriptor in the file descriptor table of
124 /* Unique ID of the opened stream in the inode. This will stay the same
125 * even if the indices of the inode's streams are changed by a deletion.
130 #define WIMFS_FD(fi) ((struct wimfs_fd *)(uintptr_t)((fi)->fh))
132 /* Context structure for a mounted WIM image. */
133 struct wimfs_context {
134 /* The WIMStruct containing the mounted image. The mounted image is the
135 * currently selected image (wim->current_image). */
138 /* Flags passed to wimlib_mount_image() (WIMLIB_MOUNT_FLAG_*). */
141 /* Default flags for path lookup in the WIM image. */
142 int default_lookup_flags;
144 /* Information about the user who has mounted the WIM image */
148 /* Information about the staging directory for a read-write mount. */
151 char *staging_dir_name;
153 /* For read-write mounts, the inode number to be assigned to the next
154 * created file. Note: since this isn't a persistent filesystem and we
155 * can re-assign the inode numbers just before mounting the image, it's
156 * good enough to just generate inode numbers sequentially. */
159 /* Number of file descriptors open to the mounted WIM image. */
160 unsigned long num_open_fds;
162 /* Original list of blobs in the mounted image, linked by
163 * 'struct blob_descriptor'.orig_blob_list. */
164 struct list_head orig_blob_list;
166 /* Parameters for unmounting the image (can be set via extended
167 * attribute "wimfs.unmount_info"). */
168 struct wimfs_unmount_info unmount_info;
171 #define WIMFS_CTX(fuse_ctx) ((struct wimfs_context*)(fuse_ctx)->private_data)
173 /* Retrieve the context structure for the currently mounted WIM image.
175 * Note: this is a per-thread variable. It is possible for different threads to
176 * mount different images at the same time in the same process, although they
177 * must use different WIMStructs! */
178 static inline struct wimfs_context *
179 wimfs_get_context(void)
181 return WIMFS_CTX(fuse_get_context());
185 wimfs_inc_num_open_fds(void)
187 wimfs_get_context()->num_open_fds++;
191 wimfs_dec_num_open_fds(void)
193 wimfs_get_context()->num_open_fds--;
196 /* Retrieve the WIMStruct for the currently mounted WIM image. */
197 static inline WIMStruct *
198 wimfs_get_WIMStruct(void)
200 return wimfs_get_context()->wim;
203 /* Is write permission requested on the file? */
205 flags_writable(int open_flags)
207 int accmode = (open_flags & O_ACCMODE);
208 return (accmode == O_RDWR || accmode == O_WRONLY);
212 fuse_mask_mode(mode_t mode, const struct fuse_context *fuse_ctx)
214 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 8)
215 mode &= ~fuse_ctx->umask;
221 * Allocate a file descriptor to a data stream in the mounted WIM image.
224 * The inode containing the stream being opened
226 * The stream of the inode being opened
228 * On success, a pointer to the new file descriptor will be stored here.
230 * Returns 0 or a -errno code.
233 alloc_wimfs_fd(struct wim_inode *inode,
234 struct wim_inode_stream *strm,
235 struct wimfs_fd **fd_ret)
237 static const u16 min_fds_per_alloc = 8;
238 static const u16 max_fds = 0xffff;
242 if (inode->i_num_opened_fds == inode->i_num_allocated_fds) {
244 struct wimfs_fd **fds;
246 /* Expand this inode's file descriptor table. */
248 num_new_fds = max(min_fds_per_alloc,
249 inode->i_num_allocated_fds / 4);
251 num_new_fds = min(num_new_fds,
252 max_fds - inode->i_num_allocated_fds);
254 if (num_new_fds == 0)
257 fds = REALLOC(inode->i_fds,
258 (inode->i_num_allocated_fds + num_new_fds) *
263 memset(&fds[inode->i_num_allocated_fds], 0,
264 num_new_fds * sizeof(fds[0]));
266 inode->i_num_allocated_fds += num_new_fds;
267 inode->i_next_fd = inode->i_num_opened_fds;
270 /* Allocate the file descriptor in the first available space in the
271 * inode's file descriptor table.
273 * i_next_fd is the lower bound on the next open slot. */
274 for (i = inode->i_next_fd; inode->i_fds[i]; i++)
277 fd = MALLOC(sizeof(*fd));
282 fd->f_blob = stream_blob_resolved(strm);
283 filedes_invalidate(&fd->f_staging_fd);
285 fd->f_stream_id = strm->stream_id;
287 inode->i_fds[i] = fd;
288 inode->i_num_opened_fds++;
290 fd->f_blob->num_opened_fds++;
291 wimfs_inc_num_open_fds();
292 inode->i_next_fd = i + 1;
297 * Close a file descriptor to a data stream in the mounted WIM image.
299 * Returns 0 or a -errno code. The file descriptor is always closed.
302 close_wimfs_fd(struct wimfs_fd *fd)
305 struct wim_inode *inode;
307 /* Close the staging file if open. */
308 if (filedes_valid(&fd->f_staging_fd))
309 if (filedes_close(&fd->f_staging_fd))
312 /* Release this file descriptor from its blob descriptor. */
314 blob_decrement_num_opened_fds(fd->f_blob);
316 wimfs_dec_num_open_fds();
318 /* Release this file descriptor from its inode. */
320 inode->i_fds[fd->f_idx] = NULL;
321 if (fd->f_idx < inode->i_next_fd)
322 inode->i_next_fd = fd->f_idx;
324 inode_dec_num_opened_fds(inode);
329 * Translate a path into the corresponding inode in the mounted WIM image.
331 * See get_dentry() for more information.
333 * Returns a pointer to the resulting inode, or NULL with errno set.
335 static struct wim_inode *
336 wim_pathname_to_inode(WIMStruct *wim, const char *path)
338 struct wim_dentry *dentry;
340 dentry = get_dentry(wim, path, WIMLIB_CASE_SENSITIVE);
343 return dentry->d_inode;
346 /* Can look up named data stream with colon syntax */
347 #define LOOKUP_FLAG_ADS_OK 0x01
349 /* Can look up directory (otherwise get -ENOTDIR) */
350 #define LOOKUP_FLAG_DIRECTORY_OK 0x02
352 /* Get the data stream of the specified name from the specified inode. Returns
353 * NULL with errno set if not found. */
354 static struct wim_inode_stream *
355 inode_get_data_stream_tstr(const struct wim_inode *inode,
356 const char *stream_name)
358 struct wim_inode_stream *strm;
360 if (!stream_name || !*stream_name) {
361 strm = inode_get_unnamed_data_stream(inode);
363 const utf16lechar *uname;
365 if (tstr_get_utf16le(stream_name, &uname))
367 strm = inode_get_stream(inode, STREAM_TYPE_DATA, uname);
368 tstr_put_utf16le(uname);
376 * Translate a path into the corresponding dentry and stream in the mounted WIM
379 * Returns 0 or a -errno code. @dentry_ret and @strm_ret are both optional.
382 wim_pathname_to_stream(const struct wimfs_context *ctx,
385 struct wim_dentry **dentry_ret,
386 struct wim_inode_stream **strm_ret)
388 WIMStruct *wim = ctx->wim;
389 struct wim_dentry *dentry;
390 struct wim_inode *inode;
391 struct wim_inode_stream *strm;
392 const char *stream_name = NULL;
395 lookup_flags |= ctx->default_lookup_flags;
397 if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
398 stream_name = path_stream_name(path);
400 p = (char *)stream_name - 1;
405 dentry = get_dentry(wim, path, WIMLIB_CASE_SENSITIVE);
411 inode = dentry->d_inode;
413 if (inode_resolve_streams(inode, wim->blob_table, false))
416 if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
417 && inode_is_directory(inode))
420 strm = inode_get_data_stream_tstr(inode, stream_name);
422 /* Force creation of an unnamed data stream */
424 strm = inode_add_stream(inode, STREAM_TYPE_DATA,
425 NO_STREAM_NAME, NULL);
431 *dentry_ret = dentry;
438 * Create a new file in the mounted WIM image.
441 * The FUSE context for the mounted image.
443 * The path at which to create the first link to the new file. If a file
444 * already exists at this path, -EEXIST is returned.
446 * The UNIX mode for the new file. This is only honored if
447 * WIMLIB_MOUNT_FLAG_UNIX_DATA was passed to wimlib_mount_image().
449 * The device ID for the new file, encoding the major and minor device
450 * numbers. This is only honored if WIMLIB_MOUNT_FLAG_UNIX_DATA was passed
451 * to wimlib_mount_image().
453 * Windows file attributes to use for the new file.
455 * On success, a pointer to the new dentry is returned here. Its d_inode
456 * member will point to the new inode that was created for it and added to
457 * the mounted WIM image.
459 * Returns 0 or a -errno code.
462 create_file(struct fuse_context *fuse_ctx, const char *path,
463 mode_t mode, dev_t rdev, u32 attributes,
464 struct wim_dentry **dentry_ret)
466 struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
467 struct wim_dentry *parent;
468 const char *basename;
469 struct wim_dentry *new_dentry;
470 struct wim_inode *new_inode;
472 parent = get_parent_dentry(wimfs_ctx->wim, path, WIMLIB_CASE_SENSITIVE);
476 if (!dentry_is_directory(parent))
479 basename = path_basename(path);
481 if (get_dentry_child_with_name(parent, basename, WIMLIB_CASE_SENSITIVE))
484 if (new_dentry_with_new_inode(basename, true, &new_dentry))
487 new_inode = new_dentry->d_inode;
489 new_inode->i_ino = wimfs_ctx->next_ino++;
490 new_inode->i_attributes = attributes;
492 if (wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA) {
493 struct wimlib_unix_data unix_data;
495 unix_data.uid = fuse_ctx->uid;
496 unix_data.gid = fuse_ctx->gid;
497 unix_data.mode = fuse_mask_mode(mode, fuse_ctx);
498 unix_data.rdev = rdev;
499 if (!inode_set_unix_data(new_inode, &unix_data, UNIX_DATA_ALL))
501 free_dentry(new_dentry);
506 hlist_add_head(&new_inode->i_hlist,
507 &wim_get_current_image_metadata(wimfs_ctx->wim)->inode_list);
509 dentry_add_child(parent, new_dentry);
511 *dentry_ret = new_dentry;
516 * Remove a dentry from the mounted WIM image; i.e. remove an alias for an
520 remove_dentry(struct wim_dentry *dentry, struct blob_table *blob_table)
522 /* Drop blob references. */
523 inode_unref_blobs(dentry->d_inode, blob_table);
525 /* Unlink the dentry from the image's dentry tree. */
526 unlink_dentry(dentry);
528 /* Delete the dentry. This will also decrement the link count of the
529 * corresponding inode, and possibly cause it to be deleted as well. */
533 /* Generate UNIX filetype mode bits for the specified WIM inode, based on its
534 * Windows file attributes. */
536 inode_unix_file_type(const struct wim_inode *inode)
538 if (inode_is_symlink(inode))
540 else if (inode_is_directory(inode))
546 /* Generate a default UNIX mode for the specified WIM inode. */
548 inode_default_unix_mode(const struct wim_inode *inode)
550 return inode_unix_file_type(inode) | 0777;
554 blob_size(const struct blob_descriptor *blob)
562 blob_stored_size(const struct blob_descriptor *blob)
566 if (blob->blob_location == BLOB_IN_WIM &&
567 blob->size == blob->rdesc->uncompressed_size)
568 return blob->rdesc->size_in_wim;
573 * Retrieve standard UNIX metadata ('struct stat') for a WIM inode.
575 * @blob is the blob descriptor for the stream of the inode that is being
576 * queried, or NULL. We mostly return the same information for all streams, but
577 * st_size and st_blocks may be different for different streams.
579 * This always returns 0.
582 inode_to_stbuf(const struct wim_inode *inode,
583 const struct blob_descriptor *blob, struct stat *stbuf)
585 const struct wimfs_context *ctx = wimfs_get_context();
586 struct wimlib_unix_data unix_data;
588 memset(stbuf, 0, sizeof(struct stat));
589 if ((ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA) &&
590 inode_get_unix_data(inode, &unix_data))
592 /* Use the user ID, group ID, mode, and device ID from the
593 * inode's extra UNIX metadata information. */
594 stbuf->st_uid = unix_data.uid;
595 stbuf->st_gid = unix_data.gid;
596 stbuf->st_mode = unix_data.mode;
597 stbuf->st_rdev = unix_data.rdev;
599 /* Generate default values for the user ID, group ID, and mode.
601 * Note: in the case of an allow_other mount, fuse_context.uid
602 * may not be the same as wimfs_context.owner_uid! */
603 stbuf->st_uid = ctx->owner_uid;
604 stbuf->st_gid = ctx->owner_gid;
605 stbuf->st_mode = inode_default_unix_mode(inode);
607 stbuf->st_ino = inode->i_ino;
608 stbuf->st_nlink = inode->i_nlink;
609 stbuf->st_size = blob_size(blob);
610 #ifdef HAVE_STAT_NANOSECOND_PRECISION
611 stbuf->st_atim = wim_timestamp_to_timespec(inode->i_last_access_time);
612 stbuf->st_mtim = wim_timestamp_to_timespec(inode->i_last_write_time);
613 stbuf->st_ctim = stbuf->st_mtim;
615 stbuf->st_atime = wim_timestamp_to_time_t(inode->i_last_access_time);
616 stbuf->st_mtime = wim_timestamp_to_time_t(inode->i_last_write_time);
617 stbuf->st_ctime = stbuf->st_mtime;
619 stbuf->st_blocks = DIV_ROUND_UP(blob_stored_size(blob), 512);
623 /* Update the last access and last write timestamps of a WIM inode. */
625 touch_inode(struct wim_inode *inode)
627 u64 now = now_as_wim_timestamp();
628 inode->i_last_access_time = now;
629 inode->i_last_write_time = now;
633 touch_parent(struct wim_dentry *dentry)
635 touch_inode(dentry->d_parent->d_inode);
639 * Create a new file in the staging directory for a read-write mounted image.
641 * On success, returns the file descriptor for the new staging file, opened for
642 * writing. In addition, stores the allocated name of the staging file in
645 * On failure, returns -1 and sets errno.
648 create_staging_file(const struct wimfs_context *ctx, char **name_ret)
651 static const size_t STAGING_FILE_NAME_LEN = 20;
655 name = MALLOC(STAGING_FILE_NAME_LEN + 1);
658 name[STAGING_FILE_NAME_LEN] = '\0';
661 randomize_char_array_with_alnum(name, STAGING_FILE_NAME_LEN);
662 fd = openat(ctx->staging_dir_fd, name,
663 O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, 0600);
664 if (unlikely(fd < 0)) {
665 if (unlikely(errno == EEXIST))
666 /* Try again with another name. */
676 * Extract a blob to the staging directory. This is necessary when a stream
677 * using the blob is being opened for writing and the blob has not already been
678 * extracted to the staging directory.
681 * The inode containing the stream being opened for writing.
683 * The stream being opened for writing. The blob descriptor to which the
684 * stream refers will be changed by this function.
686 * Number of bytes of the blob to extract and include in the staging file.
687 * It may be less than the actual blob length, in which case only a prefix
688 * of the blob will be extracted. It may also be more than the actual blob
689 * length, in which case the extra space will be zero-filled.
691 * Returns 0 or a -errno code.
694 extract_blob_to_staging_dir(struct wim_inode *inode,
695 struct wim_inode_stream *strm,
696 off_t size, const struct wimfs_context *ctx)
698 struct blob_descriptor *old_blob;
699 struct blob_descriptor *new_blob;
700 char *staging_file_name;
706 old_blob = stream_blob_resolved(strm);
708 /* Create the staging file. */
709 staging_fd = create_staging_file(ctx, &staging_file_name);
710 if (unlikely(staging_fd < 0))
713 /* Extract the stream to the staging file (possibly truncated). */
717 filedes_init(&fd, staging_fd);
719 extract_size = min(old_blob->size, size);
720 result = extract_blob_to_fd(old_blob, &fd, extract_size);
726 /* In the case of truncate() to more than the file length, extend the
727 * staging file with zeroes by calling ftruncate(). */
728 if (!result && size > extract_size)
729 result = ftruncate(staging_fd, size);
731 /* Close the staging file. */
732 if (close(staging_fd))
735 /* If an error occurred, unlink the staging file. */
736 if (unlikely(result)) {
737 /* extract_blob_to_fd() should set errno, but if it didn't,
738 * set a default value. */
739 ret = errno ? -errno : -EIO;
740 goto out_delete_staging_file;
743 /* Create a blob descriptor for the staging file. */
744 new_blob = new_blob_descriptor();
745 if (unlikely(!new_blob)) {
747 goto out_delete_staging_file;
750 /* There may already be open file descriptors to this stream if it's
751 * previously been opened read-only, but just now we're opening it
752 * read-write. Identify those file descriptors, update them to use the
753 * new blob descriptor, and open staging file descriptors for them. */
754 for (u16 i = 0, j = 0; j < inode->i_num_opened_fds; i++) {
758 fd = inode->i_fds[i];
764 if (fd->f_stream_id != strm->stream_id)
767 /* This is a readonly fd for the same stream. */
768 fd->f_blob = new_blob;
769 new_blob->num_opened_fds++;
770 raw_fd = openat(ctx->staging_dir_fd, staging_file_name,
771 O_RDONLY | O_NOFOLLOW);
772 if (unlikely(raw_fd < 0)) {
774 goto out_revert_fd_changes;
776 filedes_init(&fd->f_staging_fd, raw_fd);
780 old_blob->num_opened_fds -= new_blob->num_opened_fds;
782 new_blob->blob_location = BLOB_IN_STAGING_FILE;
783 new_blob->staging_file_name = staging_file_name;
784 new_blob->staging_dir_fd = ctx->staging_dir_fd;
785 new_blob->size = size;
787 prepare_unhashed_blob(new_blob, inode, strm->stream_id,
788 &wim_get_current_image_metadata(ctx->wim)->unhashed_blobs);
789 inode_replace_stream_blob(inode, strm, new_blob, ctx->wim->blob_table);
792 out_revert_fd_changes:
793 for (u16 i = 0; new_blob->num_opened_fds; i++) {
794 struct wimfs_fd *fd = inode->i_fds[i];
795 if (fd && fd->f_stream_id == strm->stream_id) {
796 fd->f_blob = old_blob;
797 if (filedes_valid(&fd->f_staging_fd)) {
798 filedes_close(&fd->f_staging_fd);
799 filedes_invalidate(&fd->f_staging_fd);
801 new_blob->num_opened_fds--;
804 free_blob_descriptor(new_blob);
805 out_delete_staging_file:
806 unlinkat(ctx->staging_dir_fd, staging_file_name, 0);
807 FREE(staging_file_name);
812 * Create the staging directory for the WIM file.
814 * The staging directory will be created in the directory specified by the open
815 * file descriptor @parent_dir_fd. It will be given a randomly generated name
816 * based on @wim_basename, the name of the WIM file.
818 * On success, returns a file descriptor to the open staging directory with
819 * O_RDONLY access. In addition, stores the allocated name of the staging
820 * directory (relative to @parent_dir_fd) in @staging_dir_name_ret.
821 * On failure, returns -1 and sets errno.
824 make_staging_dir_at(int parent_dir_fd, const char *wim_basename,
825 char **staging_dir_name_ret)
827 static const char common_suffix[8] = ".staging";
828 static const size_t random_suffix_len = 10;
829 size_t wim_basename_len;
830 size_t staging_dir_name_len;
831 char *staging_dir_name;
835 wim_basename_len = strlen(wim_basename);
836 staging_dir_name_len = wim_basename_len + sizeof(common_suffix) +
838 staging_dir_name = MALLOC(staging_dir_name_len + 1);
839 if (!staging_dir_name)
842 p = staging_dir_name;
843 p = mempcpy(p, wim_basename, wim_basename_len);
844 p = mempcpy(p, common_suffix, sizeof(common_suffix));
845 randomize_char_array_with_alnum(p, random_suffix_len);
846 p += random_suffix_len;
849 if (mkdirat(parent_dir_fd, staging_dir_name, 0700))
852 fd = openat(parent_dir_fd, staging_dir_name,
853 O_RDONLY | O_DIRECTORY | O_NOFOLLOW);
857 *staging_dir_name_ret = staging_dir_name;
861 unlinkat(parent_dir_fd, staging_dir_name, AT_REMOVEDIR);
863 FREE(staging_dir_name);
868 * Create the staging directory and set ctx->staging_dir_fd,
869 * ctx->staging_dir_name, and ctx->parent_dir_fd.
872 make_staging_dir(struct wimfs_context *ctx, const char *parent_dir_path)
874 const char *wim_basename;
878 wim_basename = path_basename(ctx->wim->filename);
880 if (!parent_dir_path) {
881 /* The user did not specify a directory. Default to creating
882 * the staging directory alongside the WIM file. */
883 if (wim_basename > ctx->wim->filename) {
884 parent_dir_path = ctx->wim->filename;
885 end = (char *)(wim_basename - 1);
886 /* *end must be a slash. Temporarily overwrite it so we
887 * can open the parent directory. */
890 parent_dir_path = ".";
894 /* Open the parent directory (in which we'll create our staging
896 ctx->parent_dir_fd = open(parent_dir_path, O_RDONLY | O_DIRECTORY);
897 if (ctx->parent_dir_fd < 0) {
898 ERROR_WITH_ERRNO("Can't open directory \"%s\"",
900 ret = WIMLIB_ERR_OPENDIR;
901 goto out_restore_wim_filename;
904 ctx->staging_dir_fd = make_staging_dir_at(ctx->parent_dir_fd,
906 &ctx->staging_dir_name);
907 if (ctx->staging_dir_fd < 0) {
908 ERROR_WITH_ERRNO("Can't create staging directory in \"%s\"",
910 close(ctx->parent_dir_fd);
911 ret = WIMLIB_ERR_MKDIR;
912 goto out_restore_wim_filename;
915 out_restore_wim_filename:
921 /* Deletes the staging directory, undoing the effects of a successful call to
922 * make_staging_dir(). */
924 delete_staging_dir(struct wimfs_context *ctx)
929 dir = fdopendir(ctx->staging_dir_fd);
931 while ((ent = readdir(dir)))
932 unlinkat(ctx->staging_dir_fd, ent->d_name, 0);
935 close(ctx->staging_dir_fd);
937 if (unlinkat(ctx->parent_dir_fd, ctx->staging_dir_name, AT_REMOVEDIR))
938 WARNING_WITH_ERRNO("Could not delete staging directory");
939 FREE(ctx->staging_dir_name);
940 close(ctx->parent_dir_fd);
944 prepare_inodes(struct wimfs_context *ctx)
946 struct wim_image_metadata *imd;
947 struct wim_inode *inode;
950 imd = wim_get_current_image_metadata(ctx->wim);
951 image_for_each_inode(inode, imd) {
952 inode->i_ino = ctx->next_ino++;
953 inode->i_num_opened_fds = 0;
954 inode->i_num_allocated_fds = 0;
960 release_extra_refcnts(struct wimfs_context *ctx)
962 struct list_head *list = &ctx->orig_blob_list;
963 struct blob_table *blob_table = ctx->wim->blob_table;
964 struct blob_descriptor *blob, *tmp;
966 list_for_each_entry_safe(blob, tmp, list, orig_blob_list)
967 blob_subtract_refcnt(blob, blob_table, blob->out_refcnt);
970 /* Delete the 'struct blob_descriptor' for any stream that was modified
971 * or created in the read-write mounted image and had a final size of 0. */
973 delete_empty_blobs(struct wimfs_context *ctx)
975 struct blob_descriptor *blob, *tmp;
976 struct wim_image_metadata *imd;
978 imd = wim_get_current_image_metadata(ctx->wim);
980 image_for_each_unhashed_blob_safe(blob, tmp, imd) {
982 *retrieve_pointer_to_unhashed_blob(blob) = NULL;
983 list_del(&blob->unhashed_list);
984 free_blob_descriptor(blob);
989 /* Close all file descriptors open to the specified inode.
991 * Note: closing the last file descriptor might free the inode. */
993 inode_close_fds(struct wim_inode *inode)
995 u16 num_open_fds = inode->i_num_opened_fds;
996 for (u16 i = 0; num_open_fds; i++) {
997 if (inode->i_fds[i]) {
998 close_wimfs_fd(inode->i_fds[i]);
1004 /* Close all file descriptors open to the mounted image. */
1006 close_all_fds(struct wimfs_context *ctx)
1008 struct wim_inode *inode;
1009 struct hlist_node *tmp;
1010 struct wim_image_metadata *imd;
1012 imd = wim_get_current_image_metadata(ctx->wim);
1014 image_for_each_inode_safe(inode, tmp, imd)
1015 inode_close_fds(inode);
1018 /* Moves the currently selected image, which may have been modified, to a new
1019 * index, and sets the original index to refer to a reset (unmodified) copy of
1022 renew_current_image(struct wimfs_context *ctx)
1024 WIMStruct *wim = ctx->wim;
1025 int idx = wim->current_image - 1;
1026 struct wim_image_metadata *imd = wim->image_metadata[idx];
1027 struct wim_image_metadata *replace_imd;
1028 struct blob_descriptor *new_blob;
1031 /* Create 'replace_imd' structure to use for the reset original,
1032 * unmodified image. */
1033 ret = WIMLIB_ERR_NOMEM;
1034 replace_imd = new_image_metadata();
1038 /* Create new blob descriptor for the modified image's metadata
1039 * resource, which doesn't exist yet. */
1040 ret = WIMLIB_ERR_NOMEM;
1041 new_blob = new_blob_descriptor();
1043 goto err_put_replace_imd;
1045 new_blob->refcnt = 1;
1046 new_blob->unhashed = 1;
1047 new_blob->is_metadata = 1;
1049 /* Make the image being moved available at a new index. Increments the
1050 * WIM's image count, but does not increment the reference count of the
1051 * 'struct image_metadata'. */
1052 ret = append_image_metadata(wim, imd);
1054 goto err_free_new_blob;
1056 ret = xml_add_image(wim, "");
1058 goto err_undo_append;
1060 replace_imd->metadata_blob = imd->metadata_blob;
1061 imd->metadata_blob = new_blob;
1062 wim->image_metadata[idx] = replace_imd;
1063 wim->current_image = wim->hdr.image_count;
1067 wim->hdr.image_count--;
1069 free_blob_descriptor(new_blob);
1070 err_put_replace_imd:
1071 put_image_metadata(replace_imd, NULL);
1076 static enum wimlib_progress_status
1077 commit_progress_func(enum wimlib_progress_msg msg,
1078 union wimlib_progress_info *info, void *progctx)
1080 mqd_t mq = *(mqd_t *)progctx;
1081 struct commit_progress_report report;
1083 memset(&report, 0, sizeof(report));
1086 report.info = *info;
1087 mq_send(mq, (const char *)&report, sizeof(report), 1);
1088 return WIMLIB_PROGRESS_STATUS_CONTINUE;
1091 /* Commit the mounted image to the underlying WIM file. */
1093 commit_image(struct wimfs_context *ctx, int unmount_flags, mqd_t mq)
1097 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_SEND_PROGRESS)
1098 wimlib_register_progress_function(ctx->wim,
1099 commit_progress_func, &mq);
1101 wimlib_register_progress_function(ctx->wim, NULL, NULL);
1103 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_NEW_IMAGE) {
1104 int ret = renew_current_image(ctx);
1108 release_extra_refcnts(ctx);
1110 INIT_LIST_HEAD(&ctx->orig_blob_list);
1111 delete_empty_blobs(ctx);
1112 xml_update_image_info(ctx->wim, ctx->wim->current_image);
1116 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY)
1117 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1119 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_REBUILD)
1120 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1122 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_RECOMPRESS)
1123 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
1125 return wimlib_overwrite(ctx->wim, write_flags, 0);
1128 /* In the case of an allow_other mount, only the mount owner and root are
1129 * allowed to unmount the filesystem. */
1131 may_unmount_wimfs(void)
1133 const struct fuse_context *fuse_ctx = fuse_get_context();
1134 const struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
1136 return (fuse_ctx->uid == wimfs_ctx->owner_uid ||
1137 fuse_ctx->uid == 0);
1140 /* Unmount the mounted image, called from the daemon process. */
1144 struct fuse_context *fuse_ctx = fuse_get_context();
1145 struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
1146 const struct wimfs_unmount_info *info = &wimfs_ctx->unmount_info;
1147 int unmount_flags = info->unmount_flags;
1148 mqd_t mq = (mqd_t)-1;
1151 /* Ignore COMMIT if the image is mounted read-only. */
1152 if (!(wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE))
1153 unmount_flags &= ~WIMLIB_UNMOUNT_FLAG_COMMIT;
1155 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_SEND_PROGRESS) {
1156 mq = mq_open(info->mq_name, O_WRONLY | O_NONBLOCK);
1157 if (mq == (mqd_t)-1) {
1158 ret = WIMLIB_ERR_MQUEUE;
1163 if (wimfs_ctx->num_open_fds) {
1165 /* There are still open file descriptors to the image. */
1167 /* With COMMIT, refuse to unmount unless FORCE is also
1169 if ((unmount_flags & (WIMLIB_UNMOUNT_FLAG_COMMIT |
1170 WIMLIB_UNMOUNT_FLAG_FORCE))
1171 == WIMLIB_UNMOUNT_FLAG_COMMIT)
1173 ret = WIMLIB_ERR_MOUNTED_IMAGE_IS_BUSY;
1177 /* Force-close all file descriptors. */
1178 close_all_fds(wimfs_ctx);
1181 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_COMMIT)
1182 ret = commit_image(wimfs_ctx, unmount_flags, mq);
1184 ret = 0; /* Read-only mount, or discarding changes to
1185 a read-write mount */
1188 /* Leave the image mounted if commit failed, unless this is a
1189 * forced unmount. The user can retry without COMMIT if they
1191 if (!ret || (unmount_flags & WIMLIB_UNMOUNT_FLAG_FORCE)) {
1192 unlock_wim_for_append(wimfs_ctx->wim);
1193 fuse_exit(fuse_ctx->fuse);
1195 if (mq != (mqd_t)-1)
1201 wimfs_chmod(const char *path, mode_t mask)
1203 const struct wimfs_context *ctx = wimfs_get_context();
1204 struct wim_inode *inode;
1205 struct wimlib_unix_data unix_data;
1207 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA))
1210 inode = wim_pathname_to_inode(ctx->wim, path);
1214 unix_data.uid = ctx->owner_uid;
1215 unix_data.gid = ctx->owner_gid;
1216 unix_data.mode = mask;
1219 if (!inode_set_unix_data(inode, &unix_data, UNIX_DATA_MODE))
1226 wimfs_chown(const char *path, uid_t uid, gid_t gid)
1228 const struct wimfs_context *ctx = wimfs_get_context();
1229 struct wim_inode *inode;
1230 struct wimlib_unix_data unix_data;
1233 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA))
1236 inode = wim_pathname_to_inode(ctx->wim, path);
1242 if (uid != (uid_t)-1)
1243 which |= UNIX_DATA_UID;
1245 uid = ctx->owner_uid;
1247 if (gid != (gid_t)-1)
1248 which |= UNIX_DATA_GID;
1250 gid = ctx->owner_gid;
1252 unix_data.uid = uid;
1253 unix_data.gid = gid;
1254 unix_data.mode = inode_default_unix_mode(inode);
1257 if (!inode_set_unix_data(inode, &unix_data, which))
1264 wimfs_fgetattr(const char *path, struct stat *stbuf, struct fuse_file_info *fi)
1266 struct wimfs_fd *fd = WIMFS_FD(fi);
1267 return inode_to_stbuf(fd->f_inode, fd->f_blob, stbuf);
1271 wimfs_ftruncate(const char *path, off_t size, struct fuse_file_info *fi)
1273 struct wimfs_fd *fd = WIMFS_FD(fi);
1274 if (ftruncate(fd->f_staging_fd.fd, size))
1276 touch_inode(fd->f_inode);
1277 fd->f_blob->size = size;
1282 wimfs_getattr(const char *path, struct stat *stbuf)
1284 const struct wimfs_context *ctx = wimfs_get_context();
1285 struct wim_dentry *dentry;
1286 struct wim_inode_stream *strm;
1289 ret = wim_pathname_to_stream(ctx, path, LOOKUP_FLAG_DIRECTORY_OK,
1294 return inode_to_stbuf(dentry->d_inode,
1295 stream_blob_resolved(strm), stbuf);
1299 copy_xattr(char *dest, size_t destsize, const void *src, size_t srcsize)
1302 if (destsize < srcsize)
1304 memcpy(dest, src, srcsize);
1310 wimfs_getxattr(const char *path, const char *name, char *value,
1313 const struct wimfs_context *ctx = wimfs_get_context();
1314 struct wim_inode *inode;
1315 struct wim_inode_stream *strm;
1316 struct blob_descriptor *blob;
1318 if (!strncmp(name, "wimfs.", 6)) {
1319 /* Handle some magical extended attributes. These really should
1320 * be ioctls, but directory ioctls aren't supported until
1321 * libfuse 2.9, and even then they are broken. */
1323 if (!strcmp(name, "wim_filename")) {
1324 return copy_xattr(value, size, ctx->wim->filename,
1325 strlen(ctx->wim->filename));
1327 if (!strcmp(name, "wim_info")) {
1328 struct wimlib_wim_info info;
1330 wimlib_get_wim_info(ctx->wim, &info);
1332 return copy_xattr(value, size, &info, sizeof(info));
1334 if (!strcmp(name, "mounted_image")) {
1335 return copy_xattr(value, size,
1336 &ctx->wim->current_image, sizeof(int));
1338 if (!strcmp(name, "mount_flags")) {
1339 return copy_xattr(value, size,
1340 &ctx->mount_flags, sizeof(int));
1342 if (!strcmp(name, "unmount")) {
1343 if (!may_unmount_wimfs())
1348 if (size < sizeof(int))
1350 status = unmount_wimfs();
1351 memcpy(value, &status, sizeof(int));
1358 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1361 if (strncmp(name, "user.", 5))
1368 /* Querying a named data stream */
1370 inode = wim_pathname_to_inode(ctx->wim, path);
1374 strm = inode_get_data_stream_tstr(inode, name);
1376 return (errno == ENOENT) ? -ENOATTR : -errno;
1378 blob = stream_blob_resolved(strm);
1382 if (unlikely(blob->size > INT_MAX))
1386 if (size < blob->size)
1389 if (read_full_blob_into_buf(blob, value))
1390 return errno ? -errno : -EIO;
1396 wimfs_link(const char *existing_path, const char *new_path)
1398 WIMStruct *wim = wimfs_get_WIMStruct();
1399 const char *new_name;
1400 struct wim_inode *inode;
1401 struct wim_dentry *dir;
1402 struct wim_dentry *new_alias;
1404 inode = wim_pathname_to_inode(wim, existing_path);
1408 if (inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
1409 FILE_ATTRIBUTE_REPARSE_POINT))
1412 new_name = path_basename(new_path);
1414 dir = get_parent_dentry(wim, new_path, WIMLIB_CASE_SENSITIVE);
1418 if (!dentry_is_directory(dir))
1421 if (get_dentry_child_with_name(dir, new_name, WIMLIB_CASE_SENSITIVE))
1424 if (new_dentry_with_existing_inode(new_name, inode, &new_alias))
1427 dentry_add_child(dir, new_alias);
1428 touch_inode(dir->d_inode);
1433 wimfs_listxattr(const char *path, char *list, size_t size)
1435 const struct wimfs_context *ctx = wimfs_get_context();
1436 const struct wim_inode *inode;
1438 char *end = list + size;
1441 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1444 /* List named data streams, or get the list size. We report each named
1445 * data stream "X" as an extended attribute "user.X". */
1447 inode = wim_pathname_to_inode(ctx->wim, path);
1451 for (unsigned i = 0; i < inode->i_num_streams; i++) {
1452 const struct wim_inode_stream *strm;
1453 char *stream_name_mbs;
1454 size_t stream_name_mbs_nbytes;
1456 strm = &inode->i_streams[i];
1458 if (!stream_is_named_data_stream(strm))
1461 if (utf16le_to_tstr(strm->stream_name,
1462 utf16le_len_bytes(strm->stream_name),
1464 &stream_name_mbs_nbytes))
1467 if (unlikely(INT_MAX - total_size < stream_name_mbs_nbytes + 6)) {
1468 FREE(stream_name_mbs);
1472 total_size += stream_name_mbs_nbytes + 6;
1474 if (end - p < stream_name_mbs_nbytes + 6) {
1475 FREE(stream_name_mbs);
1478 p = mempcpy(p, "user.", 5);
1479 p = mempcpy(p, stream_name_mbs, stream_name_mbs_nbytes);
1482 FREE(stream_name_mbs);
1488 wimfs_mkdir(const char *path, mode_t mode)
1490 struct wim_dentry *dentry;
1493 /* Note: according to fuse.h, mode may not include S_IFDIR */
1494 ret = create_file(fuse_get_context(), path, mode | S_IFDIR, 0,
1495 FILE_ATTRIBUTE_DIRECTORY, &dentry);
1498 touch_parent(dentry);
1503 wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
1505 struct fuse_context *fuse_ctx = fuse_get_context();
1506 struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
1507 const char *stream_name;
1509 if ((wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
1510 && (stream_name = path_stream_name(path)))
1512 struct wim_inode *inode;
1513 struct wim_inode_stream *existing_strm;
1514 struct wim_inode_stream *new_strm;
1516 const utf16lechar *uname;
1518 /* Create a named data stream. */
1523 p = (char *)stream_name - 1;
1526 inode = wim_pathname_to_inode(wimfs_ctx->wim, path);
1531 if (tstr_get_utf16le(stream_name, &uname))
1534 existing_strm = inode_get_stream(inode, STREAM_TYPE_DATA, uname);
1535 if (existing_strm) {
1536 tstr_put_utf16le(uname);
1540 new_strm = inode_add_stream(inode, STREAM_TYPE_DATA, uname, NULL);
1542 tstr_put_utf16le(uname);
1548 /* Create a regular file, device node, named pipe, or socket.
1550 struct wim_dentry *dentry;
1553 if (!S_ISREG(mode) &&
1554 !(wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA))
1557 /* Note: we still use FILE_ATTRIBUTE_NORMAL for device nodes,
1558 * named pipes, and sockets. The real mode is in the UNIX
1560 ret = create_file(fuse_ctx, path, mode, rdev,
1561 FILE_ATTRIBUTE_NORMAL, &dentry);
1564 touch_parent(dentry);
1570 wimfs_open(const char *path, struct fuse_file_info *fi)
1572 struct wimfs_context *ctx = wimfs_get_context();
1573 struct wim_dentry *dentry;
1574 struct wim_inode *inode;
1575 struct wim_inode_stream *strm;
1576 struct blob_descriptor *blob;
1577 struct wimfs_fd *fd;
1580 ret = wim_pathname_to_stream(ctx, path, 0, &dentry, &strm);
1584 inode = dentry->d_inode;
1585 blob = stream_blob_resolved(strm);
1587 /* The data of the file being opened may be in the staging directory
1588 * (read-write mounts only) or in the WIM. If it's in the staging
1589 * directory, we need to open a native file descriptor for the
1590 * corresponding file. Otherwise, we can read the file data directly
1591 * from the WIM file if we are opening it read-only, but we need to
1592 * extract the data to the staging directory if we are opening it
1595 if (flags_writable(fi->flags) &&
1596 (!blob || blob->blob_location != BLOB_IN_STAGING_FILE)) {
1597 ret = extract_blob_to_staging_dir(inode,
1603 blob = stream_blob_resolved(strm);
1606 ret = alloc_wimfs_fd(inode, strm, &fd);
1610 if (blob && blob->blob_location == BLOB_IN_STAGING_FILE) {
1613 raw_fd = openat(blob->staging_dir_fd, blob->staging_file_name,
1614 (fi->flags & O_ACCMODE) | O_NOFOLLOW);
1619 filedes_init(&fd->f_staging_fd, raw_fd);
1621 fi->fh = (uintptr_t)fd;
1626 wimfs_opendir(const char *path, struct fuse_file_info *fi)
1628 WIMStruct *wim = wimfs_get_WIMStruct();
1629 struct wim_inode *inode;
1630 struct wim_inode_stream *strm;
1631 struct wimfs_fd *fd;
1634 inode = wim_pathname_to_inode(wim, path);
1637 if (!inode_is_directory(inode))
1639 strm = inode_get_unnamed_data_stream(inode);
1642 ret = alloc_wimfs_fd(inode, strm, &fd);
1645 fi->fh = (uintptr_t)fd;
1650 wimfs_read(const char *path, char *buf, size_t size,
1651 off_t offset, struct fuse_file_info *fi)
1653 struct wimfs_fd *fd = WIMFS_FD(fi);
1654 const struct blob_descriptor *blob;
1661 if (offset >= blob->size)
1664 if (size > blob->size - offset)
1665 size = blob->size - offset;
1670 switch (blob->blob_location) {
1672 if (read_partial_wim_blob_into_buf(blob, size, offset, buf))
1673 ret = errno ? -errno : -EIO;
1677 case BLOB_IN_STAGING_FILE:
1678 ret = raw_pread(&fd->f_staging_fd, buf, size, offset);
1682 case BLOB_IN_ATTACHED_BUFFER:
1683 memcpy(buf, blob->attached_buffer + offset, size);
1694 wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
1695 off_t offset, struct fuse_file_info *fi)
1697 struct wimfs_fd *fd = WIMFS_FD(fi);
1698 const struct wim_inode *inode;
1699 const struct wim_dentry *child;
1702 inode = fd->f_inode;
1704 ret = filler(buf, ".", NULL, 0);
1707 ret = filler(buf, "..", NULL, 0);
1711 for_inode_child(child, inode) {
1712 char *file_name_mbs;
1713 size_t file_name_mbs_nbytes;
1715 ret = utf16le_to_tstr(child->file_name,
1716 child->file_name_nbytes,
1718 &file_name_mbs_nbytes);
1722 ret = filler(buf, file_name_mbs, NULL, 0);
1723 FREE(file_name_mbs);
1731 wimfs_readlink(const char *path, char *buf, size_t buf_len)
1733 WIMStruct *wim = wimfs_get_WIMStruct();
1734 const struct wim_inode *inode;
1737 inode = wim_pathname_to_inode(wim, path);
1740 if (!inode_is_symlink(inode))
1744 ret = wim_inode_readlink(inode, buf, buf_len - 1, NULL);
1748 } else if (ret == -ENAMETOOLONG) {
1749 buf[buf_len - 1] = '\0';
1754 /* We use this for both release() and releasedir(), since in both cases we
1755 * simply need to close the file descriptor. */
1757 wimfs_release(const char *path, struct fuse_file_info *fi)
1759 return close_wimfs_fd(WIMFS_FD(fi));
1763 wimfs_removexattr(const char *path, const char *name)
1765 struct wimfs_context *ctx = wimfs_get_context();
1766 struct wim_inode *inode;
1767 struct wim_inode_stream *strm;
1769 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1772 if (strncmp(name, "user.", 5))
1779 /* Removing a named data stream. */
1781 inode = wim_pathname_to_inode(ctx->wim, path);
1785 strm = inode_get_data_stream_tstr(inode, name);
1787 return (errno == ENOENT) ? -ENOATTR : -errno;
1789 inode_remove_stream(inode, strm, ctx->wim->blob_table);
1794 wimfs_rename(const char *from, const char *to)
1796 return rename_wim_path(wimfs_get_WIMStruct(), from, to,
1797 WIMLIB_CASE_SENSITIVE, NULL);
1801 wimfs_rmdir(const char *path)
1803 WIMStruct *wim = wimfs_get_WIMStruct();
1804 struct wim_dentry *dentry;
1806 dentry = get_dentry(wim, path, WIMLIB_CASE_SENSITIVE);
1810 if (!dentry_is_directory(dentry))
1813 if (dentry_has_children(dentry))
1816 touch_parent(dentry);
1817 remove_dentry(dentry, wim->blob_table);
1822 wimfs_setxattr(const char *path, const char *name,
1823 const char *value, size_t size, int flags)
1825 struct wimfs_context *ctx = wimfs_get_context();
1826 struct wim_inode *inode;
1827 struct wim_inode_stream *strm;
1828 const utf16lechar *uname;
1831 if (!strncmp(name, "wimfs.", 6)) {
1832 /* Handle some magical extended attributes. These really should
1833 * be ioctls, but directory ioctls aren't supported until
1834 * libfuse 2.9, and even then they are broken. [Fixed by
1835 * libfuse commit e3b7d4c278a26520be63d99d6ea84b26906fe73d] */
1837 if (!strcmp(name, "unmount_info")) {
1838 if (!may_unmount_wimfs())
1840 if (size < sizeof(struct wimfs_unmount_info))
1842 memcpy(&ctx->unmount_info, value,
1843 sizeof(struct wimfs_unmount_info));
1849 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1852 if (strncmp(name, "user.", 5))
1859 /* Setting the contents of a named data stream. */
1861 inode = wim_pathname_to_inode(ctx->wim, path);
1865 ret = tstr_get_utf16le(name, &uname);
1869 strm = inode_get_stream(inode, STREAM_TYPE_DATA, uname);
1872 if (flags & XATTR_CREATE)
1876 if (flags & XATTR_REPLACE)
1881 if (!inode_replace_stream_data(inode, strm, value, size,
1882 ctx->wim->blob_table))
1888 if (!inode_add_stream_with_data(inode, STREAM_TYPE_DATA, uname,
1889 value, size, ctx->wim->blob_table))
1898 tstr_put_utf16le(uname);
1903 wimfs_symlink(const char *to, const char *from)
1905 struct fuse_context *fuse_ctx = fuse_get_context();
1906 struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
1907 struct wim_dentry *dentry;
1910 ret = create_file(fuse_ctx, from, S_IFLNK | 0777, 0,
1911 FILE_ATTRIBUTE_REPARSE_POINT, &dentry);
1914 dentry->d_inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
1915 ret = wim_inode_set_symlink(dentry->d_inode, to,
1916 wimfs_ctx->wim->blob_table);
1918 remove_dentry(dentry, wimfs_ctx->wim->blob_table);
1919 if (ret == WIMLIB_ERR_NOMEM)
1924 touch_parent(dentry);
1930 wimfs_truncate(const char *path, off_t size)
1932 const struct wimfs_context *ctx = wimfs_get_context();
1933 struct wim_dentry *dentry;
1934 struct wim_inode_stream *strm;
1935 struct blob_descriptor *blob;
1939 ret = wim_pathname_to_stream(ctx, path, 0, &dentry, &strm);
1943 blob = stream_blob_resolved(strm);
1948 if (!blob || blob->blob_location != BLOB_IN_STAGING_FILE) {
1949 return extract_blob_to_staging_dir(dentry->d_inode,
1953 /* Truncate the staging file. */
1954 fd = openat(blob->staging_dir_fd, blob->staging_file_name,
1955 O_WRONLY | O_NOFOLLOW);
1958 ret = ftruncate(fd, size);
1959 if (close(fd) || ret)
1962 touch_inode(dentry->d_inode);
1967 wimfs_unlink(const char *path)
1969 const struct wimfs_context *ctx = wimfs_get_context();
1970 struct wim_dentry *dentry;
1971 struct wim_inode_stream *strm;
1974 ret = wim_pathname_to_stream(ctx, path, 0, &dentry, &strm);
1978 if (stream_is_named(strm)) {
1979 inode_remove_stream(dentry->d_inode, strm,
1980 ctx->wim->blob_table);
1982 touch_parent(dentry);
1983 remove_dentry(dentry, ctx->wim->blob_table);
1988 #ifdef HAVE_UTIMENSAT
1990 * Change the timestamp on a file dentry.
1992 * Note that alternate data streams do not have their own timestamps.
1995 wimfs_utimens(const char *path, const struct timespec tv[2])
1997 WIMStruct *wim = wimfs_get_WIMStruct();
1998 struct wim_inode *inode;
2000 inode = wim_pathname_to_inode(wim, path);
2004 if (tv[0].tv_nsec != UTIME_OMIT) {
2005 if (tv[0].tv_nsec == UTIME_NOW)
2006 inode->i_last_access_time = now_as_wim_timestamp();
2008 inode->i_last_access_time = timespec_to_wim_timestamp(&tv[0]);
2010 if (tv[1].tv_nsec != UTIME_OMIT) {
2011 if (tv[1].tv_nsec == UTIME_NOW)
2012 inode->i_last_write_time = now_as_wim_timestamp();
2014 inode->i_last_write_time = timespec_to_wim_timestamp(&tv[1]);
2018 #else /* HAVE_UTIMENSAT */
2020 wimfs_utime(const char *path, struct utimbuf *times)
2022 WIMStruct *wim = wimfs_get_WIMStruct();
2023 struct wim_inode *inode;
2025 inode = wim_pathname_to_inode(wim, path);
2029 inode->i_last_access_time = time_t_to_wim_timestamp(times->actime);
2030 inode->i_last_write_time = time_t_to_wim_timestamp(times->modtime);
2033 #endif /* !HAVE_UTIMENSAT */
2036 wimfs_write(const char *path, const char *buf, size_t size,
2037 off_t offset, struct fuse_file_info *fi)
2039 struct wimfs_fd *fd = WIMFS_FD(fi);
2042 ret = raw_pwrite(&fd->f_staging_fd, buf, size, offset);
2046 if (offset + size > fd->f_blob->size)
2047 fd->f_blob->size = offset + size;
2049 touch_inode(fd->f_inode);
2053 static struct fuse_operations wimfs_operations = {
2054 .chmod = wimfs_chmod,
2055 .chown = wimfs_chown,
2056 .fgetattr = wimfs_fgetattr,
2057 .ftruncate = wimfs_ftruncate,
2058 .getattr = wimfs_getattr,
2059 .getxattr = wimfs_getxattr,
2061 .listxattr = wimfs_listxattr,
2062 .mkdir = wimfs_mkdir,
2063 .mknod = wimfs_mknod,
2065 .opendir = wimfs_opendir,
2067 .readdir = wimfs_readdir,
2068 .readlink = wimfs_readlink,
2069 .release = wimfs_release,
2070 .releasedir = wimfs_release,
2071 .removexattr = wimfs_removexattr,
2072 .rename = wimfs_rename,
2073 .rmdir = wimfs_rmdir,
2074 .setxattr = wimfs_setxattr,
2075 .symlink = wimfs_symlink,
2076 .truncate = wimfs_truncate,
2077 .unlink = wimfs_unlink,
2078 #ifdef HAVE_UTIMENSAT
2079 .utimens = wimfs_utimens,
2081 .utime = wimfs_utime,
2083 .write = wimfs_write,
2085 /* We keep track of file descriptor structures (struct wimfs_fd), so
2086 * there is no need to have the file path provided on operations such as
2088 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 8)
2089 .flag_nullpath_ok = 1,
2091 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 9)
2093 .flag_utime_omit_ok = 1,
2097 /* API function documented in wimlib.h */
2099 wimlib_mount_image(WIMStruct *wim, int image, const char *dir,
2100 int mount_flags, const char *staging_dir)
2103 struct wim_image_metadata *imd;
2104 struct wimfs_context ctx;
2105 char *fuse_argv[16];
2108 if (!wim || !dir || !*dir)
2109 return WIMLIB_ERR_INVALID_PARAM;
2111 if (mount_flags & ~(WIMLIB_MOUNT_FLAG_READWRITE |
2112 WIMLIB_MOUNT_FLAG_DEBUG |
2113 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
2114 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
2115 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS |
2116 WIMLIB_MOUNT_FLAG_UNIX_DATA |
2117 WIMLIB_MOUNT_FLAG_ALLOW_OTHER))
2118 return WIMLIB_ERR_INVALID_PARAM;
2120 /* For read-write mount, check for write access to the WIM. */
2121 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2123 return WIMLIB_ERR_NO_FILENAME;
2124 ret = can_modify_wim(wim);
2129 /* Select the image to mount. */
2130 ret = select_wim_image(wim, image);
2134 /* Get the metadata for the image to mount. */
2135 imd = wim_get_current_image_metadata(wim);
2137 if (imd->modified) {
2138 /* To avoid complicating things, we don't support mounting
2139 * images to which in-memory modifications have already been
2141 ERROR("Cannot mount a modified WIM image!");
2142 return WIMLIB_ERR_INVALID_PARAM;
2145 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2146 ret = lock_wim_for_append(wim);
2151 if (wim_has_solid_resources(wim)) {
2152 WARNING("Mounting a WIM file containing solid-compressed data; "
2153 "file access may be slow.");
2156 /* If the user did not specify an interface for accessing named
2157 * data streams, use the default (extended attributes). */
2158 if (!(mount_flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
2159 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
2160 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
2161 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
2163 /* Start initializing the wimfs_context. */
2164 memset(&ctx, 0, sizeof(struct wimfs_context));
2166 ctx.mount_flags = mount_flags;
2167 if (mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
2168 ctx.default_lookup_flags = LOOKUP_FLAG_ADS_OK;
2169 /* For read-write mount, create the staging directory. */
2170 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2171 ret = make_staging_dir(&ctx, staging_dir);
2175 ctx.owner_uid = getuid();
2176 ctx.owner_gid = getgid();
2178 /* Add each blob referenced by files in the image to a list and
2179 * preemptively double the number of references to each. This is done
2180 * to allow implementing the WIMLIB_UNMOUNT_FLAG_NEW_IMAGE semantics.
2182 INIT_LIST_HEAD(&ctx.orig_blob_list);
2183 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2185 struct wim_inode *inode;
2186 struct blob_descriptor *blob;
2188 image_for_each_inode(inode, imd) {
2189 for (i = 0; i < inode->i_num_streams; i++) {
2190 blob = stream_blob(&inode->i_streams[i],
2193 blob->out_refcnt = 0;
2197 image_for_each_inode(inode, imd) {
2198 for (i = 0; i < inode->i_num_streams; i++) {
2199 blob = stream_blob(&inode->i_streams[i],
2202 if (blob->out_refcnt == 0)
2203 list_add(&blob->orig_blob_list,
2204 &ctx.orig_blob_list);
2205 blob->out_refcnt += inode->i_nlink;
2206 blob->refcnt += inode->i_nlink;
2212 /* Number the inodes in the mounted image sequentially and initialize
2213 * the file descriptor arrays */
2214 prepare_inodes(&ctx);
2216 /* If a read-write mount, mark the image as modified. */
2217 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2220 /* Build the FUSE command line. */
2223 fuse_argv[fuse_argc++] = "wimlib";
2224 fuse_argv[fuse_argc++] = (char *)dir;
2226 /* Disable multi-threaded operation. */
2227 fuse_argv[fuse_argc++] = "-s";
2229 /* Enable FUSE debug mode (don't fork) if requested by the user. */
2230 if (mount_flags & WIMLIB_MOUNT_FLAG_DEBUG)
2231 fuse_argv[fuse_argc++] = "-d";
2234 * Build the FUSE mount options:
2237 * FUSE will use the inode numbers we provide. We want this,
2238 * because we have inodes and will number them ourselves.
2241 * Name for our filesystem (main type is "fuse").
2244 * If an open file is unlinked, unlink it for real rather than
2245 * renaming it to a hidden file. Our code supports this; an
2246 * unlinked inode is retained until all its file descriptors have
2249 * default_permissions
2250 * FUSE will perform permission checking. Useful when
2251 * WIMLIB_MOUNT_FLAG_UNIX_DATA is provided and the WIM image
2252 * contains the UNIX permissions for each file.
2255 * Cache the contents of files. This will speed up repeated access
2256 * to files on a mounted WIM image, since they won't need to be
2257 * decompressed repeatedly. This option is valid because data in
2258 * the WIM image should never be changed externally. (Although, if
2259 * someone really wanted to they could modify the WIM file or mess
2260 * with the staging directory; but then they're asking for
2263 * entry_timeout=1000000000
2264 * Cache positive name lookups indefinitely, since names can only
2265 * be added, removed, or modified through the mounted filesystem
2268 * negative_timeout=1000000000
2269 * Cache negative name lookups indefinitely, since names can only
2270 * be added, removed, or modified through the mounted filesystem
2274 * Don't cache file/directory attributes. This is needed as a
2275 * workaround for the fact that when caching attributes, the high
2276 * level interface to libfuse considers a file which has several
2277 * hard-linked names as several different files. (Otherwise, we
2278 * could cache our file/directory attributes indefinitely, since
2279 * they can only be changed through the mounted filesystem itself.)
2281 char optstring[256] =
2285 ",default_permissions"
2287 ",entry_timeout=1000000000"
2288 ",negative_timeout=1000000000"
2291 fuse_argv[fuse_argc++] = "-o";
2292 fuse_argv[fuse_argc++] = optstring;
2293 if (!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE))
2294 strcat(optstring, ",ro");
2295 if (mount_flags & WIMLIB_MOUNT_FLAG_ALLOW_OTHER)
2296 strcat(optstring, ",allow_other");
2297 fuse_argv[fuse_argc] = NULL;
2299 /* Mount our filesystem. */
2300 ret = fuse_main(fuse_argc, fuse_argv, &wimfs_operations, &ctx);
2302 /* Cleanup and return. */
2304 ret = WIMLIB_ERR_FUSE;
2305 release_extra_refcnts(&ctx);
2306 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2307 delete_staging_dir(&ctx);
2309 unlock_wim_for_append(wim);
2313 struct commit_progress_thread_args {
2315 wimlib_progress_func_t progfunc;
2320 commit_progress_thread_proc(void *_args)
2322 struct commit_progress_thread_args *args = _args;
2323 struct commit_progress_report report;
2327 ret = mq_receive(args->mq,
2328 (char *)&report, sizeof(report), NULL);
2329 if (ret == sizeof(report)) {
2330 call_progress(args->progfunc, report.msg,
2331 &report.info, args->progctx);
2333 if (ret == 0 || (ret < 0 && errno != EINTR))
2341 generate_message_queue_name(char name[WIMFS_MQUEUE_NAME_LEN + 1])
2344 memcpy(name + 1, "wimfs-", 6);
2345 randomize_char_array_with_alnum(name + 7, WIMFS_MQUEUE_NAME_LEN - 7);
2346 name[WIMFS_MQUEUE_NAME_LEN] = '\0';
2350 create_message_queue(const char *name)
2355 struct mq_attr attr;
2358 memset(&attr, 0, sizeof(attr));
2360 attr.mq_msgsize = sizeof(struct commit_progress_report);
2362 am_root = (geteuid() == 0);
2364 /* Filesystem mounted as normal user with --allow-other should
2365 * be able to send messages to root user, if they're doing the
2367 umask_save = umask(0);
2372 mq = mq_open(name, O_RDWR | O_CREAT | O_EXCL, mode, &attr);
2378 /* Unmount a read-only or read-write mounted WIM image. */
2380 do_unmount(const char *dir)
2385 len = getxattr(dir, "wimfs.unmount", &status, sizeof(int));
2386 if (len == sizeof(int))
2388 else if (len < 0 && (errno == EACCES || errno == EPERM))
2389 return WIMLIB_ERR_NOT_PERMITTED_TO_UNMOUNT;
2391 return WIMLIB_ERR_NOT_A_MOUNTPOINT;
2395 set_unmount_info(const char *dir, const struct wimfs_unmount_info *unmount_info)
2397 if (!setxattr(dir, "wimfs.unmount_info",
2398 unmount_info, sizeof(struct wimfs_unmount_info), 0))
2400 else if (errno == EROFS)
2402 else if (errno == EACCES || errno == EPERM)
2403 return WIMLIB_ERR_NOT_PERMITTED_TO_UNMOUNT;
2405 return WIMLIB_ERR_NOT_A_MOUNTPOINT;
2409 do_unmount_discard(const char *dir)
2412 struct wimfs_unmount_info unmount_info;
2414 memset(&unmount_info, 0, sizeof(unmount_info));
2416 ret = set_unmount_info(dir, &unmount_info);
2419 return do_unmount(dir);
2422 /* Unmount a read-write mounted WIM image, committing the changes. */
2424 do_unmount_commit(const char *dir, int unmount_flags,
2425 wimlib_progress_func_t progfunc, void *progctx)
2427 struct wimfs_unmount_info unmount_info;
2429 struct commit_progress_thread_args args;
2430 pthread_t commit_progress_tid;
2433 memset(&unmount_info, 0, sizeof(unmount_info));
2434 unmount_info.unmount_flags = unmount_flags;
2436 /* The current thread will be stuck in getxattr() until the image is
2437 * committed. Create a thread to handle the progress messages. */
2439 generate_message_queue_name(unmount_info.mq_name);
2441 mq = create_message_queue(unmount_info.mq_name);
2442 if (mq == (mqd_t)-1) {
2443 ERROR_WITH_ERRNO("Can't create POSIX message queue");
2444 return WIMLIB_ERR_MQUEUE;
2447 args.progfunc = progfunc;
2448 args.progctx = progctx;
2449 ret = pthread_create(&commit_progress_tid, NULL,
2450 commit_progress_thread_proc, &args);
2453 ERROR_WITH_ERRNO("Can't create thread");
2454 ret = WIMLIB_ERR_NOMEM;
2457 unmount_info.unmount_flags |= WIMLIB_UNMOUNT_FLAG_SEND_PROGRESS;
2460 ret = set_unmount_info(dir, &unmount_info);
2462 ret = do_unmount(dir);
2464 /* Terminate the progress thread. */
2466 mq_send(mq, empty, 0, 1);
2467 pthread_join(commit_progress_tid, NULL);
2472 mq_unlink(unmount_info.mq_name);
2478 begin_unmount(const char *dir, int unmount_flags, int *mount_flags_ret,
2479 wimlib_progress_func_t progfunc, void *progctx)
2483 int wim_filename_len;
2484 union wimlib_progress_info progress;
2486 if (getxattr(dir, "wimfs.mount_flags",
2487 &mount_flags, sizeof(int)) != sizeof(int))
2488 return WIMLIB_ERR_NOT_A_MOUNTPOINT;
2490 *mount_flags_ret = mount_flags;
2495 if (getxattr(dir, "wimfs.mounted_image",
2496 &mounted_image, sizeof(int)) != sizeof(int))
2497 return WIMLIB_ERR_NOT_A_MOUNTPOINT;
2499 wim_filename_len = getxattr(dir, "wimfs.wim_filename", NULL, 0);
2500 if (wim_filename_len < 0)
2501 return WIMLIB_ERR_NOT_A_MOUNTPOINT;
2503 char wim_filename[wim_filename_len + 1];
2504 if (getxattr(dir, "wimfs.wim_filename",
2505 wim_filename, wim_filename_len) != wim_filename_len)
2506 return WIMLIB_ERR_NOT_A_MOUNTPOINT;
2507 wim_filename[wim_filename_len] = '\0';
2509 progress.unmount.mountpoint = dir;
2510 progress.unmount.mounted_wim = wim_filename;
2511 progress.unmount.mounted_image = mounted_image;
2512 progress.unmount.mount_flags = mount_flags;
2513 progress.unmount.unmount_flags = unmount_flags;
2515 return call_progress(progfunc, WIMLIB_PROGRESS_MSG_UNMOUNT_BEGIN,
2516 &progress, progctx);
2519 /* API function documented in wimlib.h */
2521 wimlib_unmount_image_with_progress(const char *dir, int unmount_flags,
2522 wimlib_progress_func_t progfunc, void *progctx)
2527 ret = wimlib_global_init(WIMLIB_INIT_FLAG_ASSUME_UTF8);
2531 if (unmount_flags & ~(WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY |
2532 WIMLIB_UNMOUNT_FLAG_COMMIT |
2533 WIMLIB_UNMOUNT_FLAG_REBUILD |
2534 WIMLIB_UNMOUNT_FLAG_RECOMPRESS |
2535 WIMLIB_UNMOUNT_FLAG_FORCE |
2536 WIMLIB_UNMOUNT_FLAG_NEW_IMAGE))
2537 return WIMLIB_ERR_INVALID_PARAM;
2539 ret = begin_unmount(dir, unmount_flags, &mount_flags,
2544 if ((unmount_flags & WIMLIB_UNMOUNT_FLAG_COMMIT) &&
2545 (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE))
2546 return do_unmount_commit(dir, unmount_flags,
2549 return do_unmount_discard(dir);
2552 #else /* WITH_FUSE */
2556 mount_unsupported_error(void)
2558 #if defined(__WIN32__)
2559 ERROR("Sorry-- Mounting WIM images is not supported on Windows!");
2561 ERROR("wimlib was compiled with --without-fuse, which disables support "
2562 "for mounting WIMs.");
2564 return WIMLIB_ERR_UNSUPPORTED;
2568 wimlib_unmount_image_with_progress(const tchar *dir, int unmount_flags,
2569 wimlib_progress_func_t progfunc, void *progctx)
2571 return mount_unsupported_error();
2575 wimlib_mount_image(WIMStruct *wim, int image, const tchar *dir,
2576 int mount_flags, const tchar *staging_dir)
2578 return mount_unsupported_error();
2581 #endif /* !WITH_FUSE */
2584 wimlib_unmount_image(const tchar *dir, int unmount_flags)
2586 return wimlib_unmount_image_with_progress(dir, unmount_flags, NULL, NULL);