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
51 struct lookup_table_entry *lte;
54 /* The WIMStruct for the mounted WIM. */
57 /* Working directory when `imagex mount' is run. */
58 static const char *working_directory;
60 /* Name of the staging directory for a read-write mount. Whenever a new file is
61 * created, it is done so in the staging directory. Furthermore, whenever a
62 * file in the WIM is modified, it is extracted to the staging directory. If
63 * changes are commited when the WIM is unmounted, the file resources are merged
64 * in from the staging directory when writing the new WIM. */
65 static char *staging_dir_name;
66 static size_t staging_dir_name_len;
68 /* Flags passed to wimlib_mount(). */
69 static int mount_flags;
71 /* Name of the directory on which the WIM file is mounted. */
72 static const char *mount_dir;
75 static inline int get_lookup_flags()
77 if (mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
78 return LOOKUP_FLAG_ADS_OK;
83 static inline int flags_writable(int open_flags)
85 return open_flags & (O_RDWR | O_WRONLY);
88 static int alloc_fd(struct lookup_table_entry *lte, struct wimlib_fd **fd_ret)
90 struct wimlib_fd *fds, *fd;
91 if (lte->num_opened_fds == lte->num_allocated_fds) {
92 if (lte->num_allocated_fds > 0xffff - 8)
95 fds = CALLOC(lte->num_allocated_fds + 8, sizeof(struct wimlib_fd));
99 lte->num_allocated_fds * sizeof(struct wimlib_fd));
106 fd->hard_link_group = 0;
116 static int close_fd(struct wimlib_fd *fd)
118 struct lookup_table_entry *lte = fd->lte;
120 wimlib_assert(lte->num_opened_fds);
122 u16 idx = fd - lte->fds;
123 if (lte->staging_file_name && fd->staging_fd != -1)
124 if (close(fd->staging_fd) != 0)
126 if (--lte->num_opened_fds == 0 && lte->refcnt == 0)
127 free_lookup_table_entry(lte);
133 * Creates a randomly named staging directory and returns its name into the
134 * static variable staging_dir_name.
136 * If the staging directory cannot be created, staging_dir_name is set to NULL.
138 static void make_staging_dir()
140 /* XXX Give the user an option of where to stage files */
142 static char prefix[] = "wimlib-staging-";
143 static const size_t prefix_len = 15;
144 static const size_t suffix_len = 10;
146 size_t pwd_len = strlen(working_directory);
148 staging_dir_name_len = pwd_len + 1 + prefix_len + suffix_len;
150 staging_dir_name = MALLOC(staging_dir_name_len + 1);
151 if (!staging_dir_name) {
152 ERROR("Out of memory");
156 memcpy(staging_dir_name, working_directory, pwd_len);
157 staging_dir_name[pwd_len] = '/';
158 memcpy(staging_dir_name + pwd_len + 1, prefix, prefix_len);
159 randomize_char_array_with_alnum(staging_dir_name + pwd_len + 1 + prefix_len,
161 staging_dir_name[staging_dir_name_len] = '\0';
163 if (mkdir(staging_dir_name, 0700) != 0) {
164 ERROR_WITH_ERRNO("Failed to create temporary directory `%s'",
166 FREE(staging_dir_name);
167 staging_dir_name = NULL;
171 static int remove_file_or_directory(const char *fpath, const struct stat *sb,
172 int typeflag, struct FTW *ftwbuf)
174 if (remove(fpath) == 0)
177 return WIMLIB_ERR_DELETE_STAGING_DIR;
182 * Deletes the staging directory and all the files contained in it.
184 static inline int delete_staging_dir()
188 ret = nftw(staging_dir_name, remove_file_or_directory,10, FTW_DEPTH);
189 staging_dir_name = NULL;
193 /* Name and message queue descriptors for message queues between the filesystem
194 * daemon process and the unmount process. These are used when the filesystem
195 * is unmounted and the process running wimlib_mount() (i.e. the `imagex
196 * unmount' command) needs to communicate with the filesystem daemon running
197 * fuse_main() (i.e. that spawned by the `imagex mount' or `imagex mountrw'
199 static char *unmount_to_daemon_mq_name;
200 static char *daemon_to_unmount_mq_name;
201 static int unmount_to_daemon_mq;
202 static int daemon_to_unmount_mq;
204 /* Simple function that returns the concatenation of 4 strings. */
205 static char *strcat_dup(const char *s1, const char *s2, const char *s3,
208 size_t len = strlen(s1) + strlen(s2) + strlen(s3) + strlen(s4) + 1;
209 char *p = MALLOC(len);
220 /* Removes trailing forward slashes in a string. */
221 static void remove_trailing_slashes(char *s)
223 long len = strlen(s);
224 for (long i = len - 1; i >= 1; i--) {
232 /* Changes forward slashes to underscores in a string. */
233 static void s_slashes_underscores_g(char *s)
243 * Opens two POSIX message queue: one for sending messages from the unmount
244 * process to the daemon process, and one to go the other way. The names of the
245 * message queues, which must be system-wide unique, are be based on the mount
246 * point. (There of course is still a possibility of a collision if one were to
247 * unmount two identically named directories simultaneously...)
249 * @daemon specifies whether the calling process is the filesystem daemon or the
252 static int open_message_queues(bool daemon)
254 static const char *slash = "/";
255 static const char *prefix = "wimlib-";
256 static const char *u2d_suffix = "unmount-to-daemon-mq";
257 static const char *d2u_suffix = "daemon-to-unmount-mq";
259 const char *mount_dir_basename = path_basename(mount_dir);
263 unmount_to_daemon_mq_name = strcat_dup(slash, mount_dir_basename,
265 if (!unmount_to_daemon_mq_name) {
266 ERROR("Out of memory");
267 return WIMLIB_ERR_NOMEM;
269 daemon_to_unmount_mq_name = strcat_dup(slash, mount_dir_basename,
271 if (!daemon_to_unmount_mq_name) {
272 ERROR("Out of memory");
273 ret = WIMLIB_ERR_NOMEM;
277 remove_trailing_slashes(unmount_to_daemon_mq_name);
278 remove_trailing_slashes(daemon_to_unmount_mq_name);
279 s_slashes_underscores_g(unmount_to_daemon_mq_name + 1);
280 s_slashes_underscores_g(daemon_to_unmount_mq_name + 1);
283 flags = O_RDONLY | O_CREAT;
285 flags = O_WRONLY | O_CREAT;
287 unmount_to_daemon_mq = mq_open(unmount_to_daemon_mq_name, flags,
290 if (unmount_to_daemon_mq == -1) {
291 ERROR_WITH_ERRNO("mq_open()");
292 ret = WIMLIB_ERR_MQUEUE;
297 flags = O_WRONLY | O_CREAT;
299 flags = O_RDONLY | O_CREAT;
301 daemon_to_unmount_mq = mq_open(daemon_to_unmount_mq_name, flags,
304 if (daemon_to_unmount_mq == -1) {
305 ERROR_WITH_ERRNO("mq_open()");
306 ret = WIMLIB_ERR_MQUEUE;
311 mq_close(unmount_to_daemon_mq);
312 mq_unlink(unmount_to_daemon_mq_name);
314 FREE(daemon_to_unmount_mq_name);
316 FREE(unmount_to_daemon_mq_name);
320 static int mq_get_msgsize(mqd_t mq)
322 static const char *msgsize_max_file = "/proc/sys/fs/mqueue/msgsize_max";
327 if (mq_getattr(unmount_to_daemon_mq, &attr) == 0) {
328 msgsize = attr.mq_msgsize;
330 ERROR_WITH_ERRNO("mq_getattr()");
331 ERROR("Attempting to read %s", msgsize_max_file);
332 fp = fopen(msgsize_max_file, "rb");
334 if (fscanf(fp, "%d", &msgsize) != 1) {
335 ERROR("Assuming message size of 8192");
340 ERROR_WITH_ERRNO("Failed to open the file `%s'",
342 ERROR("Assuming message size of 8192");
349 /* Closes the message queues, which are allocated in static variables */
350 static void close_message_queues()
352 mq_close(unmount_to_daemon_mq);
353 mq_close(daemon_to_unmount_mq);
354 mq_unlink(unmount_to_daemon_mq_name);
355 mq_unlink(daemon_to_unmount_mq_name);
358 static int wimfs_access(const char *path, int mask)
360 /* XXX Permissions not implemented */
364 /* Closes the staging file descriptor associated with the lookup table entry, if
366 static int close_lte_fds(struct lookup_table_entry *lte, void *ignore)
368 for (u16 i = 0; i < lte->num_opened_fds; i++) {
369 if (lte->fds[i].lte && lte->fds[i].staging_fd != -1) {
370 if (close(lte->fds[i].staging_fd) != 0) {
371 ERROR_WITH_ERRNO("Failed close file `%s'",
372 lte->staging_file_name);
373 return WIMLIB_ERR_WRITE;
381 /* Calculates the SHA1 sum for @dentry if its file resource is in a staging
382 * file. Updates the SHA1 sum in the dentry and the lookup table entry. If
383 * there is already a lookup table entry with the same checksum, increment its
384 * reference count and destroy the lookup entry with the updated checksum. */
385 static int calculate_sha1sum_for_staging_file(struct dentry *dentry,
388 struct lookup_table *lookup_table = _lookup_table;
389 u8 *hash = dentry->hash;
392 struct lookup_table_entry *lte = __lookup_resource(lookup_table, hash);
393 if (lte && lte->staging_file_name) {
394 struct lookup_table_entry *existing;
397 DEBUG("Calculating SHA1 hash for file `%s'",
398 dentry->file_name_utf8);
399 ret = sha1sum(lte->staging_file_name, lte->hash);
403 lookup_table_unlink(lookup_table, lte);
404 memcpy(hash, lte->hash, WIM_HASH_SIZE);
405 existing = __lookup_resource(lookup_table, hash);
407 DEBUG("Merging duplicate lookup table entries for file "
408 "`%s'", dentry->file_name_utf8);
409 free_lookup_table_entry(lte);
412 lookup_table_insert(lookup_table, lte);
415 if (i == dentry->num_ads)
417 hash = dentry->ads_entries[i].hash;
423 /* Overwrites the WIM file, with changes saved. */
424 static int rebuild_wim(WIMStruct *w, bool check_integrity)
429 root = wim_root_dentry(w);
431 DEBUG("Closing all staging file descriptors.");
432 /* Close all the staging file descriptors. */
433 ret = for_lookup_table_entry(w->lookup_table, close_lte_fds, NULL);
435 ERROR("Failed to close all staging files");
439 DEBUG("Calculating SHA1 checksums for all new staging files.");
440 /* Calculate SHA1 checksums for all staging files, and merge unnecessary
441 * lookup table entries. */
442 ret = for_dentry_in_tree(root, calculate_sha1sum_for_staging_file,
445 ERROR("Failed to calculate new SHA1 checksums");
449 xml_update_image_info(w, w->current_image);
451 ret = wimlib_overwrite(w, check_integrity);
453 ERROR("Failed to commit changes");
459 /* Called when the filesystem is unmounted. */
460 static void wimfs_destroy(void *p)
463 /* For read-write mounts, the `imagex unmount' command, which is
464 * running in a separate process and is executing the
465 * wimlib_unmount() function, will send this process a byte
466 * through a message queue that indicates whether the --commit
467 * option was specified or not. */
470 struct timespec timeout;
472 ssize_t bytes_received;
475 char check_integrity;
478 ret = open_message_queues(true);
482 msgsize = mq_get_msgsize(unmount_to_daemon_mq);
487 /* Wait at most 3 seconds before giving up and discarding changes. */
488 gettimeofday(&now, NULL);
489 timeout.tv_sec = now.tv_sec + 3;
490 timeout.tv_nsec = now.tv_usec * 1000;
491 DEBUG("Waiting for message telling us whether to commit or not, and "
492 "whether to include integrity checks.");
494 bytes_received = mq_timedreceive(unmount_to_daemon_mq, msg,
495 msgsize, NULL, &timeout);
497 check_integrity = msg[1];
498 if (bytes_received == -1) {
499 if (errno == ETIMEDOUT) {
502 ERROR_WITH_ERRNO("mq_timedreceive()");
504 ERROR("Not committing.");
506 DEBUG("Received message: [%d %d]", msg[0], msg[1]);
510 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
512 status = chdir(working_directory);
514 ERROR_WITH_ERRNO("chdir()");
515 status = WIMLIB_ERR_NOTDIR;
518 status = rebuild_wim(w, (check_integrity != 0));
520 ret = delete_staging_dir();
522 ERROR_WITH_ERRNO("Failed to delete the staging "
529 ret = mq_send(daemon_to_unmount_mq, &status, 1, 1);
531 ERROR_WITH_ERRNO("Failed to send status to unmount process");
532 close_message_queues();
536 * Fills in a `struct stat' that corresponds to a file or directory in the WIM.
538 static int wimfs_getattr(const char *path, struct stat *stbuf)
540 struct dentry *dentry = get_dentry(w, path);
543 dentry_to_stbuf(dentry, stbuf, w->lookup_table);
548 * Create a directory in the WIM.
549 * @mode is currently ignored.
551 static int wimfs_mkdir(const char *path, mode_t mode)
553 struct dentry *parent;
554 struct dentry *newdir;
555 const char *basename;
557 parent = get_parent_dentry(w, path);
561 if (!dentry_is_directory(parent))
564 basename = path_basename(path);
565 if (get_dentry_child_with_name(parent, basename))
568 newdir = new_dentry(basename);
569 newdir->attributes |= FILE_ATTRIBUTE_DIRECTORY;
570 link_dentry(newdir, parent);
574 /* Creates a new staging file and returns its file descriptor opened for
577 * @name_ret: A location into which the a pointer to the newly allocated name of
578 * the staging file is stored.
579 * @return: The file descriptor for the new file. Returns -1 and sets errno on
580 * error, for any reason possible from the creat() function.
582 static int create_staging_file(char **name_ret, int open_flags)
590 name_len = staging_dir_name_len + 1 + WIM_HASH_SIZE;
591 name = MALLOC(name_len + 1);
599 memcpy(name, staging_dir_name, staging_dir_name_len);
600 name[staging_dir_name_len] = '/';
601 randomize_char_array_with_alnum(name + staging_dir_name_len + 1,
603 name[name_len] = '\0';
606 /* Just in case, verify that the randomly generated name doesn't name an
607 * existing file, and try again if so */
608 } while (stat(name, &stbuf) == 0);
614 /* doesn't exist--- ok */
616 DEBUG("Creating staging file `%s'", name);
618 fd = open(name, open_flags | O_CREAT | O_TRUNC, 0600);
629 /* Creates a regular file. */
630 static int wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
632 const char *stream_name;
633 if ((mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
634 && (stream_name = path_stream_name(path))) {
635 struct ads_entry *new_entry;
636 struct dentry *dentry;
638 dentry = get_dentry(w, path);
639 if (!dentry || !dentry_is_regular_file(dentry))
641 if (dentry_get_ads_entry(dentry, stream_name))
643 new_entry = dentry_add_ads(dentry, stream_name);
647 struct dentry *dentry, *parent;
648 const char *basename;
650 /* Make sure that the parent of @path exists and is a directory, and
651 * that the dentry named by @path does not already exist. */
652 parent = get_parent_dentry(w, path);
655 if (!dentry_is_directory(parent))
657 basename = path_basename(path);
658 if (get_dentry_child_with_name(parent, path))
661 dentry = new_dentry(basename);
662 link_dentry(dentry, parent);
669 static int wimfs_open(const char *path, struct fuse_file_info *fi)
671 struct dentry *dentry;
672 struct lookup_table_entry *lte;
675 struct wimlib_fd *fd;
677 ret = lookup_resource(w, path, get_lookup_flags(), &dentry, <e,
683 /* Common case--- there's a lookup table entry for a file.
684 * Allocate a new file descriptor for it. */
686 ret = alloc_fd(lte, &fd);
690 /* The file resource may be in the staging directory (read-write
691 * mounts only) or in the WIM. If it's in the staging
692 * directory, we need to open a native file descriptor for the
693 * corresponding file. Otherwise, we can read the file resource
694 * directly from the WIM file if we are opening it read-only,
695 * but we need to extract the resource to the staging directory
696 * if we are opening it writable. */
697 if (lte->staging_file_name) {
698 fd->staging_fd = open(lte->staging_file_name, fi->flags);
699 if (fd->staging_fd == -1) {
703 } else if (flags_writable(fi->flags)) {
705 ret = extract_resource_to_staging_dir(lte,
706 lte->resource_entry.original_size);
709 /* Empty file with no lookup-table entry. This is fine if it's
710 * a read-only filesystem. Otherwise we need to create a lookup
711 * table entry so that we can keep track of the file descriptors
712 * (this is important in case someone opens the file for
714 if (!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
721 fd = create_staging_file(&tmpfile_name, O_RDWR);
725 lte = new_lookup_table_entry();
729 randomize_byte_array(lte->hash, WIM_HASH_SIZE);
730 memcpy(dentry_hash, lte->hash, WIM_HASH_SIZE);
731 lte->staging_file_name = tmpfile_name;
732 lte->staging_fd = fd;
733 lookup_table_insert(w->lookup_table, lte);
735 fi->fd = (uint64_t)fd;
739 /* Opens a directory. */
740 static int wimfs_opendir(const char *path, struct fuse_file_info *fi)
742 struct dentry *dentry;
744 dentry = get_dentry(w, path);
745 if (!dentry || !dentry_is_directory(dentry))
747 fi->fd = (uint64_t)dentry;
753 * Read data from a file in the WIM or in the staging directory.
755 static int wimfs_read(const char *path, char *buf, size_t size,
756 off_t offset, struct fuse_file_info *fi)
758 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
760 wimlib_assert(fd->lte);
761 wimlib_assert(fd->lte->staging_dir_name);
763 if (fd->lte->staging_file_name) {
764 /* Read from staging file */
766 wimlib_assert(fd->staging_fd != -1);
770 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
772 ret = read(fd->staging_fd, buf, size);
780 struct resource_entry *res_entry;
783 res_entry = <e->resource_entry;
785 ctype = wim_resource_compression_type(w, res_entry);
787 if (offset > res_entry->original_size)
790 size = min(size, res_entry->original_size - offset);
792 if (read_resource(w->fp, res_entry->size,
793 res_entry->original_size,
794 res_entry->offset, ctype, size,
801 /* Fills in the entries of the directory specified by @path using the
802 * FUSE-provided function @filler. */
803 static int wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
804 off_t offset, struct fuse_file_info *fi)
806 struct dentry *parent = (struct dentry*) fi->fh;
807 filler(buf, ".", NULL, 0);
808 filler(buf, "..", NULL, 0);
810 child = parent->children;
816 if (filler(buf, child->file_name_utf8, NULL, 0))
819 } while (child != parent->children);
824 static int wimfs_readlink(const char *path, char *buf, size_t buf_len)
826 struct dentry *dentry = get_dentry(w, path);
830 if (!dentry_is_symlink(dentry))
833 ret = dentry_readlink(dentry, buf, buf_len, w);
840 static int wimfs_release(const char *path, struct fuse_file_info *fi)
843 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
845 wimlib_assert(fd->lte);
846 wimlib_assert(fd->num_opened_fds);
850 /* Renames a file or directory. See rename (3) */
851 static int wimfs_rename(const char *from, const char *to)
855 struct dentry *parent_of_dst;
857 src = get_dentry(w, from);
861 dst = get_dentry(w, to);
864 if (!dentry_is_directory(src)) {
865 /* Cannot rename non-directory to directory. */
866 if (dentry_is_directory(dst))
869 /* Cannot rename directory to a non-directory or a non-empty
871 if (!dentry_is_directory(dst))
873 if (dst->children != NULL)
876 parent_of_dst = dst->parent;
878 lookup_table_decrement_refcnt(w->lookup_table, dst->hash);
881 parent_of_dst = get_parent_dentry(w, to);
887 change_dentry_name(src, path_basename(to));
888 link_dentry(src, parent_of_dst);
889 /*calculate_dentry_full_path(src);*/
893 /* Remove a directory */
894 static int wimfs_rmdir(const char *path)
896 struct dentry *dentry;
898 dentry = get_dentry(w, path);
902 if (!dentry_is_empty_directory(dentry))
905 unlink_dentry(dentry);
911 * Extract a WIM resource to the staging directory.
912 * Only @size bytes are extracted, to support truncating the file.
915 * - Create a staging file for the WIM resource
916 * - Extract the resource to it
917 * - Create a new lte for the file resource
918 * - Transfer fds from the old lte to the new lte, but
919 * only if they share the same hard link group as this
922 static int extract_resource_to_staging_dir(struct lookup_table_entry *lte,
925 char *staging_file_name;
928 struct lookup_table_entry *new_lte;
930 fd = create_staging_file(&staging_file_name);
934 ret = extract_resource_to_fd(w, <e->resource_entry, fd, size);
940 unlink(staging_file_name);
941 FREE(staging_file_name);
946 * Need to figure out how to avoid creating orphan lookup table entries.
949 if (lte->refcnt == 1) {
952 /* Need to make a new lookup table entry if we are
953 * changing only one copy of a hardlinked entry */
956 new_lte = new_lookup_table_entry();
959 randomize_byte_array(dentry_hash, WIM_HASH_SIZE);
960 memcpy(new_lte->hash, dentry_hash, WIM_HASH_SIZE);
962 new_lte->resource_entry.flags = 0;
963 new_lte->staging_num_times_opened = lte->staging_num_times_opened;
965 lookup_table_insert(w->lookup_table, new_lte);
970 lte->resource_entry.original_size = size;
971 lte->staging_file_name = staging_file_name;
973 if (lte->staging_num_times_opened == 0)
976 lte->staging_fd = fd;
980 /* Reduce the size of a file */
981 static int wimfs_truncate(const char *path, off_t size)
983 struct dentry *dentry;
984 struct lookup_table_entry *lte;
988 ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
994 if (!lte) /* Already a zero-length file */
997 if (lte->staging_file_name) {
998 /* File on disk. Call POSIX API */
999 if (lte->staging_num_times_opened != 0)
1000 ret = ftruncate(lte->staging_fd, size);
1002 ret = truncate(lte->staging_file_name, size);
1005 dentry_update_all_timestamps(dentry);
1006 lte->resource_entry.original_size = size;
1008 /* File in WIM. Extract it to the staging directory, but only
1009 * the first @size bytes of it. */
1010 ret = extract_resource_to_staging_dir(dentry_hash, lte, size);
1015 /* Remove a regular file */
1016 static int wimfs_unlink(const char *path)
1018 struct dentry *dentry;
1019 struct lookup_table_entry *lte;
1023 ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1024 <e, &dentry_hash);
1029 if (lte && lte->staging_file_name)
1030 if (unlink(lte->staging_file_name) != 0)
1033 if (dentry_hash == dentry->hash) {
1034 /* We are removing the full dentry including all alternate data
1036 const u8 *hash = dentry->hash;
1039 lookup_table_decrement_refcnt(w->lookup_table, hash);
1040 if (i == dentry->num_ads)
1042 hash = dentry->ads_entries[i].hash;
1046 unlink_dentry(dentry);
1047 free_dentry(dentry);
1049 /* We are removing an alternate data stream. */
1050 struct ads_entry *cur_entry = dentry->ads_entries;
1051 while (cur_entry->hash != dentry_hash)
1053 lookup_table_decrement_refcnt(w->lookup_table, cur_entry->hash);
1055 dentry_remove_ads(dentry, cur_entry);
1057 /* Beware: The lookup table entry(s) may still be referenced by users
1058 * that have opened the corresponding streams. They are freed later in
1059 * wimfs_release() when the last file user has closed the stream. */
1063 /* Change the timestamp on a file dentry.
1065 * There is no distinction between a file and its alternate data streams here. */
1066 static int wimfs_utimens(const char *path, const struct timespec tv[2])
1068 struct dentry *dentry = get_dentry(w, path);
1071 time_t last_access_t = (tv[0].tv_nsec == UTIME_NOW) ?
1072 time(NULL) : tv[0].tv_sec;
1073 dentry->last_access_time = unix_timestamp_to_ms(last_access_t);
1074 time_t last_mod_t = (tv[1].tv_nsec == UTIME_NOW) ?
1075 time(NULL) : tv[1].tv_sec;
1076 dentry->last_write_time = unix_timestamp_to_ms(last_mod_t);
1080 /* Writes to a file in the WIM filesystem.
1081 * It may be an alternate data stream, but here we don't even notice because we
1082 * just get a lookup table entry. */
1083 static int wimfs_write(const char *path, const char *buf, size_t size,
1084 off_t offset, struct fuse_file_info *fi)
1086 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
1089 wimlib_assert(fd->lte);
1090 wimlib_assert(fd->lte->staging_dir_name);
1091 wimlib_assert(fd->staging_fd != -1);
1093 /* Seek to correct position in file if needed. */
1094 if (lte->staging_offset != offset) {
1095 if (lseek(lte->staging_fd, offset, SEEK_SET) == -1)
1097 lte->staging_offset = offset;
1100 /* Write the data. */
1101 ret = write(lte->staging_fd, buf, size);
1105 /* Adjust the stored offset of staging_fd. */
1106 lte->staging_offset = offset + ret;
1108 /* Increase file size if needed. */
1109 if (lte->resource_entry.original_size < lte->staging_offset)
1110 lte->resource_entry.original_size = lte->staging_offset;
1112 /* The file has been modified, so all its timestamps must be
1114 dentry_update_all_timestamps(dentry);
1120 static struct fuse_operations wimfs_oper = {
1121 .access = wimfs_access,
1122 .destroy = wimfs_destroy,
1123 .getattr = wimfs_getattr,
1124 .mkdir = wimfs_mkdir,
1125 .mknod = wimfs_mknod,
1127 .opendir = wimfs_opendir,
1129 .readdir = wimfs_readdir,
1130 .readlink = wimfs_readlink,
1131 .release = wimfs_release,
1132 .rename = wimfs_rename,
1133 .rmdir = wimfs_rmdir,
1134 .truncate = wimfs_truncate,
1135 .unlink = wimfs_unlink,
1136 .utimens = wimfs_utimens,
1137 .write = wimfs_write,
1141 /* Mounts a WIM file. */
1142 WIMLIBAPI int wimlib_mount(WIMStruct *wim, int image, const char *dir,
1150 DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
1151 wim, image, dir, flags);
1154 return WIMLIB_ERR_INVALID_PARAM;
1156 ret = wimlib_select_image(wim, image);
1161 if (flags & WIMLIB_MOUNT_FLAG_READWRITE)
1162 wim_get_current_image_metadata(wim)->modified = true;
1164 if (!(flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
1165 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
1166 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
1167 flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1170 working_directory = getcwd(NULL, 0);
1171 if (!working_directory) {
1172 ERROR_WITH_ERRNO("Could not determine current directory");
1173 return WIMLIB_ERR_NOTDIR;
1178 return WIMLIB_ERR_NOMEM;
1180 argv[argc++] = "mount";
1182 argv[argc++] = "-s"; /* disable multi-threaded operation */
1184 if (flags & WIMLIB_MOUNT_FLAG_DEBUG) {
1185 argv[argc++] = "-d";
1187 if (!(flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
1188 argv[argc++] = "-o";
1189 argv[argc++] = "ro";
1192 if (!staging_dir_name) {
1194 return WIMLIB_ERR_MKDIR;
1201 DEBUG("FUSE command line (argc = %d): ", argc);
1202 for (i = 0; i < argc; i++) {
1203 fputs(argv[i], stdout);
1211 /* Set static variables. */
1213 mount_flags = flags;
1215 ret = fuse_main(argc, argv, &wimfs_oper, NULL);
1217 return (ret == 0) ? 0 : WIMLIB_ERR_FUSE;
1222 * Unmounts the WIM file that was previously mounted on @dir by using
1225 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
1232 struct timespec timeout;
1236 /* Execute `fusermount -u', which is installed setuid root, to unmount
1239 * FUSE does not yet implement synchronous unmounts. This means that
1240 * fusermount -u will return before the filesystem daemon returns from
1241 * wimfs_destroy(). This is partly what we want, because we need to
1242 * send a message from this process to the filesystem daemon telling
1243 * whether --commit was specified or not. However, after that, the
1244 * unmount process must wait for the filesystem daemon to finish writing
1251 ERROR_WITH_ERRNO("Failed to fork()");
1252 return WIMLIB_ERR_FORK;
1255 execlp("fusermount", "fusermount", "-u", dir, NULL);
1256 ERROR_WITH_ERRNO("Failed to execute `fusermount'");
1257 return WIMLIB_ERR_FUSERMOUNT;
1260 ret = waitpid(pid, &status, 0);
1262 ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
1264 return WIMLIB_ERR_FUSERMOUNT;
1268 ERROR("fusermount exited with status %d", status);
1269 return WIMLIB_ERR_FUSERMOUNT;
1272 /* Open message queues between the unmount process and the
1273 * filesystem daemon. */
1274 ret = open_message_queues(false);
1278 /* Send a message to the filesystem saying whether to commit or
1280 msg[0] = (flags & WIMLIB_UNMOUNT_FLAG_COMMIT) ? 1 : 0;
1281 msg[1] = (flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY) ? 1 : 0;
1283 DEBUG("Sending message: %s, %s",
1284 (msg[0] == 0) ? "don't commit" : "commit",
1285 (msg[1] == 0) ? "don't check" : "check");
1286 ret = mq_send(unmount_to_daemon_mq, msg, 2, 1);
1288 ERROR("Failed to notify filesystem daemon whether we want to "
1289 "commit changes or not");
1290 close_message_queues();
1291 return WIMLIB_ERR_MQUEUE;
1294 /* Wait for a message from the filesytem daemon indicating whether the
1295 * filesystem was unmounted successfully (0) or an error occurred (1).
1296 * This may take a long time if a big WIM file needs to be rewritten. */
1298 /* Wait at most 600??? seconds before giving up and returning false.
1299 * Either it's a really big WIM file, or (more likely) the
1300 * filesystem daemon has crashed or failed for some reason.
1302 * XXX come up with some method to determine if the filesystem
1303 * daemon has really crashed or not. */
1305 gettimeofday(&now, NULL);
1306 timeout.tv_sec = now.tv_sec + 600;
1307 timeout.tv_nsec = now.tv_usec * 1000;
1309 msgsize = mq_get_msgsize(daemon_to_unmount_mq);
1310 char mailbox[msgsize];
1313 DEBUG("Waiting for message telling us whether the unmount was "
1314 "successful or not.");
1315 ret = mq_timedreceive(daemon_to_unmount_mq, mailbox, msgsize,
1318 close_message_queues();
1320 if (errno_save == ETIMEDOUT) {
1321 ERROR("Timed out- probably the filesystem daemon "
1322 "crashed and the WIM was not written "
1324 return WIMLIB_ERR_TIMEOUT;
1326 ERROR("mq_receive(): %s", strerror(errno_save));
1327 return WIMLIB_ERR_MQUEUE;
1331 DEBUG("Received message: %s",
1332 (mailbox[0] == 0) ? "Unmount OK" : "Unmount Failed");
1333 if (mailbox[0] != 0)
1334 ERROR("Unmount failed");
1338 #else /* WITH_FUSE */
1341 static inline int mount_unsupported_error()
1343 ERROR("WIMLIB was compiled with --without-fuse, which disables support "
1344 "for mounting WIMs.");
1345 return WIMLIB_ERR_UNSUPPORTED;
1348 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
1350 return mount_unsupported_error();
1353 WIMLIBAPI int wimlib_mount(WIMStruct *wim_p, int image, const char *dir,
1356 return mount_unsupported_error();
1359 #endif /* WITH_FUSE */