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;
150 DEBUG("Allocating fd for stream ID %u from inode %lx (open = %u, allocated = %u)",
151 stream_id, inode->ino, inode->num_opened_fds,
152 inode->num_allocated_fds);
154 if (inode->num_opened_fds == inode->num_allocated_fds) {
155 struct wimlib_fd **fds;
158 if (inode->num_allocated_fds == max_fds)
160 num_new_fds = min(fds_per_alloc,
161 max_fds - inode->num_allocated_fds);
163 fds = REALLOC(inode->fds,
164 (inode->num_allocated_fds + num_new_fds) *
165 sizeof(inode->fds[0]));
168 memset(&fds[inode->num_allocated_fds], 0,
169 num_new_fds * sizeof(fds[0]));
171 inode->num_allocated_fds += num_new_fds;
173 for (u16 i = 0; ; i++) {
174 if (!inode->fds[i]) {
175 struct wimlib_fd *fd = CALLOC(1, sizeof(*fd));
182 fd->stream_id = stream_id;
185 inode->num_opened_fds++;
187 lte->num_opened_fds++;
188 DEBUG("Allocated fd");
194 static void inode_put_fd(struct inode *inode, struct wimlib_fd *fd)
197 wimlib_assert(inode);
198 wimlib_assert(fd->f_inode == inode);
199 wimlib_assert(inode->num_opened_fds);
200 wimlib_assert(fd->idx < inode->num_opened_fds);
201 wimlib_assert(inode->fds[fd->idx] == fd);
203 inode->fds[fd->idx] = NULL;
205 if (--inode->num_opened_fds == 0 && inode->link_count == 0)
209 static int lte_put_fd(struct lookup_table_entry *lte, struct wimlib_fd *fd)
212 wimlib_assert(fd->f_lte == lte);
214 if (!lte) /* Empty stream with no lookup table entry */
217 /* Close staging file descriptor if needed. */
219 if (lte->resource_location == RESOURCE_IN_STAGING_FILE
220 && fd->staging_fd != -1)
222 if (close(fd->staging_fd) != 0) {
223 ERROR_WITH_ERRNO("Failed to close staging file");
227 lte_decrement_num_opened_fds(lte);
231 /* Close a file descriptor. */
232 static int close_wimlib_fd(struct wimlib_fd *fd)
236 DEBUG("Closing fd (inode = %lu, opened = %u, allocated = %u)",
237 fd->f_inode->ino, fd->f_inode->num_opened_fds,
238 fd->f_inode->num_allocated_fds);
239 ret = lte_put_fd(fd->f_lte, fd);
243 inode_put_fd(fd->f_inode, fd);
247 /* Remove a dentry; i.e. remove a reference to the corresponding inode.
249 * If there are no remaining references to the inode either through dentries or
250 * open file descriptors, the inode is freed. Otherwise, the inode is not
251 * removed, but the dentry is unlinked and freed.
253 * Either way, all lookup table entries referenced by the inode have their
254 * reference count decremented. If a lookup table entry has no open file
255 * descriptors and no references remaining, it is freed, and the staging file is
258 static void remove_dentry(struct dentry *dentry,
259 struct lookup_table *lookup_table)
261 struct inode *inode = dentry->d_inode;
262 struct lookup_table_entry *lte;
265 for (i = 0; i <= inode->num_ads; i++) {
266 lte = inode_stream_lte_resolved(inode, i);
268 lte_decrement_refcnt(lte, lookup_table);
270 unlink_dentry(dentry);
274 /* Creates a new staging file and returns its file descriptor opened for
277 * @name_ret: A location into which the a pointer to the newly allocated name of
278 * the staging file is stored.
279 * @return: The file descriptor for the new file. Returns -1 and sets errno on
280 * error, for any reason possible from the creat() function.
282 static int create_staging_file(char **name_ret, int open_flags,
283 struct wimfs_context *ctx)
291 name_len = ctx->staging_dir_name_len + 1 + SHA1_HASH_SIZE;
292 name = MALLOC(name_len + 1);
300 memcpy(name, ctx->staging_dir_name, ctx->staging_dir_name_len);
301 name[ctx->staging_dir_name_len] = '/';
302 randomize_char_array_with_alnum(name + ctx->staging_dir_name_len + 1,
304 name[name_len] = '\0';
307 /* Just in case, verify that the randomly generated name doesn't name an
308 * existing file, and try again if so */
309 } while (stat(name, &stbuf) == 0);
315 /* doesn't exist--- ok */
317 DEBUG("Creating staging file `%s'", name);
319 fd = open(name, open_flags | O_CREAT | O_TRUNC, 0600);
331 * Extract a WIM resource to the staging directory.
333 * @inode: Inode that contains the stream we are extracting
335 * @stream_id: Identifier for the stream (it stays constant even if the indices
336 * of the stream entries are changed)
338 * @lte: Pointer to pointer to the lookup table entry for the stream we need to
339 * extract, or NULL if there was no lookup table entry present for the stream
341 * @size: Number of bytes of the stream we want to extract (this supports the
342 * wimfs_truncate() function).
344 static int extract_resource_to_staging_dir(struct inode *inode,
346 struct lookup_table_entry **lte,
348 struct wimfs_context *ctx)
350 char *staging_file_name;
353 struct lookup_table_entry *old_lte, *new_lte;
355 DEBUG("Extracting resource to staging dir: inode %"PRIu64", "
356 "stream id %"PRIu32, inode->ino, stream_id);
359 fd = create_staging_file(&staging_file_name, O_WRONLY, ctx);
364 ret = extract_wim_resource_to_fd(old_lte, fd, size);
367 if (ret != 0 || close(fd) != 0) {
373 goto out_delete_staging_file;
376 if (old_lte && inode->link_count == old_lte->refcnt) {
377 /* The reference count of the existing lookup table
378 * entry is the same as the link count of the inode that
379 * contains the stream we're opening. Therefore, ALL
380 * the references to the lookup table entry correspond
381 * to the stream we're trying to extract, so the lookup
382 * table entry can be re-used. */
383 DEBUG("Re-using lookup table entry");
384 lookup_table_unlink(ctx->wim->lookup_table, old_lte);
388 /* There's an existing lookup table entry, but its
389 * reference count is creater than the link count for
390 * the inode containing a stream we're opening.
391 * Therefore, we need to split the lookup table entry.
393 wimlib_assert(old_lte->refcnt > inode->link_count);
394 DEBUG("Splitting lookup table entry "
395 "(inode->link_count = %u, old_lte->refcnt = %u)",
396 inode->link_count, old_lte->refcnt);
400 new_lte = new_lookup_table_entry();
403 goto out_delete_staging_file;
406 /* There may already be open file descriptors to this stream if
407 * it's previously been opened read-only, but just now we're
408 * opening it read-write. Identify those file descriptors and
409 * change their lookup table entry pointers to point to the new
410 * lookup table entry, and open staging file descriptors for
413 * At the same time, we need to count the number of these opened
414 * file descriptors to the new lookup table entry. If there's
415 * an old lookup table entry, this number needs to be subtracted
416 * from the fd's opened to the old entry. */
417 for (u16 i = 0, j = 0; j < inode->num_opened_fds; i++) {
418 struct wimlib_fd *fd = inode->fds[i];
420 if (fd->stream_id == stream_id) {
421 wimlib_assert(fd->f_lte == old_lte);
422 wimlib_assert(fd->staging_fd == -1);
424 new_lte->num_opened_fds++;
425 fd->staging_fd = open(staging_file_name, O_RDONLY);
426 if (fd->staging_fd == -1) {
428 goto out_revert_fd_changes;
434 DEBUG("%hu fd's were already opened to the file we extracted",
435 new_lte->num_opened_fds);
437 old_lte->num_opened_fds -= new_lte->num_opened_fds;
438 old_lte->refcnt -= inode->link_count;
442 new_lte->resource_entry.original_size = size;
443 new_lte->refcnt = inode->link_count;
444 new_lte->resource_location = RESOURCE_IN_STAGING_FILE;
445 new_lte->staging_file_name = staging_file_name;
446 new_lte->lte_inode = inode;
447 random_hash(new_lte->hash);
450 inode->lte = new_lte;
452 for (u16 i = 0; i < inode->num_ads; i++)
453 if (inode->ads_entries[i].stream_id == stream_id)
454 inode->ads_entries[i].lte = new_lte;
456 lookup_table_insert(ctx->wim->lookup_table, new_lte);
457 list_add(&new_lte->staging_list, &ctx->staging_list);
460 out_revert_fd_changes:
461 for (u16 i = 0, j = 0; j < new_lte->num_opened_fds; i++) {
462 struct wimlib_fd *fd = inode->fds[i];
463 if (fd && fd->stream_id == stream_id && fd->f_lte == new_lte) {
465 if (fd->staging_fd != -1) {
466 close(fd->staging_fd);
472 free_lookup_table_entry(new_lte);
473 out_delete_staging_file:
474 unlink(staging_file_name);
475 FREE(staging_file_name);
480 * Creates a randomly named staging directory and returns its name into the
481 * static variable staging_dir_name.
483 * If the staging directory cannot be created, staging_dir_name is set to NULL.
485 static int make_staging_dir(struct wimfs_context *ctx)
487 /* XXX Give the user an option of where to stage files */
489 static const char prefix[] = "wimlib-staging-";
490 static const size_t prefix_len = sizeof(prefix) - 1;
491 static const size_t random_suffix_len = 10;
493 size_t pwd_len = strlen(ctx->working_directory);
495 ctx->staging_dir_name_len = pwd_len + 1 + prefix_len + random_suffix_len;
497 ctx->staging_dir_name = MALLOC(ctx->staging_dir_name_len + 1);
498 if (!ctx->staging_dir_name)
499 return WIMLIB_ERR_NOMEM;
501 memcpy(ctx->staging_dir_name, ctx->working_directory, pwd_len);
502 ctx->staging_dir_name[pwd_len] = '/';
503 memcpy(ctx->staging_dir_name + pwd_len + 1, prefix, prefix_len);
504 randomize_char_array_with_alnum(ctx->staging_dir_name + pwd_len +
505 1 + prefix_len, random_suffix_len);
506 ctx->staging_dir_name[ctx->staging_dir_name_len] = '\0';
508 if (mkdir(ctx->staging_dir_name, 0700) != 0) {
509 ERROR_WITH_ERRNO("Failed to create temporary directory `%s'",
510 ctx->staging_dir_name);
511 FREE(ctx->staging_dir_name);
512 ctx->staging_dir_name = NULL;
513 return WIMLIB_ERR_MKDIR;
518 static int remove_file_or_directory(const char *fpath, const struct stat *sb,
519 int typeflag, struct FTW *ftwbuf)
521 if (remove(fpath) == 0)
524 ERROR_WITH_ERRNO("Cannot remove `%s'", fpath);
525 return WIMLIB_ERR_DELETE_STAGING_DIR;
531 * Deletes the staging directory and all the files contained in it.
533 static int delete_staging_dir(struct wimfs_context *ctx)
536 wimlib_assert(ctx->staging_dir_name != NULL);
537 ret = nftw(ctx->staging_dir_name, remove_file_or_directory,
539 FREE(ctx->staging_dir_name);
540 ctx->staging_dir_name = NULL;
545 /* Simple function that returns the concatenation of 2 strings. */
546 static char *strcat_dup(const char *s1, const char *s2, size_t max_len)
548 size_t len = strlen(s1) + strlen(s2);
551 char *p = MALLOC(len + 1);
554 snprintf(p, len + 1, "%s%s", s1, s2);
558 static int set_message_queue_names(struct wimfs_context *ctx,
559 const char *mount_dir)
561 static const char *u2d_prefix = "/wimlib-unmount-to-daemon-mq";
562 static const char *d2u_prefix = "/wimlib-daemon-to-unmount-mq";
567 dir_path = realpath(mount_dir, NULL);
569 ERROR_WITH_ERRNO("Failed to resolve path \"%s\"", mount_dir);
570 return WIMLIB_ERR_NOTDIR;
573 DEBUG("Using absolute dir_path = `%s'", dir_path);
583 ctx->unmount_to_daemon_mq_name = strcat_dup(u2d_prefix, dir_path,
585 if (!ctx->unmount_to_daemon_mq_name) {
586 ret = WIMLIB_ERR_NOMEM;
587 goto out_free_dir_path;
589 ctx->daemon_to_unmount_mq_name = strcat_dup(d2u_prefix, dir_path,
591 if (!ctx->daemon_to_unmount_mq_name) {
592 ret = WIMLIB_ERR_NOMEM;
593 goto out_free_unmount_to_daemon_mq_name;
597 goto out_free_dir_path;
598 out_free_unmount_to_daemon_mq_name:
599 FREE(ctx->unmount_to_daemon_mq_name);
600 ctx->unmount_to_daemon_mq_name = NULL;
606 static void free_message_queue_names(struct wimfs_context *ctx)
608 FREE(ctx->unmount_to_daemon_mq_name);
609 FREE(ctx->daemon_to_unmount_mq_name);
610 ctx->unmount_to_daemon_mq_name = NULL;
611 ctx->daemon_to_unmount_mq_name = NULL;
615 * Opens two POSIX message queue: one for sending messages from the unmount
616 * process to the daemon process, and one to go the other way. The names of the
617 * message queues, which must be system-wide unique, are be based on the mount
618 * point. (There of course is still a possibility of a collision if one were to
619 * unmount two identically named directories simultaneously...)
621 * @daemon specifies whether the calling process is the filesystem daemon or the
624 static int open_message_queues(struct wimfs_context *ctx, bool daemon)
629 wimlib_assert(ctx->unmount_to_daemon_mq_name != NULL &&
630 ctx->daemon_to_unmount_mq_name != NULL);
633 flags = O_RDONLY | O_CREAT;
635 flags = O_WRONLY | O_CREAT;
637 DEBUG("Opening message queue \"%s\"", ctx->unmount_to_daemon_mq_name);
638 ctx->unmount_to_daemon_mq = mq_open(ctx->unmount_to_daemon_mq_name,
641 if (ctx->unmount_to_daemon_mq == (mqd_t)-1) {
642 ERROR_WITH_ERRNO("mq_open()");
643 ret = WIMLIB_ERR_MQUEUE;
648 flags = O_WRONLY | O_CREAT;
650 flags = O_RDONLY | O_CREAT;
652 DEBUG("Opening message queue \"%s\"", ctx->daemon_to_unmount_mq_name);
653 ctx->daemon_to_unmount_mq = mq_open(ctx->daemon_to_unmount_mq_name,
656 if (ctx->daemon_to_unmount_mq == (mqd_t)-1) {
657 ERROR_WITH_ERRNO("mq_open()");
658 ret = WIMLIB_ERR_MQUEUE;
659 goto out_close_unmount_to_daemon_mq;
663 out_close_unmount_to_daemon_mq:
664 mq_close(ctx->unmount_to_daemon_mq);
665 mq_unlink(ctx->unmount_to_daemon_mq_name);
666 ctx->unmount_to_daemon_mq = (mqd_t)-1;
671 /* Try to determine the maximum message size of a message queue. The return
672 * value is the maximum message size, or a guess of 8192 bytes if it cannot be
674 static long mq_get_msgsize(mqd_t mq)
676 static const char *msgsize_max_file = "/proc/sys/fs/mqueue/msgsize_max";
681 if (mq_getattr(mq, &attr) == 0) {
682 msgsize = attr.mq_msgsize;
684 ERROR_WITH_ERRNO("mq_getattr()");
685 ERROR("Attempting to read %s", msgsize_max_file);
686 fp = fopen(msgsize_max_file, "rb");
688 if (fscanf(fp, "%ld", &msgsize) != 1) {
689 ERROR("Assuming message size of 8192");
694 ERROR_WITH_ERRNO("Failed to open the file `%s'",
696 ERROR("Assuming message size of 8192");
703 static int get_mailbox(mqd_t mq, long needed_msgsize, long *msgsize_ret,
709 msgsize = mq_get_msgsize(mq);
711 if (msgsize < needed_msgsize) {
712 ERROR("Message queue max size must be at least %ld!",
714 return WIMLIB_ERR_MQUEUE;
717 mailbox = MALLOC(msgsize);
719 ERROR("Failed to allocate %ld bytes for mailbox", msgsize);
720 return WIMLIB_ERR_NOMEM;
722 *msgsize_ret = msgsize;
723 *mailbox_ret = mailbox;
727 static void unlink_message_queues(struct wimfs_context *ctx)
729 mq_unlink(ctx->unmount_to_daemon_mq_name);
730 mq_unlink(ctx->daemon_to_unmount_mq_name);
733 /* Closes the message queues, which are allocated in static variables */
734 static void close_message_queues(struct wimfs_context *ctx)
736 mq_close(ctx->unmount_to_daemon_mq);
737 ctx->unmount_to_daemon_mq = (mqd_t)(-1);
738 mq_close(ctx->daemon_to_unmount_mq);
739 ctx->daemon_to_unmount_mq = (mqd_t)(-1);
740 unlink_message_queues(ctx);
743 static int wimfs_access(const char *path, int mask)
745 /* Permissions not implemented */
749 static void inode_update_lte_ptr(struct inode *inode,
750 struct lookup_table_entry *old_lte,
751 struct lookup_table_entry *new_lte)
753 if (inode->lte == old_lte) {
754 inode->lte = new_lte;
756 for (unsigned i = 0; i < inode->num_ads; i++) {
757 if (inode->ads_entries[i].lte == old_lte) {
758 inode->ads_entries[i].lte = new_lte;
765 static int update_lte_of_staging_file(struct lookup_table_entry *lte,
766 struct lookup_table *table)
768 struct lookup_table_entry *duplicate_lte;
770 u8 hash[SHA1_HASH_SIZE];
773 wimlib_assert(lte->resource_location == RESOURCE_IN_STAGING_FILE);
774 wimlib_assert(lte->staging_file_name);
776 ret = sha1sum(lte->staging_file_name, hash);
780 lookup_table_unlink(table, lte);
782 duplicate_lte = __lookup_resource(table, hash);
785 /* Merge duplicate lookup table entries */
786 duplicate_lte->refcnt += lte->refcnt;
787 list_del(<e->staging_list);
788 inode_update_lte_ptr(lte->lte_inode, lte, duplicate_lte);
789 free_lookup_table_entry(lte);
791 if (stat(lte->staging_file_name, &stbuf) != 0) {
792 ERROR_WITH_ERRNO("Failed to stat `%s'", lte->staging_file_name);
793 return WIMLIB_ERR_STAT;
795 if (stbuf.st_size == 0) {
796 /* Zero-length stream. No lookup table entry needed. */
797 inode_update_lte_ptr(lte->lte_inode, lte, NULL);
798 free_lookup_table_entry(lte);
800 wimlib_assert(<e->file_on_disk == <e->staging_file_name);
801 lte->resource_entry.original_size = stbuf.st_size;
802 lte->resource_entry.size = stbuf.st_size;
803 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
804 lte->lte_inode = NULL;
805 copy_hash(lte->hash, hash);
806 lookup_table_insert(table, lte);
812 static int inode_close_fds(struct inode *inode)
814 u16 num_opened_fds = inode->num_opened_fds;
815 for (u16 i = 0, j = 0; j < num_opened_fds; i++) {
816 struct wimlib_fd *fd = inode->fds[i];
818 wimlib_assert(fd->f_inode == inode);
819 int ret = close_wimlib_fd(fd);
828 /* Overwrites the WIM file, with changes saved. */
829 static int rebuild_wim(struct wimfs_context *ctx, bool check_integrity)
832 struct lookup_table_entry *lte, *tmp;
833 WIMStruct *w = ctx->wim;
836 DEBUG("Closing all staging file descriptors.");
837 list_for_each_entry(lte, &ctx->staging_list, staging_list) {
838 ret = inode_close_fds(lte->lte_inode);
843 DEBUG("Calculating SHA1 checksums for all new staging files.");
844 list_for_each_entry_safe(lte, tmp, &ctx->staging_list, staging_list) {
845 ret = update_lte_of_staging_file(lte, ctx->wim->lookup_table);
850 xml_update_image_info(w, w->current_image);
852 ret = wimlib_overwrite(w, check_integrity);
854 ERROR("Failed to commit changes");
860 /* Called when the filesystem is unmounted. */
861 static void wimfs_destroy(void *p)
863 /* For read-write mounts, the `imagex unmount' command, which is
864 * running in a separate process and is executing the
865 * wimlib_unmount() function, will send this process a byte
866 * through a message queue that indicates whether the --commit
867 * option was specified or not. */
870 struct timespec timeout;
872 ssize_t bytes_received;
875 char check_integrity;
878 struct wimfs_context *ctx = wimfs_get_context();
880 if (open_message_queues(ctx, true))
883 if (get_mailbox(ctx->unmount_to_daemon_mq, 2, &msgsize, &mailbox))
884 goto out_close_message_queues;
886 /* Wait at most 3 seconds before giving up and discarding changes. */
887 gettimeofday(&now, NULL);
888 timeout.tv_sec = now.tv_sec + 3;
889 timeout.tv_nsec = now.tv_usec * 1000;
890 DEBUG("Waiting for message telling us whether to commit or not, and "
891 "whether to include integrity checks.");
893 bytes_received = mq_timedreceive(ctx->unmount_to_daemon_mq, mailbox,
894 msgsize, NULL, &timeout);
895 if (bytes_received == -1) {
896 ERROR_WITH_ERRNO("mq_timedreceive()");
897 ERROR("Not committing.");
901 DEBUG("Received message: [%d %d]", mailbox[0], mailbox[1]);
903 check_integrity = mailbox[1];
907 if (ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
909 ret = chdir(ctx->working_directory);
911 status = rebuild_wim(ctx, (check_integrity != 0));
913 ERROR_WITH_ERRNO("chdir()");
914 status = WIMLIB_ERR_NOTDIR;
917 ret = delete_staging_dir(ctx);
919 ERROR("Failed to delete the staging directory");
924 DEBUG("Read-only mount");
926 DEBUG("Sending status %hhd", status);
927 ret = mq_send(ctx->daemon_to_unmount_mq, &status, 1, 1);
929 ERROR_WITH_ERRNO("Failed to send status to unmount process");
931 out_close_message_queues:
932 close_message_queues(ctx);
936 static int wimfs_fallocate(const char *path, int mode,
937 off_t offset, off_t len, struct fuse_file_info *fi)
939 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
940 wimlib_assert(fd->staging_fd != -1);
941 return fallocate(fd->staging_fd, mode, offset, len);
946 static int wimfs_fgetattr(const char *path, struct stat *stbuf,
947 struct fuse_file_info *fi)
949 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
950 return inode_to_stbuf(fd->f_inode, fd->f_lte, stbuf);
953 static int wimfs_ftruncate(const char *path, off_t size,
954 struct fuse_file_info *fi)
956 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
957 int ret = ftruncate(fd->staging_fd, size);
960 if (fd->f_lte && size < fd->f_lte->resource_entry.original_size)
961 fd->f_lte->resource_entry.original_size = size;
966 * Fills in a `struct stat' that corresponds to a file or directory in the WIM.
968 static int wimfs_getattr(const char *path, struct stat *stbuf)
970 struct dentry *dentry;
971 struct lookup_table_entry *lte;
973 struct wimfs_context *ctx = wimfs_get_context();
975 ret = lookup_resource(ctx->wim, path,
976 get_lookup_flags(ctx) | LOOKUP_FLAG_DIRECTORY_OK,
977 &dentry, <e, NULL);
980 return inode_to_stbuf(dentry->d_inode, lte, stbuf);
984 /* Read an alternate data stream through the XATTR interface, or get its size */
985 static int wimfs_getxattr(const char *path, const char *name, char *value,
990 struct ads_entry *ads_entry;
992 struct lookup_table_entry *lte;
993 struct wimfs_context *ctx = wimfs_get_context();
995 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
998 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1002 inode = wim_pathname_to_inode(ctx->wim, path);
1006 ads_entry = inode_get_ads_entry(inode, name, NULL);
1010 lte = ads_entry->lte;
1011 res_size = wim_resource_size(lte);
1016 if (res_size > size)
1019 ret = read_full_wim_resource(lte, (u8*)value);
1027 /* Create a hard link */
1028 static int wimfs_link(const char *to, const char *from)
1030 struct dentry *from_dentry, *from_dentry_parent;
1031 const char *link_name;
1032 struct inode *inode;
1033 struct lookup_table_entry *lte;
1034 WIMStruct *w = wimfs_get_WIMStruct();
1036 inode = wim_pathname_to_inode(w, to);
1040 if (!inode_is_regular_file(inode))
1043 from_dentry_parent = get_parent_dentry(w, from);
1044 if (!from_dentry_parent)
1046 if (!dentry_is_directory(from_dentry_parent))
1049 link_name = path_basename(from);
1050 if (get_dentry_child_with_name(from_dentry_parent, link_name))
1052 from_dentry = new_dentry(link_name);
1057 inode_add_dentry(from_dentry, inode);
1058 from_dentry->d_inode = inode;
1059 inode->link_count++;
1061 for (unsigned i = 0; i <= inode->num_ads; i++) {
1062 lte = inode_stream_lte_resolved(inode, i);
1067 link_dentry(from_dentry, from_dentry_parent);
1072 static int wimfs_listxattr(const char *path, char *list, size_t size)
1075 struct inode *inode;
1076 struct wimfs_context *ctx = wimfs_get_context();
1078 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1081 /* List alternate data streams, or get the list size */
1083 inode = wim_pathname_to_inode(ctx->wim, path);
1089 for (u16 i = 0; i < inode->num_ads; i++)
1090 needed_size += inode->ads_entries[i].stream_name_utf8_len + 6;
1094 for (u16 i = 0; i < inode->num_ads; i++) {
1095 needed_size = inode->ads_entries[i].stream_name_utf8_len + 6;
1096 if (needed_size > size)
1098 p += sprintf(p, "user.%s",
1099 inode->ads_entries[i].stream_name_utf8) + 1;
1100 size -= needed_size;
1108 * Create a directory in the WIM.
1109 * @mode is currently ignored.
1111 static int wimfs_mkdir(const char *path, mode_t mode)
1113 struct dentry *parent;
1114 struct dentry *newdir;
1115 const char *basename;
1116 struct wimfs_context *ctx = wimfs_get_context();
1118 parent = get_parent_dentry(ctx->wim, path);
1122 if (!dentry_is_directory(parent))
1125 basename = path_basename(path);
1126 if (get_dentry_child_with_name(parent, basename))
1129 newdir = new_dentry_with_inode(basename);
1130 newdir->d_inode->attributes |= FILE_ATTRIBUTE_DIRECTORY;
1131 newdir->d_inode->resolved = true;
1132 newdir->d_inode->ino = ctx->next_ino++;
1133 link_dentry(newdir, parent);
1137 /* Creates a regular file. */
1138 static int wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
1140 const char *stream_name;
1141 struct wimfs_context *ctx = wimfs_get_context();
1142 if ((ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
1143 && (stream_name = path_stream_name(path))) {
1144 /* Make an alternate data stream */
1145 struct ads_entry *new_entry;
1146 struct inode *inode;
1148 char *p = (char*)stream_name - 1;
1149 wimlib_assert(*p == ':');
1152 inode = wim_pathname_to_inode(ctx->wim, path);
1155 if (!inode_is_regular_file(inode))
1157 if (inode_get_ads_entry(inode, stream_name, NULL))
1159 new_entry = inode_add_ads(inode, stream_name);
1163 struct dentry *dentry, *parent;
1164 const char *basename;
1166 /* Make a normal file (not an alternate data stream) */
1168 /* Make sure that the parent of @path exists and is a directory, and
1169 * that the dentry named by @path does not already exist. */
1170 parent = get_parent_dentry(ctx->wim, path);
1173 if (!dentry_is_directory(parent))
1176 basename = path_basename(path);
1177 if (get_dentry_child_with_name(parent, path))
1180 dentry = new_dentry_with_inode(basename);
1183 dentry->d_inode->resolved = true;
1184 dentry->d_inode->ino = ctx->next_ino++;
1185 link_dentry(dentry, parent);
1192 static int wimfs_open(const char *path, struct fuse_file_info *fi)
1194 struct dentry *dentry;
1195 struct lookup_table_entry *lte;
1197 struct wimlib_fd *fd;
1198 struct inode *inode;
1201 struct wimfs_context *ctx = wimfs_get_context();
1203 ret = lookup_resource(ctx->wim, path, get_lookup_flags(ctx),
1204 &dentry, <e, &stream_idx);
1208 inode = dentry->d_inode;
1210 if (stream_idx == 0)
1213 stream_id = inode->ads_entries[stream_idx - 1].stream_id;
1215 /* The file resource may be in the staging directory (read-write mounts
1216 * only) or in the WIM. If it's in the staging directory, we need to
1217 * open a native file descriptor for the corresponding file. Otherwise,
1218 * we can read the file resource directly from the WIM file if we are
1219 * opening it read-only, but we need to extract the resource to the
1220 * staging directory if we are opening it writable. */
1222 if (flags_writable(fi->flags) &&
1223 (!lte || lte->resource_location != RESOURCE_IN_STAGING_FILE)) {
1224 u64 size = (lte) ? wim_resource_size(lte) : 0;
1225 ret = extract_resource_to_staging_dir(inode, stream_id,
1231 ret = alloc_wimlib_fd(inode, stream_id, lte, &fd);
1235 if (lte && lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1236 fd->staging_fd = open(lte->staging_file_name, fi->flags);
1237 if (fd->staging_fd == -1) {
1238 close_wimlib_fd(fd);
1242 fi->fh = (uintptr_t)fd;
1246 /* Opens a directory. */
1247 static int wimfs_opendir(const char *path, struct fuse_file_info *fi)
1249 struct inode *inode;
1251 struct wimlib_fd *fd = NULL;
1252 WIMStruct *w = wimfs_get_WIMStruct();
1254 inode = wim_pathname_to_inode(w, path);
1257 if (!inode_is_directory(inode))
1259 ret = alloc_wimlib_fd(inode, 0, NULL, &fd);
1260 fi->fh = (uintptr_t)fd;
1266 * Read data from a file in the WIM or in the staging directory.
1268 static int wimfs_read(const char *path, char *buf, size_t size,
1269 off_t offset, struct fuse_file_info *fi)
1271 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1276 if (!fd->f_lte) /* Empty stream with no lookup table entry */
1279 if (fd->f_lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1280 /* Read from staging file */
1282 wimlib_assert(fd->f_lte->staging_file_name);
1283 wimlib_assert(fd->staging_fd != -1);
1286 DEBUG("Seek to offset %zu", offset);
1288 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1290 ret = read(fd->staging_fd, buf, size);
1297 u64 res_size = wim_resource_size(fd->f_lte);
1299 if (offset > res_size)
1302 size = min(size, res_size - offset);
1304 if (read_wim_resource(fd->f_lte, (u8*)buf,
1305 size, offset, false) != 0)
1311 struct fill_params {
1313 fuse_fill_dir_t filler;
1316 static int dentry_fuse_fill(struct dentry *dentry, void *arg)
1318 struct fill_params *fill_params = arg;
1319 return fill_params->filler(fill_params->buf, dentry->file_name_utf8,
1323 /* Fills in the entries of the directory specified by @path using the
1324 * FUSE-provided function @filler. */
1325 static int wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
1326 off_t offset, struct fuse_file_info *fi)
1328 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1329 struct inode *inode;
1334 inode = fd->f_inode;
1336 struct fill_params fill_params = {
1341 filler(buf, ".", NULL, 0);
1342 filler(buf, "..", NULL, 0);
1344 return for_dentry_in_rbtree(inode->children.rb_node,
1345 dentry_fuse_fill, &fill_params);
1349 static int wimfs_readlink(const char *path, char *buf, size_t buf_len)
1351 struct wimfs_context *ctx = wimfs_get_context();
1352 struct inode *inode = wim_pathname_to_inode(ctx->wim, path);
1356 if (!inode_is_symlink(inode))
1359 ret = inode_readlink(inode, buf, buf_len, ctx->wim);
1366 static int wimfs_release(const char *path, struct fuse_file_info *fi)
1368 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1369 return close_wimlib_fd(fd);
1372 /* Close a directory */
1373 static int wimfs_releasedir(const char *path, struct fuse_file_info *fi)
1375 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1376 return close_wimlib_fd(fd);
1380 /* Remove an alternate data stream through the XATTR interface */
1381 static int wimfs_removexattr(const char *path, const char *name)
1383 struct inode *inode;
1384 struct ads_entry *ads_entry;
1386 struct wimfs_context *ctx = wimfs_get_context();
1388 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1391 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1395 inode = wim_pathname_to_inode(ctx->wim, path);
1399 ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
1402 inode_remove_ads(inode, ads_idx, ctx->wim->lookup_table);
1407 /* Renames a file or directory. See rename (3) */
1408 static int wimfs_rename(const char *from, const char *to)
1412 struct dentry *parent_of_dst;
1413 char *file_name_utf16 = NULL, *file_name_utf8 = NULL;
1414 u16 file_name_utf16_len, file_name_utf8_len;
1415 WIMStruct *w = wimfs_get_WIMStruct();
1418 /* This rename() implementation currently only supports actual files
1419 * (not alternate data streams) */
1421 src = get_dentry(w, from);
1425 dst = get_dentry(w, to);
1428 ret = get_names(&file_name_utf16, &file_name_utf8,
1429 &file_name_utf16_len, &file_name_utf8_len,
1435 /* Destination file exists */
1437 if (src == dst) /* Same file */
1440 if (!dentry_is_directory(src)) {
1441 /* Cannot rename non-directory to directory. */
1442 if (dentry_is_directory(dst))
1445 /* Cannot rename directory to a non-directory or a non-empty
1447 if (!dentry_is_directory(dst))
1449 if (inode_has_children(dst->d_inode))
1452 parent_of_dst = dst->parent;
1453 remove_dentry(dst, w->lookup_table);
1455 /* Destination does not exist */
1456 parent_of_dst = get_parent_dentry(w, to);
1460 if (!dentry_is_directory(parent_of_dst))
1464 FREE(src->file_name);
1465 FREE(src->file_name_utf8);
1466 src->file_name = file_name_utf16;
1467 src->file_name_utf8 = file_name_utf8;
1468 src->file_name_len = file_name_utf16_len;
1469 src->file_name_utf8_len = file_name_utf8_len;
1472 link_dentry(src, parent_of_dst);
1476 /* Remove a directory */
1477 static int wimfs_rmdir(const char *path)
1479 struct dentry *dentry;
1480 WIMStruct *w = wimfs_get_WIMStruct();
1482 dentry = get_dentry(w, path);
1486 if (!dentry_is_empty_directory(dentry))
1489 remove_dentry(dentry, w->lookup_table);
1494 /* Write an alternate data stream through the XATTR interface */
1495 static int wimfs_setxattr(const char *path, const char *name,
1496 const char *value, size_t size, int flags)
1498 struct ads_entry *existing_ads_entry;
1499 struct ads_entry *new_ads_entry;
1500 struct lookup_table_entry *existing_lte;
1501 struct lookup_table_entry *lte;
1502 struct inode *inode;
1503 u8 value_hash[SHA1_HASH_SIZE];
1505 struct wimfs_context *ctx = wimfs_get_context();
1507 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1510 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1514 inode = wim_pathname_to_inode(ctx->wim, path);
1518 existing_ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
1519 if (existing_ads_entry) {
1520 if (flags & XATTR_CREATE)
1522 inode_remove_ads(inode, ads_idx, ctx->wim->lookup_table);
1524 if (flags & XATTR_REPLACE)
1527 new_ads_entry = inode_add_ads(inode, name);
1531 sha1_buffer((const u8*)value, size, value_hash);
1533 existing_lte = __lookup_resource(ctx->wim->lookup_table, value_hash);
1540 lte = new_lookup_table_entry();
1543 value_copy = MALLOC(size);
1548 memcpy(value_copy, value, size);
1549 lte->resource_location = RESOURCE_IN_ATTACHED_BUFFER;
1550 lte->attached_buffer = value_copy;
1551 lte->resource_entry.original_size = size;
1552 lte->resource_entry.size = size;
1553 lte->resource_entry.flags = 0;
1554 copy_hash(lte->hash, value_hash);
1555 lookup_table_insert(ctx->wim->lookup_table, lte);
1557 new_ads_entry->lte = lte;
1562 static int wimfs_symlink(const char *to, const char *from)
1564 struct dentry *dentry_parent, *dentry;
1565 const char *link_name;
1566 struct inode *inode;
1567 struct wimfs_context *ctx = wimfs_get_context();
1569 dentry_parent = get_parent_dentry(ctx->wim, from);
1572 if (!dentry_is_directory(dentry_parent))
1575 link_name = path_basename(from);
1577 if (get_dentry_child_with_name(dentry_parent, link_name))
1579 dentry = new_dentry_with_inode(link_name);
1582 inode = dentry->d_inode;
1584 inode->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
1585 inode->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
1586 inode->ino = ctx->next_ino++;
1587 inode->resolved = true;
1589 if (inode_set_symlink(inode, to, ctx->wim->lookup_table, NULL) != 0)
1590 goto out_free_dentry;
1592 link_dentry(dentry, dentry_parent);
1595 free_dentry(dentry);
1600 /* Reduce the size of a file */
1601 static int wimfs_truncate(const char *path, off_t size)
1603 struct dentry *dentry;
1604 struct lookup_table_entry *lte;
1608 struct inode *inode;
1609 struct wimfs_context *ctx = wimfs_get_context();
1611 ret = lookup_resource(ctx->wim, path, get_lookup_flags(ctx),
1612 &dentry, <e, &stream_idx);
1617 if (!lte) /* Already a zero-length file */
1620 inode = dentry->d_inode;
1622 if (stream_idx == 0)
1625 stream_id = inode->ads_entries[stream_idx - 1].stream_id;
1627 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1628 wimlib_assert(lte->staging_file_name);
1629 ret = truncate(lte->staging_file_name, size);
1632 lte->resource_entry.original_size = size;
1634 wimlib_assert(lte->resource_location == RESOURCE_IN_WIM);
1635 /* File in WIM. Extract it to the staging directory, but only
1636 * the first @size bytes of it. */
1637 ret = extract_resource_to_staging_dir(inode, stream_id,
1643 /* Remove a regular file */
1644 static int wimfs_unlink(const char *path)
1646 struct dentry *dentry;
1647 struct lookup_table_entry *lte;
1650 struct wimfs_context *ctx = wimfs_get_context();
1652 ret = lookup_resource(ctx->wim, path, get_lookup_flags(ctx),
1653 &dentry, <e, &stream_idx);
1658 if (stream_idx == 0)
1659 remove_dentry(dentry, ctx->wim->lookup_table);
1661 inode_remove_ads(dentry->d_inode, stream_idx - 1,
1662 ctx->wim->lookup_table);
1666 #ifdef HAVE_UTIMENSAT
1668 * Change the timestamp on a file dentry.
1670 * Note that alternate data streams do not have their own timestamps.
1672 static int wimfs_utimens(const char *path, const struct timespec tv[2])
1674 struct dentry *dentry;
1675 struct inode *inode;
1676 WIMStruct *w = wimfs_get_WIMStruct();
1678 dentry = get_dentry(w, path);
1681 inode = dentry->d_inode;
1683 if (tv[0].tv_nsec != UTIME_OMIT) {
1684 if (tv[0].tv_nsec == UTIME_NOW)
1685 inode->last_access_time = get_wim_timestamp();
1687 inode->last_access_time = timespec_to_wim_timestamp(&tv[0]);
1689 if (tv[1].tv_nsec != UTIME_OMIT) {
1690 if (tv[1].tv_nsec == UTIME_NOW)
1691 inode->last_write_time = get_wim_timestamp();
1693 inode->last_write_time = timespec_to_wim_timestamp(&tv[1]);
1698 static int wimfs_utime(const char *path, struct utimbuf *times)
1700 struct dentry *dentry;
1701 struct inode *inode;
1702 WIMStruct *w = wimfs_get_WIMStruct();
1704 dentry = get_dentry(w, path);
1707 inode = dentry->d_inode;
1709 inode->last_write_time = unix_timestamp_to_wim(times->modtime);
1710 inode->last_access_time = unix_timestamp_to_wim(times->actime);
1715 /* Writes to a file in the WIM filesystem.
1716 * It may be an alternate data stream, but here we don't even notice because we
1717 * just get a lookup table entry. */
1718 static int wimfs_write(const char *path, const char *buf, size_t size,
1719 off_t offset, struct fuse_file_info *fi)
1721 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1728 wimlib_assert(fd->f_lte);
1729 wimlib_assert(fd->f_lte->staging_file_name);
1730 wimlib_assert(fd->staging_fd != -1);
1731 wimlib_assert(fd->f_inode);
1733 /* Seek to the requested position */
1734 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1737 /* Write the data. */
1738 ret = write(fd->staging_fd, buf, size);
1742 now = get_wim_timestamp();
1743 fd->f_inode->last_write_time = now;
1744 fd->f_inode->last_access_time = now;
1748 static struct fuse_operations wimfs_operations = {
1749 .access = wimfs_access,
1750 .destroy = wimfs_destroy,
1752 .fallocate = wimfs_fallocate,
1754 .fgetattr = wimfs_fgetattr,
1755 .ftruncate = wimfs_ftruncate,
1756 .getattr = wimfs_getattr,
1758 .getxattr = wimfs_getxattr,
1762 .listxattr = wimfs_listxattr,
1764 .mkdir = wimfs_mkdir,
1765 .mknod = wimfs_mknod,
1767 .opendir = wimfs_opendir,
1769 .readdir = wimfs_readdir,
1770 .readlink = wimfs_readlink,
1771 .release = wimfs_release,
1772 .releasedir = wimfs_releasedir,
1774 .removexattr = wimfs_removexattr,
1776 .rename = wimfs_rename,
1777 .rmdir = wimfs_rmdir,
1779 .setxattr = wimfs_setxattr,
1781 .symlink = wimfs_symlink,
1782 .truncate = wimfs_truncate,
1783 .unlink = wimfs_unlink,
1784 #ifdef HAVE_UTIMENSAT
1785 .utimens = wimfs_utimens,
1787 .utime = wimfs_utime,
1789 .write = wimfs_write,
1793 /* Mounts a WIM file. */
1794 WIMLIBAPI int wimlib_mount(WIMStruct *wim, int image, const char *dir,
1795 int flags, WIMStruct **additional_swms,
1796 unsigned num_additional_swms)
1802 struct lookup_table *joined_tab, *wim_tab_save;
1803 struct image_metadata *imd;
1804 struct wimfs_context ctx;
1806 DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
1807 wim, image, dir, flags);
1810 return WIMLIB_ERR_INVALID_PARAM;
1812 ret = verify_swm_set(wim, additional_swms, num_additional_swms);
1816 if (num_additional_swms) {
1817 ret = new_joined_lookup_table(wim, additional_swms,
1818 num_additional_swms,
1822 wim_tab_save = wim->lookup_table;
1823 wim->lookup_table = joined_tab;
1826 ret = select_wim_image(wim, image);
1831 imd = &wim->image_metadata[image - 1];
1833 DEBUG("Selected image %d", image);
1835 if (imd->root_dentry->refcnt != 1) {
1836 ERROR("Cannot mount image that was just exported with "
1838 ret = WIMLIB_ERR_INVALID_PARAM;
1842 if (!(flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
1843 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
1844 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
1845 flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1848 DEBUG("Initializing struct wimfs_context");
1849 init_wimfs_context(&ctx);
1851 DEBUG("Getting current directory");
1852 ctx.working_directory = getcwd(NULL, 0);
1853 if (!ctx.working_directory) {
1854 ERROR_WITH_ERRNO("Could not determine current directory");
1855 ret = WIMLIB_ERR_NOTDIR;
1859 DEBUG("Unlinking message queues in case they already exist");
1860 ret = set_message_queue_names(&ctx, dir);
1862 goto out_free_working_directory;
1863 unlink_message_queues(&ctx);
1865 DEBUG("Preparing arguments to fuse_main()");
1867 dir_copy = STRDUP(dir);
1869 goto out_free_message_queue_names;
1871 argv[argc++] = "imagex";
1872 argv[argc++] = dir_copy;
1873 argv[argc++] = "-s"; /* disable multi-threaded operation */
1875 if (flags & WIMLIB_MOUNT_FLAG_DEBUG)
1876 argv[argc++] = "-d";
1879 * We provide the use_ino option because we are going to assign inode
1880 * numbers oursides. The inodes will be given unique numbers in the
1881 * assign_inode_numbers() function, and the static variable @next_ino is
1882 * set to the next available inode number.
1884 char optstring[256] = "use_ino,subtype=wimfs,attr_timeout=0";
1885 argv[argc++] = "-o";
1886 argv[argc++] = optstring;
1887 if ((flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
1888 /* Read-write mount. Make the staging directory */
1889 ret = make_staging_dir(&ctx);
1891 goto out_free_dir_copy;
1893 /* Read-only mount */
1894 strcat(optstring, ",ro");
1901 DEBUG("FUSE command line (argc = %d): ", argc);
1902 for (i = 0; i < argc; i++) {
1903 fputs(argv[i], stdout);
1911 /* Mark dentry tree as modified if read-write mount. */
1912 if (flags & WIMLIB_MOUNT_FLAG_READWRITE)
1913 imd->modified = true;
1915 /* Resolve all the lookup table entries of the dentry tree */
1916 DEBUG("Resolving lookup table entries");
1917 for_dentry_in_tree(imd->root_dentry, dentry_resolve_ltes,
1920 ctx.next_ino = assign_inode_numbers(&imd->inode_list);
1921 DEBUG("(next_ino = %"PRIu64")", ctx.next_ino);
1923 /* Finish initializing the filesystem context. */
1925 ctx.mount_flags = flags;
1927 DEBUG("Calling fuse_main()");
1929 ret = fuse_main(argc, argv, &wimfs_operations, &ctx);
1931 DEBUG("Returned from fuse_main() (ret = %d)", ret);
1933 ret = WIMLIB_ERR_FUSE;
1936 out_free_message_queue_names:
1937 free_message_queue_names(&ctx);
1938 out_free_working_directory:
1939 FREE(ctx.working_directory);
1940 ctx.working_directory = NULL;
1942 if (num_additional_swms) {
1943 free_lookup_table(wim->lookup_table);
1944 wim->lookup_table = wim_tab_save;
1951 * Unmounts the WIM file that was previously mounted on @dir by using
1954 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
1961 struct timespec timeout;
1963 struct wimfs_context ctx;
1966 init_wimfs_context(&ctx);
1968 /* Open message queues between the unmount process and the
1969 * filesystem daemon. */
1970 ret = set_message_queue_names(&ctx, dir);
1974 ret = open_message_queues(&ctx, false);
1976 goto out_free_message_queue_names;
1978 /* Send a message to the filesystem daemon saying whether to commit or
1980 msg[0] = (flags & WIMLIB_UNMOUNT_FLAG_COMMIT) ? 1 : 0;
1981 msg[1] = (flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY) ? 1 : 0;
1983 DEBUG("Sending message: %scommit, %scheck",
1984 (msg[0] ? "" : "don't "),
1985 (msg[1] ? "" : "don't "));
1986 ret = mq_send(ctx.unmount_to_daemon_mq, msg, 2, 1);
1988 ERROR_WITH_ERRNO("Failed to notify filesystem daemon whether "
1989 "we want to commit changes or not");
1990 ret = WIMLIB_ERR_MQUEUE;
1991 goto out_close_message_queues;
1994 /* Execute `fusermount -u', which is installed setuid root, to unmount
1997 * FUSE does not yet implement synchronous unmounts. This means that
1998 * fusermount -u will return before the filesystem daemon returns from
1999 * wimfs_destroy(). This is partly what we want, because we need to
2000 * send a message from this process to the filesystem daemon telling
2001 * whether --commit was specified or not. However, after that, the
2002 * unmount process must wait for the filesystem daemon to finish writing
2008 ERROR_WITH_ERRNO("Failed to fork()");
2009 ret = WIMLIB_ERR_FORK;
2010 goto out_close_message_queues;
2014 execlp("fusermount", "fusermount", "-u", dir, NULL);
2015 ERROR_WITH_ERRNO("Failed to execute `fusermount'");
2016 exit(WIMLIB_ERR_FUSERMOUNT);
2020 ret = wait(&status);
2022 ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
2024 ret = WIMLIB_ERR_FUSERMOUNT;
2025 goto out_close_message_queues;
2029 ERROR("fusermount exited with status %d", status);
2031 /* Try again, but with the `umount' program. This is required
2032 * on other FUSE implementations such as FreeBSD's that do not
2033 * have a `fusermount' program. */
2037 ERROR_WITH_ERRNO("Failed to fork()");
2038 ret = WIMLIB_ERR_FORK;
2039 goto out_close_message_queues;
2043 execlp("umount", "umount", dir, NULL);
2044 ERROR_WITH_ERRNO("Failed to execute `umount'");
2045 exit(WIMLIB_ERR_FUSERMOUNT);
2049 ret = wait(&status);
2051 ERROR_WITH_ERRNO("Failed to wait for `umount' process to "
2053 ret = WIMLIB_ERR_FUSERMOUNT;
2054 goto out_close_message_queues;
2057 ERROR("`umount' exited with failure status");
2058 ret = WIMLIB_ERR_FUSERMOUNT;
2059 goto out_close_message_queues;
2063 wimlib_assert(status == 0);
2065 /* Wait for a message from the filesytem daemon indicating whether the
2066 * filesystem was unmounted successfully (0) or an error occurred (1).
2067 * This may take a long time if a big WIM file needs to be rewritten. */
2069 /* Wait at most 600??? seconds before giving up and returning false.
2070 * Either it's a really big WIM file, or (more likely) the
2071 * filesystem daemon has crashed or failed for some reason.
2073 * XXX come up with some method to determine if the filesystem
2074 * daemon has really crashed or not.
2076 * XXX Idea: have mount daemon write its PID into the WIM file header?
2077 * No, this wouldn't work because we know the directory but not the WIM
2081 gettimeofday(&now, NULL);
2082 timeout.tv_sec = now.tv_sec + 600;
2083 timeout.tv_nsec = now.tv_usec * 1000;
2085 ret = get_mailbox(ctx.daemon_to_unmount_mq, 2, &msgsize, &mailbox);
2087 goto out_close_message_queues;
2090 DEBUG("Waiting for message telling us whether the unmount was "
2091 "successful or not.");
2092 ret = mq_timedreceive(ctx.daemon_to_unmount_mq, mailbox,
2093 msgsize, NULL, &timeout);
2095 if (errno == ETIMEDOUT) {
2096 ERROR("Timed out- probably the filesystem daemon "
2097 "crashed and the WIM was not written "
2099 ret = WIMLIB_ERR_TIMEOUT;
2101 ERROR_WITH_ERRNO("mq_receive()");
2102 ret = WIMLIB_ERR_MQUEUE;
2104 goto out_free_mailbox;
2107 DEBUG("Received message: Unmount %s", (mailbox[0] ? "Failed" : "Ok"));
2110 ERROR("Unmount failed");
2113 out_close_message_queues:
2114 close_message_queues(&ctx);
2115 out_free_message_queue_names:
2116 free_message_queue_names(&ctx);
2121 #else /* WITH_FUSE */
2124 static inline int mount_unsupported_error()
2126 ERROR("wimlib was compiled with --without-fuse, which disables support "
2127 "for mounting WIMs.");
2128 return WIMLIB_ERR_UNSUPPORTED;
2131 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
2133 return mount_unsupported_error();
2136 WIMLIBAPI int wimlib_mount(WIMStruct *wim_p, int image, const char *dir,
2137 int flags, WIMStruct **additional_swms,
2138 unsigned num_additional_swms)
2140 return mount_unsupported_error();
2143 #endif /* WITH_FUSE */