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;
76 /* Next hard link group ID to be assigned. These are also used as the inode
78 static u64 next_link_group_id;
81 static inline int get_lookup_flags()
83 if (mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
84 return LOOKUP_FLAG_ADS_OK;
89 static inline int flags_writable(int open_flags)
91 return open_flags & (O_RDWR | O_WRONLY);
94 static int alloc_wimlib_fd(struct lookup_table_entry *lte,
95 struct wimlib_fd **fd_ret)
97 static const u16 fds_per_alloc = 8;
98 static const u16 max_fds = 0xffff;
100 if (lte->num_opened_fds == lte->num_allocated_fds) {
101 struct wimlib_fd **fds;
104 if (lte->num_allocated_fds == max_fds)
106 num_new_fds = min(fds_per_alloc, max_fds - lte->num_allocated_fds);
108 fds = CALLOC(lte->num_allocated_fds + num_new_fds,
109 sizeof(lte->fds[0]));
112 memcpy(fds, lte->fds,
113 lte->num_allocated_fds * sizeof(lte->fds[0]));
116 lte->num_allocated_fds += num_new_fds;
118 for (u16 i = 0; ; i++) {
120 struct wimlib_fd *fd = CALLOC(1, sizeof(*fd));
127 lte->num_opened_fds++;
134 static int close_wimlib_fd(struct wimlib_fd *fd)
136 struct lookup_table_entry *lte = fd->lte;
139 wimlib_assert(lte->num_opened_fds);
141 if (lte->staging_file_name) {
142 wimlib_assert(fd->staging_fd != -1);
143 if (close(fd->staging_fd) != 0)
146 if (--lte->num_opened_fds == 0 && lte->refcnt == 0) {
147 if (lte->staging_file_name)
148 unlink(lte->staging_file_name);
149 free_lookup_table_entry(lte);
151 lte->fds[fd->idx] = NULL;
156 static void remove_dentry(struct dentry *dentry,
157 struct lookup_table *lookup_table)
159 const u8 *hash = dentry->hash;
161 struct lookup_table_entry *lte;
163 lte = lookup_table_decrement_refcnt(lookup_table, hash);
164 if (lte && lte->num_opened_fds)
165 for (u16 i = 0; i < lte->num_allocated_fds; i++)
166 if (lte->fds[i] && lte->fds[i]->dentry == dentry)
167 lte->fds[i]->dentry = NULL;
168 if (i == dentry->num_ads)
170 hash = dentry->ads_entries[i].hash;
174 unlink_dentry(dentry);
178 static void dentry_increment_lookup_table_refcnts(struct dentry *dentry,
179 struct lookup_table *lookup_table)
182 const u8 *hash = dentry->hash;
183 struct lookup_table_entry *lte;
185 lte = __lookup_resource(lookup_table, hash);
188 if (i == dentry->num_ads)
190 hash = dentry->ads_entries[i].hash;
195 /* Creates a new staging file and returns its file descriptor opened for
198 * @name_ret: A location into which the a pointer to the newly allocated name of
199 * the staging file is stored.
200 * @return: The file descriptor for the new file. Returns -1 and sets errno on
201 * error, for any reason possible from the creat() function.
203 static int create_staging_file(char **name_ret, int open_flags)
211 name_len = staging_dir_name_len + 1 + WIM_HASH_SIZE;
212 name = MALLOC(name_len + 1);
220 memcpy(name, staging_dir_name, staging_dir_name_len);
221 name[staging_dir_name_len] = '/';
222 randomize_char_array_with_alnum(name + staging_dir_name_len + 1,
224 name[name_len] = '\0';
227 /* Just in case, verify that the randomly generated name doesn't name an
228 * existing file, and try again if so */
229 } while (stat(name, &stbuf) == 0);
235 /* doesn't exist--- ok */
237 DEBUG("Creating staging file `%s'", name);
239 fd = open(name, open_flags | O_CREAT | O_TRUNC, 0600);
251 * Extract a WIM resource to the staging directory.
254 * - Create a staging file for the WIM resource
255 * - Extract the resource to it
256 * - Create a new lte for the file resource
257 * - Transfer fds from the old lte to the new lte, but
258 * only if they share the same hard link group as this
261 static int extract_resource_to_staging_dir(struct dentry *dentry,
262 struct lookup_table_entry **lte,
265 char *staging_file_name;
268 struct lookup_table_entry *old_lte, *new_lte;
269 size_t link_group_size;
271 DEBUG("Extracting resource `%s' to staging directory", dentry->full_path_utf8);
274 fd = create_staging_file(&staging_file_name, O_WRONLY);
279 ret = extract_resource_to_fd(w, &old_lte->resource_entry, fd,
283 if (ret != 0 || close(fd) != 0) {
289 goto out_delete_staging_file;
292 link_group_size = dentry_link_group_size(dentry);
295 if (link_group_size == old_lte->refcnt) {
296 /* This hard link group is the only user of the lookup
297 * table entry, so we can re-use it. */
298 DEBUG("Re-using lookup table entry");
299 lookup_table_remove(w->lookup_table, old_lte);
302 DEBUG("Splitting lookup table entry "
303 "(link_group_size = %u, lte refcnt = %u)",
304 link_group_size, old_lte->refcnt);
305 /* Split a hard link group away from the "lookup table
306 * entry" hard link group (i.e. we had two hard link
307 * groups that were identical, but now we are changing
310 /* XXX The ADS really complicate things here and not
311 * everything is going to work correctly yet. For
312 * example it could be the same that a file contains two
313 * file streams that are identical and therefore share
314 * the same lookup table entry despite the fact that the
315 * streams themselves are not hardlinked. */
316 wimlib_assert(old_lte->refcnt > link_group_size);
318 new_lte = new_lookup_table_entry();
321 goto out_delete_staging_file;
324 u16 num_transferred_fds = 0;
325 for (u16 i = 0; i < old_lte->num_allocated_fds; i++) {
326 if (old_lte->fds[i] &&
327 old_lte->fds[i]->dentry->hard_link ==
330 num_transferred_fds++;
333 DEBUG("Transferring %u file descriptors",
334 num_transferred_fds);
335 new_lte->fds = MALLOC(num_transferred_fds *
336 sizeof(new_lte->fds[0]));
340 goto out_delete_staging_file;
342 for (u16 i = 0, j = 0; ; i++) {
343 if (old_lte->fds[i] &&
344 old_lte->fds[i]->dentry->hard_link ==
347 struct wimlib_fd *fd = old_lte->fds[i];
348 old_lte->fds[i] = NULL;
351 new_lte->fds[j] = fd;
352 if (++j == num_transferred_fds)
356 old_lte->refcnt -= link_group_size;
357 old_lte->num_opened_fds -= num_transferred_fds;
358 new_lte->num_opened_fds = num_transferred_fds;
359 new_lte->num_allocated_fds = num_transferred_fds;
362 new_lte = new_lookup_table_entry();
365 goto out_delete_staging_file;
368 new_lte->resource_entry.original_size = size;
369 new_lte->refcnt = link_group_size;
370 randomize_byte_array(new_lte->hash, WIM_HASH_SIZE);
371 new_lte->staging_file_name = staging_file_name;
373 lookup_table_insert(w->lookup_table, new_lte);
376 out_delete_staging_file:
377 unlink(staging_file_name);
378 FREE(staging_file_name);
383 * Creates a randomly named staging directory and returns its name into the
384 * static variable staging_dir_name.
386 * If the staging directory cannot be created, staging_dir_name is set to NULL.
388 static void make_staging_dir()
390 /* XXX Give the user an option of where to stage files */
392 static char prefix[] = "wimlib-staging-";
393 static const size_t prefix_len = 15;
394 static const size_t suffix_len = 10;
396 size_t pwd_len = strlen(working_directory);
398 staging_dir_name_len = pwd_len + 1 + prefix_len + suffix_len;
400 staging_dir_name = MALLOC(staging_dir_name_len + 1);
401 if (!staging_dir_name) {
402 ERROR("Out of memory");
406 memcpy(staging_dir_name, working_directory, pwd_len);
407 staging_dir_name[pwd_len] = '/';
408 memcpy(staging_dir_name + pwd_len + 1, prefix, prefix_len);
409 randomize_char_array_with_alnum(staging_dir_name + pwd_len + 1 + prefix_len,
411 staging_dir_name[staging_dir_name_len] = '\0';
413 if (mkdir(staging_dir_name, 0700) != 0) {
414 ERROR_WITH_ERRNO("Failed to create temporary directory `%s'",
416 FREE(staging_dir_name);
417 staging_dir_name = NULL;
421 static int remove_file_or_directory(const char *fpath, const struct stat *sb,
422 int typeflag, struct FTW *ftwbuf)
424 if (remove(fpath) == 0)
427 return WIMLIB_ERR_DELETE_STAGING_DIR;
432 * Deletes the staging directory and all the files contained in it.
434 static inline int delete_staging_dir()
438 ret = nftw(staging_dir_name, remove_file_or_directory,10, FTW_DEPTH);
439 staging_dir_name = NULL;
443 /* Name and message queue descriptors for message queues between the filesystem
444 * daemon process and the unmount process. These are used when the filesystem
445 * is unmounted and the process running wimlib_mount() (i.e. the `imagex
446 * unmount' command) needs to communicate with the filesystem daemon running
447 * fuse_main() (i.e. that spawned by the `imagex mount' or `imagex mountrw'
449 static char *unmount_to_daemon_mq_name;
450 static char *daemon_to_unmount_mq_name;
451 static int unmount_to_daemon_mq;
452 static int daemon_to_unmount_mq;
454 /* Simple function that returns the concatenation of 4 strings. */
455 static char *strcat_dup(const char *s1, const char *s2, const char *s3,
458 size_t len = strlen(s1) + strlen(s2) + strlen(s3) + strlen(s4) + 1;
459 char *p = MALLOC(len);
470 /* Removes trailing forward slashes in a string. */
471 static void remove_trailing_slashes(char *s)
473 long len = strlen(s);
474 for (long i = len - 1; i >= 1; i--) {
482 /* Changes forward slashes to underscores in a string. */
483 static void s_slashes_underscores_g(char *s)
493 * Opens two POSIX message queue: one for sending messages from the unmount
494 * process to the daemon process, and one to go the other way. The names of the
495 * message queues, which must be system-wide unique, are be based on the mount
496 * point. (There of course is still a possibility of a collision if one were to
497 * unmount two identically named directories simultaneously...)
499 * @daemon specifies whether the calling process is the filesystem daemon or the
502 static int open_message_queues(bool daemon)
504 static const char *slash = "/";
505 static const char *prefix = "wimlib-";
506 static const char *u2d_suffix = "unmount-to-daemon-mq";
507 static const char *d2u_suffix = "daemon-to-unmount-mq";
509 const char *mount_dir_basename = path_basename(mount_dir);
513 unmount_to_daemon_mq_name = strcat_dup(slash, mount_dir_basename,
515 if (!unmount_to_daemon_mq_name) {
516 ERROR("Out of memory");
517 return WIMLIB_ERR_NOMEM;
519 daemon_to_unmount_mq_name = strcat_dup(slash, mount_dir_basename,
521 if (!daemon_to_unmount_mq_name) {
522 ERROR("Out of memory");
523 ret = WIMLIB_ERR_NOMEM;
527 remove_trailing_slashes(unmount_to_daemon_mq_name);
528 remove_trailing_slashes(daemon_to_unmount_mq_name);
529 s_slashes_underscores_g(unmount_to_daemon_mq_name + 1);
530 s_slashes_underscores_g(daemon_to_unmount_mq_name + 1);
533 flags = O_RDONLY | O_CREAT;
535 flags = O_WRONLY | O_CREAT;
537 unmount_to_daemon_mq = mq_open(unmount_to_daemon_mq_name, flags,
540 if (unmount_to_daemon_mq == -1) {
541 ERROR_WITH_ERRNO("mq_open()");
542 ret = WIMLIB_ERR_MQUEUE;
547 flags = O_WRONLY | O_CREAT;
549 flags = O_RDONLY | O_CREAT;
551 daemon_to_unmount_mq = mq_open(daemon_to_unmount_mq_name, flags,
554 if (daemon_to_unmount_mq == -1) {
555 ERROR_WITH_ERRNO("mq_open()");
556 ret = WIMLIB_ERR_MQUEUE;
561 mq_close(unmount_to_daemon_mq);
562 mq_unlink(unmount_to_daemon_mq_name);
564 FREE(daemon_to_unmount_mq_name);
566 FREE(unmount_to_daemon_mq_name);
570 static int mq_get_msgsize(mqd_t mq)
572 static const char *msgsize_max_file = "/proc/sys/fs/mqueue/msgsize_max";
577 if (mq_getattr(unmount_to_daemon_mq, &attr) == 0) {
578 msgsize = attr.mq_msgsize;
580 ERROR_WITH_ERRNO("mq_getattr()");
581 ERROR("Attempting to read %s", msgsize_max_file);
582 fp = fopen(msgsize_max_file, "rb");
584 if (fscanf(fp, "%d", &msgsize) != 1) {
585 ERROR("Assuming message size of 8192");
590 ERROR_WITH_ERRNO("Failed to open the file `%s'",
592 ERROR("Assuming message size of 8192");
599 /* Closes the message queues, which are allocated in static variables */
600 static void close_message_queues()
602 mq_close(unmount_to_daemon_mq);
603 mq_close(daemon_to_unmount_mq);
604 mq_unlink(unmount_to_daemon_mq_name);
605 mq_unlink(daemon_to_unmount_mq_name);
608 static int wimfs_access(const char *path, int mask)
610 /* XXX Permissions not implemented */
614 /* Closes the staging file descriptor associated with the lookup table entry, if
616 static int close_lte_fds(struct lookup_table_entry *lte, void *ignore)
618 for (u16 i = 0; i < lte->num_opened_fds; i++) {
619 if (lte->fds[i] && lte->fds[i]->staging_fd != -1) {
620 if (close(lte->fds[i]->staging_fd) != 0) {
621 ERROR_WITH_ERRNO("Failed close file `%s'",
622 lte->staging_file_name);
623 return WIMLIB_ERR_WRITE;
631 /* Calculates the SHA1 sum for @dentry if its file resource is in a staging
632 * file. Updates the SHA1 sum in the dentry and the lookup table entry. If
633 * there is already a lookup table entry with the same checksum, increment its
634 * reference count and destroy the lookup entry with the updated checksum. */
635 static int calculate_sha1sum_for_staging_file(struct dentry *dentry,
638 struct lookup_table *lookup_table = _lookup_table;
639 u8 *hash = dentry->hash;
642 struct lookup_table_entry *lte = __lookup_resource(lookup_table, hash);
643 if (lte && lte->staging_file_name) {
644 struct lookup_table_entry *existing;
647 DEBUG("Calculating SHA1 hash for file `%s'",
648 dentry->file_name_utf8);
649 ret = sha1sum(lte->staging_file_name, lte->hash);
653 lookup_table_unlink(lookup_table, lte);
654 memcpy(hash, lte->hash, WIM_HASH_SIZE);
655 existing = __lookup_resource(lookup_table, hash);
657 DEBUG("Merging duplicate lookup table entries for file "
658 "`%s'", dentry->file_name_utf8);
659 free_lookup_table_entry(lte);
662 lookup_table_insert(lookup_table, lte);
665 if (i == dentry->num_ads)
667 hash = dentry->ads_entries[i].hash;
673 /* Overwrites the WIM file, with changes saved. */
674 static int rebuild_wim(WIMStruct *w, bool check_integrity)
679 root = wim_root_dentry(w);
681 DEBUG("Closing all staging file descriptors.");
682 /* Close all the staging file descriptors. */
683 ret = for_lookup_table_entry(w->lookup_table, close_lte_fds, NULL);
685 ERROR("Failed to close all staging files");
689 DEBUG("Calculating SHA1 checksums for all new staging files.");
690 /* Calculate SHA1 checksums for all staging files, and merge unnecessary
691 * lookup table entries. */
692 ret = for_dentry_in_tree(root, calculate_sha1sum_for_staging_file,
695 ERROR("Failed to calculate new SHA1 checksums");
699 xml_update_image_info(w, w->current_image);
701 ret = wimlib_overwrite(w, check_integrity);
703 ERROR("Failed to commit changes");
709 /* Called when the filesystem is unmounted. */
710 static void wimfs_destroy(void *p)
712 /* For read-write mounts, the `imagex unmount' command, which is
713 * running in a separate process and is executing the
714 * wimlib_unmount() function, will send this process a byte
715 * through a message queue that indicates whether the --commit
716 * option was specified or not. */
719 struct timespec timeout;
721 ssize_t bytes_received;
724 char check_integrity;
727 ret = open_message_queues(true);
731 msgsize = mq_get_msgsize(unmount_to_daemon_mq);
736 /* Wait at most 3 seconds before giving up and discarding changes. */
737 gettimeofday(&now, NULL);
738 timeout.tv_sec = now.tv_sec + 3;
739 timeout.tv_nsec = now.tv_usec * 1000;
740 DEBUG("Waiting for message telling us whether to commit or not, and "
741 "whether to include integrity checks.");
743 bytes_received = mq_timedreceive(unmount_to_daemon_mq, msg,
744 msgsize, NULL, &timeout);
746 check_integrity = msg[1];
747 if (bytes_received == -1) {
748 if (errno == ETIMEDOUT) {
751 ERROR_WITH_ERRNO("mq_timedreceive()");
753 ERROR("Not committing.");
755 DEBUG("Received message: [%d %d]", msg[0], msg[1]);
759 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
761 status = chdir(working_directory);
763 ERROR_WITH_ERRNO("chdir()");
764 status = WIMLIB_ERR_NOTDIR;
767 status = rebuild_wim(w, (check_integrity != 0));
769 ret = delete_staging_dir();
771 ERROR_WITH_ERRNO("Failed to delete the staging "
778 ret = mq_send(daemon_to_unmount_mq, &status, 1, 1);
780 ERROR_WITH_ERRNO("Failed to send status to unmount process");
781 close_message_queues();
784 static int wimfs_fallocate(const char *path, int mode,
785 off_t offset, off_t len, struct fuse_file_info *fi)
787 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
788 wimlib_assert(fd->staging_fd != -1);
789 return fallocate(fd->staging_fd, mode, offset, len);
792 static int wimfs_fgetattr(const char *path, struct stat *stbuf,
793 struct fuse_file_info *fi)
795 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
796 return dentry_to_stbuf(fd->dentry, stbuf, w->lookup_table);
799 static int wimfs_ftruncate(const char *path, off_t size,
800 struct fuse_file_info *fi)
802 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
803 int ret = ftruncate(fd->staging_fd, size);
806 fd->lte->resource_entry.original_size = size;
811 * Fills in a `struct stat' that corresponds to a file or directory in the WIM.
813 static int wimfs_getattr(const char *path, struct stat *stbuf)
815 struct dentry *dentry = get_dentry(w, path);
818 return dentry_to_stbuf(dentry, stbuf, w->lookup_table);
821 static int wimfs_getxattr(const char *path, const char *name, char *value,
828 /* Create a hard link */
829 static int wimfs_link(const char *to, const char *from)
831 struct dentry *to_dentry, *from_dentry, *from_dentry_parent;
832 const char *link_name;
834 to_dentry = get_dentry(w, to);
837 if (!dentry_is_regular_file(to_dentry))
840 from_dentry_parent = get_parent_dentry(w, from);
841 if (!from_dentry_parent)
843 if (!dentry_is_directory(from_dentry_parent))
846 link_name = path_basename(from);
847 if (get_dentry_child_with_name(from_dentry_parent, link_name))
849 from_dentry = clone_dentry(to_dentry);
852 if (change_dentry_name(from_dentry, link_name) != 0) {
856 list_add(&from_dentry->link_group_list, &to_dentry->link_group_list);
857 link_dentry(from_dentry, from_dentry_parent);
858 dentry_increment_lookup_table_refcnts(from_dentry, w->lookup_table);
859 from_dentry->link_group_master_status = GROUP_SLAVE;
863 static int wimfs_listxattr(const char *path, char *list, size_t size)
870 * Create a directory in the WIM.
871 * @mode is currently ignored.
873 static int wimfs_mkdir(const char *path, mode_t mode)
875 struct dentry *parent;
876 struct dentry *newdir;
877 const char *basename;
879 parent = get_parent_dentry(w, path);
883 if (!dentry_is_directory(parent))
886 basename = path_basename(path);
887 if (get_dentry_child_with_name(parent, basename))
890 newdir = new_dentry(basename);
891 newdir->attributes |= FILE_ATTRIBUTE_DIRECTORY;
892 link_dentry(newdir, parent);
897 /* Creates a regular file. */
898 static int wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
900 const char *stream_name;
901 if ((mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
902 && (stream_name = path_stream_name(path))) {
903 struct ads_entry *new_entry;
904 struct dentry *dentry;
906 dentry = get_dentry(w, path);
907 if (!dentry || !dentry_is_regular_file(dentry))
909 if (dentry_get_ads_entry(dentry, stream_name))
911 new_entry = dentry_add_ads(dentry, stream_name);
915 struct dentry *dentry, *parent;
916 const char *basename;
918 /* Make sure that the parent of @path exists and is a directory, and
919 * that the dentry named by @path does not already exist. */
920 parent = get_parent_dentry(w, path);
923 if (!dentry_is_directory(parent))
926 basename = path_basename(path);
927 if (get_dentry_child_with_name(parent, path))
930 dentry = new_dentry(basename);
933 dentry->hard_link = next_link_group_id++;
934 link_dentry(dentry, parent);
941 static int wimfs_open(const char *path, struct fuse_file_info *fi)
943 struct dentry *dentry;
944 struct lookup_table_entry *lte;
947 struct wimlib_fd *fd;
949 ret = lookup_resource(w, path, get_lookup_flags(), &dentry, <e,
955 /* Empty file with no lookup-table entry. This is fine if it's
956 * a read-only filesystem. Otherwise we need to create a lookup
957 * table entry so that we can keep track of the file descriptors
958 * (this is important in case someone opens the file for
960 if (!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
965 ret = extract_resource_to_staging_dir(dentry, <e, 0);
968 memcpy(dentry_hash, lte->hash, WIM_HASH_SIZE);
971 ret = alloc_wimlib_fd(lte, &fd);
977 /* The file resource may be in the staging directory (read-write
978 * mounts only) or in the WIM. If it's in the staging
979 * directory, we need to open a native file descriptor for the
980 * corresponding file. Otherwise, we can read the file resource
981 * directly from the WIM file if we are opening it read-only,
982 * but we need to extract the resource to the staging directory
983 * if we are opening it writable. */
984 if (flags_writable(fi->flags) && !lte->staging_file_name) {
985 ret = extract_resource_to_staging_dir(dentry, <e,
986 lte->resource_entry.original_size);
989 memcpy(dentry_hash, lte->hash, WIM_HASH_SIZE);
991 if (lte->staging_file_name) {
992 fd->staging_fd = open(lte->staging_file_name, fi->flags);
993 if (fd->staging_fd == -1) {
998 fi->fh = (uint64_t)fd;
1002 /* Opens a directory. */
1003 static int wimfs_opendir(const char *path, struct fuse_file_info *fi)
1005 struct dentry *dentry;
1007 dentry = get_dentry(w, path);
1010 if (!dentry_is_directory(dentry))
1012 dentry->num_times_opened++;
1013 fi->fh = (uint64_t)dentry;
1019 * Read data from a file in the WIM or in the staging directory.
1021 static int wimfs_read(const char *path, char *buf, size_t size,
1022 off_t offset, struct fuse_file_info *fi)
1024 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
1027 /* Empty file with no lookup table entry on read-only mounted
1029 wimlib_assert(!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE));
1033 if (fd->lte->staging_file_name) {
1034 /* Read from staging file */
1036 wimlib_assert(fd->staging_fd != -1);
1039 DEBUG("Seek to offset %zu", offset);
1041 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1043 ret = read(fd->staging_fd, buf, size);
1051 struct resource_entry *res_entry;
1054 res_entry = &fd->lte->resource_entry;
1056 ctype = wim_resource_compression_type(w, res_entry);
1058 if (offset > res_entry->original_size)
1061 size = min(size, res_entry->original_size - offset);
1063 if (read_resource(w->fp, res_entry->size,
1064 res_entry->original_size,
1065 res_entry->offset, ctype, size,
1072 /* Fills in the entries of the directory specified by @path using the
1073 * FUSE-provided function @filler. */
1074 static int wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
1075 off_t offset, struct fuse_file_info *fi)
1077 struct dentry *parent, *child;
1079 parent = (struct dentry*)fi->fh;
1080 child = parent->children;
1082 filler(buf, ".", NULL, 0);
1083 filler(buf, "..", NULL, 0);
1089 if (filler(buf, child->file_name_utf8, NULL, 0))
1091 child = child->next;
1092 } while (child != parent->children);
1097 static int wimfs_readlink(const char *path, char *buf, size_t buf_len)
1099 struct dentry *dentry = get_dentry(w, path);
1103 if (!dentry_is_symlink(dentry))
1106 ret = dentry_readlink(dentry, buf, buf_len, w);
1113 static int wimfs_release(const char *path, struct fuse_file_info *fi)
1116 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
1119 /* Empty file with no lookup table entry on read-only mounted
1121 wimlib_assert(!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE));
1125 if (flags_writable(fi->flags) && fd->dentry) {
1126 u64 now = get_timestamp();
1127 fd->dentry->last_access_time = now;
1128 fd->dentry->last_write_time = now;
1131 return close_wimlib_fd(fd);
1134 static int wimfs_releasedir(const char *path, struct fuse_file_info *fi)
1136 struct dentry *dentry = (struct dentry *)fi->fh;
1138 wimlib_assert(dentry->num_times_opened);
1139 if (--dentry->num_times_opened == 0)
1140 free_dentry(dentry);
1144 static int wimfs_removexattr(const char *path, const char *name)
1150 /* Renames a file or directory. See rename (3) */
1151 static int wimfs_rename(const char *from, const char *to)
1155 struct dentry *parent_of_dst;
1156 char *file_name_utf16 = NULL, *file_name_utf8 = NULL;
1157 u16 file_name_utf16_len, file_name_utf8_len;
1160 /* This rename() implementation currently only supports actual files
1161 * (not alternate data streams) */
1163 src = get_dentry(w, from);
1167 dst = get_dentry(w, to);
1170 ret = get_names(&file_name_utf16, &file_name_utf8,
1171 &file_name_utf16_len, &file_name_utf8_len,
1177 if (src == dst) /* Same file */
1180 if (!dentry_is_directory(src)) {
1181 /* Cannot rename non-directory to directory. */
1182 if (dentry_is_directory(dst))
1185 /* Cannot rename directory to a non-directory or a non-empty
1187 if (!dentry_is_directory(dst))
1189 if (dst->children != NULL)
1192 parent_of_dst = dst->parent;
1193 remove_dentry(dst, w->lookup_table);
1195 parent_of_dst = get_parent_dentry(w, to);
1200 FREE(src->file_name);
1201 FREE(src->file_name_utf8);
1202 src->file_name = file_name_utf16;
1203 src->file_name_utf8 = file_name_utf8;
1204 src->file_name_len = file_name_utf16_len;
1205 src->file_name_utf8_len = file_name_utf8_len;
1208 link_dentry(src, parent_of_dst);
1212 /* Remove a directory */
1213 static int wimfs_rmdir(const char *path)
1215 struct dentry *dentry;
1217 dentry = get_dentry(w, path);
1221 if (!dentry_is_empty_directory(dentry))
1224 unlink_dentry(dentry);
1225 if (dentry->num_times_opened == 0)
1226 free_dentry(dentry);
1230 static int wimfs_setxattr(const char *path, const char *name,
1231 const char *value, size_t size, int flags)
1237 static int wimfs_symlink(const char *to, const char *from)
1239 struct dentry *dentry_parent, *dentry;
1240 const char *link_name;
1242 dentry_parent = get_parent_dentry(w, from);
1245 if (!dentry_is_directory(dentry_parent))
1248 link_name = path_basename(from);
1250 if (get_dentry_child_with_name(dentry_parent, link_name))
1252 dentry = new_dentry(link_name);
1256 dentry->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
1257 dentry->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
1259 if (dentry_set_symlink(dentry, to, w->lookup_table) != 0)
1260 goto out_free_dentry;
1262 link_dentry(dentry, dentry_parent);
1265 free_dentry(dentry);
1270 /* Reduce the size of a file */
1271 static int wimfs_truncate(const char *path, off_t size)
1273 struct dentry *dentry;
1274 struct lookup_table_entry *lte;
1278 ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1279 <e, &dentry_hash);
1284 if (!lte) /* Already a zero-length file */
1287 if (lte->staging_file_name) {
1288 ret = truncate(lte->staging_file_name, size);
1291 lte->resource_entry.original_size = size;
1293 /* File in WIM. Extract it to the staging directory, but only
1294 * the first @size bytes of it. */
1295 ret = extract_resource_to_staging_dir(dentry, <e, size);
1297 dentry_update_all_timestamps(dentry);
1301 /* Remove a regular file */
1302 static int wimfs_unlink(const char *path)
1304 struct dentry *dentry;
1305 struct lookup_table_entry *lte;
1309 ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1310 <e, &dentry_hash);
1315 if (dentry_hash == dentry->hash) {
1316 /* We are removing the full dentry including all alternate data
1318 remove_dentry(dentry, w->lookup_table);
1320 /* We are removing an alternate data stream. */
1321 struct ads_entry *cur_entry = dentry->ads_entries;
1322 while (cur_entry->hash != dentry_hash)
1324 lookup_table_decrement_refcnt(w->lookup_table, cur_entry->hash);
1326 dentry_remove_ads(dentry, cur_entry);
1328 /* Beware: The lookup table entry(s) may still be referenced by users
1329 * that have opened the corresponding streams. They are freed later in
1330 * wimfs_release() when the last file user has closed the stream. */
1334 /* Change the timestamp on a file dentry.
1336 * There is no distinction between a file and its alternate data streams here. */
1337 static int wimfs_utimens(const char *path, const struct timespec tv[2])
1339 struct dentry *dentry = get_dentry(w, path);
1342 time_t last_access_t = (tv[0].tv_nsec == UTIME_NOW) ?
1343 time(NULL) : tv[0].tv_sec;
1344 dentry->last_access_time = unix_timestamp_to_ms(last_access_t);
1345 time_t last_mod_t = (tv[1].tv_nsec == UTIME_NOW) ?
1346 time(NULL) : tv[1].tv_sec;
1347 dentry->last_write_time = unix_timestamp_to_ms(last_mod_t);
1351 /* Writes to a file in the WIM filesystem.
1352 * It may be an alternate data stream, but here we don't even notice because we
1353 * just get a lookup table entry. */
1354 static int wimfs_write(const char *path, const char *buf, size_t size,
1355 off_t offset, struct fuse_file_info *fi)
1357 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
1360 wimlib_assert(fd->lte);
1361 wimlib_assert(fd->lte->staging_file_name);
1362 wimlib_assert(fd->staging_fd != -1);
1364 /* Seek to the requested position */
1365 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1368 /* Write the data. */
1369 ret = write(fd->staging_fd, buf, size);
1377 static struct fuse_operations wimfs_operations = {
1378 .access = wimfs_access,
1379 .destroy = wimfs_destroy,
1380 .fallocate = wimfs_fallocate,
1381 .fgetattr = wimfs_fgetattr,
1382 .ftruncate = wimfs_ftruncate,
1383 .getattr = wimfs_getattr,
1384 .getxattr = wimfs_getxattr,
1386 .listxattr = wimfs_listxattr,
1387 .mkdir = wimfs_mkdir,
1388 .mknod = wimfs_mknod,
1390 .opendir = wimfs_opendir,
1392 .readdir = wimfs_readdir,
1393 .readlink = wimfs_readlink,
1394 .release = wimfs_release,
1395 .releasedir = wimfs_releasedir,
1396 .removexattr = wimfs_removexattr,
1397 .rename = wimfs_rename,
1398 .rmdir = wimfs_rmdir,
1399 .setxattr = wimfs_setxattr,
1400 .symlink = wimfs_symlink,
1401 .truncate = wimfs_truncate,
1402 .unlink = wimfs_unlink,
1403 .utimens = wimfs_utimens,
1404 .write = wimfs_write,
1408 /* Mounts a WIM file. */
1409 WIMLIBAPI int wimlib_mount(WIMStruct *wim, int image, const char *dir,
1417 DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
1418 wim, image, dir, flags);
1421 return WIMLIB_ERR_INVALID_PARAM;
1423 ret = wimlib_select_image(wim, image);
1428 DEBUG("Selected image %d", image);
1430 next_link_group_id = assign_link_groups(wim->image_metadata[image - 1].lgt);
1432 if (flags & WIMLIB_MOUNT_FLAG_READWRITE)
1433 wim_get_current_image_metadata(wim)->modified = true;
1435 if (!(flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
1436 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
1437 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
1438 flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1441 working_directory = getcwd(NULL, 0);
1442 if (!working_directory) {
1443 ERROR_WITH_ERRNO("Could not determine current directory");
1444 return WIMLIB_ERR_NOTDIR;
1449 return WIMLIB_ERR_NOMEM;
1451 argv[argc++] = "mount";
1453 argv[argc++] = "-s"; /* disable multi-threaded operation */
1455 if (flags & WIMLIB_MOUNT_FLAG_DEBUG) {
1456 argv[argc++] = "-d";
1458 char optstring[256] = "use_ino";
1459 argv[argc++] = "-o";
1460 argv[argc++] = optstring;
1461 if ((flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
1463 if (!staging_dir_name) {
1465 return WIMLIB_ERR_MKDIR;
1468 strcat(optstring, ",ro");
1475 DEBUG("FUSE command line (argc = %d): ", argc);
1476 for (i = 0; i < argc; i++) {
1477 fputs(argv[i], stdout);
1485 /* Set static variables. */
1487 mount_flags = flags;
1489 ret = fuse_main(argc, argv, &wimfs_operations, NULL);
1491 return (ret == 0) ? 0 : WIMLIB_ERR_FUSE;
1496 * Unmounts the WIM file that was previously mounted on @dir by using
1499 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
1506 struct timespec timeout;
1510 /* Execute `fusermount -u', which is installed setuid root, to unmount
1513 * FUSE does not yet implement synchronous unmounts. This means that
1514 * fusermount -u will return before the filesystem daemon returns from
1515 * wimfs_destroy(). This is partly what we want, because we need to
1516 * send a message from this process to the filesystem daemon telling
1517 * whether --commit was specified or not. However, after that, the
1518 * unmount process must wait for the filesystem daemon to finish writing
1525 ERROR_WITH_ERRNO("Failed to fork()");
1526 return WIMLIB_ERR_FORK;
1529 execlp("fusermount", "fusermount", "-u", dir, NULL);
1530 ERROR_WITH_ERRNO("Failed to execute `fusermount'");
1531 return WIMLIB_ERR_FUSERMOUNT;
1534 ret = waitpid(pid, &status, 0);
1536 ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
1538 return WIMLIB_ERR_FUSERMOUNT;
1542 ERROR("fusermount exited with status %d", status);
1543 return WIMLIB_ERR_FUSERMOUNT;
1546 /* Open message queues between the unmount process and the
1547 * filesystem daemon. */
1548 ret = open_message_queues(false);
1552 /* Send a message to the filesystem saying whether to commit or
1554 msg[0] = (flags & WIMLIB_UNMOUNT_FLAG_COMMIT) ? 1 : 0;
1555 msg[1] = (flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY) ? 1 : 0;
1557 DEBUG("Sending message: %s, %s",
1558 (msg[0] == 0) ? "don't commit" : "commit",
1559 (msg[1] == 0) ? "don't check" : "check");
1560 ret = mq_send(unmount_to_daemon_mq, msg, 2, 1);
1562 ERROR("Failed to notify filesystem daemon whether we want to "
1563 "commit changes or not");
1564 close_message_queues();
1565 return WIMLIB_ERR_MQUEUE;
1568 /* Wait for a message from the filesytem daemon indicating whether the
1569 * filesystem was unmounted successfully (0) or an error occurred (1).
1570 * This may take a long time if a big WIM file needs to be rewritten. */
1572 /* Wait at most 600??? seconds before giving up and returning false.
1573 * Either it's a really big WIM file, or (more likely) the
1574 * filesystem daemon has crashed or failed for some reason.
1576 * XXX come up with some method to determine if the filesystem
1577 * daemon has really crashed or not. */
1579 gettimeofday(&now, NULL);
1580 timeout.tv_sec = now.tv_sec + 600;
1581 timeout.tv_nsec = now.tv_usec * 1000;
1583 msgsize = mq_get_msgsize(daemon_to_unmount_mq);
1584 char mailbox[msgsize];
1587 DEBUG("Waiting for message telling us whether the unmount was "
1588 "successful or not.");
1589 ret = mq_timedreceive(daemon_to_unmount_mq, mailbox, msgsize,
1592 close_message_queues();
1594 if (errno_save == ETIMEDOUT) {
1595 ERROR("Timed out- probably the filesystem daemon "
1596 "crashed and the WIM was not written "
1598 return WIMLIB_ERR_TIMEOUT;
1600 ERROR("mq_receive(): %s", strerror(errno_save));
1601 return WIMLIB_ERR_MQUEUE;
1605 DEBUG("Received message: %s",
1606 (mailbox[0] == 0) ? "Unmount OK" : "Unmount Failed");
1607 if (mailbox[0] != 0)
1608 ERROR("Unmount failed");
1612 #else /* WITH_FUSE */
1615 static inline int mount_unsupported_error()
1617 ERROR("WIMLIB was compiled with --without-fuse, which disables support "
1618 "for mounting WIMs.");
1619 return WIMLIB_ERR_UNSUPPORTED;
1622 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
1624 return mount_unsupported_error();
1627 WIMLIBAPI int wimlib_mount(WIMStruct *wim_p, int image, const char *dir,
1630 return mount_unsupported_error();
1633 #endif /* WITH_FUSE */