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 if (lte->num_opened_fds == lte->num_allocated_fds) {
94 struct wimlib_fd **fds;
95 if (lte->num_allocated_fds > 0xffff - 8)
98 fds = CALLOC(lte->num_allocated_fds + 8, sizeof(lte->fds[0]));
101 memcpy(fds, lte->fds,
102 lte->num_allocated_fds * sizeof(lte->fds[0]));
106 for (u16 i = 0; ; i++) {
107 struct wimlib_fd *fd = lte->fds[i];
109 fd = CALLOC(1, sizeof(*fd));
122 static int close_wimlib_fd(struct wimlib_fd *fd)
124 struct lookup_table_entry *lte = fd->lte;
127 wimlib_assert(lte->num_opened_fds);
129 if (lte->staging_file_name) {
130 wimlib_assert(fd->staging_fd != -1);
131 if (close(fd->staging_fd) != 0)
134 if (--lte->num_opened_fds == 0 && lte->refcnt == 0)
135 free_lookup_table_entry(lte);
136 lte->fds[fd->idx] = NULL;
141 /* Creates a new staging file and returns its file descriptor opened for
144 * @name_ret: A location into which the a pointer to the newly allocated name of
145 * the staging file is stored.
146 * @return: The file descriptor for the new file. Returns -1 and sets errno on
147 * error, for any reason possible from the creat() function.
149 static int create_staging_file(char **name_ret, int open_flags)
157 name_len = staging_dir_name_len + 1 + WIM_HASH_SIZE;
158 name = MALLOC(name_len + 1);
166 memcpy(name, staging_dir_name, staging_dir_name_len);
167 name[staging_dir_name_len] = '/';
168 randomize_char_array_with_alnum(name + staging_dir_name_len + 1,
170 name[name_len] = '\0';
173 /* Just in case, verify that the randomly generated name doesn't name an
174 * existing file, and try again if so */
175 } while (stat(name, &stbuf) == 0);
181 /* doesn't exist--- ok */
183 DEBUG("Creating staging file `%s'", name);
185 fd = open(name, open_flags | O_CREAT | O_TRUNC, 0600);
197 * Extract a WIM resource to the staging directory.
200 * - Create a staging file for the WIM resource
201 * - Extract the resource to it
202 * - Create a new lte for the file resource
203 * - Transfer fds from the old lte to the new lte, but
204 * only if they share the same hard link group as this
207 static int extract_resource_to_staging_dir(struct dentry *dentry,
208 struct lookup_table_entry **lte,
211 char *staging_file_name;
214 struct lookup_table_entry *old_lte, *new_lte;
215 size_t link_group_size;
218 fd = create_staging_file(&staging_file_name, O_WRONLY);
223 ret = extract_resource_to_fd(w, &old_lte->resource_entry, fd,
227 if (ret != 0 || close(fd) != 0) {
233 unlink(staging_file_name);
234 FREE(staging_file_name);
238 link_group_size = dentry_link_group_size(dentry);
241 if (link_group_size == old_lte->refcnt) {
242 /* This hard link group is the only user of the lookup
243 * table entry, so we can re-use it. */
244 lookup_table_remove(w->lookup_table, old_lte);
247 /* Split a hard link group away from the "lookup table
248 * entry" hard link group (i.e. we had two hard link
249 * groups that were identical, but now we are changing
252 /* XXX The ADS really complicate things here and not
253 * everything is going to work correctly yet. For
254 * example it could be the same that a file contains two
255 * file streams that are identical and therefore share
256 * the same lookup table entry despite the fact that the
257 * streams themselves are not hardlinked. */
258 wimlib_assert(old_lte->refcnt > link_group_size);
260 new_lte = new_lookup_table_entry();
264 u16 num_transferred_fds = 0;
265 for (u16 i = 0; i < old_lte->num_allocated_fds; i++) {
266 if (old_lte->fds[i] &&
267 old_lte->fds[i]->dentry->hard_link ==
270 num_transferred_fds++;
273 new_lte->fds = MALLOC(num_transferred_fds *
274 sizeof(new_lte->fds[0]));
276 free_lookup_table_entry(new_lte);
280 for (u16 i = 0; i < old_lte->num_allocated_fds; i++) {
281 if (old_lte->fds[i] &&
282 old_lte->fds[i]->dentry->hard_link ==
285 struct wimlib_fd *fd = old_lte->fds[i];
286 old_lte->fds[i] = NULL;
289 new_lte->fds[j] = fd;
292 old_lte->refcnt -= link_group_size;
293 old_lte->num_opened_fds -= j;
294 new_lte->num_opened_fds = j;
298 new_lte->resource_entry.original_size = size;
299 new_lte->refcnt = link_group_size;
300 randomize_byte_array(new_lte->hash, WIM_HASH_SIZE);
301 new_lte->staging_file_name = staging_file_name;
303 lookup_table_insert(w->lookup_table, new_lte);
309 * Creates a randomly named staging directory and returns its name into the
310 * static variable staging_dir_name.
312 * If the staging directory cannot be created, staging_dir_name is set to NULL.
314 static void make_staging_dir()
316 /* XXX Give the user an option of where to stage files */
318 static char prefix[] = "wimlib-staging-";
319 static const size_t prefix_len = 15;
320 static const size_t suffix_len = 10;
322 size_t pwd_len = strlen(working_directory);
324 staging_dir_name_len = pwd_len + 1 + prefix_len + suffix_len;
326 staging_dir_name = MALLOC(staging_dir_name_len + 1);
327 if (!staging_dir_name) {
328 ERROR("Out of memory");
332 memcpy(staging_dir_name, working_directory, pwd_len);
333 staging_dir_name[pwd_len] = '/';
334 memcpy(staging_dir_name + pwd_len + 1, prefix, prefix_len);
335 randomize_char_array_with_alnum(staging_dir_name + pwd_len + 1 + prefix_len,
337 staging_dir_name[staging_dir_name_len] = '\0';
339 if (mkdir(staging_dir_name, 0700) != 0) {
340 ERROR_WITH_ERRNO("Failed to create temporary directory `%s'",
342 FREE(staging_dir_name);
343 staging_dir_name = NULL;
347 static int remove_file_or_directory(const char *fpath, const struct stat *sb,
348 int typeflag, struct FTW *ftwbuf)
350 if (remove(fpath) == 0)
353 return WIMLIB_ERR_DELETE_STAGING_DIR;
358 * Deletes the staging directory and all the files contained in it.
360 static inline int delete_staging_dir()
364 ret = nftw(staging_dir_name, remove_file_or_directory,10, FTW_DEPTH);
365 staging_dir_name = NULL;
369 /* Name and message queue descriptors for message queues between the filesystem
370 * daemon process and the unmount process. These are used when the filesystem
371 * is unmounted and the process running wimlib_mount() (i.e. the `imagex
372 * unmount' command) needs to communicate with the filesystem daemon running
373 * fuse_main() (i.e. that spawned by the `imagex mount' or `imagex mountrw'
375 static char *unmount_to_daemon_mq_name;
376 static char *daemon_to_unmount_mq_name;
377 static int unmount_to_daemon_mq;
378 static int daemon_to_unmount_mq;
380 /* Simple function that returns the concatenation of 4 strings. */
381 static char *strcat_dup(const char *s1, const char *s2, const char *s3,
384 size_t len = strlen(s1) + strlen(s2) + strlen(s3) + strlen(s4) + 1;
385 char *p = MALLOC(len);
396 /* Removes trailing forward slashes in a string. */
397 static void remove_trailing_slashes(char *s)
399 long len = strlen(s);
400 for (long i = len - 1; i >= 1; i--) {
408 /* Changes forward slashes to underscores in a string. */
409 static void s_slashes_underscores_g(char *s)
419 * Opens two POSIX message queue: one for sending messages from the unmount
420 * process to the daemon process, and one to go the other way. The names of the
421 * message queues, which must be system-wide unique, are be based on the mount
422 * point. (There of course is still a possibility of a collision if one were to
423 * unmount two identically named directories simultaneously...)
425 * @daemon specifies whether the calling process is the filesystem daemon or the
428 static int open_message_queues(bool daemon)
430 static const char *slash = "/";
431 static const char *prefix = "wimlib-";
432 static const char *u2d_suffix = "unmount-to-daemon-mq";
433 static const char *d2u_suffix = "daemon-to-unmount-mq";
435 const char *mount_dir_basename = path_basename(mount_dir);
439 unmount_to_daemon_mq_name = strcat_dup(slash, mount_dir_basename,
441 if (!unmount_to_daemon_mq_name) {
442 ERROR("Out of memory");
443 return WIMLIB_ERR_NOMEM;
445 daemon_to_unmount_mq_name = strcat_dup(slash, mount_dir_basename,
447 if (!daemon_to_unmount_mq_name) {
448 ERROR("Out of memory");
449 ret = WIMLIB_ERR_NOMEM;
453 remove_trailing_slashes(unmount_to_daemon_mq_name);
454 remove_trailing_slashes(daemon_to_unmount_mq_name);
455 s_slashes_underscores_g(unmount_to_daemon_mq_name + 1);
456 s_slashes_underscores_g(daemon_to_unmount_mq_name + 1);
459 flags = O_RDONLY | O_CREAT;
461 flags = O_WRONLY | O_CREAT;
463 unmount_to_daemon_mq = mq_open(unmount_to_daemon_mq_name, flags,
466 if (unmount_to_daemon_mq == -1) {
467 ERROR_WITH_ERRNO("mq_open()");
468 ret = WIMLIB_ERR_MQUEUE;
473 flags = O_WRONLY | O_CREAT;
475 flags = O_RDONLY | O_CREAT;
477 daemon_to_unmount_mq = mq_open(daemon_to_unmount_mq_name, flags,
480 if (daemon_to_unmount_mq == -1) {
481 ERROR_WITH_ERRNO("mq_open()");
482 ret = WIMLIB_ERR_MQUEUE;
487 mq_close(unmount_to_daemon_mq);
488 mq_unlink(unmount_to_daemon_mq_name);
490 FREE(daemon_to_unmount_mq_name);
492 FREE(unmount_to_daemon_mq_name);
496 static int mq_get_msgsize(mqd_t mq)
498 static const char *msgsize_max_file = "/proc/sys/fs/mqueue/msgsize_max";
503 if (mq_getattr(unmount_to_daemon_mq, &attr) == 0) {
504 msgsize = attr.mq_msgsize;
506 ERROR_WITH_ERRNO("mq_getattr()");
507 ERROR("Attempting to read %s", msgsize_max_file);
508 fp = fopen(msgsize_max_file, "rb");
510 if (fscanf(fp, "%d", &msgsize) != 1) {
511 ERROR("Assuming message size of 8192");
516 ERROR_WITH_ERRNO("Failed to open the file `%s'",
518 ERROR("Assuming message size of 8192");
525 /* Closes the message queues, which are allocated in static variables */
526 static void close_message_queues()
528 mq_close(unmount_to_daemon_mq);
529 mq_close(daemon_to_unmount_mq);
530 mq_unlink(unmount_to_daemon_mq_name);
531 mq_unlink(daemon_to_unmount_mq_name);
534 static int wimfs_access(const char *path, int mask)
536 /* XXX Permissions not implemented */
540 /* Closes the staging file descriptor associated with the lookup table entry, if
542 static int close_lte_fds(struct lookup_table_entry *lte, void *ignore)
544 for (u16 i = 0; i < lte->num_opened_fds; i++) {
545 if (lte->fds[i] && lte->fds[i]->staging_fd != -1) {
546 if (close(lte->fds[i]->staging_fd) != 0) {
547 ERROR_WITH_ERRNO("Failed close file `%s'",
548 lte->staging_file_name);
549 return WIMLIB_ERR_WRITE;
557 /* Calculates the SHA1 sum for @dentry if its file resource is in a staging
558 * file. Updates the SHA1 sum in the dentry and the lookup table entry. If
559 * there is already a lookup table entry with the same checksum, increment its
560 * reference count and destroy the lookup entry with the updated checksum. */
561 static int calculate_sha1sum_for_staging_file(struct dentry *dentry,
564 struct lookup_table *lookup_table = _lookup_table;
565 u8 *hash = dentry->hash;
568 struct lookup_table_entry *lte = __lookup_resource(lookup_table, hash);
569 if (lte && lte->staging_file_name) {
570 struct lookup_table_entry *existing;
573 DEBUG("Calculating SHA1 hash for file `%s'",
574 dentry->file_name_utf8);
575 ret = sha1sum(lte->staging_file_name, lte->hash);
579 lookup_table_unlink(lookup_table, lte);
580 memcpy(hash, lte->hash, WIM_HASH_SIZE);
581 existing = __lookup_resource(lookup_table, hash);
583 DEBUG("Merging duplicate lookup table entries for file "
584 "`%s'", dentry->file_name_utf8);
585 free_lookup_table_entry(lte);
588 lookup_table_insert(lookup_table, lte);
591 if (i == dentry->num_ads)
593 hash = dentry->ads_entries[i].hash;
599 /* Overwrites the WIM file, with changes saved. */
600 static int rebuild_wim(WIMStruct *w, bool check_integrity)
605 root = wim_root_dentry(w);
607 DEBUG("Closing all staging file descriptors.");
608 /* Close all the staging file descriptors. */
609 ret = for_lookup_table_entry(w->lookup_table, close_lte_fds, NULL);
611 ERROR("Failed to close all staging files");
615 DEBUG("Calculating SHA1 checksums for all new staging files.");
616 /* Calculate SHA1 checksums for all staging files, and merge unnecessary
617 * lookup table entries. */
618 ret = for_dentry_in_tree(root, calculate_sha1sum_for_staging_file,
621 ERROR("Failed to calculate new SHA1 checksums");
625 xml_update_image_info(w, w->current_image);
627 ret = wimlib_overwrite(w, check_integrity);
629 ERROR("Failed to commit changes");
635 /* Called when the filesystem is unmounted. */
636 static void wimfs_destroy(void *p)
639 /* For read-write mounts, the `imagex unmount' command, which is
640 * running in a separate process and is executing the
641 * wimlib_unmount() function, will send this process a byte
642 * through a message queue that indicates whether the --commit
643 * option was specified or not. */
646 struct timespec timeout;
648 ssize_t bytes_received;
651 char check_integrity;
654 ret = open_message_queues(true);
658 msgsize = mq_get_msgsize(unmount_to_daemon_mq);
663 /* Wait at most 3 seconds before giving up and discarding changes. */
664 gettimeofday(&now, NULL);
665 timeout.tv_sec = now.tv_sec + 3;
666 timeout.tv_nsec = now.tv_usec * 1000;
667 DEBUG("Waiting for message telling us whether to commit or not, and "
668 "whether to include integrity checks.");
670 bytes_received = mq_timedreceive(unmount_to_daemon_mq, msg,
671 msgsize, NULL, &timeout);
673 check_integrity = msg[1];
674 if (bytes_received == -1) {
675 if (errno == ETIMEDOUT) {
678 ERROR_WITH_ERRNO("mq_timedreceive()");
680 ERROR("Not committing.");
682 DEBUG("Received message: [%d %d]", msg[0], msg[1]);
686 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
688 status = chdir(working_directory);
690 ERROR_WITH_ERRNO("chdir()");
691 status = WIMLIB_ERR_NOTDIR;
694 status = rebuild_wim(w, (check_integrity != 0));
696 ret = delete_staging_dir();
698 ERROR_WITH_ERRNO("Failed to delete the staging "
705 ret = mq_send(daemon_to_unmount_mq, &status, 1, 1);
707 ERROR_WITH_ERRNO("Failed to send status to unmount process");
708 close_message_queues();
711 static int wimfs_ftruncate(const char *path, off_t size,
712 struct fuse_file_info *fi)
714 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
715 int ret = ftruncate(fd->staging_fd, size);
718 fd->lte->resource_entry.original_size = size;
723 * Fills in a `struct stat' that corresponds to a file or directory in the WIM.
725 static int wimfs_getattr(const char *path, struct stat *stbuf)
727 struct dentry *dentry = get_dentry(w, path);
730 dentry_to_stbuf(dentry, stbuf, w->lookup_table);
735 * Create a directory in the WIM.
736 * @mode is currently ignored.
738 static int wimfs_mkdir(const char *path, mode_t mode)
740 struct dentry *parent;
741 struct dentry *newdir;
742 const char *basename;
744 parent = get_parent_dentry(w, path);
748 if (!dentry_is_directory(parent))
751 basename = path_basename(path);
752 if (get_dentry_child_with_name(parent, basename))
755 newdir = new_dentry(basename);
756 newdir->attributes |= FILE_ATTRIBUTE_DIRECTORY;
757 link_dentry(newdir, parent);
762 /* Creates a regular file. */
763 static int wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
765 const char *stream_name;
766 if ((mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
767 && (stream_name = path_stream_name(path))) {
768 struct ads_entry *new_entry;
769 struct dentry *dentry;
771 dentry = get_dentry(w, path);
772 if (!dentry || !dentry_is_regular_file(dentry))
774 if (dentry_get_ads_entry(dentry, stream_name))
776 new_entry = dentry_add_ads(dentry, stream_name);
780 struct dentry *dentry, *parent;
781 const char *basename;
783 /* Make sure that the parent of @path exists and is a directory, and
784 * that the dentry named by @path does not already exist. */
785 parent = get_parent_dentry(w, path);
788 if (!dentry_is_directory(parent))
790 basename = path_basename(path);
791 if (get_dentry_child_with_name(parent, path))
794 dentry = new_dentry(basename);
795 link_dentry(dentry, parent);
802 static int wimfs_open(const char *path, struct fuse_file_info *fi)
804 struct dentry *dentry;
805 struct lookup_table_entry *lte;
808 struct wimlib_fd *fd;
810 ret = lookup_resource(w, path, get_lookup_flags(), &dentry, <e,
816 /* Empty file with no lookup-table entry. This is fine if it's
817 * a read-only filesystem. Otherwise we need to create a lookup
818 * table entry so that we can keep track of the file descriptors
819 * (this is important in case someone opens the file for
821 if (!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
826 ret = extract_resource_to_staging_dir(dentry, <e,
827 lte->resource_entry.original_size);
832 ret = alloc_wimlib_fd(lte, &fd);
838 /* The file resource may be in the staging directory (read-write
839 * mounts only) or in the WIM. If it's in the staging
840 * directory, we need to open a native file descriptor for the
841 * corresponding file. Otherwise, we can read the file resource
842 * directly from the WIM file if we are opening it read-only,
843 * but we need to extract the resource to the staging directory
844 * if we are opening it writable. */
845 if (flags_writable(fi->flags) && !lte->staging_file_name) {
846 ret = extract_resource_to_staging_dir(dentry, <e,
847 lte->resource_entry.original_size);
851 if (lte->staging_file_name) {
852 fd->staging_fd = open(lte->staging_file_name, fi->flags);
853 if (fd->staging_fd == -1) {
858 fi->fh = (uint64_t)fd;
862 /* Opens a directory. */
863 static int wimfs_opendir(const char *path, struct fuse_file_info *fi)
865 struct dentry *dentry;
867 dentry = get_dentry(w, path);
868 if (!dentry || !dentry_is_directory(dentry))
870 fi->fh = (uint64_t)dentry;
876 * Read data from a file in the WIM or in the staging directory.
878 static int wimfs_read(const char *path, char *buf, size_t size,
879 off_t offset, struct fuse_file_info *fi)
881 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
884 /* Empty file with no lookup table entry on read-only mounted
886 wimlib_assert(!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE));
890 if (fd->lte->staging_file_name) {
891 /* Read from staging file */
893 wimlib_assert(fd->staging_fd != -1);
897 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
899 ret = read(fd->staging_fd, buf, size);
907 struct resource_entry *res_entry;
910 res_entry = &fd->lte->resource_entry;
912 ctype = wim_resource_compression_type(w, res_entry);
914 if (offset > res_entry->original_size)
917 size = min(size, res_entry->original_size - offset);
919 if (read_resource(w->fp, res_entry->size,
920 res_entry->original_size,
921 res_entry->offset, ctype, size,
928 /* Fills in the entries of the directory specified by @path using the
929 * FUSE-provided function @filler. */
930 static int wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
931 off_t offset, struct fuse_file_info *fi)
933 struct dentry *parent, *child;
935 parent = (struct dentry*)fi->fh;
936 child = parent->children;
938 filler(buf, ".", NULL, 0);
939 filler(buf, "..", NULL, 0);
945 if (filler(buf, child->file_name_utf8, NULL, 0))
948 } while (child != parent->children);
953 static int wimfs_readlink(const char *path, char *buf, size_t buf_len)
955 struct dentry *dentry = get_dentry(w, path);
959 if (!dentry_is_symlink(dentry))
962 ret = dentry_readlink(dentry, buf, buf_len, w);
969 static int wimfs_release(const char *path, struct fuse_file_info *fi)
972 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
975 /* Empty file with no lookup table entry on read-only mounted
977 wimlib_assert(!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE));
980 return close_wimlib_fd(fd);
983 /* Renames a file or directory. See rename (3) */
984 static int wimfs_rename(const char *from, const char *to)
988 struct dentry *parent_of_dst;
990 src = get_dentry(w, from);
994 dst = get_dentry(w, to);
997 if (!dentry_is_directory(src)) {
998 /* Cannot rename non-directory to directory. */
999 if (dentry_is_directory(dst))
1002 /* Cannot rename directory to a non-directory or a non-empty
1004 if (!dentry_is_directory(dst))
1006 if (dst->children != NULL)
1009 parent_of_dst = dst->parent;
1011 lookup_table_decrement_refcnt(w->lookup_table, dst->hash);
1014 parent_of_dst = get_parent_dentry(w, to);
1020 change_dentry_name(src, path_basename(to));
1021 link_dentry(src, parent_of_dst);
1022 /*calculate_dentry_full_path(src);*/
1026 /* Remove a directory */
1027 static int wimfs_rmdir(const char *path)
1029 struct dentry *dentry;
1031 dentry = get_dentry(w, path);
1035 if (!dentry_is_empty_directory(dentry))
1038 unlink_dentry(dentry);
1039 free_dentry(dentry);
1044 /* Reduce the size of a file */
1045 static int wimfs_truncate(const char *path, off_t size)
1047 struct dentry *dentry;
1048 struct lookup_table_entry *lte;
1052 ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1053 <e, &dentry_hash);
1058 if (!lte) /* Already a zero-length file */
1061 if (lte->staging_file_name) {
1062 ret = truncate(lte->staging_file_name, size);
1065 lte->resource_entry.original_size = size;
1067 /* File in WIM. Extract it to the staging directory, but only
1068 * the first @size bytes of it. */
1069 ret = extract_resource_to_staging_dir(dentry, <e, size);
1071 dentry_update_all_timestamps(dentry);
1075 /* Remove a regular file */
1076 static int wimfs_unlink(const char *path)
1078 struct dentry *dentry;
1079 struct lookup_table_entry *lte;
1083 ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1084 <e, &dentry_hash);
1089 if (lte && lte->staging_file_name)
1090 if (unlink(lte->staging_file_name) != 0)
1093 if (dentry_hash == dentry->hash) {
1094 /* We are removing the full dentry including all alternate data
1096 const u8 *hash = dentry->hash;
1099 lookup_table_decrement_refcnt(w->lookup_table, hash);
1100 if (i == dentry->num_ads)
1102 hash = dentry->ads_entries[i].hash;
1106 unlink_dentry(dentry);
1107 free_dentry(dentry);
1109 /* We are removing an alternate data stream. */
1110 struct ads_entry *cur_entry = dentry->ads_entries;
1111 while (cur_entry->hash != dentry_hash)
1113 lookup_table_decrement_refcnt(w->lookup_table, cur_entry->hash);
1115 dentry_remove_ads(dentry, cur_entry);
1117 /* Beware: The lookup table entry(s) may still be referenced by users
1118 * that have opened the corresponding streams. They are freed later in
1119 * wimfs_release() when the last file user has closed the stream. */
1123 /* Change the timestamp on a file dentry.
1125 * There is no distinction between a file and its alternate data streams here. */
1126 static int wimfs_utimens(const char *path, const struct timespec tv[2])
1128 struct dentry *dentry = get_dentry(w, path);
1131 time_t last_access_t = (tv[0].tv_nsec == UTIME_NOW) ?
1132 time(NULL) : tv[0].tv_sec;
1133 dentry->last_access_time = unix_timestamp_to_ms(last_access_t);
1134 time_t last_mod_t = (tv[1].tv_nsec == UTIME_NOW) ?
1135 time(NULL) : tv[1].tv_sec;
1136 dentry->last_write_time = unix_timestamp_to_ms(last_mod_t);
1140 /* Writes to a file in the WIM filesystem.
1141 * It may be an alternate data stream, but here we don't even notice because we
1142 * just get a lookup table entry. */
1143 static int wimfs_write(const char *path, const char *buf, size_t size,
1144 off_t offset, struct fuse_file_info *fi)
1146 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
1149 wimlib_assert(fd->lte);
1150 wimlib_assert(fd->lte->staging_file_name);
1151 wimlib_assert(fd->staging_fd != -1);
1153 /* Write the data. */
1154 ret = write(fd->staging_fd, buf, size);
1158 /* The file has been modified, so all its timestamps must be
1160 dentry_update_all_timestamps(fd->dentry);
1166 static struct fuse_operations wimfs_oper = {
1167 .access = wimfs_access,
1168 .destroy = wimfs_destroy,
1169 .ftruncate = wimfs_ftruncate,
1170 .getattr = wimfs_getattr,
1171 .mkdir = wimfs_mkdir,
1172 .mknod = wimfs_mknod,
1174 .opendir = wimfs_opendir,
1176 .readdir = wimfs_readdir,
1177 .readlink = wimfs_readlink,
1178 .release = wimfs_release,
1179 .rename = wimfs_rename,
1180 .rmdir = wimfs_rmdir,
1181 .truncate = wimfs_truncate,
1182 .unlink = wimfs_unlink,
1183 .utimens = wimfs_utimens,
1184 .write = wimfs_write,
1188 /* Mounts a WIM file. */
1189 WIMLIBAPI int wimlib_mount(WIMStruct *wim, int image, const char *dir,
1197 DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
1198 wim, image, dir, flags);
1201 return WIMLIB_ERR_INVALID_PARAM;
1203 ret = wimlib_select_image(wim, image);
1208 if (flags & WIMLIB_MOUNT_FLAG_READWRITE)
1209 wim_get_current_image_metadata(wim)->modified = true;
1211 if (!(flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
1212 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
1213 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
1214 flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1217 working_directory = getcwd(NULL, 0);
1218 if (!working_directory) {
1219 ERROR_WITH_ERRNO("Could not determine current directory");
1220 return WIMLIB_ERR_NOTDIR;
1225 return WIMLIB_ERR_NOMEM;
1227 argv[argc++] = "mount";
1229 argv[argc++] = "-s"; /* disable multi-threaded operation */
1231 if (flags & WIMLIB_MOUNT_FLAG_DEBUG) {
1232 argv[argc++] = "-d";
1234 if (!(flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
1235 argv[argc++] = "-o";
1236 argv[argc++] = "ro";
1239 if (!staging_dir_name) {
1241 return WIMLIB_ERR_MKDIR;
1248 DEBUG("FUSE command line (argc = %d): ", argc);
1249 for (i = 0; i < argc; i++) {
1250 fputs(argv[i], stdout);
1258 /* Set static variables. */
1260 mount_flags = flags;
1262 ret = fuse_main(argc, argv, &wimfs_oper, NULL);
1264 return (ret == 0) ? 0 : WIMLIB_ERR_FUSE;
1269 * Unmounts the WIM file that was previously mounted on @dir by using
1272 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
1279 struct timespec timeout;
1283 /* Execute `fusermount -u', which is installed setuid root, to unmount
1286 * FUSE does not yet implement synchronous unmounts. This means that
1287 * fusermount -u will return before the filesystem daemon returns from
1288 * wimfs_destroy(). This is partly what we want, because we need to
1289 * send a message from this process to the filesystem daemon telling
1290 * whether --commit was specified or not. However, after that, the
1291 * unmount process must wait for the filesystem daemon to finish writing
1298 ERROR_WITH_ERRNO("Failed to fork()");
1299 return WIMLIB_ERR_FORK;
1302 execlp("fusermount", "fusermount", "-u", dir, NULL);
1303 ERROR_WITH_ERRNO("Failed to execute `fusermount'");
1304 return WIMLIB_ERR_FUSERMOUNT;
1307 ret = waitpid(pid, &status, 0);
1309 ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
1311 return WIMLIB_ERR_FUSERMOUNT;
1315 ERROR("fusermount exited with status %d", status);
1316 return WIMLIB_ERR_FUSERMOUNT;
1319 /* Open message queues between the unmount process and the
1320 * filesystem daemon. */
1321 ret = open_message_queues(false);
1325 /* Send a message to the filesystem saying whether to commit or
1327 msg[0] = (flags & WIMLIB_UNMOUNT_FLAG_COMMIT) ? 1 : 0;
1328 msg[1] = (flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY) ? 1 : 0;
1330 DEBUG("Sending message: %s, %s",
1331 (msg[0] == 0) ? "don't commit" : "commit",
1332 (msg[1] == 0) ? "don't check" : "check");
1333 ret = mq_send(unmount_to_daemon_mq, msg, 2, 1);
1335 ERROR("Failed to notify filesystem daemon whether we want to "
1336 "commit changes or not");
1337 close_message_queues();
1338 return WIMLIB_ERR_MQUEUE;
1341 /* Wait for a message from the filesytem daemon indicating whether the
1342 * filesystem was unmounted successfully (0) or an error occurred (1).
1343 * This may take a long time if a big WIM file needs to be rewritten. */
1345 /* Wait at most 600??? seconds before giving up and returning false.
1346 * Either it's a really big WIM file, or (more likely) the
1347 * filesystem daemon has crashed or failed for some reason.
1349 * XXX come up with some method to determine if the filesystem
1350 * daemon has really crashed or not. */
1352 gettimeofday(&now, NULL);
1353 timeout.tv_sec = now.tv_sec + 600;
1354 timeout.tv_nsec = now.tv_usec * 1000;
1356 msgsize = mq_get_msgsize(daemon_to_unmount_mq);
1357 char mailbox[msgsize];
1360 DEBUG("Waiting for message telling us whether the unmount was "
1361 "successful or not.");
1362 ret = mq_timedreceive(daemon_to_unmount_mq, mailbox, msgsize,
1365 close_message_queues();
1367 if (errno_save == ETIMEDOUT) {
1368 ERROR("Timed out- probably the filesystem daemon "
1369 "crashed and the WIM was not written "
1371 return WIMLIB_ERR_TIMEOUT;
1373 ERROR("mq_receive(): %s", strerror(errno_save));
1374 return WIMLIB_ERR_MQUEUE;
1378 DEBUG("Received message: %s",
1379 (mailbox[0] == 0) ? "Unmount OK" : "Unmount Failed");
1380 if (mailbox[0] != 0)
1381 ERROR("Unmount failed");
1385 #else /* WITH_FUSE */
1388 static inline int mount_unsupported_error()
1390 ERROR("WIMLIB was compiled with --without-fuse, which disables support "
1391 "for mounting WIMs.");
1392 return WIMLIB_ERR_UNSUPPORTED;
1395 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
1397 return mount_unsupported_error();
1400 WIMLIBAPI int wimlib_mount(WIMStruct *wim_p, int image, const char *dir,
1403 return mount_unsupported_error();
1406 #endif /* WITH_FUSE */