4 * This file implements mounting of WIM files using FUSE, which stands for
5 * Filesystem in Userspace. FUSE allows a filesystem to be implemented in a
6 * userspace process by implementing the filesystem primitives--- read(),
7 * write(), readdir(), etc.
11 * Copyright (C) 2012, 2013 Eric Biggers
13 * This file is part of wimlib, a library for working with WIM files.
15 * wimlib is free software; you can redistribute it and/or modify it under the
16 * terms of the GNU General Public License as published by the Free
17 * Software Foundation; either version 3 of the License, or (at your option)
20 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
21 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
22 * A PARTICULAR PURPOSE. See the GNU General Public License for more
25 * You should have received a copy of the GNU General Public License
26 * along with wimlib; if not, see http://www.gnu.org/licenses/.
29 #include "wimlib_internal.h"
34 #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;
117 static void init_wimfs_context(struct wimfs_context *ctx)
119 memset(ctx, 0, sizeof(*ctx));
120 ctx->unmount_to_daemon_mq = (mqd_t)-1;
121 ctx->daemon_to_unmount_mq = (mqd_t)-1;
122 INIT_LIST_HEAD(&ctx->staging_list);
125 #define WIMFS_CTX(fuse_ctx) ((struct wimfs_context*)(fuse_ctx)->private_data)
127 static inline struct wimfs_context *wimfs_get_context()
129 return WIMFS_CTX(fuse_get_context());
132 static inline WIMStruct *wimfs_get_WIMStruct()
134 return wimfs_get_context()->wim;
137 static inline bool wimfs_ctx_readonly(const struct wimfs_context *ctx)
139 return (ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) == 0;
142 static inline int get_lookup_flags(const struct wimfs_context *ctx)
144 return ctx->default_lookup_flags;
147 /* Returns nonzero if write permission is requested on the file open flags */
148 static inline int flags_writable(int open_flags)
150 return open_flags & (O_RDWR | O_WRONLY);
154 * Allocate a file descriptor for a stream.
156 * @inode: inode containing the stream we're opening
157 * @stream_id: ID of the stream we're opening
158 * @lte: Lookup table entry for the stream (may be NULL)
159 * @fd_ret: Return the allocated file descriptor if successful.
160 * @readonly: True if this is a read-only mount.
162 * Return 0 iff successful or error code if unsuccessful.
164 static int alloc_wimfs_fd(struct wim_inode *inode,
166 struct wim_lookup_table_entry *lte,
167 struct wimfs_fd **fd_ret,
170 static const u16 fds_per_alloc = 8;
171 static const u16 max_fds = 0xffff;
174 pthread_mutex_lock(&inode->i_mutex);
176 DEBUG("Allocating fd for stream ID %u from inode %#"PRIx64" "
177 "(open = %u, allocated = %u)",
178 stream_id, inode->i_ino, inode->i_num_opened_fds,
179 inode->i_num_allocated_fds);
181 if (inode->i_num_opened_fds == inode->i_num_allocated_fds) {
182 struct wimfs_fd **fds;
185 if (inode->i_num_allocated_fds == max_fds) {
189 num_new_fds = min(fds_per_alloc,
190 max_fds - inode->i_num_allocated_fds);
192 fds = REALLOC(inode->i_fds,
193 (inode->i_num_allocated_fds + num_new_fds) *
194 sizeof(inode->i_fds[0]));
199 memset(&fds[inode->i_num_allocated_fds], 0,
200 num_new_fds * sizeof(fds[0]));
202 inode->i_num_allocated_fds += num_new_fds;
204 for (u16 i = 0; ; i++) {
205 if (!inode->i_fds[i]) {
206 struct wimfs_fd *fd = CALLOC(1, sizeof(*fd));
215 fd->stream_id = stream_id;
217 inode->i_fds[i] = fd;
218 inode->i_num_opened_fds++;
219 if (lte && !readonly)
220 lte->num_opened_fds++;
221 DEBUG("Allocated fd (idx = %u)", fd->idx);
227 pthread_mutex_unlock(&inode->i_mutex);
231 static void inode_put_fd(struct wim_inode *inode, struct wimfs_fd *fd)
233 wimlib_assert(inode != NULL);
235 pthread_mutex_lock(&inode->i_mutex);
237 wimlib_assert(fd->f_inode == inode);
238 wimlib_assert(inode->i_num_opened_fds != 0);
239 wimlib_assert(fd->idx < inode->i_num_allocated_fds);
240 wimlib_assert(inode->i_fds[fd->idx] == fd);
242 inode->i_fds[fd->idx] = NULL;
244 if (--inode->i_num_opened_fds == 0 && inode->i_nlink == 0) {
245 pthread_mutex_unlock(&inode->i_mutex);
248 pthread_mutex_unlock(&inode->i_mutex);
252 static int lte_put_fd(struct wim_lookup_table_entry *lte, struct wimfs_fd *fd)
254 wimlib_assert(fd->f_lte == lte);
256 if (!lte) /* Empty stream with no lookup table entry */
259 /* Close staging file descriptor if needed. */
261 if (lte->resource_location == RESOURCE_IN_STAGING_FILE
262 && fd->staging_fd != -1)
264 if (close(fd->staging_fd) != 0) {
265 ERROR_WITH_ERRNO("Failed to close staging file");
269 lte_decrement_num_opened_fds(lte);
273 /* Close a file descriptor. */
274 static int close_wimfs_fd(struct wimfs_fd *fd)
277 DEBUG("Closing fd (ino = %#"PRIx64", opened = %u, allocated = %u)",
278 fd->f_inode->i_ino, fd->f_inode->i_num_opened_fds,
279 fd->f_inode->i_num_allocated_fds);
280 ret = lte_put_fd(fd->f_lte, fd);
284 inode_put_fd(fd->f_inode, fd);
289 * Add a new dentry with a new inode to a WIM image.
291 * Returns 0 on success, or negative error number on failure.
293 static int create_dentry(struct fuse_context *fuse_ctx,const char *path,
294 mode_t mode, int attributes,
295 struct wim_dentry **dentry_ret)
297 struct wim_dentry *parent;
298 struct wim_dentry *new;
299 const char *basename;
300 struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
302 parent = get_parent_dentry(wimfs_ctx->wim, path);
306 if (!dentry_is_directory(parent))
309 basename = path_basename(path);
310 if (get_dentry_child_with_name(parent, basename))
313 new = new_dentry_with_inode(basename);
317 new->d_inode->i_resolved = 1;
318 new->d_inode->i_ino = wimfs_ctx->next_ino++;
319 new->d_inode->i_attributes = attributes;
321 if (wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA) {
322 if (inode_set_unix_data(new->d_inode,
325 mode & ~fuse_ctx->umask,
326 wimfs_ctx->wim->lookup_table,
327 UNIX_DATA_ALL | UNIX_DATA_CREATE))
333 dentry_add_child(parent, new);
334 hlist_add_head(&new->d_inode->i_hlist, wimfs_ctx->image_inode_list);
340 /* Remove a dentry from a mounted WIM image; i.e. remove an alias for the
341 * corresponding inode.
343 * If there are no remaining references to the inode either through dentries or
344 * open file descriptors, the inode is freed. Otherwise, the inode is not
345 * removed, but the dentry is unlinked and freed.
347 * Either way, all lookup table entries referenced by the inode have their
348 * reference count decremented. If a lookup table entry has no open file
349 * descriptors and no references remaining, it is freed, and the corresponding
350 * staging file is unlinked.
352 static void remove_dentry(struct wim_dentry *dentry,
353 struct wim_lookup_table *lookup_table)
355 struct wim_inode *inode = dentry->d_inode;
356 struct wim_lookup_table_entry *lte;
359 for (i = 0; i <= inode->i_num_ads; i++) {
360 lte = inode_stream_lte_resolved(inode, i);
362 lte_decrement_refcnt(lte, lookup_table);
364 unlink_dentry(dentry);
368 static mode_t inode_default_unix_mode(const struct wim_inode *inode)
370 if (inode_is_symlink(inode))
371 return S_IFLNK | 0777;
372 else if (inode_is_directory(inode))
373 return S_IFDIR | 0777;
375 return S_IFREG | 0777;
378 /* Transfers file attributes from a struct wim_inode to a `stat' buffer.
380 * The lookup table entry tells us which stream in the inode we are statting.
381 * For a named data stream, everything returned is the same as the unnamed data
382 * stream except possibly the size and block count. */
383 static int inode_to_stbuf(const struct wim_inode *inode,
384 const struct wim_lookup_table_entry *lte,
387 const struct wimfs_context *ctx = wimfs_get_context();
389 memset(stbuf, 0, sizeof(struct stat));
390 stbuf->st_mode = inode_default_unix_mode(inode);
391 stbuf->st_uid = ctx->default_uid;
392 stbuf->st_gid = ctx->default_gid;
393 if (ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA) {
394 struct wimlib_unix_data unix_data;
395 if (inode_get_unix_data(inode, &unix_data, NULL) == 0) {
396 stbuf->st_uid = unix_data.uid;
397 stbuf->st_gid = unix_data.gid;
398 stbuf->st_mode = unix_data.mode;
401 stbuf->st_ino = (ino_t)inode->i_ino;
402 stbuf->st_nlink = inode->i_nlink;
404 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
405 struct stat native_stat;
406 if (stat(lte->staging_file_name, &native_stat) != 0) {
407 DEBUG("Failed to stat `%s': %m",
408 lte->staging_file_name);
411 stbuf->st_size = native_stat.st_size;
413 stbuf->st_size = wim_resource_size(lte);
419 stbuf->st_atime = wim_timestamp_to_unix(inode->i_last_access_time);
420 stbuf->st_mtime = wim_timestamp_to_unix(inode->i_last_write_time);
421 stbuf->st_ctime = wim_timestamp_to_unix(inode->i_creation_time);
422 stbuf->st_blocks = (stbuf->st_size + 511) / 512;
426 /* Creates a new staging file and returns its file descriptor opened for
429 * @name_ret: A location into which the a pointer to the newly allocated name of
430 * the staging file is stored.
432 * @ctx: Context for the WIM filesystem; this provides the name of the
435 * On success, returns the file descriptor for the staging file, opened for
436 * writing. On failure, returns -1 and sets errno.
438 static int create_staging_file(char **name_ret, struct wimfs_context *ctx)
446 static const size_t STAGING_FILE_NAME_LEN = 20;
448 name_len = ctx->staging_dir_name_len + 1 + STAGING_FILE_NAME_LEN;
449 name = MALLOC(name_len + 1);
457 memcpy(name, ctx->staging_dir_name, ctx->staging_dir_name_len);
458 name[ctx->staging_dir_name_len] = '/';
459 randomize_char_array_with_alnum(name + ctx->staging_dir_name_len + 1,
460 STAGING_FILE_NAME_LEN);
461 name[name_len] = '\0';
464 /* Just in case, verify that the randomly generated name doesn't name an
465 * existing file, and try again if so */
466 } while (stat(name, &stbuf) == 0);
468 if (errno != ENOENT) /* other error?! */
471 /* doesn't exist--- ok */
473 DEBUG("Creating staging file `%s'", name);
475 fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0600);
487 * Extract a WIM resource to the staging directory.
489 * @inode: Inode that contains the stream we are extracting
491 * @stream_id: Identifier for the stream (it stays constant even if the indices
492 * of the stream entries are changed)
494 * @lte: Pointer to pointer to the lookup table entry for the stream we need to
495 * extract, or NULL if there was no lookup table entry present for the stream
497 * @size: Number of bytes of the stream we want to extract (this supports the
498 * wimfs_truncate() function). It may be more than the actual stream length, in
499 * which case the extra space is filled with zeroes.
501 * @ctx: Context for the WIM filesystem.
503 * Returns 0 on success or a negative error code on failure.
505 static int extract_resource_to_staging_dir(struct wim_inode *inode,
507 struct wim_lookup_table_entry **lte,
509 struct wimfs_context *ctx)
511 char *staging_file_name;
514 struct wim_lookup_table_entry *old_lte, *new_lte;
517 DEBUG("Extracting resource to staging dir: inode %"PRIu64", "
518 "stream id %"PRIu32, inode->i_ino, stream_id);
522 wimlib_assert(old_lte == NULL ||
523 old_lte->resource_location != RESOURCE_IN_STAGING_FILE);
525 /* Create the staging file */
526 fd = create_staging_file(&staging_file_name, ctx);
530 /* Extract the stream to the staging file (possibly truncated) */
532 extract_size = min(wim_resource_size(old_lte), size);
533 ret = extract_wim_resource_to_fd(old_lte, fd, extract_size);
539 /* In the case of truncate() to more than the file length, extend the
540 * file with zeroes by calling ftruncate() on the underlying staging
542 if (ret == 0 && size > extract_size)
543 ret = ftruncate(fd, size);
545 /* Close the staging file descriptor and check for errors. If there's
546 * an error, unlink the staging file. */
547 if (ret != 0 || close(fd) != 0) {
553 goto out_delete_staging_file;
556 /* Now deal with the lookup table entries. We may be able to re-use the
557 * existing entry, but we may have to create a new one instead. */
559 if (old_lte && inode->i_nlink == old_lte->refcnt) {
560 /* The reference count of the existing lookup table entry is the
561 * same as the link count of the inode that contains the stream
562 * we're opening. Therefore, ALL the references to the lookup
563 * table entry correspond to the stream we're trying to extract,
564 * so the lookup table entry can be re-used. */
565 DEBUG("Re-using lookup table entry");
566 lookup_table_unlink(ctx->wim->lookup_table, old_lte);
570 /* There's an existing lookup table entry, but its
571 * reference count is greater than the link count for
572 * the inode containing a stream we're opening.
573 * Therefore, we need to split the lookup table entry.
575 wimlib_assert(old_lte->refcnt > inode->i_nlink);
576 DEBUG("Splitting lookup table entry "
577 "(inode->i_nlink = %u, old_lte->refcnt = %u)",
578 inode->i_nlink, old_lte->refcnt);
581 new_lte = new_lookup_table_entry();
584 goto out_delete_staging_file;
587 /* There may already be open file descriptors to this stream if
588 * it's previously been opened read-only, but just now we're
589 * opening it read-write. Identify those file descriptors and
590 * change their lookup table entry pointers to point to the new
591 * lookup table entry, and open staging file descriptors for
594 * At the same time, we need to count the number of these opened
595 * file descriptors to the new lookup table entry. If there's
596 * an old lookup table entry, this number needs to be subtracted
597 * from the fd's opened to the old entry. */
598 for (u16 i = 0, j = 0; j < inode->i_num_opened_fds; i++) {
599 struct wimfs_fd *fd = inode->i_fds[i];
601 if (fd->stream_id == stream_id) {
602 wimlib_assert(fd->f_lte == old_lte);
603 wimlib_assert(fd->staging_fd == -1);
605 new_lte->num_opened_fds++;
606 fd->staging_fd = open(staging_file_name, O_RDONLY);
607 if (fd->staging_fd == -1) {
609 goto out_revert_fd_changes;
615 DEBUG("%hu fd's were already opened to the file we extracted",
616 new_lte->num_opened_fds);
618 old_lte->num_opened_fds -= new_lte->num_opened_fds;
619 old_lte->refcnt -= inode->i_nlink;
623 new_lte->refcnt = inode->i_nlink;
624 new_lte->resource_location = RESOURCE_IN_STAGING_FILE;
625 new_lte->staging_file_name = staging_file_name;
626 new_lte->lte_inode = inode;
627 random_hash(new_lte->hash);
630 inode->i_lte = new_lte;
632 for (u16 i = 0; i < inode->i_num_ads; i++)
633 if (inode->i_ads_entries[i].stream_id == stream_id)
634 inode->i_ads_entries[i].lte = new_lte;
636 lookup_table_insert(ctx->wim->lookup_table, new_lte);
637 list_add(&new_lte->staging_list, &ctx->staging_list);
640 out_revert_fd_changes:
641 for (u16 i = 0, j = 0; j < new_lte->num_opened_fds; i++) {
642 struct wimfs_fd *fd = inode->i_fds[i];
643 if (fd && fd->stream_id == stream_id && fd->f_lte == new_lte) {
645 if (fd->staging_fd != -1) {
646 close(fd->staging_fd);
652 free_lookup_table_entry(new_lte);
653 out_delete_staging_file:
654 unlink(staging_file_name);
655 FREE(staging_file_name);
660 * Creates a randomly named staging directory and saves its name in the
661 * filesystem context structure.
663 static int make_staging_dir(struct wimfs_context *ctx, const char *user_prefix)
665 static const size_t random_suffix_len = 10;
666 static const char *common_suffix = ".staging";
667 static const size_t common_suffix_len = 8;
669 char *staging_dir_name = NULL;
670 size_t staging_dir_name_len;
672 const char *wim_basename;
673 char *real_user_prefix = NULL;
677 real_user_prefix = realpath(user_prefix, NULL);
678 if (!real_user_prefix) {
679 ERROR_WITH_ERRNO("Could not resolve `%s'",
681 ret = WIMLIB_ERR_NOTDIR;
684 wim_basename = path_basename(ctx->wim->filename);
685 prefix_len = strlen(real_user_prefix) + 1 + strlen(wim_basename);
687 prefix_len = strlen(ctx->wim->filename);
690 staging_dir_name_len = prefix_len + common_suffix_len + random_suffix_len;
692 staging_dir_name = MALLOC(staging_dir_name_len + 1);
693 if (!staging_dir_name) {
694 ret = WIMLIB_ERR_NOMEM;
698 if (real_user_prefix)
699 sprintf(staging_dir_name, "%s/%s", real_user_prefix, wim_basename);
701 strcpy(staging_dir_name, ctx->wim->filename);
703 strcat(staging_dir_name, common_suffix);
705 randomize_char_array_with_alnum(staging_dir_name + prefix_len + common_suffix_len,
708 staging_dir_name[staging_dir_name_len] = '\0';
710 if (mkdir(staging_dir_name, 0700) != 0) {
711 ERROR_WITH_ERRNO("Failed to create temporary directory `%s'",
713 ret = WIMLIB_ERR_MKDIR;
718 FREE(real_user_prefix);
720 ctx->staging_dir_name = staging_dir_name;
721 ctx->staging_dir_name_len = staging_dir_name_len;
723 FREE(staging_dir_name);
728 static int remove_file_or_directory(const char *fpath, const struct stat *sb,
729 int typeflag, struct FTW *ftwbuf)
731 if (remove(fpath) == 0)
734 ERROR_WITH_ERRNO("Cannot remove `%s'", fpath);
735 return WIMLIB_ERR_DELETE_STAGING_DIR;
740 * Deletes the staging directory and all the files contained in it.
742 static int delete_staging_dir(struct wimfs_context *ctx)
745 ret = nftw(ctx->staging_dir_name, remove_file_or_directory,
747 FREE(ctx->staging_dir_name);
748 ctx->staging_dir_name = NULL;
752 static void inode_update_lte_ptr(struct wim_inode *inode,
753 struct wim_lookup_table_entry *old_lte,
754 struct wim_lookup_table_entry *new_lte)
756 if (inode->i_lte == old_lte) {
757 inode->i_lte = new_lte;
759 for (unsigned i = 0; i < inode->i_num_ads; i++) {
760 if (inode->i_ads_entries[i].lte == old_lte) {
761 inode->i_ads_entries[i].lte = new_lte;
768 static int update_lte_of_staging_file(struct wim_lookup_table_entry *lte,
769 struct wim_lookup_table *table)
771 struct wim_lookup_table_entry *duplicate_lte;
773 u8 hash[SHA1_HASH_SIZE];
776 ret = sha1sum(lte->staging_file_name, hash);
779 lookup_table_unlink(table, lte);
780 duplicate_lte = __lookup_resource(table, hash);
782 /* Merge duplicate lookup table entries */
783 duplicate_lte->refcnt += lte->refcnt;
784 inode_update_lte_ptr(lte->lte_inode, lte, duplicate_lte);
785 free_lookup_table_entry(lte);
787 if (stat(lte->staging_file_name, &stbuf) != 0) {
788 ERROR_WITH_ERRNO("Failed to stat `%s'", lte->staging_file_name);
789 return WIMLIB_ERR_STAT;
791 if (stbuf.st_size == 0) {
792 /* Zero-length stream. No lookup table entry needed. */
793 inode_update_lte_ptr(lte->lte_inode, lte, NULL);
794 free_lookup_table_entry(lte);
796 BUILD_BUG_ON(<e->file_on_disk != <e->staging_file_name);
797 lte->resource_entry.original_size = stbuf.st_size;
798 lte->resource_entry.size = stbuf.st_size;
799 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
800 lte->file_on_disk_fp = NULL;
801 copy_hash(lte->hash, hash);
802 lookup_table_insert(table, lte);
808 static int inode_close_fds(struct wim_inode *inode)
810 u16 num_opened_fds = inode->i_num_opened_fds;
811 for (u16 i = 0, j = 0; j < num_opened_fds; i++) {
812 struct wimfs_fd *fd = inode->i_fds[i];
814 wimlib_assert(fd->f_inode == inode);
815 int ret = close_wimfs_fd(fd);
824 /* Overwrites the WIM file, with changes saved. */
825 static int rebuild_wim(struct wimfs_context *ctx, int write_flags,
826 wimlib_progress_func_t progress_func)
829 struct wim_lookup_table_entry *lte, *tmp;
830 WIMStruct *w = ctx->wim;
832 DEBUG("Closing all staging file descriptors.");
833 list_for_each_entry_safe(lte, tmp, &ctx->staging_list, staging_list) {
834 ret = inode_close_fds(lte->lte_inode);
839 DEBUG("Calculating SHA1 checksums for all new staging files.");
840 list_for_each_entry_safe(lte, tmp, &ctx->staging_list, staging_list) {
841 ret = update_lte_of_staging_file(lte, w->lookup_table);
846 xml_update_image_info(w, w->current_image);
847 ret = wimlib_overwrite(w, write_flags, 0, progress_func);
849 ERROR("Failed to commit changes to mounted WIM image");
855 /* Simple function that returns the concatenation of 2 strings. */
856 static char *strcat_dup(const char *s1, const char *s2, size_t max_len)
858 size_t len = strlen(s1) + strlen(s2);
861 char *p = MALLOC(len + 1);
864 snprintf(p, len + 1, "%s%s", s1, s2);
868 static int set_message_queue_names(struct wimfs_context *ctx,
869 const char *mount_dir)
871 static const char *u2d_prefix = "/wimlib-unmount-to-daemon-mq";
872 static const char *d2u_prefix = "/wimlib-daemon-to-unmount-mq";
877 dir_path = realpath(mount_dir, NULL);
879 ERROR_WITH_ERRNO("Failed to resolve path \"%s\"", mount_dir);
881 return WIMLIB_ERR_NOMEM;
883 return WIMLIB_ERR_NOTDIR;
886 for (p = dir_path; *p; p++)
890 ctx->unmount_to_daemon_mq_name = strcat_dup(u2d_prefix, dir_path,
892 if (!ctx->unmount_to_daemon_mq_name) {
893 ret = WIMLIB_ERR_NOMEM;
894 goto out_free_dir_path;
896 ctx->daemon_to_unmount_mq_name = strcat_dup(d2u_prefix, dir_path,
898 if (!ctx->daemon_to_unmount_mq_name) {
899 ret = WIMLIB_ERR_NOMEM;
900 goto out_free_unmount_to_daemon_mq_name;
904 goto out_free_dir_path;
905 out_free_unmount_to_daemon_mq_name:
906 FREE(ctx->unmount_to_daemon_mq_name);
907 ctx->unmount_to_daemon_mq_name = NULL;
913 static void free_message_queue_names(struct wimfs_context *ctx)
915 FREE(ctx->unmount_to_daemon_mq_name);
916 FREE(ctx->daemon_to_unmount_mq_name);
917 ctx->unmount_to_daemon_mq_name = NULL;
918 ctx->daemon_to_unmount_mq_name = NULL;
922 * Opens two POSIX message queue: one for sending messages from the unmount
923 * process to the daemon process, and one to go the other way. The names of the
924 * message queues, which must be system-wide unique, are be based on the mount
927 * @daemon specifies whether the calling process is the filesystem daemon or the
930 static int open_message_queues(struct wimfs_context *ctx, bool daemon)
932 int unmount_to_daemon_mq_flags = O_WRONLY | O_CREAT;
933 int daemon_to_unmount_mq_flags = O_RDONLY | O_CREAT;
939 swap(unmount_to_daemon_mq_flags, daemon_to_unmount_mq_flags);
945 orig_umask = umask(0000);
946 DEBUG("Opening message queue \"%s\"", ctx->unmount_to_daemon_mq_name);
947 ctx->unmount_to_daemon_mq = mq_open(ctx->unmount_to_daemon_mq_name,
948 unmount_to_daemon_mq_flags, mode, NULL);
950 if (ctx->unmount_to_daemon_mq == (mqd_t)-1) {
951 ERROR_WITH_ERRNO("mq_open()");
952 ret = WIMLIB_ERR_MQUEUE;
956 DEBUG("Opening message queue \"%s\"", ctx->daemon_to_unmount_mq_name);
957 ctx->daemon_to_unmount_mq = mq_open(ctx->daemon_to_unmount_mq_name,
958 daemon_to_unmount_mq_flags, mode, NULL);
960 if (ctx->daemon_to_unmount_mq == (mqd_t)-1) {
961 ERROR_WITH_ERRNO("mq_open()");
962 mq_close(ctx->unmount_to_daemon_mq);
963 mq_unlink(ctx->unmount_to_daemon_mq_name);
964 ctx->unmount_to_daemon_mq = (mqd_t)-1;
965 ret = WIMLIB_ERR_MQUEUE;
974 /* Try to determine the maximum message size of a message queue. The return
975 * value is the maximum message size, or a guess of 8192 bytes if it cannot be
977 static long mq_get_msgsize(mqd_t mq)
979 static const char *msgsize_max_file = "/proc/sys/fs/mqueue/msgsize_max";
984 if (mq_getattr(mq, &attr) == 0) {
985 msgsize = attr.mq_msgsize;
987 ERROR_WITH_ERRNO("mq_getattr()");
988 ERROR("Attempting to read %s", msgsize_max_file);
989 fp = fopen(msgsize_max_file, "rb");
991 if (fscanf(fp, "%ld", &msgsize) != 1) {
992 ERROR("Assuming message size of 8192");
997 ERROR_WITH_ERRNO("Failed to open the file `%s'",
999 ERROR("Assuming message size of 8192");
1006 static int get_mailbox(mqd_t mq, long needed_msgsize, long *msgsize_ret,
1012 msgsize = mq_get_msgsize(mq);
1014 if (msgsize < needed_msgsize) {
1015 ERROR("Message queue max size must be at least %ld!",
1017 return WIMLIB_ERR_MQUEUE;
1020 mailbox = MALLOC(msgsize);
1022 ERROR("Failed to allocate %ld bytes for mailbox", msgsize);
1023 return WIMLIB_ERR_NOMEM;
1025 *msgsize_ret = msgsize;
1026 *mailbox_ret = mailbox;
1030 static void unlink_message_queues(struct wimfs_context *ctx)
1032 mq_unlink(ctx->unmount_to_daemon_mq_name);
1033 mq_unlink(ctx->daemon_to_unmount_mq_name);
1036 /* Closes the message queues, which are allocated in static variables */
1037 static void close_message_queues(struct wimfs_context *ctx)
1039 DEBUG("Closing message queues");
1040 mq_close(ctx->unmount_to_daemon_mq);
1041 ctx->unmount_to_daemon_mq = (mqd_t)(-1);
1042 mq_close(ctx->daemon_to_unmount_mq);
1043 ctx->daemon_to_unmount_mq = (mqd_t)(-1);
1044 unlink_message_queues(ctx);
1048 struct unmount_msg_hdr {
1055 struct msg_unmount_request {
1056 struct unmount_msg_hdr hdr;
1058 u8 want_progress_messages;
1061 struct msg_daemon_info {
1062 struct unmount_msg_hdr hdr;
1067 struct msg_unmount_finished {
1068 struct unmount_msg_hdr hdr;
1072 struct msg_write_streams_progress {
1073 struct unmount_msg_hdr hdr;
1074 union wimlib_progress_info info;
1078 MSG_TYPE_UNMOUNT_REQUEST,
1079 MSG_TYPE_DAEMON_INFO,
1080 MSG_TYPE_WRITE_STREAMS_PROGRESS,
1081 MSG_TYPE_UNMOUNT_FINISHED,
1085 struct msg_handler_context_hdr {
1086 int timeout_seconds;
1089 struct unmount_msg_handler_context {
1090 struct msg_handler_context_hdr hdr;
1094 wimlib_progress_func_t progress_func;
1097 struct daemon_msg_handler_context {
1098 struct msg_handler_context_hdr hdr;
1099 struct wimfs_context *wimfs_ctx;
1102 static int send_unmount_request_msg(mqd_t mq, int unmount_flags,
1103 u8 want_progress_messages)
1105 DEBUG("Sending unmount request msg");
1106 struct msg_unmount_request msg = {
1108 .min_version = WIMLIB_MAKEVERSION(1, 2, 1),
1109 .cur_version = WIMLIB_VERSION_CODE,
1110 .msg_type = MSG_TYPE_UNMOUNT_REQUEST,
1111 .msg_size = sizeof(msg),
1113 .unmount_flags = unmount_flags,
1114 .want_progress_messages = want_progress_messages,
1117 if (mq_send(mq, (void*)&msg, sizeof(msg), 1)) {
1118 ERROR_WITH_ERRNO("Failed to communicate with filesystem daemon");
1119 return WIMLIB_ERR_MQUEUE;
1124 static int send_daemon_info_msg(mqd_t mq, pid_t pid, int mount_flags)
1126 DEBUG("Sending daemon info msg (pid = %d, mount_flags=%x)",
1129 struct msg_daemon_info msg = {
1131 .min_version = WIMLIB_MAKEVERSION(1, 2, 1),
1132 .cur_version = WIMLIB_VERSION_CODE,
1133 .msg_type = MSG_TYPE_DAEMON_INFO,
1134 .msg_size = sizeof(msg),
1137 .mount_flags = mount_flags,
1139 if (mq_send(mq, (void*)&msg, sizeof(msg), 1)) {
1140 ERROR_WITH_ERRNO("Failed to send daemon info to unmount process");
1141 return WIMLIB_ERR_MQUEUE;
1146 static void send_unmount_finished_msg(mqd_t mq, int status)
1148 DEBUG("Sending unmount finished msg");
1149 struct msg_unmount_finished msg = {
1151 .min_version = WIMLIB_MAKEVERSION(1, 2, 1),
1152 .cur_version = WIMLIB_VERSION_CODE,
1153 .msg_type = MSG_TYPE_UNMOUNT_FINISHED,
1154 .msg_size = sizeof(msg),
1158 if (mq_send(mq, (void*)&msg, sizeof(msg), 1))
1159 ERROR_WITH_ERRNO("Failed to send status to unmount process");
1162 static int unmount_progress_func(enum wimlib_progress_msg msg,
1163 const union wimlib_progress_info *info)
1165 if (msg == WIMLIB_PROGRESS_MSG_WRITE_STREAMS) {
1166 struct msg_write_streams_progress msg = {
1168 .min_version = WIMLIB_MAKEVERSION(1, 2, 1),
1169 .cur_version = WIMLIB_VERSION_CODE,
1170 .msg_type = MSG_TYPE_WRITE_STREAMS_PROGRESS,
1171 .msg_size = sizeof(msg),
1175 if (mq_send(wimfs_get_context()->daemon_to_unmount_mq,
1176 (void*)&msg, sizeof(msg), 1))
1178 ERROR_WITH_ERRNO("Failed to send progress information "
1179 "to unmount process");
1185 static int msg_unmount_request_handler(const void *_msg, void *_handler_ctx)
1187 const struct msg_unmount_request *msg = _msg;
1188 struct daemon_msg_handler_context *handler_ctx = _handler_ctx;
1189 struct wimfs_context *wimfs_ctx;
1193 wimlib_progress_func_t progress_func;
1195 DEBUG("Handling unmount request msg");
1197 wimfs_ctx = handler_ctx->wimfs_ctx;
1198 if (msg->hdr.msg_size < sizeof(*msg)) {
1199 status = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1203 unmount_flags = msg->unmount_flags;
1204 if (msg->want_progress_messages)
1205 progress_func = unmount_progress_func;
1207 progress_func = NULL;
1209 ret = send_daemon_info_msg(wimfs_ctx->daemon_to_unmount_mq, getpid(),
1210 wimfs_ctx->mount_flags);
1216 if (wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1217 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_COMMIT) {
1218 int write_flags = 0;
1219 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY)
1220 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1221 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_REBUILD)
1222 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1223 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_RECOMPRESS)
1224 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
1225 status = rebuild_wim(wimfs_ctx, write_flags,
1229 DEBUG("Read-only mount");
1234 if (wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1235 ret = delete_staging_dir(wimfs_ctx);
1237 ERROR("Failed to delete the staging directory");
1242 wimfs_ctx->status = status;
1243 wimfs_ctx->have_status = true;
1244 return MSG_BREAK_LOOP;
1247 static int msg_daemon_info_handler(const void *_msg, void *_handler_ctx)
1249 const struct msg_daemon_info *msg = _msg;
1250 struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1252 DEBUG("Handling daemon info msg");
1253 if (msg->hdr.msg_size < sizeof(*msg))
1254 return WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1255 handler_ctx->daemon_pid = msg->daemon_pid;
1256 handler_ctx->mount_flags = msg->mount_flags;
1257 handler_ctx->hdr.timeout_seconds = 1;
1258 DEBUG("pid of daemon is %d; mount flags were %#x",
1259 handler_ctx->daemon_pid,
1260 handler_ctx->mount_flags);
1264 static int msg_write_streams_progress_handler(const void *_msg,
1267 const struct msg_write_streams_progress *msg = _msg;
1268 struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1270 if (msg->hdr.msg_size < sizeof(*msg))
1271 return WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1272 if (handler_ctx->progress_func) {
1273 handler_ctx->progress_func(WIMLIB_PROGRESS_MSG_WRITE_STREAMS,
1279 static int msg_unmount_finished_handler(const void *_msg, void *_handler_ctx)
1281 const struct msg_unmount_finished *msg = _msg;
1282 struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1284 DEBUG("Handling unmount finished message");
1285 if (msg->hdr.msg_size < sizeof(*msg))
1286 return WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1287 handler_ctx->status = msg->status;
1288 DEBUG("status is %d", handler_ctx->status);
1289 return MSG_BREAK_LOOP;
1292 static int unmount_timed_out_cb(void *_handler_ctx)
1294 struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1296 if (handler_ctx->daemon_pid == 0) {
1299 kill(handler_ctx->daemon_pid, 0);
1300 if (errno == ESRCH) {
1303 DEBUG("Filesystem daemon is still alive... "
1304 "Waiting another %d seconds\n",
1305 handler_ctx->hdr.timeout_seconds);
1310 ERROR("The filesystem daemon has crashed! Changes to the "
1311 "WIM may not have been commited.");
1312 return WIMLIB_ERR_FILESYSTEM_DAEMON_CRASHED;
1315 static int daemon_timed_out_cb(void *_handler_ctx)
1317 ERROR("Timed out waiting for unmount request! "
1318 "Changes to the mounted WIM will not be committed.");
1319 return WIMLIB_ERR_TIMEOUT;
1322 typedef int (*msg_handler_t)(const void *_msg, void *_handler_ctx);
1324 struct msg_handler_callbacks {
1325 int (*timed_out)(void * _handler_ctx);
1326 msg_handler_t msg_handlers[MSG_TYPE_MAX];
1329 static const struct msg_handler_callbacks unmount_msg_handler_callbacks = {
1330 .timed_out = unmount_timed_out_cb,
1332 [MSG_TYPE_DAEMON_INFO] = msg_daemon_info_handler,
1333 [MSG_TYPE_WRITE_STREAMS_PROGRESS] = msg_write_streams_progress_handler,
1334 [MSG_TYPE_UNMOUNT_FINISHED] = msg_unmount_finished_handler,
1338 static const struct msg_handler_callbacks daemon_msg_handler_callbacks = {
1339 .timed_out = daemon_timed_out_cb,
1341 [MSG_TYPE_UNMOUNT_REQUEST] = msg_unmount_request_handler,
1345 static int receive_message(mqd_t mq,
1346 struct msg_handler_context_hdr *handler_ctx,
1347 const msg_handler_t msg_handlers[],
1348 long mailbox_size, void *mailbox)
1351 struct timespec timeout;
1352 ssize_t bytes_received;
1353 struct unmount_msg_hdr *hdr;
1356 gettimeofday(&now, NULL);
1357 timeout.tv_sec = now.tv_sec + handler_ctx->timeout_seconds;
1358 timeout.tv_nsec = now.tv_usec * 1000;
1360 bytes_received = mq_timedreceive(mq, mailbox,
1361 mailbox_size, NULL, &timeout);
1363 if (bytes_received == -1) {
1364 if (errno == ETIMEDOUT) {
1365 ret = WIMLIB_ERR_TIMEOUT;
1367 ERROR_WITH_ERRNO("mq_timedreceive()");
1368 ret = WIMLIB_ERR_MQUEUE;
1370 } else if (bytes_received < sizeof(*hdr) ||
1371 bytes_received != hdr->msg_size) {
1372 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1373 } else if (WIMLIB_VERSION_CODE < hdr->min_version) {
1374 /*ERROR("Cannot understand the received message. "*/
1375 /*"Please upgrade wimlib to at least v%d.%d.%d",*/
1376 /*WIMLIB_GET_MAJOR_VERSION(hdr->min_version),*/
1377 /*WIMLIB_GET_MINOR_VERSION(hdr->min_version),*/
1378 /*WIMLIB_GET_PATCH_VERSION(hdr->min_version));*/
1379 ret = MSG_VERSION_TOO_HIGH;
1380 } else if (hdr->msg_type >= MSG_TYPE_MAX) {
1381 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1382 } else if (msg_handlers[hdr->msg_type] == NULL) {
1383 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1385 ret = msg_handlers[hdr->msg_type](mailbox, handler_ctx);
1390 static int message_loop(mqd_t mq,
1391 const struct msg_handler_callbacks *callbacks,
1392 struct msg_handler_context_hdr *handler_ctx)
1394 static const size_t MAX_MSG_SIZE = 512;
1399 DEBUG("Entering message loop");
1401 ret = get_mailbox(mq, MAX_MSG_SIZE, &msgsize, &mailbox);
1405 ret = receive_message(mq, handler_ctx,
1406 callbacks->msg_handlers,
1408 if (ret == 0 || ret == MSG_VERSION_TOO_HIGH) {
1410 } else if (ret == MSG_BREAK_LOOP) {
1413 } else if (ret == WIMLIB_ERR_TIMEOUT) {
1414 if (callbacks->timed_out)
1415 ret = callbacks->timed_out(handler_ctx);
1421 ERROR_WITH_ERRNO("Error communicating with "
1422 "filesystem daemon");
1427 DEBUG("Exiting message loop");
1431 /* Execute `fusermount -u', which is installed setuid root, to unmount the WIM.
1433 * FUSE does not yet implement synchronous unmounts. This means that fusermount
1434 * -u will return before the filesystem daemon returns from wimfs_destroy().
1435 * This is partly what we want, because we need to send a message from this
1436 * process to the filesystem daemon telling whether --commit was specified or
1437 * not. However, after that, the unmount process must wait for the filesystem
1438 * daemon to finish writing the WIM file.
1440 static int execute_fusermount(const char *dir)
1448 ERROR_WITH_ERRNO("Failed to fork()");
1449 return WIMLIB_ERR_FORK;
1453 execlp("fusermount", "fusermount", "-u", dir, NULL);
1454 ERROR_WITH_ERRNO("Failed to execute `fusermount'");
1455 exit(WIMLIB_ERR_FUSERMOUNT);
1459 ret = waitpid(pid, &status, 0);
1461 ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
1463 return WIMLIB_ERR_FUSERMOUNT;
1466 if (!WIFEXITED(status)) {
1467 ERROR("'fusermount' did not terminate normally!");
1468 return WIMLIB_ERR_FUSERMOUNT;
1471 status = WEXITSTATUS(status);
1476 if (status != WIMLIB_ERR_FUSERMOUNT)
1477 return WIMLIB_ERR_FUSERMOUNT;
1479 /* Try again, but with the `umount' program. This is required on other
1480 * FUSE implementations such as FreeBSD's that do not have a
1481 * `fusermount' program. */
1482 ERROR("Falling back to 'umount'. Note: you may need to be "
1483 "root for this to work");
1486 ERROR_WITH_ERRNO("Failed to fork()");
1487 return WIMLIB_ERR_FORK;
1491 execlp("umount", "umount", dir, NULL);
1492 ERROR_WITH_ERRNO("Failed to execute `umount'");
1493 exit(WIMLIB_ERR_FUSERMOUNT);
1497 ret = waitpid(pid, &status, 0);
1499 ERROR_WITH_ERRNO("Failed to wait for `umount' process to "
1501 return WIMLIB_ERR_FUSERMOUNT;
1504 ERROR("`umount' did not successfully complete");
1505 return WIMLIB_ERR_FUSERMOUNT;
1511 static int wimfs_access(const char *path, int mask)
1517 static int wimfs_chmod(const char *path, mode_t mask)
1519 struct wim_dentry *dentry;
1520 struct wimfs_context *ctx = wimfs_get_context();
1523 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA))
1526 ret = lookup_resource(ctx->wim, path, LOOKUP_FLAG_DIRECTORY_OK,
1527 &dentry, NULL, NULL);
1531 ret = inode_set_unix_data(dentry->d_inode, ctx->default_uid,
1532 ctx->default_gid, mask,
1533 ctx->wim->lookup_table, UNIX_DATA_MODE);
1534 return ret ? -ENOMEM : 0;
1537 static int wimfs_chown(const char *path, uid_t uid, gid_t gid)
1539 struct wim_dentry *dentry;
1540 struct wimfs_context *ctx = wimfs_get_context();
1543 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA))
1546 ret = lookup_resource(ctx->wim, path, LOOKUP_FLAG_DIRECTORY_OK,
1547 &dentry, NULL, NULL);
1551 ret = inode_set_unix_data(dentry->d_inode, uid, gid,
1552 inode_default_unix_mode(dentry->d_inode),
1553 ctx->wim->lookup_table,
1554 UNIX_DATA_UID | UNIX_DATA_GID);
1555 return ret ? -ENOMEM : 0;
1558 /* Called when the filesystem is unmounted. */
1559 static void wimfs_destroy(void *p)
1561 struct wimfs_context *wimfs_ctx = wimfs_get_context();
1562 if (open_message_queues(wimfs_ctx, true) == 0) {
1563 struct daemon_msg_handler_context handler_ctx = {
1565 .timeout_seconds = 5,
1567 .wimfs_ctx = wimfs_ctx,
1569 message_loop(wimfs_ctx->unmount_to_daemon_mq,
1570 &daemon_msg_handler_callbacks,
1576 static int wimfs_fallocate(const char *path, int mode,
1577 off_t offset, off_t len, struct fuse_file_info *fi)
1579 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1580 wimlib_assert(fd->staging_fd != -1);
1581 return fallocate(fd->staging_fd, mode, offset, len);
1586 static int wimfs_fgetattr(const char *path, struct stat *stbuf,
1587 struct fuse_file_info *fi)
1589 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1590 return inode_to_stbuf(fd->f_inode, fd->f_lte, stbuf);
1593 static int wimfs_ftruncate(const char *path, off_t size,
1594 struct fuse_file_info *fi)
1596 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1597 int ret = ftruncate(fd->staging_fd, size);
1600 if (fd->f_lte && size < fd->f_lte->resource_entry.original_size)
1601 fd->f_lte->resource_entry.original_size = size;
1606 * Fills in a `struct stat' that corresponds to a file or directory in the WIM.
1608 static int wimfs_getattr(const char *path, struct stat *stbuf)
1610 struct wim_dentry *dentry;
1611 struct wim_lookup_table_entry *lte;
1613 struct wimfs_context *ctx = wimfs_get_context();
1615 ret = lookup_resource(ctx->wim, path,
1616 get_lookup_flags(ctx) | LOOKUP_FLAG_DIRECTORY_OK,
1617 &dentry, <e, NULL);
1620 return inode_to_stbuf(dentry->d_inode, lte, stbuf);
1624 /* Read an alternate data stream through the XATTR interface, or get its size */
1625 static int wimfs_getxattr(const char *path, const char *name, char *value,
1629 struct wim_inode *inode;
1630 struct wim_ads_entry *ads_entry;
1632 struct wim_lookup_table_entry *lte;
1633 struct wimfs_context *ctx = wimfs_get_context();
1635 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1638 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1642 inode = wim_pathname_to_inode(ctx->wim, path);
1646 ads_entry = inode_get_ads_entry(inode, name, NULL);
1650 lte = ads_entry->lte;
1651 res_size = wim_resource_size(lte);
1656 if (res_size > size)
1659 ret = read_full_wim_resource(lte, (u8*)value,
1660 WIMLIB_RESOURCE_FLAG_MULTITHREADED);
1668 /* Create a hard link */
1669 static int wimfs_link(const char *to, const char *from)
1671 struct wim_dentry *from_dentry, *from_dentry_parent;
1672 const char *link_name;
1673 struct wim_inode *inode;
1674 struct wim_lookup_table_entry *lte;
1675 WIMStruct *w = wimfs_get_WIMStruct();
1678 inode = wim_pathname_to_inode(w, to);
1682 if (inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
1683 FILE_ATTRIBUTE_REPARSE_POINT))
1686 from_dentry_parent = get_parent_dentry(w, from);
1687 if (!from_dentry_parent)
1689 if (!dentry_is_directory(from_dentry_parent))
1692 link_name = path_basename(from);
1693 if (get_dentry_child_with_name(from_dentry_parent, link_name))
1695 from_dentry = new_dentry(link_name);
1699 inode_add_dentry(from_dentry, inode);
1700 from_dentry->d_inode = inode;
1703 for (i = 0; i <= inode->i_num_ads; i++) {
1704 lte = inode_stream_lte_resolved(inode, i);
1708 dentry_add_child(from_dentry_parent, from_dentry);
1713 static int wimfs_listxattr(const char *path, char *list, size_t size)
1716 struct wim_inode *inode;
1717 struct wimfs_context *ctx = wimfs_get_context();
1721 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1724 /* List alternate data streams, or get the list size */
1726 inode = wim_pathname_to_inode(ctx->wim, path);
1732 for (i = 0; i < inode->i_num_ads; i++)
1733 needed_size += inode->i_ads_entries[i].stream_name_utf8_len + 6;
1737 for (i = 0; i < inode->i_num_ads; i++) {
1738 needed_size = inode->i_ads_entries[i].stream_name_utf8_len + 6;
1739 if (needed_size > size)
1741 p += sprintf(p, "user.%s",
1742 inode->i_ads_entries[i].stream_name_utf8) + 1;
1743 size -= needed_size;
1751 /* Create a directory in the WIM image. */
1752 static int wimfs_mkdir(const char *path, mode_t mode)
1754 return create_dentry(fuse_get_context(), path, mode | S_IFDIR,
1755 FILE_ATTRIBUTE_DIRECTORY, NULL);
1758 /* Create a regular file or alternate data stream in the WIM image. */
1759 static int wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
1761 const char *stream_name;
1762 struct fuse_context *fuse_ctx = fuse_get_context();
1763 struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
1768 if ((wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
1769 && (stream_name = path_stream_name(path))) {
1770 /* Make an alternate data stream */
1771 struct wim_ads_entry *new_entry;
1772 struct wim_inode *inode;
1774 char *p = (char*)stream_name - 1;
1775 wimlib_assert(*p == ':');
1778 inode = wim_pathname_to_inode(wimfs_ctx->wim, path);
1781 if (inode->i_attributes &
1782 (FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_DIRECTORY))
1784 if (inode_get_ads_entry(inode, stream_name, NULL))
1786 new_entry = inode_add_ads(inode, stream_name);
1791 /* Make a normal file (not an alternate data stream) */
1792 return create_dentry(fuse_ctx, path, mode | S_IFREG,
1793 FILE_ATTRIBUTE_NORMAL, NULL);
1799 static int wimfs_open(const char *path, struct fuse_file_info *fi)
1801 struct wim_dentry *dentry;
1802 struct wim_lookup_table_entry *lte;
1804 struct wimfs_fd *fd;
1805 struct wim_inode *inode;
1808 struct wimfs_context *ctx = wimfs_get_context();
1810 ret = lookup_resource(ctx->wim, path, get_lookup_flags(ctx),
1811 &dentry, <e, &stream_idx);
1815 inode = dentry->d_inode;
1817 if (stream_idx == 0)
1820 stream_id = inode->i_ads_entries[stream_idx - 1].stream_id;
1822 /* The file resource may be in the staging directory (read-write mounts
1823 * only) or in the WIM. If it's in the staging directory, we need to
1824 * open a native file descriptor for the corresponding file. Otherwise,
1825 * we can read the file resource directly from the WIM file if we are
1826 * opening it read-only, but we need to extract the resource to the
1827 * staging directory if we are opening it writable. */
1829 if (flags_writable(fi->flags) &&
1830 (!lte || lte->resource_location != RESOURCE_IN_STAGING_FILE)) {
1831 u64 size = (lte) ? wim_resource_size(lte) : 0;
1832 ret = extract_resource_to_staging_dir(inode, stream_id,
1838 ret = alloc_wimfs_fd(inode, stream_id, lte, &fd,
1839 wimfs_ctx_readonly(ctx));
1843 if (lte && lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1844 fd->staging_fd = open(lte->staging_file_name, fi->flags);
1845 if (fd->staging_fd == -1) {
1846 int errno_save = errno;
1851 fi->fh = (uintptr_t)fd;
1855 /* Opens a directory. */
1856 static int wimfs_opendir(const char *path, struct fuse_file_info *fi)
1858 struct wim_inode *inode;
1860 struct wimfs_fd *fd = NULL;
1861 struct wimfs_context *ctx = wimfs_get_context();
1862 WIMStruct *w = ctx->wim;
1864 inode = wim_pathname_to_inode(w, path);
1867 if (!inode_is_directory(inode))
1869 ret = alloc_wimfs_fd(inode, 0, NULL, &fd, wimfs_ctx_readonly(ctx));
1870 fi->fh = (uintptr_t)fd;
1876 * Read data from a file in the WIM or in the staging directory.
1878 static int wimfs_read(const char *path, char *buf, size_t size,
1879 off_t offset, struct fuse_file_info *fi)
1881 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1887 if (!fd->f_lte) /* Empty stream with no lookup table entry */
1890 if (fd->f_lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1891 /* Read from staging file */
1893 wimlib_assert(fd->f_lte->staging_file_name);
1894 wimlib_assert(fd->staging_fd != -1);
1896 DEBUG("Seek to offset %"PRIu64, offset);
1898 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1900 ret = read(fd->staging_fd, buf, size);
1906 u64 res_size = wim_resource_size(fd->f_lte);
1907 if (offset > res_size)
1909 size = min(size, res_size - offset);
1910 if (read_wim_resource(fd->f_lte, (u8*)buf,
1912 WIMLIB_RESOURCE_FLAG_MULTITHREADED) != 0)
1918 struct fill_params {
1920 fuse_fill_dir_t filler;
1923 static int dentry_fuse_fill(struct wim_dentry *dentry, void *arg)
1925 struct fill_params *fill_params = arg;
1926 return fill_params->filler(fill_params->buf, dentry->file_name_utf8,
1930 /* Fills in the entries of the directory specified by @path using the
1931 * FUSE-provided function @filler. */
1932 static int wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
1933 off_t offset, struct fuse_file_info *fi)
1935 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1936 struct wim_inode *inode;
1941 inode = fd->f_inode;
1943 struct fill_params fill_params = {
1948 filler(buf, ".", NULL, 0);
1949 filler(buf, "..", NULL, 0);
1951 return for_dentry_in_rbtree(inode->i_children.rb_node,
1952 dentry_fuse_fill, &fill_params);
1956 static int wimfs_readlink(const char *path, char *buf, size_t buf_len)
1958 struct wimfs_context *ctx = wimfs_get_context();
1959 struct wim_inode *inode = wim_pathname_to_inode(ctx->wim, path);
1963 if (!inode_is_symlink(inode))
1966 ret = inode_readlink(inode, buf, buf_len, ctx->wim,
1967 WIMLIB_RESOURCE_FLAG_MULTITHREADED);
1974 static int wimfs_release(const char *path, struct fuse_file_info *fi)
1976 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1977 return close_wimfs_fd(fd);
1980 /* Close a directory */
1981 static int wimfs_releasedir(const char *path, struct fuse_file_info *fi)
1983 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1984 return close_wimfs_fd(fd);
1988 /* Remove an alternate data stream through the XATTR interface */
1989 static int wimfs_removexattr(const char *path, const char *name)
1991 struct wim_inode *inode;
1992 struct wim_ads_entry *ads_entry;
1994 struct wimfs_context *ctx = wimfs_get_context();
1996 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1999 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
2003 inode = wim_pathname_to_inode(ctx->wim, path);
2007 ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
2010 inode_remove_ads(inode, ads_idx, ctx->wim->lookup_table);
2015 /* Renames a file or directory. See rename (3) */
2016 static int wimfs_rename(const char *from, const char *to)
2018 struct wim_dentry *src;
2019 struct wim_dentry *dst;
2020 struct wim_dentry *parent_of_dst;
2021 WIMStruct *w = wimfs_get_WIMStruct();
2024 /* This rename() implementation currently only supports actual files
2025 * (not alternate data streams) */
2027 src = get_dentry(w, from);
2031 dst = get_dentry(w, to);
2034 /* Destination file exists */
2036 if (src == dst) /* Same file */
2039 if (!dentry_is_directory(src)) {
2040 /* Cannot rename non-directory to directory. */
2041 if (dentry_is_directory(dst))
2044 /* Cannot rename directory to a non-directory or a non-empty
2046 if (!dentry_is_directory(dst))
2048 if (inode_has_children(dst->d_inode))
2051 parent_of_dst = dst->parent;
2053 /* Destination does not exist */
2054 parent_of_dst = get_parent_dentry(w, to);
2058 if (!dentry_is_directory(parent_of_dst))
2062 ret = set_dentry_name(src, path_basename(to));
2066 remove_dentry(dst, w->lookup_table);
2068 dentry_add_child(parent_of_dst, src);
2072 /* Remove a directory */
2073 static int wimfs_rmdir(const char *path)
2075 struct wim_dentry *dentry;
2076 WIMStruct *w = wimfs_get_WIMStruct();
2078 dentry = get_dentry(w, path);
2082 if (!dentry_is_directory(dentry))
2085 if (dentry_has_children(dentry))
2088 remove_dentry(dentry, w->lookup_table);
2093 /* Write an alternate data stream through the XATTR interface */
2094 static int wimfs_setxattr(const char *path, const char *name,
2095 const char *value, size_t size, int flags)
2097 struct wim_ads_entry *existing_ads_entry;
2098 struct wim_inode *inode;
2100 struct wimfs_context *ctx = wimfs_get_context();
2103 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
2106 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
2110 inode = wim_pathname_to_inode(ctx->wim, path);
2114 existing_ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
2115 if (existing_ads_entry) {
2116 if (flags & XATTR_CREATE)
2119 if (flags & XATTR_REPLACE)
2123 ret = inode_add_ads_with_data(inode, name, (const u8*)value,
2124 size, ctx->wim->lookup_table);
2126 if (existing_ads_entry)
2127 inode_remove_ads(inode, ads_idx, ctx->wim->lookup_table);
2135 static int wimfs_symlink(const char *to, const char *from)
2137 struct fuse_context *fuse_ctx = fuse_get_context();
2138 struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
2139 struct wim_dentry *dentry;
2142 ret = create_dentry(fuse_ctx, from, S_IFLNK | 0777,
2143 FILE_ATTRIBUTE_REPARSE_POINT, &dentry);
2145 dentry->d_inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
2146 if (inode_set_symlink(dentry->d_inode, to,
2147 wimfs_ctx->wim->lookup_table, NULL))
2149 remove_dentry(dentry, wimfs_ctx->wim->lookup_table);
2157 /* Reduce the size of a file */
2158 static int wimfs_truncate(const char *path, off_t size)
2160 struct wim_dentry *dentry;
2161 struct wim_lookup_table_entry *lte;
2165 struct wim_inode *inode;
2166 struct wimfs_context *ctx = wimfs_get_context();
2168 ret = lookup_resource(ctx->wim, path, get_lookup_flags(ctx),
2169 &dentry, <e, &stream_idx);
2174 if (lte == NULL && size == 0)
2177 inode = dentry->d_inode;
2178 if (stream_idx == 0)
2181 stream_id = inode->i_ads_entries[stream_idx - 1].stream_id;
2183 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
2184 ret = truncate(lte->staging_file_name, size);
2188 /* File in WIM. Extract it to the staging directory, but only
2189 * the first @size bytes of it. */
2190 ret = extract_resource_to_staging_dir(inode, stream_id,
2196 /* Unlink a non-directory or alternate data stream */
2197 static int wimfs_unlink(const char *path)
2199 struct wim_dentry *dentry;
2200 struct wim_lookup_table_entry *lte;
2203 struct wimfs_context *ctx = wimfs_get_context();
2205 ret = lookup_resource(ctx->wim, path, get_lookup_flags(ctx),
2206 &dentry, <e, &stream_idx);
2211 if (stream_idx == 0)
2212 remove_dentry(dentry, ctx->wim->lookup_table);
2214 inode_remove_ads(dentry->d_inode, stream_idx - 1,
2215 ctx->wim->lookup_table);
2219 #ifdef HAVE_UTIMENSAT
2221 * Change the timestamp on a file dentry.
2223 * Note that alternate data streams do not have their own timestamps.
2225 static int wimfs_utimens(const char *path, const struct timespec tv[2])
2227 struct wim_dentry *dentry;
2228 struct wim_inode *inode;
2229 WIMStruct *w = wimfs_get_WIMStruct();
2231 dentry = get_dentry(w, path);
2234 inode = dentry->d_inode;
2236 if (tv[0].tv_nsec != UTIME_OMIT) {
2237 if (tv[0].tv_nsec == UTIME_NOW)
2238 inode->i_last_access_time = get_wim_timestamp();
2240 inode->i_last_access_time = timespec_to_wim_timestamp(&tv[0]);
2242 if (tv[1].tv_nsec != UTIME_OMIT) {
2243 if (tv[1].tv_nsec == UTIME_NOW)
2244 inode->i_last_write_time = get_wim_timestamp();
2246 inode->i_last_write_time = timespec_to_wim_timestamp(&tv[1]);
2251 static int wimfs_utime(const char *path, struct utimbuf *times)
2253 struct wim_dentry *dentry;
2254 struct wim_inode *inode;
2255 WIMStruct *w = wimfs_get_WIMStruct();
2257 dentry = get_dentry(w, path);
2260 inode = dentry->d_inode;
2262 inode->i_last_write_time = unix_timestamp_to_wim(times->modtime);
2263 inode->i_last_access_time = unix_timestamp_to_wim(times->actime);
2268 /* Writes to a file in the WIM filesystem.
2269 * It may be an alternate data stream, but here we don't even notice because we
2270 * just get a lookup table entry. */
2271 static int wimfs_write(const char *path, const char *buf, size_t size,
2272 off_t offset, struct fuse_file_info *fi)
2274 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
2281 wimlib_assert(fd->f_lte);
2282 wimlib_assert(fd->f_lte->staging_file_name);
2283 wimlib_assert(fd->staging_fd != -1);
2284 wimlib_assert(fd->f_inode);
2286 /* Seek to the requested position */
2287 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
2290 /* Write the data. */
2291 ret = write(fd->staging_fd, buf, size);
2295 now = get_wim_timestamp();
2296 fd->f_inode->i_last_write_time = now;
2297 fd->f_inode->i_last_access_time = now;
2301 static struct fuse_operations wimfs_operations = {
2303 .access = wimfs_access,
2305 .chmod = wimfs_chmod,
2306 .chown = wimfs_chown,
2307 .destroy = wimfs_destroy,
2309 .fallocate = wimfs_fallocate,
2311 .fgetattr = wimfs_fgetattr,
2312 .ftruncate = wimfs_ftruncate,
2313 .getattr = wimfs_getattr,
2315 .getxattr = wimfs_getxattr,
2319 .listxattr = wimfs_listxattr,
2321 .mkdir = wimfs_mkdir,
2322 .mknod = wimfs_mknod,
2324 .opendir = wimfs_opendir,
2326 .readdir = wimfs_readdir,
2327 .readlink = wimfs_readlink,
2328 .release = wimfs_release,
2329 .releasedir = wimfs_releasedir,
2331 .removexattr = wimfs_removexattr,
2333 .rename = wimfs_rename,
2334 .rmdir = wimfs_rmdir,
2336 .setxattr = wimfs_setxattr,
2338 .symlink = wimfs_symlink,
2339 .truncate = wimfs_truncate,
2340 .unlink = wimfs_unlink,
2341 #ifdef HAVE_UTIMENSAT
2342 .utimens = wimfs_utimens,
2344 .utime = wimfs_utime,
2346 .write = wimfs_write,
2348 /* wimfs keeps file descriptor structures (struct wimfs_fd), so there is
2349 * no need to have the file path provided on operations such as read()
2350 * where only the file descriptor is needed. */
2351 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 8)
2352 .flag_nullpath_ok = 1,
2354 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 9)
2356 .flag_utime_omit_ok = 1,
2361 /* Mounts an image from a WIM file. */
2362 WIMLIBAPI int wimlib_mount_image(WIMStruct *wim, int image, const char *dir,
2363 int mount_flags, WIMStruct **additional_swms,
2364 unsigned num_additional_swms,
2365 const char *staging_dir)
2371 struct wim_lookup_table *joined_tab, *wim_tab_save;
2372 struct wim_image_metadata *imd;
2373 struct wimfs_context ctx;
2374 struct hlist_node *cur_node;
2375 struct wim_inode *inode;
2377 DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
2378 wim, image, dir, mount_flags);
2381 return WIMLIB_ERR_INVALID_PARAM;
2383 ret = verify_swm_set(wim, additional_swms, num_additional_swms);
2387 if ((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) && (wim->hdr.total_parts != 1)) {
2388 ERROR("Cannot mount a split WIM read-write");
2389 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
2392 if (num_additional_swms) {
2393 ret = new_joined_lookup_table(wim, additional_swms,
2394 num_additional_swms,
2398 wim_tab_save = wim->lookup_table;
2399 wim->lookup_table = joined_tab;
2402 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2403 ret = wim_run_full_verifications(wim);
2408 ret = select_wim_image(wim, image);
2412 DEBUG("Selected image %d", image);
2414 imd = wim_get_current_image_metadata(wim);
2416 if (imd->root_dentry->refcnt != 1) {
2417 ERROR("Cannot mount image that was just exported with "
2418 "wimlib_export_image()");
2419 ret = WIMLIB_ERR_INVALID_PARAM;
2423 if (imd->inode_list.first) /* May be unneeded? */
2424 imd->inode_list.first->pprev = &imd->inode_list.first;
2426 if (imd->modified) {
2427 ERROR("Cannot mount image that was added "
2428 "with wimlib_add_image()");
2429 ret = WIMLIB_ERR_INVALID_PARAM;
2433 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2434 ret = lock_wim(wim, wim->fp);
2439 /* Use default stream interface if one was not specified */
2440 if (!(mount_flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
2441 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
2442 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
2443 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
2446 DEBUG("Initializing struct wimfs_context");
2447 init_wimfs_context(&ctx);
2449 ctx.mount_flags = mount_flags;
2450 ctx.image_inode_list = &imd->inode_list;
2451 ctx.default_uid = getuid();
2452 ctx.default_gid = getgid();
2453 if (mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
2454 ctx.default_lookup_flags = LOOKUP_FLAG_ADS_OK;
2456 DEBUG("Unlinking message queues in case they already exist");
2457 ret = set_message_queue_names(&ctx, dir);
2460 unlink_message_queues(&ctx);
2462 DEBUG("Preparing arguments to fuse_main()");
2464 dir_copy = STRDUP(dir);
2466 goto out_free_message_queue_names;
2469 argv[argc++] = "imagex";
2470 argv[argc++] = dir_copy;
2472 /* disable multi-threaded operation for read-write mounts */
2473 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2474 argv[argc++] = "-s";
2476 if (mount_flags & WIMLIB_MOUNT_FLAG_DEBUG)
2477 argv[argc++] = "-d";
2480 * We provide the use_ino option to the FUSE mount because we are going
2481 * to assign inode numbers ourselves. */
2482 char optstring[256] =
2486 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 8)
2489 ",default_permissions"
2491 argv[argc++] = "-o";
2492 argv[argc++] = optstring;
2493 if ((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
2494 /* Read-write mount. Make the staging directory */
2495 ret = make_staging_dir(&ctx, staging_dir);
2497 goto out_free_dir_copy;
2499 /* Read-only mount */
2500 strcat(optstring, ",ro");
2502 if (mount_flags & WIMLIB_MOUNT_FLAG_ALLOW_OTHER)
2503 strcat(optstring, ",allow_other");
2509 DEBUG("FUSE command line (argc = %d): ", argc);
2510 for (i = 0; i < argc; i++) {
2511 fputs(argv[i], stdout);
2519 /* Mark dentry tree as modified if read-write mount. */
2520 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2522 imd->has_been_mounted_rw = 1;
2525 /* Resolve the lookup table entries for every inode in the image, and
2526 * assign inode numbers */
2527 DEBUG("Resolving lookup table entries and assigning inode numbers");
2529 hlist_for_each_entry(inode, cur_node, &imd->inode_list, i_hlist) {
2530 inode_resolve_ltes(inode, wim->lookup_table);
2531 inode->i_ino = ctx.next_ino++;
2533 DEBUG("(next_ino = %"PRIu64")", ctx.next_ino);
2535 DEBUG("Calling fuse_main()");
2537 ret = fuse_main(argc, argv, &wimfs_operations, &ctx);
2539 DEBUG("Returned from fuse_main() (ret = %d)", ret);
2542 ret = WIMLIB_ERR_FUSE;
2544 if (ctx.have_status)
2547 ret = WIMLIB_ERR_TIMEOUT;
2549 if (ctx.daemon_to_unmount_mq != (mqd_t)(-1)) {
2550 send_unmount_finished_msg(ctx.daemon_to_unmount_mq, ret);
2551 close_message_queues(&ctx);
2554 /* Try to delete the staging directory if a deletion wasn't yet
2555 * attempted due to an earlier error */
2556 if (ctx.staging_dir_name)
2557 delete_staging_dir(&ctx);
2561 wim->wim_locked = 0;
2562 out_free_message_queue_names:
2563 free_message_queue_names(&ctx);
2565 if (num_additional_swms) {
2566 free_lookup_table(wim->lookup_table);
2567 wim->lookup_table = wim_tab_save;
2573 * Unmounts the WIM file that was previously mounted on @dir by using
2574 * wimlib_mount_image().
2576 WIMLIBAPI int wimlib_unmount_image(const char *dir, int unmount_flags,
2577 wimlib_progress_func_t progress_func)
2580 struct wimfs_context wimfs_ctx;
2582 init_wimfs_context(&wimfs_ctx);
2584 ret = set_message_queue_names(&wimfs_ctx, dir);
2588 ret = open_message_queues(&wimfs_ctx, false);
2590 goto out_free_message_queue_names;
2592 ret = send_unmount_request_msg(wimfs_ctx.unmount_to_daemon_mq,
2594 progress_func != NULL);
2596 goto out_close_message_queues;
2598 ret = execute_fusermount(dir);
2600 goto out_close_message_queues;
2602 struct unmount_msg_handler_context handler_ctx = {
2604 .timeout_seconds = 5,
2607 .progress_func = progress_func,
2610 ret = message_loop(wimfs_ctx.daemon_to_unmount_mq,
2611 &unmount_msg_handler_callbacks,
2614 ret = handler_ctx.status;
2615 out_close_message_queues:
2616 close_message_queues(&wimfs_ctx);
2617 out_free_message_queue_names:
2618 free_message_queue_names(&wimfs_ctx);
2623 #else /* WITH_FUSE */
2626 static inline int mount_unsupported_error()
2628 ERROR("wimlib was compiled with --without-fuse, which disables support "
2629 "for mounting WIMs.");
2630 return WIMLIB_ERR_UNSUPPORTED;
2633 WIMLIBAPI int wimlib_unmount_image(const char *dir, int unmount_flags,
2634 wimlib_progress_func_t progress_func)
2636 return mount_unsupported_error();
2639 WIMLIBAPI int wimlib_mount_image(WIMStruct *wim_p, int image, const char *dir,
2640 int mount_flags, WIMStruct **additional_swms,
2641 unsigned num_additional_swms,
2642 const char *staging_dir)
2644 return mount_unsupported_error();
2647 #endif /* WITH_FUSE */