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 /* Creates a new staging file and returns its file descriptor opened for
152 * @name_ret: A location into which the a pointer to the newly allocated name of
153 * the staging file is stored.
154 * @return: The file descriptor for the new file. Returns -1 and sets errno on
155 * error, for any reason possible from the creat() function.
157 static int create_staging_file(char **name_ret, int open_flags)
165 name_len = staging_dir_name_len + 1 + WIM_HASH_SIZE;
166 name = MALLOC(name_len + 1);
174 memcpy(name, staging_dir_name, staging_dir_name_len);
175 name[staging_dir_name_len] = '/';
176 randomize_char_array_with_alnum(name + staging_dir_name_len + 1,
178 name[name_len] = '\0';
181 /* Just in case, verify that the randomly generated name doesn't name an
182 * existing file, and try again if so */
183 } while (stat(name, &stbuf) == 0);
189 /* doesn't exist--- ok */
191 DEBUG("Creating staging file `%s'", name);
193 fd = open(name, open_flags | O_CREAT | O_TRUNC, 0600);
205 * Extract a WIM resource to the staging directory.
208 * - Create a staging file for the WIM resource
209 * - Extract the resource to it
210 * - Create a new lte for the file resource
211 * - Transfer fds from the old lte to the new lte, but
212 * only if they share the same hard link group as this
215 static int extract_resource_to_staging_dir(struct dentry *dentry,
216 struct lookup_table_entry **lte,
219 char *staging_file_name;
222 struct lookup_table_entry *old_lte, *new_lte;
223 size_t link_group_size;
226 fd = create_staging_file(&staging_file_name, O_WRONLY);
231 ret = extract_resource_to_fd(w, &old_lte->resource_entry, fd,
235 if (ret != 0 || close(fd) != 0) {
241 unlink(staging_file_name);
242 FREE(staging_file_name);
246 link_group_size = dentry_link_group_size(dentry);
249 if (link_group_size == old_lte->refcnt) {
250 /* This hard link group is the only user of the lookup
251 * table entry, so we can re-use it. */
252 lookup_table_remove(w->lookup_table, old_lte);
255 /* Split a hard link group away from the "lookup table
256 * entry" hard link group (i.e. we had two hard link
257 * groups that were identical, but now we are changing
260 /* XXX The ADS really complicate things here and not
261 * everything is going to work correctly yet. For
262 * example it could be the same that a file contains two
263 * file streams that are identical and therefore share
264 * the same lookup table entry despite the fact that the
265 * streams themselves are not hardlinked. */
266 wimlib_assert(old_lte->refcnt > link_group_size);
268 new_lte = new_lookup_table_entry();
272 u16 num_transferred_fds = 0;
273 for (u16 i = 0; i < old_lte->num_allocated_fds; i++) {
274 if (old_lte->fds[i] &&
275 old_lte->fds[i]->dentry->hard_link ==
278 num_transferred_fds++;
281 new_lte->fds = MALLOC(num_transferred_fds *
282 sizeof(new_lte->fds[0]));
284 free_lookup_table_entry(new_lte);
288 for (u16 i = 0; i < old_lte->num_allocated_fds; i++) {
289 if (old_lte->fds[i] &&
290 old_lte->fds[i]->dentry->hard_link ==
293 struct wimlib_fd *fd = old_lte->fds[i];
294 old_lte->fds[i] = NULL;
297 new_lte->fds[j] = fd;
300 old_lte->refcnt -= link_group_size;
301 old_lte->num_opened_fds -= j;
302 new_lte->num_opened_fds = j;
306 new_lte->resource_entry.original_size = size;
307 new_lte->refcnt = link_group_size;
308 randomize_byte_array(new_lte->hash, WIM_HASH_SIZE);
309 new_lte->staging_file_name = staging_file_name;
311 lookup_table_insert(w->lookup_table, new_lte);
317 * Creates a randomly named staging directory and returns its name into the
318 * static variable staging_dir_name.
320 * If the staging directory cannot be created, staging_dir_name is set to NULL.
322 static void make_staging_dir()
324 /* XXX Give the user an option of where to stage files */
326 static char prefix[] = "wimlib-staging-";
327 static const size_t prefix_len = 15;
328 static const size_t suffix_len = 10;
330 size_t pwd_len = strlen(working_directory);
332 staging_dir_name_len = pwd_len + 1 + prefix_len + suffix_len;
334 staging_dir_name = MALLOC(staging_dir_name_len + 1);
335 if (!staging_dir_name) {
336 ERROR("Out of memory");
340 memcpy(staging_dir_name, working_directory, pwd_len);
341 staging_dir_name[pwd_len] = '/';
342 memcpy(staging_dir_name + pwd_len + 1, prefix, prefix_len);
343 randomize_char_array_with_alnum(staging_dir_name + pwd_len + 1 + prefix_len,
345 staging_dir_name[staging_dir_name_len] = '\0';
347 if (mkdir(staging_dir_name, 0700) != 0) {
348 ERROR_WITH_ERRNO("Failed to create temporary directory `%s'",
350 FREE(staging_dir_name);
351 staging_dir_name = NULL;
355 static int remove_file_or_directory(const char *fpath, const struct stat *sb,
356 int typeflag, struct FTW *ftwbuf)
358 if (remove(fpath) == 0)
361 return WIMLIB_ERR_DELETE_STAGING_DIR;
366 * Deletes the staging directory and all the files contained in it.
368 static inline int delete_staging_dir()
372 ret = nftw(staging_dir_name, remove_file_or_directory,10, FTW_DEPTH);
373 staging_dir_name = NULL;
377 /* Name and message queue descriptors for message queues between the filesystem
378 * daemon process and the unmount process. These are used when the filesystem
379 * is unmounted and the process running wimlib_mount() (i.e. the `imagex
380 * unmount' command) needs to communicate with the filesystem daemon running
381 * fuse_main() (i.e. that spawned by the `imagex mount' or `imagex mountrw'
383 static char *unmount_to_daemon_mq_name;
384 static char *daemon_to_unmount_mq_name;
385 static int unmount_to_daemon_mq;
386 static int daemon_to_unmount_mq;
388 /* Simple function that returns the concatenation of 4 strings. */
389 static char *strcat_dup(const char *s1, const char *s2, const char *s3,
392 size_t len = strlen(s1) + strlen(s2) + strlen(s3) + strlen(s4) + 1;
393 char *p = MALLOC(len);
404 /* Removes trailing forward slashes in a string. */
405 static void remove_trailing_slashes(char *s)
407 long len = strlen(s);
408 for (long i = len - 1; i >= 1; i--) {
416 /* Changes forward slashes to underscores in a string. */
417 static void s_slashes_underscores_g(char *s)
427 * Opens two POSIX message queue: one for sending messages from the unmount
428 * process to the daemon process, and one to go the other way. The names of the
429 * message queues, which must be system-wide unique, are be based on the mount
430 * point. (There of course is still a possibility of a collision if one were to
431 * unmount two identically named directories simultaneously...)
433 * @daemon specifies whether the calling process is the filesystem daemon or the
436 static int open_message_queues(bool daemon)
438 static const char *slash = "/";
439 static const char *prefix = "wimlib-";
440 static const char *u2d_suffix = "unmount-to-daemon-mq";
441 static const char *d2u_suffix = "daemon-to-unmount-mq";
443 const char *mount_dir_basename = path_basename(mount_dir);
447 unmount_to_daemon_mq_name = strcat_dup(slash, mount_dir_basename,
449 if (!unmount_to_daemon_mq_name) {
450 ERROR("Out of memory");
451 return WIMLIB_ERR_NOMEM;
453 daemon_to_unmount_mq_name = strcat_dup(slash, mount_dir_basename,
455 if (!daemon_to_unmount_mq_name) {
456 ERROR("Out of memory");
457 ret = WIMLIB_ERR_NOMEM;
461 remove_trailing_slashes(unmount_to_daemon_mq_name);
462 remove_trailing_slashes(daemon_to_unmount_mq_name);
463 s_slashes_underscores_g(unmount_to_daemon_mq_name + 1);
464 s_slashes_underscores_g(daemon_to_unmount_mq_name + 1);
467 flags = O_RDONLY | O_CREAT;
469 flags = O_WRONLY | O_CREAT;
471 unmount_to_daemon_mq = mq_open(unmount_to_daemon_mq_name, flags,
474 if (unmount_to_daemon_mq == -1) {
475 ERROR_WITH_ERRNO("mq_open()");
476 ret = WIMLIB_ERR_MQUEUE;
481 flags = O_WRONLY | O_CREAT;
483 flags = O_RDONLY | O_CREAT;
485 daemon_to_unmount_mq = mq_open(daemon_to_unmount_mq_name, flags,
488 if (daemon_to_unmount_mq == -1) {
489 ERROR_WITH_ERRNO("mq_open()");
490 ret = WIMLIB_ERR_MQUEUE;
495 mq_close(unmount_to_daemon_mq);
496 mq_unlink(unmount_to_daemon_mq_name);
498 FREE(daemon_to_unmount_mq_name);
500 FREE(unmount_to_daemon_mq_name);
504 static int mq_get_msgsize(mqd_t mq)
506 static const char *msgsize_max_file = "/proc/sys/fs/mqueue/msgsize_max";
511 if (mq_getattr(unmount_to_daemon_mq, &attr) == 0) {
512 msgsize = attr.mq_msgsize;
514 ERROR_WITH_ERRNO("mq_getattr()");
515 ERROR("Attempting to read %s", msgsize_max_file);
516 fp = fopen(msgsize_max_file, "rb");
518 if (fscanf(fp, "%d", &msgsize) != 1) {
519 ERROR("Assuming message size of 8192");
524 ERROR_WITH_ERRNO("Failed to open the file `%s'",
526 ERROR("Assuming message size of 8192");
533 /* Closes the message queues, which are allocated in static variables */
534 static void close_message_queues()
536 mq_close(unmount_to_daemon_mq);
537 mq_close(daemon_to_unmount_mq);
538 mq_unlink(unmount_to_daemon_mq_name);
539 mq_unlink(daemon_to_unmount_mq_name);
542 static int wimfs_access(const char *path, int mask)
544 /* XXX Permissions not implemented */
548 /* Closes the staging file descriptor associated with the lookup table entry, if
550 static int close_lte_fds(struct lookup_table_entry *lte, void *ignore)
552 for (u16 i = 0; i < lte->num_opened_fds; i++) {
553 if (lte->fds[i] && lte->fds[i]->staging_fd != -1) {
554 if (close(lte->fds[i]->staging_fd) != 0) {
555 ERROR_WITH_ERRNO("Failed close file `%s'",
556 lte->staging_file_name);
557 return WIMLIB_ERR_WRITE;
565 /* Calculates the SHA1 sum for @dentry if its file resource is in a staging
566 * file. Updates the SHA1 sum in the dentry and the lookup table entry. If
567 * there is already a lookup table entry with the same checksum, increment its
568 * reference count and destroy the lookup entry with the updated checksum. */
569 static int calculate_sha1sum_for_staging_file(struct dentry *dentry,
572 struct lookup_table *lookup_table = _lookup_table;
573 u8 *hash = dentry->hash;
576 struct lookup_table_entry *lte = __lookup_resource(lookup_table, hash);
577 if (lte && lte->staging_file_name) {
578 struct lookup_table_entry *existing;
581 DEBUG("Calculating SHA1 hash for file `%s'",
582 dentry->file_name_utf8);
583 ret = sha1sum(lte->staging_file_name, lte->hash);
587 lookup_table_unlink(lookup_table, lte);
588 memcpy(hash, lte->hash, WIM_HASH_SIZE);
589 existing = __lookup_resource(lookup_table, hash);
591 DEBUG("Merging duplicate lookup table entries for file "
592 "`%s'", dentry->file_name_utf8);
593 free_lookup_table_entry(lte);
596 lookup_table_insert(lookup_table, lte);
599 if (i == dentry->num_ads)
601 hash = dentry->ads_entries[i].hash;
607 /* Overwrites the WIM file, with changes saved. */
608 static int rebuild_wim(WIMStruct *w, bool check_integrity)
613 root = wim_root_dentry(w);
615 DEBUG("Closing all staging file descriptors.");
616 /* Close all the staging file descriptors. */
617 ret = for_lookup_table_entry(w->lookup_table, close_lte_fds, NULL);
619 ERROR("Failed to close all staging files");
623 DEBUG("Calculating SHA1 checksums for all new staging files.");
624 /* Calculate SHA1 checksums for all staging files, and merge unnecessary
625 * lookup table entries. */
626 ret = for_dentry_in_tree(root, calculate_sha1sum_for_staging_file,
629 ERROR("Failed to calculate new SHA1 checksums");
633 xml_update_image_info(w, w->current_image);
635 ret = wimlib_overwrite(w, check_integrity);
637 ERROR("Failed to commit changes");
643 /* Called when the filesystem is unmounted. */
644 static void wimfs_destroy(void *p)
647 /* For read-write mounts, the `imagex unmount' command, which is
648 * running in a separate process and is executing the
649 * wimlib_unmount() function, will send this process a byte
650 * through a message queue that indicates whether the --commit
651 * option was specified or not. */
654 struct timespec timeout;
656 ssize_t bytes_received;
659 char check_integrity;
662 ret = open_message_queues(true);
666 msgsize = mq_get_msgsize(unmount_to_daemon_mq);
671 /* Wait at most 3 seconds before giving up and discarding changes. */
672 gettimeofday(&now, NULL);
673 timeout.tv_sec = now.tv_sec + 3;
674 timeout.tv_nsec = now.tv_usec * 1000;
675 DEBUG("Waiting for message telling us whether to commit or not, and "
676 "whether to include integrity checks.");
678 bytes_received = mq_timedreceive(unmount_to_daemon_mq, msg,
679 msgsize, NULL, &timeout);
681 check_integrity = msg[1];
682 if (bytes_received == -1) {
683 if (errno == ETIMEDOUT) {
686 ERROR_WITH_ERRNO("mq_timedreceive()");
688 ERROR("Not committing.");
690 DEBUG("Received message: [%d %d]", msg[0], msg[1]);
694 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
696 status = chdir(working_directory);
698 ERROR_WITH_ERRNO("chdir()");
699 status = WIMLIB_ERR_NOTDIR;
702 status = rebuild_wim(w, (check_integrity != 0));
704 ret = delete_staging_dir();
706 ERROR_WITH_ERRNO("Failed to delete the staging "
713 ret = mq_send(daemon_to_unmount_mq, &status, 1, 1);
715 ERROR_WITH_ERRNO("Failed to send status to unmount process");
716 close_message_queues();
719 static int wimfs_ftruncate(const char *path, off_t size,
720 struct fuse_file_info *fi)
722 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
723 int ret = ftruncate(fd->staging_fd, size);
726 fd->lte->resource_entry.original_size = size;
731 * Fills in a `struct stat' that corresponds to a file or directory in the WIM.
733 static int wimfs_getattr(const char *path, struct stat *stbuf)
735 struct dentry *dentry = get_dentry(w, path);
738 dentry_to_stbuf(dentry, stbuf, w->lookup_table);
743 * Create a directory in the WIM.
744 * @mode is currently ignored.
746 static int wimfs_mkdir(const char *path, mode_t mode)
748 struct dentry *parent;
749 struct dentry *newdir;
750 const char *basename;
752 parent = get_parent_dentry(w, path);
756 if (!dentry_is_directory(parent))
759 basename = path_basename(path);
760 if (get_dentry_child_with_name(parent, basename))
763 newdir = new_dentry(basename);
764 newdir->attributes |= FILE_ATTRIBUTE_DIRECTORY;
765 link_dentry(newdir, parent);
770 /* Creates a regular file. */
771 static int wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
773 const char *stream_name;
774 if ((mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
775 && (stream_name = path_stream_name(path))) {
776 struct ads_entry *new_entry;
777 struct dentry *dentry;
779 dentry = get_dentry(w, path);
780 if (!dentry || !dentry_is_regular_file(dentry))
782 if (dentry_get_ads_entry(dentry, stream_name))
784 new_entry = dentry_add_ads(dentry, stream_name);
788 struct dentry *dentry, *parent;
789 const char *basename;
791 /* Make sure that the parent of @path exists and is a directory, and
792 * that the dentry named by @path does not already exist. */
793 parent = get_parent_dentry(w, path);
796 if (!dentry_is_directory(parent))
798 basename = path_basename(path);
799 if (get_dentry_child_with_name(parent, path))
802 dentry = new_dentry(basename);
803 link_dentry(dentry, parent);
810 static int wimfs_open(const char *path, struct fuse_file_info *fi)
812 struct dentry *dentry;
813 struct lookup_table_entry *lte;
816 struct wimlib_fd *fd;
818 ret = lookup_resource(w, path, get_lookup_flags(), &dentry, <e,
824 /* Empty file with no lookup-table entry. This is fine if it's
825 * a read-only filesystem. Otherwise we need to create a lookup
826 * table entry so that we can keep track of the file descriptors
827 * (this is important in case someone opens the file for
829 if (!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
834 ret = extract_resource_to_staging_dir(dentry, <e,
835 lte->resource_entry.original_size);
840 ret = alloc_wimlib_fd(lte, &fd);
846 /* The file resource may be in the staging directory (read-write
847 * mounts only) or in the WIM. If it's in the staging
848 * directory, we need to open a native file descriptor for the
849 * corresponding file. Otherwise, we can read the file resource
850 * directly from the WIM file if we are opening it read-only,
851 * but we need to extract the resource to the staging directory
852 * if we are opening it writable. */
853 if (flags_writable(fi->flags) && !lte->staging_file_name) {
854 ret = extract_resource_to_staging_dir(dentry, <e,
855 lte->resource_entry.original_size);
859 if (lte->staging_file_name) {
860 fd->staging_fd = open(lte->staging_file_name, fi->flags);
861 if (fd->staging_fd == -1) {
866 fi->fh = (uint64_t)fd;
870 /* Opens a directory. */
871 static int wimfs_opendir(const char *path, struct fuse_file_info *fi)
873 struct dentry *dentry;
875 dentry = get_dentry(w, path);
876 if (!dentry || !dentry_is_directory(dentry))
878 fi->fh = (uint64_t)dentry;
884 * Read data from a file in the WIM or in the staging directory.
886 static int wimfs_read(const char *path, char *buf, size_t size,
887 off_t offset, struct fuse_file_info *fi)
889 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
892 /* Empty file with no lookup table entry on read-only mounted
894 wimlib_assert(!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE));
898 if (fd->lte->staging_file_name) {
899 /* Read from staging file */
901 wimlib_assert(fd->staging_fd != -1);
905 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
907 ret = read(fd->staging_fd, buf, size);
915 struct resource_entry *res_entry;
918 res_entry = &fd->lte->resource_entry;
920 ctype = wim_resource_compression_type(w, res_entry);
922 if (offset > res_entry->original_size)
925 size = min(size, res_entry->original_size - offset);
927 if (read_resource(w->fp, res_entry->size,
928 res_entry->original_size,
929 res_entry->offset, ctype, size,
936 /* Fills in the entries of the directory specified by @path using the
937 * FUSE-provided function @filler. */
938 static int wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
939 off_t offset, struct fuse_file_info *fi)
941 struct dentry *parent, *child;
943 parent = (struct dentry*)fi->fh;
944 child = parent->children;
946 filler(buf, ".", NULL, 0);
947 filler(buf, "..", NULL, 0);
953 if (filler(buf, child->file_name_utf8, NULL, 0))
956 } while (child != parent->children);
961 static int wimfs_readlink(const char *path, char *buf, size_t buf_len)
963 struct dentry *dentry = get_dentry(w, path);
967 if (!dentry_is_symlink(dentry))
970 ret = dentry_readlink(dentry, buf, buf_len, w);
977 static int wimfs_release(const char *path, struct fuse_file_info *fi)
980 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
983 /* Empty file with no lookup table entry on read-only mounted
985 wimlib_assert(!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE));
988 return close_wimlib_fd(fd);
991 /* Renames a file or directory. See rename (3) */
992 static int wimfs_rename(const char *from, const char *to)
996 struct dentry *parent_of_dst;
998 src = get_dentry(w, from);
1002 dst = get_dentry(w, to);
1005 if (!dentry_is_directory(src)) {
1006 /* Cannot rename non-directory to directory. */
1007 if (dentry_is_directory(dst))
1010 /* Cannot rename directory to a non-directory or a non-empty
1012 if (!dentry_is_directory(dst))
1014 if (dst->children != NULL)
1017 parent_of_dst = dst->parent;
1019 lookup_table_decrement_refcnt(w->lookup_table, dst->hash);
1022 parent_of_dst = get_parent_dentry(w, to);
1028 change_dentry_name(src, path_basename(to));
1029 link_dentry(src, parent_of_dst);
1030 /*calculate_dentry_full_path(src);*/
1034 /* Remove a directory */
1035 static int wimfs_rmdir(const char *path)
1037 struct dentry *dentry;
1039 dentry = get_dentry(w, path);
1043 if (!dentry_is_empty_directory(dentry))
1046 unlink_dentry(dentry);
1047 free_dentry(dentry);
1052 /* Reduce the size of a file */
1053 static int wimfs_truncate(const char *path, off_t size)
1055 struct dentry *dentry;
1056 struct lookup_table_entry *lte;
1060 ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1061 <e, &dentry_hash);
1066 if (!lte) /* Already a zero-length file */
1069 if (lte->staging_file_name) {
1070 ret = truncate(lte->staging_file_name, size);
1073 lte->resource_entry.original_size = size;
1075 /* File in WIM. Extract it to the staging directory, but only
1076 * the first @size bytes of it. */
1077 ret = extract_resource_to_staging_dir(dentry, <e, size);
1079 dentry_update_all_timestamps(dentry);
1083 /* Remove a regular file */
1084 static int wimfs_unlink(const char *path)
1086 struct dentry *dentry;
1087 struct lookup_table_entry *lte;
1091 ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1092 <e, &dentry_hash);
1097 if (lte && lte->staging_file_name)
1098 if (unlink(lte->staging_file_name) != 0)
1101 if (dentry_hash == dentry->hash) {
1102 /* We are removing the full dentry including all alternate data
1104 const u8 *hash = dentry->hash;
1107 lookup_table_decrement_refcnt(w->lookup_table, hash);
1108 if (i == dentry->num_ads)
1110 hash = dentry->ads_entries[i].hash;
1114 unlink_dentry(dentry);
1115 free_dentry(dentry);
1117 /* We are removing an alternate data stream. */
1118 struct ads_entry *cur_entry = dentry->ads_entries;
1119 while (cur_entry->hash != dentry_hash)
1121 lookup_table_decrement_refcnt(w->lookup_table, cur_entry->hash);
1123 dentry_remove_ads(dentry, cur_entry);
1125 /* Beware: The lookup table entry(s) may still be referenced by users
1126 * that have opened the corresponding streams. They are freed later in
1127 * wimfs_release() when the last file user has closed the stream. */
1131 /* Change the timestamp on a file dentry.
1133 * There is no distinction between a file and its alternate data streams here. */
1134 static int wimfs_utimens(const char *path, const struct timespec tv[2])
1136 struct dentry *dentry = get_dentry(w, path);
1139 time_t last_access_t = (tv[0].tv_nsec == UTIME_NOW) ?
1140 time(NULL) : tv[0].tv_sec;
1141 dentry->last_access_time = unix_timestamp_to_ms(last_access_t);
1142 time_t last_mod_t = (tv[1].tv_nsec == UTIME_NOW) ?
1143 time(NULL) : tv[1].tv_sec;
1144 dentry->last_write_time = unix_timestamp_to_ms(last_mod_t);
1148 /* Writes to a file in the WIM filesystem.
1149 * It may be an alternate data stream, but here we don't even notice because we
1150 * just get a lookup table entry. */
1151 static int wimfs_write(const char *path, const char *buf, size_t size,
1152 off_t offset, struct fuse_file_info *fi)
1154 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
1157 wimlib_assert(fd->lte);
1158 wimlib_assert(fd->lte->staging_file_name);
1159 wimlib_assert(fd->staging_fd != -1);
1161 /* Write the data. */
1162 ret = write(fd->staging_fd, buf, size);
1166 /* The file has been modified, so all its timestamps must be
1168 dentry_update_all_timestamps(fd->dentry);
1174 static struct fuse_operations wimfs_oper = {
1175 .access = wimfs_access,
1176 .destroy = wimfs_destroy,
1177 .ftruncate = wimfs_ftruncate,
1178 .getattr = wimfs_getattr,
1179 .mkdir = wimfs_mkdir,
1180 .mknod = wimfs_mknod,
1182 .opendir = wimfs_opendir,
1184 .readdir = wimfs_readdir,
1185 .readlink = wimfs_readlink,
1186 .release = wimfs_release,
1187 .rename = wimfs_rename,
1188 .rmdir = wimfs_rmdir,
1189 .truncate = wimfs_truncate,
1190 .unlink = wimfs_unlink,
1191 .utimens = wimfs_utimens,
1192 .write = wimfs_write,
1196 /* Mounts a WIM file. */
1197 WIMLIBAPI int wimlib_mount(WIMStruct *wim, int image, const char *dir,
1205 DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
1206 wim, image, dir, flags);
1209 return WIMLIB_ERR_INVALID_PARAM;
1211 ret = wimlib_select_image(wim, image);
1216 if (flags & WIMLIB_MOUNT_FLAG_READWRITE)
1217 wim_get_current_image_metadata(wim)->modified = true;
1219 if (!(flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
1220 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
1221 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
1222 flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1225 working_directory = getcwd(NULL, 0);
1226 if (!working_directory) {
1227 ERROR_WITH_ERRNO("Could not determine current directory");
1228 return WIMLIB_ERR_NOTDIR;
1233 return WIMLIB_ERR_NOMEM;
1235 argv[argc++] = "mount";
1237 argv[argc++] = "-s"; /* disable multi-threaded operation */
1239 if (flags & WIMLIB_MOUNT_FLAG_DEBUG) {
1240 argv[argc++] = "-d";
1242 if (!(flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
1243 argv[argc++] = "-o";
1244 argv[argc++] = "ro";
1247 if (!staging_dir_name) {
1249 return WIMLIB_ERR_MKDIR;
1256 DEBUG("FUSE command line (argc = %d): ", argc);
1257 for (i = 0; i < argc; i++) {
1258 fputs(argv[i], stdout);
1266 /* Set static variables. */
1268 mount_flags = flags;
1270 ret = fuse_main(argc, argv, &wimfs_oper, NULL);
1272 return (ret == 0) ? 0 : WIMLIB_ERR_FUSE;
1277 * Unmounts the WIM file that was previously mounted on @dir by using
1280 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
1287 struct timespec timeout;
1291 /* Execute `fusermount -u', which is installed setuid root, to unmount
1294 * FUSE does not yet implement synchronous unmounts. This means that
1295 * fusermount -u will return before the filesystem daemon returns from
1296 * wimfs_destroy(). This is partly what we want, because we need to
1297 * send a message from this process to the filesystem daemon telling
1298 * whether --commit was specified or not. However, after that, the
1299 * unmount process must wait for the filesystem daemon to finish writing
1306 ERROR_WITH_ERRNO("Failed to fork()");
1307 return WIMLIB_ERR_FORK;
1310 execlp("fusermount", "fusermount", "-u", dir, NULL);
1311 ERROR_WITH_ERRNO("Failed to execute `fusermount'");
1312 return WIMLIB_ERR_FUSERMOUNT;
1315 ret = waitpid(pid, &status, 0);
1317 ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
1319 return WIMLIB_ERR_FUSERMOUNT;
1323 ERROR("fusermount exited with status %d", status);
1324 return WIMLIB_ERR_FUSERMOUNT;
1327 /* Open message queues between the unmount process and the
1328 * filesystem daemon. */
1329 ret = open_message_queues(false);
1333 /* Send a message to the filesystem saying whether to commit or
1335 msg[0] = (flags & WIMLIB_UNMOUNT_FLAG_COMMIT) ? 1 : 0;
1336 msg[1] = (flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY) ? 1 : 0;
1338 DEBUG("Sending message: %s, %s",
1339 (msg[0] == 0) ? "don't commit" : "commit",
1340 (msg[1] == 0) ? "don't check" : "check");
1341 ret = mq_send(unmount_to_daemon_mq, msg, 2, 1);
1343 ERROR("Failed to notify filesystem daemon whether we want to "
1344 "commit changes or not");
1345 close_message_queues();
1346 return WIMLIB_ERR_MQUEUE;
1349 /* Wait for a message from the filesytem daemon indicating whether the
1350 * filesystem was unmounted successfully (0) or an error occurred (1).
1351 * This may take a long time if a big WIM file needs to be rewritten. */
1353 /* Wait at most 600??? seconds before giving up and returning false.
1354 * Either it's a really big WIM file, or (more likely) the
1355 * filesystem daemon has crashed or failed for some reason.
1357 * XXX come up with some method to determine if the filesystem
1358 * daemon has really crashed or not. */
1360 gettimeofday(&now, NULL);
1361 timeout.tv_sec = now.tv_sec + 600;
1362 timeout.tv_nsec = now.tv_usec * 1000;
1364 msgsize = mq_get_msgsize(daemon_to_unmount_mq);
1365 char mailbox[msgsize];
1368 DEBUG("Waiting for message telling us whether the unmount was "
1369 "successful or not.");
1370 ret = mq_timedreceive(daemon_to_unmount_mq, mailbox, msgsize,
1373 close_message_queues();
1375 if (errno_save == ETIMEDOUT) {
1376 ERROR("Timed out- probably the filesystem daemon "
1377 "crashed and the WIM was not written "
1379 return WIMLIB_ERR_TIMEOUT;
1381 ERROR("mq_receive(): %s", strerror(errno_save));
1382 return WIMLIB_ERR_MQUEUE;
1386 DEBUG("Received message: %s",
1387 (mailbox[0] == 0) ? "Unmount OK" : "Unmount Failed");
1388 if (mailbox[0] != 0)
1389 ERROR("Unmount failed");
1393 #else /* WITH_FUSE */
1396 static inline int mount_unsupported_error()
1398 ERROR("WIMLIB was compiled with --without-fuse, which disables support "
1399 "for mounting WIMs.");
1400 return WIMLIB_ERR_UNSUPPORTED;
1403 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
1405 return mount_unsupported_error();
1408 WIMLIBAPI int wimlib_mount(WIMStruct *wim_p, int image, const char *dir,
1411 return mount_unsupported_error();
1414 #endif /* WITH_FUSE */