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, dev_t rdev, int attributes,
319 struct wim_dentry **dentry_ret)
321 struct wim_dentry *parent;
322 struct wim_dentry *new;
323 const char *basename;
324 struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
327 parent = get_parent_dentry(wimfs_ctx->wim, path, WIMLIB_CASE_SENSITIVE);
331 if (!dentry_is_directory(parent))
334 basename = path_basename(path);
335 if (get_dentry_child_with_name(parent, basename, WIMLIB_CASE_SENSITIVE))
338 ret = new_dentry_with_inode(basename, &new);
342 new->d_inode->i_resolved = 1;
343 new->d_inode->i_ino = wimfs_ctx->next_ino++;
344 new->d_inode->i_attributes = attributes;
346 if (wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA) {
347 struct wimlib_unix_data unix_data;
349 unix_data.uid = fuse_ctx->uid;
350 unix_data.gid = fuse_ctx->gid;
351 unix_data.mode = fuse_mask_mode(mode, fuse_ctx);
352 unix_data.rdev = rdev;
354 if (!inode_set_unix_data(new->d_inode, &unix_data,
361 dentry_add_child(parent, new);
362 list_add_tail(&new->d_inode->i_list, wimfs_ctx->image_inode_list);
368 static struct wim_inode *
369 wim_pathname_to_inode(WIMStruct *wim, const tchar *path)
371 struct wim_dentry *dentry;
372 dentry = get_dentry(wim, path, WIMLIB_CASE_SENSITIVE);
374 return dentry->d_inode;
380 * Remove a dentry from a mounted WIM image; i.e. remove an alias for an inode.
383 remove_dentry(struct wim_dentry *dentry,
384 struct wim_lookup_table *lookup_table)
386 /* Put a reference to each stream the inode contains. */
387 inode_unref_streams(dentry->d_inode, lookup_table);
389 /* Unlink the dentry from the image's dentry tree. */
390 unlink_dentry(dentry);
392 /* Delete the dentry. This will also decrement the link count of the
393 * corresponding inode. */
398 inode_unix_file_type(const struct wim_inode *inode)
400 if (inode_is_symlink(inode))
402 else if (inode_is_directory(inode))
409 inode_default_unix_mode(const struct wim_inode *inode)
411 return inode_unix_file_type(inode) | 0777;
414 /* Transfers file attributes from a struct wim_inode to a `stat' buffer.
416 * The lookup table entry tells us which stream in the inode we are statting.
417 * For a named data stream, everything returned is the same as the unnamed data
418 * stream except possibly the size and block count. */
420 inode_to_stbuf(const struct wim_inode *inode,
421 const struct wim_lookup_table_entry *lte,
424 const struct wimfs_context *ctx = wimfs_get_context();
425 struct wimlib_unix_data unix_data;
427 memset(stbuf, 0, sizeof(struct stat));
428 if ((ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA) &&
429 inode_get_unix_data(inode, &unix_data))
431 stbuf->st_uid = unix_data.uid;
432 stbuf->st_gid = unix_data.gid;
433 stbuf->st_mode = unix_data.mode;
434 stbuf->st_rdev = unix_data.rdev;
436 stbuf->st_uid = ctx->default_uid;
437 stbuf->st_gid = ctx->default_gid;
438 stbuf->st_mode = inode_default_unix_mode(inode);
441 stbuf->st_ino = (ino_t)inode->i_ino;
442 stbuf->st_nlink = inode->i_nlink;
444 stbuf->st_size = lte->size;
447 #ifdef HAVE_STAT_NANOSECOND_PRECISION
448 stbuf->st_atim = wim_timestamp_to_timespec(inode->i_last_access_time);
449 stbuf->st_mtim = wim_timestamp_to_timespec(inode->i_last_write_time);
450 stbuf->st_ctim = stbuf->st_mtim;
452 stbuf->st_atime = wim_timestamp_to_unix(inode->i_last_access_time);
453 stbuf->st_mtime = wim_timestamp_to_unix(inode->i_last_write_time);
454 stbuf->st_ctime = stbuf->st_mtime;
456 stbuf->st_blocks = (stbuf->st_size + 511) / 512;
461 touch_inode(struct wim_inode *inode)
463 u64 now = get_wim_timestamp();
464 inode->i_last_access_time = now;
465 inode->i_last_write_time = now;
468 /* Creates a new staging file and returns its file descriptor opened for
471 * @name_ret: A location into which the a pointer to the newly allocated name of
472 * the staging file is stored.
474 * @ctx: Context for the WIM filesystem; this provides the name of the
477 * On success, returns the file descriptor for the staging file, opened for
478 * writing. On failure, returns -1 and sets errno.
481 create_staging_file(char **name_ret, struct wimfs_context *ctx)
489 static const size_t STAGING_FILE_NAME_LEN = 20;
491 name_len = ctx->staging_dir_name_len + 1 + STAGING_FILE_NAME_LEN;
492 name = MALLOC(name_len + 1);
500 memcpy(name, ctx->staging_dir_name, ctx->staging_dir_name_len);
501 name[ctx->staging_dir_name_len] = '/';
502 randomize_char_array_with_alnum(name + ctx->staging_dir_name_len + 1,
503 STAGING_FILE_NAME_LEN);
504 name[name_len] = '\0';
507 /* Just in case, verify that the randomly generated name doesn't name an
508 * existing file, and try again if so */
509 } while (stat(name, &stbuf) == 0);
511 if (errno != ENOENT) /* other error?! */
514 /* doesn't exist--- ok */
516 DEBUG("Creating staging file `%s'", name);
518 fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0600);
530 * Extract a WIM resource to the staging directory.
532 * @inode: Inode that contains the stream we are extracting
534 * @stream_id: Identifier for the stream (it stays constant even if the indices
535 * of the stream entries are changed)
537 * @lte: Pointer to pointer to the lookup table entry for the stream we need to
538 * extract, or NULL if there was no lookup table entry present for the stream
540 * @size: Number of bytes of the stream we want to extract (this supports the
541 * wimfs_truncate() function). It may be more than the actual stream length, in
542 * which case the extra space is filled with zeroes.
544 * @ctx: Context for the WIM filesystem.
546 * Returns 0 on success or a negative error code on failure.
549 extract_resource_to_staging_dir(struct wim_inode *inode,
551 struct wim_lookup_table_entry **lte,
553 struct wimfs_context *ctx)
555 char *staging_file_name;
558 struct wim_lookup_table_entry *old_lte, *new_lte;
561 DEBUG("Extracting resource to staging dir: inode %"PRIu64", "
562 "stream id %"PRIu32, inode->i_ino, stream_id);
566 wimlib_assert(old_lte == NULL ||
567 old_lte->resource_location != RESOURCE_IN_STAGING_FILE);
569 /* Create the staging file */
570 fd = create_staging_file(&staging_file_name, ctx);
574 /* Extract the stream to the staging file (possibly truncated) */
576 struct filedes wimlib_fd;
577 filedes_init(&wimlib_fd, fd);
578 extract_size = min(old_lte->size, size);
579 ret = extract_stream_to_fd(old_lte, &wimlib_fd, extract_size);
585 /* In the case of truncate() to more than the file length, extend the
586 * file with zeroes by calling ftruncate() on the underlying staging
588 if (ret == 0 && size > extract_size)
589 ret = ftruncate(fd, size);
591 /* Close the staging file descriptor and check for errors. If there's
592 * an error, unlink the staging file. */
593 if (ret != 0 || close(fd) != 0) {
599 goto out_delete_staging_file;
602 /* Now deal with the lookup table entries. We may be able to re-use the
603 * existing entry, but we may have to create a new one instead. */
605 if (old_lte && inode->i_nlink == old_lte->refcnt) {
606 /* The reference count of the existing lookup table entry is the
607 * same as the link count of the inode that contains the stream
608 * we're opening. Therefore, ALL the references to the lookup
609 * table entry correspond to the stream we're trying to extract,
610 * so the lookup table entry can be re-used. */
611 DEBUG("Re-using lookup table entry");
612 lookup_table_unlink(ctx->wim->lookup_table, old_lte);
616 /* There's an existing lookup table entry, but its
617 * reference count is greater than the link count for
618 * the inode containing a stream we're opening.
619 * Therefore, we need to split the lookup table entry.
621 wimlib_assert(old_lte->refcnt > inode->i_nlink);
622 DEBUG("Splitting lookup table entry "
623 "(inode->i_nlink = %u, old_lte->refcnt = %u)",
624 inode->i_nlink, old_lte->refcnt);
627 new_lte = new_lookup_table_entry();
630 goto out_delete_staging_file;
633 /* There may already be open file descriptors to this stream if
634 * it's previously been opened read-only, but just now we're
635 * opening it read-write. Identify those file descriptors and
636 * change their lookup table entry pointers to point to the new
637 * lookup table entry, and open staging file descriptors for
640 * At the same time, we need to count the number of these opened
641 * file descriptors to the new lookup table entry. If there's
642 * an old lookup table entry, this number needs to be subtracted
643 * from the fd's opened to the old entry. */
644 for (u16 i = 0, j = 0; j < inode->i_num_opened_fds; i++) {
645 struct wimfs_fd *fd = inode->i_fds[i];
647 if (fd->stream_id == stream_id) {
650 wimlib_assert(fd->f_lte == old_lte);
651 wimlib_assert(!filedes_valid(&fd->staging_fd));
653 new_lte->num_opened_fds++;
654 raw_fd = open(staging_file_name, O_RDONLY);
657 goto out_revert_fd_changes;
659 filedes_init(&fd->staging_fd, raw_fd);
664 DEBUG("%hu fd's were already opened to the file we extracted",
665 new_lte->num_opened_fds);
667 old_lte->num_opened_fds -= new_lte->num_opened_fds;
668 old_lte->refcnt -= inode->i_nlink;
672 lte_put_resource(new_lte);
673 new_lte->refcnt = inode->i_nlink;
674 new_lte->resource_location = RESOURCE_IN_STAGING_FILE;
675 new_lte->staging_file_name = staging_file_name;
676 new_lte->size = size;
678 add_unhashed_stream(new_lte, inode, stream_id,
679 &wim_get_current_image_metadata(ctx->wim)->unhashed_streams);
680 *retrieve_lte_pointer(new_lte) = new_lte;
683 out_revert_fd_changes:
684 for (u16 i = 0, j = 0; j < new_lte->num_opened_fds; i++) {
685 struct wimfs_fd *fd = inode->i_fds[i];
686 if (fd && fd->stream_id == stream_id && fd->f_lte == new_lte) {
688 if (filedes_valid(&fd->staging_fd)) {
689 filedes_close(&fd->staging_fd);
690 filedes_invalidate(&fd->staging_fd);
695 free_lookup_table_entry(new_lte);
696 out_delete_staging_file:
697 unlink(staging_file_name);
698 FREE(staging_file_name);
703 * Creates a randomly named staging directory and saves its name in the
704 * filesystem context structure.
707 make_staging_dir(struct wimfs_context *ctx, const char *user_prefix)
709 static const size_t random_suffix_len = 10;
710 static const char *common_suffix = ".staging";
711 static const size_t common_suffix_len = 8;
713 char *staging_dir_name = NULL;
714 size_t staging_dir_name_len;
716 const char *wim_basename;
717 char *real_user_prefix = NULL;
721 real_user_prefix = realpath(user_prefix, NULL);
722 if (!real_user_prefix) {
723 ERROR_WITH_ERRNO("Could not resolve `%s'",
725 ret = WIMLIB_ERR_NOTDIR;
728 wim_basename = path_basename(ctx->wim->filename);
729 prefix_len = strlen(real_user_prefix) + 1 + strlen(wim_basename);
731 prefix_len = strlen(ctx->wim->filename);
734 staging_dir_name_len = prefix_len + common_suffix_len + random_suffix_len;
736 staging_dir_name = MALLOC(staging_dir_name_len + 1);
737 if (!staging_dir_name) {
738 ret = WIMLIB_ERR_NOMEM;
742 if (real_user_prefix)
743 sprintf(staging_dir_name, "%s/%s", real_user_prefix, wim_basename);
745 strcpy(staging_dir_name, ctx->wim->filename);
747 strcat(staging_dir_name, common_suffix);
749 randomize_char_array_with_alnum(staging_dir_name + prefix_len + common_suffix_len,
752 staging_dir_name[staging_dir_name_len] = '\0';
754 if (mkdir(staging_dir_name, 0700) != 0) {
755 ERROR_WITH_ERRNO("Failed to create temporary directory `%s'",
757 ret = WIMLIB_ERR_MKDIR;
762 FREE(real_user_prefix);
764 ctx->staging_dir_name = staging_dir_name;
765 ctx->staging_dir_name_len = staging_dir_name_len;
767 FREE(staging_dir_name);
773 remove_file_or_directory(const char *fpath, const struct stat *sb,
774 int typeflag, struct FTW *ftwbuf)
776 if (remove(fpath) == 0)
779 ERROR_WITH_ERRNO("Cannot remove `%s'", fpath);
780 return WIMLIB_ERR_DELETE_STAGING_DIR;
785 * Deletes the staging directory and all the files contained in it.
788 delete_staging_dir(struct wimfs_context *ctx)
791 ret = nftw(ctx->staging_dir_name, remove_file_or_directory,
793 FREE(ctx->staging_dir_name);
794 ctx->staging_dir_name = NULL;
799 inode_close_fds(struct wim_inode *inode)
801 u16 num_opened_fds = inode->i_num_opened_fds;
802 for (u16 i = 0, j = 0; j < num_opened_fds; i++) {
803 struct wimfs_fd *fd = inode->i_fds[i];
805 wimlib_assert(fd->f_inode == inode);
806 int ret = close_wimfs_fd(fd);
815 /* Overwrites the WIM file, with changes saved. */
817 rebuild_wim(struct wimfs_context *ctx, int write_flags)
820 struct wim_lookup_table_entry *lte, *tmp;
821 WIMStruct *wim = ctx->wim;
822 struct wim_image_metadata *imd = wim_get_current_image_metadata(ctx->wim);
824 DEBUG("Closing all staging file descriptors.");
825 image_for_each_unhashed_stream_safe(lte, tmp, imd) {
826 ret = inode_close_fds(lte->back_inode);
831 DEBUG("Freeing entries for zero-length streams");
832 image_for_each_unhashed_stream_safe(lte, tmp, imd) {
833 wimlib_assert(lte->unhashed);
834 if (lte->size == 0) {
835 struct wim_lookup_table_entry **back_ptr;
836 back_ptr = retrieve_lte_pointer(lte);
838 list_del(<e->unhashed_list);
839 free_lookup_table_entry(lte);
843 xml_update_image_info(wim, wim->current_image);
844 ret = wimlib_overwrite(wim, write_flags, 0);
846 ERROR("Failed to commit changes to mounted WIM image");
850 /* Simple function that returns the concatenation of 2 strings. */
852 strcat_dup(const char *s1, const char *s2, size_t max_len)
854 size_t len = strlen(s1) + strlen(s2);
857 char *p = MALLOC(len + 1);
860 snprintf(p, len + 1, "%s%s", s1, s2);
865 set_message_queue_names(struct wimfs_context *ctx, const char *mount_dir)
867 static const char *u2d_prefix = "/wimlib-unmount-to-daemon-mq";
868 static const char *d2u_prefix = "/wimlib-daemon-to-unmount-mq";
873 dir_path = realpath(mount_dir, NULL);
875 ERROR_WITH_ERRNO("Failed to resolve path \"%s\"", mount_dir);
877 return WIMLIB_ERR_NOMEM;
879 return WIMLIB_ERR_NOTDIR;
882 for (p = dir_path; *p; p++)
886 ctx->unmount_to_daemon_mq_name = strcat_dup(u2d_prefix, dir_path,
888 if (!ctx->unmount_to_daemon_mq_name) {
889 ret = WIMLIB_ERR_NOMEM;
890 goto out_free_dir_path;
892 ctx->daemon_to_unmount_mq_name = strcat_dup(d2u_prefix, dir_path,
894 if (!ctx->daemon_to_unmount_mq_name) {
895 ret = WIMLIB_ERR_NOMEM;
896 goto out_free_unmount_to_daemon_mq_name;
900 goto out_free_dir_path;
901 out_free_unmount_to_daemon_mq_name:
902 FREE(ctx->unmount_to_daemon_mq_name);
903 ctx->unmount_to_daemon_mq_name = NULL;
910 free_message_queue_names(struct wimfs_context *ctx)
912 FREE(ctx->unmount_to_daemon_mq_name);
913 FREE(ctx->daemon_to_unmount_mq_name);
914 ctx->unmount_to_daemon_mq_name = NULL;
915 ctx->daemon_to_unmount_mq_name = NULL;
919 * Opens two POSIX message queue: one for sending messages from the unmount
920 * process to the daemon process, and one to go the other way. The names of the
921 * message queues, which must be system-wide unique, are be based on the mount
924 * @daemon specifies whether the calling process is the filesystem daemon or the
928 open_message_queues(struct wimfs_context *ctx, bool daemon)
930 int unmount_to_daemon_mq_flags = O_WRONLY | O_CREAT;
931 int daemon_to_unmount_mq_flags = O_RDONLY | O_CREAT;
937 swap(unmount_to_daemon_mq_flags, daemon_to_unmount_mq_flags);
943 orig_umask = umask(0000);
944 DEBUG("Opening message queue \"%s\"", ctx->unmount_to_daemon_mq_name);
945 ctx->unmount_to_daemon_mq = mq_open(ctx->unmount_to_daemon_mq_name,
946 unmount_to_daemon_mq_flags, mode, NULL);
948 if (ctx->unmount_to_daemon_mq == (mqd_t)-1) {
949 ERROR_WITH_ERRNO("mq_open()");
950 ret = WIMLIB_ERR_MQUEUE;
954 DEBUG("Opening message queue \"%s\"", ctx->daemon_to_unmount_mq_name);
955 ctx->daemon_to_unmount_mq = mq_open(ctx->daemon_to_unmount_mq_name,
956 daemon_to_unmount_mq_flags, mode, NULL);
958 if (ctx->daemon_to_unmount_mq == (mqd_t)-1) {
959 ERROR_WITH_ERRNO("mq_open()");
960 mq_close(ctx->unmount_to_daemon_mq);
961 mq_unlink(ctx->unmount_to_daemon_mq_name);
962 ctx->unmount_to_daemon_mq = (mqd_t)-1;
963 ret = WIMLIB_ERR_MQUEUE;
972 /* Try to determine the maximum message size of a message queue. The return
973 * value is the maximum message size, or a guess of 8192 bytes if it cannot be
976 mq_get_msgsize(mqd_t mq)
978 static const char *msgsize_max_file = "/proc/sys/fs/mqueue/msgsize_max";
983 if (mq_getattr(mq, &attr) == 0) {
984 msgsize = attr.mq_msgsize;
986 ERROR_WITH_ERRNO("mq_getattr()");
987 ERROR("Attempting to read %s", msgsize_max_file);
988 fp = fopen(msgsize_max_file, "rb");
990 if (fscanf(fp, "%ld", &msgsize) != 1) {
991 ERROR("Assuming message size of 8192");
996 ERROR_WITH_ERRNO("Failed to open the file `%s'",
998 ERROR("Assuming message size of 8192");
1006 get_mailbox(mqd_t mq, long needed_msgsize, long *msgsize_ret,
1012 msgsize = mq_get_msgsize(mq);
1014 if (msgsize < needed_msgsize) {
1015 ERROR("Message queue max size must be at least %ld!",
1017 return WIMLIB_ERR_MQUEUE;
1020 mailbox = MALLOC(msgsize);
1022 ERROR("Failed to allocate %ld bytes for mailbox", msgsize);
1023 return WIMLIB_ERR_NOMEM;
1025 *msgsize_ret = msgsize;
1026 *mailbox_ret = mailbox;
1031 unlink_message_queues(struct wimfs_context *ctx)
1033 mq_unlink(ctx->unmount_to_daemon_mq_name);
1034 mq_unlink(ctx->daemon_to_unmount_mq_name);
1037 /* Closes the message queues, which are allocated in static variables */
1039 close_message_queues(struct wimfs_context *ctx)
1041 DEBUG("Closing message queues");
1042 mq_close(ctx->unmount_to_daemon_mq);
1043 ctx->unmount_to_daemon_mq = (mqd_t)(-1);
1044 mq_close(ctx->daemon_to_unmount_mq);
1045 ctx->daemon_to_unmount_mq = (mqd_t)(-1);
1046 unlink_message_queues(ctx);
1050 struct unmount_msg_hdr {
1055 } _packed_attribute;
1057 struct msg_unmount_request {
1058 struct unmount_msg_hdr hdr;
1060 u8 want_progress_messages;
1061 } _packed_attribute;
1063 struct msg_daemon_info {
1064 struct unmount_msg_hdr hdr;
1067 } _packed_attribute;
1069 struct msg_unmount_finished {
1070 struct unmount_msg_hdr hdr;
1072 } _packed_attribute;
1074 struct msg_write_streams_progress {
1075 struct unmount_msg_hdr hdr;
1076 union wimlib_progress_info info;
1077 } _packed_attribute;
1080 MSG_TYPE_UNMOUNT_REQUEST,
1081 MSG_TYPE_DAEMON_INFO,
1082 MSG_TYPE_WRITE_STREAMS_PROGRESS,
1083 MSG_TYPE_UNMOUNT_FINISHED,
1087 struct msg_handler_context_hdr {
1088 int timeout_seconds;
1091 struct unmount_msg_handler_context {
1092 struct msg_handler_context_hdr hdr;
1096 wimlib_progress_func_t progfunc;
1100 struct daemon_msg_handler_context {
1101 struct msg_handler_context_hdr hdr;
1102 struct wimfs_context *wimfs_ctx;
1106 send_unmount_request_msg(mqd_t mq, int unmount_flags, u8 want_progress_messages)
1108 DEBUG("Sending unmount request msg");
1109 struct msg_unmount_request msg = {
1111 .min_version = ((unmount_flags & WIMLIB_UNMOUNT_FLAG_NEW_IMAGE) ?
1112 WIMLIB_MAKEVERSION(1, 6, 2) :
1113 WIMLIB_MAKEVERSION(1, 2, 1)),
1114 .cur_version = WIMLIB_VERSION_CODE,
1115 .msg_type = MSG_TYPE_UNMOUNT_REQUEST,
1116 .msg_size = sizeof(msg),
1118 .unmount_flags = unmount_flags,
1119 .want_progress_messages = want_progress_messages,
1122 if (mq_send(mq, (void*)&msg, sizeof(msg), 1)) {
1123 ERROR_WITH_ERRNO("Failed to communicate with filesystem daemon");
1124 return WIMLIB_ERR_MQUEUE;
1130 send_daemon_info_msg(mqd_t mq, pid_t pid, int mount_flags)
1132 DEBUG("Sending daemon info msg (pid = %d, mount_flags=%x)",
1135 struct msg_daemon_info msg = {
1137 .min_version = WIMLIB_MAKEVERSION(1, 2, 1),
1138 .cur_version = WIMLIB_VERSION_CODE,
1139 .msg_type = MSG_TYPE_DAEMON_INFO,
1140 .msg_size = sizeof(msg),
1143 .mount_flags = mount_flags,
1145 if (mq_send(mq, (void*)&msg, sizeof(msg), 1)) {
1146 ERROR_WITH_ERRNO("Failed to send daemon info to unmount process");
1147 return WIMLIB_ERR_MQUEUE;
1153 send_unmount_finished_msg(mqd_t mq, int status)
1155 DEBUG("Sending unmount finished msg");
1156 struct msg_unmount_finished msg = {
1158 .min_version = WIMLIB_MAKEVERSION(1, 2, 1),
1159 .cur_version = WIMLIB_VERSION_CODE,
1160 .msg_type = MSG_TYPE_UNMOUNT_FINISHED,
1161 .msg_size = sizeof(msg),
1165 if (mq_send(mq, (void*)&msg, sizeof(msg), 1))
1166 ERROR_WITH_ERRNO("Failed to send status to unmount process");
1169 static enum wimlib_progress_status
1170 unmount_progress_func(enum wimlib_progress_msg msg,
1171 union wimlib_progress_info *info, void *_ignored_context)
1173 if (msg == WIMLIB_PROGRESS_MSG_WRITE_STREAMS) {
1174 struct msg_write_streams_progress msg = {
1176 .min_version = WIMLIB_MAKEVERSION(1, 2, 1),
1177 .cur_version = WIMLIB_VERSION_CODE,
1178 .msg_type = MSG_TYPE_WRITE_STREAMS_PROGRESS,
1179 .msg_size = sizeof(msg),
1183 if (mq_send(wimfs_get_context()->daemon_to_unmount_mq,
1184 (void*)&msg, sizeof(msg), 1))
1186 ERROR_WITH_ERRNO("Failed to send progress information "
1187 "to unmount process");
1190 return WIMLIB_PROGRESS_STATUS_CONTINUE;
1194 release_extra_refcnts(struct wimfs_context *ctx)
1196 struct list_head *list = &ctx->orig_stream_list;
1197 struct wim_lookup_table *lookup_table = ctx->wim->lookup_table;
1198 struct wim_lookup_table_entry *lte, *tmp;
1200 list_for_each_entry_safe(lte, tmp, list, orig_stream_list) {
1201 u32 n = lte->out_refcnt;
1203 lte_decrement_refcnt(lte, lookup_table);
1207 /* Moves the currently selected image, which may have been modified, to a new
1208 * index, and sets the original index to refer to a reset (unmodified) copy of
1211 renew_current_image(struct wimfs_context *ctx)
1213 WIMStruct *wim = ctx->wim;
1215 int idx = wim->current_image - 1;
1216 struct wim_image_metadata *imd = wim->image_metadata[idx];
1217 struct wim_image_metadata *replace_imd;
1218 struct wim_lookup_table_entry *new_lte;
1220 if (imd->metadata_lte->resource_location != RESOURCE_IN_WIM) {
1221 ERROR("Can't reset modified image that doesn't yet "
1222 "exist in the on-disk WIM file!");
1223 return WIMLIB_ERR_METADATA_NOT_FOUND;
1226 /* Create 'replace_imd' structure to use for the reset original,
1227 * unmodified image. */
1228 replace_imd = new_image_metadata();
1230 return WIMLIB_ERR_NOMEM;
1232 /* Create new stream reference for the modified image's metadata
1233 * resource, which doesn't exist yet. */
1234 ret = WIMLIB_ERR_NOMEM;
1235 new_lte = new_lookup_table_entry();
1237 goto err_put_replace_imd;
1238 new_lte->flags = WIM_RESHDR_FLAG_METADATA;
1239 new_lte->unhashed = 1;
1241 /* Make the image being moved available at a new index. Increments the
1242 * WIM's image count, but does not increment the reference count of the
1243 * 'struct image_metadata'. */
1244 ret = append_image_metadata(wim, imd);
1246 goto err_free_new_lte;
1248 ret = xml_add_image(wim, T(""));
1250 goto err_undo_append;
1252 replace_imd->metadata_lte = imd->metadata_lte;
1253 imd->metadata_lte = new_lte;
1254 wim->image_metadata[idx] = replace_imd;
1255 wim->current_image = wim->hdr.image_count;
1259 wim->hdr.image_count--;
1261 free_lookup_table_entry(new_lte);
1262 err_put_replace_imd:
1263 put_image_metadata(replace_imd, NULL);
1268 msg_unmount_request_handler(const void *_msg, void *_handler_ctx)
1270 const struct msg_unmount_request *msg = _msg;
1271 struct daemon_msg_handler_context *handler_ctx = _handler_ctx;
1272 struct wimfs_context *wimfs_ctx;
1277 DEBUG("Handling unmount request msg");
1279 wimfs_ctx = handler_ctx->wimfs_ctx;
1280 if (msg->hdr.msg_size < sizeof(*msg)) {
1281 status = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1285 unmount_flags = msg->unmount_flags;
1287 wimlib_register_progress_function(wimfs_ctx->wim,
1288 (msg->want_progress_messages ?
1289 unmount_progress_func : NULL),
1292 ret = send_daemon_info_msg(wimfs_ctx->daemon_to_unmount_mq, getpid(),
1293 wimfs_ctx->mount_flags);
1299 if (wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1300 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_COMMIT) {
1302 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_NEW_IMAGE) {
1303 ret = renew_current_image(wimfs_ctx);
1309 release_extra_refcnts(wimfs_ctx);
1311 INIT_LIST_HEAD(&wimfs_ctx->orig_stream_list);
1313 int write_flags = 0;
1314 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY)
1315 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1316 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_REBUILD)
1317 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1318 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_RECOMPRESS)
1319 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
1320 status = rebuild_wim(wimfs_ctx, write_flags);
1323 DEBUG("Read-only mount");
1328 if (wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1329 ret = delete_staging_dir(wimfs_ctx);
1331 ERROR("Failed to delete the staging directory");
1336 wimfs_ctx->status = status;
1337 wimfs_ctx->have_status = true;
1338 return MSG_BREAK_LOOP;
1342 msg_daemon_info_handler(const void *_msg, void *_handler_ctx)
1344 const struct msg_daemon_info *msg = _msg;
1345 struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1347 DEBUG("Handling daemon info msg");
1348 if (msg->hdr.msg_size < sizeof(*msg))
1349 return WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1350 handler_ctx->daemon_pid = msg->daemon_pid;
1351 handler_ctx->mount_flags = msg->mount_flags;
1352 handler_ctx->hdr.timeout_seconds = 1;
1353 DEBUG("pid of daemon is %d; mount flags were %#x",
1354 handler_ctx->daemon_pid,
1355 handler_ctx->mount_flags);
1360 msg_write_streams_progress_handler(const void *_msg, void *_handler_ctx)
1362 const struct msg_write_streams_progress *msg = _msg;
1363 struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1365 if (msg->hdr.msg_size < sizeof(*msg))
1366 return WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1367 return call_progress(handler_ctx->progfunc,
1368 WIMLIB_PROGRESS_MSG_WRITE_STREAMS,
1369 (union wimlib_progress_info *)&msg->info,
1370 handler_ctx->progctx);
1374 msg_unmount_finished_handler(const void *_msg, void *_handler_ctx)
1376 const struct msg_unmount_finished *msg = _msg;
1377 struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1379 DEBUG("Handling unmount finished message");
1380 if (msg->hdr.msg_size < sizeof(*msg))
1381 return WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1382 handler_ctx->status = msg->status;
1383 DEBUG("status is %d", handler_ctx->status);
1384 return MSG_BREAK_LOOP;
1388 unmount_timed_out_cb(void *_handler_ctx)
1390 const struct unmount_msg_handler_context *handler_ctx = _handler_ctx;
1392 if (handler_ctx->daemon_pid == 0 ||
1393 (kill(handler_ctx->daemon_pid, 0) != 0 && errno == ESRCH))
1395 ERROR("The filesystem daemon has crashed! Changes to the "
1396 "WIM may not have been commited.");
1397 return WIMLIB_ERR_FILESYSTEM_DAEMON_CRASHED;
1400 DEBUG("Filesystem daemon is still alive... "
1401 "Waiting another %d seconds", handler_ctx->hdr.timeout_seconds);
1406 daemon_timed_out_cb(void *_handler_ctx)
1408 ERROR("Timed out waiting for unmount request! "
1409 "Changes to the mounted WIM will not be committed.");
1410 return WIMLIB_ERR_TIMEOUT;
1413 typedef int (*msg_handler_t)(const void *_msg, void *_handler_ctx);
1415 struct msg_handler_callbacks {
1416 int (*timed_out)(void * _handler_ctx);
1417 msg_handler_t msg_handlers[MSG_TYPE_MAX];
1420 static const struct msg_handler_callbacks unmount_msg_handler_callbacks = {
1421 .timed_out = unmount_timed_out_cb,
1423 [MSG_TYPE_DAEMON_INFO] = msg_daemon_info_handler,
1424 [MSG_TYPE_WRITE_STREAMS_PROGRESS] = msg_write_streams_progress_handler,
1425 [MSG_TYPE_UNMOUNT_FINISHED] = msg_unmount_finished_handler,
1429 static const struct msg_handler_callbacks daemon_msg_handler_callbacks = {
1430 .timed_out = daemon_timed_out_cb,
1432 [MSG_TYPE_UNMOUNT_REQUEST] = msg_unmount_request_handler,
1437 receive_message(mqd_t mq,
1438 struct msg_handler_context_hdr *handler_ctx,
1439 const msg_handler_t msg_handlers[],
1440 long mailbox_size, void *mailbox)
1443 struct timespec timeout;
1444 ssize_t bytes_received;
1445 struct unmount_msg_hdr *hdr;
1448 gettimeofday(&now, NULL);
1449 timeout.tv_sec = now.tv_sec + handler_ctx->timeout_seconds;
1450 timeout.tv_nsec = now.tv_usec * 1000;
1452 bytes_received = mq_timedreceive(mq, mailbox,
1453 mailbox_size, NULL, &timeout);
1455 if (bytes_received == -1) {
1456 if (errno == ETIMEDOUT) {
1457 ret = WIMLIB_ERR_TIMEOUT;
1459 ERROR_WITH_ERRNO("mq_timedreceive()");
1460 ret = WIMLIB_ERR_MQUEUE;
1462 } else if (bytes_received < sizeof(*hdr) ||
1463 bytes_received != hdr->msg_size) {
1464 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1465 } else if (WIMLIB_VERSION_CODE < hdr->min_version) {
1466 /*ERROR("Cannot understand the received message. "*/
1467 /*"Please upgrade wimlib to at least v%d.%d.%d",*/
1468 /*WIMLIB_GET_MAJOR_VERSION(hdr->min_version),*/
1469 /*WIMLIB_GET_MINOR_VERSION(hdr->min_version),*/
1470 /*WIMLIB_GET_PATCH_VERSION(hdr->min_version));*/
1471 ret = MSG_VERSION_TOO_HIGH;
1472 } else if (hdr->msg_type >= MSG_TYPE_MAX) {
1473 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1474 } else if (msg_handlers[hdr->msg_type] == NULL) {
1475 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1477 ret = msg_handlers[hdr->msg_type](mailbox, handler_ctx);
1483 message_loop(mqd_t mq,
1484 const struct msg_handler_callbacks *callbacks,
1485 struct msg_handler_context_hdr *handler_ctx)
1487 static const size_t MAX_MSG_SIZE = 512;
1492 DEBUG("Entering message loop");
1494 ret = get_mailbox(mq, MAX_MSG_SIZE, &msgsize, &mailbox);
1498 ret = receive_message(mq, handler_ctx,
1499 callbacks->msg_handlers,
1501 if (ret == 0 || ret == MSG_VERSION_TOO_HIGH) {
1503 } else if (ret == MSG_BREAK_LOOP) {
1506 } else if (ret == WIMLIB_ERR_TIMEOUT) {
1507 if (callbacks->timed_out)
1508 ret = callbacks->timed_out(handler_ctx);
1514 ERROR_WITH_ERRNO("Error communicating with "
1515 "filesystem daemon");
1520 DEBUG("Exiting message loop");
1524 /* Execute `fusermount -u', which is installed setuid root, to unmount the WIM.
1526 * FUSE does not yet implement synchronous unmounts. This means that fusermount
1527 * -u will return before the filesystem daemon returns from wimfs_destroy().
1528 * This is partly what we want, because we need to send a message from this
1529 * process to the filesystem daemon telling whether --commit was specified or
1530 * not. However, after that, the unmount process must wait for the filesystem
1531 * daemon to finish writing the WIM file.
1534 execute_fusermount(const char *dir, bool lazy)
1542 ERROR_WITH_ERRNO("Failed to fork()");
1543 return WIMLIB_ERR_FORK;
1549 *argp++ = "fusermount";
1553 *argp++ = (char*)dir;
1555 execvp("fusermount", argv);
1556 ERROR_WITH_ERRNO("Failed to execute `fusermount'");
1557 exit(WIMLIB_ERR_FUSERMOUNT);
1561 ret = waitpid(pid, &status, 0);
1563 ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
1565 return WIMLIB_ERR_FUSERMOUNT;
1568 if (!WIFEXITED(status)) {
1569 ERROR("'fusermount' did not terminate normally!");
1570 return WIMLIB_ERR_FUSERMOUNT;
1573 status = WEXITSTATUS(status);
1578 if (status != WIMLIB_ERR_FUSERMOUNT)
1579 return WIMLIB_ERR_FUSERMOUNT;
1581 /* Try again, but with the `umount' program. This is required on other
1582 * FUSE implementations such as FreeBSD's that do not have a
1583 * `fusermount' program. */
1584 ERROR("Falling back to 'umount'. Note: you may need to be "
1585 "root for this to work");
1588 ERROR_WITH_ERRNO("Failed to fork()");
1589 return WIMLIB_ERR_FORK;
1598 *argp++ = (char*)dir;
1600 execvp("umount", argv);
1601 ERROR_WITH_ERRNO("Failed to execute `umount'");
1602 exit(WIMLIB_ERR_FUSERMOUNT);
1606 ret = waitpid(pid, &status, 0);
1608 ERROR_WITH_ERRNO("Failed to wait for `umount' process to "
1610 return WIMLIB_ERR_FUSERMOUNT;
1613 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
1614 ERROR("`umount' did not successfully complete");
1615 return WIMLIB_ERR_FUSERMOUNT;
1622 wimfs_chmod(const char *path, mode_t mask)
1624 struct wim_dentry *dentry;
1625 struct wim_inode *inode;
1626 struct wimfs_context *ctx = wimfs_get_context();
1627 struct wimlib_unix_data unix_data;
1630 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA))
1633 ret = wim_pathname_to_stream(ctx->wim, path, LOOKUP_FLAG_DIRECTORY_OK,
1634 &dentry, NULL, NULL);
1638 inode = dentry->d_inode;
1640 unix_data.uid = ctx->default_uid;
1641 unix_data.gid = ctx->default_gid;
1642 unix_data.mode = mask;
1644 if (!inode_set_unix_data(inode, &unix_data, UNIX_DATA_MODE))
1651 wimfs_chown(const char *path, uid_t uid, gid_t gid)
1653 struct wim_dentry *dentry;
1654 struct wim_inode *inode;
1655 struct wimfs_context *ctx = wimfs_get_context();
1657 struct wimlib_unix_data unix_data;
1660 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA))
1663 ret = wim_pathname_to_stream(ctx->wim, path, LOOKUP_FLAG_DIRECTORY_OK,
1664 &dentry, NULL, NULL);
1668 inode = dentry->d_inode;
1672 if (uid != (uid_t)-1)
1673 which |= UNIX_DATA_UID;
1675 uid = ctx->default_uid;
1677 if (gid != (gid_t)-1)
1678 which |= UNIX_DATA_GID;
1680 gid = ctx->default_gid;
1683 unix_data.uid = uid;
1684 unix_data.gid = gid;
1685 unix_data.mode = inode_default_unix_mode(inode);
1687 if (!inode_set_unix_data(inode, &unix_data, which))
1693 /* Called when the filesystem is unmounted. */
1695 wimfs_destroy(void *p)
1697 struct wimfs_context *wimfs_ctx = wimfs_get_context();
1698 if (open_message_queues(wimfs_ctx, true) == 0) {
1699 struct daemon_msg_handler_context handler_ctx = {
1701 .timeout_seconds = 5,
1703 .wimfs_ctx = wimfs_ctx,
1705 message_loop(wimfs_ctx->unmount_to_daemon_mq,
1706 &daemon_msg_handler_callbacks,
1712 wimfs_fgetattr(const char *path, struct stat *stbuf,
1713 struct fuse_file_info *fi)
1715 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1716 return inode_to_stbuf(fd->f_inode, fd->f_lte, stbuf);
1720 wimfs_ftruncate(const char *path, off_t size, struct fuse_file_info *fi)
1722 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
1723 int ret = ftruncate(fd->staging_fd.fd, size);
1726 touch_inode(fd->f_inode);
1727 fd->f_lte->size = size;
1732 * Fills in a `struct stat' that corresponds to a file or directory in the WIM.
1735 wimfs_getattr(const char *path, struct stat *stbuf)
1737 struct wim_dentry *dentry;
1738 struct wim_lookup_table_entry *lte;
1740 struct wimfs_context *ctx = wimfs_get_context();
1742 ret = wim_pathname_to_stream(ctx->wim, path,
1743 get_lookup_flags(ctx) |
1744 LOOKUP_FLAG_DIRECTORY_OK,
1745 &dentry, <e, NULL);
1748 return inode_to_stbuf(dentry->d_inode, lte, stbuf);
1752 /* Read an alternate data stream through the XATTR interface, or get its size */
1754 wimfs_getxattr(const char *path, const char *name, char *value,
1758 struct wim_inode *inode;
1759 struct wim_ads_entry *ads_entry;
1761 struct wim_lookup_table_entry *lte;
1762 struct wimfs_context *ctx = wimfs_get_context();
1764 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1767 if (strlen(name) <= 5 || memcmp(name, "user.", 5) != 0)
1771 inode = wim_pathname_to_inode(ctx->wim, path);
1775 ads_entry = inode_get_ads_entry(inode, name, NULL);
1779 lte = ads_entry->lte;
1780 stream_size = lte->size;
1785 if (stream_size > size)
1788 ret = read_full_stream_into_buf(lte, value);
1799 /* Create a hard link */
1801 wimfs_link(const char *to, const char *from)
1803 struct wim_dentry *from_dentry, *from_dentry_parent;
1804 const char *link_name;
1805 struct wim_inode *inode;
1806 WIMStruct *wim = wimfs_get_WIMStruct();
1809 inode = wim_pathname_to_inode(wim, to);
1813 if (inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
1814 FILE_ATTRIBUTE_REPARSE_POINT))
1817 from_dentry_parent = get_parent_dentry(wim, from, WIMLIB_CASE_SENSITIVE);
1818 if (!from_dentry_parent)
1820 if (!dentry_is_directory(from_dentry_parent))
1823 link_name = path_basename(from);
1824 if (get_dentry_child_with_name(from_dentry_parent, link_name,
1825 WIMLIB_CASE_SENSITIVE))
1828 ret = new_dentry(link_name, &from_dentry);
1833 inode_ref_streams(inode);
1834 from_dentry->d_inode = inode;
1835 inode_add_dentry(from_dentry, inode);
1836 dentry_add_child(from_dentry_parent, from_dentry);
1842 wimfs_listxattr(const char *path, char *list, size_t size)
1845 struct wim_inode *inode;
1846 struct wimfs_context *ctx = wimfs_get_context();
1849 bool size_only = (size == 0);
1851 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1854 /* List alternate data streams, or get the list size */
1856 inode = wim_pathname_to_inode(ctx->wim, path);
1861 for (i = 0; i < inode->i_num_ads; i++) {
1863 if (!ads_entry_is_named_stream(&inode->i_ads_entries[i]))
1866 char *stream_name_mbs;
1867 size_t stream_name_mbs_nbytes;
1870 ret = utf16le_to_tstr(inode->i_ads_entries[i].stream_name,
1871 inode->i_ads_entries[i].stream_name_nbytes,
1873 &stream_name_mbs_nbytes);
1877 needed_size = stream_name_mbs_nbytes + 6;
1879 if (needed_size > size) {
1880 FREE(stream_name_mbs);
1883 sprintf(p, "user.%s", stream_name_mbs);
1884 size -= needed_size;
1887 FREE(stream_name_mbs);
1894 /* Create a directory in the WIM image. */
1896 wimfs_mkdir(const char *path, mode_t mode)
1898 return create_dentry(fuse_get_context(), path, mode | S_IFDIR, 0,
1899 FILE_ATTRIBUTE_DIRECTORY, NULL);
1902 /* Create a non-directory, non-symbolic-link file or alternate data stream in
1905 wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
1907 const char *stream_name;
1908 struct fuse_context *fuse_ctx = fuse_get_context();
1909 struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
1911 if ((wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
1912 && (stream_name = path_stream_name(path))) {
1917 /* Make an alternate data stream */
1918 struct wim_ads_entry *new_entry;
1919 struct wim_inode *inode;
1921 char *p = (char*)stream_name - 1;
1922 wimlib_assert(*p == ':');
1925 inode = wim_pathname_to_inode(wimfs_ctx->wim, path);
1928 if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT)
1930 if (inode_get_ads_entry(inode, stream_name, NULL))
1932 new_entry = inode_add_ads(inode, stream_name);
1937 if (!S_ISREG(mode) &&
1938 !(wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_UNIX_DATA))
1941 /* Make a regular file, device node, named pipe, or socket. */
1942 return create_dentry(fuse_ctx, path, mode, rdev,
1943 FILE_ATTRIBUTE_NORMAL, NULL);
1949 wimfs_open(const char *path, struct fuse_file_info *fi)
1951 struct wim_dentry *dentry;
1952 struct wim_lookup_table_entry *lte;
1954 struct wimfs_fd *fd;
1955 struct wim_inode *inode;
1958 struct wimfs_context *ctx = wimfs_get_context();
1959 struct wim_lookup_table_entry **back_ptr;
1961 ret = wim_pathname_to_stream(ctx->wim, path, get_lookup_flags(ctx),
1962 &dentry, <e, &stream_idx);
1966 inode = dentry->d_inode;
1968 if (stream_idx == 0) {
1970 back_ptr = &inode->i_lte;
1972 stream_id = inode->i_ads_entries[stream_idx - 1].stream_id;
1973 back_ptr = &inode->i_ads_entries[stream_idx - 1].lte;
1976 /* The file resource may be in the staging directory (read-write mounts
1977 * only) or in the WIM. If it's in the staging directory, we need to
1978 * open a native file descriptor for the corresponding file. Otherwise,
1979 * we can read the file resource directly from the WIM file if we are
1980 * opening it read-only, but we need to extract the resource to the
1981 * staging directory if we are opening it writable. */
1983 if (flags_writable(fi->flags) &&
1984 (!lte || lte->resource_location != RESOURCE_IN_STAGING_FILE)) {
1985 u64 size = (lte) ? lte->size : 0;
1986 ret = extract_resource_to_staging_dir(inode, stream_id,
1993 ret = alloc_wimfs_fd(inode, stream_id, lte, &fd);
1997 if (lte && lte->resource_location == RESOURCE_IN_STAGING_FILE) {
2000 raw_fd = open(lte->staging_file_name, fi->flags);
2002 int errno_save = errno;
2006 filedes_init(&fd->staging_fd, raw_fd);
2008 fi->fh = (uintptr_t)fd;
2012 /* Opens a directory. */
2014 wimfs_opendir(const char *path, struct fuse_file_info *fi)
2016 struct wim_inode *inode;
2018 struct wimfs_fd *fd = NULL;
2019 struct wimfs_context *ctx = wimfs_get_context();
2020 WIMStruct *wim = ctx->wim;
2022 inode = wim_pathname_to_inode(wim, path);
2025 if (!inode_is_directory(inode))
2027 ret = alloc_wimfs_fd(inode, 0, NULL, &fd);
2028 fi->fh = (uintptr_t)fd;
2034 * Read data from a file in the WIM or in the staging directory.
2037 wimfs_read(const char *path, char *buf, size_t size,
2038 off_t offset, struct fuse_file_info *fi)
2040 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
2051 stream_size = fd->f_lte->size;
2055 if (offset > stream_size)
2058 size = min(size, stream_size - offset);
2062 switch (fd->f_lte->resource_location) {
2063 case RESOURCE_IN_STAGING_FILE:
2064 ret = raw_pread(&fd->staging_fd, buf, size, offset);
2068 case RESOURCE_IN_WIM:
2069 if (read_partial_wim_stream_into_buf(fd->f_lte, size,
2071 ret = errno ? -errno : -EIO;
2075 case RESOURCE_IN_ATTACHED_BUFFER:
2076 memcpy(buf, fd->f_lte->attached_buffer + offset, size);
2080 ERROR("Invalid resource location");
2087 /* Fills in the entries of the directory specified by @path using the
2088 * FUSE-provided function @filler. */
2090 wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
2091 off_t offset, struct fuse_file_info *fi)
2093 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
2094 struct wim_inode *inode;
2095 struct wim_dentry *child;
2101 inode = fd->f_inode;
2103 ret = filler(buf, ".", NULL, 0);
2106 ret = filler(buf, "..", NULL, 0);
2110 for_inode_child(child, inode) {
2111 char *file_name_mbs;
2112 size_t file_name_mbs_nbytes;
2114 ret = utf16le_to_tstr(child->file_name,
2115 child->file_name_nbytes,
2117 &file_name_mbs_nbytes);
2121 ret = filler(buf, file_name_mbs, NULL, 0);
2122 FREE(file_name_mbs);
2131 wimfs_readlink(const char *path, char *buf, size_t buf_len)
2133 struct wimfs_context *ctx = wimfs_get_context();
2134 struct wim_inode *inode = wim_pathname_to_inode(ctx->wim, path);
2138 if (!inode_is_symlink(inode))
2142 ret = wim_inode_readlink(inode, buf, buf_len - 1, NULL);
2144 wimlib_assert(ret <= buf_len - 1);
2147 } else if (ret == -ENAMETOOLONG) {
2148 buf[buf_len - 1] = '\0';
2155 wimfs_release(const char *path, struct fuse_file_info *fi)
2157 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
2158 return close_wimfs_fd(fd);
2161 /* Close a directory */
2163 wimfs_releasedir(const char *path, struct fuse_file_info *fi)
2165 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
2166 return close_wimfs_fd(fd);
2170 /* Remove an alternate data stream through the XATTR interface */
2172 wimfs_removexattr(const char *path, const char *name)
2174 struct wim_inode *inode;
2175 struct wim_ads_entry *ads_entry;
2177 struct wimfs_context *ctx = wimfs_get_context();
2179 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
2182 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
2186 inode = wim_pathname_to_inode(ctx->wim, path);
2190 ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
2193 inode_remove_ads(inode, ads_idx, ctx->wim->lookup_table);
2198 /* Renames a file or directory. See rename (3) */
2200 wimfs_rename(const char *from, const char *to)
2202 return rename_wim_path(wimfs_get_WIMStruct(), from, to,
2203 WIMLIB_CASE_SENSITIVE, NULL);
2206 /* Remove a directory */
2208 wimfs_rmdir(const char *path)
2210 struct wim_dentry *dentry;
2211 WIMStruct *wim = wimfs_get_WIMStruct();
2213 dentry = get_dentry(wim, path, WIMLIB_CASE_SENSITIVE);
2217 if (!dentry_is_directory(dentry))
2220 if (dentry_has_children(dentry))
2223 remove_dentry(dentry, wim->lookup_table);
2228 /* Write an alternate data stream through the XATTR interface */
2230 wimfs_setxattr(const char *path, const char *name,
2231 const char *value, size_t size, int flags)
2233 struct wim_ads_entry *existing_ads_entry;
2234 struct wim_inode *inode;
2236 struct wimfs_context *ctx = wimfs_get_context();
2239 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
2242 if (strlen(name) <= 5 || memcmp(name, "user.", 5) != 0)
2246 inode = wim_pathname_to_inode(ctx->wim, path);
2250 existing_ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
2251 if (existing_ads_entry) {
2252 if (flags & XATTR_CREATE)
2255 if (flags & XATTR_REPLACE)
2259 ret = inode_add_ads_with_data(inode, name, value,
2260 size, ctx->wim->lookup_table);
2262 if (existing_ads_entry)
2263 inode_remove_ads(inode, ads_idx, ctx->wim->lookup_table);
2272 wimfs_symlink(const char *to, const char *from)
2274 struct fuse_context *fuse_ctx = fuse_get_context();
2275 struct wimfs_context *wimfs_ctx = WIMFS_CTX(fuse_ctx);
2276 struct wim_dentry *dentry;
2279 ret = create_dentry(fuse_ctx, from, S_IFLNK | 0777, 0,
2280 FILE_ATTRIBUTE_REPARSE_POINT, &dentry);
2282 dentry->d_inode->i_reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
2283 ret = wim_inode_set_symlink(dentry->d_inode, to,
2284 wimfs_ctx->wim->lookup_table);
2286 remove_dentry(dentry, wimfs_ctx->wim->lookup_table);
2287 if (ret == WIMLIB_ERR_NOMEM)
2297 /* Reduce the size of a file */
2299 wimfs_truncate(const char *path, off_t size)
2301 struct wim_dentry *dentry;
2302 struct wim_lookup_table_entry *lte;
2306 struct wim_inode *inode;
2307 struct wimfs_context *ctx = wimfs_get_context();
2309 ret = wim_pathname_to_stream(ctx->wim, path, get_lookup_flags(ctx),
2310 &dentry, <e, &stream_idx);
2315 if (lte == NULL && size == 0)
2318 if (lte != NULL && lte->resource_location == RESOURCE_IN_STAGING_FILE) {
2319 ret = truncate(lte->staging_file_name, size);
2325 /* File in WIM. Extract it to the staging directory, but only
2326 * the first @size bytes of it. */
2327 struct wim_lookup_table_entry **back_ptr;
2329 inode = dentry->d_inode;
2330 if (stream_idx == 0) {
2332 back_ptr = &inode->i_lte;
2334 stream_id = inode->i_ads_entries[stream_idx - 1].stream_id;
2335 back_ptr = &inode->i_ads_entries[stream_idx - 1].lte;
2337 ret = extract_resource_to_staging_dir(inode, stream_id,
2344 /* Unlink a non-directory or alternate data stream */
2346 wimfs_unlink(const char *path)
2348 struct wim_dentry *dentry;
2349 struct wim_lookup_table_entry *lte;
2352 struct wimfs_context *ctx = wimfs_get_context();
2354 ret = wim_pathname_to_stream(ctx->wim, path, get_lookup_flags(ctx),
2355 &dentry, <e, &stream_idx);
2360 if (inode_stream_name_nbytes(dentry->d_inode, stream_idx) == 0)
2361 remove_dentry(dentry, ctx->wim->lookup_table);
2363 inode_remove_ads(dentry->d_inode, stream_idx - 1,
2364 ctx->wim->lookup_table);
2368 #ifdef HAVE_UTIMENSAT
2370 * Change the timestamp on a file dentry.
2372 * Note that alternate data streams do not have their own timestamps.
2375 wimfs_utimens(const char *path, const struct timespec tv[2])
2377 struct wim_inode *inode;
2378 WIMStruct *wim = wimfs_get_WIMStruct();
2380 inode = wim_pathname_to_inode(wim, path);
2384 if (tv[0].tv_nsec != UTIME_OMIT) {
2385 if (tv[0].tv_nsec == UTIME_NOW)
2386 inode->i_last_access_time = get_wim_timestamp();
2388 inode->i_last_access_time = timespec_to_wim_timestamp(tv[0]);
2390 if (tv[1].tv_nsec != UTIME_OMIT) {
2391 if (tv[1].tv_nsec == UTIME_NOW)
2392 inode->i_last_write_time = get_wim_timestamp();
2394 inode->i_last_write_time = timespec_to_wim_timestamp(tv[1]);
2398 #else /* HAVE_UTIMENSAT */
2400 wimfs_utime(const char *path, struct utimbuf *times)
2402 struct wim_inode *inode;
2403 WIMStruct *wim = wimfs_get_WIMStruct();
2405 inode = wim_pathname_to_inode(wim, path);
2409 inode->i_last_write_time = unix_timestamp_to_wim(times->modtime);
2410 inode->i_last_access_time = unix_timestamp_to_wim(times->actime);
2413 #endif /* !HAVE_UTIMENSAT */
2415 /* Writes to a file in the WIM filesystem.
2416 * It may be an alternate data stream, but here we don't even notice because we
2417 * just get a lookup table entry. */
2419 wimfs_write(const char *path, const char *buf, size_t size,
2420 off_t offset, struct fuse_file_info *fi)
2422 struct wimfs_fd *fd = (struct wimfs_fd*)(uintptr_t)fi->fh;
2428 wimlib_assert(fd->f_lte != NULL);
2429 wimlib_assert(fd->f_lte->staging_file_name != NULL);
2430 wimlib_assert(filedes_valid(&fd->staging_fd));
2431 wimlib_assert(fd->f_inode != NULL);
2433 /* Write the data. */
2434 ret = raw_pwrite(&fd->staging_fd, buf, size, offset);
2438 /* Update file size */
2439 if (offset + size > fd->f_lte->size) {
2440 DEBUG("Update file size %"PRIu64 " => %"PRIu64"",
2441 fd->f_lte->size, offset + size);
2442 fd->f_lte->size = offset + size;
2445 /* Update timestamps */
2446 touch_inode(fd->f_inode);
2450 static struct fuse_operations wimfs_operations = {
2451 .chmod = wimfs_chmod,
2452 .chown = wimfs_chown,
2453 .destroy = wimfs_destroy,
2454 .fgetattr = wimfs_fgetattr,
2455 .ftruncate = wimfs_ftruncate,
2456 .getattr = wimfs_getattr,
2458 .getxattr = wimfs_getxattr,
2462 .listxattr = wimfs_listxattr,
2464 .mkdir = wimfs_mkdir,
2465 .mknod = wimfs_mknod,
2467 .opendir = wimfs_opendir,
2469 .readdir = wimfs_readdir,
2470 .readlink = wimfs_readlink,
2471 .release = wimfs_release,
2472 .releasedir = wimfs_releasedir,
2474 .removexattr = wimfs_removexattr,
2476 .rename = wimfs_rename,
2477 .rmdir = wimfs_rmdir,
2479 .setxattr = wimfs_setxattr,
2481 .symlink = wimfs_symlink,
2482 .truncate = wimfs_truncate,
2483 .unlink = wimfs_unlink,
2484 #ifdef HAVE_UTIMENSAT
2485 .utimens = wimfs_utimens,
2487 .utime = wimfs_utime,
2489 .write = wimfs_write,
2491 /* wimfs keeps file descriptor structures (struct wimfs_fd), so there is
2492 * no need to have the file path provided on operations such as read()
2493 * where only the file descriptor is needed. */
2494 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 8)
2495 .flag_nullpath_ok = 1,
2497 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 9)
2499 .flag_utime_omit_ok = 1,
2504 /* API function documented in wimlib.h */
2506 wimlib_mount_image(WIMStruct *wim, int image, const char *dir,
2507 int mount_flags, const char *staging_dir)
2513 struct wim_image_metadata *imd;
2514 struct wimfs_context ctx;
2515 struct wim_inode *inode;
2517 DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
2518 wim, image, dir, mount_flags);
2521 return WIMLIB_ERR_INVALID_PARAM;
2523 if (mount_flags & ~(WIMLIB_MOUNT_FLAG_READWRITE |
2524 WIMLIB_MOUNT_FLAG_DEBUG |
2525 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
2526 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
2527 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS |
2528 WIMLIB_MOUNT_FLAG_UNIX_DATA |
2529 WIMLIB_MOUNT_FLAG_ALLOW_OTHER))
2530 return WIMLIB_ERR_INVALID_PARAM;
2532 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2533 ret = can_delete_from_wim(wim);
2538 ret = select_wim_image(wim, image);
2542 DEBUG("Selected image %d", image);
2544 imd = wim_get_current_image_metadata(wim);
2546 if (imd->modified) {
2547 /* wimfs_read() only supports a limited number of stream
2548 * locations, not including RESOURCE_IN_FILE_ON_DISK,
2549 * RESOURCE_IN_NTFS_VOLUME, etc. that might appear if files were
2550 * added to the WIM image. */
2551 ERROR("Cannot mount an image with newly added files!");
2552 return WIMLIB_ERR_INVALID_PARAM;
2555 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2556 ret = lock_wim(wim, wim->in_fd.fd);
2561 /* Use default stream interface if one was not specified */
2562 if (!(mount_flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
2563 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
2564 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
2565 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
2567 DEBUG("Initializing struct wimfs_context");
2568 init_wimfs_context(&ctx);
2570 ctx.mount_flags = mount_flags;
2571 ctx.image_inode_list = &imd->inode_list;
2572 ctx.default_uid = getuid();
2573 ctx.default_gid = getgid();
2574 wimlib_assert(list_empty(&imd->unhashed_streams));
2575 if (mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
2576 ctx.default_lookup_flags = LOOKUP_FLAG_ADS_OK;
2578 DEBUG("Unlinking message queues in case they already exist");
2579 ret = set_message_queue_names(&ctx, dir);
2582 unlink_message_queues(&ctx);
2584 DEBUG("Preparing arguments to fuse_main()");
2586 dir_copy = STRDUP(dir);
2588 ret = WIMLIB_ERR_NOMEM;
2589 goto out_free_message_queue_names;
2593 argv[argc++] = "wimlib";
2594 argv[argc++] = dir_copy;
2596 /* disable multi-threaded operation */
2597 argv[argc++] = "-s";
2599 if (mount_flags & WIMLIB_MOUNT_FLAG_DEBUG)
2600 argv[argc++] = "-d";
2603 * We provide the use_ino option to the FUSE mount because we are going
2604 * to assign inode numbers ourselves. */
2605 char optstring[256] =
2609 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 8)
2612 ",default_permissions"
2614 argv[argc++] = "-o";
2615 argv[argc++] = optstring;
2616 if ((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
2617 /* Read-write mount. Make the staging directory */
2618 ret = make_staging_dir(&ctx, staging_dir);
2620 goto out_free_dir_copy;
2622 /* Read-only mount */
2623 strcat(optstring, ",ro");
2625 if (mount_flags & WIMLIB_MOUNT_FLAG_ALLOW_OTHER)
2626 strcat(optstring, ",allow_other");
2632 DEBUG("FUSE command line (argc = %d): ", argc);
2633 for (i = 0; i < argc; i++) {
2634 fputs(argv[i], stdout);
2642 /* Assign inode numbers. Also, if a read-write mount was requested,
2643 * mark the dentry tree as modified, and add each stream referenced by
2644 * files in the image to a list and preemptively double the number of
2645 * references to each. The latter is done to allow implementing the
2646 * WIMLIB_UNMOUNT_FLAG_NEW_IMAGE semantics. */
2648 INIT_LIST_HEAD(&ctx.orig_stream_list);
2649 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2651 image_for_each_inode(inode, imd) {
2652 inode->i_ino = ctx.next_ino++;
2653 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
2654 struct wim_lookup_table_entry *lte;
2656 lte = inode_stream_lte(inode, i, wim->lookup_table);
2658 lte->out_refcnt = 0;
2661 image_for_each_inode(inode, imd) {
2662 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
2663 struct wim_lookup_table_entry *lte;
2665 lte = inode_stream_lte(inode, i,
2668 if (lte->out_refcnt == 0)
2669 list_add(<e->orig_stream_list,
2670 &ctx.orig_stream_list);
2671 lte->out_refcnt += inode->i_nlink;
2672 lte->refcnt += inode->i_nlink;
2677 image_for_each_inode(inode, imd)
2678 inode->i_ino = ctx.next_ino++;
2681 DEBUG("(next_ino = %"PRIu64")", ctx.next_ino);
2683 DEBUG("Calling fuse_main()");
2685 ret = fuse_main(argc, argv, &wimfs_operations, &ctx);
2687 DEBUG("Returned from fuse_main() (ret = %d)", ret);
2690 ret = WIMLIB_ERR_FUSE;
2692 if (ctx.have_status)
2695 ret = WIMLIB_ERR_TIMEOUT;
2697 if (ctx.daemon_to_unmount_mq != (mqd_t)(-1)) {
2698 send_unmount_finished_msg(ctx.daemon_to_unmount_mq, ret);
2699 close_message_queues(&ctx);
2702 release_extra_refcnts(&ctx);
2704 /* Try to delete the staging directory if a deletion wasn't yet
2705 * attempted due to an earlier error */
2706 if (ctx.staging_dir_name)
2707 delete_staging_dir(&ctx);
2711 wim->wim_locked = 0;
2712 out_free_message_queue_names:
2713 free_message_queue_names(&ctx);
2717 /* API function documented in wimlib.h */
2719 wimlib_unmount_image_with_progress(const tchar *dir, int unmount_flags,
2720 wimlib_progress_func_t progfunc, void *progctx)
2723 struct wimfs_context wimfs_ctx;
2725 if (unmount_flags & ~(WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY |
2726 WIMLIB_UNMOUNT_FLAG_COMMIT |
2727 WIMLIB_UNMOUNT_FLAG_REBUILD |
2728 WIMLIB_UNMOUNT_FLAG_RECOMPRESS |
2729 WIMLIB_UNMOUNT_FLAG_LAZY |
2730 WIMLIB_UNMOUNT_FLAG_NEW_IMAGE))
2731 return WIMLIB_ERR_INVALID_PARAM;
2733 init_wimfs_context(&wimfs_ctx);
2735 ret = set_message_queue_names(&wimfs_ctx, dir);
2739 ret = open_message_queues(&wimfs_ctx, false);
2741 goto out_free_message_queue_names;
2743 ret = send_unmount_request_msg(wimfs_ctx.unmount_to_daemon_mq,
2747 goto out_close_message_queues;
2749 ret = execute_fusermount(dir, (unmount_flags & WIMLIB_UNMOUNT_FLAG_LAZY) != 0);
2751 goto out_close_message_queues;
2753 struct unmount_msg_handler_context handler_ctx = {
2755 .timeout_seconds = 5,
2758 .progfunc = progfunc,
2762 ret = message_loop(wimfs_ctx.daemon_to_unmount_mq,
2763 &unmount_msg_handler_callbacks,
2766 ret = handler_ctx.status;
2767 out_close_message_queues:
2768 close_message_queues(&wimfs_ctx);
2769 out_free_message_queue_names:
2770 free_message_queue_names(&wimfs_ctx);
2775 #else /* WITH_FUSE */
2779 mount_unsupported_error(void)
2781 #if defined(__WIN32__)
2782 ERROR("Sorry-- Mounting WIM images is not supported on Windows!");
2784 ERROR("wimlib was compiled with --without-fuse, which disables support "
2785 "for mounting WIMs.");
2787 return WIMLIB_ERR_UNSUPPORTED;
2791 wimlib_unmount_image_with_progress(const tchar *dir, int unmount_flags,
2792 wimlib_progress_func_t progfunc, void *progctx)
2794 return mount_unsupported_error();
2798 wimlib_mount_image(WIMStruct *wim, int image, const tchar *dir,
2799 int mount_flags, const tchar *staging_dir)
2801 return mount_unsupported_error();
2804 #endif /* !WITH_FUSE */
2808 wimlib_unmount_image(const tchar *dir, int unmount_flags)
2810 return wimlib_unmount_image_with_progress(dir, unmount_flags, NULL, NULL);