4 * This file implements mounting of WIM files using FUSE, which stands for
5 * Filesystem in Userspace. FUSE allows a filesystem to be implemented in a
6 * userspace process by implementing the filesystem primitives--- read(),
7 * write(), readdir(), etc.
11 * Copyright (C) 2012, 2013 Eric Biggers
13 * This file is part of wimlib, a library for working with WIM files.
15 * wimlib is free software; you can redistribute it and/or modify it under the
16 * terms of the GNU General Public License as published by the Free
17 * Software Foundation; either version 3 of the License, or (at your option)
20 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
21 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
22 * A PARTICULAR PURPOSE. See the GNU General Public License for more
25 * You should have received a copy of the GNU General Public License
26 * along with wimlib; if not, see http://www.gnu.org/licenses/.
29 #include "wimlib_internal.h"
33 #include "buffer_io.h"
34 #include "lookup_table.h"
36 #include "timestamp.h"
48 #include <sys/types.h>
53 #define FUSE_USE_VERSION 26
57 #include <attr/xattr.h>
60 #define MSG_VERSION_TOO_HIGH -1
61 #define MSG_BREAK_LOOP -2
63 /* File descriptor to a file open on the WIM filesystem. */
65 struct wim_inode *f_inode;
66 struct wim_lookup_table_entry *f_lte;
72 struct wimfs_context {
73 /* The WIMStruct for the mounted WIM. */
76 /* Name of the staging directory for a read-write mount. Whenever a new file is
77 * created, it is done so in the staging directory. Furthermore, whenever a
78 * file in the WIM is modified, it is extracted to the staging directory. If
79 * changes are commited when the WIM is unmounted, the file resources are merged
80 * in from the staging directory when writing the new WIM. */
81 char *staging_dir_name;
82 size_t staging_dir_name_len;
84 /* Flags passed to wimlib_mount(). */
87 /* Default flags to use when looking up a WIM dentry (depends on whether
88 * the Windows interface to alternate data streams is being used or
90 int default_lookup_flags;
92 /* Next inode number to be assigned. Note: I didn't bother with a
93 * bitmap of free inode numbers since this isn't even a "real"
94 * filesystem anyway. */
97 /* List of lookup table entries for files in the staging directory */
98 struct list_head staging_list;
100 /* List of inodes in the mounted image */
101 struct hlist_head *image_inode_list;
103 /* Name and message queue descriptors for message queues between the
104 * filesystem daemon process and the unmount process. These are used
105 * when the filesystem is unmounted and the process running
106 * wimlib_unmount_image() (i.e. the `imagex unmount' command) needs to
107 * communicate with the filesystem daemon running fuse_main() (i.e. the
108 * daemon created by the `imagex mount' or `imagex mountrw' commands */
109 char *unmount_to_daemon_mq_name;
110 char *daemon_to_unmount_mq_name;
111 mqd_t unmount_to_daemon_mq;
112 mqd_t daemon_to_unmount_mq;
121 static void init_wimfs_context(struct wimfs_context *ctx)
123 memset(ctx, 0, sizeof(*ctx));
124 ctx->unmount_to_daemon_mq = (mqd_t)-1;
125 ctx->daemon_to_unmount_mq = (mqd_t)-1;
126 INIT_LIST_HEAD(&ctx->staging_list);
129 #define WIMFS_CTX(fuse_ctx) ((struct wimfs_context*)(fuse_ctx)->private_data)
131 static inline struct wimfs_context *wimfs_get_context()
133 return WIMFS_CTX(fuse_get_context());
136 static inline WIMStruct *wimfs_get_WIMStruct()
138 return wimfs_get_context()->wim;
141 static inline bool wimfs_ctx_readonly(const struct wimfs_context *ctx)
143 return (ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) == 0;
146 static inline int get_lookup_flags(const struct wimfs_context *ctx)
148 return ctx->default_lookup_flags;
151 /* Returns nonzero if write permission is requested on the file open flags */
152 static inline int flags_writable(int open_flags)
154 return open_flags & (O_RDWR | O_WRONLY);
158 * Allocate a file descriptor for a stream.
160 * @inode: inode containing the stream we're opening
161 * @stream_id: ID of the stream we're opening
162 * @lte: Lookup table entry for the stream (may be NULL)
163 * @fd_ret: Return the allocated file descriptor if successful.
164 * @readonly: True if this is a read-only mount.
166 * Return 0 iff successful or error code if unsuccessful.
168 static int alloc_wimfs_fd(struct wim_inode *inode,
170 struct wim_lookup_table_entry *lte,
171 struct wimfs_fd **fd_ret,
174 static const u16 fds_per_alloc = 8;
175 static const u16 max_fds = 0xffff;
178 pthread_mutex_lock(&inode->i_mutex);
180 DEBUG("Allocating fd for stream ID %u from inode %#"PRIx64" "
181 "(open = %u, allocated = %u)",
182 stream_id, inode->i_ino, inode->i_num_opened_fds,
183 inode->i_num_allocated_fds);
185 if (inode->i_num_opened_fds == inode->i_num_allocated_fds) {
186 struct wimfs_fd **fds;
189 if (inode->i_num_allocated_fds == max_fds) {
193 num_new_fds = min(fds_per_alloc,
194 max_fds - inode->i_num_allocated_fds);
196 fds = REALLOC(inode->i_fds,
197 (inode->i_num_allocated_fds + num_new_fds) *
198 sizeof(inode->i_fds[0]));
203 memset(&fds[inode->i_num_allocated_fds], 0,
204 num_new_fds * sizeof(fds[0]));
206 inode->i_num_allocated_fds += num_new_fds;
208 for (u16 i = 0; ; i++) {
209 if (!inode->i_fds[i]) {
210 struct wimfs_fd *fd = CALLOC(1, sizeof(*fd));
219 fd->stream_id = stream_id;
221 inode->i_fds[i] = fd;
222 inode->i_num_opened_fds++;
223 if (lte && !readonly)
224 lte->num_opened_fds++;
225 DEBUG("Allocated fd (idx = %u)", fd->idx);
231 pthread_mutex_unlock(&inode->i_mutex);
235 static void inode_put_fd(struct wim_inode *inode, struct wimfs_fd *fd)
237 wimlib_assert(inode != NULL);
239 pthread_mutex_lock(&inode->i_mutex);
241 wimlib_assert(fd->f_inode == inode);
242 wimlib_assert(inode->i_num_opened_fds != 0);
243 wimlib_assert(fd->idx < inode->i_num_allocated_fds);
244 wimlib_assert(inode->i_fds[fd->idx] == fd);
246 inode->i_fds[fd->idx] = NULL;
248 if (--inode->i_num_opened_fds == 0 && inode->i_nlink == 0) {
249 pthread_mutex_unlock(&inode->i_mutex);
252 pthread_mutex_unlock(&inode->i_mutex);
256 static int lte_put_fd(struct wim_lookup_table_entry *lte, struct wimfs_fd *fd)
258 wimlib_assert(fd->f_lte == lte);
260 if (!lte) /* Empty stream with no lookup table entry */
263 /* Close staging file descriptor if needed. */
265 if (lte->resource_location == RESOURCE_IN_STAGING_FILE
266 && fd->staging_fd != -1)
268 if (close(fd->staging_fd) != 0) {
269 ERROR_WITH_ERRNO("Failed to close staging file");
273 lte_decrement_num_opened_fds(lte);
277 /* Close a file descriptor. */
278 static int close_wimfs_fd(struct wimfs_fd *fd)
281 DEBUG("Closing fd (ino = %#"PRIx64", opened = %u, allocated = %u)",
282 fd->f_inode->i_ino, fd->f_inode->i_num_opened_fds,
283 fd->f_inode->i_num_allocated_fds);
284 ret = lte_put_fd(fd->f_lte, fd);
288 inode_put_fd(fd->f_inode, fd);
293 * Add a new dentry with a new inode to a WIM image.
295 * Returns 0 on success, or negative error number on failure.
297 static int create_dentry(struct fuse_context *fuse_ctx,const char *path,
298 mode_t mode, int attributes,
299 struct wim_dentry **dentry_ret)
301 struct wim_dentry *parent;
302 struct wim_dentry *new;
303 const char *basename;
304 struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
306 parent = get_parent_dentry(wimfs_ctx->wim, path);
310 if (!dentry_is_directory(parent))
313 basename = path_basename(path);
314 if (get_dentry_child_with_name(parent, basename))
317 new = new_dentry_with_inode(basename);
321 new->d_inode->i_resolved = 1;
322 new->d_inode->i_ino = wimfs_ctx->next_ino++;
323 new->d_inode->i_attributes = attributes;
325 if (wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA) {
326 if (inode_set_unix_data(new->d_inode,
329 mode & ~fuse_ctx->umask,
330 wimfs_ctx->wim->lookup_table,
331 UNIX_DATA_ALL | UNIX_DATA_CREATE))
337 dentry_add_child(parent, new);
338 hlist_add_head(&new->d_inode->i_hlist, wimfs_ctx->image_inode_list);
344 /* Remove a dentry from a mounted WIM image; i.e. remove an alias for the
345 * corresponding inode.
347 * If there are no remaining references to the inode either through dentries or
348 * open file descriptors, the inode is freed. Otherwise, the inode is not
349 * removed, but the dentry is unlinked and freed.
351 * Either way, all lookup table entries referenced by the inode have their
352 * reference count decremented. If a lookup table entry has no open file
353 * descriptors and no references remaining, it is freed, and the corresponding
354 * staging file is unlinked.
356 static void remove_dentry(struct wim_dentry *dentry,
357 struct wim_lookup_table *lookup_table)
359 struct wim_inode *inode = dentry->d_inode;
360 struct wim_lookup_table_entry *lte;
363 for (i = 0; i <= inode->i_num_ads; i++) {
364 lte = inode_stream_lte_resolved(inode, i);
366 lte_decrement_refcnt(lte, lookup_table);
368 unlink_dentry(dentry);
372 static mode_t inode_default_unix_mode(const struct wim_inode *inode)
374 if (inode_is_symlink(inode))
375 return S_IFLNK | 0777;
376 else if (inode_is_directory(inode))
377 return S_IFDIR | 0777;
379 return S_IFREG | 0777;
382 /* Transfers file attributes from a struct wim_inode to a `stat' buffer.
384 * The lookup table entry tells us which stream in the inode we are statting.
385 * For a named data stream, everything returned is the same as the unnamed data
386 * stream except possibly the size and block count. */
387 static int inode_to_stbuf(const struct wim_inode *inode,
388 const struct wim_lookup_table_entry *lte,
391 const struct wimfs_context *ctx = wimfs_get_context();
393 memset(stbuf, 0, sizeof(struct stat));
394 stbuf->st_mode = inode_default_unix_mode(inode);
395 stbuf->st_uid = ctx->default_uid;
396 stbuf->st_gid = ctx->default_gid;
397 if (ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA) {
398 struct wimlib_unix_data unix_data;
399 if (inode_get_unix_data(inode, &unix_data, NULL) == 0) {
400 stbuf->st_uid = unix_data.uid;
401 stbuf->st_gid = unix_data.gid;
402 stbuf->st_mode = unix_data.mode;
405 stbuf->st_ino = (ino_t)inode->i_ino;
406 stbuf->st_nlink = inode->i_nlink;
408 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
409 struct stat native_stat;
410 if (stat(lte->staging_file_name, &native_stat) != 0) {
411 DEBUG("Failed to stat `%s': %m",
412 lte->staging_file_name);
415 stbuf->st_size = native_stat.st_size;
417 stbuf->st_size = wim_resource_size(lte);
423 stbuf->st_atime = wim_timestamp_to_unix(inode->i_last_access_time);
424 stbuf->st_mtime = wim_timestamp_to_unix(inode->i_last_write_time);
425 stbuf->st_ctime = wim_timestamp_to_unix(inode->i_creation_time);
426 stbuf->st_blocks = (stbuf->st_size + 511) / 512;
430 /* Creates a new staging file and returns its file descriptor opened for
433 * @name_ret: A location into which the a pointer to the newly allocated name of
434 * the staging file is stored.
436 * @ctx: Context for the WIM filesystem; this provides the name of the
439 * On success, returns the file descriptor for the staging file, opened for
440 * writing. On failure, returns -1 and sets errno.
442 static int create_staging_file(char **name_ret, struct wimfs_context *ctx)
450 static const size_t STAGING_FILE_NAME_LEN = 20;
452 name_len = ctx->staging_dir_name_len + 1 + STAGING_FILE_NAME_LEN;
453 name = MALLOC(name_len + 1);
461 memcpy(name, ctx->staging_dir_name, ctx->staging_dir_name_len);
462 name[ctx->staging_dir_name_len] = '/';
463 randomize_char_array_with_alnum(name + ctx->staging_dir_name_len + 1,
464 STAGING_FILE_NAME_LEN);
465 name[name_len] = '\0';
468 /* Just in case, verify that the randomly generated name doesn't name an
469 * existing file, and try again if so */
470 } while (stat(name, &stbuf) == 0);
472 if (errno != ENOENT) /* other error?! */
475 /* doesn't exist--- ok */
477 DEBUG("Creating staging file `%s'", name);
479 fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0600);
491 * Extract a WIM resource to the staging directory.
493 * @inode: Inode that contains the stream we are extracting
495 * @stream_id: Identifier for the stream (it stays constant even if the indices
496 * of the stream entries are changed)
498 * @lte: Pointer to pointer to the lookup table entry for the stream we need to
499 * extract, or NULL if there was no lookup table entry present for the stream
501 * @size: Number of bytes of the stream we want to extract (this supports the
502 * wimfs_truncate() function). It may be more than the actual stream length, in
503 * which case the extra space is filled with zeroes.
505 * @ctx: Context for the WIM filesystem.
507 * Returns 0 on success or a negative error code on failure.
509 static int extract_resource_to_staging_dir(struct wim_inode *inode,
511 struct wim_lookup_table_entry **lte,
513 struct wimfs_context *ctx)
515 char *staging_file_name;
518 struct wim_lookup_table_entry *old_lte, *new_lte;
521 DEBUG("Extracting resource to staging dir: inode %"PRIu64", "
522 "stream id %"PRIu32, inode->i_ino, stream_id);
526 wimlib_assert(old_lte == NULL ||
527 old_lte->resource_location != RESOURCE_IN_STAGING_FILE);
529 /* Create the staging file */
530 fd = create_staging_file(&staging_file_name, ctx);
534 /* Extract the stream to the staging file (possibly truncated) */
536 extract_size = min(wim_resource_size(old_lte), size);
537 ret = extract_wim_resource_to_fd(old_lte, fd, extract_size);
543 /* In the case of truncate() to more than the file length, extend the
544 * file with zeroes by calling ftruncate() on the underlying staging
546 if (ret == 0 && size > extract_size)
547 ret = ftruncate(fd, size);
549 /* Close the staging file descriptor and check for errors. If there's
550 * an error, unlink the staging file. */
551 if (ret != 0 || close(fd) != 0) {
557 goto out_delete_staging_file;
560 /* Now deal with the lookup table entries. We may be able to re-use the
561 * existing entry, but we may have to create a new one instead. */
563 if (old_lte && inode->i_nlink == old_lte->refcnt) {
564 /* The reference count of the existing lookup table entry is the
565 * same as the link count of the inode that contains the stream
566 * we're opening. Therefore, ALL the references to the lookup
567 * table entry correspond to the stream we're trying to extract,
568 * so the lookup table entry can be re-used. */
569 DEBUG("Re-using lookup table entry");
570 lookup_table_unlink(ctx->wim->lookup_table, old_lte);
574 /* There's an existing lookup table entry, but its
575 * reference count is greater than the link count for
576 * the inode containing a stream we're opening.
577 * Therefore, we need to split the lookup table entry.
579 wimlib_assert(old_lte->refcnt > inode->i_nlink);
580 DEBUG("Splitting lookup table entry "
581 "(inode->i_nlink = %u, old_lte->refcnt = %u)",
582 inode->i_nlink, old_lte->refcnt);
585 new_lte = new_lookup_table_entry();
588 goto out_delete_staging_file;
591 /* There may already be open file descriptors to this stream if
592 * it's previously been opened read-only, but just now we're
593 * opening it read-write. Identify those file descriptors and
594 * change their lookup table entry pointers to point to the new
595 * lookup table entry, and open staging file descriptors for
598 * At the same time, we need to count the number of these opened
599 * file descriptors to the new lookup table entry. If there's
600 * an old lookup table entry, this number needs to be subtracted
601 * from the fd's opened to the old entry. */
602 for (u16 i = 0, j = 0; j < inode->i_num_opened_fds; i++) {
603 struct wimfs_fd *fd = inode->i_fds[i];
605 if (fd->stream_id == stream_id) {
606 wimlib_assert(fd->f_lte == old_lte);
607 wimlib_assert(fd->staging_fd == -1);
609 new_lte->num_opened_fds++;
610 fd->staging_fd = open(staging_file_name, O_RDONLY);
611 if (fd->staging_fd == -1) {
613 goto out_revert_fd_changes;
619 DEBUG("%hu fd's were already opened to the file we extracted",
620 new_lte->num_opened_fds);
622 old_lte->num_opened_fds -= new_lte->num_opened_fds;
623 old_lte->refcnt -= inode->i_nlink;
627 new_lte->refcnt = inode->i_nlink;
628 new_lte->resource_location = RESOURCE_IN_STAGING_FILE;
629 new_lte->staging_file_name = staging_file_name;
630 new_lte->lte_inode = inode;
631 random_hash(new_lte->hash);
634 inode->i_lte = new_lte;
636 for (u16 i = 0; i < inode->i_num_ads; i++)
637 if (inode->i_ads_entries[i].stream_id == stream_id)
638 inode->i_ads_entries[i].lte = new_lte;
640 lookup_table_insert(ctx->wim->lookup_table, new_lte);
641 list_add(&new_lte->staging_list, &ctx->staging_list);
644 out_revert_fd_changes:
645 for (u16 i = 0, j = 0; j < new_lte->num_opened_fds; i++) {
646 struct wimfs_fd *fd = inode->i_fds[i];
647 if (fd && fd->stream_id == stream_id && fd->f_lte == new_lte) {
649 if (fd->staging_fd != -1) {
650 close(fd->staging_fd);
656 free_lookup_table_entry(new_lte);
657 out_delete_staging_file:
658 unlink(staging_file_name);
659 FREE(staging_file_name);
664 * Creates a randomly named staging directory and saves its name in the
665 * filesystem context structure.
667 static int make_staging_dir(struct wimfs_context *ctx, const char *user_prefix)
669 static const size_t random_suffix_len = 10;
670 static const char *common_suffix = ".staging";
671 static const size_t common_suffix_len = 8;
673 char *staging_dir_name = NULL;
674 size_t staging_dir_name_len;
676 const char *wim_basename;
677 char *real_user_prefix = NULL;
681 real_user_prefix = realpath(user_prefix, NULL);
682 if (!real_user_prefix) {
683 ERROR_WITH_ERRNO("Could not resolve `%s'",
685 ret = WIMLIB_ERR_NOTDIR;
688 wim_basename = path_basename(ctx->wim->filename);
689 prefix_len = strlen(real_user_prefix) + 1 + strlen(wim_basename);
691 prefix_len = strlen(ctx->wim->filename);
694 staging_dir_name_len = prefix_len + common_suffix_len + random_suffix_len;
696 staging_dir_name = MALLOC(staging_dir_name_len + 1);
697 if (!staging_dir_name) {
698 ret = WIMLIB_ERR_NOMEM;
702 if (real_user_prefix)
703 sprintf(staging_dir_name, "%s/%s", real_user_prefix, wim_basename);
705 strcpy(staging_dir_name, ctx->wim->filename);
707 strcat(staging_dir_name, common_suffix);
709 randomize_char_array_with_alnum(staging_dir_name + prefix_len + common_suffix_len,
712 staging_dir_name[staging_dir_name_len] = '\0';
714 if (mkdir(staging_dir_name, 0700) != 0) {
715 ERROR_WITH_ERRNO("Failed to create temporary directory `%s'",
717 ret = WIMLIB_ERR_MKDIR;
722 FREE(real_user_prefix);
724 ctx->staging_dir_name = staging_dir_name;
725 ctx->staging_dir_name_len = staging_dir_name_len;
727 FREE(staging_dir_name);
732 static int remove_file_or_directory(const char *fpath, const struct stat *sb,
733 int typeflag, struct FTW *ftwbuf)
735 if (remove(fpath) == 0)
738 ERROR_WITH_ERRNO("Cannot remove `%s'", fpath);
739 return WIMLIB_ERR_DELETE_STAGING_DIR;
744 * Deletes the staging directory and all the files contained in it.
746 static int delete_staging_dir(struct wimfs_context *ctx)
749 ret = nftw(ctx->staging_dir_name, remove_file_or_directory,
751 FREE(ctx->staging_dir_name);
752 ctx->staging_dir_name = NULL;
756 static void inode_update_lte_ptr(struct wim_inode *inode,
757 struct wim_lookup_table_entry *old_lte,
758 struct wim_lookup_table_entry *new_lte)
760 if (inode->i_lte == old_lte) {
761 inode->i_lte = new_lte;
763 for (unsigned i = 0; i < inode->i_num_ads; i++) {
764 if (inode->i_ads_entries[i].lte == old_lte) {
765 inode->i_ads_entries[i].lte = new_lte;
772 static int update_lte_of_staging_file(struct wim_lookup_table_entry *lte,
773 struct wim_lookup_table *table)
775 struct wim_lookup_table_entry *duplicate_lte;
777 u8 hash[SHA1_HASH_SIZE];
780 ret = sha1sum(lte->staging_file_name, hash);
783 lookup_table_unlink(table, lte);
784 duplicate_lte = __lookup_resource(table, hash);
786 /* Merge duplicate lookup table entries */
787 duplicate_lte->refcnt += lte->refcnt;
788 inode_update_lte_ptr(lte->lte_inode, lte, duplicate_lte);
789 free_lookup_table_entry(lte);
791 if (stat(lte->staging_file_name, &stbuf) != 0) {
792 ERROR_WITH_ERRNO("Failed to stat `%s'", lte->staging_file_name);
793 return WIMLIB_ERR_STAT;
795 if (stbuf.st_size == 0) {
796 /* Zero-length stream. No lookup table entry needed. */
797 inode_update_lte_ptr(lte->lte_inode, lte, NULL);
798 free_lookup_table_entry(lte);
800 BUILD_BUG_ON(<e->file_on_disk != <e->staging_file_name);
801 lte->resource_entry.original_size = stbuf.st_size;
802 lte->resource_entry.size = stbuf.st_size;
803 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
804 lte->file_on_disk_fp = NULL;
805 copy_hash(lte->hash, hash);
806 lookup_table_insert(table, lte);
812 static int inode_close_fds(struct wim_inode *inode)
814 u16 num_opened_fds = inode->i_num_opened_fds;
815 for (u16 i = 0, j = 0; j < num_opened_fds; i++) {
816 struct wimfs_fd *fd = inode->i_fds[i];
818 wimlib_assert(fd->f_inode == inode);
819 int ret = close_wimfs_fd(fd);
828 /* Overwrites the WIM file, with changes saved. */
829 static int rebuild_wim(struct wimfs_context *ctx, int write_flags,
830 wimlib_progress_func_t progress_func)
833 struct wim_lookup_table_entry *lte, *tmp;
834 WIMStruct *w = ctx->wim;
836 DEBUG("Closing all staging file descriptors.");
837 list_for_each_entry_safe(lte, tmp, &ctx->staging_list, staging_list) {
838 ret = inode_close_fds(lte->lte_inode);
843 DEBUG("Calculating SHA1 checksums for all new staging files.");
844 list_for_each_entry_safe(lte, tmp, &ctx->staging_list, staging_list) {
845 ret = update_lte_of_staging_file(lte, w->lookup_table);
850 xml_update_image_info(w, w->current_image);
851 ret = wimlib_overwrite(w, write_flags, 0, progress_func);
853 ERROR("Failed to commit changes to mounted WIM image");
859 /* Simple function that returns the concatenation of 2 strings. */
860 static char *strcat_dup(const char *s1, const char *s2, size_t max_len)
862 size_t len = strlen(s1) + strlen(s2);
865 char *p = MALLOC(len + 1);
868 snprintf(p, len + 1, "%s%s", s1, s2);
872 static int set_message_queue_names(struct wimfs_context *ctx,
873 const char *mount_dir)
875 static const char *u2d_prefix = "/wimlib-unmount-to-daemon-mq";
876 static const char *d2u_prefix = "/wimlib-daemon-to-unmount-mq";
881 dir_path = realpath(mount_dir, NULL);
883 ERROR_WITH_ERRNO("Failed to resolve path \"%s\"", mount_dir);
885 return WIMLIB_ERR_NOMEM;
887 return WIMLIB_ERR_NOTDIR;
890 for (p = dir_path; *p; p++)
894 ctx->unmount_to_daemon_mq_name = strcat_dup(u2d_prefix, dir_path,
896 if (!ctx->unmount_to_daemon_mq_name) {
897 ret = WIMLIB_ERR_NOMEM;
898 goto out_free_dir_path;
900 ctx->daemon_to_unmount_mq_name = strcat_dup(d2u_prefix, dir_path,
902 if (!ctx->daemon_to_unmount_mq_name) {
903 ret = WIMLIB_ERR_NOMEM;
904 goto out_free_unmount_to_daemon_mq_name;
908 goto out_free_dir_path;
909 out_free_unmount_to_daemon_mq_name:
910 FREE(ctx->unmount_to_daemon_mq_name);
911 ctx->unmount_to_daemon_mq_name = NULL;
917 static void free_message_queue_names(struct wimfs_context *ctx)
919 FREE(ctx->unmount_to_daemon_mq_name);
920 FREE(ctx->daemon_to_unmount_mq_name);
921 ctx->unmount_to_daemon_mq_name = NULL;
922 ctx->daemon_to_unmount_mq_name = NULL;
926 * Opens two POSIX message queue: one for sending messages from the unmount
927 * process to the daemon process, and one to go the other way. The names of the
928 * message queues, which must be system-wide unique, are be based on the mount
931 * @daemon specifies whether the calling process is the filesystem daemon or the
934 static int open_message_queues(struct wimfs_context *ctx, bool daemon)
936 int unmount_to_daemon_mq_flags = O_WRONLY | O_CREAT;
937 int daemon_to_unmount_mq_flags = O_RDONLY | O_CREAT;
943 swap(unmount_to_daemon_mq_flags, daemon_to_unmount_mq_flags);
949 orig_umask = umask(0000);
950 DEBUG("Opening message queue \"%s\"", ctx->unmount_to_daemon_mq_name);
951 ctx->unmount_to_daemon_mq = mq_open(ctx->unmount_to_daemon_mq_name,
952 unmount_to_daemon_mq_flags, mode, NULL);
954 if (ctx->unmount_to_daemon_mq == (mqd_t)-1) {
955 ERROR_WITH_ERRNO("mq_open()");
956 ret = WIMLIB_ERR_MQUEUE;
960 DEBUG("Opening message queue \"%s\"", ctx->daemon_to_unmount_mq_name);
961 ctx->daemon_to_unmount_mq = mq_open(ctx->daemon_to_unmount_mq_name,
962 daemon_to_unmount_mq_flags, mode, NULL);
964 if (ctx->daemon_to_unmount_mq == (mqd_t)-1) {
965 ERROR_WITH_ERRNO("mq_open()");
966 mq_close(ctx->unmount_to_daemon_mq);
967 mq_unlink(ctx->unmount_to_daemon_mq_name);
968 ctx->unmount_to_daemon_mq = (mqd_t)-1;
969 ret = WIMLIB_ERR_MQUEUE;
978 /* Try to determine the maximum message size of a message queue. The return
979 * value is the maximum message size, or a guess of 8192 bytes if it cannot be
981 static long mq_get_msgsize(mqd_t mq)
983 static const char *msgsize_max_file = "/proc/sys/fs/mqueue/msgsize_max";
988 if (mq_getattr(mq, &attr) == 0) {
989 msgsize = attr.mq_msgsize;
991 ERROR_WITH_ERRNO("mq_getattr()");
992 ERROR("Attempting to read %s", msgsize_max_file);
993 fp = fopen(msgsize_max_file, "rb");
995 if (fscanf(fp, "%ld", &msgsize) != 1) {
996 ERROR("Assuming message size of 8192");
1001 ERROR_WITH_ERRNO("Failed to open the file `%s'",
1003 ERROR("Assuming message size of 8192");
1010 static int get_mailbox(mqd_t mq, long needed_msgsize, long *msgsize_ret,
1016 msgsize = mq_get_msgsize(mq);
1018 if (msgsize < needed_msgsize) {
1019 ERROR("Message queue max size must be at least %ld!",
1021 return WIMLIB_ERR_MQUEUE;
1024 mailbox = MALLOC(msgsize);
1026 ERROR("Failed to allocate %ld bytes for mailbox", msgsize);
1027 return WIMLIB_ERR_NOMEM;
1029 *msgsize_ret = msgsize;
1030 *mailbox_ret = mailbox;
1034 static void unlink_message_queues(struct wimfs_context *ctx)
1036 mq_unlink(ctx->unmount_to_daemon_mq_name);
1037 mq_unlink(ctx->daemon_to_unmount_mq_name);
1040 /* Closes the message queues, which are allocated in static variables */
1041 static void close_message_queues(struct wimfs_context *ctx)
1043 DEBUG("Closing message queues");
1044 mq_close(ctx->unmount_to_daemon_mq);
1045 ctx->unmount_to_daemon_mq = (mqd_t)(-1);
1046 mq_close(ctx->daemon_to_unmount_mq);
1047 ctx->daemon_to_unmount_mq = (mqd_t)(-1);
1048 unlink_message_queues(ctx);
1052 struct unmount_msg_hdr {
1059 struct msg_unmount_request {
1060 struct unmount_msg_hdr hdr;
1062 u8 want_progress_messages;
1065 struct msg_daemon_info {
1066 struct unmount_msg_hdr hdr;
1071 struct msg_unmount_finished {
1072 struct unmount_msg_hdr hdr;
1076 struct msg_write_streams_progress {
1077 struct unmount_msg_hdr hdr;
1078 union wimlib_progress_info info;
1082 MSG_TYPE_UNMOUNT_REQUEST,
1083 MSG_TYPE_DAEMON_INFO,
1084 MSG_TYPE_WRITE_STREAMS_PROGRESS,
1085 MSG_TYPE_UNMOUNT_FINISHED,
1089 struct msg_handler_context_hdr {
1090 int timeout_seconds;
1093 struct unmount_msg_handler_context {
1094 struct msg_handler_context_hdr hdr;
1098 wimlib_progress_func_t progress_func;
1101 struct daemon_msg_handler_context {
1102 struct msg_handler_context_hdr hdr;
1103 struct wimfs_context *wimfs_ctx;
1106 static int send_unmount_request_msg(mqd_t mq, int unmount_flags,
1107 u8 want_progress_messages)
1109 DEBUG("Sending unmount request msg");
1110 struct msg_unmount_request msg = {
1112 .min_version = WIMLIB_MAKEVERSION(1, 2, 1),
1113 .cur_version = WIMLIB_VERSION_CODE,
1114 .msg_type = MSG_TYPE_UNMOUNT_REQUEST,
1115 .msg_size = sizeof(msg),
1117 .unmount_flags = unmount_flags,
1118 .want_progress_messages = want_progress_messages,
1121 if (mq_send(mq, (void*)&msg, sizeof(msg), 1)) {
1122 ERROR_WITH_ERRNO("Failed to communicate with filesystem daemon");
1123 return WIMLIB_ERR_MQUEUE;
1128 static int send_daemon_info_msg(mqd_t mq, pid_t pid, int mount_flags)
1130 DEBUG("Sending daemon info msg (pid = %d, mount_flags=%x)",
1133 struct msg_daemon_info msg = {
1135 .min_version = WIMLIB_MAKEVERSION(1, 2, 1),
1136 .cur_version = WIMLIB_VERSION_CODE,
1137 .msg_type = MSG_TYPE_DAEMON_INFO,
1138 .msg_size = sizeof(msg),
1141 .mount_flags = mount_flags,
1143 if (mq_send(mq, (void*)&msg, sizeof(msg), 1)) {
1144 ERROR_WITH_ERRNO("Failed to send daemon info to unmount process");
1145 return WIMLIB_ERR_MQUEUE;
1150 static void send_unmount_finished_msg(mqd_t mq, int status)
1152 DEBUG("Sending unmount finished msg");
1153 struct msg_unmount_finished msg = {
1155 .min_version = WIMLIB_MAKEVERSION(1, 2, 1),
1156 .cur_version = WIMLIB_VERSION_CODE,
1157 .msg_type = MSG_TYPE_UNMOUNT_FINISHED,
1158 .msg_size = sizeof(msg),
1162 if (mq_send(mq, (void*)&msg, sizeof(msg), 1))
1163 ERROR_WITH_ERRNO("Failed to send status to unmount process");
1166 static int unmount_progress_func(enum wimlib_progress_msg msg,
1167 const union wimlib_progress_info *info)
1169 if (msg == WIMLIB_PROGRESS_MSG_WRITE_STREAMS) {
1170 struct msg_write_streams_progress msg = {
1172 .min_version = WIMLIB_MAKEVERSION(1, 2, 1),
1173 .cur_version = WIMLIB_VERSION_CODE,
1174 .msg_type = MSG_TYPE_WRITE_STREAMS_PROGRESS,
1175 .msg_size = sizeof(msg),
1179 if (mq_send(wimfs_get_context()->daemon_to_unmount_mq,
1180 (void*)&msg, sizeof(msg), 1))
1182 ERROR_WITH_ERRNO("Failed to send progress information "
1183 "to unmount process");
1189 static int msg_unmount_request_handler(const void *_msg, void *_handler_ctx)
1191 const struct msg_unmount_request *msg = _msg;
1192 struct daemon_msg_handler_context *handler_ctx = _handler_ctx;
1193 struct wimfs_context *wimfs_ctx;
1197 wimlib_progress_func_t progress_func;
1199 DEBUG("Handling unmount request msg");
1201 wimfs_ctx = handler_ctx->wimfs_ctx;
1202 if (msg->hdr.msg_size < sizeof(*msg)) {
1203 status = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1207 unmount_flags = msg->unmount_flags;
1208 if (msg->want_progress_messages)
1209 progress_func = unmount_progress_func;
1211 progress_func = NULL;
1213 ret = send_daemon_info_msg(wimfs_ctx->daemon_to_unmount_mq, getpid(),
1214 wimfs_ctx->mount_flags);
1220 if (wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1221 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_COMMIT) {
1222 int write_flags = 0;
1223 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY)
1224 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1225 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_REBUILD)
1226 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1227 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_RECOMPRESS)
1228 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
1229 status = rebuild_wim(wimfs_ctx, write_flags,
1233 DEBUG("Read-only mount");
1238 if (wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1239 ret = delete_staging_dir(wimfs_ctx);
1241 ERROR("Failed to delete the staging directory");
1246 wimfs_ctx->status = status;
1247 wimfs_ctx->have_status = true;
1248 return MSG_BREAK_LOOP;
1251 static int msg_daemon_info_handler(const void *_msg, void *_handler_ctx)
1253 const struct msg_daemon_info *msg = _msg;
1254 struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1256 DEBUG("Handling daemon info msg");
1257 if (msg->hdr.msg_size < sizeof(*msg))
1258 return WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1259 handler_ctx->daemon_pid = msg->daemon_pid;
1260 handler_ctx->mount_flags = msg->mount_flags;
1261 handler_ctx->hdr.timeout_seconds = 1;
1262 DEBUG("pid of daemon is %d; mount flags were %#x",
1263 handler_ctx->daemon_pid,
1264 handler_ctx->mount_flags);
1268 static int msg_write_streams_progress_handler(const void *_msg,
1271 const struct msg_write_streams_progress *msg = _msg;
1272 struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1274 if (msg->hdr.msg_size < sizeof(*msg))
1275 return WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1276 if (handler_ctx->progress_func) {
1277 handler_ctx->progress_func(WIMLIB_PROGRESS_MSG_WRITE_STREAMS,
1283 static int msg_unmount_finished_handler(const void *_msg, void *_handler_ctx)
1285 const struct msg_unmount_finished *msg = _msg;
1286 struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1288 DEBUG("Handling unmount finished message");
1289 if (msg->hdr.msg_size < sizeof(*msg))
1290 return WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1291 handler_ctx->status = msg->status;
1292 DEBUG("status is %d", handler_ctx->status);
1293 return MSG_BREAK_LOOP;
1296 static int unmount_timed_out_cb(void *_handler_ctx)
1298 struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1300 if (handler_ctx->daemon_pid == 0) {
1303 kill(handler_ctx->daemon_pid, 0);
1304 if (errno == ESRCH) {
1307 DEBUG("Filesystem daemon is still alive... "
1308 "Waiting another %d seconds\n",
1309 handler_ctx->hdr.timeout_seconds);
1314 ERROR("The filesystem daemon has crashed! Changes to the "
1315 "WIM may not have been commited.");
1316 return WIMLIB_ERR_FILESYSTEM_DAEMON_CRASHED;
1319 static int daemon_timed_out_cb(void *_handler_ctx)
1321 ERROR("Timed out waiting for unmount request! "
1322 "Changes to the mounted WIM will not be committed.");
1323 return WIMLIB_ERR_TIMEOUT;
1326 typedef int (*msg_handler_t)(const void *_msg, void *_handler_ctx);
1328 struct msg_handler_callbacks {
1329 int (*timed_out)(void * _handler_ctx);
1330 msg_handler_t msg_handlers[MSG_TYPE_MAX];
1333 static const struct msg_handler_callbacks unmount_msg_handler_callbacks = {
1334 .timed_out = unmount_timed_out_cb,
1336 [MSG_TYPE_DAEMON_INFO] = msg_daemon_info_handler,
1337 [MSG_TYPE_WRITE_STREAMS_PROGRESS] = msg_write_streams_progress_handler,
1338 [MSG_TYPE_UNMOUNT_FINISHED] = msg_unmount_finished_handler,
1342 static const struct msg_handler_callbacks daemon_msg_handler_callbacks = {
1343 .timed_out = daemon_timed_out_cb,
1345 [MSG_TYPE_UNMOUNT_REQUEST] = msg_unmount_request_handler,
1349 static int receive_message(mqd_t mq,
1350 struct msg_handler_context_hdr *handler_ctx,
1351 const msg_handler_t msg_handlers[],
1352 long mailbox_size, void *mailbox)
1355 struct timespec timeout;
1356 ssize_t bytes_received;
1357 struct unmount_msg_hdr *hdr;
1360 gettimeofday(&now, NULL);
1361 timeout.tv_sec = now.tv_sec + handler_ctx->timeout_seconds;
1362 timeout.tv_nsec = now.tv_usec * 1000;
1364 bytes_received = mq_timedreceive(mq, mailbox,
1365 mailbox_size, NULL, &timeout);
1367 if (bytes_received == -1) {
1368 if (errno == ETIMEDOUT) {
1369 ret = WIMLIB_ERR_TIMEOUT;
1371 ERROR_WITH_ERRNO("mq_timedreceive()");
1372 ret = WIMLIB_ERR_MQUEUE;
1374 } else if (bytes_received < sizeof(*hdr) ||
1375 bytes_received != hdr->msg_size) {
1376 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1377 } else if (WIMLIB_VERSION_CODE < hdr->min_version) {
1378 /*ERROR("Cannot understand the received message. "*/
1379 /*"Please upgrade wimlib to at least v%d.%d.%d",*/
1380 /*WIMLIB_GET_MAJOR_VERSION(hdr->min_version),*/
1381 /*WIMLIB_GET_MINOR_VERSION(hdr->min_version),*/
1382 /*WIMLIB_GET_PATCH_VERSION(hdr->min_version));*/
1383 ret = MSG_VERSION_TOO_HIGH;
1384 } else if (hdr->msg_type >= MSG_TYPE_MAX) {
1385 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1386 } else if (msg_handlers[hdr->msg_type] == NULL) {
1387 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1389 ret = msg_handlers[hdr->msg_type](mailbox, handler_ctx);
1394 static int message_loop(mqd_t mq,
1395 const struct msg_handler_callbacks *callbacks,
1396 struct msg_handler_context_hdr *handler_ctx)
1398 static const size_t MAX_MSG_SIZE = 512;
1403 DEBUG("Entering message loop");
1405 ret = get_mailbox(mq, MAX_MSG_SIZE, &msgsize, &mailbox);
1409 ret = receive_message(mq, handler_ctx,
1410 callbacks->msg_handlers,
1412 if (ret == 0 || ret == MSG_VERSION_TOO_HIGH) {
1414 } else if (ret == MSG_BREAK_LOOP) {
1417 } else if (ret == WIMLIB_ERR_TIMEOUT) {
1418 if (callbacks->timed_out)
1419 ret = callbacks->timed_out(handler_ctx);
1425 ERROR_WITH_ERRNO("Error communicating with "
1426 "filesystem daemon");
1431 DEBUG("Exiting message loop");
1435 /* Execute `fusermount -u', which is installed setuid root, to unmount the WIM.
1437 * FUSE does not yet implement synchronous unmounts. This means that fusermount
1438 * -u will return before the filesystem daemon returns from wimfs_destroy().
1439 * This is partly what we want, because we need to send a message from this
1440 * process to the filesystem daemon telling whether --commit was specified or
1441 * not. However, after that, the unmount process must wait for the filesystem
1442 * daemon to finish writing the WIM file.
1444 static int execute_fusermount(const char *dir)
1452 ERROR_WITH_ERRNO("Failed to fork()");
1453 return WIMLIB_ERR_FORK;
1457 execlp("fusermount", "fusermount", "-u", dir, NULL);
1458 ERROR_WITH_ERRNO("Failed to execute `fusermount'");
1459 exit(WIMLIB_ERR_FUSERMOUNT);
1463 ret = waitpid(pid, &status, 0);
1465 ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
1467 return WIMLIB_ERR_FUSERMOUNT;
1470 if (!WIFEXITED(status)) {
1471 ERROR("'fusermount' did not terminate normally!");
1472 return WIMLIB_ERR_FUSERMOUNT;
1475 status = WEXITSTATUS(status);
1480 if (status != WIMLIB_ERR_FUSERMOUNT)
1481 return WIMLIB_ERR_FUSERMOUNT;
1483 /* Try again, but with the `umount' program. This is required on other
1484 * FUSE implementations such as FreeBSD's that do not have a
1485 * `fusermount' program. */
1486 ERROR("Falling back to 'umount'. Note: you may need to be "
1487 "root for this to work");
1490 ERROR_WITH_ERRNO("Failed to fork()");
1491 return WIMLIB_ERR_FORK;
1495 execlp("umount", "umount", dir, NULL);
1496 ERROR_WITH_ERRNO("Failed to execute `umount'");
1497 exit(WIMLIB_ERR_FUSERMOUNT);
1501 ret = waitpid(pid, &status, 0);
1503 ERROR_WITH_ERRNO("Failed to wait for `umount' process to "
1505 return WIMLIB_ERR_FUSERMOUNT;
1508 ERROR("`umount' did not successfully complete");
1509 return WIMLIB_ERR_FUSERMOUNT;
1515 static int wimfs_access(const char *path, int mask)
1521 static int wimfs_chmod(const char *path, mode_t mask)
1523 struct wim_dentry *dentry;
1524 struct wimfs_context *ctx = wimfs_get_context();
1527 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA))
1530 ret = lookup_resource(ctx->wim, path, LOOKUP_FLAG_DIRECTORY_OK,
1531 &dentry, NULL, NULL);
1535 ret = inode_set_unix_data(dentry->d_inode, ctx->default_uid,
1536 ctx->default_gid, mask,
1537 ctx->wim->lookup_table, UNIX_DATA_MODE);
1538 return ret ? -ENOMEM : 0;
1541 static int wimfs_chown(const char *path, uid_t uid, gid_t gid)
1543 struct wim_dentry *dentry;
1544 struct wimfs_context *ctx = wimfs_get_context();
1547 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA))
1550 ret = lookup_resource(ctx->wim, path, LOOKUP_FLAG_DIRECTORY_OK,
1551 &dentry, NULL, NULL);
1555 ret = inode_set_unix_data(dentry->d_inode, uid, gid,
1556 inode_default_unix_mode(dentry->d_inode),
1557 ctx->wim->lookup_table,
1558 UNIX_DATA_UID | UNIX_DATA_GID);
1559 return ret ? -ENOMEM : 0;
1562 /* Called when the filesystem is unmounted. */
1563 static void wimfs_destroy(void *p)
1565 struct wimfs_context *wimfs_ctx = wimfs_get_context();
1566 if (open_message_queues(wimfs_ctx, true) == 0) {
1567 struct daemon_msg_handler_context handler_ctx = {
1569 .timeout_seconds = 5,
1571 .wimfs_ctx = wimfs_ctx,
1573 message_loop(wimfs_ctx->unmount_to_daemon_mq,
1574 &daemon_msg_handler_callbacks,
1580 static int wimfs_fallocate(const char *path, int mode,
1581 off_t offset, off_t len, struct fuse_file_info *fi)
1583 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1584 wimlib_assert(fd->staging_fd != -1);
1585 return fallocate(fd->staging_fd, mode, offset, len);
1590 static int wimfs_fgetattr(const char *path, struct stat *stbuf,
1591 struct fuse_file_info *fi)
1593 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1594 return inode_to_stbuf(fd->f_inode, fd->f_lte, stbuf);
1597 static int wimfs_ftruncate(const char *path, off_t size,
1598 struct fuse_file_info *fi)
1600 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1601 int ret = ftruncate(fd->staging_fd, size);
1604 if (fd->f_lte && size < fd->f_lte->resource_entry.original_size)
1605 fd->f_lte->resource_entry.original_size = size;
1610 * Fills in a `struct stat' that corresponds to a file or directory in the WIM.
1612 static int wimfs_getattr(const char *path, struct stat *stbuf)
1614 struct wim_dentry *dentry;
1615 struct wim_lookup_table_entry *lte;
1617 struct wimfs_context *ctx = wimfs_get_context();
1619 ret = lookup_resource(ctx->wim, path,
1620 get_lookup_flags(ctx) | LOOKUP_FLAG_DIRECTORY_OK,
1621 &dentry, <e, NULL);
1624 return inode_to_stbuf(dentry->d_inode, lte, stbuf);
1628 /* Read an alternate data stream through the XATTR interface, or get its size */
1629 static int wimfs_getxattr(const char *path, const char *name, char *value,
1633 struct wim_inode *inode;
1634 struct wim_ads_entry *ads_entry;
1636 struct wim_lookup_table_entry *lte;
1637 struct wimfs_context *ctx = wimfs_get_context();
1639 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1642 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1646 inode = wim_pathname_to_inode(ctx->wim, path);
1650 ads_entry = inode_get_ads_entry(inode, name, NULL);
1654 lte = ads_entry->lte;
1655 res_size = wim_resource_size(lte);
1660 if (res_size > size)
1663 ret = read_full_wim_resource(lte, (u8*)value,
1664 WIMLIB_RESOURCE_FLAG_MULTITHREADED);
1672 /* Create a hard link */
1673 static int wimfs_link(const char *to, const char *from)
1675 struct wim_dentry *from_dentry, *from_dentry_parent;
1676 const char *link_name;
1677 struct wim_inode *inode;
1678 struct wim_lookup_table_entry *lte;
1679 WIMStruct *w = wimfs_get_WIMStruct();
1682 inode = wim_pathname_to_inode(w, to);
1686 if (inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
1687 FILE_ATTRIBUTE_REPARSE_POINT))
1690 from_dentry_parent = get_parent_dentry(w, from);
1691 if (!from_dentry_parent)
1693 if (!dentry_is_directory(from_dentry_parent))
1696 link_name = path_basename(from);
1697 if (get_dentry_child_with_name(from_dentry_parent, link_name))
1699 from_dentry = new_dentry(link_name);
1703 inode_add_dentry(from_dentry, inode);
1704 from_dentry->d_inode = inode;
1707 for (i = 0; i <= inode->i_num_ads; i++) {
1708 lte = inode_stream_lte_resolved(inode, i);
1712 dentry_add_child(from_dentry_parent, from_dentry);
1717 static int wimfs_listxattr(const char *path, char *list, size_t size)
1720 struct wim_inode *inode;
1721 struct wimfs_context *ctx = wimfs_get_context();
1725 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1728 /* List alternate data streams, or get the list size */
1730 inode = wim_pathname_to_inode(ctx->wim, path);
1736 for (i = 0; i < inode->i_num_ads; i++)
1737 needed_size += inode->i_ads_entries[i].stream_name_utf8_len + 6;
1741 for (i = 0; i < inode->i_num_ads; i++) {
1742 needed_size = inode->i_ads_entries[i].stream_name_utf8_len + 6;
1743 if (needed_size > size)
1745 p += sprintf(p, "user.%s",
1746 inode->i_ads_entries[i].stream_name_utf8) + 1;
1747 size -= needed_size;
1755 /* Create a directory in the WIM image. */
1756 static int wimfs_mkdir(const char *path, mode_t mode)
1758 return create_dentry(fuse_get_context(), path, mode | S_IFDIR,
1759 FILE_ATTRIBUTE_DIRECTORY, NULL);
1762 /* Create a regular file or alternate data stream in the WIM image. */
1763 static int wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
1765 const char *stream_name;
1766 struct fuse_context *fuse_ctx = fuse_get_context();
1767 struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
1772 if ((wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
1773 && (stream_name = path_stream_name(path))) {
1774 /* Make an alternate data stream */
1775 struct wim_ads_entry *new_entry;
1776 struct wim_inode *inode;
1778 char *p = (char*)stream_name - 1;
1779 wimlib_assert(*p == ':');
1782 inode = wim_pathname_to_inode(wimfs_ctx->wim, path);
1785 if (inode->i_attributes &
1786 (FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_DIRECTORY))
1788 if (inode_get_ads_entry(inode, stream_name, NULL))
1790 new_entry = inode_add_ads(inode, stream_name);
1795 /* Make a normal file (not an alternate data stream) */
1796 return create_dentry(fuse_ctx, path, mode | S_IFREG,
1797 FILE_ATTRIBUTE_NORMAL, NULL);
1803 static int wimfs_open(const char *path, struct fuse_file_info *fi)
1805 struct wim_dentry *dentry;
1806 struct wim_lookup_table_entry *lte;
1808 struct wimfs_fd *fd;
1809 struct wim_inode *inode;
1812 struct wimfs_context *ctx = wimfs_get_context();
1814 ret = lookup_resource(ctx->wim, path, get_lookup_flags(ctx),
1815 &dentry, <e, &stream_idx);
1819 inode = dentry->d_inode;
1821 if (stream_idx == 0)
1824 stream_id = inode->i_ads_entries[stream_idx - 1].stream_id;
1826 /* The file resource may be in the staging directory (read-write mounts
1827 * only) or in the WIM. If it's in the staging directory, we need to
1828 * open a native file descriptor for the corresponding file. Otherwise,
1829 * we can read the file resource directly from the WIM file if we are
1830 * opening it read-only, but we need to extract the resource to the
1831 * staging directory if we are opening it writable. */
1833 if (flags_writable(fi->flags) &&
1834 (!lte || lte->resource_location != RESOURCE_IN_STAGING_FILE)) {
1835 u64 size = (lte) ? wim_resource_size(lte) : 0;
1836 ret = extract_resource_to_staging_dir(inode, stream_id,
1842 ret = alloc_wimfs_fd(inode, stream_id, lte, &fd,
1843 wimfs_ctx_readonly(ctx));
1847 if (lte && lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1848 fd->staging_fd = open(lte->staging_file_name, fi->flags);
1849 if (fd->staging_fd == -1) {
1850 int errno_save = errno;
1855 fi->fh = (uintptr_t)fd;
1859 /* Opens a directory. */
1860 static int wimfs_opendir(const char *path, struct fuse_file_info *fi)
1862 struct wim_inode *inode;
1864 struct wimfs_fd *fd = NULL;
1865 struct wimfs_context *ctx = wimfs_get_context();
1866 WIMStruct *w = ctx->wim;
1868 inode = wim_pathname_to_inode(w, path);
1871 if (!inode_is_directory(inode))
1873 ret = alloc_wimfs_fd(inode, 0, NULL, &fd, wimfs_ctx_readonly(ctx));
1874 fi->fh = (uintptr_t)fd;
1880 * Read data from a file in the WIM or in the staging directory.
1882 static int wimfs_read(const char *path, char *buf, size_t size,
1883 off_t offset, struct fuse_file_info *fi)
1885 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1891 if (!fd->f_lte) /* Empty stream with no lookup table entry */
1894 if (fd->f_lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1895 /* Read from staging file */
1897 wimlib_assert(fd->f_lte->staging_file_name);
1898 wimlib_assert(fd->staging_fd != -1);
1900 DEBUG("Seek to offset %"PRIu64, offset);
1902 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1904 ret = read(fd->staging_fd, buf, size);
1910 u64 res_size = wim_resource_size(fd->f_lte);
1911 if (offset > res_size)
1913 size = min(size, res_size - offset);
1914 if (read_wim_resource(fd->f_lte, (u8*)buf,
1916 WIMLIB_RESOURCE_FLAG_MULTITHREADED) != 0)
1922 struct fill_params {
1924 fuse_fill_dir_t filler;
1927 static int dentry_fuse_fill(struct wim_dentry *dentry, void *arg)
1929 struct fill_params *fill_params = arg;
1930 return fill_params->filler(fill_params->buf, dentry->file_name_utf8,
1934 /* Fills in the entries of the directory specified by @path using the
1935 * FUSE-provided function @filler. */
1936 static int wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
1937 off_t offset, struct fuse_file_info *fi)
1939 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1940 struct wim_inode *inode;
1945 inode = fd->f_inode;
1947 struct fill_params fill_params = {
1952 filler(buf, ".", NULL, 0);
1953 filler(buf, "..", NULL, 0);
1955 return for_dentry_in_rbtree(inode->i_children.rb_node,
1956 dentry_fuse_fill, &fill_params);
1960 static int wimfs_readlink(const char *path, char *buf, size_t buf_len)
1962 struct wimfs_context *ctx = wimfs_get_context();
1963 struct wim_inode *inode = wim_pathname_to_inode(ctx->wim, path);
1967 if (!inode_is_symlink(inode))
1970 ret = inode_readlink(inode, buf, buf_len, ctx->wim,
1971 WIMLIB_RESOURCE_FLAG_MULTITHREADED);
1978 static int wimfs_release(const char *path, struct fuse_file_info *fi)
1980 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1981 return close_wimfs_fd(fd);
1984 /* Close a directory */
1985 static int wimfs_releasedir(const char *path, struct fuse_file_info *fi)
1987 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1988 return close_wimfs_fd(fd);
1992 /* Remove an alternate data stream through the XATTR interface */
1993 static int wimfs_removexattr(const char *path, const char *name)
1995 struct wim_inode *inode;
1996 struct wim_ads_entry *ads_entry;
1998 struct wimfs_context *ctx = wimfs_get_context();
2000 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
2003 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
2007 inode = wim_pathname_to_inode(ctx->wim, path);
2011 ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
2014 inode_remove_ads(inode, ads_idx, ctx->wim->lookup_table);
2019 /* Renames a file or directory. See rename (3) */
2020 static int wimfs_rename(const char *from, const char *to)
2022 struct wim_dentry *src;
2023 struct wim_dentry *dst;
2024 struct wim_dentry *parent_of_dst;
2025 WIMStruct *w = wimfs_get_WIMStruct();
2028 /* This rename() implementation currently only supports actual files
2029 * (not alternate data streams) */
2031 src = get_dentry(w, from);
2035 dst = get_dentry(w, to);
2038 /* Destination file exists */
2040 if (src == dst) /* Same file */
2043 if (!dentry_is_directory(src)) {
2044 /* Cannot rename non-directory to directory. */
2045 if (dentry_is_directory(dst))
2048 /* Cannot rename directory to a non-directory or a non-empty
2050 if (!dentry_is_directory(dst))
2052 if (inode_has_children(dst->d_inode))
2055 parent_of_dst = dst->parent;
2057 /* Destination does not exist */
2058 parent_of_dst = get_parent_dentry(w, to);
2062 if (!dentry_is_directory(parent_of_dst))
2066 ret = set_dentry_name(src, path_basename(to));
2070 remove_dentry(dst, w->lookup_table);
2072 dentry_add_child(parent_of_dst, src);
2076 /* Remove a directory */
2077 static int wimfs_rmdir(const char *path)
2079 struct wim_dentry *dentry;
2080 WIMStruct *w = wimfs_get_WIMStruct();
2082 dentry = get_dentry(w, path);
2086 if (!dentry_is_directory(dentry))
2089 if (dentry_has_children(dentry))
2092 remove_dentry(dentry, w->lookup_table);
2097 /* Write an alternate data stream through the XATTR interface */
2098 static int wimfs_setxattr(const char *path, const char *name,
2099 const char *value, size_t size, int flags)
2101 struct wim_ads_entry *existing_ads_entry;
2102 struct wim_inode *inode;
2104 struct wimfs_context *ctx = wimfs_get_context();
2107 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
2110 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
2114 inode = wim_pathname_to_inode(ctx->wim, path);
2118 existing_ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
2119 if (existing_ads_entry) {
2120 if (flags & XATTR_CREATE)
2123 if (flags & XATTR_REPLACE)
2127 ret = inode_add_ads_with_data(inode, name, (const u8*)value,
2128 size, ctx->wim->lookup_table);
2130 if (existing_ads_entry)
2131 inode_remove_ads(inode, ads_idx, ctx->wim->lookup_table);
2139 static int wimfs_symlink(const char *to, const char *from)
2141 struct fuse_context *fuse_ctx = fuse_get_context();
2142 struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
2143 struct wim_dentry *dentry;
2146 ret = create_dentry(fuse_ctx, from, S_IFLNK | 0777,
2147 FILE_ATTRIBUTE_REPARSE_POINT, &dentry);
2149 dentry->d_inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
2150 if (inode_set_symlink(dentry->d_inode, to,
2151 wimfs_ctx->wim->lookup_table, NULL))
2153 remove_dentry(dentry, wimfs_ctx->wim->lookup_table);
2161 /* Reduce the size of a file */
2162 static int wimfs_truncate(const char *path, off_t size)
2164 struct wim_dentry *dentry;
2165 struct wim_lookup_table_entry *lte;
2169 struct wim_inode *inode;
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 (lte == NULL && size == 0)
2181 inode = dentry->d_inode;
2182 if (stream_idx == 0)
2185 stream_id = inode->i_ads_entries[stream_idx - 1].stream_id;
2187 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
2188 ret = truncate(lte->staging_file_name, size);
2192 /* File in WIM. Extract it to the staging directory, but only
2193 * the first @size bytes of it. */
2194 ret = extract_resource_to_staging_dir(inode, stream_id,
2200 /* Unlink a non-directory or alternate data stream */
2201 static int wimfs_unlink(const char *path)
2203 struct wim_dentry *dentry;
2204 struct wim_lookup_table_entry *lte;
2207 struct wimfs_context *ctx = wimfs_get_context();
2209 ret = lookup_resource(ctx->wim, path, get_lookup_flags(ctx),
2210 &dentry, <e, &stream_idx);
2215 if (stream_idx == 0)
2216 remove_dentry(dentry, ctx->wim->lookup_table);
2218 inode_remove_ads(dentry->d_inode, stream_idx - 1,
2219 ctx->wim->lookup_table);
2223 #ifdef HAVE_UTIMENSAT
2225 * Change the timestamp on a file dentry.
2227 * Note that alternate data streams do not have their own timestamps.
2229 static int wimfs_utimens(const char *path, const struct timespec tv[2])
2231 struct wim_dentry *dentry;
2232 struct wim_inode *inode;
2233 WIMStruct *w = wimfs_get_WIMStruct();
2235 dentry = get_dentry(w, path);
2238 inode = dentry->d_inode;
2240 if (tv[0].tv_nsec != UTIME_OMIT) {
2241 if (tv[0].tv_nsec == UTIME_NOW)
2242 inode->i_last_access_time = get_wim_timestamp();
2244 inode->i_last_access_time = timespec_to_wim_timestamp(&tv[0]);
2246 if (tv[1].tv_nsec != UTIME_OMIT) {
2247 if (tv[1].tv_nsec == UTIME_NOW)
2248 inode->i_last_write_time = get_wim_timestamp();
2250 inode->i_last_write_time = timespec_to_wim_timestamp(&tv[1]);
2255 static int wimfs_utime(const char *path, struct utimbuf *times)
2257 struct wim_dentry *dentry;
2258 struct wim_inode *inode;
2259 WIMStruct *w = wimfs_get_WIMStruct();
2261 dentry = get_dentry(w, path);
2264 inode = dentry->d_inode;
2266 inode->i_last_write_time = unix_timestamp_to_wim(times->modtime);
2267 inode->i_last_access_time = unix_timestamp_to_wim(times->actime);
2272 /* Writes to a file in the WIM filesystem.
2273 * It may be an alternate data stream, but here we don't even notice because we
2274 * just get a lookup table entry. */
2275 static int wimfs_write(const char *path, const char *buf, size_t size,
2276 off_t offset, struct fuse_file_info *fi)
2278 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
2285 wimlib_assert(fd->f_lte);
2286 wimlib_assert(fd->f_lte->staging_file_name);
2287 wimlib_assert(fd->staging_fd != -1);
2288 wimlib_assert(fd->f_inode);
2290 /* Seek to the requested position */
2291 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
2294 /* Write the data. */
2295 ret = write(fd->staging_fd, buf, size);
2299 now = get_wim_timestamp();
2300 fd->f_inode->i_last_write_time = now;
2301 fd->f_inode->i_last_access_time = now;
2305 static struct fuse_operations wimfs_operations = {
2307 .access = wimfs_access,
2309 .chmod = wimfs_chmod,
2310 .chown = wimfs_chown,
2311 .destroy = wimfs_destroy,
2313 .fallocate = wimfs_fallocate,
2315 .fgetattr = wimfs_fgetattr,
2316 .ftruncate = wimfs_ftruncate,
2317 .getattr = wimfs_getattr,
2319 .getxattr = wimfs_getxattr,
2323 .listxattr = wimfs_listxattr,
2325 .mkdir = wimfs_mkdir,
2326 .mknod = wimfs_mknod,
2328 .opendir = wimfs_opendir,
2330 .readdir = wimfs_readdir,
2331 .readlink = wimfs_readlink,
2332 .release = wimfs_release,
2333 .releasedir = wimfs_releasedir,
2335 .removexattr = wimfs_removexattr,
2337 .rename = wimfs_rename,
2338 .rmdir = wimfs_rmdir,
2340 .setxattr = wimfs_setxattr,
2342 .symlink = wimfs_symlink,
2343 .truncate = wimfs_truncate,
2344 .unlink = wimfs_unlink,
2345 #ifdef HAVE_UTIMENSAT
2346 .utimens = wimfs_utimens,
2348 .utime = wimfs_utime,
2350 .write = wimfs_write,
2352 /* wimfs keeps file descriptor structures (struct wimfs_fd), so there is
2353 * no need to have the file path provided on operations such as read()
2354 * where only the file descriptor is needed. */
2355 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 8)
2356 .flag_nullpath_ok = 1,
2358 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 9)
2360 .flag_utime_omit_ok = 1,
2365 /* Mounts an image from a WIM file. */
2366 WIMLIBAPI int wimlib_mount_image(WIMStruct *wim, int image, const char *dir,
2367 int mount_flags, WIMStruct **additional_swms,
2368 unsigned num_additional_swms,
2369 const char *staging_dir)
2375 struct wim_lookup_table *joined_tab, *wim_tab_save;
2376 struct wim_image_metadata *imd;
2377 struct wimfs_context ctx;
2378 struct hlist_node *cur_node;
2379 struct wim_inode *inode;
2381 DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
2382 wim, image, dir, mount_flags);
2385 return WIMLIB_ERR_INVALID_PARAM;
2387 ret = verify_swm_set(wim, additional_swms, num_additional_swms);
2391 if ((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) && (wim->hdr.total_parts != 1)) {
2392 ERROR("Cannot mount a split WIM read-write");
2393 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
2396 if (num_additional_swms) {
2397 ret = new_joined_lookup_table(wim, additional_swms,
2398 num_additional_swms,
2402 wim_tab_save = wim->lookup_table;
2403 wim->lookup_table = joined_tab;
2406 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2407 ret = wim_run_full_verifications(wim);
2412 ret = select_wim_image(wim, image);
2416 DEBUG("Selected image %d", image);
2418 imd = wim_get_current_image_metadata(wim);
2420 if (imd->root_dentry->refcnt != 1) {
2421 ERROR("Cannot mount image that was just exported with "
2422 "wimlib_export_image()");
2423 ret = WIMLIB_ERR_INVALID_PARAM;
2427 if (imd->inode_list.first) /* May be unneeded? */
2428 imd->inode_list.first->pprev = &imd->inode_list.first;
2430 if (imd->modified) {
2431 ERROR("Cannot mount image that was added "
2432 "with wimlib_add_image()");
2433 ret = WIMLIB_ERR_INVALID_PARAM;
2437 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2438 ret = lock_wim(wim, wim->fp);
2443 /* Use default stream interface if one was not specified */
2444 if (!(mount_flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
2445 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
2446 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
2447 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
2450 DEBUG("Initializing struct wimfs_context");
2451 init_wimfs_context(&ctx);
2453 ctx.mount_flags = mount_flags;
2454 ctx.image_inode_list = &imd->inode_list;
2455 ctx.default_uid = getuid();
2456 ctx.default_gid = getgid();
2457 if (mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
2458 ctx.default_lookup_flags = LOOKUP_FLAG_ADS_OK;
2460 DEBUG("Unlinking message queues in case they already exist");
2461 ret = set_message_queue_names(&ctx, dir);
2464 unlink_message_queues(&ctx);
2466 DEBUG("Preparing arguments to fuse_main()");
2468 dir_copy = STRDUP(dir);
2470 goto out_free_message_queue_names;
2473 argv[argc++] = "imagex";
2474 argv[argc++] = dir_copy;
2476 /* disable multi-threaded operation for read-write mounts */
2477 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2478 argv[argc++] = "-s";
2480 if (mount_flags & WIMLIB_MOUNT_FLAG_DEBUG)
2481 argv[argc++] = "-d";
2484 * We provide the use_ino option to the FUSE mount because we are going
2485 * to assign inode numbers ourselves. */
2486 char optstring[256] =
2490 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 8)
2493 ",default_permissions"
2495 argv[argc++] = "-o";
2496 argv[argc++] = optstring;
2497 if ((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
2498 /* Read-write mount. Make the staging directory */
2499 ret = make_staging_dir(&ctx, staging_dir);
2501 goto out_free_dir_copy;
2503 /* Read-only mount */
2504 strcat(optstring, ",ro");
2506 if (mount_flags & WIMLIB_MOUNT_FLAG_ALLOW_OTHER)
2507 strcat(optstring, ",allow_other");
2513 DEBUG("FUSE command line (argc = %d): ", argc);
2514 for (i = 0; i < argc; i++) {
2515 fputs(argv[i], stdout);
2523 /* Mark dentry tree as modified if read-write mount. */
2524 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2526 imd->has_been_mounted_rw = 1;
2529 /* Resolve the lookup table entries for every inode in the image, and
2530 * assign inode numbers */
2531 DEBUG("Resolving lookup table entries and assigning inode numbers");
2533 hlist_for_each_entry(inode, cur_node, &imd->inode_list, i_hlist) {
2534 inode_resolve_ltes(inode, wim->lookup_table);
2535 inode->i_ino = ctx.next_ino++;
2537 DEBUG("(next_ino = %"PRIu64")", ctx.next_ino);
2539 DEBUG("Calling fuse_main()");
2541 ret = fuse_main(argc, argv, &wimfs_operations, &ctx);
2543 DEBUG("Returned from fuse_main() (ret = %d)", ret);
2546 ret = WIMLIB_ERR_FUSE;
2548 if (ctx.have_status)
2551 ret = WIMLIB_ERR_TIMEOUT;
2553 if (ctx.daemon_to_unmount_mq != (mqd_t)(-1)) {
2554 send_unmount_finished_msg(ctx.daemon_to_unmount_mq, ret);
2555 close_message_queues(&ctx);
2558 /* Try to delete the staging directory if a deletion wasn't yet
2559 * attempted due to an earlier error */
2560 if (ctx.staging_dir_name)
2561 delete_staging_dir(&ctx);
2565 wim->wim_locked = 0;
2566 out_free_message_queue_names:
2567 free_message_queue_names(&ctx);
2569 if (num_additional_swms) {
2570 free_lookup_table(wim->lookup_table);
2571 wim->lookup_table = wim_tab_save;
2577 * Unmounts the WIM file that was previously mounted on @dir by using
2578 * wimlib_mount_image().
2580 WIMLIBAPI int wimlib_unmount_image(const char *dir, int unmount_flags,
2581 wimlib_progress_func_t progress_func)
2584 struct wimfs_context wimfs_ctx;
2586 init_wimfs_context(&wimfs_ctx);
2588 ret = set_message_queue_names(&wimfs_ctx, dir);
2592 ret = open_message_queues(&wimfs_ctx, false);
2594 goto out_free_message_queue_names;
2596 ret = send_unmount_request_msg(wimfs_ctx.unmount_to_daemon_mq,
2598 progress_func != NULL);
2600 goto out_close_message_queues;
2602 ret = execute_fusermount(dir);
2604 goto out_close_message_queues;
2606 struct unmount_msg_handler_context handler_ctx = {
2608 .timeout_seconds = 5,
2611 .progress_func = progress_func,
2614 ret = message_loop(wimfs_ctx.daemon_to_unmount_mq,
2615 &unmount_msg_handler_callbacks,
2618 ret = handler_ctx.status;
2619 out_close_message_queues:
2620 close_message_queues(&wimfs_ctx);
2621 out_free_message_queue_names:
2622 free_message_queue_names(&wimfs_ctx);
2627 #else /* WITH_FUSE */
2630 static inline int mount_unsupported_error()
2632 #if defined(__WIN32__)
2633 ERROR("Sorry-- Mounting WIM images is not supported on Windows!");
2635 ERROR("wimlib was compiled with --without-fuse, which disables support "
2636 "for mounting WIMs.");
2638 return WIMLIB_ERR_UNSUPPORTED;
2641 WIMLIBAPI int wimlib_unmount_image(const char *dir, int unmount_flags,
2642 wimlib_progress_func_t progress_func)
2644 return mount_unsupported_error();
2647 WIMLIBAPI int wimlib_mount_image(WIMStruct *wim_p, int image, const char *dir,
2648 int mount_flags, WIMStruct **additional_swms,
2649 unsigned num_additional_swms,
2650 const char *staging_dir)
2652 return mount_unsupported_error();
2655 #endif /* WITH_FUSE */