4 * This file implements mounting of WIM files using FUSE, which stands for
5 * Filesystem in Userspace. FUSE allows a filesystem to be implemented in a
6 * userspace process by implementing the filesystem primitives--- read(),
7 * write(), readdir(), etc.
11 * Copyright (C) 2012 Eric Biggers
13 * This file is part of wimlib, a library for working with WIM files.
15 * wimlib is free software; you can redistribute it and/or modify it under the
16 * terms of the GNU General Public License as published by the Free
17 * Software Foundation; either version 3 of the License, or (at your option)
20 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
21 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
22 * A PARTICULAR PURPOSE. See the GNU General Public License for more
25 * You should have received a copy of the GNU General Public License
26 * along with wimlib; if not, see http://www.gnu.org/licenses/.
29 #include "wimlib_internal.h"
34 #include "lookup_table.h"
36 #include "buffer_io.h"
37 #include "timestamp.h"
42 #define FUSE_USE_VERSION 26
52 #include <attr/xattr.h>
55 #define MSG_VERSION_TOO_HIGH -1
56 #define MSG_BREAK_LOOP -2
58 /* File descriptor to a file open on the WIM filesystem. */
60 struct inode *f_inode;
61 struct lookup_table_entry *f_lte;
67 struct wimfs_context {
68 /* The WIMStruct for the mounted WIM. */
71 /* Name of the staging directory for a read-write mount. Whenever a new file is
72 * created, it is done so in the staging directory. Furthermore, whenever a
73 * file in the WIM is modified, it is extracted to the staging directory. If
74 * changes are commited when the WIM is unmounted, the file resources are merged
75 * in from the staging directory when writing the new WIM. */
76 char *staging_dir_name;
77 size_t staging_dir_name_len;
79 /* Flags passed to wimlib_mount(). */
82 int default_lookup_flags;
84 /* Next inode number to be assigned. */
87 /* List of lookup table entries in the staging directory */
88 struct list_head staging_list;
90 /* List of inodes in the mounted image */
91 struct hlist_head *image_inode_list;
93 /* Name and message queue descriptors for message queues between the filesystem
94 * daemon process and the unmount process. These are used when the filesystem
95 * is unmounted and the process running wimlib_mount() (i.e. the `imagex
96 * unmount' command) needs to communicate with the filesystem daemon running
97 * fuse_main() (i.e. that spawned by the `imagex mount' or `imagex mountrw'
99 char *unmount_to_daemon_mq_name;
100 char *daemon_to_unmount_mq_name;
101 mqd_t unmount_to_daemon_mq;
102 mqd_t daemon_to_unmount_mq;
105 static void init_wimfs_context(struct wimfs_context *ctx)
107 memset(ctx, 0, sizeof(*ctx));
108 ctx->unmount_to_daemon_mq = (mqd_t)-1;
109 ctx->daemon_to_unmount_mq = (mqd_t)-1;
110 INIT_LIST_HEAD(&ctx->staging_list);
113 static inline struct wimfs_context *wimfs_get_context()
115 return (struct wimfs_context*)fuse_get_context()->private_data;
118 static inline WIMStruct *wimfs_get_WIMStruct()
120 return wimfs_get_context()->wim;
123 static inline bool wimfs_ctx_readonly(const struct wimfs_context *ctx)
125 return (ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) == 0;
128 static inline int get_lookup_flags(const struct wimfs_context *ctx)
130 return ctx->default_lookup_flags;
133 /* Returns nonzero if write permission is requested on the file open flags */
134 static inline int flags_writable(int open_flags)
136 return open_flags & (O_RDWR | O_WRONLY);
140 * Allocate a file descriptor for a stream.
142 * @inode: inode containing the stream we're opening
143 * @stream_id: ID of the stream we're opening
144 * @lte: Lookup table entry for the stream (may be NULL)
145 * @fd_ret: Return the allocated file descriptor if successful.
146 * @readonly: True if this is a read-only mount.
148 * Return 0 iff successful or error code if unsuccessful.
150 static int alloc_wimlib_fd(struct inode *inode,
152 struct lookup_table_entry *lte,
153 struct wimlib_fd **fd_ret,
156 static const u16 fds_per_alloc = 8;
157 static const u16 max_fds = 0xffff;
160 pthread_mutex_lock(&inode->i_mutex);
162 DEBUG("Allocating fd for stream ID %u from inode %lx (open = %u, allocated = %u)",
163 stream_id, inode->ino, inode->num_opened_fds,
164 inode->num_allocated_fds);
166 if (inode->num_opened_fds == inode->num_allocated_fds) {
167 struct wimlib_fd **fds;
170 if (inode->num_allocated_fds == max_fds) {
174 num_new_fds = min(fds_per_alloc,
175 max_fds - inode->num_allocated_fds);
177 fds = REALLOC(inode->fds,
178 (inode->num_allocated_fds + num_new_fds) *
179 sizeof(inode->fds[0]));
184 memset(&fds[inode->num_allocated_fds], 0,
185 num_new_fds * sizeof(fds[0]));
187 inode->num_allocated_fds += num_new_fds;
189 for (u16 i = 0; ; i++) {
190 if (!inode->fds[i]) {
191 struct wimlib_fd *fd = CALLOC(1, sizeof(*fd));
200 fd->stream_id = stream_id;
203 inode->num_opened_fds++;
204 if (lte && !readonly)
205 lte->num_opened_fds++;
206 DEBUG("Allocated fd (idx = %u)", fd->idx);
212 pthread_mutex_unlock(&inode->i_mutex);
216 static void inode_put_fd(struct inode *inode, struct wimlib_fd *fd)
218 wimlib_assert(inode != NULL);
220 pthread_mutex_lock(&inode->i_mutex);
222 wimlib_assert(fd->f_inode == inode);
223 wimlib_assert(inode->num_opened_fds != 0);
224 wimlib_assert(fd->idx < inode->num_allocated_fds);
225 wimlib_assert(inode->fds[fd->idx] == fd);
227 inode->fds[fd->idx] = NULL;
229 if (--inode->num_opened_fds == 0 && inode->link_count == 0) {
230 pthread_mutex_unlock(&inode->i_mutex);
233 pthread_mutex_unlock(&inode->i_mutex);
237 static int lte_put_fd(struct lookup_table_entry *lte, struct wimlib_fd *fd)
239 wimlib_assert(fd->f_lte == lte);
241 if (!lte) /* Empty stream with no lookup table entry */
244 /* Close staging file descriptor if needed. */
246 if (lte->resource_location == RESOURCE_IN_STAGING_FILE
247 && fd->staging_fd != -1)
249 if (close(fd->staging_fd) != 0) {
250 ERROR_WITH_ERRNO("Failed to close staging file");
254 lte_decrement_num_opened_fds(lte);
258 /* Close a file descriptor. */
259 static int close_wimlib_fd(struct wimlib_fd *fd)
262 DEBUG("Closing fd (inode = %lu, opened = %u, allocated = %u)",
263 fd->f_inode->ino, fd->f_inode->num_opened_fds,
264 fd->f_inode->num_allocated_fds);
265 ret = lte_put_fd(fd->f_lte, fd);
269 inode_put_fd(fd->f_inode, fd);
273 static int create_dentry(struct wimfs_context *ctx, const char *path,
274 struct dentry **dentry_ret)
276 struct dentry *parent;
278 const char *basename;
280 parent = get_parent_dentry(ctx->wim, path);
284 if (!dentry_is_directory(parent))
287 basename = path_basename(path);
288 if (get_dentry_child_with_name(parent, basename))
291 new = new_dentry_with_inode(basename);
295 new->d_inode->resolved = 1;
296 new->d_inode->ino = ctx->next_ino++;
297 dentry_add_child(parent, new);
298 hlist_add_head(&new->d_inode->hlist, ctx->image_inode_list);
303 /* Remove a dentry; i.e. remove a reference to the corresponding inode.
305 * If there are no remaining references to the inode either through dentries or
306 * open file descriptors, the inode is freed. Otherwise, the inode is not
307 * removed, but the dentry is unlinked and freed.
309 * Either way, all lookup table entries referenced by the inode have their
310 * reference count decremented. If a lookup table entry has no open file
311 * descriptors and no references remaining, it is freed, and the staging file is
314 static void remove_dentry(struct dentry *dentry,
315 struct lookup_table *lookup_table)
317 struct inode *inode = dentry->d_inode;
318 struct lookup_table_entry *lte;
321 for (i = 0; i <= inode->num_ads; i++) {
322 lte = inode_stream_lte_resolved(inode, i);
324 lte_decrement_refcnt(lte, lookup_table);
326 unlink_dentry(dentry);
330 /* Transfers file attributes from a struct inode to a `stat' buffer.
332 * The lookup table entry tells us which stream in the inode we are statting.
333 * For a named data stream, everything returned is the same as the unnamed data
334 * stream except possibly the size and block count. */
335 static int inode_to_stbuf(const struct inode *inode,
336 struct lookup_table_entry *lte, struct stat *stbuf)
338 if (inode_is_symlink(inode))
339 stbuf->st_mode = S_IFLNK | 0777;
340 else if (inode_is_directory(inode))
341 stbuf->st_mode = S_IFDIR | 0755;
343 stbuf->st_mode = S_IFREG | 0755;
345 stbuf->st_ino = (ino_t)inode->ino;
346 stbuf->st_nlink = inode->link_count;
347 stbuf->st_uid = getuid();
348 stbuf->st_gid = getgid();
351 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
352 struct stat native_stat;
353 if (stat(lte->staging_file_name, &native_stat) != 0) {
354 DEBUG("Failed to stat `%s': %m",
355 lte->staging_file_name);
358 stbuf->st_size = native_stat.st_size;
360 stbuf->st_size = wim_resource_size(lte);
366 stbuf->st_atime = wim_timestamp_to_unix(inode->last_access_time);
367 stbuf->st_mtime = wim_timestamp_to_unix(inode->last_write_time);
368 stbuf->st_ctime = wim_timestamp_to_unix(inode->creation_time);
369 stbuf->st_blocks = (stbuf->st_size + 511) / 512;
373 /* Creates a new staging file and returns its file descriptor opened for
376 * @name_ret: A location into which the a pointer to the newly allocated name of
377 * the staging file is stored.
378 * @return: The file descriptor for the new file. Returns -1 and sets errno on
379 * error, for any reason possible from the creat() function.
381 static int create_staging_file(char **name_ret, int open_flags,
382 struct wimfs_context *ctx)
390 name_len = ctx->staging_dir_name_len + 1 + SHA1_HASH_SIZE;
391 name = MALLOC(name_len + 1);
399 memcpy(name, ctx->staging_dir_name, ctx->staging_dir_name_len);
400 name[ctx->staging_dir_name_len] = '/';
401 randomize_char_array_with_alnum(name + ctx->staging_dir_name_len + 1,
403 name[name_len] = '\0';
406 /* Just in case, verify that the randomly generated name doesn't name an
407 * existing file, and try again if so */
408 } while (stat(name, &stbuf) == 0);
414 /* doesn't exist--- ok */
416 DEBUG("Creating staging file `%s'", name);
418 fd = open(name, open_flags | O_CREAT | O_TRUNC, 0600);
430 * Extract a WIM resource to the staging directory.
432 * @inode: Inode that contains the stream we are extracting
434 * @stream_id: Identifier for the stream (it stays constant even if the indices
435 * of the stream entries are changed)
437 * @lte: Pointer to pointer to the lookup table entry for the stream we need to
438 * extract, or NULL if there was no lookup table entry present for the stream
440 * @size: Number of bytes of the stream we want to extract (this supports the
441 * wimfs_truncate() function). It may be more than the actual stream length, in
442 * which case the extra space is filled with zeroes.
444 * @ctx: Context for the WIM filesystem.
446 * Returns 0 on success or a negative error code on failure.
448 static int extract_resource_to_staging_dir(struct inode *inode,
450 struct lookup_table_entry **lte,
452 struct wimfs_context *ctx)
454 char *staging_file_name;
457 struct lookup_table_entry *old_lte, *new_lte;
460 DEBUG("Extracting resource to staging dir: inode %"PRIu64", "
461 "stream id %"PRIu32, inode->ino, stream_id);
465 wimlib_assert(old_lte == NULL ||
466 old_lte->resource_location != RESOURCE_IN_STAGING_FILE);
468 /* Create the staging file */
469 fd = create_staging_file(&staging_file_name, O_WRONLY, ctx);
473 /* Extract the stream to the staging file (possibly truncated) */
475 extract_size = min(wim_resource_size(old_lte), size);
476 ret = extract_wim_resource_to_fd(old_lte, fd, extract_size);
482 /* In the case of truncate() to more than the file length, extend the
483 * file with zeroes by calling ftruncate() on the underlying staging
485 if (ret == 0 && size > extract_size)
486 ret = ftruncate(fd, size);
488 /* Close the staging file descriptor and check for errors. If there's
489 * an error, unlink the staging file. */
490 if (ret != 0 || close(fd) != 0) {
496 goto out_delete_staging_file;
499 /* Now deal with the lookup table entries. We may be able to re-use the
500 * existing entry, but we may have to create a new one instead. */
502 if (old_lte && inode->link_count == old_lte->refcnt) {
503 /* The reference count of the existing lookup table entry is the
504 * same as the link count of the inode that contains the stream
505 * we're opening. Therefore, ALL the references to the lookup
506 * table entry correspond to the stream we're trying to extract,
507 * so the lookup table entry can be re-used. */
508 DEBUG("Re-using lookup table entry");
509 lookup_table_unlink(ctx->wim->lookup_table, old_lte);
513 /* There's an existing lookup table entry, but its
514 * reference count is greater than the link count for
515 * the inode containing a stream we're opening.
516 * Therefore, we need to split the lookup table entry.
518 wimlib_assert(old_lte->refcnt > inode->link_count);
519 DEBUG("Splitting lookup table entry "
520 "(inode->link_count = %u, old_lte->refcnt = %u)",
521 inode->link_count, old_lte->refcnt);
524 new_lte = new_lookup_table_entry();
527 goto out_delete_staging_file;
530 /* There may already be open file descriptors to this stream if
531 * it's previously been opened read-only, but just now we're
532 * opening it read-write. Identify those file descriptors and
533 * change their lookup table entry pointers to point to the new
534 * lookup table entry, and open staging file descriptors for
537 * At the same time, we need to count the number of these opened
538 * file descriptors to the new lookup table entry. If there's
539 * an old lookup table entry, this number needs to be subtracted
540 * from the fd's opened to the old entry. */
541 for (u16 i = 0, j = 0; j < inode->num_opened_fds; i++) {
542 struct wimlib_fd *fd = inode->fds[i];
544 if (fd->stream_id == stream_id) {
545 wimlib_assert(fd->f_lte == old_lte);
546 wimlib_assert(fd->staging_fd == -1);
548 new_lte->num_opened_fds++;
549 fd->staging_fd = open(staging_file_name, O_RDONLY);
550 if (fd->staging_fd == -1) {
552 goto out_revert_fd_changes;
558 DEBUG("%hu fd's were already opened to the file we extracted",
559 new_lte->num_opened_fds);
561 old_lte->num_opened_fds -= new_lte->num_opened_fds;
562 old_lte->refcnt -= inode->link_count;
566 new_lte->refcnt = inode->link_count;
567 new_lte->resource_location = RESOURCE_IN_STAGING_FILE;
568 new_lte->staging_file_name = staging_file_name;
569 new_lte->lte_inode = inode;
570 random_hash(new_lte->hash);
573 inode->lte = new_lte;
575 for (u16 i = 0; i < inode->num_ads; i++)
576 if (inode->ads_entries[i].stream_id == stream_id)
577 inode->ads_entries[i].lte = new_lte;
579 lookup_table_insert(ctx->wim->lookup_table, new_lte);
580 list_add(&new_lte->staging_list, &ctx->staging_list);
583 out_revert_fd_changes:
584 for (u16 i = 0, j = 0; j < new_lte->num_opened_fds; i++) {
585 struct wimlib_fd *fd = inode->fds[i];
586 if (fd && fd->stream_id == stream_id && fd->f_lte == new_lte) {
588 if (fd->staging_fd != -1) {
589 close(fd->staging_fd);
595 free_lookup_table_entry(new_lte);
596 out_delete_staging_file:
597 unlink(staging_file_name);
598 FREE(staging_file_name);
603 * Creates a randomly named staging directory and saves its name in the
604 * filesystem context structure.
606 static int make_staging_dir(struct wimfs_context *ctx, const char *user_prefix)
608 static const size_t random_suffix_len = 10;
609 static const char *common_suffix = ".staging";
610 static const size_t common_suffix_len = 8;
612 char *staging_dir_name = NULL;
613 size_t staging_dir_name_len;
615 const char *wim_basename;
616 char *real_user_prefix = NULL;
620 real_user_prefix = realpath(user_prefix, NULL);
621 if (!real_user_prefix) {
622 ERROR_WITH_ERRNO("Could not resolve `%s'",
624 ret = WIMLIB_ERR_NOTDIR;
627 wim_basename = path_basename(ctx->wim->filename);
628 prefix_len = strlen(real_user_prefix) + 1 + strlen(wim_basename);
630 prefix_len = strlen(ctx->wim->filename);
633 staging_dir_name_len = prefix_len + common_suffix_len + random_suffix_len;
635 staging_dir_name = MALLOC(staging_dir_name_len + 1);
636 if (!staging_dir_name) {
637 ret = WIMLIB_ERR_NOMEM;
641 if (real_user_prefix)
642 sprintf(staging_dir_name, "%s/%s", real_user_prefix, wim_basename);
644 strcpy(staging_dir_name, ctx->wim->filename);
646 strcat(staging_dir_name, common_suffix);
648 randomize_char_array_with_alnum(staging_dir_name + prefix_len + common_suffix_len,
651 staging_dir_name[staging_dir_name_len] = '\0';
653 if (mkdir(staging_dir_name, 0700) != 0) {
654 ERROR_WITH_ERRNO("Failed to create temporary directory `%s'",
656 ret = WIMLIB_ERR_MKDIR;
661 FREE(real_user_prefix);
663 ctx->staging_dir_name = staging_dir_name;
664 ctx->staging_dir_name_len = staging_dir_name_len;
666 FREE(staging_dir_name);
671 static int remove_file_or_directory(const char *fpath, const struct stat *sb,
672 int typeflag, struct FTW *ftwbuf)
674 if (remove(fpath) == 0)
677 ERROR_WITH_ERRNO("Cannot remove `%s'", fpath);
678 return WIMLIB_ERR_DELETE_STAGING_DIR;
683 * Deletes the staging directory and all the files contained in it.
685 static int delete_staging_dir(struct wimfs_context *ctx)
688 ret = nftw(ctx->staging_dir_name, remove_file_or_directory,
690 FREE(ctx->staging_dir_name);
691 ctx->staging_dir_name = NULL;
695 static void inode_update_lte_ptr(struct inode *inode,
696 struct lookup_table_entry *old_lte,
697 struct lookup_table_entry *new_lte)
699 if (inode->lte == old_lte) {
700 inode->lte = new_lte;
702 for (unsigned i = 0; i < inode->num_ads; i++) {
703 if (inode->ads_entries[i].lte == old_lte) {
704 inode->ads_entries[i].lte = new_lte;
711 static int update_lte_of_staging_file(struct lookup_table_entry *lte,
712 struct lookup_table *table)
714 struct lookup_table_entry *duplicate_lte;
716 u8 hash[SHA1_HASH_SIZE];
719 ret = sha1sum(lte->staging_file_name, hash);
722 lookup_table_unlink(table, lte);
723 duplicate_lte = __lookup_resource(table, hash);
725 /* Merge duplicate lookup table entries */
726 duplicate_lte->refcnt += lte->refcnt;
727 inode_update_lte_ptr(lte->lte_inode, lte, duplicate_lte);
728 free_lookup_table_entry(lte);
730 if (stat(lte->staging_file_name, &stbuf) != 0) {
731 ERROR_WITH_ERRNO("Failed to stat `%s'", lte->staging_file_name);
732 return WIMLIB_ERR_STAT;
734 if (stbuf.st_size == 0) {
735 /* Zero-length stream. No lookup table entry needed. */
736 inode_update_lte_ptr(lte->lte_inode, lte, NULL);
737 free_lookup_table_entry(lte);
739 wimlib_assert(<e->file_on_disk == <e->staging_file_name);
740 lte->resource_entry.original_size = stbuf.st_size;
741 lte->resource_entry.size = stbuf.st_size;
742 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
743 lte->file_on_disk_fp = NULL;
744 copy_hash(lte->hash, hash);
745 lookup_table_insert(table, lte);
751 static int inode_close_fds(struct inode *inode)
753 u16 num_opened_fds = inode->num_opened_fds;
754 for (u16 i = 0, j = 0; j < num_opened_fds; i++) {
755 struct wimlib_fd *fd = inode->fds[i];
757 wimlib_assert(fd->f_inode == inode);
758 int ret = close_wimlib_fd(fd);
767 /* Overwrites the WIM file, with changes saved. */
768 static int rebuild_wim(struct wimfs_context *ctx, int write_flags)
771 struct lookup_table_entry *lte, *tmp;
772 WIMStruct *w = ctx->wim;
774 DEBUG("Closing all staging file descriptors.");
775 list_for_each_entry_safe(lte, tmp, &ctx->staging_list, staging_list) {
776 ret = inode_close_fds(lte->lte_inode);
781 DEBUG("Calculating SHA1 checksums for all new staging files.");
782 list_for_each_entry(lte, &ctx->staging_list, staging_list) {
783 ret = update_lte_of_staging_file(lte, w->lookup_table);
788 xml_update_image_info(w, w->current_image);
789 ret = wimlib_overwrite(w, write_flags, 0, NULL);
791 ERROR("Failed to commit changes to mounted WIM image");
797 /* Simple function that returns the concatenation of 2 strings. */
798 static char *strcat_dup(const char *s1, const char *s2, size_t max_len)
800 size_t len = strlen(s1) + strlen(s2);
803 char *p = MALLOC(len + 1);
806 snprintf(p, len + 1, "%s%s", s1, s2);
810 static int set_message_queue_names(struct wimfs_context *ctx,
811 const char *mount_dir)
813 static const char *u2d_prefix = "/wimlib-unmount-to-daemon-mq";
814 static const char *d2u_prefix = "/wimlib-daemon-to-unmount-mq";
819 dir_path = realpath(mount_dir, NULL);
821 ERROR_WITH_ERRNO("Failed to resolve path \"%s\"", mount_dir);
823 return WIMLIB_ERR_NOMEM;
825 return WIMLIB_ERR_NOTDIR;
835 ctx->unmount_to_daemon_mq_name = strcat_dup(u2d_prefix, dir_path,
837 if (!ctx->unmount_to_daemon_mq_name) {
838 ret = WIMLIB_ERR_NOMEM;
839 goto out_free_dir_path;
841 ctx->daemon_to_unmount_mq_name = strcat_dup(d2u_prefix, dir_path,
843 if (!ctx->daemon_to_unmount_mq_name) {
844 ret = WIMLIB_ERR_NOMEM;
845 goto out_free_unmount_to_daemon_mq_name;
849 goto out_free_dir_path;
850 out_free_unmount_to_daemon_mq_name:
851 FREE(ctx->unmount_to_daemon_mq_name);
852 ctx->unmount_to_daemon_mq_name = NULL;
858 static void free_message_queue_names(struct wimfs_context *ctx)
860 FREE(ctx->unmount_to_daemon_mq_name);
861 FREE(ctx->daemon_to_unmount_mq_name);
862 ctx->unmount_to_daemon_mq_name = NULL;
863 ctx->daemon_to_unmount_mq_name = NULL;
867 * Opens two POSIX message queue: one for sending messages from the unmount
868 * process to the daemon process, and one to go the other way. The names of the
869 * message queues, which must be system-wide unique, are be based on the mount
872 * @daemon specifies whether the calling process is the filesystem daemon or the
875 static int open_message_queues(struct wimfs_context *ctx, bool daemon)
877 int unmount_to_daemon_mq_flags = O_WRONLY | O_CREAT;
878 int daemon_to_unmount_mq_flags = O_RDONLY | O_CREAT;
881 swap(unmount_to_daemon_mq_flags, daemon_to_unmount_mq_flags);
883 DEBUG("Opening message queue \"%s\"", ctx->unmount_to_daemon_mq_name);
884 ctx->unmount_to_daemon_mq = mq_open(ctx->unmount_to_daemon_mq_name,
885 unmount_to_daemon_mq_flags, 0700, NULL);
887 if (ctx->unmount_to_daemon_mq == (mqd_t)-1) {
888 ERROR_WITH_ERRNO("mq_open()");
889 return WIMLIB_ERR_MQUEUE;
892 DEBUG("Opening message queue \"%s\"", ctx->daemon_to_unmount_mq_name);
893 ctx->daemon_to_unmount_mq = mq_open(ctx->daemon_to_unmount_mq_name,
894 daemon_to_unmount_mq_flags, 0700, NULL);
896 if (ctx->daemon_to_unmount_mq == (mqd_t)-1) {
897 ERROR_WITH_ERRNO("mq_open()");
898 mq_close(ctx->unmount_to_daemon_mq);
899 mq_unlink(ctx->unmount_to_daemon_mq_name);
900 ctx->unmount_to_daemon_mq = (mqd_t)-1;
901 return WIMLIB_ERR_MQUEUE;
906 /* Try to determine the maximum message size of a message queue. The return
907 * value is the maximum message size, or a guess of 8192 bytes if it cannot be
909 static long mq_get_msgsize(mqd_t mq)
911 static const char *msgsize_max_file = "/proc/sys/fs/mqueue/msgsize_max";
916 if (mq_getattr(mq, &attr) == 0) {
917 msgsize = attr.mq_msgsize;
919 ERROR_WITH_ERRNO("mq_getattr()");
920 ERROR("Attempting to read %s", msgsize_max_file);
921 fp = fopen(msgsize_max_file, "rb");
923 if (fscanf(fp, "%ld", &msgsize) != 1) {
924 ERROR("Assuming message size of 8192");
929 ERROR_WITH_ERRNO("Failed to open the file `%s'",
931 ERROR("Assuming message size of 8192");
938 static int get_mailbox(mqd_t mq, long needed_msgsize, long *msgsize_ret,
944 msgsize = mq_get_msgsize(mq);
946 if (msgsize < needed_msgsize) {
947 ERROR("Message queue max size must be at least %ld!",
949 return WIMLIB_ERR_MQUEUE;
952 mailbox = MALLOC(msgsize);
954 ERROR("Failed to allocate %ld bytes for mailbox", msgsize);
955 return WIMLIB_ERR_NOMEM;
957 *msgsize_ret = msgsize;
958 *mailbox_ret = mailbox;
962 static void unlink_message_queues(struct wimfs_context *ctx)
964 mq_unlink(ctx->unmount_to_daemon_mq_name);
965 mq_unlink(ctx->daemon_to_unmount_mq_name);
968 /* Closes the message queues, which are allocated in static variables */
969 static void close_message_queues(struct wimfs_context *ctx)
971 DEBUG("Closing message queues");
972 mq_close(ctx->unmount_to_daemon_mq);
973 ctx->unmount_to_daemon_mq = (mqd_t)(-1);
974 mq_close(ctx->daemon_to_unmount_mq);
975 ctx->daemon_to_unmount_mq = (mqd_t)(-1);
976 unlink_message_queues(ctx);
980 struct unmount_msg_hdr {
987 struct msg_unmount_request {
988 struct unmount_msg_hdr hdr;
992 struct msg_daemon_info {
993 struct unmount_msg_hdr hdr;
998 struct msg_unmount_finished {
999 struct unmount_msg_hdr hdr;
1004 MSG_TYPE_UNMOUNT_REQUEST,
1005 MSG_TYPE_DAEMON_INFO,
1006 MSG_TYPE_UNMOUNT_FINISHED,
1010 struct msg_handler_context {
1012 int timeout_seconds;
1015 /*bool unmount_complete;*/
1021 /*int unmount_flags;*/
1022 struct wimfs_context *wimfs_ctx;
1027 static int send_unmount_request_msg(mqd_t mq, int unmount_flags)
1029 DEBUG("Sending unmount request msg");
1030 struct msg_unmount_request msg = {
1032 .min_version = WIMLIB_MAKEVERSION(1, 2, 0),
1033 .cur_version = WIMLIB_VERSION_CODE,
1034 .msg_type = MSG_TYPE_UNMOUNT_REQUEST,
1035 .msg_size = sizeof(msg),
1037 .unmount_flags = unmount_flags,
1040 if (mq_send(mq, (void*)&msg, sizeof(msg), 1)) {
1041 ERROR_WITH_ERRNO("Failed to communicate with filesystem daemon");
1042 return WIMLIB_ERR_MQUEUE;
1047 static int send_daemon_info_msg(mqd_t mq, pid_t pid, int mount_flags)
1049 DEBUG("Sending daemon info msg (pid = %d, mount_flags=%x)",
1052 struct msg_daemon_info msg = {
1054 .min_version = WIMLIB_MAKEVERSION(1, 2, 0),
1055 .cur_version = WIMLIB_VERSION_CODE,
1056 .msg_type = MSG_TYPE_DAEMON_INFO,
1057 .msg_size = sizeof(msg),
1060 .mount_flags = mount_flags,
1062 if (mq_send(mq, (void*)&msg, sizeof(msg), 1)) {
1063 ERROR_WITH_ERRNO("Failed to send daemon info to unmount process");
1064 return WIMLIB_ERR_MQUEUE;
1069 static void send_unmount_finished_msg(mqd_t mq, int status)
1071 DEBUG("Sending unmount finished msg");
1072 struct msg_unmount_finished msg = {
1074 .min_version = WIMLIB_MAKEVERSION(1, 2, 0),
1075 .cur_version = WIMLIB_VERSION_CODE,
1076 .msg_type = MSG_TYPE_UNMOUNT_FINISHED,
1077 .msg_size = sizeof(msg),
1081 if (mq_send(mq, (void*)&msg, sizeof(msg), 1))
1082 ERROR_WITH_ERRNO("Failed to send status to unmount process");
1085 static int msg_unmount_request_handler(const void *_msg,
1086 struct msg_handler_context *handler_ctx)
1088 const struct msg_unmount_request *msg;
1089 struct wimfs_context *wimfs_ctx;
1094 DEBUG("Handling unmount request msg");
1097 wimfs_ctx = handler_ctx->daemon.wimfs_ctx;
1099 if (msg->hdr.msg_size < sizeof(*msg)) {
1100 status = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1104 unmount_flags = msg->unmount_flags;
1106 ret = send_daemon_info_msg(wimfs_ctx->daemon_to_unmount_mq, getpid(),
1107 wimfs_ctx->mount_flags);
1113 if (wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1114 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_COMMIT) {
1115 int write_flags = 0;
1116 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY)
1117 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1118 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_REBUILD)
1119 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1120 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_RECOMPRESS)
1121 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
1122 status = rebuild_wim(wimfs_ctx, write_flags);
1125 DEBUG("Read-only mount");
1130 if (wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1131 ret = delete_staging_dir(wimfs_ctx);
1133 ERROR("Failed to delete the staging directory");
1138 send_unmount_finished_msg(wimfs_ctx->daemon_to_unmount_mq, status);
1139 return MSG_BREAK_LOOP;
1142 static int msg_daemon_info_handler(const void *_msg,
1143 struct msg_handler_context *handler_ctx)
1145 const struct msg_daemon_info *msg = _msg;
1146 DEBUG("Handling daemon info msg");
1147 if (msg->hdr.msg_size < sizeof(*msg))
1148 return WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1149 handler_ctx->unmount.daemon_pid = msg->daemon_pid;
1150 handler_ctx->unmount.mount_flags = msg->mount_flags;
1151 handler_ctx->timeout_seconds = 1;
1152 DEBUG("pid of daemon is %d; mount flags were %#x",
1153 handler_ctx->unmount.daemon_pid,
1154 handler_ctx->unmount.mount_flags);
1158 static int msg_unmount_finished_handler(const void *_msg,
1159 struct msg_handler_context *handler_ctx)
1161 const struct msg_unmount_finished *msg = _msg;
1162 DEBUG("Handling unmount finished message");
1163 if (msg->hdr.msg_size < sizeof(*msg))
1164 return WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1165 handler_ctx->unmount.status = msg->status;
1166 DEBUG("status is %d", handler_ctx->unmount.status);
1167 return MSG_BREAK_LOOP;
1170 static int unmount_timed_out_cb(struct msg_handler_context *handler_ctx)
1172 if (handler_ctx->unmount.daemon_pid == 0) {
1176 kill(handler_ctx->unmount.daemon_pid, 0);
1177 if (errno == ESRCH) {
1179 } else if (errno != 0) {
1180 ERROR_WITH_ERRNO("Error determining state of "
1181 "filesystem daemon");
1182 return WIMLIB_ERR_MQUEUE;
1184 DEBUG("Filesystem daemon is still alive... "
1185 "Waiting another %d seconds",
1186 handler_ctx->timeout_seconds);
1191 ERROR("The filesystem daemon has crashed! Changes to the "
1192 "WIM may not have been commited.");
1193 return WIMLIB_ERR_FILESYSTEM_DAEMON_CRASHED;
1196 static int daemon_timed_out_cb(struct msg_handler_context *handler_ctx)
1199 return WIMLIB_ERR_TIMEOUT;
1202 typedef int (*msg_handler_t)(const void *_msg,
1203 struct msg_handler_context *handler_ctx);
1205 struct msg_handler_callbacks {
1206 int (*timed_out)(struct msg_handler_context *);
1207 msg_handler_t msg_handlers[MSG_TYPE_MAX];
1210 static const struct msg_handler_callbacks unmount_msg_handler_callbacks = {
1211 .timed_out = unmount_timed_out_cb,
1213 [MSG_TYPE_DAEMON_INFO] = msg_daemon_info_handler,
1214 [MSG_TYPE_UNMOUNT_FINISHED] = msg_unmount_finished_handler,
1218 static const struct msg_handler_callbacks daemon_msg_handler_callbacks = {
1219 .timed_out = daemon_timed_out_cb,
1221 [MSG_TYPE_UNMOUNT_REQUEST] = msg_unmount_request_handler,
1225 static int receive_message(mqd_t mq, struct msg_handler_context *handler_ctx,
1226 const msg_handler_t msg_handlers[],
1227 long mailbox_size, void *mailbox)
1230 struct timespec timeout;
1231 ssize_t bytes_received;
1232 struct unmount_msg_hdr *hdr;
1235 gettimeofday(&now, NULL);
1236 timeout.tv_sec = now.tv_sec + handler_ctx->timeout_seconds;
1237 timeout.tv_nsec = now.tv_usec * 1000;
1239 bytes_received = mq_timedreceive(mq, mailbox,
1240 mailbox_size, NULL, &timeout);
1242 if (bytes_received == -1) {
1243 ERROR_WITH_ERRNO("mq_timedreceive()");
1244 if (errno == ETIMEDOUT)
1245 ret = WIMLIB_ERR_TIMEOUT;
1247 ret = WIMLIB_ERR_MQUEUE;
1248 } else if (bytes_received < sizeof(*hdr) ||
1249 bytes_received != hdr->msg_size) {
1250 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1251 } else if (WIMLIB_VERSION_CODE < hdr->min_version) {
1252 /*ERROR("Cannot understand the received message. "*/
1253 /*"Please upgrade wimlib to at least v%d.%d.%d",*/
1254 /*WIMLIB_GET_MAJOR_VERSION(hdr->min_version),*/
1255 /*WIMLIB_GET_MINOR_VERSION(hdr->min_version),*/
1256 /*WIMLIB_GET_PATCH_VERSION(hdr->min_version));*/
1257 ret = MSG_VERSION_TOO_HIGH;
1258 } else if (hdr->msg_type >= MSG_TYPE_MAX) {
1259 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1260 } else if (msg_handlers[hdr->msg_type] == NULL) {
1261 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1263 ret = msg_handlers[hdr->msg_type](mailbox, handler_ctx);
1268 static int message_loop(mqd_t mq,
1269 const struct msg_handler_callbacks *callbacks,
1270 struct msg_handler_context *handler_ctx)
1272 static const size_t MAX_MSG_SIZE = 512;
1277 DEBUG("Entering message loop");
1279 ret = get_mailbox(mq, MAX_MSG_SIZE, &msgsize, &mailbox);
1283 ret = receive_message(mq, handler_ctx,
1284 callbacks->msg_handlers,
1286 if (ret == 0 || ret == MSG_VERSION_TOO_HIGH) {
1288 } else if (ret == MSG_BREAK_LOOP) {
1291 } else if (ret == WIMLIB_ERR_TIMEOUT) {
1292 if (callbacks->timed_out)
1293 ret = callbacks->timed_out(handler_ctx);
1299 ERROR_WITH_ERRNO("Error communicating with "
1300 "filesystem daemon");
1305 DEBUG("Exiting message loop");
1309 /* Execute `fusermount -u', which is installed setuid root, to unmount the WIM.
1311 * FUSE does not yet implement synchronous unmounts. This means that fusermount
1312 * -u will return before the filesystem daemon returns from wimfs_destroy().
1313 * This is partly what we want, because we need to send a message from this
1314 * process to the filesystem daemon telling whether --commit was specified or
1315 * not. However, after that, the unmount process must wait for the filesystem
1316 * daemon to finish writing the WIM file.
1318 static int execute_fusermount(const char *dir)
1326 ERROR_WITH_ERRNO("Failed to fork()");
1327 return WIMLIB_ERR_FORK;
1331 execlp("fusermount", "fusermount", "-u", dir, NULL);
1332 ERROR_WITH_ERRNO("Failed to execute `fusermount'");
1333 exit(WIMLIB_ERR_FUSERMOUNT);
1337 ret = wait(&status);
1339 ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
1341 return WIMLIB_ERR_FUSERMOUNT;
1345 if (status == WIMLIB_ERR_FUSERMOUNT)
1346 ERROR("Could not find the `fusermount' program");
1348 ERROR("fusermount exited with status %d", status);
1350 /* Try again, but with the `umount' program. This is required
1351 * on other FUSE implementations such as FreeBSD's that do not
1352 * have a `fusermount' program. */
1356 ERROR_WITH_ERRNO("Failed to fork()");
1357 return WIMLIB_ERR_FORK;
1361 execlp("umount", "umount", dir, NULL);
1362 ERROR_WITH_ERRNO("Failed to execute `umount'");
1363 exit(WIMLIB_ERR_FUSERMOUNT);
1367 ret = wait(&status);
1369 ERROR_WITH_ERRNO("Failed to wait for `umount' process to "
1371 return WIMLIB_ERR_FUSERMOUNT;
1374 ERROR("`umount' exited with failure status");
1375 return WIMLIB_ERR_FUSERMOUNT;
1381 static int wimfs_access(const char *path, int mask)
1383 /* Permissions not implemented */
1387 static int wimfs_chmod(const char *path, mode_t mask)
1389 struct dentry *dentry;
1390 struct wimfs_context *ctx = wimfs_get_context();
1391 struct inode *inode;
1395 ret = lookup_resource(ctx->wim, path,
1396 get_lookup_flags(ctx) | LOOKUP_FLAG_DIRECTORY_OK,
1397 &dentry, NULL, NULL);
1400 inode = dentry->d_inode;
1401 inode_to_stbuf(inode, NULL, &stbuf);
1402 if (mask == stbuf.st_mode)
1408 /* Called when the filesystem is unmounted. */
1409 static void wimfs_destroy(void *p)
1411 struct wimfs_context *wimfs_ctx;
1413 wimfs_ctx = wimfs_get_context();
1415 if (open_message_queues(wimfs_ctx, true))
1418 struct msg_handler_context handler_ctx = {
1420 .timeout_seconds = 5,
1422 .wimfs_ctx = wimfs_ctx,
1426 message_loop(wimfs_ctx->unmount_to_daemon_mq,
1427 &daemon_msg_handler_callbacks,
1430 close_message_queues(wimfs_ctx);
1434 static int wimfs_fallocate(const char *path, int mode,
1435 off_t offset, off_t len, struct fuse_file_info *fi)
1437 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1438 wimlib_assert(fd->staging_fd != -1);
1439 return fallocate(fd->staging_fd, mode, offset, len);
1444 static int wimfs_fgetattr(const char *path, struct stat *stbuf,
1445 struct fuse_file_info *fi)
1447 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1448 return inode_to_stbuf(fd->f_inode, fd->f_lte, stbuf);
1451 static int wimfs_ftruncate(const char *path, off_t size,
1452 struct fuse_file_info *fi)
1454 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1455 int ret = ftruncate(fd->staging_fd, size);
1458 if (fd->f_lte && size < fd->f_lte->resource_entry.original_size)
1459 fd->f_lte->resource_entry.original_size = size;
1464 * Fills in a `struct stat' that corresponds to a file or directory in the WIM.
1466 static int wimfs_getattr(const char *path, struct stat *stbuf)
1468 struct dentry *dentry;
1469 struct lookup_table_entry *lte;
1471 struct wimfs_context *ctx = wimfs_get_context();
1473 ret = lookup_resource(ctx->wim, path,
1474 get_lookup_flags(ctx) | LOOKUP_FLAG_DIRECTORY_OK,
1475 &dentry, <e, NULL);
1478 return inode_to_stbuf(dentry->d_inode, lte, stbuf);
1482 /* Read an alternate data stream through the XATTR interface, or get its size */
1483 static int wimfs_getxattr(const char *path, const char *name, char *value,
1487 struct inode *inode;
1488 struct ads_entry *ads_entry;
1490 struct lookup_table_entry *lte;
1491 struct wimfs_context *ctx = wimfs_get_context();
1493 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1496 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1500 inode = wim_pathname_to_inode(ctx->wim, path);
1504 ads_entry = inode_get_ads_entry(inode, name, NULL);
1508 lte = ads_entry->lte;
1509 res_size = wim_resource_size(lte);
1514 if (res_size > size)
1517 ret = read_full_wim_resource(lte, (u8*)value,
1518 WIMLIB_RESOURCE_FLAG_MULTITHREADED);
1526 /* Create a hard link */
1527 static int wimfs_link(const char *to, const char *from)
1529 struct dentry *from_dentry, *from_dentry_parent;
1530 const char *link_name;
1531 struct inode *inode;
1532 struct lookup_table_entry *lte;
1533 WIMStruct *w = wimfs_get_WIMStruct();
1536 inode = wim_pathname_to_inode(w, to);
1540 if (inode->attributes & FILE_ATTRIBUTE_REPARSE_POINT)
1543 if (inode->attributes & FILE_ATTRIBUTE_DIRECTORY)
1546 from_dentry_parent = get_parent_dentry(w, from);
1547 if (!from_dentry_parent)
1549 if (!dentry_is_directory(from_dentry_parent))
1552 link_name = path_basename(from);
1553 if (get_dentry_child_with_name(from_dentry_parent, link_name))
1555 from_dentry = new_dentry(link_name);
1559 inode_add_dentry(from_dentry, inode);
1560 from_dentry->d_inode = inode;
1561 inode->link_count++;
1563 for (i = 0; i <= inode->num_ads; i++) {
1564 lte = inode_stream_lte_resolved(inode, i);
1568 dentry_add_child(from_dentry_parent, from_dentry);
1573 static int wimfs_listxattr(const char *path, char *list, size_t size)
1576 struct inode *inode;
1577 struct wimfs_context *ctx = wimfs_get_context();
1581 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1584 /* List alternate data streams, or get the list size */
1586 inode = wim_pathname_to_inode(ctx->wim, path);
1592 for (i = 0; i < inode->num_ads; i++)
1593 needed_size += inode->ads_entries[i].stream_name_utf8_len + 6;
1597 for (i = 0; i < inode->num_ads; i++) {
1598 needed_size = inode->ads_entries[i].stream_name_utf8_len + 6;
1599 if (needed_size > size)
1601 p += sprintf(p, "user.%s",
1602 inode->ads_entries[i].stream_name_utf8) + 1;
1603 size -= needed_size;
1611 /* Create a directory in the WIM.
1612 * @mode is currently ignored. */
1613 static int wimfs_mkdir(const char *path, mode_t mode)
1615 struct dentry *dentry;
1618 ret = create_dentry(wimfs_get_context(), path, &dentry);
1620 dentry->d_inode->attributes = FILE_ATTRIBUTE_DIRECTORY;
1624 /* Create a regular file in the WIM.
1625 * @mode is currently ignored. */
1626 static int wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
1628 const char *stream_name;
1629 struct wimfs_context *ctx = wimfs_get_context();
1630 if ((ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
1631 && (stream_name = path_stream_name(path))) {
1632 /* Make an alternate data stream */
1633 struct ads_entry *new_entry;
1634 struct inode *inode;
1636 char *p = (char*)stream_name - 1;
1637 wimlib_assert(*p == ':');
1640 inode = wim_pathname_to_inode(ctx->wim, path);
1643 if (inode->attributes &
1644 (FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_DIRECTORY))
1646 if (inode_get_ads_entry(inode, stream_name, NULL))
1648 new_entry = inode_add_ads(inode, stream_name);
1653 struct dentry *dentry;
1656 /* Make a normal file (not an alternate data stream) */
1657 ret = create_dentry(ctx, path, &dentry);
1659 dentry->d_inode->attributes = FILE_ATTRIBUTE_NORMAL;
1666 static int wimfs_open(const char *path, struct fuse_file_info *fi)
1668 struct dentry *dentry;
1669 struct lookup_table_entry *lte;
1671 struct wimlib_fd *fd;
1672 struct inode *inode;
1675 struct wimfs_context *ctx = wimfs_get_context();
1677 ret = lookup_resource(ctx->wim, path, get_lookup_flags(ctx),
1678 &dentry, <e, &stream_idx);
1682 inode = dentry->d_inode;
1684 if (stream_idx == 0)
1687 stream_id = inode->ads_entries[stream_idx - 1].stream_id;
1689 /* The file resource may be in the staging directory (read-write mounts
1690 * only) or in the WIM. If it's in the staging directory, we need to
1691 * open a native file descriptor for the corresponding file. Otherwise,
1692 * we can read the file resource directly from the WIM file if we are
1693 * opening it read-only, but we need to extract the resource to the
1694 * staging directory if we are opening it writable. */
1696 if (flags_writable(fi->flags) &&
1697 (!lte || lte->resource_location != RESOURCE_IN_STAGING_FILE)) {
1698 u64 size = (lte) ? wim_resource_size(lte) : 0;
1699 ret = extract_resource_to_staging_dir(inode, stream_id,
1705 ret = alloc_wimlib_fd(inode, stream_id, lte, &fd,
1706 wimfs_ctx_readonly(ctx));
1710 if (lte && lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1711 fd->staging_fd = open(lte->staging_file_name, fi->flags);
1712 if (fd->staging_fd == -1) {
1713 int errno_save = errno;
1714 close_wimlib_fd(fd);
1718 fi->fh = (uintptr_t)fd;
1722 /* Opens a directory. */
1723 static int wimfs_opendir(const char *path, struct fuse_file_info *fi)
1725 struct inode *inode;
1727 struct wimlib_fd *fd = NULL;
1728 struct wimfs_context *ctx = wimfs_get_context();
1729 WIMStruct *w = ctx->wim;
1731 inode = wim_pathname_to_inode(w, path);
1734 if (!inode_is_directory(inode))
1736 ret = alloc_wimlib_fd(inode, 0, NULL, &fd, wimfs_ctx_readonly(ctx));
1737 fi->fh = (uintptr_t)fd;
1743 * Read data from a file in the WIM or in the staging directory.
1745 static int wimfs_read(const char *path, char *buf, size_t size,
1746 off_t offset, struct fuse_file_info *fi)
1748 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1754 if (!fd->f_lte) /* Empty stream with no lookup table entry */
1757 if (fd->f_lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1758 /* Read from staging file */
1760 wimlib_assert(fd->f_lte->staging_file_name);
1761 wimlib_assert(fd->staging_fd != -1);
1763 DEBUG("Seek to offset %zu", offset);
1765 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1767 ret = read(fd->staging_fd, buf, size);
1773 u64 res_size = wim_resource_size(fd->f_lte);
1774 if (offset > res_size)
1776 size = min(size, res_size - offset);
1777 if (read_wim_resource(fd->f_lte, (u8*)buf,
1779 WIMLIB_RESOURCE_FLAG_MULTITHREADED) != 0)
1785 struct fill_params {
1787 fuse_fill_dir_t filler;
1790 static int dentry_fuse_fill(struct dentry *dentry, void *arg)
1792 struct fill_params *fill_params = arg;
1793 return fill_params->filler(fill_params->buf, dentry->file_name_utf8,
1797 /* Fills in the entries of the directory specified by @path using the
1798 * FUSE-provided function @filler. */
1799 static int wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
1800 off_t offset, struct fuse_file_info *fi)
1802 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1803 struct inode *inode;
1808 inode = fd->f_inode;
1810 struct fill_params fill_params = {
1815 filler(buf, ".", NULL, 0);
1816 filler(buf, "..", NULL, 0);
1818 return for_dentry_in_rbtree(inode->children.rb_node,
1819 dentry_fuse_fill, &fill_params);
1823 static int wimfs_readlink(const char *path, char *buf, size_t buf_len)
1825 struct wimfs_context *ctx = wimfs_get_context();
1826 struct inode *inode = wim_pathname_to_inode(ctx->wim, path);
1830 if (!inode_is_symlink(inode))
1833 ret = inode_readlink(inode, buf, buf_len, ctx->wim,
1834 WIMLIB_RESOURCE_FLAG_MULTITHREADED);
1841 static int wimfs_release(const char *path, struct fuse_file_info *fi)
1843 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1844 return close_wimlib_fd(fd);
1847 /* Close a directory */
1848 static int wimfs_releasedir(const char *path, struct fuse_file_info *fi)
1850 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1851 return close_wimlib_fd(fd);
1855 /* Remove an alternate data stream through the XATTR interface */
1856 static int wimfs_removexattr(const char *path, const char *name)
1858 struct inode *inode;
1859 struct ads_entry *ads_entry;
1861 struct wimfs_context *ctx = wimfs_get_context();
1863 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1866 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1870 inode = wim_pathname_to_inode(ctx->wim, path);
1874 ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
1877 inode_remove_ads(inode, ads_idx, ctx->wim->lookup_table);
1882 /* Renames a file or directory. See rename (3) */
1883 static int wimfs_rename(const char *from, const char *to)
1887 struct dentry *parent_of_dst;
1888 char *file_name_utf16 = NULL, *file_name_utf8 = NULL;
1889 u16 file_name_utf16_len, file_name_utf8_len;
1890 WIMStruct *w = wimfs_get_WIMStruct();
1893 /* This rename() implementation currently only supports actual files
1894 * (not alternate data streams) */
1896 src = get_dentry(w, from);
1900 dst = get_dentry(w, to);
1902 ret = get_names(&file_name_utf16, &file_name_utf8,
1903 &file_name_utf16_len, &file_name_utf8_len,
1909 /* Destination file exists */
1911 if (src == dst) /* Same file */
1914 if (!dentry_is_directory(src)) {
1915 /* Cannot rename non-directory to directory. */
1916 if (dentry_is_directory(dst))
1919 /* Cannot rename directory to a non-directory or a non-empty
1921 if (!dentry_is_directory(dst))
1923 if (inode_has_children(dst->d_inode))
1926 parent_of_dst = dst->parent;
1927 remove_dentry(dst, w->lookup_table);
1929 /* Destination does not exist */
1930 parent_of_dst = get_parent_dentry(w, to);
1934 if (!dentry_is_directory(parent_of_dst))
1938 FREE(src->file_name);
1939 FREE(src->file_name_utf8);
1940 src->file_name = file_name_utf16;
1941 src->file_name_utf8 = file_name_utf8;
1942 src->file_name_len = file_name_utf16_len;
1943 src->file_name_utf8_len = file_name_utf8_len;
1946 dentry_add_child(parent_of_dst, src);
1950 /* Remove a directory */
1951 static int wimfs_rmdir(const char *path)
1953 struct dentry *dentry;
1954 WIMStruct *w = wimfs_get_WIMStruct();
1956 dentry = get_dentry(w, path);
1960 if (!dentry_is_empty_directory(dentry))
1963 remove_dentry(dentry, w->lookup_table);
1968 /* Write an alternate data stream through the XATTR interface */
1969 static int wimfs_setxattr(const char *path, const char *name,
1970 const char *value, size_t size, int flags)
1972 struct ads_entry *existing_ads_entry;
1973 struct ads_entry *new_ads_entry;
1974 struct lookup_table_entry *existing_lte;
1975 struct lookup_table_entry *lte;
1976 struct inode *inode;
1977 u8 value_hash[SHA1_HASH_SIZE];
1979 struct wimfs_context *ctx = wimfs_get_context();
1981 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1984 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1988 inode = wim_pathname_to_inode(ctx->wim, path);
1992 existing_ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
1993 if (existing_ads_entry) {
1994 if (flags & XATTR_CREATE)
1996 inode_remove_ads(inode, ads_idx, ctx->wim->lookup_table);
1998 if (flags & XATTR_REPLACE)
2001 new_ads_entry = inode_add_ads(inode, name);
2005 sha1_buffer((const u8*)value, size, value_hash);
2007 existing_lte = __lookup_resource(ctx->wim->lookup_table, value_hash);
2014 lte = new_lookup_table_entry();
2017 value_copy = MALLOC(size);
2022 memcpy(value_copy, value, size);
2023 lte->resource_location = RESOURCE_IN_ATTACHED_BUFFER;
2024 lte->attached_buffer = value_copy;
2025 lte->resource_entry.original_size = size;
2026 lte->resource_entry.size = size;
2027 lte->resource_entry.flags = 0;
2028 copy_hash(lte->hash, value_hash);
2029 lookup_table_insert(ctx->wim->lookup_table, lte);
2031 new_ads_entry->lte = lte;
2036 static int wimfs_symlink(const char *to, const char *from)
2038 struct wimfs_context *ctx = wimfs_get_context();
2039 struct dentry *dentry;
2042 ret = create_dentry(ctx, from, &dentry);
2044 dentry->d_inode->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
2045 dentry->d_inode->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
2046 if (inode_set_symlink(dentry->d_inode, to,
2047 ctx->wim->lookup_table, NULL))
2049 unlink_dentry(dentry);
2050 free_dentry(dentry);
2058 /* Reduce the size of a file */
2059 static int wimfs_truncate(const char *path, off_t size)
2061 struct dentry *dentry;
2062 struct lookup_table_entry *lte;
2066 struct inode *inode;
2067 struct wimfs_context *ctx = wimfs_get_context();
2069 ret = lookup_resource(ctx->wim, path, get_lookup_flags(ctx),
2070 &dentry, <e, &stream_idx);
2075 if (lte == NULL && size == 0)
2078 inode = dentry->d_inode;
2079 if (stream_idx == 0)
2082 stream_id = inode->ads_entries[stream_idx - 1].stream_id;
2084 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
2085 ret = truncate(lte->staging_file_name, size);
2089 /* File in WIM. Extract it to the staging directory, but only
2090 * the first @size bytes of it. */
2091 ret = extract_resource_to_staging_dir(inode, stream_id,
2097 /* Unlink a non-directory or alternate data stream */
2098 static int wimfs_unlink(const char *path)
2100 struct dentry *dentry;
2101 struct lookup_table_entry *lte;
2104 struct wimfs_context *ctx = wimfs_get_context();
2106 ret = lookup_resource(ctx->wim, path, get_lookup_flags(ctx),
2107 &dentry, <e, &stream_idx);
2112 if (stream_idx == 0)
2113 remove_dentry(dentry, ctx->wim->lookup_table);
2115 inode_remove_ads(dentry->d_inode, stream_idx - 1,
2116 ctx->wim->lookup_table);
2120 #ifdef HAVE_UTIMENSAT
2122 * Change the timestamp on a file dentry.
2124 * Note that alternate data streams do not have their own timestamps.
2126 static int wimfs_utimens(const char *path, const struct timespec tv[2])
2128 struct dentry *dentry;
2129 struct inode *inode;
2130 WIMStruct *w = wimfs_get_WIMStruct();
2132 dentry = get_dentry(w, path);
2135 inode = dentry->d_inode;
2137 if (tv[0].tv_nsec != UTIME_OMIT) {
2138 if (tv[0].tv_nsec == UTIME_NOW)
2139 inode->last_access_time = get_wim_timestamp();
2141 inode->last_access_time = timespec_to_wim_timestamp(&tv[0]);
2143 if (tv[1].tv_nsec != UTIME_OMIT) {
2144 if (tv[1].tv_nsec == UTIME_NOW)
2145 inode->last_write_time = get_wim_timestamp();
2147 inode->last_write_time = timespec_to_wim_timestamp(&tv[1]);
2152 static int wimfs_utime(const char *path, struct utimbuf *times)
2154 struct dentry *dentry;
2155 struct inode *inode;
2156 WIMStruct *w = wimfs_get_WIMStruct();
2158 dentry = get_dentry(w, path);
2161 inode = dentry->d_inode;
2163 inode->last_write_time = unix_timestamp_to_wim(times->modtime);
2164 inode->last_access_time = unix_timestamp_to_wim(times->actime);
2169 /* Writes to a file in the WIM filesystem.
2170 * It may be an alternate data stream, but here we don't even notice because we
2171 * just get a lookup table entry. */
2172 static int wimfs_write(const char *path, const char *buf, size_t size,
2173 off_t offset, struct fuse_file_info *fi)
2175 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
2182 wimlib_assert(fd->f_lte);
2183 wimlib_assert(fd->f_lte->staging_file_name);
2184 wimlib_assert(fd->staging_fd != -1);
2185 wimlib_assert(fd->f_inode);
2187 /* Seek to the requested position */
2188 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
2191 /* Write the data. */
2192 ret = write(fd->staging_fd, buf, size);
2196 now = get_wim_timestamp();
2197 fd->f_inode->last_write_time = now;
2198 fd->f_inode->last_access_time = now;
2202 static struct fuse_operations wimfs_operations = {
2203 .access = wimfs_access,
2204 .chmod = wimfs_chmod,
2205 .destroy = wimfs_destroy,
2207 .fallocate = wimfs_fallocate,
2209 .fgetattr = wimfs_fgetattr,
2210 .ftruncate = wimfs_ftruncate,
2211 .getattr = wimfs_getattr,
2213 .getxattr = wimfs_getxattr,
2217 .listxattr = wimfs_listxattr,
2219 .mkdir = wimfs_mkdir,
2220 .mknod = wimfs_mknod,
2222 .opendir = wimfs_opendir,
2224 .readdir = wimfs_readdir,
2225 .readlink = wimfs_readlink,
2226 .release = wimfs_release,
2227 .releasedir = wimfs_releasedir,
2229 .removexattr = wimfs_removexattr,
2231 .rename = wimfs_rename,
2232 .rmdir = wimfs_rmdir,
2234 .setxattr = wimfs_setxattr,
2236 .symlink = wimfs_symlink,
2237 .truncate = wimfs_truncate,
2238 .unlink = wimfs_unlink,
2239 #ifdef HAVE_UTIMENSAT
2240 .utimens = wimfs_utimens,
2242 .utime = wimfs_utime,
2244 .write = wimfs_write,
2246 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 8)
2247 .flag_nullpath_ok = 1,
2249 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 9)
2251 .flag_utime_omit_ok = 1,
2256 /* Mounts an image from a WIM file. */
2257 WIMLIBAPI int wimlib_mount_image(WIMStruct *wim, int image, const char *dir,
2258 int mount_flags, WIMStruct **additional_swms,
2259 unsigned num_additional_swms,
2260 const char *staging_dir)
2266 struct lookup_table *joined_tab, *wim_tab_save;
2267 struct image_metadata *imd;
2268 struct wimfs_context ctx;
2269 struct hlist_node *cur_node;
2270 struct inode *inode;
2272 DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
2273 wim, image, dir, mount_flags);
2276 return WIMLIB_ERR_INVALID_PARAM;
2278 ret = verify_swm_set(wim, additional_swms, num_additional_swms);
2282 if ((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) && (wim->hdr.total_parts != 1)) {
2283 ERROR("Cannot mount a split WIM read-write");
2284 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
2287 if (num_additional_swms) {
2288 ret = new_joined_lookup_table(wim, additional_swms,
2289 num_additional_swms,
2293 wim_tab_save = wim->lookup_table;
2294 wim->lookup_table = joined_tab;
2297 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2298 ret = wim_run_full_verifications(wim);
2303 ret = select_wim_image(wim, image);
2307 DEBUG("Selected image %d", image);
2309 imd = wim_get_current_image_metadata(wim);
2311 if (imd->root_dentry->refcnt != 1) {
2312 ERROR("Cannot mount image that was just exported with "
2313 "wimlib_export_image()");
2314 ret = WIMLIB_ERR_INVALID_PARAM;
2318 if (imd->modified) {
2319 ERROR("Cannot mount image that was added "
2320 "with wimlib_add_image()");
2321 ret = WIMLIB_ERR_INVALID_PARAM;
2325 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2326 ret = lock_wim(wim, wim->fp);
2331 if (!(mount_flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
2332 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
2333 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
2334 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
2337 DEBUG("Initializing struct wimfs_context");
2338 init_wimfs_context(&ctx);
2340 ctx.mount_flags = mount_flags;
2341 ctx.image_inode_list = &imd->inode_list;
2343 if (mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
2344 ctx.default_lookup_flags = LOOKUP_FLAG_ADS_OK;
2346 DEBUG("Unlinking message queues in case they already exist");
2347 ret = set_message_queue_names(&ctx, dir);
2350 unlink_message_queues(&ctx);
2352 DEBUG("Preparing arguments to fuse_main()");
2354 dir_copy = STRDUP(dir);
2356 goto out_free_message_queue_names;
2358 argv[argc++] = "imagex";
2359 argv[argc++] = dir_copy;
2361 /* disable multi-threaded operation for read-write mounts */
2362 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2363 argv[argc++] = "-s";
2365 if (mount_flags & WIMLIB_MOUNT_FLAG_DEBUG)
2366 argv[argc++] = "-d";
2369 * We provide the use_ino option because we are going to assign inode
2370 * numbers oursides. The inodes will be given unique numbers in the
2371 * assign_inode_numbers() function, and the static variable @next_ino is
2372 * set to the next available inode number.
2374 char optstring[256] =
2378 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 8)
2382 argv[argc++] = "-o";
2383 argv[argc++] = optstring;
2384 if ((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
2385 /* Read-write mount. Make the staging directory */
2386 ret = make_staging_dir(&ctx, staging_dir);
2388 goto out_free_dir_copy;
2390 /* Read-only mount */
2391 strcat(optstring, ",ro");
2398 DEBUG("FUSE command line (argc = %d): ", argc);
2399 for (i = 0; i < argc; i++) {
2400 fputs(argv[i], stdout);
2408 /* Mark dentry tree as modified if read-write mount. */
2409 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2411 imd->has_been_mounted_rw = 1;
2414 /* Resolve the lookup table entries for every inode in the image, and
2415 * assign inode numbers */
2416 DEBUG("Resolving lookup table entries and assigning inode numbers");
2419 hlist_for_each_entry(inode, cur_node, &imd->inode_list, hlist) {
2420 inode_resolve_ltes(inode, wim->lookup_table);
2421 inode->ino = ctx.next_ino++;
2423 /*ctx.next_ino = assign_inode_numbers(&imd->inode_list);*/
2424 DEBUG("(next_ino = %"PRIu64")", ctx.next_ino);
2426 DEBUG("Calling fuse_main()");
2428 ret = fuse_main(argc, argv, &wimfs_operations, &ctx);
2430 DEBUG("Returned from fuse_main() (ret = %d)", ret);
2432 ret = WIMLIB_ERR_FUSE;
2436 wim->wim_locked = 0;
2437 out_free_message_queue_names:
2438 free_message_queue_names(&ctx);
2440 if (num_additional_swms) {
2441 free_lookup_table(wim->lookup_table);
2442 wim->lookup_table = wim_tab_save;
2448 * Unmounts the WIM file that was previously mounted on @dir by using
2449 * wimlib_mount_image().
2451 WIMLIBAPI int wimlib_unmount_image(const char *dir, int unmount_flags,
2452 wimlib_progress_func_t progress_func)
2455 struct wimfs_context wimfs_ctx;
2457 init_wimfs_context(&wimfs_ctx);
2459 ret = set_message_queue_names(&wimfs_ctx, dir);
2463 ret = open_message_queues(&wimfs_ctx, false);
2465 goto out_free_message_queue_names;
2467 ret = send_unmount_request_msg(wimfs_ctx.unmount_to_daemon_mq,
2470 goto out_close_message_queues;
2472 ret = execute_fusermount(dir);
2474 goto out_close_message_queues;
2476 struct msg_handler_context handler_ctx = {
2478 .timeout_seconds = 5,
2484 ret = message_loop(wimfs_ctx.daemon_to_unmount_mq,
2485 &unmount_msg_handler_callbacks,
2488 ret = handler_ctx.unmount.status;
2489 out_close_message_queues:
2490 close_message_queues(&wimfs_ctx);
2491 out_free_message_queue_names:
2492 free_message_queue_names(&wimfs_ctx);
2497 #else /* WITH_FUSE */
2500 static inline int mount_unsupported_error()
2502 ERROR("wimlib was compiled with --without-fuse, which disables support "
2503 "for mounting WIMs.");
2504 return WIMLIB_ERR_UNSUPPORTED;
2507 WIMLIBAPI int wimlib_unmount_image(const char *dir, int unmount_flags,
2508 wimlib_progress_func_t progress_func)
2510 return mount_unsupported_error();
2513 WIMLIBAPI int wimlib_mount_image(WIMStruct *wim_p, int image, const char *dir,
2514 int mount_flags, WIMStruct **additional_swms,
2515 unsigned num_additional_swms,
2516 const char *staging_dir)
2518 return mount_unsupported_error();
2521 #endif /* WITH_FUSE */