4 * This file implements mounting of WIM files using FUSE, which stands for
5 * Filesystem in Userspace. FUSE allows a filesystem to be implemented in a
6 * userspace process by implementing the filesystem primitives--- read(),
7 * write(), readdir(), etc.
11 * Copyright (C) 2012 Eric Biggers
13 * This file is part of wimlib, a library for working with WIM files.
15 * wimlib is free software; you can redistribute it and/or modify it under the
16 * terms of the GNU General Public License as published by the Free
17 * Software Foundation; either version 3 of the License, or (at your option)
20 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
21 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
22 * A PARTICULAR PURPOSE. See the GNU General Public License for more
25 * You should have received a copy of the GNU General Public License
26 * along with wimlib; if not, see http://www.gnu.org/licenses/.
29 #include "wimlib_internal.h"
34 #include "lookup_table.h"
36 #include "buffer_io.h"
37 #include "timestamp.h"
42 #define FUSE_USE_VERSION 26
53 #include <attr/xattr.h>
56 #define MSG_VERSION_TOO_HIGH -1
57 #define MSG_BREAK_LOOP -2
59 /* File descriptor to a file open on the WIM filesystem. */
61 struct inode *f_inode;
62 struct lookup_table_entry *f_lte;
68 struct wimfs_context {
69 /* The WIMStruct for the mounted WIM. */
72 /* Name of the staging directory for a read-write mount. Whenever a new file is
73 * created, it is done so in the staging directory. Furthermore, whenever a
74 * file in the WIM is modified, it is extracted to the staging directory. If
75 * changes are commited when the WIM is unmounted, the file resources are merged
76 * in from the staging directory when writing the new WIM. */
77 char *staging_dir_name;
78 size_t staging_dir_name_len;
80 /* Flags passed to wimlib_mount(). */
83 int default_lookup_flags;
85 /* Next inode number to be assigned. */
88 /* List of lookup table entries in the staging directory */
89 struct list_head staging_list;
91 /* List of inodes in the mounted image */
92 struct hlist_head *image_inode_list;
94 /* Name and message queue descriptors for message queues between the filesystem
95 * daemon process and the unmount process. These are used when the filesystem
96 * is unmounted and the process running wimlib_mount() (i.e. the `imagex
97 * unmount' command) needs to communicate with the filesystem daemon running
98 * fuse_main() (i.e. that spawned by the `imagex mount' or `imagex mountrw'
100 char *unmount_to_daemon_mq_name;
101 char *daemon_to_unmount_mq_name;
102 mqd_t unmount_to_daemon_mq;
103 mqd_t daemon_to_unmount_mq;
106 static void init_wimfs_context(struct wimfs_context *ctx)
108 memset(ctx, 0, sizeof(*ctx));
109 ctx->unmount_to_daemon_mq = (mqd_t)-1;
110 ctx->daemon_to_unmount_mq = (mqd_t)-1;
111 INIT_LIST_HEAD(&ctx->staging_list);
114 static inline struct wimfs_context *wimfs_get_context()
116 return (struct wimfs_context*)fuse_get_context()->private_data;
119 static inline WIMStruct *wimfs_get_WIMStruct()
121 return wimfs_get_context()->wim;
124 static inline bool wimfs_ctx_readonly(const struct wimfs_context *ctx)
126 return (ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) == 0;
129 static inline int get_lookup_flags(const struct wimfs_context *ctx)
131 return ctx->default_lookup_flags;
134 /* Returns nonzero if write permission is requested on the file open flags */
135 static inline int flags_writable(int open_flags)
137 return open_flags & (O_RDWR | O_WRONLY);
141 * Allocate a file descriptor for a stream.
143 * @inode: inode containing the stream we're opening
144 * @stream_id: ID of the stream we're opening
145 * @lte: Lookup table entry for the stream (may be NULL)
146 * @fd_ret: Return the allocated file descriptor if successful.
147 * @readonly: True if this is a read-only mount.
149 * Return 0 iff successful or error code if unsuccessful.
151 static int alloc_wimlib_fd(struct inode *inode,
153 struct lookup_table_entry *lte,
154 struct wimlib_fd **fd_ret,
157 static const u16 fds_per_alloc = 8;
158 static const u16 max_fds = 0xffff;
161 pthread_mutex_lock(&inode->i_mutex);
163 DEBUG("Allocating fd for stream ID %u from inode %lx (open = %u, allocated = %u)",
164 stream_id, inode->ino, inode->num_opened_fds,
165 inode->num_allocated_fds);
167 if (inode->num_opened_fds == inode->num_allocated_fds) {
168 struct wimlib_fd **fds;
171 if (inode->num_allocated_fds == max_fds) {
175 num_new_fds = min(fds_per_alloc,
176 max_fds - inode->num_allocated_fds);
178 fds = REALLOC(inode->fds,
179 (inode->num_allocated_fds + num_new_fds) *
180 sizeof(inode->fds[0]));
185 memset(&fds[inode->num_allocated_fds], 0,
186 num_new_fds * sizeof(fds[0]));
188 inode->num_allocated_fds += num_new_fds;
190 for (u16 i = 0; ; i++) {
191 if (!inode->fds[i]) {
192 struct wimlib_fd *fd = CALLOC(1, sizeof(*fd));
201 fd->stream_id = stream_id;
204 inode->num_opened_fds++;
205 if (lte && !readonly)
206 lte->num_opened_fds++;
207 DEBUG("Allocated fd (idx = %u)", fd->idx);
213 pthread_mutex_unlock(&inode->i_mutex);
217 static void inode_put_fd(struct inode *inode, struct wimlib_fd *fd)
219 wimlib_assert(inode != NULL);
221 pthread_mutex_lock(&inode->i_mutex);
223 wimlib_assert(fd->f_inode == inode);
224 wimlib_assert(inode->num_opened_fds != 0);
225 wimlib_assert(fd->idx < inode->num_allocated_fds);
226 wimlib_assert(inode->fds[fd->idx] == fd);
228 inode->fds[fd->idx] = NULL;
230 if (--inode->num_opened_fds == 0 && inode->link_count == 0) {
231 pthread_mutex_unlock(&inode->i_mutex);
234 pthread_mutex_unlock(&inode->i_mutex);
238 static int lte_put_fd(struct lookup_table_entry *lte, struct wimlib_fd *fd)
240 wimlib_assert(fd->f_lte == lte);
242 if (!lte) /* Empty stream with no lookup table entry */
245 /* Close staging file descriptor if needed. */
247 if (lte->resource_location == RESOURCE_IN_STAGING_FILE
248 && fd->staging_fd != -1)
250 if (close(fd->staging_fd) != 0) {
251 ERROR_WITH_ERRNO("Failed to close staging file");
255 lte_decrement_num_opened_fds(lte);
259 /* Close a file descriptor. */
260 static int close_wimlib_fd(struct wimlib_fd *fd)
263 DEBUG("Closing fd (inode = %lu, opened = %u, allocated = %u)",
264 fd->f_inode->ino, fd->f_inode->num_opened_fds,
265 fd->f_inode->num_allocated_fds);
266 ret = lte_put_fd(fd->f_lte, fd);
270 inode_put_fd(fd->f_inode, fd);
274 static int create_dentry(struct wimfs_context *ctx, const char *path,
275 struct dentry **dentry_ret)
277 struct dentry *parent;
279 const char *basename;
281 parent = get_parent_dentry(ctx->wim, path);
285 if (!dentry_is_directory(parent))
288 basename = path_basename(path);
289 if (get_dentry_child_with_name(parent, basename))
292 new = new_dentry_with_inode(basename);
296 new->d_inode->resolved = 1;
297 new->d_inode->ino = ctx->next_ino++;
298 dentry_add_child(parent, new);
299 hlist_add_head(&new->d_inode->hlist, ctx->image_inode_list);
304 /* Remove a dentry; i.e. remove a reference to the corresponding inode.
306 * If there are no remaining references to the inode either through dentries or
307 * open file descriptors, the inode is freed. Otherwise, the inode is not
308 * removed, but the dentry is unlinked and freed.
310 * Either way, all lookup table entries referenced by the inode have their
311 * reference count decremented. If a lookup table entry has no open file
312 * descriptors and no references remaining, it is freed, and the staging file is
315 static void remove_dentry(struct dentry *dentry,
316 struct lookup_table *lookup_table)
318 struct inode *inode = dentry->d_inode;
319 struct lookup_table_entry *lte;
322 for (i = 0; i <= inode->num_ads; i++) {
323 lte = inode_stream_lte_resolved(inode, i);
325 lte_decrement_refcnt(lte, lookup_table);
327 unlink_dentry(dentry);
331 /* Transfers file attributes from a struct inode to a `stat' buffer.
333 * The lookup table entry tells us which stream in the inode we are statting.
334 * For a named data stream, everything returned is the same as the unnamed data
335 * stream except possibly the size and block count. */
336 static int inode_to_stbuf(const struct inode *inode,
337 struct lookup_table_entry *lte, struct stat *stbuf)
339 if (inode_is_symlink(inode))
340 stbuf->st_mode = S_IFLNK | 0777;
341 else if (inode_is_directory(inode))
342 stbuf->st_mode = S_IFDIR | 0755;
344 stbuf->st_mode = S_IFREG | 0755;
346 stbuf->st_ino = (ino_t)inode->ino;
347 stbuf->st_nlink = inode->link_count;
348 stbuf->st_uid = getuid();
349 stbuf->st_gid = getgid();
352 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
353 struct stat native_stat;
354 if (stat(lte->staging_file_name, &native_stat) != 0) {
355 DEBUG("Failed to stat `%s': %m",
356 lte->staging_file_name);
359 stbuf->st_size = native_stat.st_size;
361 stbuf->st_size = wim_resource_size(lte);
367 stbuf->st_atime = wim_timestamp_to_unix(inode->last_access_time);
368 stbuf->st_mtime = wim_timestamp_to_unix(inode->last_write_time);
369 stbuf->st_ctime = wim_timestamp_to_unix(inode->creation_time);
370 stbuf->st_blocks = (stbuf->st_size + 511) / 512;
374 /* Creates a new staging file and returns its file descriptor opened for
377 * @name_ret: A location into which the a pointer to the newly allocated name of
378 * the staging file is stored.
379 * @return: The file descriptor for the new file. Returns -1 and sets errno on
380 * error, for any reason possible from the creat() function.
382 static int create_staging_file(char **name_ret, int open_flags,
383 struct wimfs_context *ctx)
391 name_len = ctx->staging_dir_name_len + 1 + SHA1_HASH_SIZE;
392 name = MALLOC(name_len + 1);
400 memcpy(name, ctx->staging_dir_name, ctx->staging_dir_name_len);
401 name[ctx->staging_dir_name_len] = '/';
402 randomize_char_array_with_alnum(name + ctx->staging_dir_name_len + 1,
404 name[name_len] = '\0';
407 /* Just in case, verify that the randomly generated name doesn't name an
408 * existing file, and try again if so */
409 } while (stat(name, &stbuf) == 0);
415 /* doesn't exist--- ok */
417 DEBUG("Creating staging file `%s'", name);
419 fd = open(name, open_flags | O_CREAT | O_TRUNC, 0600);
431 * Extract a WIM resource to the staging directory.
433 * @inode: Inode that contains the stream we are extracting
435 * @stream_id: Identifier for the stream (it stays constant even if the indices
436 * of the stream entries are changed)
438 * @lte: Pointer to pointer to the lookup table entry for the stream we need to
439 * extract, or NULL if there was no lookup table entry present for the stream
441 * @size: Number of bytes of the stream we want to extract (this supports the
442 * wimfs_truncate() function). It may be more than the actual stream length, in
443 * which case the extra space is filled with zeroes.
445 * @ctx: Context for the WIM filesystem.
447 * Returns 0 on success or a negative error code on failure.
449 static int extract_resource_to_staging_dir(struct inode *inode,
451 struct lookup_table_entry **lte,
453 struct wimfs_context *ctx)
455 char *staging_file_name;
458 struct lookup_table_entry *old_lte, *new_lte;
461 DEBUG("Extracting resource to staging dir: inode %"PRIu64", "
462 "stream id %"PRIu32, inode->ino, stream_id);
466 wimlib_assert(old_lte == NULL ||
467 old_lte->resource_location != RESOURCE_IN_STAGING_FILE);
469 /* Create the staging file */
470 fd = create_staging_file(&staging_file_name, O_WRONLY, ctx);
474 /* Extract the stream to the staging file (possibly truncated) */
476 extract_size = min(wim_resource_size(old_lte), size);
477 ret = extract_wim_resource_to_fd(old_lte, fd, extract_size);
483 /* In the case of truncate() to more than the file length, extend the
484 * file with zeroes by calling ftruncate() on the underlying staging
486 if (ret == 0 && size > extract_size)
487 ret = ftruncate(fd, size);
489 /* Close the staging file descriptor and check for errors. If there's
490 * an error, unlink the staging file. */
491 if (ret != 0 || close(fd) != 0) {
497 goto out_delete_staging_file;
500 /* Now deal with the lookup table entries. We may be able to re-use the
501 * existing entry, but we may have to create a new one instead. */
503 if (old_lte && inode->link_count == old_lte->refcnt) {
504 /* The reference count of the existing lookup table entry is the
505 * same as the link count of the inode that contains the stream
506 * we're opening. Therefore, ALL the references to the lookup
507 * table entry correspond to the stream we're trying to extract,
508 * so the lookup table entry can be re-used. */
509 DEBUG("Re-using lookup table entry");
510 lookup_table_unlink(ctx->wim->lookup_table, old_lte);
514 /* There's an existing lookup table entry, but its
515 * reference count is greater than the link count for
516 * the inode containing a stream we're opening.
517 * Therefore, we need to split the lookup table entry.
519 wimlib_assert(old_lte->refcnt > inode->link_count);
520 DEBUG("Splitting lookup table entry "
521 "(inode->link_count = %u, old_lte->refcnt = %u)",
522 inode->link_count, old_lte->refcnt);
525 new_lte = new_lookup_table_entry();
528 goto out_delete_staging_file;
531 /* There may already be open file descriptors to this stream if
532 * it's previously been opened read-only, but just now we're
533 * opening it read-write. Identify those file descriptors and
534 * change their lookup table entry pointers to point to the new
535 * lookup table entry, and open staging file descriptors for
538 * At the same time, we need to count the number of these opened
539 * file descriptors to the new lookup table entry. If there's
540 * an old lookup table entry, this number needs to be subtracted
541 * from the fd's opened to the old entry. */
542 for (u16 i = 0, j = 0; j < inode->num_opened_fds; i++) {
543 struct wimlib_fd *fd = inode->fds[i];
545 if (fd->stream_id == stream_id) {
546 wimlib_assert(fd->f_lte == old_lte);
547 wimlib_assert(fd->staging_fd == -1);
549 new_lte->num_opened_fds++;
550 fd->staging_fd = open(staging_file_name, O_RDONLY);
551 if (fd->staging_fd == -1) {
553 goto out_revert_fd_changes;
559 DEBUG("%hu fd's were already opened to the file we extracted",
560 new_lte->num_opened_fds);
562 old_lte->num_opened_fds -= new_lte->num_opened_fds;
563 old_lte->refcnt -= inode->link_count;
567 new_lte->refcnt = inode->link_count;
568 new_lte->resource_location = RESOURCE_IN_STAGING_FILE;
569 new_lte->staging_file_name = staging_file_name;
570 new_lte->lte_inode = inode;
571 random_hash(new_lte->hash);
574 inode->lte = new_lte;
576 for (u16 i = 0; i < inode->num_ads; i++)
577 if (inode->ads_entries[i].stream_id == stream_id)
578 inode->ads_entries[i].lte = new_lte;
580 lookup_table_insert(ctx->wim->lookup_table, new_lte);
581 list_add(&new_lte->staging_list, &ctx->staging_list);
584 out_revert_fd_changes:
585 for (u16 i = 0, j = 0; j < new_lte->num_opened_fds; i++) {
586 struct wimlib_fd *fd = inode->fds[i];
587 if (fd && fd->stream_id == stream_id && fd->f_lte == new_lte) {
589 if (fd->staging_fd != -1) {
590 close(fd->staging_fd);
596 free_lookup_table_entry(new_lte);
597 out_delete_staging_file:
598 unlink(staging_file_name);
599 FREE(staging_file_name);
604 * Creates a randomly named staging directory and saves its name in the
605 * filesystem context structure.
607 static int make_staging_dir(struct wimfs_context *ctx, const char *user_prefix)
609 static const size_t random_suffix_len = 10;
610 static const char *common_suffix = ".staging";
611 static const size_t common_suffix_len = 8;
613 char *staging_dir_name = NULL;
614 size_t staging_dir_name_len;
616 const char *wim_basename;
617 char *real_user_prefix = NULL;
621 real_user_prefix = realpath(user_prefix, NULL);
622 if (!real_user_prefix) {
623 ERROR_WITH_ERRNO("Could not resolve `%s'",
625 ret = WIMLIB_ERR_NOTDIR;
628 wim_basename = path_basename(ctx->wim->filename);
629 prefix_len = strlen(real_user_prefix) + 1 + strlen(wim_basename);
631 prefix_len = strlen(ctx->wim->filename);
634 staging_dir_name_len = prefix_len + common_suffix_len + random_suffix_len;
636 staging_dir_name = MALLOC(staging_dir_name_len + 1);
637 if (!staging_dir_name) {
638 ret = WIMLIB_ERR_NOMEM;
642 if (real_user_prefix)
643 sprintf(staging_dir_name, "%s/%s", real_user_prefix, wim_basename);
645 strcpy(staging_dir_name, ctx->wim->filename);
647 strcat(staging_dir_name, common_suffix);
649 randomize_char_array_with_alnum(staging_dir_name + prefix_len + common_suffix_len,
652 staging_dir_name[staging_dir_name_len] = '\0';
654 if (mkdir(staging_dir_name, 0700) != 0) {
655 ERROR_WITH_ERRNO("Failed to create temporary directory `%s'",
657 ret = WIMLIB_ERR_MKDIR;
662 FREE(real_user_prefix);
664 ctx->staging_dir_name = staging_dir_name;
665 ctx->staging_dir_name_len = staging_dir_name_len;
667 FREE(staging_dir_name);
672 static int remove_file_or_directory(const char *fpath, const struct stat *sb,
673 int typeflag, struct FTW *ftwbuf)
675 if (remove(fpath) == 0)
678 ERROR_WITH_ERRNO("Cannot remove `%s'", fpath);
679 return WIMLIB_ERR_DELETE_STAGING_DIR;
684 * Deletes the staging directory and all the files contained in it.
686 static int delete_staging_dir(struct wimfs_context *ctx)
689 ret = nftw(ctx->staging_dir_name, remove_file_or_directory,
691 FREE(ctx->staging_dir_name);
692 ctx->staging_dir_name = NULL;
696 static void inode_update_lte_ptr(struct inode *inode,
697 struct lookup_table_entry *old_lte,
698 struct lookup_table_entry *new_lte)
700 if (inode->lte == old_lte) {
701 inode->lte = new_lte;
703 for (unsigned i = 0; i < inode->num_ads; i++) {
704 if (inode->ads_entries[i].lte == old_lte) {
705 inode->ads_entries[i].lte = new_lte;
712 static int update_lte_of_staging_file(struct lookup_table_entry *lte,
713 struct lookup_table *table)
715 struct lookup_table_entry *duplicate_lte;
717 u8 hash[SHA1_HASH_SIZE];
720 ret = sha1sum(lte->staging_file_name, hash);
723 lookup_table_unlink(table, lte);
724 duplicate_lte = __lookup_resource(table, hash);
726 /* Merge duplicate lookup table entries */
727 duplicate_lte->refcnt += lte->refcnt;
728 inode_update_lte_ptr(lte->lte_inode, lte, duplicate_lte);
729 free_lookup_table_entry(lte);
731 if (stat(lte->staging_file_name, &stbuf) != 0) {
732 ERROR_WITH_ERRNO("Failed to stat `%s'", lte->staging_file_name);
733 return WIMLIB_ERR_STAT;
735 if (stbuf.st_size == 0) {
736 /* Zero-length stream. No lookup table entry needed. */
737 inode_update_lte_ptr(lte->lte_inode, lte, NULL);
738 free_lookup_table_entry(lte);
740 wimlib_assert(<e->file_on_disk == <e->staging_file_name);
741 lte->resource_entry.original_size = stbuf.st_size;
742 lte->resource_entry.size = stbuf.st_size;
743 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
744 lte->file_on_disk_fp = NULL;
745 copy_hash(lte->hash, hash);
746 lookup_table_insert(table, lte);
752 static int inode_close_fds(struct inode *inode)
754 u16 num_opened_fds = inode->num_opened_fds;
755 for (u16 i = 0, j = 0; j < num_opened_fds; i++) {
756 struct wimlib_fd *fd = inode->fds[i];
758 wimlib_assert(fd->f_inode == inode);
759 int ret = close_wimlib_fd(fd);
768 /* Overwrites the WIM file, with changes saved. */
769 static int rebuild_wim(struct wimfs_context *ctx, int write_flags,
770 wimlib_progress_func_t progress_func)
773 struct lookup_table_entry *lte, *tmp;
774 WIMStruct *w = ctx->wim;
776 DEBUG("Closing all staging file descriptors.");
777 list_for_each_entry_safe(lte, tmp, &ctx->staging_list, staging_list) {
778 ret = inode_close_fds(lte->lte_inode);
783 DEBUG("Calculating SHA1 checksums for all new staging files.");
784 list_for_each_entry(lte, &ctx->staging_list, staging_list) {
785 ret = update_lte_of_staging_file(lte, w->lookup_table);
790 xml_update_image_info(w, w->current_image);
791 ret = wimlib_overwrite(w, write_flags, 0, progress_func);
793 ERROR("Failed to commit changes to mounted WIM image");
799 /* Simple function that returns the concatenation of 2 strings. */
800 static char *strcat_dup(const char *s1, const char *s2, size_t max_len)
802 size_t len = strlen(s1) + strlen(s2);
805 char *p = MALLOC(len + 1);
808 snprintf(p, len + 1, "%s%s", s1, s2);
812 static int set_message_queue_names(struct wimfs_context *ctx,
813 const char *mount_dir)
815 static const char *u2d_prefix = "/wimlib-unmount-to-daemon-mq";
816 static const char *d2u_prefix = "/wimlib-daemon-to-unmount-mq";
821 dir_path = realpath(mount_dir, NULL);
823 ERROR_WITH_ERRNO("Failed to resolve path \"%s\"", mount_dir);
825 return WIMLIB_ERR_NOMEM;
827 return WIMLIB_ERR_NOTDIR;
837 ctx->unmount_to_daemon_mq_name = strcat_dup(u2d_prefix, dir_path,
839 if (!ctx->unmount_to_daemon_mq_name) {
840 ret = WIMLIB_ERR_NOMEM;
841 goto out_free_dir_path;
843 ctx->daemon_to_unmount_mq_name = strcat_dup(d2u_prefix, dir_path,
845 if (!ctx->daemon_to_unmount_mq_name) {
846 ret = WIMLIB_ERR_NOMEM;
847 goto out_free_unmount_to_daemon_mq_name;
851 goto out_free_dir_path;
852 out_free_unmount_to_daemon_mq_name:
853 FREE(ctx->unmount_to_daemon_mq_name);
854 ctx->unmount_to_daemon_mq_name = NULL;
860 static void free_message_queue_names(struct wimfs_context *ctx)
862 FREE(ctx->unmount_to_daemon_mq_name);
863 FREE(ctx->daemon_to_unmount_mq_name);
864 ctx->unmount_to_daemon_mq_name = NULL;
865 ctx->daemon_to_unmount_mq_name = NULL;
869 * Opens two POSIX message queue: one for sending messages from the unmount
870 * process to the daemon process, and one to go the other way. The names of the
871 * message queues, which must be system-wide unique, are be based on the mount
874 * @daemon specifies whether the calling process is the filesystem daemon or the
877 static int open_message_queues(struct wimfs_context *ctx, bool daemon)
879 int unmount_to_daemon_mq_flags = O_WRONLY | O_CREAT;
880 int daemon_to_unmount_mq_flags = O_RDONLY | O_CREAT;
883 swap(unmount_to_daemon_mq_flags, daemon_to_unmount_mq_flags);
885 DEBUG("Opening message queue \"%s\"", ctx->unmount_to_daemon_mq_name);
886 ctx->unmount_to_daemon_mq = mq_open(ctx->unmount_to_daemon_mq_name,
887 unmount_to_daemon_mq_flags, 0700, NULL);
889 if (ctx->unmount_to_daemon_mq == (mqd_t)-1) {
890 ERROR_WITH_ERRNO("mq_open()");
891 return WIMLIB_ERR_MQUEUE;
894 DEBUG("Opening message queue \"%s\"", ctx->daemon_to_unmount_mq_name);
895 ctx->daemon_to_unmount_mq = mq_open(ctx->daemon_to_unmount_mq_name,
896 daemon_to_unmount_mq_flags, 0700, NULL);
898 if (ctx->daemon_to_unmount_mq == (mqd_t)-1) {
899 ERROR_WITH_ERRNO("mq_open()");
900 mq_close(ctx->unmount_to_daemon_mq);
901 mq_unlink(ctx->unmount_to_daemon_mq_name);
902 ctx->unmount_to_daemon_mq = (mqd_t)-1;
903 return WIMLIB_ERR_MQUEUE;
908 /* Try to determine the maximum message size of a message queue. The return
909 * value is the maximum message size, or a guess of 8192 bytes if it cannot be
911 static long mq_get_msgsize(mqd_t mq)
913 static const char *msgsize_max_file = "/proc/sys/fs/mqueue/msgsize_max";
918 if (mq_getattr(mq, &attr) == 0) {
919 msgsize = attr.mq_msgsize;
921 ERROR_WITH_ERRNO("mq_getattr()");
922 ERROR("Attempting to read %s", msgsize_max_file);
923 fp = fopen(msgsize_max_file, "rb");
925 if (fscanf(fp, "%ld", &msgsize) != 1) {
926 ERROR("Assuming message size of 8192");
931 ERROR_WITH_ERRNO("Failed to open the file `%s'",
933 ERROR("Assuming message size of 8192");
940 static int get_mailbox(mqd_t mq, long needed_msgsize, long *msgsize_ret,
946 msgsize = mq_get_msgsize(mq);
948 if (msgsize < needed_msgsize) {
949 ERROR("Message queue max size must be at least %ld!",
951 return WIMLIB_ERR_MQUEUE;
954 mailbox = MALLOC(msgsize);
956 ERROR("Failed to allocate %ld bytes for mailbox", msgsize);
957 return WIMLIB_ERR_NOMEM;
959 *msgsize_ret = msgsize;
960 *mailbox_ret = mailbox;
964 static void unlink_message_queues(struct wimfs_context *ctx)
966 mq_unlink(ctx->unmount_to_daemon_mq_name);
967 mq_unlink(ctx->daemon_to_unmount_mq_name);
970 /* Closes the message queues, which are allocated in static variables */
971 static void close_message_queues(struct wimfs_context *ctx)
973 DEBUG("Closing message queues");
974 mq_close(ctx->unmount_to_daemon_mq);
975 ctx->unmount_to_daemon_mq = (mqd_t)(-1);
976 mq_close(ctx->daemon_to_unmount_mq);
977 ctx->daemon_to_unmount_mq = (mqd_t)(-1);
978 unlink_message_queues(ctx);
982 struct unmount_msg_hdr {
989 struct msg_unmount_request {
990 struct unmount_msg_hdr hdr;
992 u8 want_progress_messages;
995 struct msg_daemon_info {
996 struct unmount_msg_hdr hdr;
1001 struct msg_unmount_finished {
1002 struct unmount_msg_hdr hdr;
1006 struct msg_write_streams_progress {
1007 struct unmount_msg_hdr hdr;
1008 union wimlib_progress_info info;
1012 MSG_TYPE_UNMOUNT_REQUEST,
1013 MSG_TYPE_DAEMON_INFO,
1014 MSG_TYPE_WRITE_STREAMS_PROGRESS,
1015 MSG_TYPE_UNMOUNT_FINISHED,
1019 struct msg_handler_context {
1020 int timeout_seconds;
1026 wimlib_progress_func_t progress_func;
1029 struct wimfs_context *wimfs_ctx;
1034 static int send_unmount_request_msg(mqd_t mq, int unmount_flags,
1035 u8 want_progress_messages)
1037 DEBUG("Sending unmount request msg");
1038 struct msg_unmount_request msg = {
1040 .min_version = WIMLIB_MAKEVERSION(1, 2, 0),
1041 .cur_version = WIMLIB_VERSION_CODE,
1042 .msg_type = MSG_TYPE_UNMOUNT_REQUEST,
1043 .msg_size = sizeof(msg),
1045 .unmount_flags = unmount_flags,
1046 .want_progress_messages = want_progress_messages,
1049 if (mq_send(mq, (void*)&msg, sizeof(msg), 1)) {
1050 ERROR_WITH_ERRNO("Failed to communicate with filesystem daemon");
1051 return WIMLIB_ERR_MQUEUE;
1056 static int send_daemon_info_msg(mqd_t mq, pid_t pid, int mount_flags)
1058 DEBUG("Sending daemon info msg (pid = %d, mount_flags=%x)",
1061 struct msg_daemon_info msg = {
1063 .min_version = WIMLIB_MAKEVERSION(1, 2, 0),
1064 .cur_version = WIMLIB_VERSION_CODE,
1065 .msg_type = MSG_TYPE_DAEMON_INFO,
1066 .msg_size = sizeof(msg),
1069 .mount_flags = mount_flags,
1071 if (mq_send(mq, (void*)&msg, sizeof(msg), 1)) {
1072 ERROR_WITH_ERRNO("Failed to send daemon info to unmount process");
1073 return WIMLIB_ERR_MQUEUE;
1078 static void send_unmount_finished_msg(mqd_t mq, int status)
1080 DEBUG("Sending unmount finished msg");
1081 struct msg_unmount_finished msg = {
1083 .min_version = WIMLIB_MAKEVERSION(1, 2, 0),
1084 .cur_version = WIMLIB_VERSION_CODE,
1085 .msg_type = MSG_TYPE_UNMOUNT_FINISHED,
1086 .msg_size = sizeof(msg),
1090 if (mq_send(mq, (void*)&msg, sizeof(msg), 1))
1091 ERROR_WITH_ERRNO("Failed to send status to unmount process");
1094 static int unmount_progress_func(enum wimlib_progress_msg msg,
1095 const union wimlib_progress_info *info)
1097 if (msg == WIMLIB_PROGRESS_MSG_WRITE_STREAMS) {
1098 struct msg_write_streams_progress msg = {
1100 .min_version = WIMLIB_MAKEVERSION(1, 2, 0),
1101 .cur_version = WIMLIB_VERSION_CODE,
1102 .msg_type = MSG_TYPE_WRITE_STREAMS_PROGRESS,
1103 .msg_size = sizeof(msg),
1107 if (mq_send(wimfs_get_context()->daemon_to_unmount_mq,
1108 (void*)&msg, sizeof(msg), 1))
1110 ERROR_WITH_ERRNO("Failed to send status to unmount process");
1116 static int msg_unmount_request_handler(const void *_msg,
1117 struct msg_handler_context *handler_ctx)
1119 const struct msg_unmount_request *msg;
1120 struct wimfs_context *wimfs_ctx;
1124 wimlib_progress_func_t progress_func;
1126 DEBUG("Handling unmount request msg");
1129 wimfs_ctx = handler_ctx->daemon.wimfs_ctx;
1131 if (msg->hdr.msg_size < sizeof(*msg)) {
1132 status = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1136 unmount_flags = msg->unmount_flags;
1137 if (msg->want_progress_messages)
1138 progress_func = unmount_progress_func;
1140 progress_func = NULL;
1142 ret = send_daemon_info_msg(wimfs_ctx->daemon_to_unmount_mq, getpid(),
1143 wimfs_ctx->mount_flags);
1149 if (wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1150 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_COMMIT) {
1151 int write_flags = 0;
1152 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY)
1153 write_flags |= WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1154 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_REBUILD)
1155 write_flags |= WIMLIB_WRITE_FLAG_REBUILD;
1156 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_RECOMPRESS)
1157 write_flags |= WIMLIB_WRITE_FLAG_RECOMPRESS;
1158 status = rebuild_wim(wimfs_ctx, write_flags,
1162 DEBUG("Read-only mount");
1167 if (wimfs_ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1168 ret = delete_staging_dir(wimfs_ctx);
1170 ERROR("Failed to delete the staging directory");
1175 send_unmount_finished_msg(wimfs_ctx->daemon_to_unmount_mq, status);
1176 return MSG_BREAK_LOOP;
1179 static int msg_daemon_info_handler(const void *_msg,
1180 struct msg_handler_context *handler_ctx)
1182 const struct msg_daemon_info *msg = _msg;
1183 DEBUG("Handling daemon info msg");
1184 if (msg->hdr.msg_size < sizeof(*msg))
1185 return WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1186 handler_ctx->unmount.daemon_pid = msg->daemon_pid;
1187 handler_ctx->unmount.mount_flags = msg->mount_flags;
1188 handler_ctx->timeout_seconds = 1;
1189 DEBUG("pid of daemon is %d; mount flags were %#x",
1190 handler_ctx->unmount.daemon_pid,
1191 handler_ctx->unmount.mount_flags);
1195 static int msg_write_streams_progress_handler(const void *_msg,
1196 struct msg_handler_context *handler_ctx)
1198 const struct msg_write_streams_progress *msg = _msg;
1199 if (msg->hdr.msg_size < sizeof(*msg))
1200 return WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1201 if (handler_ctx->unmount.progress_func) {
1202 handler_ctx->unmount.progress_func(WIMLIB_PROGRESS_MSG_WRITE_STREAMS,
1208 static int msg_unmount_finished_handler(const void *_msg,
1209 struct msg_handler_context *handler_ctx)
1211 const struct msg_unmount_finished *msg = _msg;
1212 DEBUG("Handling unmount finished message");
1213 if (msg->hdr.msg_size < sizeof(*msg))
1214 return WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1215 handler_ctx->unmount.status = msg->status;
1216 DEBUG("status is %d", handler_ctx->unmount.status);
1217 return MSG_BREAK_LOOP;
1220 static int unmount_timed_out_cb(struct msg_handler_context *handler_ctx)
1222 if (handler_ctx->unmount.daemon_pid == 0) {
1225 kill(handler_ctx->unmount.daemon_pid, 0);
1226 if (errno == ESRCH) {
1229 DEBUG("Filesystem daemon is still alive... "
1230 "Waiting another %d seconds\n",
1231 handler_ctx->timeout_seconds);
1236 ERROR("The filesystem daemon has crashed! Changes to the "
1237 "WIM may not have been commited.");
1238 return WIMLIB_ERR_FILESYSTEM_DAEMON_CRASHED;
1241 static int daemon_timed_out_cb(struct msg_handler_context *handler_ctx)
1244 return WIMLIB_ERR_TIMEOUT;
1247 typedef int (*msg_handler_t)(const void *_msg,
1248 struct msg_handler_context *handler_ctx);
1250 struct msg_handler_callbacks {
1251 int (*timed_out)(struct msg_handler_context *);
1252 msg_handler_t msg_handlers[MSG_TYPE_MAX];
1255 static const struct msg_handler_callbacks unmount_msg_handler_callbacks = {
1256 .timed_out = unmount_timed_out_cb,
1258 [MSG_TYPE_DAEMON_INFO] = msg_daemon_info_handler,
1259 [MSG_TYPE_WRITE_STREAMS_PROGRESS] = msg_write_streams_progress_handler,
1260 [MSG_TYPE_UNMOUNT_FINISHED] = msg_unmount_finished_handler,
1264 static const struct msg_handler_callbacks daemon_msg_handler_callbacks = {
1265 .timed_out = daemon_timed_out_cb,
1267 [MSG_TYPE_UNMOUNT_REQUEST] = msg_unmount_request_handler,
1271 static int receive_message(mqd_t mq, struct msg_handler_context *handler_ctx,
1272 const msg_handler_t msg_handlers[],
1273 long mailbox_size, void *mailbox)
1276 struct timespec timeout;
1277 ssize_t bytes_received;
1278 struct unmount_msg_hdr *hdr;
1281 gettimeofday(&now, NULL);
1282 timeout.tv_sec = now.tv_sec + handler_ctx->timeout_seconds;
1283 timeout.tv_nsec = now.tv_usec * 1000;
1285 bytes_received = mq_timedreceive(mq, mailbox,
1286 mailbox_size, NULL, &timeout);
1288 if (bytes_received == -1) {
1289 if (errno == ETIMEDOUT) {
1290 ret = WIMLIB_ERR_TIMEOUT;
1292 ERROR_WITH_ERRNO("mq_timedreceive()");
1293 ret = WIMLIB_ERR_MQUEUE;
1295 } else if (bytes_received < sizeof(*hdr) ||
1296 bytes_received != hdr->msg_size) {
1297 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1298 } else if (WIMLIB_VERSION_CODE < hdr->min_version) {
1299 /*ERROR("Cannot understand the received message. "*/
1300 /*"Please upgrade wimlib to at least v%d.%d.%d",*/
1301 /*WIMLIB_GET_MAJOR_VERSION(hdr->min_version),*/
1302 /*WIMLIB_GET_MINOR_VERSION(hdr->min_version),*/
1303 /*WIMLIB_GET_PATCH_VERSION(hdr->min_version));*/
1304 ret = MSG_VERSION_TOO_HIGH;
1305 } else if (hdr->msg_type >= MSG_TYPE_MAX) {
1306 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1307 } else if (msg_handlers[hdr->msg_type] == NULL) {
1308 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1310 ret = msg_handlers[hdr->msg_type](mailbox, handler_ctx);
1315 static int message_loop(mqd_t mq,
1316 const struct msg_handler_callbacks *callbacks,
1317 struct msg_handler_context *handler_ctx)
1319 static const size_t MAX_MSG_SIZE = 512;
1324 DEBUG("Entering message loop");
1326 ret = get_mailbox(mq, MAX_MSG_SIZE, &msgsize, &mailbox);
1330 ret = receive_message(mq, handler_ctx,
1331 callbacks->msg_handlers,
1333 if (ret == 0 || ret == MSG_VERSION_TOO_HIGH) {
1335 } else if (ret == MSG_BREAK_LOOP) {
1338 } else if (ret == WIMLIB_ERR_TIMEOUT) {
1339 if (callbacks->timed_out)
1340 ret = callbacks->timed_out(handler_ctx);
1346 ERROR_WITH_ERRNO("Error communicating with "
1347 "filesystem daemon");
1352 DEBUG("Exiting message loop");
1356 /* Execute `fusermount -u', which is installed setuid root, to unmount the WIM.
1358 * FUSE does not yet implement synchronous unmounts. This means that fusermount
1359 * -u will return before the filesystem daemon returns from wimfs_destroy().
1360 * This is partly what we want, because we need to send a message from this
1361 * process to the filesystem daemon telling whether --commit was specified or
1362 * not. However, after that, the unmount process must wait for the filesystem
1363 * daemon to finish writing the WIM file.
1365 static int execute_fusermount(const char *dir)
1373 ERROR_WITH_ERRNO("Failed to fork()");
1374 return WIMLIB_ERR_FORK;
1378 execlp("fusermount", "fusermount", "-u", dir, NULL);
1379 ERROR_WITH_ERRNO("Failed to execute `fusermount'");
1380 exit(WIMLIB_ERR_FUSERMOUNT);
1384 ret = wait(&status);
1386 ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
1388 return WIMLIB_ERR_FUSERMOUNT;
1392 if (status == WIMLIB_ERR_FUSERMOUNT)
1393 ERROR("Could not find the `fusermount' program");
1395 ERROR("fusermount exited with status %d", status);
1397 /* Try again, but with the `umount' program. This is required
1398 * on other FUSE implementations such as FreeBSD's that do not
1399 * have a `fusermount' program. */
1403 ERROR_WITH_ERRNO("Failed to fork()");
1404 return WIMLIB_ERR_FORK;
1408 execlp("umount", "umount", dir, NULL);
1409 ERROR_WITH_ERRNO("Failed to execute `umount'");
1410 exit(WIMLIB_ERR_FUSERMOUNT);
1414 ret = wait(&status);
1416 ERROR_WITH_ERRNO("Failed to wait for `umount' process to "
1418 return WIMLIB_ERR_FUSERMOUNT;
1421 ERROR("`umount' exited with failure status");
1422 return WIMLIB_ERR_FUSERMOUNT;
1428 static int wimfs_access(const char *path, int mask)
1430 /* Permissions not implemented */
1434 static int wimfs_chmod(const char *path, mode_t mask)
1436 struct dentry *dentry;
1437 struct wimfs_context *ctx = wimfs_get_context();
1438 struct inode *inode;
1442 ret = lookup_resource(ctx->wim, path,
1443 get_lookup_flags(ctx) | LOOKUP_FLAG_DIRECTORY_OK,
1444 &dentry, NULL, NULL);
1447 inode = dentry->d_inode;
1448 inode_to_stbuf(inode, NULL, &stbuf);
1449 if (mask == stbuf.st_mode)
1455 /* Called when the filesystem is unmounted. */
1456 static void wimfs_destroy(void *p)
1458 struct wimfs_context *wimfs_ctx;
1460 wimfs_ctx = wimfs_get_context();
1462 if (open_message_queues(wimfs_ctx, true))
1465 struct msg_handler_context handler_ctx = {
1466 .timeout_seconds = 5,
1468 .wimfs_ctx = wimfs_ctx,
1472 message_loop(wimfs_ctx->unmount_to_daemon_mq,
1473 &daemon_msg_handler_callbacks,
1476 close_message_queues(wimfs_ctx);
1480 static int wimfs_fallocate(const char *path, int mode,
1481 off_t offset, off_t len, struct fuse_file_info *fi)
1483 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1484 wimlib_assert(fd->staging_fd != -1);
1485 return fallocate(fd->staging_fd, mode, offset, len);
1490 static int wimfs_fgetattr(const char *path, struct stat *stbuf,
1491 struct fuse_file_info *fi)
1493 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1494 return inode_to_stbuf(fd->f_inode, fd->f_lte, stbuf);
1497 static int wimfs_ftruncate(const char *path, off_t size,
1498 struct fuse_file_info *fi)
1500 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1501 int ret = ftruncate(fd->staging_fd, size);
1504 if (fd->f_lte && size < fd->f_lte->resource_entry.original_size)
1505 fd->f_lte->resource_entry.original_size = size;
1510 * Fills in a `struct stat' that corresponds to a file or directory in the WIM.
1512 static int wimfs_getattr(const char *path, struct stat *stbuf)
1514 struct dentry *dentry;
1515 struct lookup_table_entry *lte;
1517 struct wimfs_context *ctx = wimfs_get_context();
1519 ret = lookup_resource(ctx->wim, path,
1520 get_lookup_flags(ctx) | LOOKUP_FLAG_DIRECTORY_OK,
1521 &dentry, <e, NULL);
1524 return inode_to_stbuf(dentry->d_inode, lte, stbuf);
1528 /* Read an alternate data stream through the XATTR interface, or get its size */
1529 static int wimfs_getxattr(const char *path, const char *name, char *value,
1533 struct inode *inode;
1534 struct ads_entry *ads_entry;
1536 struct lookup_table_entry *lte;
1537 struct wimfs_context *ctx = wimfs_get_context();
1539 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1542 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1546 inode = wim_pathname_to_inode(ctx->wim, path);
1550 ads_entry = inode_get_ads_entry(inode, name, NULL);
1554 lte = ads_entry->lte;
1555 res_size = wim_resource_size(lte);
1560 if (res_size > size)
1563 ret = read_full_wim_resource(lte, (u8*)value,
1564 WIMLIB_RESOURCE_FLAG_MULTITHREADED);
1572 /* Create a hard link */
1573 static int wimfs_link(const char *to, const char *from)
1575 struct dentry *from_dentry, *from_dentry_parent;
1576 const char *link_name;
1577 struct inode *inode;
1578 struct lookup_table_entry *lte;
1579 WIMStruct *w = wimfs_get_WIMStruct();
1582 inode = wim_pathname_to_inode(w, to);
1586 if (inode->attributes & FILE_ATTRIBUTE_REPARSE_POINT)
1589 if (inode->attributes & FILE_ATTRIBUTE_DIRECTORY)
1592 from_dentry_parent = get_parent_dentry(w, from);
1593 if (!from_dentry_parent)
1595 if (!dentry_is_directory(from_dentry_parent))
1598 link_name = path_basename(from);
1599 if (get_dentry_child_with_name(from_dentry_parent, link_name))
1601 from_dentry = new_dentry(link_name);
1605 inode_add_dentry(from_dentry, inode);
1606 from_dentry->d_inode = inode;
1607 inode->link_count++;
1609 for (i = 0; i <= inode->num_ads; i++) {
1610 lte = inode_stream_lte_resolved(inode, i);
1614 dentry_add_child(from_dentry_parent, from_dentry);
1619 static int wimfs_listxattr(const char *path, char *list, size_t size)
1622 struct inode *inode;
1623 struct wimfs_context *ctx = wimfs_get_context();
1627 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1630 /* List alternate data streams, or get the list size */
1632 inode = wim_pathname_to_inode(ctx->wim, path);
1638 for (i = 0; i < inode->num_ads; i++)
1639 needed_size += inode->ads_entries[i].stream_name_utf8_len + 6;
1643 for (i = 0; i < inode->num_ads; i++) {
1644 needed_size = inode->ads_entries[i].stream_name_utf8_len + 6;
1645 if (needed_size > size)
1647 p += sprintf(p, "user.%s",
1648 inode->ads_entries[i].stream_name_utf8) + 1;
1649 size -= needed_size;
1657 /* Create a directory in the WIM.
1658 * @mode is currently ignored. */
1659 static int wimfs_mkdir(const char *path, mode_t mode)
1661 struct dentry *dentry;
1664 ret = create_dentry(wimfs_get_context(), path, &dentry);
1666 dentry->d_inode->attributes = FILE_ATTRIBUTE_DIRECTORY;
1670 /* Create a regular file in the WIM.
1671 * @mode is currently ignored. */
1672 static int wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
1674 const char *stream_name;
1675 struct wimfs_context *ctx = wimfs_get_context();
1676 if ((ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
1677 && (stream_name = path_stream_name(path))) {
1678 /* Make an alternate data stream */
1679 struct ads_entry *new_entry;
1680 struct inode *inode;
1682 char *p = (char*)stream_name - 1;
1683 wimlib_assert(*p == ':');
1686 inode = wim_pathname_to_inode(ctx->wim, path);
1689 if (inode->attributes &
1690 (FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_DIRECTORY))
1692 if (inode_get_ads_entry(inode, stream_name, NULL))
1694 new_entry = inode_add_ads(inode, stream_name);
1699 struct dentry *dentry;
1702 /* Make a normal file (not an alternate data stream) */
1703 ret = create_dentry(ctx, path, &dentry);
1705 dentry->d_inode->attributes = FILE_ATTRIBUTE_NORMAL;
1712 static int wimfs_open(const char *path, struct fuse_file_info *fi)
1714 struct dentry *dentry;
1715 struct lookup_table_entry *lte;
1717 struct wimlib_fd *fd;
1718 struct inode *inode;
1721 struct wimfs_context *ctx = wimfs_get_context();
1723 ret = lookup_resource(ctx->wim, path, get_lookup_flags(ctx),
1724 &dentry, <e, &stream_idx);
1728 inode = dentry->d_inode;
1730 if (stream_idx == 0)
1733 stream_id = inode->ads_entries[stream_idx - 1].stream_id;
1735 /* The file resource may be in the staging directory (read-write mounts
1736 * only) or in the WIM. If it's in the staging directory, we need to
1737 * open a native file descriptor for the corresponding file. Otherwise,
1738 * we can read the file resource directly from the WIM file if we are
1739 * opening it read-only, but we need to extract the resource to the
1740 * staging directory if we are opening it writable. */
1742 if (flags_writable(fi->flags) &&
1743 (!lte || lte->resource_location != RESOURCE_IN_STAGING_FILE)) {
1744 u64 size = (lte) ? wim_resource_size(lte) : 0;
1745 ret = extract_resource_to_staging_dir(inode, stream_id,
1751 ret = alloc_wimlib_fd(inode, stream_id, lte, &fd,
1752 wimfs_ctx_readonly(ctx));
1756 if (lte && lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1757 fd->staging_fd = open(lte->staging_file_name, fi->flags);
1758 if (fd->staging_fd == -1) {
1759 int errno_save = errno;
1760 close_wimlib_fd(fd);
1764 fi->fh = (uintptr_t)fd;
1768 /* Opens a directory. */
1769 static int wimfs_opendir(const char *path, struct fuse_file_info *fi)
1771 struct inode *inode;
1773 struct wimlib_fd *fd = NULL;
1774 struct wimfs_context *ctx = wimfs_get_context();
1775 WIMStruct *w = ctx->wim;
1777 inode = wim_pathname_to_inode(w, path);
1780 if (!inode_is_directory(inode))
1782 ret = alloc_wimlib_fd(inode, 0, NULL, &fd, wimfs_ctx_readonly(ctx));
1783 fi->fh = (uintptr_t)fd;
1789 * Read data from a file in the WIM or in the staging directory.
1791 static int wimfs_read(const char *path, char *buf, size_t size,
1792 off_t offset, struct fuse_file_info *fi)
1794 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1800 if (!fd->f_lte) /* Empty stream with no lookup table entry */
1803 if (fd->f_lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1804 /* Read from staging file */
1806 wimlib_assert(fd->f_lte->staging_file_name);
1807 wimlib_assert(fd->staging_fd != -1);
1809 DEBUG("Seek to offset %zu", offset);
1811 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1813 ret = read(fd->staging_fd, buf, size);
1819 u64 res_size = wim_resource_size(fd->f_lte);
1820 if (offset > res_size)
1822 size = min(size, res_size - offset);
1823 if (read_wim_resource(fd->f_lte, (u8*)buf,
1825 WIMLIB_RESOURCE_FLAG_MULTITHREADED) != 0)
1831 struct fill_params {
1833 fuse_fill_dir_t filler;
1836 static int dentry_fuse_fill(struct dentry *dentry, void *arg)
1838 struct fill_params *fill_params = arg;
1839 return fill_params->filler(fill_params->buf, dentry->file_name_utf8,
1843 /* Fills in the entries of the directory specified by @path using the
1844 * FUSE-provided function @filler. */
1845 static int wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
1846 off_t offset, struct fuse_file_info *fi)
1848 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1849 struct inode *inode;
1854 inode = fd->f_inode;
1856 struct fill_params fill_params = {
1861 filler(buf, ".", NULL, 0);
1862 filler(buf, "..", NULL, 0);
1864 return for_dentry_in_rbtree(inode->children.rb_node,
1865 dentry_fuse_fill, &fill_params);
1869 static int wimfs_readlink(const char *path, char *buf, size_t buf_len)
1871 struct wimfs_context *ctx = wimfs_get_context();
1872 struct inode *inode = wim_pathname_to_inode(ctx->wim, path);
1876 if (!inode_is_symlink(inode))
1879 ret = inode_readlink(inode, buf, buf_len, ctx->wim,
1880 WIMLIB_RESOURCE_FLAG_MULTITHREADED);
1887 static int wimfs_release(const char *path, struct fuse_file_info *fi)
1889 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1890 return close_wimlib_fd(fd);
1893 /* Close a directory */
1894 static int wimfs_releasedir(const char *path, struct fuse_file_info *fi)
1896 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1897 return close_wimlib_fd(fd);
1901 /* Remove an alternate data stream through the XATTR interface */
1902 static int wimfs_removexattr(const char *path, const char *name)
1904 struct inode *inode;
1905 struct ads_entry *ads_entry;
1907 struct wimfs_context *ctx = wimfs_get_context();
1909 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1912 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1916 inode = wim_pathname_to_inode(ctx->wim, path);
1920 ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
1923 inode_remove_ads(inode, ads_idx, ctx->wim->lookup_table);
1928 /* Renames a file or directory. See rename (3) */
1929 static int wimfs_rename(const char *from, const char *to)
1933 struct dentry *parent_of_dst;
1934 char *file_name_utf16 = NULL, *file_name_utf8 = NULL;
1935 u16 file_name_utf16_len, file_name_utf8_len;
1936 WIMStruct *w = wimfs_get_WIMStruct();
1939 /* This rename() implementation currently only supports actual files
1940 * (not alternate data streams) */
1942 src = get_dentry(w, from);
1946 dst = get_dentry(w, to);
1948 ret = get_names(&file_name_utf16, &file_name_utf8,
1949 &file_name_utf16_len, &file_name_utf8_len,
1955 /* Destination file exists */
1957 if (src == dst) /* Same file */
1960 if (!dentry_is_directory(src)) {
1961 /* Cannot rename non-directory to directory. */
1962 if (dentry_is_directory(dst))
1965 /* Cannot rename directory to a non-directory or a non-empty
1967 if (!dentry_is_directory(dst))
1969 if (inode_has_children(dst->d_inode))
1972 parent_of_dst = dst->parent;
1973 remove_dentry(dst, w->lookup_table);
1975 /* Destination does not exist */
1976 parent_of_dst = get_parent_dentry(w, to);
1980 if (!dentry_is_directory(parent_of_dst))
1984 FREE(src->file_name);
1985 FREE(src->file_name_utf8);
1986 src->file_name = file_name_utf16;
1987 src->file_name_utf8 = file_name_utf8;
1988 src->file_name_len = file_name_utf16_len;
1989 src->file_name_utf8_len = file_name_utf8_len;
1992 dentry_add_child(parent_of_dst, src);
1996 /* Remove a directory */
1997 static int wimfs_rmdir(const char *path)
1999 struct dentry *dentry;
2000 WIMStruct *w = wimfs_get_WIMStruct();
2002 dentry = get_dentry(w, path);
2006 if (!dentry_is_empty_directory(dentry))
2009 remove_dentry(dentry, w->lookup_table);
2014 /* Write an alternate data stream through the XATTR interface */
2015 static int wimfs_setxattr(const char *path, const char *name,
2016 const char *value, size_t size, int flags)
2018 struct ads_entry *existing_ads_entry;
2019 struct ads_entry *new_ads_entry;
2020 struct lookup_table_entry *existing_lte;
2021 struct lookup_table_entry *lte;
2022 struct inode *inode;
2023 u8 value_hash[SHA1_HASH_SIZE];
2025 struct wimfs_context *ctx = wimfs_get_context();
2027 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
2030 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
2034 inode = wim_pathname_to_inode(ctx->wim, path);
2038 existing_ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
2039 if (existing_ads_entry) {
2040 if (flags & XATTR_CREATE)
2042 inode_remove_ads(inode, ads_idx, ctx->wim->lookup_table);
2044 if (flags & XATTR_REPLACE)
2047 new_ads_entry = inode_add_ads(inode, name);
2051 sha1_buffer((const u8*)value, size, value_hash);
2053 existing_lte = __lookup_resource(ctx->wim->lookup_table, value_hash);
2060 lte = new_lookup_table_entry();
2063 value_copy = MALLOC(size);
2068 memcpy(value_copy, value, size);
2069 lte->resource_location = RESOURCE_IN_ATTACHED_BUFFER;
2070 lte->attached_buffer = value_copy;
2071 lte->resource_entry.original_size = size;
2072 lte->resource_entry.size = size;
2073 lte->resource_entry.flags = 0;
2074 copy_hash(lte->hash, value_hash);
2075 lookup_table_insert(ctx->wim->lookup_table, lte);
2077 new_ads_entry->lte = lte;
2082 static int wimfs_symlink(const char *to, const char *from)
2084 struct wimfs_context *ctx = wimfs_get_context();
2085 struct dentry *dentry;
2088 ret = create_dentry(ctx, from, &dentry);
2090 dentry->d_inode->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
2091 dentry->d_inode->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
2092 if (inode_set_symlink(dentry->d_inode, to,
2093 ctx->wim->lookup_table, NULL))
2095 unlink_dentry(dentry);
2096 free_dentry(dentry);
2104 /* Reduce the size of a file */
2105 static int wimfs_truncate(const char *path, off_t size)
2107 struct dentry *dentry;
2108 struct lookup_table_entry *lte;
2112 struct inode *inode;
2113 struct wimfs_context *ctx = wimfs_get_context();
2115 ret = lookup_resource(ctx->wim, path, get_lookup_flags(ctx),
2116 &dentry, <e, &stream_idx);
2121 if (lte == NULL && size == 0)
2124 inode = dentry->d_inode;
2125 if (stream_idx == 0)
2128 stream_id = inode->ads_entries[stream_idx - 1].stream_id;
2130 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
2131 ret = truncate(lte->staging_file_name, size);
2135 /* File in WIM. Extract it to the staging directory, but only
2136 * the first @size bytes of it. */
2137 ret = extract_resource_to_staging_dir(inode, stream_id,
2143 /* Unlink a non-directory or alternate data stream */
2144 static int wimfs_unlink(const char *path)
2146 struct dentry *dentry;
2147 struct lookup_table_entry *lte;
2150 struct wimfs_context *ctx = wimfs_get_context();
2152 ret = lookup_resource(ctx->wim, path, get_lookup_flags(ctx),
2153 &dentry, <e, &stream_idx);
2158 if (stream_idx == 0)
2159 remove_dentry(dentry, ctx->wim->lookup_table);
2161 inode_remove_ads(dentry->d_inode, stream_idx - 1,
2162 ctx->wim->lookup_table);
2166 #ifdef HAVE_UTIMENSAT
2168 * Change the timestamp on a file dentry.
2170 * Note that alternate data streams do not have their own timestamps.
2172 static int wimfs_utimens(const char *path, const struct timespec tv[2])
2174 struct dentry *dentry;
2175 struct inode *inode;
2176 WIMStruct *w = wimfs_get_WIMStruct();
2178 dentry = get_dentry(w, path);
2181 inode = dentry->d_inode;
2183 if (tv[0].tv_nsec != UTIME_OMIT) {
2184 if (tv[0].tv_nsec == UTIME_NOW)
2185 inode->last_access_time = get_wim_timestamp();
2187 inode->last_access_time = timespec_to_wim_timestamp(&tv[0]);
2189 if (tv[1].tv_nsec != UTIME_OMIT) {
2190 if (tv[1].tv_nsec == UTIME_NOW)
2191 inode->last_write_time = get_wim_timestamp();
2193 inode->last_write_time = timespec_to_wim_timestamp(&tv[1]);
2198 static int wimfs_utime(const char *path, struct utimbuf *times)
2200 struct dentry *dentry;
2201 struct inode *inode;
2202 WIMStruct *w = wimfs_get_WIMStruct();
2204 dentry = get_dentry(w, path);
2207 inode = dentry->d_inode;
2209 inode->last_write_time = unix_timestamp_to_wim(times->modtime);
2210 inode->last_access_time = unix_timestamp_to_wim(times->actime);
2215 /* Writes to a file in the WIM filesystem.
2216 * It may be an alternate data stream, but here we don't even notice because we
2217 * just get a lookup table entry. */
2218 static int wimfs_write(const char *path, const char *buf, size_t size,
2219 off_t offset, struct fuse_file_info *fi)
2221 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
2228 wimlib_assert(fd->f_lte);
2229 wimlib_assert(fd->f_lte->staging_file_name);
2230 wimlib_assert(fd->staging_fd != -1);
2231 wimlib_assert(fd->f_inode);
2233 /* Seek to the requested position */
2234 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
2237 /* Write the data. */
2238 ret = write(fd->staging_fd, buf, size);
2242 now = get_wim_timestamp();
2243 fd->f_inode->last_write_time = now;
2244 fd->f_inode->last_access_time = now;
2248 static struct fuse_operations wimfs_operations = {
2249 .access = wimfs_access,
2250 .chmod = wimfs_chmod,
2251 .destroy = wimfs_destroy,
2253 .fallocate = wimfs_fallocate,
2255 .fgetattr = wimfs_fgetattr,
2256 .ftruncate = wimfs_ftruncate,
2257 .getattr = wimfs_getattr,
2259 .getxattr = wimfs_getxattr,
2263 .listxattr = wimfs_listxattr,
2265 .mkdir = wimfs_mkdir,
2266 .mknod = wimfs_mknod,
2268 .opendir = wimfs_opendir,
2270 .readdir = wimfs_readdir,
2271 .readlink = wimfs_readlink,
2272 .release = wimfs_release,
2273 .releasedir = wimfs_releasedir,
2275 .removexattr = wimfs_removexattr,
2277 .rename = wimfs_rename,
2278 .rmdir = wimfs_rmdir,
2280 .setxattr = wimfs_setxattr,
2282 .symlink = wimfs_symlink,
2283 .truncate = wimfs_truncate,
2284 .unlink = wimfs_unlink,
2285 #ifdef HAVE_UTIMENSAT
2286 .utimens = wimfs_utimens,
2288 .utime = wimfs_utime,
2290 .write = wimfs_write,
2292 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 8)
2293 .flag_nullpath_ok = 1,
2295 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 9)
2297 .flag_utime_omit_ok = 1,
2302 /* Mounts an image from a WIM file. */
2303 WIMLIBAPI int wimlib_mount_image(WIMStruct *wim, int image, const char *dir,
2304 int mount_flags, WIMStruct **additional_swms,
2305 unsigned num_additional_swms,
2306 const char *staging_dir)
2312 struct lookup_table *joined_tab, *wim_tab_save;
2313 struct image_metadata *imd;
2314 struct wimfs_context ctx;
2315 struct hlist_node *cur_node;
2316 struct inode *inode;
2318 DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
2319 wim, image, dir, mount_flags);
2322 return WIMLIB_ERR_INVALID_PARAM;
2324 ret = verify_swm_set(wim, additional_swms, num_additional_swms);
2328 if ((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) && (wim->hdr.total_parts != 1)) {
2329 ERROR("Cannot mount a split WIM read-write");
2330 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
2333 if (num_additional_swms) {
2334 ret = new_joined_lookup_table(wim, additional_swms,
2335 num_additional_swms,
2339 wim_tab_save = wim->lookup_table;
2340 wim->lookup_table = joined_tab;
2343 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2344 ret = wim_run_full_verifications(wim);
2349 ret = select_wim_image(wim, image);
2353 DEBUG("Selected image %d", image);
2355 imd = wim_get_current_image_metadata(wim);
2357 if (imd->root_dentry->refcnt != 1) {
2358 ERROR("Cannot mount image that was just exported with "
2359 "wimlib_export_image()");
2360 ret = WIMLIB_ERR_INVALID_PARAM;
2364 if (imd->modified) {
2365 ERROR("Cannot mount image that was added "
2366 "with wimlib_add_image()");
2367 ret = WIMLIB_ERR_INVALID_PARAM;
2371 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2372 ret = lock_wim(wim, wim->fp);
2377 if (!(mount_flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
2378 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
2379 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
2380 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
2383 DEBUG("Initializing struct wimfs_context");
2384 init_wimfs_context(&ctx);
2386 ctx.mount_flags = mount_flags;
2387 ctx.image_inode_list = &imd->inode_list;
2389 if (mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
2390 ctx.default_lookup_flags = LOOKUP_FLAG_ADS_OK;
2392 DEBUG("Unlinking message queues in case they already exist");
2393 ret = set_message_queue_names(&ctx, dir);
2396 unlink_message_queues(&ctx);
2398 DEBUG("Preparing arguments to fuse_main()");
2400 dir_copy = STRDUP(dir);
2402 goto out_free_message_queue_names;
2404 argv[argc++] = "imagex";
2405 argv[argc++] = dir_copy;
2407 /* disable multi-threaded operation for read-write mounts */
2408 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2409 argv[argc++] = "-s";
2411 if (mount_flags & WIMLIB_MOUNT_FLAG_DEBUG)
2412 argv[argc++] = "-d";
2415 * We provide the use_ino option because we are going to assign inode
2416 * numbers oursides. The inodes will be given unique numbers in the
2417 * assign_inode_numbers() function, and the static variable @next_ino is
2418 * set to the next available inode number.
2420 char optstring[256] =
2424 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 8)
2428 argv[argc++] = "-o";
2429 argv[argc++] = optstring;
2430 if ((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
2431 /* Read-write mount. Make the staging directory */
2432 ret = make_staging_dir(&ctx, staging_dir);
2434 goto out_free_dir_copy;
2436 /* Read-only mount */
2437 strcat(optstring, ",ro");
2444 DEBUG("FUSE command line (argc = %d): ", argc);
2445 for (i = 0; i < argc; i++) {
2446 fputs(argv[i], stdout);
2454 /* Mark dentry tree as modified if read-write mount. */
2455 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2457 imd->has_been_mounted_rw = 1;
2460 /* Resolve the lookup table entries for every inode in the image, and
2461 * assign inode numbers */
2462 DEBUG("Resolving lookup table entries and assigning inode numbers");
2465 hlist_for_each_entry(inode, cur_node, &imd->inode_list, hlist) {
2466 inode_resolve_ltes(inode, wim->lookup_table);
2467 inode->ino = ctx.next_ino++;
2469 /*ctx.next_ino = assign_inode_numbers(&imd->inode_list);*/
2470 DEBUG("(next_ino = %"PRIu64")", ctx.next_ino);
2472 DEBUG("Calling fuse_main()");
2474 ret = fuse_main(argc, argv, &wimfs_operations, &ctx);
2476 DEBUG("Returned from fuse_main() (ret = %d)", ret);
2478 ret = WIMLIB_ERR_FUSE;
2482 wim->wim_locked = 0;
2483 out_free_message_queue_names:
2484 free_message_queue_names(&ctx);
2486 if (num_additional_swms) {
2487 free_lookup_table(wim->lookup_table);
2488 wim->lookup_table = wim_tab_save;
2494 * Unmounts the WIM file that was previously mounted on @dir by using
2495 * wimlib_mount_image().
2497 WIMLIBAPI int wimlib_unmount_image(const char *dir, int unmount_flags,
2498 wimlib_progress_func_t progress_func)
2501 struct wimfs_context wimfs_ctx;
2503 init_wimfs_context(&wimfs_ctx);
2505 ret = set_message_queue_names(&wimfs_ctx, dir);
2509 ret = open_message_queues(&wimfs_ctx, false);
2511 goto out_free_message_queue_names;
2513 ret = send_unmount_request_msg(wimfs_ctx.unmount_to_daemon_mq,
2515 progress_func != NULL);
2517 goto out_close_message_queues;
2519 ret = execute_fusermount(dir);
2521 goto out_close_message_queues;
2523 struct msg_handler_context handler_ctx = {
2524 .timeout_seconds = 5,
2527 .progress_func = progress_func,
2531 ret = message_loop(wimfs_ctx.daemon_to_unmount_mq,
2532 &unmount_msg_handler_callbacks,
2535 ret = handler_ctx.unmount.status;
2536 out_close_message_queues:
2537 close_message_queues(&wimfs_ctx);
2538 out_free_message_queue_names:
2539 free_message_queue_names(&wimfs_ctx);
2544 #else /* WITH_FUSE */
2547 static inline int mount_unsupported_error()
2549 ERROR("wimlib was compiled with --without-fuse, which disables support "
2550 "for mounting WIMs.");
2551 return WIMLIB_ERR_UNSUPPORTED;
2554 WIMLIBAPI int wimlib_unmount_image(const char *dir, int unmount_flags,
2555 wimlib_progress_func_t progress_func)
2557 return mount_unsupported_error();
2560 WIMLIBAPI int wimlib_mount_image(WIMStruct *wim_p, int image, const char *dir,
2561 int mount_flags, WIMStruct **additional_swms,
2562 unsigned num_additional_swms,
2563 const char *staging_dir)
2565 return mount_unsupported_error();
2568 #endif /* WITH_FUSE */