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/.
29 #include "wimlib_internal.h"
34 # error "FUSE mount not supported on Win32! Please configure --without-fuse"
37 #include "buffer_io.h"
38 #include "lookup_table.h"
40 #include "timestamp.h"
52 #include <sys/types.h>
57 #define FUSE_USE_VERSION 26
61 #include <attr/xattr.h>
64 #define MSG_VERSION_TOO_HIGH -1
65 #define MSG_BREAK_LOOP -2
67 /* File descriptor to a file open on the WIM filesystem. */
69 struct wim_inode *f_inode;
70 struct wim_lookup_table_entry *f_lte;
76 struct wimfs_context {
77 /* The WIMStruct for the mounted WIM. */
80 /* Name of the staging directory for a read-write mount. Whenever a new file is
81 * created, it is done so in the staging directory. Furthermore, whenever a
82 * file in the WIM is modified, it is extracted to the staging directory. If
83 * changes are commited when the WIM is unmounted, the file resources are merged
84 * in from the staging directory when writing the new WIM. */
85 char *staging_dir_name;
86 size_t staging_dir_name_len;
88 /* Flags passed to wimlib_mount(). */
91 /* Default flags to use when looking up a WIM dentry (depends on whether
92 * the Windows interface to alternate data streams is being used or
94 int default_lookup_flags;
96 /* Next inode number to be assigned. Note: I didn't bother with a
97 * bitmap of free inode numbers since this isn't even a "real"
98 * filesystem anyway. */
101 /* List of inodes in the mounted image */
102 struct list_head *image_inode_list;
104 /* Name and message queue descriptors for message queues between the
105 * filesystem daemon process and the unmount process. These are used
106 * when the filesystem is unmounted and the process running
107 * wimlib_unmount_image() (i.e. the `imagex unmount' command) needs to
108 * communicate with the filesystem daemon running fuse_main() (i.e. the
109 * daemon created by the `imagex mount' or `imagex mountrw' commands */
110 char *unmount_to_daemon_mq_name;
111 char *daemon_to_unmount_mq_name;
112 mqd_t unmount_to_daemon_mq;
113 mqd_t daemon_to_unmount_mq;
123 init_wimfs_context(struct wimfs_context *ctx)
125 memset(ctx, 0, sizeof(*ctx));
126 ctx->unmount_to_daemon_mq = (mqd_t)-1;
127 ctx->daemon_to_unmount_mq = (mqd_t)-1;
130 #define WIMFS_CTX(fuse_ctx) ((struct wimfs_context*)(fuse_ctx)->private_data)
132 static inline struct wimfs_context *
135 return WIMFS_CTX(fuse_get_context());
138 static inline WIMStruct *
139 wimfs_get_WIMStruct()
141 return wimfs_get_context()->wim;
145 wimfs_ctx_readonly(const struct wimfs_context *ctx)
147 return (ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) == 0;
151 get_lookup_flags(const struct wimfs_context *ctx)
153 return ctx->default_lookup_flags;
156 /* Returns nonzero if write permission is requested on the file open flags */
158 flags_writable(int open_flags)
160 return open_flags & (O_RDWR | O_WRONLY);
163 /* Like pread(), but keep trying until everything has been read or we know for
164 * sure that there was an error. */
166 full_pread(int fd, void *buf, size_t count, off_t offset)
168 ssize_t bytes_remaining = count;
171 while (bytes_remaining > 0) {
172 bytes_read = pread(fd, buf, bytes_remaining, offset);
173 if (bytes_read <= 0) {
174 if (bytes_read < 0) {
182 bytes_remaining -= bytes_read;
184 offset += bytes_read;
186 return count - bytes_remaining;
189 /* Like pwrite(), but keep trying until everything has been written or we know
190 * for sure that there was an error. */
192 full_pwrite(int fd, const void *buf, size_t count, off_t offset)
194 ssize_t bytes_remaining = count;
195 ssize_t bytes_written;
197 while (bytes_remaining > 0) {
198 bytes_written = pwrite(fd, buf, bytes_remaining, offset);
199 if (bytes_written < 0) {
204 bytes_remaining -= bytes_written;
205 buf += bytes_written;
206 offset += bytes_written;
208 return count - bytes_remaining;
212 * Allocate a file descriptor for a stream.
214 * @inode: inode containing the stream we're opening
215 * @stream_id: ID of the stream we're opening
216 * @lte: Lookup table entry for the stream (may be NULL)
217 * @fd_ret: Return the allocated file descriptor if successful.
218 * @readonly: True if this is a read-only mount.
220 * Return 0 iff successful or error code if unsuccessful.
223 alloc_wimfs_fd(struct wim_inode *inode,
225 struct wim_lookup_table_entry *lte,
226 struct wimfs_fd **fd_ret,
229 static const u16 fds_per_alloc = 8;
230 static const u16 max_fds = 0xffff;
233 pthread_mutex_lock(&inode->i_mutex);
235 DEBUG("Allocating fd for stream ID %u from inode %#"PRIx64" "
236 "(open = %u, allocated = %u)",
237 stream_id, inode->i_ino, inode->i_num_opened_fds,
238 inode->i_num_allocated_fds);
240 if (inode->i_num_opened_fds == inode->i_num_allocated_fds) {
241 struct wimfs_fd **fds;
244 if (inode->i_num_allocated_fds == max_fds) {
248 num_new_fds = min(fds_per_alloc,
249 max_fds - inode->i_num_allocated_fds);
251 fds = REALLOC(inode->i_fds,
252 (inode->i_num_allocated_fds + num_new_fds) *
253 sizeof(inode->i_fds[0]));
258 memset(&fds[inode->i_num_allocated_fds], 0,
259 num_new_fds * sizeof(fds[0]));
261 inode->i_num_allocated_fds += num_new_fds;
263 for (u16 i = 0; ; i++) {
264 if (!inode->i_fds[i]) {
265 struct wimfs_fd *fd = CALLOC(1, sizeof(*fd));
274 fd->stream_id = stream_id;
276 inode->i_fds[i] = fd;
277 inode->i_num_opened_fds++;
278 if (lte && !readonly)
279 lte->num_opened_fds++;
280 DEBUG("Allocated fd (idx = %u)", fd->idx);
286 pthread_mutex_unlock(&inode->i_mutex);
291 inode_put_fd(struct wim_inode *inode, struct wimfs_fd *fd)
293 wimlib_assert(inode != NULL);
295 pthread_mutex_lock(&inode->i_mutex);
297 wimlib_assert(fd->f_inode == inode);
298 wimlib_assert(inode->i_num_opened_fds != 0);
299 wimlib_assert(fd->idx < inode->i_num_allocated_fds);
300 wimlib_assert(inode->i_fds[fd->idx] == fd);
302 inode->i_fds[fd->idx] = NULL;
304 if (--inode->i_num_opened_fds == 0 && inode->i_nlink == 0) {
305 pthread_mutex_unlock(&inode->i_mutex);
308 pthread_mutex_unlock(&inode->i_mutex);
313 lte_put_fd(struct wim_lookup_table_entry *lte, struct wimfs_fd *fd)
315 wimlib_assert(fd->f_lte == lte);
317 if (!lte) /* Empty stream with no lookup table entry */
320 /* Close staging file descriptor if needed. */
322 if (lte->resource_location == RESOURCE_IN_STAGING_FILE
323 && fd->staging_fd != -1)
325 if (close(fd->staging_fd) != 0) {
326 ERROR_WITH_ERRNO("Failed to close staging file");
330 lte_decrement_num_opened_fds(lte);
334 /* Close a file descriptor. */
336 close_wimfs_fd(struct wimfs_fd *fd)
339 DEBUG("Closing fd (ino = %#"PRIx64", opened = %u, allocated = %u)",
340 fd->f_inode->i_ino, fd->f_inode->i_num_opened_fds,
341 fd->f_inode->i_num_allocated_fds);
342 ret = lte_put_fd(fd->f_lte, fd);
346 inode_put_fd(fd->f_inode, fd);
351 * Add a new dentry with a new inode to a WIM image.
353 * Returns 0 on success, or negative error number on failure.
356 create_dentry(struct fuse_context *fuse_ctx, const char *path,
357 mode_t mode, int attributes, struct wim_dentry **dentry_ret)
359 struct wim_dentry *parent;
360 struct wim_dentry *new;
361 const char *basename;
362 struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
365 parent = get_parent_dentry(wimfs_ctx->wim, path);
369 if (!dentry_is_directory(parent))
372 basename = path_basename(path);
373 if (get_dentry_child_with_name(parent, basename))
376 ret = new_dentry_with_inode(basename, &new);
380 new->d_inode->i_resolved = 1;
381 new->d_inode->i_ino = wimfs_ctx->next_ino++;
382 new->d_inode->i_attributes = attributes;
384 if (wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA) {
385 if (inode_set_unix_data(new->d_inode,
388 mode & ~fuse_ctx->umask,
389 wimfs_ctx->wim->lookup_table,
390 UNIX_DATA_ALL | UNIX_DATA_CREATE))
396 dentry_add_child(parent, new);
397 list_add_tail(&new->d_inode->i_list, wimfs_ctx->image_inode_list);
403 /* Remove a dentry from a mounted WIM image; i.e. remove an alias for the
404 * corresponding inode.
406 * If there are no remaining references to the inode either through dentries or
407 * open file descriptors, the inode is freed. Otherwise, the inode is not
408 * removed, but the dentry is unlinked and freed.
410 * Either way, all lookup table entries referenced by the inode have their
411 * reference count decremented. If a lookup table entry has no open file
412 * descriptors and no references remaining, it is freed, and the corresponding
413 * staging file is unlinked.
416 remove_dentry(struct wim_dentry *dentry,
417 struct wim_lookup_table *lookup_table)
419 struct wim_inode *inode = dentry->d_inode;
420 struct wim_lookup_table_entry *lte;
423 for (i = 0; i <= inode->i_num_ads; i++) {
424 lte = inode_stream_lte_resolved(inode, i);
426 lte_decrement_refcnt(lte, lookup_table);
428 unlink_dentry(dentry);
433 inode_default_unix_mode(const struct wim_inode *inode)
435 if (inode_is_symlink(inode))
436 return S_IFLNK | 0777;
437 else if (inode_is_directory(inode))
438 return S_IFDIR | 0777;
440 return S_IFREG | 0777;
443 /* Transfers file attributes from a struct wim_inode to a `stat' buffer.
445 * The lookup table entry tells us which stream in the inode we are statting.
446 * For a named data stream, everything returned is the same as the unnamed data
447 * stream except possibly the size and block count. */
449 inode_to_stbuf(const struct wim_inode *inode,
450 const struct wim_lookup_table_entry *lte,
453 const struct wimfs_context *ctx = wimfs_get_context();
455 memset(stbuf, 0, sizeof(struct stat));
456 stbuf->st_mode = inode_default_unix_mode(inode);
457 stbuf->st_uid = ctx->default_uid;
458 stbuf->st_gid = ctx->default_gid;
459 if (ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA) {
460 struct wimlib_unix_data unix_data;
461 if (inode_get_unix_data(inode, &unix_data, NULL) == 0) {
462 stbuf->st_uid = unix_data.uid;
463 stbuf->st_gid = unix_data.gid;
464 stbuf->st_mode = unix_data.mode;
467 stbuf->st_ino = (ino_t)inode->i_ino;
468 stbuf->st_nlink = inode->i_nlink;
470 stbuf->st_size = wim_resource_size(lte);
473 #ifdef HAVE_STAT_NANOSECOND_PRECISION
474 stbuf->st_atim = wim_timestamp_to_timespec(inode->i_last_access_time);
475 stbuf->st_mtim = wim_timestamp_to_timespec(inode->i_last_write_time);
476 stbuf->st_ctim = stbuf->st_mtim;
478 stbuf->st_atime = wim_timestamp_to_unix(inode->i_last_access_time);
479 stbuf->st_mtime = wim_timestamp_to_unix(inode->i_last_write_time);
480 stbuf->st_ctime = stbuf->st_mtime;
482 stbuf->st_blocks = (stbuf->st_size + 511) / 512;
487 touch_inode(struct wim_inode *inode)
489 u64 now = get_wim_timestamp();
490 inode->i_last_access_time = now;
491 inode->i_last_write_time = now;
494 /* Creates a new staging file and returns its file descriptor opened for
497 * @name_ret: A location into which the a pointer to the newly allocated name of
498 * the staging file is stored.
500 * @ctx: Context for the WIM filesystem; this provides the name of the
503 * On success, returns the file descriptor for the staging file, opened for
504 * writing. On failure, returns -1 and sets errno.
507 create_staging_file(char **name_ret, struct wimfs_context *ctx)
515 static const size_t STAGING_FILE_NAME_LEN = 20;
517 name_len = ctx->staging_dir_name_len + 1 + STAGING_FILE_NAME_LEN;
518 name = MALLOC(name_len + 1);
526 memcpy(name, ctx->staging_dir_name, ctx->staging_dir_name_len);
527 name[ctx->staging_dir_name_len] = '/';
528 randomize_char_array_with_alnum(name + ctx->staging_dir_name_len + 1,
529 STAGING_FILE_NAME_LEN);
530 name[name_len] = '\0';
533 /* Just in case, verify that the randomly generated name doesn't name an
534 * existing file, and try again if so */
535 } while (stat(name, &stbuf) == 0);
537 if (errno != ENOENT) /* other error?! */
540 /* doesn't exist--- ok */
542 DEBUG("Creating staging file `%s'", name);
544 fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0600);
556 * Extract a WIM resource to the staging directory.
558 * @inode: Inode that contains the stream we are extracting
560 * @stream_id: Identifier for the stream (it stays constant even if the indices
561 * of the stream entries are changed)
563 * @lte: Pointer to pointer to the lookup table entry for the stream we need to
564 * extract, or NULL if there was no lookup table entry present for the stream
566 * @size: Number of bytes of the stream we want to extract (this supports the
567 * wimfs_truncate() function). It may be more than the actual stream length, in
568 * which case the extra space is filled with zeroes.
570 * @ctx: Context for the WIM filesystem.
572 * Returns 0 on success or a negative error code on failure.
575 extract_resource_to_staging_dir(struct wim_inode *inode,
577 struct wim_lookup_table_entry **lte,
579 struct wimfs_context *ctx)
581 char *staging_file_name;
584 struct wim_lookup_table_entry *old_lte, *new_lte;
587 DEBUG("Extracting resource to staging dir: inode %"PRIu64", "
588 "stream id %"PRIu32, inode->i_ino, stream_id);
592 wimlib_assert(old_lte == NULL ||
593 old_lte->resource_location != RESOURCE_IN_STAGING_FILE);
595 /* Create the staging file */
596 fd = create_staging_file(&staging_file_name, ctx);
600 /* Extract the stream to the staging file (possibly truncated) */
602 extract_size = min(wim_resource_size(old_lte), size);
603 ret = extract_wim_resource_to_fd(old_lte, fd, extract_size);
609 /* In the case of truncate() to more than the file length, extend the
610 * file with zeroes by calling ftruncate() on the underlying staging
612 if (ret == 0 && size > extract_size)
613 ret = ftruncate(fd, size);
615 /* Close the staging file descriptor and check for errors. If there's
616 * an error, unlink the staging file. */
617 if (ret != 0 || close(fd) != 0) {
623 goto out_delete_staging_file;
626 /* Now deal with the lookup table entries. We may be able to re-use the
627 * existing entry, but we may have to create a new one instead. */
629 if (old_lte && inode->i_nlink == old_lte->refcnt) {
630 /* The reference count of the existing lookup table entry is the
631 * same as the link count of the inode that contains the stream
632 * we're opening. Therefore, ALL the references to the lookup
633 * table entry correspond to the stream we're trying to extract,
634 * so the lookup table entry can be re-used. */
635 DEBUG("Re-using lookup table entry");
636 lookup_table_unlink(ctx->wim->lookup_table, old_lte);
640 /* There's an existing lookup table entry, but its
641 * reference count is greater than the link count for
642 * the inode containing a stream we're opening.
643 * Therefore, we need to split the lookup table entry.
645 wimlib_assert(old_lte->refcnt > inode->i_nlink);
646 DEBUG("Splitting lookup table entry "
647 "(inode->i_nlink = %u, old_lte->refcnt = %u)",
648 inode->i_nlink, old_lte->refcnt);
651 new_lte = new_lookup_table_entry();
654 goto out_delete_staging_file;
657 /* There may already be open file descriptors to this stream if
658 * it's previously been opened read-only, but just now we're
659 * opening it read-write. Identify those file descriptors and
660 * change their lookup table entry pointers to point to the new
661 * lookup table entry, and open staging file descriptors for
664 * At the same time, we need to count the number of these opened
665 * file descriptors to the new lookup table entry. If there's
666 * an old lookup table entry, this number needs to be subtracted
667 * from the fd's opened to the old entry. */
668 for (u16 i = 0, j = 0; j < inode->i_num_opened_fds; i++) {
669 struct wimfs_fd *fd = inode->i_fds[i];
671 if (fd->stream_id == stream_id) {
672 wimlib_assert(fd->f_lte == old_lte);
673 wimlib_assert(fd->staging_fd == -1);
675 new_lte->num_opened_fds++;
676 fd->staging_fd = open(staging_file_name, O_RDONLY);
677 if (fd->staging_fd == -1) {
679 goto out_revert_fd_changes;
685 DEBUG("%hu fd's were already opened to the file we extracted",
686 new_lte->num_opened_fds);
688 old_lte->num_opened_fds -= new_lte->num_opened_fds;
689 old_lte->refcnt -= inode->i_nlink;
693 new_lte->refcnt = inode->i_nlink;
694 new_lte->resource_location = RESOURCE_IN_STAGING_FILE;
695 new_lte->staging_file_name = staging_file_name;
696 new_lte->resource_entry.original_size = size;
698 lookup_table_insert_unhashed(ctx->wim->lookup_table, new_lte,
700 *retrieve_lte_pointer(new_lte) = new_lte;
703 out_revert_fd_changes:
704 for (u16 i = 0, j = 0; j < new_lte->num_opened_fds; i++) {
705 struct wimfs_fd *fd = inode->i_fds[i];
706 if (fd && fd->stream_id == stream_id && fd->f_lte == new_lte) {
708 if (fd->staging_fd != -1) {
709 close(fd->staging_fd);
715 free_lookup_table_entry(new_lte);
716 out_delete_staging_file:
717 unlink(staging_file_name);
718 FREE(staging_file_name);
723 * Creates a randomly named staging directory and saves its name in the
724 * filesystem context structure.
727 make_staging_dir(struct wimfs_context *ctx, const char *user_prefix)
729 static const size_t random_suffix_len = 10;
730 static const char *common_suffix = ".staging";
731 static const size_t common_suffix_len = 8;
733 char *staging_dir_name = NULL;
734 size_t staging_dir_name_len;
736 const char *wim_basename;
737 char *real_user_prefix = NULL;
741 real_user_prefix = realpath(user_prefix, NULL);
742 if (!real_user_prefix) {
743 ERROR_WITH_ERRNO("Could not resolve `%s'",
745 ret = WIMLIB_ERR_NOTDIR;
748 wim_basename = path_basename(ctx->wim->filename);
749 prefix_len = strlen(real_user_prefix) + 1 + strlen(wim_basename);
751 prefix_len = strlen(ctx->wim->filename);
754 staging_dir_name_len = prefix_len + common_suffix_len + random_suffix_len;
756 staging_dir_name = MALLOC(staging_dir_name_len + 1);
757 if (!staging_dir_name) {
758 ret = WIMLIB_ERR_NOMEM;
762 if (real_user_prefix)
763 sprintf(staging_dir_name, "%s/%s", real_user_prefix, wim_basename);
765 strcpy(staging_dir_name, ctx->wim->filename);
767 strcat(staging_dir_name, common_suffix);
769 randomize_char_array_with_alnum(staging_dir_name + prefix_len + common_suffix_len,
772 staging_dir_name[staging_dir_name_len] = '\0';
774 if (mkdir(staging_dir_name, 0700) != 0) {
775 ERROR_WITH_ERRNO("Failed to create temporary directory `%s'",
777 ret = WIMLIB_ERR_MKDIR;
782 FREE(real_user_prefix);
784 ctx->staging_dir_name = staging_dir_name;
785 ctx->staging_dir_name_len = staging_dir_name_len;
787 FREE(staging_dir_name);
793 remove_file_or_directory(const char *fpath, const struct stat *sb,
794 int typeflag, struct FTW *ftwbuf)
796 if (remove(fpath) == 0)
799 ERROR_WITH_ERRNO("Cannot remove `%s'", fpath);
800 return WIMLIB_ERR_DELETE_STAGING_DIR;
805 * Deletes the staging directory and all the files contained in it.
808 delete_staging_dir(struct wimfs_context *ctx)
811 ret = nftw(ctx->staging_dir_name, remove_file_or_directory,
813 FREE(ctx->staging_dir_name);
814 ctx->staging_dir_name = NULL;
819 inode_close_fds(struct wim_inode *inode)
821 u16 num_opened_fds = inode->i_num_opened_fds;
822 for (u16 i = 0, j = 0; j < num_opened_fds; i++) {
823 struct wimfs_fd *fd = inode->i_fds[i];
825 wimlib_assert(fd->f_inode == inode);
826 int ret = close_wimfs_fd(fd);
835 /* Overwrites the WIM file, with changes saved. */
837 rebuild_wim(struct wimfs_context *ctx, int write_flags,
838 wimlib_progress_func_t progress_func)
841 struct wim_lookup_table_entry *lte, *tmp;
842 WIMStruct *w = ctx->wim;
843 struct wim_image_metadata *imd = wim_get_current_image_metadata(ctx->wim);
845 DEBUG("Closing all staging file descriptors.");
846 image_for_each_unhashed_stream_safe(lte, tmp, imd) {
847 ret = inode_close_fds(lte->back_inode);
852 DEBUG("Freeing entries for zero-length streams");
853 image_for_each_unhashed_stream_safe(lte, tmp, imd) {
854 wimlib_assert(lte->unhashed);
855 if (wim_resource_size(lte) == 0) {
856 struct wim_lookup_table_entry **back_ptr;
857 back_ptr = retrieve_lte_pointer(lte);
859 list_del(<e->unhashed_list);
860 free_lookup_table_entry(lte);
864 xml_update_image_info(w, w->current_image);
865 ret = wimlib_overwrite(w, write_flags, 0, progress_func);
867 ERROR("Failed to commit changes to mounted WIM image");
871 /* Simple function that returns the concatenation of 2 strings. */
873 strcat_dup(const char *s1, const char *s2, size_t max_len)
875 size_t len = strlen(s1) + strlen(s2);
878 char *p = MALLOC(len + 1);
881 snprintf(p, len + 1, "%s%s", s1, s2);
886 set_message_queue_names(struct wimfs_context *ctx, const char *mount_dir)
888 static const char *u2d_prefix = "/wimlib-unmount-to-daemon-mq";
889 static const char *d2u_prefix = "/wimlib-daemon-to-unmount-mq";
894 dir_path = realpath(mount_dir, NULL);
896 ERROR_WITH_ERRNO("Failed to resolve path \"%s\"", mount_dir);
898 return WIMLIB_ERR_NOMEM;
900 return WIMLIB_ERR_NOTDIR;
903 for (p = dir_path; *p; p++)
907 ctx->unmount_to_daemon_mq_name = strcat_dup(u2d_prefix, dir_path,
909 if (!ctx->unmount_to_daemon_mq_name) {
910 ret = WIMLIB_ERR_NOMEM;
911 goto out_free_dir_path;
913 ctx->daemon_to_unmount_mq_name = strcat_dup(d2u_prefix, dir_path,
915 if (!ctx->daemon_to_unmount_mq_name) {
916 ret = WIMLIB_ERR_NOMEM;
917 goto out_free_unmount_to_daemon_mq_name;
921 goto out_free_dir_path;
922 out_free_unmount_to_daemon_mq_name:
923 FREE(ctx->unmount_to_daemon_mq_name);
924 ctx->unmount_to_daemon_mq_name = NULL;
931 free_message_queue_names(struct wimfs_context *ctx)
933 FREE(ctx->unmount_to_daemon_mq_name);
934 FREE(ctx->daemon_to_unmount_mq_name);
935 ctx->unmount_to_daemon_mq_name = NULL;
936 ctx->daemon_to_unmount_mq_name = NULL;
940 * Opens two POSIX message queue: one for sending messages from the unmount
941 * process to the daemon process, and one to go the other way. The names of the
942 * message queues, which must be system-wide unique, are be based on the mount
945 * @daemon specifies whether the calling process is the filesystem daemon or the
949 open_message_queues(struct wimfs_context *ctx, bool daemon)
951 int unmount_to_daemon_mq_flags = O_WRONLY | O_CREAT;
952 int daemon_to_unmount_mq_flags = O_RDONLY | O_CREAT;
958 swap(unmount_to_daemon_mq_flags, daemon_to_unmount_mq_flags);
964 orig_umask = umask(0000);
965 DEBUG("Opening message queue \"%s\"", ctx->unmount_to_daemon_mq_name);
966 ctx->unmount_to_daemon_mq = mq_open(ctx->unmount_to_daemon_mq_name,
967 unmount_to_daemon_mq_flags, mode, NULL);
969 if (ctx->unmount_to_daemon_mq == (mqd_t)-1) {
970 ERROR_WITH_ERRNO("mq_open()");
971 ret = WIMLIB_ERR_MQUEUE;
975 DEBUG("Opening message queue \"%s\"", ctx->daemon_to_unmount_mq_name);
976 ctx->daemon_to_unmount_mq = mq_open(ctx->daemon_to_unmount_mq_name,
977 daemon_to_unmount_mq_flags, mode, NULL);
979 if (ctx->daemon_to_unmount_mq == (mqd_t)-1) {
980 ERROR_WITH_ERRNO("mq_open()");
981 mq_close(ctx->unmount_to_daemon_mq);
982 mq_unlink(ctx->unmount_to_daemon_mq_name);
983 ctx->unmount_to_daemon_mq = (mqd_t)-1;
984 ret = WIMLIB_ERR_MQUEUE;
993 /* Try to determine the maximum message size of a message queue. The return
994 * value is the maximum message size, or a guess of 8192 bytes if it cannot be
997 mq_get_msgsize(mqd_t mq)
999 static const char *msgsize_max_file = "/proc/sys/fs/mqueue/msgsize_max";
1001 struct mq_attr attr;
1004 if (mq_getattr(mq, &attr) == 0) {
1005 msgsize = attr.mq_msgsize;
1007 ERROR_WITH_ERRNO("mq_getattr()");
1008 ERROR("Attempting to read %s", msgsize_max_file);
1009 fp = fopen(msgsize_max_file, "rb");
1011 if (fscanf(fp, "%ld", &msgsize) != 1) {
1012 ERROR("Assuming message size of 8192");
1017 ERROR_WITH_ERRNO("Failed to open the file `%s'",
1019 ERROR("Assuming message size of 8192");
1027 get_mailbox(mqd_t mq, long needed_msgsize, long *msgsize_ret,
1033 msgsize = mq_get_msgsize(mq);
1035 if (msgsize < needed_msgsize) {
1036 ERROR("Message queue max size must be at least %ld!",
1038 return WIMLIB_ERR_MQUEUE;
1041 mailbox = MALLOC(msgsize);
1043 ERROR("Failed to allocate %ld bytes for mailbox", msgsize);
1044 return WIMLIB_ERR_NOMEM;
1046 *msgsize_ret = msgsize;
1047 *mailbox_ret = mailbox;
1052 unlink_message_queues(struct wimfs_context *ctx)
1054 mq_unlink(ctx->unmount_to_daemon_mq_name);
1055 mq_unlink(ctx->daemon_to_unmount_mq_name);
1058 /* Closes the message queues, which are allocated in static variables */
1060 close_message_queues(struct wimfs_context *ctx)
1062 DEBUG("Closing message queues");
1063 mq_close(ctx->unmount_to_daemon_mq);
1064 ctx->unmount_to_daemon_mq = (mqd_t)(-1);
1065 mq_close(ctx->daemon_to_unmount_mq);
1066 ctx->daemon_to_unmount_mq = (mqd_t)(-1);
1067 unlink_message_queues(ctx);
1071 struct unmount_msg_hdr {
1078 struct msg_unmount_request {
1079 struct unmount_msg_hdr hdr;
1081 u8 want_progress_messages;
1084 struct msg_daemon_info {
1085 struct unmount_msg_hdr hdr;
1090 struct msg_unmount_finished {
1091 struct unmount_msg_hdr hdr;
1095 struct msg_write_streams_progress {
1096 struct unmount_msg_hdr hdr;
1097 union wimlib_progress_info info;
1101 MSG_TYPE_UNMOUNT_REQUEST,
1102 MSG_TYPE_DAEMON_INFO,
1103 MSG_TYPE_WRITE_STREAMS_PROGRESS,
1104 MSG_TYPE_UNMOUNT_FINISHED,
1108 struct msg_handler_context_hdr {
1109 int timeout_seconds;
1112 struct unmount_msg_handler_context {
1113 struct msg_handler_context_hdr hdr;
1117 wimlib_progress_func_t progress_func;
1120 struct daemon_msg_handler_context {
1121 struct msg_handler_context_hdr hdr;
1122 struct wimfs_context *wimfs_ctx;
1126 send_unmount_request_msg(mqd_t mq, int unmount_flags, u8 want_progress_messages)
1128 DEBUG("Sending unmount request msg");
1129 struct msg_unmount_request msg = {
1131 .min_version = WIMLIB_MAKEVERSION(1, 2, 1),
1132 .cur_version = WIMLIB_VERSION_CODE,
1133 .msg_type = MSG_TYPE_UNMOUNT_REQUEST,
1134 .msg_size = sizeof(msg),
1136 .unmount_flags = unmount_flags,
1137 .want_progress_messages = want_progress_messages,
1140 if (mq_send(mq, (void*)&msg, sizeof(msg), 1)) {
1141 ERROR_WITH_ERRNO("Failed to communicate with filesystem daemon");
1142 return WIMLIB_ERR_MQUEUE;
1148 send_daemon_info_msg(mqd_t mq, pid_t pid, int mount_flags)
1150 DEBUG("Sending daemon info msg (pid = %d, mount_flags=%x)",
1153 struct msg_daemon_info msg = {
1155 .min_version = WIMLIB_MAKEVERSION(1, 2, 1),
1156 .cur_version = WIMLIB_VERSION_CODE,
1157 .msg_type = MSG_TYPE_DAEMON_INFO,
1158 .msg_size = sizeof(msg),
1161 .mount_flags = mount_flags,
1163 if (mq_send(mq, (void*)&msg, sizeof(msg), 1)) {
1164 ERROR_WITH_ERRNO("Failed to send daemon info to unmount process");
1165 return WIMLIB_ERR_MQUEUE;
1171 send_unmount_finished_msg(mqd_t mq, int status)
1173 DEBUG("Sending unmount finished msg");
1174 struct msg_unmount_finished msg = {
1176 .min_version = WIMLIB_MAKEVERSION(1, 2, 1),
1177 .cur_version = WIMLIB_VERSION_CODE,
1178 .msg_type = MSG_TYPE_UNMOUNT_FINISHED,
1179 .msg_size = sizeof(msg),
1183 if (mq_send(mq, (void*)&msg, sizeof(msg), 1))
1184 ERROR_WITH_ERRNO("Failed to send status to unmount process");
1188 unmount_progress_func(enum wimlib_progress_msg msg,
1189 const union wimlib_progress_info *info)
1191 if (msg == WIMLIB_PROGRESS_MSG_WRITE_STREAMS) {
1192 struct msg_write_streams_progress msg = {
1194 .min_version = WIMLIB_MAKEVERSION(1, 2, 1),
1195 .cur_version = WIMLIB_VERSION_CODE,
1196 .msg_type = MSG_TYPE_WRITE_STREAMS_PROGRESS,
1197 .msg_size = sizeof(msg),
1201 if (mq_send(wimfs_get_context()->daemon_to_unmount_mq,
1202 (void*)&msg, sizeof(msg), 1))
1204 ERROR_WITH_ERRNO("Failed to send progress information "
1205 "to unmount process");
1212 msg_unmount_request_handler(const void *_msg, void *_handler_ctx)
1214 const struct msg_unmount_request *msg = _msg;
1215 struct daemon_msg_handler_context *handler_ctx = _handler_ctx;
1216 struct wimfs_context *wimfs_ctx;
1220 wimlib_progress_func_t progress_func;
1222 DEBUG("Handling unmount request msg");
1224 wimfs_ctx = handler_ctx->wimfs_ctx;
1225 if (msg->hdr.msg_size < sizeof(*msg)) {
1226 status = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1230 unmount_flags = msg->unmount_flags;
1231 if (msg->want_progress_messages)
1232 progress_func = unmount_progress_func;
1234 progress_func = NULL;
1236 ret = send_daemon_info_msg(wimfs_ctx->daemon_to_unmount_mq, getpid(),
1237 wimfs_ctx->mount_flags);
1243 if (wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1244 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_COMMIT) {
1245 int write_flags = 0;
1246 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY)
1247 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1248 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_REBUILD)
1249 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1250 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_RECOMPRESS)
1251 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
1252 status = rebuild_wim(wimfs_ctx, write_flags,
1256 DEBUG("Read-only mount");
1261 if (wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1262 ret = delete_staging_dir(wimfs_ctx);
1264 ERROR("Failed to delete the staging directory");
1269 wimfs_ctx->status = status;
1270 wimfs_ctx->have_status = true;
1271 return MSG_BREAK_LOOP;
1275 msg_daemon_info_handler(const void *_msg, void *_handler_ctx)
1277 const struct msg_daemon_info *msg = _msg;
1278 struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1280 DEBUG("Handling daemon info msg");
1281 if (msg->hdr.msg_size < sizeof(*msg))
1282 return WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1283 handler_ctx->daemon_pid = msg->daemon_pid;
1284 handler_ctx->mount_flags = msg->mount_flags;
1285 handler_ctx->hdr.timeout_seconds = 1;
1286 DEBUG("pid of daemon is %d; mount flags were %#x",
1287 handler_ctx->daemon_pid,
1288 handler_ctx->mount_flags);
1293 msg_write_streams_progress_handler(const void *_msg, void *_handler_ctx)
1295 const struct msg_write_streams_progress *msg = _msg;
1296 struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1298 if (msg->hdr.msg_size < sizeof(*msg))
1299 return WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1300 if (handler_ctx->progress_func) {
1301 handler_ctx->progress_func(WIMLIB_PROGRESS_MSG_WRITE_STREAMS,
1308 msg_unmount_finished_handler(const void *_msg, void *_handler_ctx)
1310 const struct msg_unmount_finished *msg = _msg;
1311 struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1313 DEBUG("Handling unmount finished message");
1314 if (msg->hdr.msg_size < sizeof(*msg))
1315 return WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1316 handler_ctx->status = msg->status;
1317 DEBUG("status is %d", handler_ctx->status);
1318 return MSG_BREAK_LOOP;
1322 unmount_timed_out_cb(void *_handler_ctx)
1324 struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1326 if (handler_ctx->daemon_pid == 0) {
1329 kill(handler_ctx->daemon_pid, 0);
1330 if (errno == ESRCH) {
1333 DEBUG("Filesystem daemon is still alive... "
1334 "Waiting another %d seconds\n",
1335 handler_ctx->hdr.timeout_seconds);
1340 ERROR("The filesystem daemon has crashed! Changes to the "
1341 "WIM may not have been commited.");
1342 return WIMLIB_ERR_FILESYSTEM_DAEMON_CRASHED;
1346 daemon_timed_out_cb(void *_handler_ctx)
1348 ERROR("Timed out waiting for unmount request! "
1349 "Changes to the mounted WIM will not be committed.");
1350 return WIMLIB_ERR_TIMEOUT;
1353 typedef int (*msg_handler_t)(const void *_msg, void *_handler_ctx);
1355 struct msg_handler_callbacks {
1356 int (*timed_out)(void * _handler_ctx);
1357 msg_handler_t msg_handlers[MSG_TYPE_MAX];
1360 static const struct msg_handler_callbacks unmount_msg_handler_callbacks = {
1361 .timed_out = unmount_timed_out_cb,
1363 [MSG_TYPE_DAEMON_INFO] = msg_daemon_info_handler,
1364 [MSG_TYPE_WRITE_STREAMS_PROGRESS] = msg_write_streams_progress_handler,
1365 [MSG_TYPE_UNMOUNT_FINISHED] = msg_unmount_finished_handler,
1369 static const struct msg_handler_callbacks daemon_msg_handler_callbacks = {
1370 .timed_out = daemon_timed_out_cb,
1372 [MSG_TYPE_UNMOUNT_REQUEST] = msg_unmount_request_handler,
1377 receive_message(mqd_t mq,
1378 struct msg_handler_context_hdr *handler_ctx,
1379 const msg_handler_t msg_handlers[],
1380 long mailbox_size, void *mailbox)
1383 struct timespec timeout;
1384 ssize_t bytes_received;
1385 struct unmount_msg_hdr *hdr;
1388 gettimeofday(&now, NULL);
1389 timeout.tv_sec = now.tv_sec + handler_ctx->timeout_seconds;
1390 timeout.tv_nsec = now.tv_usec * 1000;
1392 bytes_received = mq_timedreceive(mq, mailbox,
1393 mailbox_size, NULL, &timeout);
1395 if (bytes_received == -1) {
1396 if (errno == ETIMEDOUT) {
1397 ret = WIMLIB_ERR_TIMEOUT;
1399 ERROR_WITH_ERRNO("mq_timedreceive()");
1400 ret = WIMLIB_ERR_MQUEUE;
1402 } else if (bytes_received < sizeof(*hdr) ||
1403 bytes_received != hdr->msg_size) {
1404 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1405 } else if (WIMLIB_VERSION_CODE < hdr->min_version) {
1406 /*ERROR("Cannot understand the received message. "*/
1407 /*"Please upgrade wimlib to at least v%d.%d.%d",*/
1408 /*WIMLIB_GET_MAJOR_VERSION(hdr->min_version),*/
1409 /*WIMLIB_GET_MINOR_VERSION(hdr->min_version),*/
1410 /*WIMLIB_GET_PATCH_VERSION(hdr->min_version));*/
1411 ret = MSG_VERSION_TOO_HIGH;
1412 } else if (hdr->msg_type >= MSG_TYPE_MAX) {
1413 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1414 } else if (msg_handlers[hdr->msg_type] == NULL) {
1415 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1417 ret = msg_handlers[hdr->msg_type](mailbox, handler_ctx);
1423 message_loop(mqd_t mq,
1424 const struct msg_handler_callbacks *callbacks,
1425 struct msg_handler_context_hdr *handler_ctx)
1427 static const size_t MAX_MSG_SIZE = 512;
1432 DEBUG("Entering message loop");
1434 ret = get_mailbox(mq, MAX_MSG_SIZE, &msgsize, &mailbox);
1438 ret = receive_message(mq, handler_ctx,
1439 callbacks->msg_handlers,
1441 if (ret == 0 || ret == MSG_VERSION_TOO_HIGH) {
1443 } else if (ret == MSG_BREAK_LOOP) {
1446 } else if (ret == WIMLIB_ERR_TIMEOUT) {
1447 if (callbacks->timed_out)
1448 ret = callbacks->timed_out(handler_ctx);
1454 ERROR_WITH_ERRNO("Error communicating with "
1455 "filesystem daemon");
1460 DEBUG("Exiting message loop");
1464 /* Execute `fusermount -u', which is installed setuid root, to unmount the WIM.
1466 * FUSE does not yet implement synchronous unmounts. This means that fusermount
1467 * -u will return before the filesystem daemon returns from wimfs_destroy().
1468 * This is partly what we want, because we need to send a message from this
1469 * process to the filesystem daemon telling whether --commit was specified or
1470 * not. However, after that, the unmount process must wait for the filesystem
1471 * daemon to finish writing the WIM file.
1474 execute_fusermount(const char *dir)
1482 ERROR_WITH_ERRNO("Failed to fork()");
1483 return WIMLIB_ERR_FORK;
1487 execlp("fusermount", "fusermount", "-u", dir, NULL);
1488 ERROR_WITH_ERRNO("Failed to execute `fusermount'");
1489 exit(WIMLIB_ERR_FUSERMOUNT);
1493 ret = waitpid(pid, &status, 0);
1495 ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
1497 return WIMLIB_ERR_FUSERMOUNT;
1500 if (!WIFEXITED(status)) {
1501 ERROR("'fusermount' did not terminate normally!");
1502 return WIMLIB_ERR_FUSERMOUNT;
1505 status = WEXITSTATUS(status);
1510 if (status != WIMLIB_ERR_FUSERMOUNT)
1511 return WIMLIB_ERR_FUSERMOUNT;
1513 /* Try again, but with the `umount' program. This is required on other
1514 * FUSE implementations such as FreeBSD's that do not have a
1515 * `fusermount' program. */
1516 ERROR("Falling back to 'umount'. Note: you may need to be "
1517 "root for this to work");
1520 ERROR_WITH_ERRNO("Failed to fork()");
1521 return WIMLIB_ERR_FORK;
1525 execlp("umount", "umount", dir, NULL);
1526 ERROR_WITH_ERRNO("Failed to execute `umount'");
1527 exit(WIMLIB_ERR_FUSERMOUNT);
1531 ret = waitpid(pid, &status, 0);
1533 ERROR_WITH_ERRNO("Failed to wait for `umount' process to "
1535 return WIMLIB_ERR_FUSERMOUNT;
1538 ERROR("`umount' did not successfully complete");
1539 return WIMLIB_ERR_FUSERMOUNT;
1545 wimfs_chmod(const char *path, mode_t mask)
1547 struct wim_dentry *dentry;
1548 struct wimfs_context *ctx = wimfs_get_context();
1551 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA))
1554 ret = lookup_resource(ctx->wim, path, LOOKUP_FLAG_DIRECTORY_OK,
1555 &dentry, NULL, NULL);
1559 ret = inode_set_unix_data(dentry->d_inode, ctx->default_uid,
1560 ctx->default_gid, mask,
1561 ctx->wim->lookup_table, UNIX_DATA_MODE);
1562 return ret ? -ENOMEM : 0;
1566 wimfs_chown(const char *path, uid_t uid, gid_t gid)
1568 struct wim_dentry *dentry;
1569 struct wimfs_context *ctx = wimfs_get_context();
1572 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA))
1575 ret = lookup_resource(ctx->wim, path, LOOKUP_FLAG_DIRECTORY_OK,
1576 &dentry, NULL, NULL);
1580 ret = inode_set_unix_data(dentry->d_inode, uid, gid,
1581 inode_default_unix_mode(dentry->d_inode),
1582 ctx->wim->lookup_table,
1583 UNIX_DATA_UID | UNIX_DATA_GID);
1584 return ret ? -ENOMEM : 0;
1587 /* Called when the filesystem is unmounted. */
1589 wimfs_destroy(void *p)
1591 struct wimfs_context *wimfs_ctx = wimfs_get_context();
1592 if (open_message_queues(wimfs_ctx, true) == 0) {
1593 struct daemon_msg_handler_context handler_ctx = {
1595 .timeout_seconds = 5,
1597 .wimfs_ctx = wimfs_ctx,
1599 message_loop(wimfs_ctx->unmount_to_daemon_mq,
1600 &daemon_msg_handler_callbacks,
1606 wimfs_fgetattr(const char *path, struct stat *stbuf,
1607 struct fuse_file_info *fi)
1609 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1610 return inode_to_stbuf(fd->f_inode, fd->f_lte, stbuf);
1614 wimfs_ftruncate(const char *path, off_t size, struct fuse_file_info *fi)
1616 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1617 int ret = ftruncate(fd->staging_fd, size);
1620 touch_inode(fd->f_inode);
1621 fd->f_lte->resource_entry.original_size = size;
1626 * Fills in a `struct stat' that corresponds to a file or directory in the WIM.
1629 wimfs_getattr(const char *path, struct stat *stbuf)
1631 struct wim_dentry *dentry;
1632 struct wim_lookup_table_entry *lte;
1634 struct wimfs_context *ctx = wimfs_get_context();
1636 ret = lookup_resource(ctx->wim, path,
1637 get_lookup_flags(ctx) | LOOKUP_FLAG_DIRECTORY_OK,
1638 &dentry, <e, NULL);
1641 return inode_to_stbuf(dentry->d_inode, lte, stbuf);
1645 /* Read an alternate data stream through the XATTR interface, or get its size */
1647 wimfs_getxattr(const char *path, const char *name, char *value,
1651 struct wim_inode *inode;
1652 struct wim_ads_entry *ads_entry;
1654 struct wim_lookup_table_entry *lte;
1655 struct wimfs_context *ctx = wimfs_get_context();
1657 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1660 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1664 inode = wim_pathname_to_inode(ctx->wim, path);
1668 ads_entry = inode_get_ads_entry(inode, name, NULL);
1672 lte = ads_entry->lte;
1673 res_size = wim_resource_size(lte);
1678 if (res_size > size)
1681 ret = read_full_resource_into_buf(lte, value, true);
1689 /* Create a hard link */
1691 wimfs_link(const char *to, const char *from)
1693 struct wim_dentry *from_dentry, *from_dentry_parent;
1694 const char *link_name;
1695 struct wim_inode *inode;
1696 WIMStruct *w = wimfs_get_WIMStruct();
1699 inode = wim_pathname_to_inode(w, to);
1703 if (inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
1704 FILE_ATTRIBUTE_REPARSE_POINT))
1707 from_dentry_parent = get_parent_dentry(w, from);
1708 if (!from_dentry_parent)
1710 if (!dentry_is_directory(from_dentry_parent))
1713 link_name = path_basename(from);
1714 if (get_dentry_child_with_name(from_dentry_parent, link_name))
1717 ret = new_dentry(link_name, &from_dentry);
1722 inode_ref_streams(inode);
1723 from_dentry->d_inode = inode;
1724 inode_add_dentry(from_dentry, inode);
1725 dentry_add_child(from_dentry_parent, from_dentry);
1731 wimfs_listxattr(const char *path, char *list, size_t size)
1734 struct wim_inode *inode;
1735 struct wimfs_context *ctx = wimfs_get_context();
1738 bool size_only = (size == 0);
1740 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1743 /* List alternate data streams, or get the list size */
1745 inode = wim_pathname_to_inode(ctx->wim, path);
1750 for (i = 0; i < inode->i_num_ads; i++) {
1751 char *stream_name_mbs;
1752 size_t stream_name_mbs_nbytes;
1755 ret = utf16le_to_tstr(inode->i_ads_entries[i].stream_name,
1756 inode->i_ads_entries[i].stream_name_nbytes,
1758 &stream_name_mbs_nbytes);
1762 needed_size = stream_name_mbs_nbytes + 6;
1764 if (needed_size > size) {
1765 FREE(stream_name_mbs);
1768 sprintf(p, "user.%s", stream_name_mbs);
1769 size -= needed_size;
1772 FREE(stream_name_mbs);
1779 /* Create a directory in the WIM image. */
1781 wimfs_mkdir(const char *path, mode_t mode)
1783 return create_dentry(fuse_get_context(), path, mode | S_IFDIR,
1784 FILE_ATTRIBUTE_DIRECTORY, NULL);
1787 /* Create a regular file or alternate data stream in the WIM image. */
1789 wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
1791 const char *stream_name;
1792 struct fuse_context *fuse_ctx = fuse_get_context();
1793 struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
1798 if ((wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
1799 && (stream_name = path_stream_name(path))) {
1800 /* Make an alternate data stream */
1801 struct wim_ads_entry *new_entry;
1802 struct wim_inode *inode;
1804 char *p = (char*)stream_name - 1;
1805 wimlib_assert(*p == ':');
1808 inode = wim_pathname_to_inode(wimfs_ctx->wim, path);
1811 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT)
1813 if (inode_get_ads_entry(inode, stream_name, NULL))
1815 new_entry = inode_add_ads(inode, stream_name);
1820 /* Make a normal file (not an alternate data stream) */
1821 return create_dentry(fuse_ctx, path, mode | S_IFREG,
1822 FILE_ATTRIBUTE_NORMAL, NULL);
1828 wimfs_open(const char *path, struct fuse_file_info *fi)
1830 struct wim_dentry *dentry;
1831 struct wim_lookup_table_entry *lte;
1833 struct wimfs_fd *fd;
1834 struct wim_inode *inode;
1837 struct wimfs_context *ctx = wimfs_get_context();
1838 struct wim_lookup_table_entry **back_ptr;
1840 ret = lookup_resource(ctx->wim, path, get_lookup_flags(ctx),
1841 &dentry, <e, &stream_idx);
1845 inode = dentry->d_inode;
1847 if (stream_idx == 0) {
1849 back_ptr = &inode->i_lte;
1851 stream_id = inode->i_ads_entries[stream_idx - 1].stream_id;
1852 back_ptr = &inode->i_ads_entries[stream_idx - 1].lte;
1855 /* The file resource may be in the staging directory (read-write mounts
1856 * only) or in the WIM. If it's in the staging directory, we need to
1857 * open a native file descriptor for the corresponding file. Otherwise,
1858 * we can read the file resource directly from the WIM file if we are
1859 * opening it read-only, but we need to extract the resource to the
1860 * staging directory if we are opening it writable. */
1862 if (flags_writable(fi->flags) &&
1863 (!lte || lte->resource_location != RESOURCE_IN_STAGING_FILE)) {
1864 u64 size = (lte) ? wim_resource_size(lte) : 0;
1865 ret = extract_resource_to_staging_dir(inode, stream_id,
1872 ret = alloc_wimfs_fd(inode, stream_id, lte, &fd,
1873 wimfs_ctx_readonly(ctx));
1877 if (lte && lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1878 fd->staging_fd = open(lte->staging_file_name, fi->flags);
1879 if (fd->staging_fd == -1) {
1880 int errno_save = errno;
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 *w = ctx->wim;
1899 inode = wim_pathname_to_inode(w, path);
1902 if (!inode_is_directory(inode))
1904 ret = alloc_wimfs_fd(inode, 0, NULL, &fd, wimfs_ctx_readonly(ctx));
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 res_size = wim_resource_size(fd->f_lte);
1932 if (offset > res_size)
1935 size = min(size, res_size - offset);
1939 switch (fd->f_lte->resource_location) {
1940 case RESOURCE_IN_STAGING_FILE:
1941 ret = full_pread(fd->staging_fd, buf, size, offset);
1945 case RESOURCE_IN_WIM:
1946 if (read_partial_wim_resource_into_buf(fd->f_lte, size,
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))
2028 ret = inode_readlink(inode, buf, buf_len, ctx->wim, true);
2036 wimfs_release(const char *path, struct fuse_file_info *fi)
2038 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
2039 return close_wimfs_fd(fd);
2042 /* Close a directory */
2044 wimfs_releasedir(const char *path, struct fuse_file_info *fi)
2046 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
2047 return close_wimfs_fd(fd);
2051 /* Remove an alternate data stream through the XATTR interface */
2053 wimfs_removexattr(const char *path, const char *name)
2055 struct wim_inode *inode;
2056 struct wim_ads_entry *ads_entry;
2058 struct wimfs_context *ctx = wimfs_get_context();
2060 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
2063 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
2067 inode = wim_pathname_to_inode(ctx->wim, path);
2071 ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
2074 inode_remove_ads(inode, ads_idx, ctx->wim->lookup_table);
2079 /* Renames a file or directory. See rename (3) */
2081 wimfs_rename(const char *from, const char *to)
2083 struct wim_dentry *src;
2084 struct wim_dentry *dst;
2085 struct wim_dentry *parent_of_dst;
2086 WIMStruct *w = wimfs_get_WIMStruct();
2089 /* This rename() implementation currently only supports actual files
2090 * (not alternate data streams) */
2092 src = get_dentry(w, from);
2096 dst = get_dentry(w, to);
2099 /* Destination file exists */
2101 if (src == dst) /* Same file */
2104 if (!dentry_is_directory(src)) {
2105 /* Cannot rename non-directory to directory. */
2106 if (dentry_is_directory(dst))
2109 /* Cannot rename directory to a non-directory or a non-empty
2111 if (!dentry_is_directory(dst))
2113 if (inode_has_children(dst->d_inode))
2116 parent_of_dst = dst->parent;
2118 /* Destination does not exist */
2119 parent_of_dst = get_parent_dentry(w, to);
2123 if (!dentry_is_directory(parent_of_dst))
2127 ret = set_dentry_name(src, path_basename(to));
2131 remove_dentry(dst, w->lookup_table);
2133 dentry_add_child(parent_of_dst, src);
2137 /* Remove a directory */
2139 wimfs_rmdir(const char *path)
2141 struct wim_dentry *dentry;
2142 WIMStruct *w = wimfs_get_WIMStruct();
2144 dentry = get_dentry(w, path);
2148 if (!dentry_is_directory(dentry))
2151 if (dentry_has_children(dentry))
2154 remove_dentry(dentry, w->lookup_table);
2159 /* Write an alternate data stream through the XATTR interface */
2161 wimfs_setxattr(const char *path, const char *name,
2162 const char *value, size_t size, int flags)
2164 struct wim_ads_entry *existing_ads_entry;
2165 struct wim_inode *inode;
2167 struct wimfs_context *ctx = wimfs_get_context();
2170 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
2173 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
2177 inode = wim_pathname_to_inode(ctx->wim, path);
2181 existing_ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
2182 if (existing_ads_entry) {
2183 if (flags & XATTR_CREATE)
2186 if (flags & XATTR_REPLACE)
2190 ret = inode_add_ads_with_data(inode, name, value,
2191 size, ctx->wim->lookup_table);
2193 if (existing_ads_entry)
2194 inode_remove_ads(inode, ads_idx, ctx->wim->lookup_table);
2203 wimfs_symlink(const char *to, const char *from)
2205 struct fuse_context *fuse_ctx = fuse_get_context();
2206 struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
2207 struct wim_dentry *dentry;
2210 ret = create_dentry(fuse_ctx, from, S_IFLNK | 0777,
2211 FILE_ATTRIBUTE_REPARSE_POINT, &dentry);
2213 dentry->d_inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
2214 if (inode_set_symlink(dentry->d_inode, to,
2215 wimfs_ctx->wim->lookup_table, NULL))
2217 remove_dentry(dentry, wimfs_ctx->wim->lookup_table);
2225 /* Reduce the size of a file */
2227 wimfs_truncate(const char *path, off_t size)
2229 struct wim_dentry *dentry;
2230 struct wim_lookup_table_entry *lte;
2234 struct wim_inode *inode;
2235 struct wimfs_context *ctx = wimfs_get_context();
2237 ret = lookup_resource(ctx->wim, path, get_lookup_flags(ctx),
2238 &dentry, <e, &stream_idx);
2243 if (lte == NULL && size == 0)
2246 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
2247 ret = truncate(lte->staging_file_name, size);
2251 lte->resource_entry.original_size = size;
2253 /* File in WIM. Extract it to the staging directory, but only
2254 * the first @size bytes of it. */
2255 struct wim_lookup_table_entry **back_ptr;
2257 inode = dentry->d_inode;
2258 if (stream_idx == 0) {
2260 back_ptr = &inode->i_lte;
2262 stream_id = inode->i_ads_entries[stream_idx - 1].stream_id;
2263 back_ptr = &inode->i_ads_entries[stream_idx - 1].lte;
2265 ret = extract_resource_to_staging_dir(inode, stream_id,
2272 /* Unlink a non-directory or alternate data stream */
2274 wimfs_unlink(const char *path)
2276 struct wim_dentry *dentry;
2277 struct wim_lookup_table_entry *lte;
2280 struct wimfs_context *ctx = wimfs_get_context();
2282 ret = lookup_resource(ctx->wim, path, get_lookup_flags(ctx),
2283 &dentry, <e, &stream_idx);
2288 if (stream_idx == 0)
2289 remove_dentry(dentry, ctx->wim->lookup_table);
2291 inode_remove_ads(dentry->d_inode, stream_idx - 1,
2292 ctx->wim->lookup_table);
2296 #ifdef HAVE_UTIMENSAT
2298 * Change the timestamp on a file dentry.
2300 * Note that alternate data streams do not have their own timestamps.
2303 wimfs_utimens(const char *path, const struct timespec tv[2])
2305 struct wim_dentry *dentry;
2306 struct wim_inode *inode;
2307 WIMStruct *w = wimfs_get_WIMStruct();
2309 dentry = get_dentry(w, path);
2312 inode = dentry->d_inode;
2314 if (tv[0].tv_nsec != UTIME_OMIT) {
2315 if (tv[0].tv_nsec == UTIME_NOW)
2316 inode->i_last_access_time = get_wim_timestamp();
2318 inode->i_last_access_time = timespec_to_wim_timestamp(tv[0]);
2320 if (tv[1].tv_nsec != UTIME_OMIT) {
2321 if (tv[1].tv_nsec == UTIME_NOW)
2322 inode->i_last_write_time = get_wim_timestamp();
2324 inode->i_last_write_time = timespec_to_wim_timestamp(tv[1]);
2328 #else /* HAVE_UTIMENSAT */
2330 wimfs_utime(const char *path, struct utimbuf *times)
2332 struct wim_dentry *dentry;
2333 struct wim_inode *inode;
2334 WIMStruct *w = wimfs_get_WIMStruct();
2336 dentry = get_dentry(w, path);
2339 inode = dentry->d_inode;
2341 inode->i_last_write_time = unix_timestamp_to_wim(times->modtime);
2342 inode->i_last_access_time = unix_timestamp_to_wim(times->actime);
2345 #endif /* !HAVE_UTIMENSAT */
2347 /* Writes to a file in the WIM filesystem.
2348 * It may be an alternate data stream, but here we don't even notice because we
2349 * just get a lookup table entry. */
2351 wimfs_write(const char *path, const char *buf, size_t size,
2352 off_t offset, struct fuse_file_info *fi)
2354 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
2360 wimlib_assert(fd->f_lte != NULL);
2361 wimlib_assert(fd->f_lte->staging_file_name != NULL);
2362 wimlib_assert(fd->staging_fd != -1);
2363 wimlib_assert(fd->f_inode != NULL);
2365 /* Write the data. */
2366 ret = full_pwrite(fd->staging_fd, buf, size, offset);
2370 /* Update file size */
2371 if (offset + size > fd->f_lte->resource_entry.original_size) {
2372 DEBUG("Update file size %"PRIu64 " => %"PRIu64"",
2373 fd->f_lte->resource_entry.original_size,
2375 fd->f_lte->resource_entry.original_size = offset + size;
2378 /* Update timestamps */
2379 touch_inode(fd->f_inode);
2383 static struct fuse_operations wimfs_operations = {
2384 .chmod = wimfs_chmod,
2385 .chown = wimfs_chown,
2386 .destroy = wimfs_destroy,
2387 .fgetattr = wimfs_fgetattr,
2388 .ftruncate = wimfs_ftruncate,
2389 .getattr = wimfs_getattr,
2391 .getxattr = wimfs_getxattr,
2395 .listxattr = wimfs_listxattr,
2397 .mkdir = wimfs_mkdir,
2398 .mknod = wimfs_mknod,
2400 .opendir = wimfs_opendir,
2402 .readdir = wimfs_readdir,
2403 .readlink = wimfs_readlink,
2404 .release = wimfs_release,
2405 .releasedir = wimfs_releasedir,
2407 .removexattr = wimfs_removexattr,
2409 .rename = wimfs_rename,
2410 .rmdir = wimfs_rmdir,
2412 .setxattr = wimfs_setxattr,
2414 .symlink = wimfs_symlink,
2415 .truncate = wimfs_truncate,
2416 .unlink = wimfs_unlink,
2417 #ifdef HAVE_UTIMENSAT
2418 .utimens = wimfs_utimens,
2420 .utime = wimfs_utime,
2422 .write = wimfs_write,
2424 /* wimfs keeps file descriptor structures (struct wimfs_fd), so there is
2425 * no need to have the file path provided on operations such as read()
2426 * where only the file descriptor is needed. */
2427 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 8)
2428 .flag_nullpath_ok = 1,
2430 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 9)
2432 .flag_utime_omit_ok = 1,
2437 /* Mounts an image from a WIM file. */
2439 wimlib_mount_image(WIMStruct *wim, int image, const char *dir,
2440 int mount_flags, WIMStruct **additional_swms,
2441 unsigned num_additional_swms,
2442 const char *staging_dir)
2448 struct wim_lookup_table *joined_tab, *wim_tab_save;
2449 struct wim_image_metadata *imd;
2450 struct wimfs_context ctx;
2451 struct wim_inode *inode;
2453 DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
2454 wim, image, dir, mount_flags);
2457 return WIMLIB_ERR_INVALID_PARAM;
2459 ret = verify_swm_set(wim, additional_swms, num_additional_swms);
2463 if ((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) && (wim->hdr.total_parts != 1)) {
2464 ERROR("Cannot mount a split WIM read-write");
2465 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
2468 if (num_additional_swms) {
2469 ret = new_joined_lookup_table(wim, additional_swms,
2470 num_additional_swms,
2474 wim_tab_save = wim->lookup_table;
2475 wim->lookup_table = joined_tab;
2478 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2479 ret = wim_run_full_verifications(wim);
2484 ret = wim_checksum_unhashed_streams(wim);
2488 ret = select_wim_image(wim, image);
2492 DEBUG("Selected image %d", image);
2494 imd = wim_get_current_image_metadata(wim);
2496 if (imd->refcnt != 1) {
2497 ERROR("Cannot mount image that was just exported with "
2498 "wimlib_export_image()");
2499 ret = WIMLIB_ERR_INVALID_PARAM;
2503 if (imd->modified) {
2504 ERROR("Cannot mount image that was added "
2505 "with wimlib_add_image()");
2506 ret = WIMLIB_ERR_INVALID_PARAM;
2510 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2511 ret = lock_wim(wim, wim->fp);
2516 /* Use default stream interface if one was not specified */
2517 if (!(mount_flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
2518 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
2519 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
2520 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
2522 DEBUG("Initializing struct wimfs_context");
2523 init_wimfs_context(&ctx);
2525 ctx.mount_flags = mount_flags;
2526 ctx.image_inode_list = &imd->inode_list;
2527 ctx.default_uid = getuid();
2528 ctx.default_gid = getgid();
2529 wimlib_assert(list_empty(&imd->unhashed_streams));
2530 ctx.wim->lookup_table->unhashed_streams = &imd->unhashed_streams;
2531 if (mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
2532 ctx.default_lookup_flags = LOOKUP_FLAG_ADS_OK;
2534 DEBUG("Unlinking message queues in case they already exist");
2535 ret = set_message_queue_names(&ctx, dir);
2538 unlink_message_queues(&ctx);
2540 DEBUG("Preparing arguments to fuse_main()");
2542 dir_copy = STRDUP(dir);
2544 goto out_free_message_queue_names;
2547 argv[argc++] = "imagex";
2548 argv[argc++] = dir_copy;
2550 /* disable multi-threaded operation for read-write mounts */
2551 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2552 argv[argc++] = "-s";
2554 if (mount_flags & WIMLIB_MOUNT_FLAG_DEBUG)
2555 argv[argc++] = "-d";
2558 * We provide the use_ino option to the FUSE mount because we are going
2559 * to assign inode numbers ourselves. */
2560 char optstring[256] =
2564 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 8)
2567 ",default_permissions"
2569 argv[argc++] = "-o";
2570 argv[argc++] = optstring;
2571 if ((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
2572 /* Read-write mount. Make the staging directory */
2573 ret = make_staging_dir(&ctx, staging_dir);
2575 goto out_free_dir_copy;
2577 /* Read-only mount */
2578 strcat(optstring, ",ro");
2580 if (mount_flags & WIMLIB_MOUNT_FLAG_ALLOW_OTHER)
2581 strcat(optstring, ",allow_other");
2587 DEBUG("FUSE command line (argc = %d): ", argc);
2588 for (i = 0; i < argc; i++) {
2589 fputs(argv[i], stdout);
2597 /* Mark dentry tree as modified if read-write mount. */
2598 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2601 /* Resolve the lookup table entries for every inode in the image, and
2602 * assign inode numbers */
2603 DEBUG("Resolving lookup table entries and assigning inode numbers");
2605 image_for_each_inode(inode, imd) {
2606 inode_resolve_ltes(inode, wim->lookup_table);
2607 inode->i_ino = ctx.next_ino++;
2609 DEBUG("(next_ino = %"PRIu64")", ctx.next_ino);
2611 DEBUG("Calling fuse_main()");
2613 ret = fuse_main(argc, argv, &wimfs_operations, &ctx);
2615 DEBUG("Returned from fuse_main() (ret = %d)", ret);
2618 ret = WIMLIB_ERR_FUSE;
2620 if (ctx.have_status)
2623 ret = WIMLIB_ERR_TIMEOUT;
2625 if (ctx.daemon_to_unmount_mq != (mqd_t)(-1)) {
2626 send_unmount_finished_msg(ctx.daemon_to_unmount_mq, ret);
2627 close_message_queues(&ctx);
2630 /* Try to delete the staging directory if a deletion wasn't yet
2631 * attempted due to an earlier error */
2632 if (ctx.staging_dir_name)
2633 delete_staging_dir(&ctx);
2637 wim->wim_locked = 0;
2638 out_free_message_queue_names:
2639 free_message_queue_names(&ctx);
2641 if (num_additional_swms) {
2642 free_lookup_table(wim->lookup_table);
2643 wim->lookup_table = wim_tab_save;
2649 * Unmounts the WIM file that was previously mounted on @dir by using
2650 * wimlib_mount_image().
2653 wimlib_unmount_image(const char *dir, int unmount_flags,
2654 wimlib_progress_func_t progress_func)
2657 struct wimfs_context wimfs_ctx;
2659 init_wimfs_context(&wimfs_ctx);
2661 ret = set_message_queue_names(&wimfs_ctx, dir);
2665 ret = open_message_queues(&wimfs_ctx, false);
2667 goto out_free_message_queue_names;
2669 ret = send_unmount_request_msg(wimfs_ctx.unmount_to_daemon_mq,
2671 progress_func != NULL);
2673 goto out_close_message_queues;
2675 ret = execute_fusermount(dir);
2677 goto out_close_message_queues;
2679 struct unmount_msg_handler_context handler_ctx = {
2681 .timeout_seconds = 5,
2684 .progress_func = progress_func,
2687 ret = message_loop(wimfs_ctx.daemon_to_unmount_mq,
2688 &unmount_msg_handler_callbacks,
2691 ret = handler_ctx.status;
2692 out_close_message_queues:
2693 close_message_queues(&wimfs_ctx);
2694 out_free_message_queue_names:
2695 free_message_queue_names(&wimfs_ctx);
2700 #else /* WITH_FUSE */
2704 mount_unsupported_error()
2706 #if defined(__WIN32__)
2707 ERROR("Sorry-- Mounting WIM images is not supported on Windows!");
2709 ERROR("wimlib was compiled with --without-fuse, which disables support "
2710 "for mounting WIMs.");
2712 return WIMLIB_ERR_UNSUPPORTED;
2716 wimlib_unmount_image(const tchar *dir, int unmount_flags,
2717 wimlib_progress_func_t progress_func)
2719 return mount_unsupported_error();
2723 wimlib_mount_image(WIMStruct *wim, int image, const tchar *dir,
2724 int mount_flags, WIMStruct **additional_swms,
2725 unsigned num_additional_swms,
2726 const tchar *staging_dir)
2728 return mount_unsupported_error();
2731 #endif /* !WITH_FUSE */