4 * This file implements mounting of WIM files using FUSE, which stands for
5 * Filesystem in Userspace. FUSE allows a filesystem to be implemented in a
6 * userspace process by implementing the filesystem primitives--- read(),
7 * write(), readdir(), etc.
11 * Copyright (C) 2012 Eric Biggers
13 * This file is part of wimlib, a library for working with WIM files.
15 * wimlib is free software; you can redistribute it and/or modify it under the
16 * terms of the GNU Lesser General Public License as published by the Free
17 * Software Foundation; either version 2.1 of the License, or (at your option)
20 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
21 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
22 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
25 * You should have received a copy of the GNU Lesser General Public License
26 * along with wimlib; if not, see http://www.gnu.org/licenses/.
29 #include "wimlib_internal.h"
33 #include "lookup_table.h"
36 #include "timestamp.h"
40 #define FUSE_USE_VERSION 26
52 struct lookup_table_entry *lte;
53 struct dentry *dentry;
56 /* The WIMStruct for the mounted WIM. */
59 /* Working directory when `imagex mount' is run. */
60 static const char *working_directory;
62 /* Name of the staging directory for a read-write mount. Whenever a new file is
63 * created, it is done so in the staging directory. Furthermore, whenever a
64 * file in the WIM is modified, it is extracted to the staging directory. If
65 * changes are commited when the WIM is unmounted, the file resources are merged
66 * in from the staging directory when writing the new WIM. */
67 static char *staging_dir_name;
68 static size_t staging_dir_name_len;
70 /* Flags passed to wimlib_mount(). */
71 static int mount_flags;
73 /* Name of the directory on which the WIM file is mounted. */
74 static const char *mount_dir;
76 /* Next hard link group ID to be assigned. These are also used as the inode
78 static u64 next_link_group_id;
80 /* List of lookup table entries in the staging directory */
81 static LIST_HEAD(staging_list);
83 static inline int get_lookup_flags()
85 if (mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
86 return LOOKUP_FLAG_ADS_OK;
91 static inline int flags_writable(int open_flags)
93 return open_flags & (O_RDWR | O_WRONLY);
96 static int alloc_wimlib_fd(struct lookup_table_entry *lte,
97 struct wimlib_fd **fd_ret)
99 static const u16 fds_per_alloc = 8;
100 static const u16 max_fds = 0xffff;
102 if (lte->num_opened_fds == lte->num_allocated_fds) {
103 struct wimlib_fd **fds;
106 if (lte->num_allocated_fds == max_fds)
108 num_new_fds = min(fds_per_alloc, max_fds - lte->num_allocated_fds);
110 fds = CALLOC(lte->num_allocated_fds + num_new_fds,
111 sizeof(lte->fds[0]));
114 memcpy(fds, lte->fds,
115 lte->num_allocated_fds * sizeof(lte->fds[0]));
118 lte->num_allocated_fds += num_new_fds;
120 for (u16 i = 0; ; i++) {
122 struct wimlib_fd *fd = CALLOC(1, sizeof(*fd));
129 lte->num_opened_fds++;
136 static int close_wimlib_fd(struct wimlib_fd *fd)
138 struct lookup_table_entry *lte = fd->lte;
141 wimlib_assert(lte->num_opened_fds);
143 if (lte->staging_file_name) {
144 wimlib_assert(fd->staging_fd != -1);
145 if (close(fd->staging_fd) != 0)
148 if (--lte->num_opened_fds == 0 && lte->refcnt == 0) {
149 if (lte->staging_file_name)
150 unlink(lte->staging_file_name);
151 free_lookup_table_entry(lte);
153 lte->fds[fd->idx] = NULL;
158 static void remove_dentry(struct dentry *dentry,
159 struct lookup_table *lookup_table)
161 const u8 *hash = dentry->hash;
163 struct lookup_table_entry *lte;
165 lte = lookup_table_decrement_refcnt(lookup_table, hash);
166 if (lte && lte->num_opened_fds)
167 for (u16 i = 0; i < lte->num_allocated_fds; i++)
168 if (lte->fds[i] && lte->fds[i]->dentry == dentry)
169 lte->fds[i]->dentry = NULL;
170 if (i == dentry->num_ads)
172 hash = dentry->ads_entries[i].hash;
176 unlink_dentry(dentry);
180 static void dentry_increment_lookup_table_refcnts(struct dentry *dentry,
181 struct lookup_table *lookup_table)
184 const u8 *hash = dentry->hash;
185 struct lookup_table_entry *lte;
187 lte = __lookup_resource(lookup_table, hash);
190 if (i == dentry->num_ads)
192 hash = dentry->ads_entries[i].hash;
197 /* Transfers file attributes from a struct dentry to a `stat' buffer. */
198 int dentry_to_stbuf(const struct dentry *dentry, struct stat *stbuf)
200 struct lookup_table_entry *lte;
202 if (dentry_is_symlink(dentry))
203 stbuf->st_mode = S_IFLNK | 0777;
204 else if (dentry_is_directory(dentry))
205 stbuf->st_mode = S_IFDIR | 0755;
207 stbuf->st_mode = S_IFREG | 0644;
209 stbuf->st_ino = (ino_t)dentry->hard_link;
211 stbuf->st_nlink = dentry_link_group_size(dentry);
212 stbuf->st_uid = getuid();
213 stbuf->st_gid = getgid();
215 /* Use the size of the unnamed (default) file stream. */
216 if ((lte = dentry_lte(dentry))) {
217 if (lte->staging_file_name) {
218 struct stat native_stat;
219 if (stat(lte->staging_file_name, &native_stat) != 0)
221 stbuf->st_size = native_stat.st_size;
223 stbuf->st_size = lte->resource_entry.original_size;
229 stbuf->st_atime = ms_timestamp_to_unix(dentry->last_access_time);
230 stbuf->st_mtime = ms_timestamp_to_unix(dentry->last_write_time);
231 stbuf->st_ctime = ms_timestamp_to_unix(dentry->creation_time);
232 stbuf->st_blocks = (stbuf->st_size + 511) / 512;
237 /* Change the hash value of the main or alternate file stream in a hard link
238 * group. This needs to be done if the hash of the corresponding lookup table
239 * entry was changed. */
240 static void link_group_set_stream_hash(struct dentry *dentry,
244 struct list_head *head, *cur;
246 if (stream_idx == 0) {
247 head = &dentry->link_group_list;
250 dentry = container_of(cur, struct dentry, link_group_list);
251 memcpy(dentry->hash, new_hash, WIM_HASH_SIZE);
253 } while (cur != head);
255 /* Dentries in the link group share their alternate stream
257 wimlib_assert(stream_idx <= dentry->num_ads);
258 memcpy(dentry->ads_entries[stream_idx - 1].hash, new_hash,
264 /* Creates a new staging file and returns its file descriptor opened for
267 * @name_ret: A location into which the a pointer to the newly allocated name of
268 * the staging file is stored.
269 * @return: The file descriptor for the new file. Returns -1 and sets errno on
270 * error, for any reason possible from the creat() function.
272 static int create_staging_file(char **name_ret, int open_flags)
280 name_len = staging_dir_name_len + 1 + WIM_HASH_SIZE;
281 name = MALLOC(name_len + 1);
289 memcpy(name, staging_dir_name, staging_dir_name_len);
290 name[staging_dir_name_len] = '/';
291 randomize_char_array_with_alnum(name + staging_dir_name_len + 1,
293 name[name_len] = '\0';
296 /* Just in case, verify that the randomly generated name doesn't name an
297 * existing file, and try again if so */
298 } while (stat(name, &stbuf) == 0);
304 /* doesn't exist--- ok */
306 DEBUG("Creating staging file `%s'", name);
308 fd = open(name, open_flags | O_CREAT | O_TRUNC, 0600);
319 /* Removes open file descriptors from a lookup table entry @old_lte where the
320 * file descriptors have opened the corresponding file resource in the context
321 * of the hard link group @link_group; these file descriptors are extracted and
322 * placed in a new lookup table entry, which is returned. */
323 static struct lookup_table_entry *
324 lte_extract_fds(struct lookup_table_entry *old_lte, u64 link_group)
327 u16 num_transferred_fds;
328 struct lookup_table_entry *new_lte;
330 new_lte = new_lookup_table_entry();
334 num_transferred_fds = 0;
335 for (u16 i = 0; i < old_lte->num_allocated_fds; i++)
336 if (old_lte->fds[i] &&
337 old_lte->fds[i]->dentry->hard_link == link_group)
338 num_transferred_fds++;
339 DEBUG("Transferring %u file descriptors",
340 num_transferred_fds);
341 new_lte->fds = MALLOC(num_transferred_fds * sizeof(new_lte->fds[0]));
346 for (u16 i = 0, j = 0; ; i++) {
347 if (old_lte->fds[i] &&
348 old_lte->fds[i]->dentry->hard_link == link_group) {
349 struct wimlib_fd *fd = old_lte->fds[i];
350 old_lte->fds[i] = NULL;
353 new_lte->fds[j] = fd;
354 if (++j == num_transferred_fds)
358 old_lte->num_opened_fds -= num_transferred_fds;
359 new_lte->num_opened_fds = num_transferred_fds;
360 new_lte->num_allocated_fds = num_transferred_fds;
365 * Transfers an alternate data stream entry to a new lookup table entry
367 static void lte_transfer_ads_entry(struct lookup_table_entry *new_lte,
368 struct ads_entry *ads_entry)
370 list_del(&ads_entry->lte_group_list.list);
371 list_add(&ads_entry->lte_group_list.list, &new_lte->lte_group_list);
372 ads_entry->lte = new_lte;
376 * Transfers a dentry to a new lookup table entry
378 static void lte_transfer_dentry(struct lookup_table_entry *new_lte,
379 struct dentry *dentry)
381 list_del(&dentry->lte_group_list.list);
382 list_add(&dentry->lte_group_list.list, &new_lte->lte_group_list);
383 dentry->lte = new_lte;
386 static void lte_transfer_stream_entries(struct lookup_table_entry *new_lte,
387 struct dentry *dentry,
390 INIT_LIST_HEAD(&new_lte->lte_group_list);
391 if (stream_idx == 0) {
392 struct list_head *pos;
395 d = container_of(pos, struct dentry, link_group_list);
396 lte_transfer_dentry(new_lte, d);
399 } while (pos != &dentry->link_group_list);
401 struct ads_entry *ads_entry;
402 ads_entry = &dentry->ads_entries[stream_idx - 1];
403 lte_transfer_ads_entry(new_lte, ads_entry);
409 * Transfers streams part of a hard-link group from @old_lte to @new_lte.
411 * @dentry is one of the dentries in the hard link group
412 * @stream_idx is the index of the stream that we're transferring.
414 static void lte_transfer_stream_entries(struct lookup_table_entry *old_lte,
415 struct lookup_table_entry *new_lte,
416 struct dentry *dentry, unsigned stream_idx)
418 INIT_LIST_HEAD(&new_lte->lte_group_list);
420 if (stream_idx == 0) {
421 struct list_head *pos;
422 struct stream_list_head *head;
423 struct dentry *other_dentry;
424 list_for_each(pos, &old_lte->lte_group_list) {
425 head = container_of(pos, struct stream_list_head, head);
426 if (head->type != STREAM_TYPE_NORMAL) {
428 other_dentry = container_of(head, struct dentry,
430 if (other_dentry->hard_link != dentry->link_group)
433 list_del(&other_dentry->lte_group_list.list);
434 list_add(&other_dentry->lte_group_list.list,
435 &new_lte->lte_group_list);
436 other_dentry->lte = new_lte;
439 /* ADS entries are shared within a hard link group. */
440 lte_load_ads_entry(new_lte, &dentry->ads_entries[stream_idx - 1]);
443 static void lte__stream_entries(struct lookup_table_entry *new_lte,
444 struct dentry *dentry, unsigned stream_idx)
446 INIT_LIST_HEAD(new_lte->lte_group_list);
447 if (stream_idx == 0) {
448 struct list_head *cur;
452 d = container_of(cur, struct dentry, link_group_list);
453 list_del(&d->lte_group_list);
454 list_add(&d->lte_group_list, &new_lte->lte_group_list);
457 } while (cur != &dentry->link_group_list);
459 lte_load_ads_entry(new_lte, &dentry->ads_entries[stream_idx - 1]);
466 * Extract a WIM resource to the staging directory.
469 * - Create a staging file for the WIM resource
470 * - Extract the resource to it
471 * - Create a new lte for the file resource
472 * - Transfer fds from the old lte to the new lte, but
473 * only if they share the same hard link group as this
476 static int extract_resource_to_staging_dir(struct dentry *dentry,
478 struct lookup_table_entry **lte,
481 char *staging_file_name;
484 struct lookup_table_entry *old_lte, *new_lte;
485 size_t link_group_size;
487 DEBUG("Extracting resource `%s' to staging directory", dentry->full_path_utf8);
490 fd = create_staging_file(&staging_file_name, O_WRONLY);
495 ret = extract_resource_to_fd(w, &old_lte->resource_entry, fd,
499 if (ret != 0 || close(fd) != 0) {
505 goto out_delete_staging_file;
508 link_group_size = dentry_link_group_size(dentry);
511 if (link_group_size == old_lte->refcnt) {
512 /* This hard link group is the only user of the lookup
513 * table entry, so we can re-use it. */
514 DEBUG("Re-using lookup table entry");
515 lookup_table_remove(w->lookup_table, old_lte);
518 DEBUG("Splitting lookup table entry "
519 "(link_group_size = %u, lte refcnt = %u)",
520 link_group_size, old_lte->refcnt);
521 /* Split a hard link group away from the "lookup table
522 * entry" hard link group (i.e. we had two hard link
523 * groups that were identical, but now we are changing
526 /* XXX The ADS really complicate things here and not
527 * everything is going to work correctly yet. For
528 * example it could be the same that a file contains two
529 * file streams that are identical and therefore share
530 * the same lookup table entry despite the fact that the
531 * streams themselves are not hardlinked. */
532 wimlib_assert(old_lte->refcnt > link_group_size);
534 new_lte = lte_extract_fds(old_lte, dentry->hard_link);
537 goto out_delete_staging_file;
540 lte_transfer_stream_entries(new_lte, dentry, stream_idx);
541 old_lte->refcnt -= link_group_size;
544 /* No old_lte was supplied, so the resource had no lookup table
545 * entry before (it must be an empty resource) */
546 new_lte = new_lookup_table_entry();
549 goto out_delete_staging_file;
551 lte_transfer_stream_entries(new_lte, dentry, stream_idx);
553 new_lte->resource_entry.original_size = size;
554 new_lte->refcnt = link_group_size;
555 randomize_byte_array(new_lte->hash, WIM_HASH_SIZE);
556 new_lte->staging_file_name = staging_file_name;
558 lookup_table_insert(w->lookup_table, new_lte);
559 list_add(&new_lte->staging_list, &staging_list);
562 out_delete_staging_file:
563 unlink(staging_file_name);
564 FREE(staging_file_name);
569 * Creates a randomly named staging directory and returns its name into the
570 * static variable staging_dir_name.
572 * If the staging directory cannot be created, staging_dir_name is set to NULL.
574 static void make_staging_dir()
576 /* XXX Give the user an option of where to stage files */
578 static char prefix[] = "wimlib-staging-";
579 static const size_t prefix_len = 15;
580 static const size_t suffix_len = 10;
582 size_t pwd_len = strlen(working_directory);
584 staging_dir_name_len = pwd_len + 1 + prefix_len + suffix_len;
586 staging_dir_name = MALLOC(staging_dir_name_len + 1);
587 if (!staging_dir_name) {
588 ERROR("Out of memory");
592 memcpy(staging_dir_name, working_directory, pwd_len);
593 staging_dir_name[pwd_len] = '/';
594 memcpy(staging_dir_name + pwd_len + 1, prefix, prefix_len);
595 randomize_char_array_with_alnum(staging_dir_name + pwd_len + 1 + prefix_len,
597 staging_dir_name[staging_dir_name_len] = '\0';
599 if (mkdir(staging_dir_name, 0700) != 0) {
600 ERROR_WITH_ERRNO("Failed to create temporary directory `%s'",
602 FREE(staging_dir_name);
603 staging_dir_name = NULL;
607 static int remove_file_or_directory(const char *fpath, const struct stat *sb,
608 int typeflag, struct FTW *ftwbuf)
610 if (remove(fpath) == 0)
613 return WIMLIB_ERR_DELETE_STAGING_DIR;
618 * Deletes the staging directory and all the files contained in it.
620 static inline int delete_staging_dir()
624 ret = nftw(staging_dir_name, remove_file_or_directory,10, FTW_DEPTH);
625 staging_dir_name = NULL;
629 /* Name and message queue descriptors for message queues between the filesystem
630 * daemon process and the unmount process. These are used when the filesystem
631 * is unmounted and the process running wimlib_mount() (i.e. the `imagex
632 * unmount' command) needs to communicate with the filesystem daemon running
633 * fuse_main() (i.e. that spawned by the `imagex mount' or `imagex mountrw'
635 static char *unmount_to_daemon_mq_name;
636 static char *daemon_to_unmount_mq_name;
637 static int unmount_to_daemon_mq;
638 static int daemon_to_unmount_mq;
640 /* Simple function that returns the concatenation of 4 strings. */
641 static char *strcat_dup(const char *s1, const char *s2, const char *s3,
644 size_t len = strlen(s1) + strlen(s2) + strlen(s3) + strlen(s4) + 1;
645 char *p = MALLOC(len);
656 /* Removes trailing forward slashes in a string. */
657 static void remove_trailing_slashes(char *s)
659 long len = strlen(s);
660 for (long i = len - 1; i >= 1; i--) {
668 /* Changes forward slashes to underscores in a string. */
669 static void s_slashes_underscores_g(char *s)
679 * Opens two POSIX message queue: one for sending messages from the unmount
680 * process to the daemon process, and one to go the other way. The names of the
681 * message queues, which must be system-wide unique, are be based on the mount
682 * point. (There of course is still a possibility of a collision if one were to
683 * unmount two identically named directories simultaneously...)
685 * @daemon specifies whether the calling process is the filesystem daemon or the
688 static int open_message_queues(bool daemon)
690 static const char *slash = "/";
691 static const char *prefix = "wimlib-";
692 static const char *u2d_suffix = "unmount-to-daemon-mq";
693 static const char *d2u_suffix = "daemon-to-unmount-mq";
695 const char *mount_dir_basename = path_basename(mount_dir);
699 unmount_to_daemon_mq_name = strcat_dup(slash, mount_dir_basename,
701 if (!unmount_to_daemon_mq_name) {
702 ERROR("Out of memory");
703 return WIMLIB_ERR_NOMEM;
705 daemon_to_unmount_mq_name = strcat_dup(slash, mount_dir_basename,
707 if (!daemon_to_unmount_mq_name) {
708 ERROR("Out of memory");
709 ret = WIMLIB_ERR_NOMEM;
713 remove_trailing_slashes(unmount_to_daemon_mq_name);
714 remove_trailing_slashes(daemon_to_unmount_mq_name);
715 s_slashes_underscores_g(unmount_to_daemon_mq_name + 1);
716 s_slashes_underscores_g(daemon_to_unmount_mq_name + 1);
719 flags = O_RDONLY | O_CREAT;
721 flags = O_WRONLY | O_CREAT;
723 unmount_to_daemon_mq = mq_open(unmount_to_daemon_mq_name, flags,
726 if (unmount_to_daemon_mq == -1) {
727 ERROR_WITH_ERRNO("mq_open()");
728 ret = WIMLIB_ERR_MQUEUE;
733 flags = O_WRONLY | O_CREAT;
735 flags = O_RDONLY | O_CREAT;
737 daemon_to_unmount_mq = mq_open(daemon_to_unmount_mq_name, flags,
740 if (daemon_to_unmount_mq == -1) {
741 ERROR_WITH_ERRNO("mq_open()");
742 ret = WIMLIB_ERR_MQUEUE;
747 mq_close(unmount_to_daemon_mq);
748 mq_unlink(unmount_to_daemon_mq_name);
750 FREE(daemon_to_unmount_mq_name);
752 FREE(unmount_to_daemon_mq_name);
756 static int mq_get_msgsize(mqd_t mq)
758 static const char *msgsize_max_file = "/proc/sys/fs/mqueue/msgsize_max";
763 if (mq_getattr(unmount_to_daemon_mq, &attr) == 0) {
764 msgsize = attr.mq_msgsize;
766 ERROR_WITH_ERRNO("mq_getattr()");
767 ERROR("Attempting to read %s", msgsize_max_file);
768 fp = fopen(msgsize_max_file, "rb");
770 if (fscanf(fp, "%d", &msgsize) != 1) {
771 ERROR("Assuming message size of 8192");
776 ERROR_WITH_ERRNO("Failed to open the file `%s'",
778 ERROR("Assuming message size of 8192");
785 /* Closes the message queues, which are allocated in static variables */
786 static void close_message_queues()
788 mq_close(unmount_to_daemon_mq);
789 mq_close(daemon_to_unmount_mq);
790 mq_unlink(unmount_to_daemon_mq_name);
791 mq_unlink(daemon_to_unmount_mq_name);
794 static int wimfs_access(const char *path, int mask)
796 /* XXX Permissions not implemented */
800 /* Closes the staging file descriptor associated with the lookup table entry, if
802 static int close_lte_fds(struct lookup_table_entry *lte)
804 for (u16 i = 0, j = 0; j < lte->num_opened_fds; i++) {
805 if (lte->fds[i] && lte->fds[i]->staging_fd != -1) {
806 if (close(lte->fds[i]->staging_fd) != 0) {
807 ERROR_WITH_ERRNO("Failed close file `%s'",
808 lte->staging_file_name);
809 return WIMLIB_ERR_WRITE;
818 /* Calculates the SHA1 sum for @dentry if its file resource is in a staging
819 * file. Updates the SHA1 sum in the dentry and the lookup table entry. If
820 * there is already a lookup table entry with the same checksum, increment its
821 * reference count and destroy the lookup entry with the updated checksum. */
822 static int calculate_sha1sum_of_staging_file(struct dentry *dentry,
823 void *__lookup_table)
825 struct lookup_table *lookup_table = __lookup_table;
826 u8 *hash = dentry->hash;
829 struct lookup_table_entry *lte = __lookup_resource(lookup_table, hash);
830 if (lte && lte->staging_file_name) {
831 struct lookup_table_entry *existing;
834 DEBUG("Calculating SHA1 hash for file `%s'",
835 dentry->file_name_utf8);
836 ret = sha1sum(lte->staging_file_name, lte->hash);
840 lookup_table_unlink(lookup_table, lte);
841 memcpy(hash, lte->hash, WIM_HASH_SIZE);
842 existing = __lookup_resource(lookup_table, hash);
844 DEBUG("Merging duplicate lookup table entries for file "
845 "`%s'", dentry->file_name_utf8);
846 free_lookup_table_entry(lte);
849 lookup_table_insert(lookup_table, lte);
852 if (i == dentry->num_ads)
854 hash = dentry->ads_entries[i].hash;
860 /* Overwrites the WIM file, with changes saved. */
861 static int rebuild_wim(WIMStruct *w, bool check_integrity)
864 struct lookup_table_entry *lte;
866 /* Close all the staging file descriptors. */
867 DEBUG("Closing all staging file descriptors.");
868 list_for_each_entry(lte, &staging_list, staging_list) {
869 ret = close_lte_fds(lte);
874 /* Calculate SHA1 checksums for all staging files, and merge unnecessary
875 * lookup table entries. */
876 DEBUG("Calculating SHA1 checksums for all new staging files.");
877 ret = for_dentry_in_tree(wim_root_dentry(w),
878 calculate_sha1sum_of_staging_file, w->lookup_table);
882 xml_update_image_info(w, w->current_image);
884 ret = wimlib_overwrite(w, check_integrity);
886 ERROR("Failed to commit changes");
892 /* Called when the filesystem is unmounted. */
893 static void wimfs_destroy(void *p)
895 /* For read-write mounts, the `imagex unmount' command, which is
896 * running in a separate process and is executing the
897 * wimlib_unmount() function, will send this process a byte
898 * through a message queue that indicates whether the --commit
899 * option was specified or not. */
902 struct timespec timeout;
904 ssize_t bytes_received;
907 char check_integrity;
910 ret = open_message_queues(true);
914 msgsize = mq_get_msgsize(unmount_to_daemon_mq);
919 /* Wait at most 3 seconds before giving up and discarding changes. */
920 gettimeofday(&now, NULL);
921 timeout.tv_sec = now.tv_sec + 3;
922 timeout.tv_nsec = now.tv_usec * 1000;
923 DEBUG("Waiting for message telling us whether to commit or not, and "
924 "whether to include integrity checks.");
926 bytes_received = mq_timedreceive(unmount_to_daemon_mq, msg,
927 msgsize, NULL, &timeout);
929 check_integrity = msg[1];
930 if (bytes_received == -1) {
931 if (errno == ETIMEDOUT) {
934 ERROR_WITH_ERRNO("mq_timedreceive()");
936 ERROR("Not committing.");
938 DEBUG("Received message: [%d %d]", msg[0], msg[1]);
942 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
944 status = chdir(working_directory);
946 ERROR_WITH_ERRNO("chdir()");
947 status = WIMLIB_ERR_NOTDIR;
950 status = rebuild_wim(w, (check_integrity != 0));
952 ret = delete_staging_dir();
954 ERROR_WITH_ERRNO("Failed to delete the staging "
961 ret = mq_send(daemon_to_unmount_mq, &status, 1, 1);
963 ERROR_WITH_ERRNO("Failed to send status to unmount process");
964 close_message_queues();
967 static int wimfs_fallocate(const char *path, int mode,
968 off_t offset, off_t len, struct fuse_file_info *fi)
970 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
971 wimlib_assert(fd->staging_fd != -1);
972 return fallocate(fd->staging_fd, mode, offset, len);
975 static int wimfs_fgetattr(const char *path, struct stat *stbuf,
976 struct fuse_file_info *fi)
978 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
979 return dentry_to_stbuf(fd->dentry, stbuf);
982 static int wimfs_ftruncate(const char *path, off_t size,
983 struct fuse_file_info *fi)
985 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
986 int ret = ftruncate(fd->staging_fd, size);
989 fd->lte->resource_entry.original_size = size;
994 * Fills in a `struct stat' that corresponds to a file or directory in the WIM.
996 static int wimfs_getattr(const char *path, struct stat *stbuf)
998 struct dentry *dentry = get_dentry(w, path);
1001 return dentry_to_stbuf(dentry, stbuf);
1004 static int wimfs_getxattr(const char *path, const char *name, char *value,
1011 /* Create a hard link */
1012 static int wimfs_link(const char *to, const char *from)
1014 struct dentry *to_dentry, *from_dentry, *from_dentry_parent;
1015 const char *link_name;
1017 to_dentry = get_dentry(w, to);
1020 if (!dentry_is_regular_file(to_dentry))
1023 from_dentry_parent = get_parent_dentry(w, from);
1024 if (!from_dentry_parent)
1026 if (!dentry_is_directory(from_dentry_parent))
1029 link_name = path_basename(from);
1030 if (get_dentry_child_with_name(from_dentry_parent, link_name))
1032 from_dentry = clone_dentry(to_dentry);
1035 if (change_dentry_name(from_dentry, link_name) != 0) {
1039 list_add(&from_dentry->link_group_list, &to_dentry->link_group_list);
1040 link_dentry(from_dentry, from_dentry_parent);
1041 dentry_increment_lookup_table_refcnts(from_dentry, w->lookup_table);
1042 from_dentry->link_group_master_status = GROUP_SLAVE;
1046 static int wimfs_listxattr(const char *path, char *list, size_t size)
1053 * Create a directory in the WIM.
1054 * @mode is currently ignored.
1056 static int wimfs_mkdir(const char *path, mode_t mode)
1058 struct dentry *parent;
1059 struct dentry *newdir;
1060 const char *basename;
1062 parent = get_parent_dentry(w, path);
1066 if (!dentry_is_directory(parent))
1069 basename = path_basename(path);
1070 if (get_dentry_child_with_name(parent, basename))
1073 newdir = new_dentry(basename);
1074 newdir->attributes |= FILE_ATTRIBUTE_DIRECTORY;
1075 link_dentry(newdir, parent);
1080 /* Creates a regular file. */
1081 static int wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
1083 const char *stream_name;
1084 if ((mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
1085 && (stream_name = path_stream_name(path))) {
1086 struct ads_entry *new_entry;
1087 struct dentry *dentry;
1089 dentry = get_dentry(w, path);
1090 if (!dentry || !dentry_is_regular_file(dentry))
1092 if (dentry_get_ads_entry(dentry, stream_name))
1094 new_entry = dentry_add_ads(dentry, stream_name);
1098 struct dentry *dentry, *parent;
1099 const char *basename;
1101 /* Make sure that the parent of @path exists and is a directory, and
1102 * that the dentry named by @path does not already exist. */
1103 parent = get_parent_dentry(w, path);
1106 if (!dentry_is_directory(parent))
1109 basename = path_basename(path);
1110 if (get_dentry_child_with_name(parent, path))
1113 dentry = new_dentry(basename);
1116 dentry->hard_link = next_link_group_id++;
1117 link_dentry(dentry, parent);
1124 static int wimfs_open(const char *path, struct fuse_file_info *fi)
1126 struct dentry *dentry;
1127 struct lookup_table_entry *lte;
1130 struct wimlib_fd *fd;
1131 unsigned stream_idx;
1133 ret = lookup_resource(w, path, get_lookup_flags(), &dentry, <e,
1139 /* Empty file with no lookup-table entry. This is fine if it's
1140 * a read-only filesystem. Otherwise we need to create a lookup
1141 * table entry so that we can keep track of the file descriptors
1142 * (this is important in case someone opens the file for
1144 if (!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
1149 ret = extract_resource_to_staging_dir(dentry, stream_idx,
1155 ret = alloc_wimlib_fd(lte, &fd);
1159 fd->dentry = dentry;
1161 /* The file resource may be in the staging directory (read-write
1162 * mounts only) or in the WIM. If it's in the staging
1163 * directory, we need to open a native file descriptor for the
1164 * corresponding file. Otherwise, we can read the file resource
1165 * directly from the WIM file if we are opening it read-only,
1166 * but we need to extract the resource to the staging directory
1167 * if we are opening it writable. */
1168 if (flags_writable(fi->flags) && !lte->staging_file_name) {
1169 ret = extract_resource_to_staging_dir(dentry, stream_idx, <e,
1170 lte->resource_entry.original_size);
1174 if (lte->staging_file_name) {
1175 fd->staging_fd = open(lte->staging_file_name, fi->flags);
1176 if (fd->staging_fd == -1) {
1177 close_wimlib_fd(fd);
1181 fi->fh = (uint64_t)fd;
1185 /* Opens a directory. */
1186 static int wimfs_opendir(const char *path, struct fuse_file_info *fi)
1188 struct dentry *dentry;
1190 dentry = get_dentry(w, path);
1193 if (!dentry_is_directory(dentry))
1195 dentry->num_times_opened++;
1196 fi->fh = (uint64_t)dentry;
1202 * Read data from a file in the WIM or in the staging directory.
1204 static int wimfs_read(const char *path, char *buf, size_t size,
1205 off_t offset, struct fuse_file_info *fi)
1207 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
1210 /* Empty file with no lookup table entry on read-only mounted
1212 wimlib_assert(!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE));
1216 if (fd->lte->staging_file_name) {
1217 /* Read from staging file */
1219 wimlib_assert(fd->staging_fd != -1);
1222 DEBUG("Seek to offset %zu", offset);
1224 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1226 ret = read(fd->staging_fd, buf, size);
1234 struct resource_entry *res_entry;
1237 res_entry = &fd->lte->resource_entry;
1239 ctype = wim_resource_compression_type(w, res_entry);
1241 if (offset > res_entry->original_size)
1244 size = min(size, res_entry->original_size - offset);
1246 if (read_resource(w->fp, res_entry->size,
1247 res_entry->original_size,
1248 res_entry->offset, ctype, size,
1255 /* Fills in the entries of the directory specified by @path using the
1256 * FUSE-provided function @filler. */
1257 static int wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
1258 off_t offset, struct fuse_file_info *fi)
1260 struct dentry *parent, *child;
1262 parent = (struct dentry*)fi->fh;
1263 child = parent->children;
1265 filler(buf, ".", NULL, 0);
1266 filler(buf, "..", NULL, 0);
1272 if (filler(buf, child->file_name_utf8, NULL, 0))
1274 child = child->next;
1275 } while (child != parent->children);
1280 static int wimfs_readlink(const char *path, char *buf, size_t buf_len)
1282 struct dentry *dentry = get_dentry(w, path);
1286 if (!dentry_is_symlink(dentry))
1289 ret = dentry_readlink(dentry, buf, buf_len, w);
1296 static int wimfs_release(const char *path, struct fuse_file_info *fi)
1299 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
1302 /* Empty file with no lookup table entry on read-only mounted
1304 wimlib_assert(!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE));
1308 if (flags_writable(fi->flags) && fd->dentry) {
1309 u64 now = get_timestamp();
1310 fd->dentry->last_access_time = now;
1311 fd->dentry->last_write_time = now;
1314 return close_wimlib_fd(fd);
1317 static int wimfs_releasedir(const char *path, struct fuse_file_info *fi)
1319 struct dentry *dentry = (struct dentry *)fi->fh;
1321 wimlib_assert(dentry->num_times_opened);
1322 if (--dentry->num_times_opened == 0)
1323 free_dentry(dentry);
1327 static int wimfs_removexattr(const char *path, const char *name)
1333 /* Renames a file or directory. See rename (3) */
1334 static int wimfs_rename(const char *from, const char *to)
1338 struct dentry *parent_of_dst;
1339 char *file_name_utf16 = NULL, *file_name_utf8 = NULL;
1340 u16 file_name_utf16_len, file_name_utf8_len;
1343 /* This rename() implementation currently only supports actual files
1344 * (not alternate data streams) */
1346 src = get_dentry(w, from);
1350 dst = get_dentry(w, to);
1353 ret = get_names(&file_name_utf16, &file_name_utf8,
1354 &file_name_utf16_len, &file_name_utf8_len,
1360 if (src == dst) /* Same file */
1363 if (!dentry_is_directory(src)) {
1364 /* Cannot rename non-directory to directory. */
1365 if (dentry_is_directory(dst))
1368 /* Cannot rename directory to a non-directory or a non-empty
1370 if (!dentry_is_directory(dst))
1372 if (dst->children != NULL)
1375 parent_of_dst = dst->parent;
1376 remove_dentry(dst, w->lookup_table);
1378 parent_of_dst = get_parent_dentry(w, to);
1383 FREE(src->file_name);
1384 FREE(src->file_name_utf8);
1385 src->file_name = file_name_utf16;
1386 src->file_name_utf8 = file_name_utf8;
1387 src->file_name_len = file_name_utf16_len;
1388 src->file_name_utf8_len = file_name_utf8_len;
1391 link_dentry(src, parent_of_dst);
1395 /* Remove a directory */
1396 static int wimfs_rmdir(const char *path)
1398 struct dentry *dentry;
1400 dentry = get_dentry(w, path);
1404 if (!dentry_is_empty_directory(dentry))
1407 unlink_dentry(dentry);
1408 if (dentry->num_times_opened == 0)
1409 free_dentry(dentry);
1413 static int wimfs_setxattr(const char *path, const char *name,
1414 const char *value, size_t size, int flags)
1420 static int wimfs_symlink(const char *to, const char *from)
1422 struct dentry *dentry_parent, *dentry;
1423 const char *link_name;
1424 struct lookup_table_entry *lte;
1426 dentry_parent = get_parent_dentry(w, from);
1429 if (!dentry_is_directory(dentry_parent))
1432 link_name = path_basename(from);
1434 if (get_dentry_child_with_name(dentry_parent, link_name))
1436 dentry = new_dentry(link_name);
1440 dentry->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
1441 dentry->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
1443 if (dentry_set_symlink(dentry, to, w->lookup_table, <e) != 0)
1444 goto out_free_dentry;
1446 dentry->ads_entries[1].lte_group_list.type = STREAM_TYPE_ADS;
1447 list_add(&dentry->ads_entries[1].lte_group_list.list,
1448 <e->lte_group_list);
1449 dentry->ads_entries[1].lte = lte;
1451 link_dentry(dentry, dentry_parent);
1454 free_dentry(dentry);
1459 /* Reduce the size of a file */
1460 static int wimfs_truncate(const char *path, off_t size)
1462 struct dentry *dentry;
1463 struct lookup_table_entry *lte;
1465 unsigned stream_idx;
1467 ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1473 if (!lte) /* Already a zero-length file */
1476 if (lte->staging_file_name) {
1477 ret = truncate(lte->staging_file_name, size);
1480 lte->resource_entry.original_size = size;
1482 /* File in WIM. Extract it to the staging directory, but only
1483 * the first @size bytes of it. */
1484 ret = extract_resource_to_staging_dir(dentry, stream_idx,
1487 dentry_update_all_timestamps(dentry);
1491 /* Remove a regular file */
1492 static int wimfs_unlink(const char *path)
1494 struct dentry *dentry;
1495 struct lookup_table_entry *lte;
1498 unsigned stream_idx;
1500 ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1506 if (stream_idx == 0) {
1507 /* We are removing the full dentry including all alternate data
1509 remove_dentry(dentry, w->lookup_table);
1511 /* We are removing an alternate data stream. */
1512 struct ads_entry *ads_entry;
1514 ads_entry = &dentry->ads_entries[stream_idx - 1];
1515 lte_decrement_refcnt(lte, w->lookup_table);
1516 list_del(&ads_entry->lte_group_list.list);
1517 dentry_remove_ads(dentry, ads_entry);
1519 /* Beware: The lookup table entry(s) may still be referenced by users
1520 * that have opened the corresponding streams. They are freed later in
1521 * wimfs_release() when the last file user has closed the stream. */
1525 /* Change the timestamp on a file dentry.
1527 * There is no distinction between a file and its alternate data streams here. */
1528 static int wimfs_utimens(const char *path, const struct timespec tv[2])
1530 struct dentry *dentry = get_dentry(w, path);
1533 time_t last_access_t = (tv[0].tv_nsec == UTIME_NOW) ?
1534 time(NULL) : tv[0].tv_sec;
1535 dentry->last_access_time = unix_timestamp_to_ms(last_access_t);
1536 time_t last_mod_t = (tv[1].tv_nsec == UTIME_NOW) ?
1537 time(NULL) : tv[1].tv_sec;
1538 dentry->last_write_time = unix_timestamp_to_ms(last_mod_t);
1542 /* Writes to a file in the WIM filesystem.
1543 * It may be an alternate data stream, but here we don't even notice because we
1544 * just get a lookup table entry. */
1545 static int wimfs_write(const char *path, const char *buf, size_t size,
1546 off_t offset, struct fuse_file_info *fi)
1548 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
1551 wimlib_assert(fd->lte);
1552 wimlib_assert(fd->lte->staging_file_name);
1553 wimlib_assert(fd->staging_fd != -1);
1555 /* Seek to the requested position */
1556 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1559 /* Write the data. */
1560 ret = write(fd->staging_fd, buf, size);
1568 static struct fuse_operations wimfs_operations = {
1569 .access = wimfs_access,
1570 .destroy = wimfs_destroy,
1571 .fallocate = wimfs_fallocate,
1572 .fgetattr = wimfs_fgetattr,
1573 .ftruncate = wimfs_ftruncate,
1574 .getattr = wimfs_getattr,
1575 .getxattr = wimfs_getxattr,
1577 .listxattr = wimfs_listxattr,
1578 .mkdir = wimfs_mkdir,
1579 .mknod = wimfs_mknod,
1581 .opendir = wimfs_opendir,
1583 .readdir = wimfs_readdir,
1584 .readlink = wimfs_readlink,
1585 .release = wimfs_release,
1586 .releasedir = wimfs_releasedir,
1587 .removexattr = wimfs_removexattr,
1588 .rename = wimfs_rename,
1589 .rmdir = wimfs_rmdir,
1590 .setxattr = wimfs_setxattr,
1591 .symlink = wimfs_symlink,
1592 .truncate = wimfs_truncate,
1593 .unlink = wimfs_unlink,
1594 .utimens = wimfs_utimens,
1595 .write = wimfs_write,
1599 /* Mounts a WIM file. */
1600 WIMLIBAPI int wimlib_mount(WIMStruct *wim, int image, const char *dir,
1608 DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
1609 wim, image, dir, flags);
1612 return WIMLIB_ERR_INVALID_PARAM;
1614 ret = wimlib_select_image(wim, image);
1619 DEBUG("Selected image %d", image);
1621 next_link_group_id = assign_link_groups(wim->image_metadata[image - 1].lgt);
1623 resolve_lookup_table_entries(wim_root_dentry(wim), w->lookup_table);
1625 if (flags & WIMLIB_MOUNT_FLAG_READWRITE)
1626 wim_get_current_image_metadata(wim)->modified = true;
1628 if (!(flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
1629 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
1630 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
1631 flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1634 working_directory = getcwd(NULL, 0);
1635 if (!working_directory) {
1636 ERROR_WITH_ERRNO("Could not determine current directory");
1637 return WIMLIB_ERR_NOTDIR;
1642 return WIMLIB_ERR_NOMEM;
1644 argv[argc++] = "mount";
1646 argv[argc++] = "-s"; /* disable multi-threaded operation */
1648 if (flags & WIMLIB_MOUNT_FLAG_DEBUG) {
1649 argv[argc++] = "-d";
1651 char optstring[256] = "use_ino";
1652 argv[argc++] = "-o";
1653 argv[argc++] = optstring;
1654 if ((flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
1656 if (!staging_dir_name) {
1658 return WIMLIB_ERR_MKDIR;
1661 strcat(optstring, ",ro");
1668 DEBUG("FUSE command line (argc = %d): ", argc);
1669 for (i = 0; i < argc; i++) {
1670 fputs(argv[i], stdout);
1678 /* Set static variables. */
1680 mount_flags = flags;
1682 ret = fuse_main(argc, argv, &wimfs_operations, NULL);
1684 return (ret == 0) ? 0 : WIMLIB_ERR_FUSE;
1689 * Unmounts the WIM file that was previously mounted on @dir by using
1692 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
1699 struct timespec timeout;
1703 /* Execute `fusermount -u', which is installed setuid root, to unmount
1706 * FUSE does not yet implement synchronous unmounts. This means that
1707 * fusermount -u will return before the filesystem daemon returns from
1708 * wimfs_destroy(). This is partly what we want, because we need to
1709 * send a message from this process to the filesystem daemon telling
1710 * whether --commit was specified or not. However, after that, the
1711 * unmount process must wait for the filesystem daemon to finish writing
1718 ERROR_WITH_ERRNO("Failed to fork()");
1719 return WIMLIB_ERR_FORK;
1722 execlp("fusermount", "fusermount", "-u", dir, NULL);
1723 ERROR_WITH_ERRNO("Failed to execute `fusermount'");
1724 return WIMLIB_ERR_FUSERMOUNT;
1727 ret = waitpid(pid, &status, 0);
1729 ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
1731 return WIMLIB_ERR_FUSERMOUNT;
1735 ERROR("fusermount exited with status %d", status);
1736 return WIMLIB_ERR_FUSERMOUNT;
1739 /* Open message queues between the unmount process and the
1740 * filesystem daemon. */
1741 ret = open_message_queues(false);
1745 /* Send a message to the filesystem saying whether to commit or
1747 msg[0] = (flags & WIMLIB_UNMOUNT_FLAG_COMMIT) ? 1 : 0;
1748 msg[1] = (flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY) ? 1 : 0;
1750 DEBUG("Sending message: %s, %s",
1751 (msg[0] == 0) ? "don't commit" : "commit",
1752 (msg[1] == 0) ? "don't check" : "check");
1753 ret = mq_send(unmount_to_daemon_mq, msg, 2, 1);
1755 ERROR("Failed to notify filesystem daemon whether we want to "
1756 "commit changes or not");
1757 close_message_queues();
1758 return WIMLIB_ERR_MQUEUE;
1761 /* Wait for a message from the filesytem daemon indicating whether the
1762 * filesystem was unmounted successfully (0) or an error occurred (1).
1763 * This may take a long time if a big WIM file needs to be rewritten. */
1765 /* Wait at most 600??? seconds before giving up and returning false.
1766 * Either it's a really big WIM file, or (more likely) the
1767 * filesystem daemon has crashed or failed for some reason.
1769 * XXX come up with some method to determine if the filesystem
1770 * daemon has really crashed or not. */
1772 gettimeofday(&now, NULL);
1773 timeout.tv_sec = now.tv_sec + 600;
1774 timeout.tv_nsec = now.tv_usec * 1000;
1776 msgsize = mq_get_msgsize(daemon_to_unmount_mq);
1777 char mailbox[msgsize];
1780 DEBUG("Waiting for message telling us whether the unmount was "
1781 "successful or not.");
1782 ret = mq_timedreceive(daemon_to_unmount_mq, mailbox, msgsize,
1785 close_message_queues();
1787 if (errno_save == ETIMEDOUT) {
1788 ERROR("Timed out- probably the filesystem daemon "
1789 "crashed and the WIM was not written "
1791 return WIMLIB_ERR_TIMEOUT;
1793 ERROR("mq_receive(): %s", strerror(errno_save));
1794 return WIMLIB_ERR_MQUEUE;
1798 DEBUG("Received message: %s",
1799 (mailbox[0] == 0) ? "Unmount OK" : "Unmount Failed");
1800 if (mailbox[0] != 0)
1801 ERROR("Unmount failed");
1805 #else /* WITH_FUSE */
1808 static inline int mount_unsupported_error()
1810 ERROR("WIMLIB was compiled with --without-fuse, which disables support "
1811 "for mounting WIMs.");
1812 return WIMLIB_ERR_UNSUPPORTED;
1815 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
1817 return mount_unsupported_error();
1820 WIMLIBAPI int wimlib_mount(WIMStruct *wim_p, int image, const char *dir,
1823 return mount_unsupported_error();
1826 #endif /* WITH_FUSE */