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/reparse.h"
50 #include "wimlib/resource.h"
51 #include "wimlib/timestamp.h"
52 #include "wimlib/version.h"
53 #include "wimlib/write.h"
54 #include "wimlib/xml.h"
65 #include <sys/types.h>
70 #define FUSE_USE_VERSION 26
74 #include <attr/xattr.h>
77 #define MSG_VERSION_TOO_HIGH -1
78 #define MSG_BREAK_LOOP -2
80 /* File descriptor to a file open on the WIM filesystem. */
82 struct wim_inode *f_inode;
83 struct wim_lookup_table_entry *f_lte;
84 struct filedes staging_fd;
89 struct wimfs_context {
90 /* The WIMStruct for the mounted WIM. */
93 /* Name of the staging directory for a read-write mount. Whenever a new file is
94 * created, it is done so in the staging directory. Furthermore, whenever a
95 * file in the WIM is modified, it is extracted to the staging directory. If
96 * changes are commited when the WIM is unmounted, the file resources are merged
97 * in from the staging directory when writing the new WIM. */
98 char *staging_dir_name;
99 size_t staging_dir_name_len;
101 /* Flags passed to wimlib_mount(). */
104 /* Default flags to use when looking up a WIM dentry (depends on whether
105 * the Windows interface to alternate data streams is being used or
107 int default_lookup_flags;
109 /* Next inode number to be assigned. Note: I didn't bother with a
110 * bitmap of free inode numbers since this isn't even a "real"
111 * filesystem anyway. */
114 /* List of inodes in the mounted image */
115 struct list_head *image_inode_list;
117 /* Original list of streams in the mounted image, linked by
118 * mount_orig_stream_list. */
119 struct list_head orig_stream_list;
121 /* Name and message queue descriptors for message queues between the
122 * filesystem daemon process and the unmount process. These are used
123 * when the filesystem is unmounted and the process running
124 * wimlib_unmount_image() needs to communicate with the filesystem
125 * daemon running fuse_main() (i.e. the process created by a call to
126 * wimlib_mount_image(). */
127 char *unmount_to_daemon_mq_name;
128 char *daemon_to_unmount_mq_name;
129 mqd_t unmount_to_daemon_mq;
130 mqd_t daemon_to_unmount_mq;
140 init_wimfs_context(struct wimfs_context *ctx)
142 memset(ctx, 0, sizeof(*ctx));
143 ctx->unmount_to_daemon_mq = (mqd_t)-1;
144 ctx->daemon_to_unmount_mq = (mqd_t)-1;
147 #define WIMFS_CTX(fuse_ctx) ((struct wimfs_context*)(fuse_ctx)->private_data)
149 static inline struct wimfs_context *
150 wimfs_get_context(void)
152 return WIMFS_CTX(fuse_get_context());
155 static inline WIMStruct *
156 wimfs_get_WIMStruct(void)
158 return wimfs_get_context()->wim;
162 get_lookup_flags(const struct wimfs_context *ctx)
164 return ctx->default_lookup_flags;
167 /* Returns nonzero if write permission is requested on the file open flags */
169 flags_writable(int open_flags)
171 int accmode = (open_flags & O_ACCMODE);
172 return (accmode == O_RDWR || accmode == O_WRONLY);
176 * Allocate a file descriptor for a stream.
178 * @inode: inode containing the stream we're opening
179 * @stream_id: ID of the stream we're opening
180 * @lte: Lookup table entry for the stream (may be NULL)
181 * @fd_ret: Return the allocated file descriptor if successful.
183 * Return 0 iff successful or negative error code if unsuccessful.
186 alloc_wimfs_fd(struct wim_inode *inode,
188 struct wim_lookup_table_entry *lte,
189 struct wimfs_fd **fd_ret)
191 static const u16 fds_per_alloc = 8;
192 static const u16 max_fds = 0xffff;
194 DEBUG("Allocating fd for stream ID %u from inode %#"PRIx64" "
195 "(open = %u, allocated = %u)",
196 stream_id, inode->i_ino, inode->i_num_opened_fds,
197 inode->i_num_allocated_fds);
199 if (inode->i_num_opened_fds == inode->i_num_allocated_fds) {
200 struct wimfs_fd **fds;
203 if (inode->i_num_allocated_fds == max_fds)
206 num_new_fds = min(fds_per_alloc,
207 max_fds - inode->i_num_allocated_fds);
209 fds = REALLOC(inode->i_fds,
210 (inode->i_num_allocated_fds + num_new_fds) *
211 sizeof(inode->i_fds[0]));
215 memset(&fds[inode->i_num_allocated_fds], 0,
216 num_new_fds * sizeof(fds[0]));
218 inode->i_num_allocated_fds += num_new_fds;
220 for (u16 i = 0; ; i++) {
221 if (!inode->i_fds[i]) {
222 struct wimfs_fd *fd = CALLOC(1, sizeof(*fd));
228 filedes_invalidate(&fd->staging_fd);
230 fd->stream_id = stream_id;
232 inode->i_fds[i] = fd;
233 inode->i_num_opened_fds++;
235 lte->num_opened_fds++;
236 DEBUG("Allocated fd (idx = %u)", fd->idx);
243 inode_put_fd(struct wim_inode *inode, struct wimfs_fd *fd)
245 wimlib_assert(inode != NULL);
246 wimlib_assert(fd->f_inode == inode);
247 wimlib_assert(inode->i_num_opened_fds != 0);
248 wimlib_assert(fd->idx < inode->i_num_allocated_fds);
249 wimlib_assert(inode->i_fds[fd->idx] == fd);
251 inode->i_fds[fd->idx] = NULL;
253 if (--inode->i_num_opened_fds == 0) {
256 inode->i_num_allocated_fds = 0;
257 if (inode->i_nlink == 0)
263 lte_put_fd(struct wim_lookup_table_entry *lte, struct wimfs_fd *fd)
265 wimlib_assert(fd->f_lte == lte);
267 if (!lte) /* Empty stream with no lookup table entry */
270 /* Close staging file descriptor if needed. */
272 if (lte->resource_location == RESOURCE_IN_STAGING_FILE
273 && filedes_valid(&fd->staging_fd))
275 if (filedes_close(&fd->staging_fd)) {
276 ERROR_WITH_ERRNO("Failed to close staging file");
280 lte_decrement_num_opened_fds(lte);
284 /* Close a file descriptor. */
286 close_wimfs_fd(struct wimfs_fd *fd)
289 DEBUG("Closing fd (ino = %#"PRIx64", opened = %u, allocated = %u)",
290 fd->f_inode->i_ino, fd->f_inode->i_num_opened_fds,
291 fd->f_inode->i_num_allocated_fds);
292 ret = lte_put_fd(fd->f_lte, fd);
296 inode_put_fd(fd->f_inode, fd);
301 fuse_mask_mode(mode_t mode, struct fuse_context *fuse_ctx)
303 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 8)
304 mode &= ~fuse_ctx->umask;
310 * Add a new dentry with a new inode to a WIM image.
312 * Returns 0 on success, or negative error number on failure.
315 create_dentry(struct fuse_context *fuse_ctx, const char *path,
316 mode_t mode, int attributes, struct wim_dentry **dentry_ret)
318 struct wim_dentry *parent;
319 struct wim_dentry *new;
320 const char *basename;
321 struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
324 parent = get_parent_dentry(wimfs_ctx->wim, path, WIMLIB_CASE_SENSITIVE);
328 if (!dentry_is_directory(parent))
331 basename = path_basename(path);
332 if (get_dentry_child_with_name(parent, basename, WIMLIB_CASE_SENSITIVE))
335 ret = new_dentry_with_inode(basename, &new);
339 new->d_inode->i_resolved = 1;
340 new->d_inode->i_ino = wimfs_ctx->next_ino++;
341 new->d_inode->i_attributes = attributes;
343 if (wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA) {
344 if (inode_set_unix_data(new->d_inode,
347 fuse_mask_mode(mode, fuse_ctx),
348 wimfs_ctx->wim->lookup_table,
349 UNIX_DATA_ALL | UNIX_DATA_CREATE))
355 dentry_add_child(parent, new);
356 list_add_tail(&new->d_inode->i_list, wimfs_ctx->image_inode_list);
362 static struct wim_inode *
363 wim_pathname_to_inode(WIMStruct *wim, const tchar *path)
365 struct wim_dentry *dentry;
366 dentry = get_dentry(wim, path, WIMLIB_CASE_SENSITIVE);
368 return dentry->d_inode;
373 /* Remove a dentry from a mounted WIM image; i.e. remove an alias for the
374 * corresponding inode.
376 * If there are no remaining references to the inode either through dentries or
377 * open file descriptors, the inode is freed. Otherwise, the inode is not
378 * removed, but the dentry is unlinked and freed.
380 * Either way, all lookup table entries referenced by the inode have their
381 * reference count decremented. If a lookup table entry has no open file
382 * descriptors and no references remaining, it is freed, and the corresponding
383 * staging file is unlinked.
386 remove_dentry(struct wim_dentry *dentry,
387 struct wim_lookup_table *lookup_table)
389 struct wim_inode *inode = dentry->d_inode;
390 struct wim_lookup_table_entry *lte;
393 for (i = 0; i <= inode->i_num_ads; i++) {
394 lte = inode_stream_lte(inode, i, lookup_table);
396 lte_decrement_refcnt(lte, lookup_table);
398 unlink_dentry(dentry);
403 inode_default_unix_mode(const struct wim_inode *inode)
405 if (inode_is_symlink(inode))
406 return S_IFLNK | 0777;
407 else if (inode_is_directory(inode))
408 return S_IFDIR | 0777;
410 return S_IFREG | 0777;
413 /* Transfers file attributes from a struct wim_inode to a `stat' buffer.
415 * The lookup table entry tells us which stream in the inode we are statting.
416 * For a named data stream, everything returned is the same as the unnamed data
417 * stream except possibly the size and block count. */
419 inode_to_stbuf(const struct wim_inode *inode,
420 const struct wim_lookup_table_entry *lte,
423 const struct wimfs_context *ctx = wimfs_get_context();
425 memset(stbuf, 0, sizeof(struct stat));
426 stbuf->st_mode = inode_default_unix_mode(inode);
427 stbuf->st_uid = ctx->default_uid;
428 stbuf->st_gid = ctx->default_gid;
429 if (ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA) {
430 struct wimlib_unix_data unix_data;
431 if (inode_get_unix_data(inode, &unix_data, NULL) == 0) {
432 stbuf->st_uid = unix_data.uid;
433 stbuf->st_gid = unix_data.gid;
434 stbuf->st_mode = unix_data.mode;
437 stbuf->st_ino = (ino_t)inode->i_ino;
438 stbuf->st_nlink = inode->i_nlink;
440 stbuf->st_size = lte->size;
443 #ifdef HAVE_STAT_NANOSECOND_PRECISION
444 stbuf->st_atim = wim_timestamp_to_timespec(inode->i_last_access_time);
445 stbuf->st_mtim = wim_timestamp_to_timespec(inode->i_last_write_time);
446 stbuf->st_ctim = stbuf->st_mtim;
448 stbuf->st_atime = wim_timestamp_to_unix(inode->i_last_access_time);
449 stbuf->st_mtime = wim_timestamp_to_unix(inode->i_last_write_time);
450 stbuf->st_ctime = stbuf->st_mtime;
452 stbuf->st_blocks = (stbuf->st_size + 511) / 512;
457 touch_inode(struct wim_inode *inode)
459 u64 now = get_wim_timestamp();
460 inode->i_last_access_time = now;
461 inode->i_last_write_time = now;
464 /* Creates a new staging file and returns its file descriptor opened for
467 * @name_ret: A location into which the a pointer to the newly allocated name of
468 * the staging file is stored.
470 * @ctx: Context for the WIM filesystem; this provides the name of the
473 * On success, returns the file descriptor for the staging file, opened for
474 * writing. On failure, returns -1 and sets errno.
477 create_staging_file(char **name_ret, struct wimfs_context *ctx)
485 static const size_t STAGING_FILE_NAME_LEN = 20;
487 name_len = ctx->staging_dir_name_len + 1 + STAGING_FILE_NAME_LEN;
488 name = MALLOC(name_len + 1);
496 memcpy(name, ctx->staging_dir_name, ctx->staging_dir_name_len);
497 name[ctx->staging_dir_name_len] = '/';
498 randomize_char_array_with_alnum(name + ctx->staging_dir_name_len + 1,
499 STAGING_FILE_NAME_LEN);
500 name[name_len] = '\0';
503 /* Just in case, verify that the randomly generated name doesn't name an
504 * existing file, and try again if so */
505 } while (stat(name, &stbuf) == 0);
507 if (errno != ENOENT) /* other error?! */
510 /* doesn't exist--- ok */
512 DEBUG("Creating staging file `%s'", name);
514 fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0600);
526 * Extract a WIM resource to the staging directory.
528 * @inode: Inode that contains the stream we are extracting
530 * @stream_id: Identifier for the stream (it stays constant even if the indices
531 * of the stream entries are changed)
533 * @lte: Pointer to pointer to the lookup table entry for the stream we need to
534 * extract, or NULL if there was no lookup table entry present for the stream
536 * @size: Number of bytes of the stream we want to extract (this supports the
537 * wimfs_truncate() function). It may be more than the actual stream length, in
538 * which case the extra space is filled with zeroes.
540 * @ctx: Context for the WIM filesystem.
542 * Returns 0 on success or a negative error code on failure.
545 extract_resource_to_staging_dir(struct wim_inode *inode,
547 struct wim_lookup_table_entry **lte,
549 struct wimfs_context *ctx)
551 char *staging_file_name;
554 struct wim_lookup_table_entry *old_lte, *new_lte;
557 DEBUG("Extracting resource to staging dir: inode %"PRIu64", "
558 "stream id %"PRIu32, inode->i_ino, stream_id);
562 wimlib_assert(old_lte == NULL ||
563 old_lte->resource_location != RESOURCE_IN_STAGING_FILE);
565 /* Create the staging file */
566 fd = create_staging_file(&staging_file_name, ctx);
570 /* Extract the stream to the staging file (possibly truncated) */
572 struct filedes wimlib_fd;
573 filedes_init(&wimlib_fd, fd);
574 extract_size = min(old_lte->size, size);
575 ret = extract_stream_to_fd(old_lte, &wimlib_fd, extract_size);
581 /* In the case of truncate() to more than the file length, extend the
582 * file with zeroes by calling ftruncate() on the underlying staging
584 if (ret == 0 && size > extract_size)
585 ret = ftruncate(fd, size);
587 /* Close the staging file descriptor and check for errors. If there's
588 * an error, unlink the staging file. */
589 if (ret != 0 || close(fd) != 0) {
595 goto out_delete_staging_file;
598 /* Now deal with the lookup table entries. We may be able to re-use the
599 * existing entry, but we may have to create a new one instead. */
601 if (old_lte && inode->i_nlink == old_lte->refcnt) {
602 /* The reference count of the existing lookup table entry is the
603 * same as the link count of the inode that contains the stream
604 * we're opening. Therefore, ALL the references to the lookup
605 * table entry correspond to the stream we're trying to extract,
606 * so the lookup table entry can be re-used. */
607 DEBUG("Re-using lookup table entry");
608 lookup_table_unlink(ctx->wim->lookup_table, old_lte);
612 /* There's an existing lookup table entry, but its
613 * reference count is greater than the link count for
614 * the inode containing a stream we're opening.
615 * Therefore, we need to split the lookup table entry.
617 wimlib_assert(old_lte->refcnt > inode->i_nlink);
618 DEBUG("Splitting lookup table entry "
619 "(inode->i_nlink = %u, old_lte->refcnt = %u)",
620 inode->i_nlink, old_lte->refcnt);
623 new_lte = new_lookup_table_entry();
626 goto out_delete_staging_file;
629 /* There may already be open file descriptors to this stream if
630 * it's previously been opened read-only, but just now we're
631 * opening it read-write. Identify those file descriptors and
632 * change their lookup table entry pointers to point to the new
633 * lookup table entry, and open staging file descriptors for
636 * At the same time, we need to count the number of these opened
637 * file descriptors to the new lookup table entry. If there's
638 * an old lookup table entry, this number needs to be subtracted
639 * from the fd's opened to the old entry. */
640 for (u16 i = 0, j = 0; j < inode->i_num_opened_fds; i++) {
641 struct wimfs_fd *fd = inode->i_fds[i];
643 if (fd->stream_id == stream_id) {
646 wimlib_assert(fd->f_lte == old_lte);
647 wimlib_assert(!filedes_valid(&fd->staging_fd));
649 new_lte->num_opened_fds++;
650 raw_fd = open(staging_file_name, O_RDONLY);
653 goto out_revert_fd_changes;
655 filedes_init(&fd->staging_fd, raw_fd);
660 DEBUG("%hu fd's were already opened to the file we extracted",
661 new_lte->num_opened_fds);
663 old_lte->num_opened_fds -= new_lte->num_opened_fds;
664 old_lte->refcnt -= inode->i_nlink;
668 lte_put_resource(new_lte);
669 new_lte->refcnt = inode->i_nlink;
670 new_lte->resource_location = RESOURCE_IN_STAGING_FILE;
671 new_lte->staging_file_name = staging_file_name;
672 new_lte->size = size;
674 add_unhashed_stream(new_lte, inode, stream_id,
675 &wim_get_current_image_metadata(ctx->wim)->unhashed_streams);
676 *retrieve_lte_pointer(new_lte) = new_lte;
679 out_revert_fd_changes:
680 for (u16 i = 0, j = 0; j < new_lte->num_opened_fds; i++) {
681 struct wimfs_fd *fd = inode->i_fds[i];
682 if (fd && fd->stream_id == stream_id && fd->f_lte == new_lte) {
684 if (filedes_valid(&fd->staging_fd)) {
685 filedes_close(&fd->staging_fd);
686 filedes_invalidate(&fd->staging_fd);
691 free_lookup_table_entry(new_lte);
692 out_delete_staging_file:
693 unlink(staging_file_name);
694 FREE(staging_file_name);
699 * Creates a randomly named staging directory and saves its name in the
700 * filesystem context structure.
703 make_staging_dir(struct wimfs_context *ctx, const char *user_prefix)
705 static const size_t random_suffix_len = 10;
706 static const char *common_suffix = ".staging";
707 static const size_t common_suffix_len = 8;
709 char *staging_dir_name = NULL;
710 size_t staging_dir_name_len;
712 const char *wim_basename;
713 char *real_user_prefix = NULL;
717 real_user_prefix = realpath(user_prefix, NULL);
718 if (!real_user_prefix) {
719 ERROR_WITH_ERRNO("Could not resolve `%s'",
721 ret = WIMLIB_ERR_NOTDIR;
724 wim_basename = path_basename(ctx->wim->filename);
725 prefix_len = strlen(real_user_prefix) + 1 + strlen(wim_basename);
727 prefix_len = strlen(ctx->wim->filename);
730 staging_dir_name_len = prefix_len + common_suffix_len + random_suffix_len;
732 staging_dir_name = MALLOC(staging_dir_name_len + 1);
733 if (!staging_dir_name) {
734 ret = WIMLIB_ERR_NOMEM;
738 if (real_user_prefix)
739 sprintf(staging_dir_name, "%s/%s", real_user_prefix, wim_basename);
741 strcpy(staging_dir_name, ctx->wim->filename);
743 strcat(staging_dir_name, common_suffix);
745 randomize_char_array_with_alnum(staging_dir_name + prefix_len + common_suffix_len,
748 staging_dir_name[staging_dir_name_len] = '\0';
750 if (mkdir(staging_dir_name, 0700) != 0) {
751 ERROR_WITH_ERRNO("Failed to create temporary directory `%s'",
753 ret = WIMLIB_ERR_MKDIR;
758 FREE(real_user_prefix);
760 ctx->staging_dir_name = staging_dir_name;
761 ctx->staging_dir_name_len = staging_dir_name_len;
763 FREE(staging_dir_name);
769 remove_file_or_directory(const char *fpath, const struct stat *sb,
770 int typeflag, struct FTW *ftwbuf)
772 if (remove(fpath) == 0)
775 ERROR_WITH_ERRNO("Cannot remove `%s'", fpath);
776 return WIMLIB_ERR_DELETE_STAGING_DIR;
781 * Deletes the staging directory and all the files contained in it.
784 delete_staging_dir(struct wimfs_context *ctx)
787 ret = nftw(ctx->staging_dir_name, remove_file_or_directory,
789 FREE(ctx->staging_dir_name);
790 ctx->staging_dir_name = NULL;
795 inode_close_fds(struct wim_inode *inode)
797 u16 num_opened_fds = inode->i_num_opened_fds;
798 for (u16 i = 0, j = 0; j < num_opened_fds; i++) {
799 struct wimfs_fd *fd = inode->i_fds[i];
801 wimlib_assert(fd->f_inode == inode);
802 int ret = close_wimfs_fd(fd);
811 /* Overwrites the WIM file, with changes saved. */
813 rebuild_wim(struct wimfs_context *ctx, int write_flags,
814 wimlib_progress_func_t progress_func)
817 struct wim_lookup_table_entry *lte, *tmp;
818 WIMStruct *wim = ctx->wim;
819 struct wim_image_metadata *imd = wim_get_current_image_metadata(ctx->wim);
821 DEBUG("Closing all staging file descriptors.");
822 image_for_each_unhashed_stream_safe(lte, tmp, imd) {
823 ret = inode_close_fds(lte->back_inode);
828 DEBUG("Freeing entries for zero-length streams");
829 image_for_each_unhashed_stream_safe(lte, tmp, imd) {
830 wimlib_assert(lte->unhashed);
831 if (lte->size == 0) {
832 struct wim_lookup_table_entry **back_ptr;
833 back_ptr = retrieve_lte_pointer(lte);
835 list_del(<e->unhashed_list);
836 free_lookup_table_entry(lte);
840 xml_update_image_info(wim, wim->current_image);
841 ret = wimlib_overwrite(wim, write_flags, 0, progress_func);
843 ERROR("Failed to commit changes to mounted WIM image");
847 /* Simple function that returns the concatenation of 2 strings. */
849 strcat_dup(const char *s1, const char *s2, size_t max_len)
851 size_t len = strlen(s1) + strlen(s2);
854 char *p = MALLOC(len + 1);
857 snprintf(p, len + 1, "%s%s", s1, s2);
862 set_message_queue_names(struct wimfs_context *ctx, const char *mount_dir)
864 static const char *u2d_prefix = "/wimlib-unmount-to-daemon-mq";
865 static const char *d2u_prefix = "/wimlib-daemon-to-unmount-mq";
870 dir_path = realpath(mount_dir, NULL);
872 ERROR_WITH_ERRNO("Failed to resolve path \"%s\"", mount_dir);
874 return WIMLIB_ERR_NOMEM;
876 return WIMLIB_ERR_NOTDIR;
879 for (p = dir_path; *p; p++)
883 ctx->unmount_to_daemon_mq_name = strcat_dup(u2d_prefix, dir_path,
885 if (!ctx->unmount_to_daemon_mq_name) {
886 ret = WIMLIB_ERR_NOMEM;
887 goto out_free_dir_path;
889 ctx->daemon_to_unmount_mq_name = strcat_dup(d2u_prefix, dir_path,
891 if (!ctx->daemon_to_unmount_mq_name) {
892 ret = WIMLIB_ERR_NOMEM;
893 goto out_free_unmount_to_daemon_mq_name;
897 goto out_free_dir_path;
898 out_free_unmount_to_daemon_mq_name:
899 FREE(ctx->unmount_to_daemon_mq_name);
900 ctx->unmount_to_daemon_mq_name = NULL;
907 free_message_queue_names(struct wimfs_context *ctx)
909 FREE(ctx->unmount_to_daemon_mq_name);
910 FREE(ctx->daemon_to_unmount_mq_name);
911 ctx->unmount_to_daemon_mq_name = NULL;
912 ctx->daemon_to_unmount_mq_name = NULL;
916 * Opens two POSIX message queue: one for sending messages from the unmount
917 * process to the daemon process, and one to go the other way. The names of the
918 * message queues, which must be system-wide unique, are be based on the mount
921 * @daemon specifies whether the calling process is the filesystem daemon or the
925 open_message_queues(struct wimfs_context *ctx, bool daemon)
927 int unmount_to_daemon_mq_flags = O_WRONLY | O_CREAT;
928 int daemon_to_unmount_mq_flags = O_RDONLY | O_CREAT;
934 swap(unmount_to_daemon_mq_flags, daemon_to_unmount_mq_flags);
940 orig_umask = umask(0000);
941 DEBUG("Opening message queue \"%s\"", ctx->unmount_to_daemon_mq_name);
942 ctx->unmount_to_daemon_mq = mq_open(ctx->unmount_to_daemon_mq_name,
943 unmount_to_daemon_mq_flags, mode, NULL);
945 if (ctx->unmount_to_daemon_mq == (mqd_t)-1) {
946 ERROR_WITH_ERRNO("mq_open()");
947 ret = WIMLIB_ERR_MQUEUE;
951 DEBUG("Opening message queue \"%s\"", ctx->daemon_to_unmount_mq_name);
952 ctx->daemon_to_unmount_mq = mq_open(ctx->daemon_to_unmount_mq_name,
953 daemon_to_unmount_mq_flags, mode, NULL);
955 if (ctx->daemon_to_unmount_mq == (mqd_t)-1) {
956 ERROR_WITH_ERRNO("mq_open()");
957 mq_close(ctx->unmount_to_daemon_mq);
958 mq_unlink(ctx->unmount_to_daemon_mq_name);
959 ctx->unmount_to_daemon_mq = (mqd_t)-1;
960 ret = WIMLIB_ERR_MQUEUE;
969 /* Try to determine the maximum message size of a message queue. The return
970 * value is the maximum message size, or a guess of 8192 bytes if it cannot be
973 mq_get_msgsize(mqd_t mq)
975 static const char *msgsize_max_file = "/proc/sys/fs/mqueue/msgsize_max";
980 if (mq_getattr(mq, &attr) == 0) {
981 msgsize = attr.mq_msgsize;
983 ERROR_WITH_ERRNO("mq_getattr()");
984 ERROR("Attempting to read %s", msgsize_max_file);
985 fp = fopen(msgsize_max_file, "rb");
987 if (fscanf(fp, "%ld", &msgsize) != 1) {
988 ERROR("Assuming message size of 8192");
993 ERROR_WITH_ERRNO("Failed to open the file `%s'",
995 ERROR("Assuming message size of 8192");
1003 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;
1028 unlink_message_queues(struct wimfs_context *ctx)
1030 mq_unlink(ctx->unmount_to_daemon_mq_name);
1031 mq_unlink(ctx->daemon_to_unmount_mq_name);
1034 /* Closes the message queues, which are allocated in static variables */
1036 close_message_queues(struct wimfs_context *ctx)
1038 DEBUG("Closing message queues");
1039 mq_close(ctx->unmount_to_daemon_mq);
1040 ctx->unmount_to_daemon_mq = (mqd_t)(-1);
1041 mq_close(ctx->daemon_to_unmount_mq);
1042 ctx->daemon_to_unmount_mq = (mqd_t)(-1);
1043 unlink_message_queues(ctx);
1047 struct unmount_msg_hdr {
1052 } _packed_attribute;
1054 struct msg_unmount_request {
1055 struct unmount_msg_hdr hdr;
1057 u8 want_progress_messages;
1058 } _packed_attribute;
1060 struct msg_daemon_info {
1061 struct unmount_msg_hdr hdr;
1064 } _packed_attribute;
1066 struct msg_unmount_finished {
1067 struct unmount_msg_hdr hdr;
1069 } _packed_attribute;
1071 struct msg_write_streams_progress {
1072 struct unmount_msg_hdr hdr;
1073 union wimlib_progress_info info;
1074 } _packed_attribute;
1077 MSG_TYPE_UNMOUNT_REQUEST,
1078 MSG_TYPE_DAEMON_INFO,
1079 MSG_TYPE_WRITE_STREAMS_PROGRESS,
1080 MSG_TYPE_UNMOUNT_FINISHED,
1084 struct msg_handler_context_hdr {
1085 int timeout_seconds;
1088 struct unmount_msg_handler_context {
1089 struct msg_handler_context_hdr hdr;
1093 wimlib_progress_func_t progress_func;
1096 struct daemon_msg_handler_context {
1097 struct msg_handler_context_hdr hdr;
1098 struct wimfs_context *wimfs_ctx;
1102 send_unmount_request_msg(mqd_t mq, int unmount_flags, u8 want_progress_messages)
1104 DEBUG("Sending unmount request msg");
1105 struct msg_unmount_request msg = {
1107 .min_version = ((unmount_flags & WIMLIB_UNMOUNT_FLAG_NEW_IMAGE) ?
1108 WIMLIB_MAKEVERSION(1, 6, 2) :
1109 WIMLIB_MAKEVERSION(1, 2, 1)),
1110 .cur_version = WIMLIB_VERSION_CODE,
1111 .msg_type = MSG_TYPE_UNMOUNT_REQUEST,
1112 .msg_size = sizeof(msg),
1114 .unmount_flags = unmount_flags,
1115 .want_progress_messages = want_progress_messages,
1118 if (mq_send(mq, (void*)&msg, sizeof(msg), 1)) {
1119 ERROR_WITH_ERRNO("Failed to communicate with filesystem daemon");
1120 return WIMLIB_ERR_MQUEUE;
1126 send_daemon_info_msg(mqd_t mq, pid_t pid, int mount_flags)
1128 DEBUG("Sending daemon info msg (pid = %d, mount_flags=%x)",
1131 struct msg_daemon_info msg = {
1133 .min_version = WIMLIB_MAKEVERSION(1, 2, 1),
1134 .cur_version = WIMLIB_VERSION_CODE,
1135 .msg_type = MSG_TYPE_DAEMON_INFO,
1136 .msg_size = sizeof(msg),
1139 .mount_flags = mount_flags,
1141 if (mq_send(mq, (void*)&msg, sizeof(msg), 1)) {
1142 ERROR_WITH_ERRNO("Failed to send daemon info to unmount process");
1143 return WIMLIB_ERR_MQUEUE;
1149 send_unmount_finished_msg(mqd_t mq, int status)
1151 DEBUG("Sending unmount finished msg");
1152 struct msg_unmount_finished msg = {
1154 .min_version = WIMLIB_MAKEVERSION(1, 2, 1),
1155 .cur_version = WIMLIB_VERSION_CODE,
1156 .msg_type = MSG_TYPE_UNMOUNT_FINISHED,
1157 .msg_size = sizeof(msg),
1161 if (mq_send(mq, (void*)&msg, sizeof(msg), 1))
1162 ERROR_WITH_ERRNO("Failed to send status to unmount process");
1166 unmount_progress_func(enum wimlib_progress_msg msg,
1167 const union wimlib_progress_info *info)
1169 if (msg == WIMLIB_PROGRESS_MSG_WRITE_STREAMS) {
1170 struct msg_write_streams_progress msg = {
1172 .min_version = WIMLIB_MAKEVERSION(1, 2, 1),
1173 .cur_version = WIMLIB_VERSION_CODE,
1174 .msg_type = MSG_TYPE_WRITE_STREAMS_PROGRESS,
1175 .msg_size = sizeof(msg),
1179 if (mq_send(wimfs_get_context()->daemon_to_unmount_mq,
1180 (void*)&msg, sizeof(msg), 1))
1182 ERROR_WITH_ERRNO("Failed to send progress information "
1183 "to unmount process");
1190 release_extra_refcnts(struct wimfs_context *ctx)
1192 struct list_head *list = &ctx->orig_stream_list;
1193 struct wim_lookup_table *lookup_table = ctx->wim->lookup_table;
1194 struct wim_lookup_table_entry *lte, *tmp;
1196 list_for_each_entry_safe(lte, tmp, list, orig_stream_list) {
1197 u32 n = lte->out_refcnt;
1199 lte_decrement_refcnt(lte, lookup_table);
1203 /* Moves the currently selected image, which may have been modified, to a new
1204 * index, and sets the original index to refer to a reset (unmodified) copy of
1207 renew_current_image(struct wimfs_context *ctx)
1209 WIMStruct *wim = ctx->wim;
1211 int idx = wim->current_image - 1;
1212 struct wim_image_metadata *imd = wim->image_metadata[idx];
1213 struct wim_image_metadata *replace_imd;
1214 struct wim_lookup_table_entry *new_lte;
1216 if (imd->metadata_lte->resource_location != RESOURCE_IN_WIM) {
1217 ERROR("Can't reset modified image that doesn't yet "
1218 "exist in the on-disk WIM file!");
1219 return WIMLIB_ERR_METADATA_NOT_FOUND;
1222 /* Create 'replace_imd' structure to use for the reset original,
1223 * unmodified image. */
1224 replace_imd = new_image_metadata();
1226 return WIMLIB_ERR_NOMEM;
1228 /* Create new stream reference for the modified image's metadata
1229 * resource, which doesn't exist yet. */
1230 ret = WIMLIB_ERR_NOMEM;
1231 new_lte = new_lookup_table_entry();
1233 goto err_put_replace_imd;
1234 new_lte->flags = WIM_RESHDR_FLAG_METADATA;
1235 new_lte->unhashed = 1;
1237 /* Make the image being moved available at a new index. Increments the
1238 * WIM's image count, but does not increment the reference count of the
1239 * 'struct image_metadata'. */
1240 ret = append_image_metadata(wim, imd);
1242 goto err_free_new_lte;
1244 ret = xml_add_image(wim, T(""));
1246 goto err_undo_append;
1248 replace_imd->metadata_lte = imd->metadata_lte;
1249 imd->metadata_lte = new_lte;
1250 wim->image_metadata[idx] = replace_imd;
1251 wim->current_image = wim->hdr.image_count;
1255 wim->hdr.image_count--;
1257 free_lookup_table_entry(new_lte);
1258 err_put_replace_imd:
1259 put_image_metadata(replace_imd, NULL);
1264 msg_unmount_request_handler(const void *_msg, void *_handler_ctx)
1266 const struct msg_unmount_request *msg = _msg;
1267 struct daemon_msg_handler_context *handler_ctx = _handler_ctx;
1268 struct wimfs_context *wimfs_ctx;
1272 wimlib_progress_func_t progress_func;
1274 DEBUG("Handling unmount request msg");
1276 wimfs_ctx = handler_ctx->wimfs_ctx;
1277 if (msg->hdr.msg_size < sizeof(*msg)) {
1278 status = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1282 unmount_flags = msg->unmount_flags;
1283 if (msg->want_progress_messages)
1284 progress_func = unmount_progress_func;
1286 progress_func = NULL;
1288 ret = send_daemon_info_msg(wimfs_ctx->daemon_to_unmount_mq, getpid(),
1289 wimfs_ctx->mount_flags);
1295 if (wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1296 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_COMMIT) {
1298 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_NEW_IMAGE) {
1299 ret = renew_current_image(wimfs_ctx);
1305 release_extra_refcnts(wimfs_ctx);
1307 INIT_LIST_HEAD(&wimfs_ctx->orig_stream_list);
1309 int write_flags = 0;
1310 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY)
1311 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1312 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_REBUILD)
1313 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1314 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_RECOMPRESS)
1315 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
1316 status = rebuild_wim(wimfs_ctx, write_flags,
1320 DEBUG("Read-only mount");
1325 if (wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1326 ret = delete_staging_dir(wimfs_ctx);
1328 ERROR("Failed to delete the staging directory");
1333 wimfs_ctx->status = status;
1334 wimfs_ctx->have_status = true;
1335 return MSG_BREAK_LOOP;
1339 msg_daemon_info_handler(const void *_msg, void *_handler_ctx)
1341 const struct msg_daemon_info *msg = _msg;
1342 struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1344 DEBUG("Handling daemon info msg");
1345 if (msg->hdr.msg_size < sizeof(*msg))
1346 return WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1347 handler_ctx->daemon_pid = msg->daemon_pid;
1348 handler_ctx->mount_flags = msg->mount_flags;
1349 handler_ctx->hdr.timeout_seconds = 1;
1350 DEBUG("pid of daemon is %d; mount flags were %#x",
1351 handler_ctx->daemon_pid,
1352 handler_ctx->mount_flags);
1357 msg_write_streams_progress_handler(const void *_msg, void *_handler_ctx)
1359 const struct msg_write_streams_progress *msg = _msg;
1360 struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1362 if (msg->hdr.msg_size < sizeof(*msg))
1363 return WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1364 if (handler_ctx->progress_func) {
1365 handler_ctx->progress_func(WIMLIB_PROGRESS_MSG_WRITE_STREAMS,
1372 msg_unmount_finished_handler(const void *_msg, void *_handler_ctx)
1374 const struct msg_unmount_finished *msg = _msg;
1375 struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1377 DEBUG("Handling unmount finished message");
1378 if (msg->hdr.msg_size < sizeof(*msg))
1379 return WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1380 handler_ctx->status = msg->status;
1381 DEBUG("status is %d", handler_ctx->status);
1382 return MSG_BREAK_LOOP;
1386 unmount_timed_out_cb(void *_handler_ctx)
1388 const struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1390 if (handler_ctx->daemon_pid == 0 ||
1391 (kill(handler_ctx->daemon_pid, 0) != 0 && errno == ESRCH))
1393 ERROR("The filesystem daemon has crashed! Changes to the "
1394 "WIM may not have been commited.");
1395 return WIMLIB_ERR_FILESYSTEM_DAEMON_CRASHED;
1398 DEBUG("Filesystem daemon is still alive... "
1399 "Waiting another %d seconds", handler_ctx->hdr.timeout_seconds);
1404 daemon_timed_out_cb(void *_handler_ctx)
1406 ERROR("Timed out waiting for unmount request! "
1407 "Changes to the mounted WIM will not be committed.");
1408 return WIMLIB_ERR_TIMEOUT;
1411 typedef int (*msg_handler_t)(const void *_msg, void *_handler_ctx);
1413 struct msg_handler_callbacks {
1414 int (*timed_out)(void * _handler_ctx);
1415 msg_handler_t msg_handlers[MSG_TYPE_MAX];
1418 static const struct msg_handler_callbacks unmount_msg_handler_callbacks = {
1419 .timed_out = unmount_timed_out_cb,
1421 [MSG_TYPE_DAEMON_INFO] = msg_daemon_info_handler,
1422 [MSG_TYPE_WRITE_STREAMS_PROGRESS] = msg_write_streams_progress_handler,
1423 [MSG_TYPE_UNMOUNT_FINISHED] = msg_unmount_finished_handler,
1427 static const struct msg_handler_callbacks daemon_msg_handler_callbacks = {
1428 .timed_out = daemon_timed_out_cb,
1430 [MSG_TYPE_UNMOUNT_REQUEST] = msg_unmount_request_handler,
1435 receive_message(mqd_t mq,
1436 struct msg_handler_context_hdr *handler_ctx,
1437 const msg_handler_t msg_handlers[],
1438 long mailbox_size, void *mailbox)
1441 struct timespec timeout;
1442 ssize_t bytes_received;
1443 struct unmount_msg_hdr *hdr;
1446 gettimeofday(&now, NULL);
1447 timeout.tv_sec = now.tv_sec + handler_ctx->timeout_seconds;
1448 timeout.tv_nsec = now.tv_usec * 1000;
1450 bytes_received = mq_timedreceive(mq, mailbox,
1451 mailbox_size, NULL, &timeout);
1453 if (bytes_received == -1) {
1454 if (errno == ETIMEDOUT) {
1455 ret = WIMLIB_ERR_TIMEOUT;
1457 ERROR_WITH_ERRNO("mq_timedreceive()");
1458 ret = WIMLIB_ERR_MQUEUE;
1460 } else if (bytes_received < sizeof(*hdr) ||
1461 bytes_received != hdr->msg_size) {
1462 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1463 } else if (WIMLIB_VERSION_CODE < hdr->min_version) {
1464 /*ERROR("Cannot understand the received message. "*/
1465 /*"Please upgrade wimlib to at least v%d.%d.%d",*/
1466 /*WIMLIB_GET_MAJOR_VERSION(hdr->min_version),*/
1467 /*WIMLIB_GET_MINOR_VERSION(hdr->min_version),*/
1468 /*WIMLIB_GET_PATCH_VERSION(hdr->min_version));*/
1469 ret = MSG_VERSION_TOO_HIGH;
1470 } else if (hdr->msg_type >= MSG_TYPE_MAX) {
1471 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1472 } else if (msg_handlers[hdr->msg_type] == NULL) {
1473 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1475 ret = msg_handlers[hdr->msg_type](mailbox, handler_ctx);
1481 message_loop(mqd_t mq,
1482 const struct msg_handler_callbacks *callbacks,
1483 struct msg_handler_context_hdr *handler_ctx)
1485 static const size_t MAX_MSG_SIZE = 512;
1490 DEBUG("Entering message loop");
1492 ret = get_mailbox(mq, MAX_MSG_SIZE, &msgsize, &mailbox);
1496 ret = receive_message(mq, handler_ctx,
1497 callbacks->msg_handlers,
1499 if (ret == 0 || ret == MSG_VERSION_TOO_HIGH) {
1501 } else if (ret == MSG_BREAK_LOOP) {
1504 } else if (ret == WIMLIB_ERR_TIMEOUT) {
1505 if (callbacks->timed_out)
1506 ret = callbacks->timed_out(handler_ctx);
1512 ERROR_WITH_ERRNO("Error communicating with "
1513 "filesystem daemon");
1518 DEBUG("Exiting message loop");
1522 /* Execute `fusermount -u', which is installed setuid root, to unmount the WIM.
1524 * FUSE does not yet implement synchronous unmounts. This means that fusermount
1525 * -u will return before the filesystem daemon returns from wimfs_destroy().
1526 * This is partly what we want, because we need to send a message from this
1527 * process to the filesystem daemon telling whether --commit was specified or
1528 * not. However, after that, the unmount process must wait for the filesystem
1529 * daemon to finish writing the WIM file.
1532 execute_fusermount(const char *dir, bool lazy)
1540 ERROR_WITH_ERRNO("Failed to fork()");
1541 return WIMLIB_ERR_FORK;
1547 *argp++ = "fusermount";
1551 *argp++ = (char*)dir;
1553 execvp("fusermount", argv);
1554 ERROR_WITH_ERRNO("Failed to execute `fusermount'");
1555 exit(WIMLIB_ERR_FUSERMOUNT);
1559 ret = waitpid(pid, &status, 0);
1561 ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
1563 return WIMLIB_ERR_FUSERMOUNT;
1566 if (!WIFEXITED(status)) {
1567 ERROR("'fusermount' did not terminate normally!");
1568 return WIMLIB_ERR_FUSERMOUNT;
1571 status = WEXITSTATUS(status);
1576 if (status != WIMLIB_ERR_FUSERMOUNT)
1577 return WIMLIB_ERR_FUSERMOUNT;
1579 /* Try again, but with the `umount' program. This is required on other
1580 * FUSE implementations such as FreeBSD's that do not have a
1581 * `fusermount' program. */
1582 ERROR("Falling back to 'umount'. Note: you may need to be "
1583 "root for this to work");
1586 ERROR_WITH_ERRNO("Failed to fork()");
1587 return WIMLIB_ERR_FORK;
1596 *argp++ = (char*)dir;
1598 execvp("umount", argv);
1599 ERROR_WITH_ERRNO("Failed to execute `umount'");
1600 exit(WIMLIB_ERR_FUSERMOUNT);
1604 ret = waitpid(pid, &status, 0);
1606 ERROR_WITH_ERRNO("Failed to wait for `umount' process to "
1608 return WIMLIB_ERR_FUSERMOUNT;
1611 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
1612 ERROR("`umount' did not successfully complete");
1613 return WIMLIB_ERR_FUSERMOUNT;
1620 wimfs_chmod(const char *path, mode_t mask)
1622 struct wim_dentry *dentry;
1623 struct wimfs_context *ctx = wimfs_get_context();
1626 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA))
1629 ret = wim_pathname_to_stream(ctx->wim, path, LOOKUP_FLAG_DIRECTORY_OK,
1630 &dentry, NULL, NULL);
1634 ret = inode_set_unix_data(dentry->d_inode, ctx->default_uid,
1635 ctx->default_gid, mask,
1636 ctx->wim->lookup_table, UNIX_DATA_MODE);
1637 return ret ? -ENOMEM : 0;
1641 wimfs_chown(const char *path, uid_t uid, gid_t gid)
1643 struct wim_dentry *dentry;
1644 struct wimfs_context *ctx = wimfs_get_context();
1647 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA))
1650 ret = wim_pathname_to_stream(ctx->wim, path, LOOKUP_FLAG_DIRECTORY_OK,
1651 &dentry, NULL, NULL);
1655 ret = inode_set_unix_data(dentry->d_inode, uid, gid,
1656 inode_default_unix_mode(dentry->d_inode),
1657 ctx->wim->lookup_table,
1658 UNIX_DATA_UID | UNIX_DATA_GID);
1659 return ret ? -ENOMEM : 0;
1662 /* Called when the filesystem is unmounted. */
1664 wimfs_destroy(void *p)
1666 struct wimfs_context *wimfs_ctx = wimfs_get_context();
1667 if (open_message_queues(wimfs_ctx, true) == 0) {
1668 struct daemon_msg_handler_context handler_ctx = {
1670 .timeout_seconds = 5,
1672 .wimfs_ctx = wimfs_ctx,
1674 message_loop(wimfs_ctx->unmount_to_daemon_mq,
1675 &daemon_msg_handler_callbacks,
1681 wimfs_fgetattr(const char *path, struct stat *stbuf,
1682 struct fuse_file_info *fi)
1684 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1685 return inode_to_stbuf(fd->f_inode, fd->f_lte, stbuf);
1689 wimfs_ftruncate(const char *path, off_t size, struct fuse_file_info *fi)
1691 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1692 int ret = ftruncate(fd->staging_fd.fd, size);
1695 touch_inode(fd->f_inode);
1696 fd->f_lte->size = size;
1701 * Fills in a `struct stat' that corresponds to a file or directory in the WIM.
1704 wimfs_getattr(const char *path, struct stat *stbuf)
1706 struct wim_dentry *dentry;
1707 struct wim_lookup_table_entry *lte;
1709 struct wimfs_context *ctx = wimfs_get_context();
1711 ret = wim_pathname_to_stream(ctx->wim, path,
1712 get_lookup_flags(ctx) |
1713 LOOKUP_FLAG_DIRECTORY_OK,
1714 &dentry, <e, NULL);
1717 return inode_to_stbuf(dentry->d_inode, lte, stbuf);
1721 /* Read an alternate data stream through the XATTR interface, or get its size */
1723 wimfs_getxattr(const char *path, const char *name, char *value,
1727 struct wim_inode *inode;
1728 struct wim_ads_entry *ads_entry;
1730 struct wim_lookup_table_entry *lte;
1731 struct wimfs_context *ctx = wimfs_get_context();
1733 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1736 if (strlen(name) <= 5 || memcmp(name, "user.", 5) != 0)
1740 inode = wim_pathname_to_inode(ctx->wim, path);
1744 ads_entry = inode_get_ads_entry(inode, name, NULL);
1748 lte = ads_entry->lte;
1749 stream_size = lte->size;
1754 if (stream_size > size)
1757 ret = read_full_stream_into_buf(lte, value);
1768 /* Create a hard link */
1770 wimfs_link(const char *to, const char *from)
1772 struct wim_dentry *from_dentry, *from_dentry_parent;
1773 const char *link_name;
1774 struct wim_inode *inode;
1775 WIMStruct *wim = wimfs_get_WIMStruct();
1778 inode = wim_pathname_to_inode(wim, to);
1782 if (inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
1783 FILE_ATTRIBUTE_REPARSE_POINT))
1786 from_dentry_parent = get_parent_dentry(wim, from, WIMLIB_CASE_SENSITIVE);
1787 if (!from_dentry_parent)
1789 if (!dentry_is_directory(from_dentry_parent))
1792 link_name = path_basename(from);
1793 if (get_dentry_child_with_name(from_dentry_parent, link_name,
1794 WIMLIB_CASE_SENSITIVE))
1797 ret = new_dentry(link_name, &from_dentry);
1802 inode_ref_streams(inode);
1803 from_dentry->d_inode = inode;
1804 inode_add_dentry(from_dentry, inode);
1805 dentry_add_child(from_dentry_parent, from_dentry);
1811 wimfs_listxattr(const char *path, char *list, size_t size)
1814 struct wim_inode *inode;
1815 struct wimfs_context *ctx = wimfs_get_context();
1818 bool size_only = (size == 0);
1820 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1823 /* List alternate data streams, or get the list size */
1825 inode = wim_pathname_to_inode(ctx->wim, path);
1830 for (i = 0; i < inode->i_num_ads; i++) {
1832 if (!ads_entry_is_named_stream(&inode->i_ads_entries[i]))
1835 char *stream_name_mbs;
1836 size_t stream_name_mbs_nbytes;
1839 ret = utf16le_to_tstr(inode->i_ads_entries[i].stream_name,
1840 inode->i_ads_entries[i].stream_name_nbytes,
1842 &stream_name_mbs_nbytes);
1846 needed_size = stream_name_mbs_nbytes + 6;
1848 if (needed_size > size) {
1849 FREE(stream_name_mbs);
1852 sprintf(p, "user.%s", stream_name_mbs);
1853 size -= needed_size;
1856 FREE(stream_name_mbs);
1863 /* Create a directory in the WIM image. */
1865 wimfs_mkdir(const char *path, mode_t mode)
1867 return create_dentry(fuse_get_context(), path, mode | S_IFDIR,
1868 FILE_ATTRIBUTE_DIRECTORY, NULL);
1871 /* Create a regular file or alternate data stream in the WIM image. */
1873 wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
1875 const char *stream_name;
1876 struct fuse_context *fuse_ctx = fuse_get_context();
1877 struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
1882 if ((wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
1883 && (stream_name = path_stream_name(path))) {
1884 /* Make an alternate data stream */
1885 struct wim_ads_entry *new_entry;
1886 struct wim_inode *inode;
1888 char *p = (char*)stream_name - 1;
1889 wimlib_assert(*p == ':');
1892 inode = wim_pathname_to_inode(wimfs_ctx->wim, path);
1895 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT)
1897 if (inode_get_ads_entry(inode, stream_name, NULL))
1899 new_entry = inode_add_ads(inode, stream_name);
1904 /* Make a normal file (not an alternate data stream) */
1905 return create_dentry(fuse_ctx, path, mode | S_IFREG,
1906 FILE_ATTRIBUTE_NORMAL, NULL);
1912 wimfs_open(const char *path, struct fuse_file_info *fi)
1914 struct wim_dentry *dentry;
1915 struct wim_lookup_table_entry *lte;
1917 struct wimfs_fd *fd;
1918 struct wim_inode *inode;
1921 struct wimfs_context *ctx = wimfs_get_context();
1922 struct wim_lookup_table_entry **back_ptr;
1924 ret = wim_pathname_to_stream(ctx->wim, path, get_lookup_flags(ctx),
1925 &dentry, <e, &stream_idx);
1929 inode = dentry->d_inode;
1931 if (stream_idx == 0) {
1933 back_ptr = &inode->i_lte;
1935 stream_id = inode->i_ads_entries[stream_idx - 1].stream_id;
1936 back_ptr = &inode->i_ads_entries[stream_idx - 1].lte;
1939 /* The file resource may be in the staging directory (read-write mounts
1940 * only) or in the WIM. If it's in the staging directory, we need to
1941 * open a native file descriptor for the corresponding file. Otherwise,
1942 * we can read the file resource directly from the WIM file if we are
1943 * opening it read-only, but we need to extract the resource to the
1944 * staging directory if we are opening it writable. */
1946 if (flags_writable(fi->flags) &&
1947 (!lte || lte->resource_location != RESOURCE_IN_STAGING_FILE)) {
1948 u64 size = (lte) ? lte->size : 0;
1949 ret = extract_resource_to_staging_dir(inode, stream_id,
1956 ret = alloc_wimfs_fd(inode, stream_id, lte, &fd);
1960 if (lte && lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1963 raw_fd = open(lte->staging_file_name, fi->flags);
1965 int errno_save = errno;
1969 filedes_init(&fd->staging_fd, raw_fd);
1971 fi->fh = (uintptr_t)fd;
1975 /* Opens a directory. */
1977 wimfs_opendir(const char *path, struct fuse_file_info *fi)
1979 struct wim_inode *inode;
1981 struct wimfs_fd *fd = NULL;
1982 struct wimfs_context *ctx = wimfs_get_context();
1983 WIMStruct *wim = ctx->wim;
1985 inode = wim_pathname_to_inode(wim, path);
1988 if (!inode_is_directory(inode))
1990 ret = alloc_wimfs_fd(inode, 0, NULL, &fd);
1991 fi->fh = (uintptr_t)fd;
1997 * Read data from a file in the WIM or in the staging directory.
2000 wimfs_read(const char *path, char *buf, size_t size,
2001 off_t offset, struct fuse_file_info *fi)
2003 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
2014 stream_size = fd->f_lte->size;
2018 if (offset > stream_size)
2021 size = min(size, stream_size - offset);
2025 switch (fd->f_lte->resource_location) {
2026 case RESOURCE_IN_STAGING_FILE:
2027 ret = raw_pread(&fd->staging_fd, buf, size, offset);
2031 case RESOURCE_IN_WIM:
2032 if (read_partial_wim_stream_into_buf(fd->f_lte, size,
2034 ret = errno ? -errno : -EIO;
2038 case RESOURCE_IN_ATTACHED_BUFFER:
2039 memcpy(buf, fd->f_lte->attached_buffer + offset, size);
2043 ERROR("Invalid resource location");
2050 /* Fills in the entries of the directory specified by @path using the
2051 * FUSE-provided function @filler. */
2053 wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
2054 off_t offset, struct fuse_file_info *fi)
2056 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
2057 struct wim_inode *inode;
2058 struct wim_dentry *child;
2064 inode = fd->f_inode;
2066 ret = filler(buf, ".", NULL, 0);
2069 ret = filler(buf, "..", NULL, 0);
2073 for_inode_child(child, inode) {
2074 char *file_name_mbs;
2075 size_t file_name_mbs_nbytes;
2077 ret = utf16le_to_tstr(child->file_name,
2078 child->file_name_nbytes,
2080 &file_name_mbs_nbytes);
2084 ret = filler(buf, file_name_mbs, NULL, 0);
2085 FREE(file_name_mbs);
2094 wimfs_readlink(const char *path, char *buf, size_t buf_len)
2096 struct wimfs_context *ctx = wimfs_get_context();
2097 struct wim_inode *inode = wim_pathname_to_inode(ctx->wim, path);
2101 if (!inode_is_symlink(inode))
2105 ret = wim_inode_readlink(inode, buf, buf_len - 1, NULL);
2107 wimlib_assert(ret <= buf_len - 1);
2110 } else if (ret == -ENAMETOOLONG) {
2111 buf[buf_len - 1] = '\0';
2118 wimfs_release(const char *path, struct fuse_file_info *fi)
2120 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
2121 return close_wimfs_fd(fd);
2124 /* Close a directory */
2126 wimfs_releasedir(const char *path, struct fuse_file_info *fi)
2128 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
2129 return close_wimfs_fd(fd);
2133 /* Remove an alternate data stream through the XATTR interface */
2135 wimfs_removexattr(const char *path, const char *name)
2137 struct wim_inode *inode;
2138 struct wim_ads_entry *ads_entry;
2140 struct wimfs_context *ctx = wimfs_get_context();
2142 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
2145 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
2149 inode = wim_pathname_to_inode(ctx->wim, path);
2153 ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
2156 inode_remove_ads(inode, ads_idx, ctx->wim->lookup_table);
2161 /* Renames a file or directory. See rename (3) */
2163 wimfs_rename(const char *from, const char *to)
2165 return rename_wim_path(wimfs_get_WIMStruct(), from, to,
2166 WIMLIB_CASE_SENSITIVE);
2169 /* Remove a directory */
2171 wimfs_rmdir(const char *path)
2173 struct wim_dentry *dentry;
2174 WIMStruct *wim = wimfs_get_WIMStruct();
2176 dentry = get_dentry(wim, path, WIMLIB_CASE_SENSITIVE);
2180 if (!dentry_is_directory(dentry))
2183 if (dentry_has_children(dentry))
2186 remove_dentry(dentry, wim->lookup_table);
2191 /* Write an alternate data stream through the XATTR interface */
2193 wimfs_setxattr(const char *path, const char *name,
2194 const char *value, size_t size, int flags)
2196 struct wim_ads_entry *existing_ads_entry;
2197 struct wim_inode *inode;
2199 struct wimfs_context *ctx = wimfs_get_context();
2202 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
2205 if (strlen(name) <= 5 || memcmp(name, "user.", 5) != 0)
2209 inode = wim_pathname_to_inode(ctx->wim, path);
2213 existing_ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
2214 if (existing_ads_entry) {
2215 if (flags & XATTR_CREATE)
2218 if (flags & XATTR_REPLACE)
2222 ret = inode_add_ads_with_data(inode, name, value,
2223 size, ctx->wim->lookup_table);
2225 if (existing_ads_entry)
2226 inode_remove_ads(inode, ads_idx, ctx->wim->lookup_table);
2235 wimfs_symlink(const char *to, const char *from)
2237 struct fuse_context *fuse_ctx = fuse_get_context();
2238 struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
2239 struct wim_dentry *dentry;
2242 ret = create_dentry(fuse_ctx, from, S_IFLNK | 0777,
2243 FILE_ATTRIBUTE_REPARSE_POINT, &dentry);
2245 dentry->d_inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
2246 ret = wim_inode_set_symlink(dentry->d_inode, to,
2247 wimfs_ctx->wim->lookup_table);
2249 remove_dentry(dentry, wimfs_ctx->wim->lookup_table);
2250 if (ret == WIMLIB_ERR_NOMEM)
2260 /* Reduce the size of a file */
2262 wimfs_truncate(const char *path, off_t size)
2264 struct wim_dentry *dentry;
2265 struct wim_lookup_table_entry *lte;
2269 struct wim_inode *inode;
2270 struct wimfs_context *ctx = wimfs_get_context();
2272 ret = wim_pathname_to_stream(ctx->wim, path, get_lookup_flags(ctx),
2273 &dentry, <e, &stream_idx);
2278 if (lte == NULL && size == 0)
2281 if (lte != NULL && lte->resource_location == RESOURCE_IN_STAGING_FILE) {
2282 ret = truncate(lte->staging_file_name, size);
2288 /* File in WIM. Extract it to the staging directory, but only
2289 * the first @size bytes of it. */
2290 struct wim_lookup_table_entry **back_ptr;
2292 inode = dentry->d_inode;
2293 if (stream_idx == 0) {
2295 back_ptr = &inode->i_lte;
2297 stream_id = inode->i_ads_entries[stream_idx - 1].stream_id;
2298 back_ptr = &inode->i_ads_entries[stream_idx - 1].lte;
2300 ret = extract_resource_to_staging_dir(inode, stream_id,
2307 /* Unlink a non-directory or alternate data stream */
2309 wimfs_unlink(const char *path)
2311 struct wim_dentry *dentry;
2312 struct wim_lookup_table_entry *lte;
2315 struct wimfs_context *ctx = wimfs_get_context();
2317 ret = wim_pathname_to_stream(ctx->wim, path, get_lookup_flags(ctx),
2318 &dentry, <e, &stream_idx);
2323 if (inode_stream_name_nbytes(dentry->d_inode, stream_idx) == 0)
2324 remove_dentry(dentry, ctx->wim->lookup_table);
2326 inode_remove_ads(dentry->d_inode, stream_idx - 1,
2327 ctx->wim->lookup_table);
2331 #ifdef HAVE_UTIMENSAT
2333 * Change the timestamp on a file dentry.
2335 * Note that alternate data streams do not have their own timestamps.
2338 wimfs_utimens(const char *path, const struct timespec tv[2])
2340 struct wim_inode *inode;
2341 WIMStruct *wim = wimfs_get_WIMStruct();
2343 inode = wim_pathname_to_inode(wim, path);
2347 if (tv[0].tv_nsec != UTIME_OMIT) {
2348 if (tv[0].tv_nsec == UTIME_NOW)
2349 inode->i_last_access_time = get_wim_timestamp();
2351 inode->i_last_access_time = timespec_to_wim_timestamp(tv[0]);
2353 if (tv[1].tv_nsec != UTIME_OMIT) {
2354 if (tv[1].tv_nsec == UTIME_NOW)
2355 inode->i_last_write_time = get_wim_timestamp();
2357 inode->i_last_write_time = timespec_to_wim_timestamp(tv[1]);
2361 #else /* HAVE_UTIMENSAT */
2363 wimfs_utime(const char *path, struct utimbuf *times)
2365 struct wim_inode *inode;
2366 WIMStruct *wim = wimfs_get_WIMStruct();
2368 inode = wim_pathname_to_inode(wim, path);
2372 inode->i_last_write_time = unix_timestamp_to_wim(times->modtime);
2373 inode->i_last_access_time = unix_timestamp_to_wim(times->actime);
2376 #endif /* !HAVE_UTIMENSAT */
2378 /* Writes to a file in the WIM filesystem.
2379 * It may be an alternate data stream, but here we don't even notice because we
2380 * just get a lookup table entry. */
2382 wimfs_write(const char *path, const char *buf, size_t size,
2383 off_t offset, struct fuse_file_info *fi)
2385 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
2391 wimlib_assert(fd->f_lte != NULL);
2392 wimlib_assert(fd->f_lte->staging_file_name != NULL);
2393 wimlib_assert(filedes_valid(&fd->staging_fd));
2394 wimlib_assert(fd->f_inode != NULL);
2396 /* Write the data. */
2397 ret = raw_pwrite(&fd->staging_fd, buf, size, offset);
2401 /* Update file size */
2402 if (offset + size > fd->f_lte->size) {
2403 DEBUG("Update file size %"PRIu64 " => %"PRIu64"",
2404 fd->f_lte->size, offset + size);
2405 fd->f_lte->size = offset + size;
2408 /* Update timestamps */
2409 touch_inode(fd->f_inode);
2413 static struct fuse_operations wimfs_operations = {
2414 .chmod = wimfs_chmod,
2415 .chown = wimfs_chown,
2416 .destroy = wimfs_destroy,
2417 .fgetattr = wimfs_fgetattr,
2418 .ftruncate = wimfs_ftruncate,
2419 .getattr = wimfs_getattr,
2421 .getxattr = wimfs_getxattr,
2425 .listxattr = wimfs_listxattr,
2427 .mkdir = wimfs_mkdir,
2428 .mknod = wimfs_mknod,
2430 .opendir = wimfs_opendir,
2432 .readdir = wimfs_readdir,
2433 .readlink = wimfs_readlink,
2434 .release = wimfs_release,
2435 .releasedir = wimfs_releasedir,
2437 .removexattr = wimfs_removexattr,
2439 .rename = wimfs_rename,
2440 .rmdir = wimfs_rmdir,
2442 .setxattr = wimfs_setxattr,
2444 .symlink = wimfs_symlink,
2445 .truncate = wimfs_truncate,
2446 .unlink = wimfs_unlink,
2447 #ifdef HAVE_UTIMENSAT
2448 .utimens = wimfs_utimens,
2450 .utime = wimfs_utime,
2452 .write = wimfs_write,
2454 /* wimfs keeps file descriptor structures (struct wimfs_fd), so there is
2455 * no need to have the file path provided on operations such as read()
2456 * where only the file descriptor is needed. */
2457 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 8)
2458 .flag_nullpath_ok = 1,
2460 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 9)
2462 .flag_utime_omit_ok = 1,
2467 /* API function documented in wimlib.h */
2469 wimlib_mount_image(WIMStruct *wim, int image, const char *dir,
2470 int mount_flags, const char *staging_dir)
2476 struct wim_image_metadata *imd;
2477 struct wimfs_context ctx;
2478 struct wim_inode *inode;
2480 DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
2481 wim, image, dir, mount_flags);
2484 return WIMLIB_ERR_INVALID_PARAM;
2486 if (mount_flags & ~(WIMLIB_MOUNT_FLAG_READWRITE |
2487 WIMLIB_MOUNT_FLAG_DEBUG |
2488 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
2489 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
2490 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS |
2491 WIMLIB_MOUNT_FLAG_UNIX_DATA |
2492 WIMLIB_MOUNT_FLAG_ALLOW_OTHER))
2493 return WIMLIB_ERR_INVALID_PARAM;
2495 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2496 ret = can_delete_from_wim(wim);
2501 ret = select_wim_image(wim, image);
2505 DEBUG("Selected image %d", image);
2507 imd = wim_get_current_image_metadata(wim);
2509 if (imd->modified) {
2510 /* wimfs_read() only supports a limited number of stream
2511 * locations, not including RESOURCE_IN_FILE_ON_DISK,
2512 * RESOURCE_IN_NTFS_VOLUME, etc. that might appear if files were
2513 * added to the WIM image. */
2514 ERROR("Cannot mount an image with newly added files!");
2515 return WIMLIB_ERR_INVALID_PARAM;
2518 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2519 ret = lock_wim(wim, wim->in_fd.fd);
2524 /* Use default stream interface if one was not specified */
2525 if (!(mount_flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
2526 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
2527 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
2528 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
2530 DEBUG("Initializing struct wimfs_context");
2531 init_wimfs_context(&ctx);
2533 ctx.mount_flags = mount_flags;
2534 ctx.image_inode_list = &imd->inode_list;
2535 ctx.default_uid = getuid();
2536 ctx.default_gid = getgid();
2537 wimlib_assert(list_empty(&imd->unhashed_streams));
2538 if (mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
2539 ctx.default_lookup_flags = LOOKUP_FLAG_ADS_OK;
2541 DEBUG("Unlinking message queues in case they already exist");
2542 ret = set_message_queue_names(&ctx, dir);
2545 unlink_message_queues(&ctx);
2547 DEBUG("Preparing arguments to fuse_main()");
2549 dir_copy = STRDUP(dir);
2551 ret = WIMLIB_ERR_NOMEM;
2552 goto out_free_message_queue_names;
2556 argv[argc++] = "wimlib";
2557 argv[argc++] = dir_copy;
2559 /* disable multi-threaded operation */
2560 argv[argc++] = "-s";
2562 if (mount_flags & WIMLIB_MOUNT_FLAG_DEBUG)
2563 argv[argc++] = "-d";
2566 * We provide the use_ino option to the FUSE mount because we are going
2567 * to assign inode numbers ourselves. */
2568 char optstring[256] =
2572 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 8)
2575 ",default_permissions"
2577 argv[argc++] = "-o";
2578 argv[argc++] = optstring;
2579 if ((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
2580 /* Read-write mount. Make the staging directory */
2581 ret = make_staging_dir(&ctx, staging_dir);
2583 goto out_free_dir_copy;
2585 /* Read-only mount */
2586 strcat(optstring, ",ro");
2588 if (mount_flags & WIMLIB_MOUNT_FLAG_ALLOW_OTHER)
2589 strcat(optstring, ",allow_other");
2595 DEBUG("FUSE command line (argc = %d): ", argc);
2596 for (i = 0; i < argc; i++) {
2597 fputs(argv[i], stdout);
2605 /* Assign inode numbers. Also, if a read-write mount was requested,
2606 * mark the dentry tree as modified, and add each stream referenced by
2607 * files in the image to a list and preemptively double the number of
2608 * references to each. The latter is done to allow implementing the
2609 * WIMLIB_UNMOUNT_FLAG_NEW_IMAGE semantics. */
2611 INIT_LIST_HEAD(&ctx.orig_stream_list);
2612 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2614 image_for_each_inode(inode, imd) {
2615 inode->i_ino = ctx.next_ino++;
2616 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
2617 struct wim_lookup_table_entry *lte;
2619 lte = inode_stream_lte(inode, i, wim->lookup_table);
2621 lte->out_refcnt = 0;
2624 image_for_each_inode(inode, imd) {
2625 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
2626 struct wim_lookup_table_entry *lte;
2628 lte = inode_stream_lte(inode, i,
2631 if (lte->out_refcnt == 0)
2632 list_add(<e->orig_stream_list,
2633 &ctx.orig_stream_list);
2634 lte->out_refcnt += inode->i_nlink;
2635 lte->refcnt += inode->i_nlink;
2640 image_for_each_inode(inode, imd)
2641 inode->i_ino = ctx.next_ino++;
2644 DEBUG("(next_ino = %"PRIu64")", ctx.next_ino);
2646 DEBUG("Calling fuse_main()");
2648 ret = fuse_main(argc, argv, &wimfs_operations, &ctx);
2650 DEBUG("Returned from fuse_main() (ret = %d)", ret);
2653 ret = WIMLIB_ERR_FUSE;
2655 if (ctx.have_status)
2658 ret = WIMLIB_ERR_TIMEOUT;
2660 if (ctx.daemon_to_unmount_mq != (mqd_t)(-1)) {
2661 send_unmount_finished_msg(ctx.daemon_to_unmount_mq, ret);
2662 close_message_queues(&ctx);
2665 release_extra_refcnts(&ctx);
2667 /* Try to delete the staging directory if a deletion wasn't yet
2668 * attempted due to an earlier error */
2669 if (ctx.staging_dir_name)
2670 delete_staging_dir(&ctx);
2674 wim->wim_locked = 0;
2675 out_free_message_queue_names:
2676 free_message_queue_names(&ctx);
2680 /* API function documented in wimlib.h */
2682 wimlib_unmount_image(const char *dir, int unmount_flags,
2683 wimlib_progress_func_t progress_func)
2686 struct wimfs_context wimfs_ctx;
2688 if (unmount_flags & ~(WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY |
2689 WIMLIB_UNMOUNT_FLAG_COMMIT |
2690 WIMLIB_UNMOUNT_FLAG_REBUILD |
2691 WIMLIB_UNMOUNT_FLAG_RECOMPRESS |
2692 WIMLIB_UNMOUNT_FLAG_LAZY |
2693 WIMLIB_UNMOUNT_FLAG_NEW_IMAGE))
2694 return WIMLIB_ERR_INVALID_PARAM;
2696 init_wimfs_context(&wimfs_ctx);
2698 ret = set_message_queue_names(&wimfs_ctx, dir);
2702 ret = open_message_queues(&wimfs_ctx, false);
2704 goto out_free_message_queue_names;
2706 ret = send_unmount_request_msg(wimfs_ctx.unmount_to_daemon_mq,
2708 progress_func != NULL);
2710 goto out_close_message_queues;
2712 ret = execute_fusermount(dir, (unmount_flags & WIMLIB_UNMOUNT_FLAG_LAZY) != 0);
2714 goto out_close_message_queues;
2716 struct unmount_msg_handler_context handler_ctx = {
2718 .timeout_seconds = 5,
2721 .progress_func = progress_func,
2724 ret = message_loop(wimfs_ctx.daemon_to_unmount_mq,
2725 &unmount_msg_handler_callbacks,
2728 ret = handler_ctx.status;
2729 out_close_message_queues:
2730 close_message_queues(&wimfs_ctx);
2731 out_free_message_queue_names:
2732 free_message_queue_names(&wimfs_ctx);
2737 #else /* WITH_FUSE */
2741 mount_unsupported_error(void)
2743 #if defined(__WIN32__)
2744 ERROR("Sorry-- Mounting WIM images is not supported on Windows!");
2746 ERROR("wimlib was compiled with --without-fuse, which disables support "
2747 "for mounting WIMs.");
2749 return WIMLIB_ERR_UNSUPPORTED;
2753 wimlib_unmount_image(const tchar *dir, int unmount_flags,
2754 wimlib_progress_func_t progress_func)
2756 return mount_unsupported_error();
2760 wimlib_mount_image(WIMStruct *wim, int image, const tchar *dir,
2761 int mount_flags, const tchar *staging_dir)
2763 return mount_unsupported_error();
2766 #endif /* !WITH_FUSE */