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 Lesser General Public License as published by the Free
17 * Software Foundation; either version 2.1 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 Lesser General Public License for more
25 * You should have received a copy of the GNU Lesser General Public License
26 * along with wimlib; if not, see http://www.gnu.org/licenses/.
29 #include "wimlib_internal.h"
33 #include "lookup_table.h"
38 #define FUSE_USE_VERSION 26
46 /* The WIMStruct for the mounted WIM. */
49 /* Working directory when `imagex mount' is run. */
50 static const char *working_directory;
52 /* Name of the staging directory for a read-write mount. Whenever a new file is
53 * created, it is done so in the staging directory. Furthermore, whenever a
54 * file in the WIM is modified, it is extracted to the staging directory. If
55 * changes are commited when the WIM is unmounted, the file resources are merged
56 * in from the staging directory when writing the new WIM. */
57 static char *staging_dir_name;
58 static size_t staging_dir_name_len;
60 /* Flags passed to wimlib_mount(). */
61 static int mount_flags;
63 /* Name of the directory on which the WIM file is mounted. */
64 static const char *mount_dir;
68 * Creates a randomly named staging directory and returns its name into the
69 * static variable staging_dir_name.
71 * If the staging directory cannot be created, staging_dir_name is set to NULL.
73 static void make_staging_dir()
75 /* XXX Give the user an option of where to stage files */
77 static char prefix[] = "wimlib-staging-";
78 static const size_t prefix_len = 15;
79 static const size_t suffix_len = 10;
81 size_t pwd_len = strlen(working_directory);
83 staging_dir_name_len = pwd_len + 1 + prefix_len + suffix_len;
85 staging_dir_name = MALLOC(staging_dir_name_len + 1);
86 if (!staging_dir_name) {
87 ERROR("Out of memory!\n");
91 memcpy(staging_dir_name, working_directory, pwd_len);
92 staging_dir_name[pwd_len] = '/';
93 memcpy(staging_dir_name + pwd_len + 1, prefix, prefix_len);
94 randomize_char_array_with_alnum(staging_dir_name + pwd_len + 1 + prefix_len,
96 staging_dir_name[staging_dir_name_len] = '\0';
98 if (mkdir(staging_dir_name, 0700) != 0) {
99 ERROR("Failed to create temporary directory `%s': %m\n",
101 FREE(staging_dir_name);
102 staging_dir_name = NULL;
106 static int remove_file_or_directory(const char *fpath, const struct stat *sb,
107 int typeflag, struct FTW *ftwbuf)
109 if (remove(fpath) == 0)
112 return WIMLIB_ERR_DELETE_STAGING_DIR;
117 * Deletes the staging directory and all the files contained in it.
119 static inline int delete_staging_dir()
121 return nftw(staging_dir_name, remove_file_or_directory, 10, FTW_DEPTH);
124 /* Name and message queue descriptors for message queues between the filesystem
125 * daemon process and the unmount process. These are used when the filesystem
126 * is unmounted and the process running wimlib_mount() (i.e. the `imagex
127 * unmount' command) needs to communicate with the filesystem daemon running
128 * fuse_main() (i.e. that spawned by the `imagex mount' or `imagex mountrw'
130 static char *unmount_to_daemon_mq_name;
131 static char *daemon_to_unmount_mq_name;
132 static int unmount_to_daemon_mq;
133 static int daemon_to_unmount_mq;
135 /* Simple function that returns the concatenation of 4 strings. */
136 static char *strcat_dup(const char *s1, const char *s2, const char *s3,
139 size_t len = strlen(s1) + strlen(s2) + strlen(s3) + strlen(s4) + 1;
140 char *p = MALLOC(len);
151 /* Removes trailing forward slashes in a string. */
152 static void remove_trailing_slashes(char *s)
154 long len = strlen(s);
155 for (long i = len - 1; i >= 1; i--) {
163 /* Changes forward slashes to underscores in a string. */
164 static void s_slashes_underscores_g(char *s)
174 * Opens two POSIX message queue: one for sending messages from the unmount
175 * process to the daemon process, and one to go the other way. The names of the
176 * message queues, which must be system-wide unique, are be based on the mount
177 * point. (There of course is still a possibility of a collision if one were to
178 * unmount two identically named directories simultaneously...)
180 * @daemon specifies whether the calling process is the filesystem daemon or the
183 static int open_message_queues(bool daemon)
185 static const char *slash = "/";
186 static const char *prefix = "wimlib-";
187 static const char *u2d_suffix = "unmount-to-daemon-mq";
188 static const char *d2u_suffix = "daemon-to-unmount-mq";
190 const char *mount_dir_basename = path_basename(mount_dir);
194 unmount_to_daemon_mq_name = strcat_dup(slash, mount_dir_basename,
196 if (!unmount_to_daemon_mq_name) {
197 ERROR("Out of memory!\n");
198 return WIMLIB_ERR_NOMEM;
200 daemon_to_unmount_mq_name = strcat_dup(slash, mount_dir_basename,
202 if (!daemon_to_unmount_mq_name) {
203 ERROR("Out of memory!\n");
204 ret = WIMLIB_ERR_NOMEM;
208 remove_trailing_slashes(unmount_to_daemon_mq_name);
209 remove_trailing_slashes(daemon_to_unmount_mq_name);
210 s_slashes_underscores_g(unmount_to_daemon_mq_name + 1);
211 s_slashes_underscores_g(daemon_to_unmount_mq_name + 1);
214 flags = O_RDONLY | O_CREAT;
216 flags = O_WRONLY | O_CREAT;
218 unmount_to_daemon_mq = mq_open(unmount_to_daemon_mq_name, flags,
221 if (unmount_to_daemon_mq == -1) {
222 ERROR("mq_open(): %m\n");
223 ret = WIMLIB_ERR_MQUEUE;
228 flags = O_WRONLY | O_CREAT;
230 flags = O_RDONLY | O_CREAT;
232 daemon_to_unmount_mq = mq_open(daemon_to_unmount_mq_name, flags,
235 if (daemon_to_unmount_mq == -1) {
236 ERROR("mq_open(): %m\n");
237 ret = WIMLIB_ERR_MQUEUE;
242 mq_close(unmount_to_daemon_mq);
243 mq_unlink(unmount_to_daemon_mq_name);
245 FREE(daemon_to_unmount_mq_name);
247 FREE(unmount_to_daemon_mq_name);
251 static int mq_get_msgsize(mqd_t mq)
253 static const char *msgsize_max_file = "/proc/sys/fs/mqueue/msgsize_max";
258 if (mq_getattr(unmount_to_daemon_mq, &attr) == 0) {
259 msgsize = attr.mq_msgsize;
261 ERROR("mq_getattr(): %m\n");
262 ERROR("Attempting to read %s\n", msgsize_max_file);
263 fp = fopen(msgsize_max_file, "rb");
265 if (fscanf(fp, "%d", &msgsize) != 1) {
266 ERROR("Assuming message size of 8192\n");
271 ERROR("Failed to open file %s: %m\n",
273 ERROR("Assuming message size of 8192\n");
280 /* Closes the message queues, which are allocated in static variables */
281 static void close_message_queues()
283 mq_close(unmount_to_daemon_mq);
284 mq_close(daemon_to_unmount_mq);
285 mq_unlink(unmount_to_daemon_mq_name);
286 mq_unlink(daemon_to_unmount_mq_name);
289 static int wimfs_access(const char *path, int mask)
291 /* XXX Permissions not implemented */
295 /* Closes the staging file descriptor associated with the lookup table entry, if
297 static int close_staging_file(struct lookup_table_entry *lte, void *ignore)
299 if (lte->staging_file_name && lte->staging_num_times_opened) {
300 if (close(lte->staging_fd) != 0) {
301 ERROR("Failed close file `%s': %m\n",
302 lte->staging_file_name);
303 return WIMLIB_ERR_WRITE;
310 /* Calculates the SHA1 sum for @dentry if its file resource is in a staging
311 * file. Updates the SHA1 sum in the dentry and the lookup table entry. If
312 * there is already a lookup table entry with the same checksum, increment its
313 * reference count and destroy the lookup entry with the updated checksum. */
314 static int calculate_sha1sum_for_staging_file(struct dentry *dentry, void *lookup_table)
316 struct lookup_table *table;
317 struct lookup_table_entry *lte;
318 struct lookup_table_entry *existing;
321 table = lookup_table;
322 lte = lookup_resource(table, dentry->hash);
324 if (lte && lte->staging_file_name) {
326 DEBUG("Calculating SHA1 hash for file `%s'\n", dentry->file_name_utf8);
327 ret = sha1sum(lte->staging_file_name, dentry->hash);
331 lookup_table_unlink(table, lte);
332 memcpy(lte->hash, dentry->hash, WIM_HASH_SIZE);
333 existing = lookup_resource(table, dentry->hash);
335 DEBUG("Merging duplicate lookup table entries for "
336 "file `%s'\n", dentry->file_name_utf8);
337 free_lookup_table_entry(lte);
340 lookup_table_insert(table, lte);
346 /* Overwrites the WIM file, with changes saved. */
347 static int rebuild_wim(WIMStruct *w, bool check_integrity)
352 root = wim_root_dentry(w);
354 DEBUG("Closing all staging file descriptors.\n");
355 /* Close all the staging file descriptors. */
356 ret = for_lookup_table_entry(w->lookup_table,
357 close_staging_file, NULL);
359 ERROR("Failed to close all staging files!\n");
363 DEBUG("Calculating SHA1 checksums for all new staging files.\n");
364 /* Calculate SHA1 checksums for all staging files, and merge unnecessary
365 * lookup table entries. */
366 ret = for_dentry_in_tree(root, calculate_sha1sum_for_staging_file,
369 ERROR("Failed to calculate new SHA1 checksums!\n");
373 xml_update_image_info(w, w->current_image);
375 ret = wimlib_overwrite(w, check_integrity);
377 ERROR("Failed to commit changes\n");
383 /* Called when the filesystem is unmounted. */
384 static void wimfs_destroy(void *p)
387 /* For read-write mounts, the `imagex unmount' command, which is
388 * running in a separate process and is executing the
389 * wimlib_unmount() function, will send this process a byte
390 * through a message queue that indicates whether the --commit
391 * option was specified or not. */
394 struct timespec timeout;
396 ssize_t bytes_received;
399 char check_integrity;
402 ret = open_message_queues(true);
406 msgsize = mq_get_msgsize(unmount_to_daemon_mq);
411 /* Wait at most 3 seconds before giving up and discarding changes. */
412 gettimeofday(&now, NULL);
413 timeout.tv_sec = now.tv_sec + 3;
414 timeout.tv_nsec = now.tv_usec * 1000;
415 DEBUG("Waiting for message telling us whether to commit or not, "
416 "and whether to include integrity checks.\n");
418 bytes_received = mq_timedreceive(unmount_to_daemon_mq, msg,
419 msgsize, NULL, &timeout);
421 check_integrity = msg[1];
422 if (bytes_received == -1) {
423 if (errno == ETIMEDOUT) {
424 ERROR("Timed out.\n");
426 ERROR("mq_timedreceive(): %m\n");
428 ERROR("Not committing.\n");
430 DEBUG("Received message: [%d %d]\n", msg[0], msg[1]);
434 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
436 status = chdir(working_directory);
438 ERROR("chdir(): %m\n");
439 status = WIMLIB_ERR_NOTDIR;
442 status = rebuild_wim(w, (check_integrity != 0));
444 ret = delete_staging_dir();
446 ERROR("Failed to delete the staging directory: %m\n");
452 ret = mq_send(daemon_to_unmount_mq, &status, 1, 1);
454 ERROR("Failed to send status to unmount process: %m\n");
455 close_message_queues();
459 * Fills in a `struct stat' that corresponds to a file or directory in the WIM.
461 static int wimfs_getattr(const char *path, struct stat *stbuf)
463 struct dentry *dentry = get_dentry(w, path);
466 dentry_to_stbuf(dentry, stbuf, w->lookup_table);
471 * Create a directory in the WIM.
472 * @mode is currently ignored.
474 static int wimfs_mkdir(const char *path, mode_t mode)
476 struct dentry *parent;
477 struct dentry *newdir;
478 const char *basename;
480 parent = get_parent_dentry(w, path);
484 if (!dentry_is_directory(parent))
487 basename = path_basename(path);
488 if (get_dentry_child_with_name(parent, basename))
491 newdir = new_dentry(basename);
492 newdir->attributes |= WIM_FILE_ATTRIBUTE_DIRECTORY;
493 link_dentry(newdir, parent);
497 /* Creates a new staging file and returns its file descriptor opened for
500 * @name_ret: A location into which the a pointer to the newly allocated name of
501 * the staging file is stored.
502 * @return: The file descriptor for the new file. Returns -1 and sets errno on
503 * error, for any reason possible from the creat() function.
505 static int create_staging_file(char **name_ret)
513 name_len = staging_dir_name_len + 1 + WIM_HASH_SIZE;
514 name = MALLOC(name_len + 1);
520 memcpy(name, staging_dir_name, staging_dir_name_len);
521 name[staging_dir_name_len] = '/';
522 randomize_char_array_with_alnum(name + staging_dir_name_len + 1,
524 name[name_len] = '\0';
527 /* Just in case, verify that the randomly generated name doesn't name an
528 * existing file, and try again if so */
529 if (stat(name, &stbuf) == 0) {
530 /* stat succeeded-- the file must exist. Try another name. */
532 return create_staging_file(name_ret);
537 /* doesn't exist--- ok */
540 DEBUG("Creating staging file '%s'\n", name);
542 fd = creat(name, 0600);
553 /* Creates a regular file. This is done in the staging directory. */
554 static int wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
556 struct dentry *parent, *dentry;
557 const char *basename;
558 struct lookup_table_entry *lte;
563 /* Make sure that the parent of @path exists and is a directory, and
564 * that the dentry named by @path does not already exist. */
565 parent = get_parent_dentry(w, path);
568 if (!dentry_is_directory(parent))
570 basename = path_basename(path);
571 if (get_dentry_child_with_name(parent, path))
574 dentry = new_dentry(basename);
576 /* XXX fill in a temporary random hash value- really should check for
578 randomize_byte_array(dentry->hash, WIM_HASH_SIZE);
580 /* Create a lookup table entry having the same hash value */
581 lte = new_lookup_table_entry();
582 lte->staging_num_times_opened = 0;
583 lte->resource_entry.original_size = 0;
584 memcpy(lte->hash, dentry->hash, WIM_HASH_SIZE);
586 fd = create_staging_file(&tmpfile_name);
594 lte->staging_file_name = tmpfile_name;
596 /* Insert the lookup table entry, and link the new dentry with its
598 lookup_table_insert(w->lookup_table, lte);
599 link_dentry(dentry, parent);
603 free_lookup_table_entry(lte);
608 static int wimfs_open(const char *path, struct fuse_file_info *fi)
610 struct dentry *dentry;
611 struct lookup_table_entry *lte;
613 dentry = get_dentry(w, path);
617 if (dentry_is_directory(dentry))
619 lte = wim_lookup_resource(w, dentry);
622 /* If this file is in the staging directory and the file is not
623 * currently open, open it. */
624 if (lte->staging_file_name && lte->staging_num_times_opened == 0) {
625 lte->staging_fd = open(lte->staging_file_name, O_RDWR);
626 if (lte->staging_fd == -1)
628 lte->staging_offset = 0;
631 /* no lookup table entry, so the file must be empty. Create a
632 * lookup table entry for the file. */
636 lte = new_lookup_table_entry();
640 fd = create_staging_file(&tmpfile_name);
647 lte->resource_entry.original_size = 0;
648 randomize_byte_array(lte->hash, WIM_HASH_SIZE);
649 memcpy(dentry->hash, lte->hash, WIM_HASH_SIZE);
650 lte->staging_file_name = tmpfile_name;
651 lte->staging_fd = fd;
652 lte->staging_offset = 0;
653 lookup_table_insert(w->lookup_table, lte);
655 lte->staging_num_times_opened++;
659 /* Opens a directory. */
660 static int wimfs_opendir(const char *path, struct fuse_file_info *fi)
662 struct dentry *dentry;
664 dentry = get_dentry(w, path);
665 if (!dentry || !dentry_is_directory(dentry))
672 * Read data from a file in the WIM or in the staging directory.
674 static int wimfs_read(const char *path, char *buf, size_t size,
675 off_t offset, struct fuse_file_info *fi)
677 struct dentry *dentry;
678 struct lookup_table_entry *lte;
680 dentry = get_dentry(w, path);
685 if (!dentry_is_regular_file(dentry))
688 lte = wim_lookup_resource(w, dentry);
693 if (lte->staging_file_name) {
695 /* Read from staging */
700 if (lte->staging_num_times_opened == 0)
703 fd = lte->staging_fd;
704 cur_offset = lte->staging_offset;
705 if (cur_offset != offset)
706 if (lseek(fd, offset, SEEK_SET) == -1)
708 lte->staging_offset = offset;
710 ret = read(fd, buf, size);
713 lte->staging_offset = offset + ret;
720 struct resource_entry *res_entry;
723 res_entry = <e->resource_entry;
725 ctype = wim_resource_compression_type(w, res_entry);
727 if (offset > res_entry->original_size)
730 size = min(size, res_entry->original_size - offset);
732 if (read_resource(w->fp, res_entry->size,
733 res_entry->original_size,
734 res_entry->offset, ctype, size,
741 /* Fills in the entries of the directory specified by @path using the
742 * FUSE-provided function @filler. */
743 static int wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
744 off_t offset, struct fuse_file_info *fi)
746 struct dentry *parent;
747 struct dentry *child;
750 parent = get_dentry(w, path);
755 if (!dentry_is_directory(parent))
758 filler(buf, ".", NULL, 0);
759 filler(buf, "..", NULL, 0);
761 child = parent->children;
767 memset(&st, 0, sizeof(st));
768 if (filler(buf, child->file_name_utf8, &st, 0))
771 } while (child != parent->children);
776 static int wimfs_release(const char *path, struct fuse_file_info *fi)
778 struct dentry *dentry;
779 struct lookup_table_entry *lte;
782 dentry = get_dentry(w, path);
785 lte = wim_lookup_resource(w, dentry);
790 if (lte->staging_num_times_opened == 0)
793 if (--lte->staging_num_times_opened == 0 && lte->staging_file_name) {
794 ret = close(lte->staging_fd);
801 /* Renames a file or directory. See rename (3) */
802 static int wimfs_rename(const char *from, const char *to)
806 struct dentry *parent_of_dst;
808 src = get_dentry(w, from);
812 dst = get_dentry(w, to);
815 if (!dentry_is_directory(src)) {
816 /* Cannot rename non-directory to directory. */
817 if (dentry_is_directory(dst))
820 /* Cannot rename directory to a non-directory or a non-empty
822 if (!dentry_is_directory(dst))
824 if (dst->children != NULL)
827 parent_of_dst = dst->parent;
829 lookup_table_decrement_refcnt(w->lookup_table, dst->hash);
832 parent_of_dst = get_parent_dentry(w, to);
838 change_dentry_name(src, path_basename(to));
839 link_dentry(src, parent_of_dst);
840 /*calculate_dentry_full_path(src);*/
844 /* Remove a directory */
845 static int wimfs_rmdir(const char *path)
847 struct dentry *dentry;
849 dentry = get_dentry(w, path);
853 if (!dentry_is_empty_directory(dentry))
856 unlink_dentry(dentry);
861 /* Extracts the resource corresponding to @dentry and its lookup table entry
862 * @lte to a file in the staging directory. The lookup table entry for @dentry
863 * is updated to point to the new file. If @lte has multiple dentries
864 * referencing it, a new lookup table entry is created and the hash of @dentry
865 * is changed to point to the new lookup table entry.
867 * Only @size bytes are extracted, to support truncating the file.
869 * Returns the negative error code on failure.
871 static int extract_resource_to_staging_dir(struct dentry *dentry,
872 struct lookup_table_entry *lte,
877 char *staging_file_name;
878 struct lookup_table_entry *new_lte;
880 /* File in WIM. Copy it to the staging directory. */
881 fd = create_staging_file(&staging_file_name);
885 ret = extract_resource_to_fd(w, <e->resource_entry, fd, size);
891 unlink(staging_file_name);
892 FREE(staging_file_name);
896 if (lte->refcnt != 1) {
897 /* Need to make a new lookup table entry if we are
898 * changing only one copy of a hardlinked entry */
901 new_lte = new_lookup_table_entry();
904 randomize_byte_array(dentry->hash, WIM_HASH_SIZE);
905 memcpy(new_lte->hash, dentry->hash, WIM_HASH_SIZE);
907 new_lte->resource_entry.flags = 0;
908 new_lte->staging_num_times_opened = lte->staging_num_times_opened;
910 lookup_table_insert(w->lookup_table, new_lte);
915 lte->resource_entry.original_size = size;
916 lte->staging_file_name = staging_file_name;
918 if (lte->staging_num_times_opened == 0)
921 lte->staging_fd = fd;
925 /* Reduce the size of a file */
926 static int wimfs_truncate(const char *path, off_t size)
928 struct dentry *dentry;
929 struct lookup_table_entry *lte;
932 dentry = get_dentry(w, path);
935 lte = wim_lookup_resource(w, dentry);
937 if (!lte) /* Already a zero-length file */
939 if (lte->staging_file_name) {
940 /* File on disk. Call POSIX API */
941 if (lte->staging_num_times_opened != 0)
942 ret = ftruncate(lte->staging_fd, size);
944 ret = truncate(lte->staging_file_name, size);
947 dentry_update_all_timestamps(dentry);
948 lte->resource_entry.original_size = size;
951 /* File in WIM. Extract it to the staging directory, but only
952 * the first @size bytes of it. */
953 return extract_resource_to_staging_dir(dentry, lte, size);
957 /* Remove a regular file */
958 static int wimfs_unlink(const char *path)
960 struct dentry *dentry;
961 struct lookup_table_entry *lte;
963 dentry = get_dentry(w, path);
967 if (!dentry_is_regular_file(dentry))
970 lte = wim_lookup_resource(w, dentry);
972 if (lte->staging_file_name)
973 if (unlink(lte->staging_file_name) != 0)
975 lookup_table_decrement_refcnt(w->lookup_table, dentry->hash);
978 unlink_dentry(dentry);
983 /* Writes to a file in the WIM filesystem. */
984 static int wimfs_write(const char *path, const char *buf, size_t size,
985 off_t offset, struct fuse_file_info *fi)
987 struct dentry *dentry;
988 struct lookup_table_entry *lte;
991 dentry = get_dentry(w, path);
994 lte = wim_lookup_resource(w, dentry);
996 if (!lte) /* this should not happen */
999 if (lte->staging_num_times_opened == 0)
1001 if (lte->staging_file_name) {
1003 /* File in staging directory. We can write to it directly. */
1005 /* Seek to correct position in file if needed. */
1006 if (lte->staging_offset != offset) {
1007 if (lseek(lte->staging_fd, offset, SEEK_SET) == -1)
1009 lte->staging_offset = offset;
1012 /* Write the data. */
1013 ret = write(lte->staging_fd, buf, size);
1017 /* Adjust the stored offset of staging_fd. */
1018 lte->staging_offset = offset + ret;
1020 /* Increase file size if needed. */
1021 if (lte->resource_entry.original_size < lte->staging_offset)
1022 lte->resource_entry.original_size = lte->staging_offset;
1024 /* The file has been modified, so all its timestamps must be
1026 dentry_update_all_timestamps(dentry);
1029 /* File in the WIM. We must extract it to the staging directory
1030 * before it can be written to. */
1031 ret = extract_resource_to_staging_dir(dentry, lte,
1032 lte->resource_entry.original_size);
1036 return wimfs_write(path, buf, size, offset, fi);
1041 static struct fuse_operations wimfs_oper = {
1042 .access = wimfs_access,
1043 .destroy = wimfs_destroy,
1044 .getattr = wimfs_getattr,
1045 .mkdir = wimfs_mkdir,
1046 .mknod = wimfs_mknod,
1048 .opendir = wimfs_opendir,
1050 .readdir = wimfs_readdir,
1051 .release = wimfs_release,
1052 .rename = wimfs_rename,
1053 .rmdir = wimfs_rmdir,
1054 .truncate = wimfs_truncate,
1055 .unlink = wimfs_unlink,
1056 .write = wimfs_write,
1060 /* Mounts a WIM file. */
1061 WIMLIBAPI int wimlib_mount(WIMStruct *wim, int image, const char *dir,
1069 DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
1070 wim, image, dir, flags);
1073 return WIMLIB_ERR_INVALID_PARAM;
1075 ret = wimlib_select_image(wim, image);
1080 if (flags & WIMLIB_MOUNT_FLAG_READWRITE)
1081 wim_get_current_image_metadata(wim)->modified = true;
1084 working_directory = getcwd(NULL, 0);
1085 if (!working_directory) {
1086 ERROR("Could not determine current directory: %m\n");
1087 return WIMLIB_ERR_NOTDIR;
1092 return WIMLIB_ERR_NOMEM;
1094 argv[argc++] = "mount";
1096 argv[argc++] = "-s"; /* disable multi-threaded operation */
1098 if (flags & WIMLIB_MOUNT_FLAG_DEBUG) {
1099 argv[argc++] = "-d";
1101 if (!(flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
1102 argv[argc++] = "-o";
1103 argv[argc++] = "ro";
1106 if (!staging_dir_name) {
1108 return WIMLIB_ERR_MKDIR;
1115 DEBUG("FUSE command line (argc = %d): ", argc);
1116 for (i = 0; i < argc; i++) {
1117 fputs(argv[i], stdout);
1125 /* Set static variables. */
1127 mount_flags = flags;
1129 ret = fuse_main(argc, argv, &wimfs_oper, NULL);
1131 return (ret == 0) ? 0 : WIMLIB_ERR_FUSE;
1136 * Unmounts the WIM file that was previously mounted on @dir by using
1139 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
1146 struct timespec timeout;
1150 /* Execute `fusermount -u', which is installed setuid root, to unmount
1153 * FUSE does not yet implement synchronous unmounts. This means that
1154 * fusermount -u will return before the filesystem daemon returns from
1155 * wimfs_destroy(). This is partly what we want, because we need to
1156 * send a message from this process to the filesystem daemon telling
1157 * whether --commit was specified or not. However, after that, the
1158 * unmount process must wait for the filesystem daemon to finish writing
1165 ERROR("Failed to fork(): %m\n");
1166 return WIMLIB_ERR_FORK;
1169 execlp("fusermount", "fusermount", "-u", dir, NULL);
1170 ERROR("Failed to execute `fusermount': %m\n");
1171 return WIMLIB_ERR_FUSERMOUNT;
1174 ret = waitpid(pid, &status, 0);
1176 ERROR("Failed to wait for fusermount process to "
1178 return WIMLIB_ERR_FUSERMOUNT;
1182 ERROR("fusermount exited with status %d!\n", status);
1183 return WIMLIB_ERR_FUSERMOUNT;
1186 /* Open message queues between the unmount process and the
1187 * filesystem daemon. */
1188 ret = open_message_queues(false);
1192 /* Send a message to the filesystem saying whether to commit or
1194 msg[0] = (flags & WIMLIB_UNMOUNT_FLAG_COMMIT) ? 1 : 0;
1195 msg[1] = (flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY) ? 1 : 0;
1197 DEBUG("Sending message: %s, %s\n",
1198 (msg[0] == 0) ? "don't commit" : "commit",
1199 (msg[1] == 0) ? "don't check" : "check");
1200 ret = mq_send(unmount_to_daemon_mq, msg, 2, 1);
1202 ERROR("Failed to notify filesystem daemon whether "
1203 "we want to commit changes or not!\n");
1204 close_message_queues();
1205 return WIMLIB_ERR_MQUEUE;
1208 /* Wait for a message from the filesytem daemon indicating whether the
1209 * filesystem was unmounted successfully (0) or an error occurred (1).
1210 * This may take a long time if a big WIM file needs to be rewritten. */
1212 /* Wait at most 600??? seconds before giving up and returning false.
1213 * Either it's a really big WIM file, or (more likely) the
1214 * filesystem daemon has crashed or failed for some reason.
1216 * XXX come up with some method to determine if the filesystem
1217 * daemon has really crashed or not. */
1219 gettimeofday(&now, NULL);
1220 timeout.tv_sec = now.tv_sec + 600;
1221 timeout.tv_nsec = now.tv_usec * 1000;
1223 msgsize = mq_get_msgsize(daemon_to_unmount_mq);
1224 char mailbox[msgsize];
1227 DEBUG("Waiting for message telling us whether the unmount was "
1228 "successful or not.\n");
1229 ret = mq_timedreceive(daemon_to_unmount_mq, mailbox, msgsize,
1232 close_message_queues();
1234 if (errno_save == ETIMEDOUT) {
1235 ERROR("Timed out- probably the filesystem "
1236 "daemon crashed and the WIM was not "
1237 "written successfully.\n");
1238 return WIMLIB_ERR_TIMEOUT;
1240 ERROR("mq_receive(): %s\n",
1241 strerror(errno_save));
1242 return WIMLIB_ERR_MQUEUE;
1246 DEBUG("Received message: %s\n", (mailbox[0] == 0) ?
1247 "Unmount OK" : "Unmount Failed");
1248 if (mailbox[0] != 0)
1249 ERROR("Unmount failed\n");
1253 #else /* WITH_FUSE */
1256 static inline int mount_unsupported_error()
1258 ERROR("WIMLIB was compiled with --without-fuse, which "
1259 "disables support for mounting WIMs.\n");
1260 return WIMLIB_ERR_UNSUPPORTED;
1263 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
1265 return mount_unsupported_error();
1268 WIMLIBAPI int wimlib_mount(WIMStruct *wim_p, int image, const char *dir,
1271 return mount_unsupported_error();
1274 #endif /* WITH_FUSE */