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
48 /* The WIMStruct for the mounted WIM. */
51 /* Working directory when `imagex mount' is run. */
52 static const char *working_directory;
54 /* Name of the staging directory for a read-write mount. Whenever a new file is
55 * created, it is done so in the staging directory. Furthermore, whenever a
56 * file in the WIM is modified, it is extracted to the staging directory. If
57 * changes are commited when the WIM is unmounted, the file resources are merged
58 * in from the staging directory when writing the new WIM. */
59 static char *staging_dir_name;
60 static size_t staging_dir_name_len;
62 /* Flags passed to wimlib_mount(). */
63 static int mount_flags;
65 /* Name of the directory on which the WIM file is mounted. */
66 static const char *mount_dir;
69 static inline int get_lookup_flags()
71 if (mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
72 return LOOKUP_FLAG_ADS_OK;
78 * Creates a randomly named staging directory and returns its name into the
79 * static variable staging_dir_name.
81 * If the staging directory cannot be created, staging_dir_name is set to NULL.
83 static void make_staging_dir()
85 /* XXX Give the user an option of where to stage files */
87 static char prefix[] = "wimlib-staging-";
88 static const size_t prefix_len = 15;
89 static const size_t suffix_len = 10;
91 size_t pwd_len = strlen(working_directory);
93 staging_dir_name_len = pwd_len + 1 + prefix_len + suffix_len;
95 staging_dir_name = MALLOC(staging_dir_name_len + 1);
96 if (!staging_dir_name) {
97 ERROR("Out of memory");
101 memcpy(staging_dir_name, working_directory, pwd_len);
102 staging_dir_name[pwd_len] = '/';
103 memcpy(staging_dir_name + pwd_len + 1, prefix, prefix_len);
104 randomize_char_array_with_alnum(staging_dir_name + pwd_len + 1 + prefix_len,
106 staging_dir_name[staging_dir_name_len] = '\0';
108 if (mkdir(staging_dir_name, 0700) != 0) {
109 ERROR_WITH_ERRNO("Failed to create temporary directory `%s'",
111 FREE(staging_dir_name);
112 staging_dir_name = NULL;
116 static int remove_file_or_directory(const char *fpath, const struct stat *sb,
117 int typeflag, struct FTW *ftwbuf)
119 if (remove(fpath) == 0)
122 return WIMLIB_ERR_DELETE_STAGING_DIR;
127 * Deletes the staging directory and all the files contained in it.
129 static inline int delete_staging_dir()
133 ret = nftw(staging_dir_name, remove_file_or_directory,10, FTW_DEPTH);
134 staging_dir_name = NULL;
138 /* Name and message queue descriptors for message queues between the filesystem
139 * daemon process and the unmount process. These are used when the filesystem
140 * is unmounted and the process running wimlib_mount() (i.e. the `imagex
141 * unmount' command) needs to communicate with the filesystem daemon running
142 * fuse_main() (i.e. that spawned by the `imagex mount' or `imagex mountrw'
144 static char *unmount_to_daemon_mq_name;
145 static char *daemon_to_unmount_mq_name;
146 static int unmount_to_daemon_mq;
147 static int daemon_to_unmount_mq;
149 /* Simple function that returns the concatenation of 4 strings. */
150 static char *strcat_dup(const char *s1, const char *s2, const char *s3,
153 size_t len = strlen(s1) + strlen(s2) + strlen(s3) + strlen(s4) + 1;
154 char *p = MALLOC(len);
165 /* Removes trailing forward slashes in a string. */
166 static void remove_trailing_slashes(char *s)
168 long len = strlen(s);
169 for (long i = len - 1; i >= 1; i--) {
177 /* Changes forward slashes to underscores in a string. */
178 static void s_slashes_underscores_g(char *s)
188 * Opens two POSIX message queue: one for sending messages from the unmount
189 * process to the daemon process, and one to go the other way. The names of the
190 * message queues, which must be system-wide unique, are be based on the mount
191 * point. (There of course is still a possibility of a collision if one were to
192 * unmount two identically named directories simultaneously...)
194 * @daemon specifies whether the calling process is the filesystem daemon or the
197 static int open_message_queues(bool daemon)
199 static const char *slash = "/";
200 static const char *prefix = "wimlib-";
201 static const char *u2d_suffix = "unmount-to-daemon-mq";
202 static const char *d2u_suffix = "daemon-to-unmount-mq";
204 const char *mount_dir_basename = path_basename(mount_dir);
208 unmount_to_daemon_mq_name = strcat_dup(slash, mount_dir_basename,
210 if (!unmount_to_daemon_mq_name) {
211 ERROR("Out of memory");
212 return WIMLIB_ERR_NOMEM;
214 daemon_to_unmount_mq_name = strcat_dup(slash, mount_dir_basename,
216 if (!daemon_to_unmount_mq_name) {
217 ERROR("Out of memory");
218 ret = WIMLIB_ERR_NOMEM;
222 remove_trailing_slashes(unmount_to_daemon_mq_name);
223 remove_trailing_slashes(daemon_to_unmount_mq_name);
224 s_slashes_underscores_g(unmount_to_daemon_mq_name + 1);
225 s_slashes_underscores_g(daemon_to_unmount_mq_name + 1);
228 flags = O_RDONLY | O_CREAT;
230 flags = O_WRONLY | O_CREAT;
232 unmount_to_daemon_mq = mq_open(unmount_to_daemon_mq_name, flags,
235 if (unmount_to_daemon_mq == -1) {
236 ERROR_WITH_ERRNO("mq_open()");
237 ret = WIMLIB_ERR_MQUEUE;
242 flags = O_WRONLY | O_CREAT;
244 flags = O_RDONLY | O_CREAT;
246 daemon_to_unmount_mq = mq_open(daemon_to_unmount_mq_name, flags,
249 if (daemon_to_unmount_mq == -1) {
250 ERROR_WITH_ERRNO("mq_open()");
251 ret = WIMLIB_ERR_MQUEUE;
256 mq_close(unmount_to_daemon_mq);
257 mq_unlink(unmount_to_daemon_mq_name);
259 FREE(daemon_to_unmount_mq_name);
261 FREE(unmount_to_daemon_mq_name);
265 static int mq_get_msgsize(mqd_t mq)
267 static const char *msgsize_max_file = "/proc/sys/fs/mqueue/msgsize_max";
272 if (mq_getattr(unmount_to_daemon_mq, &attr) == 0) {
273 msgsize = attr.mq_msgsize;
275 ERROR_WITH_ERRNO("mq_getattr()");
276 ERROR("Attempting to read %s", msgsize_max_file);
277 fp = fopen(msgsize_max_file, "rb");
279 if (fscanf(fp, "%d", &msgsize) != 1) {
280 ERROR("Assuming message size of 8192");
285 ERROR_WITH_ERRNO("Failed to open the file `%s'",
287 ERROR("Assuming message size of 8192");
294 /* Closes the message queues, which are allocated in static variables */
295 static void close_message_queues()
297 mq_close(unmount_to_daemon_mq);
298 mq_close(daemon_to_unmount_mq);
299 mq_unlink(unmount_to_daemon_mq_name);
300 mq_unlink(daemon_to_unmount_mq_name);
303 static int wimfs_access(const char *path, int mask)
305 /* XXX Permissions not implemented */
309 /* Closes the staging file descriptor associated with the lookup table entry, if
311 static int close_staging_file(struct lookup_table_entry *lte, void *ignore)
313 if (lte->staging_file_name && lte->staging_num_times_opened) {
314 if (close(lte->staging_fd) != 0) {
315 ERROR_WITH_ERRNO("Failed close file `%s'",
316 lte->staging_file_name);
317 return WIMLIB_ERR_WRITE;
324 /* Calculates the SHA1 sum for @dentry if its file resource is in a staging
325 * file. Updates the SHA1 sum in the dentry and the lookup table entry. If
326 * there is already a lookup table entry with the same checksum, increment its
327 * reference count and destroy the lookup entry with the updated checksum. */
328 static int calculate_sha1sum_for_staging_file(struct dentry *dentry,
331 struct lookup_table *lookup_table = _lookup_table;
332 u8 *hash = dentry->hash;
335 struct lookup_table_entry *lte = __lookup_resource(lookup_table, hash);
336 if (lte && lte->staging_file_name) {
337 struct lookup_table_entry *existing;
340 DEBUG("Calculating SHA1 hash for file `%s'",
341 dentry->file_name_utf8);
342 ret = sha1sum(lte->staging_file_name, lte->hash);
346 lookup_table_unlink(lookup_table, lte);
347 memcpy(hash, lte->hash, WIM_HASH_SIZE);
348 existing = __lookup_resource(lookup_table, hash);
350 DEBUG("Merging duplicate lookup table entries for file "
351 "`%s'", dentry->file_name_utf8);
352 free_lookup_table_entry(lte);
355 lookup_table_insert(lookup_table, lte);
358 if (i == dentry->num_ads)
360 hash = dentry->ads_entries[i].hash;
366 /* Overwrites the WIM file, with changes saved. */
367 static int rebuild_wim(WIMStruct *w, bool check_integrity)
372 root = wim_root_dentry(w);
374 DEBUG("Closing all staging file descriptors.");
375 /* Close all the staging file descriptors. */
376 ret = for_lookup_table_entry(w->lookup_table, close_staging_file, NULL);
378 ERROR("Failed to close all staging files");
382 DEBUG("Calculating SHA1 checksums for all new staging files.");
383 /* Calculate SHA1 checksums for all staging files, and merge unnecessary
384 * lookup table entries. */
385 ret = for_dentry_in_tree(root, calculate_sha1sum_for_staging_file,
388 ERROR("Failed to calculate new SHA1 checksums");
392 xml_update_image_info(w, w->current_image);
394 ret = wimlib_overwrite(w, check_integrity);
396 ERROR("Failed to commit changes");
402 /* Called when the filesystem is unmounted. */
403 static void wimfs_destroy(void *p)
406 /* For read-write mounts, the `imagex unmount' command, which is
407 * running in a separate process and is executing the
408 * wimlib_unmount() function, will send this process a byte
409 * through a message queue that indicates whether the --commit
410 * option was specified or not. */
413 struct timespec timeout;
415 ssize_t bytes_received;
418 char check_integrity;
421 ret = open_message_queues(true);
425 msgsize = mq_get_msgsize(unmount_to_daemon_mq);
430 /* Wait at most 3 seconds before giving up and discarding changes. */
431 gettimeofday(&now, NULL);
432 timeout.tv_sec = now.tv_sec + 3;
433 timeout.tv_nsec = now.tv_usec * 1000;
434 DEBUG("Waiting for message telling us whether to commit or not, and "
435 "whether to include integrity checks.");
437 bytes_received = mq_timedreceive(unmount_to_daemon_mq, msg,
438 msgsize, NULL, &timeout);
440 check_integrity = msg[1];
441 if (bytes_received == -1) {
442 if (errno == ETIMEDOUT) {
445 ERROR_WITH_ERRNO("mq_timedreceive()");
447 ERROR("Not committing.");
449 DEBUG("Received message: [%d %d]", msg[0], msg[1]);
453 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
455 status = chdir(working_directory);
457 ERROR_WITH_ERRNO("chdir()");
458 status = WIMLIB_ERR_NOTDIR;
461 status = rebuild_wim(w, (check_integrity != 0));
463 ret = delete_staging_dir();
465 ERROR_WITH_ERRNO("Failed to delete the staging "
472 ret = mq_send(daemon_to_unmount_mq, &status, 1, 1);
474 ERROR_WITH_ERRNO("Failed to send status to unmount process");
475 close_message_queues();
479 * Fills in a `struct stat' that corresponds to a file or directory in the WIM.
481 static int wimfs_getattr(const char *path, struct stat *stbuf)
483 struct dentry *dentry = get_dentry(w, path);
486 dentry_to_stbuf(dentry, stbuf, w->lookup_table);
491 * Create a directory in the WIM.
492 * @mode is currently ignored.
494 static int wimfs_mkdir(const char *path, mode_t mode)
496 struct dentry *parent;
497 struct dentry *newdir;
498 const char *basename;
500 parent = get_parent_dentry(w, path);
504 if (!dentry_is_directory(parent))
507 basename = path_basename(path);
508 if (get_dentry_child_with_name(parent, basename))
511 newdir = new_dentry(basename);
512 newdir->attributes |= FILE_ATTRIBUTE_DIRECTORY;
513 link_dentry(newdir, parent);
517 /* Creates a new staging file and returns its file descriptor opened for
520 * @name_ret: A location into which the a pointer to the newly allocated name of
521 * the staging file is stored.
522 * @return: The file descriptor for the new file. Returns -1 and sets errno on
523 * error, for any reason possible from the creat() function.
525 static int create_staging_file(char **name_ret, int open_flags)
533 name_len = staging_dir_name_len + 1 + WIM_HASH_SIZE;
534 name = MALLOC(name_len + 1);
542 memcpy(name, staging_dir_name, staging_dir_name_len);
543 name[staging_dir_name_len] = '/';
544 randomize_char_array_with_alnum(name + staging_dir_name_len + 1,
546 name[name_len] = '\0';
549 /* Just in case, verify that the randomly generated name doesn't name an
550 * existing file, and try again if so */
551 } while (stat(name, &stbuf) == 0);
557 /* doesn't exist--- ok */
559 DEBUG("Creating staging file `%s'", name);
561 fd = open(name, mode | O_CREAT | O_TRUNC, 0600);
572 /* Creates a regular file. */
573 static int wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
575 const char *stream_name;
576 if ((mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
577 && (stream_name = path_stream_name(path))) {
578 struct ads_entry *new_entry;
579 struct dentry *dentry;
581 dentry = get_dentry(w, path);
582 if (!dentry || !dentry_is_regular_file(dentry))
584 if (dentry_get_ads_entry(dentry, stream_name))
586 new_entry = dentry_add_ads(dentry, stream_name);
590 struct dentry *dentry, *parent;
591 const char *basename;
593 /* Make sure that the parent of @path exists and is a directory, and
594 * that the dentry named by @path does not already exist. */
595 parent = get_parent_dentry(w, path);
598 if (!dentry_is_directory(parent))
600 basename = path_basename(path);
601 if (get_dentry_child_with_name(parent, path))
604 dentry = new_dentry(basename);
605 link_dentry(dentry, parent);
612 static int wimfs_open(const char *path, struct fuse_file_info *fi)
614 struct dentry *dentry;
615 struct lookup_table_entry *lte;
619 ret = lookup_resource(w, path, get_lookup_flags(), &dentry, <e,
625 /* If this file is in the staging directory and the file is not
626 * currently open, open it. */
627 if (lte->staging_file_name) {
628 if (lte->staging_num_times_opened == 0) {
629 lte->staging_fd = open(lte->staging_file_name, O_RDWR);
630 if (lte->staging_fd == -1)
632 lte->staging_offset = 0;
635 /* File in the WIM. We must extract it to the staging directory
636 * before it can be written to. */
637 ret = extract_resource_to_staging_dir(dentry_hash, lte,
638 lte->resource_entry.original_size);
643 /* Empty file with no lookup-table entry. This is fine if it's
644 * a read-only filesystem. Otherwise we need to move the file
645 * to the staging directory with a new lookup table entry, even
646 * if we aren't opening it for writing at the moment, so that we
647 * will have a lookup table entry for the file in case it's
649 if (!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
656 fd = create_staging_file(&tmpfile_name, O_RDWR);
660 lte = new_lookup_table_entry();
664 randomize_byte_array(lte->hash, WIM_HASH_SIZE);
665 memcpy(dentry_hash, lte->hash, WIM_HASH_SIZE);
666 lte->staging_file_name = tmpfile_name;
667 lte->staging_fd = fd;
668 lookup_table_insert(w->lookup_table, lte);
670 lte->staging_num_times_opened++;
671 fi->fd = (uint64_t)lte;
675 /* Opens a directory. */
676 static int wimfs_opendir(const char *path, struct fuse_file_info *fi)
678 struct dentry *dentry;
680 dentry = get_dentry(w, path);
681 if (!dentry || !dentry_is_directory(dentry))
683 fi->fd = (uint64_t)dentry;
689 * Read data from a file in the WIM or in the staging directory.
691 static int wimfs_read(const char *path, char *buf, size_t size,
692 off_t offset, struct fuse_file_info *fi)
694 struct lookup_table_entry *lte;
696 lte = (struct lookup_table_entry*)fi->fh;
700 if (lte->staging_file_name) {
702 /* Read from staging */
707 if (lte->staging_num_times_opened == 0)
710 fd = lte->staging_fd;
711 cur_offset = lte->staging_offset;
712 if (cur_offset != offset)
713 if (lseek(fd, offset, SEEK_SET) == -1)
715 lte->staging_offset = offset;
717 ret = read(fd, buf, size);
720 lte->staging_offset = offset + ret;
727 struct resource_entry *res_entry;
730 res_entry = <e->resource_entry;
732 ctype = wim_resource_compression_type(w, res_entry);
734 if (offset > res_entry->original_size)
737 size = min(size, res_entry->original_size - offset);
739 if (read_resource(w->fp, res_entry->size,
740 res_entry->original_size,
741 res_entry->offset, ctype, size,
748 /* Fills in the entries of the directory specified by @path using the
749 * FUSE-provided function @filler. */
750 static int wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
751 off_t offset, struct fuse_file_info *fi)
753 struct dentry *parent = (struct dentry*) fi->fh;
754 filler(buf, ".", NULL, 0);
755 filler(buf, "..", NULL, 0);
757 child = parent->children;
763 if (filler(buf, child->file_name_utf8, NULL, 0))
766 } while (child != parent->children);
771 static int wimfs_readlink(const char *path, char *buf, size_t buf_len)
773 struct dentry *dentry = get_dentry(w, path);
777 if (!dentry_is_symlink(dentry))
780 ret = dentry_readlink(dentry, buf, buf_len, w);
787 static int wimfs_release(const char *path, struct fuse_file_info *fi)
789 struct lookup_table_entry *lte;
792 lte = (struct lookup_table_entry*)fi->fh;
794 if (!lte || lte->staging_num_times_opened == 0)
797 if (--lte->staging_num_times_opened == 0 && lte->staging_file_name) {
798 ret = close(lte->staging_fd);
799 if (lte->refcnt == 0)
800 free_lookup_table_entry(lte);
807 /* Renames a file or directory. See rename (3) */
808 static int wimfs_rename(const char *from, const char *to)
812 struct dentry *parent_of_dst;
814 src = get_dentry(w, from);
818 dst = get_dentry(w, to);
821 if (!dentry_is_directory(src)) {
822 /* Cannot rename non-directory to directory. */
823 if (dentry_is_directory(dst))
826 /* Cannot rename directory to a non-directory or a non-empty
828 if (!dentry_is_directory(dst))
830 if (dst->children != NULL)
833 parent_of_dst = dst->parent;
835 lookup_table_decrement_refcnt(w->lookup_table, dst->hash);
838 parent_of_dst = get_parent_dentry(w, to);
844 change_dentry_name(src, path_basename(to));
845 link_dentry(src, parent_of_dst);
846 /*calculate_dentry_full_path(src);*/
850 /* Remove a directory */
851 static int wimfs_rmdir(const char *path)
853 struct dentry *dentry;
855 dentry = get_dentry(w, path);
859 if (!dentry_is_empty_directory(dentry))
862 unlink_dentry(dentry);
867 /* Extracts the resource corresponding to @dentry and its lookup table entry
868 * @lte to a file in the staging directory. The lookup table entry for @dentry
869 * is updated to point to the new file. If @lte has multiple dentries
870 * referencing it, a new lookup table entry is created and the hash of @dentry
871 * is changed to point to the new lookup table entry.
873 * Only @size bytes are extracted, to support truncating the file.
875 * Returns the negative error code on failure.
877 static int extract_resource_to_staging_dir(u8 *dentry_hash,
878 struct lookup_table_entry *lte,
883 char *staging_file_name;
884 struct lookup_table_entry *new_lte;
886 /* File in WIM. Copy it to the staging directory. */
887 fd = create_staging_file(&staging_file_name);
891 ret = extract_resource_to_fd(w, <e->resource_entry, fd, size);
897 unlink(staging_file_name);
898 FREE(staging_file_name);
902 if (lte->refcnt != 1) {
903 /* Need to make a new lookup table entry if we are
904 * changing only one copy of a hardlinked entry */
907 new_lte = new_lookup_table_entry();
910 randomize_byte_array(dentry_hash, WIM_HASH_SIZE);
911 memcpy(new_lte->hash, dentry_hash, WIM_HASH_SIZE);
913 new_lte->resource_entry.flags = 0;
914 new_lte->staging_num_times_opened = lte->staging_num_times_opened;
916 lookup_table_insert(w->lookup_table, new_lte);
921 lte->resource_entry.original_size = size;
922 lte->staging_file_name = staging_file_name;
924 if (lte->staging_num_times_opened == 0)
927 lte->staging_fd = fd;
931 /* Reduce the size of a file */
932 static int wimfs_truncate(const char *path, off_t size)
934 struct dentry *dentry;
935 struct lookup_table_entry *lte;
939 ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
945 if (!lte) /* Already a zero-length file */
948 if (lte->staging_file_name) {
949 /* File on disk. Call POSIX API */
950 if (lte->staging_num_times_opened != 0)
951 ret = ftruncate(lte->staging_fd, size);
953 ret = truncate(lte->staging_file_name, size);
956 dentry_update_all_timestamps(dentry);
957 lte->resource_entry.original_size = size;
959 /* File in WIM. Extract it to the staging directory, but only
960 * the first @size bytes of it. */
961 ret = extract_resource_to_staging_dir(dentry_hash, lte, size);
966 /* Remove a regular file */
967 static int wimfs_unlink(const char *path)
969 struct dentry *dentry;
970 struct lookup_table_entry *lte;
974 ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
980 if (lte && lte->staging_file_name)
981 if (unlink(lte->staging_file_name) != 0)
984 if (dentry_hash == dentry->hash) {
985 /* We are removing the full dentry including all alternate data
987 const u8 *hash = dentry->hash;
990 lookup_table_decrement_refcnt(w->lookup_table, hash);
991 if (i == dentry->num_ads)
993 hash = dentry->ads_entries[i].hash;
997 unlink_dentry(dentry);
1000 /* We are removing an alternate data stream. */
1001 struct ads_entry *cur_entry = dentry->ads_entries;
1002 while (cur_entry->hash != dentry_hash)
1004 lookup_table_decrement_refcnt(w->lookup_table, cur_entry->hash);
1006 dentry_remove_ads(dentry, cur_entry);
1008 /* Beware: The lookup table entry(s) may still be referenced by users
1009 * that have opened the corresponding streams. They are freed later in
1010 * wimfs_release() when the last file user has closed the stream. */
1014 /* Change the timestamp on a file dentry.
1016 * There is no distinction between a file and its alternate data streams here. */
1017 static int wimfs_utimens(const char *path, const struct timespec tv[2])
1019 struct dentry *dentry = get_dentry(w, path);
1022 time_t last_access_t = (tv[0].tv_nsec == UTIME_NOW) ?
1023 time(NULL) : tv[0].tv_sec;
1024 dentry->last_access_time = unix_timestamp_to_ms(last_access_t);
1025 time_t last_mod_t = (tv[1].tv_nsec == UTIME_NOW) ?
1026 time(NULL) : tv[1].tv_sec;
1027 dentry->last_write_time = unix_timestamp_to_ms(last_mod_t);
1031 /* Writes to a file in the WIM filesystem.
1032 * It may be an alternate data stream, but here we don't even notice because we
1033 * just get a lookup table entry. */
1034 static int wimfs_write(const char *path, const char *buf, size_t size,
1035 off_t offset, struct fuse_file_info *fi)
1037 /* Grab our lookup table entry from the FUSE file info structure. */
1038 struct lookup_table_entry *lte;
1039 lte = (struct lookup_table_entry*)fi->fh;
1042 if (!lte || !lte->staging_file_name)
1045 /* Seek to correct position in file if needed. */
1046 if (lte->staging_offset != offset) {
1047 if (lseek(lte->staging_fd, offset, SEEK_SET) == -1)
1049 lte->staging_offset = offset;
1052 /* Write the data. */
1053 ret = write(lte->staging_fd, buf, size);
1057 /* Adjust the stored offset of staging_fd. */
1058 lte->staging_offset = offset + ret;
1060 /* Increase file size if needed. */
1061 if (lte->resource_entry.original_size < lte->staging_offset)
1062 lte->resource_entry.original_size = lte->staging_offset;
1064 /* The file has been modified, so all its timestamps must be
1066 dentry_update_all_timestamps(dentry);
1072 static struct fuse_operations wimfs_oper = {
1073 .access = wimfs_access,
1074 .destroy = wimfs_destroy,
1075 .getattr = wimfs_getattr,
1076 .mkdir = wimfs_mkdir,
1077 .mknod = wimfs_mknod,
1079 .opendir = wimfs_opendir,
1081 .readdir = wimfs_readdir,
1082 .readlink = wimfs_readlink,
1083 .release = wimfs_release,
1084 .rename = wimfs_rename,
1085 .rmdir = wimfs_rmdir,
1086 .truncate = wimfs_truncate,
1087 .unlink = wimfs_unlink,
1088 .utimens = wimfs_utimens,
1089 .write = wimfs_write,
1093 /* Mounts a WIM file. */
1094 WIMLIBAPI int wimlib_mount(WIMStruct *wim, int image, const char *dir,
1102 DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
1103 wim, image, dir, flags);
1106 return WIMLIB_ERR_INVALID_PARAM;
1108 ret = wimlib_select_image(wim, image);
1113 if (flags & WIMLIB_MOUNT_FLAG_READWRITE)
1114 wim_get_current_image_metadata(wim)->modified = true;
1116 if (!(flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
1117 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
1118 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
1119 flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1122 working_directory = getcwd(NULL, 0);
1123 if (!working_directory) {
1124 ERROR_WITH_ERRNO("Could not determine current directory");
1125 return WIMLIB_ERR_NOTDIR;
1130 return WIMLIB_ERR_NOMEM;
1132 argv[argc++] = "mount";
1134 argv[argc++] = "-s"; /* disable multi-threaded operation */
1136 if (flags & WIMLIB_MOUNT_FLAG_DEBUG) {
1137 argv[argc++] = "-d";
1139 if (!(flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
1140 argv[argc++] = "-o";
1141 argv[argc++] = "ro";
1144 if (!staging_dir_name) {
1146 return WIMLIB_ERR_MKDIR;
1153 DEBUG("FUSE command line (argc = %d): ", argc);
1154 for (i = 0; i < argc; i++) {
1155 fputs(argv[i], stdout);
1163 /* Set static variables. */
1165 mount_flags = flags;
1167 ret = fuse_main(argc, argv, &wimfs_oper, NULL);
1169 return (ret == 0) ? 0 : WIMLIB_ERR_FUSE;
1174 * Unmounts the WIM file that was previously mounted on @dir by using
1177 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
1184 struct timespec timeout;
1188 /* Execute `fusermount -u', which is installed setuid root, to unmount
1191 * FUSE does not yet implement synchronous unmounts. This means that
1192 * fusermount -u will return before the filesystem daemon returns from
1193 * wimfs_destroy(). This is partly what we want, because we need to
1194 * send a message from this process to the filesystem daemon telling
1195 * whether --commit was specified or not. However, after that, the
1196 * unmount process must wait for the filesystem daemon to finish writing
1203 ERROR_WITH_ERRNO("Failed to fork()");
1204 return WIMLIB_ERR_FORK;
1207 execlp("fusermount", "fusermount", "-u", dir, NULL);
1208 ERROR_WITH_ERRNO("Failed to execute `fusermount'");
1209 return WIMLIB_ERR_FUSERMOUNT;
1212 ret = waitpid(pid, &status, 0);
1214 ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
1216 return WIMLIB_ERR_FUSERMOUNT;
1220 ERROR("fusermount exited with status %d", status);
1221 return WIMLIB_ERR_FUSERMOUNT;
1224 /* Open message queues between the unmount process and the
1225 * filesystem daemon. */
1226 ret = open_message_queues(false);
1230 /* Send a message to the filesystem saying whether to commit or
1232 msg[0] = (flags & WIMLIB_UNMOUNT_FLAG_COMMIT) ? 1 : 0;
1233 msg[1] = (flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY) ? 1 : 0;
1235 DEBUG("Sending message: %s, %s",
1236 (msg[0] == 0) ? "don't commit" : "commit",
1237 (msg[1] == 0) ? "don't check" : "check");
1238 ret = mq_send(unmount_to_daemon_mq, msg, 2, 1);
1240 ERROR("Failed to notify filesystem daemon whether we want to "
1241 "commit changes or not");
1242 close_message_queues();
1243 return WIMLIB_ERR_MQUEUE;
1246 /* Wait for a message from the filesytem daemon indicating whether the
1247 * filesystem was unmounted successfully (0) or an error occurred (1).
1248 * This may take a long time if a big WIM file needs to be rewritten. */
1250 /* Wait at most 600??? seconds before giving up and returning false.
1251 * Either it's a really big WIM file, or (more likely) the
1252 * filesystem daemon has crashed or failed for some reason.
1254 * XXX come up with some method to determine if the filesystem
1255 * daemon has really crashed or not. */
1257 gettimeofday(&now, NULL);
1258 timeout.tv_sec = now.tv_sec + 600;
1259 timeout.tv_nsec = now.tv_usec * 1000;
1261 msgsize = mq_get_msgsize(daemon_to_unmount_mq);
1262 char mailbox[msgsize];
1265 DEBUG("Waiting for message telling us whether the unmount was "
1266 "successful or not.");
1267 ret = mq_timedreceive(daemon_to_unmount_mq, mailbox, msgsize,
1270 close_message_queues();
1272 if (errno_save == ETIMEDOUT) {
1273 ERROR("Timed out- probably the filesystem daemon "
1274 "crashed and the WIM was not written "
1276 return WIMLIB_ERR_TIMEOUT;
1278 ERROR("mq_receive(): %s", strerror(errno_save));
1279 return WIMLIB_ERR_MQUEUE;
1283 DEBUG("Received message: %s",
1284 (mailbox[0] == 0) ? "Unmount OK" : "Unmount Failed");
1285 if (mailbox[0] != 0)
1286 ERROR("Unmount failed");
1290 #else /* WITH_FUSE */
1293 static inline int mount_unsupported_error()
1295 ERROR("WIMLIB was compiled with --without-fuse, which disables support "
1296 "for mounting WIMs.");
1297 return WIMLIB_ERR_UNSUPPORTED;
1300 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
1302 return mount_unsupported_error();
1305 WIMLIBAPI int wimlib_mount(WIMStruct *wim_p, int image, const char *dir,
1308 return mount_unsupported_error();
1311 #endif /* WITH_FUSE */