4 * This file implements mounting of WIM files using FUSE, which stands for
5 * Filesystem in Userspace. FUSE allows a filesystem to be implemented in a
6 * userspace process by implementing the filesystem primitives--- read(),
7 * write(), readdir(), etc.
11 * Copyright (C) 2012 Eric Biggers
13 * This file is part of wimlib, a library for working with WIM files.
15 * wimlib is free software; you can redistribute it and/or modify it under the
16 * terms of the GNU General Public License as published by the Free
17 * Software Foundation; either version 3 of the License, or (at your option)
20 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
21 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
22 * A PARTICULAR PURPOSE. See the GNU General Public License for more
25 * You should have received a copy of the GNU General Public License
26 * along with wimlib; if not, see http://www.gnu.org/licenses/.
29 #include "wimlib_internal.h"
34 #include "lookup_table.h"
37 #include "timestamp.h"
41 #define FUSE_USE_VERSION 26
51 #include <attr/xattr.h>
54 /* File descriptor to a file open on the WIM filesystem. */
57 struct lookup_table_entry *lte;
63 /* The WIMStruct for the mounted WIM. */
66 /* Working directory when `imagex mount' is run. */
67 static const char *working_directory;
69 /* Name of the staging directory for a read-write mount. Whenever a new file is
70 * created, it is done so in the staging directory. Furthermore, whenever a
71 * file in the WIM is modified, it is extracted to the staging directory. If
72 * changes are commited when the WIM is unmounted, the file resources are merged
73 * in from the staging directory when writing the new WIM. */
74 static char *staging_dir_name;
75 static size_t staging_dir_name_len;
77 /* Flags passed to wimlib_mount(). */
78 static int mount_flags;
80 /* Name of the directory on which the WIM file is mounted. */
81 static const char *mount_dir;
83 /* Next inode number to be assigned. */
86 /* List of lookup table entries in the staging directory */
87 static LIST_HEAD(staging_list);
89 static inline int get_lookup_flags()
91 if (mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
92 return LOOKUP_FLAG_ADS_OK;
97 /* Returns nonzero if write permission is requested on the file open flags */
98 static inline int flags_writable(int open_flags)
100 return open_flags & (O_RDWR | O_WRONLY);
103 static int alloc_wimlib_fd(struct inode *inode,
105 struct lookup_table_entry *lte,
106 struct wimlib_fd **fd_ret)
108 static const u16 fds_per_alloc = 8;
109 static const u16 max_fds = 0xffff;
111 DEBUG("Allocating fd for stream ID %u from inode %lx (open = %u, allocated = %u)",
112 stream_id, inode->ino, inode->num_opened_fds,
113 inode->num_allocated_fds);
115 if (inode->num_opened_fds == inode->num_allocated_fds) {
116 struct wimlib_fd **fds;
119 if (inode->num_allocated_fds == max_fds)
121 num_new_fds = min(fds_per_alloc, max_fds - inode->num_allocated_fds);
123 fds = REALLOC(inode->fds, (inode->num_allocated_fds + num_new_fds) *
124 sizeof(inode->fds[0]));
127 memset(&fds[inode->num_allocated_fds], 0,
128 num_new_fds * sizeof(fds[0]));
130 inode->num_allocated_fds += num_new_fds;
132 for (u16 i = 0; ; i++) {
133 if (!inode->fds[i]) {
134 struct wimlib_fd *fd = CALLOC(1, sizeof(*fd));
141 fd->stream_id = stream_id;
144 inode->num_opened_fds++;
146 lte->num_opened_fds++;
147 DEBUG("Allocated fd");
153 static void inode_put_fd(struct inode *inode, struct wimlib_fd *fd)
156 wimlib_assert(inode);
157 wimlib_assert(fd->inode == inode);
158 wimlib_assert(inode->num_opened_fds);
159 wimlib_assert(fd->idx < inode->num_opened_fds);
160 wimlib_assert(inode->fds[fd->idx] == fd);
162 inode->fds[fd->idx] = NULL;
164 if (--inode->num_opened_fds == 0 && inode->link_count == 0)
168 static int lte_put_fd(struct lookup_table_entry *lte, struct wimlib_fd *fd)
171 wimlib_assert(fd->lte == lte);
173 if (!lte) /* Empty stream with no lookup table entry */
176 wimlib_assert(lte->num_opened_fds);
178 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
179 wimlib_assert(lte->staging_file_name);
180 wimlib_assert(fd->staging_fd != -1);
181 if (close(fd->staging_fd) != 0)
184 lte_decrement_num_opened_fds(lte, w->lookup_table);
188 static int close_wimlib_fd(struct wimlib_fd *fd)
191 DEBUG("Closing fd (inode = %lu, opened = %u, allocated = %u)",
192 fd->inode->ino, fd->inode->num_opened_fds,
193 fd->inode->num_allocated_fds);
194 ret = lte_put_fd(fd->lte, fd);
198 inode_put_fd(fd->inode, fd);
202 /* Remove a dentry; i.e. remove a reference to the inode.
204 * If there are no remaining references to the inode either through detnries or
205 * open file descriptors, the inode is freed.
207 * All lookup table entries referenced by the inode have their reference count
208 * decremented. If a lookup table entry has no open file descriptors and no
209 * references remaining, it is freed, and the staging file is unlinked.
211 * Otherwise, the inode is not removed, but the dentry is unlinked and freed. */
212 static void remove_dentry(struct dentry *dentry,
213 struct lookup_table *lookup_table)
215 struct inode *inode = dentry->inode;
216 struct lookup_table_entry *lte;
219 for (i = 0; i <= inode->num_ads; i++) {
220 lte = inode_stream_lte_resolved(inode, i);
222 lte_decrement_refcnt(lte, lookup_table);
224 unlink_dentry(dentry);
228 /* Remove an alternate data stream from the inode */
229 static void inode_remove_ads(struct inode *inode, u16 idx,
230 struct lookup_table *lookup_table)
232 struct ads_entry *ads_entry;
233 struct lookup_table_entry *lte;
235 ads_entry = inode->ads_entries[idx];
237 wimlib_assert(ads_entry);
239 lte = ads_entry->lte;
242 lte_decrement_refcnt(lte, lookup_table);
244 free_ads_entry(ads_entry);
246 wimlib_assert(inode->num_ads);
247 memcpy(&inode->ads_entries[idx],
248 &inode->ads_entries[idx + 1],
249 (inode->num_ads - idx - 1) * sizeof(inode->ads_entries[0]));
253 /* Transfers file attributes from a struct inode to a `stat' buffer.
255 * The lookup table entry tells us which stream in the inode we are statting.
256 * For a named data stream, everything returned is the same as the unnamed data
257 * stream except possibly the size and block count. */
258 int inode_to_stbuf(const struct inode *inode, struct lookup_table_entry *lte,
261 if (inode_is_symlink(inode))
262 stbuf->st_mode = S_IFLNK | 0777;
263 else if (inode_is_directory(inode))
264 stbuf->st_mode = S_IFDIR | 0755;
266 stbuf->st_mode = S_IFREG | 0644;
268 stbuf->st_ino = (ino_t)inode->ino;
269 stbuf->st_nlink = inode->link_count;
270 stbuf->st_uid = getuid();
271 stbuf->st_gid = getgid();
274 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
275 wimlib_assert(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE);
276 wimlib_assert(lte->staging_file_name);
277 struct stat native_stat;
278 if (stat(lte->staging_file_name, &native_stat) != 0) {
279 DEBUG("Failed to stat `%s': %m",
280 lte->staging_file_name);
283 stbuf->st_size = native_stat.st_size;
285 stbuf->st_size = lte->resource_entry.original_size;
291 stbuf->st_atime = wim_timestamp_to_unix(inode->last_access_time);
292 stbuf->st_mtime = wim_timestamp_to_unix(inode->last_write_time);
293 stbuf->st_ctime = wim_timestamp_to_unix(inode->creation_time);
294 stbuf->st_blocks = (stbuf->st_size + 511) / 512;
298 /* Creates a new staging file and returns its file descriptor opened for
301 * @name_ret: A location into which the a pointer to the newly allocated name of
302 * the staging file is stored.
303 * @return: The file descriptor for the new file. Returns -1 and sets errno on
304 * error, for any reason possible from the creat() function.
306 static int create_staging_file(char **name_ret, int open_flags)
314 name_len = staging_dir_name_len + 1 + SHA1_HASH_SIZE;
315 name = MALLOC(name_len + 1);
323 memcpy(name, staging_dir_name, staging_dir_name_len);
324 name[staging_dir_name_len] = '/';
325 randomize_char_array_with_alnum(name + staging_dir_name_len + 1,
327 name[name_len] = '\0';
330 /* Just in case, verify that the randomly generated name doesn't name an
331 * existing file, and try again if so */
332 } while (stat(name, &stbuf) == 0);
338 /* doesn't exist--- ok */
340 DEBUG("Creating staging file `%s'", name);
342 fd = open(name, open_flags | O_CREAT | O_TRUNC, 0600);
354 * Extract a WIM resource to the staging directory.
356 * @inode: Inode that contains the stream we are extracting
358 * @stream_id: Identifier for the stream (it stays constant even if the indices
359 * of the stream entries are changed)
361 * @lte: Pointer to pointer to the lookup table entry for the stream we need to
362 * extract, or NULL if there was no lookup table entry present for the stream
364 * @size: Number of bytes of the stream we want to extract (this supports the
365 * wimfs_truncate() function).
367 static int extract_resource_to_staging_dir(struct inode *inode,
369 struct lookup_table_entry **lte,
372 char *staging_file_name;
375 struct lookup_table_entry *old_lte, *new_lte;
378 fd = create_staging_file(&staging_file_name, O_WRONLY);
383 ret = extract_wim_resource_to_fd(old_lte, fd, size);
386 if (ret != 0 || close(fd) != 0) {
392 goto out_delete_staging_file;
395 if (old_lte && inode->link_count == old_lte->refcnt) {
396 /* The reference count of the existing lookup table
397 * entry is the same as the link count of the inode that
398 * contains the stream we're opening. Therefore, ALL
399 * the references to the lookup table entry correspond
400 * to the stream we're trying to extract, so the lookup
401 * table entry can be re-used. */
402 DEBUG("Re-using lookup table entry");
403 lookup_table_unlink(w->lookup_table, old_lte);
407 /* There's an existing lookup table entry, but its
408 * reference count is creater than the link count for
409 * the inode containing a stream we're opening.
410 * Therefore, we need to split the lookup table entry.
412 wimlib_assert(old_lte->refcnt > inode->link_count);
413 DEBUG("Splitting lookup table entry "
414 "(inode->link_count = %zu, old_lte->refcnt = %u)",
415 inode->link_count, old_lte->refcnt);
419 new_lte = new_lookup_table_entry();
422 goto out_delete_staging_file;
425 /* There may already be open file descriptors to this stream if
426 * it's previously been opened read-only, but just now we're
427 * opening it read-write. Identify those file descriptors and
428 * change their lookup table entry pointers to point to the new
429 * lookup table entry, and open staging file descriptors for
432 * At the same time, we need to count the number of these opened
433 * file descriptors to the new lookup table entry. If there's
434 * an old lookup table entry, this number needs to be subtracted
435 * from the fd's opened to the old entry. */
436 for (u16 i = 0, j = 0; j < inode->num_opened_fds; i++) {
437 struct wimlib_fd *fd = inode->fds[i];
439 if (fd->stream_id == stream_id) {
440 wimlib_assert(fd->lte == old_lte);
442 new_lte->num_opened_fds++;
443 fd->staging_fd = open(staging_file_name, O_RDONLY);
444 if (fd->staging_fd == -1) {
446 goto out_revert_fd_changes;
452 DEBUG("%zu fd's were already opened to the file we extracted",
453 new_lte->num_opened_fds);
455 old_lte->num_opened_fds -= new_lte->num_opened_fds;
456 old_lte->refcnt -= inode->link_count;
460 new_lte->resource_entry.original_size = size;
461 new_lte->refcnt = inode->link_count;
462 random_hash(new_lte->hash);
463 new_lte->staging_file_name = staging_file_name;
464 new_lte->resource_location = RESOURCE_IN_STAGING_FILE;
467 inode->lte = new_lte;
469 for (u16 i = 0; i < inode->num_ads; i++)
470 if (inode->ads_entries[i]->stream_id == stream_id)
471 inode->ads_entries[i]->lte = new_lte;
473 lookup_table_insert(w->lookup_table, new_lte);
474 list_add(&new_lte->staging_list, &staging_list);
477 out_revert_fd_changes:
478 for (u16 i = 0, j = 0; j < new_lte->num_opened_fds; i++) {
479 struct wimlib_fd *fd = inode->fds[i];
480 if (fd && fd->stream_id == stream_id && fd->lte == new_lte) {
482 if (fd->staging_fd != -1) {
483 close(fd->staging_fd);
490 free_lookup_table_entry(new_lte);
491 out_delete_staging_file:
492 unlink(staging_file_name);
493 FREE(staging_file_name);
498 * Creates a randomly named staging directory and returns its name into the
499 * static variable staging_dir_name.
501 * If the staging directory cannot be created, staging_dir_name is set to NULL.
503 static void make_staging_dir()
505 /* XXX Give the user an option of where to stage files */
507 static char prefix[] = "wimlib-staging-";
508 static const size_t prefix_len = 15;
509 static const size_t suffix_len = 10;
511 size_t pwd_len = strlen(working_directory);
513 staging_dir_name_len = pwd_len + 1 + prefix_len + suffix_len;
515 staging_dir_name = MALLOC(staging_dir_name_len + 1);
516 if (!staging_dir_name) {
517 ERROR("Out of memory");
521 memcpy(staging_dir_name, working_directory, pwd_len);
522 staging_dir_name[pwd_len] = '/';
523 memcpy(staging_dir_name + pwd_len + 1, prefix, prefix_len);
524 randomize_char_array_with_alnum(staging_dir_name + pwd_len + 1 + prefix_len,
526 staging_dir_name[staging_dir_name_len] = '\0';
528 if (mkdir(staging_dir_name, 0700) != 0) {
529 ERROR_WITH_ERRNO("Failed to create temporary directory `%s'",
531 FREE(staging_dir_name);
532 staging_dir_name = NULL;
536 static int remove_file_or_directory(const char *fpath, const struct stat *sb,
537 int typeflag, struct FTW *ftwbuf)
539 if (remove(fpath) == 0)
542 return WIMLIB_ERR_DELETE_STAGING_DIR;
547 * Deletes the staging directory and all the files contained in it.
549 static int delete_staging_dir()
553 ret = nftw(staging_dir_name, remove_file_or_directory,10, FTW_DEPTH);
554 staging_dir_name = NULL;
558 /* Name and message queue descriptors for message queues between the filesystem
559 * daemon process and the unmount process. These are used when the filesystem
560 * is unmounted and the process running wimlib_mount() (i.e. the `imagex
561 * unmount' command) needs to communicate with the filesystem daemon running
562 * fuse_main() (i.e. that spawned by the `imagex mount' or `imagex mountrw'
564 static char *unmount_to_daemon_mq_name;
565 static char *daemon_to_unmount_mq_name;
566 static mqd_t unmount_to_daemon_mq;
567 static mqd_t daemon_to_unmount_mq;
569 /* Simple function that returns the concatenation of 4 strings. */
570 static char *strcat_dup(const char *s1, const char *s2, const char *s3,
573 size_t len = strlen(s1) + strlen(s2) + strlen(s3) + strlen(s4) + 1;
574 char *p = MALLOC(len);
580 return strcat(p, s4);
583 /* Removes trailing forward slashes in a string. */
584 static void remove_trailing_slashes(char *s)
586 long len = strlen(s);
587 for (long i = len - 1; i >= 1; i--) {
595 /* Changes forward slashes to underscores in a string. */
596 static void s_slashes_underscores_g(char *s)
606 * Opens two POSIX message queue: one for sending messages from the unmount
607 * process to the daemon process, and one to go the other way. The names of the
608 * message queues, which must be system-wide unique, are be based on the mount
609 * point. (There of course is still a possibility of a collision if one were to
610 * unmount two identically named directories simultaneously...)
612 * @daemon specifies whether the calling process is the filesystem daemon or the
615 static int open_message_queues(bool daemon)
617 static const char *slash = "/";
618 static const char *prefix = "wimlib-";
619 static const char *u2d_suffix = "unmount-to-daemon-mq";
620 static const char *d2u_suffix = "daemon-to-unmount-mq";
622 const char *mount_dir_basename = path_basename(mount_dir);
626 unmount_to_daemon_mq_name = strcat_dup(slash, mount_dir_basename,
628 if (!unmount_to_daemon_mq_name) {
629 ERROR("Out of memory");
630 return WIMLIB_ERR_NOMEM;
632 daemon_to_unmount_mq_name = strcat_dup(slash, mount_dir_basename,
634 if (!daemon_to_unmount_mq_name) {
635 ERROR("Out of memory");
636 ret = WIMLIB_ERR_NOMEM;
640 remove_trailing_slashes(unmount_to_daemon_mq_name);
641 remove_trailing_slashes(daemon_to_unmount_mq_name);
642 s_slashes_underscores_g(unmount_to_daemon_mq_name + 1);
643 s_slashes_underscores_g(daemon_to_unmount_mq_name + 1);
646 flags = O_RDONLY | O_CREAT;
648 flags = O_WRONLY | O_CREAT;
650 unmount_to_daemon_mq = mq_open(unmount_to_daemon_mq_name, flags,
653 if (unmount_to_daemon_mq == (mqd_t)-1) {
654 ERROR_WITH_ERRNO("mq_open()");
655 ret = WIMLIB_ERR_MQUEUE;
660 flags = O_WRONLY | O_CREAT;
662 flags = O_RDONLY | O_CREAT;
664 daemon_to_unmount_mq = mq_open(daemon_to_unmount_mq_name, flags,
667 if (daemon_to_unmount_mq == (mqd_t)-1) {
668 ERROR_WITH_ERRNO("mq_open()");
669 ret = WIMLIB_ERR_MQUEUE;
674 mq_close(unmount_to_daemon_mq);
675 mq_unlink(unmount_to_daemon_mq_name);
677 FREE(daemon_to_unmount_mq_name);
679 FREE(unmount_to_daemon_mq_name);
683 static int mq_get_msgsize(mqd_t mq)
685 static const char *msgsize_max_file = "/proc/sys/fs/mqueue/msgsize_max";
690 if (mq_getattr(unmount_to_daemon_mq, &attr) == 0) {
691 msgsize = attr.mq_msgsize;
693 ERROR_WITH_ERRNO("mq_getattr()");
694 ERROR("Attempting to read %s", msgsize_max_file);
695 fp = fopen(msgsize_max_file, "rb");
697 if (fscanf(fp, "%d", &msgsize) != 1) {
698 ERROR("Assuming message size of 8192");
703 ERROR_WITH_ERRNO("Failed to open the file `%s'",
705 ERROR("Assuming message size of 8192");
712 /* Closes the message queues, which are allocated in static variables */
713 static void close_message_queues()
715 mq_close(unmount_to_daemon_mq);
716 mq_close(daemon_to_unmount_mq);
717 mq_unlink(unmount_to_daemon_mq_name);
718 mq_unlink(daemon_to_unmount_mq_name);
721 static int wimfs_access(const char *path, int mask)
723 /* XXX Permissions not implemented */
728 /* Closes the staging file descriptor associated with the lookup table entry, if
730 static int close_lte_fds(struct lookup_table_entry *lte)
732 for (u16 i = 0, j = 0; j < lte->num_opened_fds; i++) {
733 if (lte->fds[i] && lte->fds[i]->staging_fd != -1) {
734 wimlib_assert(lte->resource_location == RESOURCE_IN_STAGING_FILE);
735 wimlib_assert(lte->staging_file_name);
736 if (close(lte->fds[i]->staging_fd) != 0) {
737 ERROR_WITH_ERRNO("Failed close file `%s'",
738 lte->staging_file_name);
739 return WIMLIB_ERR_WRITE;
747 static void lte_list_change_lte_ptr(struct lookup_table_entry *lte,
748 struct lookup_table_entry *newptr)
750 struct list_head *pos;
751 struct stream_list_head *head;
752 list_for_each(pos, <e->lte_group_list) {
753 head = container_of(pos, struct stream_list_head, list);
754 if (head->type == STREAM_TYPE_ADS) {
755 struct ads_entry *ads_entry;
756 ads_entry = container_of(head, struct ads_entry, lte_group_list);
758 ads_entry->lte = newptr;
760 wimlib_assert(head->type == STREAM_TYPE_NORMAL);
762 struct dentry *dentry;
763 dentry = container_of(head, struct dentry, lte_group_list);
765 dentry->lte = newptr;
772 static int update_lte_of_staging_file(struct lookup_table_entry *lte,
773 struct lookup_table *table)
775 struct lookup_table_entry *duplicate_lte;
777 u8 hash[SHA1_HASH_SIZE];
780 wimlib_assert(lte->resource_location == RESOURCE_IN_STAGING_FILE);
781 wimlib_assert(lte->staging_file_name);
783 ret = sha1sum(lte->staging_file_name, hash);
787 lookup_table_unlink(table, lte);
789 duplicate_lte = __lookup_resource(table, hash);
792 /* Merge duplicate lookup table entries */
795 lte_list_change_lte_ptr(lte, duplicate_lte);
797 duplicate_lte->refcnt += lte->refcnt;
799 list_splice(<e->lte_group_list,
800 &duplicate_lte->lte_group_list);
803 free_lookup_table_entry(lte);
805 if (stat(lte->staging_file_name, &stbuf) != 0) {
806 ERROR_WITH_ERRNO("Failed to stat `%s'", lte->staging_file_name);
807 return WIMLIB_ERR_STAT;
809 wimlib_assert(<e->file_on_disk == <e->staging_file_name);
810 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
811 copy_hash(lte->hash, hash);
812 lte->resource_entry.original_size = stbuf.st_size;
813 lte->resource_entry.size = stbuf.st_size;
814 lookup_table_insert(table, lte);
820 /* Overwrites the WIM file, with changes saved. */
821 static int rebuild_wim(WIMStruct *w, bool check_integrity)
824 struct lookup_table_entry *lte, *tmp;
826 /* Close all the staging file descriptors. */
828 DEBUG("Closing all staging file descriptors.");
829 list_for_each_entry(lte, &staging_list, staging_list) {
830 ret = close_lte_fds(lte);
836 /* Calculate SHA1 checksums for all staging files, and merge unnecessary
837 * lookup table entries. */
838 DEBUG("Calculating SHA1 checksums for all new staging files.");
839 list_for_each_entry_safe(lte, tmp, &staging_list, staging_list) {
840 ret = update_lte_of_staging_file(lte, w->lookup_table);
845 xml_update_image_info(w, w->current_image);
847 ret = wimlib_overwrite(w, check_integrity);
849 ERROR("Failed to commit changes");
855 /* Called when the filesystem is unmounted. */
856 static void wimfs_destroy(void *p)
858 /* For read-write mounts, the `imagex unmount' command, which is
859 * running in a separate process and is executing the
860 * wimlib_unmount() function, will send this process a byte
861 * through a message queue that indicates whether the --commit
862 * option was specified or not. */
865 struct timespec timeout;
867 ssize_t bytes_received;
870 char check_integrity;
873 ret = open_message_queues(true);
877 msgsize = mq_get_msgsize(unmount_to_daemon_mq);
882 /* Wait at most 3 seconds before giving up and discarding changes. */
883 gettimeofday(&now, NULL);
884 timeout.tv_sec = now.tv_sec + 3;
885 timeout.tv_nsec = now.tv_usec * 1000;
886 DEBUG("Waiting for message telling us whether to commit or not, and "
887 "whether to include integrity checks.");
889 bytes_received = mq_timedreceive(unmount_to_daemon_mq, msg,
890 msgsize, NULL, &timeout);
892 check_integrity = msg[1];
893 if (bytes_received == -1) {
894 if (errno == ETIMEDOUT) {
897 ERROR_WITH_ERRNO("mq_timedreceive()");
899 ERROR("Not committing.");
901 DEBUG("Received message: [%d %d]", msg[0], msg[1]);
905 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
907 status = chdir(working_directory);
909 ERROR_WITH_ERRNO("chdir()");
910 status = WIMLIB_ERR_NOTDIR;
913 status = rebuild_wim(w, (check_integrity != 0));
915 ret = delete_staging_dir();
917 ERROR_WITH_ERRNO("Failed to delete the staging "
923 DEBUG("Read-only mount");
926 DEBUG("Sending status %u", status);
927 ret = mq_send(daemon_to_unmount_mq, &status, 1, 1);
929 ERROR_WITH_ERRNO("Failed to send status to unmount process");
930 close_message_queues();
934 static int wimfs_fallocate(const char *path, int mode,
935 off_t offset, off_t len, struct fuse_file_info *fi)
937 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
938 wimlib_assert(fd->staging_fd != -1);
939 return fallocate(fd->staging_fd, mode, offset, len);
942 static int wimfs_fgetattr(const char *path, struct stat *stbuf,
943 struct fuse_file_info *fi)
945 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
946 return dentry_to_stbuf(fd->dentry, stbuf);
950 static int wimfs_ftruncate(const char *path, off_t size,
951 struct fuse_file_info *fi)
953 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
954 int ret = ftruncate(fd->staging_fd, size);
957 fd->lte->resource_entry.original_size = size;
962 * Fills in a `struct stat' that corresponds to a file or directory in the WIM.
964 static int wimfs_getattr(const char *path, struct stat *stbuf)
966 struct dentry *dentry;
967 struct lookup_table_entry *lte;
970 ret = lookup_resource(w, path,
971 get_lookup_flags() | LOOKUP_FLAG_DIRECTORY_OK,
972 &dentry, <e, NULL);
975 return inode_to_stbuf(dentry->inode, lte, stbuf);
979 /* Read an alternate data stream through the XATTR interface, or get its size */
980 static int wimfs_getxattr(const char *path, const char *name, char *value,
984 struct dentry *dentry;
985 struct ads_entry *ads_entry;
987 struct lookup_table_entry *lte;
989 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
992 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
996 dentry = get_dentry(w, path);
999 ads_entry = inode_get_ads_entry(dentry->inode, name, NULL);
1003 lte = ads_entry->lte;
1004 res_size = wim_resource_size(lte);
1008 if (res_size > size)
1010 ret = read_full_wim_resource(lte, (u8*)value);
1017 /* Create a hard link */
1018 static int wimfs_link(const char *to, const char *from)
1020 struct dentry *to_dentry, *from_dentry, *from_dentry_parent;
1021 const char *link_name;
1022 struct inode *inode;
1024 struct lookup_table_entry *lte;
1026 to_dentry = get_dentry(w, to);
1030 inode = to_dentry->inode;
1032 if (!inode_is_regular_file(inode))
1035 from_dentry_parent = get_parent_dentry(w, from);
1036 if (!from_dentry_parent)
1038 if (!dentry_is_directory(from_dentry_parent))
1041 link_name = path_basename(from);
1042 if (get_dentry_child_with_name(from_dentry_parent, link_name))
1044 from_dentry = new_dentry(link_name);
1049 inode_add_dentry(from_dentry, inode);
1050 from_dentry->inode = inode;
1051 inode->link_count++;
1053 for (i = 0; i <= inode->num_ads; i++) {
1054 lte = inode_stream_lte_resolved(inode, i);
1059 link_dentry(from_dentry, from_dentry_parent);
1064 static int wimfs_listxattr(const char *path, char *list, size_t size)
1066 struct dentry *dentry;
1071 struct inode *inode;
1072 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1075 /* List alternate data streams, or get the list size */
1077 ret = lookup_resource(w, path, get_lookup_flags(), &dentry, NULL, NULL);
1080 inode = dentry->inode;
1084 for (i = 0; i < inode->num_ads; i++)
1085 needed_size += inode->ads_entries[i]->stream_name_utf8_len + 6;
1088 for (i = 0; i < inode->num_ads; i++) {
1089 needed_size = inode->ads_entries[i]->stream_name_utf8_len + 6;
1090 if (needed_size > size)
1092 p += sprintf(p, "user.%s",
1093 inode->ads_entries[i]->stream_name_utf8) + 1;
1094 size -= needed_size;
1102 * Create a directory in the WIM.
1103 * @mode is currently ignored.
1105 static int wimfs_mkdir(const char *path, mode_t mode)
1107 struct dentry *parent;
1108 struct dentry *newdir;
1109 const char *basename;
1111 parent = get_parent_dentry(w, path);
1115 if (!dentry_is_directory(parent))
1118 basename = path_basename(path);
1119 if (get_dentry_child_with_name(parent, basename))
1122 newdir = new_dentry_with_inode(basename);
1123 newdir->inode->attributes |= FILE_ATTRIBUTE_DIRECTORY;
1124 newdir->inode->resolved = true;
1125 newdir->inode->ino = next_ino++;
1126 link_dentry(newdir, parent);
1131 /* Creates a regular file. */
1132 static int wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
1134 const char *stream_name;
1135 if ((mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
1136 && (stream_name = path_stream_name(path))) {
1137 /* Make an alternate data stream */
1138 struct ads_entry *new_entry;
1139 struct dentry *dentry;
1141 char *p = (char*)stream_name - 1;
1142 wimlib_assert(*p == ':');
1145 dentry = get_dentry(w, path);
1146 if (!dentry || !dentry_is_regular_file(dentry))
1148 if (inode_get_ads_entry(dentry->inode, stream_name, NULL))
1150 new_entry = inode_add_ads(dentry->inode, stream_name);
1154 struct dentry *dentry, *parent;
1155 const char *basename;
1157 /* Make a normal file (not an alternate data stream) */
1159 /* Make sure that the parent of @path exists and is a directory, and
1160 * that the dentry named by @path does not already exist. */
1161 parent = get_parent_dentry(w, path);
1164 if (!dentry_is_directory(parent))
1167 basename = path_basename(path);
1168 if (get_dentry_child_with_name(parent, path))
1171 dentry = new_dentry_with_inode(basename);
1174 dentry->inode->resolved = true;
1175 dentry->inode->ino = next_ino++;
1176 link_dentry(dentry, parent);
1183 static int wimfs_open(const char *path, struct fuse_file_info *fi)
1185 struct dentry *dentry;
1186 struct lookup_table_entry *lte;
1188 struct wimlib_fd *fd;
1189 struct inode *inode;
1193 ret = lookup_resource(w, path, get_lookup_flags(), &dentry, <e,
1198 inode = dentry->inode;
1200 if (stream_idx == 0)
1203 stream_id = inode->ads_entries[stream_idx - 1]->stream_id;
1205 /* The file resource may be in the staging directory (read-write mounts
1206 * only) or in the WIM. If it's in the staging directory, we need to
1207 * open a native file descriptor for the corresponding file. Otherwise,
1208 * we can read the file resource directly from the WIM file if we are
1209 * opening it read-only, but we need to extract the resource to the
1210 * staging directory if we are opening it writable. */
1211 if (flags_writable(fi->flags) &&
1212 (!lte || lte->resource_location != RESOURCE_IN_STAGING_FILE)) {
1213 u64 size = (lte) ? wim_resource_size(lte) : 0;
1214 ret = extract_resource_to_staging_dir(inode, stream_id,
1220 ret = alloc_wimlib_fd(inode, stream_id, lte, &fd);
1224 if (lte && lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1225 fd->staging_fd = open(lte->staging_file_name, fi->flags);
1226 if (fd->staging_fd == -1) {
1227 close_wimlib_fd(fd);
1231 fi->fh = (uintptr_t)fd;
1235 /* Opens a directory. */
1236 static int wimfs_opendir(const char *path, struct fuse_file_info *fi)
1238 struct inode *inode;
1240 struct wimlib_fd *fd = NULL;
1242 inode = wim_pathname_to_inode(w, path);
1245 if (!inode_is_directory(inode))
1247 ret = alloc_wimlib_fd(inode, 0, NULL, &fd);
1248 fi->fh = (uintptr_t)fd;
1254 * Read data from a file in the WIM or in the staging directory.
1256 static int wimfs_read(const char *path, char *buf, size_t size,
1257 off_t offset, struct fuse_file_info *fi)
1259 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1264 if (!fd->lte) /* Empty stream with no lookup table entry */
1267 if (fd->lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1268 /* Read from staging file */
1270 wimlib_assert(fd->lte->staging_file_name);
1271 wimlib_assert(fd->staging_fd != -1);
1274 DEBUG("Seek to offset %zu", offset);
1276 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1278 ret = read(fd->staging_fd, buf, size);
1285 u64 res_size = wim_resource_size(fd->lte);
1287 if (offset > res_size)
1290 size = min(size, res_size - offset);
1292 if (read_wim_resource(fd->lte, (u8*)buf,
1293 size, offset, false) != 0)
1299 /* Fills in the entries of the directory specified by @path using the
1300 * FUSE-provided function @filler. */
1301 static int wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
1302 off_t offset, struct fuse_file_info *fi)
1304 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1305 struct inode *inode;
1306 struct dentry *child;
1313 filler(buf, ".", NULL, 0);
1314 filler(buf, "..", NULL, 0);
1316 child = inode->children;
1322 if (filler(buf, child->file_name_utf8, NULL, 0))
1324 child = child->next;
1325 } while (child != inode->children);
1330 static int wimfs_readlink(const char *path, char *buf, size_t buf_len)
1332 struct dentry *dentry = get_dentry(w, path);
1336 if (!dentry_is_symlink(dentry))
1339 ret = inode_readlink(dentry->inode, buf, buf_len, w);
1346 static int wimfs_release(const char *path, struct fuse_file_info *fi)
1348 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1350 return close_wimlib_fd(fd);
1353 static int wimfs_releasedir(const char *path, struct fuse_file_info *fi)
1355 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1357 return close_wimlib_fd(fd);
1361 /* Remove an alternate data stream through the XATTR interface */
1362 static int wimfs_removexattr(const char *path, const char *name)
1364 struct dentry *dentry;
1365 struct ads_entry *ads_entry;
1367 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1370 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1374 dentry = get_dentry(w, path);
1378 ads_entry = inode_get_ads_entry(dentry->inode, name, &ads_idx);
1381 inode_remove_ads(dentry->inode, ads_idx, w->lookup_table);
1386 /* Renames a file or directory. See rename (3) */
1387 static int wimfs_rename(const char *from, const char *to)
1391 struct dentry *parent_of_dst;
1392 char *file_name_utf16 = NULL, *file_name_utf8 = NULL;
1393 u16 file_name_utf16_len, file_name_utf8_len;
1396 /* This rename() implementation currently only supports actual files
1397 * (not alternate data streams) */
1399 src = get_dentry(w, from);
1403 dst = get_dentry(w, to);
1406 ret = get_names(&file_name_utf16, &file_name_utf8,
1407 &file_name_utf16_len, &file_name_utf8_len,
1413 /* Destination file exists */
1415 if (src == dst) /* Same file */
1418 if (!dentry_is_directory(src)) {
1419 /* Cannot rename non-directory to directory. */
1420 if (dentry_is_directory(dst))
1423 /* Cannot rename directory to a non-directory or a non-empty
1425 if (!dentry_is_directory(dst))
1427 if (dst->inode->children != NULL)
1430 parent_of_dst = dst->parent;
1431 remove_dentry(dst, w->lookup_table);
1433 /* Destination does not exist */
1434 parent_of_dst = get_parent_dentry(w, to);
1438 if (!dentry_is_directory(parent_of_dst))
1442 FREE(src->file_name);
1443 FREE(src->file_name_utf8);
1444 src->file_name = file_name_utf16;
1445 src->file_name_utf8 = file_name_utf8;
1446 src->file_name_len = file_name_utf16_len;
1447 src->file_name_utf8_len = file_name_utf8_len;
1450 link_dentry(src, parent_of_dst);
1454 /* Remove a directory */
1455 static int wimfs_rmdir(const char *path)
1457 struct dentry *dentry;
1459 dentry = get_dentry(w, path);
1463 if (!dentry_is_empty_directory(dentry))
1466 remove_dentry(dentry, w->lookup_table);
1471 /* Write an alternate data stream through the XATTR interface */
1472 static int wimfs_setxattr(const char *path, const char *name,
1473 const char *value, size_t size, int flags)
1475 struct ads_entry *existing_ads_entry;
1476 struct ads_entry *new_ads_entry;
1477 struct lookup_table_entry *existing_lte;
1478 struct lookup_table_entry *lte;
1479 struct inode *inode;
1480 u8 value_hash[SHA1_HASH_SIZE];
1483 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1486 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1490 inode = wim_pathname_to_inode(w, path);
1494 existing_ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
1495 if (existing_ads_entry) {
1496 if (flags & XATTR_CREATE)
1498 inode_remove_ads(inode, ads_idx, w->lookup_table);
1500 if (flags & XATTR_REPLACE)
1503 new_ads_entry = inode_add_ads(inode, name);
1507 sha1_buffer((const u8*)value, size, value_hash);
1509 existing_lte = __lookup_resource(w->lookup_table, value_hash);
1516 lte = new_lookup_table_entry();
1519 value_copy = MALLOC(size);
1524 memcpy(value_copy, value, size);
1525 lte->resource_location = RESOURCE_IN_ATTACHED_BUFFER;
1526 lte->attached_buffer = value_copy;
1527 lte->resource_entry.original_size = size;
1528 lte->resource_entry.size = size;
1529 lte->resource_entry.flags = 0;
1530 copy_hash(lte->hash, value_hash);
1531 lookup_table_insert(w->lookup_table, lte);
1533 new_ads_entry->lte = lte;
1538 static int wimfs_symlink(const char *to, const char *from)
1540 struct dentry *dentry_parent, *dentry;
1541 const char *link_name;
1542 struct inode *inode;
1544 dentry_parent = get_parent_dentry(w, from);
1547 if (!dentry_is_directory(dentry_parent))
1550 link_name = path_basename(from);
1552 if (get_dentry_child_with_name(dentry_parent, link_name))
1554 dentry = new_dentry_with_inode(link_name);
1557 inode = dentry->inode;
1559 inode->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
1560 inode->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
1561 inode->ino = next_ino++;
1563 if (inode_set_symlink(inode, to, w->lookup_table, NULL) != 0)
1564 goto out_free_dentry;
1566 link_dentry(dentry, dentry_parent);
1569 free_dentry(dentry);
1574 /* Reduce the size of a file */
1575 static int wimfs_truncate(const char *path, off_t size)
1577 struct dentry *dentry;
1578 struct lookup_table_entry *lte;
1582 struct inode *inode;
1584 ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1590 if (!lte) /* Already a zero-length file */
1593 inode = dentry->inode;
1595 if (stream_idx == 0)
1598 stream_id = inode->ads_entries[stream_idx - 1]->stream_id;
1600 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1601 wimlib_assert(lte->staging_file_name);
1602 ret = truncate(lte->staging_file_name, size);
1605 lte->resource_entry.original_size = size;
1607 wimlib_assert(lte->resource_location == RESOURCE_IN_WIM);
1608 /* File in WIM. Extract it to the staging directory, but only
1609 * the first @size bytes of it. */
1610 ret = extract_resource_to_staging_dir(inode, stream_id,
1616 /* Remove a regular file */
1617 static int wimfs_unlink(const char *path)
1619 struct dentry *dentry;
1620 struct lookup_table_entry *lte;
1621 struct inode *inode;
1626 ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1632 if (stream_idx == 0)
1633 remove_dentry(dentry, w->lookup_table);
1635 inode_remove_ads(dentry->inode, stream_idx - 1, w->lookup_table);
1639 #ifdef HAVE_UTIMENSAT
1641 * Change the timestamp on a file dentry.
1643 * Note that alternate data streams do not have their own timestamps.
1645 static int wimfs_utimens(const char *path, const struct timespec tv[2])
1647 struct dentry *dentry;
1648 struct inode *inode;
1649 dentry = get_dentry(w, path);
1652 inode = dentry->inode;
1654 if (tv[0].tv_nsec != UTIME_OMIT) {
1655 if (tv[0].tv_nsec == UTIME_NOW)
1656 inode->last_access_time = get_wim_timestamp();
1658 inode->last_access_time = timespec_to_wim_timestamp(&tv[0]);
1660 if (tv[1].tv_nsec != UTIME_OMIT) {
1661 if (tv[1].tv_nsec == UTIME_NOW)
1662 inode->last_write_time = get_wim_timestamp();
1664 inode->last_write_time = timespec_to_wim_timestamp(&tv[1]);
1669 static int wimfs_utime(const char *path, struct utimbuf *times)
1671 struct dentry *dentry;
1672 struct inode *inode;
1673 dentry = get_dentry(w, path);
1676 inode = dentry->inode;
1678 inode->last_write_time = unix_timestamp_to_wim(times->modtime);
1679 inode->last_access_time = unix_timestamp_to_wim(times->actime);
1684 /* Writes to a file in the WIM filesystem.
1685 * It may be an alternate data stream, but here we don't even notice because we
1686 * just get a lookup table entry. */
1687 static int wimfs_write(const char *path, const char *buf, size_t size,
1688 off_t offset, struct fuse_file_info *fi)
1690 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1697 wimlib_assert(fd->lte);
1698 wimlib_assert(fd->lte->staging_file_name);
1699 wimlib_assert(fd->staging_fd != -1);
1700 wimlib_assert(fd->inode);
1702 /* Seek to the requested position */
1703 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1706 /* Write the data. */
1707 ret = write(fd->staging_fd, buf, size);
1711 now = get_wim_timestamp();
1712 fd->inode->last_write_time = now;
1713 fd->inode->last_access_time = now;
1717 static struct fuse_operations wimfs_operations = {
1718 .access = wimfs_access,
1719 .destroy = wimfs_destroy,
1721 .fallocate = wimfs_fallocate,
1722 .fgetattr = wimfs_fgetattr,
1724 .ftruncate = wimfs_ftruncate,
1725 .getattr = wimfs_getattr,
1727 .getxattr = wimfs_getxattr,
1731 .listxattr = wimfs_listxattr,
1733 .mkdir = wimfs_mkdir,
1734 .mknod = wimfs_mknod,
1736 .opendir = wimfs_opendir,
1738 .readdir = wimfs_readdir,
1739 .readlink = wimfs_readlink,
1740 .release = wimfs_release,
1741 .releasedir = wimfs_releasedir,
1743 .removexattr = wimfs_removexattr,
1745 .rename = wimfs_rename,
1746 .rmdir = wimfs_rmdir,
1748 .setxattr = wimfs_setxattr,
1750 .symlink = wimfs_symlink,
1751 .truncate = wimfs_truncate,
1752 .unlink = wimfs_unlink,
1753 #ifdef HAVE_UTIMENSAT
1754 .utimens = wimfs_utimens,
1756 .utime = wimfs_utime,
1758 .write = wimfs_write,
1762 /* Mounts a WIM file. */
1763 WIMLIBAPI int wimlib_mount(WIMStruct *wim, int image, const char *dir,
1764 int flags, WIMStruct **additional_swms,
1765 unsigned num_additional_swms)
1771 struct lookup_table *joined_tab, *wim_tab_save;
1772 struct image_metadata *imd;
1774 DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
1775 wim, image, dir, flags);
1778 return WIMLIB_ERR_INVALID_PARAM;
1780 ret = verify_swm_set(wim, additional_swms, num_additional_swms);
1784 if (num_additional_swms) {
1785 ret = new_joined_lookup_table(wim, additional_swms,
1786 num_additional_swms,
1790 wim_tab_save = wim->lookup_table;
1791 wim->lookup_table = joined_tab;
1794 ret = wimlib_select_image(wim, image);
1799 imd = &wim->image_metadata[image - 1];
1801 DEBUG("Selected image %d", image);
1803 next_ino = assign_inode_numbers(&imd->inode_list);
1805 DEBUG("(next_ino = %"PRIu64")", next_ino);
1807 /* Resolve all the lookup table entries of the dentry tree */
1808 DEBUG("Resolving lookup table entries");
1809 for_dentry_in_tree(imd->root_dentry, dentry_resolve_ltes,
1812 if (flags & WIMLIB_MOUNT_FLAG_READWRITE)
1813 imd->modified = true;
1815 if (!(flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
1816 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
1817 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
1818 flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1820 DEBUG("Getting current directory");
1823 working_directory = getcwd(NULL, 0);
1824 if (!working_directory) {
1825 ERROR_WITH_ERRNO("Could not determine current directory");
1826 ret = WIMLIB_ERR_NOTDIR;
1830 DEBUG("Closing POSIX message queues");
1831 /* XXX hack to get rid of the message queues if they already exist for
1832 * some reason (maybe left over from a previous mount that wasn't
1833 * unmounted correctly) */
1834 ret = open_message_queues(true);
1837 close_message_queues();
1839 DEBUG("Preparing arguments to fuse_main()");
1844 ret = WIMLIB_ERR_NOMEM;
1848 argv[argc++] = "imagex";
1850 argv[argc++] = "-s"; /* disable multi-threaded operation */
1852 if (flags & WIMLIB_MOUNT_FLAG_DEBUG)
1853 argv[argc++] = "-d";
1856 * We provide the use_ino option because we are going to assign inode
1857 * numbers oursides. We've already numbered the hard link groups with
1858 * unique numbers with the assign_link_groups() function, and the static
1859 * variable next_link_group_id is set to the next available link group
1860 * ID that we will assign to new dentries.
1862 char optstring[256] = "use_ino";
1863 argv[argc++] = "-o";
1864 argv[argc++] = optstring;
1865 if ((flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
1866 /* Read-write mount. Make the staging directory */
1868 if (!staging_dir_name) {
1870 ret = WIMLIB_ERR_MKDIR;
1874 /* Read-only mount */
1875 strcat(optstring, ",ro");
1877 strcat(optstring, ",subtype=wimfs,attr_timeout=0");
1883 DEBUG("FUSE command line (argc = %d): ", argc);
1884 for (i = 0; i < argc; i++) {
1885 fputs(argv[i], stdout);
1893 /* Set static variables. */
1895 mount_flags = flags;
1897 ret = fuse_main(argc, argv, &wimfs_operations, NULL);
1899 ret = WIMLIB_ERR_FUSE;
1901 if (num_additional_swms) {
1902 free_lookup_table(wim->lookup_table);
1903 wim->lookup_table = wim_tab_save;
1910 * Unmounts the WIM file that was previously mounted on @dir by using
1913 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
1920 struct timespec timeout;
1926 /* Open message queues between the unmount process and the
1927 * filesystem daemon. */
1928 ret = open_message_queues(false);
1932 /* Send a message to the filesystem saying whether to commit or
1934 msg[0] = (flags & WIMLIB_UNMOUNT_FLAG_COMMIT) ? 1 : 0;
1935 msg[1] = (flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY) ? 1 : 0;
1937 DEBUG("Sending message: %s, %s",
1938 (msg[0] == 0) ? "don't commit" : "commit",
1939 (msg[1] == 0) ? "don't check" : "check");
1940 ret = mq_send(unmount_to_daemon_mq, msg, 2, 1);
1942 ERROR("Failed to notify filesystem daemon whether we want to "
1943 "commit changes or not");
1944 close_message_queues();
1945 return WIMLIB_ERR_MQUEUE;
1948 /* Execute `fusermount -u', which is installed setuid root, to unmount
1951 * FUSE does not yet implement synchronous unmounts. This means that
1952 * fusermount -u will return before the filesystem daemon returns from
1953 * wimfs_destroy(). This is partly what we want, because we need to
1954 * send a message from this process to the filesystem daemon telling
1955 * whether --commit was specified or not. However, after that, the
1956 * unmount process must wait for the filesystem daemon to finish writing
1963 ERROR_WITH_ERRNO("Failed to fork()");
1964 return WIMLIB_ERR_FORK;
1967 execlp("fusermount", "fusermount", "-u", dir, NULL);
1968 ERROR_WITH_ERRNO("Failed to execute `fusermount'");
1969 exit(WIMLIB_ERR_FUSERMOUNT);
1972 ret = wait(&status);
1974 ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
1976 return WIMLIB_ERR_FUSERMOUNT;
1980 ERROR("fusermount exited with status %d", status);
1982 /* Try again, but with the `umount' program. This is required
1983 * on other FUSE implementations such as FreeBSD's that do not
1984 * have a `fusermount' program. */
1988 ERROR_WITH_ERRNO("Failed to fork()");
1989 return WIMLIB_ERR_FORK;
1992 execlp("umount", "umount", dir, NULL);
1993 ERROR_WITH_ERRNO("Failed to execute `umount'");
1994 exit(WIMLIB_ERR_FUSERMOUNT);
1997 ret = wait(&status);
1999 ERROR_WITH_ERRNO("Failed to wait for `umount' process to "
2001 return WIMLIB_ERR_FUSERMOUNT;
2004 ERROR("`umount' exited with failure status");
2005 return WIMLIB_ERR_FUSERMOUNT;
2010 /* Wait for a message from the filesytem daemon indicating whether the
2011 * filesystem was unmounted successfully (0) or an error occurred (1).
2012 * This may take a long time if a big WIM file needs to be rewritten. */
2014 /* Wait at most 600??? seconds before giving up and returning false.
2015 * Either it's a really big WIM file, or (more likely) the
2016 * filesystem daemon has crashed or failed for some reason.
2018 * XXX come up with some method to determine if the filesystem
2019 * daemon has really crashed or not.
2021 * XXX Idea: have mount daemon write its PID into the WIM file header?
2024 gettimeofday(&now, NULL);
2025 timeout.tv_sec = now.tv_sec + 600;
2026 timeout.tv_nsec = now.tv_usec * 1000;
2028 msgsize = mq_get_msgsize(daemon_to_unmount_mq);
2029 char mailbox[msgsize];
2032 DEBUG("Waiting for message telling us whether the unmount was "
2033 "successful or not.");
2034 ret = mq_timedreceive(daemon_to_unmount_mq, mailbox, msgsize,
2037 close_message_queues();
2039 if (errno_save == ETIMEDOUT) {
2040 ERROR("Timed out- probably the filesystem daemon "
2041 "crashed and the WIM was not written "
2043 return WIMLIB_ERR_TIMEOUT;
2045 ERROR("mq_receive(): %s", strerror(errno_save));
2046 return WIMLIB_ERR_MQUEUE;
2050 DEBUG("Received message: %s",
2051 (mailbox[0] == 0) ? "Unmount OK" : "Unmount Failed");
2052 if (mailbox[0] != 0)
2053 ERROR("Unmount failed");
2057 #else /* WITH_FUSE */
2060 static inline int mount_unsupported_error()
2062 ERROR("WIMLIB was compiled with --without-fuse, which disables support "
2063 "for mounting WIMs.");
2064 return WIMLIB_ERR_UNSUPPORTED;
2067 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
2069 return mount_unsupported_error();
2072 WIMLIBAPI int wimlib_mount(WIMStruct *wim_p, int image, const char *dir,
2073 int flags, WIMStruct **additional_swms,
2074 unsigned num_additional_swms)
2076 return mount_unsupported_error();
2079 #endif /* WITH_FUSE */