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"
36 #include "timestamp.h"
40 #define FUSE_USE_VERSION 26
52 struct lookup_table_entry *lte;
53 struct dentry *dentry;
56 /* The WIMStruct for the mounted WIM. */
59 /* Working directory when `imagex mount' is run. */
60 static const char *working_directory;
62 /* Name of the staging directory for a read-write mount. Whenever a new file is
63 * created, it is done so in the staging directory. Furthermore, whenever a
64 * file in the WIM is modified, it is extracted to the staging directory. If
65 * changes are commited when the WIM is unmounted, the file resources are merged
66 * in from the staging directory when writing the new WIM. */
67 static char *staging_dir_name;
68 static size_t staging_dir_name_len;
70 /* Flags passed to wimlib_mount(). */
71 static int mount_flags;
73 /* Name of the directory on which the WIM file is mounted. */
74 static const char *mount_dir;
77 static inline int get_lookup_flags()
79 if (mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
80 return LOOKUP_FLAG_ADS_OK;
85 static inline int flags_writable(int open_flags)
87 return open_flags & (O_RDWR | O_WRONLY);
90 static int alloc_wimlib_fd(struct lookup_table_entry *lte,
91 struct wimlib_fd **fd_ret)
93 static const u16 fds_per_alloc = 8;
94 static const u16 max_fds = 0xffff;
96 if (lte->num_opened_fds == lte->num_allocated_fds) {
97 struct wimlib_fd **fds;
100 if (lte->num_allocated_fds == max_fds)
102 num_new_fds = min(fds_per_alloc, max_fds - lte->num_allocated_fds);
104 fds = CALLOC(lte->num_allocated_fds + num_new_fds,
105 sizeof(lte->fds[0]));
108 memcpy(fds, lte->fds,
109 lte->num_allocated_fds * sizeof(lte->fds[0]));
112 lte->num_allocated_fds += num_new_fds;
114 for (u16 i = 0; ; i++) {
116 struct wimlib_fd *fd = CALLOC(1, sizeof(*fd));
123 lte->num_opened_fds++;
130 static int close_wimlib_fd(struct wimlib_fd *fd)
132 struct lookup_table_entry *lte = fd->lte;
135 wimlib_assert(lte->num_opened_fds);
137 if (lte->staging_file_name) {
138 wimlib_assert(fd->staging_fd != -1);
139 if (close(fd->staging_fd) != 0)
142 if (--lte->num_opened_fds == 0 && lte->refcnt == 0)
143 free_lookup_table_entry(lte);
144 lte->fds[fd->idx] = NULL;
149 static void remove_dentry(struct dentry *dentry,
150 struct lookup_table *lookup_table)
152 const u8 *hash = dentry->hash;
154 struct lookup_table_entry *lte;
156 lte = lookup_table_decrement_refcnt(lookup_table, hash);
157 if (lte && lte->num_opened_fds)
158 for (u16 i = 0; i < lte->num_allocated_fds; i++)
159 if (lte->fds[i] && lte->fds[i]->dentry == dentry)
160 lte->fds[i]->dentry = NULL;
161 if (i == dentry->num_ads)
163 hash = dentry->ads_entries[i].hash;
167 unlink_dentry(dentry);
171 static void dentry_increment_lookup_table_refcnts(struct dentry *dentry,
172 struct lookup_table *lookup_table)
175 const u8 *hash = dentry->hash;
176 struct lookup_table_entry *lte;
178 lte = __lookup_resource(lookup_table, hash);
181 if (i == dentry->num_ads)
183 hash = dentry->ads_entries[i].hash;
188 /* Creates a new staging file and returns its file descriptor opened for
191 * @name_ret: A location into which the a pointer to the newly allocated name of
192 * the staging file is stored.
193 * @return: The file descriptor for the new file. Returns -1 and sets errno on
194 * error, for any reason possible from the creat() function.
196 static int create_staging_file(char **name_ret, int open_flags)
204 name_len = staging_dir_name_len + 1 + WIM_HASH_SIZE;
205 name = MALLOC(name_len + 1);
213 memcpy(name, staging_dir_name, staging_dir_name_len);
214 name[staging_dir_name_len] = '/';
215 randomize_char_array_with_alnum(name + staging_dir_name_len + 1,
217 name[name_len] = '\0';
220 /* Just in case, verify that the randomly generated name doesn't name an
221 * existing file, and try again if so */
222 } while (stat(name, &stbuf) == 0);
228 /* doesn't exist--- ok */
230 DEBUG("Creating staging file `%s'", name);
232 fd = open(name, open_flags | O_CREAT | O_TRUNC, 0600);
244 * Extract a WIM resource to the staging directory.
247 * - Create a staging file for the WIM resource
248 * - Extract the resource to it
249 * - Create a new lte for the file resource
250 * - Transfer fds from the old lte to the new lte, but
251 * only if they share the same hard link group as this
254 static int extract_resource_to_staging_dir(struct dentry *dentry,
255 struct lookup_table_entry **lte,
258 char *staging_file_name;
261 struct lookup_table_entry *old_lte, *new_lte;
262 size_t link_group_size;
265 fd = create_staging_file(&staging_file_name, O_WRONLY);
270 ret = extract_resource_to_fd(w, &old_lte->resource_entry, fd,
274 if (ret != 0 || close(fd) != 0) {
280 goto out_delete_staging_file;
283 link_group_size = dentry_link_group_size(dentry);
286 if (link_group_size == old_lte->refcnt) {
287 /* This hard link group is the only user of the lookup
288 * table entry, so we can re-use it. */
289 lookup_table_remove(w->lookup_table, old_lte);
292 /* Split a hard link group away from the "lookup table
293 * entry" hard link group (i.e. we had two hard link
294 * groups that were identical, but now we are changing
297 /* XXX The ADS really complicate things here and not
298 * everything is going to work correctly yet. For
299 * example it could be the same that a file contains two
300 * file streams that are identical and therefore share
301 * the same lookup table entry despite the fact that the
302 * streams themselves are not hardlinked. */
303 wimlib_assert(old_lte->refcnt > link_group_size);
305 new_lte = new_lookup_table_entry();
308 goto out_delete_staging_file;
311 u16 num_transferred_fds = 0;
312 for (u16 i = 0; i < old_lte->num_allocated_fds; i++) {
313 if (old_lte->fds[i] &&
314 old_lte->fds[i]->dentry->hard_link ==
317 num_transferred_fds++;
320 new_lte->fds = MALLOC(num_transferred_fds *
321 sizeof(new_lte->fds[0]));
323 free_lookup_table_entry(new_lte);
325 goto out_delete_staging_file;
328 for (u16 i = 0; i < old_lte->num_allocated_fds; i++) {
329 if (old_lte->fds[i] &&
330 old_lte->fds[i]->dentry->hard_link ==
333 struct wimlib_fd *fd = old_lte->fds[i];
334 old_lte->fds[i] = NULL;
337 new_lte->fds[j] = fd;
340 old_lte->refcnt -= link_group_size;
341 old_lte->num_opened_fds -= j;
342 new_lte->num_opened_fds = j;
346 new_lte = new_lookup_table_entry();
349 goto out_delete_staging_file;
352 new_lte->resource_entry.original_size = size;
353 new_lte->refcnt = link_group_size;
354 randomize_byte_array(new_lte->hash, WIM_HASH_SIZE);
355 new_lte->staging_file_name = staging_file_name;
357 lookup_table_insert(w->lookup_table, new_lte);
360 out_delete_staging_file:
361 unlink(staging_file_name);
362 FREE(staging_file_name);
367 * Creates a randomly named staging directory and returns its name into the
368 * static variable staging_dir_name.
370 * If the staging directory cannot be created, staging_dir_name is set to NULL.
372 static void make_staging_dir()
374 /* XXX Give the user an option of where to stage files */
376 static char prefix[] = "wimlib-staging-";
377 static const size_t prefix_len = 15;
378 static const size_t suffix_len = 10;
380 size_t pwd_len = strlen(working_directory);
382 staging_dir_name_len = pwd_len + 1 + prefix_len + suffix_len;
384 staging_dir_name = MALLOC(staging_dir_name_len + 1);
385 if (!staging_dir_name) {
386 ERROR("Out of memory");
390 memcpy(staging_dir_name, working_directory, pwd_len);
391 staging_dir_name[pwd_len] = '/';
392 memcpy(staging_dir_name + pwd_len + 1, prefix, prefix_len);
393 randomize_char_array_with_alnum(staging_dir_name + pwd_len + 1 + prefix_len,
395 staging_dir_name[staging_dir_name_len] = '\0';
397 if (mkdir(staging_dir_name, 0700) != 0) {
398 ERROR_WITH_ERRNO("Failed to create temporary directory `%s'",
400 FREE(staging_dir_name);
401 staging_dir_name = NULL;
405 static int remove_file_or_directory(const char *fpath, const struct stat *sb,
406 int typeflag, struct FTW *ftwbuf)
408 if (remove(fpath) == 0)
411 return WIMLIB_ERR_DELETE_STAGING_DIR;
416 * Deletes the staging directory and all the files contained in it.
418 static inline int delete_staging_dir()
422 ret = nftw(staging_dir_name, remove_file_or_directory,10, FTW_DEPTH);
423 staging_dir_name = NULL;
427 /* Name and message queue descriptors for message queues between the filesystem
428 * daemon process and the unmount process. These are used when the filesystem
429 * is unmounted and the process running wimlib_mount() (i.e. the `imagex
430 * unmount' command) needs to communicate with the filesystem daemon running
431 * fuse_main() (i.e. that spawned by the `imagex mount' or `imagex mountrw'
433 static char *unmount_to_daemon_mq_name;
434 static char *daemon_to_unmount_mq_name;
435 static int unmount_to_daemon_mq;
436 static int daemon_to_unmount_mq;
438 /* Simple function that returns the concatenation of 4 strings. */
439 static char *strcat_dup(const char *s1, const char *s2, const char *s3,
442 size_t len = strlen(s1) + strlen(s2) + strlen(s3) + strlen(s4) + 1;
443 char *p = MALLOC(len);
454 /* Removes trailing forward slashes in a string. */
455 static void remove_trailing_slashes(char *s)
457 long len = strlen(s);
458 for (long i = len - 1; i >= 1; i--) {
466 /* Changes forward slashes to underscores in a string. */
467 static void s_slashes_underscores_g(char *s)
477 * Opens two POSIX message queue: one for sending messages from the unmount
478 * process to the daemon process, and one to go the other way. The names of the
479 * message queues, which must be system-wide unique, are be based on the mount
480 * point. (There of course is still a possibility of a collision if one were to
481 * unmount two identically named directories simultaneously...)
483 * @daemon specifies whether the calling process is the filesystem daemon or the
486 static int open_message_queues(bool daemon)
488 static const char *slash = "/";
489 static const char *prefix = "wimlib-";
490 static const char *u2d_suffix = "unmount-to-daemon-mq";
491 static const char *d2u_suffix = "daemon-to-unmount-mq";
493 const char *mount_dir_basename = path_basename(mount_dir);
497 unmount_to_daemon_mq_name = strcat_dup(slash, mount_dir_basename,
499 if (!unmount_to_daemon_mq_name) {
500 ERROR("Out of memory");
501 return WIMLIB_ERR_NOMEM;
503 daemon_to_unmount_mq_name = strcat_dup(slash, mount_dir_basename,
505 if (!daemon_to_unmount_mq_name) {
506 ERROR("Out of memory");
507 ret = WIMLIB_ERR_NOMEM;
511 remove_trailing_slashes(unmount_to_daemon_mq_name);
512 remove_trailing_slashes(daemon_to_unmount_mq_name);
513 s_slashes_underscores_g(unmount_to_daemon_mq_name + 1);
514 s_slashes_underscores_g(daemon_to_unmount_mq_name + 1);
517 flags = O_RDONLY | O_CREAT;
519 flags = O_WRONLY | O_CREAT;
521 unmount_to_daemon_mq = mq_open(unmount_to_daemon_mq_name, flags,
524 if (unmount_to_daemon_mq == -1) {
525 ERROR_WITH_ERRNO("mq_open()");
526 ret = WIMLIB_ERR_MQUEUE;
531 flags = O_WRONLY | O_CREAT;
533 flags = O_RDONLY | O_CREAT;
535 daemon_to_unmount_mq = mq_open(daemon_to_unmount_mq_name, flags,
538 if (daemon_to_unmount_mq == -1) {
539 ERROR_WITH_ERRNO("mq_open()");
540 ret = WIMLIB_ERR_MQUEUE;
545 mq_close(unmount_to_daemon_mq);
546 mq_unlink(unmount_to_daemon_mq_name);
548 FREE(daemon_to_unmount_mq_name);
550 FREE(unmount_to_daemon_mq_name);
554 static int mq_get_msgsize(mqd_t mq)
556 static const char *msgsize_max_file = "/proc/sys/fs/mqueue/msgsize_max";
561 if (mq_getattr(unmount_to_daemon_mq, &attr) == 0) {
562 msgsize = attr.mq_msgsize;
564 ERROR_WITH_ERRNO("mq_getattr()");
565 ERROR("Attempting to read %s", msgsize_max_file);
566 fp = fopen(msgsize_max_file, "rb");
568 if (fscanf(fp, "%d", &msgsize) != 1) {
569 ERROR("Assuming message size of 8192");
574 ERROR_WITH_ERRNO("Failed to open the file `%s'",
576 ERROR("Assuming message size of 8192");
583 /* Closes the message queues, which are allocated in static variables */
584 static void close_message_queues()
586 mq_close(unmount_to_daemon_mq);
587 mq_close(daemon_to_unmount_mq);
588 mq_unlink(unmount_to_daemon_mq_name);
589 mq_unlink(daemon_to_unmount_mq_name);
592 static int wimfs_access(const char *path, int mask)
594 /* XXX Permissions not implemented */
598 /* Closes the staging file descriptor associated with the lookup table entry, if
600 static int close_lte_fds(struct lookup_table_entry *lte, void *ignore)
602 for (u16 i = 0; i < lte->num_opened_fds; i++) {
603 if (lte->fds[i] && lte->fds[i]->staging_fd != -1) {
604 if (close(lte->fds[i]->staging_fd) != 0) {
605 ERROR_WITH_ERRNO("Failed close file `%s'",
606 lte->staging_file_name);
607 return WIMLIB_ERR_WRITE;
615 /* Calculates the SHA1 sum for @dentry if its file resource is in a staging
616 * file. Updates the SHA1 sum in the dentry and the lookup table entry. If
617 * there is already a lookup table entry with the same checksum, increment its
618 * reference count and destroy the lookup entry with the updated checksum. */
619 static int calculate_sha1sum_for_staging_file(struct dentry *dentry,
622 struct lookup_table *lookup_table = _lookup_table;
623 u8 *hash = dentry->hash;
626 struct lookup_table_entry *lte = __lookup_resource(lookup_table, hash);
627 if (lte && lte->staging_file_name) {
628 struct lookup_table_entry *existing;
631 DEBUG("Calculating SHA1 hash for file `%s'",
632 dentry->file_name_utf8);
633 ret = sha1sum(lte->staging_file_name, lte->hash);
637 lookup_table_unlink(lookup_table, lte);
638 memcpy(hash, lte->hash, WIM_HASH_SIZE);
639 existing = __lookup_resource(lookup_table, hash);
641 DEBUG("Merging duplicate lookup table entries for file "
642 "`%s'", dentry->file_name_utf8);
643 free_lookup_table_entry(lte);
646 lookup_table_insert(lookup_table, lte);
649 if (i == dentry->num_ads)
651 hash = dentry->ads_entries[i].hash;
657 /* Overwrites the WIM file, with changes saved. */
658 static int rebuild_wim(WIMStruct *w, bool check_integrity)
663 root = wim_root_dentry(w);
665 DEBUG("Closing all staging file descriptors.");
666 /* Close all the staging file descriptors. */
667 ret = for_lookup_table_entry(w->lookup_table, close_lte_fds, NULL);
669 ERROR("Failed to close all staging files");
673 DEBUG("Calculating SHA1 checksums for all new staging files.");
674 /* Calculate SHA1 checksums for all staging files, and merge unnecessary
675 * lookup table entries. */
676 ret = for_dentry_in_tree(root, calculate_sha1sum_for_staging_file,
679 ERROR("Failed to calculate new SHA1 checksums");
683 xml_update_image_info(w, w->current_image);
685 ret = wimlib_overwrite(w, check_integrity);
687 ERROR("Failed to commit changes");
693 /* Called when the filesystem is unmounted. */
694 static void wimfs_destroy(void *p)
697 /* For read-write mounts, the `imagex unmount' command, which is
698 * running in a separate process and is executing the
699 * wimlib_unmount() function, will send this process a byte
700 * through a message queue that indicates whether the --commit
701 * option was specified or not. */
704 struct timespec timeout;
706 ssize_t bytes_received;
709 char check_integrity;
712 ret = open_message_queues(true);
716 msgsize = mq_get_msgsize(unmount_to_daemon_mq);
721 /* Wait at most 3 seconds before giving up and discarding changes. */
722 gettimeofday(&now, NULL);
723 timeout.tv_sec = now.tv_sec + 3;
724 timeout.tv_nsec = now.tv_usec * 1000;
725 DEBUG("Waiting for message telling us whether to commit or not, and "
726 "whether to include integrity checks.");
728 bytes_received = mq_timedreceive(unmount_to_daemon_mq, msg,
729 msgsize, NULL, &timeout);
731 check_integrity = msg[1];
732 if (bytes_received == -1) {
733 if (errno == ETIMEDOUT) {
736 ERROR_WITH_ERRNO("mq_timedreceive()");
738 ERROR("Not committing.");
740 DEBUG("Received message: [%d %d]", msg[0], msg[1]);
744 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
746 status = chdir(working_directory);
748 ERROR_WITH_ERRNO("chdir()");
749 status = WIMLIB_ERR_NOTDIR;
752 status = rebuild_wim(w, (check_integrity != 0));
754 ret = delete_staging_dir();
756 ERROR_WITH_ERRNO("Failed to delete the staging "
763 ret = mq_send(daemon_to_unmount_mq, &status, 1, 1);
765 ERROR_WITH_ERRNO("Failed to send status to unmount process");
766 close_message_queues();
769 static int wimfs_fgetattr(const char *path, struct stat *stbuf,
770 struct fuse_file_info *fi)
772 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
773 dentry_to_stbuf(fd->dentry, stbuf, w->lookup_table);
777 static int wimfs_ftruncate(const char *path, off_t size,
778 struct fuse_file_info *fi)
780 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
781 int ret = ftruncate(fd->staging_fd, size);
784 fd->lte->resource_entry.original_size = size;
789 * Fills in a `struct stat' that corresponds to a file or directory in the WIM.
791 static int wimfs_getattr(const char *path, struct stat *stbuf)
793 struct dentry *dentry = get_dentry(w, path);
796 dentry_to_stbuf(dentry, stbuf, w->lookup_table);
800 /* Create a hard link */
801 static int wimfs_link(const char *to, const char *from)
803 struct dentry *to_dentry, *from_dentry, *from_dentry_parent;
804 const char *link_name;
806 to_dentry = get_dentry(w, to);
809 if (!dentry_is_regular_file(to_dentry))
812 from_dentry_parent = get_parent_dentry(w, from);
813 if (!from_dentry_parent)
815 if (!dentry_is_directory(from_dentry_parent))
818 link_name = path_basename(from);
819 if (get_dentry_child_with_name(from_dentry_parent, link_name))
821 from_dentry = clone_dentry(to_dentry);
824 if (change_dentry_name(from_dentry, link_name) != 0) {
828 list_add(&from_dentry->link_group_list, &to_dentry->link_group_list);
829 link_dentry(from_dentry, from_dentry_parent);
830 dentry_increment_lookup_table_refcnts(from_dentry, w->lookup_table);
835 * Create a directory in the WIM.
836 * @mode is currently ignored.
838 static int wimfs_mkdir(const char *path, mode_t mode)
840 struct dentry *parent;
841 struct dentry *newdir;
842 const char *basename;
844 parent = get_parent_dentry(w, path);
848 if (!dentry_is_directory(parent))
851 basename = path_basename(path);
852 if (get_dentry_child_with_name(parent, basename))
855 newdir = new_dentry(basename);
856 newdir->attributes |= FILE_ATTRIBUTE_DIRECTORY;
857 link_dentry(newdir, parent);
862 /* Creates a regular file. */
863 static int wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
865 const char *stream_name;
866 if ((mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
867 && (stream_name = path_stream_name(path))) {
868 struct ads_entry *new_entry;
869 struct dentry *dentry;
871 dentry = get_dentry(w, path);
872 if (!dentry || !dentry_is_regular_file(dentry))
874 if (dentry_get_ads_entry(dentry, stream_name))
876 new_entry = dentry_add_ads(dentry, stream_name);
880 struct dentry *dentry, *parent;
881 const char *basename;
883 /* Make sure that the parent of @path exists and is a directory, and
884 * that the dentry named by @path does not already exist. */
885 parent = get_parent_dentry(w, path);
888 if (!dentry_is_directory(parent))
891 basename = path_basename(path);
892 if (get_dentry_child_with_name(parent, path))
895 dentry = new_dentry(basename);
898 link_dentry(dentry, parent);
905 static int wimfs_open(const char *path, struct fuse_file_info *fi)
907 struct dentry *dentry;
908 struct lookup_table_entry *lte;
911 struct wimlib_fd *fd;
913 ret = lookup_resource(w, path, get_lookup_flags(), &dentry, <e,
919 /* Empty file with no lookup-table entry. This is fine if it's
920 * a read-only filesystem. Otherwise we need to create a lookup
921 * table entry so that we can keep track of the file descriptors
922 * (this is important in case someone opens the file for
924 if (!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
929 ret = extract_resource_to_staging_dir(dentry, <e, 0);
932 memcpy(dentry_hash, lte->hash, WIM_HASH_SIZE);
935 ret = alloc_wimlib_fd(lte, &fd);
941 /* The file resource may be in the staging directory (read-write
942 * mounts only) or in the WIM. If it's in the staging
943 * directory, we need to open a native file descriptor for the
944 * corresponding file. Otherwise, we can read the file resource
945 * directly from the WIM file if we are opening it read-only,
946 * but we need to extract the resource to the staging directory
947 * if we are opening it writable. */
948 if (flags_writable(fi->flags) && !lte->staging_file_name) {
949 ret = extract_resource_to_staging_dir(dentry, <e,
950 lte->resource_entry.original_size);
953 memcpy(dentry_hash, lte->hash, WIM_HASH_SIZE);
955 if (lte->staging_file_name) {
956 fd->staging_fd = open(lte->staging_file_name, fi->flags);
957 if (fd->staging_fd == -1) {
962 fi->fh = (uint64_t)fd;
966 /* Opens a directory. */
967 static int wimfs_opendir(const char *path, struct fuse_file_info *fi)
969 struct dentry *dentry;
971 dentry = get_dentry(w, path);
974 if (!dentry_is_directory(dentry))
976 dentry->num_times_opened++;
977 fi->fh = (uint64_t)dentry;
983 * Read data from a file in the WIM or in the staging directory.
985 static int wimfs_read(const char *path, char *buf, size_t size,
986 off_t offset, struct fuse_file_info *fi)
988 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
991 /* Empty file with no lookup table entry on read-only mounted
993 wimlib_assert(!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE));
997 if (fd->lte->staging_file_name) {
998 /* Read from staging file */
1000 wimlib_assert(fd->staging_fd != -1);
1003 DEBUG("Seek to offset %zu", offset);
1005 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1007 ret = read(fd->staging_fd, buf, size);
1015 struct resource_entry *res_entry;
1018 res_entry = &fd->lte->resource_entry;
1020 ctype = wim_resource_compression_type(w, res_entry);
1022 if (offset > res_entry->original_size)
1025 size = min(size, res_entry->original_size - offset);
1027 if (read_resource(w->fp, res_entry->size,
1028 res_entry->original_size,
1029 res_entry->offset, ctype, size,
1036 /* Fills in the entries of the directory specified by @path using the
1037 * FUSE-provided function @filler. */
1038 static int wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
1039 off_t offset, struct fuse_file_info *fi)
1041 struct dentry *parent, *child;
1043 parent = (struct dentry*)fi->fh;
1044 child = parent->children;
1046 filler(buf, ".", NULL, 0);
1047 filler(buf, "..", NULL, 0);
1053 if (filler(buf, child->file_name_utf8, NULL, 0))
1055 child = child->next;
1056 } while (child != parent->children);
1061 static int wimfs_readlink(const char *path, char *buf, size_t buf_len)
1063 struct dentry *dentry = get_dentry(w, path);
1067 if (!dentry_is_symlink(dentry))
1070 ret = dentry_readlink(dentry, buf, buf_len, w);
1077 static int wimfs_release(const char *path, struct fuse_file_info *fi)
1080 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
1083 /* Empty file with no lookup table entry on read-only mounted
1085 wimlib_assert(!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE));
1089 if (flags_writable(fi->flags) && fd->dentry) {
1090 u64 now = get_timestamp();
1091 fd->dentry->last_access_time = now;
1092 fd->dentry->last_write_time = now;
1095 return close_wimlib_fd(fd);
1098 static int wimfs_releasedir(const char *path, struct fuse_file_info *fi)
1100 struct dentry *dentry = (struct dentry *)fi->fh;
1102 wimlib_assert(dentry->num_times_opened);
1103 if (--dentry->num_times_opened == 0)
1104 free_dentry(dentry);
1108 /* Renames a file or directory. See rename (3) */
1109 static int wimfs_rename(const char *from, const char *to)
1113 struct dentry *parent_of_dst;
1114 char *file_name_utf16 = NULL, *file_name_utf8 = NULL;
1115 u16 file_name_utf16_len, file_name_utf8_len;
1118 /* This rename() implementation currently only supports actual files
1119 * (not alternate data streams) */
1121 src = get_dentry(w, from);
1125 dst = get_dentry(w, to);
1128 ret = get_names(&file_name_utf16, &file_name_utf8,
1129 &file_name_utf16_len, &file_name_utf8_len,
1135 if (src == dst) /* Same file */
1138 if (!dentry_is_directory(src)) {
1139 /* Cannot rename non-directory to directory. */
1140 if (dentry_is_directory(dst))
1143 /* Cannot rename directory to a non-directory or a non-empty
1145 if (!dentry_is_directory(dst))
1147 if (dst->children != NULL)
1150 parent_of_dst = dst->parent;
1151 remove_dentry(dst, w->lookup_table);
1153 parent_of_dst = get_parent_dentry(w, to);
1158 FREE(src->file_name);
1159 FREE(src->file_name_utf8);
1160 src->file_name = file_name_utf16;
1161 src->file_name_utf8 = file_name_utf8;
1162 src->file_name_len = file_name_utf16_len;
1163 src->file_name_utf8_len = file_name_utf8_len;
1166 link_dentry(src, parent_of_dst);
1170 /* Remove a directory */
1171 static int wimfs_rmdir(const char *path)
1173 struct dentry *dentry;
1175 dentry = get_dentry(w, path);
1179 if (!dentry_is_empty_directory(dentry))
1182 unlink_dentry(dentry);
1183 if (dentry->num_times_opened == 0)
1184 free_dentry(dentry);
1188 static int wimfs_symlink(const char *to, const char *from)
1190 struct dentry *dentry_parent, *dentry;
1191 const char *link_name;
1193 dentry_parent = get_parent_dentry(w, from);
1196 if (!dentry_is_directory(dentry_parent))
1199 link_name = path_basename(from);
1201 if (get_dentry_child_with_name(dentry_parent, link_name))
1203 dentry = new_dentry(link_name);
1207 dentry->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
1208 dentry->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
1210 if (dentry_set_symlink(dentry, to, w->lookup_table) != 0)
1211 goto out_free_dentry;
1213 link_dentry(dentry, dentry_parent);
1216 free_dentry(dentry);
1221 /* Reduce the size of a file */
1222 static int wimfs_truncate(const char *path, off_t size)
1224 struct dentry *dentry;
1225 struct lookup_table_entry *lte;
1229 ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1230 <e, &dentry_hash);
1235 if (!lte) /* Already a zero-length file */
1238 if (lte->staging_file_name) {
1239 ret = truncate(lte->staging_file_name, size);
1242 lte->resource_entry.original_size = size;
1244 /* File in WIM. Extract it to the staging directory, but only
1245 * the first @size bytes of it. */
1246 ret = extract_resource_to_staging_dir(dentry, <e, size);
1248 dentry_update_all_timestamps(dentry);
1252 /* Remove a regular file */
1253 static int wimfs_unlink(const char *path)
1255 struct dentry *dentry;
1256 struct lookup_table_entry *lte;
1260 ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1261 <e, &dentry_hash);
1266 if (lte && lte->staging_file_name)
1267 if (unlink(lte->staging_file_name) != 0)
1270 if (dentry_hash == dentry->hash) {
1271 /* We are removing the full dentry including all alternate data
1273 remove_dentry(dentry, w->lookup_table);
1275 /* We are removing an alternate data stream. */
1276 struct ads_entry *cur_entry = dentry->ads_entries;
1277 while (cur_entry->hash != dentry_hash)
1279 lookup_table_decrement_refcnt(w->lookup_table, cur_entry->hash);
1281 dentry_remove_ads(dentry, cur_entry);
1283 /* Beware: The lookup table entry(s) may still be referenced by users
1284 * that have opened the corresponding streams. They are freed later in
1285 * wimfs_release() when the last file user has closed the stream. */
1289 /* Change the timestamp on a file dentry.
1291 * There is no distinction between a file and its alternate data streams here. */
1292 static int wimfs_utimens(const char *path, const struct timespec tv[2])
1294 struct dentry *dentry = get_dentry(w, path);
1297 time_t last_access_t = (tv[0].tv_nsec == UTIME_NOW) ?
1298 time(NULL) : tv[0].tv_sec;
1299 dentry->last_access_time = unix_timestamp_to_ms(last_access_t);
1300 time_t last_mod_t = (tv[1].tv_nsec == UTIME_NOW) ?
1301 time(NULL) : tv[1].tv_sec;
1302 dentry->last_write_time = unix_timestamp_to_ms(last_mod_t);
1306 /* Writes to a file in the WIM filesystem.
1307 * It may be an alternate data stream, but here we don't even notice because we
1308 * just get a lookup table entry. */
1309 static int wimfs_write(const char *path, const char *buf, size_t size,
1310 off_t offset, struct fuse_file_info *fi)
1312 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
1315 wimlib_assert(fd->lte);
1316 wimlib_assert(fd->lte->staging_file_name);
1317 wimlib_assert(fd->staging_fd != -1);
1319 /* Write the data. */
1320 ret = write(fd->staging_fd, buf, size);
1328 static struct fuse_operations wimfs_operations = {
1329 .access = wimfs_access,
1330 .destroy = wimfs_destroy,
1331 .fgetattr = wimfs_fgetattr,
1332 .ftruncate = wimfs_ftruncate,
1333 .getattr = wimfs_getattr,
1335 .mkdir = wimfs_mkdir,
1336 .mknod = wimfs_mknod,
1338 .opendir = wimfs_opendir,
1340 .readdir = wimfs_readdir,
1341 .readlink = wimfs_readlink,
1342 .release = wimfs_release,
1343 .releasedir = wimfs_releasedir,
1344 .rename = wimfs_rename,
1345 .rmdir = wimfs_rmdir,
1346 .symlink = wimfs_symlink,
1347 .truncate = wimfs_truncate,
1348 .unlink = wimfs_unlink,
1349 .utimens = wimfs_utimens,
1350 .write = wimfs_write,
1354 /* Mounts a WIM file. */
1355 WIMLIBAPI int wimlib_mount(WIMStruct *wim, int image, const char *dir,
1363 DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
1364 wim, image, dir, flags);
1367 return WIMLIB_ERR_INVALID_PARAM;
1369 ret = wimlib_select_image(wim, image);
1374 if (flags & WIMLIB_MOUNT_FLAG_READWRITE)
1375 wim_get_current_image_metadata(wim)->modified = true;
1377 if (!(flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
1378 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
1379 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
1380 flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1383 working_directory = getcwd(NULL, 0);
1384 if (!working_directory) {
1385 ERROR_WITH_ERRNO("Could not determine current directory");
1386 return WIMLIB_ERR_NOTDIR;
1391 return WIMLIB_ERR_NOMEM;
1393 argv[argc++] = "mount";
1395 argv[argc++] = "-s"; /* disable multi-threaded operation */
1397 if (flags & WIMLIB_MOUNT_FLAG_DEBUG) {
1398 argv[argc++] = "-d";
1400 if (!(flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
1401 argv[argc++] = "-o";
1402 argv[argc++] = "ro";
1405 if (!staging_dir_name) {
1407 return WIMLIB_ERR_MKDIR;
1414 DEBUG("FUSE command line (argc = %d): ", argc);
1415 for (i = 0; i < argc; i++) {
1416 fputs(argv[i], stdout);
1424 /* Set static variables. */
1426 mount_flags = flags;
1428 ret = fuse_main(argc, argv, &wimfs_operations, NULL);
1430 return (ret == 0) ? 0 : WIMLIB_ERR_FUSE;
1435 * Unmounts the WIM file that was previously mounted on @dir by using
1438 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
1445 struct timespec timeout;
1449 /* Execute `fusermount -u', which is installed setuid root, to unmount
1452 * FUSE does not yet implement synchronous unmounts. This means that
1453 * fusermount -u will return before the filesystem daemon returns from
1454 * wimfs_destroy(). This is partly what we want, because we need to
1455 * send a message from this process to the filesystem daemon telling
1456 * whether --commit was specified or not. However, after that, the
1457 * unmount process must wait for the filesystem daemon to finish writing
1464 ERROR_WITH_ERRNO("Failed to fork()");
1465 return WIMLIB_ERR_FORK;
1468 execlp("fusermount", "fusermount", "-u", dir, NULL);
1469 ERROR_WITH_ERRNO("Failed to execute `fusermount'");
1470 return WIMLIB_ERR_FUSERMOUNT;
1473 ret = waitpid(pid, &status, 0);
1475 ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
1477 return WIMLIB_ERR_FUSERMOUNT;
1481 ERROR("fusermount exited with status %d", status);
1482 return WIMLIB_ERR_FUSERMOUNT;
1485 /* Open message queues between the unmount process and the
1486 * filesystem daemon. */
1487 ret = open_message_queues(false);
1491 /* Send a message to the filesystem saying whether to commit or
1493 msg[0] = (flags & WIMLIB_UNMOUNT_FLAG_COMMIT) ? 1 : 0;
1494 msg[1] = (flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY) ? 1 : 0;
1496 DEBUG("Sending message: %s, %s",
1497 (msg[0] == 0) ? "don't commit" : "commit",
1498 (msg[1] == 0) ? "don't check" : "check");
1499 ret = mq_send(unmount_to_daemon_mq, msg, 2, 1);
1501 ERROR("Failed to notify filesystem daemon whether we want to "
1502 "commit changes or not");
1503 close_message_queues();
1504 return WIMLIB_ERR_MQUEUE;
1507 /* Wait for a message from the filesytem daemon indicating whether the
1508 * filesystem was unmounted successfully (0) or an error occurred (1).
1509 * This may take a long time if a big WIM file needs to be rewritten. */
1511 /* Wait at most 600??? seconds before giving up and returning false.
1512 * Either it's a really big WIM file, or (more likely) the
1513 * filesystem daemon has crashed or failed for some reason.
1515 * XXX come up with some method to determine if the filesystem
1516 * daemon has really crashed or not. */
1518 gettimeofday(&now, NULL);
1519 timeout.tv_sec = now.tv_sec + 600;
1520 timeout.tv_nsec = now.tv_usec * 1000;
1522 msgsize = mq_get_msgsize(daemon_to_unmount_mq);
1523 char mailbox[msgsize];
1526 DEBUG("Waiting for message telling us whether the unmount was "
1527 "successful or not.");
1528 ret = mq_timedreceive(daemon_to_unmount_mq, mailbox, msgsize,
1531 close_message_queues();
1533 if (errno_save == ETIMEDOUT) {
1534 ERROR("Timed out- probably the filesystem daemon "
1535 "crashed and the WIM was not written "
1537 return WIMLIB_ERR_TIMEOUT;
1539 ERROR("mq_receive(): %s", strerror(errno_save));
1540 return WIMLIB_ERR_MQUEUE;
1544 DEBUG("Received message: %s",
1545 (mailbox[0] == 0) ? "Unmount OK" : "Unmount Failed");
1546 if (mailbox[0] != 0)
1547 ERROR("Unmount failed");
1551 #else /* WITH_FUSE */
1554 static inline int mount_unsupported_error()
1556 ERROR("WIMLIB was compiled with --without-fuse, which disables support "
1557 "for mounting WIMs.");
1558 return WIMLIB_ERR_UNSUPPORTED;
1561 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
1563 return mount_unsupported_error();
1566 WIMLIBAPI int wimlib_mount(WIMStruct *wim_p, int image, const char *dir,
1569 return mount_unsupported_error();
1572 #endif /* WITH_FUSE */