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 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/dentry.h"
58 #include "wimlib/encoding.h"
59 #include "wimlib/lookup_table.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 lookup table entry for the data stream that has been
101 * opened. 'num_opened_fds' of the lookup table entry tracks the number
102 * of file descriptors that reference it. Or, this value may be NULL,
103 * which indicates that the opened stream is empty and consequently does
104 * not have a lookup table entry. */
105 struct wim_lookup_table_entry *f_lte;
107 /* If valid (filedes_valid(&f_staging_fd)), this contains the
108 * corresponding native file descriptor for the staging file that has
109 * been created for reading from and/or writing to this open stream. A
110 * single staging file might have multiple file descriptors open to it
111 * simultaneously, each used by a different 'struct wimfs_fd'.
113 * Or, if invalid (!filedes_valid(&f_staging_fd)), this 'struct
114 * wimfs_fd' is not associated with a staging file. This is permissible
115 * only if this 'struct wimfs_fd' was opened read-only and the stream
116 * has not yet been extracted to a staging file. */
117 struct filedes f_staging_fd;
119 /* 0-based index of this file descriptor in the file descriptor table of
123 /* Unique ID of the opened stream in the inode. This will stay the same
124 * even if the indices of the inode's alternate data streams are changed
129 #define WIMFS_FD(fi) ((struct wimfs_fd *)(uintptr_t)((fi)->fh))
131 /* Context structure for a mounted WIM image. */
132 struct wimfs_context {
133 /* The WIMStruct containing the mounted image. The mounted image is the
134 * currently selected image (wim->current_image). */
137 /* Flags passed to wimlib_mount_image() (WIMLIB_MOUNT_FLAG_*). */
140 /* Default flags for path lookup in the WIM image. */
141 int default_lookup_flags;
143 /* Information about the user who has mounted the WIM image */
147 /* Information about the staging directory for a read-write mount. */
150 char *staging_dir_name;
152 /* For read-write mounts, the inode number to be assigned to the next
153 * created file. Note: since this isn't a persistent filesystem and we
154 * can re-assign the inode numbers just before mounting the image, it's
155 * good enough to just generate inode numbers sequentially. */
158 /* Number of file descriptors open to the mounted WIM image. */
159 unsigned long num_open_fds;
161 /* Original list of single-instance streams in the mounted image, linked
162 * by 'struct wim_lookup_table_entry'.orig_stream_list. */
163 struct list_head orig_stream_list;
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 * A pointer to the inode containing the stream being opened.
225 * The ID of the data stream being opened within the inode.
227 * A pointer to the lookup table entry for the stream data. Or, for a
228 * 0-byte stream, this may be NULL.
230 * On success, a pointer to the new file descriptor will be stored here.
232 * Returns 0 or a -errno code.
235 alloc_wimfs_fd(struct wim_inode *inode,
237 struct wim_lookup_table_entry *lte,
238 struct wimfs_fd **fd_ret)
240 static const u16 min_fds_per_alloc = 8;
241 static const u16 max_fds = 0xffff;
245 if (inode->i_num_opened_fds == inode->i_num_allocated_fds) {
247 struct wimfs_fd **fds;
249 /* Expand this inode's file descriptor table. */
251 num_new_fds = max(min_fds_per_alloc,
252 inode->i_num_allocated_fds / 4);
254 num_new_fds = min(num_new_fds,
255 max_fds - inode->i_num_allocated_fds);
257 if (num_new_fds == 0)
260 fds = REALLOC(inode->i_fds,
261 (inode->i_num_allocated_fds + num_new_fds) *
266 memset(&fds[inode->i_num_allocated_fds], 0,
267 num_new_fds * sizeof(fds[0]));
269 inode->i_num_allocated_fds += num_new_fds;
270 inode->i_next_fd = inode->i_num_opened_fds;
273 /* Allocate the file descriptor in the first available space in the
274 * inode's file descriptor table.
276 * i_next_fd is the lower bound on the next open slot. */
277 for (i = inode->i_next_fd; inode->i_fds[i]; i++)
280 fd = MALLOC(sizeof(*fd));
286 filedes_invalidate(&fd->f_staging_fd);
288 fd->f_stream_id = stream_id;
290 inode->i_fds[i] = fd;
291 inode->i_num_opened_fds++;
293 lte->num_opened_fds++;
294 wimfs_inc_num_open_fds();
295 inode->i_next_fd = i + 1;
300 * Close a file descriptor to a data stream in the mounted WIM image.
302 * Returns 0 or a -errno code. The file descriptor is always closed.
305 close_wimfs_fd(struct wimfs_fd *fd)
308 struct wim_inode *inode;
310 /* Close the staging file if open. */
311 if (filedes_valid(&fd->f_staging_fd))
312 if (filedes_close(&fd->f_staging_fd))
315 /* Release this file descriptor from its lookup table entry. */
317 lte_decrement_num_opened_fds(fd->f_lte);
319 wimfs_dec_num_open_fds();
321 /* Release this file descriptor from its inode. */
323 inode->i_fds[fd->f_idx] = NULL;
324 if (fd->f_idx < inode->i_next_fd)
325 inode->i_next_fd = fd->f_idx;
327 inode_dec_num_opened_fds(inode);
332 * Translate a path into the corresponding inode in the mounted WIM image.
334 * See get_dentry() for more information.
336 * Returns a pointer to the resulting inode, or NULL with errno set.
338 static struct wim_inode *
339 wim_pathname_to_inode(WIMStruct *wim, const char *path)
341 struct wim_dentry *dentry;
343 dentry = get_dentry(wim, path, WIMLIB_CASE_SENSITIVE);
346 return dentry->d_inode;
349 /* Can look up named data stream with colon syntax */
350 #define LOOKUP_FLAG_ADS_OK 0x01
352 /* Can look up directory (otherwise get -ENOTDIR) */
353 #define LOOKUP_FLAG_DIRECTORY_OK 0x02
356 * Translate a path into the corresponding dentry, lookup table entry, and
357 * stream index in the mounted WIM image.
359 * Returns 0 or a -errno code. All of @dentry_ret, @lte_ret, and
360 * @stream_idx_ret are optional.
363 wim_pathname_to_stream(const struct wimfs_context *ctx, const char *path,
365 struct wim_dentry **dentry_ret,
366 struct wim_lookup_table_entry **lte_ret,
367 unsigned *stream_idx_ret)
369 WIMStruct *wim = ctx->wim;
370 struct wim_dentry *dentry;
371 struct wim_lookup_table_entry *lte;
373 const char *stream_name = NULL;
374 struct wim_inode *inode;
377 lookup_flags |= ctx->default_lookup_flags;
379 if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
380 stream_name = path_stream_name(path);
382 p = (char *)stream_name - 1;
387 dentry = get_dentry(wim, path, WIMLIB_CASE_SENSITIVE);
393 inode = dentry->d_inode;
395 if (inode_resolve_streams(inode, wim->lookup_table, false))
398 if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
399 && inode_is_directory(inode))
403 struct wim_ads_entry *ads_entry;
405 ads_entry = inode_get_ads_entry(inode, stream_name);
409 stream_idx = ads_entry - inode->i_ads_entries + 1;
410 lte = ads_entry->lte;
412 lte = inode_unnamed_stream_resolved(inode, &stream_idx);
415 *dentry_ret = dentry;
419 *stream_idx_ret = stream_idx;
424 * Create a new file in the mounted WIM image.
427 * The FUSE context for the mounted image.
429 * The path at which to create the first link to the new file. If a file
430 * already exists at this path, -EEXIST is returned.
432 * The UNIX mode for the new file. This is only honored if
433 * WIMLIB_MOUNT_FLAG_UNIX_DATA was passed to wimlib_mount_image().
435 * The device ID for the new file, encoding the major and minor device
436 * numbers. This is only honored if WIMLIB_MOUNT_FLAG_UNIX_DATA was passed
437 * to wimlib_mount_image().
439 * Windows file attributes to use for the new file.
441 * On success, a pointer to the new dentry is returned here. Its d_inode
442 * member will point to the new inode that was created for it and added to
443 * the mounted WIM image.
445 * Returns 0 or a -errno code.
448 create_dentry(struct fuse_context *fuse_ctx, const char *path,
449 mode_t mode, dev_t rdev, u32 attributes,
450 struct wim_dentry **dentry_ret)
452 struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
453 struct wim_dentry *parent;
454 const char *basename;
455 struct wim_dentry *new_dentry;
456 struct wim_inode *new_inode;
458 parent = get_parent_dentry(wimfs_ctx->wim, path, WIMLIB_CASE_SENSITIVE);
462 if (!dentry_is_directory(parent))
465 basename = path_basename(path);
467 if (get_dentry_child_with_name(parent, basename, WIMLIB_CASE_SENSITIVE))
470 if (new_dentry_with_inode(basename, &new_dentry))
473 new_inode = new_dentry->d_inode;
475 new_inode->i_resolved = 1;
476 new_inode->i_ino = wimfs_ctx->next_ino++;
477 new_inode->i_attributes = attributes;
479 if (wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA) {
480 struct wimlib_unix_data unix_data;
482 unix_data.uid = fuse_ctx->uid;
483 unix_data.gid = fuse_ctx->gid;
484 unix_data.mode = fuse_mask_mode(mode, fuse_ctx);
485 unix_data.rdev = rdev;
486 if (!inode_set_unix_data(new_inode, &unix_data, UNIX_DATA_ALL))
488 free_dentry(new_dentry);
493 list_add_tail(&new_inode->i_list,
494 &wim_get_current_image_metadata(wimfs_ctx->wim)->inode_list);
496 dentry_add_child(parent, new_dentry);
498 *dentry_ret = new_dentry;
503 * Remove a dentry from the mounted WIM image; i.e. remove an alias for an
507 remove_dentry(struct wim_dentry *dentry,
508 struct wim_lookup_table *lookup_table)
510 /* Drop the reference to each stream the inode contains. */
511 inode_unref_streams(dentry->d_inode, lookup_table);
513 /* Unlink the dentry from the image's dentry tree. */
514 unlink_dentry(dentry);
516 /* Delete the dentry. This will also decrement the link count of the
517 * corresponding inode, and possibly cause it to be deleted as well. */
521 /* Generate UNIX filetype mode bits for the specified WIM inode, based on its
522 * Windows file attributes. */
524 inode_unix_file_type(const struct wim_inode *inode)
526 if (inode_is_symlink(inode))
528 else if (inode_is_directory(inode))
534 /* Generate a default UNIX mode for the specified WIM inode. */
536 inode_default_unix_mode(const struct wim_inode *inode)
538 return inode_unix_file_type(inode) | 0777;
542 * Retrieve standard UNIX metadata ('struct stat') for a WIM inode.
544 * @lte specifies the stream of the inode that is being queried. We mostly
545 * return the same information for all streams, but st_size and st_blocks may be
546 * different for different streams.
548 * This always returns 0.
551 inode_to_stbuf(const struct wim_inode *inode,
552 const struct wim_lookup_table_entry *lte,
555 const struct wimfs_context *ctx = wimfs_get_context();
556 struct wimlib_unix_data unix_data;
558 memset(stbuf, 0, sizeof(struct stat));
559 if ((ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA) &&
560 inode_get_unix_data(inode, &unix_data))
562 /* Use the user ID, group ID, mode, and device ID from the
563 * inode's extra UNIX metadata information. */
564 stbuf->st_uid = unix_data.uid;
565 stbuf->st_gid = unix_data.gid;
566 stbuf->st_mode = unix_data.mode;
567 stbuf->st_rdev = unix_data.rdev;
569 /* Generate default values for the user ID, group ID, and mode.
571 * Note: in the case of an allow_other mount, fuse_context.uid
572 * may not be the same as wimfs_context.owner_uid! */
573 stbuf->st_uid = ctx->owner_uid;
574 stbuf->st_gid = ctx->owner_gid;
575 stbuf->st_mode = inode_default_unix_mode(inode);
577 stbuf->st_ino = inode->i_ino;
578 stbuf->st_nlink = inode->i_nlink;
580 stbuf->st_size = lte->size;
581 #ifdef HAVE_STAT_NANOSECOND_PRECISION
582 stbuf->st_atim = wim_timestamp_to_timespec(inode->i_last_access_time);
583 stbuf->st_mtim = wim_timestamp_to_timespec(inode->i_last_write_time);
584 stbuf->st_ctim = stbuf->st_mtim;
586 stbuf->st_atime = wim_timestamp_to_time_t(inode->i_last_access_time);
587 stbuf->st_mtime = wim_timestamp_to_time_t(inode->i_last_write_time);
588 stbuf->st_ctime = stbuf->st_mtime;
590 stbuf->st_blocks = DIV_ROUND_UP(stbuf->st_size, 512);
594 /* Update the last access and last write timestamps of a WIM inode. */
596 touch_inode(struct wim_inode *inode)
598 u64 now = now_as_wim_timestamp();
599 inode->i_last_access_time = now;
600 inode->i_last_write_time = now;
604 touch_parent(struct wim_dentry *dentry)
606 touch_inode(dentry->d_parent->d_inode);
610 * Create a new file in the staging directory for a read-write mounted image.
612 * On success, returns the file descriptor for the new staging file, opened for
613 * writing. In addition, stores the allocated name of the staging file in
616 * On failure, returns -1 and sets errno.
619 create_staging_file(const struct wimfs_context *ctx, char **name_ret)
622 static const size_t STAGING_FILE_NAME_LEN = 20;
626 name = MALLOC(STAGING_FILE_NAME_LEN + 1);
629 name[STAGING_FILE_NAME_LEN] = '\0';
632 randomize_char_array_with_alnum(name, STAGING_FILE_NAME_LEN);
633 fd = openat(ctx->staging_dir_fd, name,
634 O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW, 0600);
635 if (unlikely(fd < 0)) {
636 if (unlikely(errno == EEXIST))
637 /* Try again with another name. */
647 * Extract a WIM resource to the staging directory.
648 * This is necessary if a stream using the resource is being opened for writing.
651 * The inode containing the stream being opened for writing.
654 * The index of the stream in @inode being opened for writing.
657 * *lte_ptr is the lookup table entry for the stream being extracted, or
658 * NULL if the stream does not have a lookup table entry (which is possible
659 * if the stream is empty). On success, *lte_ptr will be set to point to a
660 * lookup table entry that represents the resource in its new location in a
661 * staging file. This may be the same as the old entry in the case that it
662 * was reused, or it may be a new entry.
665 * Number of bytes of the stream to extract and include in the staging file
666 * resource. It may be less than the actual stream length, in which case
667 * only a prefix of the resource will be extracted. It may also be more
668 * than the actual stream length, in which case the extra space will be
671 * Returns 0 or a -errno code.
674 extract_resource_to_staging_dir(struct wim_inode *inode,
676 struct wim_lookup_table_entry **lte_ptr,
678 const struct wimfs_context *ctx)
680 struct wim_lookup_table_entry *old_lte;
681 struct wim_lookup_table_entry *new_lte;
682 char *staging_file_name;
691 /* Create the staging file. */
692 staging_fd = create_staging_file(ctx, &staging_file_name);
693 if (unlikely(staging_fd < 0))
696 /* Extract the stream to the staging file (possibly truncated). */
700 filedes_init(&fd, staging_fd);
702 extract_size = min(old_lte->size, size);
703 result = extract_stream_to_fd(old_lte, &fd, extract_size);
709 /* In the case of truncate() to more than the file length, extend the
710 * staging file with zeroes by calling ftruncate(). */
711 if (!result && size > extract_size)
712 result = ftruncate(staging_fd, size);
714 /* Close the staging file. */
715 if (close(staging_fd))
718 /* If an error occurred, unlink the staging file. */
719 if (unlikely(result)) {
720 /* extract_stream_to_fd() should set errno, but if it didn't,
721 * set a default value. */
722 ret = errno ? -errno : -EIO;
723 goto out_delete_staging_file;
726 /* Now deal with the lookup table entries. We may be able to re-use the
727 * existing entry, but we may have to create a new one instead. */
729 stream_id = inode_stream_idx_to_id(inode, stream_idx);
731 if (old_lte && inode->i_nlink == old_lte->refcnt) {
732 /* The reference count of the existing lookup table entry is the
733 * same as the link count of the inode that contains the stream
734 * we're opening. Therefore, all the references to the lookup
735 * table entry correspond to the stream we're trying to extract,
736 * so the lookup table entry can be re-used. */
737 lookup_table_unlink(ctx->wim->lookup_table, old_lte);
738 lte_put_resource(old_lte);
741 /* We need to split the old lookup table entry because it also
742 * has other references. Or, there was no old lookup table
743 * entry, so we need to create a new one anyway. */
745 new_lte = new_lookup_table_entry();
746 if (unlikely(!new_lte)) {
748 goto out_delete_staging_file;
751 /* There may already be open file descriptors to this stream if
752 * it's previously been opened read-only, but just now we're
753 * opening it read-write. Identify those file descriptors and
754 * change their lookup table entry pointers to point to the new
755 * lookup table entry, and open staging file descriptors for
758 * At the same time, we need to count the number of these opened
759 * file descriptors to the new lookup table entry. If there's
760 * an old lookup table entry, this number needs to be subtracted
761 * from the fd's opened to the old entry. */
762 for (u16 i = 0, j = 0; j < inode->i_num_opened_fds; i++) {
766 fd = inode->i_fds[i];
772 if (fd->f_stream_id != stream_id)
775 /* This is a readonly fd for the same stream. */
777 new_lte->num_opened_fds++;
778 raw_fd = openat(ctx->staging_dir_fd, staging_file_name,
779 O_RDONLY | O_NOFOLLOW);
780 if (unlikely(raw_fd < 0)) {
782 goto out_revert_fd_changes;
784 filedes_init(&fd->f_staging_fd, raw_fd);
787 old_lte->num_opened_fds -= new_lte->num_opened_fds;
788 old_lte->refcnt -= inode->i_nlink;
792 new_lte->refcnt = inode->i_nlink;
793 new_lte->resource_location = RESOURCE_IN_STAGING_FILE;
794 new_lte->staging_file_name = staging_file_name;
795 new_lte->staging_dir_fd = ctx->staging_dir_fd;
796 new_lte->size = size;
798 add_unhashed_stream(new_lte, inode, stream_id,
799 &wim_get_current_image_metadata(ctx->wim)->unhashed_streams);
801 inode->i_lte = new_lte;
803 inode->i_ads_entries[stream_idx - 1].lte = new_lte;
807 out_revert_fd_changes:
808 for (u16 i = 0; new_lte->num_opened_fds; i++) {
809 struct wimfs_fd *fd = inode->i_fds[i];
810 if (fd && fd->f_stream_id == stream_id) {
812 if (filedes_valid(&fd->f_staging_fd)) {
813 filedes_close(&fd->f_staging_fd);
814 filedes_invalidate(&fd->f_staging_fd);
816 new_lte->num_opened_fds--;
819 free_lookup_table_entry(new_lte);
820 out_delete_staging_file:
821 unlinkat(ctx->staging_dir_fd, staging_file_name, 0);
822 FREE(staging_file_name);
827 * Create the staging directory for the WIM file.
829 * The staging directory will be created in the directory specified by the open
830 * file descriptor @parent_dir_fd. It will be given a randomly generated name
831 * based on @wim_basename, the name of the WIM file.
833 * On success, returns a file descriptor to the open staging directory with
834 * O_RDONLY access. In addition, stores the allocated name of the staging
835 * directory (relative to @parent_dir_fd) in @staging_dir_name_ret.
836 * On failure, returns -1 and sets errno.
839 make_staging_dir_at(int parent_dir_fd, const char *wim_basename,
840 char **staging_dir_name_ret)
842 static const char common_suffix[8] = ".staging";
843 static const size_t random_suffix_len = 10;
844 size_t wim_basename_len;
845 size_t staging_dir_name_len;
846 char *staging_dir_name;
850 wim_basename_len = strlen(wim_basename);
851 staging_dir_name_len = wim_basename_len + sizeof(common_suffix) +
853 staging_dir_name = MALLOC(staging_dir_name_len + 1);
854 if (!staging_dir_name)
857 p = staging_dir_name;
858 p = mempcpy(p, wim_basename, wim_basename_len);
859 p = mempcpy(p, common_suffix, sizeof(common_suffix));
860 randomize_char_array_with_alnum(p, random_suffix_len);
861 p += random_suffix_len;
864 if (mkdirat(parent_dir_fd, staging_dir_name, 0700))
867 fd = openat(parent_dir_fd, staging_dir_name,
868 O_RDONLY | O_DIRECTORY | O_NOFOLLOW);
872 *staging_dir_name_ret = staging_dir_name;
876 unlinkat(parent_dir_fd, staging_dir_name, AT_REMOVEDIR);
878 FREE(staging_dir_name);
883 * Create the staging directory and set ctx->staging_dir_fd,
884 * ctx->staging_dir_name, and ctx->parent_dir_fd.
887 make_staging_dir(struct wimfs_context *ctx, const char *parent_dir_path)
889 const char *wim_basename;
893 wim_basename = path_basename(ctx->wim->filename);
895 if (!parent_dir_path) {
896 /* The user did not specify a directory. Default to creating
897 * the staging directory alongside the WIM file. */
898 if (wim_basename > ctx->wim->filename) {
899 parent_dir_path = ctx->wim->filename;
900 end = (char *)(wim_basename - 1);
901 /* *end must be a slash. Temporarily overwrite it so we
902 * can open the parent directory. */
905 parent_dir_path = ".";
909 /* Open the parent directory (in which we'll create our staging
911 ctx->parent_dir_fd = open(parent_dir_path, O_RDONLY | O_DIRECTORY);
912 if (ctx->parent_dir_fd < 0) {
913 ERROR_WITH_ERRNO("Can't open directory \"%s\"",
915 ret = WIMLIB_ERR_OPENDIR;
916 goto out_restore_wim_filename;
919 ctx->staging_dir_fd = make_staging_dir_at(ctx->parent_dir_fd,
921 &ctx->staging_dir_name);
922 if (ctx->staging_dir_fd < 0) {
923 ERROR_WITH_ERRNO("Can't create staging directory in \"%s\"",
925 close(ctx->parent_dir_fd);
926 ret = WIMLIB_ERR_MKDIR;
927 goto out_restore_wim_filename;
930 out_restore_wim_filename:
936 /* Deletes the staging directory, undoing the effects of a successful call to
937 * make_staging_dir(). */
939 delete_staging_dir(struct wimfs_context *ctx)
944 dir = fdopendir(ctx->staging_dir_fd);
946 while ((ent = readdir(dir)))
947 unlinkat(ctx->staging_dir_fd, ent->d_name, 0);
950 close(ctx->staging_dir_fd);
952 if (unlinkat(ctx->parent_dir_fd, ctx->staging_dir_name, AT_REMOVEDIR))
953 WARNING_WITH_ERRNO("Could not delete staging directory");
954 FREE(ctx->staging_dir_name);
955 close(ctx->parent_dir_fd);
958 /* Number the inodes in the mounted image sequentially. */
960 reassign_inode_numbers(struct wimfs_context *ctx)
962 struct wim_image_metadata *imd;
963 struct wim_inode *inode;
966 imd = wim_get_current_image_metadata(ctx->wim);
967 image_for_each_inode(inode, imd)
968 inode->i_ino = ctx->next_ino++;
972 release_extra_refcnts(struct wimfs_context *ctx)
974 struct list_head *list = &ctx->orig_stream_list;
975 struct wim_lookup_table *lookup_table = ctx->wim->lookup_table;
976 struct wim_lookup_table_entry *lte, *tmp;
978 list_for_each_entry_safe(lte, tmp, list, orig_stream_list) {
979 u32 n = lte->out_refcnt;
981 lte_decrement_refcnt(lte, lookup_table);
985 /* Delete the 'struct wim_lookup_table_entry' for any stream that was modified
986 * or created in the read-write mounted image and had a final size of 0. */
988 delete_empty_streams(struct wimfs_context *ctx)
990 struct wim_lookup_table_entry *lte, *tmp;
991 struct wim_image_metadata *imd;
993 imd = wim_get_current_image_metadata(ctx->wim);
995 image_for_each_unhashed_stream_safe(lte, tmp, imd) {
997 *retrieve_lte_pointer(lte) = NULL;
998 list_del(<e->unhashed_list);
999 free_lookup_table_entry(lte);
1004 /* Close all file descriptors open to the specified inode.
1006 * Note: closing the last file descriptor might free the inode. */
1008 inode_close_fds(struct wim_inode *inode)
1010 u16 num_open_fds = inode->i_num_opened_fds;
1011 for (u16 i = 0; num_open_fds; i++) {
1012 if (inode->i_fds[i]) {
1013 close_wimfs_fd(inode->i_fds[i]);
1019 /* Close all file descriptors open to the mounted image. */
1021 close_all_fds(struct wimfs_context *ctx)
1023 struct wim_inode *inode, *tmp;
1024 struct wim_image_metadata *imd;
1026 imd = wim_get_current_image_metadata(ctx->wim);
1028 list_for_each_entry_safe(inode, tmp, &imd->inode_list, i_list)
1029 inode_close_fds(inode);
1032 /* Moves the currently selected image, which may have been modified, to a new
1033 * index, and sets the original index to refer to a reset (unmodified) copy of
1036 renew_current_image(struct wimfs_context *ctx)
1038 WIMStruct *wim = ctx->wim;
1039 int idx = wim->current_image - 1;
1040 struct wim_image_metadata *imd = wim->image_metadata[idx];
1041 struct wim_image_metadata *replace_imd;
1042 struct wim_lookup_table_entry *new_lte;
1045 /* Create 'replace_imd' structure to use for the reset original,
1046 * unmodified image. */
1047 ret = WIMLIB_ERR_NOMEM;
1048 replace_imd = new_image_metadata();
1052 /* Create new stream reference for the modified image's metadata
1053 * resource, which doesn't exist yet. */
1054 ret = WIMLIB_ERR_NOMEM;
1055 new_lte = new_lookup_table_entry();
1057 goto err_put_replace_imd;
1058 new_lte->flags = WIM_RESHDR_FLAG_METADATA;
1059 new_lte->unhashed = 1;
1061 /* Make the image being moved available at a new index. Increments the
1062 * WIM's image count, but does not increment the reference count of the
1063 * 'struct image_metadata'. */
1064 ret = append_image_metadata(wim, imd);
1066 goto err_free_new_lte;
1068 ret = xml_add_image(wim, "");
1070 goto err_undo_append;
1072 replace_imd->metadata_lte = imd->metadata_lte;
1073 imd->metadata_lte = new_lte;
1074 wim->image_metadata[idx] = replace_imd;
1075 wim->current_image = wim->hdr.image_count;
1079 wim->hdr.image_count--;
1081 free_lookup_table_entry(new_lte);
1082 err_put_replace_imd:
1083 put_image_metadata(replace_imd, NULL);
1088 static enum wimlib_progress_status
1089 commit_progress_func(enum wimlib_progress_msg msg,
1090 union wimlib_progress_info *info, void *progctx)
1092 mqd_t mq = *(mqd_t *)progctx;
1093 struct commit_progress_report report;
1095 memset(&report, 0, sizeof(report));
1098 report.info = *info;
1099 mq_send(mq, (const char *)&report, sizeof(report), 1);
1100 return WIMLIB_PROGRESS_STATUS_CONTINUE;
1103 /* Commit the mounted image to the underlying WIM file. */
1105 commit_image(struct wimfs_context *ctx, int unmount_flags, mqd_t mq)
1109 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_SEND_PROGRESS)
1110 wimlib_register_progress_function(ctx->wim,
1111 commit_progress_func, &mq);
1113 wimlib_register_progress_function(ctx->wim, NULL, NULL);
1115 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_NEW_IMAGE) {
1116 int ret = renew_current_image(ctx);
1120 release_extra_refcnts(ctx);
1122 INIT_LIST_HEAD(&ctx->orig_stream_list);
1123 delete_empty_streams(ctx);
1124 xml_update_image_info(ctx->wim, ctx->wim->current_image);
1128 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY)
1129 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1131 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_REBUILD)
1132 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1134 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_RECOMPRESS)
1135 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
1137 return wimlib_overwrite(ctx->wim, write_flags, 0);
1140 /* In the case of an allow_other mount, only the mount owner and root are
1141 * allowed to unmount the filesystem. */
1143 may_unmount_wimfs(void)
1145 const struct fuse_context *fuse_ctx = fuse_get_context();
1146 const struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
1148 return (fuse_ctx->uid == wimfs_ctx->owner_uid ||
1149 fuse_ctx->uid == 0);
1152 /* Unmount the mounted image, called from the daemon process. */
1156 struct fuse_context *fuse_ctx = fuse_get_context();
1157 struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
1158 const struct wimfs_unmount_info *info = &wimfs_ctx->unmount_info;
1159 int unmount_flags = info->unmount_flags;
1160 mqd_t mq = (mqd_t)-1;
1163 /* Ignore COMMIT if the image is mounted read-only. */
1164 if (!(wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE))
1165 unmount_flags &= ~WIMLIB_UNMOUNT_FLAG_COMMIT;
1167 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_SEND_PROGRESS) {
1168 mq = mq_open(info->mq_name, O_WRONLY | O_NONBLOCK);
1169 if (mq == (mqd_t)-1) {
1170 ret = WIMLIB_ERR_MQUEUE;
1175 if (wimfs_ctx->num_open_fds) {
1177 /* There are still open file descriptors to the image. */
1179 /* With COMMIT, refuse to unmount unless FORCE is also
1181 if ((unmount_flags & (WIMLIB_UNMOUNT_FLAG_COMMIT |
1182 WIMLIB_UNMOUNT_FLAG_FORCE))
1183 == WIMLIB_UNMOUNT_FLAG_COMMIT)
1185 ret = WIMLIB_ERR_MOUNTED_IMAGE_IS_BUSY;
1189 /* Force-close all file descriptors. */
1190 close_all_fds(wimfs_ctx);
1193 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_COMMIT)
1194 ret = commit_image(wimfs_ctx, unmount_flags, mq);
1196 ret = 0; /* Read-only mount, or discarding changes to
1197 a read-write mount */
1200 /* Leave the image mounted if commit failed, unless this is a
1201 * forced unmount. The user can retry without COMMIT if they
1203 if (!ret || (unmount_flags & WIMLIB_UNMOUNT_FLAG_FORCE)) {
1204 unlock_wim_for_append(wimfs_ctx->wim);
1205 fuse_exit(fuse_ctx->fuse);
1207 if (mq != (mqd_t)-1)
1213 wimfs_chmod(const char *path, mode_t mask)
1215 const struct wimfs_context *ctx = wimfs_get_context();
1216 struct wim_inode *inode;
1217 struct wimlib_unix_data unix_data;
1219 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA))
1222 inode = wim_pathname_to_inode(ctx->wim, path);
1226 unix_data.uid = ctx->owner_uid;
1227 unix_data.gid = ctx->owner_gid;
1228 unix_data.mode = mask;
1231 if (!inode_set_unix_data(inode, &unix_data, UNIX_DATA_MODE))
1238 wimfs_chown(const char *path, uid_t uid, gid_t gid)
1240 const struct wimfs_context *ctx = wimfs_get_context();
1241 struct wim_inode *inode;
1242 struct wimlib_unix_data unix_data;
1245 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA))
1248 inode = wim_pathname_to_inode(ctx->wim, path);
1254 if (uid != (uid_t)-1)
1255 which |= UNIX_DATA_UID;
1257 uid = ctx->owner_uid;
1259 if (gid != (gid_t)-1)
1260 which |= UNIX_DATA_GID;
1262 gid = ctx->owner_gid;
1264 unix_data.uid = uid;
1265 unix_data.gid = gid;
1266 unix_data.mode = inode_default_unix_mode(inode);
1269 if (!inode_set_unix_data(inode, &unix_data, which))
1276 wimfs_fgetattr(const char *path, struct stat *stbuf, struct fuse_file_info *fi)
1278 struct wimfs_fd *fd = WIMFS_FD(fi);
1279 return inode_to_stbuf(fd->f_inode, fd->f_lte, stbuf);
1283 wimfs_ftruncate(const char *path, off_t size, struct fuse_file_info *fi)
1285 struct wimfs_fd *fd = WIMFS_FD(fi);
1286 if (ftruncate(fd->f_staging_fd.fd, size))
1288 touch_inode(fd->f_inode);
1289 fd->f_lte->size = size;
1294 wimfs_getattr(const char *path, struct stat *stbuf)
1296 const struct wimfs_context *ctx = wimfs_get_context();
1297 struct wim_dentry *dentry;
1298 struct wim_lookup_table_entry *lte;
1301 ret = wim_pathname_to_stream(ctx, path, LOOKUP_FLAG_DIRECTORY_OK,
1302 &dentry, <e, NULL);
1305 return inode_to_stbuf(dentry->d_inode, lte, stbuf);
1309 copy_xattr(char *dest, size_t destsize, const void *src, size_t srcsize)
1312 if (destsize < srcsize)
1314 memcpy(dest, src, srcsize);
1320 wimfs_getxattr(const char *path, const char *name, char *value,
1323 const struct wimfs_context *ctx = wimfs_get_context();
1324 struct wim_inode *inode;
1325 struct wim_ads_entry *ads_entry;
1326 struct wim_lookup_table_entry *lte;
1328 if (!strncmp(name, "wimfs.", 6)) {
1329 /* Handle some magical extended attributes. These really should
1330 * be ioctls, but directory ioctls aren't supported until
1331 * libfuse 2.9, and even then they are broken. */
1333 if (!strcmp(name, "wim_filename")) {
1334 return copy_xattr(value, size, ctx->wim->filename,
1335 strlen(ctx->wim->filename));
1337 if (!strcmp(name, "wim_info")) {
1338 struct wimlib_wim_info info;
1340 wimlib_get_wim_info(ctx->wim, &info);
1342 return copy_xattr(value, size, &info, sizeof(info));
1344 if (!strcmp(name, "mounted_image")) {
1345 return copy_xattr(value, size,
1346 &ctx->wim->current_image, sizeof(int));
1348 if (!strcmp(name, "mount_flags")) {
1349 return copy_xattr(value, size,
1350 &ctx->mount_flags, sizeof(int));
1352 if (!strcmp(name, "unmount")) {
1353 if (!may_unmount_wimfs())
1358 if (size < sizeof(int))
1360 status = unmount_wimfs();
1361 memcpy(value, &status, sizeof(int));
1368 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1371 if (strncmp(name, "user.", 5))
1375 /* Querying a named data stream */
1377 inode = wim_pathname_to_inode(ctx->wim, path);
1381 ads_entry = inode_get_ads_entry(inode, name);
1383 return (errno == ENOENT) ? -ENOATTR : -errno;
1385 lte = ads_entry->lte;
1389 if (unlikely(lte->size > INT_MAX))
1393 if (size < lte->size)
1396 if (read_full_stream_into_buf(lte, value))
1397 return errno ? -errno : -EIO;
1403 wimfs_link(const char *existing_path, const char *new_path)
1405 WIMStruct *wim = wimfs_get_WIMStruct();
1406 const char *new_name;
1407 struct wim_inode *inode;
1408 struct wim_dentry *dir;
1409 struct wim_dentry *new_alias;
1411 inode = wim_pathname_to_inode(wim, existing_path);
1415 if (inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
1416 FILE_ATTRIBUTE_REPARSE_POINT))
1419 new_name = path_basename(new_path);
1421 dir = get_parent_dentry(wim, new_path, WIMLIB_CASE_SENSITIVE);
1425 if (!dentry_is_directory(dir))
1428 if (get_dentry_child_with_name(dir, new_name, WIMLIB_CASE_SENSITIVE))
1431 if (new_dentry(new_name, &new_alias))
1434 inode_ref_streams(inode);
1435 d_associate(new_alias, inode);
1436 dentry_add_child(dir, new_alias);
1437 touch_inode(dir->d_inode);
1442 wimfs_listxattr(const char *path, char *list, size_t size)
1444 const struct wimfs_context *ctx = wimfs_get_context();
1445 const struct wim_inode *inode;
1447 char *end = list + size;
1450 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1453 /* List named data streams, or get the list size. We report each named
1454 * data stream "X" as an extended attribute "user.X". */
1456 inode = wim_pathname_to_inode(ctx->wim, path);
1460 for (u16 i = 0; i < inode->i_num_ads; i++) {
1461 const struct wim_ads_entry *entry;
1462 char *stream_name_mbs;
1463 size_t stream_name_mbs_nbytes;
1465 entry = &inode->i_ads_entries[i];
1467 if (!entry->stream_name_nbytes)
1470 if (utf16le_to_tstr(entry->stream_name,
1471 entry->stream_name_nbytes,
1473 &stream_name_mbs_nbytes))
1476 if (unlikely(INT_MAX - total_size < stream_name_mbs_nbytes + 6)) {
1477 FREE(stream_name_mbs);
1481 total_size += stream_name_mbs_nbytes + 6;
1483 if (end - p < stream_name_mbs_nbytes + 6) {
1484 FREE(stream_name_mbs);
1487 p = mempcpy(p, "user.", 5);
1488 p = mempcpy(p, stream_name_mbs, stream_name_mbs_nbytes);
1491 FREE(stream_name_mbs);
1497 wimfs_mkdir(const char *path, mode_t mode)
1499 struct wim_dentry *dentry;
1502 /* Note: according to fuse.h, mode may not include S_IFDIR */
1503 ret = create_dentry(fuse_get_context(), path, mode | S_IFDIR, 0,
1504 FILE_ATTRIBUTE_DIRECTORY, &dentry);
1507 touch_parent(dentry);
1512 wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
1514 struct fuse_context *fuse_ctx = fuse_get_context();
1515 struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
1516 const char *stream_name;
1518 if ((wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
1519 && (stream_name = path_stream_name(path)))
1521 struct wim_ads_entry *old_entry;
1522 struct wim_ads_entry *new_entry;
1523 struct wim_inode *inode;
1526 /* Create a named data stream. */
1531 p = (char *)stream_name - 1;
1534 inode = wim_pathname_to_inode(wimfs_ctx->wim, path);
1539 old_entry = inode_get_ads_entry(inode, stream_name);
1542 if (errno != ENOENT)
1545 new_entry = inode_add_ads(inode, stream_name);
1550 /* Create a regular file, device node, named pipe, or socket.
1552 struct wim_dentry *dentry;
1555 if (!S_ISREG(mode) &&
1556 !(wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA))
1559 /* Note: we still use FILE_ATTRIBUTE_NORMAL for device nodes,
1560 * named pipes, and sockets. The real mode is in the UNIX
1562 ret = create_dentry(fuse_ctx, path, mode, rdev,
1563 FILE_ATTRIBUTE_NORMAL, &dentry);
1566 touch_parent(dentry);
1572 wimfs_open(const char *path, struct fuse_file_info *fi)
1574 struct wimfs_context *ctx = wimfs_get_context();
1575 struct wim_dentry *dentry;
1576 struct wim_inode *inode;
1577 struct wim_lookup_table_entry *lte;
1578 unsigned stream_idx;
1579 struct wimfs_fd *fd;
1582 ret = wim_pathname_to_stream(ctx, path, 0, &dentry, <e, &stream_idx);
1586 inode = dentry->d_inode;
1588 /* The file resource may be in the staging directory (read-write mounts
1589 * only) or in the WIM. If it's in the staging directory, we need to
1590 * open a native file descriptor for the corresponding file. Otherwise,
1591 * we can read the file resource directly from the WIM file if we are
1592 * opening it read-only, but we need to extract the resource to the
1593 * staging directory if we are opening it writable. */
1595 if (flags_writable(fi->flags) &&
1596 (!lte || lte->resource_location != RESOURCE_IN_STAGING_FILE)) {
1597 ret = extract_resource_to_staging_dir(inode,
1600 lte ? lte->size : 0,
1606 ret = alloc_wimfs_fd(inode, inode_stream_idx_to_id(inode, stream_idx),
1611 if (lte && lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1614 raw_fd = openat(lte->staging_dir_fd, lte->staging_file_name,
1615 (fi->flags & O_ACCMODE) | O_NOFOLLOW);
1620 filedes_init(&fd->f_staging_fd, raw_fd);
1622 fi->fh = (uintptr_t)fd;
1627 wimfs_opendir(const char *path, struct fuse_file_info *fi)
1629 WIMStruct *wim = wimfs_get_WIMStruct();
1630 struct wim_inode *inode;
1631 struct wimfs_fd *fd;
1634 inode = wim_pathname_to_inode(wim, path);
1637 if (!inode_is_directory(inode))
1639 ret = alloc_wimfs_fd(inode, 0, NULL, &fd);
1642 fi->fh = (uintptr_t)fd;
1647 wimfs_read(const char *path, char *buf, size_t size,
1648 off_t offset, struct fuse_file_info *fi)
1650 struct wimfs_fd *fd = WIMFS_FD(fi);
1651 const struct wim_lookup_table_entry *lte;
1658 if (offset >= lte->size)
1661 if (size > lte->size - offset)
1662 size = lte->size - offset;
1667 switch (lte->resource_location) {
1668 case RESOURCE_IN_WIM:
1669 if (read_partial_wim_stream_into_buf(lte, size, offset, buf))
1670 ret = errno ? -errno : -EIO;
1674 case RESOURCE_IN_STAGING_FILE:
1675 ret = raw_pread(&fd->f_staging_fd, buf, size, offset);
1679 case RESOURCE_IN_ATTACHED_BUFFER:
1680 memcpy(buf, lte->attached_buffer + offset, size);
1691 wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
1692 off_t offset, struct fuse_file_info *fi)
1694 struct wimfs_fd *fd = WIMFS_FD(fi);
1695 const struct wim_inode *inode;
1696 const struct wim_dentry *child;
1699 inode = fd->f_inode;
1701 ret = filler(buf, ".", NULL, 0);
1704 ret = filler(buf, "..", NULL, 0);
1708 for_inode_child(child, inode) {
1709 char *file_name_mbs;
1710 size_t file_name_mbs_nbytes;
1712 ret = utf16le_to_tstr(child->file_name,
1713 child->file_name_nbytes,
1715 &file_name_mbs_nbytes);
1719 ret = filler(buf, file_name_mbs, NULL, 0);
1720 FREE(file_name_mbs);
1728 wimfs_readlink(const char *path, char *buf, size_t buf_len)
1730 WIMStruct *wim = wimfs_get_WIMStruct();
1731 const struct wim_inode *inode;
1734 inode = wim_pathname_to_inode(wim, path);
1737 if (!inode_is_symlink(inode))
1741 ret = wim_inode_readlink(inode, buf, buf_len - 1, NULL);
1745 } else if (ret == -ENAMETOOLONG) {
1746 buf[buf_len - 1] = '\0';
1751 /* We use this for both release() and releasedir(), since in both cases we
1752 * simply need to close the file descriptor. */
1754 wimfs_release(const char *path, struct fuse_file_info *fi)
1756 return close_wimfs_fd(WIMFS_FD(fi));
1760 wimfs_removexattr(const char *path, const char *name)
1762 struct wimfs_context *ctx = wimfs_get_context();
1763 struct wim_inode *inode;
1764 struct wim_ads_entry *ads_entry;
1766 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1769 if (strncmp(name, "user.", 5))
1773 /* Removing a named data stream. */
1775 inode = wim_pathname_to_inode(ctx->wim, path);
1779 ads_entry = inode_get_ads_entry(inode, name);
1781 return (errno == ENOENT) ? -ENOATTR : -errno;
1783 inode_remove_ads(inode, ads_entry, ctx->wim->lookup_table);
1788 wimfs_rename(const char *from, const char *to)
1790 return rename_wim_path(wimfs_get_WIMStruct(), from, to,
1791 WIMLIB_CASE_SENSITIVE, NULL);
1795 wimfs_rmdir(const char *path)
1797 WIMStruct *wim = wimfs_get_WIMStruct();
1798 struct wim_dentry *dentry;
1800 dentry = get_dentry(wim, path, WIMLIB_CASE_SENSITIVE);
1804 if (!dentry_is_directory(dentry))
1807 if (dentry_has_children(dentry))
1810 touch_parent(dentry);
1811 remove_dentry(dentry, wim->lookup_table);
1816 wimfs_setxattr(const char *path, const char *name,
1817 const char *value, size_t size, int flags)
1819 struct wimfs_context *ctx = wimfs_get_context();
1820 struct wim_inode *inode;
1821 struct wim_ads_entry *existing_entry;
1823 if (!strncmp(name, "wimfs.", 6)) {
1824 /* Handle some magical extended attributes. These really should
1825 * be ioctls, but directory ioctls aren't supported until
1826 * libfuse 2.9, and even then they are broken. [Fixed by
1827 * libfuse commit e3b7d4c278a26520be63d99d6ea84b26906fe73d] */
1829 if (!strcmp(name, "unmount_info")) {
1830 if (!may_unmount_wimfs())
1832 if (size < sizeof(struct wimfs_unmount_info))
1834 memcpy(&ctx->unmount_info, value,
1835 sizeof(struct wimfs_unmount_info));
1841 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1844 if (strncmp(name, "user.", 5))
1848 /* Setting the contents of a named data stream. */
1850 inode = wim_pathname_to_inode(ctx->wim, path);
1854 existing_entry = inode_get_ads_entry(inode, name);
1855 if (existing_entry) {
1856 if (flags & XATTR_CREATE)
1859 if (errno != ENOENT)
1861 if (flags & XATTR_REPLACE)
1865 if (!inode_add_ads_with_data(inode, name, value,
1866 size, ctx->wim->lookup_table))
1869 inode_remove_ads(inode, existing_entry, ctx->wim->lookup_table);
1874 wimfs_symlink(const char *to, const char *from)
1876 struct fuse_context *fuse_ctx = fuse_get_context();
1877 struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
1878 struct wim_dentry *dentry;
1881 ret = create_dentry(fuse_ctx, from, S_IFLNK | 0777, 0,
1882 FILE_ATTRIBUTE_REPARSE_POINT, &dentry);
1885 dentry->d_inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
1886 ret = wim_inode_set_symlink(dentry->d_inode, to,
1887 wimfs_ctx->wim->lookup_table);
1889 remove_dentry(dentry, wimfs_ctx->wim->lookup_table);
1890 if (ret == WIMLIB_ERR_NOMEM)
1895 touch_parent(dentry);
1901 wimfs_truncate(const char *path, off_t size)
1903 const struct wimfs_context *ctx = wimfs_get_context();
1904 struct wim_dentry *dentry;
1905 struct wim_lookup_table_entry *lte;
1906 unsigned stream_idx;
1910 ret = wim_pathname_to_stream(ctx, path, 0, &dentry, <e, &stream_idx);
1917 if (!lte || lte->resource_location != RESOURCE_IN_STAGING_FILE) {
1918 return extract_resource_to_staging_dir(dentry->d_inode,
1923 /* Truncate the staging file. */
1924 fd = openat(lte->staging_dir_fd, lte->staging_file_name,
1925 O_WRONLY | O_NOFOLLOW);
1928 ret = ftruncate(fd, size);
1929 if (close(fd) || ret)
1936 wimfs_unlink(const char *path)
1938 const struct wimfs_context *ctx = wimfs_get_context();
1939 struct wim_dentry *dentry;
1940 unsigned stream_idx;
1943 ret = wim_pathname_to_stream(ctx, path, 0, &dentry, NULL, &stream_idx);
1947 if (inode_stream_name_nbytes(dentry->d_inode, stream_idx) == 0) {
1948 touch_parent(dentry);
1949 remove_dentry(dentry, ctx->wim->lookup_table);
1951 inode_remove_ads(dentry->d_inode,
1952 &dentry->d_inode->i_ads_entries[stream_idx - 1],
1953 ctx->wim->lookup_table);
1958 #ifdef HAVE_UTIMENSAT
1960 * Change the timestamp on a file dentry.
1962 * Note that alternate data streams do not have their own timestamps.
1965 wimfs_utimens(const char *path, const struct timespec tv[2])
1967 WIMStruct *wim = wimfs_get_WIMStruct();
1968 struct wim_inode *inode;
1970 inode = wim_pathname_to_inode(wim, path);
1974 if (tv[0].tv_nsec != UTIME_OMIT) {
1975 if (tv[0].tv_nsec == UTIME_NOW)
1976 inode->i_last_access_time = now_as_wim_timestamp();
1978 inode->i_last_access_time = timespec_to_wim_timestamp(&tv[0]);
1980 if (tv[1].tv_nsec != UTIME_OMIT) {
1981 if (tv[1].tv_nsec == UTIME_NOW)
1982 inode->i_last_write_time = now_as_wim_timestamp();
1984 inode->i_last_write_time = timespec_to_wim_timestamp(&tv[1]);
1988 #else /* HAVE_UTIMENSAT */
1990 wimfs_utime(const char *path, struct utimbuf *times)
1992 WIMStruct *wim = wimfs_get_WIMStruct();
1993 struct wim_inode *inode;
1995 inode = wim_pathname_to_inode(wim, path);
1999 inode->i_last_access_time = time_t_to_wim_timestamp(times->actime);
2000 inode->i_last_write_time = time_t_to_wim_timestamp(times->modtime);
2003 #endif /* !HAVE_UTIMENSAT */
2006 wimfs_write(const char *path, const char *buf, size_t size,
2007 off_t offset, struct fuse_file_info *fi)
2009 struct wimfs_fd *fd = WIMFS_FD(fi);
2012 ret = raw_pwrite(&fd->f_staging_fd, buf, size, offset);
2016 if (offset + size > fd->f_lte->size)
2017 fd->f_lte->size = offset + size;
2019 touch_inode(fd->f_inode);
2023 static struct fuse_operations wimfs_operations = {
2024 .chmod = wimfs_chmod,
2025 .chown = wimfs_chown,
2026 .fgetattr = wimfs_fgetattr,
2027 .ftruncate = wimfs_ftruncate,
2028 .getattr = wimfs_getattr,
2029 .getxattr = wimfs_getxattr,
2031 .listxattr = wimfs_listxattr,
2032 .mkdir = wimfs_mkdir,
2033 .mknod = wimfs_mknod,
2035 .opendir = wimfs_opendir,
2037 .readdir = wimfs_readdir,
2038 .readlink = wimfs_readlink,
2039 .release = wimfs_release,
2040 .releasedir = wimfs_release,
2041 .removexattr = wimfs_removexattr,
2042 .rename = wimfs_rename,
2043 .rmdir = wimfs_rmdir,
2044 .setxattr = wimfs_setxattr,
2045 .symlink = wimfs_symlink,
2046 .truncate = wimfs_truncate,
2047 .unlink = wimfs_unlink,
2048 #ifdef HAVE_UTIMENSAT
2049 .utimens = wimfs_utimens,
2051 .utime = wimfs_utime,
2053 .write = wimfs_write,
2055 /* We keep track of file descriptor structures (struct wimfs_fd), so
2056 * there is no need to have the file path provided on operations such as
2058 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 8)
2059 .flag_nullpath_ok = 1,
2061 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 9)
2063 .flag_utime_omit_ok = 1,
2067 /* API function documented in wimlib.h */
2069 wimlib_mount_image(WIMStruct *wim, int image, const char *dir,
2070 int mount_flags, const char *staging_dir)
2073 struct wim_image_metadata *imd;
2074 struct wimfs_context ctx;
2075 char *fuse_argv[16];
2078 if (!wim || !dir || !*dir)
2079 return WIMLIB_ERR_INVALID_PARAM;
2081 if (mount_flags & ~(WIMLIB_MOUNT_FLAG_READWRITE |
2082 WIMLIB_MOUNT_FLAG_DEBUG |
2083 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
2084 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
2085 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS |
2086 WIMLIB_MOUNT_FLAG_UNIX_DATA |
2087 WIMLIB_MOUNT_FLAG_ALLOW_OTHER))
2088 return WIMLIB_ERR_INVALID_PARAM;
2090 /* For read-write mount, check for write access to the WIM. */
2091 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2093 return WIMLIB_ERR_NO_FILENAME;
2094 ret = can_modify_wim(wim);
2099 /* Select the image to mount. */
2100 ret = select_wim_image(wim, image);
2104 /* Get the metadata for the image to mount. */
2105 imd = wim_get_current_image_metadata(wim);
2107 if (imd->modified) {
2108 /* To avoid complicating things, we don't support mounting
2109 * images to which in-memory modifications have already been
2111 ERROR("Cannot mount a modified WIM image!");
2112 return WIMLIB_ERR_INVALID_PARAM;
2115 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2116 ret = lock_wim_for_append(wim);
2121 /* If the user did not specify an interface for accessing named
2122 * data streams, use the default (extended attributes). */
2123 if (!(mount_flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
2124 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
2125 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
2126 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
2128 /* Start initializing the wimfs_context. */
2129 memset(&ctx, 0, sizeof(struct wimfs_context));
2131 ctx.mount_flags = mount_flags;
2132 if (mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
2133 ctx.default_lookup_flags = LOOKUP_FLAG_ADS_OK;
2134 /* For read-write mount, create the staging directory. */
2135 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2136 ret = make_staging_dir(&ctx, staging_dir);
2140 ctx.owner_uid = getuid();
2141 ctx.owner_gid = getgid();
2143 /* Add each stream referenced by files in the image to a list and
2144 * preemptively double the number of references to each. This is done
2145 * to allow implementing the WIMLIB_UNMOUNT_FLAG_NEW_IMAGE semantics.
2147 INIT_LIST_HEAD(&ctx.orig_stream_list);
2148 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2150 struct wim_inode *inode;
2151 struct wim_lookup_table_entry *lte;
2153 image_for_each_inode(inode, imd) {
2154 for (i = 0; i <= inode->i_num_ads; i++) {
2155 lte = inode_stream_lte(inode, i,
2158 lte->out_refcnt = 0;
2162 image_for_each_inode(inode, imd) {
2163 for (i = 0; i <= inode->i_num_ads; i++) {
2164 lte = inode_stream_lte(inode, i,
2167 if (lte->out_refcnt == 0)
2168 list_add(<e->orig_stream_list,
2169 &ctx.orig_stream_list);
2170 lte->out_refcnt += inode->i_nlink;
2171 lte->refcnt += inode->i_nlink;
2177 /* Assign new inode numbers. */
2178 reassign_inode_numbers(&ctx);
2180 /* If a read-write mount, mark the image as modified. */
2181 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2184 /* Build the FUSE command line. */
2187 fuse_argv[fuse_argc++] = "wimlib";
2188 fuse_argv[fuse_argc++] = (char *)dir;
2190 /* Disable multi-threaded operation. */
2191 fuse_argv[fuse_argc++] = "-s";
2193 /* Enable FUSE debug mode (don't fork) if requested by the user. */
2194 if (mount_flags & WIMLIB_MOUNT_FLAG_DEBUG)
2195 fuse_argv[fuse_argc++] = "-d";
2198 * Build the FUSE mount options:
2201 * FUSE will use the inode numbers we provide. We want this,
2202 * because we have inodes and will number them ourselves.
2205 * Name for our filesystem (main type is "fuse").
2208 * If an open file is unlinked, unlink it for real rather than
2209 * renaming it to a hidden file. Our code supports this; an
2210 * unlinked inode is retained until all its file descriptors have
2213 * default_permissions
2214 * FUSE will perform permission checking. Useful when
2215 * WIMLIB_MOUNT_FLAG_UNIX_DATA is provided and the WIM image
2216 * contains the UNIX permissions for each file.
2219 * Cache the contents of files. This will speed up repeated access
2220 * to files on a mounted WIM image, since they won't need to be
2221 * decompressed repeatedly. This option is valid because data in
2222 * the WIM image should never be changed externally. (Although, if
2223 * someone really wanted to they could modify the WIM file or mess
2224 * with the staging directory; but then they're asking for
2227 * entry_timeout=1000000000
2228 * Cache positive name lookups indefinitely, since names can only
2229 * be added, removed, or modified through the mounted filesystem
2232 * negative_timeout=1000000000
2233 * Cache negative name lookups indefinitely, since names can only
2234 * be added, removed, or modified through the mounted filesystem
2238 * Don't cache file/directory attributes. This is needed as a
2239 * workaround for the fact that when caching attributes, the high
2240 * level interface to libfuse considers a file which has several
2241 * hard-linked names as several different files. (Otherwise, we
2242 * could cache our file/directory attributes indefinitely, since
2243 * they can only be changed through the mounted filesystem itself.)
2245 char optstring[256] =
2249 ",default_permissions"
2251 ",entry_timeout=1000000000"
2252 ",negative_timeout=1000000000"
2255 fuse_argv[fuse_argc++] = "-o";
2256 fuse_argv[fuse_argc++] = optstring;
2257 if (!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE))
2258 strcat(optstring, ",ro");
2259 if (mount_flags & WIMLIB_MOUNT_FLAG_ALLOW_OTHER)
2260 strcat(optstring, ",allow_other");
2261 fuse_argv[fuse_argc] = NULL;
2263 /* Mount our filesystem. */
2264 ret = fuse_main(fuse_argc, fuse_argv, &wimfs_operations, &ctx);
2266 /* Cleanup and return. */
2268 ret = WIMLIB_ERR_FUSE;
2269 release_extra_refcnts(&ctx);
2270 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2271 delete_staging_dir(&ctx);
2273 unlock_wim_for_append(wim);
2277 struct commit_progress_thread_args {
2279 wimlib_progress_func_t progfunc;
2284 commit_progress_thread_proc(void *_args)
2286 struct commit_progress_thread_args *args = _args;
2287 struct commit_progress_report report;
2291 ret = mq_receive(args->mq,
2292 (char *)&report, sizeof(report), NULL);
2293 if (ret == sizeof(report)) {
2294 call_progress(args->progfunc, report.msg,
2295 &report.info, args->progctx);
2297 if (ret == 0 || (ret < 0 && errno != EINTR))
2305 generate_message_queue_name(char name[WIMFS_MQUEUE_NAME_LEN + 1])
2308 memcpy(name + 1, "wimfs-", 6);
2309 randomize_char_array_with_alnum(name + 7, WIMFS_MQUEUE_NAME_LEN - 7);
2310 name[WIMFS_MQUEUE_NAME_LEN] = '\0';
2314 create_message_queue(const char *name)
2319 struct mq_attr attr;
2322 memset(&attr, 0, sizeof(attr));
2324 attr.mq_msgsize = sizeof(struct commit_progress_report);
2326 am_root = (geteuid() == 0);
2328 /* Filesystem mounted as normal user with --allow-other should
2329 * be able to send messages to root user, if they're doing the
2331 umask_save = umask(0);
2336 mq = mq_open(name, O_RDWR | O_CREAT | O_EXCL, mode, &attr);
2342 /* Unmount a read-only or read-write mounted WIM image. */
2344 do_unmount(const char *dir)
2349 len = getxattr(dir, "wimfs.unmount", &status, sizeof(int));
2350 if (len == sizeof(int))
2352 else if (len < 0 && (errno == EACCES || errno == EPERM))
2353 return WIMLIB_ERR_NOT_PERMITTED_TO_UNMOUNT;
2355 return WIMLIB_ERR_NOT_A_MOUNTPOINT;
2359 set_unmount_info(const char *dir, const struct wimfs_unmount_info *unmount_info)
2361 if (!setxattr(dir, "wimfs.unmount_info",
2362 unmount_info, sizeof(struct wimfs_unmount_info), 0))
2364 else if (errno == EROFS)
2366 else if (errno == EACCES || errno == EPERM)
2367 return WIMLIB_ERR_NOT_PERMITTED_TO_UNMOUNT;
2369 return WIMLIB_ERR_NOT_A_MOUNTPOINT;
2373 do_unmount_discard(const char *dir)
2376 struct wimfs_unmount_info unmount_info;
2378 memset(&unmount_info, 0, sizeof(unmount_info));
2380 ret = set_unmount_info(dir, &unmount_info);
2383 return do_unmount(dir);
2386 /* Unmount a read-write mounted WIM image, committing the changes. */
2388 do_unmount_commit(const char *dir, int unmount_flags,
2389 wimlib_progress_func_t progfunc, void *progctx)
2391 struct wimfs_unmount_info unmount_info;
2393 struct commit_progress_thread_args args;
2394 pthread_t commit_progress_tid;
2397 memset(&unmount_info, 0, sizeof(unmount_info));
2398 unmount_info.unmount_flags = unmount_flags;
2400 /* The current thread will be stuck in getxattr() until the image is
2401 * committed. Create a thread to handle the progress messages. */
2403 generate_message_queue_name(unmount_info.mq_name);
2405 mq = create_message_queue(unmount_info.mq_name);
2406 if (mq == (mqd_t)-1) {
2407 ERROR_WITH_ERRNO("Can't create POSIX message queue");
2408 return WIMLIB_ERR_MQUEUE;
2411 args.progfunc = progfunc;
2412 args.progctx = progctx;
2413 ret = pthread_create(&commit_progress_tid, NULL,
2414 commit_progress_thread_proc, &args);
2417 ERROR_WITH_ERRNO("Can't create thread");
2418 ret = WIMLIB_ERR_NOMEM;
2421 unmount_info.unmount_flags |= WIMLIB_UNMOUNT_FLAG_SEND_PROGRESS;
2424 ret = set_unmount_info(dir, &unmount_info);
2426 ret = do_unmount(dir);
2428 /* Terminate the progress thread. */
2430 mq_send(mq, empty, 0, 1);
2431 pthread_join(commit_progress_tid, NULL);
2436 mq_unlink(unmount_info.mq_name);
2442 begin_unmount(const char *dir, int unmount_flags, int *mount_flags_ret,
2443 wimlib_progress_func_t progfunc, void *progctx)
2447 int wim_filename_len;
2448 union wimlib_progress_info progress;
2450 if (getxattr(dir, "wimfs.mount_flags",
2451 &mount_flags, sizeof(int)) != sizeof(int))
2452 return WIMLIB_ERR_NOT_A_MOUNTPOINT;
2454 *mount_flags_ret = mount_flags;
2459 if (getxattr(dir, "wimfs.mounted_image",
2460 &mounted_image, sizeof(int)) != sizeof(int))
2461 return WIMLIB_ERR_NOT_A_MOUNTPOINT;
2463 wim_filename_len = getxattr(dir, "wimfs.wim_filename", NULL, 0);
2464 if (wim_filename_len < 0)
2465 return WIMLIB_ERR_NOT_A_MOUNTPOINT;
2467 char wim_filename[wim_filename_len + 1];
2468 if (getxattr(dir, "wimfs.wim_filename",
2469 wim_filename, wim_filename_len) != wim_filename_len)
2470 return WIMLIB_ERR_NOT_A_MOUNTPOINT;
2471 wim_filename[wim_filename_len] = '\0';
2473 progress.unmount.mountpoint = dir;
2474 progress.unmount.mounted_wim = wim_filename;
2475 progress.unmount.mounted_image = mounted_image;
2476 progress.unmount.mount_flags = mount_flags;
2477 progress.unmount.unmount_flags = unmount_flags;
2479 return call_progress(progfunc, WIMLIB_PROGRESS_MSG_UNMOUNT_BEGIN,
2480 &progress, progctx);
2483 /* API function documented in wimlib.h */
2485 wimlib_unmount_image_with_progress(const char *dir, int unmount_flags,
2486 wimlib_progress_func_t progfunc, void *progctx)
2491 ret = wimlib_global_init(WIMLIB_INIT_FLAG_ASSUME_UTF8);
2495 if (unmount_flags & ~(WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY |
2496 WIMLIB_UNMOUNT_FLAG_COMMIT |
2497 WIMLIB_UNMOUNT_FLAG_REBUILD |
2498 WIMLIB_UNMOUNT_FLAG_RECOMPRESS |
2499 WIMLIB_UNMOUNT_FLAG_FORCE |
2500 WIMLIB_UNMOUNT_FLAG_NEW_IMAGE))
2501 return WIMLIB_ERR_INVALID_PARAM;
2503 ret = begin_unmount(dir, unmount_flags, &mount_flags,
2508 if ((unmount_flags & WIMLIB_UNMOUNT_FLAG_COMMIT) &&
2509 (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE))
2510 return do_unmount_commit(dir, unmount_flags,
2513 return do_unmount_discard(dir);
2516 #else /* WITH_FUSE */
2520 mount_unsupported_error(void)
2522 #if defined(__WIN32__)
2523 ERROR("Sorry-- Mounting WIM images is not supported on Windows!");
2525 ERROR("wimlib was compiled with --without-fuse, which disables support "
2526 "for mounting WIMs.");
2528 return WIMLIB_ERR_UNSUPPORTED;
2532 wimlib_unmount_image_with_progress(const tchar *dir, int unmount_flags,
2533 wimlib_progress_func_t progfunc, void *progctx)
2535 return mount_unsupported_error();
2539 wimlib_mount_image(WIMStruct *wim, int image, const tchar *dir,
2540 int mount_flags, const tchar *staging_dir)
2542 return mount_unsupported_error();
2545 #endif /* !WITH_FUSE */
2548 wimlib_unmount_image(const tchar *dir, int unmount_flags)
2550 return wimlib_unmount_image_with_progress(dir, unmount_flags, NULL, NULL);