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 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);
104 * Allocate a file descriptor for a stream.
106 * @inode: inode containing the stream we're opening
107 * @stream_id: ID of the stream we're opening
108 * @lte: Lookup table entry for the stream (may be NULL)
109 * @fd_ret: Return the allocated file descriptor if successful.
111 * Return 0 iff successful or error code if unsuccessful.
113 static int alloc_wimlib_fd(struct inode *inode,
115 struct lookup_table_entry *lte,
116 struct wimlib_fd **fd_ret)
118 static const u16 fds_per_alloc = 8;
119 static const u16 max_fds = 0xffff;
121 DEBUG("Allocating fd for stream ID %u from inode %lx (open = %u, allocated = %u)",
122 stream_id, inode->ino, inode->num_opened_fds,
123 inode->num_allocated_fds);
125 if (inode->num_opened_fds == inode->num_allocated_fds) {
126 struct wimlib_fd **fds;
129 if (inode->num_allocated_fds == max_fds)
131 num_new_fds = min(fds_per_alloc,
132 max_fds - inode->num_allocated_fds);
134 fds = REALLOC(inode->fds,
135 (inode->num_allocated_fds + num_new_fds) *
136 sizeof(inode->fds[0]));
139 memset(&fds[inode->num_allocated_fds], 0,
140 num_new_fds * sizeof(fds[0]));
142 inode->num_allocated_fds += num_new_fds;
144 for (u16 i = 0; ; i++) {
145 if (!inode->fds[i]) {
146 struct wimlib_fd *fd = CALLOC(1, sizeof(*fd));
153 fd->stream_id = stream_id;
156 inode->num_opened_fds++;
158 lte->num_opened_fds++;
159 DEBUG("Allocated fd");
165 static void inode_put_fd(struct inode *inode, struct wimlib_fd *fd)
168 wimlib_assert(inode);
169 wimlib_assert(fd->f_inode == inode);
170 wimlib_assert(inode->num_opened_fds);
171 wimlib_assert(fd->idx < inode->num_opened_fds);
172 wimlib_assert(inode->fds[fd->idx] == fd);
174 inode->fds[fd->idx] = NULL;
176 if (--inode->num_opened_fds == 0 && inode->link_count == 0)
180 static int lte_put_fd(struct lookup_table_entry *lte, struct wimlib_fd *fd)
183 wimlib_assert(fd->f_lte == lte);
185 if (!lte) /* Empty stream with no lookup table entry */
188 /* Close staging file descriptor if needed. */
189 wimlib_assert(lte->num_opened_fds);
191 if (lte->resource_location == RESOURCE_IN_STAGING_FILE
192 && fd->staging_fd != -1)
194 if (close(fd->staging_fd) != 0) {
195 ERROR_WITH_ERRNO("Failed to close staging file");
199 lte_decrement_num_opened_fds(lte, w->lookup_table);
203 /* Close a file descriptor. */
204 static int close_wimlib_fd(struct wimlib_fd *fd)
208 DEBUG("Closing fd (inode = %lu, opened = %u, allocated = %u)",
209 fd->f_inode->ino, fd->f_inode->num_opened_fds,
210 fd->f_inode->num_allocated_fds);
211 ret = lte_put_fd(fd->f_lte, fd);
215 inode_put_fd(fd->f_inode, fd);
219 /* Remove a dentry; i.e. remove a reference to the corresponding inode.
221 * If there are no remaining references to the inode either through dentries or
222 * open file descriptors, the inode is freed. Otherwise, the inode is not
223 * removed, but the dentry is unlinked and freed.
225 * Either way, all lookup table entries referenced by the inode have their
226 * reference count decremented. If a lookup table entry has no open file
227 * descriptors and no references remaining, it is freed, and the staging file is
230 static void remove_dentry(struct dentry *dentry,
231 struct lookup_table *lookup_table)
233 struct inode *inode = dentry->d_inode;
234 struct lookup_table_entry *lte;
237 for (i = 0; i <= inode->num_ads; i++) {
238 lte = inode_stream_lte_resolved(inode, i);
240 lte_decrement_refcnt(lte, lookup_table);
242 unlink_dentry(dentry);
246 /* Creates a new staging file and returns its file descriptor opened for
249 * @name_ret: A location into which the a pointer to the newly allocated name of
250 * the staging file is stored.
251 * @return: The file descriptor for the new file. Returns -1 and sets errno on
252 * error, for any reason possible from the creat() function.
254 static int create_staging_file(char **name_ret, int open_flags)
262 name_len = staging_dir_name_len + 1 + SHA1_HASH_SIZE;
263 name = MALLOC(name_len + 1);
271 memcpy(name, staging_dir_name, staging_dir_name_len);
272 name[staging_dir_name_len] = '/';
273 randomize_char_array_with_alnum(name + staging_dir_name_len + 1,
275 name[name_len] = '\0';
278 /* Just in case, verify that the randomly generated name doesn't name an
279 * existing file, and try again if so */
280 } while (stat(name, &stbuf) == 0);
286 /* doesn't exist--- ok */
288 DEBUG("Creating staging file `%s'", name);
290 fd = open(name, open_flags | O_CREAT | O_TRUNC, 0600);
302 * Extract a WIM resource to the staging directory.
304 * @inode: Inode that contains the stream we are extracting
306 * @stream_id: Identifier for the stream (it stays constant even if the indices
307 * of the stream entries are changed)
309 * @lte: Pointer to pointer to the lookup table entry for the stream we need to
310 * extract, or NULL if there was no lookup table entry present for the stream
312 * @size: Number of bytes of the stream we want to extract (this supports the
313 * wimfs_truncate() function).
315 static int extract_resource_to_staging_dir(struct inode *inode,
317 struct lookup_table_entry **lte,
320 char *staging_file_name;
323 struct lookup_table_entry *old_lte, *new_lte;
326 fd = create_staging_file(&staging_file_name, O_WRONLY);
331 ret = extract_wim_resource_to_fd(old_lte, fd, size);
334 if (ret != 0 || close(fd) != 0) {
340 goto out_delete_staging_file;
343 if (old_lte && inode->link_count == old_lte->refcnt) {
344 /* The reference count of the existing lookup table
345 * entry is the same as the link count of the inode that
346 * contains the stream we're opening. Therefore, ALL
347 * the references to the lookup table entry correspond
348 * to the stream we're trying to extract, so the lookup
349 * table entry can be re-used. */
350 DEBUG("Re-using lookup table entry");
351 lookup_table_unlink(w->lookup_table, old_lte);
355 /* There's an existing lookup table entry, but its
356 * reference count is creater than the link count for
357 * the inode containing a stream we're opening.
358 * Therefore, we need to split the lookup table entry.
360 wimlib_assert(old_lte->refcnt > inode->link_count);
361 DEBUG("Splitting lookup table entry "
362 "(inode->link_count = %zu, old_lte->refcnt = %u)",
363 inode->link_count, old_lte->refcnt);
367 new_lte = new_lookup_table_entry();
370 goto out_delete_staging_file;
373 /* There may already be open file descriptors to this stream if
374 * it's previously been opened read-only, but just now we're
375 * opening it read-write. Identify those file descriptors and
376 * change their lookup table entry pointers to point to the new
377 * lookup table entry, and open staging file descriptors for
380 * At the same time, we need to count the number of these opened
381 * file descriptors to the new lookup table entry. If there's
382 * an old lookup table entry, this number needs to be subtracted
383 * from the fd's opened to the old entry. */
384 for (u16 i = 0, j = 0; j < inode->num_opened_fds; i++) {
385 struct wimlib_fd *fd = inode->fds[i];
387 if (fd->stream_id == stream_id) {
388 wimlib_assert(fd->f_lte == old_lte);
389 wimlib_assert(fd->staging_fd == -1);
391 new_lte->num_opened_fds++;
392 fd->staging_fd = open(staging_file_name, O_RDONLY);
393 if (fd->staging_fd == -1) {
395 goto out_revert_fd_changes;
401 DEBUG("%zu fd's were already opened to the file we extracted",
402 new_lte->num_opened_fds);
404 old_lte->num_opened_fds -= new_lte->num_opened_fds;
405 old_lte->refcnt -= inode->link_count;
409 new_lte->resource_entry.original_size = size;
410 new_lte->refcnt = inode->link_count;
411 new_lte->resource_location = RESOURCE_IN_STAGING_FILE;
412 new_lte->staging_file_name = staging_file_name;
413 new_lte->lte_inode = inode;
414 random_hash(new_lte->hash);
417 inode->lte = new_lte;
419 for (u16 i = 0; i < inode->num_ads; i++)
420 if (inode->ads_entries[i].stream_id == stream_id)
421 inode->ads_entries[i].lte = new_lte;
423 lookup_table_insert(w->lookup_table, new_lte);
424 list_add(&new_lte->staging_list, &staging_list);
427 out_revert_fd_changes:
428 for (u16 i = 0, j = 0; j < new_lte->num_opened_fds; i++) {
429 struct wimlib_fd *fd = inode->fds[i];
430 if (fd && fd->stream_id == stream_id && fd->f_lte == new_lte) {
432 if (fd->staging_fd != -1) {
433 close(fd->staging_fd);
440 free_lookup_table_entry(new_lte);
441 out_delete_staging_file:
442 unlink(staging_file_name);
443 FREE(staging_file_name);
448 * Creates a randomly named staging directory and returns its name into the
449 * static variable staging_dir_name.
451 * If the staging directory cannot be created, staging_dir_name is set to NULL.
453 static void make_staging_dir()
455 /* XXX Give the user an option of where to stage files */
457 static char prefix[] = "wimlib-staging-";
458 static const size_t prefix_len = 15;
459 static const size_t suffix_len = 10;
461 size_t pwd_len = strlen(working_directory);
463 staging_dir_name_len = pwd_len + 1 + prefix_len + suffix_len;
465 staging_dir_name = MALLOC(staging_dir_name_len + 1);
466 if (!staging_dir_name) {
467 ERROR("Out of memory");
471 memcpy(staging_dir_name, working_directory, pwd_len);
472 staging_dir_name[pwd_len] = '/';
473 memcpy(staging_dir_name + pwd_len + 1, prefix, prefix_len);
474 randomize_char_array_with_alnum(staging_dir_name + pwd_len + 1 + prefix_len,
476 staging_dir_name[staging_dir_name_len] = '\0';
478 if (mkdir(staging_dir_name, 0700) != 0) {
479 ERROR_WITH_ERRNO("Failed to create temporary directory `%s'",
481 FREE(staging_dir_name);
482 staging_dir_name = NULL;
486 static int remove_file_or_directory(const char *fpath, const struct stat *sb,
487 int typeflag, struct FTW *ftwbuf)
489 if (remove(fpath) == 0)
492 return WIMLIB_ERR_DELETE_STAGING_DIR;
497 * Deletes the staging directory and all the files contained in it.
499 static int delete_staging_dir()
503 ret = nftw(staging_dir_name, remove_file_or_directory,10, FTW_DEPTH);
504 staging_dir_name = NULL;
508 /* Name and message queue descriptors for message queues between the filesystem
509 * daemon process and the unmount process. These are used when the filesystem
510 * is unmounted and the process running wimlib_mount() (i.e. the `imagex
511 * unmount' command) needs to communicate with the filesystem daemon running
512 * fuse_main() (i.e. that spawned by the `imagex mount' or `imagex mountrw'
514 static char *unmount_to_daemon_mq_name;
515 static char *daemon_to_unmount_mq_name;
516 static mqd_t unmount_to_daemon_mq;
517 static mqd_t daemon_to_unmount_mq;
519 /* Simple function that returns the concatenation of 4 strings. */
520 static char *strcat_dup(const char *s1, const char *s2, const char *s3,
523 size_t len = strlen(s1) + strlen(s2) + strlen(s3) + strlen(s4) + 1;
524 char *p = MALLOC(len);
530 return strcat(p, s4);
533 /* Removes trailing forward slashes in a string. */
534 static void remove_trailing_slashes(char *s)
536 long len = strlen(s);
537 for (long i = len - 1; i >= 1; i--) {
545 /* Changes forward slashes to underscores in a string. */
546 static void s_slashes_underscores_g(char *s)
556 * Opens two POSIX message queue: one for sending messages from the unmount
557 * process to the daemon process, and one to go the other way. The names of the
558 * message queues, which must be system-wide unique, are be based on the mount
559 * point. (There of course is still a possibility of a collision if one were to
560 * unmount two identically named directories simultaneously...)
562 * @daemon specifies whether the calling process is the filesystem daemon or the
565 static int open_message_queues(bool daemon)
567 static const char *slash = "/";
568 static const char *prefix = "wimlib-";
569 static const char *u2d_suffix = "unmount-to-daemon-mq";
570 static const char *d2u_suffix = "daemon-to-unmount-mq";
572 const char *mount_dir_basename = path_basename(mount_dir);
576 unmount_to_daemon_mq_name = strcat_dup(slash, mount_dir_basename,
578 if (!unmount_to_daemon_mq_name) {
579 ERROR("Out of memory");
580 return WIMLIB_ERR_NOMEM;
582 daemon_to_unmount_mq_name = strcat_dup(slash, mount_dir_basename,
584 if (!daemon_to_unmount_mq_name) {
585 ERROR("Out of memory");
586 ret = WIMLIB_ERR_NOMEM;
590 remove_trailing_slashes(unmount_to_daemon_mq_name);
591 remove_trailing_slashes(daemon_to_unmount_mq_name);
592 s_slashes_underscores_g(unmount_to_daemon_mq_name + 1);
593 s_slashes_underscores_g(daemon_to_unmount_mq_name + 1);
596 flags = O_RDONLY | O_CREAT;
598 flags = O_WRONLY | O_CREAT;
600 unmount_to_daemon_mq = mq_open(unmount_to_daemon_mq_name, flags,
603 if (unmount_to_daemon_mq == (mqd_t)-1) {
604 ERROR_WITH_ERRNO("mq_open()");
605 ret = WIMLIB_ERR_MQUEUE;
610 flags = O_WRONLY | O_CREAT;
612 flags = O_RDONLY | O_CREAT;
614 daemon_to_unmount_mq = mq_open(daemon_to_unmount_mq_name, flags,
617 if (daemon_to_unmount_mq == (mqd_t)-1) {
618 ERROR_WITH_ERRNO("mq_open()");
619 ret = WIMLIB_ERR_MQUEUE;
624 mq_close(unmount_to_daemon_mq);
625 mq_unlink(unmount_to_daemon_mq_name);
627 FREE(daemon_to_unmount_mq_name);
629 FREE(unmount_to_daemon_mq_name);
633 static int mq_get_msgsize(mqd_t mq)
635 static const char *msgsize_max_file = "/proc/sys/fs/mqueue/msgsize_max";
640 if (mq_getattr(unmount_to_daemon_mq, &attr) == 0) {
641 msgsize = attr.mq_msgsize;
643 ERROR_WITH_ERRNO("mq_getattr()");
644 ERROR("Attempting to read %s", msgsize_max_file);
645 fp = fopen(msgsize_max_file, "rb");
647 if (fscanf(fp, "%d", &msgsize) != 1) {
648 ERROR("Assuming message size of 8192");
653 ERROR_WITH_ERRNO("Failed to open the file `%s'",
655 ERROR("Assuming message size of 8192");
662 /* Closes the message queues, which are allocated in static variables */
663 static void close_message_queues()
665 mq_close(unmount_to_daemon_mq);
666 mq_close(daemon_to_unmount_mq);
667 mq_unlink(unmount_to_daemon_mq_name);
668 mq_unlink(daemon_to_unmount_mq_name);
671 static int wimfs_access(const char *path, int mask)
673 /* Permissions not implemented */
677 static void inode_update_lte_ptr(struct inode *inode,
678 struct lookup_table_entry *old_lte,
679 struct lookup_table_entry *new_lte)
681 if (inode->lte == old_lte) {
682 inode->lte = new_lte;
684 for (unsigned i = 0; i < inode->num_ads; i++) {
685 if (inode->ads_entries[i].lte == old_lte) {
686 inode->ads_entries[i].lte = new_lte;
693 static int update_lte_of_staging_file(struct lookup_table_entry *lte,
694 struct lookup_table *table)
696 struct lookup_table_entry *duplicate_lte;
698 u8 hash[SHA1_HASH_SIZE];
701 wimlib_assert(lte->resource_location == RESOURCE_IN_STAGING_FILE);
702 wimlib_assert(lte->staging_file_name);
704 ret = sha1sum(lte->staging_file_name, hash);
708 lookup_table_unlink(table, lte);
710 duplicate_lte = __lookup_resource(table, hash);
713 /* Merge duplicate lookup table entries */
714 duplicate_lte->refcnt += lte->refcnt;
715 list_del(<e->staging_list);
716 inode_update_lte_ptr(lte->lte_inode, lte, duplicate_lte);
717 free_lookup_table_entry(lte);
719 if (stat(lte->staging_file_name, &stbuf) != 0) {
720 ERROR_WITH_ERRNO("Failed to stat `%s'", lte->staging_file_name);
721 return WIMLIB_ERR_STAT;
723 if (stbuf.st_size == 0) {
724 /* Zero-length stream. No lookup table entry needed. */
725 inode_update_lte_ptr(lte->lte_inode, lte, NULL);
726 free_lookup_table_entry(lte);
728 wimlib_assert(<e->file_on_disk == <e->staging_file_name);
729 lte->resource_entry.original_size = stbuf.st_size;
730 lte->resource_entry.size = stbuf.st_size;
731 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
732 lte->lte_inode = NULL;
733 copy_hash(lte->hash, hash);
734 lookup_table_insert(table, lte);
740 static int inode_close_fds(struct inode *inode)
742 u16 num_opened_fds = inode->num_opened_fds;
743 for (u16 i = 0, j = 0; j < num_opened_fds; i++) {
744 struct wimlib_fd *fd = inode->fds[i];
746 wimlib_assert(fd->f_inode == inode);
747 int ret = close_wimlib_fd(fd);
756 /* Overwrites the WIM file, with changes saved. */
757 static int rebuild_wim(WIMStruct *w, bool check_integrity)
760 struct lookup_table_entry *lte, *tmp;
763 DEBUG("Closing all staging file descriptors.");
764 list_for_each_entry(lte, &staging_list, staging_list) {
765 ret = inode_close_fds(lte->lte_inode);
770 DEBUG("Calculating SHA1 checksums for all new staging files.");
771 list_for_each_entry_safe(lte, tmp, &staging_list, staging_list) {
772 ret = update_lte_of_staging_file(lte, w->lookup_table);
777 xml_update_image_info(w, w->current_image);
779 ret = wimlib_overwrite(w, check_integrity);
781 ERROR("Failed to commit changes");
787 /* Called when the filesystem is unmounted. */
788 static void wimfs_destroy(void *p)
790 /* For read-write mounts, the `imagex unmount' command, which is
791 * running in a separate process and is executing the
792 * wimlib_unmount() function, will send this process a byte
793 * through a message queue that indicates whether the --commit
794 * option was specified or not. */
797 struct timespec timeout;
799 ssize_t bytes_received;
802 char check_integrity;
805 ret = open_message_queues(true);
809 msgsize = mq_get_msgsize(unmount_to_daemon_mq);
814 /* Wait at most 3 seconds before giving up and discarding changes. */
815 gettimeofday(&now, NULL);
816 timeout.tv_sec = now.tv_sec + 3;
817 timeout.tv_nsec = now.tv_usec * 1000;
818 DEBUG("Waiting for message telling us whether to commit or not, and "
819 "whether to include integrity checks.");
821 bytes_received = mq_timedreceive(unmount_to_daemon_mq, msg,
822 msgsize, NULL, &timeout);
824 check_integrity = msg[1];
825 if (bytes_received == -1) {
826 if (errno == ETIMEDOUT) {
829 ERROR_WITH_ERRNO("mq_timedreceive()");
831 ERROR("Not committing.");
833 DEBUG("Received message: [%d %d]", msg[0], msg[1]);
837 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
839 status = chdir(working_directory);
841 ERROR_WITH_ERRNO("chdir()");
842 status = WIMLIB_ERR_NOTDIR;
845 status = rebuild_wim(w, (check_integrity != 0));
847 ret = delete_staging_dir();
849 ERROR_WITH_ERRNO("Failed to delete the staging "
855 DEBUG("Read-only mount");
858 DEBUG("Sending status %u", status);
859 ret = mq_send(daemon_to_unmount_mq, &status, 1, 1);
861 ERROR_WITH_ERRNO("Failed to send status to unmount process");
862 close_message_queues();
866 static int wimfs_fallocate(const char *path, int mode,
867 off_t offset, off_t len, struct fuse_file_info *fi)
869 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
870 wimlib_assert(fd->staging_fd != -1);
871 return fallocate(fd->staging_fd, mode, offset, len);
876 static int wimfs_fgetattr(const char *path, struct stat *stbuf,
877 struct fuse_file_info *fi)
879 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
880 return inode_to_stbuf(fd->f_inode, fd->f_lte, stbuf);
883 static int wimfs_ftruncate(const char *path, off_t size,
884 struct fuse_file_info *fi)
886 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
887 int ret = ftruncate(fd->staging_fd, size);
890 if (fd->f_lte && size < fd->f_lte->resource_entry.original_size)
891 fd->f_lte->resource_entry.original_size = size;
896 * Fills in a `struct stat' that corresponds to a file or directory in the WIM.
898 static int wimfs_getattr(const char *path, struct stat *stbuf)
900 struct dentry *dentry;
901 struct lookup_table_entry *lte;
904 ret = lookup_resource(w, path,
905 get_lookup_flags() | LOOKUP_FLAG_DIRECTORY_OK,
906 &dentry, <e, NULL);
909 return inode_to_stbuf(dentry->d_inode, lte, stbuf);
913 /* Read an alternate data stream through the XATTR interface, or get its size */
914 static int wimfs_getxattr(const char *path, const char *name, char *value,
918 struct dentry *dentry;
919 struct ads_entry *ads_entry;
921 struct lookup_table_entry *lte;
923 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
926 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
930 dentry = get_dentry(w, path);
933 ads_entry = inode_get_ads_entry(dentry->d_inode, name, NULL);
937 lte = ads_entry->lte;
938 res_size = wim_resource_size(lte);
944 ret = read_full_wim_resource(lte, (u8*)value);
951 /* Create a hard link */
952 static int wimfs_link(const char *to, const char *from)
954 struct dentry *from_dentry, *from_dentry_parent;
955 const char *link_name;
957 struct lookup_table_entry *lte;
959 inode = wim_pathname_to_inode(w, to);
963 if (!inode_is_regular_file(inode))
966 from_dentry_parent = get_parent_dentry(w, from);
967 if (!from_dentry_parent)
969 if (!dentry_is_directory(from_dentry_parent))
972 link_name = path_basename(from);
973 if (get_dentry_child_with_name(from_dentry_parent, link_name))
975 from_dentry = new_dentry(link_name);
980 inode_add_dentry(from_dentry, inode);
981 from_dentry->d_inode = inode;
984 for (unsigned i = 0; i <= inode->num_ads; i++) {
985 lte = inode_stream_lte_resolved(inode, i);
990 link_dentry(from_dentry, from_dentry_parent);
995 static int wimfs_listxattr(const char *path, char *list, size_t size)
997 struct dentry *dentry;
1002 struct inode *inode;
1003 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1006 /* List alternate data streams, or get the list size */
1008 ret = lookup_resource(w, path, get_lookup_flags(), &dentry, NULL, NULL);
1011 inode = dentry->d_inode;
1015 for (i = 0; i < inode->num_ads; i++)
1016 needed_size += inode->ads_entries[i].stream_name_utf8_len + 6;
1019 for (i = 0; i < inode->num_ads; i++) {
1020 needed_size = inode->ads_entries[i].stream_name_utf8_len + 6;
1021 if (needed_size > size)
1023 p += sprintf(p, "user.%s",
1024 inode->ads_entries[i].stream_name_utf8) + 1;
1025 size -= needed_size;
1033 * Create a directory in the WIM.
1034 * @mode is currently ignored.
1036 static int wimfs_mkdir(const char *path, mode_t mode)
1038 struct dentry *parent;
1039 struct dentry *newdir;
1040 const char *basename;
1042 parent = get_parent_dentry(w, path);
1046 if (!dentry_is_directory(parent))
1049 basename = path_basename(path);
1050 if (get_dentry_child_with_name(parent, basename))
1053 newdir = new_dentry_with_inode(basename);
1054 newdir->d_inode->attributes |= FILE_ATTRIBUTE_DIRECTORY;
1055 newdir->d_inode->resolved = true;
1056 newdir->d_inode->ino = next_ino++;
1057 link_dentry(newdir, parent);
1061 /* Creates a regular file. */
1062 static int wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
1064 const char *stream_name;
1065 if ((mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
1066 && (stream_name = path_stream_name(path))) {
1067 /* Make an alternate data stream */
1068 struct ads_entry *new_entry;
1069 struct dentry *dentry;
1071 char *p = (char*)stream_name - 1;
1072 wimlib_assert(*p == ':');
1075 dentry = get_dentry(w, path);
1076 if (!dentry || !dentry_is_regular_file(dentry))
1078 if (inode_get_ads_entry(dentry->d_inode, stream_name, NULL))
1080 new_entry = inode_add_ads(dentry->d_inode, stream_name);
1084 struct dentry *dentry, *parent;
1085 const char *basename;
1087 /* Make a normal file (not an alternate data stream) */
1089 /* Make sure that the parent of @path exists and is a directory, and
1090 * that the dentry named by @path does not already exist. */
1091 parent = get_parent_dentry(w, path);
1094 if (!dentry_is_directory(parent))
1097 basename = path_basename(path);
1098 if (get_dentry_child_with_name(parent, path))
1101 dentry = new_dentry_with_inode(basename);
1104 dentry->d_inode->resolved = true;
1105 dentry->d_inode->ino = next_ino++;
1106 link_dentry(dentry, parent);
1113 static int wimfs_open(const char *path, struct fuse_file_info *fi)
1115 struct dentry *dentry;
1116 struct lookup_table_entry *lte;
1118 struct wimlib_fd *fd;
1119 struct inode *inode;
1123 ret = lookup_resource(w, path, get_lookup_flags(), &dentry, <e,
1128 inode = dentry->d_inode;
1130 if (stream_idx == 0)
1133 stream_id = inode->ads_entries[stream_idx - 1].stream_id;
1135 /* The file resource may be in the staging directory (read-write mounts
1136 * only) or in the WIM. If it's in the staging directory, we need to
1137 * open a native file descriptor for the corresponding file. Otherwise,
1138 * we can read the file resource directly from the WIM file if we are
1139 * opening it read-only, but we need to extract the resource to the
1140 * staging directory if we are opening it writable. */
1142 if (flags_writable(fi->flags) &&
1143 (!lte || lte->resource_location != RESOURCE_IN_STAGING_FILE)) {
1144 u64 size = (lte) ? wim_resource_size(lte) : 0;
1145 ret = extract_resource_to_staging_dir(inode, stream_id,
1151 ret = alloc_wimlib_fd(inode, stream_id, lte, &fd);
1155 if (lte && lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1156 fd->staging_fd = open(lte->staging_file_name, fi->flags);
1157 if (fd->staging_fd == -1) {
1158 close_wimlib_fd(fd);
1162 fi->fh = (uintptr_t)fd;
1166 /* Opens a directory. */
1167 static int wimfs_opendir(const char *path, struct fuse_file_info *fi)
1169 struct inode *inode;
1171 struct wimlib_fd *fd = NULL;
1173 inode = wim_pathname_to_inode(w, path);
1176 if (!inode_is_directory(inode))
1178 ret = alloc_wimlib_fd(inode, 0, NULL, &fd);
1179 fi->fh = (uintptr_t)fd;
1185 * Read data from a file in the WIM or in the staging directory.
1187 static int wimfs_read(const char *path, char *buf, size_t size,
1188 off_t offset, struct fuse_file_info *fi)
1190 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1195 if (!fd->f_lte) /* Empty stream with no lookup table entry */
1198 if (fd->f_lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1199 /* Read from staging file */
1201 wimlib_assert(fd->f_lte->staging_file_name);
1202 wimlib_assert(fd->staging_fd != -1);
1205 DEBUG("Seek to offset %zu", offset);
1207 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1209 ret = read(fd->staging_fd, buf, size);
1216 u64 res_size = wim_resource_size(fd->f_lte);
1218 if (offset > res_size)
1221 size = min(size, res_size - offset);
1223 if (read_wim_resource(fd->f_lte, (u8*)buf,
1224 size, offset, false) != 0)
1230 /* Fills in the entries of the directory specified by @path using the
1231 * FUSE-provided function @filler. */
1232 static int wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
1233 off_t offset, struct fuse_file_info *fi)
1235 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1236 struct inode *inode;
1237 struct dentry *child;
1242 inode = fd->f_inode;
1244 filler(buf, ".", NULL, 0);
1245 filler(buf, "..", NULL, 0);
1247 child = inode->children;
1253 if (filler(buf, child->file_name_utf8, NULL, 0))
1255 child = child->next;
1256 } while (child != inode->children);
1261 static int wimfs_readlink(const char *path, char *buf, size_t buf_len)
1263 struct dentry *dentry = get_dentry(w, path);
1267 if (!dentry_is_symlink(dentry))
1270 ret = inode_readlink(dentry->d_inode, buf, buf_len, w);
1277 static int wimfs_release(const char *path, struct fuse_file_info *fi)
1279 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1280 return close_wimlib_fd(fd);
1283 /* Close a directory */
1284 static int wimfs_releasedir(const char *path, struct fuse_file_info *fi)
1286 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1287 return close_wimlib_fd(fd);
1291 /* Remove an alternate data stream through the XATTR interface */
1292 static int wimfs_removexattr(const char *path, const char *name)
1294 struct dentry *dentry;
1295 struct ads_entry *ads_entry;
1297 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1300 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1304 dentry = get_dentry(w, path);
1308 ads_entry = inode_get_ads_entry(dentry->d_inode, name, &ads_idx);
1311 inode_remove_ads(dentry->d_inode, ads_idx, w->lookup_table);
1316 /* Renames a file or directory. See rename (3) */
1317 static int wimfs_rename(const char *from, const char *to)
1321 struct dentry *parent_of_dst;
1322 char *file_name_utf16 = NULL, *file_name_utf8 = NULL;
1323 u16 file_name_utf16_len, file_name_utf8_len;
1326 /* This rename() implementation currently only supports actual files
1327 * (not alternate data streams) */
1329 src = get_dentry(w, from);
1333 dst = get_dentry(w, to);
1336 ret = get_names(&file_name_utf16, &file_name_utf8,
1337 &file_name_utf16_len, &file_name_utf8_len,
1343 /* Destination file exists */
1345 if (src == dst) /* Same file */
1348 if (!dentry_is_directory(src)) {
1349 /* Cannot rename non-directory to directory. */
1350 if (dentry_is_directory(dst))
1353 /* Cannot rename directory to a non-directory or a non-empty
1355 if (!dentry_is_directory(dst))
1357 if (dst->d_inode->children != NULL)
1360 parent_of_dst = dst->parent;
1361 remove_dentry(dst, w->lookup_table);
1363 /* Destination does not exist */
1364 parent_of_dst = get_parent_dentry(w, to);
1368 if (!dentry_is_directory(parent_of_dst))
1372 FREE(src->file_name);
1373 FREE(src->file_name_utf8);
1374 src->file_name = file_name_utf16;
1375 src->file_name_utf8 = file_name_utf8;
1376 src->file_name_len = file_name_utf16_len;
1377 src->file_name_utf8_len = file_name_utf8_len;
1380 link_dentry(src, parent_of_dst);
1384 /* Remove a directory */
1385 static int wimfs_rmdir(const char *path)
1387 struct dentry *dentry;
1389 dentry = get_dentry(w, path);
1393 if (!dentry_is_empty_directory(dentry))
1396 remove_dentry(dentry, w->lookup_table);
1401 /* Write an alternate data stream through the XATTR interface */
1402 static int wimfs_setxattr(const char *path, const char *name,
1403 const char *value, size_t size, int flags)
1405 struct ads_entry *existing_ads_entry;
1406 struct ads_entry *new_ads_entry;
1407 struct lookup_table_entry *existing_lte;
1408 struct lookup_table_entry *lte;
1409 struct inode *inode;
1410 u8 value_hash[SHA1_HASH_SIZE];
1413 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1416 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1420 inode = wim_pathname_to_inode(w, path);
1424 existing_ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
1425 if (existing_ads_entry) {
1426 if (flags & XATTR_CREATE)
1428 inode_remove_ads(inode, ads_idx, w->lookup_table);
1430 if (flags & XATTR_REPLACE)
1433 new_ads_entry = inode_add_ads(inode, name);
1437 sha1_buffer((const u8*)value, size, value_hash);
1439 existing_lte = __lookup_resource(w->lookup_table, value_hash);
1446 lte = new_lookup_table_entry();
1449 value_copy = MALLOC(size);
1454 memcpy(value_copy, value, size);
1455 lte->resource_location = RESOURCE_IN_ATTACHED_BUFFER;
1456 lte->attached_buffer = value_copy;
1457 lte->resource_entry.original_size = size;
1458 lte->resource_entry.size = size;
1459 lte->resource_entry.flags = 0;
1460 copy_hash(lte->hash, value_hash);
1461 lookup_table_insert(w->lookup_table, lte);
1463 new_ads_entry->lte = lte;
1468 static int wimfs_symlink(const char *to, const char *from)
1470 struct dentry *dentry_parent, *dentry;
1471 const char *link_name;
1472 struct inode *inode;
1474 dentry_parent = get_parent_dentry(w, from);
1477 if (!dentry_is_directory(dentry_parent))
1480 link_name = path_basename(from);
1482 if (get_dentry_child_with_name(dentry_parent, link_name))
1484 dentry = new_dentry_with_inode(link_name);
1487 inode = dentry->d_inode;
1489 inode->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
1490 inode->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
1491 inode->ino = next_ino++;
1492 inode->resolved = true;
1494 if (inode_set_symlink(inode, to, w->lookup_table, NULL) != 0)
1495 goto out_free_dentry;
1497 link_dentry(dentry, dentry_parent);
1500 free_dentry(dentry);
1505 /* Reduce the size of a file */
1506 static int wimfs_truncate(const char *path, off_t size)
1508 struct dentry *dentry;
1509 struct lookup_table_entry *lte;
1513 struct inode *inode;
1515 ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1521 if (!lte) /* Already a zero-length file */
1524 inode = dentry->d_inode;
1526 if (stream_idx == 0)
1529 stream_id = inode->ads_entries[stream_idx - 1].stream_id;
1531 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1532 wimlib_assert(lte->staging_file_name);
1533 ret = truncate(lte->staging_file_name, size);
1536 lte->resource_entry.original_size = size;
1538 wimlib_assert(lte->resource_location == RESOURCE_IN_WIM);
1539 /* File in WIM. Extract it to the staging directory, but only
1540 * the first @size bytes of it. */
1541 ret = extract_resource_to_staging_dir(inode, stream_id,
1547 /* Remove a regular file */
1548 static int wimfs_unlink(const char *path)
1550 struct dentry *dentry;
1551 struct lookup_table_entry *lte;
1552 struct inode *inode;
1557 ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1563 if (stream_idx == 0)
1564 remove_dentry(dentry, w->lookup_table);
1566 inode_remove_ads(dentry->d_inode, stream_idx - 1, w->lookup_table);
1570 #ifdef HAVE_UTIMENSAT
1572 * Change the timestamp on a file dentry.
1574 * Note that alternate data streams do not have their own timestamps.
1576 static int wimfs_utimens(const char *path, const struct timespec tv[2])
1578 struct dentry *dentry;
1579 struct inode *inode;
1580 dentry = get_dentry(w, path);
1583 inode = dentry->d_inode;
1585 if (tv[0].tv_nsec != UTIME_OMIT) {
1586 if (tv[0].tv_nsec == UTIME_NOW)
1587 inode->last_access_time = get_wim_timestamp();
1589 inode->last_access_time = timespec_to_wim_timestamp(&tv[0]);
1591 if (tv[1].tv_nsec != UTIME_OMIT) {
1592 if (tv[1].tv_nsec == UTIME_NOW)
1593 inode->last_write_time = get_wim_timestamp();
1595 inode->last_write_time = timespec_to_wim_timestamp(&tv[1]);
1600 static int wimfs_utime(const char *path, struct utimbuf *times)
1602 struct dentry *dentry;
1603 struct inode *inode;
1604 dentry = get_dentry(w, path);
1607 inode = dentry->d_inode;
1609 inode->last_write_time = unix_timestamp_to_wim(times->modtime);
1610 inode->last_access_time = unix_timestamp_to_wim(times->actime);
1615 /* Writes to a file in the WIM filesystem.
1616 * It may be an alternate data stream, but here we don't even notice because we
1617 * just get a lookup table entry. */
1618 static int wimfs_write(const char *path, const char *buf, size_t size,
1619 off_t offset, struct fuse_file_info *fi)
1621 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1628 wimlib_assert(fd->f_lte);
1629 wimlib_assert(fd->f_lte->staging_file_name);
1630 wimlib_assert(fd->staging_fd != -1);
1631 wimlib_assert(fd->f_inode);
1633 /* Seek to the requested position */
1634 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1637 /* Write the data. */
1638 ret = write(fd->staging_fd, buf, size);
1642 now = get_wim_timestamp();
1643 fd->f_inode->last_write_time = now;
1644 fd->f_inode->last_access_time = now;
1648 static struct fuse_operations wimfs_operations = {
1649 .access = wimfs_access,
1650 .destroy = wimfs_destroy,
1652 .fallocate = wimfs_fallocate,
1654 .fgetattr = wimfs_fgetattr,
1655 .ftruncate = wimfs_ftruncate,
1656 .getattr = wimfs_getattr,
1658 .getxattr = wimfs_getxattr,
1662 .listxattr = wimfs_listxattr,
1664 .mkdir = wimfs_mkdir,
1665 .mknod = wimfs_mknod,
1667 .opendir = wimfs_opendir,
1669 .readdir = wimfs_readdir,
1670 .readlink = wimfs_readlink,
1671 .release = wimfs_release,
1672 .releasedir = wimfs_releasedir,
1674 .removexattr = wimfs_removexattr,
1676 .rename = wimfs_rename,
1677 .rmdir = wimfs_rmdir,
1679 .setxattr = wimfs_setxattr,
1681 .symlink = wimfs_symlink,
1682 .truncate = wimfs_truncate,
1683 .unlink = wimfs_unlink,
1684 #ifdef HAVE_UTIMENSAT
1685 .utimens = wimfs_utimens,
1687 .utime = wimfs_utime,
1689 .write = wimfs_write,
1693 /* Mounts a WIM file. */
1694 WIMLIBAPI int wimlib_mount(WIMStruct *wim, int image, const char *dir,
1695 int flags, WIMStruct **additional_swms,
1696 unsigned num_additional_swms)
1702 struct lookup_table *joined_tab, *wim_tab_save;
1703 struct image_metadata *imd;
1705 DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
1706 wim, image, dir, flags);
1709 return WIMLIB_ERR_INVALID_PARAM;
1711 ret = verify_swm_set(wim, additional_swms, num_additional_swms);
1715 if (num_additional_swms) {
1716 ret = new_joined_lookup_table(wim, additional_swms,
1717 num_additional_swms,
1721 wim_tab_save = wim->lookup_table;
1722 wim->lookup_table = joined_tab;
1725 ret = wimlib_select_image(wim, image);
1730 imd = &wim->image_metadata[image - 1];
1732 DEBUG("Selected image %d", image);
1734 next_ino = assign_inode_numbers(&imd->inode_list);
1736 DEBUG("(next_ino = %"PRIu64")", next_ino);
1738 /* Resolve all the lookup table entries of the dentry tree */
1739 DEBUG("Resolving lookup table entries");
1740 for_dentry_in_tree(imd->root_dentry, dentry_resolve_ltes,
1743 if (flags & WIMLIB_MOUNT_FLAG_READWRITE)
1744 imd->modified = true;
1746 if (!(flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
1747 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
1748 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
1749 flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1751 DEBUG("Getting current directory");
1754 working_directory = getcwd(NULL, 0);
1755 if (!working_directory) {
1756 ERROR_WITH_ERRNO("Could not determine current directory");
1757 ret = WIMLIB_ERR_NOTDIR;
1761 DEBUG("Closing POSIX message queues");
1762 /* XXX hack to get rid of the message queues if they already exist for
1763 * some reason (maybe left over from a previous mount that wasn't
1764 * unmounted correctly) */
1765 ret = open_message_queues(true);
1768 close_message_queues();
1770 DEBUG("Preparing arguments to fuse_main()");
1775 ret = WIMLIB_ERR_NOMEM;
1779 argv[argc++] = "imagex";
1781 argv[argc++] = "-s"; /* disable multi-threaded operation */
1783 if (flags & WIMLIB_MOUNT_FLAG_DEBUG)
1784 argv[argc++] = "-d";
1787 * We provide the use_ino option because we are going to assign inode
1788 * numbers oursides. We've already numbered the inodes with unique
1789 * numbers in the assign_inode_numbers() function, and the static
1790 * variable next_ino is set to the next available inode number.
1792 char optstring[256] = "use_ino,subtype=wimfs,attr_timeout=0";
1793 argv[argc++] = "-o";
1794 argv[argc++] = optstring;
1795 if ((flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
1796 /* Read-write mount. Make the staging directory */
1798 if (!staging_dir_name) {
1800 ret = WIMLIB_ERR_MKDIR;
1804 /* Read-only mount */
1805 strcat(optstring, ",ro");
1812 DEBUG("FUSE command line (argc = %d): ", argc);
1813 for (i = 0; i < argc; i++) {
1814 fputs(argv[i], stdout);
1822 /* Set static variables. */
1824 mount_flags = flags;
1826 ret = fuse_main(argc, argv, &wimfs_operations, NULL);
1828 ret = WIMLIB_ERR_FUSE;
1830 if (num_additional_swms) {
1831 free_lookup_table(wim->lookup_table);
1832 wim->lookup_table = wim_tab_save;
1839 * Unmounts the WIM file that was previously mounted on @dir by using
1842 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
1849 struct timespec timeout;
1855 /* Open message queues between the unmount process and the
1856 * filesystem daemon. */
1857 ret = open_message_queues(false);
1861 /* Send a message to the filesystem saying whether to commit or
1863 msg[0] = (flags & WIMLIB_UNMOUNT_FLAG_COMMIT) ? 1 : 0;
1864 msg[1] = (flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY) ? 1 : 0;
1866 DEBUG("Sending message: %s, %s",
1867 (msg[0] == 0) ? "don't commit" : "commit",
1868 (msg[1] == 0) ? "don't check" : "check");
1869 ret = mq_send(unmount_to_daemon_mq, msg, 2, 1);
1871 ERROR("Failed to notify filesystem daemon whether we want to "
1872 "commit changes or not");
1873 close_message_queues();
1874 return WIMLIB_ERR_MQUEUE;
1877 /* Execute `fusermount -u', which is installed setuid root, to unmount
1880 * FUSE does not yet implement synchronous unmounts. This means that
1881 * fusermount -u will return before the filesystem daemon returns from
1882 * wimfs_destroy(). This is partly what we want, because we need to
1883 * send a message from this process to the filesystem daemon telling
1884 * whether --commit was specified or not. However, after that, the
1885 * unmount process must wait for the filesystem daemon to finish writing
1892 ERROR_WITH_ERRNO("Failed to fork()");
1893 return WIMLIB_ERR_FORK;
1896 execlp("fusermount", "fusermount", "-u", dir, NULL);
1897 ERROR_WITH_ERRNO("Failed to execute `fusermount'");
1898 exit(WIMLIB_ERR_FUSERMOUNT);
1901 ret = wait(&status);
1903 ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
1905 return WIMLIB_ERR_FUSERMOUNT;
1909 ERROR("fusermount exited with status %d", status);
1911 /* Try again, but with the `umount' program. This is required
1912 * on other FUSE implementations such as FreeBSD's that do not
1913 * have a `fusermount' program. */
1917 ERROR_WITH_ERRNO("Failed to fork()");
1918 return WIMLIB_ERR_FORK;
1921 execlp("umount", "umount", dir, NULL);
1922 ERROR_WITH_ERRNO("Failed to execute `umount'");
1923 exit(WIMLIB_ERR_FUSERMOUNT);
1926 ret = wait(&status);
1928 ERROR_WITH_ERRNO("Failed to wait for `umount' process to "
1930 return WIMLIB_ERR_FUSERMOUNT;
1933 ERROR("`umount' exited with failure status");
1934 return WIMLIB_ERR_FUSERMOUNT;
1939 /* Wait for a message from the filesytem daemon indicating whether the
1940 * filesystem was unmounted successfully (0) or an error occurred (1).
1941 * This may take a long time if a big WIM file needs to be rewritten. */
1943 /* Wait at most 600??? seconds before giving up and returning false.
1944 * Either it's a really big WIM file, or (more likely) the
1945 * filesystem daemon has crashed or failed for some reason.
1947 * XXX come up with some method to determine if the filesystem
1948 * daemon has really crashed or not.
1950 * XXX Idea: have mount daemon write its PID into the WIM file header?
1953 gettimeofday(&now, NULL);
1954 timeout.tv_sec = now.tv_sec + 600;
1955 timeout.tv_nsec = now.tv_usec * 1000;
1957 msgsize = mq_get_msgsize(daemon_to_unmount_mq);
1958 char mailbox[msgsize];
1961 DEBUG("Waiting for message telling us whether the unmount was "
1962 "successful or not.");
1963 ret = mq_timedreceive(daemon_to_unmount_mq, mailbox, msgsize,
1966 close_message_queues();
1968 if (errno_save == ETIMEDOUT) {
1969 ERROR("Timed out- probably the filesystem daemon "
1970 "crashed and the WIM was not written "
1972 return WIMLIB_ERR_TIMEOUT;
1974 ERROR("mq_receive(): %s", strerror(errno_save));
1975 return WIMLIB_ERR_MQUEUE;
1979 DEBUG("Received message: %s",
1980 (mailbox[0] == 0) ? "Unmount OK" : "Unmount Failed");
1981 if (mailbox[0] != 0)
1982 ERROR("Unmount failed");
1986 #else /* WITH_FUSE */
1989 static inline int mount_unsupported_error()
1991 ERROR("WIMLIB was compiled with --without-fuse, which disables support "
1992 "for mounting WIMs.");
1993 return WIMLIB_ERR_UNSUPPORTED;
1996 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
1998 return mount_unsupported_error();
2001 WIMLIBAPI int wimlib_mount(WIMStruct *wim_p, int image, const char *dir,
2002 int flags, WIMStruct **additional_swms,
2003 unsigned num_additional_swms)
2005 return mount_unsupported_error();
2008 #endif /* WITH_FUSE */