4 * This file implements mounting of WIM files using FUSE, which stands for
5 * Filesystem in Userspace. FUSE allows a filesystem to be implemented in a
6 * userspace process by implementing the filesystem primitives--- read(),
7 * write(), readdir(), etc.
11 * Copyright (C) 2012 Eric Biggers
13 * This file is part of wimlib, a library for working with WIM files.
15 * wimlib is free software; you can redistribute it and/or modify it under the
16 * terms of the GNU General Public License as published by the Free
17 * Software Foundation; either version 3 of the License, or (at your option)
20 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
21 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
22 * A PARTICULAR PURPOSE. See the GNU General Public License for more
25 * You should have received a copy of the GNU General Public License
26 * along with wimlib; if not, see http://www.gnu.org/licenses/.
29 #include "wimlib_internal.h"
34 #include "lookup_table.h"
36 #include "buffer_io.h"
37 #include "timestamp.h"
42 #define FUSE_USE_VERSION 26
53 #include <attr/xattr.h>
56 #define MSG_VERSION_TOO_HIGH -1
57 #define MSG_BREAK_LOOP -2
59 /* File descriptor to a file open on the WIM filesystem. */
61 struct wim_inode *f_inode;
62 struct wim_lookup_table_entry *f_lte;
68 struct wimfs_context {
69 /* The WIMStruct for the mounted WIM. */
72 /* Name of the staging directory for a read-write mount. Whenever a new file is
73 * created, it is done so in the staging directory. Furthermore, whenever a
74 * file in the WIM is modified, it is extracted to the staging directory. If
75 * changes are commited when the WIM is unmounted, the file resources are merged
76 * in from the staging directory when writing the new WIM. */
77 char *staging_dir_name;
78 size_t staging_dir_name_len;
80 /* Flags passed to wimlib_mount(). */
83 /* Default flags to use when looking up a WIM dentry (depends on whether
84 * the Windows interface to alternate data streams is being used or
86 int default_lookup_flags;
88 /* Next inode number to be assigned. Note: I didn't bother with a
89 * bitmap of free inode numbers since this isn't even a "real"
90 * filesystem anyway. */
93 /* List of lookup table entries for files in the staging directory */
94 struct list_head staging_list;
96 /* List of inodes in the mounted image */
97 struct hlist_head *image_inode_list;
99 /* Name and message queue descriptors for message queues between the
100 * filesystem daemon process and the unmount process. These are used
101 * when the filesystem is unmounted and the process running
102 * wimlib_unmount_image() (i.e. the `imagex unmount' command) needs to
103 * communicate with the filesystem daemon running fuse_main() (i.e. the
104 * daemon created by the `imagex mount' or `imagex mountrw' commands */
105 char *unmount_to_daemon_mq_name;
106 char *daemon_to_unmount_mq_name;
107 mqd_t unmount_to_daemon_mq;
108 mqd_t daemon_to_unmount_mq;
114 static void init_wimfs_context(struct wimfs_context *ctx)
116 memset(ctx, 0, sizeof(*ctx));
117 ctx->unmount_to_daemon_mq = (mqd_t)-1;
118 ctx->daemon_to_unmount_mq = (mqd_t)-1;
119 INIT_LIST_HEAD(&ctx->staging_list);
122 #define WIMFS_CTX(fuse_ctx) ((struct wimfs_context*)(fuse_ctx)->private_data)
124 static inline struct wimfs_context *wimfs_get_context()
126 return WIMFS_CTX(fuse_get_context());
129 static inline WIMStruct *wimfs_get_WIMStruct()
131 return wimfs_get_context()->wim;
134 static inline bool wimfs_ctx_readonly(const struct wimfs_context *ctx)
136 return (ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) == 0;
139 static inline int get_lookup_flags(const struct wimfs_context *ctx)
141 return ctx->default_lookup_flags;
144 /* Returns nonzero if write permission is requested on the file open flags */
145 static inline int flags_writable(int open_flags)
147 return open_flags & (O_RDWR | O_WRONLY);
151 * Allocate a file descriptor for a stream.
153 * @inode: inode containing the stream we're opening
154 * @stream_id: ID of the stream we're opening
155 * @lte: Lookup table entry for the stream (may be NULL)
156 * @fd_ret: Return the allocated file descriptor if successful.
157 * @readonly: True if this is a read-only mount.
159 * Return 0 iff successful or error code if unsuccessful.
161 static int alloc_wimfs_fd(struct wim_inode *inode,
163 struct wim_lookup_table_entry *lte,
164 struct wimfs_fd **fd_ret,
167 static const u16 fds_per_alloc = 8;
168 static const u16 max_fds = 0xffff;
171 pthread_mutex_lock(&inode->i_mutex);
173 DEBUG("Allocating fd for stream ID %u from inode %#"PRIx64" "
174 "(open = %u, allocated = %u)",
175 stream_id, inode->i_ino, inode->i_num_opened_fds,
176 inode->i_num_allocated_fds);
178 if (inode->i_num_opened_fds == inode->i_num_allocated_fds) {
179 struct wimfs_fd **fds;
182 if (inode->i_num_allocated_fds == max_fds) {
186 num_new_fds = min(fds_per_alloc,
187 max_fds - inode->i_num_allocated_fds);
189 fds = REALLOC(inode->i_fds,
190 (inode->i_num_allocated_fds + num_new_fds) *
191 sizeof(inode->i_fds[0]));
196 memset(&fds[inode->i_num_allocated_fds], 0,
197 num_new_fds * sizeof(fds[0]));
199 inode->i_num_allocated_fds += num_new_fds;
201 for (u16 i = 0; ; i++) {
202 if (!inode->i_fds[i]) {
203 struct wimfs_fd *fd = CALLOC(1, sizeof(*fd));
212 fd->stream_id = stream_id;
214 inode->i_fds[i] = fd;
215 inode->i_num_opened_fds++;
216 if (lte && !readonly)
217 lte->num_opened_fds++;
218 DEBUG("Allocated fd (idx = %u)", fd->idx);
224 pthread_mutex_unlock(&inode->i_mutex);
228 static void inode_put_fd(struct wim_inode *inode, struct wimfs_fd *fd)
230 wimlib_assert(inode != NULL);
232 pthread_mutex_lock(&inode->i_mutex);
234 wimlib_assert(fd->f_inode == inode);
235 wimlib_assert(inode->i_num_opened_fds != 0);
236 wimlib_assert(fd->idx < inode->i_num_allocated_fds);
237 wimlib_assert(inode->i_fds[fd->idx] == fd);
239 inode->i_fds[fd->idx] = NULL;
241 if (--inode->i_num_opened_fds == 0 && inode->i_nlink == 0) {
242 pthread_mutex_unlock(&inode->i_mutex);
245 pthread_mutex_unlock(&inode->i_mutex);
249 static int lte_put_fd(struct wim_lookup_table_entry *lte, struct wimfs_fd *fd)
251 wimlib_assert(fd->f_lte == lte);
253 if (!lte) /* Empty stream with no lookup table entry */
256 /* Close staging file descriptor if needed. */
258 if (lte->resource_location == RESOURCE_IN_STAGING_FILE
259 && fd->staging_fd != -1)
261 if (close(fd->staging_fd) != 0) {
262 ERROR_WITH_ERRNO("Failed to close staging file");
266 lte_decrement_num_opened_fds(lte);
270 /* Close a file descriptor. */
271 static int close_wimfs_fd(struct wimfs_fd *fd)
274 DEBUG("Closing fd (ino = %#"PRIx64", opened = %u, allocated = %u)",
275 fd->f_inode->i_ino, fd->f_inode->i_num_opened_fds,
276 fd->f_inode->i_num_allocated_fds);
277 ret = lte_put_fd(fd->f_lte, fd);
281 inode_put_fd(fd->f_inode, fd);
286 * Add a new dentry with a new inode to a WIM image.
288 * Returns 0 on success, or negative error number on failure.
290 static int create_dentry(struct fuse_context *fuse_ctx,const char *path,
291 mode_t mode, int attributes,
292 struct wim_dentry **dentry_ret)
294 struct wim_dentry *parent;
295 struct wim_dentry *new;
296 const char *basename;
297 struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
299 parent = get_parent_dentry(wimfs_ctx->wim, path);
303 if (!dentry_is_directory(parent))
306 basename = path_basename(path);
307 if (get_dentry_child_with_name(parent, basename))
310 new = new_dentry_with_inode(basename);
314 new->d_inode->i_resolved = 1;
315 new->d_inode->i_ino = wimfs_ctx->next_ino++;
316 new->d_inode->i_attributes = attributes;
318 if (wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA) {
319 if (inode_set_unix_data(new->d_inode,
322 mode & ~fuse_ctx->umask,
323 wimfs_ctx->wim->lookup_table,
324 UNIX_DATA_ALL | UNIX_DATA_CREATE))
330 dentry_add_child(parent, new);
331 hlist_add_head(&new->d_inode->i_hlist, wimfs_ctx->image_inode_list);
337 /* Remove a dentry from a mounted WIM image; i.e. remove an alias for the
338 * corresponding inode.
340 * If there are no remaining references to the inode either through dentries or
341 * open file descriptors, the inode is freed. Otherwise, the inode is not
342 * removed, but the dentry is unlinked and freed.
344 * Either way, all lookup table entries referenced by the inode have their
345 * reference count decremented. If a lookup table entry has no open file
346 * descriptors and no references remaining, it is freed, and the corresponding
347 * staging file is unlinked.
349 static void remove_dentry(struct wim_dentry *dentry,
350 struct wim_lookup_table *lookup_table)
352 struct wim_inode *inode = dentry->d_inode;
353 struct wim_lookup_table_entry *lte;
356 for (i = 0; i <= inode->i_num_ads; i++) {
357 lte = inode_stream_lte_resolved(inode, i);
359 lte_decrement_refcnt(lte, lookup_table);
361 unlink_dentry(dentry);
365 static mode_t inode_default_unix_mode(const struct wim_inode *inode)
367 if (inode_is_symlink(inode))
368 return S_IFLNK | 0777;
369 else if (inode_is_directory(inode))
370 return S_IFDIR | 0777;
372 return S_IFREG | 0777;
375 /* Transfers file attributes from a struct wim_inode to a `stat' buffer.
377 * The lookup table entry tells us which stream in the inode we are statting.
378 * For a named data stream, everything returned is the same as the unnamed data
379 * stream except possibly the size and block count. */
380 static int inode_to_stbuf(const struct wim_inode *inode,
381 const struct wim_lookup_table_entry *lte,
384 const struct wimfs_context *ctx = wimfs_get_context();
386 memset(stbuf, 0, sizeof(struct stat));
387 stbuf->st_mode = inode_default_unix_mode(inode);
388 stbuf->st_uid = ctx->default_uid;
389 stbuf->st_gid = ctx->default_gid;
390 if (ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA) {
391 struct wimlib_unix_data unix_data;
392 if (inode_get_unix_data(inode, &unix_data, NULL) == 0) {
393 stbuf->st_uid = unix_data.uid;
394 stbuf->st_gid = unix_data.gid;
395 stbuf->st_mode = unix_data.mode;
398 stbuf->st_ino = (ino_t)inode->i_ino;
399 stbuf->st_nlink = inode->i_nlink;
401 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
402 struct stat native_stat;
403 if (stat(lte->staging_file_name, &native_stat) != 0) {
404 DEBUG("Failed to stat `%s': %m",
405 lte->staging_file_name);
408 stbuf->st_size = native_stat.st_size;
410 stbuf->st_size = wim_resource_size(lte);
416 stbuf->st_atime = wim_timestamp_to_unix(inode->i_last_access_time);
417 stbuf->st_mtime = wim_timestamp_to_unix(inode->i_last_write_time);
418 stbuf->st_ctime = wim_timestamp_to_unix(inode->i_creation_time);
419 stbuf->st_blocks = (stbuf->st_size + 511) / 512;
423 /* Creates a new staging file and returns its file descriptor opened for
426 * @name_ret: A location into which the a pointer to the newly allocated name of
427 * the staging file is stored.
429 * @ctx: Context for the WIM filesystem; this provides the name of the
432 * On success, returns the file descriptor for the staging file, opened for
433 * writing. On failure, returns -1 and sets errno.
435 static int create_staging_file(char **name_ret, struct wimfs_context *ctx)
443 static const size_t STAGING_FILE_NAME_LEN = 20;
445 name_len = ctx->staging_dir_name_len + 1 + STAGING_FILE_NAME_LEN;
446 name = MALLOC(name_len + 1);
454 memcpy(name, ctx->staging_dir_name, ctx->staging_dir_name_len);
455 name[ctx->staging_dir_name_len] = '/';
456 randomize_char_array_with_alnum(name + ctx->staging_dir_name_len + 1,
457 STAGING_FILE_NAME_LEN);
458 name[name_len] = '\0';
461 /* Just in case, verify that the randomly generated name doesn't name an
462 * existing file, and try again if so */
463 } while (stat(name, &stbuf) == 0);
465 if (errno != ENOENT) /* other error?! */
468 /* doesn't exist--- ok */
470 DEBUG("Creating staging file `%s'", name);
472 fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0600);
484 * Extract a WIM resource to the staging directory.
486 * @inode: Inode that contains the stream we are extracting
488 * @stream_id: Identifier for the stream (it stays constant even if the indices
489 * of the stream entries are changed)
491 * @lte: Pointer to pointer to the lookup table entry for the stream we need to
492 * extract, or NULL if there was no lookup table entry present for the stream
494 * @size: Number of bytes of the stream we want to extract (this supports the
495 * wimfs_truncate() function). It may be more than the actual stream length, in
496 * which case the extra space is filled with zeroes.
498 * @ctx: Context for the WIM filesystem.
500 * Returns 0 on success or a negative error code on failure.
502 static int extract_resource_to_staging_dir(struct wim_inode *inode,
504 struct wim_lookup_table_entry **lte,
506 struct wimfs_context *ctx)
508 char *staging_file_name;
511 struct wim_lookup_table_entry *old_lte, *new_lte;
514 DEBUG("Extracting resource to staging dir: inode %"PRIu64", "
515 "stream id %"PRIu32, inode->i_ino, stream_id);
519 wimlib_assert(old_lte == NULL ||
520 old_lte->resource_location != RESOURCE_IN_STAGING_FILE);
522 /* Create the staging file */
523 fd = create_staging_file(&staging_file_name, ctx);
527 /* Extract the stream to the staging file (possibly truncated) */
529 extract_size = min(wim_resource_size(old_lte), size);
530 ret = extract_wim_resource_to_fd(old_lte, fd, extract_size);
536 /* In the case of truncate() to more than the file length, extend the
537 * file with zeroes by calling ftruncate() on the underlying staging
539 if (ret == 0 && size > extract_size)
540 ret = ftruncate(fd, size);
542 /* Close the staging file descriptor and check for errors. If there's
543 * an error, unlink the staging file. */
544 if (ret != 0 || close(fd) != 0) {
550 goto out_delete_staging_file;
553 /* Now deal with the lookup table entries. We may be able to re-use the
554 * existing entry, but we may have to create a new one instead. */
556 if (old_lte && inode->i_nlink == old_lte->refcnt) {
557 /* The reference count of the existing lookup table entry is the
558 * same as the link count of the inode that contains the stream
559 * we're opening. Therefore, ALL the references to the lookup
560 * table entry correspond to the stream we're trying to extract,
561 * so the lookup table entry can be re-used. */
562 DEBUG("Re-using lookup table entry");
563 lookup_table_unlink(ctx->wim->lookup_table, old_lte);
567 /* There's an existing lookup table entry, but its
568 * reference count is greater than the link count for
569 * the inode containing a stream we're opening.
570 * Therefore, we need to split the lookup table entry.
572 wimlib_assert(old_lte->refcnt > inode->i_nlink);
573 DEBUG("Splitting lookup table entry "
574 "(inode->i_nlink = %u, old_lte->refcnt = %u)",
575 inode->i_nlink, old_lte->refcnt);
578 new_lte = new_lookup_table_entry();
581 goto out_delete_staging_file;
584 /* There may already be open file descriptors to this stream if
585 * it's previously been opened read-only, but just now we're
586 * opening it read-write. Identify those file descriptors and
587 * change their lookup table entry pointers to point to the new
588 * lookup table entry, and open staging file descriptors for
591 * At the same time, we need to count the number of these opened
592 * file descriptors to the new lookup table entry. If there's
593 * an old lookup table entry, this number needs to be subtracted
594 * from the fd's opened to the old entry. */
595 for (u16 i = 0, j = 0; j < inode->i_num_opened_fds; i++) {
596 struct wimfs_fd *fd = inode->i_fds[i];
598 if (fd->stream_id == stream_id) {
599 wimlib_assert(fd->f_lte == old_lte);
600 wimlib_assert(fd->staging_fd == -1);
602 new_lte->num_opened_fds++;
603 fd->staging_fd = open(staging_file_name, O_RDONLY);
604 if (fd->staging_fd == -1) {
606 goto out_revert_fd_changes;
612 DEBUG("%hu fd's were already opened to the file we extracted",
613 new_lte->num_opened_fds);
615 old_lte->num_opened_fds -= new_lte->num_opened_fds;
616 old_lte->refcnt -= inode->i_nlink;
620 new_lte->refcnt = inode->i_nlink;
621 new_lte->resource_location = RESOURCE_IN_STAGING_FILE;
622 new_lte->staging_file_name = staging_file_name;
623 new_lte->lte_inode = inode;
624 random_hash(new_lte->hash);
627 inode->i_lte = new_lte;
629 for (u16 i = 0; i < inode->i_num_ads; i++)
630 if (inode->i_ads_entries[i].stream_id == stream_id)
631 inode->i_ads_entries[i].lte = new_lte;
633 lookup_table_insert(ctx->wim->lookup_table, new_lte);
634 list_add(&new_lte->staging_list, &ctx->staging_list);
637 out_revert_fd_changes:
638 for (u16 i = 0, j = 0; j < new_lte->num_opened_fds; i++) {
639 struct wimfs_fd *fd = inode->i_fds[i];
640 if (fd && fd->stream_id == stream_id && fd->f_lte == new_lte) {
642 if (fd->staging_fd != -1) {
643 close(fd->staging_fd);
649 free_lookup_table_entry(new_lte);
650 out_delete_staging_file:
651 unlink(staging_file_name);
652 FREE(staging_file_name);
657 * Creates a randomly named staging directory and saves its name in the
658 * filesystem context structure.
660 static int make_staging_dir(struct wimfs_context *ctx, const char *user_prefix)
662 static const size_t random_suffix_len = 10;
663 static const char *common_suffix = ".staging";
664 static const size_t common_suffix_len = 8;
666 char *staging_dir_name = NULL;
667 size_t staging_dir_name_len;
669 const char *wim_basename;
670 char *real_user_prefix = NULL;
674 real_user_prefix = realpath(user_prefix, NULL);
675 if (!real_user_prefix) {
676 ERROR_WITH_ERRNO("Could not resolve `%s'",
678 ret = WIMLIB_ERR_NOTDIR;
681 wim_basename = path_basename(ctx->wim->filename);
682 prefix_len = strlen(real_user_prefix) + 1 + strlen(wim_basename);
684 prefix_len = strlen(ctx->wim->filename);
687 staging_dir_name_len = prefix_len + common_suffix_len + random_suffix_len;
689 staging_dir_name = MALLOC(staging_dir_name_len + 1);
690 if (!staging_dir_name) {
691 ret = WIMLIB_ERR_NOMEM;
695 if (real_user_prefix)
696 sprintf(staging_dir_name, "%s/%s", real_user_prefix, wim_basename);
698 strcpy(staging_dir_name, ctx->wim->filename);
700 strcat(staging_dir_name, common_suffix);
702 randomize_char_array_with_alnum(staging_dir_name + prefix_len + common_suffix_len,
705 staging_dir_name[staging_dir_name_len] = '\0';
707 if (mkdir(staging_dir_name, 0700) != 0) {
708 ERROR_WITH_ERRNO("Failed to create temporary directory `%s'",
710 ret = WIMLIB_ERR_MKDIR;
715 FREE(real_user_prefix);
717 ctx->staging_dir_name = staging_dir_name;
718 ctx->staging_dir_name_len = staging_dir_name_len;
720 FREE(staging_dir_name);
725 static int remove_file_or_directory(const char *fpath, const struct stat *sb,
726 int typeflag, struct FTW *ftwbuf)
728 if (remove(fpath) == 0)
731 ERROR_WITH_ERRNO("Cannot remove `%s'", fpath);
732 return WIMLIB_ERR_DELETE_STAGING_DIR;
737 * Deletes the staging directory and all the files contained in it.
739 static int delete_staging_dir(struct wimfs_context *ctx)
742 ret = nftw(ctx->staging_dir_name, remove_file_or_directory,
744 FREE(ctx->staging_dir_name);
745 ctx->staging_dir_name = NULL;
749 static void inode_update_lte_ptr(struct wim_inode *inode,
750 struct wim_lookup_table_entry *old_lte,
751 struct wim_lookup_table_entry *new_lte)
753 if (inode->i_lte == old_lte) {
754 inode->i_lte = new_lte;
756 for (unsigned i = 0; i < inode->i_num_ads; i++) {
757 if (inode->i_ads_entries[i].lte == old_lte) {
758 inode->i_ads_entries[i].lte = new_lte;
765 static int update_lte_of_staging_file(struct wim_lookup_table_entry *lte,
766 struct wim_lookup_table *table)
768 struct wim_lookup_table_entry *duplicate_lte;
770 u8 hash[SHA1_HASH_SIZE];
773 ret = sha1sum(lte->staging_file_name, hash);
776 lookup_table_unlink(table, lte);
777 duplicate_lte = __lookup_resource(table, hash);
779 /* Merge duplicate lookup table entries */
780 duplicate_lte->refcnt += lte->refcnt;
781 inode_update_lte_ptr(lte->lte_inode, lte, duplicate_lte);
782 free_lookup_table_entry(lte);
784 if (stat(lte->staging_file_name, &stbuf) != 0) {
785 ERROR_WITH_ERRNO("Failed to stat `%s'", lte->staging_file_name);
786 return WIMLIB_ERR_STAT;
788 if (stbuf.st_size == 0) {
789 /* Zero-length stream. No lookup table entry needed. */
790 inode_update_lte_ptr(lte->lte_inode, lte, NULL);
791 free_lookup_table_entry(lte);
793 BUILD_BUG_ON(<e->file_on_disk != <e->staging_file_name);
794 lte->resource_entry.original_size = stbuf.st_size;
795 lte->resource_entry.size = stbuf.st_size;
796 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
797 lte->file_on_disk_fp = NULL;
798 copy_hash(lte->hash, hash);
799 lookup_table_insert(table, lte);
805 static int inode_close_fds(struct wim_inode *inode)
807 u16 num_opened_fds = inode->i_num_opened_fds;
808 for (u16 i = 0, j = 0; j < num_opened_fds; i++) {
809 struct wimfs_fd *fd = inode->i_fds[i];
811 wimlib_assert(fd->f_inode == inode);
812 int ret = close_wimfs_fd(fd);
821 /* Overwrites the WIM file, with changes saved. */
822 static int rebuild_wim(struct wimfs_context *ctx, int write_flags,
823 wimlib_progress_func_t progress_func)
826 struct wim_lookup_table_entry *lte, *tmp;
827 WIMStruct *w = ctx->wim;
829 DEBUG("Closing all staging file descriptors.");
830 list_for_each_entry_safe(lte, tmp, &ctx->staging_list, staging_list) {
831 ret = inode_close_fds(lte->lte_inode);
836 DEBUG("Calculating SHA1 checksums for all new staging files.");
837 list_for_each_entry_safe(lte, tmp, &ctx->staging_list, staging_list) {
838 ret = update_lte_of_staging_file(lte, w->lookup_table);
843 xml_update_image_info(w, w->current_image);
844 ret = wimlib_overwrite(w, write_flags, 0, progress_func);
846 ERROR("Failed to commit changes to mounted WIM image");
852 /* Simple function that returns the concatenation of 2 strings. */
853 static char *strcat_dup(const char *s1, const char *s2, size_t max_len)
855 size_t len = strlen(s1) + strlen(s2);
858 char *p = MALLOC(len + 1);
861 snprintf(p, len + 1, "%s%s", s1, s2);
865 static int set_message_queue_names(struct wimfs_context *ctx,
866 const char *mount_dir)
868 static const char *u2d_prefix = "/wimlib-unmount-to-daemon-mq";
869 static const char *d2u_prefix = "/wimlib-daemon-to-unmount-mq";
874 dir_path = realpath(mount_dir, NULL);
876 ERROR_WITH_ERRNO("Failed to resolve path \"%s\"", mount_dir);
878 return WIMLIB_ERR_NOMEM;
880 return WIMLIB_ERR_NOTDIR;
883 for (p = dir_path; *p; p++)
887 ctx->unmount_to_daemon_mq_name = strcat_dup(u2d_prefix, dir_path,
889 if (!ctx->unmount_to_daemon_mq_name) {
890 ret = WIMLIB_ERR_NOMEM;
891 goto out_free_dir_path;
893 ctx->daemon_to_unmount_mq_name = strcat_dup(d2u_prefix, dir_path,
895 if (!ctx->daemon_to_unmount_mq_name) {
896 ret = WIMLIB_ERR_NOMEM;
897 goto out_free_unmount_to_daemon_mq_name;
901 goto out_free_dir_path;
902 out_free_unmount_to_daemon_mq_name:
903 FREE(ctx->unmount_to_daemon_mq_name);
904 ctx->unmount_to_daemon_mq_name = NULL;
910 static void free_message_queue_names(struct wimfs_context *ctx)
912 FREE(ctx->unmount_to_daemon_mq_name);
913 FREE(ctx->daemon_to_unmount_mq_name);
914 ctx->unmount_to_daemon_mq_name = NULL;
915 ctx->daemon_to_unmount_mq_name = NULL;
919 * Opens two POSIX message queue: one for sending messages from the unmount
920 * process to the daemon process, and one to go the other way. The names of the
921 * message queues, which must be system-wide unique, are be based on the mount
924 * @daemon specifies whether the calling process is the filesystem daemon or the
927 static int open_message_queues(struct wimfs_context *ctx, bool daemon)
929 int unmount_to_daemon_mq_flags = O_WRONLY | O_CREAT;
930 int daemon_to_unmount_mq_flags = O_RDONLY | O_CREAT;
936 swap(unmount_to_daemon_mq_flags, daemon_to_unmount_mq_flags);
942 orig_umask = umask(0000);
943 DEBUG("Opening message queue \"%s\"", ctx->unmount_to_daemon_mq_name);
944 ctx->unmount_to_daemon_mq = mq_open(ctx->unmount_to_daemon_mq_name,
945 unmount_to_daemon_mq_flags, mode, NULL);
947 if (ctx->unmount_to_daemon_mq == (mqd_t)-1) {
948 ERROR_WITH_ERRNO("mq_open()");
949 ret = WIMLIB_ERR_MQUEUE;
953 DEBUG("Opening message queue \"%s\"", ctx->daemon_to_unmount_mq_name);
954 ctx->daemon_to_unmount_mq = mq_open(ctx->daemon_to_unmount_mq_name,
955 daemon_to_unmount_mq_flags, mode, NULL);
957 if (ctx->daemon_to_unmount_mq == (mqd_t)-1) {
958 ERROR_WITH_ERRNO("mq_open()");
959 mq_close(ctx->unmount_to_daemon_mq);
960 mq_unlink(ctx->unmount_to_daemon_mq_name);
961 ctx->unmount_to_daemon_mq = (mqd_t)-1;
962 ret = WIMLIB_ERR_MQUEUE;
971 /* Try to determine the maximum message size of a message queue. The return
972 * value is the maximum message size, or a guess of 8192 bytes if it cannot be
974 static long mq_get_msgsize(mqd_t mq)
976 static const char *msgsize_max_file = "/proc/sys/fs/mqueue/msgsize_max";
981 if (mq_getattr(mq, &attr) == 0) {
982 msgsize = attr.mq_msgsize;
984 ERROR_WITH_ERRNO("mq_getattr()");
985 ERROR("Attempting to read %s", msgsize_max_file);
986 fp = fopen(msgsize_max_file, "rb");
988 if (fscanf(fp, "%ld", &msgsize) != 1) {
989 ERROR("Assuming message size of 8192");
994 ERROR_WITH_ERRNO("Failed to open the file `%s'",
996 ERROR("Assuming message size of 8192");
1003 static int get_mailbox(mqd_t mq, long needed_msgsize, long *msgsize_ret,
1009 msgsize = mq_get_msgsize(mq);
1011 if (msgsize < needed_msgsize) {
1012 ERROR("Message queue max size must be at least %ld!",
1014 return WIMLIB_ERR_MQUEUE;
1017 mailbox = MALLOC(msgsize);
1019 ERROR("Failed to allocate %ld bytes for mailbox", msgsize);
1020 return WIMLIB_ERR_NOMEM;
1022 *msgsize_ret = msgsize;
1023 *mailbox_ret = mailbox;
1027 static void unlink_message_queues(struct wimfs_context *ctx)
1029 mq_unlink(ctx->unmount_to_daemon_mq_name);
1030 mq_unlink(ctx->daemon_to_unmount_mq_name);
1033 /* Closes the message queues, which are allocated in static variables */
1034 static void close_message_queues(struct wimfs_context *ctx)
1036 DEBUG("Closing message queues");
1037 mq_close(ctx->unmount_to_daemon_mq);
1038 ctx->unmount_to_daemon_mq = (mqd_t)(-1);
1039 mq_close(ctx->daemon_to_unmount_mq);
1040 ctx->daemon_to_unmount_mq = (mqd_t)(-1);
1041 unlink_message_queues(ctx);
1045 struct unmount_msg_hdr {
1052 struct msg_unmount_request {
1053 struct unmount_msg_hdr hdr;
1055 u8 want_progress_messages;
1058 struct msg_daemon_info {
1059 struct unmount_msg_hdr hdr;
1064 struct msg_unmount_finished {
1065 struct unmount_msg_hdr hdr;
1069 struct msg_write_streams_progress {
1070 struct unmount_msg_hdr hdr;
1071 union wimlib_progress_info info;
1075 MSG_TYPE_UNMOUNT_REQUEST,
1076 MSG_TYPE_DAEMON_INFO,
1077 MSG_TYPE_WRITE_STREAMS_PROGRESS,
1078 MSG_TYPE_UNMOUNT_FINISHED,
1082 struct msg_handler_context_hdr {
1083 int timeout_seconds;
1086 struct unmount_msg_handler_context {
1087 struct msg_handler_context_hdr hdr;
1091 wimlib_progress_func_t progress_func;
1094 struct daemon_msg_handler_context {
1095 struct msg_handler_context_hdr hdr;
1096 struct wimfs_context *wimfs_ctx;
1099 static int send_unmount_request_msg(mqd_t mq, int unmount_flags,
1100 u8 want_progress_messages)
1102 DEBUG("Sending unmount request msg");
1103 struct msg_unmount_request msg = {
1105 .min_version = WIMLIB_MAKEVERSION(1, 2, 1),
1106 .cur_version = WIMLIB_VERSION_CODE,
1107 .msg_type = MSG_TYPE_UNMOUNT_REQUEST,
1108 .msg_size = sizeof(msg),
1110 .unmount_flags = unmount_flags,
1111 .want_progress_messages = want_progress_messages,
1114 if (mq_send(mq, (void*)&msg, sizeof(msg), 1)) {
1115 ERROR_WITH_ERRNO("Failed to communicate with filesystem daemon");
1116 return WIMLIB_ERR_MQUEUE;
1121 static int send_daemon_info_msg(mqd_t mq, pid_t pid, int mount_flags)
1123 DEBUG("Sending daemon info msg (pid = %d, mount_flags=%x)",
1126 struct msg_daemon_info msg = {
1128 .min_version = WIMLIB_MAKEVERSION(1, 2, 1),
1129 .cur_version = WIMLIB_VERSION_CODE,
1130 .msg_type = MSG_TYPE_DAEMON_INFO,
1131 .msg_size = sizeof(msg),
1134 .mount_flags = mount_flags,
1136 if (mq_send(mq, (void*)&msg, sizeof(msg), 1)) {
1137 ERROR_WITH_ERRNO("Failed to send daemon info to unmount process");
1138 return WIMLIB_ERR_MQUEUE;
1143 static void send_unmount_finished_msg(mqd_t mq, int status)
1145 DEBUG("Sending unmount finished msg");
1146 struct msg_unmount_finished msg = {
1148 .min_version = WIMLIB_MAKEVERSION(1, 2, 1),
1149 .cur_version = WIMLIB_VERSION_CODE,
1150 .msg_type = MSG_TYPE_UNMOUNT_FINISHED,
1151 .msg_size = sizeof(msg),
1155 if (mq_send(mq, (void*)&msg, sizeof(msg), 1))
1156 ERROR_WITH_ERRNO("Failed to send status to unmount process");
1159 static int unmount_progress_func(enum wimlib_progress_msg msg,
1160 const union wimlib_progress_info *info)
1162 if (msg == WIMLIB_PROGRESS_MSG_WRITE_STREAMS) {
1163 struct msg_write_streams_progress msg = {
1165 .min_version = WIMLIB_MAKEVERSION(1, 2, 1),
1166 .cur_version = WIMLIB_VERSION_CODE,
1167 .msg_type = MSG_TYPE_WRITE_STREAMS_PROGRESS,
1168 .msg_size = sizeof(msg),
1172 if (mq_send(wimfs_get_context()->daemon_to_unmount_mq,
1173 (void*)&msg, sizeof(msg), 1))
1175 ERROR_WITH_ERRNO("Failed to send progress information "
1176 "to unmount process");
1182 static int msg_unmount_request_handler(const void *_msg, void *_handler_ctx)
1184 const struct msg_unmount_request *msg = _msg;
1185 struct daemon_msg_handler_context *handler_ctx = _handler_ctx;
1186 struct wimfs_context *wimfs_ctx;
1190 wimlib_progress_func_t progress_func;
1192 DEBUG("Handling unmount request msg");
1194 wimfs_ctx = handler_ctx->wimfs_ctx;
1195 if (msg->hdr.msg_size < sizeof(*msg)) {
1196 status = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1200 unmount_flags = msg->unmount_flags;
1201 if (msg->want_progress_messages)
1202 progress_func = unmount_progress_func;
1204 progress_func = NULL;
1206 ret = send_daemon_info_msg(wimfs_ctx->daemon_to_unmount_mq, getpid(),
1207 wimfs_ctx->mount_flags);
1213 if (wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1214 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_COMMIT) {
1215 int write_flags = 0;
1216 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY)
1217 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1218 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_REBUILD)
1219 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1220 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_RECOMPRESS)
1221 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
1222 status = rebuild_wim(wimfs_ctx, write_flags,
1226 DEBUG("Read-only mount");
1231 if (wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1232 ret = delete_staging_dir(wimfs_ctx);
1234 ERROR("Failed to delete the staging directory");
1239 send_unmount_finished_msg(wimfs_ctx->daemon_to_unmount_mq, status);
1240 return MSG_BREAK_LOOP;
1243 static int msg_daemon_info_handler(const void *_msg, void *_handler_ctx)
1245 const struct msg_daemon_info *msg = _msg;
1246 struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1248 DEBUG("Handling daemon info msg");
1249 if (msg->hdr.msg_size < sizeof(*msg))
1250 return WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1251 handler_ctx->daemon_pid = msg->daemon_pid;
1252 handler_ctx->mount_flags = msg->mount_flags;
1253 handler_ctx->hdr.timeout_seconds = 1;
1254 DEBUG("pid of daemon is %d; mount flags were %#x",
1255 handler_ctx->daemon_pid,
1256 handler_ctx->mount_flags);
1260 static int msg_write_streams_progress_handler(const void *_msg,
1263 const struct msg_write_streams_progress *msg = _msg;
1264 struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1266 if (msg->hdr.msg_size < sizeof(*msg))
1267 return WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1268 if (handler_ctx->progress_func) {
1269 handler_ctx->progress_func(WIMLIB_PROGRESS_MSG_WRITE_STREAMS,
1275 static int msg_unmount_finished_handler(const void *_msg, void *_handler_ctx)
1277 const struct msg_unmount_finished *msg = _msg;
1278 struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1280 DEBUG("Handling unmount finished message");
1281 if (msg->hdr.msg_size < sizeof(*msg))
1282 return WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1283 handler_ctx->status = msg->status;
1284 DEBUG("status is %d", handler_ctx->status);
1285 return MSG_BREAK_LOOP;
1288 static int unmount_timed_out_cb(void *_handler_ctx)
1290 struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1292 if (handler_ctx->daemon_pid == 0) {
1295 kill(handler_ctx->daemon_pid, 0);
1296 if (errno == ESRCH) {
1299 DEBUG("Filesystem daemon is still alive... "
1300 "Waiting another %d seconds\n",
1301 handler_ctx->hdr.timeout_seconds);
1306 ERROR("The filesystem daemon has crashed! Changes to the "
1307 "WIM may not have been commited.");
1308 return WIMLIB_ERR_FILESYSTEM_DAEMON_CRASHED;
1311 static int daemon_timed_out_cb(void *_handler_ctx)
1313 ERROR("Timed out waiting for unmount request! "
1314 "Changes to the mounted WIM will not be committed.");
1315 return WIMLIB_ERR_TIMEOUT;
1318 typedef int (*msg_handler_t)(const void *_msg, void *_handler_ctx);
1320 struct msg_handler_callbacks {
1321 int (*timed_out)(void * _handler_ctx);
1322 msg_handler_t msg_handlers[MSG_TYPE_MAX];
1325 static const struct msg_handler_callbacks unmount_msg_handler_callbacks = {
1326 .timed_out = unmount_timed_out_cb,
1328 [MSG_TYPE_DAEMON_INFO] = msg_daemon_info_handler,
1329 [MSG_TYPE_WRITE_STREAMS_PROGRESS] = msg_write_streams_progress_handler,
1330 [MSG_TYPE_UNMOUNT_FINISHED] = msg_unmount_finished_handler,
1334 static const struct msg_handler_callbacks daemon_msg_handler_callbacks = {
1335 .timed_out = daemon_timed_out_cb,
1337 [MSG_TYPE_UNMOUNT_REQUEST] = msg_unmount_request_handler,
1341 static int receive_message(mqd_t mq,
1342 struct msg_handler_context_hdr *handler_ctx,
1343 const msg_handler_t msg_handlers[],
1344 long mailbox_size, void *mailbox)
1347 struct timespec timeout;
1348 ssize_t bytes_received;
1349 struct unmount_msg_hdr *hdr;
1352 gettimeofday(&now, NULL);
1353 timeout.tv_sec = now.tv_sec + handler_ctx->timeout_seconds;
1354 timeout.tv_nsec = now.tv_usec * 1000;
1356 bytes_received = mq_timedreceive(mq, mailbox,
1357 mailbox_size, NULL, &timeout);
1359 if (bytes_received == -1) {
1360 if (errno == ETIMEDOUT) {
1361 ret = WIMLIB_ERR_TIMEOUT;
1363 ERROR_WITH_ERRNO("mq_timedreceive()");
1364 ret = WIMLIB_ERR_MQUEUE;
1366 } else if (bytes_received < sizeof(*hdr) ||
1367 bytes_received != hdr->msg_size) {
1368 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1369 } else if (WIMLIB_VERSION_CODE < hdr->min_version) {
1370 /*ERROR("Cannot understand the received message. "*/
1371 /*"Please upgrade wimlib to at least v%d.%d.%d",*/
1372 /*WIMLIB_GET_MAJOR_VERSION(hdr->min_version),*/
1373 /*WIMLIB_GET_MINOR_VERSION(hdr->min_version),*/
1374 /*WIMLIB_GET_PATCH_VERSION(hdr->min_version));*/
1375 ret = MSG_VERSION_TOO_HIGH;
1376 } else if (hdr->msg_type >= MSG_TYPE_MAX) {
1377 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1378 } else if (msg_handlers[hdr->msg_type] == NULL) {
1379 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1381 ret = msg_handlers[hdr->msg_type](mailbox, handler_ctx);
1386 static int message_loop(mqd_t mq,
1387 const struct msg_handler_callbacks *callbacks,
1388 struct msg_handler_context_hdr *handler_ctx)
1390 static const size_t MAX_MSG_SIZE = 512;
1395 DEBUG("Entering message loop");
1397 ret = get_mailbox(mq, MAX_MSG_SIZE, &msgsize, &mailbox);
1401 ret = receive_message(mq, handler_ctx,
1402 callbacks->msg_handlers,
1404 if (ret == 0 || ret == MSG_VERSION_TOO_HIGH) {
1406 } else if (ret == MSG_BREAK_LOOP) {
1409 } else if (ret == WIMLIB_ERR_TIMEOUT) {
1410 if (callbacks->timed_out)
1411 ret = callbacks->timed_out(handler_ctx);
1417 ERROR_WITH_ERRNO("Error communicating with "
1418 "filesystem daemon");
1423 DEBUG("Exiting message loop");
1427 /* Execute `fusermount -u', which is installed setuid root, to unmount the WIM.
1429 * FUSE does not yet implement synchronous unmounts. This means that fusermount
1430 * -u will return before the filesystem daemon returns from wimfs_destroy().
1431 * This is partly what we want, because we need to send a message from this
1432 * process to the filesystem daemon telling whether --commit was specified or
1433 * not. However, after that, the unmount process must wait for the filesystem
1434 * daemon to finish writing the WIM file.
1436 static int execute_fusermount(const char *dir)
1444 ERROR_WITH_ERRNO("Failed to fork()");
1445 return WIMLIB_ERR_FORK;
1449 execlp("fusermount", "fusermount", "-u", dir, NULL);
1450 ERROR_WITH_ERRNO("Failed to execute `fusermount'");
1451 exit(WIMLIB_ERR_FUSERMOUNT);
1455 ret = waitpid(pid, &status, 0);
1457 ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
1459 return WIMLIB_ERR_FUSERMOUNT;
1462 if (!WIFEXITED(status)) {
1463 ERROR("'fusermount' did not terminate normally!");
1464 return WIMLIB_ERR_FUSERMOUNT;
1467 status = WEXITSTATUS(status);
1472 if (status != WIMLIB_ERR_FUSERMOUNT)
1473 return WIMLIB_ERR_FUSERMOUNT;
1475 /* Try again, but with the `umount' program. This is required on other
1476 * FUSE implementations such as FreeBSD's that do not have a
1477 * `fusermount' program. */
1478 ERROR("Falling back to 'umount'. Note: you may need to be "
1479 "root for this to work");
1482 ERROR_WITH_ERRNO("Failed to fork()");
1483 return WIMLIB_ERR_FORK;
1487 execlp("umount", "umount", dir, NULL);
1488 ERROR_WITH_ERRNO("Failed to execute `umount'");
1489 exit(WIMLIB_ERR_FUSERMOUNT);
1493 ret = waitpid(pid, &status, 0);
1495 ERROR_WITH_ERRNO("Failed to wait for `umount' process to "
1497 return WIMLIB_ERR_FUSERMOUNT;
1500 ERROR("`umount' did not successfully complete");
1501 return WIMLIB_ERR_FUSERMOUNT;
1507 static int wimfs_access(const char *path, int mask)
1513 static int wimfs_chmod(const char *path, mode_t mask)
1515 struct wim_dentry *dentry;
1516 struct wimfs_context *ctx = wimfs_get_context();
1519 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA))
1522 ret = lookup_resource(ctx->wim, path, LOOKUP_FLAG_DIRECTORY_OK,
1523 &dentry, NULL, NULL);
1527 ret = inode_set_unix_data(dentry->d_inode, ctx->default_uid,
1528 ctx->default_gid, mask,
1529 ctx->wim->lookup_table, UNIX_DATA_MODE);
1530 return ret ? -ENOMEM : 0;
1533 static int wimfs_chown(const char *path, uid_t uid, gid_t gid)
1535 struct wim_dentry *dentry;
1536 struct wimfs_context *ctx = wimfs_get_context();
1539 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA))
1542 ret = lookup_resource(ctx->wim, path, LOOKUP_FLAG_DIRECTORY_OK,
1543 &dentry, NULL, NULL);
1547 ret = inode_set_unix_data(dentry->d_inode, uid, gid,
1548 inode_default_unix_mode(dentry->d_inode),
1549 ctx->wim->lookup_table,
1550 UNIX_DATA_UID | UNIX_DATA_GID);
1551 return ret ? -ENOMEM : 0;
1554 /* Called when the filesystem is unmounted. */
1555 static void wimfs_destroy(void *p)
1557 struct wimfs_context *wimfs_ctx = wimfs_get_context();
1558 if (open_message_queues(wimfs_ctx, true) == 0) {
1559 struct daemon_msg_handler_context handler_ctx = {
1561 .timeout_seconds = 5,
1563 .wimfs_ctx = wimfs_ctx,
1565 message_loop(wimfs_ctx->unmount_to_daemon_mq,
1566 &daemon_msg_handler_callbacks,
1568 close_message_queues(wimfs_ctx);
1573 static int wimfs_fallocate(const char *path, int mode,
1574 off_t offset, off_t len, struct fuse_file_info *fi)
1576 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1577 wimlib_assert(fd->staging_fd != -1);
1578 return fallocate(fd->staging_fd, mode, offset, len);
1583 static int wimfs_fgetattr(const char *path, struct stat *stbuf,
1584 struct fuse_file_info *fi)
1586 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1587 return inode_to_stbuf(fd->f_inode, fd->f_lte, stbuf);
1590 static int wimfs_ftruncate(const char *path, off_t size,
1591 struct fuse_file_info *fi)
1593 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1594 int ret = ftruncate(fd->staging_fd, size);
1597 if (fd->f_lte && size < fd->f_lte->resource_entry.original_size)
1598 fd->f_lte->resource_entry.original_size = size;
1603 * Fills in a `struct stat' that corresponds to a file or directory in the WIM.
1605 static int wimfs_getattr(const char *path, struct stat *stbuf)
1607 struct wim_dentry *dentry;
1608 struct wim_lookup_table_entry *lte;
1610 struct wimfs_context *ctx = wimfs_get_context();
1612 ret = lookup_resource(ctx->wim, path,
1613 get_lookup_flags(ctx) | LOOKUP_FLAG_DIRECTORY_OK,
1614 &dentry, <e, NULL);
1617 return inode_to_stbuf(dentry->d_inode, lte, stbuf);
1621 /* Read an alternate data stream through the XATTR interface, or get its size */
1622 static int wimfs_getxattr(const char *path, const char *name, char *value,
1626 struct wim_inode *inode;
1627 struct wim_ads_entry *ads_entry;
1629 struct wim_lookup_table_entry *lte;
1630 struct wimfs_context *ctx = wimfs_get_context();
1632 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1635 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1639 inode = wim_pathname_to_inode(ctx->wim, path);
1643 ads_entry = inode_get_ads_entry(inode, name, NULL);
1647 lte = ads_entry->lte;
1648 res_size = wim_resource_size(lte);
1653 if (res_size > size)
1656 ret = read_full_wim_resource(lte, (u8*)value,
1657 WIMLIB_RESOURCE_FLAG_MULTITHREADED);
1665 /* Create a hard link */
1666 static int wimfs_link(const char *to, const char *from)
1668 struct wim_dentry *from_dentry, *from_dentry_parent;
1669 const char *link_name;
1670 struct wim_inode *inode;
1671 struct wim_lookup_table_entry *lte;
1672 WIMStruct *w = wimfs_get_WIMStruct();
1675 inode = wim_pathname_to_inode(w, to);
1679 if (inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
1680 FILE_ATTRIBUTE_REPARSE_POINT))
1683 from_dentry_parent = get_parent_dentry(w, from);
1684 if (!from_dentry_parent)
1686 if (!dentry_is_directory(from_dentry_parent))
1689 link_name = path_basename(from);
1690 if (get_dentry_child_with_name(from_dentry_parent, link_name))
1692 from_dentry = new_dentry(link_name);
1696 inode_add_dentry(from_dentry, inode);
1697 from_dentry->d_inode = inode;
1700 for (i = 0; i <= inode->i_num_ads; i++) {
1701 lte = inode_stream_lte_resolved(inode, i);
1705 dentry_add_child(from_dentry_parent, from_dentry);
1710 static int wimfs_listxattr(const char *path, char *list, size_t size)
1713 struct wim_inode *inode;
1714 struct wimfs_context *ctx = wimfs_get_context();
1718 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1721 /* List alternate data streams, or get the list size */
1723 inode = wim_pathname_to_inode(ctx->wim, path);
1729 for (i = 0; i < inode->i_num_ads; i++)
1730 needed_size += inode->i_ads_entries[i].stream_name_utf8_len + 6;
1734 for (i = 0; i < inode->i_num_ads; i++) {
1735 needed_size = inode->i_ads_entries[i].stream_name_utf8_len + 6;
1736 if (needed_size > size)
1738 p += sprintf(p, "user.%s",
1739 inode->i_ads_entries[i].stream_name_utf8) + 1;
1740 size -= needed_size;
1748 /* Create a directory in the WIM image. */
1749 static int wimfs_mkdir(const char *path, mode_t mode)
1751 return create_dentry(fuse_get_context(), path, mode | S_IFDIR,
1752 FILE_ATTRIBUTE_DIRECTORY, NULL);
1755 /* Create a regular file or alternate data stream in the WIM image. */
1756 static int wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
1758 const char *stream_name;
1759 struct fuse_context *fuse_ctx = fuse_get_context();
1760 struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
1765 if ((wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
1766 && (stream_name = path_stream_name(path))) {
1767 /* Make an alternate data stream */
1768 struct wim_ads_entry *new_entry;
1769 struct wim_inode *inode;
1771 char *p = (char*)stream_name - 1;
1772 wimlib_assert(*p == ':');
1775 inode = wim_pathname_to_inode(wimfs_ctx->wim, path);
1778 if (inode->i_attributes &
1779 (FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_DIRECTORY))
1781 if (inode_get_ads_entry(inode, stream_name, NULL))
1783 new_entry = inode_add_ads(inode, stream_name);
1788 /* Make a normal file (not an alternate data stream) */
1789 return create_dentry(fuse_ctx, path, mode | S_IFREG,
1790 FILE_ATTRIBUTE_NORMAL, NULL);
1796 static int wimfs_open(const char *path, struct fuse_file_info *fi)
1798 struct wim_dentry *dentry;
1799 struct wim_lookup_table_entry *lte;
1801 struct wimfs_fd *fd;
1802 struct wim_inode *inode;
1805 struct wimfs_context *ctx = wimfs_get_context();
1807 ret = lookup_resource(ctx->wim, path, get_lookup_flags(ctx),
1808 &dentry, <e, &stream_idx);
1812 inode = dentry->d_inode;
1814 if (stream_idx == 0)
1817 stream_id = inode->i_ads_entries[stream_idx - 1].stream_id;
1819 /* The file resource may be in the staging directory (read-write mounts
1820 * only) or in the WIM. If it's in the staging directory, we need to
1821 * open a native file descriptor for the corresponding file. Otherwise,
1822 * we can read the file resource directly from the WIM file if we are
1823 * opening it read-only, but we need to extract the resource to the
1824 * staging directory if we are opening it writable. */
1826 if (flags_writable(fi->flags) &&
1827 (!lte || lte->resource_location != RESOURCE_IN_STAGING_FILE)) {
1828 u64 size = (lte) ? wim_resource_size(lte) : 0;
1829 ret = extract_resource_to_staging_dir(inode, stream_id,
1835 ret = alloc_wimfs_fd(inode, stream_id, lte, &fd,
1836 wimfs_ctx_readonly(ctx));
1840 if (lte && lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1841 fd->staging_fd = open(lte->staging_file_name, fi->flags);
1842 if (fd->staging_fd == -1) {
1843 int errno_save = errno;
1848 fi->fh = (uintptr_t)fd;
1852 /* Opens a directory. */
1853 static int wimfs_opendir(const char *path, struct fuse_file_info *fi)
1855 struct wim_inode *inode;
1857 struct wimfs_fd *fd = NULL;
1858 struct wimfs_context *ctx = wimfs_get_context();
1859 WIMStruct *w = ctx->wim;
1861 inode = wim_pathname_to_inode(w, path);
1864 if (!inode_is_directory(inode))
1866 ret = alloc_wimfs_fd(inode, 0, NULL, &fd, wimfs_ctx_readonly(ctx));
1867 fi->fh = (uintptr_t)fd;
1873 * Read data from a file in the WIM or in the staging directory.
1875 static int wimfs_read(const char *path, char *buf, size_t size,
1876 off_t offset, struct fuse_file_info *fi)
1878 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1884 if (!fd->f_lte) /* Empty stream with no lookup table entry */
1887 if (fd->f_lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1888 /* Read from staging file */
1890 wimlib_assert(fd->f_lte->staging_file_name);
1891 wimlib_assert(fd->staging_fd != -1);
1893 DEBUG("Seek to offset %"PRIu64, offset);
1895 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1897 ret = read(fd->staging_fd, buf, size);
1903 u64 res_size = wim_resource_size(fd->f_lte);
1904 if (offset > res_size)
1906 size = min(size, res_size - offset);
1907 if (read_wim_resource(fd->f_lte, (u8*)buf,
1909 WIMLIB_RESOURCE_FLAG_MULTITHREADED) != 0)
1915 struct fill_params {
1917 fuse_fill_dir_t filler;
1920 static int dentry_fuse_fill(struct wim_dentry *dentry, void *arg)
1922 struct fill_params *fill_params = arg;
1923 return fill_params->filler(fill_params->buf, dentry->file_name_utf8,
1927 /* Fills in the entries of the directory specified by @path using the
1928 * FUSE-provided function @filler. */
1929 static int wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
1930 off_t offset, struct fuse_file_info *fi)
1932 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1933 struct wim_inode *inode;
1938 inode = fd->f_inode;
1940 struct fill_params fill_params = {
1945 filler(buf, ".", NULL, 0);
1946 filler(buf, "..", NULL, 0);
1948 return for_dentry_in_rbtree(inode->i_children.rb_node,
1949 dentry_fuse_fill, &fill_params);
1953 static int wimfs_readlink(const char *path, char *buf, size_t buf_len)
1955 struct wimfs_context *ctx = wimfs_get_context();
1956 struct wim_inode *inode = wim_pathname_to_inode(ctx->wim, path);
1960 if (!inode_is_symlink(inode))
1963 ret = inode_readlink(inode, buf, buf_len, ctx->wim,
1964 WIMLIB_RESOURCE_FLAG_MULTITHREADED);
1971 static int wimfs_release(const char *path, struct fuse_file_info *fi)
1973 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1974 return close_wimfs_fd(fd);
1977 /* Close a directory */
1978 static int wimfs_releasedir(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);
1985 /* Remove an alternate data stream through the XATTR interface */
1986 static int wimfs_removexattr(const char *path, const char *name)
1988 struct wim_inode *inode;
1989 struct wim_ads_entry *ads_entry;
1991 struct wimfs_context *ctx = wimfs_get_context();
1993 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1996 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
2000 inode = wim_pathname_to_inode(ctx->wim, path);
2004 ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
2007 inode_remove_ads(inode, ads_idx, ctx->wim->lookup_table);
2012 /* Renames a file or directory. See rename (3) */
2013 static int wimfs_rename(const char *from, const char *to)
2015 struct wim_dentry *src;
2016 struct wim_dentry *dst;
2017 struct wim_dentry *parent_of_dst;
2018 WIMStruct *w = wimfs_get_WIMStruct();
2021 /* This rename() implementation currently only supports actual files
2022 * (not alternate data streams) */
2024 src = get_dentry(w, from);
2028 dst = get_dentry(w, to);
2031 /* Destination file exists */
2033 if (src == dst) /* Same file */
2036 if (!dentry_is_directory(src)) {
2037 /* Cannot rename non-directory to directory. */
2038 if (dentry_is_directory(dst))
2041 /* Cannot rename directory to a non-directory or a non-empty
2043 if (!dentry_is_directory(dst))
2045 if (inode_has_children(dst->d_inode))
2048 parent_of_dst = dst->parent;
2050 /* Destination does not exist */
2051 parent_of_dst = get_parent_dentry(w, to);
2055 if (!dentry_is_directory(parent_of_dst))
2059 ret = set_dentry_name(src, path_basename(to));
2063 remove_dentry(dst, w->lookup_table);
2065 dentry_add_child(parent_of_dst, src);
2069 /* Remove a directory */
2070 static int wimfs_rmdir(const char *path)
2072 struct wim_dentry *dentry;
2073 WIMStruct *w = wimfs_get_WIMStruct();
2075 dentry = get_dentry(w, path);
2079 if (!dentry_is_directory(dentry))
2082 if (dentry_has_children(dentry))
2085 remove_dentry(dentry, w->lookup_table);
2090 /* Write an alternate data stream through the XATTR interface */
2091 static int wimfs_setxattr(const char *path, const char *name,
2092 const char *value, size_t size, int flags)
2094 struct wim_ads_entry *existing_ads_entry;
2095 struct wim_inode *inode;
2097 struct wimfs_context *ctx = wimfs_get_context();
2100 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
2103 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
2107 inode = wim_pathname_to_inode(ctx->wim, path);
2111 existing_ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
2112 if (existing_ads_entry) {
2113 if (flags & XATTR_CREATE)
2115 inode_remove_ads(inode, ads_idx, ctx->wim->lookup_table);
2117 if (flags & XATTR_REPLACE)
2121 ret = inode_add_ads_with_data(inode, name, (const u8*)value,
2122 size, ctx->wim->lookup_table);
2123 return ret ? -ENOMEM : 0;
2127 static int wimfs_symlink(const char *to, const char *from)
2129 struct fuse_context *fuse_ctx = fuse_get_context();
2130 struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
2131 struct wim_dentry *dentry;
2134 ret = create_dentry(fuse_ctx, from, S_IFLNK | 0777,
2135 FILE_ATTRIBUTE_REPARSE_POINT, &dentry);
2137 dentry->d_inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
2138 if (inode_set_symlink(dentry->d_inode, to,
2139 wimfs_ctx->wim->lookup_table, NULL))
2141 remove_dentry(dentry, wimfs_ctx->wim->lookup_table);
2149 /* Reduce the size of a file */
2150 static int wimfs_truncate(const char *path, off_t size)
2152 struct wim_dentry *dentry;
2153 struct wim_lookup_table_entry *lte;
2157 struct wim_inode *inode;
2158 struct wimfs_context *ctx = wimfs_get_context();
2160 ret = lookup_resource(ctx->wim, path, get_lookup_flags(ctx),
2161 &dentry, <e, &stream_idx);
2166 if (lte == NULL && size == 0)
2169 inode = dentry->d_inode;
2170 if (stream_idx == 0)
2173 stream_id = inode->i_ads_entries[stream_idx - 1].stream_id;
2175 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
2176 ret = truncate(lte->staging_file_name, size);
2180 /* File in WIM. Extract it to the staging directory, but only
2181 * the first @size bytes of it. */
2182 ret = extract_resource_to_staging_dir(inode, stream_id,
2188 /* Unlink a non-directory or alternate data stream */
2189 static int wimfs_unlink(const char *path)
2191 struct wim_dentry *dentry;
2192 struct wim_lookup_table_entry *lte;
2195 struct wimfs_context *ctx = wimfs_get_context();
2197 ret = lookup_resource(ctx->wim, path, get_lookup_flags(ctx),
2198 &dentry, <e, &stream_idx);
2203 if (stream_idx == 0)
2204 remove_dentry(dentry, ctx->wim->lookup_table);
2206 inode_remove_ads(dentry->d_inode, stream_idx - 1,
2207 ctx->wim->lookup_table);
2211 #ifdef HAVE_UTIMENSAT
2213 * Change the timestamp on a file dentry.
2215 * Note that alternate data streams do not have their own timestamps.
2217 static int wimfs_utimens(const char *path, const struct timespec tv[2])
2219 struct wim_dentry *dentry;
2220 struct wim_inode *inode;
2221 WIMStruct *w = wimfs_get_WIMStruct();
2223 dentry = get_dentry(w, path);
2226 inode = dentry->d_inode;
2228 if (tv[0].tv_nsec != UTIME_OMIT) {
2229 if (tv[0].tv_nsec == UTIME_NOW)
2230 inode->i_last_access_time = get_wim_timestamp();
2232 inode->i_last_access_time = timespec_to_wim_timestamp(&tv[0]);
2234 if (tv[1].tv_nsec != UTIME_OMIT) {
2235 if (tv[1].tv_nsec == UTIME_NOW)
2236 inode->i_last_write_time = get_wim_timestamp();
2238 inode->i_last_write_time = timespec_to_wim_timestamp(&tv[1]);
2243 static int wimfs_utime(const char *path, struct utimbuf *times)
2245 struct wim_dentry *dentry;
2246 struct wim_inode *inode;
2247 WIMStruct *w = wimfs_get_WIMStruct();
2249 dentry = get_dentry(w, path);
2252 inode = dentry->d_inode;
2254 inode->i_last_write_time = unix_timestamp_to_wim(times->modtime);
2255 inode->i_last_access_time = unix_timestamp_to_wim(times->actime);
2260 /* Writes to a file in the WIM filesystem.
2261 * It may be an alternate data stream, but here we don't even notice because we
2262 * just get a lookup table entry. */
2263 static int wimfs_write(const char *path, const char *buf, size_t size,
2264 off_t offset, struct fuse_file_info *fi)
2266 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
2273 wimlib_assert(fd->f_lte);
2274 wimlib_assert(fd->f_lte->staging_file_name);
2275 wimlib_assert(fd->staging_fd != -1);
2276 wimlib_assert(fd->f_inode);
2278 /* Seek to the requested position */
2279 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
2282 /* Write the data. */
2283 ret = write(fd->staging_fd, buf, size);
2287 now = get_wim_timestamp();
2288 fd->f_inode->i_last_write_time = now;
2289 fd->f_inode->i_last_access_time = now;
2293 static struct fuse_operations wimfs_operations = {
2295 .access = wimfs_access,
2297 .chmod = wimfs_chmod,
2298 .chown = wimfs_chown,
2299 .destroy = wimfs_destroy,
2301 .fallocate = wimfs_fallocate,
2303 .fgetattr = wimfs_fgetattr,
2304 .ftruncate = wimfs_ftruncate,
2305 .getattr = wimfs_getattr,
2307 .getxattr = wimfs_getxattr,
2311 .listxattr = wimfs_listxattr,
2313 .mkdir = wimfs_mkdir,
2314 .mknod = wimfs_mknod,
2316 .opendir = wimfs_opendir,
2318 .readdir = wimfs_readdir,
2319 .readlink = wimfs_readlink,
2320 .release = wimfs_release,
2321 .releasedir = wimfs_releasedir,
2323 .removexattr = wimfs_removexattr,
2325 .rename = wimfs_rename,
2326 .rmdir = wimfs_rmdir,
2328 .setxattr = wimfs_setxattr,
2330 .symlink = wimfs_symlink,
2331 .truncate = wimfs_truncate,
2332 .unlink = wimfs_unlink,
2333 #ifdef HAVE_UTIMENSAT
2334 .utimens = wimfs_utimens,
2336 .utime = wimfs_utime,
2338 .write = wimfs_write,
2340 /* wimfs keeps file descriptor structures (struct wimfs_fd), so there is
2341 * no need to have the file path provided on operations such as read()
2342 * where only the file descriptor is needed. */
2343 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 8)
2344 .flag_nullpath_ok = 1,
2346 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 9)
2348 .flag_utime_omit_ok = 1,
2353 /* Mounts an image from a WIM file. */
2354 WIMLIBAPI int wimlib_mount_image(WIMStruct *wim, int image, const char *dir,
2355 int mount_flags, WIMStruct **additional_swms,
2356 unsigned num_additional_swms,
2357 const char *staging_dir)
2363 struct wim_lookup_table *joined_tab, *wim_tab_save;
2364 struct wim_image_metadata *imd;
2365 struct wimfs_context ctx;
2366 struct hlist_node *cur_node;
2367 struct wim_inode *inode;
2369 DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
2370 wim, image, dir, mount_flags);
2373 return WIMLIB_ERR_INVALID_PARAM;
2375 ret = verify_swm_set(wim, additional_swms, num_additional_swms);
2379 if ((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) && (wim->hdr.total_parts != 1)) {
2380 ERROR("Cannot mount a split WIM read-write");
2381 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
2384 if (num_additional_swms) {
2385 ret = new_joined_lookup_table(wim, additional_swms,
2386 num_additional_swms,
2390 wim_tab_save = wim->lookup_table;
2391 wim->lookup_table = joined_tab;
2394 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2395 ret = wim_run_full_verifications(wim);
2400 ret = select_wim_image(wim, image);
2404 DEBUG("Selected image %d", image);
2406 imd = wim_get_current_image_metadata(wim);
2408 if (imd->root_dentry->refcnt != 1) {
2409 ERROR("Cannot mount image that was just exported with "
2410 "wimlib_export_image()");
2411 ret = WIMLIB_ERR_INVALID_PARAM;
2415 if (imd->inode_list.first) /* May be unneeded? */
2416 imd->inode_list.first->pprev = &imd->inode_list.first;
2418 if (imd->modified) {
2419 ERROR("Cannot mount image that was added "
2420 "with wimlib_add_image()");
2421 ret = WIMLIB_ERR_INVALID_PARAM;
2425 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2426 ret = lock_wim(wim, wim->fp);
2431 /* Use default stream interface if one was not specified */
2432 if (!(mount_flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
2433 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
2434 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
2435 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
2438 DEBUG("Initializing struct wimfs_context");
2439 init_wimfs_context(&ctx);
2441 ctx.mount_flags = mount_flags;
2442 ctx.image_inode_list = &imd->inode_list;
2443 ctx.default_uid = getuid();
2444 ctx.default_gid = getgid();
2445 if (mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
2446 ctx.default_lookup_flags = LOOKUP_FLAG_ADS_OK;
2448 DEBUG("Unlinking message queues in case they already exist");
2449 ret = set_message_queue_names(&ctx, dir);
2452 unlink_message_queues(&ctx);
2454 DEBUG("Preparing arguments to fuse_main()");
2456 dir_copy = STRDUP(dir);
2458 goto out_free_message_queue_names;
2461 argv[argc++] = "imagex";
2462 argv[argc++] = dir_copy;
2464 /* disable multi-threaded operation for read-write mounts */
2465 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2466 argv[argc++] = "-s";
2468 if (mount_flags & WIMLIB_MOUNT_FLAG_DEBUG)
2469 argv[argc++] = "-d";
2472 * We provide the use_ino option to the FUSE mount because we are going
2473 * to assign inode numbers ourselves. */
2474 char optstring[256] =
2478 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 8)
2481 ",default_permissions"
2483 argv[argc++] = "-o";
2484 argv[argc++] = optstring;
2485 if ((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
2486 /* Read-write mount. Make the staging directory */
2487 ret = make_staging_dir(&ctx, staging_dir);
2489 goto out_free_dir_copy;
2491 /* Read-only mount */
2492 strcat(optstring, ",ro");
2494 if (mount_flags & WIMLIB_MOUNT_FLAG_ALLOW_OTHER)
2495 strcat(optstring, ",allow_other");
2501 DEBUG("FUSE command line (argc = %d): ", argc);
2502 for (i = 0; i < argc; i++) {
2503 fputs(argv[i], stdout);
2511 /* Mark dentry tree as modified if read-write mount. */
2512 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2514 imd->has_been_mounted_rw = 1;
2517 /* Resolve the lookup table entries for every inode in the image, and
2518 * assign inode numbers */
2519 DEBUG("Resolving lookup table entries and assigning inode numbers");
2521 hlist_for_each_entry(inode, cur_node, &imd->inode_list, i_hlist) {
2522 inode_resolve_ltes(inode, wim->lookup_table);
2523 inode->i_ino = ctx.next_ino++;
2525 DEBUG("(next_ino = %"PRIu64")", ctx.next_ino);
2527 DEBUG("Calling fuse_main()");
2529 ret = fuse_main(argc, argv, &wimfs_operations, &ctx);
2531 DEBUG("Returned from fuse_main() (ret = %d)", ret);
2534 ret = WIMLIB_ERR_FUSE;
2536 /* Try to delete the staging directory if a deletion wasn't yet
2537 * attempted due to an earlier error */
2538 if (ctx.staging_dir_name)
2539 delete_staging_dir(&ctx);
2544 wim->wim_locked = 0;
2545 out_free_message_queue_names:
2546 free_message_queue_names(&ctx);
2548 if (num_additional_swms) {
2549 free_lookup_table(wim->lookup_table);
2550 wim->lookup_table = wim_tab_save;
2556 * Unmounts the WIM file that was previously mounted on @dir by using
2557 * wimlib_mount_image().
2559 WIMLIBAPI int wimlib_unmount_image(const char *dir, int unmount_flags,
2560 wimlib_progress_func_t progress_func)
2563 struct wimfs_context wimfs_ctx;
2565 init_wimfs_context(&wimfs_ctx);
2567 ret = set_message_queue_names(&wimfs_ctx, dir);
2571 ret = open_message_queues(&wimfs_ctx, false);
2573 goto out_free_message_queue_names;
2575 ret = send_unmount_request_msg(wimfs_ctx.unmount_to_daemon_mq,
2577 progress_func != NULL);
2579 goto out_close_message_queues;
2581 ret = execute_fusermount(dir);
2583 goto out_close_message_queues;
2585 struct unmount_msg_handler_context handler_ctx = {
2587 .timeout_seconds = 5,
2590 .progress_func = progress_func,
2593 ret = message_loop(wimfs_ctx.daemon_to_unmount_mq,
2594 &unmount_msg_handler_callbacks,
2597 ret = handler_ctx.status;
2598 out_close_message_queues:
2599 close_message_queues(&wimfs_ctx);
2600 out_free_message_queue_names:
2601 free_message_queue_names(&wimfs_ctx);
2606 #else /* WITH_FUSE */
2609 static inline int mount_unsupported_error()
2611 ERROR("wimlib was compiled with --without-fuse, which disables support "
2612 "for mounting WIMs.");
2613 return WIMLIB_ERR_UNSUPPORTED;
2616 WIMLIBAPI int wimlib_unmount_image(const char *dir, int unmount_flags,
2617 wimlib_progress_func_t progress_func)
2619 return mount_unsupported_error();
2622 WIMLIBAPI int wimlib_mount_image(WIMStruct *wim_p, int image, const char *dir,
2623 int mount_flags, WIMStruct **additional_swms,
2624 unsigned num_additional_swms,
2625 const char *staging_dir)
2627 return mount_unsupported_error();
2630 #endif /* WITH_FUSE */