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 if (inode->num_opened_fds == inode->num_allocated_fds) {
112 struct wimlib_fd **fds;
115 if (inode->num_allocated_fds == max_fds)
117 num_new_fds = min(fds_per_alloc, max_fds - inode->num_allocated_fds);
119 fds = REALLOC(inode->fds, (inode->num_allocated_fds + num_new_fds) *
120 sizeof(inode->fds[0]));
123 memset(&fds[inode->num_allocated_fds], 0,
124 num_new_fds * sizeof(fds[0]));
126 inode->num_allocated_fds += num_new_fds;
128 for (u16 i = 0; ; i++) {
129 if (!inode->fds[i]) {
130 struct wimlib_fd *fd = CALLOC(1, sizeof(*fd));
137 fd->stream_id = stream_id;
140 inode->num_opened_fds++;
141 lte->num_opened_fds++;
147 static void inode_put_fd(struct inode *inode, struct wimlib_fd *fd)
150 wimlib_assert(inode);
151 wimlib_assert(fd->inode == inode);
152 wimlib_assert(inode->num_opened_fds);
153 wimlib_assert(fd->idx < inode->num_opened_fds);
154 wimlib_assert(inode->fds[fd->idx] == fd);
156 inode->fds[fd->idx] = NULL;
158 if (--inode->num_opened_fds == 0 && inode->link_count == 0)
162 static int lte_put_fd(struct lookup_table_entry *lte, struct wimlib_fd *fd)
165 wimlib_assert(fd->lte == lte);
167 if (!lte) /* Empty stream with no lookup table entry */
170 wimlib_assert(lte->num_opened_fds);
172 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
173 wimlib_assert(lte->staging_file_name);
174 wimlib_assert(fd->staging_fd != -1);
175 if (close(fd->staging_fd) != 0)
178 lte_decrement_num_opened_fds(lte, w->lookup_table);
182 static int close_wimlib_fd(struct wimlib_fd *fd)
185 ret = lte_put_fd(fd->lte, fd);
189 inode_put_fd(fd->inode, fd);
193 /* Remove a dentry; i.e. remove a reference to the inode.
195 * If there are no remaining references to the inode either through detnries or
196 * open file descriptors, the inode is freed.
198 * All lookup table entries referenced by the inode have their reference count
199 * decremented. If a lookup table entry has no open file descriptors and no
200 * references remaining, it is freed, and the staging file is unlinked.
202 * Otherwise, the inode is not removed, but the dentry is unlinked and freed. */
203 static void remove_dentry(struct dentry *dentry,
204 struct lookup_table *lookup_table)
206 struct inode *inode = dentry->inode;
207 struct lookup_table_entry *lte;
210 for (i = 0; <= inode->num_ads; i++) {
211 lte = inode_stream_lte_resolved(inode, i);
213 lte_decrement_refcnt(lte, lookup_table);
215 unlink_dentry(dentry);
219 /* Remove an alternate data stream from the inode */
220 static void inode_remove_ads(struct inode *inode, u16 idx,
221 struct lookup_table *lookup_table)
223 struct ads_entry *ads_entry;
224 struct lookup_table_entry *lte;
226 ads_entry = inode->ads_entries[idx];
228 wimlib_assert(ads_entry);
230 lte = ads_entry->lte;
233 lte_decrement_refcnt(lte, lookup_table);
235 free_ads_entry(ads_entry);
237 wimlib_assert(inode->num_ads);
238 memcpy(&inode->ads_entries[idx],
239 &inode->ads_entries[idx + 1],
240 (inode->num_ads - idx - 1) * sizeof(inode->ads_entries[0]));
244 /* Transfers file attributes from a struct inode to a `stat' buffer.
246 * The lookup table entry tells us which stream in the inode we are statting.
247 * For a named data stream, everything returned is the same as the unnamed data
248 * stream except possibly the size and block count. */
249 int inode_to_stbuf(const struct inode *inode, struct lookup_table_entry *lte,
252 if (inode_is_symlink(inode))
253 stbuf->st_mode = S_IFLNK | 0777;
254 else if (inode_is_directory(inode))
255 stbuf->st_mode = S_IFDIR | 0755;
257 stbuf->st_mode = S_IFREG | 0644;
259 stbuf->st_ino = (ino_t)inode->ino;
260 stbuf->st_nlink = inode->link_count;
261 stbuf->st_uid = getuid();
262 stbuf->st_gid = getgid();
265 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
266 wimlib_assert(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE);
267 wimlib_assert(lte->staging_file_name);
268 struct stat native_stat;
269 if (stat(lte->staging_file_name, &native_stat) != 0) {
270 DEBUG("Failed to stat `%s': %m",
271 lte->staging_file_name);
274 stbuf->st_size = native_stat.st_size;
276 stbuf->st_size = lte->resource_entry.original_size;
282 stbuf->st_atime = wim_timestamp_to_unix(inode->last_access_time);
283 stbuf->st_mtime = wim_timestamp_to_unix(inode->last_write_time);
284 stbuf->st_ctime = wim_timestamp_to_unix(inode->creation_time);
285 stbuf->st_blocks = (stbuf->st_size + 511) / 512;
289 /* Creates a new staging file and returns its file descriptor opened for
292 * @name_ret: A location into which the a pointer to the newly allocated name of
293 * the staging file is stored.
294 * @return: The file descriptor for the new file. Returns -1 and sets errno on
295 * error, for any reason possible from the creat() function.
297 static int create_staging_file(char **name_ret, int open_flags)
305 name_len = staging_dir_name_len + 1 + SHA1_HASH_SIZE;
306 name = MALLOC(name_len + 1);
314 memcpy(name, staging_dir_name, staging_dir_name_len);
315 name[staging_dir_name_len] = '/';
316 randomize_char_array_with_alnum(name + staging_dir_name_len + 1,
318 name[name_len] = '\0';
321 /* Just in case, verify that the randomly generated name doesn't name an
322 * existing file, and try again if so */
323 } while (stat(name, &stbuf) == 0);
329 /* doesn't exist--- ok */
331 DEBUG("Creating staging file `%s'", name);
333 fd = open(name, open_flags | O_CREAT | O_TRUNC, 0600);
345 * Extract a WIM resource to the staging directory.
347 * @inode: Inode that contains the stream we are extracting
349 * @lte: Pointer to pointer to the lookup table entry for the stream we need to
350 * extract, or NULL if there was no lookup table entry present for the stream
352 * @size: Number of bytes of the stream we want to extract (this supports the
353 * wimfs_truncate() function).
355 static int extract_resource_to_staging_dir(struct inode *inode,
357 struct lookup_table_entry **lte,
360 char *staging_file_name;
363 struct lookup_table_entry *old_lte, *new_lte;
366 fd = create_staging_file(&staging_file_name, O_WRONLY);
371 ret = extract_wim_resource_to_fd(old_lte, fd, size);
374 if (ret != 0 || close(fd) != 0) {
380 goto out_delete_staging_file;
384 wimlib_assert(old_lte->resource_location == RESOURCE_IN_WIM);
385 if (inode->link_count == old_lte->refcnt) {
386 /* The reference count of the existing lookup table
387 * entry is the same as the size of the hard link group
388 * associated with the dentry; therefore, ALL the
389 * references to the lookup table entry correspond to
390 * the stream we're trying to extract. So the lookup
391 * table entry can be re-used.
393 DEBUG("Re-using lookup table entry");
394 lookup_table_unlink(w->lookup_table, old_lte);
397 DEBUG("Splitting lookup table entry "
398 "(inode->link_count = %zu, lte refcnt = %u)",
399 inode->link_count, old_lte->refcnt);
401 wimlib_assert(old_lte->refcnt > inode->link_count);
403 new_lte = new_lookup_table_entry();
406 goto out_delete_staging_file;
408 old_lte->refcnt -= inode->link_count;
410 for (u16 i = 0, j = 0; j < inode->num_opened_fds; i++) {
412 if (inode->fds[i]->stream_id == stream_id) {
413 wimlib_assert(inode->fds[i]->lte == old_lte);
414 inode->fds[i]->lte = new_lte;
421 /* No old_lte was supplied, so the resource had no lookup table
422 * entry before (it must be an empty resource) */
423 wimlib_assert(inode->num_opened_fds == 0);
424 new_lte = new_lookup_table_entry();
427 goto out_delete_staging_file;
430 new_lte->resource_entry.original_size = size;
431 new_lte->refcnt = inode->link_count;
432 random_hash(new_lte->hash);
433 new_lte->staging_file_name = staging_file_name;
434 new_lte->resource_location = RESOURCE_IN_STAGING_FILE;
436 lookup_table_insert(w->lookup_table, new_lte);
437 list_add(&new_lte->staging_list, &staging_list);
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 /* XXX Permissions not implemented */
677 /* Closes the staging file descriptor associated with the lookup table entry, if
679 static int close_lte_fds(struct lookup_table_entry *lte)
681 for (u16 i = 0, j = 0; j < lte->num_opened_fds; i++) {
682 if (lte->fds[i] && lte->fds[i]->staging_fd != -1) {
683 wimlib_assert(lte->resource_location == RESOURCE_IN_STAGING_FILE);
684 wimlib_assert(lte->staging_file_name);
685 if (close(lte->fds[i]->staging_fd) != 0) {
686 ERROR_WITH_ERRNO("Failed close file `%s'",
687 lte->staging_file_name);
688 return WIMLIB_ERR_WRITE;
696 static void lte_list_change_lte_ptr(struct lookup_table_entry *lte,
697 struct lookup_table_entry *newptr)
699 struct list_head *pos;
700 struct stream_list_head *head;
701 list_for_each(pos, <e->lte_group_list) {
702 head = container_of(pos, struct stream_list_head, list);
703 if (head->type == STREAM_TYPE_ADS) {
704 struct ads_entry *ads_entry;
705 ads_entry = container_of(head, struct ads_entry, lte_group_list);
707 ads_entry->lte = newptr;
709 wimlib_assert(head->type == STREAM_TYPE_NORMAL);
711 struct dentry *dentry;
712 dentry = container_of(head, struct dentry, lte_group_list);
714 dentry->lte = newptr;
721 static int update_lte_of_staging_file(struct lookup_table_entry *lte,
722 struct lookup_table *table)
724 struct lookup_table_entry *duplicate_lte;
726 u8 hash[SHA1_HASH_SIZE];
729 wimlib_assert(lte->resource_location == RESOURCE_IN_STAGING_FILE);
730 wimlib_assert(lte->staging_file_name);
732 ret = sha1sum(lte->staging_file_name, hash);
736 lookup_table_unlink(table, lte);
738 duplicate_lte = __lookup_resource(table, hash);
741 /* Merge duplicate lookup table entries */
744 lte_list_change_lte_ptr(lte, duplicate_lte);
746 duplicate_lte->refcnt += lte->refcnt;
748 list_splice(<e->lte_group_list,
749 &duplicate_lte->lte_group_list);
752 free_lookup_table_entry(lte);
754 if (stat(lte->staging_file_name, &stbuf) != 0) {
755 ERROR_WITH_ERRNO("Failed to stat `%s'", lte->staging_file_name);
756 return WIMLIB_ERR_STAT;
758 wimlib_assert(<e->file_on_disk == <e->staging_file_name);
759 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
760 copy_hash(lte->hash, hash);
761 lte->resource_entry.original_size = stbuf.st_size;
762 lte->resource_entry.size = stbuf.st_size;
763 lookup_table_insert(table, lte);
769 /* Overwrites the WIM file, with changes saved. */
770 static int rebuild_wim(WIMStruct *w, bool check_integrity)
773 struct lookup_table_entry *lte, *tmp;
775 /* Close all the staging file descriptors. */
777 DEBUG("Closing all staging file descriptors.");
778 list_for_each_entry(lte, &staging_list, staging_list) {
779 ret = close_lte_fds(lte);
785 /* Calculate SHA1 checksums for all staging files, and merge unnecessary
786 * lookup table entries. */
787 DEBUG("Calculating SHA1 checksums for all new staging files.");
788 list_for_each_entry_safe(lte, tmp, &staging_list, staging_list) {
789 ret = update_lte_of_staging_file(lte, w->lookup_table);
794 xml_update_image_info(w, w->current_image);
796 ret = wimlib_overwrite(w, check_integrity);
798 ERROR("Failed to commit changes");
804 /* Called when the filesystem is unmounted. */
805 static void wimfs_destroy(void *p)
807 /* For read-write mounts, the `imagex unmount' command, which is
808 * running in a separate process and is executing the
809 * wimlib_unmount() function, will send this process a byte
810 * through a message queue that indicates whether the --commit
811 * option was specified or not. */
814 struct timespec timeout;
816 ssize_t bytes_received;
819 char check_integrity;
822 ret = open_message_queues(true);
826 msgsize = mq_get_msgsize(unmount_to_daemon_mq);
831 /* Wait at most 3 seconds before giving up and discarding changes. */
832 gettimeofday(&now, NULL);
833 timeout.tv_sec = now.tv_sec + 3;
834 timeout.tv_nsec = now.tv_usec * 1000;
835 DEBUG("Waiting for message telling us whether to commit or not, and "
836 "whether to include integrity checks.");
838 bytes_received = mq_timedreceive(unmount_to_daemon_mq, msg,
839 msgsize, NULL, &timeout);
841 check_integrity = msg[1];
842 if (bytes_received == -1) {
843 if (errno == ETIMEDOUT) {
846 ERROR_WITH_ERRNO("mq_timedreceive()");
848 ERROR("Not committing.");
850 DEBUG("Received message: [%d %d]", msg[0], msg[1]);
854 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
856 status = chdir(working_directory);
858 ERROR_WITH_ERRNO("chdir()");
859 status = WIMLIB_ERR_NOTDIR;
862 status = rebuild_wim(w, (check_integrity != 0));
864 ret = delete_staging_dir();
866 ERROR_WITH_ERRNO("Failed to delete the staging "
872 DEBUG("Read-only mount");
875 DEBUG("Sending status %u", status);
876 ret = mq_send(daemon_to_unmount_mq, &status, 1, 1);
878 ERROR_WITH_ERRNO("Failed to send status to unmount process");
879 close_message_queues();
883 static int wimfs_fallocate(const char *path, int mode,
884 off_t offset, off_t len, struct fuse_file_info *fi)
886 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
887 wimlib_assert(fd->staging_fd != -1);
888 return fallocate(fd->staging_fd, mode, offset, len);
891 static int wimfs_fgetattr(const char *path, struct stat *stbuf,
892 struct fuse_file_info *fi)
894 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
895 return dentry_to_stbuf(fd->dentry, stbuf);
899 static int wimfs_ftruncate(const char *path, off_t size,
900 struct fuse_file_info *fi)
902 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
903 int ret = ftruncate(fd->staging_fd, size);
906 fd->lte->resource_entry.original_size = size;
911 * Fills in a `struct stat' that corresponds to a file or directory in the WIM.
913 static int wimfs_getattr(const char *path, struct stat *stbuf)
915 struct dentry *dentry;
916 struct lookup_table_entry *lte;
919 ret = lookup_resource(w, path,
920 get_lookup_flags() | LOOKUP_FLAG_DIRECTORY_OK,
921 &dentry, <e, NULL);
924 return inode_to_stbuf(dentry->inode, lte, stbuf);
928 /* Read an alternate data stream through the XATTR interface, or get its size */
929 static int wimfs_getxattr(const char *path, const char *name, char *value,
933 struct dentry *dentry;
934 struct ads_entry *ads_entry;
936 struct lookup_table_entry *lte;
938 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
941 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
945 dentry = get_dentry(w, path);
948 ads_entry = inode_get_ads_entry(dentry->inode, name, NULL);
952 lte = ads_entry->lte;
953 res_size = wim_resource_size(lte);
959 ret = read_full_wim_resource(lte, (u8*)value);
966 /* Create a hard link */
967 static int wimfs_link(const char *to, const char *from)
969 struct dentry *to_dentry, *from_dentry, *from_dentry_parent;
970 const char *link_name;
973 struct lookup_table_entry *lte;
975 to_dentry = get_dentry(w, to);
979 inode = to_dentry->inode;
981 if (!inode_is_regular_file(inode))
984 from_dentry_parent = get_parent_dentry(w, from);
985 if (!from_dentry_parent)
987 if (!dentry_is_directory(from_dentry_parent))
990 link_name = path_basename(from);
991 if (get_dentry_child_with_name(from_dentry_parent, link_name))
993 from_dentry = new_dentry(link_name);
998 inode_add_dentry(from_dentry, inode);
999 from_dentry->inode = inode;
1000 inode->link_count++;
1002 for (i = 0; i <= inode->num_ads; i++) {
1003 lte = inode_stream_lte_resolved(inode, i);
1008 link_dentry(from_dentry, from_dentry_parent);
1013 static int wimfs_listxattr(const char *path, char *list, size_t size)
1015 struct dentry *dentry;
1020 struct inode *inode;
1021 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1024 /* List alternate data streams, or get the list size */
1026 ret = lookup_resource(w, path, get_lookup_flags(), &dentry, NULL, NULL);
1029 inode = dentry->inode;
1033 for (i = 0; i < inode->num_ads; i++)
1034 needed_size += inode->ads_entries[i]->stream_name_utf8_len + 6;
1037 for (i = 0; i < inode->num_ads; i++) {
1038 needed_size = inode->ads_entries[i]->stream_name_utf8_len + 6;
1039 if (needed_size > size)
1041 p += sprintf(p, "user.%s",
1042 inode->ads_entries[i]->stream_name_utf8) + 1;
1043 size -= needed_size;
1051 * Create a directory in the WIM.
1052 * @mode is currently ignored.
1054 static int wimfs_mkdir(const char *path, mode_t mode)
1056 struct dentry *parent;
1057 struct dentry *newdir;
1058 const char *basename;
1060 parent = get_parent_dentry(w, path);
1064 if (!dentry_is_directory(parent))
1067 basename = path_basename(path);
1068 if (get_dentry_child_with_name(parent, basename))
1071 newdir = new_dentry_with_inode(basename);
1072 newdir->inode->attributes |= FILE_ATTRIBUTE_DIRECTORY;
1073 newdir->inode->resolved = true;
1074 newdir->inode->ino = next_ino++;
1075 link_dentry(newdir, parent);
1080 /* Creates a regular file. */
1081 static int wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
1083 const char *stream_name;
1084 if ((mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
1085 && (stream_name = path_stream_name(path))) {
1086 /* Make an alternate data stream */
1087 struct ads_entry *new_entry;
1088 struct dentry *dentry;
1090 char *p = (char*)stream_name - 1;
1091 wimlib_assert(*p == ':');
1094 dentry = get_dentry(w, path);
1095 if (!dentry || !dentry_is_regular_file(dentry))
1097 if (inode_get_ads_entry(dentry->inode, stream_name, NULL))
1099 new_entry = inode_add_ads(dentry->inode, stream_name);
1103 struct dentry *dentry, *parent;
1104 const char *basename;
1106 /* Make a normal file (not an alternate data stream) */
1108 /* Make sure that the parent of @path exists and is a directory, and
1109 * that the dentry named by @path does not already exist. */
1110 parent = get_parent_dentry(w, path);
1113 if (!dentry_is_directory(parent))
1116 basename = path_basename(path);
1117 if (get_dentry_child_with_name(parent, path))
1120 dentry = new_dentry_with_inode(basename);
1123 dentry->inode->resolved = true;
1124 dentry->inode->ino = next_ino++;
1125 link_dentry(dentry, parent);
1132 static int wimfs_open(const char *path, struct fuse_file_info *fi)
1134 struct dentry *dentry;
1135 struct lookup_table_entry *lte;
1137 struct wimlib_fd *fd;
1138 struct inode *inode;
1142 ret = lookup_resource(w, path, get_lookup_flags(), &dentry, <e,
1147 inode = dentry->inode;
1149 if (stream_idx == 0)
1152 stream_id = inode->ads_entries[stream_idx - 1]->stream_id;
1154 /* The file resource may be in the staging directory (read-write mounts
1155 * only) or in the WIM. If it's in the staging directory, we need to
1156 * open a native file descriptor for the corresponding file. Otherwise,
1157 * we can read the file resource directly from the WIM file if we are
1158 * opening it read-only, but we need to extract the resource to the
1159 * staging directory if we are opening it writable. */
1160 if (flags_writable(fi->flags) &&
1161 lte->resource_location != RESOURCE_IN_STAGING_FILE) {
1162 ret = extract_resource_to_staging_dir(inode, stream_id,
1164 wim_resource_size(lte));
1169 ret = alloc_wimlib_fd(inode, stream_id, lte, &fd);
1173 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1174 fd->staging_fd = open(lte->staging_file_name, fi->flags);
1175 if (fd->staging_fd == -1) {
1176 close_wimlib_fd(fd);
1180 fi->fh = (uintptr_t)fd;
1184 /* Opens a directory. */
1185 static int wimfs_opendir(const char *path, struct fuse_file_info *fi)
1187 struct dentry *dentry;
1188 struct inode *inode;
1190 struct wimlib_fd *fd = NULL;
1192 dentry = get_dentry(w, path);
1196 inode = dentry->inode;
1198 if (!inode_is_directory(inode))
1200 ret = alloc_wimlib_fd(inode, 0, NULL, &fd);
1201 fi->fh = (uintptr_t)fd;
1207 * Read data from a file in the WIM or in the staging directory.
1209 static int wimfs_read(const char *path, char *buf, size_t size,
1210 off_t offset, struct fuse_file_info *fi)
1212 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1217 if (!fd->lte) /* Empty stream with no lookup table entry */
1220 if (fd->lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1221 /* Read from staging file */
1223 wimlib_assert(fd->lte->staging_file_name);
1224 wimlib_assert(fd->staging_fd != -1);
1227 DEBUG("Seek to offset %zu", offset);
1229 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1231 ret = read(fd->staging_fd, buf, size);
1238 u64 res_size = wim_resource_size(fd->lte);
1240 if (offset > res_size)
1243 size = min(size, res_size - offset);
1245 if (read_wim_resource(fd->lte, (u8*)buf,
1246 size, offset, false) != 0)
1252 /* Fills in the entries of the directory specified by @path using the
1253 * FUSE-provided function @filler. */
1254 static int wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
1255 off_t offset, struct fuse_file_info *fi)
1257 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1258 struct inode *inode;
1259 struct dentry *child;
1266 filler(buf, ".", NULL, 0);
1267 filler(buf, "..", NULL, 0);
1269 child = inode->children;
1275 if (filler(buf, child->file_name_utf8, NULL, 0))
1277 child = child->next;
1278 } while (child != inode->children);
1283 static int wimfs_readlink(const char *path, char *buf, size_t buf_len)
1285 struct dentry *dentry = get_dentry(w, path);
1289 if (!dentry_is_symlink(dentry))
1292 ret = inode_readlink(dentry->inode, buf, buf_len, w);
1299 static int wimfs_release(const char *path, struct fuse_file_info *fi)
1301 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1303 return close_wimlib_fd(fd);
1306 static int wimfs_releasedir(const char *path, struct fuse_file_info *fi)
1308 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1310 return close_wimlib_fd(fd);
1314 /* Remove an alternate data stream through the XATTR interface */
1315 static int wimfs_removexattr(const char *path, const char *name)
1317 struct dentry *dentry;
1318 struct ads_entry *ads_entry;
1320 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1323 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1327 dentry = get_dentry(w, path);
1331 ads_entry = inode_get_ads_entry(dentry->inode, name, &ads_idx);
1334 inode_remove_ads(dentry->inode, ads_idx, w->lookup_table);
1339 /* Renames a file or directory. See rename (3) */
1340 static int wimfs_rename(const char *from, const char *to)
1344 struct dentry *parent_of_dst;
1345 char *file_name_utf16 = NULL, *file_name_utf8 = NULL;
1346 u16 file_name_utf16_len, file_name_utf8_len;
1349 /* This rename() implementation currently only supports actual files
1350 * (not alternate data streams) */
1352 src = get_dentry(w, from);
1356 dst = get_dentry(w, to);
1359 ret = get_names(&file_name_utf16, &file_name_utf8,
1360 &file_name_utf16_len, &file_name_utf8_len,
1366 /* Destination file exists */
1368 if (src == dst) /* Same file */
1371 if (!dentry_is_directory(src)) {
1372 /* Cannot rename non-directory to directory. */
1373 if (dentry_is_directory(dst))
1376 /* Cannot rename directory to a non-directory or a non-empty
1378 if (!dentry_is_directory(dst))
1380 if (dst->inode->children != NULL)
1383 parent_of_dst = dst->parent;
1384 remove_dentry(dst, w->lookup_table);
1386 /* Destination does not exist */
1387 parent_of_dst = get_parent_dentry(w, to);
1391 if (!dentry_is_directory(parent_of_dst))
1395 FREE(src->file_name);
1396 FREE(src->file_name_utf8);
1397 src->file_name = file_name_utf16;
1398 src->file_name_utf8 = file_name_utf8;
1399 src->file_name_len = file_name_utf16_len;
1400 src->file_name_utf8_len = file_name_utf8_len;
1403 link_dentry(src, parent_of_dst);
1407 /* Remove a directory */
1408 static int wimfs_rmdir(const char *path)
1410 struct dentry *dentry;
1412 dentry = get_dentry(w, path);
1416 if (!dentry_is_empty_directory(dentry))
1419 remove_dentry(dentry, w->lookup_table);
1424 /* Write an alternate data stream through the XATTR interface */
1425 static int wimfs_setxattr(const char *path, const char *name,
1426 const char *value, size_t size, int flags)
1428 struct dentry *dentry;
1429 struct ads_entry *existing_ads_entry;
1430 struct ads_entry *new_ads_entry;
1431 struct lookup_table_entry *existing_lte;
1432 struct lookup_table_entry *lte;
1433 struct inode *inode;
1434 u8 value_hash[SHA1_HASH_SIZE];
1437 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1440 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1444 dentry = get_dentry(w, path);
1447 inode = dentry->inode;
1449 existing_ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
1450 if (existing_ads_entry) {
1451 if (flags & XATTR_CREATE)
1453 inode_remove_ads(dentry->inode, ads_idx, w->lookup_table);
1455 if (flags & XATTR_REPLACE)
1458 new_ads_entry = inode_add_ads(dentry, name);
1462 sha1_buffer((const u8*)value, size, value_hash);
1464 existing_lte = __lookup_resource(w->lookup_table, value_hash);
1471 lte = new_lookup_table_entry();
1474 value_copy = MALLOC(size);
1479 memcpy(value_copy, value, size);
1480 lte->resource_location = RESOURCE_IN_ATTACHED_BUFFER;
1481 lte->attached_buffer = value_copy;
1482 lte->resource_entry.original_size = size;
1483 lte->resource_entry.size = size;
1484 lte->resource_entry.flags = 0;
1485 copy_hash(lte->hash, value_hash);
1486 lookup_table_insert(w->lookup_table, lte);
1488 new_ads_entry->lte = lte;
1493 static int wimfs_symlink(const char *to, const char *from)
1495 struct dentry *dentry_parent, *dentry;
1496 const char *link_name;
1497 struct lookup_table_entry *lte;
1499 dentry_parent = get_parent_dentry(w, from);
1502 if (!dentry_is_directory(dentry_parent))
1505 link_name = path_basename(from);
1507 if (get_dentry_child_with_name(dentry_parent, link_name))
1509 dentry = new_dentry(link_name);
1513 dentry->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
1514 dentry->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
1515 dentry->link_group_id = next_link_group_id++;
1517 if (dentry_set_symlink(dentry, to, w->lookup_table, <e) != 0)
1518 goto out_free_dentry;
1522 dentry->ads_entries[1].lte_group_list.type = STREAM_TYPE_ADS;
1523 list_add(&dentry->ads_entries[1].lte_group_list.list,
1524 <e->lte_group_list);
1525 wimlib_assert(dentry->resolved);
1527 link_dentry(dentry, dentry_parent);
1530 free_dentry(dentry);
1535 /* Reduce the size of a file */
1536 static int wimfs_truncate(const char *path, off_t size)
1538 struct dentry *dentry;
1539 struct lookup_table_entry *lte;
1541 unsigned stream_idx;
1543 ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1549 if (!lte) /* Already a zero-length file */
1552 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1553 wimlib_assert(lte->staging_file_name);
1554 ret = truncate(lte->staging_file_name, size);
1557 lte->resource_entry.original_size = size;
1559 wimlib_assert(lte->resource_location == RESOURCE_IN_WIM);
1560 /* File in WIM. Extract it to the staging directory, but only
1561 * the first @size bytes of it. */
1562 ret = extract_resource_to_staging_dir(dentry, stream_idx,
1568 /* Remove a regular file */
1569 static int wimfs_unlink(const char *path)
1571 struct dentry *dentry;
1572 struct lookup_table_entry *lte;
1573 struct inode *inode;
1575 unsigned stream_idx;
1578 ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1584 if (stream_idx == 0)
1585 remove_dentry(dentry, w->lookup_table);
1587 inode_remove_ads(dentry->inode, stream_idx - 1, w->lookup_table);
1591 #ifdef HAVE_UTIMENSAT
1593 * Change the timestamp on a file dentry.
1595 * Note that alternate data streams do not have their own timestamps.
1597 static int wimfs_utimens(const char *path, const struct timespec tv[2])
1599 struct dentry *dentry;
1600 struct inode *inode;
1601 dentry = get_dentry(w, path);
1604 inode = dentry->inode;
1606 if (tv[0].tv_nsec != UTIME_OMIT) {
1607 if (tv[0].tv_nsec == UTIME_NOW)
1608 inode->last_access_time = get_wim_timestamp();
1610 inode->last_access_time = timespec_to_wim_timestamp(&tv[0]);
1612 if (tv[1].tv_nsec != UTIME_OMIT) {
1613 if (tv[1].tv_nsec == UTIME_NOW)
1614 inode->last_write_time = get_wim_timestamp();
1616 inode->last_write_time = timespec_to_wim_timestamp(&tv[1]);
1621 static int wimfs_utime(const char *path, struct utimbuf *times)
1623 struct dentry *dentry;
1624 struct inode *inode;
1625 dentry = get_dentry(w, path);
1628 inode = dentry->inode;
1630 inode->last_write_time = unix_timestamp_to_wim(times->modtime);
1631 inode->last_access_time = unix_timestamp_to_wim(times->actime);
1636 /* Writes to a file in the WIM filesystem.
1637 * It may be an alternate data stream, but here we don't even notice because we
1638 * just get a lookup table entry. */
1639 static int wimfs_write(const char *path, const char *buf, size_t size,
1640 off_t offset, struct fuse_file_info *fi)
1642 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1649 wimlib_assert(fd->lte);
1650 wimlib_assert(fd->lte->staging_file_name);
1651 wimlib_assert(fd->staging_fd != -1);
1652 wimlib_assert(fd->inode);
1654 /* Seek to the requested position */
1655 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1658 /* Write the data. */
1659 ret = write(fd->staging_fd, buf, size);
1663 now = get_wim_timestamp();
1664 fd->inode->last_write_time = now;
1665 fd->inode->last_access_time = now;
1669 static struct fuse_operations wimfs_operations = {
1670 .access = wimfs_access,
1671 .destroy = wimfs_destroy,
1673 .fallocate = wimfs_fallocate,
1674 .fgetattr = wimfs_fgetattr,
1676 .ftruncate = wimfs_ftruncate,
1677 .getattr = wimfs_getattr,
1679 .getxattr = wimfs_getxattr,
1683 .listxattr = wimfs_listxattr,
1685 .mkdir = wimfs_mkdir,
1686 .mknod = wimfs_mknod,
1688 .opendir = wimfs_opendir,
1690 .readdir = wimfs_readdir,
1691 .readlink = wimfs_readlink,
1692 .release = wimfs_release,
1693 .releasedir = wimfs_releasedir,
1695 .removexattr = wimfs_removexattr,
1697 .rename = wimfs_rename,
1698 .rmdir = wimfs_rmdir,
1700 .setxattr = wimfs_setxattr,
1702 .symlink = wimfs_symlink,
1703 .truncate = wimfs_truncate,
1704 .unlink = wimfs_unlink,
1705 #ifdef HAVE_UTIMENSAT
1706 .utimens = wimfs_utimens,
1708 .utime = wimfs_utime,
1710 .write = wimfs_write,
1714 static int check_lte_refcnt(struct lookup_table_entry *lte, void *ignore)
1716 size_t lte_group_size = 0;
1717 struct list_head *cur;
1718 list_for_each(cur, <e->lte_group_list)
1720 if (lte_group_size > lte->refcnt) {
1721 #ifdef ENABLE_ERROR_MESSAGES
1722 struct dentry *example_dentry;
1723 struct list_head *next;
1724 struct stream_list_head *head;
1725 WARNING("The following lookup table entry has a reference count "
1726 "of %u, but", lte->refcnt);
1727 WARNING("We found %zu references to it", lte_group_size);
1728 next = lte->lte_group_list.next;
1729 head = container_of(next, struct stream_list_head, list);
1730 if (head->type == STREAM_TYPE_NORMAL) {
1731 example_dentry = container_of(head, struct dentry,
1733 WARNING("(One dentry referencing it is at `%s')",
1734 example_dentry->full_path_utf8);
1736 print_lookup_table_entry(lte);
1738 /* Guess what! install.wim for Windows 8 contains a stream with
1739 * 2 dentries referencing it, but the lookup table entry has
1740 * reference count of 1. So we will need to handle this case
1741 * and not just make it be an error... I'm just setting the
1742 * reference count to the number of references we found. */
1745 lte->refcnt = lte_group_size;
1746 WARNING("Fixing reference count");
1748 return WIMLIB_ERR_INVALID_DENTRY;
1754 /* Mounts a WIM file. */
1755 WIMLIBAPI int wimlib_mount(WIMStruct *wim, int image, const char *dir,
1756 int flags, WIMStruct **additional_swms,
1757 unsigned num_additional_swms)
1763 struct lookup_table *joined_tab, *wim_tab_save;
1765 DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
1766 wim, image, dir, flags);
1769 return WIMLIB_ERR_INVALID_PARAM;
1771 ret = verify_swm_set(wim, additional_swms, num_additional_swms);
1775 if (num_additional_swms) {
1776 ret = new_joined_lookup_table(wim, additional_swms,
1777 num_additional_swms,
1781 wim_tab_save = wim->lookup_table;
1782 wim->lookup_table = joined_tab;
1785 ret = wimlib_select_image(wim, image);
1790 DEBUG("Selected image %d", image);
1792 next_link_group_id = assign_link_group_ids(wim->image_metadata[image - 1].lgt);
1794 DEBUG("Resolving lookup table entries");
1795 /* Resolve all the lookup table entries of the dentry tree */
1796 for_dentry_in_tree(wim_root_dentry(wim), dentry_resolve_ltes,
1799 DEBUG("Checking lookup table entry reference counts");
1801 ret = for_lookup_table_entry(wim->lookup_table, check_lte_refcnt, NULL);
1805 if (flags & WIMLIB_MOUNT_FLAG_READWRITE)
1806 wim_get_current_image_metadata(wim)->modified = true;
1808 if (!(flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
1809 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
1810 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
1811 flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1813 DEBUG("Getting current directory");
1816 working_directory = getcwd(NULL, 0);
1817 if (!working_directory) {
1818 ERROR_WITH_ERRNO("Could not determine current directory");
1819 ret = WIMLIB_ERR_NOTDIR;
1823 DEBUG("Closing POSIX message queues");
1824 /* XXX hack to get rid of the message queues if they already exist for
1825 * some reason (maybe left over from a previous mount that wasn't
1826 * unmounted correctly) */
1827 ret = open_message_queues(true);
1830 close_message_queues();
1832 DEBUG("Preparing arguments to fuse_main()");
1837 ret = WIMLIB_ERR_NOMEM;
1841 argv[argc++] = "imagex";
1843 argv[argc++] = "-s"; /* disable multi-threaded operation */
1845 if (flags & WIMLIB_MOUNT_FLAG_DEBUG)
1846 argv[argc++] = "-d";
1849 * We provide the use_ino option because we are going to assign inode
1850 * numbers oursides. We've already numbered the hard link groups with
1851 * unique numbers with the assign_link_groups() function, and the static
1852 * variable next_link_group_id is set to the next available link group
1853 * ID that we will assign to new dentries.
1855 char optstring[256] = "use_ino";
1856 argv[argc++] = "-o";
1857 argv[argc++] = optstring;
1858 if ((flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
1859 /* Read-write mount. Make the staging directory */
1861 if (!staging_dir_name) {
1863 ret = WIMLIB_ERR_MKDIR;
1867 /* Read-only mount */
1868 strcat(optstring, ",ro");
1870 strcat(optstring, ",subtype=wimfs,attr_timeout=0");
1876 DEBUG("FUSE command line (argc = %d): ", argc);
1877 for (i = 0; i < argc; i++) {
1878 fputs(argv[i], stdout);
1886 /* Set static variables. */
1888 mount_flags = flags;
1890 ret = fuse_main(argc, argv, &wimfs_operations, NULL);
1892 ret = WIMLIB_ERR_FUSE;
1894 if (num_additional_swms) {
1895 free_lookup_table(wim->lookup_table);
1896 wim->lookup_table = wim_tab_save;
1903 * Unmounts the WIM file that was previously mounted on @dir by using
1906 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
1913 struct timespec timeout;
1919 /* Open message queues between the unmount process and the
1920 * filesystem daemon. */
1921 ret = open_message_queues(false);
1925 /* Send a message to the filesystem saying whether to commit or
1927 msg[0] = (flags & WIMLIB_UNMOUNT_FLAG_COMMIT) ? 1 : 0;
1928 msg[1] = (flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY) ? 1 : 0;
1930 DEBUG("Sending message: %s, %s",
1931 (msg[0] == 0) ? "don't commit" : "commit",
1932 (msg[1] == 0) ? "don't check" : "check");
1933 ret = mq_send(unmount_to_daemon_mq, msg, 2, 1);
1935 ERROR("Failed to notify filesystem daemon whether we want to "
1936 "commit changes or not");
1937 close_message_queues();
1938 return WIMLIB_ERR_MQUEUE;
1941 /* Execute `fusermount -u', which is installed setuid root, to unmount
1944 * FUSE does not yet implement synchronous unmounts. This means that
1945 * fusermount -u will return before the filesystem daemon returns from
1946 * wimfs_destroy(). This is partly what we want, because we need to
1947 * send a message from this process to the filesystem daemon telling
1948 * whether --commit was specified or not. However, after that, the
1949 * unmount process must wait for the filesystem daemon to finish writing
1956 ERROR_WITH_ERRNO("Failed to fork()");
1957 return WIMLIB_ERR_FORK;
1960 execlp("fusermount", "fusermount", "-u", dir, NULL);
1961 ERROR_WITH_ERRNO("Failed to execute `fusermount'");
1962 exit(WIMLIB_ERR_FUSERMOUNT);
1965 ret = wait(&status);
1967 ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
1969 return WIMLIB_ERR_FUSERMOUNT;
1973 ERROR("fusermount exited with status %d", status);
1975 /* Try again, but with the `umount' program. This is required
1976 * on other FUSE implementations such as FreeBSD's that do not
1977 * have a `fusermount' program. */
1981 ERROR_WITH_ERRNO("Failed to fork()");
1982 return WIMLIB_ERR_FORK;
1985 execlp("umount", "umount", dir, NULL);
1986 ERROR_WITH_ERRNO("Failed to execute `umount'");
1987 exit(WIMLIB_ERR_FUSERMOUNT);
1990 ret = wait(&status);
1992 ERROR_WITH_ERRNO("Failed to wait for `umount' process to "
1994 return WIMLIB_ERR_FUSERMOUNT;
1997 ERROR("`umount' exited with failure status");
1998 return WIMLIB_ERR_FUSERMOUNT;
2003 /* Wait for a message from the filesytem daemon indicating whether the
2004 * filesystem was unmounted successfully (0) or an error occurred (1).
2005 * This may take a long time if a big WIM file needs to be rewritten. */
2007 /* Wait at most 600??? seconds before giving up and returning false.
2008 * Either it's a really big WIM file, or (more likely) the
2009 * filesystem daemon has crashed or failed for some reason.
2011 * XXX come up with some method to determine if the filesystem
2012 * daemon has really crashed or not.
2014 * XXX Idea: have mount daemon write its PID into the WIM file header?
2017 gettimeofday(&now, NULL);
2018 timeout.tv_sec = now.tv_sec + 600;
2019 timeout.tv_nsec = now.tv_usec * 1000;
2021 msgsize = mq_get_msgsize(daemon_to_unmount_mq);
2022 char mailbox[msgsize];
2025 DEBUG("Waiting for message telling us whether the unmount was "
2026 "successful or not.");
2027 ret = mq_timedreceive(daemon_to_unmount_mq, mailbox, msgsize,
2030 close_message_queues();
2032 if (errno_save == ETIMEDOUT) {
2033 ERROR("Timed out- probably the filesystem daemon "
2034 "crashed and the WIM was not written "
2036 return WIMLIB_ERR_TIMEOUT;
2038 ERROR("mq_receive(): %s", strerror(errno_save));
2039 return WIMLIB_ERR_MQUEUE;
2043 DEBUG("Received message: %s",
2044 (mailbox[0] == 0) ? "Unmount OK" : "Unmount Failed");
2045 if (mailbox[0] != 0)
2046 ERROR("Unmount failed");
2050 #else /* WITH_FUSE */
2053 static inline int mount_unsupported_error()
2055 ERROR("WIMLIB was compiled with --without-fuse, which disables support "
2056 "for mounting WIMs.");
2057 return WIMLIB_ERR_UNSUPPORTED;
2060 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
2062 return mount_unsupported_error();
2065 WIMLIBAPI int wimlib_mount(WIMStruct *wim_p, int image, const char *dir,
2066 int flags, WIMStruct **additional_swms,
2067 unsigned num_additional_swms)
2069 return mount_unsupported_error();
2072 #endif /* WITH_FUSE */