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. */
190 if (lte->resource_location == RESOURCE_IN_STAGING_FILE
191 && fd->staging_fd != -1)
193 if (close(fd->staging_fd) != 0) {
194 ERROR_WITH_ERRNO("Failed to close staging file");
198 lte_decrement_num_opened_fds(lte, w->lookup_table);
202 /* Close a file descriptor. */
203 static int close_wimlib_fd(struct wimlib_fd *fd)
207 DEBUG("Closing fd (inode = %lu, opened = %u, allocated = %u)",
208 fd->f_inode->ino, fd->f_inode->num_opened_fds,
209 fd->f_inode->num_allocated_fds);
210 ret = lte_put_fd(fd->f_lte, fd);
214 inode_put_fd(fd->f_inode, fd);
218 /* Remove a dentry; i.e. remove a reference to the corresponding inode.
220 * If there are no remaining references to the inode either through dentries or
221 * open file descriptors, the inode is freed. Otherwise, the inode is not
222 * removed, but the dentry is unlinked and freed.
224 * Either way, all lookup table entries referenced by the inode have their
225 * reference count decremented. If a lookup table entry has no open file
226 * descriptors and no references remaining, it is freed, and the staging file is
229 static void remove_dentry(struct dentry *dentry,
230 struct lookup_table *lookup_table)
232 struct inode *inode = dentry->d_inode;
233 struct lookup_table_entry *lte;
236 for (i = 0; i <= inode->num_ads; i++) {
237 lte = inode_stream_lte_resolved(inode, i);
239 lte_decrement_refcnt(lte, lookup_table);
241 unlink_dentry(dentry);
245 /* Creates a new staging file and returns its file descriptor opened for
248 * @name_ret: A location into which the a pointer to the newly allocated name of
249 * the staging file is stored.
250 * @return: The file descriptor for the new file. Returns -1 and sets errno on
251 * error, for any reason possible from the creat() function.
253 static int create_staging_file(char **name_ret, int open_flags)
261 name_len = staging_dir_name_len + 1 + SHA1_HASH_SIZE;
262 name = MALLOC(name_len + 1);
270 memcpy(name, staging_dir_name, staging_dir_name_len);
271 name[staging_dir_name_len] = '/';
272 randomize_char_array_with_alnum(name + staging_dir_name_len + 1,
274 name[name_len] = '\0';
277 /* Just in case, verify that the randomly generated name doesn't name an
278 * existing file, and try again if so */
279 } while (stat(name, &stbuf) == 0);
285 /* doesn't exist--- ok */
287 DEBUG("Creating staging file `%s'", name);
289 fd = open(name, open_flags | O_CREAT | O_TRUNC, 0600);
301 * Extract a WIM resource to the staging directory.
303 * @inode: Inode that contains the stream we are extracting
305 * @stream_id: Identifier for the stream (it stays constant even if the indices
306 * of the stream entries are changed)
308 * @lte: Pointer to pointer to the lookup table entry for the stream we need to
309 * extract, or NULL if there was no lookup table entry present for the stream
311 * @size: Number of bytes of the stream we want to extract (this supports the
312 * wimfs_truncate() function).
314 static int extract_resource_to_staging_dir(struct inode *inode,
316 struct lookup_table_entry **lte,
319 char *staging_file_name;
322 struct lookup_table_entry *old_lte, *new_lte;
324 DEBUG("Extracting resource to staging dir: inode %"PRIu64", "
325 "stream id %"PRIu32, inode->ino, stream_id);
328 fd = create_staging_file(&staging_file_name, O_WRONLY);
333 ret = extract_wim_resource_to_fd(old_lte, fd, size);
336 if (ret != 0 || close(fd) != 0) {
342 goto out_delete_staging_file;
345 if (old_lte && inode->link_count == old_lte->refcnt) {
346 /* The reference count of the existing lookup table
347 * entry is the same as the link count of the inode that
348 * contains the stream we're opening. Therefore, ALL
349 * the references to the lookup table entry correspond
350 * to the stream we're trying to extract, so the lookup
351 * table entry can be re-used. */
352 DEBUG("Re-using lookup table entry");
353 lookup_table_unlink(w->lookup_table, old_lte);
357 /* There's an existing lookup table entry, but its
358 * reference count is creater than the link count for
359 * the inode containing a stream we're opening.
360 * Therefore, we need to split the lookup table entry.
362 wimlib_assert(old_lte->refcnt > inode->link_count);
363 DEBUG("Splitting lookup table entry "
364 "(inode->link_count = %zu, old_lte->refcnt = %u)",
365 inode->link_count, old_lte->refcnt);
369 new_lte = new_lookup_table_entry();
372 goto out_delete_staging_file;
375 /* There may already be open file descriptors to this stream if
376 * it's previously been opened read-only, but just now we're
377 * opening it read-write. Identify those file descriptors and
378 * change their lookup table entry pointers to point to the new
379 * lookup table entry, and open staging file descriptors for
382 * At the same time, we need to count the number of these opened
383 * file descriptors to the new lookup table entry. If there's
384 * an old lookup table entry, this number needs to be subtracted
385 * from the fd's opened to the old entry. */
386 for (u16 i = 0, j = 0; j < inode->num_opened_fds; i++) {
387 struct wimlib_fd *fd = inode->fds[i];
389 if (fd->stream_id == stream_id) {
390 wimlib_assert(fd->f_lte == old_lte);
391 wimlib_assert(fd->staging_fd == -1);
393 new_lte->num_opened_fds++;
394 fd->staging_fd = open(staging_file_name, O_RDONLY);
395 if (fd->staging_fd == -1) {
397 goto out_revert_fd_changes;
403 DEBUG("%zu fd's were already opened to the file we extracted",
404 new_lte->num_opened_fds);
406 old_lte->num_opened_fds -= new_lte->num_opened_fds;
407 old_lte->refcnt -= inode->link_count;
411 new_lte->resource_entry.original_size = size;
412 new_lte->refcnt = inode->link_count;
413 new_lte->resource_location = RESOURCE_IN_STAGING_FILE;
414 new_lte->staging_file_name = staging_file_name;
415 new_lte->lte_inode = inode;
416 random_hash(new_lte->hash);
419 inode->lte = new_lte;
421 for (u16 i = 0; i < inode->num_ads; i++)
422 if (inode->ads_entries[i].stream_id == stream_id)
423 inode->ads_entries[i].lte = new_lte;
425 lookup_table_insert(w->lookup_table, new_lte);
426 list_add(&new_lte->staging_list, &staging_list);
429 out_revert_fd_changes:
430 for (u16 i = 0, j = 0; j < new_lte->num_opened_fds; i++) {
431 struct wimlib_fd *fd = inode->fds[i];
432 if (fd && fd->stream_id == stream_id && fd->f_lte == new_lte) {
434 if (fd->staging_fd != -1) {
435 close(fd->staging_fd);
442 free_lookup_table_entry(new_lte);
443 out_delete_staging_file:
444 unlink(staging_file_name);
445 FREE(staging_file_name);
450 * Creates a randomly named staging directory and returns its name into the
451 * static variable staging_dir_name.
453 * If the staging directory cannot be created, staging_dir_name is set to NULL.
455 static void make_staging_dir()
457 /* XXX Give the user an option of where to stage files */
459 static char prefix[] = "wimlib-staging-";
460 static const size_t prefix_len = 15;
461 static const size_t suffix_len = 10;
463 size_t pwd_len = strlen(working_directory);
465 staging_dir_name_len = pwd_len + 1 + prefix_len + suffix_len;
467 staging_dir_name = MALLOC(staging_dir_name_len + 1);
468 if (!staging_dir_name) {
469 ERROR("Out of memory");
473 memcpy(staging_dir_name, working_directory, pwd_len);
474 staging_dir_name[pwd_len] = '/';
475 memcpy(staging_dir_name + pwd_len + 1, prefix, prefix_len);
476 randomize_char_array_with_alnum(staging_dir_name + pwd_len + 1 + prefix_len,
478 staging_dir_name[staging_dir_name_len] = '\0';
480 if (mkdir(staging_dir_name, 0700) != 0) {
481 ERROR_WITH_ERRNO("Failed to create temporary directory `%s'",
483 FREE(staging_dir_name);
484 staging_dir_name = NULL;
488 static int remove_file_or_directory(const char *fpath, const struct stat *sb,
489 int typeflag, struct FTW *ftwbuf)
491 if (remove(fpath) == 0)
494 return WIMLIB_ERR_DELETE_STAGING_DIR;
499 * Deletes the staging directory and all the files contained in it.
501 static int delete_staging_dir()
505 ret = nftw(staging_dir_name, remove_file_or_directory,10, FTW_DEPTH);
506 staging_dir_name = NULL;
510 /* Name and message queue descriptors for message queues between the filesystem
511 * daemon process and the unmount process. These are used when the filesystem
512 * is unmounted and the process running wimlib_mount() (i.e. the `imagex
513 * unmount' command) needs to communicate with the filesystem daemon running
514 * fuse_main() (i.e. that spawned by the `imagex mount' or `imagex mountrw'
516 static char *unmount_to_daemon_mq_name;
517 static char *daemon_to_unmount_mq_name;
518 static mqd_t unmount_to_daemon_mq;
519 static mqd_t daemon_to_unmount_mq;
521 /* Simple function that returns the concatenation of 4 strings. */
522 static char *strcat_dup(const char *s1, const char *s2, const char *s3,
525 size_t len = strlen(s1) + strlen(s2) + strlen(s3) + strlen(s4) + 1;
526 char *p = MALLOC(len);
532 return strcat(p, s4);
535 /* Removes trailing forward slashes in a string. */
536 static void remove_trailing_slashes(char *s)
538 long len = strlen(s);
539 for (long i = len - 1; i >= 1; i--) {
547 /* Changes forward slashes to underscores in a string. */
548 static void s_slashes_underscores_g(char *s)
558 * Opens two POSIX message queue: one for sending messages from the unmount
559 * process to the daemon process, and one to go the other way. The names of the
560 * message queues, which must be system-wide unique, are be based on the mount
561 * point. (There of course is still a possibility of a collision if one were to
562 * unmount two identically named directories simultaneously...)
564 * @daemon specifies whether the calling process is the filesystem daemon or the
567 static int open_message_queues(bool daemon)
569 static const char *slash = "/";
570 static const char *prefix = "wimlib-";
571 static const char *u2d_suffix = "unmount-to-daemon-mq";
572 static const char *d2u_suffix = "daemon-to-unmount-mq";
574 const char *mount_dir_basename = path_basename(mount_dir);
578 unmount_to_daemon_mq_name = strcat_dup(slash, mount_dir_basename,
580 if (!unmount_to_daemon_mq_name) {
581 ERROR("Out of memory");
582 return WIMLIB_ERR_NOMEM;
584 daemon_to_unmount_mq_name = strcat_dup(slash, mount_dir_basename,
586 if (!daemon_to_unmount_mq_name) {
587 ERROR("Out of memory");
588 ret = WIMLIB_ERR_NOMEM;
592 remove_trailing_slashes(unmount_to_daemon_mq_name);
593 remove_trailing_slashes(daemon_to_unmount_mq_name);
594 s_slashes_underscores_g(unmount_to_daemon_mq_name + 1);
595 s_slashes_underscores_g(daemon_to_unmount_mq_name + 1);
598 flags = O_RDONLY | O_CREAT;
600 flags = O_WRONLY | O_CREAT;
602 unmount_to_daemon_mq = mq_open(unmount_to_daemon_mq_name, flags,
605 if (unmount_to_daemon_mq == (mqd_t)-1) {
606 ERROR_WITH_ERRNO("mq_open()");
607 ret = WIMLIB_ERR_MQUEUE;
612 flags = O_WRONLY | O_CREAT;
614 flags = O_RDONLY | O_CREAT;
616 daemon_to_unmount_mq = mq_open(daemon_to_unmount_mq_name, flags,
619 if (daemon_to_unmount_mq == (mqd_t)-1) {
620 ERROR_WITH_ERRNO("mq_open()");
621 ret = WIMLIB_ERR_MQUEUE;
626 mq_close(unmount_to_daemon_mq);
627 mq_unlink(unmount_to_daemon_mq_name);
629 FREE(daemon_to_unmount_mq_name);
631 FREE(unmount_to_daemon_mq_name);
635 static int mq_get_msgsize(mqd_t mq)
637 static const char *msgsize_max_file = "/proc/sys/fs/mqueue/msgsize_max";
642 if (mq_getattr(unmount_to_daemon_mq, &attr) == 0) {
643 msgsize = attr.mq_msgsize;
645 ERROR_WITH_ERRNO("mq_getattr()");
646 ERROR("Attempting to read %s", msgsize_max_file);
647 fp = fopen(msgsize_max_file, "rb");
649 if (fscanf(fp, "%d", &msgsize) != 1) {
650 ERROR("Assuming message size of 8192");
655 ERROR_WITH_ERRNO("Failed to open the file `%s'",
657 ERROR("Assuming message size of 8192");
664 /* Closes the message queues, which are allocated in static variables */
665 static void close_message_queues()
667 mq_close(unmount_to_daemon_mq);
668 mq_close(daemon_to_unmount_mq);
669 mq_unlink(unmount_to_daemon_mq_name);
670 mq_unlink(daemon_to_unmount_mq_name);
673 static int wimfs_access(const char *path, int mask)
675 /* Permissions not implemented */
679 static void inode_update_lte_ptr(struct inode *inode,
680 struct lookup_table_entry *old_lte,
681 struct lookup_table_entry *new_lte)
683 if (inode->lte == old_lte) {
684 inode->lte = new_lte;
686 for (unsigned i = 0; i < inode->num_ads; i++) {
687 if (inode->ads_entries[i].lte == old_lte) {
688 inode->ads_entries[i].lte = new_lte;
695 static int update_lte_of_staging_file(struct lookup_table_entry *lte,
696 struct lookup_table *table)
698 struct lookup_table_entry *duplicate_lte;
700 u8 hash[SHA1_HASH_SIZE];
703 wimlib_assert(lte->resource_location == RESOURCE_IN_STAGING_FILE);
704 wimlib_assert(lte->staging_file_name);
706 ret = sha1sum(lte->staging_file_name, hash);
710 lookup_table_unlink(table, lte);
712 duplicate_lte = __lookup_resource(table, hash);
715 /* Merge duplicate lookup table entries */
716 duplicate_lte->refcnt += lte->refcnt;
717 list_del(<e->staging_list);
718 inode_update_lte_ptr(lte->lte_inode, lte, duplicate_lte);
719 free_lookup_table_entry(lte);
721 if (stat(lte->staging_file_name, &stbuf) != 0) {
722 ERROR_WITH_ERRNO("Failed to stat `%s'", lte->staging_file_name);
723 return WIMLIB_ERR_STAT;
725 if (stbuf.st_size == 0) {
726 /* Zero-length stream. No lookup table entry needed. */
727 inode_update_lte_ptr(lte->lte_inode, lte, NULL);
728 free_lookup_table_entry(lte);
730 wimlib_assert(<e->file_on_disk == <e->staging_file_name);
731 lte->resource_entry.original_size = stbuf.st_size;
732 lte->resource_entry.size = stbuf.st_size;
733 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
734 lte->lte_inode = NULL;
735 copy_hash(lte->hash, hash);
736 lookup_table_insert(table, lte);
742 static int inode_close_fds(struct inode *inode)
744 u16 num_opened_fds = inode->num_opened_fds;
745 for (u16 i = 0, j = 0; j < num_opened_fds; i++) {
746 struct wimlib_fd *fd = inode->fds[i];
748 wimlib_assert(fd->f_inode == inode);
749 int ret = close_wimlib_fd(fd);
758 /* Overwrites the WIM file, with changes saved. */
759 static int rebuild_wim(WIMStruct *w, bool check_integrity)
762 struct lookup_table_entry *lte, *tmp;
765 DEBUG("Closing all staging file descriptors.");
766 list_for_each_entry(lte, &staging_list, staging_list) {
767 ret = inode_close_fds(lte->lte_inode);
772 DEBUG("Calculating SHA1 checksums for all new staging files.");
773 list_for_each_entry_safe(lte, tmp, &staging_list, staging_list) {
774 ret = update_lte_of_staging_file(lte, w->lookup_table);
779 xml_update_image_info(w, w->current_image);
781 ret = wimlib_overwrite(w, check_integrity);
783 ERROR("Failed to commit changes");
789 /* Called when the filesystem is unmounted. */
790 static void wimfs_destroy(void *p)
792 /* For read-write mounts, the `imagex unmount' command, which is
793 * running in a separate process and is executing the
794 * wimlib_unmount() function, will send this process a byte
795 * through a message queue that indicates whether the --commit
796 * option was specified or not. */
799 struct timespec timeout;
801 ssize_t bytes_received;
804 char check_integrity;
807 ret = open_message_queues(true);
811 msgsize = mq_get_msgsize(unmount_to_daemon_mq);
816 /* Wait at most 3 seconds before giving up and discarding changes. */
817 gettimeofday(&now, NULL);
818 timeout.tv_sec = now.tv_sec + 3;
819 timeout.tv_nsec = now.tv_usec * 1000;
820 DEBUG("Waiting for message telling us whether to commit or not, and "
821 "whether to include integrity checks.");
823 bytes_received = mq_timedreceive(unmount_to_daemon_mq, msg,
824 msgsize, NULL, &timeout);
826 check_integrity = msg[1];
827 if (bytes_received == -1) {
828 if (errno == ETIMEDOUT) {
831 ERROR_WITH_ERRNO("mq_timedreceive()");
833 ERROR("Not committing.");
835 DEBUG("Received message: [%d %d]", msg[0], msg[1]);
839 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
841 status = chdir(working_directory);
843 ERROR_WITH_ERRNO("chdir()");
844 status = WIMLIB_ERR_NOTDIR;
847 status = rebuild_wim(w, (check_integrity != 0));
849 ret = delete_staging_dir();
851 ERROR_WITH_ERRNO("Failed to delete the staging "
857 DEBUG("Read-only mount");
860 DEBUG("Sending status %u", status);
861 ret = mq_send(daemon_to_unmount_mq, &status, 1, 1);
863 ERROR_WITH_ERRNO("Failed to send status to unmount process");
864 close_message_queues();
868 static int wimfs_fallocate(const char *path, int mode,
869 off_t offset, off_t len, struct fuse_file_info *fi)
871 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
872 wimlib_assert(fd->staging_fd != -1);
873 return fallocate(fd->staging_fd, mode, offset, len);
878 static int wimfs_fgetattr(const char *path, struct stat *stbuf,
879 struct fuse_file_info *fi)
881 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
882 return inode_to_stbuf(fd->f_inode, fd->f_lte, stbuf);
885 static int wimfs_ftruncate(const char *path, off_t size,
886 struct fuse_file_info *fi)
888 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
889 int ret = ftruncate(fd->staging_fd, size);
892 if (fd->f_lte && size < fd->f_lte->resource_entry.original_size)
893 fd->f_lte->resource_entry.original_size = size;
898 * Fills in a `struct stat' that corresponds to a file or directory in the WIM.
900 static int wimfs_getattr(const char *path, struct stat *stbuf)
902 struct dentry *dentry;
903 struct lookup_table_entry *lte;
906 ret = lookup_resource(w, path,
907 get_lookup_flags() | LOOKUP_FLAG_DIRECTORY_OK,
908 &dentry, <e, NULL);
911 return inode_to_stbuf(dentry->d_inode, lte, stbuf);
915 /* Read an alternate data stream through the XATTR interface, or get its size */
916 static int wimfs_getxattr(const char *path, const char *name, char *value,
921 struct ads_entry *ads_entry;
923 struct lookup_table_entry *lte;
925 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
928 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
932 inode = wim_pathname_to_inode(w, path);
936 ads_entry = inode_get_ads_entry(inode, name, NULL);
940 lte = ads_entry->lte;
941 res_size = wim_resource_size(lte);
949 ret = read_full_wim_resource(lte, (u8*)value);
957 /* Create a hard link */
958 static int wimfs_link(const char *to, const char *from)
960 struct dentry *from_dentry, *from_dentry_parent;
961 const char *link_name;
963 struct lookup_table_entry *lte;
965 inode = wim_pathname_to_inode(w, to);
969 if (!inode_is_regular_file(inode))
972 from_dentry_parent = get_parent_dentry(w, from);
973 if (!from_dentry_parent)
975 if (!dentry_is_directory(from_dentry_parent))
978 link_name = path_basename(from);
979 if (get_dentry_child_with_name(from_dentry_parent, link_name))
981 from_dentry = new_dentry(link_name);
986 inode_add_dentry(from_dentry, inode);
987 from_dentry->d_inode = inode;
990 for (unsigned i = 0; i <= inode->num_ads; i++) {
991 lte = inode_stream_lte_resolved(inode, i);
996 link_dentry(from_dentry, from_dentry_parent);
1001 static int wimfs_listxattr(const char *path, char *list, size_t size)
1006 struct inode *inode;
1008 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1011 /* List alternate data streams, or get the list size */
1013 inode = wim_pathname_to_inode(w, path);
1019 for (i = 0; i < inode->num_ads; i++)
1020 needed_size += inode->ads_entries[i].stream_name_utf8_len + 6;
1024 for (i = 0; i < inode->num_ads; i++) {
1025 needed_size = inode->ads_entries[i].stream_name_utf8_len + 6;
1026 if (needed_size > size)
1028 p += sprintf(p, "user.%s",
1029 inode->ads_entries[i].stream_name_utf8) + 1;
1030 size -= needed_size;
1038 * Create a directory in the WIM.
1039 * @mode is currently ignored.
1041 static int wimfs_mkdir(const char *path, mode_t mode)
1043 struct dentry *parent;
1044 struct dentry *newdir;
1045 const char *basename;
1047 parent = get_parent_dentry(w, path);
1051 if (!dentry_is_directory(parent))
1054 basename = path_basename(path);
1055 if (get_dentry_child_with_name(parent, basename))
1058 newdir = new_dentry_with_inode(basename);
1059 newdir->d_inode->attributes |= FILE_ATTRIBUTE_DIRECTORY;
1060 newdir->d_inode->resolved = true;
1061 newdir->d_inode->ino = next_ino++;
1062 link_dentry(newdir, parent);
1066 /* Creates a regular file. */
1067 static int wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
1069 const char *stream_name;
1070 if ((mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
1071 && (stream_name = path_stream_name(path))) {
1072 /* Make an alternate data stream */
1073 struct ads_entry *new_entry;
1074 struct inode *inode;
1076 char *p = (char*)stream_name - 1;
1077 wimlib_assert(*p == ':');
1080 inode = wim_pathname_to_inode(w, path);
1083 if (!inode_is_regular_file(inode))
1085 if (inode_get_ads_entry(inode, stream_name, NULL))
1087 new_entry = inode_add_ads(inode, stream_name);
1091 struct dentry *dentry, *parent;
1092 const char *basename;
1094 /* Make a normal file (not an alternate data stream) */
1096 /* Make sure that the parent of @path exists and is a directory, and
1097 * that the dentry named by @path does not already exist. */
1098 parent = get_parent_dentry(w, path);
1101 if (!dentry_is_directory(parent))
1104 basename = path_basename(path);
1105 if (get_dentry_child_with_name(parent, path))
1108 dentry = new_dentry_with_inode(basename);
1111 dentry->d_inode->resolved = true;
1112 dentry->d_inode->ino = next_ino++;
1113 link_dentry(dentry, parent);
1120 static int wimfs_open(const char *path, struct fuse_file_info *fi)
1122 struct dentry *dentry;
1123 struct lookup_table_entry *lte;
1125 struct wimlib_fd *fd;
1126 struct inode *inode;
1130 ret = lookup_resource(w, path, get_lookup_flags(), &dentry, <e,
1135 inode = dentry->d_inode;
1137 if (stream_idx == 0)
1140 stream_id = inode->ads_entries[stream_idx - 1].stream_id;
1142 /* The file resource may be in the staging directory (read-write mounts
1143 * only) or in the WIM. If it's in the staging directory, we need to
1144 * open a native file descriptor for the corresponding file. Otherwise,
1145 * we can read the file resource directly from the WIM file if we are
1146 * opening it read-only, but we need to extract the resource to the
1147 * staging directory if we are opening it writable. */
1149 if (flags_writable(fi->flags) &&
1150 (!lte || lte->resource_location != RESOURCE_IN_STAGING_FILE)) {
1151 u64 size = (lte) ? wim_resource_size(lte) : 0;
1152 ret = extract_resource_to_staging_dir(inode, stream_id,
1158 ret = alloc_wimlib_fd(inode, stream_id, lte, &fd);
1162 if (lte && lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1163 fd->staging_fd = open(lte->staging_file_name, fi->flags);
1164 if (fd->staging_fd == -1) {
1165 close_wimlib_fd(fd);
1169 fi->fh = (uintptr_t)fd;
1173 /* Opens a directory. */
1174 static int wimfs_opendir(const char *path, struct fuse_file_info *fi)
1176 struct inode *inode;
1178 struct wimlib_fd *fd = NULL;
1180 inode = wim_pathname_to_inode(w, path);
1183 if (!inode_is_directory(inode))
1185 ret = alloc_wimlib_fd(inode, 0, NULL, &fd);
1186 fi->fh = (uintptr_t)fd;
1192 * Read data from a file in the WIM or in the staging directory.
1194 static int wimfs_read(const char *path, char *buf, size_t size,
1195 off_t offset, struct fuse_file_info *fi)
1197 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1202 if (!fd->f_lte) /* Empty stream with no lookup table entry */
1205 if (fd->f_lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1206 /* Read from staging file */
1208 wimlib_assert(fd->f_lte->staging_file_name);
1209 wimlib_assert(fd->staging_fd != -1);
1212 DEBUG("Seek to offset %zu", offset);
1214 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1216 ret = read(fd->staging_fd, buf, size);
1223 u64 res_size = wim_resource_size(fd->f_lte);
1225 if (offset > res_size)
1228 size = min(size, res_size - offset);
1230 if (read_wim_resource(fd->f_lte, (u8*)buf,
1231 size, offset, false) != 0)
1237 /* Fills in the entries of the directory specified by @path using the
1238 * FUSE-provided function @filler. */
1239 static int wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
1240 off_t offset, struct fuse_file_info *fi)
1242 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1243 struct inode *inode;
1244 struct dentry *child;
1249 inode = fd->f_inode;
1251 filler(buf, ".", NULL, 0);
1252 filler(buf, "..", NULL, 0);
1254 child = inode->children;
1260 if (filler(buf, child->file_name_utf8, NULL, 0))
1262 child = child->next;
1263 } while (child != inode->children);
1268 static int wimfs_readlink(const char *path, char *buf, size_t buf_len)
1270 struct inode *inode = wim_pathname_to_inode(w, path);
1274 if (!inode_is_symlink(inode))
1277 ret = inode_readlink(inode, buf, buf_len, w);
1284 static int wimfs_release(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);
1290 /* Close a directory */
1291 static int wimfs_releasedir(const char *path, struct fuse_file_info *fi)
1293 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1294 return close_wimlib_fd(fd);
1298 /* Remove an alternate data stream through the XATTR interface */
1299 static int wimfs_removexattr(const char *path, const char *name)
1301 struct inode *inode;
1302 struct ads_entry *ads_entry;
1304 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1307 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1311 inode = wim_pathname_to_inode(w, path);
1315 ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
1318 inode_remove_ads(inode, ads_idx, w->lookup_table);
1323 /* Renames a file or directory. See rename (3) */
1324 static int wimfs_rename(const char *from, const char *to)
1328 struct dentry *parent_of_dst;
1329 char *file_name_utf16 = NULL, *file_name_utf8 = NULL;
1330 u16 file_name_utf16_len, file_name_utf8_len;
1333 /* This rename() implementation currently only supports actual files
1334 * (not alternate data streams) */
1336 src = get_dentry(w, from);
1340 dst = get_dentry(w, to);
1343 ret = get_names(&file_name_utf16, &file_name_utf8,
1344 &file_name_utf16_len, &file_name_utf8_len,
1350 /* Destination file exists */
1352 if (src == dst) /* Same file */
1355 if (!dentry_is_directory(src)) {
1356 /* Cannot rename non-directory to directory. */
1357 if (dentry_is_directory(dst))
1360 /* Cannot rename directory to a non-directory or a non-empty
1362 if (!dentry_is_directory(dst))
1364 if (dst->d_inode->children != NULL)
1367 parent_of_dst = dst->parent;
1368 remove_dentry(dst, w->lookup_table);
1370 /* Destination does not exist */
1371 parent_of_dst = get_parent_dentry(w, to);
1375 if (!dentry_is_directory(parent_of_dst))
1379 FREE(src->file_name);
1380 FREE(src->file_name_utf8);
1381 src->file_name = file_name_utf16;
1382 src->file_name_utf8 = file_name_utf8;
1383 src->file_name_len = file_name_utf16_len;
1384 src->file_name_utf8_len = file_name_utf8_len;
1387 link_dentry(src, parent_of_dst);
1391 /* Remove a directory */
1392 static int wimfs_rmdir(const char *path)
1394 struct dentry *dentry;
1396 dentry = get_dentry(w, path);
1400 if (!dentry_is_empty_directory(dentry))
1403 remove_dentry(dentry, w->lookup_table);
1408 /* Write an alternate data stream through the XATTR interface */
1409 static int wimfs_setxattr(const char *path, const char *name,
1410 const char *value, size_t size, int flags)
1412 struct ads_entry *existing_ads_entry;
1413 struct ads_entry *new_ads_entry;
1414 struct lookup_table_entry *existing_lte;
1415 struct lookup_table_entry *lte;
1416 struct inode *inode;
1417 u8 value_hash[SHA1_HASH_SIZE];
1420 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1423 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1427 inode = wim_pathname_to_inode(w, path);
1431 existing_ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
1432 if (existing_ads_entry) {
1433 if (flags & XATTR_CREATE)
1435 inode_remove_ads(inode, ads_idx, w->lookup_table);
1437 if (flags & XATTR_REPLACE)
1440 new_ads_entry = inode_add_ads(inode, name);
1444 sha1_buffer((const u8*)value, size, value_hash);
1446 existing_lte = __lookup_resource(w->lookup_table, value_hash);
1453 lte = new_lookup_table_entry();
1456 value_copy = MALLOC(size);
1461 memcpy(value_copy, value, size);
1462 lte->resource_location = RESOURCE_IN_ATTACHED_BUFFER;
1463 lte->attached_buffer = value_copy;
1464 lte->resource_entry.original_size = size;
1465 lte->resource_entry.size = size;
1466 lte->resource_entry.flags = 0;
1467 copy_hash(lte->hash, value_hash);
1468 lookup_table_insert(w->lookup_table, lte);
1470 new_ads_entry->lte = lte;
1475 static int wimfs_symlink(const char *to, const char *from)
1477 struct dentry *dentry_parent, *dentry;
1478 const char *link_name;
1479 struct inode *inode;
1481 dentry_parent = get_parent_dentry(w, from);
1484 if (!dentry_is_directory(dentry_parent))
1487 link_name = path_basename(from);
1489 if (get_dentry_child_with_name(dentry_parent, link_name))
1491 dentry = new_dentry_with_inode(link_name);
1494 inode = dentry->d_inode;
1496 inode->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
1497 inode->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
1498 inode->ino = next_ino++;
1499 inode->resolved = true;
1501 if (inode_set_symlink(inode, to, w->lookup_table, NULL) != 0)
1502 goto out_free_dentry;
1504 link_dentry(dentry, dentry_parent);
1507 free_dentry(dentry);
1512 /* Reduce the size of a file */
1513 static int wimfs_truncate(const char *path, off_t size)
1515 struct dentry *dentry;
1516 struct lookup_table_entry *lte;
1520 struct inode *inode;
1522 ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1528 if (!lte) /* Already a zero-length file */
1531 inode = dentry->d_inode;
1533 if (stream_idx == 0)
1536 stream_id = inode->ads_entries[stream_idx - 1].stream_id;
1538 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1539 wimlib_assert(lte->staging_file_name);
1540 ret = truncate(lte->staging_file_name, size);
1543 lte->resource_entry.original_size = size;
1545 wimlib_assert(lte->resource_location == RESOURCE_IN_WIM);
1546 /* File in WIM. Extract it to the staging directory, but only
1547 * the first @size bytes of it. */
1548 ret = extract_resource_to_staging_dir(inode, stream_id,
1554 /* Remove a regular file */
1555 static int wimfs_unlink(const char *path)
1557 struct dentry *dentry;
1558 struct lookup_table_entry *lte;
1559 struct inode *inode;
1564 ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1570 if (stream_idx == 0)
1571 remove_dentry(dentry, w->lookup_table);
1573 inode_remove_ads(dentry->d_inode, stream_idx - 1, w->lookup_table);
1577 #ifdef HAVE_UTIMENSAT
1579 * Change the timestamp on a file dentry.
1581 * Note that alternate data streams do not have their own timestamps.
1583 static int wimfs_utimens(const char *path, const struct timespec tv[2])
1585 struct dentry *dentry;
1586 struct inode *inode;
1587 dentry = get_dentry(w, path);
1590 inode = dentry->d_inode;
1592 if (tv[0].tv_nsec != UTIME_OMIT) {
1593 if (tv[0].tv_nsec == UTIME_NOW)
1594 inode->last_access_time = get_wim_timestamp();
1596 inode->last_access_time = timespec_to_wim_timestamp(&tv[0]);
1598 if (tv[1].tv_nsec != UTIME_OMIT) {
1599 if (tv[1].tv_nsec == UTIME_NOW)
1600 inode->last_write_time = get_wim_timestamp();
1602 inode->last_write_time = timespec_to_wim_timestamp(&tv[1]);
1607 static int wimfs_utime(const char *path, struct utimbuf *times)
1609 struct dentry *dentry;
1610 struct inode *inode;
1611 dentry = get_dentry(w, path);
1614 inode = dentry->d_inode;
1616 inode->last_write_time = unix_timestamp_to_wim(times->modtime);
1617 inode->last_access_time = unix_timestamp_to_wim(times->actime);
1622 /* Writes to a file in the WIM filesystem.
1623 * It may be an alternate data stream, but here we don't even notice because we
1624 * just get a lookup table entry. */
1625 static int wimfs_write(const char *path, const char *buf, size_t size,
1626 off_t offset, struct fuse_file_info *fi)
1628 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1635 wimlib_assert(fd->f_lte);
1636 wimlib_assert(fd->f_lte->staging_file_name);
1637 wimlib_assert(fd->staging_fd != -1);
1638 wimlib_assert(fd->f_inode);
1640 /* Seek to the requested position */
1641 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1644 /* Write the data. */
1645 ret = write(fd->staging_fd, buf, size);
1649 now = get_wim_timestamp();
1650 fd->f_inode->last_write_time = now;
1651 fd->f_inode->last_access_time = now;
1655 static struct fuse_operations wimfs_operations = {
1656 .access = wimfs_access,
1657 .destroy = wimfs_destroy,
1659 .fallocate = wimfs_fallocate,
1661 .fgetattr = wimfs_fgetattr,
1662 .ftruncate = wimfs_ftruncate,
1663 .getattr = wimfs_getattr,
1665 .getxattr = wimfs_getxattr,
1669 .listxattr = wimfs_listxattr,
1671 .mkdir = wimfs_mkdir,
1672 .mknod = wimfs_mknod,
1674 .opendir = wimfs_opendir,
1676 .readdir = wimfs_readdir,
1677 .readlink = wimfs_readlink,
1678 .release = wimfs_release,
1679 .releasedir = wimfs_releasedir,
1681 .removexattr = wimfs_removexattr,
1683 .rename = wimfs_rename,
1684 .rmdir = wimfs_rmdir,
1686 .setxattr = wimfs_setxattr,
1688 .symlink = wimfs_symlink,
1689 .truncate = wimfs_truncate,
1690 .unlink = wimfs_unlink,
1691 #ifdef HAVE_UTIMENSAT
1692 .utimens = wimfs_utimens,
1694 .utime = wimfs_utime,
1696 .write = wimfs_write,
1700 /* Mounts a WIM file. */
1701 WIMLIBAPI int wimlib_mount(WIMStruct *wim, int image, const char *dir,
1702 int flags, WIMStruct **additional_swms,
1703 unsigned num_additional_swms)
1709 struct lookup_table *joined_tab, *wim_tab_save;
1710 struct image_metadata *imd;
1712 DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
1713 wim, image, dir, flags);
1716 return WIMLIB_ERR_INVALID_PARAM;
1718 ret = verify_swm_set(wim, additional_swms, num_additional_swms);
1722 if (num_additional_swms) {
1723 ret = new_joined_lookup_table(wim, additional_swms,
1724 num_additional_swms,
1728 wim_tab_save = wim->lookup_table;
1729 wim->lookup_table = joined_tab;
1732 ret = select_wim_image(wim, image);
1737 imd = &wim->image_metadata[image - 1];
1739 DEBUG("Selected image %d", image);
1741 if (imd->root_dentry->refcnt != 1) {
1742 ERROR("Cannot mount image that was just exported with "
1744 return WIMLIB_ERR_INVALID_PARAM;
1747 next_ino = assign_inode_numbers(&imd->inode_list);
1749 DEBUG("(next_ino = %"PRIu64")", next_ino);
1751 /* Resolve all the lookup table entries of the dentry tree */
1752 DEBUG("Resolving lookup table entries");
1753 for_dentry_in_tree(imd->root_dentry, dentry_resolve_ltes,
1756 if (flags & WIMLIB_MOUNT_FLAG_READWRITE)
1757 imd->modified = true;
1759 if (!(flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
1760 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
1761 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
1762 flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1764 DEBUG("Getting current directory");
1767 working_directory = getcwd(NULL, 0);
1768 if (!working_directory) {
1769 ERROR_WITH_ERRNO("Could not determine current directory");
1770 ret = WIMLIB_ERR_NOTDIR;
1774 DEBUG("Closing POSIX message queues");
1775 /* XXX hack to get rid of the message queues if they already exist for
1776 * some reason (maybe left over from a previous mount that wasn't
1777 * unmounted correctly) */
1778 ret = open_message_queues(true);
1781 close_message_queues();
1783 DEBUG("Preparing arguments to fuse_main()");
1788 ret = WIMLIB_ERR_NOMEM;
1792 argv[argc++] = "imagex";
1794 argv[argc++] = "-s"; /* disable multi-threaded operation */
1796 if (flags & WIMLIB_MOUNT_FLAG_DEBUG)
1797 argv[argc++] = "-d";
1800 * We provide the use_ino option because we are going to assign inode
1801 * numbers oursides. We've already numbered the inodes with unique
1802 * numbers in the assign_inode_numbers() function, and the static
1803 * variable next_ino is set to the next available inode number.
1805 char optstring[256] = "use_ino,subtype=wimfs,attr_timeout=0";
1806 argv[argc++] = "-o";
1807 argv[argc++] = optstring;
1808 if ((flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
1809 /* Read-write mount. Make the staging directory */
1811 if (!staging_dir_name) {
1813 ret = WIMLIB_ERR_MKDIR;
1817 /* Read-only mount */
1818 strcat(optstring, ",ro");
1825 DEBUG("FUSE command line (argc = %d): ", argc);
1826 for (i = 0; i < argc; i++) {
1827 fputs(argv[i], stdout);
1835 /* Set static variables. */
1837 mount_flags = flags;
1839 ret = fuse_main(argc, argv, &wimfs_operations, NULL);
1841 ret = WIMLIB_ERR_FUSE;
1843 if (num_additional_swms) {
1844 free_lookup_table(wim->lookup_table);
1845 wim->lookup_table = wim_tab_save;
1852 * Unmounts the WIM file that was previously mounted on @dir by using
1855 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
1862 struct timespec timeout;
1868 /* Open message queues between the unmount process and the
1869 * filesystem daemon. */
1870 ret = open_message_queues(false);
1874 /* Send a message to the filesystem saying whether to commit or
1876 msg[0] = (flags & WIMLIB_UNMOUNT_FLAG_COMMIT) ? 1 : 0;
1877 msg[1] = (flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY) ? 1 : 0;
1879 DEBUG("Sending message: %s, %s",
1880 (msg[0] == 0) ? "don't commit" : "commit",
1881 (msg[1] == 0) ? "don't check" : "check");
1882 ret = mq_send(unmount_to_daemon_mq, msg, 2, 1);
1884 ERROR("Failed to notify filesystem daemon whether we want to "
1885 "commit changes or not");
1886 close_message_queues();
1887 return WIMLIB_ERR_MQUEUE;
1890 /* Execute `fusermount -u', which is installed setuid root, to unmount
1893 * FUSE does not yet implement synchronous unmounts. This means that
1894 * fusermount -u will return before the filesystem daemon returns from
1895 * wimfs_destroy(). This is partly what we want, because we need to
1896 * send a message from this process to the filesystem daemon telling
1897 * whether --commit was specified or not. However, after that, the
1898 * unmount process must wait for the filesystem daemon to finish writing
1905 ERROR_WITH_ERRNO("Failed to fork()");
1906 return WIMLIB_ERR_FORK;
1909 execlp("fusermount", "fusermount", "-u", dir, NULL);
1910 ERROR_WITH_ERRNO("Failed to execute `fusermount'");
1911 exit(WIMLIB_ERR_FUSERMOUNT);
1914 ret = wait(&status);
1916 ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
1918 return WIMLIB_ERR_FUSERMOUNT;
1922 ERROR("fusermount exited with status %d", status);
1924 /* Try again, but with the `umount' program. This is required
1925 * on other FUSE implementations such as FreeBSD's that do not
1926 * have a `fusermount' program. */
1930 ERROR_WITH_ERRNO("Failed to fork()");
1931 return WIMLIB_ERR_FORK;
1934 execlp("umount", "umount", dir, NULL);
1935 ERROR_WITH_ERRNO("Failed to execute `umount'");
1936 exit(WIMLIB_ERR_FUSERMOUNT);
1939 ret = wait(&status);
1941 ERROR_WITH_ERRNO("Failed to wait for `umount' process to "
1943 return WIMLIB_ERR_FUSERMOUNT;
1946 ERROR("`umount' exited with failure status");
1947 return WIMLIB_ERR_FUSERMOUNT;
1952 /* Wait for a message from the filesytem daemon indicating whether the
1953 * filesystem was unmounted successfully (0) or an error occurred (1).
1954 * This may take a long time if a big WIM file needs to be rewritten. */
1956 /* Wait at most 600??? seconds before giving up and returning false.
1957 * Either it's a really big WIM file, or (more likely) the
1958 * filesystem daemon has crashed or failed for some reason.
1960 * XXX come up with some method to determine if the filesystem
1961 * daemon has really crashed or not.
1963 * XXX Idea: have mount daemon write its PID into the WIM file header?
1966 gettimeofday(&now, NULL);
1967 timeout.tv_sec = now.tv_sec + 600;
1968 timeout.tv_nsec = now.tv_usec * 1000;
1970 msgsize = mq_get_msgsize(daemon_to_unmount_mq);
1971 char mailbox[msgsize];
1974 DEBUG("Waiting for message telling us whether the unmount was "
1975 "successful or not.");
1976 ret = mq_timedreceive(daemon_to_unmount_mq, mailbox, msgsize,
1979 close_message_queues();
1981 if (errno_save == ETIMEDOUT) {
1982 ERROR("Timed out- probably the filesystem daemon "
1983 "crashed and the WIM was not written "
1985 return WIMLIB_ERR_TIMEOUT;
1987 ERROR("mq_receive(): %s", strerror(errno_save));
1988 return WIMLIB_ERR_MQUEUE;
1992 DEBUG("Received message: %s",
1993 (mailbox[0] == 0) ? "Unmount OK" : "Unmount Failed");
1994 if (mailbox[0] != 0)
1995 ERROR("Unmount failed");
1999 #else /* WITH_FUSE */
2002 static inline int mount_unsupported_error()
2004 ERROR("WIMLIB was compiled with --without-fuse, which disables support "
2005 "for mounting WIMs.");
2006 return WIMLIB_ERR_UNSUPPORTED;
2009 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
2011 return mount_unsupported_error();
2014 WIMLIBAPI int wimlib_mount(WIMStruct *wim_p, int image, const char *dir,
2015 int flags, WIMStruct **additional_swms,
2016 unsigned num_additional_swms)
2018 return mount_unsupported_error();
2021 #endif /* WITH_FUSE */