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, 2014 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/.
34 #include "wimlib/error.h"
39 # error "FUSE mount not supported on Win32! Please configure --without-fuse"
42 #include "wimlib/encoding.h"
43 #include "wimlib/file_io.h"
44 #include "wimlib/dentry.h"
45 #include "wimlib/inode.h"
46 #include "wimlib/lookup_table.h"
47 #include "wimlib/metadata.h"
48 #include "wimlib/paths.h"
49 #include "wimlib/progress.h"
50 #include "wimlib/reparse.h"
51 #include "wimlib/resource.h"
52 #include "wimlib/timestamp.h"
53 #include "wimlib/unix_data.h"
54 #include "wimlib/version.h"
55 #include "wimlib/write.h"
56 #include "wimlib/xml.h"
67 #include <sys/types.h>
72 #define FUSE_USE_VERSION 26
76 #include <attr/xattr.h>
79 #define MSG_VERSION_TOO_HIGH -1
80 #define MSG_BREAK_LOOP -2
82 /* File descriptor to a file open on the WIM filesystem. */
84 struct wim_inode *f_inode;
85 struct wim_lookup_table_entry *f_lte;
86 struct filedes staging_fd;
91 struct wimfs_context {
92 /* The WIMStruct for the mounted WIM. */
95 /* Name of the staging directory for a read-write mount. Whenever a new file is
96 * created, it is done so in the staging directory. Furthermore, whenever a
97 * file in the WIM is modified, it is extracted to the staging directory. If
98 * changes are commited when the WIM is unmounted, the file resources are merged
99 * in from the staging directory when writing the new WIM. */
100 char *staging_dir_name;
101 size_t staging_dir_name_len;
103 /* Flags passed to wimlib_mount(). */
106 /* Default flags to use when looking up a WIM dentry (depends on whether
107 * the Windows interface to alternate data streams is being used or
109 int default_lookup_flags;
111 /* Next inode number to be assigned. Note: I didn't bother with a
112 * bitmap of free inode numbers since this isn't even a "real"
113 * filesystem anyway. */
116 /* List of inodes in the mounted image */
117 struct list_head *image_inode_list;
119 /* Original list of streams in the mounted image, linked by
120 * mount_orig_stream_list. */
121 struct list_head orig_stream_list;
123 /* Name and message queue descriptors for message queues between the
124 * filesystem daemon process and the unmount process. These are used
125 * when the filesystem is unmounted and the process running
126 * wimlib_unmount_image() needs to communicate with the filesystem
127 * daemon running fuse_main() (i.e. the process created by a call to
128 * wimlib_mount_image(). */
129 char *unmount_to_daemon_mq_name;
130 char *daemon_to_unmount_mq_name;
131 mqd_t unmount_to_daemon_mq;
132 mqd_t daemon_to_unmount_mq;
142 init_wimfs_context(struct wimfs_context *ctx)
144 memset(ctx, 0, sizeof(*ctx));
145 ctx->unmount_to_daemon_mq = (mqd_t)-1;
146 ctx->daemon_to_unmount_mq = (mqd_t)-1;
149 #define WIMFS_CTX(fuse_ctx) ((struct wimfs_context*)(fuse_ctx)->private_data)
151 static inline struct wimfs_context *
152 wimfs_get_context(void)
154 return WIMFS_CTX(fuse_get_context());
157 static inline WIMStruct *
158 wimfs_get_WIMStruct(void)
160 return wimfs_get_context()->wim;
164 get_lookup_flags(const struct wimfs_context *ctx)
166 return ctx->default_lookup_flags;
169 /* Returns nonzero if write permission is requested on the file open flags */
171 flags_writable(int open_flags)
173 int accmode = (open_flags & O_ACCMODE);
174 return (accmode == O_RDWR || accmode == O_WRONLY);
178 * Allocate a file descriptor for a stream.
180 * @inode: inode containing the stream we're opening
181 * @stream_id: ID of the stream we're opening
182 * @lte: Lookup table entry for the stream (may be NULL)
183 * @fd_ret: Return the allocated file descriptor if successful.
185 * Return 0 iff successful or negative error code if unsuccessful.
188 alloc_wimfs_fd(struct wim_inode *inode,
190 struct wim_lookup_table_entry *lte,
191 struct wimfs_fd **fd_ret)
193 static const u16 fds_per_alloc = 8;
194 static const u16 max_fds = 0xffff;
196 DEBUG("Allocating fd for stream ID %u from inode %#"PRIx64" "
197 "(open = %u, allocated = %u)",
198 stream_id, inode->i_ino, inode->i_num_opened_fds,
199 inode->i_num_allocated_fds);
201 if (inode->i_num_opened_fds == inode->i_num_allocated_fds) {
202 struct wimfs_fd **fds;
205 if (inode->i_num_allocated_fds == max_fds)
208 num_new_fds = min(fds_per_alloc,
209 max_fds - inode->i_num_allocated_fds);
211 fds = REALLOC(inode->i_fds,
212 (inode->i_num_allocated_fds + num_new_fds) *
213 sizeof(inode->i_fds[0]));
217 memset(&fds[inode->i_num_allocated_fds], 0,
218 num_new_fds * sizeof(fds[0]));
220 inode->i_num_allocated_fds += num_new_fds;
222 for (u16 i = 0; ; i++) {
223 if (!inode->i_fds[i]) {
224 struct wimfs_fd *fd = CALLOC(1, sizeof(*fd));
230 filedes_invalidate(&fd->staging_fd);
232 fd->stream_id = stream_id;
234 inode->i_fds[i] = fd;
235 inode->i_num_opened_fds++;
237 lte->num_opened_fds++;
238 DEBUG("Allocated fd (idx = %u)", fd->idx);
245 inode_put_fd(struct wim_inode *inode, struct wimfs_fd *fd)
247 wimlib_assert(inode != NULL);
248 wimlib_assert(fd->f_inode == inode);
249 wimlib_assert(inode->i_num_opened_fds != 0);
250 wimlib_assert(fd->idx < inode->i_num_allocated_fds);
251 wimlib_assert(inode->i_fds[fd->idx] == fd);
253 inode->i_fds[fd->idx] = NULL;
255 if (--inode->i_num_opened_fds == 0) {
258 inode->i_num_allocated_fds = 0;
259 if (inode->i_nlink == 0)
265 lte_put_fd(struct wim_lookup_table_entry *lte, struct wimfs_fd *fd)
267 wimlib_assert(fd->f_lte == lte);
269 if (!lte) /* Empty stream with no lookup table entry */
272 /* Close staging file descriptor if needed. */
274 if (lte->resource_location == RESOURCE_IN_STAGING_FILE
275 && filedes_valid(&fd->staging_fd))
277 if (filedes_close(&fd->staging_fd)) {
278 ERROR_WITH_ERRNO("Failed to close staging file");
282 lte_decrement_num_opened_fds(lte);
286 /* Close a file descriptor. */
288 close_wimfs_fd(struct wimfs_fd *fd)
291 DEBUG("Closing fd (ino = %#"PRIx64", opened = %u, allocated = %u)",
292 fd->f_inode->i_ino, fd->f_inode->i_num_opened_fds,
293 fd->f_inode->i_num_allocated_fds);
294 ret = lte_put_fd(fd->f_lte, fd);
298 inode_put_fd(fd->f_inode, fd);
303 fuse_mask_mode(mode_t mode, struct fuse_context *fuse_ctx)
305 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 8)
306 mode &= ~fuse_ctx->umask;
312 * Add a new dentry with a new inode to a WIM image.
314 * Returns 0 on success, or negative error number on failure.
317 create_dentry(struct fuse_context *fuse_ctx, const char *path,
318 mode_t mode, int attributes, struct wim_dentry **dentry_ret)
320 struct wim_dentry *parent;
321 struct wim_dentry *new;
322 const char *basename;
323 struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
326 parent = get_parent_dentry(wimfs_ctx->wim, path, WIMLIB_CASE_SENSITIVE);
330 if (!dentry_is_directory(parent))
333 basename = path_basename(path);
334 if (get_dentry_child_with_name(parent, basename, WIMLIB_CASE_SENSITIVE))
337 ret = new_dentry_with_inode(basename, &new);
341 new->d_inode->i_resolved = 1;
342 new->d_inode->i_ino = wimfs_ctx->next_ino++;
343 new->d_inode->i_attributes = attributes;
345 if (wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA) {
346 if (!inode_set_unix_data(new->d_inode,
349 fuse_mask_mode(mode, fuse_ctx),
356 dentry_add_child(parent, new);
357 list_add_tail(&new->d_inode->i_list, wimfs_ctx->image_inode_list);
363 static struct wim_inode *
364 wim_pathname_to_inode(WIMStruct *wim, const tchar *path)
366 struct wim_dentry *dentry;
367 dentry = get_dentry(wim, path, WIMLIB_CASE_SENSITIVE);
369 return dentry->d_inode;
375 * Remove a dentry from a mounted WIM image; i.e. remove an alias for an inode.
378 remove_dentry(struct wim_dentry *dentry,
379 struct wim_lookup_table *lookup_table)
381 /* Put a reference to each stream the inode contains. */
382 inode_unref_streams(dentry->d_inode, lookup_table);
384 /* Unlink the dentry from the image's dentry tree. */
385 unlink_dentry(dentry);
387 /* Delete the dentry. This will also decrement the link count of the
388 * corresponding inode. */
393 inode_unix_file_type(const struct wim_inode *inode)
395 if (inode_is_symlink(inode))
397 else if (inode_is_directory(inode))
404 inode_default_unix_mode(const struct wim_inode *inode)
406 return inode_unix_file_type(inode) | 0777;
409 /* Transfers file attributes from a struct wim_inode to a `stat' buffer.
411 * The lookup table entry tells us which stream in the inode we are statting.
412 * For a named data stream, everything returned is the same as the unnamed data
413 * stream except possibly the size and block count. */
415 inode_to_stbuf(const struct wim_inode *inode,
416 const struct wim_lookup_table_entry *lte,
419 const struct wimfs_context *ctx = wimfs_get_context();
420 struct wimlib_unix_data unix_data;
422 memset(stbuf, 0, sizeof(struct stat));
423 if ((ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA) &&
424 inode_get_unix_data(inode, &unix_data))
426 stbuf->st_uid = unix_data.uid;
427 stbuf->st_gid = unix_data.gid;
428 stbuf->st_mode = unix_data.mode;
430 stbuf->st_uid = ctx->default_uid;
431 stbuf->st_gid = ctx->default_gid;
432 stbuf->st_mode = inode_default_unix_mode(inode);
434 stbuf->st_ino = (ino_t)inode->i_ino;
435 stbuf->st_nlink = inode->i_nlink;
437 stbuf->st_size = lte->size;
440 #ifdef HAVE_STAT_NANOSECOND_PRECISION
441 stbuf->st_atim = wim_timestamp_to_timespec(inode->i_last_access_time);
442 stbuf->st_mtim = wim_timestamp_to_timespec(inode->i_last_write_time);
443 stbuf->st_ctim = stbuf->st_mtim;
445 stbuf->st_atime = wim_timestamp_to_unix(inode->i_last_access_time);
446 stbuf->st_mtime = wim_timestamp_to_unix(inode->i_last_write_time);
447 stbuf->st_ctime = stbuf->st_mtime;
449 stbuf->st_blocks = (stbuf->st_size + 511) / 512;
454 touch_inode(struct wim_inode *inode)
456 u64 now = get_wim_timestamp();
457 inode->i_last_access_time = now;
458 inode->i_last_write_time = now;
461 /* Creates a new staging file and returns its file descriptor opened for
464 * @name_ret: A location into which the a pointer to the newly allocated name of
465 * the staging file is stored.
467 * @ctx: Context for the WIM filesystem; this provides the name of the
470 * On success, returns the file descriptor for the staging file, opened for
471 * writing. On failure, returns -1 and sets errno.
474 create_staging_file(char **name_ret, struct wimfs_context *ctx)
482 static const size_t STAGING_FILE_NAME_LEN = 20;
484 name_len = ctx->staging_dir_name_len + 1 + STAGING_FILE_NAME_LEN;
485 name = MALLOC(name_len + 1);
493 memcpy(name, ctx->staging_dir_name, ctx->staging_dir_name_len);
494 name[ctx->staging_dir_name_len] = '/';
495 randomize_char_array_with_alnum(name + ctx->staging_dir_name_len + 1,
496 STAGING_FILE_NAME_LEN);
497 name[name_len] = '\0';
500 /* Just in case, verify that the randomly generated name doesn't name an
501 * existing file, and try again if so */
502 } while (stat(name, &stbuf) == 0);
504 if (errno != ENOENT) /* other error?! */
507 /* doesn't exist--- ok */
509 DEBUG("Creating staging file `%s'", name);
511 fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0600);
523 * Extract a WIM resource to the staging directory.
525 * @inode: Inode that contains the stream we are extracting
527 * @stream_id: Identifier for the stream (it stays constant even if the indices
528 * of the stream entries are changed)
530 * @lte: Pointer to pointer to the lookup table entry for the stream we need to
531 * extract, or NULL if there was no lookup table entry present for the stream
533 * @size: Number of bytes of the stream we want to extract (this supports the
534 * wimfs_truncate() function). It may be more than the actual stream length, in
535 * which case the extra space is filled with zeroes.
537 * @ctx: Context for the WIM filesystem.
539 * Returns 0 on success or a negative error code on failure.
542 extract_resource_to_staging_dir(struct wim_inode *inode,
544 struct wim_lookup_table_entry **lte,
546 struct wimfs_context *ctx)
548 char *staging_file_name;
551 struct wim_lookup_table_entry *old_lte, *new_lte;
554 DEBUG("Extracting resource to staging dir: inode %"PRIu64", "
555 "stream id %"PRIu32, inode->i_ino, stream_id);
559 wimlib_assert(old_lte == NULL ||
560 old_lte->resource_location != RESOURCE_IN_STAGING_FILE);
562 /* Create the staging file */
563 fd = create_staging_file(&staging_file_name, ctx);
567 /* Extract the stream to the staging file (possibly truncated) */
569 struct filedes wimlib_fd;
570 filedes_init(&wimlib_fd, fd);
571 extract_size = min(old_lte->size, size);
572 ret = extract_stream_to_fd(old_lte, &wimlib_fd, extract_size);
578 /* In the case of truncate() to more than the file length, extend the
579 * file with zeroes by calling ftruncate() on the underlying staging
581 if (ret == 0 && size > extract_size)
582 ret = ftruncate(fd, size);
584 /* Close the staging file descriptor and check for errors. If there's
585 * an error, unlink the staging file. */
586 if (ret != 0 || close(fd) != 0) {
592 goto out_delete_staging_file;
595 /* Now deal with the lookup table entries. We may be able to re-use the
596 * existing entry, but we may have to create a new one instead. */
598 if (old_lte && inode->i_nlink == old_lte->refcnt) {
599 /* The reference count of the existing lookup table entry is the
600 * same as the link count of the inode that contains the stream
601 * we're opening. Therefore, ALL the references to the lookup
602 * table entry correspond to the stream we're trying to extract,
603 * so the lookup table entry can be re-used. */
604 DEBUG("Re-using lookup table entry");
605 lookup_table_unlink(ctx->wim->lookup_table, old_lte);
609 /* There's an existing lookup table entry, but its
610 * reference count is greater than the link count for
611 * the inode containing a stream we're opening.
612 * Therefore, we need to split the lookup table entry.
614 wimlib_assert(old_lte->refcnt > inode->i_nlink);
615 DEBUG("Splitting lookup table entry "
616 "(inode->i_nlink = %u, old_lte->refcnt = %u)",
617 inode->i_nlink, old_lte->refcnt);
620 new_lte = new_lookup_table_entry();
623 goto out_delete_staging_file;
626 /* There may already be open file descriptors to this stream if
627 * it's previously been opened read-only, but just now we're
628 * opening it read-write. Identify those file descriptors and
629 * change their lookup table entry pointers to point to the new
630 * lookup table entry, and open staging file descriptors for
633 * At the same time, we need to count the number of these opened
634 * file descriptors to the new lookup table entry. If there's
635 * an old lookup table entry, this number needs to be subtracted
636 * from the fd's opened to the old entry. */
637 for (u16 i = 0, j = 0; j < inode->i_num_opened_fds; i++) {
638 struct wimfs_fd *fd = inode->i_fds[i];
640 if (fd->stream_id == stream_id) {
643 wimlib_assert(fd->f_lte == old_lte);
644 wimlib_assert(!filedes_valid(&fd->staging_fd));
646 new_lte->num_opened_fds++;
647 raw_fd = open(staging_file_name, O_RDONLY);
650 goto out_revert_fd_changes;
652 filedes_init(&fd->staging_fd, raw_fd);
657 DEBUG("%hu fd's were already opened to the file we extracted",
658 new_lte->num_opened_fds);
660 old_lte->num_opened_fds -= new_lte->num_opened_fds;
661 old_lte->refcnt -= inode->i_nlink;
665 lte_put_resource(new_lte);
666 new_lte->refcnt = inode->i_nlink;
667 new_lte->resource_location = RESOURCE_IN_STAGING_FILE;
668 new_lte->staging_file_name = staging_file_name;
669 new_lte->size = size;
671 add_unhashed_stream(new_lte, inode, stream_id,
672 &wim_get_current_image_metadata(ctx->wim)->unhashed_streams);
673 *retrieve_lte_pointer(new_lte) = new_lte;
676 out_revert_fd_changes:
677 for (u16 i = 0, j = 0; j < new_lte->num_opened_fds; i++) {
678 struct wimfs_fd *fd = inode->i_fds[i];
679 if (fd && fd->stream_id == stream_id && fd->f_lte == new_lte) {
681 if (filedes_valid(&fd->staging_fd)) {
682 filedes_close(&fd->staging_fd);
683 filedes_invalidate(&fd->staging_fd);
688 free_lookup_table_entry(new_lte);
689 out_delete_staging_file:
690 unlink(staging_file_name);
691 FREE(staging_file_name);
696 * Creates a randomly named staging directory and saves its name in the
697 * filesystem context structure.
700 make_staging_dir(struct wimfs_context *ctx, const char *user_prefix)
702 static const size_t random_suffix_len = 10;
703 static const char *common_suffix = ".staging";
704 static const size_t common_suffix_len = 8;
706 char *staging_dir_name = NULL;
707 size_t staging_dir_name_len;
709 const char *wim_basename;
710 char *real_user_prefix = NULL;
714 real_user_prefix = realpath(user_prefix, NULL);
715 if (!real_user_prefix) {
716 ERROR_WITH_ERRNO("Could not resolve `%s'",
718 ret = WIMLIB_ERR_NOTDIR;
721 wim_basename = path_basename(ctx->wim->filename);
722 prefix_len = strlen(real_user_prefix) + 1 + strlen(wim_basename);
724 prefix_len = strlen(ctx->wim->filename);
727 staging_dir_name_len = prefix_len + common_suffix_len + random_suffix_len;
729 staging_dir_name = MALLOC(staging_dir_name_len + 1);
730 if (!staging_dir_name) {
731 ret = WIMLIB_ERR_NOMEM;
735 if (real_user_prefix)
736 sprintf(staging_dir_name, "%s/%s", real_user_prefix, wim_basename);
738 strcpy(staging_dir_name, ctx->wim->filename);
740 strcat(staging_dir_name, common_suffix);
742 randomize_char_array_with_alnum(staging_dir_name + prefix_len + common_suffix_len,
745 staging_dir_name[staging_dir_name_len] = '\0';
747 if (mkdir(staging_dir_name, 0700) != 0) {
748 ERROR_WITH_ERRNO("Failed to create temporary directory `%s'",
750 ret = WIMLIB_ERR_MKDIR;
755 FREE(real_user_prefix);
757 ctx->staging_dir_name = staging_dir_name;
758 ctx->staging_dir_name_len = staging_dir_name_len;
760 FREE(staging_dir_name);
766 remove_file_or_directory(const char *fpath, const struct stat *sb,
767 int typeflag, struct FTW *ftwbuf)
769 if (remove(fpath) == 0)
772 ERROR_WITH_ERRNO("Cannot remove `%s'", fpath);
773 return WIMLIB_ERR_DELETE_STAGING_DIR;
778 * Deletes the staging directory and all the files contained in it.
781 delete_staging_dir(struct wimfs_context *ctx)
784 ret = nftw(ctx->staging_dir_name, remove_file_or_directory,
786 FREE(ctx->staging_dir_name);
787 ctx->staging_dir_name = NULL;
792 inode_close_fds(struct wim_inode *inode)
794 u16 num_opened_fds = inode->i_num_opened_fds;
795 for (u16 i = 0, j = 0; j < num_opened_fds; i++) {
796 struct wimfs_fd *fd = inode->i_fds[i];
798 wimlib_assert(fd->f_inode == inode);
799 int ret = close_wimfs_fd(fd);
808 /* Overwrites the WIM file, with changes saved. */
810 rebuild_wim(struct wimfs_context *ctx, int write_flags)
813 struct wim_lookup_table_entry *lte, *tmp;
814 WIMStruct *wim = ctx->wim;
815 struct wim_image_metadata *imd = wim_get_current_image_metadata(ctx->wim);
817 DEBUG("Closing all staging file descriptors.");
818 image_for_each_unhashed_stream_safe(lte, tmp, imd) {
819 ret = inode_close_fds(lte->back_inode);
824 DEBUG("Freeing entries for zero-length streams");
825 image_for_each_unhashed_stream_safe(lte, tmp, imd) {
826 wimlib_assert(lte->unhashed);
827 if (lte->size == 0) {
828 struct wim_lookup_table_entry **back_ptr;
829 back_ptr = retrieve_lte_pointer(lte);
831 list_del(<e->unhashed_list);
832 free_lookup_table_entry(lte);
836 xml_update_image_info(wim, wim->current_image);
837 ret = wimlib_overwrite(wim, write_flags, 0);
839 ERROR("Failed to commit changes to mounted WIM image");
843 /* Simple function that returns the concatenation of 2 strings. */
845 strcat_dup(const char *s1, const char *s2, size_t max_len)
847 size_t len = strlen(s1) + strlen(s2);
850 char *p = MALLOC(len + 1);
853 snprintf(p, len + 1, "%s%s", s1, s2);
858 set_message_queue_names(struct wimfs_context *ctx, const char *mount_dir)
860 static const char *u2d_prefix = "/wimlib-unmount-to-daemon-mq";
861 static const char *d2u_prefix = "/wimlib-daemon-to-unmount-mq";
866 dir_path = realpath(mount_dir, NULL);
868 ERROR_WITH_ERRNO("Failed to resolve path \"%s\"", mount_dir);
870 return WIMLIB_ERR_NOMEM;
872 return WIMLIB_ERR_NOTDIR;
875 for (p = dir_path; *p; p++)
879 ctx->unmount_to_daemon_mq_name = strcat_dup(u2d_prefix, dir_path,
881 if (!ctx->unmount_to_daemon_mq_name) {
882 ret = WIMLIB_ERR_NOMEM;
883 goto out_free_dir_path;
885 ctx->daemon_to_unmount_mq_name = strcat_dup(d2u_prefix, dir_path,
887 if (!ctx->daemon_to_unmount_mq_name) {
888 ret = WIMLIB_ERR_NOMEM;
889 goto out_free_unmount_to_daemon_mq_name;
893 goto out_free_dir_path;
894 out_free_unmount_to_daemon_mq_name:
895 FREE(ctx->unmount_to_daemon_mq_name);
896 ctx->unmount_to_daemon_mq_name = NULL;
903 free_message_queue_names(struct wimfs_context *ctx)
905 FREE(ctx->unmount_to_daemon_mq_name);
906 FREE(ctx->daemon_to_unmount_mq_name);
907 ctx->unmount_to_daemon_mq_name = NULL;
908 ctx->daemon_to_unmount_mq_name = NULL;
912 * Opens two POSIX message queue: one for sending messages from the unmount
913 * process to the daemon process, and one to go the other way. The names of the
914 * message queues, which must be system-wide unique, are be based on the mount
917 * @daemon specifies whether the calling process is the filesystem daemon or the
921 open_message_queues(struct wimfs_context *ctx, bool daemon)
923 int unmount_to_daemon_mq_flags = O_WRONLY | O_CREAT;
924 int daemon_to_unmount_mq_flags = O_RDONLY | O_CREAT;
930 swap(unmount_to_daemon_mq_flags, daemon_to_unmount_mq_flags);
936 orig_umask = umask(0000);
937 DEBUG("Opening message queue \"%s\"", ctx->unmount_to_daemon_mq_name);
938 ctx->unmount_to_daemon_mq = mq_open(ctx->unmount_to_daemon_mq_name,
939 unmount_to_daemon_mq_flags, mode, NULL);
941 if (ctx->unmount_to_daemon_mq == (mqd_t)-1) {
942 ERROR_WITH_ERRNO("mq_open()");
943 ret = WIMLIB_ERR_MQUEUE;
947 DEBUG("Opening message queue \"%s\"", ctx->daemon_to_unmount_mq_name);
948 ctx->daemon_to_unmount_mq = mq_open(ctx->daemon_to_unmount_mq_name,
949 daemon_to_unmount_mq_flags, mode, NULL);
951 if (ctx->daemon_to_unmount_mq == (mqd_t)-1) {
952 ERROR_WITH_ERRNO("mq_open()");
953 mq_close(ctx->unmount_to_daemon_mq);
954 mq_unlink(ctx->unmount_to_daemon_mq_name);
955 ctx->unmount_to_daemon_mq = (mqd_t)-1;
956 ret = WIMLIB_ERR_MQUEUE;
965 /* Try to determine the maximum message size of a message queue. The return
966 * value is the maximum message size, or a guess of 8192 bytes if it cannot be
969 mq_get_msgsize(mqd_t mq)
971 static const char *msgsize_max_file = "/proc/sys/fs/mqueue/msgsize_max";
976 if (mq_getattr(mq, &attr) == 0) {
977 msgsize = attr.mq_msgsize;
979 ERROR_WITH_ERRNO("mq_getattr()");
980 ERROR("Attempting to read %s", msgsize_max_file);
981 fp = fopen(msgsize_max_file, "rb");
983 if (fscanf(fp, "%ld", &msgsize) != 1) {
984 ERROR("Assuming message size of 8192");
989 ERROR_WITH_ERRNO("Failed to open the file `%s'",
991 ERROR("Assuming message size of 8192");
999 get_mailbox(mqd_t mq, long needed_msgsize, long *msgsize_ret,
1005 msgsize = mq_get_msgsize(mq);
1007 if (msgsize < needed_msgsize) {
1008 ERROR("Message queue max size must be at least %ld!",
1010 return WIMLIB_ERR_MQUEUE;
1013 mailbox = MALLOC(msgsize);
1015 ERROR("Failed to allocate %ld bytes for mailbox", msgsize);
1016 return WIMLIB_ERR_NOMEM;
1018 *msgsize_ret = msgsize;
1019 *mailbox_ret = mailbox;
1024 unlink_message_queues(struct wimfs_context *ctx)
1026 mq_unlink(ctx->unmount_to_daemon_mq_name);
1027 mq_unlink(ctx->daemon_to_unmount_mq_name);
1030 /* Closes the message queues, which are allocated in static variables */
1032 close_message_queues(struct wimfs_context *ctx)
1034 DEBUG("Closing message queues");
1035 mq_close(ctx->unmount_to_daemon_mq);
1036 ctx->unmount_to_daemon_mq = (mqd_t)(-1);
1037 mq_close(ctx->daemon_to_unmount_mq);
1038 ctx->daemon_to_unmount_mq = (mqd_t)(-1);
1039 unlink_message_queues(ctx);
1043 struct unmount_msg_hdr {
1048 } _packed_attribute;
1050 struct msg_unmount_request {
1051 struct unmount_msg_hdr hdr;
1053 u8 want_progress_messages;
1054 } _packed_attribute;
1056 struct msg_daemon_info {
1057 struct unmount_msg_hdr hdr;
1060 } _packed_attribute;
1062 struct msg_unmount_finished {
1063 struct unmount_msg_hdr hdr;
1065 } _packed_attribute;
1067 struct msg_write_streams_progress {
1068 struct unmount_msg_hdr hdr;
1069 union wimlib_progress_info info;
1070 } _packed_attribute;
1073 MSG_TYPE_UNMOUNT_REQUEST,
1074 MSG_TYPE_DAEMON_INFO,
1075 MSG_TYPE_WRITE_STREAMS_PROGRESS,
1076 MSG_TYPE_UNMOUNT_FINISHED,
1080 struct msg_handler_context_hdr {
1081 int timeout_seconds;
1084 struct unmount_msg_handler_context {
1085 struct msg_handler_context_hdr hdr;
1089 wimlib_progress_func_t progfunc;
1093 struct daemon_msg_handler_context {
1094 struct msg_handler_context_hdr hdr;
1095 struct wimfs_context *wimfs_ctx;
1099 send_unmount_request_msg(mqd_t mq, int unmount_flags, u8 want_progress_messages)
1101 DEBUG("Sending unmount request msg");
1102 struct msg_unmount_request msg = {
1104 .min_version = ((unmount_flags & WIMLIB_UNMOUNT_FLAG_NEW_IMAGE) ?
1105 WIMLIB_MAKEVERSION(1, 6, 2) :
1106 WIMLIB_MAKEVERSION(1, 2, 1)),
1107 .cur_version = WIMLIB_VERSION_CODE,
1108 .msg_type = MSG_TYPE_UNMOUNT_REQUEST,
1109 .msg_size = sizeof(msg),
1111 .unmount_flags = unmount_flags,
1112 .want_progress_messages = want_progress_messages,
1115 if (mq_send(mq, (void*)&msg, sizeof(msg), 1)) {
1116 ERROR_WITH_ERRNO("Failed to communicate with filesystem daemon");
1117 return WIMLIB_ERR_MQUEUE;
1123 send_daemon_info_msg(mqd_t mq, pid_t pid, int mount_flags)
1125 DEBUG("Sending daemon info msg (pid = %d, mount_flags=%x)",
1128 struct msg_daemon_info msg = {
1130 .min_version = WIMLIB_MAKEVERSION(1, 2, 1),
1131 .cur_version = WIMLIB_VERSION_CODE,
1132 .msg_type = MSG_TYPE_DAEMON_INFO,
1133 .msg_size = sizeof(msg),
1136 .mount_flags = mount_flags,
1138 if (mq_send(mq, (void*)&msg, sizeof(msg), 1)) {
1139 ERROR_WITH_ERRNO("Failed to send daemon info to unmount process");
1140 return WIMLIB_ERR_MQUEUE;
1146 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 enum wimlib_progress_status
1163 unmount_progress_func(enum wimlib_progress_msg msg,
1164 union wimlib_progress_info *info, void *_ignored_context)
1166 if (msg == WIMLIB_PROGRESS_MSG_WRITE_STREAMS) {
1167 struct msg_write_streams_progress msg = {
1169 .min_version = WIMLIB_MAKEVERSION(1, 2, 1),
1170 .cur_version = WIMLIB_VERSION_CODE,
1171 .msg_type = MSG_TYPE_WRITE_STREAMS_PROGRESS,
1172 .msg_size = sizeof(msg),
1176 if (mq_send(wimfs_get_context()->daemon_to_unmount_mq,
1177 (void*)&msg, sizeof(msg), 1))
1179 ERROR_WITH_ERRNO("Failed to send progress information "
1180 "to unmount process");
1183 return WIMLIB_PROGRESS_STATUS_CONTINUE;
1187 release_extra_refcnts(struct wimfs_context *ctx)
1189 struct list_head *list = &ctx->orig_stream_list;
1190 struct wim_lookup_table *lookup_table = ctx->wim->lookup_table;
1191 struct wim_lookup_table_entry *lte, *tmp;
1193 list_for_each_entry_safe(lte, tmp, list, orig_stream_list) {
1194 u32 n = lte->out_refcnt;
1196 lte_decrement_refcnt(lte, lookup_table);
1200 /* Moves the currently selected image, which may have been modified, to a new
1201 * index, and sets the original index to refer to a reset (unmodified) copy of
1204 renew_current_image(struct wimfs_context *ctx)
1206 WIMStruct *wim = ctx->wim;
1208 int idx = wim->current_image - 1;
1209 struct wim_image_metadata *imd = wim->image_metadata[idx];
1210 struct wim_image_metadata *replace_imd;
1211 struct wim_lookup_table_entry *new_lte;
1213 if (imd->metadata_lte->resource_location != RESOURCE_IN_WIM) {
1214 ERROR("Can't reset modified image that doesn't yet "
1215 "exist in the on-disk WIM file!");
1216 return WIMLIB_ERR_METADATA_NOT_FOUND;
1219 /* Create 'replace_imd' structure to use for the reset original,
1220 * unmodified image. */
1221 replace_imd = new_image_metadata();
1223 return WIMLIB_ERR_NOMEM;
1225 /* Create new stream reference for the modified image's metadata
1226 * resource, which doesn't exist yet. */
1227 ret = WIMLIB_ERR_NOMEM;
1228 new_lte = new_lookup_table_entry();
1230 goto err_put_replace_imd;
1231 new_lte->flags = WIM_RESHDR_FLAG_METADATA;
1232 new_lte->unhashed = 1;
1234 /* Make the image being moved available at a new index. Increments the
1235 * WIM's image count, but does not increment the reference count of the
1236 * 'struct image_metadata'. */
1237 ret = append_image_metadata(wim, imd);
1239 goto err_free_new_lte;
1241 ret = xml_add_image(wim, T(""));
1243 goto err_undo_append;
1245 replace_imd->metadata_lte = imd->metadata_lte;
1246 imd->metadata_lte = new_lte;
1247 wim->image_metadata[idx] = replace_imd;
1248 wim->current_image = wim->hdr.image_count;
1252 wim->hdr.image_count--;
1254 free_lookup_table_entry(new_lte);
1255 err_put_replace_imd:
1256 put_image_metadata(replace_imd, NULL);
1261 msg_unmount_request_handler(const void *_msg, void *_handler_ctx)
1263 const struct msg_unmount_request *msg = _msg;
1264 struct daemon_msg_handler_context *handler_ctx = _handler_ctx;
1265 struct wimfs_context *wimfs_ctx;
1270 DEBUG("Handling unmount request msg");
1272 wimfs_ctx = handler_ctx->wimfs_ctx;
1273 if (msg->hdr.msg_size < sizeof(*msg)) {
1274 status = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1278 unmount_flags = msg->unmount_flags;
1280 wimlib_register_progress_function(wimfs_ctx->wim,
1281 (msg->want_progress_messages ?
1282 unmount_progress_func : NULL),
1285 ret = send_daemon_info_msg(wimfs_ctx->daemon_to_unmount_mq, getpid(),
1286 wimfs_ctx->mount_flags);
1292 if (wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1293 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_COMMIT) {
1295 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_NEW_IMAGE) {
1296 ret = renew_current_image(wimfs_ctx);
1302 release_extra_refcnts(wimfs_ctx);
1304 INIT_LIST_HEAD(&wimfs_ctx->orig_stream_list);
1306 int write_flags = 0;
1307 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY)
1308 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1309 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_REBUILD)
1310 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1311 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_RECOMPRESS)
1312 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
1313 status = rebuild_wim(wimfs_ctx, write_flags);
1316 DEBUG("Read-only mount");
1321 if (wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1322 ret = delete_staging_dir(wimfs_ctx);
1324 ERROR("Failed to delete the staging directory");
1329 wimfs_ctx->status = status;
1330 wimfs_ctx->have_status = true;
1331 return MSG_BREAK_LOOP;
1335 msg_daemon_info_handler(const void *_msg, void *_handler_ctx)
1337 const struct msg_daemon_info *msg = _msg;
1338 struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1340 DEBUG("Handling daemon info msg");
1341 if (msg->hdr.msg_size < sizeof(*msg))
1342 return WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1343 handler_ctx->daemon_pid = msg->daemon_pid;
1344 handler_ctx->mount_flags = msg->mount_flags;
1345 handler_ctx->hdr.timeout_seconds = 1;
1346 DEBUG("pid of daemon is %d; mount flags were %#x",
1347 handler_ctx->daemon_pid,
1348 handler_ctx->mount_flags);
1353 msg_write_streams_progress_handler(const void *_msg, void *_handler_ctx)
1355 const struct msg_write_streams_progress *msg = _msg;
1356 struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1358 if (msg->hdr.msg_size < sizeof(*msg))
1359 return WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1360 return call_progress(handler_ctx->progfunc,
1361 WIMLIB_PROGRESS_MSG_WRITE_STREAMS,
1362 (union wimlib_progress_info *)&msg->info,
1363 handler_ctx->progctx);
1367 msg_unmount_finished_handler(const void *_msg, void *_handler_ctx)
1369 const struct msg_unmount_finished *msg = _msg;
1370 struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1372 DEBUG("Handling unmount finished message");
1373 if (msg->hdr.msg_size < sizeof(*msg))
1374 return WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1375 handler_ctx->status = msg->status;
1376 DEBUG("status is %d", handler_ctx->status);
1377 return MSG_BREAK_LOOP;
1381 unmount_timed_out_cb(void *_handler_ctx)
1383 const struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1385 if (handler_ctx->daemon_pid == 0 ||
1386 (kill(handler_ctx->daemon_pid, 0) != 0 && errno == ESRCH))
1388 ERROR("The filesystem daemon has crashed! Changes to the "
1389 "WIM may not have been commited.");
1390 return WIMLIB_ERR_FILESYSTEM_DAEMON_CRASHED;
1393 DEBUG("Filesystem daemon is still alive... "
1394 "Waiting another %d seconds", handler_ctx->hdr.timeout_seconds);
1399 daemon_timed_out_cb(void *_handler_ctx)
1401 ERROR("Timed out waiting for unmount request! "
1402 "Changes to the mounted WIM will not be committed.");
1403 return WIMLIB_ERR_TIMEOUT;
1406 typedef int (*msg_handler_t)(const void *_msg, void *_handler_ctx);
1408 struct msg_handler_callbacks {
1409 int (*timed_out)(void * _handler_ctx);
1410 msg_handler_t msg_handlers[MSG_TYPE_MAX];
1413 static const struct msg_handler_callbacks unmount_msg_handler_callbacks = {
1414 .timed_out = unmount_timed_out_cb,
1416 [MSG_TYPE_DAEMON_INFO] = msg_daemon_info_handler,
1417 [MSG_TYPE_WRITE_STREAMS_PROGRESS] = msg_write_streams_progress_handler,
1418 [MSG_TYPE_UNMOUNT_FINISHED] = msg_unmount_finished_handler,
1422 static const struct msg_handler_callbacks daemon_msg_handler_callbacks = {
1423 .timed_out = daemon_timed_out_cb,
1425 [MSG_TYPE_UNMOUNT_REQUEST] = msg_unmount_request_handler,
1430 receive_message(mqd_t mq,
1431 struct msg_handler_context_hdr *handler_ctx,
1432 const msg_handler_t msg_handlers[],
1433 long mailbox_size, void *mailbox)
1436 struct timespec timeout;
1437 ssize_t bytes_received;
1438 struct unmount_msg_hdr *hdr;
1441 gettimeofday(&now, NULL);
1442 timeout.tv_sec = now.tv_sec + handler_ctx->timeout_seconds;
1443 timeout.tv_nsec = now.tv_usec * 1000;
1445 bytes_received = mq_timedreceive(mq, mailbox,
1446 mailbox_size, NULL, &timeout);
1448 if (bytes_received == -1) {
1449 if (errno == ETIMEDOUT) {
1450 ret = WIMLIB_ERR_TIMEOUT;
1452 ERROR_WITH_ERRNO("mq_timedreceive()");
1453 ret = WIMLIB_ERR_MQUEUE;
1455 } else if (bytes_received < sizeof(*hdr) ||
1456 bytes_received != hdr->msg_size) {
1457 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1458 } else if (WIMLIB_VERSION_CODE < hdr->min_version) {
1459 /*ERROR("Cannot understand the received message. "*/
1460 /*"Please upgrade wimlib to at least v%d.%d.%d",*/
1461 /*WIMLIB_GET_MAJOR_VERSION(hdr->min_version),*/
1462 /*WIMLIB_GET_MINOR_VERSION(hdr->min_version),*/
1463 /*WIMLIB_GET_PATCH_VERSION(hdr->min_version));*/
1464 ret = MSG_VERSION_TOO_HIGH;
1465 } else if (hdr->msg_type >= MSG_TYPE_MAX) {
1466 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1467 } else if (msg_handlers[hdr->msg_type] == NULL) {
1468 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1470 ret = msg_handlers[hdr->msg_type](mailbox, handler_ctx);
1476 message_loop(mqd_t mq,
1477 const struct msg_handler_callbacks *callbacks,
1478 struct msg_handler_context_hdr *handler_ctx)
1480 static const size_t MAX_MSG_SIZE = 512;
1485 DEBUG("Entering message loop");
1487 ret = get_mailbox(mq, MAX_MSG_SIZE, &msgsize, &mailbox);
1491 ret = receive_message(mq, handler_ctx,
1492 callbacks->msg_handlers,
1494 if (ret == 0 || ret == MSG_VERSION_TOO_HIGH) {
1496 } else if (ret == MSG_BREAK_LOOP) {
1499 } else if (ret == WIMLIB_ERR_TIMEOUT) {
1500 if (callbacks->timed_out)
1501 ret = callbacks->timed_out(handler_ctx);
1507 ERROR_WITH_ERRNO("Error communicating with "
1508 "filesystem daemon");
1513 DEBUG("Exiting message loop");
1517 /* Execute `fusermount -u', which is installed setuid root, to unmount the WIM.
1519 * FUSE does not yet implement synchronous unmounts. This means that fusermount
1520 * -u will return before the filesystem daemon returns from wimfs_destroy().
1521 * This is partly what we want, because we need to send a message from this
1522 * process to the filesystem daemon telling whether --commit was specified or
1523 * not. However, after that, the unmount process must wait for the filesystem
1524 * daemon to finish writing the WIM file.
1527 execute_fusermount(const char *dir, bool lazy)
1535 ERROR_WITH_ERRNO("Failed to fork()");
1536 return WIMLIB_ERR_FORK;
1542 *argp++ = "fusermount";
1546 *argp++ = (char*)dir;
1548 execvp("fusermount", argv);
1549 ERROR_WITH_ERRNO("Failed to execute `fusermount'");
1550 exit(WIMLIB_ERR_FUSERMOUNT);
1554 ret = waitpid(pid, &status, 0);
1556 ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
1558 return WIMLIB_ERR_FUSERMOUNT;
1561 if (!WIFEXITED(status)) {
1562 ERROR("'fusermount' did not terminate normally!");
1563 return WIMLIB_ERR_FUSERMOUNT;
1566 status = WEXITSTATUS(status);
1571 if (status != WIMLIB_ERR_FUSERMOUNT)
1572 return WIMLIB_ERR_FUSERMOUNT;
1574 /* Try again, but with the `umount' program. This is required on other
1575 * FUSE implementations such as FreeBSD's that do not have a
1576 * `fusermount' program. */
1577 ERROR("Falling back to 'umount'. Note: you may need to be "
1578 "root for this to work");
1581 ERROR_WITH_ERRNO("Failed to fork()");
1582 return WIMLIB_ERR_FORK;
1591 *argp++ = (char*)dir;
1593 execvp("umount", argv);
1594 ERROR_WITH_ERRNO("Failed to execute `umount'");
1595 exit(WIMLIB_ERR_FUSERMOUNT);
1599 ret = waitpid(pid, &status, 0);
1601 ERROR_WITH_ERRNO("Failed to wait for `umount' process to "
1603 return WIMLIB_ERR_FUSERMOUNT;
1606 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
1607 ERROR("`umount' did not successfully complete");
1608 return WIMLIB_ERR_FUSERMOUNT;
1615 wimfs_chmod(const char *path, mode_t mask)
1617 struct wim_dentry *dentry;
1618 struct wim_inode *inode;
1619 struct wimfs_context *ctx = wimfs_get_context();
1622 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA))
1625 ret = wim_pathname_to_stream(ctx->wim, path, LOOKUP_FLAG_DIRECTORY_OK,
1626 &dentry, NULL, NULL);
1630 inode = dentry->d_inode;
1632 if (!inode_set_unix_data(inode, ctx->default_uid,
1633 ctx->default_gid, mask, UNIX_DATA_MODE))
1640 wimfs_chown(const char *path, uid_t uid, gid_t gid)
1642 struct wim_dentry *dentry;
1643 struct wim_inode *inode;
1644 struct wimfs_context *ctx = wimfs_get_context();
1648 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA))
1651 ret = wim_pathname_to_stream(ctx->wim, path, LOOKUP_FLAG_DIRECTORY_OK,
1652 &dentry, NULL, NULL);
1656 inode = dentry->d_inode;
1660 if (uid != (uid_t)-1)
1661 which |= UNIX_DATA_UID;
1663 uid = ctx->default_uid;
1665 if (gid != (gid_t)-1)
1666 which |= UNIX_DATA_GID;
1668 gid = ctx->default_gid;
1671 if (!inode_set_unix_data(inode, uid, gid,
1672 inode_default_unix_mode(inode), which))
1678 /* Called when the filesystem is unmounted. */
1680 wimfs_destroy(void *p)
1682 struct wimfs_context *wimfs_ctx = wimfs_get_context();
1683 if (open_message_queues(wimfs_ctx, true) == 0) {
1684 struct daemon_msg_handler_context handler_ctx = {
1686 .timeout_seconds = 5,
1688 .wimfs_ctx = wimfs_ctx,
1690 message_loop(wimfs_ctx->unmount_to_daemon_mq,
1691 &daemon_msg_handler_callbacks,
1697 wimfs_fgetattr(const char *path, struct stat *stbuf,
1698 struct fuse_file_info *fi)
1700 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1701 return inode_to_stbuf(fd->f_inode, fd->f_lte, stbuf);
1705 wimfs_ftruncate(const char *path, off_t size, struct fuse_file_info *fi)
1707 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1708 int ret = ftruncate(fd->staging_fd.fd, size);
1711 touch_inode(fd->f_inode);
1712 fd->f_lte->size = size;
1717 * Fills in a `struct stat' that corresponds to a file or directory in the WIM.
1720 wimfs_getattr(const char *path, struct stat *stbuf)
1722 struct wim_dentry *dentry;
1723 struct wim_lookup_table_entry *lte;
1725 struct wimfs_context *ctx = wimfs_get_context();
1727 ret = wim_pathname_to_stream(ctx->wim, path,
1728 get_lookup_flags(ctx) |
1729 LOOKUP_FLAG_DIRECTORY_OK,
1730 &dentry, <e, NULL);
1733 return inode_to_stbuf(dentry->d_inode, lte, stbuf);
1737 /* Read an alternate data stream through the XATTR interface, or get its size */
1739 wimfs_getxattr(const char *path, const char *name, char *value,
1743 struct wim_inode *inode;
1744 struct wim_ads_entry *ads_entry;
1746 struct wim_lookup_table_entry *lte;
1747 struct wimfs_context *ctx = wimfs_get_context();
1749 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1752 if (strlen(name) <= 5 || memcmp(name, "user.", 5) != 0)
1756 inode = wim_pathname_to_inode(ctx->wim, path);
1760 ads_entry = inode_get_ads_entry(inode, name, NULL);
1764 lte = ads_entry->lte;
1765 stream_size = lte->size;
1770 if (stream_size > size)
1773 ret = read_full_stream_into_buf(lte, value);
1784 /* Create a hard link */
1786 wimfs_link(const char *to, const char *from)
1788 struct wim_dentry *from_dentry, *from_dentry_parent;
1789 const char *link_name;
1790 struct wim_inode *inode;
1791 WIMStruct *wim = wimfs_get_WIMStruct();
1794 inode = wim_pathname_to_inode(wim, to);
1798 if (inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
1799 FILE_ATTRIBUTE_REPARSE_POINT))
1802 from_dentry_parent = get_parent_dentry(wim, from, WIMLIB_CASE_SENSITIVE);
1803 if (!from_dentry_parent)
1805 if (!dentry_is_directory(from_dentry_parent))
1808 link_name = path_basename(from);
1809 if (get_dentry_child_with_name(from_dentry_parent, link_name,
1810 WIMLIB_CASE_SENSITIVE))
1813 ret = new_dentry(link_name, &from_dentry);
1818 inode_ref_streams(inode);
1819 from_dentry->d_inode = inode;
1820 inode_add_dentry(from_dentry, inode);
1821 dentry_add_child(from_dentry_parent, from_dentry);
1827 wimfs_listxattr(const char *path, char *list, size_t size)
1830 struct wim_inode *inode;
1831 struct wimfs_context *ctx = wimfs_get_context();
1834 bool size_only = (size == 0);
1836 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1839 /* List alternate data streams, or get the list size */
1841 inode = wim_pathname_to_inode(ctx->wim, path);
1846 for (i = 0; i < inode->i_num_ads; i++) {
1848 if (!ads_entry_is_named_stream(&inode->i_ads_entries[i]))
1851 char *stream_name_mbs;
1852 size_t stream_name_mbs_nbytes;
1855 ret = utf16le_to_tstr(inode->i_ads_entries[i].stream_name,
1856 inode->i_ads_entries[i].stream_name_nbytes,
1858 &stream_name_mbs_nbytes);
1862 needed_size = stream_name_mbs_nbytes + 6;
1864 if (needed_size > size) {
1865 FREE(stream_name_mbs);
1868 sprintf(p, "user.%s", stream_name_mbs);
1869 size -= needed_size;
1872 FREE(stream_name_mbs);
1879 /* Create a directory in the WIM image. */
1881 wimfs_mkdir(const char *path, mode_t mode)
1883 return create_dentry(fuse_get_context(), path, mode | S_IFDIR,
1884 FILE_ATTRIBUTE_DIRECTORY, NULL);
1887 /* Create a regular file or alternate data stream in the WIM image. */
1889 wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
1891 const char *stream_name;
1892 struct fuse_context *fuse_ctx = fuse_get_context();
1893 struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
1898 if ((wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
1899 && (stream_name = path_stream_name(path))) {
1900 /* Make an alternate data stream */
1901 struct wim_ads_entry *new_entry;
1902 struct wim_inode *inode;
1904 char *p = (char*)stream_name - 1;
1905 wimlib_assert(*p == ':');
1908 inode = wim_pathname_to_inode(wimfs_ctx->wim, path);
1911 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT)
1913 if (inode_get_ads_entry(inode, stream_name, NULL))
1915 new_entry = inode_add_ads(inode, stream_name);
1920 /* Make a normal file (not an alternate data stream) */
1921 return create_dentry(fuse_ctx, path, mode | S_IFREG,
1922 FILE_ATTRIBUTE_NORMAL, NULL);
1928 wimfs_open(const char *path, struct fuse_file_info *fi)
1930 struct wim_dentry *dentry;
1931 struct wim_lookup_table_entry *lte;
1933 struct wimfs_fd *fd;
1934 struct wim_inode *inode;
1937 struct wimfs_context *ctx = wimfs_get_context();
1938 struct wim_lookup_table_entry **back_ptr;
1940 ret = wim_pathname_to_stream(ctx->wim, path, get_lookup_flags(ctx),
1941 &dentry, <e, &stream_idx);
1945 inode = dentry->d_inode;
1947 if (stream_idx == 0) {
1949 back_ptr = &inode->i_lte;
1951 stream_id = inode->i_ads_entries[stream_idx - 1].stream_id;
1952 back_ptr = &inode->i_ads_entries[stream_idx - 1].lte;
1955 /* The file resource may be in the staging directory (read-write mounts
1956 * only) or in the WIM. If it's in the staging directory, we need to
1957 * open a native file descriptor for the corresponding file. Otherwise,
1958 * we can read the file resource directly from the WIM file if we are
1959 * opening it read-only, but we need to extract the resource to the
1960 * staging directory if we are opening it writable. */
1962 if (flags_writable(fi->flags) &&
1963 (!lte || lte->resource_location != RESOURCE_IN_STAGING_FILE)) {
1964 u64 size = (lte) ? lte->size : 0;
1965 ret = extract_resource_to_staging_dir(inode, stream_id,
1972 ret = alloc_wimfs_fd(inode, stream_id, lte, &fd);
1976 if (lte && lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1979 raw_fd = open(lte->staging_file_name, fi->flags);
1981 int errno_save = errno;
1985 filedes_init(&fd->staging_fd, raw_fd);
1987 fi->fh = (uintptr_t)fd;
1991 /* Opens a directory. */
1993 wimfs_opendir(const char *path, struct fuse_file_info *fi)
1995 struct wim_inode *inode;
1997 struct wimfs_fd *fd = NULL;
1998 struct wimfs_context *ctx = wimfs_get_context();
1999 WIMStruct *wim = ctx->wim;
2001 inode = wim_pathname_to_inode(wim, path);
2004 if (!inode_is_directory(inode))
2006 ret = alloc_wimfs_fd(inode, 0, NULL, &fd);
2007 fi->fh = (uintptr_t)fd;
2013 * Read data from a file in the WIM or in the staging directory.
2016 wimfs_read(const char *path, char *buf, size_t size,
2017 off_t offset, struct fuse_file_info *fi)
2019 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
2030 stream_size = fd->f_lte->size;
2034 if (offset > stream_size)
2037 size = min(size, stream_size - offset);
2041 switch (fd->f_lte->resource_location) {
2042 case RESOURCE_IN_STAGING_FILE:
2043 ret = raw_pread(&fd->staging_fd, buf, size, offset);
2047 case RESOURCE_IN_WIM:
2048 if (read_partial_wim_stream_into_buf(fd->f_lte, size,
2050 ret = errno ? -errno : -EIO;
2054 case RESOURCE_IN_ATTACHED_BUFFER:
2055 memcpy(buf, fd->f_lte->attached_buffer + offset, size);
2059 ERROR("Invalid resource location");
2066 /* Fills in the entries of the directory specified by @path using the
2067 * FUSE-provided function @filler. */
2069 wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
2070 off_t offset, struct fuse_file_info *fi)
2072 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
2073 struct wim_inode *inode;
2074 struct wim_dentry *child;
2080 inode = fd->f_inode;
2082 ret = filler(buf, ".", NULL, 0);
2085 ret = filler(buf, "..", NULL, 0);
2089 for_inode_child(child, inode) {
2090 char *file_name_mbs;
2091 size_t file_name_mbs_nbytes;
2093 ret = utf16le_to_tstr(child->file_name,
2094 child->file_name_nbytes,
2096 &file_name_mbs_nbytes);
2100 ret = filler(buf, file_name_mbs, NULL, 0);
2101 FREE(file_name_mbs);
2110 wimfs_readlink(const char *path, char *buf, size_t buf_len)
2112 struct wimfs_context *ctx = wimfs_get_context();
2113 struct wim_inode *inode = wim_pathname_to_inode(ctx->wim, path);
2117 if (!inode_is_symlink(inode))
2121 ret = wim_inode_readlink(inode, buf, buf_len - 1, NULL);
2123 wimlib_assert(ret <= buf_len - 1);
2126 } else if (ret == -ENAMETOOLONG) {
2127 buf[buf_len - 1] = '\0';
2134 wimfs_release(const char *path, struct fuse_file_info *fi)
2136 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
2137 return close_wimfs_fd(fd);
2140 /* Close a directory */
2142 wimfs_releasedir(const char *path, struct fuse_file_info *fi)
2144 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
2145 return close_wimfs_fd(fd);
2149 /* Remove an alternate data stream through the XATTR interface */
2151 wimfs_removexattr(const char *path, const char *name)
2153 struct wim_inode *inode;
2154 struct wim_ads_entry *ads_entry;
2156 struct wimfs_context *ctx = wimfs_get_context();
2158 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
2161 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
2165 inode = wim_pathname_to_inode(ctx->wim, path);
2169 ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
2172 inode_remove_ads(inode, ads_idx, ctx->wim->lookup_table);
2177 /* Renames a file or directory. See rename (3) */
2179 wimfs_rename(const char *from, const char *to)
2181 return rename_wim_path(wimfs_get_WIMStruct(), from, to,
2182 WIMLIB_CASE_SENSITIVE, NULL);
2185 /* Remove a directory */
2187 wimfs_rmdir(const char *path)
2189 struct wim_dentry *dentry;
2190 WIMStruct *wim = wimfs_get_WIMStruct();
2192 dentry = get_dentry(wim, path, WIMLIB_CASE_SENSITIVE);
2196 if (!dentry_is_directory(dentry))
2199 if (dentry_has_children(dentry))
2202 remove_dentry(dentry, wim->lookup_table);
2207 /* Write an alternate data stream through the XATTR interface */
2209 wimfs_setxattr(const char *path, const char *name,
2210 const char *value, size_t size, int flags)
2212 struct wim_ads_entry *existing_ads_entry;
2213 struct wim_inode *inode;
2215 struct wimfs_context *ctx = wimfs_get_context();
2218 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
2221 if (strlen(name) <= 5 || memcmp(name, "user.", 5) != 0)
2225 inode = wim_pathname_to_inode(ctx->wim, path);
2229 existing_ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
2230 if (existing_ads_entry) {
2231 if (flags & XATTR_CREATE)
2234 if (flags & XATTR_REPLACE)
2238 ret = inode_add_ads_with_data(inode, name, value,
2239 size, ctx->wim->lookup_table);
2241 if (existing_ads_entry)
2242 inode_remove_ads(inode, ads_idx, ctx->wim->lookup_table);
2251 wimfs_symlink(const char *to, const char *from)
2253 struct fuse_context *fuse_ctx = fuse_get_context();
2254 struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
2255 struct wim_dentry *dentry;
2258 ret = create_dentry(fuse_ctx, from, S_IFLNK | 0777,
2259 FILE_ATTRIBUTE_REPARSE_POINT, &dentry);
2261 dentry->d_inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
2262 ret = wim_inode_set_symlink(dentry->d_inode, to,
2263 wimfs_ctx->wim->lookup_table);
2265 remove_dentry(dentry, wimfs_ctx->wim->lookup_table);
2266 if (ret == WIMLIB_ERR_NOMEM)
2276 /* Reduce the size of a file */
2278 wimfs_truncate(const char *path, off_t size)
2280 struct wim_dentry *dentry;
2281 struct wim_lookup_table_entry *lte;
2285 struct wim_inode *inode;
2286 struct wimfs_context *ctx = wimfs_get_context();
2288 ret = wim_pathname_to_stream(ctx->wim, path, get_lookup_flags(ctx),
2289 &dentry, <e, &stream_idx);
2294 if (lte == NULL && size == 0)
2297 if (lte != NULL && lte->resource_location == RESOURCE_IN_STAGING_FILE) {
2298 ret = truncate(lte->staging_file_name, size);
2304 /* File in WIM. Extract it to the staging directory, but only
2305 * the first @size bytes of it. */
2306 struct wim_lookup_table_entry **back_ptr;
2308 inode = dentry->d_inode;
2309 if (stream_idx == 0) {
2311 back_ptr = &inode->i_lte;
2313 stream_id = inode->i_ads_entries[stream_idx - 1].stream_id;
2314 back_ptr = &inode->i_ads_entries[stream_idx - 1].lte;
2316 ret = extract_resource_to_staging_dir(inode, stream_id,
2323 /* Unlink a non-directory or alternate data stream */
2325 wimfs_unlink(const char *path)
2327 struct wim_dentry *dentry;
2328 struct wim_lookup_table_entry *lte;
2331 struct wimfs_context *ctx = wimfs_get_context();
2333 ret = wim_pathname_to_stream(ctx->wim, path, get_lookup_flags(ctx),
2334 &dentry, <e, &stream_idx);
2339 if (inode_stream_name_nbytes(dentry->d_inode, stream_idx) == 0)
2340 remove_dentry(dentry, ctx->wim->lookup_table);
2342 inode_remove_ads(dentry->d_inode, stream_idx - 1,
2343 ctx->wim->lookup_table);
2347 #ifdef HAVE_UTIMENSAT
2349 * Change the timestamp on a file dentry.
2351 * Note that alternate data streams do not have their own timestamps.
2354 wimfs_utimens(const char *path, const struct timespec tv[2])
2356 struct wim_inode *inode;
2357 WIMStruct *wim = wimfs_get_WIMStruct();
2359 inode = wim_pathname_to_inode(wim, path);
2363 if (tv[0].tv_nsec != UTIME_OMIT) {
2364 if (tv[0].tv_nsec == UTIME_NOW)
2365 inode->i_last_access_time = get_wim_timestamp();
2367 inode->i_last_access_time = timespec_to_wim_timestamp(tv[0]);
2369 if (tv[1].tv_nsec != UTIME_OMIT) {
2370 if (tv[1].tv_nsec == UTIME_NOW)
2371 inode->i_last_write_time = get_wim_timestamp();
2373 inode->i_last_write_time = timespec_to_wim_timestamp(tv[1]);
2377 #else /* HAVE_UTIMENSAT */
2379 wimfs_utime(const char *path, struct utimbuf *times)
2381 struct wim_inode *inode;
2382 WIMStruct *wim = wimfs_get_WIMStruct();
2384 inode = wim_pathname_to_inode(wim, path);
2388 inode->i_last_write_time = unix_timestamp_to_wim(times->modtime);
2389 inode->i_last_access_time = unix_timestamp_to_wim(times->actime);
2392 #endif /* !HAVE_UTIMENSAT */
2394 /* Writes to a file in the WIM filesystem.
2395 * It may be an alternate data stream, but here we don't even notice because we
2396 * just get a lookup table entry. */
2398 wimfs_write(const char *path, const char *buf, size_t size,
2399 off_t offset, struct fuse_file_info *fi)
2401 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
2407 wimlib_assert(fd->f_lte != NULL);
2408 wimlib_assert(fd->f_lte->staging_file_name != NULL);
2409 wimlib_assert(filedes_valid(&fd->staging_fd));
2410 wimlib_assert(fd->f_inode != NULL);
2412 /* Write the data. */
2413 ret = raw_pwrite(&fd->staging_fd, buf, size, offset);
2417 /* Update file size */
2418 if (offset + size > fd->f_lte->size) {
2419 DEBUG("Update file size %"PRIu64 " => %"PRIu64"",
2420 fd->f_lte->size, offset + size);
2421 fd->f_lte->size = offset + size;
2424 /* Update timestamps */
2425 touch_inode(fd->f_inode);
2429 static struct fuse_operations wimfs_operations = {
2430 .chmod = wimfs_chmod,
2431 .chown = wimfs_chown,
2432 .destroy = wimfs_destroy,
2433 .fgetattr = wimfs_fgetattr,
2434 .ftruncate = wimfs_ftruncate,
2435 .getattr = wimfs_getattr,
2437 .getxattr = wimfs_getxattr,
2441 .listxattr = wimfs_listxattr,
2443 .mkdir = wimfs_mkdir,
2444 .mknod = wimfs_mknod,
2446 .opendir = wimfs_opendir,
2448 .readdir = wimfs_readdir,
2449 .readlink = wimfs_readlink,
2450 .release = wimfs_release,
2451 .releasedir = wimfs_releasedir,
2453 .removexattr = wimfs_removexattr,
2455 .rename = wimfs_rename,
2456 .rmdir = wimfs_rmdir,
2458 .setxattr = wimfs_setxattr,
2460 .symlink = wimfs_symlink,
2461 .truncate = wimfs_truncate,
2462 .unlink = wimfs_unlink,
2463 #ifdef HAVE_UTIMENSAT
2464 .utimens = wimfs_utimens,
2466 .utime = wimfs_utime,
2468 .write = wimfs_write,
2470 /* wimfs keeps file descriptor structures (struct wimfs_fd), so there is
2471 * no need to have the file path provided on operations such as read()
2472 * where only the file descriptor is needed. */
2473 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 8)
2474 .flag_nullpath_ok = 1,
2476 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 9)
2478 .flag_utime_omit_ok = 1,
2483 /* API function documented in wimlib.h */
2485 wimlib_mount_image(WIMStruct *wim, int image, const char *dir,
2486 int mount_flags, const char *staging_dir)
2492 struct wim_image_metadata *imd;
2493 struct wimfs_context ctx;
2494 struct wim_inode *inode;
2496 DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
2497 wim, image, dir, mount_flags);
2500 return WIMLIB_ERR_INVALID_PARAM;
2502 if (mount_flags & ~(WIMLIB_MOUNT_FLAG_READWRITE |
2503 WIMLIB_MOUNT_FLAG_DEBUG |
2504 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
2505 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
2506 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS |
2507 WIMLIB_MOUNT_FLAG_UNIX_DATA |
2508 WIMLIB_MOUNT_FLAG_ALLOW_OTHER))
2509 return WIMLIB_ERR_INVALID_PARAM;
2511 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2512 ret = can_delete_from_wim(wim);
2517 ret = select_wim_image(wim, image);
2521 DEBUG("Selected image %d", image);
2523 imd = wim_get_current_image_metadata(wim);
2525 if (imd->modified) {
2526 /* wimfs_read() only supports a limited number of stream
2527 * locations, not including RESOURCE_IN_FILE_ON_DISK,
2528 * RESOURCE_IN_NTFS_VOLUME, etc. that might appear if files were
2529 * added to the WIM image. */
2530 ERROR("Cannot mount an image with newly added files!");
2531 return WIMLIB_ERR_INVALID_PARAM;
2534 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2535 ret = lock_wim(wim, wim->in_fd.fd);
2540 /* Use default stream interface if one was not specified */
2541 if (!(mount_flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
2542 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
2543 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
2544 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
2546 DEBUG("Initializing struct wimfs_context");
2547 init_wimfs_context(&ctx);
2549 ctx.mount_flags = mount_flags;
2550 ctx.image_inode_list = &imd->inode_list;
2551 ctx.default_uid = getuid();
2552 ctx.default_gid = getgid();
2553 wimlib_assert(list_empty(&imd->unhashed_streams));
2554 if (mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
2555 ctx.default_lookup_flags = LOOKUP_FLAG_ADS_OK;
2557 DEBUG("Unlinking message queues in case they already exist");
2558 ret = set_message_queue_names(&ctx, dir);
2561 unlink_message_queues(&ctx);
2563 DEBUG("Preparing arguments to fuse_main()");
2565 dir_copy = STRDUP(dir);
2567 ret = WIMLIB_ERR_NOMEM;
2568 goto out_free_message_queue_names;
2572 argv[argc++] = "wimlib";
2573 argv[argc++] = dir_copy;
2575 /* disable multi-threaded operation */
2576 argv[argc++] = "-s";
2578 if (mount_flags & WIMLIB_MOUNT_FLAG_DEBUG)
2579 argv[argc++] = "-d";
2582 * We provide the use_ino option to the FUSE mount because we are going
2583 * to assign inode numbers ourselves. */
2584 char optstring[256] =
2588 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 8)
2591 ",default_permissions"
2593 argv[argc++] = "-o";
2594 argv[argc++] = optstring;
2595 if ((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
2596 /* Read-write mount. Make the staging directory */
2597 ret = make_staging_dir(&ctx, staging_dir);
2599 goto out_free_dir_copy;
2601 /* Read-only mount */
2602 strcat(optstring, ",ro");
2604 if (mount_flags & WIMLIB_MOUNT_FLAG_ALLOW_OTHER)
2605 strcat(optstring, ",allow_other");
2611 DEBUG("FUSE command line (argc = %d): ", argc);
2612 for (i = 0; i < argc; i++) {
2613 fputs(argv[i], stdout);
2621 /* Assign inode numbers. Also, if a read-write mount was requested,
2622 * mark the dentry tree as modified, and add each stream referenced by
2623 * files in the image to a list and preemptively double the number of
2624 * references to each. The latter is done to allow implementing the
2625 * WIMLIB_UNMOUNT_FLAG_NEW_IMAGE semantics. */
2627 INIT_LIST_HEAD(&ctx.orig_stream_list);
2628 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2630 image_for_each_inode(inode, imd) {
2631 inode->i_ino = ctx.next_ino++;
2632 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
2633 struct wim_lookup_table_entry *lte;
2635 lte = inode_stream_lte(inode, i, wim->lookup_table);
2637 lte->out_refcnt = 0;
2640 image_for_each_inode(inode, imd) {
2641 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
2642 struct wim_lookup_table_entry *lte;
2644 lte = inode_stream_lte(inode, i,
2647 if (lte->out_refcnt == 0)
2648 list_add(<e->orig_stream_list,
2649 &ctx.orig_stream_list);
2650 lte->out_refcnt += inode->i_nlink;
2651 lte->refcnt += inode->i_nlink;
2656 image_for_each_inode(inode, imd)
2657 inode->i_ino = ctx.next_ino++;
2660 DEBUG("(next_ino = %"PRIu64")", ctx.next_ino);
2662 DEBUG("Calling fuse_main()");
2664 ret = fuse_main(argc, argv, &wimfs_operations, &ctx);
2666 DEBUG("Returned from fuse_main() (ret = %d)", ret);
2669 ret = WIMLIB_ERR_FUSE;
2671 if (ctx.have_status)
2674 ret = WIMLIB_ERR_TIMEOUT;
2676 if (ctx.daemon_to_unmount_mq != (mqd_t)(-1)) {
2677 send_unmount_finished_msg(ctx.daemon_to_unmount_mq, ret);
2678 close_message_queues(&ctx);
2681 release_extra_refcnts(&ctx);
2683 /* Try to delete the staging directory if a deletion wasn't yet
2684 * attempted due to an earlier error */
2685 if (ctx.staging_dir_name)
2686 delete_staging_dir(&ctx);
2690 wim->wim_locked = 0;
2691 out_free_message_queue_names:
2692 free_message_queue_names(&ctx);
2696 /* API function documented in wimlib.h */
2698 wimlib_unmount_image_with_progress(const tchar *dir, int unmount_flags,
2699 wimlib_progress_func_t progfunc, void *progctx)
2702 struct wimfs_context wimfs_ctx;
2704 if (unmount_flags & ~(WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY |
2705 WIMLIB_UNMOUNT_FLAG_COMMIT |
2706 WIMLIB_UNMOUNT_FLAG_REBUILD |
2707 WIMLIB_UNMOUNT_FLAG_RECOMPRESS |
2708 WIMLIB_UNMOUNT_FLAG_LAZY |
2709 WIMLIB_UNMOUNT_FLAG_NEW_IMAGE))
2710 return WIMLIB_ERR_INVALID_PARAM;
2712 init_wimfs_context(&wimfs_ctx);
2714 ret = set_message_queue_names(&wimfs_ctx, dir);
2718 ret = open_message_queues(&wimfs_ctx, false);
2720 goto out_free_message_queue_names;
2722 ret = send_unmount_request_msg(wimfs_ctx.unmount_to_daemon_mq,
2726 goto out_close_message_queues;
2728 ret = execute_fusermount(dir, (unmount_flags & WIMLIB_UNMOUNT_FLAG_LAZY) != 0);
2730 goto out_close_message_queues;
2732 struct unmount_msg_handler_context handler_ctx = {
2734 .timeout_seconds = 5,
2737 .progfunc = progfunc,
2741 ret = message_loop(wimfs_ctx.daemon_to_unmount_mq,
2742 &unmount_msg_handler_callbacks,
2745 ret = handler_ctx.status;
2746 out_close_message_queues:
2747 close_message_queues(&wimfs_ctx);
2748 out_free_message_queue_names:
2749 free_message_queue_names(&wimfs_ctx);
2754 #else /* WITH_FUSE */
2758 mount_unsupported_error(void)
2760 #if defined(__WIN32__)
2761 ERROR("Sorry-- Mounting WIM images is not supported on Windows!");
2763 ERROR("wimlib was compiled with --without-fuse, which disables support "
2764 "for mounting WIMs.");
2766 return WIMLIB_ERR_UNSUPPORTED;
2770 wimlib_unmount_image_with_progress(const tchar *dir, int unmount_flags,
2771 wimlib_progress_func_t progfunc, void *progctx)
2773 return mount_unsupported_error();
2777 wimlib_mount_image(WIMStruct *wim, int image, const tchar *dir,
2778 int mount_flags, const tchar *staging_dir)
2780 return mount_unsupported_error();
2783 #endif /* !WITH_FUSE */
2787 wimlib_unmount_image(const tchar *dir, int unmount_flags)
2789 return wimlib_unmount_image_with_progress(dir, unmount_flags, NULL, NULL);