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. */
56 struct inode *f_inode;
57 struct lookup_table_entry *f_lte;
63 /* The WIMStruct for the mounted WIM. */
66 /* Working directory when `imagex mount' is run. */
67 static 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 /* Next inode number to be assigned. */
83 /* List of lookup table entries in the staging directory */
84 static LIST_HEAD(staging_list);
86 static inline int get_lookup_flags()
88 if (mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
89 return LOOKUP_FLAG_ADS_OK;
94 /* Returns nonzero if write permission is requested on the file open flags */
95 static inline int flags_writable(int open_flags)
97 return open_flags & (O_RDWR | O_WRONLY);
101 * Allocate a file descriptor for a stream.
103 * @inode: inode containing the stream we're opening
104 * @stream_id: ID of the stream we're opening
105 * @lte: Lookup table entry for the stream (may be NULL)
106 * @fd_ret: Return the allocated file descriptor if successful.
108 * Return 0 iff successful or error code if unsuccessful.
110 static int alloc_wimlib_fd(struct inode *inode,
112 struct lookup_table_entry *lte,
113 struct wimlib_fd **fd_ret)
115 static const u16 fds_per_alloc = 8;
116 static const u16 max_fds = 0xffff;
118 DEBUG("Allocating fd for stream ID %u from inode %lx (open = %u, allocated = %u)",
119 stream_id, inode->ino, inode->num_opened_fds,
120 inode->num_allocated_fds);
122 if (inode->num_opened_fds == inode->num_allocated_fds) {
123 struct wimlib_fd **fds;
126 if (inode->num_allocated_fds == max_fds)
128 num_new_fds = min(fds_per_alloc,
129 max_fds - inode->num_allocated_fds);
131 fds = REALLOC(inode->fds,
132 (inode->num_allocated_fds + num_new_fds) *
133 sizeof(inode->fds[0]));
136 memset(&fds[inode->num_allocated_fds], 0,
137 num_new_fds * sizeof(fds[0]));
139 inode->num_allocated_fds += num_new_fds;
141 for (u16 i = 0; ; i++) {
142 if (!inode->fds[i]) {
143 struct wimlib_fd *fd = CALLOC(1, sizeof(*fd));
150 fd->stream_id = stream_id;
153 inode->num_opened_fds++;
155 lte->num_opened_fds++;
156 DEBUG("Allocated fd");
162 static void inode_put_fd(struct inode *inode, struct wimlib_fd *fd)
165 wimlib_assert(inode);
166 wimlib_assert(fd->f_inode == inode);
167 wimlib_assert(inode->num_opened_fds);
168 wimlib_assert(fd->idx < inode->num_opened_fds);
169 wimlib_assert(inode->fds[fd->idx] == fd);
171 inode->fds[fd->idx] = NULL;
173 if (--inode->num_opened_fds == 0 && inode->link_count == 0)
177 static int lte_put_fd(struct lookup_table_entry *lte, struct wimlib_fd *fd)
180 wimlib_assert(fd->f_lte == lte);
182 if (!lte) /* Empty stream with no lookup table entry */
185 /* Close staging file descriptor if needed. */
187 if (lte->resource_location == RESOURCE_IN_STAGING_FILE
188 && fd->staging_fd != -1)
190 if (close(fd->staging_fd) != 0) {
191 ERROR_WITH_ERRNO("Failed to close staging file");
195 lte_decrement_num_opened_fds(lte, w->lookup_table);
199 /* Close a file descriptor. */
200 static int close_wimlib_fd(struct wimlib_fd *fd)
204 DEBUG("Closing fd (inode = %lu, opened = %u, allocated = %u)",
205 fd->f_inode->ino, fd->f_inode->num_opened_fds,
206 fd->f_inode->num_allocated_fds);
207 ret = lte_put_fd(fd->f_lte, fd);
211 inode_put_fd(fd->f_inode, fd);
215 /* Remove a dentry; i.e. remove a reference to the corresponding inode.
217 * If there are no remaining references to the inode either through dentries or
218 * open file descriptors, the inode is freed. Otherwise, the inode is not
219 * removed, but the dentry is unlinked and freed.
221 * Either way, all lookup table entries referenced by the inode have their
222 * reference count decremented. If a lookup table entry has no open file
223 * descriptors and no references remaining, it is freed, and the staging file is
226 static void remove_dentry(struct dentry *dentry,
227 struct lookup_table *lookup_table)
229 struct inode *inode = dentry->d_inode;
230 struct lookup_table_entry *lte;
233 for (i = 0; i <= inode->num_ads; i++) {
234 lte = inode_stream_lte_resolved(inode, i);
236 lte_decrement_refcnt(lte, lookup_table);
238 unlink_dentry(dentry);
242 /* Creates a new staging file and returns its file descriptor opened for
245 * @name_ret: A location into which the a pointer to the newly allocated name of
246 * the staging file is stored.
247 * @return: The file descriptor for the new file. Returns -1 and sets errno on
248 * error, for any reason possible from the creat() function.
250 static int create_staging_file(char **name_ret, int open_flags)
258 name_len = staging_dir_name_len + 1 + SHA1_HASH_SIZE;
259 name = MALLOC(name_len + 1);
267 memcpy(name, staging_dir_name, staging_dir_name_len);
268 name[staging_dir_name_len] = '/';
269 randomize_char_array_with_alnum(name + staging_dir_name_len + 1,
271 name[name_len] = '\0';
274 /* Just in case, verify that the randomly generated name doesn't name an
275 * existing file, and try again if so */
276 } while (stat(name, &stbuf) == 0);
282 /* doesn't exist--- ok */
284 DEBUG("Creating staging file `%s'", name);
286 fd = open(name, open_flags | O_CREAT | O_TRUNC, 0600);
298 * Extract a WIM resource to the staging directory.
300 * @inode: Inode that contains the stream we are extracting
302 * @stream_id: Identifier for the stream (it stays constant even if the indices
303 * of the stream entries are changed)
305 * @lte: Pointer to pointer to the lookup table entry for the stream we need to
306 * extract, or NULL if there was no lookup table entry present for the stream
308 * @size: Number of bytes of the stream we want to extract (this supports the
309 * wimfs_truncate() function).
311 static int extract_resource_to_staging_dir(struct inode *inode,
313 struct lookup_table_entry **lte,
316 char *staging_file_name;
319 struct lookup_table_entry *old_lte, *new_lte;
321 DEBUG("Extracting resource to staging dir: inode %"PRIu64", "
322 "stream id %"PRIu32, inode->ino, stream_id);
325 fd = create_staging_file(&staging_file_name, O_WRONLY);
330 ret = extract_wim_resource_to_fd(old_lte, fd, size);
333 if (ret != 0 || close(fd) != 0) {
339 goto out_delete_staging_file;
342 if (old_lte && inode->link_count == old_lte->refcnt) {
343 /* The reference count of the existing lookup table
344 * entry is the same as the link count of the inode that
345 * contains the stream we're opening. Therefore, ALL
346 * the references to the lookup table entry correspond
347 * to the stream we're trying to extract, so the lookup
348 * table entry can be re-used. */
349 DEBUG("Re-using lookup table entry");
350 lookup_table_unlink(w->lookup_table, old_lte);
354 /* There's an existing lookup table entry, but its
355 * reference count is creater than the link count for
356 * the inode containing a stream we're opening.
357 * Therefore, we need to split the lookup table entry.
359 wimlib_assert(old_lte->refcnt > inode->link_count);
360 DEBUG("Splitting lookup table entry "
361 "(inode->link_count = %zu, old_lte->refcnt = %u)",
362 inode->link_count, old_lte->refcnt);
366 new_lte = new_lookup_table_entry();
369 goto out_delete_staging_file;
372 /* There may already be open file descriptors to this stream if
373 * it's previously been opened read-only, but just now we're
374 * opening it read-write. Identify those file descriptors and
375 * change their lookup table entry pointers to point to the new
376 * lookup table entry, and open staging file descriptors for
379 * At the same time, we need to count the number of these opened
380 * file descriptors to the new lookup table entry. If there's
381 * an old lookup table entry, this number needs to be subtracted
382 * from the fd's opened to the old entry. */
383 for (u16 i = 0, j = 0; j < inode->num_opened_fds; i++) {
384 struct wimlib_fd *fd = inode->fds[i];
386 if (fd->stream_id == stream_id) {
387 wimlib_assert(fd->f_lte == old_lte);
388 wimlib_assert(fd->staging_fd == -1);
390 new_lte->num_opened_fds++;
391 fd->staging_fd = open(staging_file_name, O_RDONLY);
392 if (fd->staging_fd == -1) {
394 goto out_revert_fd_changes;
400 DEBUG("%zu fd's were already opened to the file we extracted",
401 new_lte->num_opened_fds);
403 old_lte->num_opened_fds -= new_lte->num_opened_fds;
404 old_lte->refcnt -= inode->link_count;
408 new_lte->resource_entry.original_size = size;
409 new_lte->refcnt = inode->link_count;
410 new_lte->resource_location = RESOURCE_IN_STAGING_FILE;
411 new_lte->staging_file_name = staging_file_name;
412 new_lte->lte_inode = inode;
413 random_hash(new_lte->hash);
416 inode->lte = new_lte;
418 for (u16 i = 0; i < inode->num_ads; i++)
419 if (inode->ads_entries[i].stream_id == stream_id)
420 inode->ads_entries[i].lte = new_lte;
422 lookup_table_insert(w->lookup_table, new_lte);
423 list_add(&new_lte->staging_list, &staging_list);
426 out_revert_fd_changes:
427 for (u16 i = 0, j = 0; j < new_lte->num_opened_fds; i++) {
428 struct wimlib_fd *fd = inode->fds[i];
429 if (fd && fd->stream_id == stream_id && fd->f_lte == new_lte) {
431 if (fd->staging_fd != -1) {
432 close(fd->staging_fd);
439 free_lookup_table_entry(new_lte);
440 out_delete_staging_file:
441 unlink(staging_file_name);
442 FREE(staging_file_name);
447 * Creates a randomly named staging directory and returns its name into the
448 * static variable staging_dir_name.
450 * If the staging directory cannot be created, staging_dir_name is set to NULL.
452 static void make_staging_dir()
454 /* XXX Give the user an option of where to stage files */
456 static char prefix[] = "wimlib-staging-";
457 static const size_t prefix_len = 15;
458 static const size_t suffix_len = 10;
460 size_t pwd_len = strlen(working_directory);
462 staging_dir_name_len = pwd_len + 1 + prefix_len + suffix_len;
464 staging_dir_name = MALLOC(staging_dir_name_len + 1);
465 if (!staging_dir_name) {
466 ERROR("Out of memory");
470 memcpy(staging_dir_name, working_directory, pwd_len);
471 staging_dir_name[pwd_len] = '/';
472 memcpy(staging_dir_name + pwd_len + 1, prefix, prefix_len);
473 randomize_char_array_with_alnum(staging_dir_name + pwd_len + 1 + prefix_len,
475 staging_dir_name[staging_dir_name_len] = '\0';
477 if (mkdir(staging_dir_name, 0700) != 0) {
478 ERROR_WITH_ERRNO("Failed to create temporary directory `%s'",
480 FREE(staging_dir_name);
481 staging_dir_name = NULL;
485 static int remove_file_or_directory(const char *fpath, const struct stat *sb,
486 int typeflag, struct FTW *ftwbuf)
488 if (remove(fpath) == 0)
491 return WIMLIB_ERR_DELETE_STAGING_DIR;
496 * Deletes the staging directory and all the files contained in it.
498 static int delete_staging_dir()
502 ret = nftw(staging_dir_name, remove_file_or_directory,10, FTW_DEPTH);
503 staging_dir_name = NULL;
507 /* Name and message queue descriptors for message queues between the filesystem
508 * daemon process and the unmount process. These are used when the filesystem
509 * is unmounted and the process running wimlib_mount() (i.e. the `imagex
510 * unmount' command) needs to communicate with the filesystem daemon running
511 * fuse_main() (i.e. that spawned by the `imagex mount' or `imagex mountrw'
513 static char *unmount_to_daemon_mq_name;
514 static char *daemon_to_unmount_mq_name;
515 static mqd_t unmount_to_daemon_mq;
516 static mqd_t daemon_to_unmount_mq;
518 /* Simple function that returns the concatenation of 4 strings. */
519 static char *strcat_dup(const char *s1, const char *s2, const char *s3,
522 size_t len = strlen(s1) + strlen(s2) + strlen(s3) + strlen(s4) + 1;
523 char *p = MALLOC(len);
529 return strcat(p, s4);
532 /* Removes trailing forward slashes in a string. */
533 static void remove_trailing_slashes(char *s)
535 long len = strlen(s);
536 for (long i = len - 1; i >= 1; i--) {
544 /* Changes forward slashes to underscores in a string. */
545 static void s_slashes_underscores_g(char *s)
554 static int set_message_queue_names(const char *mount_dir)
556 static const char *slash = "/";
557 static const char *prefix = "wimlib-";
558 static const char *u2d_suffix = "unmount-to-daemon-mq";
559 static const char *d2u_suffix = "daemon-to-unmount-mq";
561 const char *mount_dir_basename = path_basename(mount_dir);
563 unmount_to_daemon_mq_name = strcat_dup(slash, mount_dir_basename,
565 if (!unmount_to_daemon_mq_name) {
566 ERROR("Out of memory");
567 return WIMLIB_ERR_NOMEM;
569 daemon_to_unmount_mq_name = strcat_dup(slash, mount_dir_basename,
571 if (!daemon_to_unmount_mq_name) {
572 ERROR("Out of memory");
573 FREE(unmount_to_daemon_mq_name);
574 unmount_to_daemon_mq_name = NULL;
575 return WIMLIB_ERR_NOMEM;
578 remove_trailing_slashes(unmount_to_daemon_mq_name);
579 remove_trailing_slashes(daemon_to_unmount_mq_name);
580 s_slashes_underscores_g(unmount_to_daemon_mq_name + 1);
581 s_slashes_underscores_g(daemon_to_unmount_mq_name + 1);
585 static void free_message_queue_names()
587 FREE(unmount_to_daemon_mq_name);
588 FREE(daemon_to_unmount_mq_name);
589 unmount_to_daemon_mq_name = NULL;
590 daemon_to_unmount_mq_name = NULL;
594 * Opens two POSIX message queue: one for sending messages from the unmount
595 * process to the daemon process, and one to go the other way. The names of the
596 * message queues, which must be system-wide unique, are be based on the mount
597 * point. (There of course is still a possibility of a collision if one were to
598 * unmount two identically named directories simultaneously...)
600 * @daemon specifies whether the calling process is the filesystem daemon or the
603 static int open_message_queues(bool daemon)
608 wimlib_assert(unmount_to_daemon_mq_name != NULL &&
609 daemon_to_unmount_mq_name != NULL);
612 flags = O_RDONLY | O_CREAT;
614 flags = O_WRONLY | O_CREAT;
616 unmount_to_daemon_mq = mq_open(unmount_to_daemon_mq_name, flags,
619 if (unmount_to_daemon_mq == (mqd_t)-1) {
620 ERROR_WITH_ERRNO("mq_open()");
621 ret = WIMLIB_ERR_MQUEUE;
626 flags = O_WRONLY | O_CREAT;
628 flags = O_RDONLY | O_CREAT;
630 daemon_to_unmount_mq = mq_open(daemon_to_unmount_mq_name, flags,
633 if (daemon_to_unmount_mq == (mqd_t)-1) {
634 ERROR_WITH_ERRNO("mq_open()");
635 ret = WIMLIB_ERR_MQUEUE;
636 goto out_close_unmount_to_daemon_mq;
640 out_close_unmount_to_daemon_mq:
641 mq_close(unmount_to_daemon_mq);
642 mq_unlink(unmount_to_daemon_mq_name);
643 unmount_to_daemon_mq = (mqd_t)-1;
648 static int mq_get_msgsize(mqd_t mq)
650 static const char *msgsize_max_file = "/proc/sys/fs/mqueue/msgsize_max";
655 if (mq_getattr(unmount_to_daemon_mq, &attr) == 0) {
656 msgsize = attr.mq_msgsize;
658 ERROR_WITH_ERRNO("mq_getattr()");
659 ERROR("Attempting to read %s", msgsize_max_file);
660 fp = fopen(msgsize_max_file, "rb");
662 if (fscanf(fp, "%d", &msgsize) != 1) {
663 ERROR("Assuming message size of 8192");
668 ERROR_WITH_ERRNO("Failed to open the file `%s'",
670 ERROR("Assuming message size of 8192");
677 static void unlink_message_queues()
679 mq_unlink(unmount_to_daemon_mq_name);
680 mq_unlink(daemon_to_unmount_mq_name);
683 /* Closes the message queues, which are allocated in static variables */
684 static void close_message_queues()
686 mq_close(unmount_to_daemon_mq);
687 unmount_to_daemon_mq = (mqd_t)(-1);
688 mq_close(daemon_to_unmount_mq);
689 daemon_to_unmount_mq = (mqd_t)(-1);
690 unlink_message_queues();
693 static int wimfs_access(const char *path, int mask)
695 /* Permissions not implemented */
699 static void inode_update_lte_ptr(struct inode *inode,
700 struct lookup_table_entry *old_lte,
701 struct lookup_table_entry *new_lte)
703 if (inode->lte == old_lte) {
704 inode->lte = new_lte;
706 for (unsigned i = 0; i < inode->num_ads; i++) {
707 if (inode->ads_entries[i].lte == old_lte) {
708 inode->ads_entries[i].lte = new_lte;
715 static int update_lte_of_staging_file(struct lookup_table_entry *lte,
716 struct lookup_table *table)
718 struct lookup_table_entry *duplicate_lte;
720 u8 hash[SHA1_HASH_SIZE];
723 wimlib_assert(lte->resource_location == RESOURCE_IN_STAGING_FILE);
724 wimlib_assert(lte->staging_file_name);
726 ret = sha1sum(lte->staging_file_name, hash);
730 lookup_table_unlink(table, lte);
732 duplicate_lte = __lookup_resource(table, hash);
735 /* Merge duplicate lookup table entries */
736 duplicate_lte->refcnt += lte->refcnt;
737 list_del(<e->staging_list);
738 inode_update_lte_ptr(lte->lte_inode, lte, duplicate_lte);
739 free_lookup_table_entry(lte);
741 if (stat(lte->staging_file_name, &stbuf) != 0) {
742 ERROR_WITH_ERRNO("Failed to stat `%s'", lte->staging_file_name);
743 return WIMLIB_ERR_STAT;
745 if (stbuf.st_size == 0) {
746 /* Zero-length stream. No lookup table entry needed. */
747 inode_update_lte_ptr(lte->lte_inode, lte, NULL);
748 free_lookup_table_entry(lte);
750 wimlib_assert(<e->file_on_disk == <e->staging_file_name);
751 lte->resource_entry.original_size = stbuf.st_size;
752 lte->resource_entry.size = stbuf.st_size;
753 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
754 lte->lte_inode = NULL;
755 copy_hash(lte->hash, hash);
756 lookup_table_insert(table, lte);
762 static int inode_close_fds(struct inode *inode)
764 u16 num_opened_fds = inode->num_opened_fds;
765 for (u16 i = 0, j = 0; j < num_opened_fds; i++) {
766 struct wimlib_fd *fd = inode->fds[i];
768 wimlib_assert(fd->f_inode == inode);
769 int ret = close_wimlib_fd(fd);
778 /* Overwrites the WIM file, with changes saved. */
779 static int rebuild_wim(WIMStruct *w, bool check_integrity)
782 struct lookup_table_entry *lte, *tmp;
785 DEBUG("Closing all staging file descriptors.");
786 list_for_each_entry(lte, &staging_list, staging_list) {
787 ret = inode_close_fds(lte->lte_inode);
792 DEBUG("Calculating SHA1 checksums for all new staging files.");
793 list_for_each_entry_safe(lte, tmp, &staging_list, staging_list) {
794 ret = update_lte_of_staging_file(lte, w->lookup_table);
799 xml_update_image_info(w, w->current_image);
801 ret = wimlib_overwrite(w, check_integrity);
803 ERROR("Failed to commit changes");
809 /* Called when the filesystem is unmounted. */
810 static void wimfs_destroy(void *p)
812 /* For read-write mounts, the `imagex unmount' command, which is
813 * running in a separate process and is executing the
814 * wimlib_unmount() function, will send this process a byte
815 * through a message queue that indicates whether the --commit
816 * option was specified or not. */
819 struct timespec timeout;
821 ssize_t bytes_received;
824 char check_integrity;
827 ret = open_message_queues(true);
831 msgsize = mq_get_msgsize(unmount_to_daemon_mq);
836 /* Wait at most 3 seconds before giving up and discarding changes. */
837 gettimeofday(&now, NULL);
838 timeout.tv_sec = now.tv_sec + 3;
839 timeout.tv_nsec = now.tv_usec * 1000;
840 DEBUG("Waiting for message telling us whether to commit or not, and "
841 "whether to include integrity checks.");
843 bytes_received = mq_timedreceive(unmount_to_daemon_mq, msg,
844 msgsize, NULL, &timeout);
845 if (bytes_received == -1) {
846 if (errno == ETIMEDOUT)
849 ERROR_WITH_ERRNO("mq_timedreceive()");
850 ERROR("Not committing.");
854 DEBUG("Received message: [%d %d]", msg[0], msg[1]);
856 check_integrity = msg[1];
860 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
862 status = chdir(working_directory);
864 ERROR_WITH_ERRNO("chdir()");
865 status = WIMLIB_ERR_NOTDIR;
868 status = rebuild_wim(w, (check_integrity != 0));
870 ret = delete_staging_dir();
872 ERROR_WITH_ERRNO("Failed to delete the staging "
878 DEBUG("Read-only mount");
881 DEBUG("Sending status %hhd", status);
882 ret = mq_send(daemon_to_unmount_mq, &status, 1, 1);
884 ERROR_WITH_ERRNO("Failed to send status to unmount process");
885 close_message_queues();
889 static int wimfs_fallocate(const char *path, int mode,
890 off_t offset, off_t len, struct fuse_file_info *fi)
892 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
893 wimlib_assert(fd->staging_fd != -1);
894 return fallocate(fd->staging_fd, mode, offset, len);
899 static int wimfs_fgetattr(const char *path, struct stat *stbuf,
900 struct fuse_file_info *fi)
902 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
903 return inode_to_stbuf(fd->f_inode, fd->f_lte, stbuf);
906 static int wimfs_ftruncate(const char *path, off_t size,
907 struct fuse_file_info *fi)
909 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
910 int ret = ftruncate(fd->staging_fd, size);
913 if (fd->f_lte && size < fd->f_lte->resource_entry.original_size)
914 fd->f_lte->resource_entry.original_size = size;
919 * Fills in a `struct stat' that corresponds to a file or directory in the WIM.
921 static int wimfs_getattr(const char *path, struct stat *stbuf)
923 struct dentry *dentry;
924 struct lookup_table_entry *lte;
927 ret = lookup_resource(w, path,
928 get_lookup_flags() | LOOKUP_FLAG_DIRECTORY_OK,
929 &dentry, <e, NULL);
932 return inode_to_stbuf(dentry->d_inode, lte, stbuf);
936 /* Read an alternate data stream through the XATTR interface, or get its size */
937 static int wimfs_getxattr(const char *path, const char *name, char *value,
942 struct ads_entry *ads_entry;
944 struct lookup_table_entry *lte;
946 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
949 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
953 inode = wim_pathname_to_inode(w, path);
957 ads_entry = inode_get_ads_entry(inode, name, NULL);
961 lte = ads_entry->lte;
962 res_size = wim_resource_size(lte);
970 ret = read_full_wim_resource(lte, (u8*)value);
978 /* Create a hard link */
979 static int wimfs_link(const char *to, const char *from)
981 struct dentry *from_dentry, *from_dentry_parent;
982 const char *link_name;
984 struct lookup_table_entry *lte;
986 inode = wim_pathname_to_inode(w, to);
990 if (!inode_is_regular_file(inode))
993 from_dentry_parent = get_parent_dentry(w, from);
994 if (!from_dentry_parent)
996 if (!dentry_is_directory(from_dentry_parent))
999 link_name = path_basename(from);
1000 if (get_dentry_child_with_name(from_dentry_parent, link_name))
1002 from_dentry = new_dentry(link_name);
1007 inode_add_dentry(from_dentry, inode);
1008 from_dentry->d_inode = inode;
1009 inode->link_count++;
1011 for (unsigned i = 0; i <= inode->num_ads; i++) {
1012 lte = inode_stream_lte_resolved(inode, i);
1017 link_dentry(from_dentry, from_dentry_parent);
1022 static int wimfs_listxattr(const char *path, char *list, size_t size)
1027 struct inode *inode;
1029 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1032 /* List alternate data streams, or get the list size */
1034 inode = wim_pathname_to_inode(w, path);
1040 for (i = 0; i < inode->num_ads; i++)
1041 needed_size += inode->ads_entries[i].stream_name_utf8_len + 6;
1045 for (i = 0; i < inode->num_ads; i++) {
1046 needed_size = inode->ads_entries[i].stream_name_utf8_len + 6;
1047 if (needed_size > size)
1049 p += sprintf(p, "user.%s",
1050 inode->ads_entries[i].stream_name_utf8) + 1;
1051 size -= needed_size;
1059 * Create a directory in the WIM.
1060 * @mode is currently ignored.
1062 static int wimfs_mkdir(const char *path, mode_t mode)
1064 struct dentry *parent;
1065 struct dentry *newdir;
1066 const char *basename;
1068 parent = get_parent_dentry(w, path);
1072 if (!dentry_is_directory(parent))
1075 basename = path_basename(path);
1076 if (get_dentry_child_with_name(parent, basename))
1079 newdir = new_dentry_with_inode(basename);
1080 newdir->d_inode->attributes |= FILE_ATTRIBUTE_DIRECTORY;
1081 newdir->d_inode->resolved = true;
1082 newdir->d_inode->ino = next_ino++;
1083 link_dentry(newdir, parent);
1087 /* Creates a regular file. */
1088 static int wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
1090 const char *stream_name;
1091 if ((mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
1092 && (stream_name = path_stream_name(path))) {
1093 /* Make an alternate data stream */
1094 struct ads_entry *new_entry;
1095 struct inode *inode;
1097 char *p = (char*)stream_name - 1;
1098 wimlib_assert(*p == ':');
1101 inode = wim_pathname_to_inode(w, path);
1104 if (!inode_is_regular_file(inode))
1106 if (inode_get_ads_entry(inode, stream_name, NULL))
1108 new_entry = inode_add_ads(inode, stream_name);
1112 struct dentry *dentry, *parent;
1113 const char *basename;
1115 /* Make a normal file (not an alternate data stream) */
1117 /* Make sure that the parent of @path exists and is a directory, and
1118 * that the dentry named by @path does not already exist. */
1119 parent = get_parent_dentry(w, path);
1122 if (!dentry_is_directory(parent))
1125 basename = path_basename(path);
1126 if (get_dentry_child_with_name(parent, path))
1129 dentry = new_dentry_with_inode(basename);
1132 dentry->d_inode->resolved = true;
1133 dentry->d_inode->ino = next_ino++;
1134 link_dentry(dentry, parent);
1141 static int wimfs_open(const char *path, struct fuse_file_info *fi)
1143 struct dentry *dentry;
1144 struct lookup_table_entry *lte;
1146 struct wimlib_fd *fd;
1147 struct inode *inode;
1151 ret = lookup_resource(w, path, get_lookup_flags(), &dentry, <e,
1156 inode = dentry->d_inode;
1158 if (stream_idx == 0)
1161 stream_id = inode->ads_entries[stream_idx - 1].stream_id;
1163 /* The file resource may be in the staging directory (read-write mounts
1164 * only) or in the WIM. If it's in the staging directory, we need to
1165 * open a native file descriptor for the corresponding file. Otherwise,
1166 * we can read the file resource directly from the WIM file if we are
1167 * opening it read-only, but we need to extract the resource to the
1168 * staging directory if we are opening it writable. */
1170 if (flags_writable(fi->flags) &&
1171 (!lte || lte->resource_location != RESOURCE_IN_STAGING_FILE)) {
1172 u64 size = (lte) ? wim_resource_size(lte) : 0;
1173 ret = extract_resource_to_staging_dir(inode, stream_id,
1179 ret = alloc_wimlib_fd(inode, stream_id, lte, &fd);
1183 if (lte && lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1184 fd->staging_fd = open(lte->staging_file_name, fi->flags);
1185 if (fd->staging_fd == -1) {
1186 close_wimlib_fd(fd);
1190 fi->fh = (uintptr_t)fd;
1194 /* Opens a directory. */
1195 static int wimfs_opendir(const char *path, struct fuse_file_info *fi)
1197 struct inode *inode;
1199 struct wimlib_fd *fd = NULL;
1201 inode = wim_pathname_to_inode(w, path);
1204 if (!inode_is_directory(inode))
1206 ret = alloc_wimlib_fd(inode, 0, NULL, &fd);
1207 fi->fh = (uintptr_t)fd;
1213 * Read data from a file in the WIM or in the staging directory.
1215 static int wimfs_read(const char *path, char *buf, size_t size,
1216 off_t offset, struct fuse_file_info *fi)
1218 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1223 if (!fd->f_lte) /* Empty stream with no lookup table entry */
1226 if (fd->f_lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1227 /* Read from staging file */
1229 wimlib_assert(fd->f_lte->staging_file_name);
1230 wimlib_assert(fd->staging_fd != -1);
1233 DEBUG("Seek to offset %zu", offset);
1235 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1237 ret = read(fd->staging_fd, buf, size);
1244 u64 res_size = wim_resource_size(fd->f_lte);
1246 if (offset > res_size)
1249 size = min(size, res_size - offset);
1251 if (read_wim_resource(fd->f_lte, (u8*)buf,
1252 size, offset, false) != 0)
1258 /* Fills in the entries of the directory specified by @path using the
1259 * FUSE-provided function @filler. */
1260 static int wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
1261 off_t offset, struct fuse_file_info *fi)
1263 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1264 struct inode *inode;
1265 struct dentry *child;
1270 inode = fd->f_inode;
1272 filler(buf, ".", NULL, 0);
1273 filler(buf, "..", NULL, 0);
1275 child = inode->children;
1281 if (filler(buf, child->file_name_utf8, NULL, 0))
1283 child = child->next;
1284 } while (child != inode->children);
1289 static int wimfs_readlink(const char *path, char *buf, size_t buf_len)
1291 struct inode *inode = wim_pathname_to_inode(w, path);
1295 if (!inode_is_symlink(inode))
1298 ret = inode_readlink(inode, buf, buf_len, w);
1305 static int wimfs_release(const char *path, struct fuse_file_info *fi)
1307 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1308 return close_wimlib_fd(fd);
1311 /* Close a directory */
1312 static int wimfs_releasedir(const char *path, struct fuse_file_info *fi)
1314 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1315 return close_wimlib_fd(fd);
1319 /* Remove an alternate data stream through the XATTR interface */
1320 static int wimfs_removexattr(const char *path, const char *name)
1322 struct inode *inode;
1323 struct ads_entry *ads_entry;
1325 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1328 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1332 inode = wim_pathname_to_inode(w, path);
1336 ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
1339 inode_remove_ads(inode, ads_idx, w->lookup_table);
1344 /* Renames a file or directory. See rename (3) */
1345 static int wimfs_rename(const char *from, const char *to)
1349 struct dentry *parent_of_dst;
1350 char *file_name_utf16 = NULL, *file_name_utf8 = NULL;
1351 u16 file_name_utf16_len, file_name_utf8_len;
1354 /* This rename() implementation currently only supports actual files
1355 * (not alternate data streams) */
1357 src = get_dentry(w, from);
1361 dst = get_dentry(w, to);
1364 ret = get_names(&file_name_utf16, &file_name_utf8,
1365 &file_name_utf16_len, &file_name_utf8_len,
1371 /* Destination file exists */
1373 if (src == dst) /* Same file */
1376 if (!dentry_is_directory(src)) {
1377 /* Cannot rename non-directory to directory. */
1378 if (dentry_is_directory(dst))
1381 /* Cannot rename directory to a non-directory or a non-empty
1383 if (!dentry_is_directory(dst))
1385 if (dst->d_inode->children != NULL)
1388 parent_of_dst = dst->parent;
1389 remove_dentry(dst, w->lookup_table);
1391 /* Destination does not exist */
1392 parent_of_dst = get_parent_dentry(w, to);
1396 if (!dentry_is_directory(parent_of_dst))
1400 FREE(src->file_name);
1401 FREE(src->file_name_utf8);
1402 src->file_name = file_name_utf16;
1403 src->file_name_utf8 = file_name_utf8;
1404 src->file_name_len = file_name_utf16_len;
1405 src->file_name_utf8_len = file_name_utf8_len;
1408 link_dentry(src, parent_of_dst);
1412 /* Remove a directory */
1413 static int wimfs_rmdir(const char *path)
1415 struct dentry *dentry;
1417 dentry = get_dentry(w, path);
1421 if (!dentry_is_empty_directory(dentry))
1424 remove_dentry(dentry, w->lookup_table);
1429 /* Write an alternate data stream through the XATTR interface */
1430 static int wimfs_setxattr(const char *path, const char *name,
1431 const char *value, size_t size, int flags)
1433 struct ads_entry *existing_ads_entry;
1434 struct ads_entry *new_ads_entry;
1435 struct lookup_table_entry *existing_lte;
1436 struct lookup_table_entry *lte;
1437 struct inode *inode;
1438 u8 value_hash[SHA1_HASH_SIZE];
1441 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1444 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1448 inode = wim_pathname_to_inode(w, path);
1452 existing_ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
1453 if (existing_ads_entry) {
1454 if (flags & XATTR_CREATE)
1456 inode_remove_ads(inode, ads_idx, w->lookup_table);
1458 if (flags & XATTR_REPLACE)
1461 new_ads_entry = inode_add_ads(inode, name);
1465 sha1_buffer((const u8*)value, size, value_hash);
1467 existing_lte = __lookup_resource(w->lookup_table, value_hash);
1474 lte = new_lookup_table_entry();
1477 value_copy = MALLOC(size);
1482 memcpy(value_copy, value, size);
1483 lte->resource_location = RESOURCE_IN_ATTACHED_BUFFER;
1484 lte->attached_buffer = value_copy;
1485 lte->resource_entry.original_size = size;
1486 lte->resource_entry.size = size;
1487 lte->resource_entry.flags = 0;
1488 copy_hash(lte->hash, value_hash);
1489 lookup_table_insert(w->lookup_table, lte);
1491 new_ads_entry->lte = lte;
1496 static int wimfs_symlink(const char *to, const char *from)
1498 struct dentry *dentry_parent, *dentry;
1499 const char *link_name;
1500 struct inode *inode;
1502 dentry_parent = get_parent_dentry(w, from);
1505 if (!dentry_is_directory(dentry_parent))
1508 link_name = path_basename(from);
1510 if (get_dentry_child_with_name(dentry_parent, link_name))
1512 dentry = new_dentry_with_inode(link_name);
1515 inode = dentry->d_inode;
1517 inode->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
1518 inode->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
1519 inode->ino = next_ino++;
1520 inode->resolved = true;
1522 if (inode_set_symlink(inode, to, w->lookup_table, NULL) != 0)
1523 goto out_free_dentry;
1525 link_dentry(dentry, dentry_parent);
1528 free_dentry(dentry);
1533 /* Reduce the size of a file */
1534 static int wimfs_truncate(const char *path, off_t size)
1536 struct dentry *dentry;
1537 struct lookup_table_entry *lte;
1541 struct inode *inode;
1543 ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1549 if (!lte) /* Already a zero-length file */
1552 inode = dentry->d_inode;
1554 if (stream_idx == 0)
1557 stream_id = inode->ads_entries[stream_idx - 1].stream_id;
1559 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1560 wimlib_assert(lte->staging_file_name);
1561 ret = truncate(lte->staging_file_name, size);
1564 lte->resource_entry.original_size = size;
1566 wimlib_assert(lte->resource_location == RESOURCE_IN_WIM);
1567 /* File in WIM. Extract it to the staging directory, but only
1568 * the first @size bytes of it. */
1569 ret = extract_resource_to_staging_dir(inode, stream_id,
1575 /* Remove a regular file */
1576 static int wimfs_unlink(const char *path)
1578 struct dentry *dentry;
1579 struct lookup_table_entry *lte;
1580 struct inode *inode;
1585 ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1591 if (stream_idx == 0)
1592 remove_dentry(dentry, w->lookup_table);
1594 inode_remove_ads(dentry->d_inode, stream_idx - 1, w->lookup_table);
1598 #ifdef HAVE_UTIMENSAT
1600 * Change the timestamp on a file dentry.
1602 * Note that alternate data streams do not have their own timestamps.
1604 static int wimfs_utimens(const char *path, const struct timespec tv[2])
1606 struct dentry *dentry;
1607 struct inode *inode;
1608 dentry = get_dentry(w, path);
1611 inode = dentry->d_inode;
1613 if (tv[0].tv_nsec != UTIME_OMIT) {
1614 if (tv[0].tv_nsec == UTIME_NOW)
1615 inode->last_access_time = get_wim_timestamp();
1617 inode->last_access_time = timespec_to_wim_timestamp(&tv[0]);
1619 if (tv[1].tv_nsec != UTIME_OMIT) {
1620 if (tv[1].tv_nsec == UTIME_NOW)
1621 inode->last_write_time = get_wim_timestamp();
1623 inode->last_write_time = timespec_to_wim_timestamp(&tv[1]);
1628 static int wimfs_utime(const char *path, struct utimbuf *times)
1630 struct dentry *dentry;
1631 struct inode *inode;
1632 dentry = get_dentry(w, path);
1635 inode = dentry->d_inode;
1637 inode->last_write_time = unix_timestamp_to_wim(times->modtime);
1638 inode->last_access_time = unix_timestamp_to_wim(times->actime);
1643 /* Writes to a file in the WIM filesystem.
1644 * It may be an alternate data stream, but here we don't even notice because we
1645 * just get a lookup table entry. */
1646 static int wimfs_write(const char *path, const char *buf, size_t size,
1647 off_t offset, struct fuse_file_info *fi)
1649 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1656 wimlib_assert(fd->f_lte);
1657 wimlib_assert(fd->f_lte->staging_file_name);
1658 wimlib_assert(fd->staging_fd != -1);
1659 wimlib_assert(fd->f_inode);
1661 /* Seek to the requested position */
1662 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1665 /* Write the data. */
1666 ret = write(fd->staging_fd, buf, size);
1670 now = get_wim_timestamp();
1671 fd->f_inode->last_write_time = now;
1672 fd->f_inode->last_access_time = now;
1676 static struct fuse_operations wimfs_operations = {
1677 .access = wimfs_access,
1678 .destroy = wimfs_destroy,
1680 .fallocate = wimfs_fallocate,
1682 .fgetattr = wimfs_fgetattr,
1683 .ftruncate = wimfs_ftruncate,
1684 .getattr = wimfs_getattr,
1686 .getxattr = wimfs_getxattr,
1690 .listxattr = wimfs_listxattr,
1692 .mkdir = wimfs_mkdir,
1693 .mknod = wimfs_mknod,
1695 .opendir = wimfs_opendir,
1697 .readdir = wimfs_readdir,
1698 .readlink = wimfs_readlink,
1699 .release = wimfs_release,
1700 .releasedir = wimfs_releasedir,
1702 .removexattr = wimfs_removexattr,
1704 .rename = wimfs_rename,
1705 .rmdir = wimfs_rmdir,
1707 .setxattr = wimfs_setxattr,
1709 .symlink = wimfs_symlink,
1710 .truncate = wimfs_truncate,
1711 .unlink = wimfs_unlink,
1712 #ifdef HAVE_UTIMENSAT
1713 .utimens = wimfs_utimens,
1715 .utime = wimfs_utime,
1717 .write = wimfs_write,
1721 /* Mounts a WIM file. */
1722 WIMLIBAPI int wimlib_mount(WIMStruct *wim, int image, const char *dir,
1723 int flags, WIMStruct **additional_swms,
1724 unsigned num_additional_swms)
1730 struct lookup_table *joined_tab, *wim_tab_save;
1731 struct image_metadata *imd;
1733 DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
1734 wim, image, dir, flags);
1737 return WIMLIB_ERR_INVALID_PARAM;
1739 ret = verify_swm_set(wim, additional_swms, num_additional_swms);
1743 if (num_additional_swms) {
1744 ret = new_joined_lookup_table(wim, additional_swms,
1745 num_additional_swms,
1749 wim_tab_save = wim->lookup_table;
1750 wim->lookup_table = joined_tab;
1753 ret = select_wim_image(wim, image);
1758 imd = &wim->image_metadata[image - 1];
1760 DEBUG("Selected image %d", image);
1762 if (imd->root_dentry->refcnt != 1) {
1763 ERROR("Cannot mount image that was just exported with "
1765 ret = WIMLIB_ERR_INVALID_PARAM;
1769 if (!(flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
1770 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
1771 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
1772 flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1774 DEBUG("Getting current directory");
1776 working_directory = getcwd(NULL, 0);
1777 if (!working_directory) {
1778 ERROR_WITH_ERRNO("Could not determine current directory");
1779 ret = WIMLIB_ERR_NOTDIR;
1783 DEBUG("Unlinking message queues in case they already exist");
1784 ret = set_message_queue_names(dir);
1786 goto out_free_working_directory;
1787 unlink_message_queues();
1789 DEBUG("Preparing arguments to fuse_main()");
1791 dir_copy = STRDUP(dir);
1793 goto out_free_message_queue_names;
1795 argv[argc++] = "imagex";
1796 argv[argc++] = dir_copy;
1797 argv[argc++] = "-s"; /* disable multi-threaded operation */
1799 if (flags & WIMLIB_MOUNT_FLAG_DEBUG)
1800 argv[argc++] = "-d";
1803 * We provide the use_ino option because we are going to assign inode
1804 * numbers oursides. The inodes will be given unique numbers in the
1805 * assign_inode_numbers() function, and the static variable @next_ino is
1806 * set to the next available inode number.
1808 char optstring[256] = "use_ino,subtype=wimfs,attr_timeout=0";
1809 argv[argc++] = "-o";
1810 argv[argc++] = optstring;
1811 if ((flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
1812 /* Read-write mount. Make the staging directory */
1814 if (!staging_dir_name) {
1815 ret = WIMLIB_ERR_MKDIR;
1816 goto out_free_dir_copy;
1819 /* Read-only mount */
1820 strcat(optstring, ",ro");
1827 DEBUG("FUSE command line (argc = %d): ", argc);
1828 for (i = 0; i < argc; i++) {
1829 fputs(argv[i], stdout);
1838 /* Mark dentry tree as modified if read-write mount. */
1839 if (flags & WIMLIB_MOUNT_FLAG_READWRITE)
1840 imd->modified = true;
1842 next_ino = assign_inode_numbers(&imd->inode_list);
1844 DEBUG("(next_ino = %"PRIu64")", next_ino);
1846 /* Resolve all the lookup table entries of the dentry tree */
1847 DEBUG("Resolving lookup table entries");
1848 for_dentry_in_tree(imd->root_dentry, dentry_resolve_ltes,
1851 /* Set static variables. */
1853 mount_flags = flags;
1855 ret = fuse_main(argc, argv, &wimfs_operations, NULL);
1857 ret = WIMLIB_ERR_FUSE;
1860 out_free_message_queue_names:
1861 free_message_queue_names();
1862 out_free_working_directory:
1863 FREE(working_directory);
1864 working_directory = NULL;
1866 if (num_additional_swms) {
1867 free_lookup_table(wim->lookup_table);
1868 wim->lookup_table = wim_tab_save;
1875 * Unmounts the WIM file that was previously mounted on @dir by using
1878 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
1885 struct timespec timeout;
1889 /* Open message queues between the unmount process and the
1890 * filesystem daemon. */
1891 ret = set_message_queue_names(dir);
1895 ret = open_message_queues(false);
1899 /* Send a message to the filesystem saying whether to commit or
1901 msg[0] = (flags & WIMLIB_UNMOUNT_FLAG_COMMIT) ? 1 : 0;
1902 msg[1] = (flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY) ? 1 : 0;
1904 DEBUG("Sending message: %s, %s",
1905 (msg[0] == 0) ? "don't commit" : "commit",
1906 (msg[1] == 0) ? "don't check" : "check");
1907 ret = mq_send(unmount_to_daemon_mq, msg, 2, 1);
1909 ERROR("Failed to notify filesystem daemon whether we want to "
1910 "commit changes or not");
1911 close_message_queues();
1912 return WIMLIB_ERR_MQUEUE;
1915 /* Execute `fusermount -u', which is installed setuid root, to unmount
1918 * FUSE does not yet implement synchronous unmounts. This means that
1919 * fusermount -u will return before the filesystem daemon returns from
1920 * wimfs_destroy(). This is partly what we want, because we need to
1921 * send a message from this process to the filesystem daemon telling
1922 * whether --commit was specified or not. However, after that, the
1923 * unmount process must wait for the filesystem daemon to finish writing
1930 ERROR_WITH_ERRNO("Failed to fork()");
1931 return WIMLIB_ERR_FORK;
1934 execlp("fusermount", "fusermount", "-u", dir, NULL);
1935 ERROR_WITH_ERRNO("Failed to execute `fusermount'");
1936 exit(WIMLIB_ERR_FUSERMOUNT);
1939 ret = wait(&status);
1941 ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
1943 return WIMLIB_ERR_FUSERMOUNT;
1947 ERROR("fusermount exited with status %d", status);
1949 /* Try again, but with the `umount' program. This is required
1950 * on other FUSE implementations such as FreeBSD's that do not
1951 * have a `fusermount' program. */
1955 ERROR_WITH_ERRNO("Failed to fork()");
1956 return WIMLIB_ERR_FORK;
1959 execlp("umount", "umount", dir, NULL);
1960 ERROR_WITH_ERRNO("Failed to execute `umount'");
1961 exit(WIMLIB_ERR_FUSERMOUNT);
1964 ret = wait(&status);
1966 ERROR_WITH_ERRNO("Failed to wait for `umount' process to "
1968 return WIMLIB_ERR_FUSERMOUNT;
1971 ERROR("`umount' exited with failure status");
1972 return WIMLIB_ERR_FUSERMOUNT;
1977 /* Wait for a message from the filesytem daemon indicating whether the
1978 * filesystem was unmounted successfully (0) or an error occurred (1).
1979 * This may take a long time if a big WIM file needs to be rewritten. */
1981 /* Wait at most 600??? seconds before giving up and returning false.
1982 * Either it's a really big WIM file, or (more likely) the
1983 * filesystem daemon has crashed or failed for some reason.
1985 * XXX come up with some method to determine if the filesystem
1986 * daemon has really crashed or not.
1988 * XXX Idea: have mount daemon write its PID into the WIM file header?
1991 gettimeofday(&now, NULL);
1992 timeout.tv_sec = now.tv_sec + 600;
1993 timeout.tv_nsec = now.tv_usec * 1000;
1995 msgsize = mq_get_msgsize(daemon_to_unmount_mq);
1996 char mailbox[msgsize];
1999 DEBUG("Waiting for message telling us whether the unmount was "
2000 "successful or not.");
2001 ret = mq_timedreceive(daemon_to_unmount_mq, mailbox, msgsize,
2004 close_message_queues();
2006 if (errno_save == ETIMEDOUT) {
2007 ERROR("Timed out- probably the filesystem daemon "
2008 "crashed and the WIM was not written "
2010 return WIMLIB_ERR_TIMEOUT;
2012 ERROR("mq_receive(): %s", strerror(errno_save));
2013 return WIMLIB_ERR_MQUEUE;
2017 DEBUG("Received message: %s",
2018 (mailbox[0] == 0) ? "Unmount OK" : "Unmount Failed");
2019 if (mailbox[0] != 0)
2020 ERROR("Unmount failed");
2024 #else /* WITH_FUSE */
2027 static inline int mount_unsupported_error()
2029 ERROR("WIMLIB was compiled with --without-fuse, which disables support "
2030 "for mounting WIMs.");
2031 return WIMLIB_ERR_UNSUPPORTED;
2034 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
2036 return mount_unsupported_error();
2039 WIMLIBAPI int wimlib_mount(WIMStruct *wim_p, int image, const char *dir,
2040 int flags, WIMStruct **additional_swms,
2041 unsigned num_additional_swms)
2043 return mount_unsupported_error();
2046 #endif /* WITH_FUSE */