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 wimlib_assert(lte->staging_file_name);
193 wimlib_assert(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);
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->staging_file_name = staging_file_name;
411 new_lte->resource_location = RESOURCE_IN_STAGING_FILE;
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)
555 * Opens two POSIX message queue: one for sending messages from the unmount
556 * process to the daemon process, and one to go the other way. The names of the
557 * message queues, which must be system-wide unique, are be based on the mount
558 * point. (There of course is still a possibility of a collision if one were to
559 * unmount two identically named directories simultaneously...)
561 * @daemon specifies whether the calling process is the filesystem daemon or the
564 static int open_message_queues(bool daemon)
566 static const char *slash = "/";
567 static const char *prefix = "wimlib-";
568 static const char *u2d_suffix = "unmount-to-daemon-mq";
569 static const char *d2u_suffix = "daemon-to-unmount-mq";
571 const char *mount_dir_basename = path_basename(mount_dir);
575 unmount_to_daemon_mq_name = strcat_dup(slash, mount_dir_basename,
577 if (!unmount_to_daemon_mq_name) {
578 ERROR("Out of memory");
579 return WIMLIB_ERR_NOMEM;
581 daemon_to_unmount_mq_name = strcat_dup(slash, mount_dir_basename,
583 if (!daemon_to_unmount_mq_name) {
584 ERROR("Out of memory");
585 ret = WIMLIB_ERR_NOMEM;
589 remove_trailing_slashes(unmount_to_daemon_mq_name);
590 remove_trailing_slashes(daemon_to_unmount_mq_name);
591 s_slashes_underscores_g(unmount_to_daemon_mq_name + 1);
592 s_slashes_underscores_g(daemon_to_unmount_mq_name + 1);
595 flags = O_RDONLY | O_CREAT;
597 flags = O_WRONLY | O_CREAT;
599 unmount_to_daemon_mq = mq_open(unmount_to_daemon_mq_name, flags,
602 if (unmount_to_daemon_mq == (mqd_t)-1) {
603 ERROR_WITH_ERRNO("mq_open()");
604 ret = WIMLIB_ERR_MQUEUE;
609 flags = O_WRONLY | O_CREAT;
611 flags = O_RDONLY | O_CREAT;
613 daemon_to_unmount_mq = mq_open(daemon_to_unmount_mq_name, flags,
616 if (daemon_to_unmount_mq == (mqd_t)-1) {
617 ERROR_WITH_ERRNO("mq_open()");
618 ret = WIMLIB_ERR_MQUEUE;
623 mq_close(unmount_to_daemon_mq);
624 mq_unlink(unmount_to_daemon_mq_name);
626 FREE(daemon_to_unmount_mq_name);
628 FREE(unmount_to_daemon_mq_name);
632 static int mq_get_msgsize(mqd_t mq)
634 static const char *msgsize_max_file = "/proc/sys/fs/mqueue/msgsize_max";
639 if (mq_getattr(unmount_to_daemon_mq, &attr) == 0) {
640 msgsize = attr.mq_msgsize;
642 ERROR_WITH_ERRNO("mq_getattr()");
643 ERROR("Attempting to read %s", msgsize_max_file);
644 fp = fopen(msgsize_max_file, "rb");
646 if (fscanf(fp, "%d", &msgsize) != 1) {
647 ERROR("Assuming message size of 8192");
652 ERROR_WITH_ERRNO("Failed to open the file `%s'",
654 ERROR("Assuming message size of 8192");
661 /* Closes the message queues, which are allocated in static variables */
662 static void close_message_queues()
664 mq_close(unmount_to_daemon_mq);
665 mq_close(daemon_to_unmount_mq);
666 mq_unlink(unmount_to_daemon_mq_name);
667 mq_unlink(daemon_to_unmount_mq_name);
670 static int wimfs_access(const char *path, int mask)
672 /* Permissions not implemented */
676 static int update_lte_of_staging_file(struct lookup_table_entry *lte,
677 struct lookup_table *table)
679 struct lookup_table_entry *duplicate_lte;
681 u8 hash[SHA1_HASH_SIZE];
684 wimlib_assert(lte->resource_location == RESOURCE_IN_STAGING_FILE);
685 wimlib_assert(lte->staging_file_name);
687 ret = sha1sum(lte->staging_file_name, hash);
691 lookup_table_unlink(table, lte);
693 duplicate_lte = __lookup_resource(table, hash);
696 /* Merge duplicate lookup table entries */
697 duplicate_lte->refcnt += lte->refcnt;
698 list_del(<e->staging_list);
699 free_lookup_table_entry(lte);
701 if (stat(lte->staging_file_name, &stbuf) != 0) {
702 ERROR_WITH_ERRNO("Failed to stat `%s'", lte->staging_file_name);
703 return WIMLIB_ERR_STAT;
705 wimlib_assert(<e->file_on_disk == <e->staging_file_name);
706 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
707 copy_hash(lte->hash, hash);
708 lte->resource_entry.original_size = stbuf.st_size;
709 lte->resource_entry.size = stbuf.st_size;
710 lte->lte_inode = NULL;
711 lookup_table_insert(table, lte);
717 static int inode_close_fds(struct inode *inode)
719 for (u16 i = 0, j = 0; j < inode->num_opened_fds; i++) {
720 struct wimlib_fd *fd = inode->fds[i];
722 wimlib_assert(fd->f_inode == inode);
723 int ret = close_wimlib_fd(fd);
732 /*static int dentry_close_fds(struct dentry *dentry, void *ignore)*/
734 /*return inode_close_fds(dentry->d_inode);*/
737 /* Overwrites the WIM file, with changes saved. */
738 static int rebuild_wim(WIMStruct *w, bool check_integrity)
741 struct lookup_table_entry *lte, *tmp;
744 DEBUG("Closing all staging file descriptors.");
745 list_for_each_entry(lte, &staging_list, staging_list) {
746 ret = inode_close_fds(lte->lte_inode);
750 /*ret = for_dentry_in_tree(wim_root_dentry(w), dentry_close_fds, NULL);*/
754 DEBUG("Calculating SHA1 checksums for all new staging files.");
755 list_for_each_entry_safe(lte, tmp, &staging_list, staging_list) {
756 ret = update_lte_of_staging_file(lte, w->lookup_table);
761 xml_update_image_info(w, w->current_image);
763 ret = wimlib_overwrite(w, check_integrity);
765 ERROR("Failed to commit changes");
771 /* Called when the filesystem is unmounted. */
772 static void wimfs_destroy(void *p)
774 /* For read-write mounts, the `imagex unmount' command, which is
775 * running in a separate process and is executing the
776 * wimlib_unmount() function, will send this process a byte
777 * through a message queue that indicates whether the --commit
778 * option was specified or not. */
781 struct timespec timeout;
783 ssize_t bytes_received;
786 char check_integrity;
789 ret = open_message_queues(true);
793 msgsize = mq_get_msgsize(unmount_to_daemon_mq);
798 /* Wait at most 3 seconds before giving up and discarding changes. */
799 gettimeofday(&now, NULL);
800 timeout.tv_sec = now.tv_sec + 3;
801 timeout.tv_nsec = now.tv_usec * 1000;
802 DEBUG("Waiting for message telling us whether to commit or not, and "
803 "whether to include integrity checks.");
805 bytes_received = mq_timedreceive(unmount_to_daemon_mq, msg,
806 msgsize, NULL, &timeout);
808 check_integrity = msg[1];
809 if (bytes_received == -1) {
810 if (errno == ETIMEDOUT) {
813 ERROR_WITH_ERRNO("mq_timedreceive()");
815 ERROR("Not committing.");
817 DEBUG("Received message: [%d %d]", msg[0], msg[1]);
821 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
823 status = chdir(working_directory);
825 ERROR_WITH_ERRNO("chdir()");
826 status = WIMLIB_ERR_NOTDIR;
829 status = rebuild_wim(w, (check_integrity != 0));
831 ret = delete_staging_dir();
833 ERROR_WITH_ERRNO("Failed to delete the staging "
839 DEBUG("Read-only mount");
842 DEBUG("Sending status %u", status);
843 ret = mq_send(daemon_to_unmount_mq, &status, 1, 1);
845 ERROR_WITH_ERRNO("Failed to send status to unmount process");
846 close_message_queues();
850 static int wimfs_fallocate(const char *path, int mode,
851 off_t offset, off_t len, struct fuse_file_info *fi)
853 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
854 wimlib_assert(fd->staging_fd != -1);
855 return fallocate(fd->staging_fd, mode, offset, len);
860 static int wimfs_fgetattr(const char *path, struct stat *stbuf,
861 struct fuse_file_info *fi)
863 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
864 return inode_to_stbuf(fd->f_inode, fd->f_lte, stbuf);
867 static int wimfs_ftruncate(const char *path, off_t size,
868 struct fuse_file_info *fi)
870 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
871 int ret = ftruncate(fd->staging_fd, size);
874 if (fd->f_lte && size < fd->f_lte->resource_entry.original_size)
875 fd->f_lte->resource_entry.original_size = size;
880 * Fills in a `struct stat' that corresponds to a file or directory in the WIM.
882 static int wimfs_getattr(const char *path, struct stat *stbuf)
884 struct dentry *dentry;
885 struct lookup_table_entry *lte;
888 ret = lookup_resource(w, path,
889 get_lookup_flags() | LOOKUP_FLAG_DIRECTORY_OK,
890 &dentry, <e, NULL);
893 return inode_to_stbuf(dentry->d_inode, lte, stbuf);
897 /* Read an alternate data stream through the XATTR interface, or get its size */
898 static int wimfs_getxattr(const char *path, const char *name, char *value,
902 struct dentry *dentry;
903 struct ads_entry *ads_entry;
905 struct lookup_table_entry *lte;
907 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
910 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
914 dentry = get_dentry(w, path);
917 ads_entry = inode_get_ads_entry(dentry->d_inode, name, NULL);
921 lte = ads_entry->lte;
922 res_size = wim_resource_size(lte);
928 ret = read_full_wim_resource(lte, (u8*)value);
935 /* Create a hard link */
936 static int wimfs_link(const char *to, const char *from)
938 struct dentry *from_dentry, *from_dentry_parent;
939 const char *link_name;
941 struct lookup_table_entry *lte;
943 inode = wim_pathname_to_inode(w, to);
947 if (!inode_is_regular_file(inode))
950 from_dentry_parent = get_parent_dentry(w, from);
951 if (!from_dentry_parent)
953 if (!dentry_is_directory(from_dentry_parent))
956 link_name = path_basename(from);
957 if (get_dentry_child_with_name(from_dentry_parent, link_name))
959 from_dentry = new_dentry(link_name);
964 inode_add_dentry(from_dentry, inode);
965 from_dentry->d_inode = inode;
968 for (unsigned i = 0; i <= inode->num_ads; i++) {
969 lte = inode_stream_lte_resolved(inode, i);
974 link_dentry(from_dentry, from_dentry_parent);
979 static int wimfs_listxattr(const char *path, char *list, size_t size)
981 struct dentry *dentry;
987 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
990 /* List alternate data streams, or get the list size */
992 ret = lookup_resource(w, path, get_lookup_flags(), &dentry, NULL, NULL);
995 inode = dentry->d_inode;
999 for (i = 0; i < inode->num_ads; i++)
1000 needed_size += inode->ads_entries[i].stream_name_utf8_len + 6;
1003 for (i = 0; i < inode->num_ads; i++) {
1004 needed_size = inode->ads_entries[i].stream_name_utf8_len + 6;
1005 if (needed_size > size)
1007 p += sprintf(p, "user.%s",
1008 inode->ads_entries[i].stream_name_utf8) + 1;
1009 size -= needed_size;
1017 * Create a directory in the WIM.
1018 * @mode is currently ignored.
1020 static int wimfs_mkdir(const char *path, mode_t mode)
1022 struct dentry *parent;
1023 struct dentry *newdir;
1024 const char *basename;
1026 parent = get_parent_dentry(w, path);
1030 if (!dentry_is_directory(parent))
1033 basename = path_basename(path);
1034 if (get_dentry_child_with_name(parent, basename))
1037 newdir = new_dentry_with_inode(basename);
1038 newdir->d_inode->attributes |= FILE_ATTRIBUTE_DIRECTORY;
1039 newdir->d_inode->resolved = true;
1040 newdir->d_inode->ino = next_ino++;
1041 link_dentry(newdir, parent);
1045 /* Creates a regular file. */
1046 static int wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
1048 const char *stream_name;
1049 if ((mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
1050 && (stream_name = path_stream_name(path))) {
1051 /* Make an alternate data stream */
1052 struct ads_entry *new_entry;
1053 struct dentry *dentry;
1055 char *p = (char*)stream_name - 1;
1056 wimlib_assert(*p == ':');
1059 dentry = get_dentry(w, path);
1060 if (!dentry || !dentry_is_regular_file(dentry))
1062 if (inode_get_ads_entry(dentry->d_inode, stream_name, NULL))
1064 new_entry = inode_add_ads(dentry->d_inode, stream_name);
1068 struct dentry *dentry, *parent;
1069 const char *basename;
1071 /* Make a normal file (not an alternate data stream) */
1073 /* Make sure that the parent of @path exists and is a directory, and
1074 * that the dentry named by @path does not already exist. */
1075 parent = get_parent_dentry(w, path);
1078 if (!dentry_is_directory(parent))
1081 basename = path_basename(path);
1082 if (get_dentry_child_with_name(parent, path))
1085 dentry = new_dentry_with_inode(basename);
1088 dentry->d_inode->resolved = true;
1089 dentry->d_inode->ino = next_ino++;
1090 link_dentry(dentry, parent);
1097 static int wimfs_open(const char *path, struct fuse_file_info *fi)
1099 struct dentry *dentry;
1100 struct lookup_table_entry *lte;
1102 struct wimlib_fd *fd;
1103 struct inode *inode;
1107 ret = lookup_resource(w, path, get_lookup_flags(), &dentry, <e,
1112 inode = dentry->d_inode;
1114 if (stream_idx == 0)
1117 stream_id = inode->ads_entries[stream_idx - 1].stream_id;
1119 /* The file resource may be in the staging directory (read-write mounts
1120 * only) or in the WIM. If it's in the staging directory, we need to
1121 * open a native file descriptor for the corresponding file. Otherwise,
1122 * we can read the file resource directly from the WIM file if we are
1123 * opening it read-only, but we need to extract the resource to the
1124 * staging directory if we are opening it writable. */
1126 if (flags_writable(fi->flags) &&
1127 (!lte || lte->resource_location != RESOURCE_IN_STAGING_FILE)) {
1128 u64 size = (lte) ? wim_resource_size(lte) : 0;
1129 ret = extract_resource_to_staging_dir(inode, stream_id,
1135 ret = alloc_wimlib_fd(inode, stream_id, lte, &fd);
1139 if (lte && lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1140 fd->staging_fd = open(lte->staging_file_name, fi->flags);
1141 if (fd->staging_fd == -1) {
1142 close_wimlib_fd(fd);
1146 fi->fh = (uintptr_t)fd;
1150 /* Opens a directory. */
1151 static int wimfs_opendir(const char *path, struct fuse_file_info *fi)
1153 struct inode *inode;
1155 struct wimlib_fd *fd = NULL;
1157 inode = wim_pathname_to_inode(w, path);
1160 if (!inode_is_directory(inode))
1162 ret = alloc_wimlib_fd(inode, 0, NULL, &fd);
1163 fi->fh = (uintptr_t)fd;
1169 * Read data from a file in the WIM or in the staging directory.
1171 static int wimfs_read(const char *path, char *buf, size_t size,
1172 off_t offset, struct fuse_file_info *fi)
1174 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1179 if (!fd->f_lte) /* Empty stream with no lookup table entry */
1182 if (fd->f_lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1183 /* Read from staging file */
1185 wimlib_assert(fd->f_lte->staging_file_name);
1186 wimlib_assert(fd->staging_fd != -1);
1189 DEBUG("Seek to offset %zu", offset);
1191 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1193 ret = read(fd->staging_fd, buf, size);
1200 u64 res_size = wim_resource_size(fd->f_lte);
1202 if (offset > res_size)
1205 size = min(size, res_size - offset);
1207 if (read_wim_resource(fd->f_lte, (u8*)buf,
1208 size, offset, false) != 0)
1214 /* Fills in the entries of the directory specified by @path using the
1215 * FUSE-provided function @filler. */
1216 static int wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
1217 off_t offset, struct fuse_file_info *fi)
1219 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1220 struct inode *inode;
1221 struct dentry *child;
1226 inode = fd->f_inode;
1228 filler(buf, ".", NULL, 0);
1229 filler(buf, "..", NULL, 0);
1231 child = inode->children;
1237 if (filler(buf, child->file_name_utf8, NULL, 0))
1239 child = child->next;
1240 } while (child != inode->children);
1245 static int wimfs_readlink(const char *path, char *buf, size_t buf_len)
1247 struct dentry *dentry = get_dentry(w, path);
1251 if (!dentry_is_symlink(dentry))
1254 ret = inode_readlink(dentry->d_inode, buf, buf_len, w);
1261 static int wimfs_release(const char *path, struct fuse_file_info *fi)
1263 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1264 return close_wimlib_fd(fd);
1267 /* Close a directory */
1268 static int wimfs_releasedir(const char *path, struct fuse_file_info *fi)
1270 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1271 return close_wimlib_fd(fd);
1275 /* Remove an alternate data stream through the XATTR interface */
1276 static int wimfs_removexattr(const char *path, const char *name)
1278 struct dentry *dentry;
1279 struct ads_entry *ads_entry;
1281 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1284 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1288 dentry = get_dentry(w, path);
1292 ads_entry = inode_get_ads_entry(dentry->d_inode, name, &ads_idx);
1295 inode_remove_ads(dentry->d_inode, ads_idx, w->lookup_table);
1300 /* Renames a file or directory. See rename (3) */
1301 static int wimfs_rename(const char *from, const char *to)
1305 struct dentry *parent_of_dst;
1306 char *file_name_utf16 = NULL, *file_name_utf8 = NULL;
1307 u16 file_name_utf16_len, file_name_utf8_len;
1310 /* This rename() implementation currently only supports actual files
1311 * (not alternate data streams) */
1313 src = get_dentry(w, from);
1317 dst = get_dentry(w, to);
1320 ret = get_names(&file_name_utf16, &file_name_utf8,
1321 &file_name_utf16_len, &file_name_utf8_len,
1327 /* Destination file exists */
1329 if (src == dst) /* Same file */
1332 if (!dentry_is_directory(src)) {
1333 /* Cannot rename non-directory to directory. */
1334 if (dentry_is_directory(dst))
1337 /* Cannot rename directory to a non-directory or a non-empty
1339 if (!dentry_is_directory(dst))
1341 if (dst->d_inode->children != NULL)
1344 parent_of_dst = dst->parent;
1345 remove_dentry(dst, w->lookup_table);
1347 /* Destination does not exist */
1348 parent_of_dst = get_parent_dentry(w, to);
1352 if (!dentry_is_directory(parent_of_dst))
1356 FREE(src->file_name);
1357 FREE(src->file_name_utf8);
1358 src->file_name = file_name_utf16;
1359 src->file_name_utf8 = file_name_utf8;
1360 src->file_name_len = file_name_utf16_len;
1361 src->file_name_utf8_len = file_name_utf8_len;
1364 link_dentry(src, parent_of_dst);
1368 /* Remove a directory */
1369 static int wimfs_rmdir(const char *path)
1371 struct dentry *dentry;
1373 dentry = get_dentry(w, path);
1377 if (!dentry_is_empty_directory(dentry))
1380 remove_dentry(dentry, w->lookup_table);
1385 /* Write an alternate data stream through the XATTR interface */
1386 static int wimfs_setxattr(const char *path, const char *name,
1387 const char *value, size_t size, int flags)
1389 struct ads_entry *existing_ads_entry;
1390 struct ads_entry *new_ads_entry;
1391 struct lookup_table_entry *existing_lte;
1392 struct lookup_table_entry *lte;
1393 struct inode *inode;
1394 u8 value_hash[SHA1_HASH_SIZE];
1397 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1400 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1404 inode = wim_pathname_to_inode(w, path);
1408 existing_ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
1409 if (existing_ads_entry) {
1410 if (flags & XATTR_CREATE)
1412 inode_remove_ads(inode, ads_idx, w->lookup_table);
1414 if (flags & XATTR_REPLACE)
1417 new_ads_entry = inode_add_ads(inode, name);
1421 sha1_buffer((const u8*)value, size, value_hash);
1423 existing_lte = __lookup_resource(w->lookup_table, value_hash);
1430 lte = new_lookup_table_entry();
1433 value_copy = MALLOC(size);
1438 memcpy(value_copy, value, size);
1439 lte->resource_location = RESOURCE_IN_ATTACHED_BUFFER;
1440 lte->attached_buffer = value_copy;
1441 lte->resource_entry.original_size = size;
1442 lte->resource_entry.size = size;
1443 lte->resource_entry.flags = 0;
1444 copy_hash(lte->hash, value_hash);
1445 lookup_table_insert(w->lookup_table, lte);
1447 new_ads_entry->lte = lte;
1452 static int wimfs_symlink(const char *to, const char *from)
1454 struct dentry *dentry_parent, *dentry;
1455 const char *link_name;
1456 struct inode *inode;
1458 dentry_parent = get_parent_dentry(w, from);
1461 if (!dentry_is_directory(dentry_parent))
1464 link_name = path_basename(from);
1466 if (get_dentry_child_with_name(dentry_parent, link_name))
1468 dentry = new_dentry_with_inode(link_name);
1471 inode = dentry->d_inode;
1473 inode->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
1474 inode->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
1475 inode->ino = next_ino++;
1476 inode->resolved = true;
1478 if (inode_set_symlink(inode, to, w->lookup_table, NULL) != 0)
1479 goto out_free_dentry;
1481 link_dentry(dentry, dentry_parent);
1484 free_dentry(dentry);
1489 /* Reduce the size of a file */
1490 static int wimfs_truncate(const char *path, off_t size)
1492 struct dentry *dentry;
1493 struct lookup_table_entry *lte;
1497 struct inode *inode;
1499 ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1505 if (!lte) /* Already a zero-length file */
1508 inode = dentry->d_inode;
1510 if (stream_idx == 0)
1513 stream_id = inode->ads_entries[stream_idx - 1].stream_id;
1515 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1516 wimlib_assert(lte->staging_file_name);
1517 ret = truncate(lte->staging_file_name, size);
1520 lte->resource_entry.original_size = size;
1522 wimlib_assert(lte->resource_location == RESOURCE_IN_WIM);
1523 /* File in WIM. Extract it to the staging directory, but only
1524 * the first @size bytes of it. */
1525 ret = extract_resource_to_staging_dir(inode, stream_id,
1531 /* Remove a regular file */
1532 static int wimfs_unlink(const char *path)
1534 struct dentry *dentry;
1535 struct lookup_table_entry *lte;
1536 struct inode *inode;
1541 ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1547 if (stream_idx == 0)
1548 remove_dentry(dentry, w->lookup_table);
1550 inode_remove_ads(dentry->d_inode, stream_idx - 1, w->lookup_table);
1554 #ifdef HAVE_UTIMENSAT
1556 * Change the timestamp on a file dentry.
1558 * Note that alternate data streams do not have their own timestamps.
1560 static int wimfs_utimens(const char *path, const struct timespec tv[2])
1562 struct dentry *dentry;
1563 struct inode *inode;
1564 dentry = get_dentry(w, path);
1567 inode = dentry->d_inode;
1569 if (tv[0].tv_nsec != UTIME_OMIT) {
1570 if (tv[0].tv_nsec == UTIME_NOW)
1571 inode->last_access_time = get_wim_timestamp();
1573 inode->last_access_time = timespec_to_wim_timestamp(&tv[0]);
1575 if (tv[1].tv_nsec != UTIME_OMIT) {
1576 if (tv[1].tv_nsec == UTIME_NOW)
1577 inode->last_write_time = get_wim_timestamp();
1579 inode->last_write_time = timespec_to_wim_timestamp(&tv[1]);
1584 static int wimfs_utime(const char *path, struct utimbuf *times)
1586 struct dentry *dentry;
1587 struct inode *inode;
1588 dentry = get_dentry(w, path);
1591 inode = dentry->d_inode;
1593 inode->last_write_time = unix_timestamp_to_wim(times->modtime);
1594 inode->last_access_time = unix_timestamp_to_wim(times->actime);
1599 /* Writes to a file in the WIM filesystem.
1600 * It may be an alternate data stream, but here we don't even notice because we
1601 * just get a lookup table entry. */
1602 static int wimfs_write(const char *path, const char *buf, size_t size,
1603 off_t offset, struct fuse_file_info *fi)
1605 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1612 wimlib_assert(fd->f_lte);
1613 wimlib_assert(fd->f_lte->staging_file_name);
1614 wimlib_assert(fd->staging_fd != -1);
1615 wimlib_assert(fd->f_inode);
1617 /* Seek to the requested position */
1618 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1621 /* Write the data. */
1622 ret = write(fd->staging_fd, buf, size);
1626 now = get_wim_timestamp();
1627 fd->f_inode->last_write_time = now;
1628 fd->f_inode->last_access_time = now;
1632 static struct fuse_operations wimfs_operations = {
1633 .access = wimfs_access,
1634 .destroy = wimfs_destroy,
1636 .fallocate = wimfs_fallocate,
1638 .fgetattr = wimfs_fgetattr,
1639 .ftruncate = wimfs_ftruncate,
1640 .getattr = wimfs_getattr,
1642 .getxattr = wimfs_getxattr,
1646 .listxattr = wimfs_listxattr,
1648 .mkdir = wimfs_mkdir,
1649 .mknod = wimfs_mknod,
1651 .opendir = wimfs_opendir,
1653 .readdir = wimfs_readdir,
1654 .readlink = wimfs_readlink,
1655 .release = wimfs_release,
1656 .releasedir = wimfs_releasedir,
1658 .removexattr = wimfs_removexattr,
1660 .rename = wimfs_rename,
1661 .rmdir = wimfs_rmdir,
1663 .setxattr = wimfs_setxattr,
1665 .symlink = wimfs_symlink,
1666 .truncate = wimfs_truncate,
1667 .unlink = wimfs_unlink,
1668 #ifdef HAVE_UTIMENSAT
1669 .utimens = wimfs_utimens,
1671 .utime = wimfs_utime,
1673 .write = wimfs_write,
1677 /* Mounts a WIM file. */
1678 WIMLIBAPI int wimlib_mount(WIMStruct *wim, int image, const char *dir,
1679 int flags, WIMStruct **additional_swms,
1680 unsigned num_additional_swms)
1686 struct lookup_table *joined_tab, *wim_tab_save;
1687 struct image_metadata *imd;
1689 DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
1690 wim, image, dir, flags);
1693 return WIMLIB_ERR_INVALID_PARAM;
1695 ret = verify_swm_set(wim, additional_swms, num_additional_swms);
1699 if (num_additional_swms) {
1700 ret = new_joined_lookup_table(wim, additional_swms,
1701 num_additional_swms,
1705 wim_tab_save = wim->lookup_table;
1706 wim->lookup_table = joined_tab;
1709 ret = wimlib_select_image(wim, image);
1714 imd = &wim->image_metadata[image - 1];
1716 DEBUG("Selected image %d", image);
1718 next_ino = assign_inode_numbers(&imd->inode_list);
1720 DEBUG("(next_ino = %"PRIu64")", next_ino);
1722 /* Resolve all the lookup table entries of the dentry tree */
1723 DEBUG("Resolving lookup table entries");
1724 for_dentry_in_tree(imd->root_dentry, dentry_resolve_ltes,
1727 if (flags & WIMLIB_MOUNT_FLAG_READWRITE)
1728 imd->modified = true;
1730 if (!(flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
1731 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
1732 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
1733 flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1735 DEBUG("Getting current directory");
1738 working_directory = getcwd(NULL, 0);
1739 if (!working_directory) {
1740 ERROR_WITH_ERRNO("Could not determine current directory");
1741 ret = WIMLIB_ERR_NOTDIR;
1745 DEBUG("Closing POSIX message queues");
1746 /* XXX hack to get rid of the message queues if they already exist for
1747 * some reason (maybe left over from a previous mount that wasn't
1748 * unmounted correctly) */
1749 ret = open_message_queues(true);
1752 close_message_queues();
1754 DEBUG("Preparing arguments to fuse_main()");
1759 ret = WIMLIB_ERR_NOMEM;
1763 argv[argc++] = "imagex";
1765 argv[argc++] = "-s"; /* disable multi-threaded operation */
1767 if (flags & WIMLIB_MOUNT_FLAG_DEBUG)
1768 argv[argc++] = "-d";
1771 * We provide the use_ino option because we are going to assign inode
1772 * numbers oursides. We've already numbered the inodes with unique
1773 * numbers in the assign_inode_numbers() function, and the static
1774 * variable next_ino is set to the next available inode number.
1776 char optstring[256] = "use_ino,subtype=wimfs,attr_timeout=0";
1777 argv[argc++] = "-o";
1778 argv[argc++] = optstring;
1779 if ((flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
1780 /* Read-write mount. Make the staging directory */
1782 if (!staging_dir_name) {
1784 ret = WIMLIB_ERR_MKDIR;
1788 /* Read-only mount */
1789 strcat(optstring, ",ro");
1796 DEBUG("FUSE command line (argc = %d): ", argc);
1797 for (i = 0; i < argc; i++) {
1798 fputs(argv[i], stdout);
1806 /* Set static variables. */
1808 mount_flags = flags;
1810 ret = fuse_main(argc, argv, &wimfs_operations, NULL);
1812 ret = WIMLIB_ERR_FUSE;
1814 if (num_additional_swms) {
1815 free_lookup_table(wim->lookup_table);
1816 wim->lookup_table = wim_tab_save;
1823 * Unmounts the WIM file that was previously mounted on @dir by using
1826 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
1833 struct timespec timeout;
1839 /* Open message queues between the unmount process and the
1840 * filesystem daemon. */
1841 ret = open_message_queues(false);
1845 /* Send a message to the filesystem saying whether to commit or
1847 msg[0] = (flags & WIMLIB_UNMOUNT_FLAG_COMMIT) ? 1 : 0;
1848 msg[1] = (flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY) ? 1 : 0;
1850 DEBUG("Sending message: %s, %s",
1851 (msg[0] == 0) ? "don't commit" : "commit",
1852 (msg[1] == 0) ? "don't check" : "check");
1853 ret = mq_send(unmount_to_daemon_mq, msg, 2, 1);
1855 ERROR("Failed to notify filesystem daemon whether we want to "
1856 "commit changes or not");
1857 close_message_queues();
1858 return WIMLIB_ERR_MQUEUE;
1861 /* Execute `fusermount -u', which is installed setuid root, to unmount
1864 * FUSE does not yet implement synchronous unmounts. This means that
1865 * fusermount -u will return before the filesystem daemon returns from
1866 * wimfs_destroy(). This is partly what we want, because we need to
1867 * send a message from this process to the filesystem daemon telling
1868 * whether --commit was specified or not. However, after that, the
1869 * unmount process must wait for the filesystem daemon to finish writing
1876 ERROR_WITH_ERRNO("Failed to fork()");
1877 return WIMLIB_ERR_FORK;
1880 execlp("fusermount", "fusermount", "-u", dir, NULL);
1881 ERROR_WITH_ERRNO("Failed to execute `fusermount'");
1882 exit(WIMLIB_ERR_FUSERMOUNT);
1885 ret = wait(&status);
1887 ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
1889 return WIMLIB_ERR_FUSERMOUNT;
1893 ERROR("fusermount exited with status %d", status);
1895 /* Try again, but with the `umount' program. This is required
1896 * on other FUSE implementations such as FreeBSD's that do not
1897 * have a `fusermount' program. */
1901 ERROR_WITH_ERRNO("Failed to fork()");
1902 return WIMLIB_ERR_FORK;
1905 execlp("umount", "umount", dir, NULL);
1906 ERROR_WITH_ERRNO("Failed to execute `umount'");
1907 exit(WIMLIB_ERR_FUSERMOUNT);
1910 ret = wait(&status);
1912 ERROR_WITH_ERRNO("Failed to wait for `umount' process to "
1914 return WIMLIB_ERR_FUSERMOUNT;
1917 ERROR("`umount' exited with failure status");
1918 return WIMLIB_ERR_FUSERMOUNT;
1923 /* Wait for a message from the filesytem daemon indicating whether the
1924 * filesystem was unmounted successfully (0) or an error occurred (1).
1925 * This may take a long time if a big WIM file needs to be rewritten. */
1927 /* Wait at most 600??? seconds before giving up and returning false.
1928 * Either it's a really big WIM file, or (more likely) the
1929 * filesystem daemon has crashed or failed for some reason.
1931 * XXX come up with some method to determine if the filesystem
1932 * daemon has really crashed or not.
1934 * XXX Idea: have mount daemon write its PID into the WIM file header?
1937 gettimeofday(&now, NULL);
1938 timeout.tv_sec = now.tv_sec + 600;
1939 timeout.tv_nsec = now.tv_usec * 1000;
1941 msgsize = mq_get_msgsize(daemon_to_unmount_mq);
1942 char mailbox[msgsize];
1945 DEBUG("Waiting for message telling us whether the unmount was "
1946 "successful or not.");
1947 ret = mq_timedreceive(daemon_to_unmount_mq, mailbox, msgsize,
1950 close_message_queues();
1952 if (errno_save == ETIMEDOUT) {
1953 ERROR("Timed out- probably the filesystem daemon "
1954 "crashed and the WIM was not written "
1956 return WIMLIB_ERR_TIMEOUT;
1958 ERROR("mq_receive(): %s", strerror(errno_save));
1959 return WIMLIB_ERR_MQUEUE;
1963 DEBUG("Received message: %s",
1964 (mailbox[0] == 0) ? "Unmount OK" : "Unmount Failed");
1965 if (mailbox[0] != 0)
1966 ERROR("Unmount failed");
1970 #else /* WITH_FUSE */
1973 static inline int mount_unsupported_error()
1975 ERROR("WIMLIB was compiled with --without-fuse, which disables support "
1976 "for mounting WIMs.");
1977 return WIMLIB_ERR_UNSUPPORTED;
1980 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
1982 return mount_unsupported_error();
1985 WIMLIBAPI int wimlib_mount(WIMStruct *wim_p, int image, const char *dir,
1986 int flags, WIMStruct **additional_swms,
1987 unsigned num_additional_swms)
1989 return mount_unsupported_error();
1992 #endif /* WITH_FUSE */