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"
37 #include "timestamp.h"
42 #define FUSE_USE_VERSION 26
52 #include <attr/xattr.h>
55 /* File descriptor to a file open on the WIM filesystem. */
57 struct inode *f_inode;
58 struct lookup_table_entry *f_lte;
64 struct wimfs_context {
65 /* The WIMStruct for the mounted WIM. */
68 /* Working directory when `imagex mount' is run. */
69 char *working_directory;
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 /* Next inode number to be assigned. */
85 /* List of lookup table entries in the staging directory */
86 struct list_head staging_list;
88 /* Name and message queue descriptors for message queues between the filesystem
89 * daemon process and the unmount process. These are used when the filesystem
90 * is unmounted and the process running wimlib_mount() (i.e. the `imagex
91 * unmount' command) needs to communicate with the filesystem daemon running
92 * fuse_main() (i.e. that spawned by the `imagex mount' or `imagex mountrw'
94 char *unmount_to_daemon_mq_name;
95 char *daemon_to_unmount_mq_name;
96 mqd_t unmount_to_daemon_mq;
97 mqd_t daemon_to_unmount_mq;
100 static void init_wimfs_context(struct wimfs_context *ctx)
102 memset(ctx, 0, sizeof(*ctx));
103 ctx->unmount_to_daemon_mq = (mqd_t)-1;
104 ctx->daemon_to_unmount_mq = (mqd_t)-1;
105 INIT_LIST_HEAD(&ctx->staging_list);
108 static inline struct wimfs_context *wimfs_get_context()
110 return (struct wimfs_context*)fuse_get_context()->private_data;
113 static inline WIMStruct *wimfs_get_WIMStruct()
115 return wimfs_get_context()->wim;
118 static inline int get_lookup_flags(const struct wimfs_context *ctx)
120 if (ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
121 return LOOKUP_FLAG_ADS_OK;
126 /* Returns nonzero if write permission is requested on the file open flags */
127 static inline int flags_writable(int open_flags)
129 return open_flags & (O_RDWR | O_WRONLY);
133 * Allocate a file descriptor for a stream.
135 * @inode: inode containing the stream we're opening
136 * @stream_id: ID of the stream we're opening
137 * @lte: Lookup table entry for the stream (may be NULL)
138 * @fd_ret: Return the allocated file descriptor if successful.
140 * Return 0 iff successful or error code if unsuccessful.
142 static int alloc_wimlib_fd(struct inode *inode,
144 struct lookup_table_entry *lte,
145 struct wimlib_fd **fd_ret)
147 static const u16 fds_per_alloc = 8;
148 static const u16 max_fds = 0xffff;
151 pthread_mutex_lock(&inode->i_mutex);
153 DEBUG("Allocating fd for stream ID %u from inode %lx (open = %u, allocated = %u)",
154 stream_id, inode->ino, inode->num_opened_fds,
155 inode->num_allocated_fds);
157 if (inode->num_opened_fds == inode->num_allocated_fds) {
158 struct wimlib_fd **fds;
161 if (inode->num_allocated_fds == max_fds) {
165 num_new_fds = min(fds_per_alloc,
166 max_fds - inode->num_allocated_fds);
168 fds = REALLOC(inode->fds,
169 (inode->num_allocated_fds + num_new_fds) *
170 sizeof(inode->fds[0]));
175 memset(&fds[inode->num_allocated_fds], 0,
176 num_new_fds * sizeof(fds[0]));
178 inode->num_allocated_fds += num_new_fds;
180 for (u16 i = 0; ; i++) {
181 if (!inode->fds[i]) {
182 struct wimlib_fd *fd = CALLOC(1, sizeof(*fd));
191 fd->stream_id = stream_id;
194 inode->num_opened_fds++;
196 atomic_inc(<e->num_opened_fds);
197 DEBUG("Allocated fd (idx = %u)", fd->idx);
203 pthread_mutex_unlock(&inode->i_mutex);
207 static void inode_put_fd(struct inode *inode, struct wimlib_fd *fd)
209 wimlib_assert(fd != NULL);
210 wimlib_assert(inode != NULL);
212 pthread_mutex_lock(&inode->i_mutex);
214 wimlib_assert(fd->f_inode == inode);
215 wimlib_assert(inode->num_opened_fds != 0);
216 wimlib_assert(fd->idx < inode->num_allocated_fds);
217 wimlib_assert(inode->fds[fd->idx] == fd);
219 inode->fds[fd->idx] = NULL;
221 if (--inode->num_opened_fds == 0 && inode->link_count == 0) {
222 pthread_mutex_unlock(&inode->i_mutex);
225 pthread_mutex_unlock(&inode->i_mutex);
229 static int lte_put_fd(struct lookup_table_entry *lte, struct wimlib_fd *fd)
231 wimlib_assert(fd != NULL);
232 wimlib_assert(fd->f_lte == lte);
234 if (!lte) /* Empty stream with no lookup table entry */
237 /* Close staging file descriptor if needed. */
239 if (lte->resource_location == RESOURCE_IN_STAGING_FILE
240 && fd->staging_fd != -1)
242 if (close(fd->staging_fd) != 0) {
243 ERROR_WITH_ERRNO("Failed to close staging file");
247 lte_decrement_num_opened_fds(lte);
251 /* Close a file descriptor. */
252 static int close_wimlib_fd(struct wimlib_fd *fd)
255 wimlib_assert(fd != NULL);
256 DEBUG("Closing fd (inode = %lu, opened = %u, allocated = %u)",
257 fd->f_inode->ino, fd->f_inode->num_opened_fds,
258 fd->f_inode->num_allocated_fds);
259 ret = lte_put_fd(fd->f_lte, fd);
263 inode_put_fd(fd->f_inode, fd);
267 /* Remove a dentry; i.e. remove a reference to the corresponding inode.
269 * If there are no remaining references to the inode either through dentries or
270 * open file descriptors, the inode is freed. Otherwise, the inode is not
271 * removed, but the dentry is unlinked and freed.
273 * Either way, all lookup table entries referenced by the inode have their
274 * reference count decremented. If a lookup table entry has no open file
275 * descriptors and no references remaining, it is freed, and the staging file is
278 static void remove_dentry(struct dentry *dentry,
279 struct lookup_table *lookup_table)
281 struct inode *inode = dentry->d_inode;
282 struct lookup_table_entry *lte;
285 for (i = 0; i <= inode->num_ads; i++) {
286 lte = inode_stream_lte_resolved(inode, i);
288 lte_decrement_refcnt(lte, lookup_table);
290 unlink_dentry(dentry);
294 /* Creates a new staging file and returns its file descriptor opened for
297 * @name_ret: A location into which the a pointer to the newly allocated name of
298 * the staging file is stored.
299 * @return: The file descriptor for the new file. Returns -1 and sets errno on
300 * error, for any reason possible from the creat() function.
302 static int create_staging_file(char **name_ret, int open_flags,
303 struct wimfs_context *ctx)
311 name_len = ctx->staging_dir_name_len + 1 + SHA1_HASH_SIZE;
312 name = MALLOC(name_len + 1);
320 memcpy(name, ctx->staging_dir_name, ctx->staging_dir_name_len);
321 name[ctx->staging_dir_name_len] = '/';
322 randomize_char_array_with_alnum(name + ctx->staging_dir_name_len + 1,
324 name[name_len] = '\0';
327 /* Just in case, verify that the randomly generated name doesn't name an
328 * existing file, and try again if so */
329 } while (stat(name, &stbuf) == 0);
335 /* doesn't exist--- ok */
337 DEBUG("Creating staging file `%s'", name);
339 fd = open(name, open_flags | O_CREAT | O_TRUNC, 0600);
351 * Extract a WIM resource to the staging directory.
353 * @inode: Inode that contains the stream we are extracting
355 * @stream_id: Identifier for the stream (it stays constant even if the indices
356 * of the stream entries are changed)
358 * @lte: Pointer to pointer to the lookup table entry for the stream we need to
359 * extract, or NULL if there was no lookup table entry present for the stream
361 * @size: Number of bytes of the stream we want to extract (this supports the
362 * wimfs_truncate() function).
364 static int extract_resource_to_staging_dir(struct inode *inode,
366 struct lookup_table_entry **lte,
368 struct wimfs_context *ctx)
370 char *staging_file_name;
373 struct lookup_table_entry *old_lte, *new_lte;
375 DEBUG("Extracting resource to staging dir: inode %"PRIu64", "
376 "stream id %"PRIu32, inode->ino, stream_id);
379 fd = create_staging_file(&staging_file_name, O_WRONLY, ctx);
384 ret = extract_wim_resource_to_fd(old_lte, fd, size);
387 if (ret != 0 || close(fd) != 0) {
393 goto out_delete_staging_file;
396 if (old_lte && inode->link_count == old_lte->refcnt) {
397 /* The reference count of the existing lookup table
398 * entry is the same as the link count of the inode that
399 * contains the stream we're opening. Therefore, ALL
400 * the references to the lookup table entry correspond
401 * to the stream we're trying to extract, so the lookup
402 * table entry can be re-used. */
403 DEBUG("Re-using lookup table entry");
404 lookup_table_unlink(ctx->wim->lookup_table, old_lte);
408 /* There's an existing lookup table entry, but its
409 * reference count is creater than the link count for
410 * the inode containing a stream we're opening.
411 * Therefore, we need to split the lookup table entry.
413 wimlib_assert(old_lte->refcnt > inode->link_count);
414 DEBUG("Splitting lookup table entry "
415 "(inode->link_count = %u, old_lte->refcnt = %u)",
416 inode->link_count, old_lte->refcnt);
420 new_lte = new_lookup_table_entry();
423 goto out_delete_staging_file;
426 /* There may already be open file descriptors to this stream if
427 * it's previously been opened read-only, but just now we're
428 * opening it read-write. Identify those file descriptors and
429 * change their lookup table entry pointers to point to the new
430 * lookup table entry, and open staging file descriptors for
433 * At the same time, we need to count the number of these opened
434 * file descriptors to the new lookup table entry. If there's
435 * an old lookup table entry, this number needs to be subtracted
436 * from the fd's opened to the old entry. */
437 for (u16 i = 0, j = 0; j < inode->num_opened_fds; i++) {
438 struct wimlib_fd *fd = inode->fds[i];
440 if (fd->stream_id == stream_id) {
441 wimlib_assert(fd->f_lte == old_lte);
442 wimlib_assert(fd->staging_fd == -1);
444 new_lte->num_opened_fds++;
445 fd->staging_fd = open(staging_file_name, O_RDONLY);
446 if (fd->staging_fd == -1) {
448 goto out_revert_fd_changes;
454 DEBUG("%hu fd's were already opened to the file we extracted",
455 new_lte->num_opened_fds);
457 old_lte->num_opened_fds -= new_lte->num_opened_fds;
458 old_lte->refcnt -= inode->link_count;
462 new_lte->resource_entry.original_size = size;
463 new_lte->refcnt = inode->link_count;
464 new_lte->resource_location = RESOURCE_IN_STAGING_FILE;
465 new_lte->staging_file_name = staging_file_name;
466 new_lte->lte_inode = inode;
467 random_hash(new_lte->hash);
470 inode->lte = new_lte;
472 for (u16 i = 0; i < inode->num_ads; i++)
473 if (inode->ads_entries[i].stream_id == stream_id)
474 inode->ads_entries[i].lte = new_lte;
476 lookup_table_insert(ctx->wim->lookup_table, new_lte);
477 list_add(&new_lte->staging_list, &ctx->staging_list);
480 out_revert_fd_changes:
481 for (u16 i = 0, j = 0; j < new_lte->num_opened_fds; i++) {
482 struct wimlib_fd *fd = inode->fds[i];
483 if (fd && fd->stream_id == stream_id && fd->f_lte == new_lte) {
485 if (fd->staging_fd != -1) {
486 close(fd->staging_fd);
492 free_lookup_table_entry(new_lte);
493 out_delete_staging_file:
494 unlink(staging_file_name);
495 FREE(staging_file_name);
500 * Creates a randomly named staging directory and saves its name in the
501 * filesystem context structure.
503 static int make_staging_dir(struct wimfs_context *ctx)
505 /* XXX Give the user an option of where to stage files */
507 static const char prefix[] = "wimlib-staging-";
508 static const size_t prefix_len = sizeof(prefix) - 1;
509 static const size_t random_suffix_len = 10;
511 size_t pwd_len = strlen(ctx->working_directory);
513 ctx->staging_dir_name_len = pwd_len + 1 + prefix_len + random_suffix_len;
515 ctx->staging_dir_name = MALLOC(ctx->staging_dir_name_len + 1);
516 if (!ctx->staging_dir_name)
517 return WIMLIB_ERR_NOMEM;
519 memcpy(ctx->staging_dir_name, ctx->working_directory, pwd_len);
520 ctx->staging_dir_name[pwd_len] = '/';
521 memcpy(ctx->staging_dir_name + pwd_len + 1, prefix, prefix_len);
522 randomize_char_array_with_alnum(ctx->staging_dir_name + pwd_len +
523 1 + prefix_len, random_suffix_len);
524 ctx->staging_dir_name[ctx->staging_dir_name_len] = '\0';
526 if (mkdir(ctx->staging_dir_name, 0700) != 0) {
527 ERROR_WITH_ERRNO("Failed to create temporary directory `%s'",
528 ctx->staging_dir_name);
529 FREE(ctx->staging_dir_name);
530 ctx->staging_dir_name = NULL;
531 return WIMLIB_ERR_MKDIR;
536 static int remove_file_or_directory(const char *fpath, const struct stat *sb,
537 int typeflag, struct FTW *ftwbuf)
539 if (remove(fpath) == 0)
542 ERROR_WITH_ERRNO("Cannot remove `%s'", fpath);
543 return WIMLIB_ERR_DELETE_STAGING_DIR;
549 * Deletes the staging directory and all the files contained in it.
551 static int delete_staging_dir(struct wimfs_context *ctx)
554 wimlib_assert(ctx->staging_dir_name != NULL);
555 ret = nftw(ctx->staging_dir_name, remove_file_or_directory,
557 FREE(ctx->staging_dir_name);
558 ctx->staging_dir_name = NULL;
563 /* Simple function that returns the concatenation of 2 strings. */
564 static char *strcat_dup(const char *s1, const char *s2, size_t max_len)
566 size_t len = strlen(s1) + strlen(s2);
569 char *p = MALLOC(len + 1);
572 snprintf(p, len + 1, "%s%s", s1, s2);
576 static int set_message_queue_names(struct wimfs_context *ctx,
577 const char *mount_dir)
579 static const char *u2d_prefix = "/wimlib-unmount-to-daemon-mq";
580 static const char *d2u_prefix = "/wimlib-daemon-to-unmount-mq";
585 dir_path = realpath(mount_dir, NULL);
587 ERROR_WITH_ERRNO("Failed to resolve path \"%s\"", mount_dir);
588 return WIMLIB_ERR_NOTDIR;
591 DEBUG("Using absolute dir_path = `%s'", dir_path);
601 ctx->unmount_to_daemon_mq_name = strcat_dup(u2d_prefix, dir_path,
603 if (!ctx->unmount_to_daemon_mq_name) {
604 ret = WIMLIB_ERR_NOMEM;
605 goto out_free_dir_path;
607 ctx->daemon_to_unmount_mq_name = strcat_dup(d2u_prefix, dir_path,
609 if (!ctx->daemon_to_unmount_mq_name) {
610 ret = WIMLIB_ERR_NOMEM;
611 goto out_free_unmount_to_daemon_mq_name;
615 goto out_free_dir_path;
616 out_free_unmount_to_daemon_mq_name:
617 FREE(ctx->unmount_to_daemon_mq_name);
618 ctx->unmount_to_daemon_mq_name = NULL;
624 static void free_message_queue_names(struct wimfs_context *ctx)
626 FREE(ctx->unmount_to_daemon_mq_name);
627 FREE(ctx->daemon_to_unmount_mq_name);
628 ctx->unmount_to_daemon_mq_name = NULL;
629 ctx->daemon_to_unmount_mq_name = NULL;
633 * Opens two POSIX message queue: one for sending messages from the unmount
634 * process to the daemon process, and one to go the other way. The names of the
635 * message queues, which must be system-wide unique, are be based on the mount
636 * point. (There of course is still a possibility of a collision if one were to
637 * unmount two identically named directories simultaneously...)
639 * @daemon specifies whether the calling process is the filesystem daemon or the
642 static int open_message_queues(struct wimfs_context *ctx, bool daemon)
647 wimlib_assert(ctx->unmount_to_daemon_mq_name != NULL &&
648 ctx->daemon_to_unmount_mq_name != NULL);
651 flags = O_RDONLY | O_CREAT;
653 flags = O_WRONLY | O_CREAT;
655 DEBUG("Opening message queue \"%s\"", ctx->unmount_to_daemon_mq_name);
656 ctx->unmount_to_daemon_mq = mq_open(ctx->unmount_to_daemon_mq_name,
659 if (ctx->unmount_to_daemon_mq == (mqd_t)-1) {
660 ERROR_WITH_ERRNO("mq_open()");
661 ret = WIMLIB_ERR_MQUEUE;
666 flags = O_WRONLY | O_CREAT;
668 flags = O_RDONLY | O_CREAT;
670 DEBUG("Opening message queue \"%s\"", ctx->daemon_to_unmount_mq_name);
671 ctx->daemon_to_unmount_mq = mq_open(ctx->daemon_to_unmount_mq_name,
674 if (ctx->daemon_to_unmount_mq == (mqd_t)-1) {
675 ERROR_WITH_ERRNO("mq_open()");
676 ret = WIMLIB_ERR_MQUEUE;
677 goto out_close_unmount_to_daemon_mq;
681 out_close_unmount_to_daemon_mq:
682 mq_close(ctx->unmount_to_daemon_mq);
683 mq_unlink(ctx->unmount_to_daemon_mq_name);
684 ctx->unmount_to_daemon_mq = (mqd_t)-1;
689 /* Try to determine the maximum message size of a message queue. The return
690 * value is the maximum message size, or a guess of 8192 bytes if it cannot be
692 static long mq_get_msgsize(mqd_t mq)
694 static const char *msgsize_max_file = "/proc/sys/fs/mqueue/msgsize_max";
699 if (mq_getattr(mq, &attr) == 0) {
700 msgsize = attr.mq_msgsize;
702 ERROR_WITH_ERRNO("mq_getattr()");
703 ERROR("Attempting to read %s", msgsize_max_file);
704 fp = fopen(msgsize_max_file, "rb");
706 if (fscanf(fp, "%ld", &msgsize) != 1) {
707 ERROR("Assuming message size of 8192");
712 ERROR_WITH_ERRNO("Failed to open the file `%s'",
714 ERROR("Assuming message size of 8192");
721 static int get_mailbox(mqd_t mq, long needed_msgsize, long *msgsize_ret,
727 msgsize = mq_get_msgsize(mq);
729 if (msgsize < needed_msgsize) {
730 ERROR("Message queue max size must be at least %ld!",
732 return WIMLIB_ERR_MQUEUE;
735 mailbox = MALLOC(msgsize);
737 ERROR("Failed to allocate %ld bytes for mailbox", msgsize);
738 return WIMLIB_ERR_NOMEM;
740 *msgsize_ret = msgsize;
741 *mailbox_ret = mailbox;
745 static void unlink_message_queues(struct wimfs_context *ctx)
747 mq_unlink(ctx->unmount_to_daemon_mq_name);
748 mq_unlink(ctx->daemon_to_unmount_mq_name);
751 /* Closes the message queues, which are allocated in static variables */
752 static void close_message_queues(struct wimfs_context *ctx)
754 mq_close(ctx->unmount_to_daemon_mq);
755 ctx->unmount_to_daemon_mq = (mqd_t)(-1);
756 mq_close(ctx->daemon_to_unmount_mq);
757 ctx->daemon_to_unmount_mq = (mqd_t)(-1);
758 unlink_message_queues(ctx);
761 static int wimfs_access(const char *path, int mask)
763 /* Permissions not implemented */
767 static int wimfs_chmod(const char *path, mode_t mask)
769 struct dentry *dentry;
770 struct wimfs_context *ctx = wimfs_get_context();
775 ret = lookup_resource(ctx->wim, path,
776 get_lookup_flags(ctx) | LOOKUP_FLAG_DIRECTORY_OK,
777 &dentry, NULL, NULL);
780 inode = dentry->d_inode;
781 inode_to_stbuf(inode, NULL, &stbuf);
782 if (mask == stbuf.st_mode)
789 static void inode_update_lte_ptr(struct inode *inode,
790 struct lookup_table_entry *old_lte,
791 struct lookup_table_entry *new_lte)
793 if (inode->lte == old_lte) {
794 inode->lte = new_lte;
796 for (unsigned i = 0; i < inode->num_ads; i++) {
797 if (inode->ads_entries[i].lte == old_lte) {
798 inode->ads_entries[i].lte = new_lte;
805 static int update_lte_of_staging_file(struct lookup_table_entry *lte,
806 struct lookup_table *table)
808 struct lookup_table_entry *duplicate_lte;
810 u8 hash[SHA1_HASH_SIZE];
813 wimlib_assert(lte->resource_location == RESOURCE_IN_STAGING_FILE);
814 wimlib_assert(lte->staging_file_name);
816 ret = sha1sum(lte->staging_file_name, hash);
820 lookup_table_unlink(table, lte);
822 duplicate_lte = __lookup_resource(table, hash);
825 /* Merge duplicate lookup table entries */
826 duplicate_lte->refcnt += lte->refcnt;
827 list_del(<e->staging_list);
828 inode_update_lte_ptr(lte->lte_inode, lte, duplicate_lte);
829 free_lookup_table_entry(lte);
831 if (stat(lte->staging_file_name, &stbuf) != 0) {
832 ERROR_WITH_ERRNO("Failed to stat `%s'", lte->staging_file_name);
833 return WIMLIB_ERR_STAT;
835 if (stbuf.st_size == 0) {
836 /* Zero-length stream. No lookup table entry needed. */
837 inode_update_lte_ptr(lte->lte_inode, lte, NULL);
838 free_lookup_table_entry(lte);
840 wimlib_assert(<e->file_on_disk == <e->staging_file_name);
841 lte->resource_entry.original_size = stbuf.st_size;
842 lte->resource_entry.size = stbuf.st_size;
843 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
844 lte->lte_inode = NULL;
845 copy_hash(lte->hash, hash);
846 lookup_table_insert(table, lte);
852 static int inode_close_fds(struct inode *inode)
854 u16 num_opened_fds = inode->num_opened_fds;
855 for (u16 i = 0, j = 0; j < num_opened_fds; i++) {
856 struct wimlib_fd *fd = inode->fds[i];
858 wimlib_assert(fd->f_inode == inode);
859 int ret = close_wimlib_fd(fd);
868 /* Overwrites the WIM file, with changes saved. */
869 static int rebuild_wim(struct wimfs_context *ctx, bool check_integrity)
872 struct lookup_table_entry *lte, *tmp;
873 WIMStruct *w = ctx->wim;
876 DEBUG("Closing all staging file descriptors.");
877 list_for_each_entry_safe(lte, tmp, &ctx->staging_list, staging_list) {
878 ret = inode_close_fds(lte->lte_inode);
883 DEBUG("Calculating SHA1 checksums for all new staging files.");
884 list_for_each_entry_safe(lte, tmp, &ctx->staging_list, staging_list) {
885 ret = update_lte_of_staging_file(lte, ctx->wim->lookup_table);
890 xml_update_image_info(w, w->current_image);
892 ret = wimlib_overwrite(w, check_integrity);
894 ERROR("Failed to commit changes");
900 /* Called when the filesystem is unmounted. */
901 static void wimfs_destroy(void *p)
903 /* For read-write mounts, the `imagex unmount' command, which is
904 * running in a separate process and is executing the
905 * wimlib_unmount() function, will send this process a byte
906 * through a message queue that indicates whether the --commit
907 * option was specified or not. */
910 struct timespec timeout;
912 ssize_t bytes_received;
915 char check_integrity;
918 struct wimfs_context *ctx = wimfs_get_context();
920 if (open_message_queues(ctx, true))
923 if (get_mailbox(ctx->unmount_to_daemon_mq, 2, &msgsize, &mailbox))
924 goto out_close_message_queues;
926 /* Wait at most 3 seconds before giving up and discarding changes. */
927 gettimeofday(&now, NULL);
928 timeout.tv_sec = now.tv_sec + 3;
929 timeout.tv_nsec = now.tv_usec * 1000;
930 DEBUG("Waiting for message telling us whether to commit or not, and "
931 "whether to include integrity checks.");
933 bytes_received = mq_timedreceive(ctx->unmount_to_daemon_mq, mailbox,
934 msgsize, NULL, &timeout);
935 if (bytes_received == -1) {
936 ERROR_WITH_ERRNO("mq_timedreceive()");
937 ERROR("Not committing.");
941 DEBUG("Received message: [%d %d]", mailbox[0], mailbox[1]);
943 check_integrity = mailbox[1];
947 if (ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
949 ret = chdir(ctx->working_directory);
951 status = rebuild_wim(ctx, (check_integrity != 0));
953 ERROR_WITH_ERRNO("chdir()");
954 status = WIMLIB_ERR_NOTDIR;
957 ret = delete_staging_dir(ctx);
959 ERROR("Failed to delete the staging directory");
964 DEBUG("Read-only mount");
966 DEBUG("Sending status %hhd", status);
967 ret = mq_send(ctx->daemon_to_unmount_mq, &status, 1, 1);
969 ERROR_WITH_ERRNO("Failed to send status to unmount process");
971 out_close_message_queues:
972 close_message_queues(ctx);
976 static int wimfs_fallocate(const char *path, int mode,
977 off_t offset, off_t len, struct fuse_file_info *fi)
979 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
980 wimlib_assert(fd->staging_fd != -1);
981 return fallocate(fd->staging_fd, mode, offset, len);
986 static int wimfs_fgetattr(const char *path, struct stat *stbuf,
987 struct fuse_file_info *fi)
989 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
990 return inode_to_stbuf(fd->f_inode, fd->f_lte, stbuf);
993 static int wimfs_ftruncate(const char *path, off_t size,
994 struct fuse_file_info *fi)
996 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
997 int ret = ftruncate(fd->staging_fd, size);
1000 if (fd->f_lte && size < fd->f_lte->resource_entry.original_size)
1001 fd->f_lte->resource_entry.original_size = size;
1006 * Fills in a `struct stat' that corresponds to a file or directory in the WIM.
1008 static int wimfs_getattr(const char *path, struct stat *stbuf)
1010 struct dentry *dentry;
1011 struct lookup_table_entry *lte;
1013 struct wimfs_context *ctx = wimfs_get_context();
1015 ret = lookup_resource(ctx->wim, path,
1016 get_lookup_flags(ctx) | LOOKUP_FLAG_DIRECTORY_OK,
1017 &dentry, <e, NULL);
1020 return inode_to_stbuf(dentry->d_inode, lte, stbuf);
1024 /* Read an alternate data stream through the XATTR interface, or get its size */
1025 static int wimfs_getxattr(const char *path, const char *name, char *value,
1029 struct inode *inode;
1030 struct ads_entry *ads_entry;
1032 struct lookup_table_entry *lte;
1033 struct wimfs_context *ctx = wimfs_get_context();
1035 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1038 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1042 inode = wim_pathname_to_inode(ctx->wim, path);
1046 ads_entry = inode_get_ads_entry(inode, name, NULL);
1050 lte = ads_entry->lte;
1051 res_size = wim_resource_size(lte);
1056 if (res_size > size)
1059 ret = read_full_wim_resource(lte, (u8*)value);
1067 /* Create a hard link */
1068 static int wimfs_link(const char *to, const char *from)
1070 struct dentry *from_dentry, *from_dentry_parent;
1071 const char *link_name;
1072 struct inode *inode;
1073 struct lookup_table_entry *lte;
1074 WIMStruct *w = wimfs_get_WIMStruct();
1076 inode = wim_pathname_to_inode(w, to);
1080 if (!inode_is_regular_file(inode))
1083 from_dentry_parent = get_parent_dentry(w, from);
1084 if (!from_dentry_parent)
1086 if (!dentry_is_directory(from_dentry_parent))
1089 link_name = path_basename(from);
1090 if (get_dentry_child_with_name(from_dentry_parent, link_name))
1092 from_dentry = new_dentry(link_name);
1097 inode_add_dentry(from_dentry, inode);
1098 from_dentry->d_inode = inode;
1099 inode->link_count++;
1101 for (unsigned i = 0; i <= inode->num_ads; i++) {
1102 lte = inode_stream_lte_resolved(inode, i);
1107 dentry_add_child(from_dentry_parent, from_dentry);
1112 static int wimfs_listxattr(const char *path, char *list, size_t size)
1115 struct inode *inode;
1116 struct wimfs_context *ctx = wimfs_get_context();
1118 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1121 /* List alternate data streams, or get the list size */
1123 inode = wim_pathname_to_inode(ctx->wim, path);
1129 for (u16 i = 0; i < inode->num_ads; i++)
1130 needed_size += inode->ads_entries[i].stream_name_utf8_len + 6;
1134 for (u16 i = 0; i < inode->num_ads; i++) {
1135 needed_size = inode->ads_entries[i].stream_name_utf8_len + 6;
1136 if (needed_size > size)
1138 p += sprintf(p, "user.%s",
1139 inode->ads_entries[i].stream_name_utf8) + 1;
1140 size -= needed_size;
1148 * Create a directory in the WIM.
1149 * @mode is currently ignored.
1151 static int wimfs_mkdir(const char *path, mode_t mode)
1153 struct dentry *parent;
1154 struct dentry *newdir;
1155 const char *basename;
1156 struct wimfs_context *ctx = wimfs_get_context();
1158 parent = get_parent_dentry(ctx->wim, path);
1162 if (!dentry_is_directory(parent))
1165 basename = path_basename(path);
1166 if (get_dentry_child_with_name(parent, basename))
1169 newdir = new_dentry_with_inode(basename);
1170 newdir->d_inode->attributes |= FILE_ATTRIBUTE_DIRECTORY;
1171 newdir->d_inode->resolved = true;
1172 newdir->d_inode->ino = ctx->next_ino++;
1173 dentry_add_child(parent, newdir);
1177 /* Creates a regular file. */
1178 static int wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
1180 const char *stream_name;
1181 struct wimfs_context *ctx = wimfs_get_context();
1182 if ((ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
1183 && (stream_name = path_stream_name(path))) {
1184 /* Make an alternate data stream */
1185 struct ads_entry *new_entry;
1186 struct inode *inode;
1188 char *p = (char*)stream_name - 1;
1189 wimlib_assert(*p == ':');
1192 inode = wim_pathname_to_inode(ctx->wim, path);
1195 if (!inode_is_regular_file(inode))
1197 if (inode_get_ads_entry(inode, stream_name, NULL))
1199 new_entry = inode_add_ads(inode, stream_name);
1203 struct dentry *dentry, *parent;
1204 const char *basename;
1206 /* Make a normal file (not an alternate data stream) */
1208 /* Make sure that the parent of @path exists and is a directory, and
1209 * that the dentry named by @path does not already exist. */
1210 parent = get_parent_dentry(ctx->wim, path);
1213 if (!dentry_is_directory(parent))
1216 basename = path_basename(path);
1217 if (get_dentry_child_with_name(parent, path))
1220 dentry = new_dentry_with_inode(basename);
1223 dentry->d_inode->resolved = true;
1224 dentry->d_inode->ino = ctx->next_ino++;
1225 dentry_add_child(parent, dentry);
1232 static int wimfs_open(const char *path, struct fuse_file_info *fi)
1234 struct dentry *dentry;
1235 struct lookup_table_entry *lte;
1237 struct wimlib_fd *fd;
1238 struct inode *inode;
1241 struct wimfs_context *ctx = wimfs_get_context();
1243 ret = lookup_resource(ctx->wim, path, get_lookup_flags(ctx),
1244 &dentry, <e, &stream_idx);
1248 inode = dentry->d_inode;
1250 if (stream_idx == 0)
1253 stream_id = inode->ads_entries[stream_idx - 1].stream_id;
1255 /* The file resource may be in the staging directory (read-write mounts
1256 * only) or in the WIM. If it's in the staging directory, we need to
1257 * open a native file descriptor for the corresponding file. Otherwise,
1258 * we can read the file resource directly from the WIM file if we are
1259 * opening it read-only, but we need to extract the resource to the
1260 * staging directory if we are opening it writable. */
1262 if (flags_writable(fi->flags) &&
1263 (!lte || lte->resource_location != RESOURCE_IN_STAGING_FILE)) {
1264 u64 size = (lte) ? wim_resource_size(lte) : 0;
1265 ret = extract_resource_to_staging_dir(inode, stream_id,
1271 ret = alloc_wimlib_fd(inode, stream_id, lte, &fd);
1275 if (lte && lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1276 fd->staging_fd = open(lte->staging_file_name, fi->flags);
1277 if (fd->staging_fd == -1) {
1278 close_wimlib_fd(fd);
1282 fi->fh = (uintptr_t)fd;
1286 /* Opens a directory. */
1287 static int wimfs_opendir(const char *path, struct fuse_file_info *fi)
1289 struct inode *inode;
1291 struct wimlib_fd *fd = NULL;
1292 WIMStruct *w = wimfs_get_WIMStruct();
1294 inode = wim_pathname_to_inode(w, path);
1297 if (!inode_is_directory(inode))
1299 ret = alloc_wimlib_fd(inode, 0, NULL, &fd);
1300 fi->fh = (uintptr_t)fd;
1306 * Read data from a file in the WIM or in the staging directory.
1308 static int wimfs_read(const char *path, char *buf, size_t size,
1309 off_t offset, struct fuse_file_info *fi)
1311 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1316 if (!fd->f_lte) /* Empty stream with no lookup table entry */
1319 if (fd->f_lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1320 /* Read from staging file */
1322 wimlib_assert(fd->f_lte->staging_file_name);
1323 wimlib_assert(fd->staging_fd != -1);
1326 DEBUG("Seek to offset %zu", offset);
1328 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1330 ret = read(fd->staging_fd, buf, size);
1337 u64 res_size = wim_resource_size(fd->f_lte);
1339 if (offset > res_size)
1342 size = min(size, res_size - offset);
1344 if (read_wim_resource(fd->f_lte, (u8*)buf,
1345 size, offset, false) != 0)
1351 struct fill_params {
1353 fuse_fill_dir_t filler;
1356 static int dentry_fuse_fill(struct dentry *dentry, void *arg)
1358 struct fill_params *fill_params = arg;
1359 return fill_params->filler(fill_params->buf, dentry->file_name_utf8,
1363 /* Fills in the entries of the directory specified by @path using the
1364 * FUSE-provided function @filler. */
1365 static int wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
1366 off_t offset, struct fuse_file_info *fi)
1368 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1369 struct inode *inode;
1374 inode = fd->f_inode;
1376 struct fill_params fill_params = {
1381 filler(buf, ".", NULL, 0);
1382 filler(buf, "..", NULL, 0);
1384 return for_dentry_in_rbtree(inode->children.rb_node,
1385 dentry_fuse_fill, &fill_params);
1389 static int wimfs_readlink(const char *path, char *buf, size_t buf_len)
1391 struct wimfs_context *ctx = wimfs_get_context();
1392 struct inode *inode = wim_pathname_to_inode(ctx->wim, path);
1396 if (!inode_is_symlink(inode))
1399 ret = inode_readlink(inode, buf, buf_len, ctx->wim);
1406 static int wimfs_release(const char *path, struct fuse_file_info *fi)
1408 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1409 return close_wimlib_fd(fd);
1412 /* Close a directory */
1413 static int wimfs_releasedir(const char *path, struct fuse_file_info *fi)
1415 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1416 return close_wimlib_fd(fd);
1420 /* Remove an alternate data stream through the XATTR interface */
1421 static int wimfs_removexattr(const char *path, const char *name)
1423 struct inode *inode;
1424 struct ads_entry *ads_entry;
1426 struct wimfs_context *ctx = wimfs_get_context();
1428 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1431 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1435 inode = wim_pathname_to_inode(ctx->wim, path);
1439 ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
1442 inode_remove_ads(inode, ads_idx, ctx->wim->lookup_table);
1447 /* Renames a file or directory. See rename (3) */
1448 static int wimfs_rename(const char *from, const char *to)
1452 struct dentry *parent_of_dst;
1453 char *file_name_utf16 = NULL, *file_name_utf8 = NULL;
1454 u16 file_name_utf16_len, file_name_utf8_len;
1455 WIMStruct *w = wimfs_get_WIMStruct();
1458 /* This rename() implementation currently only supports actual files
1459 * (not alternate data streams) */
1461 src = get_dentry(w, from);
1465 dst = get_dentry(w, to);
1468 ret = get_names(&file_name_utf16, &file_name_utf8,
1469 &file_name_utf16_len, &file_name_utf8_len,
1475 /* Destination file exists */
1477 if (src == dst) /* Same file */
1480 if (!dentry_is_directory(src)) {
1481 /* Cannot rename non-directory to directory. */
1482 if (dentry_is_directory(dst))
1485 /* Cannot rename directory to a non-directory or a non-empty
1487 if (!dentry_is_directory(dst))
1489 if (inode_has_children(dst->d_inode))
1492 parent_of_dst = dst->parent;
1493 remove_dentry(dst, w->lookup_table);
1495 /* Destination does not exist */
1496 parent_of_dst = get_parent_dentry(w, to);
1500 if (!dentry_is_directory(parent_of_dst))
1504 FREE(src->file_name);
1505 FREE(src->file_name_utf8);
1506 src->file_name = file_name_utf16;
1507 src->file_name_utf8 = file_name_utf8;
1508 src->file_name_len = file_name_utf16_len;
1509 src->file_name_utf8_len = file_name_utf8_len;
1512 dentry_add_child(parent_of_dst, src);
1516 /* Remove a directory */
1517 static int wimfs_rmdir(const char *path)
1519 struct dentry *dentry;
1520 WIMStruct *w = wimfs_get_WIMStruct();
1522 dentry = get_dentry(w, path);
1526 if (!dentry_is_empty_directory(dentry))
1529 remove_dentry(dentry, w->lookup_table);
1534 /* Write an alternate data stream through the XATTR interface */
1535 static int wimfs_setxattr(const char *path, const char *name,
1536 const char *value, size_t size, int flags)
1538 struct ads_entry *existing_ads_entry;
1539 struct ads_entry *new_ads_entry;
1540 struct lookup_table_entry *existing_lte;
1541 struct lookup_table_entry *lte;
1542 struct inode *inode;
1543 u8 value_hash[SHA1_HASH_SIZE];
1545 struct wimfs_context *ctx = wimfs_get_context();
1547 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1550 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1554 inode = wim_pathname_to_inode(ctx->wim, path);
1558 existing_ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
1559 if (existing_ads_entry) {
1560 if (flags & XATTR_CREATE)
1562 inode_remove_ads(inode, ads_idx, ctx->wim->lookup_table);
1564 if (flags & XATTR_REPLACE)
1567 new_ads_entry = inode_add_ads(inode, name);
1571 sha1_buffer((const u8*)value, size, value_hash);
1573 existing_lte = __lookup_resource(ctx->wim->lookup_table, value_hash);
1580 lte = new_lookup_table_entry();
1583 value_copy = MALLOC(size);
1588 memcpy(value_copy, value, size);
1589 lte->resource_location = RESOURCE_IN_ATTACHED_BUFFER;
1590 lte->attached_buffer = value_copy;
1591 lte->resource_entry.original_size = size;
1592 lte->resource_entry.size = size;
1593 lte->resource_entry.flags = 0;
1594 copy_hash(lte->hash, value_hash);
1595 lookup_table_insert(ctx->wim->lookup_table, lte);
1597 new_ads_entry->lte = lte;
1602 static int wimfs_symlink(const char *to, const char *from)
1604 struct dentry *dentry_parent, *dentry;
1605 const char *link_name;
1606 struct inode *inode;
1607 struct wimfs_context *ctx = wimfs_get_context();
1609 dentry_parent = get_parent_dentry(ctx->wim, from);
1612 if (!dentry_is_directory(dentry_parent))
1615 link_name = path_basename(from);
1617 if (get_dentry_child_with_name(dentry_parent, link_name))
1619 dentry = new_dentry_with_inode(link_name);
1622 inode = dentry->d_inode;
1624 inode->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
1625 inode->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
1626 inode->ino = ctx->next_ino++;
1627 inode->resolved = true;
1629 if (inode_set_symlink(inode, to, ctx->wim->lookup_table, NULL) != 0)
1630 goto out_free_dentry;
1632 dentry_add_child(dentry_parent, dentry);
1635 free_dentry(dentry);
1640 /* Reduce the size of a file */
1641 static int wimfs_truncate(const char *path, off_t size)
1643 struct dentry *dentry;
1644 struct lookup_table_entry *lte;
1648 struct inode *inode;
1649 struct wimfs_context *ctx = wimfs_get_context();
1651 ret = lookup_resource(ctx->wim, path, get_lookup_flags(ctx),
1652 &dentry, <e, &stream_idx);
1657 if (!lte) /* Already a zero-length file */
1660 inode = dentry->d_inode;
1662 if (stream_idx == 0)
1665 stream_id = inode->ads_entries[stream_idx - 1].stream_id;
1667 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1668 wimlib_assert(lte->staging_file_name);
1669 ret = truncate(lte->staging_file_name, size);
1672 lte->resource_entry.original_size = size;
1674 wimlib_assert(lte->resource_location == RESOURCE_IN_WIM);
1675 /* File in WIM. Extract it to the staging directory, but only
1676 * the first @size bytes of it. */
1677 ret = extract_resource_to_staging_dir(inode, stream_id,
1683 /* Remove a regular file */
1684 static int wimfs_unlink(const char *path)
1686 struct dentry *dentry;
1687 struct lookup_table_entry *lte;
1690 struct wimfs_context *ctx = wimfs_get_context();
1692 ret = lookup_resource(ctx->wim, path, get_lookup_flags(ctx),
1693 &dentry, <e, &stream_idx);
1698 if (stream_idx == 0)
1699 remove_dentry(dentry, ctx->wim->lookup_table);
1701 inode_remove_ads(dentry->d_inode, stream_idx - 1,
1702 ctx->wim->lookup_table);
1706 #ifdef HAVE_UTIMENSAT
1708 * Change the timestamp on a file dentry.
1710 * Note that alternate data streams do not have their own timestamps.
1712 static int wimfs_utimens(const char *path, const struct timespec tv[2])
1714 struct dentry *dentry;
1715 struct inode *inode;
1716 WIMStruct *w = wimfs_get_WIMStruct();
1718 dentry = get_dentry(w, path);
1721 inode = dentry->d_inode;
1723 if (tv[0].tv_nsec != UTIME_OMIT) {
1724 if (tv[0].tv_nsec == UTIME_NOW)
1725 inode->last_access_time = get_wim_timestamp();
1727 inode->last_access_time = timespec_to_wim_timestamp(&tv[0]);
1729 if (tv[1].tv_nsec != UTIME_OMIT) {
1730 if (tv[1].tv_nsec == UTIME_NOW)
1731 inode->last_write_time = get_wim_timestamp();
1733 inode->last_write_time = timespec_to_wim_timestamp(&tv[1]);
1738 static int wimfs_utime(const char *path, struct utimbuf *times)
1740 struct dentry *dentry;
1741 struct inode *inode;
1742 WIMStruct *w = wimfs_get_WIMStruct();
1744 dentry = get_dentry(w, path);
1747 inode = dentry->d_inode;
1749 inode->last_write_time = unix_timestamp_to_wim(times->modtime);
1750 inode->last_access_time = unix_timestamp_to_wim(times->actime);
1755 /* Writes to a file in the WIM filesystem.
1756 * It may be an alternate data stream, but here we don't even notice because we
1757 * just get a lookup table entry. */
1758 static int wimfs_write(const char *path, const char *buf, size_t size,
1759 off_t offset, struct fuse_file_info *fi)
1761 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1768 wimlib_assert(fd->f_lte);
1769 wimlib_assert(fd->f_lte->staging_file_name);
1770 wimlib_assert(fd->staging_fd != -1);
1771 wimlib_assert(fd->f_inode);
1773 /* Seek to the requested position */
1774 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1777 /* Write the data. */
1778 ret = write(fd->staging_fd, buf, size);
1782 now = get_wim_timestamp();
1783 fd->f_inode->last_write_time = now;
1784 fd->f_inode->last_access_time = now;
1788 static struct fuse_operations wimfs_operations = {
1789 .access = wimfs_access,
1790 .chmod = wimfs_chmod,
1791 .destroy = wimfs_destroy,
1793 .fallocate = wimfs_fallocate,
1795 .fgetattr = wimfs_fgetattr,
1796 .ftruncate = wimfs_ftruncate,
1797 .getattr = wimfs_getattr,
1799 .getxattr = wimfs_getxattr,
1803 .listxattr = wimfs_listxattr,
1805 .mkdir = wimfs_mkdir,
1806 .mknod = wimfs_mknod,
1808 .opendir = wimfs_opendir,
1810 .readdir = wimfs_readdir,
1811 .readlink = wimfs_readlink,
1812 .release = wimfs_release,
1813 .releasedir = wimfs_releasedir,
1815 .removexattr = wimfs_removexattr,
1817 .rename = wimfs_rename,
1818 .rmdir = wimfs_rmdir,
1820 .setxattr = wimfs_setxattr,
1822 .symlink = wimfs_symlink,
1823 .truncate = wimfs_truncate,
1824 .unlink = wimfs_unlink,
1825 #ifdef HAVE_UTIMENSAT
1826 .utimens = wimfs_utimens,
1828 .utime = wimfs_utime,
1830 .write = wimfs_write,
1834 /* Mounts a WIM file. */
1835 WIMLIBAPI int wimlib_mount(WIMStruct *wim, int image, const char *dir,
1836 int flags, WIMStruct **additional_swms,
1837 unsigned num_additional_swms)
1843 struct lookup_table *joined_tab, *wim_tab_save;
1844 struct image_metadata *imd;
1845 struct wimfs_context ctx;
1847 DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
1848 wim, image, dir, flags);
1851 return WIMLIB_ERR_INVALID_PARAM;
1853 ret = verify_swm_set(wim, additional_swms, num_additional_swms);
1857 if ((flags & WIMLIB_MOUNT_FLAG_READWRITE) && (wim->hdr.total_parts != 1)) {
1858 ERROR("Cannot mount a split WIM read-write");
1859 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
1862 if (num_additional_swms) {
1863 ret = new_joined_lookup_table(wim, additional_swms,
1864 num_additional_swms,
1868 wim_tab_save = wim->lookup_table;
1869 wim->lookup_table = joined_tab;
1872 ret = select_wim_image(wim, image);
1877 imd = &wim->image_metadata[image - 1];
1879 DEBUG("Selected image %d", image);
1881 if (imd->root_dentry->refcnt != 1) {
1882 ERROR("Cannot mount image that was just exported with "
1884 ret = WIMLIB_ERR_INVALID_PARAM;
1888 if (!(flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
1889 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
1890 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
1891 flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1894 DEBUG("Initializing struct wimfs_context");
1895 init_wimfs_context(&ctx);
1897 DEBUG("Getting current directory");
1898 ctx.working_directory = getcwd(NULL, 0);
1899 if (!ctx.working_directory) {
1900 ERROR_WITH_ERRNO("Could not determine current directory");
1901 ret = WIMLIB_ERR_NOTDIR;
1905 DEBUG("Unlinking message queues in case they already exist");
1906 ret = set_message_queue_names(&ctx, dir);
1908 goto out_free_working_directory;
1909 unlink_message_queues(&ctx);
1911 DEBUG("Preparing arguments to fuse_main()");
1913 dir_copy = STRDUP(dir);
1915 goto out_free_message_queue_names;
1917 argv[argc++] = "imagex";
1918 argv[argc++] = dir_copy;
1920 /* disable multi-threaded operation for read-write mounts */
1921 if (flags & WIMLIB_MOUNT_FLAG_READWRITE)
1922 argv[argc++] = "-s";
1924 if (flags & WIMLIB_MOUNT_FLAG_DEBUG)
1925 argv[argc++] = "-d";
1928 * We provide the use_ino option because we are going to assign inode
1929 * numbers oursides. The inodes will be given unique numbers in the
1930 * assign_inode_numbers() function, and the static variable @next_ino is
1931 * set to the next available inode number.
1933 char optstring[256] = "use_ino,subtype=wimfs,attr_timeout=0";
1934 argv[argc++] = "-o";
1935 argv[argc++] = optstring;
1936 if ((flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
1937 /* Read-write mount. Make the staging directory */
1938 ret = make_staging_dir(&ctx);
1940 goto out_free_dir_copy;
1942 /* Read-only mount */
1943 strcat(optstring, ",ro");
1950 DEBUG("FUSE command line (argc = %d): ", argc);
1951 for (i = 0; i < argc; i++) {
1952 fputs(argv[i], stdout);
1960 /* Mark dentry tree as modified if read-write mount. */
1961 if (flags & WIMLIB_MOUNT_FLAG_READWRITE)
1962 imd->modified = true;
1964 /* Resolve all the lookup table entries of the dentry tree */
1965 DEBUG("Resolving lookup table entries");
1966 for_dentry_in_tree(imd->root_dentry, dentry_resolve_ltes,
1969 ctx.next_ino = assign_inode_numbers(&imd->inode_list);
1970 DEBUG("(next_ino = %"PRIu64")", ctx.next_ino);
1972 /* Finish initializing the filesystem context. */
1974 ctx.mount_flags = flags;
1976 DEBUG("Calling fuse_main()");
1978 ret = fuse_main(argc, argv, &wimfs_operations, &ctx);
1980 DEBUG("Returned from fuse_main() (ret = %d)", ret);
1982 ret = WIMLIB_ERR_FUSE;
1985 out_free_message_queue_names:
1986 free_message_queue_names(&ctx);
1987 out_free_working_directory:
1988 FREE(ctx.working_directory);
1989 ctx.working_directory = NULL;
1991 if (num_additional_swms) {
1992 free_lookup_table(wim->lookup_table);
1993 wim->lookup_table = wim_tab_save;
2000 * Unmounts the WIM file that was previously mounted on @dir by using
2003 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
2010 struct timespec timeout;
2012 struct wimfs_context ctx;
2015 init_wimfs_context(&ctx);
2017 /* Open message queues between the unmount process and the
2018 * filesystem daemon. */
2019 ret = set_message_queue_names(&ctx, dir);
2023 ret = open_message_queues(&ctx, false);
2025 goto out_free_message_queue_names;
2027 /* Send a message to the filesystem daemon saying whether to commit or
2029 msg[0] = (flags & WIMLIB_UNMOUNT_FLAG_COMMIT) ? 1 : 0;
2030 msg[1] = (flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY) ? 1 : 0;
2032 DEBUG("Sending message: %scommit, %scheck",
2033 (msg[0] ? "" : "don't "),
2034 (msg[1] ? "" : "don't "));
2035 ret = mq_send(ctx.unmount_to_daemon_mq, msg, 2, 1);
2037 ERROR_WITH_ERRNO("Failed to notify filesystem daemon whether "
2038 "we want to commit changes or not");
2039 ret = WIMLIB_ERR_MQUEUE;
2040 goto out_close_message_queues;
2043 /* Execute `fusermount -u', which is installed setuid root, to unmount
2046 * FUSE does not yet implement synchronous unmounts. This means that
2047 * fusermount -u will return before the filesystem daemon returns from
2048 * wimfs_destroy(). This is partly what we want, because we need to
2049 * send a message from this process to the filesystem daemon telling
2050 * whether --commit was specified or not. However, after that, the
2051 * unmount process must wait for the filesystem daemon to finish writing
2057 ERROR_WITH_ERRNO("Failed to fork()");
2058 ret = WIMLIB_ERR_FORK;
2059 goto out_close_message_queues;
2063 execlp("fusermount", "fusermount", "-u", dir, NULL);
2064 ERROR_WITH_ERRNO("Failed to execute `fusermount'");
2065 exit(WIMLIB_ERR_FUSERMOUNT);
2069 ret = wait(&status);
2071 ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
2073 ret = WIMLIB_ERR_FUSERMOUNT;
2074 goto out_close_message_queues;
2078 ERROR("fusermount exited with status %d", status);
2080 /* Try again, but with the `umount' program. This is required
2081 * on other FUSE implementations such as FreeBSD's that do not
2082 * have a `fusermount' program. */
2086 ERROR_WITH_ERRNO("Failed to fork()");
2087 ret = WIMLIB_ERR_FORK;
2088 goto out_close_message_queues;
2092 execlp("umount", "umount", dir, NULL);
2093 ERROR_WITH_ERRNO("Failed to execute `umount'");
2094 exit(WIMLIB_ERR_FUSERMOUNT);
2098 ret = wait(&status);
2100 ERROR_WITH_ERRNO("Failed to wait for `umount' process to "
2102 ret = WIMLIB_ERR_FUSERMOUNT;
2103 goto out_close_message_queues;
2106 ERROR("`umount' exited with failure status");
2107 ret = WIMLIB_ERR_FUSERMOUNT;
2108 goto out_close_message_queues;
2112 wimlib_assert(status == 0);
2114 /* Wait for a message from the filesytem daemon indicating whether the
2115 * filesystem was unmounted successfully (0) or an error occurred (1).
2116 * This may take a long time if a big WIM file needs to be rewritten. */
2118 /* Wait at most 600??? seconds before giving up and returning false.
2119 * Either it's a really big WIM file, or (more likely) the
2120 * filesystem daemon has crashed or failed for some reason.
2122 * XXX come up with some method to determine if the filesystem
2123 * daemon has really crashed or not.
2125 * XXX Idea: have mount daemon write its PID into the WIM file header?
2126 * No, this wouldn't work because we know the directory but not the WIM
2130 gettimeofday(&now, NULL);
2131 timeout.tv_sec = now.tv_sec + 600;
2132 timeout.tv_nsec = now.tv_usec * 1000;
2134 ret = get_mailbox(ctx.daemon_to_unmount_mq, 2, &msgsize, &mailbox);
2136 goto out_close_message_queues;
2139 DEBUG("Waiting for message telling us whether the unmount was "
2140 "successful or not.");
2141 ret = mq_timedreceive(ctx.daemon_to_unmount_mq, mailbox,
2142 msgsize, NULL, &timeout);
2144 if (errno == ETIMEDOUT) {
2145 ERROR("Timed out- probably the filesystem daemon "
2146 "crashed and the WIM was not written "
2148 ret = WIMLIB_ERR_TIMEOUT;
2150 ERROR_WITH_ERRNO("mq_receive()");
2151 ret = WIMLIB_ERR_MQUEUE;
2153 goto out_free_mailbox;
2156 DEBUG("Received message: Unmount %s", (mailbox[0] ? "Failed" : "Ok"));
2159 ERROR("Unmount failed");
2162 out_close_message_queues:
2163 close_message_queues(&ctx);
2164 out_free_message_queue_names:
2165 free_message_queue_names(&ctx);
2170 #else /* WITH_FUSE */
2173 static inline int mount_unsupported_error()
2175 ERROR("wimlib was compiled with --without-fuse, which disables support "
2176 "for mounting WIMs.");
2177 return WIMLIB_ERR_UNSUPPORTED;
2180 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
2182 return mount_unsupported_error();
2185 WIMLIBAPI int wimlib_mount(WIMStruct *wim_p, int image, const char *dir,
2186 int flags, WIMStruct **additional_swms,
2187 unsigned num_additional_swms)
2189 return mount_unsupported_error();
2192 #endif /* WITH_FUSE */