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
49 #include <attr/xattr.h>
56 struct lookup_table_entry *lte;
57 struct dentry *dentry;
60 /* The WIMStruct for the mounted WIM. */
63 /* Working directory when `imagex mount' is run. */
64 static const char *working_directory;
66 /* Name of the staging directory for a read-write mount. Whenever a new file is
67 * created, it is done so in the staging directory. Furthermore, whenever a
68 * file in the WIM is modified, it is extracted to the staging directory. If
69 * changes are commited when the WIM is unmounted, the file resources are merged
70 * in from the staging directory when writing the new WIM. */
71 static char *staging_dir_name;
72 static size_t staging_dir_name_len;
74 /* Flags passed to wimlib_mount(). */
75 static int mount_flags;
77 /* Name of the directory on which the WIM file is mounted. */
78 static const char *mount_dir;
80 /* Next hard link group ID to be assigned. These are also used as the inode
82 static u64 next_link_group_id;
84 /* List of lookup table entries in the staging directory */
85 static LIST_HEAD(staging_list);
87 static inline int get_lookup_flags()
89 if (mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
90 return LOOKUP_FLAG_ADS_OK;
95 /* Returns nonzero if write permission is requested on the file open flags */
96 static inline int flags_writable(int open_flags)
98 return open_flags & (O_RDWR | O_WRONLY);
102 * Allocate a file descriptor for a lookup table entry
104 static int alloc_wimlib_fd(struct lookup_table_entry *lte,
105 struct wimlib_fd **fd_ret)
107 static const u16 fds_per_alloc = 8;
108 static const u16 max_fds = 0xffff;
110 if (lte->num_opened_fds == lte->num_allocated_fds) {
111 struct wimlib_fd **fds;
114 if (lte->num_allocated_fds == max_fds)
116 num_new_fds = min(fds_per_alloc, max_fds - lte->num_allocated_fds);
118 fds = REALLOC(lte->fds, (lte->num_allocated_fds + num_new_fds) *
119 sizeof(lte->fds[0]));
122 memset(&fds[lte->num_allocated_fds], 0,
123 num_new_fds * sizeof(fds[0]));
125 lte->num_allocated_fds += num_new_fds;
127 for (u16 i = 0; ; i++) {
129 struct wimlib_fd *fd = CALLOC(1, sizeof(*fd));
136 lte->num_opened_fds++;
143 static int close_wimlib_fd(struct wimlib_fd *fd)
145 struct lookup_table_entry *lte = fd->lte;
148 wimlib_assert(lte->num_opened_fds);
150 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
151 wimlib_assert(lte->staging_file_name);
152 wimlib_assert(fd->staging_fd != -1);
153 if (close(fd->staging_fd) != 0)
156 if (--lte->num_opened_fds == 0 && lte->refcnt == 0) {
157 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
158 wimlib_assert(lte->staging_file_name);
159 unlink(lte->staging_file_name);
161 free_lookup_table_entry(lte);
163 wimlib_assert(lte->fds[fd->idx] == fd);
164 lte->fds[fd->idx] = NULL;
169 /* Remove a dentry and all its alternate file streams */
170 static void remove_dentry(struct dentry *dentry,
171 struct lookup_table *lookup_table)
173 wimlib_assert(dentry);
174 wimlib_assert(dentry->resolved);
176 struct lookup_table_entry *lte = dentry->lte;
179 lte = lte_decrement_refcnt(lte, lookup_table);
180 if (lte && lte->num_opened_fds)
181 for (u16 i = 0; i < lte->num_allocated_fds; i++)
182 if (lte->fds[i] && lte->fds[i]->dentry == dentry)
183 lte->fds[i]->dentry = NULL;
184 if (i == dentry->num_ads)
186 lte = dentry->ads_entries[i].lte;
190 unlink_dentry(dentry);
194 static void remove_ads(struct dentry *dentry,
195 struct ads_entry *ads_entry,
196 struct lookup_table *lookup_table)
198 struct lookup_table_entry *lte;
200 wimlib_assert(dentry->resolved);
202 lte = lte_decrement_refcnt(ads_entry->lte, lookup_table);
204 list_del(&ads_entry->lte_group_list.list);
205 dentry_remove_ads(dentry, ads_entry);
208 /* Transfers file attributes from a struct dentry to a `stat' buffer. */
209 int dentry_to_stbuf(const struct dentry *dentry, struct stat *stbuf)
211 struct lookup_table_entry *lte;
213 if (dentry_is_symlink(dentry))
214 stbuf->st_mode = S_IFLNK | 0777;
215 else if (dentry_is_directory(dentry))
216 stbuf->st_mode = S_IFDIR | 0755;
218 stbuf->st_mode = S_IFREG | 0644;
220 stbuf->st_ino = (ino_t)dentry->link_group_id;
222 stbuf->st_nlink = dentry_link_group_size(dentry);
223 stbuf->st_uid = getuid();
224 stbuf->st_gid = getgid();
226 /* Use the size of the unnamed (default) file stream. */
227 lte = dentry_unnamed_lte_resolved(dentry);
229 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
230 wimlib_assert(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE);
231 wimlib_assert(lte->staging_file_name);
232 struct stat native_stat;
233 if (stat(lte->staging_file_name, &native_stat) != 0) {
234 DEBUG("Failed to stat `%s': %m",
235 lte->staging_file_name);
238 stbuf->st_size = native_stat.st_size;
240 stbuf->st_size = lte->resource_entry.original_size;
246 stbuf->st_atime = wim_timestamp_to_unix(dentry->last_access_time);
247 stbuf->st_mtime = wim_timestamp_to_unix(dentry->last_write_time);
248 stbuf->st_ctime = wim_timestamp_to_unix(dentry->creation_time);
249 stbuf->st_blocks = (stbuf->st_size + 511) / 512;
253 /* Creates a new staging file and returns its file descriptor opened for
256 * @name_ret: A location into which the a pointer to the newly allocated name of
257 * the staging file is stored.
258 * @return: The file descriptor for the new file. Returns -1 and sets errno on
259 * error, for any reason possible from the creat() function.
261 static int create_staging_file(char **name_ret, int open_flags)
269 name_len = staging_dir_name_len + 1 + SHA1_HASH_SIZE;
270 name = MALLOC(name_len + 1);
278 memcpy(name, staging_dir_name, staging_dir_name_len);
279 name[staging_dir_name_len] = '/';
280 randomize_char_array_with_alnum(name + staging_dir_name_len + 1,
282 name[name_len] = '\0';
285 /* Just in case, verify that the randomly generated name doesn't name an
286 * existing file, and try again if so */
287 } while (stat(name, &stbuf) == 0);
293 /* doesn't exist--- ok */
295 DEBUG("Creating staging file `%s'", name);
297 fd = open(name, open_flags | O_CREAT | O_TRUNC, 0600);
309 * Removes open file descriptors from a lookup table entry @old_lte where the
310 * file descriptors have opened the corresponding file resource in the context
311 * of the hard link group @link_group; these file descriptors are extracted and
312 * placed in a new lookup table entry, which is returned.
314 static struct lookup_table_entry *
315 lte_extract_fds(struct lookup_table_entry *old_lte, u64 link_group)
317 u16 num_transferred_fds;
318 struct lookup_table_entry *new_lte;
320 new_lte = new_lookup_table_entry();
324 num_transferred_fds = 0;
325 for (u16 i = 0; i < old_lte->num_allocated_fds; i++)
326 if (old_lte->fds[i] && old_lte->fds[i]->dentry &&
327 old_lte->fds[i]->dentry->link_group_id == link_group)
328 num_transferred_fds++;
329 DEBUG("Transferring %u file descriptors",
330 num_transferred_fds);
331 new_lte->fds = MALLOC(num_transferred_fds * sizeof(new_lte->fds[0]));
336 for (u16 i = 0, j = 0; ; i++) {
337 if (old_lte->fds[i] && old_lte->fds[i]->dentry &&
338 old_lte->fds[i]->dentry->link_group_id == link_group) {
339 struct wimlib_fd *fd = old_lte->fds[i];
340 old_lte->fds[i] = NULL;
343 new_lte->fds[j] = fd;
344 if (++j == num_transferred_fds)
348 DEBUG("old_lte: %u fds open; new_lte: %u fds open",
349 old_lte->num_opened_fds, new_lte->num_opened_fds);
350 old_lte->num_opened_fds -= num_transferred_fds;
351 new_lte->num_opened_fds = num_transferred_fds;
352 new_lte->num_allocated_fds = num_transferred_fds;
357 * Transfers an alternate data stream entry to a new lookup table entry
359 static void lte_transfer_ads_entry(struct lookup_table_entry *new_lte,
360 struct ads_entry *ads_entry)
362 list_del(&ads_entry->lte_group_list.list);
363 list_add(&ads_entry->lte_group_list.list, &new_lte->lte_group_list);
364 ads_entry->lte = new_lte;
368 * Transfers a dentry to a new lookup table entry
370 static void lte_transfer_dentry(struct lookup_table_entry *new_lte,
371 struct dentry *dentry)
373 wimlib_assert(dentry->lte_group_list.list.next);
374 wimlib_assert(new_lte->lte_group_list.next);
375 list_del(&dentry->lte_group_list.list);
376 list_add(&dentry->lte_group_list.list, &new_lte->lte_group_list);
377 dentry->lte = new_lte;
380 static void lte_transfer_stream_entries(struct lookup_table_entry *new_lte,
381 struct dentry *dentry,
384 INIT_LIST_HEAD(&new_lte->lte_group_list);
385 if (stream_idx == 0) {
386 struct list_head *pos = &dentry->link_group_list;
389 d = container_of(pos, struct dentry, link_group_list);
390 wimlib_assert(d->link_group_id == dentry->link_group_id);
391 lte_transfer_dentry(new_lte, d);
393 } while (pos != &dentry->link_group_list);
395 struct ads_entry *ads_entry;
396 wimlib_assert(stream_idx <= dentry->num_ads);
397 ads_entry = &dentry->ads_entries[stream_idx - 1];
398 lte_transfer_ads_entry(new_lte, ads_entry);
403 * Extract a WIM resource to the staging directory.
405 * @dentry, @stream_idx: The stream on whose behalf we are modifying the lookup
406 * table entry (these may be more streams than this that reference the lookup
409 * @lte: Pointer to pointer to the lookup table entry for the stream we need to
410 * extract, or NULL if there was no lookup table entry present for the stream
412 * @size: Number of bytes of the stream we want to extract (this supports the
413 * wimfs_truncate() function).
415 static int extract_resource_to_staging_dir(struct dentry *dentry,
417 struct lookup_table_entry **lte,
420 char *staging_file_name;
423 struct lookup_table_entry *old_lte, *new_lte;
424 size_t link_group_size;
428 * - Create a staging file for the WIM resource
429 * - Extract the resource to it
430 * - Create a new lte for the file resource
431 * - Transfer fds from the old lte to the new lte, but only if they share the
432 * same hard link group as this dentry. If there is no old lte, then this
433 * step does not need to be done
434 * - Transfer stream entries from the old lte's list to the new lte's list. If
435 * there is no old lte, we instead transfer entries for the hard link group.
437 * Note: *lte is permitted to be NULL, in which case there is no old
438 * lookup table entry.
441 DEBUG("Extracting resource `%s' to staging directory", dentry->full_path_utf8);
444 fd = create_staging_file(&staging_file_name, O_WRONLY);
449 ret = extract_wim_resource_to_fd(old_lte, fd, size);
452 if (ret != 0 || close(fd) != 0) {
458 goto out_delete_staging_file;
461 link_group_size = dentry_link_group_size(dentry);
464 if (link_group_size == old_lte->refcnt) {
465 /* This hard link group is the only user of the lookup
466 * table entry, so we can re-use it. */
467 DEBUG("Re-using lookup table entry");
468 lookup_table_unlink(w->lookup_table, old_lte);
471 DEBUG("Splitting lookup table entry "
472 "(link_group_size = %zu, lte refcnt = %u)",
473 link_group_size, old_lte->refcnt);
474 /* Split a hard link group away from the "lookup table
475 * entry" hard link group (i.e. we had two hard link
476 * groups that were identical, but now we are changing
480 * The ADS really complicate things here and not
481 * everything is going to work correctly yet. For
482 * example it could be the same that a file contains two
483 * file streams that are identical and therefore share
484 * the same lookup table entry despite the fact that the
485 * streams themselves are not hardlinked.
487 wimlib_assert(old_lte->refcnt > link_group_size);
489 new_lte = lte_extract_fds(old_lte, dentry->link_group_id);
492 goto out_delete_staging_file;
495 lte_transfer_stream_entries(new_lte, dentry, stream_idx);
496 old_lte->refcnt -= link_group_size;
499 /* No old_lte was supplied, so the resource had no lookup table
500 * entry before (it must be an empty resource) */
501 new_lte = new_lookup_table_entry();
504 goto out_delete_staging_file;
506 lte_transfer_stream_entries(new_lte, dentry, stream_idx);
508 new_lte->resource_entry.original_size = size;
509 new_lte->refcnt = link_group_size;
510 random_hash(new_lte->hash);
511 if (new_lte->staging_file_name)
512 FREE(new_lte->staging_file_name);
513 new_lte->staging_file_name = staging_file_name;
514 new_lte->resource_location = RESOURCE_IN_STAGING_FILE;
516 lookup_table_insert(w->lookup_table, new_lte);
517 list_add(&new_lte->staging_list, &staging_list);
520 out_delete_staging_file:
521 unlink(staging_file_name);
522 FREE(staging_file_name);
527 * Creates a randomly named staging directory and returns its name into the
528 * static variable staging_dir_name.
530 * If the staging directory cannot be created, staging_dir_name is set to NULL.
532 static void make_staging_dir()
534 /* XXX Give the user an option of where to stage files */
536 static char prefix[] = "wimlib-staging-";
537 static const size_t prefix_len = 15;
538 static const size_t suffix_len = 10;
540 size_t pwd_len = strlen(working_directory);
542 staging_dir_name_len = pwd_len + 1 + prefix_len + suffix_len;
544 staging_dir_name = MALLOC(staging_dir_name_len + 1);
545 if (!staging_dir_name) {
546 ERROR("Out of memory");
550 memcpy(staging_dir_name, working_directory, pwd_len);
551 staging_dir_name[pwd_len] = '/';
552 memcpy(staging_dir_name + pwd_len + 1, prefix, prefix_len);
553 randomize_char_array_with_alnum(staging_dir_name + pwd_len + 1 + prefix_len,
555 staging_dir_name[staging_dir_name_len] = '\0';
557 if (mkdir(staging_dir_name, 0700) != 0) {
558 ERROR_WITH_ERRNO("Failed to create temporary directory `%s'",
560 FREE(staging_dir_name);
561 staging_dir_name = NULL;
565 static int remove_file_or_directory(const char *fpath, const struct stat *sb,
566 int typeflag, struct FTW *ftwbuf)
568 if (remove(fpath) == 0)
571 return WIMLIB_ERR_DELETE_STAGING_DIR;
576 * Deletes the staging directory and all the files contained in it.
578 static inline int delete_staging_dir()
582 ret = nftw(staging_dir_name, remove_file_or_directory,10, FTW_DEPTH);
583 staging_dir_name = NULL;
587 /* Name and message queue descriptors for message queues between the filesystem
588 * daemon process and the unmount process. These are used when the filesystem
589 * is unmounted and the process running wimlib_mount() (i.e. the `imagex
590 * unmount' command) needs to communicate with the filesystem daemon running
591 * fuse_main() (i.e. that spawned by the `imagex mount' or `imagex mountrw'
593 static char *unmount_to_daemon_mq_name;
594 static char *daemon_to_unmount_mq_name;
595 static int unmount_to_daemon_mq;
596 static int daemon_to_unmount_mq;
598 /* Simple function that returns the concatenation of 4 strings. */
599 static char *strcat_dup(const char *s1, const char *s2, const char *s3,
602 size_t len = strlen(s1) + strlen(s2) + strlen(s3) + strlen(s4) + 1;
603 char *p = MALLOC(len);
614 /* Removes trailing forward slashes in a string. */
615 static void remove_trailing_slashes(char *s)
617 long len = strlen(s);
618 for (long i = len - 1; i >= 1; i--) {
626 /* Changes forward slashes to underscores in a string. */
627 static void s_slashes_underscores_g(char *s)
637 * Opens two POSIX message queue: one for sending messages from the unmount
638 * process to the daemon process, and one to go the other way. The names of the
639 * message queues, which must be system-wide unique, are be based on the mount
640 * point. (There of course is still a possibility of a collision if one were to
641 * unmount two identically named directories simultaneously...)
643 * @daemon specifies whether the calling process is the filesystem daemon or the
646 static int open_message_queues(bool daemon)
648 static const char *slash = "/";
649 static const char *prefix = "wimlib-";
650 static const char *u2d_suffix = "unmount-to-daemon-mq";
651 static const char *d2u_suffix = "daemon-to-unmount-mq";
653 const char *mount_dir_basename = path_basename(mount_dir);
657 unmount_to_daemon_mq_name = strcat_dup(slash, mount_dir_basename,
659 if (!unmount_to_daemon_mq_name) {
660 ERROR("Out of memory");
661 return WIMLIB_ERR_NOMEM;
663 daemon_to_unmount_mq_name = strcat_dup(slash, mount_dir_basename,
665 if (!daemon_to_unmount_mq_name) {
666 ERROR("Out of memory");
667 ret = WIMLIB_ERR_NOMEM;
671 remove_trailing_slashes(unmount_to_daemon_mq_name);
672 remove_trailing_slashes(daemon_to_unmount_mq_name);
673 s_slashes_underscores_g(unmount_to_daemon_mq_name + 1);
674 s_slashes_underscores_g(daemon_to_unmount_mq_name + 1);
677 flags = O_RDONLY | O_CREAT;
679 flags = O_WRONLY | O_CREAT;
681 unmount_to_daemon_mq = mq_open(unmount_to_daemon_mq_name, flags,
684 if (unmount_to_daemon_mq == -1) {
685 ERROR_WITH_ERRNO("mq_open()");
686 ret = WIMLIB_ERR_MQUEUE;
691 flags = O_WRONLY | O_CREAT;
693 flags = O_RDONLY | O_CREAT;
695 daemon_to_unmount_mq = mq_open(daemon_to_unmount_mq_name, flags,
698 if (daemon_to_unmount_mq == -1) {
699 ERROR_WITH_ERRNO("mq_open()");
700 ret = WIMLIB_ERR_MQUEUE;
705 mq_close(unmount_to_daemon_mq);
706 mq_unlink(unmount_to_daemon_mq_name);
708 FREE(daemon_to_unmount_mq_name);
710 FREE(unmount_to_daemon_mq_name);
714 static int mq_get_msgsize(mqd_t mq)
716 static const char *msgsize_max_file = "/proc/sys/fs/mqueue/msgsize_max";
721 if (mq_getattr(unmount_to_daemon_mq, &attr) == 0) {
722 msgsize = attr.mq_msgsize;
724 ERROR_WITH_ERRNO("mq_getattr()");
725 ERROR("Attempting to read %s", msgsize_max_file);
726 fp = fopen(msgsize_max_file, "rb");
728 if (fscanf(fp, "%d", &msgsize) != 1) {
729 ERROR("Assuming message size of 8192");
734 ERROR_WITH_ERRNO("Failed to open the file `%s'",
736 ERROR("Assuming message size of 8192");
743 /* Closes the message queues, which are allocated in static variables */
744 static void close_message_queues()
746 mq_close(unmount_to_daemon_mq);
747 mq_close(daemon_to_unmount_mq);
748 mq_unlink(unmount_to_daemon_mq_name);
749 mq_unlink(daemon_to_unmount_mq_name);
752 static int wimfs_access(const char *path, int mask)
754 /* XXX Permissions not implemented */
758 /* Closes the staging file descriptor associated with the lookup table entry, if
760 static int close_lte_fds(struct lookup_table_entry *lte)
762 for (u16 i = 0, j = 0; j < lte->num_opened_fds; i++) {
763 if (lte->fds[i] && lte->fds[i]->staging_fd != -1) {
764 wimlib_assert(lte->resource_location == RESOURCE_IN_STAGING_FILE);
765 wimlib_assert(lte->staging_file_name);
766 if (close(lte->fds[i]->staging_fd) != 0) {
767 ERROR_WITH_ERRNO("Failed close file `%s'",
768 lte->staging_file_name);
769 return WIMLIB_ERR_WRITE;
777 static void lte_list_change_lte_ptr(struct lookup_table_entry *lte,
778 struct lookup_table_entry *newptr)
780 struct list_head *pos;
781 struct stream_list_head *head;
782 list_for_each(pos, <e->lte_group_list) {
783 head = container_of(pos, struct stream_list_head, list);
784 if (head->type == STREAM_TYPE_ADS) {
785 struct ads_entry *ads_entry;
786 ads_entry = container_of(head, struct ads_entry, lte_group_list);
788 ads_entry->lte = newptr;
790 wimlib_assert(head->type == STREAM_TYPE_NORMAL);
792 struct dentry *dentry;
793 dentry = container_of(head, struct dentry, lte_group_list);
795 dentry->lte = newptr;
801 static int update_lte_of_staging_file(struct lookup_table_entry *lte,
802 struct lookup_table *table)
804 struct lookup_table_entry *duplicate_lte;
806 u8 hash[SHA1_HASH_SIZE];
809 wimlib_assert(lte->resource_location == RESOURCE_IN_STAGING_FILE);
810 wimlib_assert(lte->staging_file_name);
812 ret = sha1sum(lte->staging_file_name, hash);
816 lookup_table_unlink(table, lte);
818 duplicate_lte = __lookup_resource(table, hash);
821 /* Merge duplicate lookup table entries */
823 lte_list_change_lte_ptr(lte, duplicate_lte);
824 duplicate_lte->refcnt += lte->refcnt;
825 list_splice(<e->lte_group_list,
826 &duplicate_lte->lte_group_list);
828 free_lookup_table_entry(lte);
830 if (stat(lte->staging_file_name, &stbuf) != 0) {
831 ERROR_WITH_ERRNO("Failed to stat `%s'", lte->staging_file_name);
832 return WIMLIB_ERR_STAT;
834 wimlib_assert(<e->file_on_disk == <e->staging_file_name);
835 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
836 copy_hash(lte->hash, hash);
837 lte->resource_entry.original_size = stbuf.st_size;
838 lte->resource_entry.size = stbuf.st_size;
839 lookup_table_insert(table, lte);
845 /* Overwrites the WIM file, with changes saved. */
846 static int rebuild_wim(WIMStruct *w, bool check_integrity)
849 struct lookup_table_entry *lte, *tmp;
851 /* Close all the staging file descriptors. */
852 DEBUG("Closing all staging file descriptors.");
853 list_for_each_entry(lte, &staging_list, staging_list) {
854 ret = close_lte_fds(lte);
859 /* Calculate SHA1 checksums for all staging files, and merge unnecessary
860 * lookup table entries. */
861 DEBUG("Calculating SHA1 checksums for all new staging files.");
862 list_for_each_entry_safe(lte, tmp, &staging_list, staging_list) {
863 ret = update_lte_of_staging_file(lte, w->lookup_table);
868 xml_update_image_info(w, w->current_image);
870 ret = wimlib_overwrite(w, check_integrity);
872 ERROR("Failed to commit changes");
878 /* Called when the filesystem is unmounted. */
879 static void wimfs_destroy(void *p)
881 /* For read-write mounts, the `imagex unmount' command, which is
882 * running in a separate process and is executing the
883 * wimlib_unmount() function, will send this process a byte
884 * through a message queue that indicates whether the --commit
885 * option was specified or not. */
888 struct timespec timeout;
890 ssize_t bytes_received;
893 char check_integrity;
896 ret = open_message_queues(true);
900 msgsize = mq_get_msgsize(unmount_to_daemon_mq);
905 /* Wait at most 3 seconds before giving up and discarding changes. */
906 gettimeofday(&now, NULL);
907 timeout.tv_sec = now.tv_sec + 3;
908 timeout.tv_nsec = now.tv_usec * 1000;
909 DEBUG("Waiting for message telling us whether to commit or not, and "
910 "whether to include integrity checks.");
912 bytes_received = mq_timedreceive(unmount_to_daemon_mq, msg,
913 msgsize, NULL, &timeout);
915 check_integrity = msg[1];
916 if (bytes_received == -1) {
917 if (errno == ETIMEDOUT) {
920 ERROR_WITH_ERRNO("mq_timedreceive()");
922 ERROR("Not committing.");
924 DEBUG("Received message: [%d %d]", msg[0], msg[1]);
928 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
930 status = chdir(working_directory);
932 ERROR_WITH_ERRNO("chdir()");
933 status = WIMLIB_ERR_NOTDIR;
936 status = rebuild_wim(w, (check_integrity != 0));
938 ret = delete_staging_dir();
940 ERROR_WITH_ERRNO("Failed to delete the staging "
946 DEBUG("Read-only mount");
949 DEBUG("Sending status %u", status);
950 ret = mq_send(daemon_to_unmount_mq, &status, 1, 1);
952 ERROR_WITH_ERRNO("Failed to send status to unmount process");
953 close_message_queues();
957 static int wimfs_fallocate(const char *path, int mode,
958 off_t offset, off_t len, struct fuse_file_info *fi)
960 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
961 wimlib_assert(fd->staging_fd != -1);
962 return fallocate(fd->staging_fd, mode, offset, len);
965 static int wimfs_fgetattr(const char *path, struct stat *stbuf,
966 struct fuse_file_info *fi)
968 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
969 return dentry_to_stbuf(fd->dentry, stbuf);
973 static int wimfs_ftruncate(const char *path, off_t size,
974 struct fuse_file_info *fi)
976 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
977 int ret = ftruncate(fd->staging_fd, size);
980 fd->lte->resource_entry.original_size = size;
985 * Fills in a `struct stat' that corresponds to a file or directory in the WIM.
987 static int wimfs_getattr(const char *path, struct stat *stbuf)
989 struct dentry *dentry;
992 ret = lookup_resource(w, path,
993 get_lookup_flags() | LOOKUP_FLAG_DIRECTORY_OK,
994 &dentry, NULL, NULL);
997 return dentry_to_stbuf(dentry, stbuf);
1001 /* Read an alternate data stream through the XATTR interface, or get its size */
1002 static int wimfs_getxattr(const char *path, const char *name, char *value,
1006 struct dentry *dentry;
1007 struct ads_entry *ads_entry;
1009 struct lookup_table_entry *lte;
1011 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1014 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1018 dentry = get_dentry(w, path);
1021 ads_entry = dentry_get_ads_entry(dentry, name);
1025 lte = ads_entry->lte;
1026 res_size = wim_resource_size(lte);
1030 if (res_size > size)
1032 ret = read_full_wim_resource(lte, (u8*)value);
1039 /* Create a hard link */
1040 static int wimfs_link(const char *to, const char *from)
1042 struct dentry *to_dentry, *from_dentry, *from_dentry_parent;
1043 const char *link_name;
1045 to_dentry = get_dentry(w, to);
1048 if (!dentry_is_regular_file(to_dentry))
1051 from_dentry_parent = get_parent_dentry(w, from);
1052 if (!from_dentry_parent)
1054 if (!dentry_is_directory(from_dentry_parent))
1057 link_name = path_basename(from);
1058 if (get_dentry_child_with_name(from_dentry_parent, link_name))
1060 from_dentry = clone_dentry(to_dentry);
1063 if (change_dentry_name(from_dentry, link_name) != 0) {
1068 /* Add the new dentry to the dentry list for the link group */
1069 list_add(&from_dentry->link_group_list, &to_dentry->link_group_list);
1071 /* Increment reference counts for the unnamed file stream and all
1072 * alternate data streams. */
1073 if (from_dentry->lte) {
1074 list_add(&from_dentry->lte_group_list.list,
1075 &to_dentry->lte_group_list.list);
1076 from_dentry->lte->refcnt++;
1078 for (u16 i = 0; i < from_dentry->num_ads; i++) {
1079 struct ads_entry *ads_entry = &from_dentry->ads_entries[i];
1081 ads_entry->lte->refcnt++;
1084 /* The ADS entries are owned by another dentry. */
1085 from_dentry->ads_entries_status = ADS_ENTRIES_USER;
1087 link_dentry(from_dentry, from_dentry_parent);
1092 static int wimfs_listxattr(const char *path, char *list, size_t size)
1094 struct dentry *dentry;
1099 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1102 /* List alternate data streams, or get the list size */
1104 ret = lookup_resource(w, path, get_lookup_flags(), &dentry, NULL, NULL);
1109 for (i = 0; i < dentry->num_ads; i++)
1110 needed_size += dentry->ads_entries[i].stream_name_utf8_len + 6;
1113 for (i = 0; i < dentry->num_ads; i++) {
1114 needed_size = dentry->ads_entries[i].stream_name_utf8_len + 6;
1115 if (needed_size > size)
1117 p += sprintf(p, "user.%s",
1118 dentry->ads_entries[i].stream_name_utf8) + 1;
1119 size -= needed_size;
1127 * Create a directory in the WIM.
1128 * @mode is currently ignored.
1130 static int wimfs_mkdir(const char *path, mode_t mode)
1132 struct dentry *parent;
1133 struct dentry *newdir;
1134 const char *basename;
1136 parent = get_parent_dentry(w, path);
1140 if (!dentry_is_directory(parent))
1143 basename = path_basename(path);
1144 if (get_dentry_child_with_name(parent, basename))
1147 newdir = new_dentry(basename);
1148 newdir->attributes |= FILE_ATTRIBUTE_DIRECTORY;
1149 newdir->resolved = true;
1150 newdir->link_group_id = next_link_group_id++;
1151 link_dentry(newdir, parent);
1156 /* Creates a regular file. */
1157 static int wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
1159 const char *stream_name;
1160 if ((mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
1161 && (stream_name = path_stream_name(path))) {
1162 /* Make an alternate data stream */
1163 struct ads_entry *new_entry;
1164 struct dentry *dentry;
1166 char *p = (char*)stream_name - 1;
1167 wimlib_assert(*p == ':');
1170 dentry = get_dentry(w, path);
1171 if (!dentry || !dentry_is_regular_file(dentry))
1173 if (dentry_get_ads_entry(dentry, stream_name))
1175 new_entry = dentry_add_ads(dentry, stream_name);
1179 struct dentry *dentry, *parent;
1180 const char *basename;
1182 /* Make a normal file (not an alternate data stream) */
1184 /* Make sure that the parent of @path exists and is a directory, and
1185 * that the dentry named by @path does not already exist. */
1186 parent = get_parent_dentry(w, path);
1189 if (!dentry_is_directory(parent))
1192 basename = path_basename(path);
1193 if (get_dentry_child_with_name(parent, path))
1196 dentry = new_dentry(basename);
1199 dentry->resolved = true;
1200 dentry->link_group_id = next_link_group_id++;
1201 dentry->lte_group_list.type = STREAM_TYPE_NORMAL;
1202 INIT_LIST_HEAD(&dentry->lte_group_list.list);
1203 link_dentry(dentry, parent);
1210 static int wimfs_open(const char *path, struct fuse_file_info *fi)
1212 struct dentry *dentry;
1213 struct lookup_table_entry *lte;
1215 struct wimlib_fd *fd;
1216 unsigned stream_idx;
1218 ret = lookup_resource(w, path, get_lookup_flags(), &dentry, <e,
1224 /* Empty file with no lookup-table entry. This is fine if it's
1225 * a read-only filesystem. Otherwise we need to create a lookup
1226 * table entry so that we can keep track of the file descriptors
1227 * (this is important in case someone opens the file for
1229 if (!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
1234 ret = extract_resource_to_staging_dir(dentry, stream_idx,
1240 ret = alloc_wimlib_fd(lte, &fd);
1244 fd->dentry = dentry;
1246 /* The file resource may be in the staging directory (read-write
1247 * mounts only) or in the WIM. If it's in the staging
1248 * directory, we need to open a native file descriptor for the
1249 * corresponding file. Otherwise, we can read the file resource
1250 * directly from the WIM file if we are opening it read-only,
1251 * but we need to extract the resource to the staging directory
1252 * if we are opening it writable. */
1253 if (flags_writable(fi->flags) &&
1254 lte->resource_location != RESOURCE_IN_STAGING_FILE) {
1255 ret = extract_resource_to_staging_dir(dentry, stream_idx, <e,
1256 lte->resource_entry.original_size);
1260 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1261 fd->staging_fd = open(lte->staging_file_name, fi->flags);
1262 if (fd->staging_fd == -1) {
1263 close_wimlib_fd(fd);
1267 fi->fh = (uintptr_t)fd;
1271 /* Opens a directory. */
1272 static int wimfs_opendir(const char *path, struct fuse_file_info *fi)
1274 struct dentry *dentry;
1276 dentry = get_dentry(w, path);
1279 if (!dentry_is_directory(dentry))
1281 dentry->num_times_opened++;
1282 fi->fh = (uintptr_t)dentry;
1288 * Read data from a file in the WIM or in the staging directory.
1290 static int wimfs_read(const char *path, char *buf, size_t size,
1291 off_t offset, struct fuse_file_info *fi)
1293 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1296 /* Empty file with no lookup table entry on read-only mounted
1298 wimlib_assert(!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE));
1302 wimlib_assert(fd->lte);
1304 if (fd->lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1305 /* Read from staging file */
1307 wimlib_assert(fd->lte->staging_file_name);
1308 wimlib_assert(fd->staging_fd != -1);
1311 DEBUG("Seek to offset %zu", offset);
1313 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1315 ret = read(fd->staging_fd, buf, size);
1322 const struct resource_entry *res_entry;
1324 res_entry = &fd->lte->resource_entry;
1326 if (offset > res_entry->original_size)
1329 size = min(size, res_entry->original_size - offset);
1331 if (read_wim_resource(fd->lte, (u8*)buf,
1332 size, offset, false) != 0)
1338 /* Fills in the entries of the directory specified by @path using the
1339 * FUSE-provided function @filler. */
1340 static int wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
1341 off_t offset, struct fuse_file_info *fi)
1343 struct dentry *parent, *child;
1345 parent = (struct dentry*)(uintptr_t)fi->fh;
1346 wimlib_assert(parent);
1347 child = parent->children;
1349 filler(buf, ".", NULL, 0);
1350 filler(buf, "..", NULL, 0);
1356 if (filler(buf, child->file_name_utf8, NULL, 0))
1358 child = child->next;
1359 } while (child != parent->children);
1364 static int wimfs_readlink(const char *path, char *buf, size_t buf_len)
1366 struct dentry *dentry = get_dentry(w, path);
1370 if (!dentry_is_symlink(dentry))
1373 ret = dentry_readlink(dentry, buf, buf_len, w);
1380 static int wimfs_release(const char *path, struct fuse_file_info *fi)
1382 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1385 /* Empty file with no lookup table entry on read-only mounted
1387 wimlib_assert(!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE));
1391 if (flags_writable(fi->flags) && fd->dentry) {
1392 u64 now = get_wim_timestamp();
1393 fd->dentry->last_access_time = now;
1394 fd->dentry->last_write_time = now;
1397 return close_wimlib_fd(fd);
1400 static int wimfs_releasedir(const char *path, struct fuse_file_info *fi)
1402 struct dentry *dentry = (struct dentry *)(uintptr_t)fi->fh;
1404 wimlib_assert(dentry);
1405 wimlib_assert(dentry->num_times_opened);
1406 if (--dentry->num_times_opened == 0)
1407 free_dentry(dentry);
1412 /* Remove an alternate data stream through the XATTR interface */
1413 static int wimfs_removexattr(const char *path, const char *name)
1415 struct dentry *dentry;
1416 struct ads_entry *ads_entry;
1417 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1420 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1424 dentry = get_dentry(w, path);
1428 ads_entry = dentry_get_ads_entry(dentry, name);
1431 remove_ads(dentry, ads_entry, w->lookup_table);
1436 /* Renames a file or directory. See rename (3) */
1437 static int wimfs_rename(const char *from, const char *to)
1441 struct dentry *parent_of_dst;
1442 char *file_name_utf16 = NULL, *file_name_utf8 = NULL;
1443 u16 file_name_utf16_len, file_name_utf8_len;
1446 /* This rename() implementation currently only supports actual files
1447 * (not alternate data streams) */
1449 src = get_dentry(w, from);
1453 dst = get_dentry(w, to);
1456 ret = get_names(&file_name_utf16, &file_name_utf8,
1457 &file_name_utf16_len, &file_name_utf8_len,
1463 /* Destination file exists */
1465 if (src == dst) /* Same file */
1468 if (!dentry_is_directory(src)) {
1469 /* Cannot rename non-directory to directory. */
1470 if (dentry_is_directory(dst))
1473 /* Cannot rename directory to a non-directory or a non-empty
1475 if (!dentry_is_directory(dst))
1477 if (dst->children != NULL)
1480 parent_of_dst = dst->parent;
1481 remove_dentry(dst, w->lookup_table);
1483 /* Destination does not exist */
1484 parent_of_dst = get_parent_dentry(w, to);
1488 if (!dentry_is_directory(parent_of_dst))
1492 FREE(src->file_name);
1493 FREE(src->file_name_utf8);
1494 src->file_name = file_name_utf16;
1495 src->file_name_utf8 = file_name_utf8;
1496 src->file_name_len = file_name_utf16_len;
1497 src->file_name_utf8_len = file_name_utf8_len;
1500 link_dentry(src, parent_of_dst);
1504 /* Remove a directory */
1505 static int wimfs_rmdir(const char *path)
1507 struct dentry *dentry;
1509 dentry = get_dentry(w, path);
1513 if (!dentry_is_empty_directory(dentry))
1516 unlink_dentry(dentry);
1517 if (dentry->num_times_opened == 0)
1518 free_dentry(dentry);
1523 /* Write an alternate data stream through the XATTR interface */
1524 static int wimfs_setxattr(const char *path, const char *name,
1525 const char *value, size_t size, int flags)
1527 struct dentry *dentry;
1528 struct ads_entry *existing_ads_entry;
1529 struct ads_entry *new_ads_entry;
1530 struct lookup_table_entry *existing_lte;
1531 struct lookup_table_entry *lte;
1532 u8 value_hash[SHA1_HASH_SIZE];
1534 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1537 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1541 dentry = get_dentry(w, path);
1544 existing_ads_entry = dentry_get_ads_entry(dentry, name);
1545 if (existing_ads_entry) {
1546 if (flags & XATTR_CREATE)
1548 remove_ads(dentry, existing_ads_entry, w->lookup_table);
1550 if (flags & XATTR_REPLACE)
1553 new_ads_entry = dentry_add_ads(dentry, name);
1557 sha1_buffer((const u8*)value, size, value_hash);
1559 existing_lte = __lookup_resource(w->lookup_table, value_hash);
1566 lte = new_lookup_table_entry();
1569 value_copy = MALLOC(size);
1574 memcpy(value_copy, value, size);
1575 lte->resource_location = RESOURCE_IN_ATTACHED_BUFFER;
1576 lte->attached_buffer = value_copy;
1577 lte->resource_entry.original_size = size;
1578 lte->resource_entry.size = size;
1579 lte->resource_entry.flags = 0;
1580 copy_hash(lte->hash, value_hash);
1581 lookup_table_insert(w->lookup_table, lte);
1583 new_ads_entry->lte = lte;
1588 static int wimfs_symlink(const char *to, const char *from)
1590 struct dentry *dentry_parent, *dentry;
1591 const char *link_name;
1592 struct lookup_table_entry *lte;
1594 dentry_parent = get_parent_dentry(w, from);
1597 if (!dentry_is_directory(dentry_parent))
1600 link_name = path_basename(from);
1602 if (get_dentry_child_with_name(dentry_parent, link_name))
1604 dentry = new_dentry(link_name);
1608 dentry->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
1609 dentry->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
1610 dentry->link_group_id = next_link_group_id++;
1612 if (dentry_set_symlink(dentry, to, w->lookup_table, <e) != 0)
1613 goto out_free_dentry;
1617 dentry->ads_entries[1].lte_group_list.type = STREAM_TYPE_ADS;
1618 list_add(&dentry->ads_entries[1].lte_group_list.list,
1619 <e->lte_group_list);
1620 wimlib_assert(dentry->resolved);
1622 link_dentry(dentry, dentry_parent);
1625 free_dentry(dentry);
1630 /* Reduce the size of a file */
1631 static int wimfs_truncate(const char *path, off_t size)
1633 struct dentry *dentry;
1634 struct lookup_table_entry *lte;
1636 unsigned stream_idx;
1638 ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1644 if (!lte) /* Already a zero-length file */
1647 if (lte->staging_file_name) {
1648 ret = truncate(lte->staging_file_name, size);
1651 lte->resource_entry.original_size = size;
1653 /* File in WIM. Extract it to the staging directory, but only
1654 * the first @size bytes of it. */
1655 ret = extract_resource_to_staging_dir(dentry, stream_idx,
1658 dentry_update_all_timestamps(dentry);
1662 /* Remove a regular file */
1663 static int wimfs_unlink(const char *path)
1665 struct dentry *dentry;
1666 struct lookup_table_entry *lte;
1668 unsigned stream_idx;
1670 ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1676 if (stream_idx == 0) {
1677 /* We are removing the full dentry including all alternate data
1679 remove_dentry(dentry, w->lookup_table);
1681 /* We are removing an alternate data stream. */
1682 remove_ads(dentry, &dentry->ads_entries[stream_idx - 1],
1685 /* Beware: The lookup table entry(s) may still be referenced by users
1686 * that have opened the corresponding streams. They are freed later in
1687 * wimfs_release() when the last file user has closed the stream. */
1692 * Change the timestamp on a file dentry.
1694 * Note that alternate data streams do not have their own timestamps.
1696 static int wimfs_utimens(const char *path, const struct timespec tv[2])
1698 struct dentry *dentry = get_dentry(w, path);
1701 if (tv[0].tv_nsec != UTIME_OMIT) {
1702 if (tv[0].tv_nsec == UTIME_NOW)
1703 dentry->last_access_time = get_wim_timestamp();
1705 dentry->last_access_time = timespec_to_wim_timestamp(&tv[0]);
1707 if (tv[1].tv_nsec != UTIME_OMIT) {
1708 if (tv[1].tv_nsec == UTIME_NOW)
1709 dentry->last_write_time = get_wim_timestamp();
1711 dentry->last_write_time = timespec_to_wim_timestamp(&tv[1]);
1716 /* Writes to a file in the WIM filesystem.
1717 * It may be an alternate data stream, but here we don't even notice because we
1718 * just get a lookup table entry. */
1719 static int wimfs_write(const char *path, const char *buf, size_t size,
1720 off_t offset, struct fuse_file_info *fi)
1722 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1726 wimlib_assert(fd->lte);
1727 wimlib_assert(fd->lte->staging_file_name);
1728 wimlib_assert(fd->staging_fd != -1);
1730 /* Seek to the requested position */
1731 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1734 /* Write the data. */
1735 ret = write(fd->staging_fd, buf, size);
1742 static struct fuse_operations wimfs_operations = {
1743 .access = wimfs_access,
1744 .destroy = wimfs_destroy,
1746 .fallocate = wimfs_fallocate,
1747 .fgetattr = wimfs_fgetattr,
1749 .ftruncate = wimfs_ftruncate,
1750 .getattr = wimfs_getattr,
1752 .getxattr = wimfs_getxattr,
1756 .listxattr = wimfs_listxattr,
1758 .mkdir = wimfs_mkdir,
1759 .mknod = wimfs_mknod,
1761 .opendir = wimfs_opendir,
1763 .readdir = wimfs_readdir,
1764 .readlink = wimfs_readlink,
1765 .release = wimfs_release,
1766 .releasedir = wimfs_releasedir,
1768 .removexattr = wimfs_removexattr,
1770 .rename = wimfs_rename,
1771 .rmdir = wimfs_rmdir,
1773 .setxattr = wimfs_setxattr,
1775 .symlink = wimfs_symlink,
1776 .truncate = wimfs_truncate,
1777 .unlink = wimfs_unlink,
1778 .utimens = wimfs_utimens,
1779 .write = wimfs_write,
1783 static int check_lte_refcnt(struct lookup_table_entry *lte, void *ignore)
1785 size_t lte_group_size = 0;
1786 struct list_head *cur;
1787 list_for_each(cur, <e->lte_group_list)
1789 if (lte_group_size > lte->refcnt) {
1790 #ifdef ENABLE_ERROR_MESSAGES
1791 struct dentry *example_dentry;
1792 struct list_head *next;
1793 struct stream_list_head *head;
1794 WARNING("The following lookup table entry has a reference count "
1795 "of %u, but", lte->refcnt);
1796 WARNING("We found %zu references to it", lte_group_size);
1797 next = lte->lte_group_list.next;
1798 head = container_of(next, struct stream_list_head, list);
1799 if (head->type == STREAM_TYPE_NORMAL) {
1800 example_dentry = container_of(head, struct dentry,
1802 WARNING("(One dentry referencing it is at `%s')",
1803 example_dentry->full_path_utf8);
1805 print_lookup_table_entry(lte);
1807 /* Guess what! install.wim for Windows 8 contains a stream with
1808 * 2 dentries referencing it, but the lookup table entry has
1809 * reference count of 1. So we will need to handle this case
1810 * and not just make it be an error... I'm just setting the
1811 * reference count to the number of references we found. */
1814 lte->refcnt = lte_group_size;
1815 WARNING("Fixing reference count");
1817 return WIMLIB_ERR_INVALID_DENTRY;
1823 /* Mounts a WIM file. */
1824 WIMLIBAPI int wimlib_mount(WIMStruct *wim, int image, const char *dir,
1825 int flags, WIMStruct **additional_swms,
1826 unsigned num_additional_swms)
1832 struct lookup_table *joined_tab, *wim_tab_save;
1834 DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
1835 wim, image, dir, flags);
1838 return WIMLIB_ERR_INVALID_PARAM;
1840 ret = verify_swm_set(wim, additional_swms, num_additional_swms);
1844 if (num_additional_swms) {
1845 ret = new_joined_lookup_table(wim, additional_swms,
1846 num_additional_swms,
1850 wim_tab_save = wim->lookup_table;
1851 wim->lookup_table = joined_tab;
1854 ret = wimlib_select_image(wim, image);
1859 DEBUG("Selected image %d", image);
1861 next_link_group_id = assign_link_group_ids(wim->image_metadata[image - 1].lgt);
1863 DEBUG("Resolving lookup table entries");
1864 /* Resolve all the lookup table entries of the dentry tree */
1865 for_dentry_in_tree(wim_root_dentry(wim), dentry_resolve_ltes,
1868 DEBUG("Checking lookup table entry reference counts");
1870 ret = for_lookup_table_entry(wim->lookup_table, check_lte_refcnt, NULL);
1874 if (flags & WIMLIB_MOUNT_FLAG_READWRITE)
1875 wim_get_current_image_metadata(wim)->modified = true;
1877 if (!(flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
1878 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
1879 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
1880 flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1882 DEBUG("Getting current directory");
1885 working_directory = getcwd(NULL, 0);
1886 if (!working_directory) {
1887 ERROR_WITH_ERRNO("Could not determine current directory");
1888 ret = WIMLIB_ERR_NOTDIR;
1892 DEBUG("Closing POSIX message queues");
1893 /* XXX hack to get rid of the message queues if they already exist for
1894 * some reason (maybe left over from a previous mount that wasn't
1895 * unmounted correctly) */
1896 ret = open_message_queues(true);
1899 close_message_queues();
1901 DEBUG("Preparing arguments to fuse_main()");
1906 ret = WIMLIB_ERR_NOMEM;
1910 argv[argc++] = "imagex";
1912 argv[argc++] = "-s"; /* disable multi-threaded operation */
1914 if (flags & WIMLIB_MOUNT_FLAG_DEBUG)
1915 argv[argc++] = "-d";
1918 * We provide the use_ino option because we are going to assign inode
1919 * numbers oursides. We've already numbered the hard link groups with
1920 * unique numbers with the assign_link_groups() function, and the static
1921 * variable next_link_group_id is set to the next available link group
1922 * ID that we will assign to new dentries.
1924 char optstring[256] = "use_ino";
1925 argv[argc++] = "-o";
1926 argv[argc++] = optstring;
1927 if ((flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
1928 /* Read-write mount. Make the staging directory */
1930 if (!staging_dir_name) {
1932 ret = WIMLIB_ERR_MKDIR;
1936 /* Read-only mount */
1937 strcat(optstring, ",ro");
1944 DEBUG("FUSE command line (argc = %d): ", argc);
1945 for (i = 0; i < argc; i++) {
1946 fputs(argv[i], stdout);
1954 /* Set static variables. */
1956 mount_flags = flags;
1958 ret = fuse_main(argc, argv, &wimfs_operations, NULL);
1960 ret = WIMLIB_ERR_FUSE;
1962 if (num_additional_swms) {
1963 free_lookup_table(wim->lookup_table);
1964 wim->lookup_table = wim_tab_save;
1971 * Unmounts the WIM file that was previously mounted on @dir by using
1974 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
1981 struct timespec timeout;
1985 /* Execute `fusermount -u', which is installed setuid root, to unmount
1988 * FUSE does not yet implement synchronous unmounts. This means that
1989 * fusermount -u will return before the filesystem daemon returns from
1990 * wimfs_destroy(). This is partly what we want, because we need to
1991 * send a message from this process to the filesystem daemon telling
1992 * whether --commit was specified or not. However, after that, the
1993 * unmount process must wait for the filesystem daemon to finish writing
2000 ERROR_WITH_ERRNO("Failed to fork()");
2001 return WIMLIB_ERR_FORK;
2004 execlp("fusermount", "fusermount", "-u", dir, NULL);
2005 ERROR_WITH_ERRNO("Failed to execute `fusermount'");
2006 return WIMLIB_ERR_FUSERMOUNT;
2009 ret = waitpid(pid, &status, 0);
2011 ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
2013 return WIMLIB_ERR_FUSERMOUNT;
2017 ERROR("fusermount exited with status %d", status);
2018 return WIMLIB_ERR_FUSERMOUNT;
2021 /* Open message queues between the unmount process and the
2022 * filesystem daemon. */
2023 ret = open_message_queues(false);
2027 /* Send a message to the filesystem saying whether to commit or
2029 msg[0] = (flags & WIMLIB_UNMOUNT_FLAG_COMMIT) ? 1 : 0;
2030 msg[1] = (flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY) ? 1 : 0;
2032 DEBUG("Sending message: %s, %s",
2033 (msg[0] == 0) ? "don't commit" : "commit",
2034 (msg[1] == 0) ? "don't check" : "check");
2035 ret = mq_send(unmount_to_daemon_mq, msg, 2, 1);
2037 ERROR("Failed to notify filesystem daemon whether we want to "
2038 "commit changes or not");
2039 close_message_queues();
2040 return WIMLIB_ERR_MQUEUE;
2043 /* Wait for a message from the filesytem daemon indicating whether the
2044 * filesystem was unmounted successfully (0) or an error occurred (1).
2045 * This may take a long time if a big WIM file needs to be rewritten. */
2047 /* Wait at most 600??? seconds before giving up and returning false.
2048 * Either it's a really big WIM file, or (more likely) the
2049 * filesystem daemon has crashed or failed for some reason.
2051 * XXX come up with some method to determine if the filesystem
2052 * daemon has really crashed or not.
2054 * XXX Idea: have mount daemon write its PID into the WIM file header?
2057 gettimeofday(&now, NULL);
2058 timeout.tv_sec = now.tv_sec + 600;
2059 timeout.tv_nsec = now.tv_usec * 1000;
2061 msgsize = mq_get_msgsize(daemon_to_unmount_mq);
2062 char mailbox[msgsize];
2065 DEBUG("Waiting for message telling us whether the unmount was "
2066 "successful or not.");
2067 ret = mq_timedreceive(daemon_to_unmount_mq, mailbox, msgsize,
2070 close_message_queues();
2072 if (errno_save == ETIMEDOUT) {
2073 ERROR("Timed out- probably the filesystem daemon "
2074 "crashed and the WIM was not written "
2076 return WIMLIB_ERR_TIMEOUT;
2078 ERROR("mq_receive(): %s", strerror(errno_save));
2079 return WIMLIB_ERR_MQUEUE;
2083 DEBUG("Received message: %s",
2084 (mailbox[0] == 0) ? "Unmount OK" : "Unmount Failed");
2085 if (mailbox[0] != 0)
2086 ERROR("Unmount failed");
2090 #else /* WITH_FUSE */
2093 static inline int mount_unsupported_error()
2095 ERROR("WIMLIB was compiled with --without-fuse, which disables support "
2096 "for mounting WIMs.");
2097 return WIMLIB_ERR_UNSUPPORTED;
2100 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
2102 return mount_unsupported_error();
2105 WIMLIBAPI int wimlib_mount(WIMStruct *wim_p, int image, const char *dir,
2106 int flags, WIMStruct **additional_swms,
2107 unsigned num_additional_swms)
2109 return mount_unsupported_error();
2112 #endif /* WITH_FUSE */