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. */
637 lte = new_lookup_table_entry();
641 fd = create_staging_file(&tmpfile_name);
648 lte->resource_entry.original_size = 0;
649 randomize_byte_array(lte->hash, WIM_HASH_SIZE);
650 memcpy(dentry->hash, lte->hash, WIM_HASH_SIZE);
651 lte->staging_file_name = tmpfile_name;
652 lte->staging_fd = fd;
653 lte->staging_offset = 0;
654 lookup_table_insert(w->lookup_table, lte);
656 lte->staging_num_times_opened++;
660 /* Opens a directory. */
661 static int wimfs_opendir(const char *path, struct fuse_file_info *fi)
663 struct dentry *dentry;
665 dentry = get_dentry(w, path);
666 if (!dentry || !dentry_is_directory(dentry))
673 * Read data from a file in the WIM or in the staging directory.
675 static int wimfs_read(const char *path, char *buf, size_t size,
676 off_t offset, struct fuse_file_info *fi)
678 struct dentry *dentry;
679 struct lookup_table_entry *lte;
681 dentry = get_dentry(w, path);
686 if (!dentry_is_regular_file(dentry))
689 lte = wim_lookup_resource(w, dentry);
694 if (lte->staging_file_name) {
696 /* Read from staging */
701 if (lte->staging_num_times_opened == 0)
704 fd = lte->staging_fd;
705 cur_offset = lte->staging_offset;
706 if (cur_offset != offset)
707 if (lseek(fd, offset, SEEK_SET) == -1)
709 lte->staging_offset = offset;
711 ret = read(fd, buf, size);
714 lte->staging_offset = offset + ret;
721 struct resource_entry *res_entry;
724 res_entry = <e->resource_entry;
726 ctype = wim_resource_compression_type(w, res_entry);
728 if (offset > res_entry->original_size)
731 size = min(size, res_entry->original_size - offset);
733 if (read_resource(w->fp, res_entry->size,
734 res_entry->original_size,
735 res_entry->offset, ctype, size,
742 /* Fills in the entries of the directory specified by @path using the
743 * FUSE-provided function @filler. */
744 static int wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
745 off_t offset, struct fuse_file_info *fi)
747 struct dentry *parent;
748 struct dentry *child;
751 parent = get_dentry(w, path);
756 if (!dentry_is_directory(parent))
759 filler(buf, ".", NULL, 0);
760 filler(buf, "..", NULL, 0);
762 child = parent->children;
768 memset(&st, 0, sizeof(st));
769 if (filler(buf, child->file_name_utf8, &st, 0))
772 } while (child != parent->children);
777 static int wimfs_release(const char *path, struct fuse_file_info *fi)
779 struct dentry *dentry;
780 struct lookup_table_entry *lte;
783 dentry = get_dentry(w, path);
786 lte = wim_lookup_resource(w, dentry);
791 if (lte->staging_num_times_opened == 0)
794 if (--lte->staging_num_times_opened == 0 && lte->staging_file_name) {
795 ret = close(lte->staging_fd);
802 /* Renames a file or directory. See rename (3) */
803 static int wimfs_rename(const char *from, const char *to)
807 struct dentry *parent_of_dst;
809 src = get_dentry(w, from);
813 dst = get_dentry(w, to);
816 if (!dentry_is_directory(src)) {
817 /* Cannot rename non-directory to directory. */
818 if (dentry_is_directory(dst))
821 /* Cannot rename directory to a non-directory or a non-empty
823 if (!dentry_is_directory(dst))
825 if (dst->children != NULL)
828 parent_of_dst = dst->parent;
830 lookup_table_decrement_refcnt(w->lookup_table, dst->hash);
833 parent_of_dst = get_parent_dentry(w, to);
839 change_dentry_name(src, path_basename(to));
840 link_dentry(src, parent_of_dst);
841 /*calculate_dentry_full_path(src);*/
845 /* Remove a directory */
846 static int wimfs_rmdir(const char *path)
848 struct dentry *dentry;
850 dentry = get_dentry(w, path);
854 if (!dentry_is_empty_directory(dentry))
857 unlink_dentry(dentry);
862 /* Extracts the resource corresponding to @dentry and its lookup table entry
863 * @lte to a file in the staging directory. The lookup table entry for @dentry
864 * is updated to point to the new file. If @lte has multiple dentries
865 * referencing it, a new lookup table entry is created and the hash of @dentry
866 * is changed to point to the new lookup table entry.
868 * Only @size bytes are extracted, to support truncating the file.
870 * Returns the negative error code on failure.
872 static int extract_resource_to_staging_dir(struct dentry *dentry,
873 struct lookup_table_entry *lte,
878 char *staging_file_name;
879 struct lookup_table_entry *new_lte;
881 /* File in WIM. Copy it to the staging directory. */
882 fd = create_staging_file(&staging_file_name);
886 ret = extract_resource_to_fd(w, <e->resource_entry, fd, size);
892 unlink(staging_file_name);
893 FREE(staging_file_name);
897 if (lte->refcnt != 1) {
898 /* Need to make a new lookup table entry if we are
899 * changing only one copy of a hardlinked entry */
902 new_lte = new_lookup_table_entry();
905 randomize_byte_array(dentry->hash, WIM_HASH_SIZE);
906 memcpy(new_lte->hash, dentry->hash, WIM_HASH_SIZE);
908 new_lte->resource_entry.flags = 0;
909 new_lte->staging_num_times_opened = lte->staging_num_times_opened;
911 lookup_table_insert(w->lookup_table, new_lte);
916 lte->resource_entry.original_size = size;
917 lte->staging_file_name = staging_file_name;
919 if (lte->staging_num_times_opened == 0)
922 lte->staging_fd = fd;
926 /* Reduce the size of a file */
927 static int wimfs_truncate(const char *path, off_t size)
929 struct dentry *dentry;
930 struct lookup_table_entry *lte;
933 dentry = get_dentry(w, path);
936 lte = wim_lookup_resource(w, dentry);
938 if (!lte) /* Already a zero-length file */
940 if (lte->staging_file_name) {
941 /* File on disk. Call POSIX API */
942 if (lte->staging_num_times_opened != 0)
943 ret = ftruncate(lte->staging_fd, size);
945 ret = truncate(lte->staging_file_name, size);
948 dentry_update_all_timestamps(dentry);
949 lte->resource_entry.original_size = size;
952 /* File in WIM. Extract it to the staging directory, but only
953 * the first @size bytes of it. */
954 return extract_resource_to_staging_dir(dentry, lte, size);
958 /* Remove a regular file */
959 static int wimfs_unlink(const char *path)
961 struct dentry *dentry;
962 struct lookup_table_entry *lte;
964 dentry = get_dentry(w, path);
968 if (!dentry_is_regular_file(dentry))
971 lte = wim_lookup_resource(w, dentry);
973 if (lte->staging_file_name)
974 if (unlink(lte->staging_file_name) != 0)
976 lookup_table_decrement_refcnt(w->lookup_table, dentry->hash);
979 unlink_dentry(dentry);
984 /* Writes to a file in the WIM filesystem. */
985 static int wimfs_write(const char *path, const char *buf, size_t size,
986 off_t offset, struct fuse_file_info *fi)
988 struct dentry *dentry;
989 struct lookup_table_entry *lte;
992 dentry = get_dentry(w, path);
995 lte = wim_lookup_resource(w, dentry);
997 if (!lte) /* this should not happen */
1000 if (lte->staging_num_times_opened == 0)
1002 if (lte->staging_file_name) {
1004 /* File in staging directory. We can write to it directly. */
1006 /* Seek to correct position in file if needed. */
1007 if (lte->staging_offset != offset) {
1008 if (lseek(lte->staging_fd, offset, SEEK_SET) == -1)
1010 lte->staging_offset = offset;
1013 /* Write the data. */
1014 ret = write(lte->staging_fd, buf, size);
1018 /* Adjust the stored offset of staging_fd. */
1019 lte->staging_offset = offset + ret;
1021 /* Increase file size if needed. */
1022 if (lte->resource_entry.original_size < lte->staging_offset)
1023 lte->resource_entry.original_size = lte->staging_offset;
1025 /* The file has been modified, so all its timestamps must be
1027 dentry_update_all_timestamps(dentry);
1030 /* File in the WIM. We must extract it to the staging directory
1031 * before it can be written to. */
1032 ret = extract_resource_to_staging_dir(dentry, lte,
1033 lte->resource_entry.original_size);
1037 return wimfs_write(path, buf, size, offset, fi);
1042 static struct fuse_operations wimfs_oper = {
1043 .access = wimfs_access,
1044 .destroy = wimfs_destroy,
1045 .getattr = wimfs_getattr,
1046 .mkdir = wimfs_mkdir,
1047 .mknod = wimfs_mknod,
1049 .opendir = wimfs_opendir,
1051 .readdir = wimfs_readdir,
1052 .release = wimfs_release,
1053 .rename = wimfs_rename,
1054 .rmdir = wimfs_rmdir,
1055 .truncate = wimfs_truncate,
1056 .unlink = wimfs_unlink,
1057 .write = wimfs_write,
1061 /* Mounts a WIM file. */
1062 WIMLIBAPI int wimlib_mount(WIMStruct *wim, int image, const char *dir,
1070 DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
1071 wim, image, dir, flags);
1074 return WIMLIB_ERR_INVALID_PARAM;
1076 ret = wimlib_select_image(wim, image);
1081 if (flags & WIMLIB_MOUNT_FLAG_READWRITE)
1082 wim_get_current_image_metadata(wim)->modified = true;
1085 working_directory = getcwd(NULL, 0);
1086 if (!working_directory) {
1087 ERROR("Could not determine current directory: %m\n");
1088 return WIMLIB_ERR_NOTDIR;
1093 return WIMLIB_ERR_NOMEM;
1095 argv[argc++] = "mount";
1097 argv[argc++] = "-s"; /* disable multi-threaded operation */
1099 if (flags & WIMLIB_MOUNT_FLAG_DEBUG) {
1100 argv[argc++] = "-d";
1102 if (!(flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
1103 argv[argc++] = "-o";
1104 argv[argc++] = "ro";
1107 if (!staging_dir_name) {
1109 return WIMLIB_ERR_MKDIR;
1116 DEBUG("FUSE command line (argc = %d): ", argc);
1117 for (i = 0; i < argc; i++) {
1118 fputs(argv[i], stdout);
1126 /* Set static variables. */
1128 mount_flags = flags;
1130 ret = fuse_main(argc, argv, &wimfs_oper, NULL);
1132 return (ret == 0) ? 0 : WIMLIB_ERR_FUSE;
1137 * Unmounts the WIM file that was previously mounted on @dir by using
1140 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
1147 struct timespec timeout;
1151 /* Execute `fusermount -u', which is installed setuid root, to unmount
1154 * FUSE does not yet implement synchronous unmounts. This means that
1155 * fusermount -u will return before the filesystem daemon returns from
1156 * wimfs_destroy(). This is partly what we want, because we need to
1157 * send a message from this process to the filesystem daemon telling
1158 * whether --commit was specified or not. However, after that, the
1159 * unmount process must wait for the filesystem daemon to finish writing
1166 ERROR("Failed to fork(): %m\n");
1167 return WIMLIB_ERR_FORK;
1170 execlp("fusermount", "fusermount", "-u", dir, NULL);
1171 ERROR("Failed to execute `fusermount': %m\n");
1172 return WIMLIB_ERR_FUSERMOUNT;
1175 ret = waitpid(pid, &status, 0);
1177 ERROR("Failed to wait for fusermount process to "
1179 return WIMLIB_ERR_FUSERMOUNT;
1183 ERROR("fusermount exited with status %d!\n", status);
1184 return WIMLIB_ERR_FUSERMOUNT;
1187 /* Open message queues between the unmount process and the
1188 * filesystem daemon. */
1189 ret = open_message_queues(false);
1193 /* Send a message to the filesystem saying whether to commit or
1195 msg[0] = (flags & WIMLIB_UNMOUNT_FLAG_COMMIT) ? 1 : 0;
1196 msg[1] = (flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY) ? 1 : 0;
1198 DEBUG("Sending message: %s, %s\n",
1199 (msg[0] == 0) ? "don't commit" : "commit",
1200 (msg[1] == 0) ? "don't check" : "check");
1201 ret = mq_send(unmount_to_daemon_mq, msg, 2, 1);
1203 ERROR("Failed to notify filesystem daemon whether "
1204 "we want to commit changes or not!\n");
1205 close_message_queues();
1206 return WIMLIB_ERR_MQUEUE;
1209 /* Wait for a message from the filesytem daemon indicating whether the
1210 * filesystem was unmounted successfully (0) or an error occurred (1).
1211 * This may take a long time if a big WIM file needs to be rewritten. */
1213 /* Wait at most 600??? seconds before giving up and returning false.
1214 * Either it's a really big WIM file, or (more likely) the
1215 * filesystem daemon has crashed or failed for some reason.
1217 * XXX come up with some method to determine if the filesystem
1218 * daemon has really crashed or not. */
1220 gettimeofday(&now, NULL);
1221 timeout.tv_sec = now.tv_sec + 600;
1222 timeout.tv_nsec = now.tv_usec * 1000;
1224 msgsize = mq_get_msgsize(daemon_to_unmount_mq);
1225 char mailbox[msgsize];
1228 DEBUG("Waiting for message telling us whether the unmount was "
1229 "successful or not.\n");
1230 ret = mq_timedreceive(daemon_to_unmount_mq, mailbox, msgsize,
1233 close_message_queues();
1235 if (errno_save == ETIMEDOUT) {
1236 ERROR("Timed out- probably the filesystem "
1237 "daemon crashed and the WIM was not "
1238 "written successfully.\n");
1239 return WIMLIB_ERR_TIMEOUT;
1241 ERROR("mq_receive(): %s\n",
1242 strerror(errno_save));
1243 return WIMLIB_ERR_MQUEUE;
1247 DEBUG("Received message: %s\n", (mailbox[0] == 0) ?
1248 "Unmount OK" : "Unmount Failed");
1249 if (mailbox[0] != 0)
1250 ERROR("Unmount failed\n");
1254 #else /* WITH_FUSE */
1257 static inline int mount_unsupported_error()
1259 ERROR("WIMLIB was compiled with --without-fuse, which "
1260 "disables support for mounting WIMs.\n");
1261 return WIMLIB_ERR_UNSUPPORTED;
1264 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
1266 return mount_unsupported_error();
1269 WIMLIBAPI int wimlib_mount(WIMStruct *wim_p, int image, const char *dir,
1272 return mount_unsupported_error();
1275 #endif /* WITH_FUSE */