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 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 #include "lookup_table.h"
36 #include "buffer_io.h"
37 #include "timestamp.h"
42 #define FUSE_USE_VERSION 26
53 #include <attr/xattr.h>
56 #define MSG_VERSION_TOO_HIGH -1
57 #define MSG_BREAK_LOOP -2
59 /* File descriptor to a file open on the WIM filesystem. */
61 struct wim_inode *f_inode;
62 struct wim_lookup_table_entry *f_lte;
68 struct wimfs_context {
69 /* The WIMStruct for the mounted WIM. */
72 /* Name of the staging directory for a read-write mount. Whenever a new file is
73 * created, it is done so in the staging directory. Furthermore, whenever a
74 * file in the WIM is modified, it is extracted to the staging directory. If
75 * changes are commited when the WIM is unmounted, the file resources are merged
76 * in from the staging directory when writing the new WIM. */
77 char *staging_dir_name;
78 size_t staging_dir_name_len;
80 /* Flags passed to wimlib_mount(). */
83 /* Default flags to use when looking up a WIM dentry (depends on whether
84 * the Windows interface to alternate data streams is being used or
86 int default_lookup_flags;
88 /* Next inode number to be assigned. Note: I didn't bother with a
89 * bitmap of free inode numbers since this isn't even a "real"
90 * filesystem anyway. */
93 /* List of lookup table entries for files in the staging directory */
94 struct list_head staging_list;
96 /* List of inodes in the mounted image */
97 struct hlist_head *image_inode_list;
99 /* Name and message queue descriptors for message queues between the
100 * filesystem daemon process and the unmount process. These are used
101 * when the filesystem is unmounted and the process running
102 * wimlib_unmount_image() (i.e. the `imagex unmount' command) needs to
103 * communicate with the filesystem daemon running fuse_main() (i.e. the
104 * daemon created by the `imagex mount' or `imagex mountrw' commands */
105 char *unmount_to_daemon_mq_name;
106 char *daemon_to_unmount_mq_name;
107 mqd_t unmount_to_daemon_mq;
108 mqd_t daemon_to_unmount_mq;
111 static void init_wimfs_context(struct wimfs_context *ctx)
113 memset(ctx, 0, sizeof(*ctx));
114 ctx->unmount_to_daemon_mq = (mqd_t)-1;
115 ctx->daemon_to_unmount_mq = (mqd_t)-1;
116 INIT_LIST_HEAD(&ctx->staging_list);
119 static inline struct wimfs_context *wimfs_get_context()
121 return (struct wimfs_context*)fuse_get_context()->private_data;
124 static inline WIMStruct *wimfs_get_WIMStruct()
126 return wimfs_get_context()->wim;
129 static inline bool wimfs_ctx_readonly(const struct wimfs_context *ctx)
131 return (ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) == 0;
134 static inline int get_lookup_flags(const struct wimfs_context *ctx)
136 return ctx->default_lookup_flags;
139 /* Returns nonzero if write permission is requested on the file open flags */
140 static inline int flags_writable(int open_flags)
142 return open_flags & (O_RDWR | O_WRONLY);
146 * Allocate a file descriptor for a stream.
148 * @inode: inode containing the stream we're opening
149 * @stream_id: ID of the stream we're opening
150 * @lte: Lookup table entry for the stream (may be NULL)
151 * @fd_ret: Return the allocated file descriptor if successful.
152 * @readonly: True if this is a read-only mount.
154 * Return 0 iff successful or error code if unsuccessful.
156 static int alloc_wimfs_fd(struct wim_inode *inode,
158 struct wim_lookup_table_entry *lte,
159 struct wimfs_fd **fd_ret,
162 static const u16 fds_per_alloc = 8;
163 static const u16 max_fds = 0xffff;
166 pthread_mutex_lock(&inode->i_mutex);
168 DEBUG("Allocating fd for stream ID %u from inode %#"PRIx64" "
169 "(open = %u, allocated = %u)",
170 stream_id, inode->i_ino, inode->i_num_opened_fds,
171 inode->i_num_allocated_fds);
173 if (inode->i_num_opened_fds == inode->i_num_allocated_fds) {
174 struct wimfs_fd **fds;
177 if (inode->i_num_allocated_fds == max_fds) {
181 num_new_fds = min(fds_per_alloc,
182 max_fds - inode->i_num_allocated_fds);
184 fds = REALLOC(inode->i_fds,
185 (inode->i_num_allocated_fds + num_new_fds) *
186 sizeof(inode->i_fds[0]));
191 memset(&fds[inode->i_num_allocated_fds], 0,
192 num_new_fds * sizeof(fds[0]));
194 inode->i_num_allocated_fds += num_new_fds;
196 for (u16 i = 0; ; i++) {
197 if (!inode->i_fds[i]) {
198 struct wimfs_fd *fd = CALLOC(1, sizeof(*fd));
207 fd->stream_id = stream_id;
209 inode->i_fds[i] = fd;
210 inode->i_num_opened_fds++;
211 if (lte && !readonly)
212 lte->num_opened_fds++;
213 DEBUG("Allocated fd (idx = %u)", fd->idx);
219 pthread_mutex_unlock(&inode->i_mutex);
223 static void inode_put_fd(struct wim_inode *inode, struct wimfs_fd *fd)
225 wimlib_assert(inode != NULL);
227 pthread_mutex_lock(&inode->i_mutex);
229 wimlib_assert(fd->f_inode == inode);
230 wimlib_assert(inode->i_num_opened_fds != 0);
231 wimlib_assert(fd->idx < inode->i_num_allocated_fds);
232 wimlib_assert(inode->i_fds[fd->idx] == fd);
234 inode->i_fds[fd->idx] = NULL;
236 if (--inode->i_num_opened_fds == 0 && inode->i_nlink == 0) {
237 pthread_mutex_unlock(&inode->i_mutex);
240 pthread_mutex_unlock(&inode->i_mutex);
244 static int lte_put_fd(struct wim_lookup_table_entry *lte, struct wimfs_fd *fd)
246 wimlib_assert(fd->f_lte == lte);
248 if (!lte) /* Empty stream with no lookup table entry */
251 /* Close staging file descriptor if needed. */
253 if (lte->resource_location == RESOURCE_IN_STAGING_FILE
254 && fd->staging_fd != -1)
256 if (close(fd->staging_fd) != 0) {
257 ERROR_WITH_ERRNO("Failed to close staging file");
261 lte_decrement_num_opened_fds(lte);
265 /* Close a file descriptor. */
266 static int close_wimfs_fd(struct wimfs_fd *fd)
269 DEBUG("Closing fd (ino = %#"PRIx64", opened = %u, allocated = %u)",
270 fd->f_inode->i_ino, fd->f_inode->i_num_opened_fds,
271 fd->f_inode->i_num_allocated_fds);
272 ret = lte_put_fd(fd->f_lte, fd);
276 inode_put_fd(fd->f_inode, fd);
281 * Add a new dentry with a new inode to a WIM image.
283 * @ctx: Context for the mounted WIM image.
284 * @path: Path to create the dentry at.
285 * @dentry_ret: Return the pointer to the dentry if successful.
287 * Returns 0 on success, or negative error number on failure.
289 static int create_dentry(struct wimfs_context *ctx, const char *path,
290 struct wim_dentry **dentry_ret)
292 struct wim_dentry *parent;
293 struct wim_dentry *new;
294 const char *basename;
296 parent = get_parent_dentry(ctx->wim, path);
300 if (!dentry_is_directory(parent))
303 basename = path_basename(path);
304 if (get_dentry_child_with_name(parent, basename))
307 new = new_dentry_with_inode(basename);
311 new->d_inode->i_resolved = 1;
312 new->d_inode->i_ino = ctx->next_ino++;
313 dentry_add_child(parent, new);
314 hlist_add_head(&new->d_inode->i_hlist, ctx->image_inode_list);
319 /* Remove a dentry from a mounted WIM image; i.e. remove an alias for the
320 * corresponding inode.
322 * If there are no remaining references to the inode either through dentries or
323 * open file descriptors, the inode is freed. Otherwise, the inode is not
324 * removed, but the dentry is unlinked and freed.
326 * Either way, all lookup table entries referenced by the inode have their
327 * reference count decremented. If a lookup table entry has no open file
328 * descriptors and no references remaining, it is freed, and the corresponding
329 * staging file is unlinked.
331 static void remove_dentry(struct wim_dentry *dentry,
332 struct wim_lookup_table *lookup_table)
334 struct wim_inode *inode = dentry->d_inode;
335 struct wim_lookup_table_entry *lte;
338 for (i = 0; i <= inode->i_num_ads; i++) {
339 lte = inode_stream_lte_resolved(inode, i);
341 lte_decrement_refcnt(lte, lookup_table);
343 unlink_dentry(dentry);
347 /* Transfers file attributes from a struct wim_inode to a `stat' buffer.
349 * The lookup table entry tells us which stream in the inode we are statting.
350 * For a named data stream, everything returned is the same as the unnamed data
351 * stream except possibly the size and block count. */
352 static int inode_to_stbuf(const struct wim_inode *inode,
353 const struct wim_lookup_table_entry *lte,
356 if (inode_is_symlink(inode))
357 stbuf->st_mode = S_IFLNK | 0777;
358 else if (inode_is_directory(inode))
359 stbuf->st_mode = S_IFDIR | 0755;
361 stbuf->st_mode = S_IFREG | 0755;
363 stbuf->st_ino = (ino_t)inode->i_ino;
364 stbuf->st_nlink = inode->i_nlink;
365 stbuf->st_uid = getuid();
366 stbuf->st_gid = getgid();
369 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
370 struct stat native_stat;
371 if (stat(lte->staging_file_name, &native_stat) != 0) {
372 DEBUG("Failed to stat `%s': %m",
373 lte->staging_file_name);
376 stbuf->st_size = native_stat.st_size;
378 stbuf->st_size = wim_resource_size(lte);
384 stbuf->st_atime = wim_timestamp_to_unix(inode->i_last_access_time);
385 stbuf->st_mtime = wim_timestamp_to_unix(inode->i_last_write_time);
386 stbuf->st_ctime = wim_timestamp_to_unix(inode->i_creation_time);
387 stbuf->st_blocks = (stbuf->st_size + 511) / 512;
391 /* Creates a new staging file and returns its file descriptor opened for
394 * @name_ret: A location into which the a pointer to the newly allocated name of
395 * the staging file is stored.
397 * @ctx: Context for the WIM filesystem; this provides the name of the
400 * On success, returns the file descriptor for the staging file, opened for
401 * writing. On failure, returns -1 and sets errno.
403 static int create_staging_file(char **name_ret, struct wimfs_context *ctx)
411 static const size_t STAGING_FILE_NAME_LEN = 20;
413 name_len = ctx->staging_dir_name_len + 1 + STAGING_FILE_NAME_LEN;
414 name = MALLOC(name_len + 1);
422 memcpy(name, ctx->staging_dir_name, ctx->staging_dir_name_len);
423 name[ctx->staging_dir_name_len] = '/';
424 randomize_char_array_with_alnum(name + ctx->staging_dir_name_len + 1,
425 STAGING_FILE_NAME_LEN);
426 name[name_len] = '\0';
429 /* Just in case, verify that the randomly generated name doesn't name an
430 * existing file, and try again if so */
431 } while (stat(name, &stbuf) == 0);
433 if (errno != ENOENT) /* other error?! */
436 /* doesn't exist--- ok */
438 DEBUG("Creating staging file `%s'", name);
440 fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0600);
452 * Extract a WIM resource to the staging directory.
454 * @inode: Inode that contains the stream we are extracting
456 * @stream_id: Identifier for the stream (it stays constant even if the indices
457 * of the stream entries are changed)
459 * @lte: Pointer to pointer to the lookup table entry for the stream we need to
460 * extract, or NULL if there was no lookup table entry present for the stream
462 * @size: Number of bytes of the stream we want to extract (this supports the
463 * wimfs_truncate() function). It may be more than the actual stream length, in
464 * which case the extra space is filled with zeroes.
466 * @ctx: Context for the WIM filesystem.
468 * Returns 0 on success or a negative error code on failure.
470 static int extract_resource_to_staging_dir(struct wim_inode *inode,
472 struct wim_lookup_table_entry **lte,
474 struct wimfs_context *ctx)
476 char *staging_file_name;
479 struct wim_lookup_table_entry *old_lte, *new_lte;
482 DEBUG("Extracting resource to staging dir: inode %"PRIu64", "
483 "stream id %"PRIu32, inode->i_ino, stream_id);
487 wimlib_assert(old_lte == NULL ||
488 old_lte->resource_location != RESOURCE_IN_STAGING_FILE);
490 /* Create the staging file */
491 fd = create_staging_file(&staging_file_name, ctx);
495 /* Extract the stream to the staging file (possibly truncated) */
497 extract_size = min(wim_resource_size(old_lte), size);
498 ret = extract_wim_resource_to_fd(old_lte, fd, extract_size);
504 /* In the case of truncate() to more than the file length, extend the
505 * file with zeroes by calling ftruncate() on the underlying staging
507 if (ret == 0 && size > extract_size)
508 ret = ftruncate(fd, size);
510 /* Close the staging file descriptor and check for errors. If there's
511 * an error, unlink the staging file. */
512 if (ret != 0 || close(fd) != 0) {
518 goto out_delete_staging_file;
521 /* Now deal with the lookup table entries. We may be able to re-use the
522 * existing entry, but we may have to create a new one instead. */
524 if (old_lte && inode->i_nlink == old_lte->refcnt) {
525 /* The reference count of the existing lookup table entry is the
526 * same as the link count of the inode that contains the stream
527 * we're opening. Therefore, ALL the references to the lookup
528 * table entry correspond to the stream we're trying to extract,
529 * so the lookup table entry can be re-used. */
530 DEBUG("Re-using lookup table entry");
531 lookup_table_unlink(ctx->wim->lookup_table, old_lte);
535 /* There's an existing lookup table entry, but its
536 * reference count is greater than the link count for
537 * the inode containing a stream we're opening.
538 * Therefore, we need to split the lookup table entry.
540 wimlib_assert(old_lte->refcnt > inode->i_nlink);
541 DEBUG("Splitting lookup table entry "
542 "(inode->i_nlink = %u, old_lte->refcnt = %u)",
543 inode->i_nlink, old_lte->refcnt);
546 new_lte = new_lookup_table_entry();
549 goto out_delete_staging_file;
552 /* There may already be open file descriptors to this stream if
553 * it's previously been opened read-only, but just now we're
554 * opening it read-write. Identify those file descriptors and
555 * change their lookup table entry pointers to point to the new
556 * lookup table entry, and open staging file descriptors for
559 * At the same time, we need to count the number of these opened
560 * file descriptors to the new lookup table entry. If there's
561 * an old lookup table entry, this number needs to be subtracted
562 * from the fd's opened to the old entry. */
563 for (u16 i = 0, j = 0; j < inode->i_num_opened_fds; i++) {
564 struct wimfs_fd *fd = inode->i_fds[i];
566 if (fd->stream_id == stream_id) {
567 wimlib_assert(fd->f_lte == old_lte);
568 wimlib_assert(fd->staging_fd == -1);
570 new_lte->num_opened_fds++;
571 fd->staging_fd = open(staging_file_name, O_RDONLY);
572 if (fd->staging_fd == -1) {
574 goto out_revert_fd_changes;
580 DEBUG("%hu fd's were already opened to the file we extracted",
581 new_lte->num_opened_fds);
583 old_lte->num_opened_fds -= new_lte->num_opened_fds;
584 old_lte->refcnt -= inode->i_nlink;
588 new_lte->refcnt = inode->i_nlink;
589 new_lte->resource_location = RESOURCE_IN_STAGING_FILE;
590 new_lte->staging_file_name = staging_file_name;
591 new_lte->lte_inode = inode;
592 random_hash(new_lte->hash);
595 inode->i_lte = new_lte;
597 for (u16 i = 0; i < inode->i_num_ads; i++)
598 if (inode->i_ads_entries[i].stream_id == stream_id)
599 inode->i_ads_entries[i].lte = new_lte;
601 lookup_table_insert(ctx->wim->lookup_table, new_lte);
602 list_add(&new_lte->staging_list, &ctx->staging_list);
605 out_revert_fd_changes:
606 for (u16 i = 0, j = 0; j < new_lte->num_opened_fds; i++) {
607 struct wimfs_fd *fd = inode->i_fds[i];
608 if (fd && fd->stream_id == stream_id && fd->f_lte == new_lte) {
610 if (fd->staging_fd != -1) {
611 close(fd->staging_fd);
617 free_lookup_table_entry(new_lte);
618 out_delete_staging_file:
619 unlink(staging_file_name);
620 FREE(staging_file_name);
625 * Creates a randomly named staging directory and saves its name in the
626 * filesystem context structure.
628 static int make_staging_dir(struct wimfs_context *ctx, const char *user_prefix)
630 static const size_t random_suffix_len = 10;
631 static const char *common_suffix = ".staging";
632 static const size_t common_suffix_len = 8;
634 char *staging_dir_name = NULL;
635 size_t staging_dir_name_len;
637 const char *wim_basename;
638 char *real_user_prefix = NULL;
642 real_user_prefix = realpath(user_prefix, NULL);
643 if (!real_user_prefix) {
644 ERROR_WITH_ERRNO("Could not resolve `%s'",
646 ret = WIMLIB_ERR_NOTDIR;
649 wim_basename = path_basename(ctx->wim->filename);
650 prefix_len = strlen(real_user_prefix) + 1 + strlen(wim_basename);
652 prefix_len = strlen(ctx->wim->filename);
655 staging_dir_name_len = prefix_len + common_suffix_len + random_suffix_len;
657 staging_dir_name = MALLOC(staging_dir_name_len + 1);
658 if (!staging_dir_name) {
659 ret = WIMLIB_ERR_NOMEM;
663 if (real_user_prefix)
664 sprintf(staging_dir_name, "%s/%s", real_user_prefix, wim_basename);
666 strcpy(staging_dir_name, ctx->wim->filename);
668 strcat(staging_dir_name, common_suffix);
670 randomize_char_array_with_alnum(staging_dir_name + prefix_len + common_suffix_len,
673 staging_dir_name[staging_dir_name_len] = '\0';
675 if (mkdir(staging_dir_name, 0700) != 0) {
676 ERROR_WITH_ERRNO("Failed to create temporary directory `%s'",
678 ret = WIMLIB_ERR_MKDIR;
683 FREE(real_user_prefix);
685 ctx->staging_dir_name = staging_dir_name;
686 ctx->staging_dir_name_len = staging_dir_name_len;
688 FREE(staging_dir_name);
693 static int remove_file_or_directory(const char *fpath, const struct stat *sb,
694 int typeflag, struct FTW *ftwbuf)
696 if (remove(fpath) == 0)
699 ERROR_WITH_ERRNO("Cannot remove `%s'", fpath);
700 return WIMLIB_ERR_DELETE_STAGING_DIR;
705 * Deletes the staging directory and all the files contained in it.
707 static int delete_staging_dir(struct wimfs_context *ctx)
710 ret = nftw(ctx->staging_dir_name, remove_file_or_directory,
712 FREE(ctx->staging_dir_name);
713 ctx->staging_dir_name = NULL;
717 static void inode_update_lte_ptr(struct wim_inode *inode,
718 struct wim_lookup_table_entry *old_lte,
719 struct wim_lookup_table_entry *new_lte)
721 if (inode->i_lte == old_lte) {
722 inode->i_lte = new_lte;
724 for (unsigned i = 0; i < inode->i_num_ads; i++) {
725 if (inode->i_ads_entries[i].lte == old_lte) {
726 inode->i_ads_entries[i].lte = new_lte;
733 static int update_lte_of_staging_file(struct wim_lookup_table_entry *lte,
734 struct wim_lookup_table *table)
736 struct wim_lookup_table_entry *duplicate_lte;
738 u8 hash[SHA1_HASH_SIZE];
741 ret = sha1sum(lte->staging_file_name, hash);
744 lookup_table_unlink(table, lte);
745 duplicate_lte = __lookup_resource(table, hash);
747 /* Merge duplicate lookup table entries */
748 duplicate_lte->refcnt += lte->refcnt;
749 inode_update_lte_ptr(lte->lte_inode, lte, duplicate_lte);
750 free_lookup_table_entry(lte);
752 if (stat(lte->staging_file_name, &stbuf) != 0) {
753 ERROR_WITH_ERRNO("Failed to stat `%s'", lte->staging_file_name);
754 return WIMLIB_ERR_STAT;
756 if (stbuf.st_size == 0) {
757 /* Zero-length stream. No lookup table entry needed. */
758 inode_update_lte_ptr(lte->lte_inode, lte, NULL);
759 free_lookup_table_entry(lte);
761 wimlib_assert(<e->file_on_disk == <e->staging_file_name);
762 lte->resource_entry.original_size = stbuf.st_size;
763 lte->resource_entry.size = stbuf.st_size;
764 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
765 lte->file_on_disk_fp = NULL;
766 copy_hash(lte->hash, hash);
767 lookup_table_insert(table, lte);
773 static int inode_close_fds(struct wim_inode *inode)
775 u16 num_opened_fds = inode->i_num_opened_fds;
776 for (u16 i = 0, j = 0; j < num_opened_fds; i++) {
777 struct wimfs_fd *fd = inode->i_fds[i];
779 wimlib_assert(fd->f_inode == inode);
780 int ret = close_wimfs_fd(fd);
789 /* Overwrites the WIM file, with changes saved. */
790 static int rebuild_wim(struct wimfs_context *ctx, int write_flags,
791 wimlib_progress_func_t progress_func)
794 struct wim_lookup_table_entry *lte, *tmp;
795 WIMStruct *w = ctx->wim;
797 DEBUG("Closing all staging file descriptors.");
798 list_for_each_entry_safe(lte, tmp, &ctx->staging_list, staging_list) {
799 ret = inode_close_fds(lte->lte_inode);
804 DEBUG("Calculating SHA1 checksums for all new staging files.");
805 list_for_each_entry_safe(lte, tmp, &ctx->staging_list, staging_list) {
806 ret = update_lte_of_staging_file(lte, w->lookup_table);
811 xml_update_image_info(w, w->current_image);
812 ret = wimlib_overwrite(w, write_flags, 0, progress_func);
814 ERROR("Failed to commit changes to mounted WIM image");
820 /* Simple function that returns the concatenation of 2 strings. */
821 static char *strcat_dup(const char *s1, const char *s2, size_t max_len)
823 size_t len = strlen(s1) + strlen(s2);
826 char *p = MALLOC(len + 1);
829 snprintf(p, len + 1, "%s%s", s1, s2);
833 static int set_message_queue_names(struct wimfs_context *ctx,
834 const char *mount_dir)
836 static const char *u2d_prefix = "/wimlib-unmount-to-daemon-mq";
837 static const char *d2u_prefix = "/wimlib-daemon-to-unmount-mq";
842 dir_path = realpath(mount_dir, NULL);
844 ERROR_WITH_ERRNO("Failed to resolve path \"%s\"", mount_dir);
846 return WIMLIB_ERR_NOMEM;
848 return WIMLIB_ERR_NOTDIR;
851 for (p = dir_path; *p; p++)
855 ctx->unmount_to_daemon_mq_name = strcat_dup(u2d_prefix, dir_path,
857 if (!ctx->unmount_to_daemon_mq_name) {
858 ret = WIMLIB_ERR_NOMEM;
859 goto out_free_dir_path;
861 ctx->daemon_to_unmount_mq_name = strcat_dup(d2u_prefix, dir_path,
863 if (!ctx->daemon_to_unmount_mq_name) {
864 ret = WIMLIB_ERR_NOMEM;
865 goto out_free_unmount_to_daemon_mq_name;
869 goto out_free_dir_path;
870 out_free_unmount_to_daemon_mq_name:
871 FREE(ctx->unmount_to_daemon_mq_name);
872 ctx->unmount_to_daemon_mq_name = NULL;
878 static void free_message_queue_names(struct wimfs_context *ctx)
880 FREE(ctx->unmount_to_daemon_mq_name);
881 FREE(ctx->daemon_to_unmount_mq_name);
882 ctx->unmount_to_daemon_mq_name = NULL;
883 ctx->daemon_to_unmount_mq_name = NULL;
887 * Opens two POSIX message queue: one for sending messages from the unmount
888 * process to the daemon process, and one to go the other way. The names of the
889 * message queues, which must be system-wide unique, are be based on the mount
892 * @daemon specifies whether the calling process is the filesystem daemon or the
895 static int open_message_queues(struct wimfs_context *ctx, bool daemon)
897 int unmount_to_daemon_mq_flags = O_WRONLY | O_CREAT;
898 int daemon_to_unmount_mq_flags = O_RDONLY | O_CREAT;
901 swap(unmount_to_daemon_mq_flags, daemon_to_unmount_mq_flags);
903 DEBUG("Opening message queue \"%s\"", ctx->unmount_to_daemon_mq_name);
904 ctx->unmount_to_daemon_mq = mq_open(ctx->unmount_to_daemon_mq_name,
905 unmount_to_daemon_mq_flags, 0700, NULL);
907 if (ctx->unmount_to_daemon_mq == (mqd_t)-1) {
908 ERROR_WITH_ERRNO("mq_open()");
909 return WIMLIB_ERR_MQUEUE;
912 DEBUG("Opening message queue \"%s\"", ctx->daemon_to_unmount_mq_name);
913 ctx->daemon_to_unmount_mq = mq_open(ctx->daemon_to_unmount_mq_name,
914 daemon_to_unmount_mq_flags, 0700, NULL);
916 if (ctx->daemon_to_unmount_mq == (mqd_t)-1) {
917 ERROR_WITH_ERRNO("mq_open()");
918 mq_close(ctx->unmount_to_daemon_mq);
919 mq_unlink(ctx->unmount_to_daemon_mq_name);
920 ctx->unmount_to_daemon_mq = (mqd_t)-1;
921 return WIMLIB_ERR_MQUEUE;
926 /* Try to determine the maximum message size of a message queue. The return
927 * value is the maximum message size, or a guess of 8192 bytes if it cannot be
929 static long mq_get_msgsize(mqd_t mq)
931 static const char *msgsize_max_file = "/proc/sys/fs/mqueue/msgsize_max";
936 if (mq_getattr(mq, &attr) == 0) {
937 msgsize = attr.mq_msgsize;
939 ERROR_WITH_ERRNO("mq_getattr()");
940 ERROR("Attempting to read %s", msgsize_max_file);
941 fp = fopen(msgsize_max_file, "rb");
943 if (fscanf(fp, "%ld", &msgsize) != 1) {
944 ERROR("Assuming message size of 8192");
949 ERROR_WITH_ERRNO("Failed to open the file `%s'",
951 ERROR("Assuming message size of 8192");
958 static int get_mailbox(mqd_t mq, long needed_msgsize, long *msgsize_ret,
964 msgsize = mq_get_msgsize(mq);
966 if (msgsize < needed_msgsize) {
967 ERROR("Message queue max size must be at least %ld!",
969 return WIMLIB_ERR_MQUEUE;
972 mailbox = MALLOC(msgsize);
974 ERROR("Failed to allocate %ld bytes for mailbox", msgsize);
975 return WIMLIB_ERR_NOMEM;
977 *msgsize_ret = msgsize;
978 *mailbox_ret = mailbox;
982 static void unlink_message_queues(struct wimfs_context *ctx)
984 mq_unlink(ctx->unmount_to_daemon_mq_name);
985 mq_unlink(ctx->daemon_to_unmount_mq_name);
988 /* Closes the message queues, which are allocated in static variables */
989 static void close_message_queues(struct wimfs_context *ctx)
991 DEBUG("Closing message queues");
992 mq_close(ctx->unmount_to_daemon_mq);
993 ctx->unmount_to_daemon_mq = (mqd_t)(-1);
994 mq_close(ctx->daemon_to_unmount_mq);
995 ctx->daemon_to_unmount_mq = (mqd_t)(-1);
996 unlink_message_queues(ctx);
1000 struct unmount_msg_hdr {
1007 struct msg_unmount_request {
1008 struct unmount_msg_hdr hdr;
1010 u8 want_progress_messages;
1013 struct msg_daemon_info {
1014 struct unmount_msg_hdr hdr;
1019 struct msg_unmount_finished {
1020 struct unmount_msg_hdr hdr;
1024 struct msg_write_streams_progress {
1025 struct unmount_msg_hdr hdr;
1026 union wimlib_progress_info info;
1030 MSG_TYPE_UNMOUNT_REQUEST,
1031 MSG_TYPE_DAEMON_INFO,
1032 MSG_TYPE_WRITE_STREAMS_PROGRESS,
1033 MSG_TYPE_UNMOUNT_FINISHED,
1037 struct msg_handler_context_hdr {
1038 int timeout_seconds;
1041 struct unmount_msg_handler_context {
1042 struct msg_handler_context_hdr hdr;
1046 wimlib_progress_func_t progress_func;
1049 struct daemon_msg_handler_context {
1050 struct msg_handler_context_hdr hdr;
1051 struct wimfs_context *wimfs_ctx;
1054 static int send_unmount_request_msg(mqd_t mq, int unmount_flags,
1055 u8 want_progress_messages)
1057 DEBUG("Sending unmount request msg");
1058 struct msg_unmount_request msg = {
1060 .min_version = WIMLIB_MAKEVERSION(1, 2, 1),
1061 .cur_version = WIMLIB_VERSION_CODE,
1062 .msg_type = MSG_TYPE_UNMOUNT_REQUEST,
1063 .msg_size = sizeof(msg),
1065 .unmount_flags = unmount_flags,
1066 .want_progress_messages = want_progress_messages,
1069 if (mq_send(mq, (void*)&msg, sizeof(msg), 1)) {
1070 ERROR_WITH_ERRNO("Failed to communicate with filesystem daemon");
1071 return WIMLIB_ERR_MQUEUE;
1076 static int send_daemon_info_msg(mqd_t mq, pid_t pid, int mount_flags)
1078 DEBUG("Sending daemon info msg (pid = %d, mount_flags=%x)",
1081 struct msg_daemon_info msg = {
1083 .min_version = WIMLIB_MAKEVERSION(1, 2, 1),
1084 .cur_version = WIMLIB_VERSION_CODE,
1085 .msg_type = MSG_TYPE_DAEMON_INFO,
1086 .msg_size = sizeof(msg),
1089 .mount_flags = mount_flags,
1091 if (mq_send(mq, (void*)&msg, sizeof(msg), 1)) {
1092 ERROR_WITH_ERRNO("Failed to send daemon info to unmount process");
1093 return WIMLIB_ERR_MQUEUE;
1098 static void send_unmount_finished_msg(mqd_t mq, int status)
1100 DEBUG("Sending unmount finished msg");
1101 struct msg_unmount_finished msg = {
1103 .min_version = WIMLIB_MAKEVERSION(1, 2, 1),
1104 .cur_version = WIMLIB_VERSION_CODE,
1105 .msg_type = MSG_TYPE_UNMOUNT_FINISHED,
1106 .msg_size = sizeof(msg),
1110 if (mq_send(mq, (void*)&msg, sizeof(msg), 1))
1111 ERROR_WITH_ERRNO("Failed to send status to unmount process");
1114 static int unmount_progress_func(enum wimlib_progress_msg msg,
1115 const union wimlib_progress_info *info)
1117 if (msg == WIMLIB_PROGRESS_MSG_WRITE_STREAMS) {
1118 struct msg_write_streams_progress msg = {
1120 .min_version = WIMLIB_MAKEVERSION(1, 2, 1),
1121 .cur_version = WIMLIB_VERSION_CODE,
1122 .msg_type = MSG_TYPE_WRITE_STREAMS_PROGRESS,
1123 .msg_size = sizeof(msg),
1127 if (mq_send(wimfs_get_context()->daemon_to_unmount_mq,
1128 (void*)&msg, sizeof(msg), 1))
1130 ERROR_WITH_ERRNO("Failed to send progress information "
1131 "to unmount process");
1137 static int msg_unmount_request_handler(const void *_msg, void *_handler_ctx)
1139 const struct msg_unmount_request *msg = _msg;
1140 struct daemon_msg_handler_context *handler_ctx = _handler_ctx;
1141 struct wimfs_context *wimfs_ctx;
1145 wimlib_progress_func_t progress_func;
1147 DEBUG("Handling unmount request msg");
1149 wimfs_ctx = handler_ctx->wimfs_ctx;
1150 if (msg->hdr.msg_size < sizeof(*msg)) {
1151 status = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1155 unmount_flags = msg->unmount_flags;
1156 if (msg->want_progress_messages)
1157 progress_func = unmount_progress_func;
1159 progress_func = NULL;
1161 ret = send_daemon_info_msg(wimfs_ctx->daemon_to_unmount_mq, getpid(),
1162 wimfs_ctx->mount_flags);
1168 if (wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1169 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_COMMIT) {
1170 int write_flags = 0;
1171 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY)
1172 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1173 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_REBUILD)
1174 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1175 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_RECOMPRESS)
1176 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
1177 status = rebuild_wim(wimfs_ctx, write_flags,
1181 DEBUG("Read-only mount");
1186 if (wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1187 ret = delete_staging_dir(wimfs_ctx);
1189 ERROR("Failed to delete the staging directory");
1194 send_unmount_finished_msg(wimfs_ctx->daemon_to_unmount_mq, status);
1195 return MSG_BREAK_LOOP;
1198 static int msg_daemon_info_handler(const void *_msg, void *_handler_ctx)
1200 const struct msg_daemon_info *msg = _msg;
1201 struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1203 DEBUG("Handling daemon info msg");
1204 if (msg->hdr.msg_size < sizeof(*msg))
1205 return WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1206 handler_ctx->daemon_pid = msg->daemon_pid;
1207 handler_ctx->mount_flags = msg->mount_flags;
1208 handler_ctx->hdr.timeout_seconds = 1;
1209 DEBUG("pid of daemon is %d; mount flags were %#x",
1210 handler_ctx->daemon_pid,
1211 handler_ctx->mount_flags);
1215 static int msg_write_streams_progress_handler(const void *_msg,
1218 const struct msg_write_streams_progress *msg = _msg;
1219 struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1221 if (msg->hdr.msg_size < sizeof(*msg))
1222 return WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1223 if (handler_ctx->progress_func) {
1224 handler_ctx->progress_func(WIMLIB_PROGRESS_MSG_WRITE_STREAMS,
1230 static int msg_unmount_finished_handler(const void *_msg, void *_handler_ctx)
1232 const struct msg_unmount_finished *msg = _msg;
1233 struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1235 DEBUG("Handling unmount finished message");
1236 if (msg->hdr.msg_size < sizeof(*msg))
1237 return WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1238 handler_ctx->status = msg->status;
1239 DEBUG("status is %d", handler_ctx->status);
1240 return MSG_BREAK_LOOP;
1243 static int unmount_timed_out_cb(void *_handler_ctx)
1245 struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1247 if (handler_ctx->daemon_pid == 0) {
1250 kill(handler_ctx->daemon_pid, 0);
1251 if (errno == ESRCH) {
1254 DEBUG("Filesystem daemon is still alive... "
1255 "Waiting another %d seconds\n",
1256 handler_ctx->hdr.timeout_seconds);
1261 ERROR("The filesystem daemon has crashed! Changes to the "
1262 "WIM may not have been commited.");
1263 return WIMLIB_ERR_FILESYSTEM_DAEMON_CRASHED;
1266 static int daemon_timed_out_cb(void *_handler_ctx)
1268 ERROR("Timed out waiting for unmount request! "
1269 "Changes to the mounted WIM will not be committed.");
1270 return WIMLIB_ERR_TIMEOUT;
1273 typedef int (*msg_handler_t)(const void *_msg, void *_handler_ctx);
1275 struct msg_handler_callbacks {
1276 int (*timed_out)(void * _handler_ctx);
1277 msg_handler_t msg_handlers[MSG_TYPE_MAX];
1280 static const struct msg_handler_callbacks unmount_msg_handler_callbacks = {
1281 .timed_out = unmount_timed_out_cb,
1283 [MSG_TYPE_DAEMON_INFO] = msg_daemon_info_handler,
1284 [MSG_TYPE_WRITE_STREAMS_PROGRESS] = msg_write_streams_progress_handler,
1285 [MSG_TYPE_UNMOUNT_FINISHED] = msg_unmount_finished_handler,
1289 static const struct msg_handler_callbacks daemon_msg_handler_callbacks = {
1290 .timed_out = daemon_timed_out_cb,
1292 [MSG_TYPE_UNMOUNT_REQUEST] = msg_unmount_request_handler,
1296 static int receive_message(mqd_t mq,
1297 struct msg_handler_context_hdr *handler_ctx,
1298 const msg_handler_t msg_handlers[],
1299 long mailbox_size, void *mailbox)
1302 struct timespec timeout;
1303 ssize_t bytes_received;
1304 struct unmount_msg_hdr *hdr;
1307 gettimeofday(&now, NULL);
1308 timeout.tv_sec = now.tv_sec + handler_ctx->timeout_seconds;
1309 timeout.tv_nsec = now.tv_usec * 1000;
1311 bytes_received = mq_timedreceive(mq, mailbox,
1312 mailbox_size, NULL, &timeout);
1314 if (bytes_received == -1) {
1315 if (errno == ETIMEDOUT) {
1316 ret = WIMLIB_ERR_TIMEOUT;
1318 ERROR_WITH_ERRNO("mq_timedreceive()");
1319 ret = WIMLIB_ERR_MQUEUE;
1321 } else if (bytes_received < sizeof(*hdr) ||
1322 bytes_received != hdr->msg_size) {
1323 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1324 } else if (WIMLIB_VERSION_CODE < hdr->min_version) {
1325 /*ERROR("Cannot understand the received message. "*/
1326 /*"Please upgrade wimlib to at least v%d.%d.%d",*/
1327 /*WIMLIB_GET_MAJOR_VERSION(hdr->min_version),*/
1328 /*WIMLIB_GET_MINOR_VERSION(hdr->min_version),*/
1329 /*WIMLIB_GET_PATCH_VERSION(hdr->min_version));*/
1330 ret = MSG_VERSION_TOO_HIGH;
1331 } else if (hdr->msg_type >= MSG_TYPE_MAX) {
1332 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1333 } else if (msg_handlers[hdr->msg_type] == NULL) {
1334 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1336 ret = msg_handlers[hdr->msg_type](mailbox, handler_ctx);
1341 static int message_loop(mqd_t mq,
1342 const struct msg_handler_callbacks *callbacks,
1343 struct msg_handler_context_hdr *handler_ctx)
1345 static const size_t MAX_MSG_SIZE = 512;
1350 DEBUG("Entering message loop");
1352 ret = get_mailbox(mq, MAX_MSG_SIZE, &msgsize, &mailbox);
1356 ret = receive_message(mq, handler_ctx,
1357 callbacks->msg_handlers,
1359 if (ret == 0 || ret == MSG_VERSION_TOO_HIGH) {
1361 } else if (ret == MSG_BREAK_LOOP) {
1364 } else if (ret == WIMLIB_ERR_TIMEOUT) {
1365 if (callbacks->timed_out)
1366 ret = callbacks->timed_out(handler_ctx);
1372 ERROR_WITH_ERRNO("Error communicating with "
1373 "filesystem daemon");
1378 DEBUG("Exiting message loop");
1382 /* Execute `fusermount -u', which is installed setuid root, to unmount the WIM.
1384 * FUSE does not yet implement synchronous unmounts. This means that fusermount
1385 * -u will return before the filesystem daemon returns from wimfs_destroy().
1386 * This is partly what we want, because we need to send a message from this
1387 * process to the filesystem daemon telling whether --commit was specified or
1388 * not. However, after that, the unmount process must wait for the filesystem
1389 * daemon to finish writing the WIM file.
1391 static int execute_fusermount(const char *dir)
1399 ERROR_WITH_ERRNO("Failed to fork()");
1400 return WIMLIB_ERR_FORK;
1404 execlp("fusermount", "fusermount", "-u", dir, NULL);
1405 ERROR_WITH_ERRNO("Failed to execute `fusermount'");
1406 exit(WIMLIB_ERR_FUSERMOUNT);
1410 ret = waitpid(pid, &status, 0);
1412 ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
1414 return WIMLIB_ERR_FUSERMOUNT;
1417 if (!WIFEXITED(status)) {
1418 ERROR("'fusermount' did not terminate normally!");
1419 return WIMLIB_ERR_FUSERMOUNT;
1422 status = WEXITSTATUS(status);
1427 if (status != WIMLIB_ERR_FUSERMOUNT)
1428 return WIMLIB_ERR_FUSERMOUNT;
1430 /* Try again, but with the `umount' program. This is required on other
1431 * FUSE implementations such as FreeBSD's that do not have a
1432 * `fusermount' program. */
1433 ERROR("Falling back to 'umount'. Note: you may need to be "
1434 "root for this to work");
1437 ERROR_WITH_ERRNO("Failed to fork()");
1438 return WIMLIB_ERR_FORK;
1442 execlp("umount", "umount", dir, NULL);
1443 ERROR_WITH_ERRNO("Failed to execute `umount'");
1444 exit(WIMLIB_ERR_FUSERMOUNT);
1448 ret = waitpid(pid, &status, 0);
1450 ERROR_WITH_ERRNO("Failed to wait for `umount' process to "
1452 return WIMLIB_ERR_FUSERMOUNT;
1455 ERROR("`umount' did not successfully complete");
1456 return WIMLIB_ERR_FUSERMOUNT;
1461 static int wimfs_access(const char *path, int mask)
1463 /* Permissions not implemented */
1467 static int wimfs_chmod(const char *path, mode_t mask)
1469 struct wim_dentry *dentry;
1470 struct wimfs_context *ctx = wimfs_get_context();
1471 struct wim_inode *inode;
1475 ret = lookup_resource(ctx->wim, path,
1476 get_lookup_flags(ctx) | LOOKUP_FLAG_DIRECTORY_OK,
1477 &dentry, NULL, NULL);
1480 inode = dentry->d_inode;
1481 inode_to_stbuf(inode, NULL, &stbuf);
1482 if (mask == stbuf.st_mode)
1488 /* Called when the filesystem is unmounted. */
1489 static void wimfs_destroy(void *p)
1491 struct wimfs_context *wimfs_ctx = wimfs_get_context();
1492 if (open_message_queues(wimfs_ctx, true) == 0) {
1493 struct daemon_msg_handler_context handler_ctx = {
1495 .timeout_seconds = 5,
1497 .wimfs_ctx = wimfs_ctx,
1499 message_loop(wimfs_ctx->unmount_to_daemon_mq,
1500 &daemon_msg_handler_callbacks,
1502 close_message_queues(wimfs_ctx);
1507 static int wimfs_fallocate(const char *path, int mode,
1508 off_t offset, off_t len, struct fuse_file_info *fi)
1510 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1511 wimlib_assert(fd->staging_fd != -1);
1512 return fallocate(fd->staging_fd, mode, offset, len);
1517 static int wimfs_fgetattr(const char *path, struct stat *stbuf,
1518 struct fuse_file_info *fi)
1520 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1521 return inode_to_stbuf(fd->f_inode, fd->f_lte, stbuf);
1524 static int wimfs_ftruncate(const char *path, off_t size,
1525 struct fuse_file_info *fi)
1527 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1528 int ret = ftruncate(fd->staging_fd, size);
1531 if (fd->f_lte && size < fd->f_lte->resource_entry.original_size)
1532 fd->f_lte->resource_entry.original_size = size;
1537 * Fills in a `struct stat' that corresponds to a file or directory in the WIM.
1539 static int wimfs_getattr(const char *path, struct stat *stbuf)
1541 struct wim_dentry *dentry;
1542 struct wim_lookup_table_entry *lte;
1544 struct wimfs_context *ctx = wimfs_get_context();
1546 ret = lookup_resource(ctx->wim, path,
1547 get_lookup_flags(ctx) | LOOKUP_FLAG_DIRECTORY_OK,
1548 &dentry, <e, NULL);
1551 return inode_to_stbuf(dentry->d_inode, lte, stbuf);
1555 /* Read an alternate data stream through the XATTR interface, or get its size */
1556 static int wimfs_getxattr(const char *path, const char *name, char *value,
1560 struct wim_inode *inode;
1561 struct wim_ads_entry *ads_entry;
1563 struct wim_lookup_table_entry *lte;
1564 struct wimfs_context *ctx = wimfs_get_context();
1566 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1569 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1573 inode = wim_pathname_to_inode(ctx->wim, path);
1577 ads_entry = inode_get_ads_entry(inode, name, NULL);
1581 lte = ads_entry->lte;
1582 res_size = wim_resource_size(lte);
1587 if (res_size > size)
1590 ret = read_full_wim_resource(lte, (u8*)value,
1591 WIMLIB_RESOURCE_FLAG_MULTITHREADED);
1599 /* Create a hard link */
1600 static int wimfs_link(const char *to, const char *from)
1602 struct wim_dentry *from_dentry, *from_dentry_parent;
1603 const char *link_name;
1604 struct wim_inode *inode;
1605 struct wim_lookup_table_entry *lte;
1606 WIMStruct *w = wimfs_get_WIMStruct();
1609 inode = wim_pathname_to_inode(w, to);
1613 if (inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
1614 FILE_ATTRIBUTE_REPARSE_POINT))
1617 from_dentry_parent = get_parent_dentry(w, from);
1618 if (!from_dentry_parent)
1620 if (!dentry_is_directory(from_dentry_parent))
1623 link_name = path_basename(from);
1624 if (get_dentry_child_with_name(from_dentry_parent, link_name))
1626 from_dentry = new_dentry(link_name);
1630 inode_add_dentry(from_dentry, inode);
1631 from_dentry->d_inode = inode;
1634 for (i = 0; i <= inode->i_num_ads; i++) {
1635 lte = inode_stream_lte_resolved(inode, i);
1639 dentry_add_child(from_dentry_parent, from_dentry);
1644 static int wimfs_listxattr(const char *path, char *list, size_t size)
1647 struct wim_inode *inode;
1648 struct wimfs_context *ctx = wimfs_get_context();
1652 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1655 /* List alternate data streams, or get the list size */
1657 inode = wim_pathname_to_inode(ctx->wim, path);
1663 for (i = 0; i < inode->i_num_ads; i++)
1664 needed_size += inode->i_ads_entries[i].stream_name_utf8_len + 6;
1668 for (i = 0; i < inode->i_num_ads; i++) {
1669 needed_size = inode->i_ads_entries[i].stream_name_utf8_len + 6;
1670 if (needed_size > size)
1672 p += sprintf(p, "user.%s",
1673 inode->i_ads_entries[i].stream_name_utf8) + 1;
1674 size -= needed_size;
1682 /* Create a directory in the WIM.
1683 * @mode is currently ignored. */
1684 static int wimfs_mkdir(const char *path, mode_t mode)
1686 struct wim_dentry *dentry;
1689 ret = create_dentry(wimfs_get_context(), path, &dentry);
1691 dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
1695 /* Create a regular file in the WIM.
1696 * @mode is currently ignored. */
1697 static int wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
1699 const char *stream_name;
1700 struct wimfs_context *ctx = wimfs_get_context();
1701 if ((ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
1702 && (stream_name = path_stream_name(path))) {
1703 /* Make an alternate data stream */
1704 struct wim_ads_entry *new_entry;
1705 struct wim_inode *inode;
1707 char *p = (char*)stream_name - 1;
1708 wimlib_assert(*p == ':');
1711 inode = wim_pathname_to_inode(ctx->wim, path);
1714 if (inode->i_attributes &
1715 (FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_DIRECTORY))
1717 if (inode_get_ads_entry(inode, stream_name, NULL))
1719 new_entry = inode_add_ads(inode, stream_name);
1724 struct wim_dentry *dentry;
1727 /* Make a normal file (not an alternate data stream) */
1728 ret = create_dentry(ctx, path, &dentry);
1730 dentry->d_inode->i_attributes = FILE_ATTRIBUTE_NORMAL;
1737 static int wimfs_open(const char *path, struct fuse_file_info *fi)
1739 struct wim_dentry *dentry;
1740 struct wim_lookup_table_entry *lte;
1742 struct wimfs_fd *fd;
1743 struct wim_inode *inode;
1746 struct wimfs_context *ctx = wimfs_get_context();
1748 ret = lookup_resource(ctx->wim, path, get_lookup_flags(ctx),
1749 &dentry, <e, &stream_idx);
1753 inode = dentry->d_inode;
1755 if (stream_idx == 0)
1758 stream_id = inode->i_ads_entries[stream_idx - 1].stream_id;
1760 /* The file resource may be in the staging directory (read-write mounts
1761 * only) or in the WIM. If it's in the staging directory, we need to
1762 * open a native file descriptor for the corresponding file. Otherwise,
1763 * we can read the file resource directly from the WIM file if we are
1764 * opening it read-only, but we need to extract the resource to the
1765 * staging directory if we are opening it writable. */
1767 if (flags_writable(fi->flags) &&
1768 (!lte || lte->resource_location != RESOURCE_IN_STAGING_FILE)) {
1769 u64 size = (lte) ? wim_resource_size(lte) : 0;
1770 ret = extract_resource_to_staging_dir(inode, stream_id,
1776 ret = alloc_wimfs_fd(inode, stream_id, lte, &fd,
1777 wimfs_ctx_readonly(ctx));
1781 if (lte && lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1782 fd->staging_fd = open(lte->staging_file_name, fi->flags);
1783 if (fd->staging_fd == -1) {
1784 int errno_save = errno;
1789 fi->fh = (uintptr_t)fd;
1793 /* Opens a directory. */
1794 static int wimfs_opendir(const char *path, struct fuse_file_info *fi)
1796 struct wim_inode *inode;
1798 struct wimfs_fd *fd = NULL;
1799 struct wimfs_context *ctx = wimfs_get_context();
1800 WIMStruct *w = ctx->wim;
1802 inode = wim_pathname_to_inode(w, path);
1805 if (!inode_is_directory(inode))
1807 ret = alloc_wimfs_fd(inode, 0, NULL, &fd, wimfs_ctx_readonly(ctx));
1808 fi->fh = (uintptr_t)fd;
1814 * Read data from a file in the WIM or in the staging directory.
1816 static int wimfs_read(const char *path, char *buf, size_t size,
1817 off_t offset, struct fuse_file_info *fi)
1819 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1825 if (!fd->f_lte) /* Empty stream with no lookup table entry */
1828 if (fd->f_lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1829 /* Read from staging file */
1831 wimlib_assert(fd->f_lte->staging_file_name);
1832 wimlib_assert(fd->staging_fd != -1);
1834 DEBUG("Seek to offset %"PRIu64, offset);
1836 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1838 ret = read(fd->staging_fd, buf, size);
1844 u64 res_size = wim_resource_size(fd->f_lte);
1845 if (offset > res_size)
1847 size = min(size, res_size - offset);
1848 if (read_wim_resource(fd->f_lte, (u8*)buf,
1850 WIMLIB_RESOURCE_FLAG_MULTITHREADED) != 0)
1856 struct fill_params {
1858 fuse_fill_dir_t filler;
1861 static int dentry_fuse_fill(struct wim_dentry *dentry, void *arg)
1863 struct fill_params *fill_params = arg;
1864 return fill_params->filler(fill_params->buf, dentry->file_name_utf8,
1868 /* Fills in the entries of the directory specified by @path using the
1869 * FUSE-provided function @filler. */
1870 static int wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
1871 off_t offset, struct fuse_file_info *fi)
1873 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1874 struct wim_inode *inode;
1879 inode = fd->f_inode;
1881 struct fill_params fill_params = {
1886 filler(buf, ".", NULL, 0);
1887 filler(buf, "..", NULL, 0);
1889 return for_dentry_in_rbtree(inode->i_children.rb_node,
1890 dentry_fuse_fill, &fill_params);
1894 static int wimfs_readlink(const char *path, char *buf, size_t buf_len)
1896 struct wimfs_context *ctx = wimfs_get_context();
1897 struct wim_inode *inode = wim_pathname_to_inode(ctx->wim, path);
1901 if (!inode_is_symlink(inode))
1904 ret = inode_readlink(inode, buf, buf_len, ctx->wim,
1905 WIMLIB_RESOURCE_FLAG_MULTITHREADED);
1912 static int wimfs_release(const char *path, struct fuse_file_info *fi)
1914 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1915 return close_wimfs_fd(fd);
1918 /* Close a directory */
1919 static int wimfs_releasedir(const char *path, struct fuse_file_info *fi)
1921 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1922 return close_wimfs_fd(fd);
1926 /* Remove an alternate data stream through the XATTR interface */
1927 static int wimfs_removexattr(const char *path, const char *name)
1929 struct wim_inode *inode;
1930 struct wim_ads_entry *ads_entry;
1932 struct wimfs_context *ctx = wimfs_get_context();
1934 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1937 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1941 inode = wim_pathname_to_inode(ctx->wim, path);
1945 ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
1948 inode_remove_ads(inode, ads_idx, ctx->wim->lookup_table);
1953 /* Renames a file or directory. See rename (3) */
1954 static int wimfs_rename(const char *from, const char *to)
1956 struct wim_dentry *src;
1957 struct wim_dentry *dst;
1958 struct wim_dentry *parent_of_dst;
1959 WIMStruct *w = wimfs_get_WIMStruct();
1962 /* This rename() implementation currently only supports actual files
1963 * (not alternate data streams) */
1965 src = get_dentry(w, from);
1969 dst = get_dentry(w, to);
1972 /* Destination file exists */
1974 if (src == dst) /* Same file */
1977 if (!dentry_is_directory(src)) {
1978 /* Cannot rename non-directory to directory. */
1979 if (dentry_is_directory(dst))
1982 /* Cannot rename directory to a non-directory or a non-empty
1984 if (!dentry_is_directory(dst))
1986 if (inode_has_children(dst->d_inode))
1989 parent_of_dst = dst->parent;
1991 /* Destination does not exist */
1992 parent_of_dst = get_parent_dentry(w, to);
1996 if (!dentry_is_directory(parent_of_dst))
2000 ret = set_dentry_name(src, path_basename(to));
2004 remove_dentry(dst, w->lookup_table);
2006 dentry_add_child(parent_of_dst, src);
2010 /* Remove a directory */
2011 static int wimfs_rmdir(const char *path)
2013 struct wim_dentry *parent, *dentry;
2014 WIMStruct *w = wimfs_get_WIMStruct();
2016 dentry = get_dentry(w, path);
2020 if (!dentry_is_directory(dentry))
2023 if (dentry_has_children(dentry))
2026 remove_dentry(dentry, w->lookup_table);
2031 /* Write an alternate data stream through the XATTR interface */
2032 static int wimfs_setxattr(const char *path, const char *name,
2033 const char *value, size_t size, int flags)
2035 struct wim_ads_entry *existing_ads_entry;
2036 struct wim_ads_entry *new_ads_entry;
2037 struct wim_lookup_table_entry *existing_lte;
2038 struct wim_lookup_table_entry *lte;
2039 struct wim_inode *inode;
2040 u8 value_hash[SHA1_HASH_SIZE];
2042 struct wimfs_context *ctx = wimfs_get_context();
2044 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
2047 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
2051 inode = wim_pathname_to_inode(ctx->wim, path);
2055 existing_ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
2056 if (existing_ads_entry) {
2057 if (flags & XATTR_CREATE)
2059 inode_remove_ads(inode, ads_idx, ctx->wim->lookup_table);
2061 if (flags & XATTR_REPLACE)
2064 new_ads_entry = inode_add_ads(inode, name);
2068 sha1_buffer((const u8*)value, size, value_hash);
2070 existing_lte = __lookup_resource(ctx->wim->lookup_table, value_hash);
2077 lte = new_lookup_table_entry();
2080 value_copy = MALLOC(size);
2085 memcpy(value_copy, value, size);
2086 lte->resource_location = RESOURCE_IN_ATTACHED_BUFFER;
2087 lte->attached_buffer = value_copy;
2088 lte->resource_entry.original_size = size;
2089 lte->resource_entry.size = size;
2090 lte->resource_entry.flags = 0;
2091 copy_hash(lte->hash, value_hash);
2092 lookup_table_insert(ctx->wim->lookup_table, lte);
2094 new_ads_entry->lte = lte;
2099 static int wimfs_symlink(const char *to, const char *from)
2101 struct wimfs_context *ctx = wimfs_get_context();
2102 struct wim_dentry *dentry;
2105 ret = create_dentry(ctx, from, &dentry);
2107 dentry->d_inode->i_attributes = FILE_ATTRIBUTE_REPARSE_POINT;
2108 dentry->d_inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
2109 if (inode_set_symlink(dentry->d_inode, to,
2110 ctx->wim->lookup_table, NULL))
2112 unlink_dentry(dentry);
2113 free_dentry(dentry);
2121 /* Reduce the size of a file */
2122 static int wimfs_truncate(const char *path, off_t size)
2124 struct wim_dentry *dentry;
2125 struct wim_lookup_table_entry *lte;
2129 struct wim_inode *inode;
2130 struct wimfs_context *ctx = wimfs_get_context();
2132 ret = lookup_resource(ctx->wim, path, get_lookup_flags(ctx),
2133 &dentry, <e, &stream_idx);
2138 if (lte == NULL && size == 0)
2141 inode = dentry->d_inode;
2142 if (stream_idx == 0)
2145 stream_id = inode->i_ads_entries[stream_idx - 1].stream_id;
2147 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
2148 ret = truncate(lte->staging_file_name, size);
2152 /* File in WIM. Extract it to the staging directory, but only
2153 * the first @size bytes of it. */
2154 ret = extract_resource_to_staging_dir(inode, stream_id,
2160 /* Unlink a non-directory or alternate data stream */
2161 static int wimfs_unlink(const char *path)
2163 struct wim_dentry *dentry;
2164 struct wim_lookup_table_entry *lte;
2167 struct wimfs_context *ctx = wimfs_get_context();
2169 ret = lookup_resource(ctx->wim, path, get_lookup_flags(ctx),
2170 &dentry, <e, &stream_idx);
2175 if (stream_idx == 0)
2176 remove_dentry(dentry, ctx->wim->lookup_table);
2178 inode_remove_ads(dentry->d_inode, stream_idx - 1,
2179 ctx->wim->lookup_table);
2183 #ifdef HAVE_UTIMENSAT
2185 * Change the timestamp on a file dentry.
2187 * Note that alternate data streams do not have their own timestamps.
2189 static int wimfs_utimens(const char *path, const struct timespec tv[2])
2191 struct wim_dentry *dentry;
2192 struct wim_inode *inode;
2193 WIMStruct *w = wimfs_get_WIMStruct();
2195 dentry = get_dentry(w, path);
2198 inode = dentry->d_inode;
2200 if (tv[0].tv_nsec != UTIME_OMIT) {
2201 if (tv[0].tv_nsec == UTIME_NOW)
2202 inode->i_last_access_time = get_wim_timestamp();
2204 inode->i_last_access_time = timespec_to_wim_timestamp(&tv[0]);
2206 if (tv[1].tv_nsec != UTIME_OMIT) {
2207 if (tv[1].tv_nsec == UTIME_NOW)
2208 inode->i_last_write_time = get_wim_timestamp();
2210 inode->i_last_write_time = timespec_to_wim_timestamp(&tv[1]);
2215 static int wimfs_utime(const char *path, struct utimbuf *times)
2217 struct wim_dentry *dentry;
2218 struct wim_inode *inode;
2219 WIMStruct *w = wimfs_get_WIMStruct();
2221 dentry = get_dentry(w, path);
2224 inode = dentry->d_inode;
2226 inode->i_last_write_time = unix_timestamp_to_wim(times->modtime);
2227 inode->i_last_access_time = unix_timestamp_to_wim(times->actime);
2232 /* Writes to a file in the WIM filesystem.
2233 * It may be an alternate data stream, but here we don't even notice because we
2234 * just get a lookup table entry. */
2235 static int wimfs_write(const char *path, const char *buf, size_t size,
2236 off_t offset, struct fuse_file_info *fi)
2238 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
2245 wimlib_assert(fd->f_lte);
2246 wimlib_assert(fd->f_lte->staging_file_name);
2247 wimlib_assert(fd->staging_fd != -1);
2248 wimlib_assert(fd->f_inode);
2250 /* Seek to the requested position */
2251 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
2254 /* Write the data. */
2255 ret = write(fd->staging_fd, buf, size);
2259 now = get_wim_timestamp();
2260 fd->f_inode->i_last_write_time = now;
2261 fd->f_inode->i_last_access_time = now;
2265 static struct fuse_operations wimfs_operations = {
2266 .access = wimfs_access,
2267 .chmod = wimfs_chmod,
2268 .destroy = wimfs_destroy,
2270 .fallocate = wimfs_fallocate,
2272 .fgetattr = wimfs_fgetattr,
2273 .ftruncate = wimfs_ftruncate,
2274 .getattr = wimfs_getattr,
2276 .getxattr = wimfs_getxattr,
2280 .listxattr = wimfs_listxattr,
2282 .mkdir = wimfs_mkdir,
2283 .mknod = wimfs_mknod,
2285 .opendir = wimfs_opendir,
2287 .readdir = wimfs_readdir,
2288 .readlink = wimfs_readlink,
2289 .release = wimfs_release,
2290 .releasedir = wimfs_releasedir,
2292 .removexattr = wimfs_removexattr,
2294 .rename = wimfs_rename,
2295 .rmdir = wimfs_rmdir,
2297 .setxattr = wimfs_setxattr,
2299 .symlink = wimfs_symlink,
2300 .truncate = wimfs_truncate,
2301 .unlink = wimfs_unlink,
2302 #ifdef HAVE_UTIMENSAT
2303 .utimens = wimfs_utimens,
2305 .utime = wimfs_utime,
2307 .write = wimfs_write,
2309 /* wimfs keeps file descriptor structures (struct wimfs_fd), so there is
2310 * no need to have the file path provided on operations such as read()
2311 * where only the file descriptor is needed. */
2312 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 8)
2313 .flag_nullpath_ok = 1,
2315 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 9)
2317 .flag_utime_omit_ok = 1,
2322 /* Mounts an image from a WIM file. */
2323 WIMLIBAPI int wimlib_mount_image(WIMStruct *wim, int image, const char *dir,
2324 int mount_flags, WIMStruct **additional_swms,
2325 unsigned num_additional_swms,
2326 const char *staging_dir)
2332 struct wim_lookup_table *joined_tab, *wim_tab_save;
2333 struct wim_image_metadata *imd;
2334 struct wimfs_context ctx;
2335 struct hlist_node *cur_node;
2336 struct wim_inode *inode;
2338 DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
2339 wim, image, dir, mount_flags);
2342 return WIMLIB_ERR_INVALID_PARAM;
2344 ret = verify_swm_set(wim, additional_swms, num_additional_swms);
2348 if ((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) && (wim->hdr.total_parts != 1)) {
2349 ERROR("Cannot mount a split WIM read-write");
2350 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
2353 if (num_additional_swms) {
2354 ret = new_joined_lookup_table(wim, additional_swms,
2355 num_additional_swms,
2359 wim_tab_save = wim->lookup_table;
2360 wim->lookup_table = joined_tab;
2363 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2364 ret = wim_run_full_verifications(wim);
2369 ret = select_wim_image(wim, image);
2373 DEBUG("Selected image %d", image);
2375 imd = wim_get_current_image_metadata(wim);
2377 if (imd->root_dentry->refcnt != 1) {
2378 ERROR("Cannot mount image that was just exported with "
2379 "wimlib_export_image()");
2380 ret = WIMLIB_ERR_INVALID_PARAM;
2384 if (imd->inode_list.first) /* May be unneeded? */
2385 imd->inode_list.first->pprev = &imd->inode_list.first;
2387 if (imd->modified) {
2388 ERROR("Cannot mount image that was added "
2389 "with wimlib_add_image()");
2390 ret = WIMLIB_ERR_INVALID_PARAM;
2394 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2395 ret = lock_wim(wim, wim->fp);
2400 /* Use default stream interface if one was not specified */
2401 if (!(mount_flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
2402 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
2403 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
2404 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
2407 DEBUG("Initializing struct wimfs_context");
2408 init_wimfs_context(&ctx);
2410 ctx.mount_flags = mount_flags;
2411 ctx.image_inode_list = &imd->inode_list;
2413 if (mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
2414 ctx.default_lookup_flags = LOOKUP_FLAG_ADS_OK;
2416 DEBUG("Unlinking message queues in case they already exist");
2417 ret = set_message_queue_names(&ctx, dir);
2420 unlink_message_queues(&ctx);
2422 DEBUG("Preparing arguments to fuse_main()");
2424 dir_copy = STRDUP(dir);
2426 goto out_free_message_queue_names;
2428 argv[argc++] = "imagex";
2429 argv[argc++] = dir_copy;
2431 /* disable multi-threaded operation for read-write mounts */
2432 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2433 argv[argc++] = "-s";
2435 if (mount_flags & WIMLIB_MOUNT_FLAG_DEBUG)
2436 argv[argc++] = "-d";
2439 * We provide the use_ino option to the FUSE mount because we are going
2440 * to assign inode numbers ourselves. */
2441 char optstring[256] =
2445 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 8)
2449 argv[argc++] = "-o";
2450 argv[argc++] = optstring;
2451 if ((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
2452 /* Read-write mount. Make the staging directory */
2453 ret = make_staging_dir(&ctx, staging_dir);
2455 goto out_free_dir_copy;
2457 /* Read-only mount */
2458 strcat(optstring, ",ro");
2465 DEBUG("FUSE command line (argc = %d): ", argc);
2466 for (i = 0; i < argc; i++) {
2467 fputs(argv[i], stdout);
2475 /* Mark dentry tree as modified if read-write mount. */
2476 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2478 imd->has_been_mounted_rw = 1;
2481 /* Resolve the lookup table entries for every inode in the image, and
2482 * assign inode numbers */
2483 DEBUG("Resolving lookup table entries and assigning inode numbers");
2485 hlist_for_each_entry(inode, cur_node, &imd->inode_list, i_hlist) {
2486 inode_resolve_ltes(inode, wim->lookup_table);
2487 inode->i_ino = ctx.next_ino++;
2489 DEBUG("(next_ino = %"PRIu64")", ctx.next_ino);
2491 DEBUG("Calling fuse_main()");
2493 ret = fuse_main(argc, argv, &wimfs_operations, &ctx);
2495 DEBUG("Returned from fuse_main() (ret = %d)", ret);
2498 ret = WIMLIB_ERR_FUSE;
2500 /* Try to delete the staging directory if a deletion wasn't yet
2501 * attempted due to an earlier error */
2502 if (ctx.staging_dir_name)
2503 delete_staging_dir(&ctx);
2508 wim->wim_locked = 0;
2509 out_free_message_queue_names:
2510 free_message_queue_names(&ctx);
2512 if (num_additional_swms) {
2513 free_lookup_table(wim->lookup_table);
2514 wim->lookup_table = wim_tab_save;
2520 * Unmounts the WIM file that was previously mounted on @dir by using
2521 * wimlib_mount_image().
2523 WIMLIBAPI int wimlib_unmount_image(const char *dir, int unmount_flags,
2524 wimlib_progress_func_t progress_func)
2527 struct wimfs_context wimfs_ctx;
2529 init_wimfs_context(&wimfs_ctx);
2531 ret = set_message_queue_names(&wimfs_ctx, dir);
2535 ret = open_message_queues(&wimfs_ctx, false);
2537 goto out_free_message_queue_names;
2539 ret = send_unmount_request_msg(wimfs_ctx.unmount_to_daemon_mq,
2541 progress_func != NULL);
2543 goto out_close_message_queues;
2545 ret = execute_fusermount(dir);
2547 goto out_close_message_queues;
2549 struct unmount_msg_handler_context handler_ctx = {
2551 .timeout_seconds = 5,
2554 .progress_func = progress_func,
2557 ret = message_loop(wimfs_ctx.daemon_to_unmount_mq,
2558 &unmount_msg_handler_callbacks,
2561 ret = handler_ctx.status;
2562 out_close_message_queues:
2563 close_message_queues(&wimfs_ctx);
2564 out_free_message_queue_names:
2565 free_message_queue_names(&wimfs_ctx);
2570 #else /* WITH_FUSE */
2573 static inline int mount_unsupported_error()
2575 ERROR("wimlib was compiled with --without-fuse, which disables support "
2576 "for mounting WIMs.");
2577 return WIMLIB_ERR_UNSUPPORTED;
2580 WIMLIBAPI int wimlib_unmount_image(const char *dir, int unmount_flags,
2581 wimlib_progress_func_t progress_func)
2583 return mount_unsupported_error();
2586 WIMLIBAPI int wimlib_mount_image(WIMStruct *wim_p, int image, const char *dir,
2587 int mount_flags, WIMStruct **additional_swms,
2588 unsigned num_additional_swms,
2589 const char *staging_dir)
2591 return mount_unsupported_error();
2594 #endif /* WITH_FUSE */