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"
35 #include "timestamp.h"
39 #define FUSE_USE_VERSION 26
47 /* The WIMStruct for the mounted WIM. */
50 /* Working directory when `imagex mount' is run. */
51 static const char *working_directory;
53 /* Name of the staging directory for a read-write mount. Whenever a new file is
54 * created, it is done so in the staging directory. Furthermore, whenever a
55 * file in the WIM is modified, it is extracted to the staging directory. If
56 * changes are commited when the WIM is unmounted, the file resources are merged
57 * in from the staging directory when writing the new WIM. */
58 static char *staging_dir_name;
59 static size_t staging_dir_name_len;
61 /* Flags passed to wimlib_mount(). */
62 static int mount_flags;
64 /* Name of the directory on which the WIM file is mounted. */
65 static const char *mount_dir;
69 * Creates a randomly named staging directory and returns its name into the
70 * static variable staging_dir_name.
72 * If the staging directory cannot be created, staging_dir_name is set to NULL.
74 static void make_staging_dir()
76 /* XXX Give the user an option of where to stage files */
78 static char prefix[] = "wimlib-staging-";
79 static const size_t prefix_len = 15;
80 static const size_t suffix_len = 10;
82 size_t pwd_len = strlen(working_directory);
84 staging_dir_name_len = pwd_len + 1 + prefix_len + suffix_len;
86 staging_dir_name = MALLOC(staging_dir_name_len + 1);
87 if (!staging_dir_name) {
88 ERROR("Out of memory");
92 memcpy(staging_dir_name, working_directory, pwd_len);
93 staging_dir_name[pwd_len] = '/';
94 memcpy(staging_dir_name + pwd_len + 1, prefix, prefix_len);
95 randomize_char_array_with_alnum(staging_dir_name + pwd_len + 1 + prefix_len,
97 staging_dir_name[staging_dir_name_len] = '\0';
99 if (mkdir(staging_dir_name, 0700) != 0) {
100 ERROR_WITH_ERRNO("Failed to create temporary directory `%s'",
102 FREE(staging_dir_name);
103 staging_dir_name = NULL;
107 static int remove_file_or_directory(const char *fpath, const struct stat *sb,
108 int typeflag, struct FTW *ftwbuf)
110 if (remove(fpath) == 0)
113 return WIMLIB_ERR_DELETE_STAGING_DIR;
118 * Deletes the staging directory and all the files contained in it.
120 static inline int delete_staging_dir()
124 ret = nftw(staging_dir_name, remove_file_or_directory,10, FTW_DEPTH);
125 staging_dir_name = NULL;
129 /* Name and message queue descriptors for message queues between the filesystem
130 * daemon process and the unmount process. These are used when the filesystem
131 * is unmounted and the process running wimlib_mount() (i.e. the `imagex
132 * unmount' command) needs to communicate with the filesystem daemon running
133 * fuse_main() (i.e. that spawned by the `imagex mount' or `imagex mountrw'
135 static char *unmount_to_daemon_mq_name;
136 static char *daemon_to_unmount_mq_name;
137 static int unmount_to_daemon_mq;
138 static int daemon_to_unmount_mq;
140 /* Simple function that returns the concatenation of 4 strings. */
141 static char *strcat_dup(const char *s1, const char *s2, const char *s3,
144 size_t len = strlen(s1) + strlen(s2) + strlen(s3) + strlen(s4) + 1;
145 char *p = MALLOC(len);
156 /* Removes trailing forward slashes in a string. */
157 static void remove_trailing_slashes(char *s)
159 long len = strlen(s);
160 for (long i = len - 1; i >= 1; i--) {
168 /* Changes forward slashes to underscores in a string. */
169 static void s_slashes_underscores_g(char *s)
179 * Opens two POSIX message queue: one for sending messages from the unmount
180 * process to the daemon process, and one to go the other way. The names of the
181 * message queues, which must be system-wide unique, are be based on the mount
182 * point. (There of course is still a possibility of a collision if one were to
183 * unmount two identically named directories simultaneously...)
185 * @daemon specifies whether the calling process is the filesystem daemon or the
188 static int open_message_queues(bool daemon)
190 static const char *slash = "/";
191 static const char *prefix = "wimlib-";
192 static const char *u2d_suffix = "unmount-to-daemon-mq";
193 static const char *d2u_suffix = "daemon-to-unmount-mq";
195 const char *mount_dir_basename = path_basename(mount_dir);
199 unmount_to_daemon_mq_name = strcat_dup(slash, mount_dir_basename,
201 if (!unmount_to_daemon_mq_name) {
202 ERROR("Out of memory");
203 return WIMLIB_ERR_NOMEM;
205 daemon_to_unmount_mq_name = strcat_dup(slash, mount_dir_basename,
207 if (!daemon_to_unmount_mq_name) {
208 ERROR("Out of memory");
209 ret = WIMLIB_ERR_NOMEM;
213 remove_trailing_slashes(unmount_to_daemon_mq_name);
214 remove_trailing_slashes(daemon_to_unmount_mq_name);
215 s_slashes_underscores_g(unmount_to_daemon_mq_name + 1);
216 s_slashes_underscores_g(daemon_to_unmount_mq_name + 1);
219 flags = O_RDONLY | O_CREAT;
221 flags = O_WRONLY | O_CREAT;
223 unmount_to_daemon_mq = mq_open(unmount_to_daemon_mq_name, flags,
226 if (unmount_to_daemon_mq == -1) {
227 ERROR_WITH_ERRNO("mq_open()");
228 ret = WIMLIB_ERR_MQUEUE;
233 flags = O_WRONLY | O_CREAT;
235 flags = O_RDONLY | O_CREAT;
237 daemon_to_unmount_mq = mq_open(daemon_to_unmount_mq_name, flags,
240 if (daemon_to_unmount_mq == -1) {
241 ERROR_WITH_ERRNO("mq_open()");
242 ret = WIMLIB_ERR_MQUEUE;
247 mq_close(unmount_to_daemon_mq);
248 mq_unlink(unmount_to_daemon_mq_name);
250 FREE(daemon_to_unmount_mq_name);
252 FREE(unmount_to_daemon_mq_name);
256 static int mq_get_msgsize(mqd_t mq)
258 static const char *msgsize_max_file = "/proc/sys/fs/mqueue/msgsize_max";
263 if (mq_getattr(unmount_to_daemon_mq, &attr) == 0) {
264 msgsize = attr.mq_msgsize;
266 ERROR_WITH_ERRNO("mq_getattr()");
267 ERROR("Attempting to read %s", msgsize_max_file);
268 fp = fopen(msgsize_max_file, "rb");
270 if (fscanf(fp, "%d", &msgsize) != 1) {
271 ERROR("Assuming message size of 8192");
276 ERROR_WITH_ERRNO("Failed to open the file `%s'",
278 ERROR("Assuming message size of 8192");
285 /* Closes the message queues, which are allocated in static variables */
286 static void close_message_queues()
288 mq_close(unmount_to_daemon_mq);
289 mq_close(daemon_to_unmount_mq);
290 mq_unlink(unmount_to_daemon_mq_name);
291 mq_unlink(daemon_to_unmount_mq_name);
294 static int wimfs_access(const char *path, int mask)
296 /* XXX Permissions not implemented */
300 /* Closes the staging file descriptor associated with the lookup table entry, if
302 static int close_staging_file(struct lookup_table_entry *lte, void *ignore)
304 if (lte->staging_file_name && lte->staging_num_times_opened) {
305 if (close(lte->staging_fd) != 0) {
306 ERROR_WITH_ERRNO("Failed close file `%s'",
307 lte->staging_file_name);
308 return WIMLIB_ERR_WRITE;
315 /* Calculates the SHA1 sum for @dentry if its file resource is in a staging
316 * file. Updates the SHA1 sum in the dentry and the lookup table entry. If
317 * there is already a lookup table entry with the same checksum, increment its
318 * reference count and destroy the lookup entry with the updated checksum. */
319 static int calculate_sha1sum_for_staging_file(struct dentry *dentry, void *lookup_table)
321 struct lookup_table *table;
322 struct lookup_table_entry *lte;
323 struct lookup_table_entry *existing;
326 table = lookup_table;
327 lte = lookup_resource(table, dentry->hash);
329 if (lte && lte->staging_file_name) {
331 DEBUG("Calculating SHA1 hash for file `%s'",
332 dentry->file_name_utf8);
333 ret = sha1sum(lte->staging_file_name, dentry->hash);
337 lookup_table_unlink(table, lte);
338 memcpy(lte->hash, dentry->hash, WIM_HASH_SIZE);
339 existing = lookup_resource(table, dentry->hash);
341 DEBUG("Merging duplicate lookup table entries for file "
342 "`%s'", dentry->file_name_utf8);
343 free_lookup_table_entry(lte);
346 lookup_table_insert(table, lte);
352 /* Overwrites the WIM file, with changes saved. */
353 static int rebuild_wim(WIMStruct *w, bool check_integrity)
358 root = wim_root_dentry(w);
360 DEBUG("Closing all staging file descriptors.");
361 /* Close all the staging file descriptors. */
362 ret = for_lookup_table_entry(w->lookup_table, close_staging_file, NULL);
364 ERROR("Failed to close all staging files");
368 DEBUG("Calculating SHA1 checksums for all new staging files.");
369 /* Calculate SHA1 checksums for all staging files, and merge unnecessary
370 * lookup table entries. */
371 ret = for_dentry_in_tree(root, calculate_sha1sum_for_staging_file,
374 ERROR("Failed to calculate new SHA1 checksums");
378 xml_update_image_info(w, w->current_image);
380 ret = wimlib_overwrite(w, check_integrity);
382 ERROR("Failed to commit changes");
388 /* Called when the filesystem is unmounted. */
389 static void wimfs_destroy(void *p)
392 /* For read-write mounts, the `imagex unmount' command, which is
393 * running in a separate process and is executing the
394 * wimlib_unmount() function, will send this process a byte
395 * through a message queue that indicates whether the --commit
396 * option was specified or not. */
399 struct timespec timeout;
401 ssize_t bytes_received;
404 char check_integrity;
407 ret = open_message_queues(true);
411 msgsize = mq_get_msgsize(unmount_to_daemon_mq);
416 /* Wait at most 3 seconds before giving up and discarding changes. */
417 gettimeofday(&now, NULL);
418 timeout.tv_sec = now.tv_sec + 3;
419 timeout.tv_nsec = now.tv_usec * 1000;
420 DEBUG("Waiting for message telling us whether to commit or not, and "
421 "whether to include integrity checks.");
423 bytes_received = mq_timedreceive(unmount_to_daemon_mq, msg,
424 msgsize, NULL, &timeout);
426 check_integrity = msg[1];
427 if (bytes_received == -1) {
428 if (errno == ETIMEDOUT) {
431 ERROR_WITH_ERRNO("mq_timedreceive()");
433 ERROR("Not committing.");
435 DEBUG("Received message: [%d %d]", msg[0], msg[1]);
439 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
441 status = chdir(working_directory);
443 ERROR_WITH_ERRNO("chdir()");
444 status = WIMLIB_ERR_NOTDIR;
447 status = rebuild_wim(w, (check_integrity != 0));
449 ret = delete_staging_dir();
451 ERROR_WITH_ERRNO("Failed to delete the staging "
458 ret = mq_send(daemon_to_unmount_mq, &status, 1, 1);
460 ERROR_WITH_ERRNO("Failed to send status to unmount process");
461 close_message_queues();
465 * Fills in a `struct stat' that corresponds to a file or directory in the WIM.
467 static int wimfs_getattr(const char *path, struct stat *stbuf)
469 struct dentry *dentry = get_dentry(w, path);
472 dentry_to_stbuf(dentry, stbuf, w->lookup_table);
477 * Create a directory in the WIM.
478 * @mode is currently ignored.
480 static int wimfs_mkdir(const char *path, mode_t mode)
482 struct dentry *parent;
483 struct dentry *newdir;
484 const char *basename;
486 parent = get_parent_dentry(w, path);
490 if (!dentry_is_directory(parent))
493 basename = path_basename(path);
494 if (get_dentry_child_with_name(parent, basename))
497 newdir = new_dentry(basename);
498 newdir->attributes |= FILE_ATTRIBUTE_DIRECTORY;
499 link_dentry(newdir, parent);
503 /* Creates a new staging file and returns its file descriptor opened for
506 * @name_ret: A location into which the a pointer to the newly allocated name of
507 * the staging file is stored.
508 * @return: The file descriptor for the new file. Returns -1 and sets errno on
509 * error, for any reason possible from the creat() function.
511 static int create_staging_file(char **name_ret)
519 name_len = staging_dir_name_len + 1 + WIM_HASH_SIZE;
520 name = MALLOC(name_len + 1);
526 memcpy(name, staging_dir_name, staging_dir_name_len);
527 name[staging_dir_name_len] = '/';
528 randomize_char_array_with_alnum(name + staging_dir_name_len + 1,
530 name[name_len] = '\0';
533 /* Just in case, verify that the randomly generated name doesn't name an
534 * existing file, and try again if so */
535 if (stat(name, &stbuf) == 0) {
536 /* stat succeeded-- the file must exist. Try another name. */
538 return create_staging_file(name_ret);
543 /* doesn't exist--- ok */
546 DEBUG("Creating staging file '%s'", name);
548 fd = creat(name, 0600);
559 /* Creates a regular file. This is done in the staging directory. */
560 static int wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
562 struct dentry *parent, *dentry;
563 const char *basename;
564 struct lookup_table_entry *lte;
569 /* Make sure that the parent of @path exists and is a directory, and
570 * that the dentry named by @path does not already exist. */
571 parent = get_parent_dentry(w, path);
574 if (!dentry_is_directory(parent))
576 basename = path_basename(path);
577 if (get_dentry_child_with_name(parent, path))
580 dentry = new_dentry(basename);
582 /* XXX fill in a temporary random hash value- really should check for
584 randomize_byte_array(dentry->hash, WIM_HASH_SIZE);
586 /* Create a lookup table entry having the same hash value */
587 lte = new_lookup_table_entry();
588 memcpy(lte->hash, dentry->hash, WIM_HASH_SIZE);
590 fd = create_staging_file(&tmpfile_name);
598 lte->staging_file_name = tmpfile_name;
600 /* Insert the lookup table entry, and link the new dentry with its
602 lookup_table_insert(w->lookup_table, lte);
603 link_dentry(dentry, parent);
607 free_lookup_table_entry(lte);
612 static int wimfs_open(const char *path, struct fuse_file_info *fi)
614 struct dentry *dentry;
615 struct lookup_table_entry *lte;
617 dentry = get_dentry(w, path);
621 if (dentry_is_directory(dentry))
623 lte = wim_lookup_resource(w, dentry);
626 /* If this file is in the staging directory and the file is not
627 * currently open, open it. */
628 if (lte->staging_file_name && lte->staging_num_times_opened == 0) {
629 lte->staging_fd = open(lte->staging_file_name, O_RDWR);
630 if (lte->staging_fd == -1)
632 lte->staging_offset = 0;
635 /* no lookup table entry, so the file must be empty. Create a
636 * lookup table entry for the file, unless it's a read-only
641 if (!staging_dir_name) /* Read-only filesystem */
644 lte = new_lookup_table_entry();
648 fd = create_staging_file(&tmpfile_name);
655 lte->resource_entry.original_size = 0;
656 randomize_byte_array(lte->hash, WIM_HASH_SIZE);
657 memcpy(dentry->hash, lte->hash, WIM_HASH_SIZE);
658 lte->staging_file_name = tmpfile_name;
659 lte->staging_fd = fd;
660 lte->staging_offset = 0;
661 lookup_table_insert(w->lookup_table, lte);
663 lte->staging_num_times_opened++;
667 /* Opens a directory. */
668 static int wimfs_opendir(const char *path, struct fuse_file_info *fi)
670 struct dentry *dentry;
672 dentry = get_dentry(w, path);
673 if (!dentry || !dentry_is_directory(dentry))
680 * Read data from a file in the WIM or in the staging directory.
682 static int wimfs_read(const char *path, char *buf, size_t size,
683 off_t offset, struct fuse_file_info *fi)
685 struct dentry *dentry;
686 struct lookup_table_entry *lte;
688 dentry = get_dentry(w, path);
693 if (!dentry_is_regular_file(dentry))
696 lte = wim_lookup_resource(w, dentry);
701 if (lte->staging_file_name) {
703 /* Read from staging */
708 if (lte->staging_num_times_opened == 0)
711 fd = lte->staging_fd;
712 cur_offset = lte->staging_offset;
713 if (cur_offset != offset)
714 if (lseek(fd, offset, SEEK_SET) == -1)
716 lte->staging_offset = offset;
718 ret = read(fd, buf, size);
721 lte->staging_offset = offset + ret;
728 struct resource_entry *res_entry;
731 res_entry = <e->resource_entry;
733 ctype = wim_resource_compression_type(w, res_entry);
735 if (offset > res_entry->original_size)
738 size = min(size, res_entry->original_size - offset);
740 if (read_resource(w->fp, res_entry->size,
741 res_entry->original_size,
742 res_entry->offset, ctype, size,
749 /* Fills in the entries of the directory specified by @path using the
750 * FUSE-provided function @filler. */
751 static int wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
752 off_t offset, struct fuse_file_info *fi)
754 struct dentry *parent;
755 struct dentry *child;
758 parent = get_dentry(w, path);
763 if (!dentry_is_directory(parent))
766 filler(buf, ".", NULL, 0);
767 filler(buf, "..", NULL, 0);
769 child = parent->children;
775 memset(&st, 0, sizeof(st));
776 if (filler(buf, child->file_name_utf8, &st, 0))
779 } while (child != parent->children);
784 static int wimfs_release(const char *path, struct fuse_file_info *fi)
786 struct dentry *dentry;
787 struct lookup_table_entry *lte;
790 dentry = get_dentry(w, path);
793 lte = wim_lookup_resource(w, dentry);
798 if (lte->staging_num_times_opened == 0)
801 if (--lte->staging_num_times_opened == 0 && lte->staging_file_name) {
802 ret = close(lte->staging_fd);
809 /* Renames a file or directory. See rename (3) */
810 static int wimfs_rename(const char *from, const char *to)
814 struct dentry *parent_of_dst;
816 src = get_dentry(w, from);
820 dst = get_dentry(w, to);
823 if (!dentry_is_directory(src)) {
824 /* Cannot rename non-directory to directory. */
825 if (dentry_is_directory(dst))
828 /* Cannot rename directory to a non-directory or a non-empty
830 if (!dentry_is_directory(dst))
832 if (dst->children != NULL)
835 parent_of_dst = dst->parent;
837 lookup_table_decrement_refcnt(w->lookup_table, dst->hash);
840 parent_of_dst = get_parent_dentry(w, to);
846 change_dentry_name(src, path_basename(to));
847 link_dentry(src, parent_of_dst);
848 /*calculate_dentry_full_path(src);*/
852 /* Remove a directory */
853 static int wimfs_rmdir(const char *path)
855 struct dentry *dentry;
857 dentry = get_dentry(w, path);
861 if (!dentry_is_empty_directory(dentry))
864 unlink_dentry(dentry);
869 /* Extracts the resource corresponding to @dentry and its lookup table entry
870 * @lte to a file in the staging directory. The lookup table entry for @dentry
871 * is updated to point to the new file. If @lte has multiple dentries
872 * referencing it, a new lookup table entry is created and the hash of @dentry
873 * is changed to point to the new lookup table entry.
875 * Only @size bytes are extracted, to support truncating the file.
877 * Returns the negative error code on failure.
879 static int extract_resource_to_staging_dir(struct dentry *dentry,
880 struct lookup_table_entry *lte,
885 char *staging_file_name;
886 struct lookup_table_entry *new_lte;
888 /* File in WIM. Copy it to the staging directory. */
889 fd = create_staging_file(&staging_file_name);
893 ret = extract_resource_to_fd(w, <e->resource_entry, fd, size);
899 unlink(staging_file_name);
900 FREE(staging_file_name);
904 if (lte->refcnt != 1) {
905 /* Need to make a new lookup table entry if we are
906 * changing only one copy of a hardlinked entry */
909 new_lte = new_lookup_table_entry();
912 randomize_byte_array(dentry->hash, WIM_HASH_SIZE);
913 memcpy(new_lte->hash, dentry->hash, WIM_HASH_SIZE);
915 new_lte->resource_entry.flags = 0;
916 new_lte->staging_num_times_opened = lte->staging_num_times_opened;
918 lookup_table_insert(w->lookup_table, new_lte);
923 lte->resource_entry.original_size = size;
924 lte->staging_file_name = staging_file_name;
926 if (lte->staging_num_times_opened == 0)
929 lte->staging_fd = fd;
933 /* Reduce the size of a file */
934 static int wimfs_truncate(const char *path, off_t size)
936 struct dentry *dentry;
937 struct lookup_table_entry *lte;
940 dentry = get_dentry(w, path);
943 lte = wim_lookup_resource(w, dentry);
945 if (!lte) /* Already a zero-length file */
947 if (lte->staging_file_name) {
948 /* File on disk. Call POSIX API */
949 if (lte->staging_num_times_opened != 0)
950 ret = ftruncate(lte->staging_fd, size);
952 ret = truncate(lte->staging_file_name, size);
955 dentry_update_all_timestamps(dentry);
956 lte->resource_entry.original_size = size;
959 /* File in WIM. Extract it to the staging directory, but only
960 * the first @size bytes of it. */
961 return extract_resource_to_staging_dir(dentry, lte, size);
965 /* Remove a regular file */
966 static int wimfs_unlink(const char *path)
968 struct dentry *dentry;
969 struct lookup_table_entry *lte;
971 dentry = get_dentry(w, path);
975 if (!dentry_is_regular_file(dentry))
978 lte = wim_lookup_resource(w, dentry);
980 if (lte->staging_file_name)
981 if (unlink(lte->staging_file_name) != 0)
983 lookup_table_decrement_refcnt(w->lookup_table, dentry->hash);
986 unlink_dentry(dentry);
991 static int wimfs_utimens(const char *path, const struct timespec tv[2])
993 struct dentry *dentry = get_dentry(w, path);
996 time_t last_access_t = (tv[0].tv_nsec == UTIME_NOW) ?
997 time(NULL) : tv[0].tv_sec;
998 dentry->last_access_time = unix_timestamp_to_ms(last_access_t);
999 time_t last_mod_t = (tv[1].tv_nsec == UTIME_NOW) ?
1000 time(NULL) : tv[1].tv_sec;
1001 dentry->last_write_time = unix_timestamp_to_ms(last_mod_t);
1005 /* Writes to a file in the WIM filesystem. */
1006 static int wimfs_write(const char *path, const char *buf, size_t size,
1007 off_t offset, struct fuse_file_info *fi)
1009 struct dentry *dentry;
1010 struct lookup_table_entry *lte;
1013 dentry = get_dentry(w, path);
1016 lte = wim_lookup_resource(w, dentry);
1018 if (!lte) /* this should not happen */
1021 if (lte->staging_num_times_opened == 0)
1023 if (lte->staging_file_name) {
1025 /* File in staging directory. We can write to it directly. */
1027 /* Seek to correct position in file if needed. */
1028 if (lte->staging_offset != offset) {
1029 if (lseek(lte->staging_fd, offset, SEEK_SET) == -1)
1031 lte->staging_offset = offset;
1034 /* Write the data. */
1035 ret = write(lte->staging_fd, buf, size);
1039 /* Adjust the stored offset of staging_fd. */
1040 lte->staging_offset = offset + ret;
1042 /* Increase file size if needed. */
1043 if (lte->resource_entry.original_size < lte->staging_offset)
1044 lte->resource_entry.original_size = lte->staging_offset;
1046 /* The file has been modified, so all its timestamps must be
1048 dentry_update_all_timestamps(dentry);
1051 /* File in the WIM. We must extract it to the staging directory
1052 * before it can be written to. */
1053 ret = extract_resource_to_staging_dir(dentry, lte,
1054 lte->resource_entry.original_size);
1058 return wimfs_write(path, buf, size, offset, fi);
1063 static struct fuse_operations wimfs_oper = {
1064 .access = wimfs_access,
1065 .destroy = wimfs_destroy,
1066 .getattr = wimfs_getattr,
1067 .mkdir = wimfs_mkdir,
1068 .mknod = wimfs_mknod,
1070 .opendir = wimfs_opendir,
1072 .readdir = wimfs_readdir,
1073 .release = wimfs_release,
1074 .rename = wimfs_rename,
1075 .rmdir = wimfs_rmdir,
1076 .truncate = wimfs_truncate,
1077 .unlink = wimfs_unlink,
1078 .utimens = wimfs_utimens,
1079 .write = wimfs_write,
1083 /* Mounts a WIM file. */
1084 WIMLIBAPI int wimlib_mount(WIMStruct *wim, int image, const char *dir,
1092 DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
1093 wim, image, dir, flags);
1096 return WIMLIB_ERR_INVALID_PARAM;
1098 ret = wimlib_select_image(wim, image);
1103 if (flags & WIMLIB_MOUNT_FLAG_READWRITE)
1104 wim_get_current_image_metadata(wim)->modified = true;
1106 if (!(flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
1107 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
1108 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
1109 flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1112 working_directory = getcwd(NULL, 0);
1113 if (!working_directory) {
1114 ERROR_WITH_ERRNO("Could not determine current directory");
1115 return WIMLIB_ERR_NOTDIR;
1120 return WIMLIB_ERR_NOMEM;
1122 argv[argc++] = "mount";
1124 argv[argc++] = "-s"; /* disable multi-threaded operation */
1126 if (flags & WIMLIB_MOUNT_FLAG_DEBUG) {
1127 argv[argc++] = "-d";
1129 if (!(flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
1130 argv[argc++] = "-o";
1131 argv[argc++] = "ro";
1134 if (!staging_dir_name) {
1136 return WIMLIB_ERR_MKDIR;
1143 DEBUG("FUSE command line (argc = %d): ", argc);
1144 for (i = 0; i < argc; i++) {
1145 fputs(argv[i], stdout);
1153 /* Set static variables. */
1155 mount_flags = flags;
1157 ret = fuse_main(argc, argv, &wimfs_oper, NULL);
1159 return (ret == 0) ? 0 : WIMLIB_ERR_FUSE;
1164 * Unmounts the WIM file that was previously mounted on @dir by using
1167 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
1174 struct timespec timeout;
1178 /* Execute `fusermount -u', which is installed setuid root, to unmount
1181 * FUSE does not yet implement synchronous unmounts. This means that
1182 * fusermount -u will return before the filesystem daemon returns from
1183 * wimfs_destroy(). This is partly what we want, because we need to
1184 * send a message from this process to the filesystem daemon telling
1185 * whether --commit was specified or not. However, after that, the
1186 * unmount process must wait for the filesystem daemon to finish writing
1193 ERROR_WITH_ERRNO("Failed to fork()");
1194 return WIMLIB_ERR_FORK;
1197 execlp("fusermount", "fusermount", "-u", dir, NULL);
1198 ERROR_WITH_ERRNO("Failed to execute `fusermount'");
1199 return WIMLIB_ERR_FUSERMOUNT;
1202 ret = waitpid(pid, &status, 0);
1204 ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
1206 return WIMLIB_ERR_FUSERMOUNT;
1210 ERROR("fusermount exited with status %d", status);
1211 return WIMLIB_ERR_FUSERMOUNT;
1214 /* Open message queues between the unmount process and the
1215 * filesystem daemon. */
1216 ret = open_message_queues(false);
1220 /* Send a message to the filesystem saying whether to commit or
1222 msg[0] = (flags & WIMLIB_UNMOUNT_FLAG_COMMIT) ? 1 : 0;
1223 msg[1] = (flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY) ? 1 : 0;
1225 DEBUG("Sending message: %s, %s",
1226 (msg[0] == 0) ? "don't commit" : "commit",
1227 (msg[1] == 0) ? "don't check" : "check");
1228 ret = mq_send(unmount_to_daemon_mq, msg, 2, 1);
1230 ERROR("Failed to notify filesystem daemon whether we want to "
1231 "commit changes or not");
1232 close_message_queues();
1233 return WIMLIB_ERR_MQUEUE;
1236 /* Wait for a message from the filesytem daemon indicating whether the
1237 * filesystem was unmounted successfully (0) or an error occurred (1).
1238 * This may take a long time if a big WIM file needs to be rewritten. */
1240 /* Wait at most 600??? seconds before giving up and returning false.
1241 * Either it's a really big WIM file, or (more likely) the
1242 * filesystem daemon has crashed or failed for some reason.
1244 * XXX come up with some method to determine if the filesystem
1245 * daemon has really crashed or not. */
1247 gettimeofday(&now, NULL);
1248 timeout.tv_sec = now.tv_sec + 600;
1249 timeout.tv_nsec = now.tv_usec * 1000;
1251 msgsize = mq_get_msgsize(daemon_to_unmount_mq);
1252 char mailbox[msgsize];
1255 DEBUG("Waiting for message telling us whether the unmount was "
1256 "successful or not.");
1257 ret = mq_timedreceive(daemon_to_unmount_mq, mailbox, msgsize,
1260 close_message_queues();
1262 if (errno_save == ETIMEDOUT) {
1263 ERROR("Timed out- probably the filesystem daemon "
1264 "crashed and the WIM was not written "
1266 return WIMLIB_ERR_TIMEOUT;
1268 ERROR("mq_receive(): %s", strerror(errno_save));
1269 return WIMLIB_ERR_MQUEUE;
1273 DEBUG("Received message: %s",
1274 (mailbox[0] == 0) ? "Unmount OK" : "Unmount Failed");
1275 if (mailbox[0] != 0)
1276 ERROR("Unmount failed");
1280 #else /* WITH_FUSE */
1283 static inline int mount_unsupported_error()
1285 ERROR("WIMLIB was compiled with --without-fuse, which disables support "
1286 "for mounting WIMs.");
1287 return WIMLIB_ERR_UNSUPPORTED;
1290 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
1292 return mount_unsupported_error();
1295 WIMLIBAPI int wimlib_mount(WIMStruct *wim_p, int image, const char *dir,
1298 return mount_unsupported_error();
1301 #endif /* WITH_FUSE */