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 bool wimfs_ctx_readonly(const struct wimfs_context *ctx)
120 return (ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) == 0;
123 static inline int get_lookup_flags(const struct wimfs_context *ctx)
125 if (ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
126 return LOOKUP_FLAG_ADS_OK;
131 /* Returns nonzero if write permission is requested on the file open flags */
132 static inline int flags_writable(int open_flags)
134 return open_flags & (O_RDWR | O_WRONLY);
138 * Allocate a file descriptor for a stream.
140 * @inode: inode containing the stream we're opening
141 * @stream_id: ID of the stream we're opening
142 * @lte: Lookup table entry for the stream (may be NULL)
143 * @fd_ret: Return the allocated file descriptor if successful.
144 * @readonly: True if this is a read-only mount.
146 * Return 0 iff successful or error code if unsuccessful.
148 static int alloc_wimlib_fd(struct inode *inode,
150 struct lookup_table_entry *lte,
151 struct wimlib_fd **fd_ret,
154 static const u16 fds_per_alloc = 8;
155 static const u16 max_fds = 0xffff;
158 pthread_mutex_lock(&inode->i_mutex);
160 DEBUG("Allocating fd for stream ID %u from inode %lx (open = %u, allocated = %u)",
161 stream_id, inode->ino, inode->num_opened_fds,
162 inode->num_allocated_fds);
164 if (inode->num_opened_fds == inode->num_allocated_fds) {
165 struct wimlib_fd **fds;
168 if (inode->num_allocated_fds == max_fds) {
172 num_new_fds = min(fds_per_alloc,
173 max_fds - inode->num_allocated_fds);
175 fds = REALLOC(inode->fds,
176 (inode->num_allocated_fds + num_new_fds) *
177 sizeof(inode->fds[0]));
182 memset(&fds[inode->num_allocated_fds], 0,
183 num_new_fds * sizeof(fds[0]));
185 inode->num_allocated_fds += num_new_fds;
187 for (u16 i = 0; ; i++) {
188 if (!inode->fds[i]) {
189 struct wimlib_fd *fd = CALLOC(1, sizeof(*fd));
198 fd->stream_id = stream_id;
201 inode->num_opened_fds++;
202 if (lte && !readonly)
203 lte->num_opened_fds++;
204 DEBUG("Allocated fd (idx = %u)", fd->idx);
210 pthread_mutex_unlock(&inode->i_mutex);
214 static void inode_put_fd(struct inode *inode, struct wimlib_fd *fd)
216 wimlib_assert(fd != NULL);
217 wimlib_assert(inode != NULL);
219 pthread_mutex_lock(&inode->i_mutex);
221 wimlib_assert(fd->f_inode == inode);
222 wimlib_assert(inode->num_opened_fds != 0);
223 wimlib_assert(fd->idx < inode->num_allocated_fds);
224 wimlib_assert(inode->fds[fd->idx] == fd);
226 inode->fds[fd->idx] = NULL;
228 if (--inode->num_opened_fds == 0 && inode->link_count == 0) {
229 pthread_mutex_unlock(&inode->i_mutex);
232 pthread_mutex_unlock(&inode->i_mutex);
236 static int lte_put_fd(struct lookup_table_entry *lte, struct wimlib_fd *fd)
238 wimlib_assert(fd != NULL);
239 wimlib_assert(fd->f_lte == lte);
241 if (!lte) /* Empty stream with no lookup table entry */
244 /* Close staging file descriptor if needed. */
246 if (lte->resource_location == RESOURCE_IN_STAGING_FILE
247 && fd->staging_fd != -1)
249 if (close(fd->staging_fd) != 0) {
250 ERROR_WITH_ERRNO("Failed to close staging file");
254 lte_decrement_num_opened_fds(lte);
258 /* Close a file descriptor. */
259 static int close_wimlib_fd(struct wimlib_fd *fd)
262 wimlib_assert(fd != NULL);
263 DEBUG("Closing fd (inode = %lu, opened = %u, allocated = %u)",
264 fd->f_inode->ino, fd->f_inode->num_opened_fds,
265 fd->f_inode->num_allocated_fds);
266 ret = lte_put_fd(fd->f_lte, fd);
270 inode_put_fd(fd->f_inode, fd);
274 /* Remove a dentry; i.e. remove a reference to the corresponding inode.
276 * If there are no remaining references to the inode either through dentries or
277 * open file descriptors, the inode is freed. Otherwise, the inode is not
278 * removed, but the dentry is unlinked and freed.
280 * Either way, all lookup table entries referenced by the inode have their
281 * reference count decremented. If a lookup table entry has no open file
282 * descriptors and no references remaining, it is freed, and the staging file is
285 static void remove_dentry(struct dentry *dentry,
286 struct lookup_table *lookup_table)
288 struct inode *inode = dentry->d_inode;
289 struct lookup_table_entry *lte;
292 for (i = 0; i <= inode->num_ads; i++) {
293 lte = inode_stream_lte_resolved(inode, i);
295 lte_decrement_refcnt(lte, lookup_table);
297 unlink_dentry(dentry);
301 /* Creates a new staging file and returns its file descriptor opened for
304 * @name_ret: A location into which the a pointer to the newly allocated name of
305 * the staging file is stored.
306 * @return: The file descriptor for the new file. Returns -1 and sets errno on
307 * error, for any reason possible from the creat() function.
309 static int create_staging_file(char **name_ret, int open_flags,
310 struct wimfs_context *ctx)
318 name_len = ctx->staging_dir_name_len + 1 + SHA1_HASH_SIZE;
319 name = MALLOC(name_len + 1);
327 memcpy(name, ctx->staging_dir_name, ctx->staging_dir_name_len);
328 name[ctx->staging_dir_name_len] = '/';
329 randomize_char_array_with_alnum(name + ctx->staging_dir_name_len + 1,
331 name[name_len] = '\0';
334 /* Just in case, verify that the randomly generated name doesn't name an
335 * existing file, and try again if so */
336 } while (stat(name, &stbuf) == 0);
342 /* doesn't exist--- ok */
344 DEBUG("Creating staging file `%s'", name);
346 fd = open(name, open_flags | O_CREAT | O_TRUNC, 0600);
358 * Extract a WIM resource to the staging directory.
360 * @inode: Inode that contains the stream we are extracting
362 * @stream_id: Identifier for the stream (it stays constant even if the indices
363 * of the stream entries are changed)
365 * @lte: Pointer to pointer to the lookup table entry for the stream we need to
366 * extract, or NULL if there was no lookup table entry present for the stream
368 * @size: Number of bytes of the stream we want to extract (this supports the
369 * wimfs_truncate() function).
371 static int extract_resource_to_staging_dir(struct inode *inode,
373 struct lookup_table_entry **lte,
375 struct wimfs_context *ctx)
377 char *staging_file_name;
380 struct lookup_table_entry *old_lte, *new_lte;
382 DEBUG("Extracting resource to staging dir: inode %"PRIu64", "
383 "stream id %"PRIu32, inode->ino, stream_id);
386 fd = create_staging_file(&staging_file_name, O_WRONLY, ctx);
391 ret = extract_wim_resource_to_fd(old_lte, fd, size);
394 if (ret != 0 || close(fd) != 0) {
400 goto out_delete_staging_file;
403 if (old_lte && inode->link_count == old_lte->refcnt) {
404 /* The reference count of the existing lookup table
405 * entry is the same as the link count of the inode that
406 * contains the stream we're opening. Therefore, ALL
407 * the references to the lookup table entry correspond
408 * to the stream we're trying to extract, so the lookup
409 * table entry can be re-used. */
410 DEBUG("Re-using lookup table entry");
411 lookup_table_unlink(ctx->wim->lookup_table, old_lte);
415 /* There's an existing lookup table entry, but its
416 * reference count is creater than the link count for
417 * the inode containing a stream we're opening.
418 * Therefore, we need to split the lookup table entry.
420 wimlib_assert(old_lte->refcnt > inode->link_count);
421 DEBUG("Splitting lookup table entry "
422 "(inode->link_count = %u, old_lte->refcnt = %u)",
423 inode->link_count, old_lte->refcnt);
427 new_lte = new_lookup_table_entry();
430 goto out_delete_staging_file;
433 /* There may already be open file descriptors to this stream if
434 * it's previously been opened read-only, but just now we're
435 * opening it read-write. Identify those file descriptors and
436 * change their lookup table entry pointers to point to the new
437 * lookup table entry, and open staging file descriptors for
440 * At the same time, we need to count the number of these opened
441 * file descriptors to the new lookup table entry. If there's
442 * an old lookup table entry, this number needs to be subtracted
443 * from the fd's opened to the old entry. */
444 for (u16 i = 0, j = 0; j < inode->num_opened_fds; i++) {
445 struct wimlib_fd *fd = inode->fds[i];
447 if (fd->stream_id == stream_id) {
448 wimlib_assert(fd->f_lte == old_lte);
449 wimlib_assert(fd->staging_fd == -1);
451 new_lte->num_opened_fds++;
452 fd->staging_fd = open(staging_file_name, O_RDONLY);
453 if (fd->staging_fd == -1) {
455 goto out_revert_fd_changes;
461 DEBUG("%hu fd's were already opened to the file we extracted",
462 new_lte->num_opened_fds);
464 old_lte->num_opened_fds -= new_lte->num_opened_fds;
465 old_lte->refcnt -= inode->link_count;
469 new_lte->resource_entry.original_size = size;
470 new_lte->refcnt = inode->link_count;
471 new_lte->resource_location = RESOURCE_IN_STAGING_FILE;
472 new_lte->staging_file_name = staging_file_name;
473 new_lte->lte_inode = inode;
474 random_hash(new_lte->hash);
477 inode->lte = new_lte;
479 for (u16 i = 0; i < inode->num_ads; i++)
480 if (inode->ads_entries[i].stream_id == stream_id)
481 inode->ads_entries[i].lte = new_lte;
483 lookup_table_insert(ctx->wim->lookup_table, new_lte);
484 list_add(&new_lte->staging_list, &ctx->staging_list);
487 out_revert_fd_changes:
488 for (u16 i = 0, j = 0; j < new_lte->num_opened_fds; i++) {
489 struct wimlib_fd *fd = inode->fds[i];
490 if (fd && fd->stream_id == stream_id && fd->f_lte == new_lte) {
492 if (fd->staging_fd != -1) {
493 close(fd->staging_fd);
499 free_lookup_table_entry(new_lte);
500 out_delete_staging_file:
501 unlink(staging_file_name);
502 FREE(staging_file_name);
507 * Creates a randomly named staging directory and saves its name in the
508 * filesystem context structure.
510 static int make_staging_dir(struct wimfs_context *ctx)
512 /* XXX Give the user an option of where to stage files */
514 static const char prefix[] = "wimlib-staging-";
515 static const size_t prefix_len = sizeof(prefix) - 1;
516 static const size_t random_suffix_len = 10;
518 size_t pwd_len = strlen(ctx->working_directory);
520 ctx->staging_dir_name_len = pwd_len + 1 + prefix_len + random_suffix_len;
522 ctx->staging_dir_name = MALLOC(ctx->staging_dir_name_len + 1);
523 if (!ctx->staging_dir_name)
524 return WIMLIB_ERR_NOMEM;
526 memcpy(ctx->staging_dir_name, ctx->working_directory, pwd_len);
527 ctx->staging_dir_name[pwd_len] = '/';
528 memcpy(ctx->staging_dir_name + pwd_len + 1, prefix, prefix_len);
529 randomize_char_array_with_alnum(ctx->staging_dir_name + pwd_len +
530 1 + prefix_len, random_suffix_len);
531 ctx->staging_dir_name[ctx->staging_dir_name_len] = '\0';
533 if (mkdir(ctx->staging_dir_name, 0700) != 0) {
534 ERROR_WITH_ERRNO("Failed to create temporary directory `%s'",
535 ctx->staging_dir_name);
536 FREE(ctx->staging_dir_name);
537 ctx->staging_dir_name = NULL;
538 return WIMLIB_ERR_MKDIR;
543 static int remove_file_or_directory(const char *fpath, const struct stat *sb,
544 int typeflag, struct FTW *ftwbuf)
546 if (remove(fpath) == 0)
549 ERROR_WITH_ERRNO("Cannot remove `%s'", fpath);
550 return WIMLIB_ERR_DELETE_STAGING_DIR;
556 * Deletes the staging directory and all the files contained in it.
558 static int delete_staging_dir(struct wimfs_context *ctx)
561 wimlib_assert(ctx->staging_dir_name != NULL);
562 ret = nftw(ctx->staging_dir_name, remove_file_or_directory,
564 FREE(ctx->staging_dir_name);
565 ctx->staging_dir_name = NULL;
570 /* Simple function that returns the concatenation of 2 strings. */
571 static char *strcat_dup(const char *s1, const char *s2, size_t max_len)
573 size_t len = strlen(s1) + strlen(s2);
576 char *p = MALLOC(len + 1);
579 snprintf(p, len + 1, "%s%s", s1, s2);
583 static int set_message_queue_names(struct wimfs_context *ctx,
584 const char *mount_dir)
586 static const char *u2d_prefix = "/wimlib-unmount-to-daemon-mq";
587 static const char *d2u_prefix = "/wimlib-daemon-to-unmount-mq";
592 dir_path = realpath(mount_dir, NULL);
594 ERROR_WITH_ERRNO("Failed to resolve path \"%s\"", mount_dir);
595 return WIMLIB_ERR_NOTDIR;
598 DEBUG("Using absolute dir_path = `%s'", dir_path);
608 ctx->unmount_to_daemon_mq_name = strcat_dup(u2d_prefix, dir_path,
610 if (!ctx->unmount_to_daemon_mq_name) {
611 ret = WIMLIB_ERR_NOMEM;
612 goto out_free_dir_path;
614 ctx->daemon_to_unmount_mq_name = strcat_dup(d2u_prefix, dir_path,
616 if (!ctx->daemon_to_unmount_mq_name) {
617 ret = WIMLIB_ERR_NOMEM;
618 goto out_free_unmount_to_daemon_mq_name;
622 goto out_free_dir_path;
623 out_free_unmount_to_daemon_mq_name:
624 FREE(ctx->unmount_to_daemon_mq_name);
625 ctx->unmount_to_daemon_mq_name = NULL;
631 static void free_message_queue_names(struct wimfs_context *ctx)
633 FREE(ctx->unmount_to_daemon_mq_name);
634 FREE(ctx->daemon_to_unmount_mq_name);
635 ctx->unmount_to_daemon_mq_name = NULL;
636 ctx->daemon_to_unmount_mq_name = NULL;
640 * Opens two POSIX message queue: one for sending messages from the unmount
641 * process to the daemon process, and one to go the other way. The names of the
642 * message queues, which must be system-wide unique, are be based on the mount
643 * point. (There of course is still a possibility of a collision if one were to
644 * unmount two identically named directories simultaneously...)
646 * @daemon specifies whether the calling process is the filesystem daemon or the
649 static int open_message_queues(struct wimfs_context *ctx, bool daemon)
654 wimlib_assert(ctx->unmount_to_daemon_mq_name != NULL &&
655 ctx->daemon_to_unmount_mq_name != NULL);
658 flags = O_RDONLY | O_CREAT;
660 flags = O_WRONLY | O_CREAT;
662 DEBUG("Opening message queue \"%s\"", ctx->unmount_to_daemon_mq_name);
663 ctx->unmount_to_daemon_mq = mq_open(ctx->unmount_to_daemon_mq_name,
666 if (ctx->unmount_to_daemon_mq == (mqd_t)-1) {
667 ERROR_WITH_ERRNO("mq_open()");
668 ret = WIMLIB_ERR_MQUEUE;
673 flags = O_WRONLY | O_CREAT;
675 flags = O_RDONLY | O_CREAT;
677 DEBUG("Opening message queue \"%s\"", ctx->daemon_to_unmount_mq_name);
678 ctx->daemon_to_unmount_mq = mq_open(ctx->daemon_to_unmount_mq_name,
681 if (ctx->daemon_to_unmount_mq == (mqd_t)-1) {
682 ERROR_WITH_ERRNO("mq_open()");
683 ret = WIMLIB_ERR_MQUEUE;
684 goto out_close_unmount_to_daemon_mq;
688 out_close_unmount_to_daemon_mq:
689 mq_close(ctx->unmount_to_daemon_mq);
690 mq_unlink(ctx->unmount_to_daemon_mq_name);
691 ctx->unmount_to_daemon_mq = (mqd_t)-1;
696 /* Try to determine the maximum message size of a message queue. The return
697 * value is the maximum message size, or a guess of 8192 bytes if it cannot be
699 static long mq_get_msgsize(mqd_t mq)
701 static const char *msgsize_max_file = "/proc/sys/fs/mqueue/msgsize_max";
706 if (mq_getattr(mq, &attr) == 0) {
707 msgsize = attr.mq_msgsize;
709 ERROR_WITH_ERRNO("mq_getattr()");
710 ERROR("Attempting to read %s", msgsize_max_file);
711 fp = fopen(msgsize_max_file, "rb");
713 if (fscanf(fp, "%ld", &msgsize) != 1) {
714 ERROR("Assuming message size of 8192");
719 ERROR_WITH_ERRNO("Failed to open the file `%s'",
721 ERROR("Assuming message size of 8192");
728 static int get_mailbox(mqd_t mq, long needed_msgsize, long *msgsize_ret,
734 msgsize = mq_get_msgsize(mq);
736 if (msgsize < needed_msgsize) {
737 ERROR("Message queue max size must be at least %ld!",
739 return WIMLIB_ERR_MQUEUE;
742 mailbox = MALLOC(msgsize);
744 ERROR("Failed to allocate %ld bytes for mailbox", msgsize);
745 return WIMLIB_ERR_NOMEM;
747 *msgsize_ret = msgsize;
748 *mailbox_ret = mailbox;
752 static void unlink_message_queues(struct wimfs_context *ctx)
754 mq_unlink(ctx->unmount_to_daemon_mq_name);
755 mq_unlink(ctx->daemon_to_unmount_mq_name);
758 /* Closes the message queues, which are allocated in static variables */
759 static void close_message_queues(struct wimfs_context *ctx)
761 mq_close(ctx->unmount_to_daemon_mq);
762 ctx->unmount_to_daemon_mq = (mqd_t)(-1);
763 mq_close(ctx->daemon_to_unmount_mq);
764 ctx->daemon_to_unmount_mq = (mqd_t)(-1);
765 unlink_message_queues(ctx);
768 static int wimfs_access(const char *path, int mask)
770 /* Permissions not implemented */
774 static int wimfs_chmod(const char *path, mode_t mask)
776 struct dentry *dentry;
777 struct wimfs_context *ctx = wimfs_get_context();
782 ret = lookup_resource(ctx->wim, path,
783 get_lookup_flags(ctx) | LOOKUP_FLAG_DIRECTORY_OK,
784 &dentry, NULL, NULL);
787 inode = dentry->d_inode;
788 inode_to_stbuf(inode, NULL, &stbuf);
789 if (mask == stbuf.st_mode)
796 static void inode_update_lte_ptr(struct inode *inode,
797 struct lookup_table_entry *old_lte,
798 struct lookup_table_entry *new_lte)
800 if (inode->lte == old_lte) {
801 inode->lte = new_lte;
803 for (unsigned i = 0; i < inode->num_ads; i++) {
804 if (inode->ads_entries[i].lte == old_lte) {
805 inode->ads_entries[i].lte = new_lte;
812 static int update_lte_of_staging_file(struct lookup_table_entry *lte,
813 struct lookup_table *table)
815 struct lookup_table_entry *duplicate_lte;
817 u8 hash[SHA1_HASH_SIZE];
820 wimlib_assert(lte->resource_location == RESOURCE_IN_STAGING_FILE);
821 wimlib_assert(lte->staging_file_name);
823 ret = sha1sum(lte->staging_file_name, hash);
827 lookup_table_unlink(table, lte);
829 duplicate_lte = __lookup_resource(table, hash);
832 /* Merge duplicate lookup table entries */
833 duplicate_lte->refcnt += lte->refcnt;
834 list_del(<e->staging_list);
835 inode_update_lte_ptr(lte->lte_inode, lte, duplicate_lte);
836 free_lookup_table_entry(lte);
838 if (stat(lte->staging_file_name, &stbuf) != 0) {
839 ERROR_WITH_ERRNO("Failed to stat `%s'", lte->staging_file_name);
840 return WIMLIB_ERR_STAT;
842 if (stbuf.st_size == 0) {
843 /* Zero-length stream. No lookup table entry needed. */
844 inode_update_lte_ptr(lte->lte_inode, lte, NULL);
845 free_lookup_table_entry(lte);
847 wimlib_assert(<e->file_on_disk == <e->staging_file_name);
848 lte->resource_entry.original_size = stbuf.st_size;
849 lte->resource_entry.size = stbuf.st_size;
850 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
851 lte->lte_inode = NULL;
852 copy_hash(lte->hash, hash);
853 lookup_table_insert(table, lte);
859 static int inode_close_fds(struct inode *inode)
861 u16 num_opened_fds = inode->num_opened_fds;
862 for (u16 i = 0, j = 0; j < num_opened_fds; i++) {
863 struct wimlib_fd *fd = inode->fds[i];
865 wimlib_assert(fd->f_inode == inode);
866 int ret = close_wimlib_fd(fd);
875 /* Overwrites the WIM file, with changes saved. */
876 static int rebuild_wim(struct wimfs_context *ctx, bool check_integrity)
879 struct lookup_table_entry *lte, *tmp;
880 WIMStruct *w = ctx->wim;
883 DEBUG("Closing all staging file descriptors.");
884 list_for_each_entry_safe(lte, tmp, &ctx->staging_list, staging_list) {
885 ret = inode_close_fds(lte->lte_inode);
890 DEBUG("Calculating SHA1 checksums for all new staging files.");
891 list_for_each_entry_safe(lte, tmp, &ctx->staging_list, staging_list) {
892 ret = update_lte_of_staging_file(lte, ctx->wim->lookup_table);
897 xml_update_image_info(w, w->current_image);
899 ret = wimlib_overwrite(w, check_integrity, 0);
901 ERROR("Failed to commit changes");
907 /* Called when the filesystem is unmounted. */
908 static void wimfs_destroy(void *p)
910 /* For read-write mounts, the `imagex unmount' command, which is
911 * running in a separate process and is executing the
912 * wimlib_unmount() function, will send this process a byte
913 * through a message queue that indicates whether the --commit
914 * option was specified or not. */
917 struct timespec timeout;
919 ssize_t bytes_received;
922 char check_integrity;
925 struct wimfs_context *ctx = wimfs_get_context();
927 if (open_message_queues(ctx, true))
930 if (get_mailbox(ctx->unmount_to_daemon_mq, 2, &msgsize, &mailbox))
931 goto out_close_message_queues;
933 /* Wait at most 3 seconds before giving up and discarding changes. */
934 gettimeofday(&now, NULL);
935 timeout.tv_sec = now.tv_sec + 3;
936 timeout.tv_nsec = now.tv_usec * 1000;
937 DEBUG("Waiting for message telling us whether to commit or not, and "
938 "whether to include integrity checks.");
940 bytes_received = mq_timedreceive(ctx->unmount_to_daemon_mq, mailbox,
941 msgsize, NULL, &timeout);
942 if (bytes_received == -1) {
943 ERROR_WITH_ERRNO("mq_timedreceive()");
944 ERROR("Not committing.");
948 DEBUG("Received message: [%d %d]", mailbox[0], mailbox[1]);
950 check_integrity = mailbox[1];
954 if (ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
956 ret = chdir(ctx->working_directory);
958 status = rebuild_wim(ctx, (check_integrity != 0));
960 ERROR_WITH_ERRNO("chdir()");
961 status = WIMLIB_ERR_NOTDIR;
964 ret = delete_staging_dir(ctx);
966 ERROR("Failed to delete the staging directory");
971 DEBUG("Read-only mount");
973 DEBUG("Sending status %hhd", status);
974 ret = mq_send(ctx->daemon_to_unmount_mq, &status, 1, 1);
976 ERROR_WITH_ERRNO("Failed to send status to unmount process");
978 out_close_message_queues:
979 close_message_queues(ctx);
983 static int wimfs_fallocate(const char *path, int mode,
984 off_t offset, off_t len, struct fuse_file_info *fi)
986 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
987 wimlib_assert(fd->staging_fd != -1);
988 return fallocate(fd->staging_fd, mode, offset, len);
993 static int wimfs_fgetattr(const char *path, struct stat *stbuf,
994 struct fuse_file_info *fi)
996 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
997 return inode_to_stbuf(fd->f_inode, fd->f_lte, stbuf);
1000 static int wimfs_ftruncate(const char *path, off_t size,
1001 struct fuse_file_info *fi)
1003 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1004 int ret = ftruncate(fd->staging_fd, size);
1007 if (fd->f_lte && size < fd->f_lte->resource_entry.original_size)
1008 fd->f_lte->resource_entry.original_size = size;
1013 * Fills in a `struct stat' that corresponds to a file or directory in the WIM.
1015 static int wimfs_getattr(const char *path, struct stat *stbuf)
1017 struct dentry *dentry;
1018 struct lookup_table_entry *lte;
1020 struct wimfs_context *ctx = wimfs_get_context();
1022 ret = lookup_resource(ctx->wim, path,
1023 get_lookup_flags(ctx) | LOOKUP_FLAG_DIRECTORY_OK,
1024 &dentry, <e, NULL);
1027 return inode_to_stbuf(dentry->d_inode, lte, stbuf);
1031 /* Read an alternate data stream through the XATTR interface, or get its size */
1032 static int wimfs_getxattr(const char *path, const char *name, char *value,
1036 struct inode *inode;
1037 struct ads_entry *ads_entry;
1039 struct lookup_table_entry *lte;
1040 struct wimfs_context *ctx = wimfs_get_context();
1042 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1045 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1049 inode = wim_pathname_to_inode(ctx->wim, path);
1053 ads_entry = inode_get_ads_entry(inode, name, NULL);
1057 lte = ads_entry->lte;
1058 res_size = wim_resource_size(lte);
1063 if (res_size > size)
1066 ret = read_full_wim_resource(lte, (u8*)value,
1067 WIMLIB_RESOURCE_FLAG_MULTITHREADED);
1075 /* Create a hard link */
1076 static int wimfs_link(const char *to, const char *from)
1078 struct dentry *from_dentry, *from_dentry_parent;
1079 const char *link_name;
1080 struct inode *inode;
1081 struct lookup_table_entry *lte;
1082 WIMStruct *w = wimfs_get_WIMStruct();
1084 inode = wim_pathname_to_inode(w, to);
1088 if (!inode_is_regular_file(inode))
1091 from_dentry_parent = get_parent_dentry(w, from);
1092 if (!from_dentry_parent)
1094 if (!dentry_is_directory(from_dentry_parent))
1097 link_name = path_basename(from);
1098 if (get_dentry_child_with_name(from_dentry_parent, link_name))
1100 from_dentry = new_dentry(link_name);
1105 inode_add_dentry(from_dentry, inode);
1106 from_dentry->d_inode = inode;
1107 inode->link_count++;
1109 for (unsigned i = 0; i <= inode->num_ads; i++) {
1110 lte = inode_stream_lte_resolved(inode, i);
1115 dentry_add_child(from_dentry_parent, from_dentry);
1120 static int wimfs_listxattr(const char *path, char *list, size_t size)
1123 struct inode *inode;
1124 struct wimfs_context *ctx = wimfs_get_context();
1126 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1129 /* List alternate data streams, or get the list size */
1131 inode = wim_pathname_to_inode(ctx->wim, path);
1137 for (u16 i = 0; i < inode->num_ads; i++)
1138 needed_size += inode->ads_entries[i].stream_name_utf8_len + 6;
1142 for (u16 i = 0; i < inode->num_ads; i++) {
1143 needed_size = inode->ads_entries[i].stream_name_utf8_len + 6;
1144 if (needed_size > size)
1146 p += sprintf(p, "user.%s",
1147 inode->ads_entries[i].stream_name_utf8) + 1;
1148 size -= needed_size;
1156 * Create a directory in the WIM.
1157 * @mode is currently ignored.
1159 static int wimfs_mkdir(const char *path, mode_t mode)
1161 struct dentry *parent;
1162 struct dentry *newdir;
1163 const char *basename;
1164 struct wimfs_context *ctx = wimfs_get_context();
1166 parent = get_parent_dentry(ctx->wim, path);
1170 if (!dentry_is_directory(parent))
1173 basename = path_basename(path);
1174 if (get_dentry_child_with_name(parent, basename))
1177 newdir = new_dentry_with_inode(basename);
1178 newdir->d_inode->attributes |= FILE_ATTRIBUTE_DIRECTORY;
1179 newdir->d_inode->resolved = true;
1180 newdir->d_inode->ino = ctx->next_ino++;
1181 dentry_add_child(parent, newdir);
1185 /* Creates a regular file. */
1186 static int wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
1188 const char *stream_name;
1189 struct wimfs_context *ctx = wimfs_get_context();
1190 if ((ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
1191 && (stream_name = path_stream_name(path))) {
1192 /* Make an alternate data stream */
1193 struct ads_entry *new_entry;
1194 struct inode *inode;
1196 char *p = (char*)stream_name - 1;
1197 wimlib_assert(*p == ':');
1200 inode = wim_pathname_to_inode(ctx->wim, path);
1203 if (!inode_is_regular_file(inode))
1205 if (inode_get_ads_entry(inode, stream_name, NULL))
1207 new_entry = inode_add_ads(inode, stream_name);
1211 struct dentry *dentry, *parent;
1212 const char *basename;
1214 /* Make a normal file (not an alternate data stream) */
1216 /* Make sure that the parent of @path exists and is a directory, and
1217 * that the dentry named by @path does not already exist. */
1218 parent = get_parent_dentry(ctx->wim, path);
1221 if (!dentry_is_directory(parent))
1224 basename = path_basename(path);
1225 if (get_dentry_child_with_name(parent, path))
1228 dentry = new_dentry_with_inode(basename);
1231 dentry->d_inode->resolved = true;
1232 dentry->d_inode->ino = ctx->next_ino++;
1233 dentry_add_child(parent, dentry);
1240 static int wimfs_open(const char *path, struct fuse_file_info *fi)
1242 struct dentry *dentry;
1243 struct lookup_table_entry *lte;
1245 struct wimlib_fd *fd;
1246 struct inode *inode;
1249 struct wimfs_context *ctx = wimfs_get_context();
1251 ret = lookup_resource(ctx->wim, path, get_lookup_flags(ctx),
1252 &dentry, <e, &stream_idx);
1256 inode = dentry->d_inode;
1258 if (stream_idx == 0)
1261 stream_id = inode->ads_entries[stream_idx - 1].stream_id;
1263 /* The file resource may be in the staging directory (read-write mounts
1264 * only) or in the WIM. If it's in the staging directory, we need to
1265 * open a native file descriptor for the corresponding file. Otherwise,
1266 * we can read the file resource directly from the WIM file if we are
1267 * opening it read-only, but we need to extract the resource to the
1268 * staging directory if we are opening it writable. */
1270 if (flags_writable(fi->flags) &&
1271 (!lte || lte->resource_location != RESOURCE_IN_STAGING_FILE)) {
1272 u64 size = (lte) ? wim_resource_size(lte) : 0;
1273 ret = extract_resource_to_staging_dir(inode, stream_id,
1279 ret = alloc_wimlib_fd(inode, stream_id, lte, &fd,
1280 wimfs_ctx_readonly(ctx));
1284 if (lte && lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1285 fd->staging_fd = open(lte->staging_file_name, fi->flags);
1286 if (fd->staging_fd == -1) {
1287 close_wimlib_fd(fd);
1291 fi->fh = (uintptr_t)fd;
1295 /* Opens a directory. */
1296 static int wimfs_opendir(const char *path, struct fuse_file_info *fi)
1298 struct inode *inode;
1300 struct wimlib_fd *fd = NULL;
1301 struct wimfs_context *ctx = wimfs_get_context();
1302 WIMStruct *w = ctx->wim;
1304 inode = wim_pathname_to_inode(w, path);
1307 if (!inode_is_directory(inode))
1309 ret = alloc_wimlib_fd(inode, 0, NULL, &fd, wimfs_ctx_readonly(ctx));
1310 fi->fh = (uintptr_t)fd;
1316 * Read data from a file in the WIM or in the staging directory.
1318 static int wimfs_read(const char *path, char *buf, size_t size,
1319 off_t offset, struct fuse_file_info *fi)
1321 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1326 if (!fd->f_lte) /* Empty stream with no lookup table entry */
1329 if (fd->f_lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1330 /* Read from staging file */
1332 wimlib_assert(fd->f_lte->staging_file_name);
1333 wimlib_assert(fd->staging_fd != -1);
1336 DEBUG("Seek to offset %zu", offset);
1338 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1340 ret = read(fd->staging_fd, buf, size);
1347 wimlib_assert(fd->f_lte->resource_location == RESOURCE_IN_WIM);
1349 u64 res_size = wim_resource_size(fd->f_lte);
1351 if (offset > res_size)
1354 size = min(size, res_size - offset);
1356 if (read_wim_resource(fd->f_lte, (u8*)buf,
1358 WIMLIB_RESOURCE_FLAG_MULTITHREADED) != 0)
1364 struct fill_params {
1366 fuse_fill_dir_t filler;
1369 static int dentry_fuse_fill(struct dentry *dentry, void *arg)
1371 struct fill_params *fill_params = arg;
1372 return fill_params->filler(fill_params->buf, dentry->file_name_utf8,
1376 /* Fills in the entries of the directory specified by @path using the
1377 * FUSE-provided function @filler. */
1378 static int wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
1379 off_t offset, struct fuse_file_info *fi)
1381 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1382 struct inode *inode;
1387 inode = fd->f_inode;
1389 struct fill_params fill_params = {
1394 filler(buf, ".", NULL, 0);
1395 filler(buf, "..", NULL, 0);
1397 return for_dentry_in_rbtree(inode->children.rb_node,
1398 dentry_fuse_fill, &fill_params);
1402 static int wimfs_readlink(const char *path, char *buf, size_t buf_len)
1404 struct wimfs_context *ctx = wimfs_get_context();
1405 struct inode *inode = wim_pathname_to_inode(ctx->wim, path);
1409 if (!inode_is_symlink(inode))
1412 ret = inode_readlink(inode, buf, buf_len, ctx->wim,
1413 WIMLIB_RESOURCE_FLAG_MULTITHREADED);
1420 static int wimfs_release(const char *path, struct fuse_file_info *fi)
1422 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1423 return close_wimlib_fd(fd);
1426 /* Close a directory */
1427 static int wimfs_releasedir(const char *path, struct fuse_file_info *fi)
1429 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1430 return close_wimlib_fd(fd);
1434 /* Remove an alternate data stream through the XATTR interface */
1435 static int wimfs_removexattr(const char *path, const char *name)
1437 struct inode *inode;
1438 struct ads_entry *ads_entry;
1440 struct wimfs_context *ctx = wimfs_get_context();
1442 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1445 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1449 inode = wim_pathname_to_inode(ctx->wim, path);
1453 ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
1456 inode_remove_ads(inode, ads_idx, ctx->wim->lookup_table);
1461 /* Renames a file or directory. See rename (3) */
1462 static int wimfs_rename(const char *from, const char *to)
1466 struct dentry *parent_of_dst;
1467 char *file_name_utf16 = NULL, *file_name_utf8 = NULL;
1468 u16 file_name_utf16_len, file_name_utf8_len;
1469 WIMStruct *w = wimfs_get_WIMStruct();
1472 /* This rename() implementation currently only supports actual files
1473 * (not alternate data streams) */
1475 src = get_dentry(w, from);
1479 dst = get_dentry(w, to);
1482 ret = get_names(&file_name_utf16, &file_name_utf8,
1483 &file_name_utf16_len, &file_name_utf8_len,
1489 /* Destination file exists */
1491 if (src == dst) /* Same file */
1494 if (!dentry_is_directory(src)) {
1495 /* Cannot rename non-directory to directory. */
1496 if (dentry_is_directory(dst))
1499 /* Cannot rename directory to a non-directory or a non-empty
1501 if (!dentry_is_directory(dst))
1503 if (inode_has_children(dst->d_inode))
1506 parent_of_dst = dst->parent;
1507 remove_dentry(dst, w->lookup_table);
1509 /* Destination does not exist */
1510 parent_of_dst = get_parent_dentry(w, to);
1514 if (!dentry_is_directory(parent_of_dst))
1518 FREE(src->file_name);
1519 FREE(src->file_name_utf8);
1520 src->file_name = file_name_utf16;
1521 src->file_name_utf8 = file_name_utf8;
1522 src->file_name_len = file_name_utf16_len;
1523 src->file_name_utf8_len = file_name_utf8_len;
1526 dentry_add_child(parent_of_dst, src);
1530 /* Remove a directory */
1531 static int wimfs_rmdir(const char *path)
1533 struct dentry *dentry;
1534 WIMStruct *w = wimfs_get_WIMStruct();
1536 dentry = get_dentry(w, path);
1540 if (!dentry_is_empty_directory(dentry))
1543 remove_dentry(dentry, w->lookup_table);
1548 /* Write an alternate data stream through the XATTR interface */
1549 static int wimfs_setxattr(const char *path, const char *name,
1550 const char *value, size_t size, int flags)
1552 struct ads_entry *existing_ads_entry;
1553 struct ads_entry *new_ads_entry;
1554 struct lookup_table_entry *existing_lte;
1555 struct lookup_table_entry *lte;
1556 struct inode *inode;
1557 u8 value_hash[SHA1_HASH_SIZE];
1559 struct wimfs_context *ctx = wimfs_get_context();
1561 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1564 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1568 inode = wim_pathname_to_inode(ctx->wim, path);
1572 existing_ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
1573 if (existing_ads_entry) {
1574 if (flags & XATTR_CREATE)
1576 inode_remove_ads(inode, ads_idx, ctx->wim->lookup_table);
1578 if (flags & XATTR_REPLACE)
1581 new_ads_entry = inode_add_ads(inode, name);
1585 sha1_buffer((const u8*)value, size, value_hash);
1587 existing_lte = __lookup_resource(ctx->wim->lookup_table, value_hash);
1594 lte = new_lookup_table_entry();
1597 value_copy = MALLOC(size);
1602 memcpy(value_copy, value, size);
1603 lte->resource_location = RESOURCE_IN_ATTACHED_BUFFER;
1604 lte->attached_buffer = value_copy;
1605 lte->resource_entry.original_size = size;
1606 lte->resource_entry.size = size;
1607 lte->resource_entry.flags = 0;
1608 copy_hash(lte->hash, value_hash);
1609 lookup_table_insert(ctx->wim->lookup_table, lte);
1611 new_ads_entry->lte = lte;
1616 static int wimfs_symlink(const char *to, const char *from)
1618 struct dentry *dentry_parent, *dentry;
1619 const char *link_name;
1620 struct inode *inode;
1621 struct wimfs_context *ctx = wimfs_get_context();
1623 dentry_parent = get_parent_dentry(ctx->wim, from);
1626 if (!dentry_is_directory(dentry_parent))
1629 link_name = path_basename(from);
1631 if (get_dentry_child_with_name(dentry_parent, link_name))
1633 dentry = new_dentry_with_inode(link_name);
1636 inode = dentry->d_inode;
1638 inode->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
1639 inode->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
1640 inode->ino = ctx->next_ino++;
1641 inode->resolved = true;
1643 if (inode_set_symlink(inode, to, ctx->wim->lookup_table, NULL) != 0)
1644 goto out_free_dentry;
1646 dentry_add_child(dentry_parent, dentry);
1649 free_dentry(dentry);
1654 /* Reduce the size of a file */
1655 static int wimfs_truncate(const char *path, off_t size)
1657 struct dentry *dentry;
1658 struct lookup_table_entry *lte;
1662 struct inode *inode;
1663 struct wimfs_context *ctx = wimfs_get_context();
1665 ret = lookup_resource(ctx->wim, path, get_lookup_flags(ctx),
1666 &dentry, <e, &stream_idx);
1671 if (!lte) /* Already a zero-length file */
1674 inode = dentry->d_inode;
1676 if (stream_idx == 0)
1679 stream_id = inode->ads_entries[stream_idx - 1].stream_id;
1681 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1682 wimlib_assert(lte->staging_file_name);
1683 ret = truncate(lte->staging_file_name, size);
1686 lte->resource_entry.original_size = size;
1688 wimlib_assert(lte->resource_location == RESOURCE_IN_WIM);
1689 /* File in WIM. Extract it to the staging directory, but only
1690 * the first @size bytes of it. */
1691 ret = extract_resource_to_staging_dir(inode, stream_id,
1697 /* Remove a regular file */
1698 static int wimfs_unlink(const char *path)
1700 struct dentry *dentry;
1701 struct lookup_table_entry *lte;
1704 struct wimfs_context *ctx = wimfs_get_context();
1706 ret = lookup_resource(ctx->wim, path, get_lookup_flags(ctx),
1707 &dentry, <e, &stream_idx);
1712 if (stream_idx == 0)
1713 remove_dentry(dentry, ctx->wim->lookup_table);
1715 inode_remove_ads(dentry->d_inode, stream_idx - 1,
1716 ctx->wim->lookup_table);
1720 #ifdef HAVE_UTIMENSAT
1722 * Change the timestamp on a file dentry.
1724 * Note that alternate data streams do not have their own timestamps.
1726 static int wimfs_utimens(const char *path, const struct timespec tv[2])
1728 struct dentry *dentry;
1729 struct inode *inode;
1730 WIMStruct *w = wimfs_get_WIMStruct();
1732 dentry = get_dentry(w, path);
1735 inode = dentry->d_inode;
1737 if (tv[0].tv_nsec != UTIME_OMIT) {
1738 if (tv[0].tv_nsec == UTIME_NOW)
1739 inode->last_access_time = get_wim_timestamp();
1741 inode->last_access_time = timespec_to_wim_timestamp(&tv[0]);
1743 if (tv[1].tv_nsec != UTIME_OMIT) {
1744 if (tv[1].tv_nsec == UTIME_NOW)
1745 inode->last_write_time = get_wim_timestamp();
1747 inode->last_write_time = timespec_to_wim_timestamp(&tv[1]);
1752 static int wimfs_utime(const char *path, struct utimbuf *times)
1754 struct dentry *dentry;
1755 struct inode *inode;
1756 WIMStruct *w = wimfs_get_WIMStruct();
1758 dentry = get_dentry(w, path);
1761 inode = dentry->d_inode;
1763 inode->last_write_time = unix_timestamp_to_wim(times->modtime);
1764 inode->last_access_time = unix_timestamp_to_wim(times->actime);
1769 /* Writes to a file in the WIM filesystem.
1770 * It may be an alternate data stream, but here we don't even notice because we
1771 * just get a lookup table entry. */
1772 static int wimfs_write(const char *path, const char *buf, size_t size,
1773 off_t offset, struct fuse_file_info *fi)
1775 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1782 wimlib_assert(fd->f_lte);
1783 wimlib_assert(fd->f_lte->staging_file_name);
1784 wimlib_assert(fd->staging_fd != -1);
1785 wimlib_assert(fd->f_inode);
1787 /* Seek to the requested position */
1788 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1791 /* Write the data. */
1792 ret = write(fd->staging_fd, buf, size);
1796 now = get_wim_timestamp();
1797 fd->f_inode->last_write_time = now;
1798 fd->f_inode->last_access_time = now;
1802 static struct fuse_operations wimfs_operations = {
1803 .access = wimfs_access,
1804 .chmod = wimfs_chmod,
1805 .destroy = wimfs_destroy,
1807 .fallocate = wimfs_fallocate,
1809 .fgetattr = wimfs_fgetattr,
1810 .ftruncate = wimfs_ftruncate,
1811 .getattr = wimfs_getattr,
1813 .getxattr = wimfs_getxattr,
1817 .listxattr = wimfs_listxattr,
1819 .mkdir = wimfs_mkdir,
1820 .mknod = wimfs_mknod,
1822 .opendir = wimfs_opendir,
1824 .readdir = wimfs_readdir,
1825 .readlink = wimfs_readlink,
1826 .release = wimfs_release,
1827 .releasedir = wimfs_releasedir,
1829 .removexattr = wimfs_removexattr,
1831 .rename = wimfs_rename,
1832 .rmdir = wimfs_rmdir,
1834 .setxattr = wimfs_setxattr,
1836 .symlink = wimfs_symlink,
1837 .truncate = wimfs_truncate,
1838 .unlink = wimfs_unlink,
1839 #ifdef HAVE_UTIMENSAT
1840 .utimens = wimfs_utimens,
1842 .utime = wimfs_utime,
1844 .write = wimfs_write,
1846 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 8)
1847 .flag_nullpath_ok = 1,
1849 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 9)
1851 .flag_utime_omit_ok = 1,
1856 /* Mounts a WIM file. */
1857 WIMLIBAPI int wimlib_mount(WIMStruct *wim, int image, const char *dir,
1858 int flags, WIMStruct **additional_swms,
1859 unsigned num_additional_swms)
1865 struct lookup_table *joined_tab, *wim_tab_save;
1866 struct image_metadata *imd;
1867 struct wimfs_context ctx;
1869 DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
1870 wim, image, dir, flags);
1873 return WIMLIB_ERR_INVALID_PARAM;
1875 ret = verify_swm_set(wim, additional_swms, num_additional_swms);
1879 if ((flags & WIMLIB_MOUNT_FLAG_READWRITE) && (wim->hdr.total_parts != 1)) {
1880 ERROR("Cannot mount a split WIM read-write");
1881 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
1884 if (num_additional_swms) {
1885 ret = new_joined_lookup_table(wim, additional_swms,
1886 num_additional_swms,
1890 wim_tab_save = wim->lookup_table;
1891 wim->lookup_table = joined_tab;
1894 ret = select_wim_image(wim, image);
1899 imd = &wim->image_metadata[image - 1];
1901 DEBUG("Selected image %d", image);
1903 if (imd->root_dentry->refcnt != 1) {
1904 ERROR("Cannot mount image that was just exported with "
1905 "wimlib_export_image()");
1906 ret = WIMLIB_ERR_INVALID_PARAM;
1910 if (imd->modified) {
1911 ERROR("Cannot mount image that was added "
1912 "with wimlib_add_image()");
1913 ret = WIMLIB_ERR_INVALID_PARAM;
1917 if (!(flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
1918 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
1919 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
1920 flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1923 DEBUG("Initializing struct wimfs_context");
1924 init_wimfs_context(&ctx);
1926 DEBUG("Getting current directory");
1927 ctx.working_directory = getcwd(NULL, 0);
1928 if (!ctx.working_directory) {
1929 ERROR_WITH_ERRNO("Could not determine current directory");
1930 ret = WIMLIB_ERR_NOTDIR;
1934 DEBUG("Unlinking message queues in case they already exist");
1935 ret = set_message_queue_names(&ctx, dir);
1937 goto out_free_working_directory;
1938 unlink_message_queues(&ctx);
1940 DEBUG("Preparing arguments to fuse_main()");
1942 dir_copy = STRDUP(dir);
1944 goto out_free_message_queue_names;
1946 argv[argc++] = "imagex";
1947 argv[argc++] = dir_copy;
1949 /* disable multi-threaded operation for read-write mounts */
1950 if (flags & WIMLIB_MOUNT_FLAG_READWRITE)
1951 argv[argc++] = "-s";
1953 if (flags & WIMLIB_MOUNT_FLAG_DEBUG)
1954 argv[argc++] = "-d";
1957 * We provide the use_ino option because we are going to assign inode
1958 * numbers oursides. The inodes will be given unique numbers in the
1959 * assign_inode_numbers() function, and the static variable @next_ino is
1960 * set to the next available inode number.
1962 char optstring[256] = "use_ino,subtype=wimfs,attr_timeout=0";
1963 argv[argc++] = "-o";
1964 argv[argc++] = optstring;
1965 if ((flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
1966 /* Read-write mount. Make the staging directory */
1967 ret = make_staging_dir(&ctx);
1969 goto out_free_dir_copy;
1971 /* Read-only mount */
1972 strcat(optstring, ",ro");
1979 DEBUG("FUSE command line (argc = %d): ", argc);
1980 for (i = 0; i < argc; i++) {
1981 fputs(argv[i], stdout);
1989 /* Mark dentry tree as modified if read-write mount. */
1990 if (flags & WIMLIB_MOUNT_FLAG_READWRITE)
1991 imd->modified = true;
1993 /* Resolve all the lookup table entries of the dentry tree */
1994 DEBUG("Resolving lookup table entries");
1995 for_dentry_in_tree(imd->root_dentry, dentry_resolve_ltes,
1998 ctx.next_ino = assign_inode_numbers(&imd->inode_list);
1999 DEBUG("(next_ino = %"PRIu64")", ctx.next_ino);
2001 /* Finish initializing the filesystem context. */
2003 ctx.mount_flags = flags;
2005 DEBUG("Calling fuse_main()");
2007 ret = fuse_main(argc, argv, &wimfs_operations, &ctx);
2009 DEBUG("Returned from fuse_main() (ret = %d)", ret);
2011 ret = WIMLIB_ERR_FUSE;
2014 out_free_message_queue_names:
2015 free_message_queue_names(&ctx);
2016 out_free_working_directory:
2017 FREE(ctx.working_directory);
2018 ctx.working_directory = NULL;
2020 if (num_additional_swms) {
2021 free_lookup_table(wim->lookup_table);
2022 wim->lookup_table = wim_tab_save;
2029 * Unmounts the WIM file that was previously mounted on @dir by using
2032 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
2039 struct timespec timeout;
2041 struct wimfs_context ctx;
2044 init_wimfs_context(&ctx);
2046 /* Open message queues between the unmount process and the
2047 * filesystem daemon. */
2048 ret = set_message_queue_names(&ctx, dir);
2052 ret = open_message_queues(&ctx, false);
2054 goto out_free_message_queue_names;
2056 /* Send a message to the filesystem daemon saying whether to commit or
2058 msg[0] = (flags & WIMLIB_UNMOUNT_FLAG_COMMIT) ? 1 : 0;
2059 msg[1] = (flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY) ? 1 : 0;
2061 DEBUG("Sending message: %scommit, %scheck",
2062 (msg[0] ? "" : "don't "),
2063 (msg[1] ? "" : "don't "));
2064 ret = mq_send(ctx.unmount_to_daemon_mq, msg, 2, 1);
2066 ERROR_WITH_ERRNO("Failed to notify filesystem daemon whether "
2067 "we want to commit changes or not");
2068 ret = WIMLIB_ERR_MQUEUE;
2069 goto out_close_message_queues;
2072 /* Execute `fusermount -u', which is installed setuid root, to unmount
2075 * FUSE does not yet implement synchronous unmounts. This means that
2076 * fusermount -u will return before the filesystem daemon returns from
2077 * wimfs_destroy(). This is partly what we want, because we need to
2078 * send a message from this process to the filesystem daemon telling
2079 * whether --commit was specified or not. However, after that, the
2080 * unmount process must wait for the filesystem daemon to finish writing
2086 ERROR_WITH_ERRNO("Failed to fork()");
2087 ret = WIMLIB_ERR_FORK;
2088 goto out_close_message_queues;
2092 execlp("fusermount", "fusermount", "-u", dir, NULL);
2093 ERROR_WITH_ERRNO("Failed to execute `fusermount'");
2094 exit(WIMLIB_ERR_FUSERMOUNT);
2098 ret = wait(&status);
2100 ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
2102 ret = WIMLIB_ERR_FUSERMOUNT;
2103 goto out_close_message_queues;
2107 ERROR("fusermount exited with status %d", status);
2109 /* Try again, but with the `umount' program. This is required
2110 * on other FUSE implementations such as FreeBSD's that do not
2111 * have a `fusermount' program. */
2115 ERROR_WITH_ERRNO("Failed to fork()");
2116 ret = WIMLIB_ERR_FORK;
2117 goto out_close_message_queues;
2121 execlp("umount", "umount", dir, NULL);
2122 ERROR_WITH_ERRNO("Failed to execute `umount'");
2123 exit(WIMLIB_ERR_FUSERMOUNT);
2127 ret = wait(&status);
2129 ERROR_WITH_ERRNO("Failed to wait for `umount' process to "
2131 ret = WIMLIB_ERR_FUSERMOUNT;
2132 goto out_close_message_queues;
2135 ERROR("`umount' exited with failure status");
2136 ret = WIMLIB_ERR_FUSERMOUNT;
2137 goto out_close_message_queues;
2141 wimlib_assert(status == 0);
2143 /* Wait for a message from the filesytem daemon indicating whether the
2144 * filesystem was unmounted successfully (0) or an error occurred (1).
2145 * This may take a long time if a big WIM file needs to be rewritten. */
2147 /* Wait at most 600??? seconds before giving up and returning false.
2148 * Either it's a really big WIM file, or (more likely) the
2149 * filesystem daemon has crashed or failed for some reason.
2151 * XXX come up with some method to determine if the filesystem
2152 * daemon has really crashed or not.
2154 * XXX Idea: have mount daemon write its PID into the WIM file header?
2155 * No, this wouldn't work because we know the directory but not the WIM
2159 gettimeofday(&now, NULL);
2160 timeout.tv_sec = now.tv_sec + 600;
2161 timeout.tv_nsec = now.tv_usec * 1000;
2163 ret = get_mailbox(ctx.daemon_to_unmount_mq, 2, &msgsize, &mailbox);
2165 goto out_close_message_queues;
2168 DEBUG("Waiting for message telling us whether the unmount was "
2169 "successful or not.");
2170 ret = mq_timedreceive(ctx.daemon_to_unmount_mq, mailbox,
2171 msgsize, NULL, &timeout);
2173 if (errno == ETIMEDOUT) {
2174 ERROR("Timed out- probably the filesystem daemon "
2175 "crashed and the WIM was not written "
2177 ret = WIMLIB_ERR_TIMEOUT;
2179 ERROR_WITH_ERRNO("mq_receive()");
2180 ret = WIMLIB_ERR_MQUEUE;
2182 goto out_free_mailbox;
2185 DEBUG("Received message: Unmount %s", (mailbox[0] ? "Failed" : "Ok"));
2188 ERROR("Unmount failed");
2191 out_close_message_queues:
2192 close_message_queues(&ctx);
2193 out_free_message_queue_names:
2194 free_message_queue_names(&ctx);
2199 #else /* WITH_FUSE */
2202 static inline int mount_unsupported_error()
2204 ERROR("wimlib was compiled with --without-fuse, which disables support "
2205 "for mounting WIMs.");
2206 return WIMLIB_ERR_UNSUPPORTED;
2209 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
2211 return mount_unsupported_error();
2214 WIMLIBAPI int wimlib_mount(WIMStruct *wim_p, int image, const char *dir,
2215 int flags, WIMStruct **additional_swms,
2216 unsigned num_additional_swms)
2218 return mount_unsupported_error();
2221 #endif /* WITH_FUSE */