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"
41 #define FUSE_USE_VERSION 26
51 #include <attr/xattr.h>
54 /* File descriptor to a file open on the WIM filesystem. */
57 struct lookup_table_entry *lte;
63 /* The WIMStruct for the mounted WIM. */
66 /* Working directory when `imagex mount' is run. */
67 static const char *working_directory;
69 /* Name of the staging directory for a read-write mount. Whenever a new file is
70 * created, it is done so in the staging directory. Furthermore, whenever a
71 * file in the WIM is modified, it is extracted to the staging directory. If
72 * changes are commited when the WIM is unmounted, the file resources are merged
73 * in from the staging directory when writing the new WIM. */
74 static char *staging_dir_name;
75 static size_t staging_dir_name_len;
77 /* Flags passed to wimlib_mount(). */
78 static int mount_flags;
80 /* Name of the directory on which the WIM file is mounted. */
81 static const char *mount_dir;
83 /* Next inode number to be assigned. */
86 /* List of lookup table entries in the staging directory */
87 static LIST_HEAD(staging_list);
89 static inline int get_lookup_flags()
91 if (mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
92 return LOOKUP_FLAG_ADS_OK;
97 /* Returns nonzero if write permission is requested on the file open flags */
98 static inline int flags_writable(int open_flags)
100 return open_flags & (O_RDWR | O_WRONLY);
103 static int alloc_wimlib_fd(struct inode *inode,
105 struct lookup_table_entry *lte,
106 struct wimlib_fd **fd_ret)
108 static const u16 fds_per_alloc = 8;
109 static const u16 max_fds = 0xffff;
111 DEBUG("Allocating fd for stream ID %u from inode %lx (open = %u, allocated = %u)",
112 stream_id, inode->ino, inode->num_opened_fds,
113 inode->num_allocated_fds);
115 if (inode->num_opened_fds == inode->num_allocated_fds) {
116 struct wimlib_fd **fds;
119 if (inode->num_allocated_fds == max_fds)
121 num_new_fds = min(fds_per_alloc, max_fds - inode->num_allocated_fds);
123 fds = REALLOC(inode->fds, (inode->num_allocated_fds + num_new_fds) *
124 sizeof(inode->fds[0]));
127 memset(&fds[inode->num_allocated_fds], 0,
128 num_new_fds * sizeof(fds[0]));
130 inode->num_allocated_fds += num_new_fds;
132 for (u16 i = 0; ; i++) {
133 if (!inode->fds[i]) {
134 struct wimlib_fd *fd = CALLOC(1, sizeof(*fd));
141 fd->stream_id = stream_id;
144 inode->num_opened_fds++;
146 lte->num_opened_fds++;
147 DEBUG("Allocated fd");
153 static void inode_put_fd(struct inode *inode, struct wimlib_fd *fd)
156 wimlib_assert(inode);
157 wimlib_assert(fd->inode == inode);
158 wimlib_assert(inode->num_opened_fds);
159 wimlib_assert(fd->idx < inode->num_opened_fds);
160 wimlib_assert(inode->fds[fd->idx] == fd);
162 inode->fds[fd->idx] = NULL;
164 if (--inode->num_opened_fds == 0 && inode->link_count == 0)
168 static int lte_put_fd(struct lookup_table_entry *lte, struct wimlib_fd *fd)
171 wimlib_assert(fd->lte == lte);
173 if (!lte) /* Empty stream with no lookup table entry */
176 wimlib_assert(lte->num_opened_fds);
178 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
179 wimlib_assert(lte->staging_file_name);
180 wimlib_assert(fd->staging_fd != -1);
181 if (close(fd->staging_fd) != 0) {
182 ERROR_WITH_ERRNO("Failed to close staging file");
186 lte_decrement_num_opened_fds(lte, w->lookup_table);
190 static int close_wimlib_fd(struct wimlib_fd *fd)
193 DEBUG("Closing fd (inode = %lu, opened = %u, allocated = %u)",
194 fd->inode->ino, fd->inode->num_opened_fds,
195 fd->inode->num_allocated_fds);
196 ret = lte_put_fd(fd->lte, fd);
200 inode_put_fd(fd->inode, fd);
204 /* Remove a dentry; i.e. remove a reference to the inode.
206 * If there are no remaining references to the inode either through detnries or
207 * open file descriptors, the inode is freed.
209 * All lookup table entries referenced by the inode have their reference count
210 * decremented. If a lookup table entry has no open file descriptors and no
211 * references remaining, it is freed, and the staging file is unlinked.
213 * Otherwise, the inode is not removed, but the dentry is unlinked and freed. */
214 static void remove_dentry(struct dentry *dentry,
215 struct lookup_table *lookup_table)
217 struct inode *inode = dentry->inode;
218 struct lookup_table_entry *lte;
221 for (i = 0; i <= inode->num_ads; i++) {
222 lte = inode_stream_lte_resolved(inode, i);
224 lte_decrement_refcnt(lte, lookup_table);
226 unlink_dentry(dentry);
230 /* Creates a new staging file and returns its file descriptor opened for
233 * @name_ret: A location into which the a pointer to the newly allocated name of
234 * the staging file is stored.
235 * @return: The file descriptor for the new file. Returns -1 and sets errno on
236 * error, for any reason possible from the creat() function.
238 static int create_staging_file(char **name_ret, int open_flags)
246 name_len = staging_dir_name_len + 1 + SHA1_HASH_SIZE;
247 name = MALLOC(name_len + 1);
255 memcpy(name, staging_dir_name, staging_dir_name_len);
256 name[staging_dir_name_len] = '/';
257 randomize_char_array_with_alnum(name + staging_dir_name_len + 1,
259 name[name_len] = '\0';
262 /* Just in case, verify that the randomly generated name doesn't name an
263 * existing file, and try again if so */
264 } while (stat(name, &stbuf) == 0);
270 /* doesn't exist--- ok */
272 DEBUG("Creating staging file `%s'", name);
274 fd = open(name, open_flags | O_CREAT | O_TRUNC, 0600);
286 * Extract a WIM resource to the staging directory.
288 * @inode: Inode that contains the stream we are extracting
290 * @stream_id: Identifier for the stream (it stays constant even if the indices
291 * of the stream entries are changed)
293 * @lte: Pointer to pointer to the lookup table entry for the stream we need to
294 * extract, or NULL if there was no lookup table entry present for the stream
296 * @size: Number of bytes of the stream we want to extract (this supports the
297 * wimfs_truncate() function).
299 static int extract_resource_to_staging_dir(struct inode *inode,
301 struct lookup_table_entry **lte,
304 char *staging_file_name;
307 struct lookup_table_entry *old_lte, *new_lte;
310 fd = create_staging_file(&staging_file_name, O_WRONLY);
315 ret = extract_wim_resource_to_fd(old_lte, fd, size);
318 if (ret != 0 || close(fd) != 0) {
324 goto out_delete_staging_file;
327 if (old_lte && inode->link_count == old_lte->refcnt) {
328 /* The reference count of the existing lookup table
329 * entry is the same as the link count of the inode that
330 * contains the stream we're opening. Therefore, ALL
331 * the references to the lookup table entry correspond
332 * to the stream we're trying to extract, so the lookup
333 * table entry can be re-used. */
334 DEBUG("Re-using lookup table entry");
335 lookup_table_unlink(w->lookup_table, old_lte);
339 /* There's an existing lookup table entry, but its
340 * reference count is creater than the link count for
341 * the inode containing a stream we're opening.
342 * Therefore, we need to split the lookup table entry.
344 wimlib_assert(old_lte->refcnt > inode->link_count);
345 DEBUG("Splitting lookup table entry "
346 "(inode->link_count = %zu, old_lte->refcnt = %u)",
347 inode->link_count, old_lte->refcnt);
351 new_lte = new_lookup_table_entry();
354 goto out_delete_staging_file;
357 /* There may already be open file descriptors to this stream if
358 * it's previously been opened read-only, but just now we're
359 * opening it read-write. Identify those file descriptors and
360 * change their lookup table entry pointers to point to the new
361 * lookup table entry, and open staging file descriptors for
364 * At the same time, we need to count the number of these opened
365 * file descriptors to the new lookup table entry. If there's
366 * an old lookup table entry, this number needs to be subtracted
367 * from the fd's opened to the old entry. */
368 for (u16 i = 0, j = 0; j < inode->num_opened_fds; i++) {
369 struct wimlib_fd *fd = inode->fds[i];
371 if (fd->stream_id == stream_id) {
372 wimlib_assert(fd->lte == old_lte);
374 new_lte->num_opened_fds++;
375 fd->staging_fd = open(staging_file_name, O_RDONLY);
376 if (fd->staging_fd == -1) {
378 goto out_revert_fd_changes;
384 DEBUG("%zu fd's were already opened to the file we extracted",
385 new_lte->num_opened_fds);
387 old_lte->num_opened_fds -= new_lte->num_opened_fds;
388 old_lte->refcnt -= inode->link_count;
392 new_lte->resource_entry.original_size = size;
393 new_lte->refcnt = inode->link_count;
394 new_lte->staging_file_name = staging_file_name;
395 new_lte->resource_location = RESOURCE_IN_STAGING_FILE;
396 new_lte->inode = inode;
397 random_hash(new_lte->hash);
400 inode->lte = new_lte;
402 for (u16 i = 0; i < inode->num_ads; i++)
403 if (inode->ads_entries[i].stream_id == stream_id)
404 inode->ads_entries[i].lte = new_lte;
406 lookup_table_insert(w->lookup_table, new_lte);
407 list_add(&new_lte->staging_list, &staging_list);
410 out_revert_fd_changes:
411 for (u16 i = 0, j = 0; j < new_lte->num_opened_fds; i++) {
412 struct wimlib_fd *fd = inode->fds[i];
413 if (fd && fd->stream_id == stream_id && fd->lte == new_lte) {
415 if (fd->staging_fd != -1) {
416 close(fd->staging_fd);
423 free_lookup_table_entry(new_lte);
424 out_delete_staging_file:
425 unlink(staging_file_name);
426 FREE(staging_file_name);
431 * Creates a randomly named staging directory and returns its name into the
432 * static variable staging_dir_name.
434 * If the staging directory cannot be created, staging_dir_name is set to NULL.
436 static void make_staging_dir()
438 /* XXX Give the user an option of where to stage files */
440 static char prefix[] = "wimlib-staging-";
441 static const size_t prefix_len = 15;
442 static const size_t suffix_len = 10;
444 size_t pwd_len = strlen(working_directory);
446 staging_dir_name_len = pwd_len + 1 + prefix_len + suffix_len;
448 staging_dir_name = MALLOC(staging_dir_name_len + 1);
449 if (!staging_dir_name) {
450 ERROR("Out of memory");
454 memcpy(staging_dir_name, working_directory, pwd_len);
455 staging_dir_name[pwd_len] = '/';
456 memcpy(staging_dir_name + pwd_len + 1, prefix, prefix_len);
457 randomize_char_array_with_alnum(staging_dir_name + pwd_len + 1 + prefix_len,
459 staging_dir_name[staging_dir_name_len] = '\0';
461 if (mkdir(staging_dir_name, 0700) != 0) {
462 ERROR_WITH_ERRNO("Failed to create temporary directory `%s'",
464 FREE(staging_dir_name);
465 staging_dir_name = NULL;
469 static int remove_file_or_directory(const char *fpath, const struct stat *sb,
470 int typeflag, struct FTW *ftwbuf)
472 if (remove(fpath) == 0)
475 return WIMLIB_ERR_DELETE_STAGING_DIR;
480 * Deletes the staging directory and all the files contained in it.
482 static int delete_staging_dir()
486 ret = nftw(staging_dir_name, remove_file_or_directory,10, FTW_DEPTH);
487 staging_dir_name = NULL;
491 /* Name and message queue descriptors for message queues between the filesystem
492 * daemon process and the unmount process. These are used when the filesystem
493 * is unmounted and the process running wimlib_mount() (i.e. the `imagex
494 * unmount' command) needs to communicate with the filesystem daemon running
495 * fuse_main() (i.e. that spawned by the `imagex mount' or `imagex mountrw'
497 static char *unmount_to_daemon_mq_name;
498 static char *daemon_to_unmount_mq_name;
499 static mqd_t unmount_to_daemon_mq;
500 static mqd_t daemon_to_unmount_mq;
502 /* Simple function that returns the concatenation of 4 strings. */
503 static char *strcat_dup(const char *s1, const char *s2, const char *s3,
506 size_t len = strlen(s1) + strlen(s2) + strlen(s3) + strlen(s4) + 1;
507 char *p = MALLOC(len);
513 return strcat(p, s4);
516 /* Removes trailing forward slashes in a string. */
517 static void remove_trailing_slashes(char *s)
519 long len = strlen(s);
520 for (long i = len - 1; i >= 1; i--) {
528 /* Changes forward slashes to underscores in a string. */
529 static void s_slashes_underscores_g(char *s)
539 * Opens two POSIX message queue: one for sending messages from the unmount
540 * process to the daemon process, and one to go the other way. The names of the
541 * message queues, which must be system-wide unique, are be based on the mount
542 * point. (There of course is still a possibility of a collision if one were to
543 * unmount two identically named directories simultaneously...)
545 * @daemon specifies whether the calling process is the filesystem daemon or the
548 static int open_message_queues(bool daemon)
550 static const char *slash = "/";
551 static const char *prefix = "wimlib-";
552 static const char *u2d_suffix = "unmount-to-daemon-mq";
553 static const char *d2u_suffix = "daemon-to-unmount-mq";
555 const char *mount_dir_basename = path_basename(mount_dir);
559 unmount_to_daemon_mq_name = strcat_dup(slash, mount_dir_basename,
561 if (!unmount_to_daemon_mq_name) {
562 ERROR("Out of memory");
563 return WIMLIB_ERR_NOMEM;
565 daemon_to_unmount_mq_name = strcat_dup(slash, mount_dir_basename,
567 if (!daemon_to_unmount_mq_name) {
568 ERROR("Out of memory");
569 ret = WIMLIB_ERR_NOMEM;
573 remove_trailing_slashes(unmount_to_daemon_mq_name);
574 remove_trailing_slashes(daemon_to_unmount_mq_name);
575 s_slashes_underscores_g(unmount_to_daemon_mq_name + 1);
576 s_slashes_underscores_g(daemon_to_unmount_mq_name + 1);
579 flags = O_RDONLY | O_CREAT;
581 flags = O_WRONLY | O_CREAT;
583 unmount_to_daemon_mq = mq_open(unmount_to_daemon_mq_name, flags,
586 if (unmount_to_daemon_mq == (mqd_t)-1) {
587 ERROR_WITH_ERRNO("mq_open()");
588 ret = WIMLIB_ERR_MQUEUE;
593 flags = O_WRONLY | O_CREAT;
595 flags = O_RDONLY | O_CREAT;
597 daemon_to_unmount_mq = mq_open(daemon_to_unmount_mq_name, flags,
600 if (daemon_to_unmount_mq == (mqd_t)-1) {
601 ERROR_WITH_ERRNO("mq_open()");
602 ret = WIMLIB_ERR_MQUEUE;
607 mq_close(unmount_to_daemon_mq);
608 mq_unlink(unmount_to_daemon_mq_name);
610 FREE(daemon_to_unmount_mq_name);
612 FREE(unmount_to_daemon_mq_name);
616 static int mq_get_msgsize(mqd_t mq)
618 static const char *msgsize_max_file = "/proc/sys/fs/mqueue/msgsize_max";
623 if (mq_getattr(unmount_to_daemon_mq, &attr) == 0) {
624 msgsize = attr.mq_msgsize;
626 ERROR_WITH_ERRNO("mq_getattr()");
627 ERROR("Attempting to read %s", msgsize_max_file);
628 fp = fopen(msgsize_max_file, "rb");
630 if (fscanf(fp, "%d", &msgsize) != 1) {
631 ERROR("Assuming message size of 8192");
636 ERROR_WITH_ERRNO("Failed to open the file `%s'",
638 ERROR("Assuming message size of 8192");
645 /* Closes the message queues, which are allocated in static variables */
646 static void close_message_queues()
648 mq_close(unmount_to_daemon_mq);
649 mq_close(daemon_to_unmount_mq);
650 mq_unlink(unmount_to_daemon_mq_name);
651 mq_unlink(daemon_to_unmount_mq_name);
654 static int wimfs_access(const char *path, int mask)
656 /* Permissions not implemented */
660 static int update_lte_of_staging_file(struct lookup_table_entry *lte,
661 struct lookup_table *table)
663 struct lookup_table_entry *duplicate_lte;
665 u8 hash[SHA1_HASH_SIZE];
668 wimlib_assert(lte->resource_location == RESOURCE_IN_STAGING_FILE);
669 wimlib_assert(lte->staging_file_name);
671 ret = sha1sum(lte->staging_file_name, hash);
675 lookup_table_unlink(table, lte);
677 duplicate_lte = __lookup_resource(table, hash);
680 /* Merge duplicate lookup table entries */
681 duplicate_lte->refcnt += lte->refcnt;
682 list_del(<e->staging_list);
683 free_lookup_table_entry(lte);
685 if (stat(lte->staging_file_name, &stbuf) != 0) {
686 ERROR_WITH_ERRNO("Failed to stat `%s'", lte->staging_file_name);
687 return WIMLIB_ERR_STAT;
689 wimlib_assert(<e->file_on_disk == <e->staging_file_name);
690 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
691 copy_hash(lte->hash, hash);
692 lte->resource_entry.original_size = stbuf.st_size;
693 lte->resource_entry.size = stbuf.st_size;
695 lookup_table_insert(table, lte);
701 static int inode_close_fds(struct inode *inode)
703 for (u16 i = 0, j = 0; j < inode->num_opened_fds; i++) {
704 struct wimlib_fd *fd = inode->fds[i];
706 wimlib_assert(fd->inode == inode);
707 int ret = close_wimlib_fd(fd);
716 /*static int dentry_close_fds(struct dentry *dentry, void *ignore)*/
718 /*return inode_close_fds(dentry->inode);*/
721 /* Overwrites the WIM file, with changes saved. */
722 static int rebuild_wim(WIMStruct *w, bool check_integrity)
725 struct lookup_table_entry *lte, *tmp;
728 DEBUG("Closing all staging file descriptors.");
729 list_for_each_entry(lte, &staging_list, staging_list) {
730 ret = inode_close_fds(lte->inode);
734 /*ret = for_dentry_in_tree(wim_root_dentry(w), dentry_close_fds, NULL);*/
738 DEBUG("Calculating SHA1 checksums for all new staging files.");
739 list_for_each_entry_safe(lte, tmp, &staging_list, staging_list) {
740 ret = update_lte_of_staging_file(lte, w->lookup_table);
745 xml_update_image_info(w, w->current_image);
747 ret = wimlib_overwrite(w, check_integrity);
749 ERROR("Failed to commit changes");
755 /* Called when the filesystem is unmounted. */
756 static void wimfs_destroy(void *p)
758 /* For read-write mounts, the `imagex unmount' command, which is
759 * running in a separate process and is executing the
760 * wimlib_unmount() function, will send this process a byte
761 * through a message queue that indicates whether the --commit
762 * option was specified or not. */
765 struct timespec timeout;
767 ssize_t bytes_received;
770 char check_integrity;
773 ret = open_message_queues(true);
777 msgsize = mq_get_msgsize(unmount_to_daemon_mq);
782 /* Wait at most 3 seconds before giving up and discarding changes. */
783 gettimeofday(&now, NULL);
784 timeout.tv_sec = now.tv_sec + 3;
785 timeout.tv_nsec = now.tv_usec * 1000;
786 DEBUG("Waiting for message telling us whether to commit or not, and "
787 "whether to include integrity checks.");
789 bytes_received = mq_timedreceive(unmount_to_daemon_mq, msg,
790 msgsize, NULL, &timeout);
792 check_integrity = msg[1];
793 if (bytes_received == -1) {
794 if (errno == ETIMEDOUT) {
797 ERROR_WITH_ERRNO("mq_timedreceive()");
799 ERROR("Not committing.");
801 DEBUG("Received message: [%d %d]", msg[0], msg[1]);
805 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
807 status = chdir(working_directory);
809 ERROR_WITH_ERRNO("chdir()");
810 status = WIMLIB_ERR_NOTDIR;
813 status = rebuild_wim(w, (check_integrity != 0));
815 ret = delete_staging_dir();
817 ERROR_WITH_ERRNO("Failed to delete the staging "
823 DEBUG("Read-only mount");
826 DEBUG("Sending status %u", status);
827 ret = mq_send(daemon_to_unmount_mq, &status, 1, 1);
829 ERROR_WITH_ERRNO("Failed to send status to unmount process");
830 close_message_queues();
834 static int wimfs_fallocate(const char *path, int mode,
835 off_t offset, off_t len, struct fuse_file_info *fi)
837 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
838 wimlib_assert(fd->staging_fd != -1);
839 return fallocate(fd->staging_fd, mode, offset, len);
844 static int wimfs_fgetattr(const char *path, struct stat *stbuf,
845 struct fuse_file_info *fi)
847 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
848 return inode_to_stbuf(fd->inode, fd->lte, stbuf);
851 static int wimfs_ftruncate(const char *path, off_t size,
852 struct fuse_file_info *fi)
854 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
855 int ret = ftruncate(fd->staging_fd, size);
858 if (fd->lte && size < fd->lte->resource_entry.original_size)
859 fd->lte->resource_entry.original_size = size;
864 * Fills in a `struct stat' that corresponds to a file or directory in the WIM.
866 static int wimfs_getattr(const char *path, struct stat *stbuf)
868 struct dentry *dentry;
869 struct lookup_table_entry *lte;
872 ret = lookup_resource(w, path,
873 get_lookup_flags() | LOOKUP_FLAG_DIRECTORY_OK,
874 &dentry, <e, NULL);
877 return inode_to_stbuf(dentry->inode, lte, stbuf);
881 /* Read an alternate data stream through the XATTR interface, or get its size */
882 static int wimfs_getxattr(const char *path, const char *name, char *value,
886 struct dentry *dentry;
887 struct ads_entry *ads_entry;
889 struct lookup_table_entry *lte;
891 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
894 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
898 dentry = get_dentry(w, path);
901 ads_entry = inode_get_ads_entry(dentry->inode, name, NULL);
905 lte = ads_entry->lte;
906 res_size = wim_resource_size(lte);
912 ret = read_full_wim_resource(lte, (u8*)value);
919 /* Create a hard link */
920 static int wimfs_link(const char *to, const char *from)
922 struct dentry *to_dentry, *from_dentry, *from_dentry_parent;
923 const char *link_name;
926 struct lookup_table_entry *lte;
928 to_dentry = get_dentry(w, to);
932 inode = to_dentry->inode;
934 if (!inode_is_regular_file(inode))
937 from_dentry_parent = get_parent_dentry(w, from);
938 if (!from_dentry_parent)
940 if (!dentry_is_directory(from_dentry_parent))
943 link_name = path_basename(from);
944 if (get_dentry_child_with_name(from_dentry_parent, link_name))
946 from_dentry = new_dentry(link_name);
951 inode_add_dentry(from_dentry, inode);
952 from_dentry->inode = inode;
955 for (i = 0; i <= inode->num_ads; i++) {
956 lte = inode_stream_lte_resolved(inode, i);
961 link_dentry(from_dentry, from_dentry_parent);
966 static int wimfs_listxattr(const char *path, char *list, size_t size)
968 struct dentry *dentry;
974 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
977 /* List alternate data streams, or get the list size */
979 ret = lookup_resource(w, path, get_lookup_flags(), &dentry, NULL, NULL);
982 inode = dentry->inode;
986 for (i = 0; i < inode->num_ads; i++)
987 needed_size += inode->ads_entries[i].stream_name_utf8_len + 6;
990 for (i = 0; i < inode->num_ads; i++) {
991 needed_size = inode->ads_entries[i].stream_name_utf8_len + 6;
992 if (needed_size > size)
994 p += sprintf(p, "user.%s",
995 inode->ads_entries[i].stream_name_utf8) + 1;
1004 * Create a directory in the WIM.
1005 * @mode is currently ignored.
1007 static int wimfs_mkdir(const char *path, mode_t mode)
1009 struct dentry *parent;
1010 struct dentry *newdir;
1011 const char *basename;
1013 parent = get_parent_dentry(w, path);
1017 if (!dentry_is_directory(parent))
1020 basename = path_basename(path);
1021 if (get_dentry_child_with_name(parent, basename))
1024 newdir = new_dentry_with_inode(basename);
1025 newdir->inode->attributes |= FILE_ATTRIBUTE_DIRECTORY;
1026 newdir->inode->resolved = true;
1027 newdir->inode->ino = next_ino++;
1028 link_dentry(newdir, parent);
1033 /* Creates a regular file. */
1034 static int wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
1036 const char *stream_name;
1037 if ((mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
1038 && (stream_name = path_stream_name(path))) {
1039 /* Make an alternate data stream */
1040 struct ads_entry *new_entry;
1041 struct dentry *dentry;
1043 char *p = (char*)stream_name - 1;
1044 wimlib_assert(*p == ':');
1047 dentry = get_dentry(w, path);
1048 if (!dentry || !dentry_is_regular_file(dentry))
1050 if (inode_get_ads_entry(dentry->inode, stream_name, NULL))
1052 new_entry = inode_add_ads(dentry->inode, stream_name);
1056 struct dentry *dentry, *parent;
1057 const char *basename;
1059 /* Make a normal file (not an alternate data stream) */
1061 /* Make sure that the parent of @path exists and is a directory, and
1062 * that the dentry named by @path does not already exist. */
1063 parent = get_parent_dentry(w, path);
1066 if (!dentry_is_directory(parent))
1069 basename = path_basename(path);
1070 if (get_dentry_child_with_name(parent, path))
1073 dentry = new_dentry_with_inode(basename);
1076 dentry->inode->resolved = true;
1077 dentry->inode->ino = next_ino++;
1078 link_dentry(dentry, parent);
1085 static int wimfs_open(const char *path, struct fuse_file_info *fi)
1087 struct dentry *dentry;
1088 struct lookup_table_entry *lte;
1090 struct wimlib_fd *fd;
1091 struct inode *inode;
1095 ret = lookup_resource(w, path, get_lookup_flags(), &dentry, <e,
1100 inode = dentry->inode;
1102 if (stream_idx == 0)
1105 stream_id = inode->ads_entries[stream_idx - 1].stream_id;
1107 /* The file resource may be in the staging directory (read-write mounts
1108 * only) or in the WIM. If it's in the staging directory, we need to
1109 * open a native file descriptor for the corresponding file. Otherwise,
1110 * we can read the file resource directly from the WIM file if we are
1111 * opening it read-only, but we need to extract the resource to the
1112 * staging directory if we are opening it writable. */
1113 if (flags_writable(fi->flags) &&
1114 (!lte || lte->resource_location != RESOURCE_IN_STAGING_FILE)) {
1115 u64 size = (lte) ? wim_resource_size(lte) : 0;
1116 ret = extract_resource_to_staging_dir(inode, stream_id,
1122 ret = alloc_wimlib_fd(inode, stream_id, lte, &fd);
1126 if (lte && lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1127 fd->staging_fd = open(lte->staging_file_name, fi->flags);
1128 if (fd->staging_fd == -1) {
1129 close_wimlib_fd(fd);
1133 fi->fh = (uintptr_t)fd;
1137 /* Opens a directory. */
1138 static int wimfs_opendir(const char *path, struct fuse_file_info *fi)
1140 struct inode *inode;
1142 struct wimlib_fd *fd = NULL;
1144 inode = wim_pathname_to_inode(w, path);
1147 if (!inode_is_directory(inode))
1149 ret = alloc_wimlib_fd(inode, 0, NULL, &fd);
1150 fi->fh = (uintptr_t)fd;
1156 * Read data from a file in the WIM or in the staging directory.
1158 static int wimfs_read(const char *path, char *buf, size_t size,
1159 off_t offset, struct fuse_file_info *fi)
1161 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1166 if (!fd->lte) /* Empty stream with no lookup table entry */
1169 if (fd->lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1170 /* Read from staging file */
1172 wimlib_assert(fd->lte->staging_file_name);
1173 wimlib_assert(fd->staging_fd != -1);
1176 DEBUG("Seek to offset %zu", offset);
1178 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1180 ret = read(fd->staging_fd, buf, size);
1187 u64 res_size = wim_resource_size(fd->lte);
1189 if (offset > res_size)
1192 size = min(size, res_size - offset);
1194 if (read_wim_resource(fd->lte, (u8*)buf,
1195 size, offset, false) != 0)
1201 /* Fills in the entries of the directory specified by @path using the
1202 * FUSE-provided function @filler. */
1203 static int wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
1204 off_t offset, struct fuse_file_info *fi)
1206 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1207 struct inode *inode;
1208 struct dentry *child;
1215 filler(buf, ".", NULL, 0);
1216 filler(buf, "..", NULL, 0);
1218 child = inode->children;
1224 if (filler(buf, child->file_name_utf8, NULL, 0))
1226 child = child->next;
1227 } while (child != inode->children);
1232 static int wimfs_readlink(const char *path, char *buf, size_t buf_len)
1234 struct dentry *dentry = get_dentry(w, path);
1238 if (!dentry_is_symlink(dentry))
1241 ret = inode_readlink(dentry->inode, buf, buf_len, w);
1248 static int wimfs_release(const char *path, struct fuse_file_info *fi)
1250 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1252 return close_wimlib_fd(fd);
1255 static int wimfs_releasedir(const char *path, struct fuse_file_info *fi)
1257 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1259 return close_wimlib_fd(fd);
1263 /* Remove an alternate data stream through the XATTR interface */
1264 static int wimfs_removexattr(const char *path, const char *name)
1266 struct dentry *dentry;
1267 struct ads_entry *ads_entry;
1269 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1272 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1276 dentry = get_dentry(w, path);
1280 ads_entry = inode_get_ads_entry(dentry->inode, name, &ads_idx);
1283 inode_remove_ads(dentry->inode, ads_idx, w->lookup_table);
1288 /* Renames a file or directory. See rename (3) */
1289 static int wimfs_rename(const char *from, const char *to)
1293 struct dentry *parent_of_dst;
1294 char *file_name_utf16 = NULL, *file_name_utf8 = NULL;
1295 u16 file_name_utf16_len, file_name_utf8_len;
1298 /* This rename() implementation currently only supports actual files
1299 * (not alternate data streams) */
1301 src = get_dentry(w, from);
1305 dst = get_dentry(w, to);
1308 ret = get_names(&file_name_utf16, &file_name_utf8,
1309 &file_name_utf16_len, &file_name_utf8_len,
1315 /* Destination file exists */
1317 if (src == dst) /* Same file */
1320 if (!dentry_is_directory(src)) {
1321 /* Cannot rename non-directory to directory. */
1322 if (dentry_is_directory(dst))
1325 /* Cannot rename directory to a non-directory or a non-empty
1327 if (!dentry_is_directory(dst))
1329 if (dst->inode->children != NULL)
1332 parent_of_dst = dst->parent;
1333 remove_dentry(dst, w->lookup_table);
1335 /* Destination does not exist */
1336 parent_of_dst = get_parent_dentry(w, to);
1340 if (!dentry_is_directory(parent_of_dst))
1344 FREE(src->file_name);
1345 FREE(src->file_name_utf8);
1346 src->file_name = file_name_utf16;
1347 src->file_name_utf8 = file_name_utf8;
1348 src->file_name_len = file_name_utf16_len;
1349 src->file_name_utf8_len = file_name_utf8_len;
1352 link_dentry(src, parent_of_dst);
1356 /* Remove a directory */
1357 static int wimfs_rmdir(const char *path)
1359 struct dentry *dentry;
1361 dentry = get_dentry(w, path);
1365 if (!dentry_is_empty_directory(dentry))
1368 remove_dentry(dentry, w->lookup_table);
1373 /* Write an alternate data stream through the XATTR interface */
1374 static int wimfs_setxattr(const char *path, const char *name,
1375 const char *value, size_t size, int flags)
1377 struct ads_entry *existing_ads_entry;
1378 struct ads_entry *new_ads_entry;
1379 struct lookup_table_entry *existing_lte;
1380 struct lookup_table_entry *lte;
1381 struct inode *inode;
1382 u8 value_hash[SHA1_HASH_SIZE];
1385 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1388 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1392 inode = wim_pathname_to_inode(w, path);
1396 existing_ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
1397 if (existing_ads_entry) {
1398 if (flags & XATTR_CREATE)
1400 inode_remove_ads(inode, ads_idx, w->lookup_table);
1402 if (flags & XATTR_REPLACE)
1405 new_ads_entry = inode_add_ads(inode, name);
1409 sha1_buffer((const u8*)value, size, value_hash);
1411 existing_lte = __lookup_resource(w->lookup_table, value_hash);
1418 lte = new_lookup_table_entry();
1421 value_copy = MALLOC(size);
1426 memcpy(value_copy, value, size);
1427 lte->resource_location = RESOURCE_IN_ATTACHED_BUFFER;
1428 lte->attached_buffer = value_copy;
1429 lte->resource_entry.original_size = size;
1430 lte->resource_entry.size = size;
1431 lte->resource_entry.flags = 0;
1432 copy_hash(lte->hash, value_hash);
1433 lookup_table_insert(w->lookup_table, lte);
1435 new_ads_entry->lte = lte;
1440 static int wimfs_symlink(const char *to, const char *from)
1442 struct dentry *dentry_parent, *dentry;
1443 const char *link_name;
1444 struct inode *inode;
1446 dentry_parent = get_parent_dentry(w, from);
1449 if (!dentry_is_directory(dentry_parent))
1452 link_name = path_basename(from);
1454 if (get_dentry_child_with_name(dentry_parent, link_name))
1456 dentry = new_dentry_with_inode(link_name);
1459 inode = dentry->inode;
1461 inode->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
1462 inode->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
1463 inode->ino = next_ino++;
1464 inode->resolved = true;
1466 if (inode_set_symlink(inode, to, w->lookup_table, NULL) != 0)
1467 goto out_free_dentry;
1469 link_dentry(dentry, dentry_parent);
1472 free_dentry(dentry);
1477 /* Reduce the size of a file */
1478 static int wimfs_truncate(const char *path, off_t size)
1480 struct dentry *dentry;
1481 struct lookup_table_entry *lte;
1485 struct inode *inode;
1487 ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1493 if (!lte) /* Already a zero-length file */
1496 inode = dentry->inode;
1498 if (stream_idx == 0)
1501 stream_id = inode->ads_entries[stream_idx - 1].stream_id;
1503 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1504 wimlib_assert(lte->staging_file_name);
1505 ret = truncate(lte->staging_file_name, size);
1508 lte->resource_entry.original_size = size;
1510 wimlib_assert(lte->resource_location == RESOURCE_IN_WIM);
1511 /* File in WIM. Extract it to the staging directory, but only
1512 * the first @size bytes of it. */
1513 ret = extract_resource_to_staging_dir(inode, stream_id,
1519 /* Remove a regular file */
1520 static int wimfs_unlink(const char *path)
1522 struct dentry *dentry;
1523 struct lookup_table_entry *lte;
1524 struct inode *inode;
1529 ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1535 if (stream_idx == 0)
1536 remove_dentry(dentry, w->lookup_table);
1538 inode_remove_ads(dentry->inode, stream_idx - 1, w->lookup_table);
1542 #ifdef HAVE_UTIMENSAT
1544 * Change the timestamp on a file dentry.
1546 * Note that alternate data streams do not have their own timestamps.
1548 static int wimfs_utimens(const char *path, const struct timespec tv[2])
1550 struct dentry *dentry;
1551 struct inode *inode;
1552 dentry = get_dentry(w, path);
1555 inode = dentry->inode;
1557 if (tv[0].tv_nsec != UTIME_OMIT) {
1558 if (tv[0].tv_nsec == UTIME_NOW)
1559 inode->last_access_time = get_wim_timestamp();
1561 inode->last_access_time = timespec_to_wim_timestamp(&tv[0]);
1563 if (tv[1].tv_nsec != UTIME_OMIT) {
1564 if (tv[1].tv_nsec == UTIME_NOW)
1565 inode->last_write_time = get_wim_timestamp();
1567 inode->last_write_time = timespec_to_wim_timestamp(&tv[1]);
1572 static int wimfs_utime(const char *path, struct utimbuf *times)
1574 struct dentry *dentry;
1575 struct inode *inode;
1576 dentry = get_dentry(w, path);
1579 inode = dentry->inode;
1581 inode->last_write_time = unix_timestamp_to_wim(times->modtime);
1582 inode->last_access_time = unix_timestamp_to_wim(times->actime);
1587 /* Writes to a file in the WIM filesystem.
1588 * It may be an alternate data stream, but here we don't even notice because we
1589 * just get a lookup table entry. */
1590 static int wimfs_write(const char *path, const char *buf, size_t size,
1591 off_t offset, struct fuse_file_info *fi)
1593 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1600 wimlib_assert(fd->lte);
1601 wimlib_assert(fd->lte->staging_file_name);
1602 wimlib_assert(fd->staging_fd != -1);
1603 wimlib_assert(fd->inode);
1605 /* Seek to the requested position */
1606 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1609 /* Write the data. */
1610 ret = write(fd->staging_fd, buf, size);
1614 now = get_wim_timestamp();
1615 fd->inode->last_write_time = now;
1616 fd->inode->last_access_time = now;
1620 static struct fuse_operations wimfs_operations = {
1621 .access = wimfs_access,
1622 .destroy = wimfs_destroy,
1624 .fallocate = wimfs_fallocate,
1626 .fgetattr = wimfs_fgetattr,
1627 .ftruncate = wimfs_ftruncate,
1628 .getattr = wimfs_getattr,
1630 .getxattr = wimfs_getxattr,
1634 .listxattr = wimfs_listxattr,
1636 .mkdir = wimfs_mkdir,
1637 .mknod = wimfs_mknod,
1639 .opendir = wimfs_opendir,
1641 .readdir = wimfs_readdir,
1642 .readlink = wimfs_readlink,
1643 .release = wimfs_release,
1644 .releasedir = wimfs_releasedir,
1646 .removexattr = wimfs_removexattr,
1648 .rename = wimfs_rename,
1649 .rmdir = wimfs_rmdir,
1651 .setxattr = wimfs_setxattr,
1653 .symlink = wimfs_symlink,
1654 .truncate = wimfs_truncate,
1655 .unlink = wimfs_unlink,
1656 #ifdef HAVE_UTIMENSAT
1657 .utimens = wimfs_utimens,
1659 .utime = wimfs_utime,
1661 .write = wimfs_write,
1665 /* Mounts a WIM file. */
1666 WIMLIBAPI int wimlib_mount(WIMStruct *wim, int image, const char *dir,
1667 int flags, WIMStruct **additional_swms,
1668 unsigned num_additional_swms)
1674 struct lookup_table *joined_tab, *wim_tab_save;
1675 struct image_metadata *imd;
1677 DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
1678 wim, image, dir, flags);
1681 return WIMLIB_ERR_INVALID_PARAM;
1683 ret = verify_swm_set(wim, additional_swms, num_additional_swms);
1687 if (num_additional_swms) {
1688 ret = new_joined_lookup_table(wim, additional_swms,
1689 num_additional_swms,
1693 wim_tab_save = wim->lookup_table;
1694 wim->lookup_table = joined_tab;
1697 ret = wimlib_select_image(wim, image);
1702 imd = &wim->image_metadata[image - 1];
1704 DEBUG("Selected image %d", image);
1706 next_ino = assign_inode_numbers(&imd->inode_list);
1708 DEBUG("(next_ino = %"PRIu64")", next_ino);
1710 /* Resolve all the lookup table entries of the dentry tree */
1711 DEBUG("Resolving lookup table entries");
1712 for_dentry_in_tree(imd->root_dentry, dentry_resolve_ltes,
1715 if (flags & WIMLIB_MOUNT_FLAG_READWRITE)
1716 imd->modified = true;
1718 if (!(flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
1719 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
1720 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
1721 flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1723 DEBUG("Getting current directory");
1726 working_directory = getcwd(NULL, 0);
1727 if (!working_directory) {
1728 ERROR_WITH_ERRNO("Could not determine current directory");
1729 ret = WIMLIB_ERR_NOTDIR;
1733 DEBUG("Closing POSIX message queues");
1734 /* XXX hack to get rid of the message queues if they already exist for
1735 * some reason (maybe left over from a previous mount that wasn't
1736 * unmounted correctly) */
1737 ret = open_message_queues(true);
1740 close_message_queues();
1742 DEBUG("Preparing arguments to fuse_main()");
1747 ret = WIMLIB_ERR_NOMEM;
1751 argv[argc++] = "imagex";
1753 argv[argc++] = "-s"; /* disable multi-threaded operation */
1755 if (flags & WIMLIB_MOUNT_FLAG_DEBUG)
1756 argv[argc++] = "-d";
1759 * We provide the use_ino option because we are going to assign inode
1760 * numbers oursides. We've already numbered the inodes with unique
1761 * numbers in the assign_inode_numbers() function, and the static
1762 * variable next_ino is set to the next available inode number.
1764 char optstring[256] = "use_ino,subtype=wimfs,attr_timeout=0";
1765 argv[argc++] = "-o";
1766 argv[argc++] = optstring;
1767 if ((flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
1768 /* Read-write mount. Make the staging directory */
1770 if (!staging_dir_name) {
1772 ret = WIMLIB_ERR_MKDIR;
1776 /* Read-only mount */
1777 strcat(optstring, ",ro");
1784 DEBUG("FUSE command line (argc = %d): ", argc);
1785 for (i = 0; i < argc; i++) {
1786 fputs(argv[i], stdout);
1794 /* Set static variables. */
1796 mount_flags = flags;
1798 ret = fuse_main(argc, argv, &wimfs_operations, NULL);
1800 ret = WIMLIB_ERR_FUSE;
1802 if (num_additional_swms) {
1803 free_lookup_table(wim->lookup_table);
1804 wim->lookup_table = wim_tab_save;
1811 * Unmounts the WIM file that was previously mounted on @dir by using
1814 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
1821 struct timespec timeout;
1827 /* Open message queues between the unmount process and the
1828 * filesystem daemon. */
1829 ret = open_message_queues(false);
1833 /* Send a message to the filesystem saying whether to commit or
1835 msg[0] = (flags & WIMLIB_UNMOUNT_FLAG_COMMIT) ? 1 : 0;
1836 msg[1] = (flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY) ? 1 : 0;
1838 DEBUG("Sending message: %s, %s",
1839 (msg[0] == 0) ? "don't commit" : "commit",
1840 (msg[1] == 0) ? "don't check" : "check");
1841 ret = mq_send(unmount_to_daemon_mq, msg, 2, 1);
1843 ERROR("Failed to notify filesystem daemon whether we want to "
1844 "commit changes or not");
1845 close_message_queues();
1846 return WIMLIB_ERR_MQUEUE;
1849 /* Execute `fusermount -u', which is installed setuid root, to unmount
1852 * FUSE does not yet implement synchronous unmounts. This means that
1853 * fusermount -u will return before the filesystem daemon returns from
1854 * wimfs_destroy(). This is partly what we want, because we need to
1855 * send a message from this process to the filesystem daemon telling
1856 * whether --commit was specified or not. However, after that, the
1857 * unmount process must wait for the filesystem daemon to finish writing
1864 ERROR_WITH_ERRNO("Failed to fork()");
1865 return WIMLIB_ERR_FORK;
1868 execlp("fusermount", "fusermount", "-u", dir, NULL);
1869 ERROR_WITH_ERRNO("Failed to execute `fusermount'");
1870 exit(WIMLIB_ERR_FUSERMOUNT);
1873 ret = wait(&status);
1875 ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
1877 return WIMLIB_ERR_FUSERMOUNT;
1881 ERROR("fusermount exited with status %d", status);
1883 /* Try again, but with the `umount' program. This is required
1884 * on other FUSE implementations such as FreeBSD's that do not
1885 * have a `fusermount' program. */
1889 ERROR_WITH_ERRNO("Failed to fork()");
1890 return WIMLIB_ERR_FORK;
1893 execlp("umount", "umount", dir, NULL);
1894 ERROR_WITH_ERRNO("Failed to execute `umount'");
1895 exit(WIMLIB_ERR_FUSERMOUNT);
1898 ret = wait(&status);
1900 ERROR_WITH_ERRNO("Failed to wait for `umount' process to "
1902 return WIMLIB_ERR_FUSERMOUNT;
1905 ERROR("`umount' exited with failure status");
1906 return WIMLIB_ERR_FUSERMOUNT;
1911 /* Wait for a message from the filesytem daemon indicating whether the
1912 * filesystem was unmounted successfully (0) or an error occurred (1).
1913 * This may take a long time if a big WIM file needs to be rewritten. */
1915 /* Wait at most 600??? seconds before giving up and returning false.
1916 * Either it's a really big WIM file, or (more likely) the
1917 * filesystem daemon has crashed or failed for some reason.
1919 * XXX come up with some method to determine if the filesystem
1920 * daemon has really crashed or not.
1922 * XXX Idea: have mount daemon write its PID into the WIM file header?
1925 gettimeofday(&now, NULL);
1926 timeout.tv_sec = now.tv_sec + 600;
1927 timeout.tv_nsec = now.tv_usec * 1000;
1929 msgsize = mq_get_msgsize(daemon_to_unmount_mq);
1930 char mailbox[msgsize];
1933 DEBUG("Waiting for message telling us whether the unmount was "
1934 "successful or not.");
1935 ret = mq_timedreceive(daemon_to_unmount_mq, mailbox, msgsize,
1938 close_message_queues();
1940 if (errno_save == ETIMEDOUT) {
1941 ERROR("Timed out- probably the filesystem daemon "
1942 "crashed and the WIM was not written "
1944 return WIMLIB_ERR_TIMEOUT;
1946 ERROR("mq_receive(): %s", strerror(errno_save));
1947 return WIMLIB_ERR_MQUEUE;
1951 DEBUG("Received message: %s",
1952 (mailbox[0] == 0) ? "Unmount OK" : "Unmount Failed");
1953 if (mailbox[0] != 0)
1954 ERROR("Unmount failed");
1958 #else /* WITH_FUSE */
1961 static inline int mount_unsupported_error()
1963 ERROR("WIMLIB was compiled with --without-fuse, which disables support "
1964 "for mounting WIMs.");
1965 return WIMLIB_ERR_UNSUPPORTED;
1968 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
1970 return mount_unsupported_error();
1973 WIMLIBAPI int wimlib_mount(WIMStruct *wim_p, int image, const char *dir,
1974 int flags, WIMStruct **additional_swms,
1975 unsigned num_additional_swms)
1977 return mount_unsupported_error();
1980 #endif /* WITH_FUSE */