4 * This file implements mounting of WIM files using FUSE, which stands for
5 * Filesystem in Userspace. FUSE allows a filesystem to be implemented in a
6 * userspace process by implementing the filesystem primitives--- read(),
7 * write(), readdir(), etc.
11 * Copyright (C) 2012, 2013 Eric Biggers
13 * This file is part of wimlib, a library for working with WIM files.
15 * wimlib is free software; you can redistribute it and/or modify it under the
16 * terms of the GNU General Public License as published by the Free
17 * Software Foundation; either version 3 of the License, or (at your option)
20 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
21 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
22 * A PARTICULAR PURPOSE. See the GNU General Public License for more
25 * You should have received a copy of the GNU General Public License
26 * along with wimlib; if not, see http://www.gnu.org/licenses/.
34 #include "wimlib/error.h"
39 # error "FUSE mount not supported on Win32! Please configure --without-fuse"
42 #include "wimlib/encoding.h"
43 #include "wimlib/file_io.h"
44 #include "wimlib/dentry.h"
45 #include "wimlib/inode.h"
46 #include "wimlib/lookup_table.h"
47 #include "wimlib/metadata.h"
48 #include "wimlib/paths.h"
49 #include "wimlib/reparse.h"
50 #include "wimlib/resource.h"
51 #include "wimlib/timestamp.h"
52 #include "wimlib/version.h"
53 #include "wimlib/write.h"
54 #include "wimlib/xml.h"
65 #include <sys/types.h>
70 #define FUSE_USE_VERSION 26
74 #include <attr/xattr.h>
77 #define MSG_VERSION_TOO_HIGH -1
78 #define MSG_BREAK_LOOP -2
80 /* File descriptor to a file open on the WIM filesystem. */
82 struct wim_inode *f_inode;
83 struct wim_lookup_table_entry *f_lte;
84 struct filedes staging_fd;
89 struct wimfs_context {
90 /* The WIMStruct for the mounted WIM. */
93 /* Name of the staging directory for a read-write mount. Whenever a new file is
94 * created, it is done so in the staging directory. Furthermore, whenever a
95 * file in the WIM is modified, it is extracted to the staging directory. If
96 * changes are commited when the WIM is unmounted, the file resources are merged
97 * in from the staging directory when writing the new WIM. */
98 char *staging_dir_name;
99 size_t staging_dir_name_len;
101 /* Flags passed to wimlib_mount(). */
104 /* Default flags to use when looking up a WIM dentry (depends on whether
105 * the Windows interface to alternate data streams is being used or
107 int default_lookup_flags;
109 /* Next inode number to be assigned. Note: I didn't bother with a
110 * bitmap of free inode numbers since this isn't even a "real"
111 * filesystem anyway. */
114 /* List of inodes in the mounted image */
115 struct list_head *image_inode_list;
117 /* Name and message queue descriptors for message queues between the
118 * filesystem daemon process and the unmount process. These are used
119 * when the filesystem is unmounted and the process running
120 * wimlib_unmount_image() needs to communicate with the filesystem
121 * daemon running fuse_main() (i.e. the process created by a call to
122 * wimlib_mount_image(). */
123 char *unmount_to_daemon_mq_name;
124 char *daemon_to_unmount_mq_name;
125 mqd_t unmount_to_daemon_mq;
126 mqd_t daemon_to_unmount_mq;
136 init_wimfs_context(struct wimfs_context *ctx)
138 memset(ctx, 0, sizeof(*ctx));
139 ctx->unmount_to_daemon_mq = (mqd_t)-1;
140 ctx->daemon_to_unmount_mq = (mqd_t)-1;
143 #define WIMFS_CTX(fuse_ctx) ((struct wimfs_context*)(fuse_ctx)->private_data)
145 static inline struct wimfs_context *
146 wimfs_get_context(void)
148 return WIMFS_CTX(fuse_get_context());
151 static inline WIMStruct *
152 wimfs_get_WIMStruct(void)
154 return wimfs_get_context()->wim;
158 get_lookup_flags(const struct wimfs_context *ctx)
160 return ctx->default_lookup_flags;
163 /* Returns nonzero if write permission is requested on the file open flags */
165 flags_writable(int open_flags)
167 return open_flags & (O_RDWR | O_WRONLY);
171 * Allocate a file descriptor for a stream.
173 * @inode: inode containing the stream we're opening
174 * @stream_id: ID of the stream we're opening
175 * @lte: Lookup table entry for the stream (may be NULL)
176 * @fd_ret: Return the allocated file descriptor if successful.
178 * Return 0 iff successful or error code if unsuccessful.
181 alloc_wimfs_fd(struct wim_inode *inode,
183 struct wim_lookup_table_entry *lte,
184 struct wimfs_fd **fd_ret)
186 static const u16 fds_per_alloc = 8;
187 static const u16 max_fds = 0xffff;
190 DEBUG("Allocating fd for stream ID %u from inode %#"PRIx64" "
191 "(open = %u, allocated = %u)",
192 stream_id, inode->i_ino, inode->i_num_opened_fds,
193 inode->i_num_allocated_fds);
195 if (inode->i_num_opened_fds == inode->i_num_allocated_fds) {
196 struct wimfs_fd **fds;
199 if (inode->i_num_allocated_fds == max_fds) {
203 num_new_fds = min(fds_per_alloc,
204 max_fds - inode->i_num_allocated_fds);
206 fds = REALLOC(inode->i_fds,
207 (inode->i_num_allocated_fds + num_new_fds) *
208 sizeof(inode->i_fds[0]));
213 memset(&fds[inode->i_num_allocated_fds], 0,
214 num_new_fds * sizeof(fds[0]));
216 inode->i_num_allocated_fds += num_new_fds;
218 for (u16 i = 0; ; i++) {
219 if (!inode->i_fds[i]) {
220 struct wimfs_fd *fd = CALLOC(1, sizeof(*fd));
227 filedes_invalidate(&fd->staging_fd);
229 fd->stream_id = stream_id;
231 inode->i_fds[i] = fd;
232 inode->i_num_opened_fds++;
234 lte->num_opened_fds++;
235 DEBUG("Allocated fd (idx = %u)", fd->idx);
245 inode_put_fd(struct wim_inode *inode, struct wimfs_fd *fd)
247 wimlib_assert(inode != NULL);
248 wimlib_assert(fd->f_inode == inode);
249 wimlib_assert(inode->i_num_opened_fds != 0);
250 wimlib_assert(fd->idx < inode->i_num_allocated_fds);
251 wimlib_assert(inode->i_fds[fd->idx] == fd);
253 inode->i_fds[fd->idx] = NULL;
255 if (--inode->i_num_opened_fds == 0) {
258 inode->i_num_allocated_fds = 0;
259 if (inode->i_nlink == 0)
265 lte_put_fd(struct wim_lookup_table_entry *lte, struct wimfs_fd *fd)
267 wimlib_assert(fd->f_lte == lte);
269 if (!lte) /* Empty stream with no lookup table entry */
272 /* Close staging file descriptor if needed. */
274 if (lte->resource_location == RESOURCE_IN_STAGING_FILE
275 && filedes_valid(&fd->staging_fd))
277 if (filedes_close(&fd->staging_fd)) {
278 ERROR_WITH_ERRNO("Failed to close staging file");
282 lte_decrement_num_opened_fds(lte);
286 /* Close a file descriptor. */
288 close_wimfs_fd(struct wimfs_fd *fd)
291 DEBUG("Closing fd (ino = %#"PRIx64", opened = %u, allocated = %u)",
292 fd->f_inode->i_ino, fd->f_inode->i_num_opened_fds,
293 fd->f_inode->i_num_allocated_fds);
294 ret = lte_put_fd(fd->f_lte, fd);
298 inode_put_fd(fd->f_inode, fd);
303 fuse_mask_mode(mode_t mode, struct fuse_context *fuse_ctx)
305 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 8)
306 mode &= ~fuse_ctx->umask;
312 * Add a new dentry with a new inode to a WIM image.
314 * Returns 0 on success, or negative error number on failure.
317 create_dentry(struct fuse_context *fuse_ctx, const char *path,
318 mode_t mode, int attributes, struct wim_dentry **dentry_ret)
320 struct wim_dentry *parent;
321 struct wim_dentry *new;
322 const char *basename;
323 struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
326 parent = get_parent_dentry(wimfs_ctx->wim, path, WIMLIB_CASE_SENSITIVE);
330 if (!dentry_is_directory(parent))
333 basename = path_basename(path);
334 if (get_dentry_child_with_name(parent, basename, WIMLIB_CASE_SENSITIVE))
337 ret = new_dentry_with_inode(basename, &new);
341 new->d_inode->i_resolved = 1;
342 new->d_inode->i_ino = wimfs_ctx->next_ino++;
343 new->d_inode->i_attributes = attributes;
345 if (wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA) {
346 if (inode_set_unix_data(new->d_inode,
349 fuse_mask_mode(mode, fuse_ctx),
350 wimfs_ctx->wim->lookup_table,
351 UNIX_DATA_ALL | UNIX_DATA_CREATE))
357 dentry_add_child(parent, new);
358 list_add_tail(&new->d_inode->i_list, wimfs_ctx->image_inode_list);
364 static struct wim_inode *
365 wim_pathname_to_inode(WIMStruct *wim, const tchar *path)
367 struct wim_dentry *dentry;
368 dentry = get_dentry(wim, path, WIMLIB_CASE_SENSITIVE);
370 return dentry->d_inode;
375 /* Remove a dentry from a mounted WIM image; i.e. remove an alias for the
376 * corresponding inode.
378 * If there are no remaining references to the inode either through dentries or
379 * open file descriptors, the inode is freed. Otherwise, the inode is not
380 * removed, but the dentry is unlinked and freed.
382 * Either way, all lookup table entries referenced by the inode have their
383 * reference count decremented. If a lookup table entry has no open file
384 * descriptors and no references remaining, it is freed, and the corresponding
385 * staging file is unlinked.
388 remove_dentry(struct wim_dentry *dentry,
389 struct wim_lookup_table *lookup_table)
391 struct wim_inode *inode = dentry->d_inode;
392 struct wim_lookup_table_entry *lte;
395 for (i = 0; i <= inode->i_num_ads; i++) {
396 lte = inode_stream_lte_resolved(inode, i);
398 lte_decrement_refcnt(lte, lookup_table);
400 unlink_dentry(dentry);
405 inode_default_unix_mode(const struct wim_inode *inode)
407 if (inode_is_symlink(inode))
408 return S_IFLNK | 0777;
409 else if (inode_is_directory(inode))
410 return S_IFDIR | 0777;
412 return S_IFREG | 0777;
415 /* Transfers file attributes from a struct wim_inode to a `stat' buffer.
417 * The lookup table entry tells us which stream in the inode we are statting.
418 * For a named data stream, everything returned is the same as the unnamed data
419 * stream except possibly the size and block count. */
421 inode_to_stbuf(const struct wim_inode *inode,
422 const struct wim_lookup_table_entry *lte,
425 const struct wimfs_context *ctx = wimfs_get_context();
427 memset(stbuf, 0, sizeof(struct stat));
428 stbuf->st_mode = inode_default_unix_mode(inode);
429 stbuf->st_uid = ctx->default_uid;
430 stbuf->st_gid = ctx->default_gid;
431 if (ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA) {
432 struct wimlib_unix_data unix_data;
433 if (inode_get_unix_data(inode, &unix_data, NULL) == 0) {
434 stbuf->st_uid = unix_data.uid;
435 stbuf->st_gid = unix_data.gid;
436 stbuf->st_mode = unix_data.mode;
439 stbuf->st_ino = (ino_t)inode->i_ino;
440 stbuf->st_nlink = inode->i_nlink;
442 stbuf->st_size = lte->size;
445 #ifdef HAVE_STAT_NANOSECOND_PRECISION
446 stbuf->st_atim = wim_timestamp_to_timespec(inode->i_last_access_time);
447 stbuf->st_mtim = wim_timestamp_to_timespec(inode->i_last_write_time);
448 stbuf->st_ctim = stbuf->st_mtim;
450 stbuf->st_atime = wim_timestamp_to_unix(inode->i_last_access_time);
451 stbuf->st_mtime = wim_timestamp_to_unix(inode->i_last_write_time);
452 stbuf->st_ctime = stbuf->st_mtime;
454 stbuf->st_blocks = (stbuf->st_size + 511) / 512;
459 touch_inode(struct wim_inode *inode)
461 u64 now = get_wim_timestamp();
462 inode->i_last_access_time = now;
463 inode->i_last_write_time = now;
466 /* Creates a new staging file and returns its file descriptor opened for
469 * @name_ret: A location into which the a pointer to the newly allocated name of
470 * the staging file is stored.
472 * @ctx: Context for the WIM filesystem; this provides the name of the
475 * On success, returns the file descriptor for the staging file, opened for
476 * writing. On failure, returns -1 and sets errno.
479 create_staging_file(char **name_ret, struct wimfs_context *ctx)
487 static const size_t STAGING_FILE_NAME_LEN = 20;
489 name_len = ctx->staging_dir_name_len + 1 + STAGING_FILE_NAME_LEN;
490 name = MALLOC(name_len + 1);
498 memcpy(name, ctx->staging_dir_name, ctx->staging_dir_name_len);
499 name[ctx->staging_dir_name_len] = '/';
500 randomize_char_array_with_alnum(name + ctx->staging_dir_name_len + 1,
501 STAGING_FILE_NAME_LEN);
502 name[name_len] = '\0';
505 /* Just in case, verify that the randomly generated name doesn't name an
506 * existing file, and try again if so */
507 } while (stat(name, &stbuf) == 0);
509 if (errno != ENOENT) /* other error?! */
512 /* doesn't exist--- ok */
514 DEBUG("Creating staging file `%s'", name);
516 fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0600);
528 * Extract a WIM resource to the staging directory.
530 * @inode: Inode that contains the stream we are extracting
532 * @stream_id: Identifier for the stream (it stays constant even if the indices
533 * of the stream entries are changed)
535 * @lte: Pointer to pointer to the lookup table entry for the stream we need to
536 * extract, or NULL if there was no lookup table entry present for the stream
538 * @size: Number of bytes of the stream we want to extract (this supports the
539 * wimfs_truncate() function). It may be more than the actual stream length, in
540 * which case the extra space is filled with zeroes.
542 * @ctx: Context for the WIM filesystem.
544 * Returns 0 on success or a negative error code on failure.
547 extract_resource_to_staging_dir(struct wim_inode *inode,
549 struct wim_lookup_table_entry **lte,
551 struct wimfs_context *ctx)
553 char *staging_file_name;
556 struct wim_lookup_table_entry *old_lte, *new_lte;
559 DEBUG("Extracting resource to staging dir: inode %"PRIu64", "
560 "stream id %"PRIu32, inode->i_ino, stream_id);
564 wimlib_assert(old_lte == NULL ||
565 old_lte->resource_location != RESOURCE_IN_STAGING_FILE);
567 /* Create the staging file */
568 fd = create_staging_file(&staging_file_name, ctx);
572 /* Extract the stream to the staging file (possibly truncated) */
574 struct filedes wimlib_fd;
575 filedes_init(&wimlib_fd, fd);
576 extract_size = min(old_lte->size, size);
577 ret = extract_stream_to_fd(old_lte, &wimlib_fd, extract_size);
583 /* In the case of truncate() to more than the file length, extend the
584 * file with zeroes by calling ftruncate() on the underlying staging
586 if (ret == 0 && size > extract_size)
587 ret = ftruncate(fd, size);
589 /* Close the staging file descriptor and check for errors. If there's
590 * an error, unlink the staging file. */
591 if (ret != 0 || close(fd) != 0) {
597 goto out_delete_staging_file;
600 /* Now deal with the lookup table entries. We may be able to re-use the
601 * existing entry, but we may have to create a new one instead. */
603 if (old_lte && inode->i_nlink == old_lte->refcnt) {
604 /* The reference count of the existing lookup table entry is the
605 * same as the link count of the inode that contains the stream
606 * we're opening. Therefore, ALL the references to the lookup
607 * table entry correspond to the stream we're trying to extract,
608 * so the lookup table entry can be re-used. */
609 DEBUG("Re-using lookup table entry");
610 lookup_table_unlink(ctx->wim->lookup_table, old_lte);
614 /* There's an existing lookup table entry, but its
615 * reference count is greater than the link count for
616 * the inode containing a stream we're opening.
617 * Therefore, we need to split the lookup table entry.
619 wimlib_assert(old_lte->refcnt > inode->i_nlink);
620 DEBUG("Splitting lookup table entry "
621 "(inode->i_nlink = %u, old_lte->refcnt = %u)",
622 inode->i_nlink, old_lte->refcnt);
625 new_lte = new_lookup_table_entry();
628 goto out_delete_staging_file;
631 /* There may already be open file descriptors to this stream if
632 * it's previously been opened read-only, but just now we're
633 * opening it read-write. Identify those file descriptors and
634 * change their lookup table entry pointers to point to the new
635 * lookup table entry, and open staging file descriptors for
638 * At the same time, we need to count the number of these opened
639 * file descriptors to the new lookup table entry. If there's
640 * an old lookup table entry, this number needs to be subtracted
641 * from the fd's opened to the old entry. */
642 for (u16 i = 0, j = 0; j < inode->i_num_opened_fds; i++) {
643 struct wimfs_fd *fd = inode->i_fds[i];
645 if (fd->stream_id == stream_id) {
648 wimlib_assert(fd->f_lte == old_lte);
649 wimlib_assert(!filedes_valid(&fd->staging_fd));
651 new_lte->num_opened_fds++;
652 raw_fd = open(staging_file_name, O_RDONLY);
655 goto out_revert_fd_changes;
657 filedes_init(&fd->staging_fd, raw_fd);
662 DEBUG("%hu fd's were already opened to the file we extracted",
663 new_lte->num_opened_fds);
665 old_lte->num_opened_fds -= new_lte->num_opened_fds;
666 old_lte->refcnt -= inode->i_nlink;
670 lte_put_resource(new_lte);
671 new_lte->refcnt = inode->i_nlink;
672 new_lte->resource_location = RESOURCE_IN_STAGING_FILE;
673 new_lte->staging_file_name = staging_file_name;
674 new_lte->size = size;
676 add_unhashed_stream(new_lte, inode, stream_id,
677 &wim_get_current_image_metadata(ctx->wim)->unhashed_streams);
678 *retrieve_lte_pointer(new_lte) = new_lte;
681 out_revert_fd_changes:
682 for (u16 i = 0, j = 0; j < new_lte->num_opened_fds; i++) {
683 struct wimfs_fd *fd = inode->i_fds[i];
684 if (fd && fd->stream_id == stream_id && fd->f_lte == new_lte) {
686 if (filedes_valid(&fd->staging_fd)) {
687 filedes_close(&fd->staging_fd);
688 filedes_invalidate(&fd->staging_fd);
693 free_lookup_table_entry(new_lte);
694 out_delete_staging_file:
695 unlink(staging_file_name);
696 FREE(staging_file_name);
701 * Creates a randomly named staging directory and saves its name in the
702 * filesystem context structure.
705 make_staging_dir(struct wimfs_context *ctx, const char *user_prefix)
707 static const size_t random_suffix_len = 10;
708 static const char *common_suffix = ".staging";
709 static const size_t common_suffix_len = 8;
711 char *staging_dir_name = NULL;
712 size_t staging_dir_name_len;
714 const char *wim_basename;
715 char *real_user_prefix = NULL;
719 real_user_prefix = realpath(user_prefix, NULL);
720 if (!real_user_prefix) {
721 ERROR_WITH_ERRNO("Could not resolve `%s'",
723 ret = WIMLIB_ERR_NOTDIR;
726 wim_basename = path_basename(ctx->wim->filename);
727 prefix_len = strlen(real_user_prefix) + 1 + strlen(wim_basename);
729 prefix_len = strlen(ctx->wim->filename);
732 staging_dir_name_len = prefix_len + common_suffix_len + random_suffix_len;
734 staging_dir_name = MALLOC(staging_dir_name_len + 1);
735 if (!staging_dir_name) {
736 ret = WIMLIB_ERR_NOMEM;
740 if (real_user_prefix)
741 sprintf(staging_dir_name, "%s/%s", real_user_prefix, wim_basename);
743 strcpy(staging_dir_name, ctx->wim->filename);
745 strcat(staging_dir_name, common_suffix);
747 randomize_char_array_with_alnum(staging_dir_name + prefix_len + common_suffix_len,
750 staging_dir_name[staging_dir_name_len] = '\0';
752 if (mkdir(staging_dir_name, 0700) != 0) {
753 ERROR_WITH_ERRNO("Failed to create temporary directory `%s'",
755 ret = WIMLIB_ERR_MKDIR;
760 FREE(real_user_prefix);
762 ctx->staging_dir_name = staging_dir_name;
763 ctx->staging_dir_name_len = staging_dir_name_len;
765 FREE(staging_dir_name);
771 remove_file_or_directory(const char *fpath, const struct stat *sb,
772 int typeflag, struct FTW *ftwbuf)
774 if (remove(fpath) == 0)
777 ERROR_WITH_ERRNO("Cannot remove `%s'", fpath);
778 return WIMLIB_ERR_DELETE_STAGING_DIR;
783 * Deletes the staging directory and all the files contained in it.
786 delete_staging_dir(struct wimfs_context *ctx)
789 ret = nftw(ctx->staging_dir_name, remove_file_or_directory,
791 FREE(ctx->staging_dir_name);
792 ctx->staging_dir_name = NULL;
797 inode_close_fds(struct wim_inode *inode)
799 u16 num_opened_fds = inode->i_num_opened_fds;
800 for (u16 i = 0, j = 0; j < num_opened_fds; i++) {
801 struct wimfs_fd *fd = inode->i_fds[i];
803 wimlib_assert(fd->f_inode == inode);
804 int ret = close_wimfs_fd(fd);
813 /* Overwrites the WIM file, with changes saved. */
815 rebuild_wim(struct wimfs_context *ctx, int write_flags,
816 wimlib_progress_func_t progress_func)
819 struct wim_lookup_table_entry *lte, *tmp;
820 WIMStruct *wim = ctx->wim;
821 struct wim_image_metadata *imd = wim_get_current_image_metadata(ctx->wim);
823 DEBUG("Closing all staging file descriptors.");
824 image_for_each_unhashed_stream_safe(lte, tmp, imd) {
825 ret = inode_close_fds(lte->back_inode);
830 DEBUG("Freeing entries for zero-length streams");
831 image_for_each_unhashed_stream_safe(lte, tmp, imd) {
832 wimlib_assert(lte->unhashed);
833 if (lte->size == 0) {
834 struct wim_lookup_table_entry **back_ptr;
835 back_ptr = retrieve_lte_pointer(lte);
837 list_del(<e->unhashed_list);
838 free_lookup_table_entry(lte);
842 xml_update_image_info(wim, wim->current_image);
843 ret = wimlib_overwrite(wim, write_flags, 0, progress_func);
845 ERROR("Failed to commit changes to mounted WIM image");
849 /* Simple function that returns the concatenation of 2 strings. */
851 strcat_dup(const char *s1, const char *s2, size_t max_len)
853 size_t len = strlen(s1) + strlen(s2);
856 char *p = MALLOC(len + 1);
859 snprintf(p, len + 1, "%s%s", s1, s2);
864 set_message_queue_names(struct wimfs_context *ctx, const char *mount_dir)
866 static const char *u2d_prefix = "/wimlib-unmount-to-daemon-mq";
867 static const char *d2u_prefix = "/wimlib-daemon-to-unmount-mq";
872 dir_path = realpath(mount_dir, NULL);
874 ERROR_WITH_ERRNO("Failed to resolve path \"%s\"", mount_dir);
876 return WIMLIB_ERR_NOMEM;
878 return WIMLIB_ERR_NOTDIR;
881 for (p = dir_path; *p; p++)
885 ctx->unmount_to_daemon_mq_name = strcat_dup(u2d_prefix, dir_path,
887 if (!ctx->unmount_to_daemon_mq_name) {
888 ret = WIMLIB_ERR_NOMEM;
889 goto out_free_dir_path;
891 ctx->daemon_to_unmount_mq_name = strcat_dup(d2u_prefix, dir_path,
893 if (!ctx->daemon_to_unmount_mq_name) {
894 ret = WIMLIB_ERR_NOMEM;
895 goto out_free_unmount_to_daemon_mq_name;
899 goto out_free_dir_path;
900 out_free_unmount_to_daemon_mq_name:
901 FREE(ctx->unmount_to_daemon_mq_name);
902 ctx->unmount_to_daemon_mq_name = NULL;
909 free_message_queue_names(struct wimfs_context *ctx)
911 FREE(ctx->unmount_to_daemon_mq_name);
912 FREE(ctx->daemon_to_unmount_mq_name);
913 ctx->unmount_to_daemon_mq_name = NULL;
914 ctx->daemon_to_unmount_mq_name = NULL;
918 * Opens two POSIX message queue: one for sending messages from the unmount
919 * process to the daemon process, and one to go the other way. The names of the
920 * message queues, which must be system-wide unique, are be based on the mount
923 * @daemon specifies whether the calling process is the filesystem daemon or the
927 open_message_queues(struct wimfs_context *ctx, bool daemon)
929 int unmount_to_daemon_mq_flags = O_WRONLY | O_CREAT;
930 int daemon_to_unmount_mq_flags = O_RDONLY | O_CREAT;
936 swap(unmount_to_daemon_mq_flags, daemon_to_unmount_mq_flags);
942 orig_umask = umask(0000);
943 DEBUG("Opening message queue \"%s\"", ctx->unmount_to_daemon_mq_name);
944 ctx->unmount_to_daemon_mq = mq_open(ctx->unmount_to_daemon_mq_name,
945 unmount_to_daemon_mq_flags, mode, NULL);
947 if (ctx->unmount_to_daemon_mq == (mqd_t)-1) {
948 ERROR_WITH_ERRNO("mq_open()");
949 ret = WIMLIB_ERR_MQUEUE;
953 DEBUG("Opening message queue \"%s\"", ctx->daemon_to_unmount_mq_name);
954 ctx->daemon_to_unmount_mq = mq_open(ctx->daemon_to_unmount_mq_name,
955 daemon_to_unmount_mq_flags, mode, NULL);
957 if (ctx->daemon_to_unmount_mq == (mqd_t)-1) {
958 ERROR_WITH_ERRNO("mq_open()");
959 mq_close(ctx->unmount_to_daemon_mq);
960 mq_unlink(ctx->unmount_to_daemon_mq_name);
961 ctx->unmount_to_daemon_mq = (mqd_t)-1;
962 ret = WIMLIB_ERR_MQUEUE;
971 /* Try to determine the maximum message size of a message queue. The return
972 * value is the maximum message size, or a guess of 8192 bytes if it cannot be
975 mq_get_msgsize(mqd_t mq)
977 static const char *msgsize_max_file = "/proc/sys/fs/mqueue/msgsize_max";
982 if (mq_getattr(mq, &attr) == 0) {
983 msgsize = attr.mq_msgsize;
985 ERROR_WITH_ERRNO("mq_getattr()");
986 ERROR("Attempting to read %s", msgsize_max_file);
987 fp = fopen(msgsize_max_file, "rb");
989 if (fscanf(fp, "%ld", &msgsize) != 1) {
990 ERROR("Assuming message size of 8192");
995 ERROR_WITH_ERRNO("Failed to open the file `%s'",
997 ERROR("Assuming message size of 8192");
1005 get_mailbox(mqd_t mq, long needed_msgsize, long *msgsize_ret,
1011 msgsize = mq_get_msgsize(mq);
1013 if (msgsize < needed_msgsize) {
1014 ERROR("Message queue max size must be at least %ld!",
1016 return WIMLIB_ERR_MQUEUE;
1019 mailbox = MALLOC(msgsize);
1021 ERROR("Failed to allocate %ld bytes for mailbox", msgsize);
1022 return WIMLIB_ERR_NOMEM;
1024 *msgsize_ret = msgsize;
1025 *mailbox_ret = mailbox;
1030 unlink_message_queues(struct wimfs_context *ctx)
1032 mq_unlink(ctx->unmount_to_daemon_mq_name);
1033 mq_unlink(ctx->daemon_to_unmount_mq_name);
1036 /* Closes the message queues, which are allocated in static variables */
1038 close_message_queues(struct wimfs_context *ctx)
1040 DEBUG("Closing message queues");
1041 mq_close(ctx->unmount_to_daemon_mq);
1042 ctx->unmount_to_daemon_mq = (mqd_t)(-1);
1043 mq_close(ctx->daemon_to_unmount_mq);
1044 ctx->daemon_to_unmount_mq = (mqd_t)(-1);
1045 unlink_message_queues(ctx);
1049 struct unmount_msg_hdr {
1054 } _packed_attribute;
1056 struct msg_unmount_request {
1057 struct unmount_msg_hdr hdr;
1059 u8 want_progress_messages;
1060 } _packed_attribute;
1062 struct msg_daemon_info {
1063 struct unmount_msg_hdr hdr;
1066 } _packed_attribute;
1068 struct msg_unmount_finished {
1069 struct unmount_msg_hdr hdr;
1071 } _packed_attribute;
1073 struct msg_write_streams_progress {
1074 struct unmount_msg_hdr hdr;
1075 union wimlib_progress_info info;
1076 } _packed_attribute;
1079 MSG_TYPE_UNMOUNT_REQUEST,
1080 MSG_TYPE_DAEMON_INFO,
1081 MSG_TYPE_WRITE_STREAMS_PROGRESS,
1082 MSG_TYPE_UNMOUNT_FINISHED,
1086 struct msg_handler_context_hdr {
1087 int timeout_seconds;
1090 struct unmount_msg_handler_context {
1091 struct msg_handler_context_hdr hdr;
1095 wimlib_progress_func_t progress_func;
1098 struct daemon_msg_handler_context {
1099 struct msg_handler_context_hdr hdr;
1100 struct wimfs_context *wimfs_ctx;
1104 send_unmount_request_msg(mqd_t mq, int unmount_flags, u8 want_progress_messages)
1106 DEBUG("Sending unmount request msg");
1107 struct msg_unmount_request msg = {
1109 .min_version = WIMLIB_MAKEVERSION(1, 2, 1),
1110 .cur_version = WIMLIB_VERSION_CODE,
1111 .msg_type = MSG_TYPE_UNMOUNT_REQUEST,
1112 .msg_size = sizeof(msg),
1114 .unmount_flags = unmount_flags,
1115 .want_progress_messages = want_progress_messages,
1118 if (mq_send(mq, (void*)&msg, sizeof(msg), 1)) {
1119 ERROR_WITH_ERRNO("Failed to communicate with filesystem daemon");
1120 return WIMLIB_ERR_MQUEUE;
1126 send_daemon_info_msg(mqd_t mq, pid_t pid, int mount_flags)
1128 DEBUG("Sending daemon info msg (pid = %d, mount_flags=%x)",
1131 struct msg_daemon_info msg = {
1133 .min_version = WIMLIB_MAKEVERSION(1, 2, 1),
1134 .cur_version = WIMLIB_VERSION_CODE,
1135 .msg_type = MSG_TYPE_DAEMON_INFO,
1136 .msg_size = sizeof(msg),
1139 .mount_flags = mount_flags,
1141 if (mq_send(mq, (void*)&msg, sizeof(msg), 1)) {
1142 ERROR_WITH_ERRNO("Failed to send daemon info to unmount process");
1143 return WIMLIB_ERR_MQUEUE;
1149 send_unmount_finished_msg(mqd_t mq, int status)
1151 DEBUG("Sending unmount finished msg");
1152 struct msg_unmount_finished msg = {
1154 .min_version = WIMLIB_MAKEVERSION(1, 2, 1),
1155 .cur_version = WIMLIB_VERSION_CODE,
1156 .msg_type = MSG_TYPE_UNMOUNT_FINISHED,
1157 .msg_size = sizeof(msg),
1161 if (mq_send(mq, (void*)&msg, sizeof(msg), 1))
1162 ERROR_WITH_ERRNO("Failed to send status to unmount process");
1166 unmount_progress_func(enum wimlib_progress_msg msg,
1167 const union wimlib_progress_info *info)
1169 if (msg == WIMLIB_PROGRESS_MSG_WRITE_STREAMS) {
1170 struct msg_write_streams_progress msg = {
1172 .min_version = WIMLIB_MAKEVERSION(1, 2, 1),
1173 .cur_version = WIMLIB_VERSION_CODE,
1174 .msg_type = MSG_TYPE_WRITE_STREAMS_PROGRESS,
1175 .msg_size = sizeof(msg),
1179 if (mq_send(wimfs_get_context()->daemon_to_unmount_mq,
1180 (void*)&msg, sizeof(msg), 1))
1182 ERROR_WITH_ERRNO("Failed to send progress information "
1183 "to unmount process");
1190 msg_unmount_request_handler(const void *_msg, void *_handler_ctx)
1192 const struct msg_unmount_request *msg = _msg;
1193 struct daemon_msg_handler_context *handler_ctx = _handler_ctx;
1194 struct wimfs_context *wimfs_ctx;
1198 wimlib_progress_func_t progress_func;
1200 DEBUG("Handling unmount request msg");
1202 wimfs_ctx = handler_ctx->wimfs_ctx;
1203 if (msg->hdr.msg_size < sizeof(*msg)) {
1204 status = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1208 unmount_flags = msg->unmount_flags;
1209 if (msg->want_progress_messages)
1210 progress_func = unmount_progress_func;
1212 progress_func = NULL;
1214 ret = send_daemon_info_msg(wimfs_ctx->daemon_to_unmount_mq, getpid(),
1215 wimfs_ctx->mount_flags);
1221 if (wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1222 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_COMMIT) {
1223 int write_flags = 0;
1224 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY)
1225 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1226 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_REBUILD)
1227 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1228 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_RECOMPRESS)
1229 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
1230 status = rebuild_wim(wimfs_ctx, write_flags,
1234 DEBUG("Read-only mount");
1239 if (wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1240 ret = delete_staging_dir(wimfs_ctx);
1242 ERROR("Failed to delete the staging directory");
1247 wimfs_ctx->status = status;
1248 wimfs_ctx->have_status = true;
1249 return MSG_BREAK_LOOP;
1253 msg_daemon_info_handler(const void *_msg, void *_handler_ctx)
1255 const struct msg_daemon_info *msg = _msg;
1256 struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1258 DEBUG("Handling daemon info msg");
1259 if (msg->hdr.msg_size < sizeof(*msg))
1260 return WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1261 handler_ctx->daemon_pid = msg->daemon_pid;
1262 handler_ctx->mount_flags = msg->mount_flags;
1263 handler_ctx->hdr.timeout_seconds = 1;
1264 DEBUG("pid of daemon is %d; mount flags were %#x",
1265 handler_ctx->daemon_pid,
1266 handler_ctx->mount_flags);
1271 msg_write_streams_progress_handler(const void *_msg, void *_handler_ctx)
1273 const struct msg_write_streams_progress *msg = _msg;
1274 struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1276 if (msg->hdr.msg_size < sizeof(*msg))
1277 return WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1278 if (handler_ctx->progress_func) {
1279 handler_ctx->progress_func(WIMLIB_PROGRESS_MSG_WRITE_STREAMS,
1286 msg_unmount_finished_handler(const void *_msg, void *_handler_ctx)
1288 const struct msg_unmount_finished *msg = _msg;
1289 struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1291 DEBUG("Handling unmount finished message");
1292 if (msg->hdr.msg_size < sizeof(*msg))
1293 return WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1294 handler_ctx->status = msg->status;
1295 DEBUG("status is %d", handler_ctx->status);
1296 return MSG_BREAK_LOOP;
1300 unmount_timed_out_cb(void *_handler_ctx)
1302 const struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1304 if (handler_ctx->daemon_pid == 0 ||
1305 (kill(handler_ctx->daemon_pid, 0) != 0 && errno == ESRCH))
1307 ERROR("The filesystem daemon has crashed! Changes to the "
1308 "WIM may not have been commited.");
1309 return WIMLIB_ERR_FILESYSTEM_DAEMON_CRASHED;
1312 DEBUG("Filesystem daemon is still alive... "
1313 "Waiting another %d seconds", handler_ctx->hdr.timeout_seconds);
1318 daemon_timed_out_cb(void *_handler_ctx)
1320 ERROR("Timed out waiting for unmount request! "
1321 "Changes to the mounted WIM will not be committed.");
1322 return WIMLIB_ERR_TIMEOUT;
1325 typedef int (*msg_handler_t)(const void *_msg, void *_handler_ctx);
1327 struct msg_handler_callbacks {
1328 int (*timed_out)(void * _handler_ctx);
1329 msg_handler_t msg_handlers[MSG_TYPE_MAX];
1332 static const struct msg_handler_callbacks unmount_msg_handler_callbacks = {
1333 .timed_out = unmount_timed_out_cb,
1335 [MSG_TYPE_DAEMON_INFO] = msg_daemon_info_handler,
1336 [MSG_TYPE_WRITE_STREAMS_PROGRESS] = msg_write_streams_progress_handler,
1337 [MSG_TYPE_UNMOUNT_FINISHED] = msg_unmount_finished_handler,
1341 static const struct msg_handler_callbacks daemon_msg_handler_callbacks = {
1342 .timed_out = daemon_timed_out_cb,
1344 [MSG_TYPE_UNMOUNT_REQUEST] = msg_unmount_request_handler,
1349 receive_message(mqd_t mq,
1350 struct msg_handler_context_hdr *handler_ctx,
1351 const msg_handler_t msg_handlers[],
1352 long mailbox_size, void *mailbox)
1355 struct timespec timeout;
1356 ssize_t bytes_received;
1357 struct unmount_msg_hdr *hdr;
1360 gettimeofday(&now, NULL);
1361 timeout.tv_sec = now.tv_sec + handler_ctx->timeout_seconds;
1362 timeout.tv_nsec = now.tv_usec * 1000;
1364 bytes_received = mq_timedreceive(mq, mailbox,
1365 mailbox_size, NULL, &timeout);
1367 if (bytes_received == -1) {
1368 if (errno == ETIMEDOUT) {
1369 ret = WIMLIB_ERR_TIMEOUT;
1371 ERROR_WITH_ERRNO("mq_timedreceive()");
1372 ret = WIMLIB_ERR_MQUEUE;
1374 } else if (bytes_received < sizeof(*hdr) ||
1375 bytes_received != hdr->msg_size) {
1376 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1377 } else if (WIMLIB_VERSION_CODE < hdr->min_version) {
1378 /*ERROR("Cannot understand the received message. "*/
1379 /*"Please upgrade wimlib to at least v%d.%d.%d",*/
1380 /*WIMLIB_GET_MAJOR_VERSION(hdr->min_version),*/
1381 /*WIMLIB_GET_MINOR_VERSION(hdr->min_version),*/
1382 /*WIMLIB_GET_PATCH_VERSION(hdr->min_version));*/
1383 ret = MSG_VERSION_TOO_HIGH;
1384 } else if (hdr->msg_type >= MSG_TYPE_MAX) {
1385 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1386 } else if (msg_handlers[hdr->msg_type] == NULL) {
1387 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1389 ret = msg_handlers[hdr->msg_type](mailbox, handler_ctx);
1395 message_loop(mqd_t mq,
1396 const struct msg_handler_callbacks *callbacks,
1397 struct msg_handler_context_hdr *handler_ctx)
1399 static const size_t MAX_MSG_SIZE = 512;
1404 DEBUG("Entering message loop");
1406 ret = get_mailbox(mq, MAX_MSG_SIZE, &msgsize, &mailbox);
1410 ret = receive_message(mq, handler_ctx,
1411 callbacks->msg_handlers,
1413 if (ret == 0 || ret == MSG_VERSION_TOO_HIGH) {
1415 } else if (ret == MSG_BREAK_LOOP) {
1418 } else if (ret == WIMLIB_ERR_TIMEOUT) {
1419 if (callbacks->timed_out)
1420 ret = callbacks->timed_out(handler_ctx);
1426 ERROR_WITH_ERRNO("Error communicating with "
1427 "filesystem daemon");
1432 DEBUG("Exiting message loop");
1436 /* Execute `fusermount -u', which is installed setuid root, to unmount the WIM.
1438 * FUSE does not yet implement synchronous unmounts. This means that fusermount
1439 * -u will return before the filesystem daemon returns from wimfs_destroy().
1440 * This is partly what we want, because we need to send a message from this
1441 * process to the filesystem daemon telling whether --commit was specified or
1442 * not. However, after that, the unmount process must wait for the filesystem
1443 * daemon to finish writing the WIM file.
1446 execute_fusermount(const char *dir, bool lazy)
1454 ERROR_WITH_ERRNO("Failed to fork()");
1455 return WIMLIB_ERR_FORK;
1461 *argp++ = "fusermount";
1465 *argp++ = (char*)dir;
1467 execvp("fusermount", argv);
1468 ERROR_WITH_ERRNO("Failed to execute `fusermount'");
1469 exit(WIMLIB_ERR_FUSERMOUNT);
1473 ret = waitpid(pid, &status, 0);
1475 ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
1477 return WIMLIB_ERR_FUSERMOUNT;
1480 if (!WIFEXITED(status)) {
1481 ERROR("'fusermount' did not terminate normally!");
1482 return WIMLIB_ERR_FUSERMOUNT;
1485 status = WEXITSTATUS(status);
1490 if (status != WIMLIB_ERR_FUSERMOUNT)
1491 return WIMLIB_ERR_FUSERMOUNT;
1493 /* Try again, but with the `umount' program. This is required on other
1494 * FUSE implementations such as FreeBSD's that do not have a
1495 * `fusermount' program. */
1496 ERROR("Falling back to 'umount'. Note: you may need to be "
1497 "root for this to work");
1500 ERROR_WITH_ERRNO("Failed to fork()");
1501 return WIMLIB_ERR_FORK;
1510 *argp++ = (char*)dir;
1512 execvp("umount", argv);
1513 ERROR_WITH_ERRNO("Failed to execute `umount'");
1514 exit(WIMLIB_ERR_FUSERMOUNT);
1518 ret = waitpid(pid, &status, 0);
1520 ERROR_WITH_ERRNO("Failed to wait for `umount' process to "
1522 return WIMLIB_ERR_FUSERMOUNT;
1525 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
1526 ERROR("`umount' did not successfully complete");
1527 return WIMLIB_ERR_FUSERMOUNT;
1534 wimfs_chmod(const char *path, mode_t mask)
1536 struct wim_dentry *dentry;
1537 struct wimfs_context *ctx = wimfs_get_context();
1540 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA))
1543 ret = wim_pathname_to_stream(ctx->wim, path, LOOKUP_FLAG_DIRECTORY_OK,
1544 &dentry, NULL, NULL);
1548 ret = inode_set_unix_data(dentry->d_inode, ctx->default_uid,
1549 ctx->default_gid, mask,
1550 ctx->wim->lookup_table, UNIX_DATA_MODE);
1551 return ret ? -ENOMEM : 0;
1555 wimfs_chown(const char *path, uid_t uid, gid_t gid)
1557 struct wim_dentry *dentry;
1558 struct wimfs_context *ctx = wimfs_get_context();
1561 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA))
1564 ret = wim_pathname_to_stream(ctx->wim, path, LOOKUP_FLAG_DIRECTORY_OK,
1565 &dentry, NULL, NULL);
1569 ret = inode_set_unix_data(dentry->d_inode, uid, gid,
1570 inode_default_unix_mode(dentry->d_inode),
1571 ctx->wim->lookup_table,
1572 UNIX_DATA_UID | UNIX_DATA_GID);
1573 return ret ? -ENOMEM : 0;
1576 /* Called when the filesystem is unmounted. */
1578 wimfs_destroy(void *p)
1580 struct wimfs_context *wimfs_ctx = wimfs_get_context();
1581 if (open_message_queues(wimfs_ctx, true) == 0) {
1582 struct daemon_msg_handler_context handler_ctx = {
1584 .timeout_seconds = 5,
1586 .wimfs_ctx = wimfs_ctx,
1588 message_loop(wimfs_ctx->unmount_to_daemon_mq,
1589 &daemon_msg_handler_callbacks,
1595 wimfs_fgetattr(const char *path, struct stat *stbuf,
1596 struct fuse_file_info *fi)
1598 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1599 return inode_to_stbuf(fd->f_inode, fd->f_lte, stbuf);
1603 wimfs_ftruncate(const char *path, off_t size, struct fuse_file_info *fi)
1605 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1606 int ret = ftruncate(fd->staging_fd.fd, size);
1609 touch_inode(fd->f_inode);
1610 fd->f_lte->size = size;
1615 * Fills in a `struct stat' that corresponds to a file or directory in the WIM.
1618 wimfs_getattr(const char *path, struct stat *stbuf)
1620 struct wim_dentry *dentry;
1621 struct wim_lookup_table_entry *lte;
1623 struct wimfs_context *ctx = wimfs_get_context();
1625 ret = wim_pathname_to_stream(ctx->wim, path,
1626 get_lookup_flags(ctx) |
1627 LOOKUP_FLAG_DIRECTORY_OK,
1628 &dentry, <e, NULL);
1631 return inode_to_stbuf(dentry->d_inode, lte, stbuf);
1635 /* Read an alternate data stream through the XATTR interface, or get its size */
1637 wimfs_getxattr(const char *path, const char *name, char *value,
1641 struct wim_inode *inode;
1642 struct wim_ads_entry *ads_entry;
1644 struct wim_lookup_table_entry *lte;
1645 struct wimfs_context *ctx = wimfs_get_context();
1647 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1650 if (strlen(name) <= 5 || memcmp(name, "user.", 5) != 0)
1654 inode = wim_pathname_to_inode(ctx->wim, path);
1658 ads_entry = inode_get_ads_entry(inode, name, NULL);
1662 lte = ads_entry->lte;
1663 stream_size = lte->size;
1668 if (stream_size > size)
1671 ret = read_full_stream_into_buf(lte, value);
1682 /* Create a hard link */
1684 wimfs_link(const char *to, const char *from)
1686 struct wim_dentry *from_dentry, *from_dentry_parent;
1687 const char *link_name;
1688 struct wim_inode *inode;
1689 WIMStruct *wim = wimfs_get_WIMStruct();
1692 inode = wim_pathname_to_inode(wim, to);
1696 if (inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
1697 FILE_ATTRIBUTE_REPARSE_POINT))
1700 from_dentry_parent = get_parent_dentry(wim, from, WIMLIB_CASE_SENSITIVE);
1701 if (!from_dentry_parent)
1703 if (!dentry_is_directory(from_dentry_parent))
1706 link_name = path_basename(from);
1707 if (get_dentry_child_with_name(from_dentry_parent, link_name,
1708 WIMLIB_CASE_SENSITIVE))
1711 ret = new_dentry(link_name, &from_dentry);
1716 inode_ref_streams(inode);
1717 from_dentry->d_inode = inode;
1718 inode_add_dentry(from_dentry, inode);
1719 dentry_add_child(from_dentry_parent, from_dentry);
1725 wimfs_listxattr(const char *path, char *list, size_t size)
1728 struct wim_inode *inode;
1729 struct wimfs_context *ctx = wimfs_get_context();
1732 bool size_only = (size == 0);
1734 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1737 /* List alternate data streams, or get the list size */
1739 inode = wim_pathname_to_inode(ctx->wim, path);
1744 for (i = 0; i < inode->i_num_ads; i++) {
1746 if (!ads_entry_is_named_stream(&inode->i_ads_entries[i]))
1749 char *stream_name_mbs;
1750 size_t stream_name_mbs_nbytes;
1753 ret = utf16le_to_tstr(inode->i_ads_entries[i].stream_name,
1754 inode->i_ads_entries[i].stream_name_nbytes,
1756 &stream_name_mbs_nbytes);
1760 needed_size = stream_name_mbs_nbytes + 6;
1762 if (needed_size > size) {
1763 FREE(stream_name_mbs);
1766 sprintf(p, "user.%s", stream_name_mbs);
1767 size -= needed_size;
1770 FREE(stream_name_mbs);
1777 /* Create a directory in the WIM image. */
1779 wimfs_mkdir(const char *path, mode_t mode)
1781 return create_dentry(fuse_get_context(), path, mode | S_IFDIR,
1782 FILE_ATTRIBUTE_DIRECTORY, NULL);
1785 /* Create a regular file or alternate data stream in the WIM image. */
1787 wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
1789 const char *stream_name;
1790 struct fuse_context *fuse_ctx = fuse_get_context();
1791 struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
1796 if ((wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
1797 && (stream_name = path_stream_name(path))) {
1798 /* Make an alternate data stream */
1799 struct wim_ads_entry *new_entry;
1800 struct wim_inode *inode;
1802 char *p = (char*)stream_name - 1;
1803 wimlib_assert(*p == ':');
1806 inode = wim_pathname_to_inode(wimfs_ctx->wim, path);
1809 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT)
1811 if (inode_get_ads_entry(inode, stream_name, NULL))
1813 new_entry = inode_add_ads(inode, stream_name);
1818 /* Make a normal file (not an alternate data stream) */
1819 return create_dentry(fuse_ctx, path, mode | S_IFREG,
1820 FILE_ATTRIBUTE_NORMAL, NULL);
1826 wimfs_open(const char *path, struct fuse_file_info *fi)
1828 struct wim_dentry *dentry;
1829 struct wim_lookup_table_entry *lte;
1831 struct wimfs_fd *fd;
1832 struct wim_inode *inode;
1835 struct wimfs_context *ctx = wimfs_get_context();
1836 struct wim_lookup_table_entry **back_ptr;
1838 ret = wim_pathname_to_stream(ctx->wim, path, get_lookup_flags(ctx),
1839 &dentry, <e, &stream_idx);
1843 inode = dentry->d_inode;
1845 if (stream_idx == 0) {
1847 back_ptr = &inode->i_lte;
1849 stream_id = inode->i_ads_entries[stream_idx - 1].stream_id;
1850 back_ptr = &inode->i_ads_entries[stream_idx - 1].lte;
1853 /* The file resource may be in the staging directory (read-write mounts
1854 * only) or in the WIM. If it's in the staging directory, we need to
1855 * open a native file descriptor for the corresponding file. Otherwise,
1856 * we can read the file resource directly from the WIM file if we are
1857 * opening it read-only, but we need to extract the resource to the
1858 * staging directory if we are opening it writable. */
1860 if (flags_writable(fi->flags) &&
1861 (!lte || lte->resource_location != RESOURCE_IN_STAGING_FILE)) {
1862 u64 size = (lte) ? lte->size : 0;
1863 ret = extract_resource_to_staging_dir(inode, stream_id,
1870 ret = alloc_wimfs_fd(inode, stream_id, lte, &fd);
1874 if (lte && lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1877 raw_fd = open(lte->staging_file_name, fi->flags);
1879 int errno_save = errno;
1883 filedes_init(&fd->staging_fd, raw_fd);
1885 fi->fh = (uintptr_t)fd;
1889 /* Opens a directory. */
1891 wimfs_opendir(const char *path, struct fuse_file_info *fi)
1893 struct wim_inode *inode;
1895 struct wimfs_fd *fd = NULL;
1896 struct wimfs_context *ctx = wimfs_get_context();
1897 WIMStruct *wim = ctx->wim;
1899 inode = wim_pathname_to_inode(wim, path);
1902 if (!inode_is_directory(inode))
1904 ret = alloc_wimfs_fd(inode, 0, NULL, &fd);
1905 fi->fh = (uintptr_t)fd;
1911 * Read data from a file in the WIM or in the staging directory.
1914 wimfs_read(const char *path, char *buf, size_t size,
1915 off_t offset, struct fuse_file_info *fi)
1917 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1928 stream_size = fd->f_lte->size;
1932 if (offset > stream_size)
1935 size = min(size, stream_size - offset);
1939 switch (fd->f_lte->resource_location) {
1940 case RESOURCE_IN_STAGING_FILE:
1941 ret = raw_pread(&fd->staging_fd, buf, size, offset);
1945 case RESOURCE_IN_WIM:
1946 if (read_partial_wim_stream_into_buf(fd->f_lte, size,
1948 ret = errno ? -errno : -EIO;
1952 case RESOURCE_IN_ATTACHED_BUFFER:
1953 memcpy(buf, fd->f_lte->attached_buffer + offset, size);
1957 ERROR("Invalid resource location");
1964 struct fill_params {
1966 fuse_fill_dir_t filler;
1970 dentry_fuse_fill(struct wim_dentry *dentry, void *arg)
1972 struct fill_params *fill_params = arg;
1974 char *file_name_mbs;
1975 size_t file_name_mbs_nbytes;
1978 ret = utf16le_to_tstr(dentry->file_name,
1979 dentry->file_name_nbytes,
1981 &file_name_mbs_nbytes);
1985 ret = fill_params->filler(fill_params->buf, file_name_mbs, NULL, 0);
1986 FREE(file_name_mbs);
1990 /* Fills in the entries of the directory specified by @path using the
1991 * FUSE-provided function @filler. */
1993 wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
1994 off_t offset, struct fuse_file_info *fi)
1996 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1997 struct wim_inode *inode;
2002 inode = fd->f_inode;
2004 struct fill_params fill_params = {
2009 filler(buf, ".", NULL, 0);
2010 filler(buf, "..", NULL, 0);
2012 return for_dentry_in_rbtree(inode->i_children.rb_node,
2013 dentry_fuse_fill, &fill_params);
2018 wimfs_readlink(const char *path, char *buf, size_t buf_len)
2020 struct wimfs_context *ctx = wimfs_get_context();
2021 struct wim_inode *inode = wim_pathname_to_inode(ctx->wim, path);
2025 if (!inode_is_symlink(inode))
2029 ret = wim_inode_readlink(inode, buf, buf_len - 1, NULL);
2031 wimlib_assert(ret <= buf_len - 1);
2034 } else if (ret == -ENAMETOOLONG) {
2035 buf[buf_len - 1] = '\0';
2042 wimfs_release(const char *path, struct fuse_file_info *fi)
2044 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
2045 return close_wimfs_fd(fd);
2048 /* Close a directory */
2050 wimfs_releasedir(const char *path, struct fuse_file_info *fi)
2052 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
2053 return close_wimfs_fd(fd);
2057 /* Remove an alternate data stream through the XATTR interface */
2059 wimfs_removexattr(const char *path, const char *name)
2061 struct wim_inode *inode;
2062 struct wim_ads_entry *ads_entry;
2064 struct wimfs_context *ctx = wimfs_get_context();
2066 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
2069 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
2073 inode = wim_pathname_to_inode(ctx->wim, path);
2077 ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
2080 inode_remove_ads(inode, ads_idx, ctx->wim->lookup_table);
2085 /* Renames a file or directory. See rename (3) */
2087 wimfs_rename(const char *from, const char *to)
2089 return rename_wim_path(wimfs_get_WIMStruct(), from, to,
2090 WIMLIB_CASE_SENSITIVE);
2093 /* Remove a directory */
2095 wimfs_rmdir(const char *path)
2097 struct wim_dentry *dentry;
2098 WIMStruct *wim = wimfs_get_WIMStruct();
2100 dentry = get_dentry(wim, path, WIMLIB_CASE_SENSITIVE);
2104 if (!dentry_is_directory(dentry))
2107 if (dentry_has_children(dentry))
2110 remove_dentry(dentry, wim->lookup_table);
2115 /* Write an alternate data stream through the XATTR interface */
2117 wimfs_setxattr(const char *path, const char *name,
2118 const char *value, size_t size, int flags)
2120 struct wim_ads_entry *existing_ads_entry;
2121 struct wim_inode *inode;
2123 struct wimfs_context *ctx = wimfs_get_context();
2126 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
2129 if (strlen(name) <= 5 || memcmp(name, "user.", 5) != 0)
2133 inode = wim_pathname_to_inode(ctx->wim, path);
2137 existing_ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
2138 if (existing_ads_entry) {
2139 if (flags & XATTR_CREATE)
2142 if (flags & XATTR_REPLACE)
2146 ret = inode_add_ads_with_data(inode, name, value,
2147 size, ctx->wim->lookup_table);
2149 if (existing_ads_entry)
2150 inode_remove_ads(inode, ads_idx, ctx->wim->lookup_table);
2159 wimfs_symlink(const char *to, const char *from)
2161 struct fuse_context *fuse_ctx = fuse_get_context();
2162 struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
2163 struct wim_dentry *dentry;
2166 ret = create_dentry(fuse_ctx, from, S_IFLNK | 0777,
2167 FILE_ATTRIBUTE_REPARSE_POINT, &dentry);
2169 dentry->d_inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
2170 ret = wim_inode_set_symlink(dentry->d_inode, to,
2171 wimfs_ctx->wim->lookup_table);
2173 remove_dentry(dentry, wimfs_ctx->wim->lookup_table);
2174 if (ret == WIMLIB_ERR_NOMEM)
2184 /* Reduce the size of a file */
2186 wimfs_truncate(const char *path, off_t size)
2188 struct wim_dentry *dentry;
2189 struct wim_lookup_table_entry *lte;
2193 struct wim_inode *inode;
2194 struct wimfs_context *ctx = wimfs_get_context();
2196 ret = wim_pathname_to_stream(ctx->wim, path, get_lookup_flags(ctx),
2197 &dentry, <e, &stream_idx);
2202 if (lte == NULL && size == 0)
2205 if (lte != NULL && lte->resource_location == RESOURCE_IN_STAGING_FILE) {
2206 ret = truncate(lte->staging_file_name, size);
2212 /* File in WIM. Extract it to the staging directory, but only
2213 * the first @size bytes of it. */
2214 struct wim_lookup_table_entry **back_ptr;
2216 inode = dentry->d_inode;
2217 if (stream_idx == 0) {
2219 back_ptr = &inode->i_lte;
2221 stream_id = inode->i_ads_entries[stream_idx - 1].stream_id;
2222 back_ptr = &inode->i_ads_entries[stream_idx - 1].lte;
2224 ret = extract_resource_to_staging_dir(inode, stream_id,
2231 /* Unlink a non-directory or alternate data stream */
2233 wimfs_unlink(const char *path)
2235 struct wim_dentry *dentry;
2236 struct wim_lookup_table_entry *lte;
2239 struct wimfs_context *ctx = wimfs_get_context();
2241 ret = wim_pathname_to_stream(ctx->wim, path, get_lookup_flags(ctx),
2242 &dentry, <e, &stream_idx);
2247 if (inode_stream_name_nbytes(dentry->d_inode, stream_idx) == 0)
2248 remove_dentry(dentry, ctx->wim->lookup_table);
2250 inode_remove_ads(dentry->d_inode, stream_idx - 1,
2251 ctx->wim->lookup_table);
2255 #ifdef HAVE_UTIMENSAT
2257 * Change the timestamp on a file dentry.
2259 * Note that alternate data streams do not have their own timestamps.
2262 wimfs_utimens(const char *path, const struct timespec tv[2])
2264 struct wim_dentry *dentry;
2265 struct wim_inode *inode;
2266 WIMStruct *wim = wimfs_get_WIMStruct();
2268 dentry = get_dentry(wim, path, WIMLIB_CASE_SENSITIVE);
2271 inode = dentry->d_inode;
2273 if (tv[0].tv_nsec != UTIME_OMIT) {
2274 if (tv[0].tv_nsec == UTIME_NOW)
2275 inode->i_last_access_time = get_wim_timestamp();
2277 inode->i_last_access_time = timespec_to_wim_timestamp(tv[0]);
2279 if (tv[1].tv_nsec != UTIME_OMIT) {
2280 if (tv[1].tv_nsec == UTIME_NOW)
2281 inode->i_last_write_time = get_wim_timestamp();
2283 inode->i_last_write_time = timespec_to_wim_timestamp(tv[1]);
2287 #else /* HAVE_UTIMENSAT */
2289 wimfs_utime(const char *path, struct utimbuf *times)
2291 struct wim_dentry *dentry;
2292 struct wim_inode *inode;
2293 WIMStruct *wim = wimfs_get_WIMStruct();
2295 dentry = get_dentry(wim, path, WIMLIB_CASE_SENSITIVE);
2298 inode = dentry->d_inode;
2300 inode->i_last_write_time = unix_timestamp_to_wim(times->modtime);
2301 inode->i_last_access_time = unix_timestamp_to_wim(times->actime);
2304 #endif /* !HAVE_UTIMENSAT */
2306 /* Writes to a file in the WIM filesystem.
2307 * It may be an alternate data stream, but here we don't even notice because we
2308 * just get a lookup table entry. */
2310 wimfs_write(const char *path, const char *buf, size_t size,
2311 off_t offset, struct fuse_file_info *fi)
2313 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
2319 wimlib_assert(fd->f_lte != NULL);
2320 wimlib_assert(fd->f_lte->staging_file_name != NULL);
2321 wimlib_assert(filedes_valid(&fd->staging_fd));
2322 wimlib_assert(fd->f_inode != NULL);
2324 /* Write the data. */
2325 ret = raw_pwrite(&fd->staging_fd, buf, size, offset);
2329 /* Update file size */
2330 if (offset + size > fd->f_lte->size) {
2331 DEBUG("Update file size %"PRIu64 " => %"PRIu64"",
2332 fd->f_lte->size, offset + size);
2333 fd->f_lte->size = offset + size;
2336 /* Update timestamps */
2337 touch_inode(fd->f_inode);
2341 static struct fuse_operations wimfs_operations = {
2342 .chmod = wimfs_chmod,
2343 .chown = wimfs_chown,
2344 .destroy = wimfs_destroy,
2345 .fgetattr = wimfs_fgetattr,
2346 .ftruncate = wimfs_ftruncate,
2347 .getattr = wimfs_getattr,
2349 .getxattr = wimfs_getxattr,
2353 .listxattr = wimfs_listxattr,
2355 .mkdir = wimfs_mkdir,
2356 .mknod = wimfs_mknod,
2358 .opendir = wimfs_opendir,
2360 .readdir = wimfs_readdir,
2361 .readlink = wimfs_readlink,
2362 .release = wimfs_release,
2363 .releasedir = wimfs_releasedir,
2365 .removexattr = wimfs_removexattr,
2367 .rename = wimfs_rename,
2368 .rmdir = wimfs_rmdir,
2370 .setxattr = wimfs_setxattr,
2372 .symlink = wimfs_symlink,
2373 .truncate = wimfs_truncate,
2374 .unlink = wimfs_unlink,
2375 #ifdef HAVE_UTIMENSAT
2376 .utimens = wimfs_utimens,
2378 .utime = wimfs_utime,
2380 .write = wimfs_write,
2382 /* wimfs keeps file descriptor structures (struct wimfs_fd), so there is
2383 * no need to have the file path provided on operations such as read()
2384 * where only the file descriptor is needed. */
2385 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 8)
2386 .flag_nullpath_ok = 1,
2388 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 9)
2390 .flag_utime_omit_ok = 1,
2395 /* API function documented in wimlib.h */
2397 wimlib_mount_image(WIMStruct *wim, int image, const char *dir,
2398 int mount_flags, const char *staging_dir)
2404 struct wim_image_metadata *imd;
2405 struct wimfs_context ctx;
2406 struct wim_inode *inode;
2408 DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
2409 wim, image, dir, mount_flags);
2412 return WIMLIB_ERR_INVALID_PARAM;
2414 if (mount_flags & ~(WIMLIB_MOUNT_FLAG_READWRITE |
2415 WIMLIB_MOUNT_FLAG_DEBUG |
2416 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
2417 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
2418 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS |
2419 WIMLIB_MOUNT_FLAG_UNIX_DATA |
2420 WIMLIB_MOUNT_FLAG_ALLOW_OTHER))
2421 return WIMLIB_ERR_INVALID_PARAM;
2423 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2424 ret = can_delete_from_wim(wim);
2429 ret = select_wim_image(wim, image);
2433 DEBUG("Selected image %d", image);
2435 imd = wim_get_current_image_metadata(wim);
2437 if (imd->modified) {
2438 /* wimfs_read() only supports a limited number of stream
2439 * locations, not including RESOURCE_IN_FILE_ON_DISK,
2440 * RESOURCE_IN_NTFS_VOLUME, etc. that might appear if files were
2441 * added to the WIM image. */
2442 ERROR("Cannot mount an image with newly added files!");
2443 return WIMLIB_ERR_INVALID_PARAM;
2446 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2447 ret = lock_wim(wim, wim->in_fd.fd);
2452 /* Use default stream interface if one was not specified */
2453 if (!(mount_flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
2454 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
2455 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
2456 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
2458 DEBUG("Initializing struct wimfs_context");
2459 init_wimfs_context(&ctx);
2461 ctx.mount_flags = mount_flags;
2462 ctx.image_inode_list = &imd->inode_list;
2463 ctx.default_uid = getuid();
2464 ctx.default_gid = getgid();
2465 wimlib_assert(list_empty(&imd->unhashed_streams));
2466 if (mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
2467 ctx.default_lookup_flags = LOOKUP_FLAG_ADS_OK;
2469 DEBUG("Unlinking message queues in case they already exist");
2470 ret = set_message_queue_names(&ctx, dir);
2473 unlink_message_queues(&ctx);
2475 DEBUG("Preparing arguments to fuse_main()");
2477 dir_copy = STRDUP(dir);
2479 goto out_free_message_queue_names;
2482 argv[argc++] = "wimlib";
2483 argv[argc++] = dir_copy;
2485 /* disable multi-threaded operation */
2486 argv[argc++] = "-s";
2488 if (mount_flags & WIMLIB_MOUNT_FLAG_DEBUG)
2489 argv[argc++] = "-d";
2492 * We provide the use_ino option to the FUSE mount because we are going
2493 * to assign inode numbers ourselves. */
2494 char optstring[256] =
2498 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 8)
2501 ",default_permissions"
2503 argv[argc++] = "-o";
2504 argv[argc++] = optstring;
2505 if ((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
2506 /* Read-write mount. Make the staging directory */
2507 ret = make_staging_dir(&ctx, staging_dir);
2509 goto out_free_dir_copy;
2511 /* Read-only mount */
2512 strcat(optstring, ",ro");
2514 if (mount_flags & WIMLIB_MOUNT_FLAG_ALLOW_OTHER)
2515 strcat(optstring, ",allow_other");
2521 DEBUG("FUSE command line (argc = %d): ", argc);
2522 for (i = 0; i < argc; i++) {
2523 fputs(argv[i], stdout);
2531 /* Mark dentry tree as modified if read-write mount. */
2532 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2535 /* Resolve the lookup table entries for every inode in the image, and
2536 * assign inode numbers */
2537 DEBUG("Resolving lookup table entries and assigning inode numbers");
2539 image_for_each_inode(inode, imd)
2540 inode->i_ino = ctx.next_ino++;
2541 DEBUG("(next_ino = %"PRIu64")", ctx.next_ino);
2543 DEBUG("Calling fuse_main()");
2545 ret = fuse_main(argc, argv, &wimfs_operations, &ctx);
2547 DEBUG("Returned from fuse_main() (ret = %d)", ret);
2550 ret = WIMLIB_ERR_FUSE;
2552 if (ctx.have_status)
2555 ret = WIMLIB_ERR_TIMEOUT;
2557 if (ctx.daemon_to_unmount_mq != (mqd_t)(-1)) {
2558 send_unmount_finished_msg(ctx.daemon_to_unmount_mq, ret);
2559 close_message_queues(&ctx);
2562 /* Try to delete the staging directory if a deletion wasn't yet
2563 * attempted due to an earlier error */
2564 if (ctx.staging_dir_name)
2565 delete_staging_dir(&ctx);
2569 wim->wim_locked = 0;
2570 out_free_message_queue_names:
2571 free_message_queue_names(&ctx);
2575 /* API function documented in wimlib.h */
2577 wimlib_unmount_image(const char *dir, int unmount_flags,
2578 wimlib_progress_func_t progress_func)
2581 struct wimfs_context wimfs_ctx;
2583 if (unmount_flags & ~(WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY |
2584 WIMLIB_UNMOUNT_FLAG_COMMIT |
2585 WIMLIB_UNMOUNT_FLAG_REBUILD |
2586 WIMLIB_UNMOUNT_FLAG_RECOMPRESS |
2587 WIMLIB_UNMOUNT_FLAG_LAZY))
2588 return WIMLIB_ERR_INVALID_PARAM;
2590 init_wimfs_context(&wimfs_ctx);
2592 ret = set_message_queue_names(&wimfs_ctx, dir);
2596 ret = open_message_queues(&wimfs_ctx, false);
2598 goto out_free_message_queue_names;
2600 ret = send_unmount_request_msg(wimfs_ctx.unmount_to_daemon_mq,
2602 progress_func != NULL);
2604 goto out_close_message_queues;
2606 ret = execute_fusermount(dir, (unmount_flags & WIMLIB_UNMOUNT_FLAG_LAZY) != 0);
2608 goto out_close_message_queues;
2610 struct unmount_msg_handler_context handler_ctx = {
2612 .timeout_seconds = 5,
2615 .progress_func = progress_func,
2618 ret = message_loop(wimfs_ctx.daemon_to_unmount_mq,
2619 &unmount_msg_handler_callbacks,
2622 ret = handler_ctx.status;
2623 out_close_message_queues:
2624 close_message_queues(&wimfs_ctx);
2625 out_free_message_queue_names:
2626 free_message_queue_names(&wimfs_ctx);
2631 #else /* WITH_FUSE */
2635 mount_unsupported_error(void)
2637 #if defined(__WIN32__)
2638 ERROR("Sorry-- Mounting WIM images is not supported on Windows!");
2640 ERROR("wimlib was compiled with --without-fuse, which disables support "
2641 "for mounting WIMs.");
2643 return WIMLIB_ERR_UNSUPPORTED;
2647 wimlib_unmount_image(const tchar *dir, int unmount_flags,
2648 wimlib_progress_func_t progress_func)
2650 return mount_unsupported_error();
2654 wimlib_mount_image(WIMStruct *wim, int image, const tchar *dir,
2655 int mount_flags, const tchar *staging_dir)
2657 return mount_unsupported_error();
2660 #endif /* !WITH_FUSE */