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(lte, &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;
858 ctx->unmount_to_daemon_mq_name = strcat_dup(u2d_prefix, dir_path,
860 if (!ctx->unmount_to_daemon_mq_name) {
861 ret = WIMLIB_ERR_NOMEM;
862 goto out_free_dir_path;
864 ctx->daemon_to_unmount_mq_name = strcat_dup(d2u_prefix, dir_path,
866 if (!ctx->daemon_to_unmount_mq_name) {
867 ret = WIMLIB_ERR_NOMEM;
868 goto out_free_unmount_to_daemon_mq_name;
872 goto out_free_dir_path;
873 out_free_unmount_to_daemon_mq_name:
874 FREE(ctx->unmount_to_daemon_mq_name);
875 ctx->unmount_to_daemon_mq_name = NULL;
881 static void free_message_queue_names(struct wimfs_context *ctx)
883 FREE(ctx->unmount_to_daemon_mq_name);
884 FREE(ctx->daemon_to_unmount_mq_name);
885 ctx->unmount_to_daemon_mq_name = NULL;
886 ctx->daemon_to_unmount_mq_name = NULL;
890 * Opens two POSIX message queue: one for sending messages from the unmount
891 * process to the daemon process, and one to go the other way. The names of the
892 * message queues, which must be system-wide unique, are be based on the mount
895 * @daemon specifies whether the calling process is the filesystem daemon or the
898 static int open_message_queues(struct wimfs_context *ctx, bool daemon)
900 int unmount_to_daemon_mq_flags = O_WRONLY | O_CREAT;
901 int daemon_to_unmount_mq_flags = O_RDONLY | O_CREAT;
904 swap(unmount_to_daemon_mq_flags, daemon_to_unmount_mq_flags);
906 DEBUG("Opening message queue \"%s\"", ctx->unmount_to_daemon_mq_name);
907 ctx->unmount_to_daemon_mq = mq_open(ctx->unmount_to_daemon_mq_name,
908 unmount_to_daemon_mq_flags, 0700, NULL);
910 if (ctx->unmount_to_daemon_mq == (mqd_t)-1) {
911 ERROR_WITH_ERRNO("mq_open()");
912 return WIMLIB_ERR_MQUEUE;
915 DEBUG("Opening message queue \"%s\"", ctx->daemon_to_unmount_mq_name);
916 ctx->daemon_to_unmount_mq = mq_open(ctx->daemon_to_unmount_mq_name,
917 daemon_to_unmount_mq_flags, 0700, NULL);
919 if (ctx->daemon_to_unmount_mq == (mqd_t)-1) {
920 ERROR_WITH_ERRNO("mq_open()");
921 mq_close(ctx->unmount_to_daemon_mq);
922 mq_unlink(ctx->unmount_to_daemon_mq_name);
923 ctx->unmount_to_daemon_mq = (mqd_t)-1;
924 return WIMLIB_ERR_MQUEUE;
929 /* Try to determine the maximum message size of a message queue. The return
930 * value is the maximum message size, or a guess of 8192 bytes if it cannot be
932 static long mq_get_msgsize(mqd_t mq)
934 static const char *msgsize_max_file = "/proc/sys/fs/mqueue/msgsize_max";
939 if (mq_getattr(mq, &attr) == 0) {
940 msgsize = attr.mq_msgsize;
942 ERROR_WITH_ERRNO("mq_getattr()");
943 ERROR("Attempting to read %s", msgsize_max_file);
944 fp = fopen(msgsize_max_file, "rb");
946 if (fscanf(fp, "%ld", &msgsize) != 1) {
947 ERROR("Assuming message size of 8192");
952 ERROR_WITH_ERRNO("Failed to open the file `%s'",
954 ERROR("Assuming message size of 8192");
961 static int get_mailbox(mqd_t mq, long needed_msgsize, long *msgsize_ret,
967 msgsize = mq_get_msgsize(mq);
969 if (msgsize < needed_msgsize) {
970 ERROR("Message queue max size must be at least %ld!",
972 return WIMLIB_ERR_MQUEUE;
975 mailbox = MALLOC(msgsize);
977 ERROR("Failed to allocate %ld bytes for mailbox", msgsize);
978 return WIMLIB_ERR_NOMEM;
980 *msgsize_ret = msgsize;
981 *mailbox_ret = mailbox;
985 static void unlink_message_queues(struct wimfs_context *ctx)
987 mq_unlink(ctx->unmount_to_daemon_mq_name);
988 mq_unlink(ctx->daemon_to_unmount_mq_name);
991 /* Closes the message queues, which are allocated in static variables */
992 static void close_message_queues(struct wimfs_context *ctx)
994 DEBUG("Closing message queues");
995 mq_close(ctx->unmount_to_daemon_mq);
996 ctx->unmount_to_daemon_mq = (mqd_t)(-1);
997 mq_close(ctx->daemon_to_unmount_mq);
998 ctx->daemon_to_unmount_mq = (mqd_t)(-1);
999 unlink_message_queues(ctx);
1003 struct unmount_msg_hdr {
1010 struct msg_unmount_request {
1011 struct unmount_msg_hdr hdr;
1013 u8 want_progress_messages;
1016 struct msg_daemon_info {
1017 struct unmount_msg_hdr hdr;
1022 struct msg_unmount_finished {
1023 struct unmount_msg_hdr hdr;
1027 struct msg_write_streams_progress {
1028 struct unmount_msg_hdr hdr;
1029 union wimlib_progress_info info;
1033 MSG_TYPE_UNMOUNT_REQUEST,
1034 MSG_TYPE_DAEMON_INFO,
1035 MSG_TYPE_WRITE_STREAMS_PROGRESS,
1036 MSG_TYPE_UNMOUNT_FINISHED,
1040 struct msg_handler_context_hdr {
1041 int timeout_seconds;
1044 struct unmount_msg_handler_context {
1045 struct msg_handler_context_hdr hdr;
1049 wimlib_progress_func_t progress_func;
1052 struct daemon_msg_handler_context {
1053 struct msg_handler_context_hdr hdr;
1054 struct wimfs_context *wimfs_ctx;
1057 static int send_unmount_request_msg(mqd_t mq, int unmount_flags,
1058 u8 want_progress_messages)
1060 DEBUG("Sending unmount request msg");
1061 struct msg_unmount_request msg = {
1063 .min_version = WIMLIB_MAKEVERSION(1, 2, 1),
1064 .cur_version = WIMLIB_VERSION_CODE,
1065 .msg_type = MSG_TYPE_UNMOUNT_REQUEST,
1066 .msg_size = sizeof(msg),
1068 .unmount_flags = unmount_flags,
1069 .want_progress_messages = want_progress_messages,
1072 if (mq_send(mq, (void*)&msg, sizeof(msg), 1)) {
1073 ERROR_WITH_ERRNO("Failed to communicate with filesystem daemon");
1074 return WIMLIB_ERR_MQUEUE;
1079 static int send_daemon_info_msg(mqd_t mq, pid_t pid, int mount_flags)
1081 DEBUG("Sending daemon info msg (pid = %d, mount_flags=%x)",
1084 struct msg_daemon_info msg = {
1086 .min_version = WIMLIB_MAKEVERSION(1, 2, 1),
1087 .cur_version = WIMLIB_VERSION_CODE,
1088 .msg_type = MSG_TYPE_DAEMON_INFO,
1089 .msg_size = sizeof(msg),
1092 .mount_flags = mount_flags,
1094 if (mq_send(mq, (void*)&msg, sizeof(msg), 1)) {
1095 ERROR_WITH_ERRNO("Failed to send daemon info to unmount process");
1096 return WIMLIB_ERR_MQUEUE;
1101 static void send_unmount_finished_msg(mqd_t mq, int status)
1103 DEBUG("Sending unmount finished msg");
1104 struct msg_unmount_finished msg = {
1106 .min_version = WIMLIB_MAKEVERSION(1, 2, 1),
1107 .cur_version = WIMLIB_VERSION_CODE,
1108 .msg_type = MSG_TYPE_UNMOUNT_FINISHED,
1109 .msg_size = sizeof(msg),
1113 if (mq_send(mq, (void*)&msg, sizeof(msg), 1))
1114 ERROR_WITH_ERRNO("Failed to send status to unmount process");
1117 static int unmount_progress_func(enum wimlib_progress_msg msg,
1118 const union wimlib_progress_info *info)
1120 if (msg == WIMLIB_PROGRESS_MSG_WRITE_STREAMS) {
1121 struct msg_write_streams_progress msg = {
1123 .min_version = WIMLIB_MAKEVERSION(1, 2, 1),
1124 .cur_version = WIMLIB_VERSION_CODE,
1125 .msg_type = MSG_TYPE_WRITE_STREAMS_PROGRESS,
1126 .msg_size = sizeof(msg),
1130 if (mq_send(wimfs_get_context()->daemon_to_unmount_mq,
1131 (void*)&msg, sizeof(msg), 1))
1133 ERROR_WITH_ERRNO("Failed to send progress information "
1134 "to unmount process");
1140 static int msg_unmount_request_handler(const void *_msg, void *_handler_ctx)
1142 const struct msg_unmount_request *msg = _msg;
1143 struct daemon_msg_handler_context *handler_ctx = _handler_ctx;
1144 struct wimfs_context *wimfs_ctx;
1148 wimlib_progress_func_t progress_func;
1150 DEBUG("Handling unmount request msg");
1152 wimfs_ctx = handler_ctx->wimfs_ctx;
1153 if (msg->hdr.msg_size < sizeof(*msg)) {
1154 status = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1158 unmount_flags = msg->unmount_flags;
1159 if (msg->want_progress_messages)
1160 progress_func = unmount_progress_func;
1162 progress_func = NULL;
1164 ret = send_daemon_info_msg(wimfs_ctx->daemon_to_unmount_mq, getpid(),
1165 wimfs_ctx->mount_flags);
1171 if (wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1172 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_COMMIT) {
1173 int write_flags = 0;
1174 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY)
1175 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1176 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_REBUILD)
1177 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1178 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_RECOMPRESS)
1179 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
1180 status = rebuild_wim(wimfs_ctx, write_flags,
1184 DEBUG("Read-only mount");
1189 if (wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1190 ret = delete_staging_dir(wimfs_ctx);
1192 ERROR("Failed to delete the staging directory");
1197 send_unmount_finished_msg(wimfs_ctx->daemon_to_unmount_mq, status);
1198 return MSG_BREAK_LOOP;
1201 static int msg_daemon_info_handler(const void *_msg, void *_handler_ctx)
1203 const struct msg_daemon_info *msg = _msg;
1204 struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1206 DEBUG("Handling daemon info msg");
1207 if (msg->hdr.msg_size < sizeof(*msg))
1208 return WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1209 handler_ctx->daemon_pid = msg->daemon_pid;
1210 handler_ctx->mount_flags = msg->mount_flags;
1211 handler_ctx->hdr.timeout_seconds = 1;
1212 DEBUG("pid of daemon is %d; mount flags were %#x",
1213 handler_ctx->daemon_pid,
1214 handler_ctx->mount_flags);
1218 static int msg_write_streams_progress_handler(const void *_msg,
1221 const struct msg_write_streams_progress *msg = _msg;
1222 struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1224 if (msg->hdr.msg_size < sizeof(*msg))
1225 return WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1226 if (handler_ctx->progress_func) {
1227 handler_ctx->progress_func(WIMLIB_PROGRESS_MSG_WRITE_STREAMS,
1233 static int msg_unmount_finished_handler(const void *_msg, void *_handler_ctx)
1235 const struct msg_unmount_finished *msg = _msg;
1236 struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1238 DEBUG("Handling unmount finished message");
1239 if (msg->hdr.msg_size < sizeof(*msg))
1240 return WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1241 handler_ctx->status = msg->status;
1242 DEBUG("status is %d", handler_ctx->status);
1243 return MSG_BREAK_LOOP;
1246 static int unmount_timed_out_cb(void *_handler_ctx)
1248 struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1250 if (handler_ctx->daemon_pid == 0) {
1253 kill(handler_ctx->daemon_pid, 0);
1254 if (errno == ESRCH) {
1257 DEBUG("Filesystem daemon is still alive... "
1258 "Waiting another %d seconds\n",
1259 handler_ctx->hdr.timeout_seconds);
1264 ERROR("The filesystem daemon has crashed! Changes to the "
1265 "WIM may not have been commited.");
1266 return WIMLIB_ERR_FILESYSTEM_DAEMON_CRASHED;
1269 static int daemon_timed_out_cb(void *_handler_ctx)
1271 ERROR("Timed out waiting for unmount request! "
1272 "Changes to the mounted WIM will not be committed.");
1273 return WIMLIB_ERR_TIMEOUT;
1276 typedef int (*msg_handler_t)(const void *_msg, void *_handler_ctx);
1278 struct msg_handler_callbacks {
1279 int (*timed_out)(void * _handler_ctx);
1280 msg_handler_t msg_handlers[MSG_TYPE_MAX];
1283 static const struct msg_handler_callbacks unmount_msg_handler_callbacks = {
1284 .timed_out = unmount_timed_out_cb,
1286 [MSG_TYPE_DAEMON_INFO] = msg_daemon_info_handler,
1287 [MSG_TYPE_WRITE_STREAMS_PROGRESS] = msg_write_streams_progress_handler,
1288 [MSG_TYPE_UNMOUNT_FINISHED] = msg_unmount_finished_handler,
1292 static const struct msg_handler_callbacks daemon_msg_handler_callbacks = {
1293 .timed_out = daemon_timed_out_cb,
1295 [MSG_TYPE_UNMOUNT_REQUEST] = msg_unmount_request_handler,
1299 static int receive_message(mqd_t mq,
1300 struct msg_handler_context_hdr *handler_ctx,
1301 const msg_handler_t msg_handlers[],
1302 long mailbox_size, void *mailbox)
1305 struct timespec timeout;
1306 ssize_t bytes_received;
1307 struct unmount_msg_hdr *hdr;
1310 gettimeofday(&now, NULL);
1311 timeout.tv_sec = now.tv_sec + handler_ctx->timeout_seconds;
1312 timeout.tv_nsec = now.tv_usec * 1000;
1314 bytes_received = mq_timedreceive(mq, mailbox,
1315 mailbox_size, NULL, &timeout);
1317 if (bytes_received == -1) {
1318 if (errno == ETIMEDOUT) {
1319 ret = WIMLIB_ERR_TIMEOUT;
1321 ERROR_WITH_ERRNO("mq_timedreceive()");
1322 ret = WIMLIB_ERR_MQUEUE;
1324 } else if (bytes_received < sizeof(*hdr) ||
1325 bytes_received != hdr->msg_size) {
1326 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1327 } else if (WIMLIB_VERSION_CODE < hdr->min_version) {
1328 /*ERROR("Cannot understand the received message. "*/
1329 /*"Please upgrade wimlib to at least v%d.%d.%d",*/
1330 /*WIMLIB_GET_MAJOR_VERSION(hdr->min_version),*/
1331 /*WIMLIB_GET_MINOR_VERSION(hdr->min_version),*/
1332 /*WIMLIB_GET_PATCH_VERSION(hdr->min_version));*/
1333 ret = MSG_VERSION_TOO_HIGH;
1334 } else if (hdr->msg_type >= MSG_TYPE_MAX) {
1335 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1336 } else if (msg_handlers[hdr->msg_type] == NULL) {
1337 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1339 ret = msg_handlers[hdr->msg_type](mailbox, handler_ctx);
1344 static int message_loop(mqd_t mq,
1345 const struct msg_handler_callbacks *callbacks,
1346 struct msg_handler_context_hdr *handler_ctx)
1348 static const size_t MAX_MSG_SIZE = 512;
1353 DEBUG("Entering message loop");
1355 ret = get_mailbox(mq, MAX_MSG_SIZE, &msgsize, &mailbox);
1359 ret = receive_message(mq, handler_ctx,
1360 callbacks->msg_handlers,
1362 if (ret == 0 || ret == MSG_VERSION_TOO_HIGH) {
1364 } else if (ret == MSG_BREAK_LOOP) {
1367 } else if (ret == WIMLIB_ERR_TIMEOUT) {
1368 if (callbacks->timed_out)
1369 ret = callbacks->timed_out(handler_ctx);
1375 ERROR_WITH_ERRNO("Error communicating with "
1376 "filesystem daemon");
1381 DEBUG("Exiting message loop");
1385 /* Execute `fusermount -u', which is installed setuid root, to unmount the WIM.
1387 * FUSE does not yet implement synchronous unmounts. This means that fusermount
1388 * -u will return before the filesystem daemon returns from wimfs_destroy().
1389 * This is partly what we want, because we need to send a message from this
1390 * process to the filesystem daemon telling whether --commit was specified or
1391 * not. However, after that, the unmount process must wait for the filesystem
1392 * daemon to finish writing the WIM file.
1394 static int execute_fusermount(const char *dir)
1402 ERROR_WITH_ERRNO("Failed to fork()");
1403 return WIMLIB_ERR_FORK;
1407 execlp("fusermount", "fusermount", "-u", dir, NULL);
1408 ERROR_WITH_ERRNO("Failed to execute `fusermount'");
1409 exit(WIMLIB_ERR_FUSERMOUNT);
1413 ret = waitpid(pid, &status, 0);
1415 ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
1417 return WIMLIB_ERR_FUSERMOUNT;
1420 if (!WIFEXITED(status)) {
1421 ERROR("'fusermount' did not terminate normally!");
1422 return WIMLIB_ERR_FUSERMOUNT;
1425 status = WEXITSTATUS(status);
1430 if (status != WIMLIB_ERR_FUSERMOUNT)
1431 return WIMLIB_ERR_FUSERMOUNT;
1433 /* Try again, but with the `umount' program. This is required on other
1434 * FUSE implementations such as FreeBSD's that do not have a
1435 * `fusermount' program. */
1436 ERROR("Falling back to 'umount'. Note: you may need to be "
1437 "root for this to work");
1440 ERROR_WITH_ERRNO("Failed to fork()");
1441 return WIMLIB_ERR_FORK;
1445 execlp("umount", "umount", dir, NULL);
1446 ERROR_WITH_ERRNO("Failed to execute `umount'");
1447 exit(WIMLIB_ERR_FUSERMOUNT);
1451 ret = waitpid(pid, &status, 0);
1453 ERROR_WITH_ERRNO("Failed to wait for `umount' process to "
1455 return WIMLIB_ERR_FUSERMOUNT;
1458 ERROR("`umount' did not successfully complete");
1459 return WIMLIB_ERR_FUSERMOUNT;
1464 static int wimfs_access(const char *path, int mask)
1466 /* Permissions not implemented */
1470 static int wimfs_chmod(const char *path, mode_t mask)
1472 struct wim_dentry *dentry;
1473 struct wimfs_context *ctx = wimfs_get_context();
1474 struct wim_inode *inode;
1478 ret = lookup_resource(ctx->wim, path,
1479 get_lookup_flags(ctx) | LOOKUP_FLAG_DIRECTORY_OK,
1480 &dentry, NULL, NULL);
1483 inode = dentry->d_inode;
1484 inode_to_stbuf(inode, NULL, &stbuf);
1485 if (mask == stbuf.st_mode)
1491 /* Called when the filesystem is unmounted. */
1492 static void wimfs_destroy(void *p)
1494 struct wimfs_context *wimfs_ctx = wimfs_get_context();
1495 if (open_message_queues(wimfs_ctx, true) == 0) {
1496 struct daemon_msg_handler_context handler_ctx = {
1498 .timeout_seconds = 5,
1500 .wimfs_ctx = wimfs_ctx,
1502 message_loop(wimfs_ctx->unmount_to_daemon_mq,
1503 &daemon_msg_handler_callbacks,
1505 close_message_queues(wimfs_ctx);
1510 static int wimfs_fallocate(const char *path, int mode,
1511 off_t offset, off_t len, struct fuse_file_info *fi)
1513 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1514 wimlib_assert(fd->staging_fd != -1);
1515 return fallocate(fd->staging_fd, mode, offset, len);
1520 static int wimfs_fgetattr(const char *path, struct stat *stbuf,
1521 struct fuse_file_info *fi)
1523 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1524 return inode_to_stbuf(fd->f_inode, fd->f_lte, stbuf);
1527 static int wimfs_ftruncate(const char *path, off_t size,
1528 struct fuse_file_info *fi)
1530 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1531 int ret = ftruncate(fd->staging_fd, size);
1534 if (fd->f_lte && size < fd->f_lte->resource_entry.original_size)
1535 fd->f_lte->resource_entry.original_size = size;
1540 * Fills in a `struct stat' that corresponds to a file or directory in the WIM.
1542 static int wimfs_getattr(const char *path, struct stat *stbuf)
1544 struct wim_dentry *dentry;
1545 struct wim_lookup_table_entry *lte;
1547 struct wimfs_context *ctx = wimfs_get_context();
1549 ret = lookup_resource(ctx->wim, path,
1550 get_lookup_flags(ctx) | LOOKUP_FLAG_DIRECTORY_OK,
1551 &dentry, <e, NULL);
1554 return inode_to_stbuf(dentry->d_inode, lte, stbuf);
1558 /* Read an alternate data stream through the XATTR interface, or get its size */
1559 static int wimfs_getxattr(const char *path, const char *name, char *value,
1563 struct wim_inode *inode;
1564 struct wim_ads_entry *ads_entry;
1566 struct wim_lookup_table_entry *lte;
1567 struct wimfs_context *ctx = wimfs_get_context();
1569 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1572 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1576 inode = wim_pathname_to_inode(ctx->wim, path);
1580 ads_entry = inode_get_ads_entry(inode, name, NULL);
1584 lte = ads_entry->lte;
1585 res_size = wim_resource_size(lte);
1590 if (res_size > size)
1593 ret = read_full_wim_resource(lte, (u8*)value,
1594 WIMLIB_RESOURCE_FLAG_MULTITHREADED);
1602 /* Create a hard link */
1603 static int wimfs_link(const char *to, const char *from)
1605 struct wim_dentry *from_dentry, *from_dentry_parent;
1606 const char *link_name;
1607 struct wim_inode *inode;
1608 struct wim_lookup_table_entry *lte;
1609 WIMStruct *w = wimfs_get_WIMStruct();
1612 inode = wim_pathname_to_inode(w, to);
1616 if (inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
1617 FILE_ATTRIBUTE_REPARSE_POINT))
1620 from_dentry_parent = get_parent_dentry(w, from);
1621 if (!from_dentry_parent)
1623 if (!dentry_is_directory(from_dentry_parent))
1626 link_name = path_basename(from);
1627 if (get_dentry_child_with_name(from_dentry_parent, link_name))
1629 from_dentry = new_dentry(link_name);
1633 inode_add_dentry(from_dentry, inode);
1634 from_dentry->d_inode = inode;
1637 for (i = 0; i <= inode->i_num_ads; i++) {
1638 lte = inode_stream_lte_resolved(inode, i);
1642 dentry_add_child(from_dentry_parent, from_dentry);
1647 static int wimfs_listxattr(const char *path, char *list, size_t size)
1650 struct wim_inode *inode;
1651 struct wimfs_context *ctx = wimfs_get_context();
1655 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1658 /* List alternate data streams, or get the list size */
1660 inode = wim_pathname_to_inode(ctx->wim, path);
1666 for (i = 0; i < inode->i_num_ads; i++)
1667 needed_size += inode->i_ads_entries[i].stream_name_utf8_len + 6;
1671 for (i = 0; i < inode->i_num_ads; i++) {
1672 needed_size = inode->i_ads_entries[i].stream_name_utf8_len + 6;
1673 if (needed_size > size)
1675 p += sprintf(p, "user.%s",
1676 inode->i_ads_entries[i].stream_name_utf8) + 1;
1677 size -= needed_size;
1685 /* Create a directory in the WIM.
1686 * @mode is currently ignored. */
1687 static int wimfs_mkdir(const char *path, mode_t mode)
1689 struct wim_dentry *dentry;
1692 ret = create_dentry(wimfs_get_context(), path, &dentry);
1694 dentry->d_inode->i_attributes = FILE_ATTRIBUTE_DIRECTORY;
1698 /* Create a regular file in the WIM.
1699 * @mode is currently ignored. */
1700 static int wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
1702 const char *stream_name;
1703 struct wimfs_context *ctx = wimfs_get_context();
1704 if ((ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
1705 && (stream_name = path_stream_name(path))) {
1706 /* Make an alternate data stream */
1707 struct wim_ads_entry *new_entry;
1708 struct wim_inode *inode;
1710 char *p = (char*)stream_name - 1;
1711 wimlib_assert(*p == ':');
1714 inode = wim_pathname_to_inode(ctx->wim, path);
1717 if (inode->i_attributes &
1718 (FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_DIRECTORY))
1720 if (inode_get_ads_entry(inode, stream_name, NULL))
1722 new_entry = inode_add_ads(inode, stream_name);
1727 struct wim_dentry *dentry;
1730 /* Make a normal file (not an alternate data stream) */
1731 ret = create_dentry(ctx, path, &dentry);
1733 dentry->d_inode->i_attributes = FILE_ATTRIBUTE_NORMAL;
1740 static int wimfs_open(const char *path, struct fuse_file_info *fi)
1742 struct wim_dentry *dentry;
1743 struct wim_lookup_table_entry *lte;
1745 struct wimfs_fd *fd;
1746 struct wim_inode *inode;
1749 struct wimfs_context *ctx = wimfs_get_context();
1751 ret = lookup_resource(ctx->wim, path, get_lookup_flags(ctx),
1752 &dentry, <e, &stream_idx);
1756 inode = dentry->d_inode;
1758 if (stream_idx == 0)
1761 stream_id = inode->i_ads_entries[stream_idx - 1].stream_id;
1763 /* The file resource may be in the staging directory (read-write mounts
1764 * only) or in the WIM. If it's in the staging directory, we need to
1765 * open a native file descriptor for the corresponding file. Otherwise,
1766 * we can read the file resource directly from the WIM file if we are
1767 * opening it read-only, but we need to extract the resource to the
1768 * staging directory if we are opening it writable. */
1770 if (flags_writable(fi->flags) &&
1771 (!lte || lte->resource_location != RESOURCE_IN_STAGING_FILE)) {
1772 u64 size = (lte) ? wim_resource_size(lte) : 0;
1773 ret = extract_resource_to_staging_dir(inode, stream_id,
1779 ret = alloc_wimfs_fd(inode, stream_id, lte, &fd,
1780 wimfs_ctx_readonly(ctx));
1784 if (lte && lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1785 fd->staging_fd = open(lte->staging_file_name, fi->flags);
1786 if (fd->staging_fd == -1) {
1787 int errno_save = errno;
1792 fi->fh = (uintptr_t)fd;
1796 /* Opens a directory. */
1797 static int wimfs_opendir(const char *path, struct fuse_file_info *fi)
1799 struct wim_inode *inode;
1801 struct wimfs_fd *fd = NULL;
1802 struct wimfs_context *ctx = wimfs_get_context();
1803 WIMStruct *w = ctx->wim;
1805 inode = wim_pathname_to_inode(w, path);
1808 if (!inode_is_directory(inode))
1810 ret = alloc_wimfs_fd(inode, 0, NULL, &fd, wimfs_ctx_readonly(ctx));
1811 fi->fh = (uintptr_t)fd;
1817 * Read data from a file in the WIM or in the staging directory.
1819 static int wimfs_read(const char *path, char *buf, size_t size,
1820 off_t offset, struct fuse_file_info *fi)
1822 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1828 if (!fd->f_lte) /* Empty stream with no lookup table entry */
1831 if (fd->f_lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1832 /* Read from staging file */
1834 wimlib_assert(fd->f_lte->staging_file_name);
1835 wimlib_assert(fd->staging_fd != -1);
1837 DEBUG("Seek to offset %"PRIu64, offset);
1839 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1841 ret = read(fd->staging_fd, buf, size);
1847 u64 res_size = wim_resource_size(fd->f_lte);
1848 if (offset > res_size)
1850 size = min(size, res_size - offset);
1851 if (read_wim_resource(fd->f_lte, (u8*)buf,
1853 WIMLIB_RESOURCE_FLAG_MULTITHREADED) != 0)
1859 struct fill_params {
1861 fuse_fill_dir_t filler;
1864 static int dentry_fuse_fill(struct wim_dentry *dentry, void *arg)
1866 struct fill_params *fill_params = arg;
1867 return fill_params->filler(fill_params->buf, dentry->file_name_utf8,
1871 /* Fills in the entries of the directory specified by @path using the
1872 * FUSE-provided function @filler. */
1873 static int wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
1874 off_t offset, struct fuse_file_info *fi)
1876 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1877 struct wim_inode *inode;
1882 inode = fd->f_inode;
1884 struct fill_params fill_params = {
1889 filler(buf, ".", NULL, 0);
1890 filler(buf, "..", NULL, 0);
1892 return for_dentry_in_rbtree(inode->i_children.rb_node,
1893 dentry_fuse_fill, &fill_params);
1897 static int wimfs_readlink(const char *path, char *buf, size_t buf_len)
1899 struct wimfs_context *ctx = wimfs_get_context();
1900 struct wim_inode *inode = wim_pathname_to_inode(ctx->wim, path);
1904 if (!inode_is_symlink(inode))
1907 ret = inode_readlink(inode, buf, buf_len, ctx->wim,
1908 WIMLIB_RESOURCE_FLAG_MULTITHREADED);
1915 static int wimfs_release(const char *path, struct fuse_file_info *fi)
1917 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1918 return close_wimfs_fd(fd);
1921 /* Close a directory */
1922 static int wimfs_releasedir(const char *path, struct fuse_file_info *fi)
1924 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1925 return close_wimfs_fd(fd);
1929 /* Remove an alternate data stream through the XATTR interface */
1930 static int wimfs_removexattr(const char *path, const char *name)
1932 struct wim_inode *inode;
1933 struct wim_ads_entry *ads_entry;
1935 struct wimfs_context *ctx = wimfs_get_context();
1937 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1940 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1944 inode = wim_pathname_to_inode(ctx->wim, path);
1948 ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
1951 inode_remove_ads(inode, ads_idx, ctx->wim->lookup_table);
1956 /* Renames a file or directory. See rename (3) */
1957 static int wimfs_rename(const char *from, const char *to)
1959 struct wim_dentry *src;
1960 struct wim_dentry *dst;
1961 struct wim_dentry *parent_of_dst;
1962 WIMStruct *w = wimfs_get_WIMStruct();
1965 /* This rename() implementation currently only supports actual files
1966 * (not alternate data streams) */
1968 src = get_dentry(w, from);
1972 dst = get_dentry(w, to);
1975 /* Destination file exists */
1977 if (src == dst) /* Same file */
1980 if (!dentry_is_directory(src)) {
1981 /* Cannot rename non-directory to directory. */
1982 if (dentry_is_directory(dst))
1985 /* Cannot rename directory to a non-directory or a non-empty
1987 if (!dentry_is_directory(dst))
1989 if (inode_has_children(dst->d_inode))
1992 parent_of_dst = dst->parent;
1994 /* Destination does not exist */
1995 parent_of_dst = get_parent_dentry(w, to);
1999 if (!dentry_is_directory(parent_of_dst))
2003 ret = set_dentry_name(src, path_basename(to));
2007 remove_dentry(dst, w->lookup_table);
2009 dentry_add_child(parent_of_dst, src);
2013 /* Remove a directory */
2014 static int wimfs_rmdir(const char *path)
2016 struct wim_dentry *parent, *dentry;
2017 WIMStruct *w = wimfs_get_WIMStruct();
2019 dentry = get_dentry(w, path);
2023 if (!dentry_is_directory(dentry))
2026 if (dentry_has_children(dentry))
2029 remove_dentry(dentry, w->lookup_table);
2034 /* Write an alternate data stream through the XATTR interface */
2035 static int wimfs_setxattr(const char *path, const char *name,
2036 const char *value, size_t size, int flags)
2038 struct wim_ads_entry *existing_ads_entry;
2039 struct wim_ads_entry *new_ads_entry;
2040 struct wim_lookup_table_entry *existing_lte;
2041 struct wim_lookup_table_entry *lte;
2042 struct wim_inode *inode;
2043 u8 value_hash[SHA1_HASH_SIZE];
2045 struct wimfs_context *ctx = wimfs_get_context();
2047 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
2050 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
2054 inode = wim_pathname_to_inode(ctx->wim, path);
2058 existing_ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
2059 if (existing_ads_entry) {
2060 if (flags & XATTR_CREATE)
2062 inode_remove_ads(inode, ads_idx, ctx->wim->lookup_table);
2064 if (flags & XATTR_REPLACE)
2067 new_ads_entry = inode_add_ads(inode, name);
2071 sha1_buffer((const u8*)value, size, value_hash);
2073 existing_lte = __lookup_resource(ctx->wim->lookup_table, value_hash);
2080 lte = new_lookup_table_entry();
2083 value_copy = MALLOC(size);
2088 memcpy(value_copy, value, size);
2089 lte->resource_location = RESOURCE_IN_ATTACHED_BUFFER;
2090 lte->attached_buffer = value_copy;
2091 lte->resource_entry.original_size = size;
2092 lte->resource_entry.size = size;
2093 lte->resource_entry.flags = 0;
2094 copy_hash(lte->hash, value_hash);
2095 lookup_table_insert(ctx->wim->lookup_table, lte);
2097 new_ads_entry->lte = lte;
2102 static int wimfs_symlink(const char *to, const char *from)
2104 struct wimfs_context *ctx = wimfs_get_context();
2105 struct wim_dentry *dentry;
2108 ret = create_dentry(ctx, from, &dentry);
2110 dentry->d_inode->i_attributes = FILE_ATTRIBUTE_REPARSE_POINT;
2111 dentry->d_inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
2112 if (inode_set_symlink(dentry->d_inode, to,
2113 ctx->wim->lookup_table, NULL))
2115 unlink_dentry(dentry);
2116 free_dentry(dentry);
2124 /* Reduce the size of a file */
2125 static int wimfs_truncate(const char *path, off_t size)
2127 struct wim_dentry *dentry;
2128 struct wim_lookup_table_entry *lte;
2132 struct wim_inode *inode;
2133 struct wimfs_context *ctx = wimfs_get_context();
2135 ret = lookup_resource(ctx->wim, path, get_lookup_flags(ctx),
2136 &dentry, <e, &stream_idx);
2141 if (lte == NULL && size == 0)
2144 inode = dentry->d_inode;
2145 if (stream_idx == 0)
2148 stream_id = inode->i_ads_entries[stream_idx - 1].stream_id;
2150 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
2151 ret = truncate(lte->staging_file_name, size);
2155 /* File in WIM. Extract it to the staging directory, but only
2156 * the first @size bytes of it. */
2157 ret = extract_resource_to_staging_dir(inode, stream_id,
2163 /* Unlink a non-directory or alternate data stream */
2164 static int wimfs_unlink(const char *path)
2166 struct wim_dentry *dentry;
2167 struct wim_lookup_table_entry *lte;
2170 struct wimfs_context *ctx = wimfs_get_context();
2172 ret = lookup_resource(ctx->wim, path, get_lookup_flags(ctx),
2173 &dentry, <e, &stream_idx);
2178 if (stream_idx == 0)
2179 remove_dentry(dentry, ctx->wim->lookup_table);
2181 inode_remove_ads(dentry->d_inode, stream_idx - 1,
2182 ctx->wim->lookup_table);
2186 #ifdef HAVE_UTIMENSAT
2188 * Change the timestamp on a file dentry.
2190 * Note that alternate data streams do not have their own timestamps.
2192 static int wimfs_utimens(const char *path, const struct timespec tv[2])
2194 struct wim_dentry *dentry;
2195 struct wim_inode *inode;
2196 WIMStruct *w = wimfs_get_WIMStruct();
2198 dentry = get_dentry(w, path);
2201 inode = dentry->d_inode;
2203 if (tv[0].tv_nsec != UTIME_OMIT) {
2204 if (tv[0].tv_nsec == UTIME_NOW)
2205 inode->i_last_access_time = get_wim_timestamp();
2207 inode->i_last_access_time = timespec_to_wim_timestamp(&tv[0]);
2209 if (tv[1].tv_nsec != UTIME_OMIT) {
2210 if (tv[1].tv_nsec == UTIME_NOW)
2211 inode->i_last_write_time = get_wim_timestamp();
2213 inode->i_last_write_time = timespec_to_wim_timestamp(&tv[1]);
2218 static int wimfs_utime(const char *path, struct utimbuf *times)
2220 struct wim_dentry *dentry;
2221 struct wim_inode *inode;
2222 WIMStruct *w = wimfs_get_WIMStruct();
2224 dentry = get_dentry(w, path);
2227 inode = dentry->d_inode;
2229 inode->i_last_write_time = unix_timestamp_to_wim(times->modtime);
2230 inode->i_last_access_time = unix_timestamp_to_wim(times->actime);
2235 /* Writes to a file in the WIM filesystem.
2236 * It may be an alternate data stream, but here we don't even notice because we
2237 * just get a lookup table entry. */
2238 static int wimfs_write(const char *path, const char *buf, size_t size,
2239 off_t offset, struct fuse_file_info *fi)
2241 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
2248 wimlib_assert(fd->f_lte);
2249 wimlib_assert(fd->f_lte->staging_file_name);
2250 wimlib_assert(fd->staging_fd != -1);
2251 wimlib_assert(fd->f_inode);
2253 /* Seek to the requested position */
2254 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
2257 /* Write the data. */
2258 ret = write(fd->staging_fd, buf, size);
2262 now = get_wim_timestamp();
2263 fd->f_inode->i_last_write_time = now;
2264 fd->f_inode->i_last_access_time = now;
2268 static struct fuse_operations wimfs_operations = {
2269 .access = wimfs_access,
2270 .chmod = wimfs_chmod,
2271 .destroy = wimfs_destroy,
2273 .fallocate = wimfs_fallocate,
2275 .fgetattr = wimfs_fgetattr,
2276 .ftruncate = wimfs_ftruncate,
2277 .getattr = wimfs_getattr,
2279 .getxattr = wimfs_getxattr,
2283 .listxattr = wimfs_listxattr,
2285 .mkdir = wimfs_mkdir,
2286 .mknod = wimfs_mknod,
2288 .opendir = wimfs_opendir,
2290 .readdir = wimfs_readdir,
2291 .readlink = wimfs_readlink,
2292 .release = wimfs_release,
2293 .releasedir = wimfs_releasedir,
2295 .removexattr = wimfs_removexattr,
2297 .rename = wimfs_rename,
2298 .rmdir = wimfs_rmdir,
2300 .setxattr = wimfs_setxattr,
2302 .symlink = wimfs_symlink,
2303 .truncate = wimfs_truncate,
2304 .unlink = wimfs_unlink,
2305 #ifdef HAVE_UTIMENSAT
2306 .utimens = wimfs_utimens,
2308 .utime = wimfs_utime,
2310 .write = wimfs_write,
2312 /* wimfs keeps file descriptor structures (struct wimfs_fd), so there is
2313 * no need to have the file path provided on operations such as read()
2314 * where only the file descriptor is needed. */
2315 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 8)
2316 .flag_nullpath_ok = 1,
2318 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 9)
2320 .flag_utime_omit_ok = 1,
2325 /* Mounts an image from a WIM file. */
2326 WIMLIBAPI int wimlib_mount_image(WIMStruct *wim, int image, const char *dir,
2327 int mount_flags, WIMStruct **additional_swms,
2328 unsigned num_additional_swms,
2329 const char *staging_dir)
2335 struct wim_lookup_table *joined_tab, *wim_tab_save;
2336 struct wim_image_metadata *imd;
2337 struct wimfs_context ctx;
2338 struct hlist_node *cur_node;
2339 struct wim_inode *inode;
2341 DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
2342 wim, image, dir, mount_flags);
2345 return WIMLIB_ERR_INVALID_PARAM;
2347 ret = verify_swm_set(wim, additional_swms, num_additional_swms);
2351 if ((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) && (wim->hdr.total_parts != 1)) {
2352 ERROR("Cannot mount a split WIM read-write");
2353 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
2356 if (num_additional_swms) {
2357 ret = new_joined_lookup_table(wim, additional_swms,
2358 num_additional_swms,
2362 wim_tab_save = wim->lookup_table;
2363 wim->lookup_table = joined_tab;
2366 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2367 ret = wim_run_full_verifications(wim);
2372 ret = select_wim_image(wim, image);
2376 DEBUG("Selected image %d", image);
2378 imd = wim_get_current_image_metadata(wim);
2380 if (imd->root_dentry->refcnt != 1) {
2381 ERROR("Cannot mount image that was just exported with "
2382 "wimlib_export_image()");
2383 ret = WIMLIB_ERR_INVALID_PARAM;
2387 if (imd->inode_list.first) /* May be unneeded? */
2388 imd->inode_list.first->pprev = &imd->inode_list.first;
2390 if (imd->modified) {
2391 ERROR("Cannot mount image that was added "
2392 "with wimlib_add_image()");
2393 ret = WIMLIB_ERR_INVALID_PARAM;
2397 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2398 ret = lock_wim(wim, wim->fp);
2403 /* Use default stream interface if one was not specified */
2404 if (!(mount_flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
2405 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
2406 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
2407 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
2410 DEBUG("Initializing struct wimfs_context");
2411 init_wimfs_context(&ctx);
2413 ctx.mount_flags = mount_flags;
2414 ctx.image_inode_list = &imd->inode_list;
2416 if (mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
2417 ctx.default_lookup_flags = LOOKUP_FLAG_ADS_OK;
2419 DEBUG("Unlinking message queues in case they already exist");
2420 ret = set_message_queue_names(&ctx, dir);
2423 unlink_message_queues(&ctx);
2425 DEBUG("Preparing arguments to fuse_main()");
2427 dir_copy = STRDUP(dir);
2429 goto out_free_message_queue_names;
2431 argv[argc++] = "imagex";
2432 argv[argc++] = dir_copy;
2434 /* disable multi-threaded operation for read-write mounts */
2435 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2436 argv[argc++] = "-s";
2438 if (mount_flags & WIMLIB_MOUNT_FLAG_DEBUG)
2439 argv[argc++] = "-d";
2442 * We provide the use_ino option to the FUSE mount because we are going
2443 * to assign inode numbers ourselves. */
2444 char optstring[256] =
2448 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 8)
2452 argv[argc++] = "-o";
2453 argv[argc++] = optstring;
2454 if ((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
2455 /* Read-write mount. Make the staging directory */
2456 ret = make_staging_dir(&ctx, staging_dir);
2458 goto out_free_dir_copy;
2460 /* Read-only mount */
2461 strcat(optstring, ",ro");
2468 DEBUG("FUSE command line (argc = %d): ", argc);
2469 for (i = 0; i < argc; i++) {
2470 fputs(argv[i], stdout);
2478 /* Mark dentry tree as modified if read-write mount. */
2479 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2481 imd->has_been_mounted_rw = 1;
2484 /* Resolve the lookup table entries for every inode in the image, and
2485 * assign inode numbers */
2486 DEBUG("Resolving lookup table entries and assigning inode numbers");
2488 hlist_for_each_entry(inode, cur_node, &imd->inode_list, i_hlist) {
2489 inode_resolve_ltes(inode, wim->lookup_table);
2490 inode->i_ino = ctx.next_ino++;
2492 DEBUG("(next_ino = %"PRIu64")", ctx.next_ino);
2494 DEBUG("Calling fuse_main()");
2496 ret = fuse_main(argc, argv, &wimfs_operations, &ctx);
2498 DEBUG("Returned from fuse_main() (ret = %d)", ret);
2501 ret = WIMLIB_ERR_FUSE;
2505 wim->wim_locked = 0;
2506 out_free_message_queue_names:
2507 free_message_queue_names(&ctx);
2509 if (num_additional_swms) {
2510 free_lookup_table(wim->lookup_table);
2511 wim->lookup_table = wim_tab_save;
2517 * Unmounts the WIM file that was previously mounted on @dir by using
2518 * wimlib_mount_image().
2520 WIMLIBAPI int wimlib_unmount_image(const char *dir, int unmount_flags,
2521 wimlib_progress_func_t progress_func)
2524 struct wimfs_context wimfs_ctx;
2526 init_wimfs_context(&wimfs_ctx);
2528 ret = set_message_queue_names(&wimfs_ctx, dir);
2532 ret = open_message_queues(&wimfs_ctx, false);
2534 goto out_free_message_queue_names;
2536 ret = send_unmount_request_msg(wimfs_ctx.unmount_to_daemon_mq,
2538 progress_func != NULL);
2540 goto out_close_message_queues;
2542 ret = execute_fusermount(dir);
2544 goto out_close_message_queues;
2546 struct unmount_msg_handler_context handler_ctx = {
2548 .timeout_seconds = 5,
2551 .progress_func = progress_func,
2554 ret = message_loop(wimfs_ctx.daemon_to_unmount_mq,
2555 &unmount_msg_handler_callbacks,
2558 ret = handler_ctx.status;
2559 out_close_message_queues:
2560 close_message_queues(&wimfs_ctx);
2561 out_free_message_queue_names:
2562 free_message_queue_names(&wimfs_ctx);
2567 #else /* WITH_FUSE */
2570 static inline int mount_unsupported_error()
2572 ERROR("wimlib was compiled with --without-fuse, which disables support "
2573 "for mounting WIMs.");
2574 return WIMLIB_ERR_UNSUPPORTED;
2577 WIMLIBAPI int wimlib_unmount_image(const char *dir, int unmount_flags,
2578 wimlib_progress_func_t progress_func)
2580 return mount_unsupported_error();
2583 WIMLIBAPI int wimlib_mount_image(WIMStruct *wim_p, int image, const char *dir,
2584 int mount_flags, WIMStruct **additional_swms,
2585 unsigned num_additional_swms,
2586 const char *staging_dir)
2588 return mount_unsupported_error();
2591 #endif /* WITH_FUSE */