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 General Public License as published by the Free
17 * Software Foundation; either version 3 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 General Public License for more
25 * You should have received a copy of the GNU 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
50 #include <attr/xattr.h>
57 struct lookup_table_entry *lte;
58 struct dentry *dentry;
61 /* The WIMStruct for the mounted WIM. */
64 /* Working directory when `imagex mount' is run. */
65 static const char *working_directory;
67 /* Name of the staging directory for a read-write mount. Whenever a new file is
68 * created, it is done so in the staging directory. Furthermore, whenever a
69 * file in the WIM is modified, it is extracted to the staging directory. If
70 * changes are commited when the WIM is unmounted, the file resources are merged
71 * in from the staging directory when writing the new WIM. */
72 static char *staging_dir_name;
73 static size_t staging_dir_name_len;
75 /* Flags passed to wimlib_mount(). */
76 static int mount_flags;
78 /* Name of the directory on which the WIM file is mounted. */
79 static const char *mount_dir;
81 /* Next hard link group ID to be assigned. These are also used as the inode
83 static u64 next_link_group_id;
85 /* List of lookup table entries in the staging directory */
86 static LIST_HEAD(staging_list);
88 static inline int get_lookup_flags()
90 if (mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
91 return LOOKUP_FLAG_ADS_OK;
96 /* Returns nonzero if write permission is requested on the file open flags */
97 static inline int flags_writable(int open_flags)
99 return open_flags & (O_RDWR | O_WRONLY);
103 * Allocate a file descriptor for a lookup table entry
105 static int alloc_wimlib_fd(struct lookup_table_entry *lte,
106 struct wimlib_fd **fd_ret)
108 static const u16 fds_per_alloc = 8;
109 static const u16 max_fds = 0xffff;
111 if (lte->num_opened_fds == lte->num_allocated_fds) {
112 struct wimlib_fd **fds;
115 if (lte->num_allocated_fds == max_fds)
117 num_new_fds = min(fds_per_alloc, max_fds - lte->num_allocated_fds);
119 fds = REALLOC(lte->fds, (lte->num_allocated_fds + num_new_fds) *
120 sizeof(lte->fds[0]));
123 memset(&fds[lte->num_allocated_fds], 0,
124 num_new_fds * sizeof(fds[0]));
126 lte->num_allocated_fds += num_new_fds;
128 for (u16 i = 0; ; i++) {
130 struct wimlib_fd *fd = CALLOC(1, sizeof(*fd));
137 lte->num_opened_fds++;
144 static int close_wimlib_fd(struct wimlib_fd *fd)
146 struct lookup_table_entry *lte = fd->lte;
149 wimlib_assert(lte->num_opened_fds);
151 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
152 wimlib_assert(lte->staging_file_name);
153 wimlib_assert(fd->staging_fd != -1);
154 if (close(fd->staging_fd) != 0)
157 if (--lte->num_opened_fds == 0 && lte->refcnt == 0) {
158 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
159 wimlib_assert(lte->staging_file_name);
160 unlink(lte->staging_file_name);
162 free_lookup_table_entry(lte);
164 wimlib_assert(lte->fds[fd->idx] == fd);
165 lte->fds[fd->idx] = NULL;
170 /* Remove a dentry and all its alternate file streams */
171 static void remove_dentry(struct dentry *dentry,
172 struct lookup_table *lookup_table)
174 wimlib_assert(dentry);
175 wimlib_assert(dentry->resolved);
177 struct lookup_table_entry *lte = dentry->lte;
180 lte = lte_decrement_refcnt(lte, lookup_table);
181 if (lte && lte->num_opened_fds)
182 for (u16 i = 0; i < lte->num_allocated_fds; i++)
183 if (lte->fds[i] && lte->fds[i]->dentry == dentry)
184 lte->fds[i]->dentry = NULL;
185 if (i == dentry->num_ads)
187 lte = dentry->ads_entries[i].lte;
191 unlink_dentry(dentry);
195 static void remove_ads(struct dentry *dentry,
196 struct ads_entry *ads_entry,
197 struct lookup_table *lookup_table)
199 struct lookup_table_entry *lte;
201 wimlib_assert(dentry->resolved);
203 lte = lte_decrement_refcnt(ads_entry->lte, lookup_table);
205 list_del(&ads_entry->lte_group_list.list);
206 dentry_remove_ads(dentry, ads_entry);
209 /* Transfers file attributes from a struct dentry to a `stat' buffer. */
210 int dentry_to_stbuf(const struct dentry *dentry, struct stat *stbuf)
212 struct lookup_table_entry *lte;
214 if (dentry_is_symlink(dentry))
215 stbuf->st_mode = S_IFLNK | 0777;
216 else if (dentry_is_directory(dentry))
217 stbuf->st_mode = S_IFDIR | 0755;
219 stbuf->st_mode = S_IFREG | 0644;
221 stbuf->st_ino = (ino_t)dentry->link_group_id;
223 stbuf->st_nlink = dentry_link_group_size(dentry);
224 stbuf->st_uid = getuid();
225 stbuf->st_gid = getgid();
227 /* Use the size of the unnamed (default) file stream. */
228 lte = dentry_unnamed_lte_resolved(dentry);
230 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
231 wimlib_assert(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE);
232 wimlib_assert(lte->staging_file_name);
233 struct stat native_stat;
234 if (stat(lte->staging_file_name, &native_stat) != 0) {
235 DEBUG("Failed to stat `%s': %m",
236 lte->staging_file_name);
239 stbuf->st_size = native_stat.st_size;
241 stbuf->st_size = lte->resource_entry.original_size;
247 stbuf->st_atime = wim_timestamp_to_unix(dentry->last_access_time);
248 stbuf->st_mtime = wim_timestamp_to_unix(dentry->last_write_time);
249 stbuf->st_ctime = wim_timestamp_to_unix(dentry->creation_time);
250 stbuf->st_blocks = (stbuf->st_size + 511) / 512;
254 /* Creates a new staging file and returns its file descriptor opened for
257 * @name_ret: A location into which the a pointer to the newly allocated name of
258 * the staging file is stored.
259 * @return: The file descriptor for the new file. Returns -1 and sets errno on
260 * error, for any reason possible from the creat() function.
262 static int create_staging_file(char **name_ret, int open_flags)
270 name_len = staging_dir_name_len + 1 + SHA1_HASH_SIZE;
271 name = MALLOC(name_len + 1);
279 memcpy(name, staging_dir_name, staging_dir_name_len);
280 name[staging_dir_name_len] = '/';
281 randomize_char_array_with_alnum(name + staging_dir_name_len + 1,
283 name[name_len] = '\0';
286 /* Just in case, verify that the randomly generated name doesn't name an
287 * existing file, and try again if so */
288 } while (stat(name, &stbuf) == 0);
294 /* doesn't exist--- ok */
296 DEBUG("Creating staging file `%s'", name);
298 fd = open(name, open_flags | O_CREAT | O_TRUNC, 0600);
310 * Removes open file descriptors from a lookup table entry @old_lte where the
311 * file descriptors have opened the corresponding file resource in the context
312 * of the hard link group @link_group; these file descriptors are extracted and
313 * placed in a new lookup table entry, which is returned.
315 static struct lookup_table_entry *
316 lte_extract_fds(struct lookup_table_entry *old_lte, u64 link_group)
318 u16 num_transferred_fds;
319 struct lookup_table_entry *new_lte;
321 new_lte = new_lookup_table_entry();
325 num_transferred_fds = 0;
326 for (u16 i = 0; i < old_lte->num_allocated_fds; i++)
327 if (old_lte->fds[i] && old_lte->fds[i]->dentry &&
328 old_lte->fds[i]->dentry->link_group_id == link_group)
329 num_transferred_fds++;
330 DEBUG("Transferring %u file descriptors",
331 num_transferred_fds);
332 new_lte->fds = MALLOC(num_transferred_fds * sizeof(new_lte->fds[0]));
337 for (u16 i = 0, j = 0; ; i++) {
338 if (old_lte->fds[i] && old_lte->fds[i]->dentry &&
339 old_lte->fds[i]->dentry->link_group_id == link_group) {
340 struct wimlib_fd *fd = old_lte->fds[i];
341 old_lte->fds[i] = NULL;
344 new_lte->fds[j] = fd;
345 if (++j == num_transferred_fds)
349 DEBUG("old_lte: %u fds open; new_lte: %u fds open",
350 old_lte->num_opened_fds, new_lte->num_opened_fds);
351 old_lte->num_opened_fds -= num_transferred_fds;
352 new_lte->num_opened_fds = num_transferred_fds;
353 new_lte->num_allocated_fds = num_transferred_fds;
358 * Transfers an alternate data stream entry to a new lookup table entry
360 static void lte_transfer_ads_entry(struct lookup_table_entry *new_lte,
361 struct ads_entry *ads_entry)
363 list_del(&ads_entry->lte_group_list.list);
364 list_add(&ads_entry->lte_group_list.list, &new_lte->lte_group_list);
365 ads_entry->lte = new_lte;
369 * Transfers a dentry to a new lookup table entry
371 static void lte_transfer_dentry(struct lookup_table_entry *new_lte,
372 struct dentry *dentry)
374 wimlib_assert(dentry->lte_group_list.list.next);
375 wimlib_assert(new_lte->lte_group_list.next);
376 list_del(&dentry->lte_group_list.list);
377 list_add(&dentry->lte_group_list.list, &new_lte->lte_group_list);
378 dentry->lte = new_lte;
381 static void lte_transfer_stream_entries(struct lookup_table_entry *new_lte,
382 struct dentry *dentry,
385 INIT_LIST_HEAD(&new_lte->lte_group_list);
386 if (stream_idx == 0) {
387 struct list_head *pos = &dentry->link_group_list;
390 d = container_of(pos, struct dentry, link_group_list);
391 wimlib_assert(d->link_group_id == dentry->link_group_id);
392 lte_transfer_dentry(new_lte, d);
394 } while (pos != &dentry->link_group_list);
396 struct ads_entry *ads_entry;
397 wimlib_assert(stream_idx <= dentry->num_ads);
398 ads_entry = &dentry->ads_entries[stream_idx - 1];
399 lte_transfer_ads_entry(new_lte, ads_entry);
404 * Extract a WIM resource to the staging directory.
406 * @dentry, @stream_idx: The stream on whose behalf we are modifying the lookup
407 * table entry (these may be more streams than this that reference the lookup
410 * @lte: Pointer to pointer to the lookup table entry for the stream we need to
411 * extract, or NULL if there was no lookup table entry present for the stream
413 * @size: Number of bytes of the stream we want to extract (this supports the
414 * wimfs_truncate() function).
416 static int extract_resource_to_staging_dir(struct dentry *dentry,
418 struct lookup_table_entry **lte,
421 char *staging_file_name;
424 struct lookup_table_entry *old_lte, *new_lte;
425 size_t link_group_size;
429 * - Create a staging file for the WIM resource
430 * - Extract the resource to it
431 * - Create a new lte for the file resource
432 * - Transfer fds from the old lte to the new lte, but only if they share the
433 * same hard link group as this dentry. If there is no old lte, then this
434 * step does not need to be done
435 * - Transfer stream entries from the old lte's list to the new lte's list. If
436 * there is no old lte, we instead transfer entries for the hard link group.
438 * Note: *lte is permitted to be NULL, in which case there is no old
439 * lookup table entry.
442 DEBUG("Extracting resource `%s' to staging directory", dentry->full_path_utf8);
445 fd = create_staging_file(&staging_file_name, O_WRONLY);
450 ret = extract_wim_resource_to_fd(old_lte, fd, size);
453 if (ret != 0 || close(fd) != 0) {
459 goto out_delete_staging_file;
462 link_group_size = dentry_link_group_size(dentry);
465 if (link_group_size == old_lte->refcnt) {
466 /* This hard link group is the only user of the lookup
467 * table entry, so we can re-use it. */
468 DEBUG("Re-using lookup table entry");
469 lookup_table_unlink(w->lookup_table, old_lte);
472 DEBUG("Splitting lookup table entry "
473 "(link_group_size = %zu, lte refcnt = %u)",
474 link_group_size, old_lte->refcnt);
475 /* Split a hard link group away from the "lookup table
476 * entry" hard link group (i.e. we had two hard link
477 * groups that were identical, but now we are changing
481 * The ADS really complicate things here and not
482 * everything is going to work correctly yet. For
483 * example it could be the same that a file contains two
484 * file streams that are identical and therefore share
485 * the same lookup table entry despite the fact that the
486 * streams themselves are not hardlinked.
488 wimlib_assert(old_lte->refcnt > link_group_size);
490 new_lte = lte_extract_fds(old_lte, dentry->link_group_id);
493 goto out_delete_staging_file;
496 lte_transfer_stream_entries(new_lte, dentry, stream_idx);
497 old_lte->refcnt -= link_group_size;
500 /* No old_lte was supplied, so the resource had no lookup table
501 * entry before (it must be an empty resource) */
502 new_lte = new_lookup_table_entry();
505 goto out_delete_staging_file;
507 lte_transfer_stream_entries(new_lte, dentry, stream_idx);
509 new_lte->resource_entry.original_size = size;
510 new_lte->refcnt = link_group_size;
511 random_hash(new_lte->hash);
512 if (new_lte->staging_file_name)
513 FREE(new_lte->staging_file_name);
514 new_lte->staging_file_name = staging_file_name;
515 new_lte->resource_location = RESOURCE_IN_STAGING_FILE;
517 lookup_table_insert(w->lookup_table, new_lte);
518 list_add(&new_lte->staging_list, &staging_list);
521 out_delete_staging_file:
522 unlink(staging_file_name);
523 FREE(staging_file_name);
528 * Creates a randomly named staging directory and returns its name into the
529 * static variable staging_dir_name.
531 * If the staging directory cannot be created, staging_dir_name is set to NULL.
533 static void make_staging_dir()
535 /* XXX Give the user an option of where to stage files */
537 static char prefix[] = "wimlib-staging-";
538 static const size_t prefix_len = 15;
539 static const size_t suffix_len = 10;
541 size_t pwd_len = strlen(working_directory);
543 staging_dir_name_len = pwd_len + 1 + prefix_len + suffix_len;
545 staging_dir_name = MALLOC(staging_dir_name_len + 1);
546 if (!staging_dir_name) {
547 ERROR("Out of memory");
551 memcpy(staging_dir_name, working_directory, pwd_len);
552 staging_dir_name[pwd_len] = '/';
553 memcpy(staging_dir_name + pwd_len + 1, prefix, prefix_len);
554 randomize_char_array_with_alnum(staging_dir_name + pwd_len + 1 + prefix_len,
556 staging_dir_name[staging_dir_name_len] = '\0';
558 if (mkdir(staging_dir_name, 0700) != 0) {
559 ERROR_WITH_ERRNO("Failed to create temporary directory `%s'",
561 FREE(staging_dir_name);
562 staging_dir_name = NULL;
566 static int remove_file_or_directory(const char *fpath, const struct stat *sb,
567 int typeflag, struct FTW *ftwbuf)
569 if (remove(fpath) == 0)
572 return WIMLIB_ERR_DELETE_STAGING_DIR;
577 * Deletes the staging directory and all the files contained in it.
579 static inline int delete_staging_dir()
583 ret = nftw(staging_dir_name, remove_file_or_directory,10, FTW_DEPTH);
584 staging_dir_name = NULL;
588 /* Name and message queue descriptors for message queues between the filesystem
589 * daemon process and the unmount process. These are used when the filesystem
590 * is unmounted and the process running wimlib_mount() (i.e. the `imagex
591 * unmount' command) needs to communicate with the filesystem daemon running
592 * fuse_main() (i.e. that spawned by the `imagex mount' or `imagex mountrw'
594 static char *unmount_to_daemon_mq_name;
595 static char *daemon_to_unmount_mq_name;
596 static mqd_t unmount_to_daemon_mq;
597 static mqd_t daemon_to_unmount_mq;
599 /* Simple function that returns the concatenation of 4 strings. */
600 static char *strcat_dup(const char *s1, const char *s2, const char *s3,
603 size_t len = strlen(s1) + strlen(s2) + strlen(s3) + strlen(s4) + 1;
604 char *p = MALLOC(len);
615 /* Removes trailing forward slashes in a string. */
616 static void remove_trailing_slashes(char *s)
618 long len = strlen(s);
619 for (long i = len - 1; i >= 1; i--) {
627 /* Changes forward slashes to underscores in a string. */
628 static void s_slashes_underscores_g(char *s)
638 * Opens two POSIX message queue: one for sending messages from the unmount
639 * process to the daemon process, and one to go the other way. The names of the
640 * message queues, which must be system-wide unique, are be based on the mount
641 * point. (There of course is still a possibility of a collision if one were to
642 * unmount two identically named directories simultaneously...)
644 * @daemon specifies whether the calling process is the filesystem daemon or the
647 static int open_message_queues(bool daemon)
649 static const char *slash = "/";
650 static const char *prefix = "wimlib-";
651 static const char *u2d_suffix = "unmount-to-daemon-mq";
652 static const char *d2u_suffix = "daemon-to-unmount-mq";
654 const char *mount_dir_basename = path_basename(mount_dir);
658 unmount_to_daemon_mq_name = strcat_dup(slash, mount_dir_basename,
660 if (!unmount_to_daemon_mq_name) {
661 ERROR("Out of memory");
662 return WIMLIB_ERR_NOMEM;
664 daemon_to_unmount_mq_name = strcat_dup(slash, mount_dir_basename,
666 if (!daemon_to_unmount_mq_name) {
667 ERROR("Out of memory");
668 ret = WIMLIB_ERR_NOMEM;
672 remove_trailing_slashes(unmount_to_daemon_mq_name);
673 remove_trailing_slashes(daemon_to_unmount_mq_name);
674 s_slashes_underscores_g(unmount_to_daemon_mq_name + 1);
675 s_slashes_underscores_g(daemon_to_unmount_mq_name + 1);
678 flags = O_RDONLY | O_CREAT;
680 flags = O_WRONLY | O_CREAT;
682 unmount_to_daemon_mq = mq_open(unmount_to_daemon_mq_name, flags,
685 if (unmount_to_daemon_mq == (mqd_t)-1) {
686 ERROR_WITH_ERRNO("mq_open()");
687 ret = WIMLIB_ERR_MQUEUE;
692 flags = O_WRONLY | O_CREAT;
694 flags = O_RDONLY | O_CREAT;
696 daemon_to_unmount_mq = mq_open(daemon_to_unmount_mq_name, flags,
699 if (daemon_to_unmount_mq == (mqd_t)-1) {
700 ERROR_WITH_ERRNO("mq_open()");
701 ret = WIMLIB_ERR_MQUEUE;
706 mq_close(unmount_to_daemon_mq);
707 mq_unlink(unmount_to_daemon_mq_name);
709 FREE(daemon_to_unmount_mq_name);
711 FREE(unmount_to_daemon_mq_name);
715 static int mq_get_msgsize(mqd_t mq)
717 static const char *msgsize_max_file = "/proc/sys/fs/mqueue/msgsize_max";
722 if (mq_getattr(unmount_to_daemon_mq, &attr) == 0) {
723 msgsize = attr.mq_msgsize;
725 ERROR_WITH_ERRNO("mq_getattr()");
726 ERROR("Attempting to read %s", msgsize_max_file);
727 fp = fopen(msgsize_max_file, "rb");
729 if (fscanf(fp, "%d", &msgsize) != 1) {
730 ERROR("Assuming message size of 8192");
735 ERROR_WITH_ERRNO("Failed to open the file `%s'",
737 ERROR("Assuming message size of 8192");
744 /* Closes the message queues, which are allocated in static variables */
745 static void close_message_queues()
747 mq_close(unmount_to_daemon_mq);
748 mq_close(daemon_to_unmount_mq);
749 mq_unlink(unmount_to_daemon_mq_name);
750 mq_unlink(daemon_to_unmount_mq_name);
753 static int wimfs_access(const char *path, int mask)
755 /* XXX Permissions not implemented */
759 /* Closes the staging file descriptor associated with the lookup table entry, if
761 static int close_lte_fds(struct lookup_table_entry *lte)
763 for (u16 i = 0, j = 0; j < lte->num_opened_fds; i++) {
764 if (lte->fds[i] && lte->fds[i]->staging_fd != -1) {
765 wimlib_assert(lte->resource_location == RESOURCE_IN_STAGING_FILE);
766 wimlib_assert(lte->staging_file_name);
767 if (close(lte->fds[i]->staging_fd) != 0) {
768 ERROR_WITH_ERRNO("Failed close file `%s'",
769 lte->staging_file_name);
770 return WIMLIB_ERR_WRITE;
778 static void lte_list_change_lte_ptr(struct lookup_table_entry *lte,
779 struct lookup_table_entry *newptr)
781 struct list_head *pos;
782 struct stream_list_head *head;
783 list_for_each(pos, <e->lte_group_list) {
784 head = container_of(pos, struct stream_list_head, list);
785 if (head->type == STREAM_TYPE_ADS) {
786 struct ads_entry *ads_entry;
787 ads_entry = container_of(head, struct ads_entry, lte_group_list);
789 ads_entry->lte = newptr;
791 wimlib_assert(head->type == STREAM_TYPE_NORMAL);
793 struct dentry *dentry;
794 dentry = container_of(head, struct dentry, lte_group_list);
796 dentry->lte = newptr;
802 static int update_lte_of_staging_file(struct lookup_table_entry *lte,
803 struct lookup_table *table)
805 struct lookup_table_entry *duplicate_lte;
807 u8 hash[SHA1_HASH_SIZE];
810 wimlib_assert(lte->resource_location == RESOURCE_IN_STAGING_FILE);
811 wimlib_assert(lte->staging_file_name);
813 ret = sha1sum(lte->staging_file_name, hash);
817 lookup_table_unlink(table, lte);
819 duplicate_lte = __lookup_resource(table, hash);
822 /* Merge duplicate lookup table entries */
824 lte_list_change_lte_ptr(lte, duplicate_lte);
825 duplicate_lte->refcnt += lte->refcnt;
826 list_splice(<e->lte_group_list,
827 &duplicate_lte->lte_group_list);
829 free_lookup_table_entry(lte);
831 if (stat(lte->staging_file_name, &stbuf) != 0) {
832 ERROR_WITH_ERRNO("Failed to stat `%s'", lte->staging_file_name);
833 return WIMLIB_ERR_STAT;
835 wimlib_assert(<e->file_on_disk == <e->staging_file_name);
836 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
837 copy_hash(lte->hash, hash);
838 lte->resource_entry.original_size = stbuf.st_size;
839 lte->resource_entry.size = stbuf.st_size;
840 lookup_table_insert(table, lte);
846 /* Overwrites the WIM file, with changes saved. */
847 static int rebuild_wim(WIMStruct *w, bool check_integrity)
850 struct lookup_table_entry *lte, *tmp;
852 /* Close all the staging file descriptors. */
853 DEBUG("Closing all staging file descriptors.");
854 list_for_each_entry(lte, &staging_list, staging_list) {
855 ret = close_lte_fds(lte);
860 /* Calculate SHA1 checksums for all staging files, and merge unnecessary
861 * lookup table entries. */
862 DEBUG("Calculating SHA1 checksums for all new staging files.");
863 list_for_each_entry_safe(lte, tmp, &staging_list, staging_list) {
864 ret = update_lte_of_staging_file(lte, w->lookup_table);
869 xml_update_image_info(w, w->current_image);
871 ret = wimlib_overwrite(w, check_integrity);
873 ERROR("Failed to commit changes");
879 /* Called when the filesystem is unmounted. */
880 static void wimfs_destroy(void *p)
882 /* For read-write mounts, the `imagex unmount' command, which is
883 * running in a separate process and is executing the
884 * wimlib_unmount() function, will send this process a byte
885 * through a message queue that indicates whether the --commit
886 * option was specified or not. */
889 struct timespec timeout;
891 ssize_t bytes_received;
894 char check_integrity;
897 ret = open_message_queues(true);
901 msgsize = mq_get_msgsize(unmount_to_daemon_mq);
906 /* Wait at most 3 seconds before giving up and discarding changes. */
907 gettimeofday(&now, NULL);
908 timeout.tv_sec = now.tv_sec + 3;
909 timeout.tv_nsec = now.tv_usec * 1000;
910 DEBUG("Waiting for message telling us whether to commit or not, and "
911 "whether to include integrity checks.");
913 bytes_received = mq_timedreceive(unmount_to_daemon_mq, msg,
914 msgsize, NULL, &timeout);
916 check_integrity = msg[1];
917 if (bytes_received == -1) {
918 if (errno == ETIMEDOUT) {
921 ERROR_WITH_ERRNO("mq_timedreceive()");
923 ERROR("Not committing.");
925 DEBUG("Received message: [%d %d]", msg[0], msg[1]);
929 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
931 status = chdir(working_directory);
933 ERROR_WITH_ERRNO("chdir()");
934 status = WIMLIB_ERR_NOTDIR;
937 status = rebuild_wim(w, (check_integrity != 0));
939 ret = delete_staging_dir();
941 ERROR_WITH_ERRNO("Failed to delete the staging "
947 DEBUG("Read-only mount");
950 DEBUG("Sending status %u", status);
951 ret = mq_send(daemon_to_unmount_mq, &status, 1, 1);
953 ERROR_WITH_ERRNO("Failed to send status to unmount process");
954 close_message_queues();
958 static int wimfs_fallocate(const char *path, int mode,
959 off_t offset, off_t len, struct fuse_file_info *fi)
961 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
962 wimlib_assert(fd->staging_fd != -1);
963 return fallocate(fd->staging_fd, mode, offset, len);
966 static int wimfs_fgetattr(const char *path, struct stat *stbuf,
967 struct fuse_file_info *fi)
969 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
970 return dentry_to_stbuf(fd->dentry, stbuf);
974 static int wimfs_ftruncate(const char *path, off_t size,
975 struct fuse_file_info *fi)
977 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
978 int ret = ftruncate(fd->staging_fd, size);
981 fd->lte->resource_entry.original_size = size;
986 * Fills in a `struct stat' that corresponds to a file or directory in the WIM.
988 static int wimfs_getattr(const char *path, struct stat *stbuf)
990 struct dentry *dentry;
993 ret = lookup_resource(w, path,
994 get_lookup_flags() | LOOKUP_FLAG_DIRECTORY_OK,
995 &dentry, NULL, NULL);
998 return dentry_to_stbuf(dentry, stbuf);
1002 /* Read an alternate data stream through the XATTR interface, or get its size */
1003 static int wimfs_getxattr(const char *path, const char *name, char *value,
1007 struct dentry *dentry;
1008 struct ads_entry *ads_entry;
1010 struct lookup_table_entry *lte;
1012 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1015 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1019 dentry = get_dentry(w, path);
1022 ads_entry = dentry_get_ads_entry(dentry, name);
1026 lte = ads_entry->lte;
1027 res_size = wim_resource_size(lte);
1031 if (res_size > size)
1033 ret = read_full_wim_resource(lte, (u8*)value);
1040 /* Create a hard link */
1041 static int wimfs_link(const char *to, const char *from)
1043 struct dentry *to_dentry, *from_dentry, *from_dentry_parent;
1044 const char *link_name;
1046 to_dentry = get_dentry(w, to);
1049 if (!dentry_is_regular_file(to_dentry))
1052 from_dentry_parent = get_parent_dentry(w, from);
1053 if (!from_dentry_parent)
1055 if (!dentry_is_directory(from_dentry_parent))
1058 link_name = path_basename(from);
1059 if (get_dentry_child_with_name(from_dentry_parent, link_name))
1061 from_dentry = clone_dentry(to_dentry);
1064 if (change_dentry_name(from_dentry, link_name) != 0) {
1069 /* Add the new dentry to the dentry list for the link group */
1070 list_add(&from_dentry->link_group_list, &to_dentry->link_group_list);
1072 /* Increment reference counts for the unnamed file stream and all
1073 * alternate data streams. */
1074 if (from_dentry->lte) {
1075 list_add(&from_dentry->lte_group_list.list,
1076 &to_dentry->lte_group_list.list);
1077 from_dentry->lte->refcnt++;
1079 for (u16 i = 0; i < from_dentry->num_ads; i++) {
1080 struct ads_entry *ads_entry = &from_dentry->ads_entries[i];
1082 ads_entry->lte->refcnt++;
1085 /* The ADS entries are owned by another dentry. */
1086 from_dentry->ads_entries_status = ADS_ENTRIES_USER;
1088 link_dentry(from_dentry, from_dentry_parent);
1093 static int wimfs_listxattr(const char *path, char *list, size_t size)
1095 struct dentry *dentry;
1100 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1103 /* List alternate data streams, or get the list size */
1105 ret = lookup_resource(w, path, get_lookup_flags(), &dentry, NULL, NULL);
1110 for (i = 0; i < dentry->num_ads; i++)
1111 needed_size += dentry->ads_entries[i].stream_name_utf8_len + 6;
1114 for (i = 0; i < dentry->num_ads; i++) {
1115 needed_size = dentry->ads_entries[i].stream_name_utf8_len + 6;
1116 if (needed_size > size)
1118 p += sprintf(p, "user.%s",
1119 dentry->ads_entries[i].stream_name_utf8) + 1;
1120 size -= needed_size;
1128 * Create a directory in the WIM.
1129 * @mode is currently ignored.
1131 static int wimfs_mkdir(const char *path, mode_t mode)
1133 struct dentry *parent;
1134 struct dentry *newdir;
1135 const char *basename;
1137 parent = get_parent_dentry(w, path);
1141 if (!dentry_is_directory(parent))
1144 basename = path_basename(path);
1145 if (get_dentry_child_with_name(parent, basename))
1148 newdir = new_dentry(basename);
1149 newdir->attributes |= FILE_ATTRIBUTE_DIRECTORY;
1150 newdir->resolved = true;
1151 newdir->link_group_id = next_link_group_id++;
1152 link_dentry(newdir, parent);
1157 /* Creates a regular file. */
1158 static int wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
1160 const char *stream_name;
1161 if ((mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
1162 && (stream_name = path_stream_name(path))) {
1163 /* Make an alternate data stream */
1164 struct ads_entry *new_entry;
1165 struct dentry *dentry;
1167 char *p = (char*)stream_name - 1;
1168 wimlib_assert(*p == ':');
1171 dentry = get_dentry(w, path);
1172 if (!dentry || !dentry_is_regular_file(dentry))
1174 if (dentry_get_ads_entry(dentry, stream_name))
1176 new_entry = dentry_add_ads(dentry, stream_name);
1180 struct dentry *dentry, *parent;
1181 const char *basename;
1183 /* Make a normal file (not an alternate data stream) */
1185 /* Make sure that the parent of @path exists and is a directory, and
1186 * that the dentry named by @path does not already exist. */
1187 parent = get_parent_dentry(w, path);
1190 if (!dentry_is_directory(parent))
1193 basename = path_basename(path);
1194 if (get_dentry_child_with_name(parent, path))
1197 dentry = new_dentry(basename);
1200 dentry->resolved = true;
1201 dentry->link_group_id = next_link_group_id++;
1202 dentry->lte_group_list.type = STREAM_TYPE_NORMAL;
1203 INIT_LIST_HEAD(&dentry->lte_group_list.list);
1204 link_dentry(dentry, parent);
1211 static int wimfs_open(const char *path, struct fuse_file_info *fi)
1213 struct dentry *dentry;
1214 struct lookup_table_entry *lte;
1216 struct wimlib_fd *fd;
1217 unsigned stream_idx;
1219 ret = lookup_resource(w, path, get_lookup_flags(), &dentry, <e,
1225 /* Empty file with no lookup-table entry. This is fine if it's
1226 * a read-only filesystem. Otherwise we need to create a lookup
1227 * table entry so that we can keep track of the file descriptors
1228 * (this is important in case someone opens the file for
1230 if (!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
1235 ret = extract_resource_to_staging_dir(dentry, stream_idx,
1241 ret = alloc_wimlib_fd(lte, &fd);
1245 fd->dentry = dentry;
1247 /* The file resource may be in the staging directory (read-write
1248 * mounts only) or in the WIM. If it's in the staging
1249 * directory, we need to open a native file descriptor for the
1250 * corresponding file. Otherwise, we can read the file resource
1251 * directly from the WIM file if we are opening it read-only,
1252 * but we need to extract the resource to the staging directory
1253 * if we are opening it writable. */
1254 if (flags_writable(fi->flags) &&
1255 lte->resource_location != RESOURCE_IN_STAGING_FILE) {
1256 ret = extract_resource_to_staging_dir(dentry, stream_idx, <e,
1257 lte->resource_entry.original_size);
1261 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1262 fd->staging_fd = open(lte->staging_file_name, fi->flags);
1263 if (fd->staging_fd == -1) {
1264 close_wimlib_fd(fd);
1268 fi->fh = (uintptr_t)fd;
1272 /* Opens a directory. */
1273 static int wimfs_opendir(const char *path, struct fuse_file_info *fi)
1275 struct dentry *dentry;
1277 dentry = get_dentry(w, path);
1280 if (!dentry_is_directory(dentry))
1282 dentry->num_times_opened++;
1283 fi->fh = (uintptr_t)dentry;
1289 * Read data from a file in the WIM or in the staging directory.
1291 static int wimfs_read(const char *path, char *buf, size_t size,
1292 off_t offset, struct fuse_file_info *fi)
1294 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1297 /* Empty file with no lookup table entry on read-only mounted
1299 wimlib_assert(!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE));
1303 wimlib_assert(fd->lte);
1305 if (fd->lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1306 /* Read from staging file */
1308 wimlib_assert(fd->lte->staging_file_name);
1309 wimlib_assert(fd->staging_fd != -1);
1312 DEBUG("Seek to offset %zu", offset);
1314 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1316 ret = read(fd->staging_fd, buf, size);
1323 const struct resource_entry *res_entry;
1325 res_entry = &fd->lte->resource_entry;
1327 if (offset > res_entry->original_size)
1330 size = min(size, res_entry->original_size - offset);
1332 if (read_wim_resource(fd->lte, (u8*)buf,
1333 size, offset, false) != 0)
1339 /* Fills in the entries of the directory specified by @path using the
1340 * FUSE-provided function @filler. */
1341 static int wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
1342 off_t offset, struct fuse_file_info *fi)
1344 struct dentry *parent, *child;
1346 parent = (struct dentry*)(uintptr_t)fi->fh;
1347 wimlib_assert(parent);
1348 child = parent->children;
1350 filler(buf, ".", NULL, 0);
1351 filler(buf, "..", NULL, 0);
1357 if (filler(buf, child->file_name_utf8, NULL, 0))
1359 child = child->next;
1360 } while (child != parent->children);
1365 static int wimfs_readlink(const char *path, char *buf, size_t buf_len)
1367 struct dentry *dentry = get_dentry(w, path);
1371 if (!dentry_is_symlink(dentry))
1374 ret = dentry_readlink(dentry, buf, buf_len, w);
1380 /* Creation time, write time, access time */
1382 dentry_link_group_set_times(struct dentry *dentry, u64 times[3])
1384 struct dentry *cur = dentry;
1387 cur->creation_time = times[0];
1389 cur->last_write_time = times[1];
1391 cur->last_access_time = times[2];
1392 } while ((cur = container_of(dentry->link_group_list.next,
1394 link_group_list)) != dentry);
1398 dentry_link_group_update_times(struct dentry *dentry)
1400 u64 now = get_wim_timestamp();
1401 u64 times[3] = {now, now, now};
1402 dentry_link_group_set_times(dentry, times);
1406 static int wimfs_release(const char *path, struct fuse_file_info *fi)
1408 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1411 /* Empty file with no lookup table entry on read-only mounted
1413 wimlib_assert(!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE));
1417 return close_wimlib_fd(fd);
1420 static int wimfs_releasedir(const char *path, struct fuse_file_info *fi)
1422 struct dentry *dentry = (struct dentry *)(uintptr_t)fi->fh;
1424 wimlib_assert(dentry);
1425 wimlib_assert(dentry->num_times_opened);
1426 if (--dentry->num_times_opened == 0)
1427 free_dentry(dentry);
1432 /* Remove an alternate data stream through the XATTR interface */
1433 static int wimfs_removexattr(const char *path, const char *name)
1435 struct dentry *dentry;
1436 struct ads_entry *ads_entry;
1437 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1440 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1444 dentry = get_dentry(w, path);
1448 ads_entry = dentry_get_ads_entry(dentry, name);
1451 remove_ads(dentry, ads_entry, w->lookup_table);
1456 /* Renames a file or directory. See rename (3) */
1457 static int wimfs_rename(const char *from, const char *to)
1461 struct dentry *parent_of_dst;
1462 char *file_name_utf16 = NULL, *file_name_utf8 = NULL;
1463 u16 file_name_utf16_len, file_name_utf8_len;
1466 /* This rename() implementation currently only supports actual files
1467 * (not alternate data streams) */
1469 src = get_dentry(w, from);
1473 dst = get_dentry(w, to);
1476 ret = get_names(&file_name_utf16, &file_name_utf8,
1477 &file_name_utf16_len, &file_name_utf8_len,
1483 /* Destination file exists */
1485 if (src == dst) /* Same file */
1488 if (!dentry_is_directory(src)) {
1489 /* Cannot rename non-directory to directory. */
1490 if (dentry_is_directory(dst))
1493 /* Cannot rename directory to a non-directory or a non-empty
1495 if (!dentry_is_directory(dst))
1497 if (dst->children != NULL)
1500 parent_of_dst = dst->parent;
1501 remove_dentry(dst, w->lookup_table);
1503 /* Destination does not exist */
1504 parent_of_dst = get_parent_dentry(w, to);
1508 if (!dentry_is_directory(parent_of_dst))
1512 FREE(src->file_name);
1513 FREE(src->file_name_utf8);
1514 src->file_name = file_name_utf16;
1515 src->file_name_utf8 = file_name_utf8;
1516 src->file_name_len = file_name_utf16_len;
1517 src->file_name_utf8_len = file_name_utf8_len;
1520 link_dentry(src, parent_of_dst);
1524 /* Remove a directory */
1525 static int wimfs_rmdir(const char *path)
1527 struct dentry *dentry;
1529 dentry = get_dentry(w, path);
1533 if (!dentry_is_empty_directory(dentry))
1536 unlink_dentry(dentry);
1537 if (dentry->num_times_opened == 0)
1538 free_dentry(dentry);
1543 /* Write an alternate data stream through the XATTR interface */
1544 static int wimfs_setxattr(const char *path, const char *name,
1545 const char *value, size_t size, int flags)
1547 struct dentry *dentry;
1548 struct ads_entry *existing_ads_entry;
1549 struct ads_entry *new_ads_entry;
1550 struct lookup_table_entry *existing_lte;
1551 struct lookup_table_entry *lte;
1552 u8 value_hash[SHA1_HASH_SIZE];
1554 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1557 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1561 dentry = get_dentry(w, path);
1564 existing_ads_entry = dentry_get_ads_entry(dentry, name);
1565 if (existing_ads_entry) {
1566 if (flags & XATTR_CREATE)
1568 remove_ads(dentry, existing_ads_entry, w->lookup_table);
1570 if (flags & XATTR_REPLACE)
1573 new_ads_entry = dentry_add_ads(dentry, name);
1577 sha1_buffer((const u8*)value, size, value_hash);
1579 existing_lte = __lookup_resource(w->lookup_table, value_hash);
1586 lte = new_lookup_table_entry();
1589 value_copy = MALLOC(size);
1594 memcpy(value_copy, value, size);
1595 lte->resource_location = RESOURCE_IN_ATTACHED_BUFFER;
1596 lte->attached_buffer = value_copy;
1597 lte->resource_entry.original_size = size;
1598 lte->resource_entry.size = size;
1599 lte->resource_entry.flags = 0;
1600 copy_hash(lte->hash, value_hash);
1601 lookup_table_insert(w->lookup_table, lte);
1603 new_ads_entry->lte = lte;
1608 static int wimfs_symlink(const char *to, const char *from)
1610 struct dentry *dentry_parent, *dentry;
1611 const char *link_name;
1612 struct lookup_table_entry *lte;
1614 dentry_parent = get_parent_dentry(w, from);
1617 if (!dentry_is_directory(dentry_parent))
1620 link_name = path_basename(from);
1622 if (get_dentry_child_with_name(dentry_parent, link_name))
1624 dentry = new_dentry(link_name);
1628 dentry->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
1629 dentry->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
1630 dentry->link_group_id = next_link_group_id++;
1632 if (dentry_set_symlink(dentry, to, w->lookup_table, <e) != 0)
1633 goto out_free_dentry;
1637 dentry->ads_entries[1].lte_group_list.type = STREAM_TYPE_ADS;
1638 list_add(&dentry->ads_entries[1].lte_group_list.list,
1639 <e->lte_group_list);
1640 wimlib_assert(dentry->resolved);
1642 link_dentry(dentry, dentry_parent);
1645 free_dentry(dentry);
1650 /* Reduce the size of a file */
1651 static int wimfs_truncate(const char *path, off_t size)
1653 struct dentry *dentry;
1654 struct lookup_table_entry *lte;
1656 unsigned stream_idx;
1658 ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1664 if (!lte) /* Already a zero-length file */
1667 if (lte->staging_file_name) {
1668 ret = truncate(lte->staging_file_name, size);
1671 lte->resource_entry.original_size = size;
1673 /* File in WIM. Extract it to the staging directory, but only
1674 * the first @size bytes of it. */
1675 ret = extract_resource_to_staging_dir(dentry, stream_idx,
1678 dentry_link_group_update_times(dentry);
1682 /* Remove a regular file */
1683 static int wimfs_unlink(const char *path)
1685 struct dentry *dentry;
1686 struct lookup_table_entry *lte;
1688 unsigned stream_idx;
1690 ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1696 if (stream_idx == 0) {
1697 /* We are removing the full dentry including all alternate data
1699 remove_dentry(dentry, w->lookup_table);
1701 /* We are removing an alternate data stream. */
1702 remove_ads(dentry, &dentry->ads_entries[stream_idx - 1],
1705 /* Beware: The lookup table entry(s) may still be referenced by users
1706 * that have opened the corresponding streams. They are freed later in
1707 * wimfs_release() when the last file user has closed the stream. */
1711 #ifdef HAVE_UTIMENSAT
1713 * Change the timestamp on a file dentry.
1715 * Note that alternate data streams do not have their own timestamps.
1717 static int wimfs_utimens(const char *path, const struct timespec tv[2])
1719 struct dentry *dentry = get_dentry(w, path);
1722 u64 times[3] = {0, 0, 0};
1723 if (tv[0].tv_nsec != UTIME_OMIT) {
1724 if (tv[0].tv_nsec == UTIME_NOW)
1725 times[2] = get_wim_timestamp();
1727 times[2] = timespec_to_wim_timestamp(&tv[0]);
1729 if (tv[1].tv_nsec != UTIME_OMIT) {
1730 if (tv[1].tv_nsec == UTIME_NOW)
1731 times[1] = get_wim_timestamp();
1733 times[1] = timespec_to_wim_timestamp(&tv[1]);
1735 dentry_link_group_set_times(dentry, times);
1739 static int wimfs_utime(const char *path, struct utimbuf *times)
1741 struct dentry *dentry = get_dentry(w, path);
1746 wim_times[1] = unix_timestamp_to_wim(times->modtime);
1747 wim_times[2] = unix_timestamp_to_wim(times->actime);
1748 dentry_link_group_set_times(dentry, wim_times);
1753 /* Writes to a file in the WIM filesystem.
1754 * It may be an alternate data stream, but here we don't even notice because we
1755 * just get a lookup table entry. */
1756 static int wimfs_write(const char *path, const char *buf, size_t size,
1757 off_t offset, struct fuse_file_info *fi)
1759 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1763 wimlib_assert(fd->lte);
1764 wimlib_assert(fd->lte->staging_file_name);
1765 wimlib_assert(fd->staging_fd != -1);
1767 /* Seek to the requested position */
1768 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1771 /* Write the data. */
1772 ret = write(fd->staging_fd, buf, size);
1777 u64 now = get_wim_timestamp();
1778 u64 times[3] = {0, now, now};
1779 dentry_link_group_set_times(fd->dentry, times);
1785 static struct fuse_operations wimfs_operations = {
1786 .access = wimfs_access,
1787 .destroy = wimfs_destroy,
1789 .fallocate = wimfs_fallocate,
1790 .fgetattr = wimfs_fgetattr,
1792 .ftruncate = wimfs_ftruncate,
1793 .getattr = wimfs_getattr,
1795 .getxattr = wimfs_getxattr,
1799 .listxattr = wimfs_listxattr,
1801 .mkdir = wimfs_mkdir,
1802 .mknod = wimfs_mknod,
1804 .opendir = wimfs_opendir,
1806 .readdir = wimfs_readdir,
1807 .readlink = wimfs_readlink,
1808 .release = wimfs_release,
1809 .releasedir = wimfs_releasedir,
1811 .removexattr = wimfs_removexattr,
1813 .rename = wimfs_rename,
1814 .rmdir = wimfs_rmdir,
1816 .setxattr = wimfs_setxattr,
1818 .symlink = wimfs_symlink,
1819 .truncate = wimfs_truncate,
1820 .unlink = wimfs_unlink,
1821 #ifdef HAVE_UTIMENSAT
1822 .utimens = wimfs_utimens,
1824 .utime = wimfs_utime,
1826 .write = wimfs_write,
1830 static int check_lte_refcnt(struct lookup_table_entry *lte, void *ignore)
1832 size_t lte_group_size = 0;
1833 struct list_head *cur;
1834 list_for_each(cur, <e->lte_group_list)
1836 if (lte_group_size > lte->refcnt) {
1837 #ifdef ENABLE_ERROR_MESSAGES
1838 struct dentry *example_dentry;
1839 struct list_head *next;
1840 struct stream_list_head *head;
1841 WARNING("The following lookup table entry has a reference count "
1842 "of %u, but", lte->refcnt);
1843 WARNING("We found %zu references to it", lte_group_size);
1844 next = lte->lte_group_list.next;
1845 head = container_of(next, struct stream_list_head, list);
1846 if (head->type == STREAM_TYPE_NORMAL) {
1847 example_dentry = container_of(head, struct dentry,
1849 WARNING("(One dentry referencing it is at `%s')",
1850 example_dentry->full_path_utf8);
1852 print_lookup_table_entry(lte);
1854 /* Guess what! install.wim for Windows 8 contains a stream with
1855 * 2 dentries referencing it, but the lookup table entry has
1856 * reference count of 1. So we will need to handle this case
1857 * and not just make it be an error... I'm just setting the
1858 * reference count to the number of references we found. */
1861 lte->refcnt = lte_group_size;
1862 WARNING("Fixing reference count");
1864 return WIMLIB_ERR_INVALID_DENTRY;
1870 /* Mounts a WIM file. */
1871 WIMLIBAPI int wimlib_mount(WIMStruct *wim, int image, const char *dir,
1872 int flags, WIMStruct **additional_swms,
1873 unsigned num_additional_swms)
1879 struct lookup_table *joined_tab, *wim_tab_save;
1881 DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
1882 wim, image, dir, flags);
1885 return WIMLIB_ERR_INVALID_PARAM;
1887 ret = verify_swm_set(wim, additional_swms, num_additional_swms);
1891 if (num_additional_swms) {
1892 ret = new_joined_lookup_table(wim, additional_swms,
1893 num_additional_swms,
1897 wim_tab_save = wim->lookup_table;
1898 wim->lookup_table = joined_tab;
1901 ret = wimlib_select_image(wim, image);
1906 DEBUG("Selected image %d", image);
1908 next_link_group_id = assign_link_group_ids(wim->image_metadata[image - 1].lgt);
1910 DEBUG("Resolving lookup table entries");
1911 /* Resolve all the lookup table entries of the dentry tree */
1912 for_dentry_in_tree(wim_root_dentry(wim), dentry_resolve_ltes,
1915 DEBUG("Checking lookup table entry reference counts");
1917 ret = for_lookup_table_entry(wim->lookup_table, check_lte_refcnt, NULL);
1921 if (flags & WIMLIB_MOUNT_FLAG_READWRITE)
1922 wim_get_current_image_metadata(wim)->modified = true;
1924 if (!(flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
1925 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
1926 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
1927 flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1929 DEBUG("Getting current directory");
1932 working_directory = getcwd(NULL, 0);
1933 if (!working_directory) {
1934 ERROR_WITH_ERRNO("Could not determine current directory");
1935 ret = WIMLIB_ERR_NOTDIR;
1939 DEBUG("Closing POSIX message queues");
1940 /* XXX hack to get rid of the message queues if they already exist for
1941 * some reason (maybe left over from a previous mount that wasn't
1942 * unmounted correctly) */
1943 ret = open_message_queues(true);
1946 close_message_queues();
1948 DEBUG("Preparing arguments to fuse_main()");
1953 ret = WIMLIB_ERR_NOMEM;
1957 argv[argc++] = "imagex";
1959 argv[argc++] = "-s"; /* disable multi-threaded operation */
1961 if (flags & WIMLIB_MOUNT_FLAG_DEBUG)
1962 argv[argc++] = "-d";
1965 * We provide the use_ino option because we are going to assign inode
1966 * numbers oursides. We've already numbered the hard link groups with
1967 * unique numbers with the assign_link_groups() function, and the static
1968 * variable next_link_group_id is set to the next available link group
1969 * ID that we will assign to new dentries.
1971 char optstring[256] = "use_ino";
1972 argv[argc++] = "-o";
1973 argv[argc++] = optstring;
1974 if ((flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
1975 /* Read-write mount. Make the staging directory */
1977 if (!staging_dir_name) {
1979 ret = WIMLIB_ERR_MKDIR;
1983 /* Read-only mount */
1984 strcat(optstring, ",ro");
1986 strcat(optstring, ",subtype=wimfs,attr_timeout=0");
1992 DEBUG("FUSE command line (argc = %d): ", argc);
1993 for (i = 0; i < argc; i++) {
1994 fputs(argv[i], stdout);
2002 /* Set static variables. */
2004 mount_flags = flags;
2006 ret = fuse_main(argc, argv, &wimfs_operations, NULL);
2008 ret = WIMLIB_ERR_FUSE;
2010 if (num_additional_swms) {
2011 free_lookup_table(wim->lookup_table);
2012 wim->lookup_table = wim_tab_save;
2019 * Unmounts the WIM file that was previously mounted on @dir by using
2022 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
2029 struct timespec timeout;
2033 /* Execute `fusermount -u', which is installed setuid root, to unmount
2036 * FUSE does not yet implement synchronous unmounts. This means that
2037 * fusermount -u will return before the filesystem daemon returns from
2038 * wimfs_destroy(). This is partly what we want, because we need to
2039 * send a message from this process to the filesystem daemon telling
2040 * whether --commit was specified or not. However, after that, the
2041 * unmount process must wait for the filesystem daemon to finish writing
2049 ERROR_WITH_ERRNO("Failed to fork()");
2050 return WIMLIB_ERR_FORK;
2053 execlp("fusermount", "fusermount", "-u", dir, NULL);
2054 ERROR_WITH_ERRNO("Failed to execute `fusermount'");
2055 return WIMLIB_ERR_FUSERMOUNT;
2058 ret = waitpid(pid, &status, 0);
2060 ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
2062 return WIMLIB_ERR_FUSERMOUNT;
2066 ERROR("fusermount exited with status %d", status);
2067 return WIMLIB_ERR_FUSERMOUNT;
2070 /* Open message queues between the unmount process and the
2071 * filesystem daemon. */
2072 ret = open_message_queues(false);
2076 /* Send a message to the filesystem saying whether to commit or
2078 msg[0] = (flags & WIMLIB_UNMOUNT_FLAG_COMMIT) ? 1 : 0;
2079 msg[1] = (flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY) ? 1 : 0;
2081 DEBUG("Sending message: %s, %s",
2082 (msg[0] == 0) ? "don't commit" : "commit",
2083 (msg[1] == 0) ? "don't check" : "check");
2084 ret = mq_send(unmount_to_daemon_mq, msg, 2, 1);
2086 ERROR("Failed to notify filesystem daemon whether we want to "
2087 "commit changes or not");
2088 close_message_queues();
2089 return WIMLIB_ERR_MQUEUE;
2092 /* Wait for a message from the filesytem daemon indicating whether the
2093 * filesystem was unmounted successfully (0) or an error occurred (1).
2094 * This may take a long time if a big WIM file needs to be rewritten. */
2096 /* Wait at most 600??? seconds before giving up and returning false.
2097 * Either it's a really big WIM file, or (more likely) the
2098 * filesystem daemon has crashed or failed for some reason.
2100 * XXX come up with some method to determine if the filesystem
2101 * daemon has really crashed or not.
2103 * XXX Idea: have mount daemon write its PID into the WIM file header?
2106 gettimeofday(&now, NULL);
2107 timeout.tv_sec = now.tv_sec + 600;
2108 timeout.tv_nsec = now.tv_usec * 1000;
2110 msgsize = mq_get_msgsize(daemon_to_unmount_mq);
2111 char mailbox[msgsize];
2114 DEBUG("Waiting for message telling us whether the unmount was "
2115 "successful or not.");
2116 ret = mq_timedreceive(daemon_to_unmount_mq, mailbox, msgsize,
2119 close_message_queues();
2121 if (errno_save == ETIMEDOUT) {
2122 ERROR("Timed out- probably the filesystem daemon "
2123 "crashed and the WIM was not written "
2125 return WIMLIB_ERR_TIMEOUT;
2127 ERROR("mq_receive(): %s", strerror(errno_save));
2128 return WIMLIB_ERR_MQUEUE;
2132 DEBUG("Received message: %s",
2133 (mailbox[0] == 0) ? "Unmount OK" : "Unmount Failed");
2134 if (mailbox[0] != 0)
2135 ERROR("Unmount failed");
2139 #else /* WITH_FUSE */
2142 static inline int mount_unsupported_error()
2144 ERROR("WIMLIB was compiled with --without-fuse, which disables support "
2145 "for mounting WIMs.");
2146 return WIMLIB_ERR_UNSUPPORTED;
2149 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
2151 return mount_unsupported_error();
2154 WIMLIBAPI int wimlib_mount(WIMStruct *wim_p, int image, const char *dir,
2155 int flags, WIMStruct **additional_swms,
2156 unsigned num_additional_swms)
2158 return mount_unsupported_error();
2161 #endif /* WITH_FUSE */