4 * This file implements mounting of WIM files using FUSE, which stands for
5 * Filesystem in Userspace. FUSE allows a filesystem to be implemented in a
6 * userspace process by implementing the filesystem primitives--- read(),
7 * write(), readdir(), etc.
11 * Copyright (C) 2012 Eric Biggers
13 * This file is part of wimlib, a library for working with WIM files.
15 * wimlib is free software; you can redistribute it and/or modify it under the
16 * terms of the GNU Lesser General Public License as published by the Free
17 * Software Foundation; either version 2.1 of the License, or (at your option)
20 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
21 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
22 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
25 * You should have received a copy of the GNU Lesser General Public License
26 * along with wimlib; if not, see http://www.gnu.org/licenses/.
29 #include "wimlib_internal.h"
33 #include "lookup_table.h"
36 #include "timestamp.h"
40 #define FUSE_USE_VERSION 26
47 #include <attr/xattr.h>
53 struct lookup_table_entry *lte;
54 struct dentry *dentry;
57 /* The WIMStruct for the mounted WIM. */
60 /* Working directory when `imagex mount' is run. */
61 static const char *working_directory;
63 /* Name of the staging directory for a read-write mount. Whenever a new file is
64 * created, it is done so in the staging directory. Furthermore, whenever a
65 * file in the WIM is modified, it is extracted to the staging directory. If
66 * changes are commited when the WIM is unmounted, the file resources are merged
67 * in from the staging directory when writing the new WIM. */
68 static char *staging_dir_name;
69 static size_t staging_dir_name_len;
71 /* Flags passed to wimlib_mount(). */
72 static int mount_flags;
74 /* Name of the directory on which the WIM file is mounted. */
75 static const char *mount_dir;
77 /* Next hard link group ID to be assigned. These are also used as the inode
79 static u64 next_link_group_id;
81 /* List of lookup table entries in the staging directory */
82 static LIST_HEAD(staging_list);
84 static inline int get_lookup_flags()
86 if (mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
87 return LOOKUP_FLAG_ADS_OK;
92 /* Returns nonzero if write permission is requested on the file open flags */
93 static inline int flags_writable(int open_flags)
95 return open_flags & (O_RDWR | O_WRONLY);
99 * Allocate a file descriptor for a lookup table entry
101 static int alloc_wimlib_fd(struct lookup_table_entry *lte,
102 struct wimlib_fd **fd_ret)
104 static const u16 fds_per_alloc = 8;
105 static const u16 max_fds = 0xffff;
107 if (lte->num_opened_fds == lte->num_allocated_fds) {
108 struct wimlib_fd **fds;
111 if (lte->num_allocated_fds == max_fds)
113 num_new_fds = min(fds_per_alloc, max_fds - lte->num_allocated_fds);
115 fds = REALLOC(lte->fds, (lte->num_allocated_fds + num_new_fds) *
116 sizeof(lte->fds[0]));
119 memset(&fds[lte->num_allocated_fds], 0,
120 num_new_fds * sizeof(fds[0]));
122 lte->num_allocated_fds += num_new_fds;
124 for (u16 i = 0; ; i++) {
126 struct wimlib_fd *fd = CALLOC(1, sizeof(*fd));
133 lte->num_opened_fds++;
140 static int close_wimlib_fd(struct wimlib_fd *fd)
142 struct lookup_table_entry *lte = fd->lte;
145 wimlib_assert(lte->num_opened_fds);
147 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
148 wimlib_assert(lte->staging_file_name);
149 wimlib_assert(fd->staging_fd != -1);
150 if (close(fd->staging_fd) != 0)
153 if (--lte->num_opened_fds == 0 && lte->refcnt == 0) {
154 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
155 wimlib_assert(lte->staging_file_name);
156 unlink(lte->staging_file_name);
158 free_lookup_table_entry(lte);
160 wimlib_assert(lte->fds[fd->idx] == fd);
161 lte->fds[fd->idx] = NULL;
166 /* Remove a dentry and all its alternate file streams */
167 static void remove_dentry(struct dentry *dentry,
168 struct lookup_table *lookup_table)
170 wimlib_assert(dentry);
171 wimlib_assert(dentry->resolved);
173 struct lookup_table_entry *lte = dentry->lte;
176 lte = lte_decrement_refcnt(lte, lookup_table);
177 if (lte && lte->num_opened_fds)
178 for (u16 i = 0; i < lte->num_allocated_fds; i++)
179 if (lte->fds[i] && lte->fds[i]->dentry == dentry)
180 lte->fds[i]->dentry = NULL;
181 if (i == dentry->num_ads)
183 lte = dentry->ads_entries[i].lte;
187 unlink_dentry(dentry);
191 static void remove_ads(struct dentry *dentry,
192 struct ads_entry *ads_entry,
193 struct lookup_table *lookup_table)
195 struct lookup_table_entry *lte;
197 wimlib_assert(dentry->resolved);
199 lte = lte_decrement_refcnt(lte, lookup_table);
201 list_del(&ads_entry->lte_group_list.list);
202 dentry_remove_ads(dentry, ads_entry);
205 /* Transfers file attributes from a struct dentry to a `stat' buffer. */
206 int dentry_to_stbuf(const struct dentry *dentry, struct stat *stbuf)
208 struct lookup_table_entry *lte;
210 if (dentry_is_symlink(dentry))
211 stbuf->st_mode = S_IFLNK | 0777;
212 else if (dentry_is_directory(dentry))
213 stbuf->st_mode = S_IFDIR | 0755;
215 stbuf->st_mode = S_IFREG | 0644;
217 stbuf->st_ino = (ino_t)dentry->hard_link;
219 stbuf->st_nlink = dentry_link_group_size(dentry);
220 stbuf->st_uid = getuid();
221 stbuf->st_gid = getgid();
223 /* Use the size of the unnamed (default) file stream. */
224 lte = dentry_first_lte_resolved(dentry);
226 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
227 wimlib_assert(lte->staging_file_name);
228 struct stat native_stat;
229 if (stat(lte->staging_file_name, &native_stat) != 0) {
230 DEBUG("Failed to stat `%s': %m",
231 lte->staging_file_name);
234 stbuf->st_size = native_stat.st_size;
236 stbuf->st_size = lte->resource_entry.original_size;
242 stbuf->st_atime = wim_timestamp_to_unix(dentry->last_access_time);
243 stbuf->st_mtime = wim_timestamp_to_unix(dentry->last_write_time);
244 stbuf->st_ctime = wim_timestamp_to_unix(dentry->creation_time);
245 stbuf->st_blocks = (stbuf->st_size + 511) / 512;
249 /* Creates a new staging file and returns its file descriptor opened for
252 * @name_ret: A location into which the a pointer to the newly allocated name of
253 * the staging file is stored.
254 * @return: The file descriptor for the new file. Returns -1 and sets errno on
255 * error, for any reason possible from the creat() function.
257 static int create_staging_file(char **name_ret, int open_flags)
265 name_len = staging_dir_name_len + 1 + SHA1_HASH_SIZE;
266 name = MALLOC(name_len + 1);
274 memcpy(name, staging_dir_name, staging_dir_name_len);
275 name[staging_dir_name_len] = '/';
276 randomize_char_array_with_alnum(name + staging_dir_name_len + 1,
278 name[name_len] = '\0';
281 /* Just in case, verify that the randomly generated name doesn't name an
282 * existing file, and try again if so */
283 } while (stat(name, &stbuf) == 0);
289 /* doesn't exist--- ok */
291 DEBUG("Creating staging file `%s'", name);
293 fd = open(name, open_flags | O_CREAT | O_TRUNC, 0600);
305 * Removes open file descriptors from a lookup table entry @old_lte where the
306 * file descriptors have opened the corresponding file resource in the context
307 * of the hard link group @link_group; these file descriptors are extracted and
308 * placed in a new lookup table entry, which is returned.
310 static struct lookup_table_entry *
311 lte_extract_fds(struct lookup_table_entry *old_lte, u64 link_group)
314 u16 num_transferred_fds;
315 struct lookup_table_entry *new_lte;
317 new_lte = new_lookup_table_entry();
321 num_transferred_fds = 0;
322 for (u16 i = 0; i < old_lte->num_allocated_fds; i++)
323 if (old_lte->fds[i] && old_lte->fds[i]->dentry &&
324 old_lte->fds[i]->dentry->hard_link == link_group)
325 num_transferred_fds++;
326 DEBUG("Transferring %u file descriptors",
327 num_transferred_fds);
328 new_lte->fds = MALLOC(num_transferred_fds * sizeof(new_lte->fds[0]));
333 for (u16 i = 0, j = 0; ; i++) {
334 if (old_lte->fds[i] && old_lte->fds[i]->dentry &&
335 old_lte->fds[i]->dentry->hard_link == link_group) {
336 struct wimlib_fd *fd = old_lte->fds[i];
337 old_lte->fds[i] = NULL;
340 new_lte->fds[j] = fd;
341 if (++j == num_transferred_fds)
345 DEBUG("old_lte: %u fds open; new_lte: %u fds open",
346 old_lte->num_opened_fds, new_lte->num_opened_fds);
347 old_lte->num_opened_fds -= num_transferred_fds;
348 new_lte->num_opened_fds = num_transferred_fds;
349 new_lte->num_allocated_fds = num_transferred_fds;
354 * Transfers an alternate data stream entry to a new lookup table entry
356 static void lte_transfer_ads_entry(struct lookup_table_entry *new_lte,
357 struct ads_entry *ads_entry)
359 list_del(&ads_entry->lte_group_list.list);
360 list_add(&ads_entry->lte_group_list.list, &new_lte->lte_group_list);
361 ads_entry->lte = new_lte;
365 * Transfers a dentry to a new lookup table entry
367 static void lte_transfer_dentry(struct lookup_table_entry *new_lte,
368 struct dentry *dentry)
370 wimlib_assert(dentry->lte_group_list.list.next);
371 wimlib_assert(new_lte->lte_group_list.next);
372 list_del(&dentry->lte_group_list.list);
373 list_add(&dentry->lte_group_list.list, &new_lte->lte_group_list);
374 dentry->lte = new_lte;
377 static void lte_transfer_stream_entries(struct lookup_table_entry *new_lte,
378 struct dentry *dentry,
381 INIT_LIST_HEAD(&new_lte->lte_group_list);
382 if (stream_idx == 0) {
383 struct list_head *pos = &dentry->link_group_list;
386 d = container_of(pos, struct dentry, link_group_list);
387 wimlib_assert(d->hard_link == dentry->hard_link);
388 lte_transfer_dentry(new_lte, d);
390 } while (pos != &dentry->link_group_list);
392 struct ads_entry *ads_entry;
393 wimlib_assert(stream_idx <= dentry->num_ads);
394 ads_entry = &dentry->ads_entries[stream_idx - 1];
395 lte_transfer_ads_entry(new_lte, ads_entry);
400 * Extract a WIM resource to the staging directory.
402 * @dentry, @stream_idx: The stream on whose behalf we are modifying the lookup
403 * table entry (these may be more streams than this that reference the lookup
406 * @lte: Pointer to pointer to the lookup table entry for the stream we need to
407 * extract, or NULL if there was no lookup table entry present for the stream
409 * @size: Number of bytes of the stream we want to extract (this supports the
410 * wimfs_truncate() function).
412 static int extract_resource_to_staging_dir(struct dentry *dentry,
414 struct lookup_table_entry **lte,
417 char *staging_file_name;
420 struct lookup_table_entry *old_lte, *new_lte;
421 size_t link_group_size;
425 * - Create a staging file for the WIM resource
426 * - Extract the resource to it
427 * - Create a new lte for the file resource
428 * - Transfer fds from the old lte to the new lte, but only if they share the
429 * same hard link group as this dentry. If there is no old lte, then this
430 * step does not need to be done
431 * - Transfer stream entries from the old lte's list to the new lte's list. If
432 * there is no old lte, we instead transfer entries for the hard link group.
434 * Note: *lte is permitted to be NULL, in which case there is no old
435 * lookup table entry.
438 DEBUG("Extracting resource `%s' to staging directory", dentry->full_path_utf8);
441 fd = create_staging_file(&staging_file_name, O_WRONLY);
446 ret = extract_wim_resource_to_fd(old_lte, fd, size);
449 if (ret != 0 || close(fd) != 0) {
455 goto out_delete_staging_file;
458 link_group_size = dentry_link_group_size(dentry);
461 if (link_group_size == old_lte->refcnt) {
462 /* This hard link group is the only user of the lookup
463 * table entry, so we can re-use it. */
464 DEBUG("Re-using lookup table entry");
465 lookup_table_unlink(w->lookup_table, old_lte);
468 DEBUG("Splitting lookup table entry "
469 "(link_group_size = %u, lte refcnt = %u)",
470 link_group_size, old_lte->refcnt);
471 /* Split a hard link group away from the "lookup table
472 * entry" hard link group (i.e. we had two hard link
473 * groups that were identical, but now we are changing
477 * The ADS really complicate things here and not
478 * everything is going to work correctly yet. For
479 * example it could be the same that a file contains two
480 * file streams that are identical and therefore share
481 * the same lookup table entry despite the fact that the
482 * streams themselves are not hardlinked.
484 wimlib_assert(old_lte->refcnt > link_group_size);
486 new_lte = lte_extract_fds(old_lte, dentry->hard_link);
489 goto out_delete_staging_file;
492 lte_transfer_stream_entries(new_lte, dentry, stream_idx);
493 old_lte->refcnt -= link_group_size;
496 /* No old_lte was supplied, so the resource had no lookup table
497 * entry before (it must be an empty resource) */
498 new_lte = new_lookup_table_entry();
501 goto out_delete_staging_file;
503 lte_transfer_stream_entries(new_lte, dentry, stream_idx);
505 new_lte->resource_entry.original_size = size;
506 new_lte->refcnt = link_group_size;
507 random_hash(new_lte->hash);
508 if (new_lte->staging_file_name)
509 FREE(new_lte->staging_file_name);
510 new_lte->staging_file_name = staging_file_name;
511 new_lte->resource_location = RESOURCE_IN_STAGING_FILE;
513 lookup_table_insert(w->lookup_table, new_lte);
514 list_add(&new_lte->staging_list, &staging_list);
517 out_delete_staging_file:
518 unlink(staging_file_name);
519 FREE(staging_file_name);
524 * Creates a randomly named staging directory and returns its name into the
525 * static variable staging_dir_name.
527 * If the staging directory cannot be created, staging_dir_name is set to NULL.
529 static void make_staging_dir()
531 /* XXX Give the user an option of where to stage files */
533 static char prefix[] = "wimlib-staging-";
534 static const size_t prefix_len = 15;
535 static const size_t suffix_len = 10;
537 size_t pwd_len = strlen(working_directory);
539 staging_dir_name_len = pwd_len + 1 + prefix_len + suffix_len;
541 staging_dir_name = MALLOC(staging_dir_name_len + 1);
542 if (!staging_dir_name) {
543 ERROR("Out of memory");
547 memcpy(staging_dir_name, working_directory, pwd_len);
548 staging_dir_name[pwd_len] = '/';
549 memcpy(staging_dir_name + pwd_len + 1, prefix, prefix_len);
550 randomize_char_array_with_alnum(staging_dir_name + pwd_len + 1 + prefix_len,
552 staging_dir_name[staging_dir_name_len] = '\0';
554 if (mkdir(staging_dir_name, 0700) != 0) {
555 ERROR_WITH_ERRNO("Failed to create temporary directory `%s'",
557 FREE(staging_dir_name);
558 staging_dir_name = NULL;
562 static int remove_file_or_directory(const char *fpath, const struct stat *sb,
563 int typeflag, struct FTW *ftwbuf)
565 if (remove(fpath) == 0)
568 return WIMLIB_ERR_DELETE_STAGING_DIR;
573 * Deletes the staging directory and all the files contained in it.
575 static inline int delete_staging_dir()
579 ret = nftw(staging_dir_name, remove_file_or_directory,10, FTW_DEPTH);
580 staging_dir_name = NULL;
584 /* Name and message queue descriptors for message queues between the filesystem
585 * daemon process and the unmount process. These are used when the filesystem
586 * is unmounted and the process running wimlib_mount() (i.e. the `imagex
587 * unmount' command) needs to communicate with the filesystem daemon running
588 * fuse_main() (i.e. that spawned by the `imagex mount' or `imagex mountrw'
590 static char *unmount_to_daemon_mq_name;
591 static char *daemon_to_unmount_mq_name;
592 static int unmount_to_daemon_mq;
593 static int daemon_to_unmount_mq;
595 /* Simple function that returns the concatenation of 4 strings. */
596 static char *strcat_dup(const char *s1, const char *s2, const char *s3,
599 size_t len = strlen(s1) + strlen(s2) + strlen(s3) + strlen(s4) + 1;
600 char *p = MALLOC(len);
611 /* Removes trailing forward slashes in a string. */
612 static void remove_trailing_slashes(char *s)
614 long len = strlen(s);
615 for (long i = len - 1; i >= 1; i--) {
623 /* Changes forward slashes to underscores in a string. */
624 static void s_slashes_underscores_g(char *s)
634 * Opens two POSIX message queue: one for sending messages from the unmount
635 * process to the daemon process, and one to go the other way. The names of the
636 * message queues, which must be system-wide unique, are be based on the mount
637 * point. (There of course is still a possibility of a collision if one were to
638 * unmount two identically named directories simultaneously...)
640 * @daemon specifies whether the calling process is the filesystem daemon or the
643 static int open_message_queues(bool daemon)
645 static const char *slash = "/";
646 static const char *prefix = "wimlib-";
647 static const char *u2d_suffix = "unmount-to-daemon-mq";
648 static const char *d2u_suffix = "daemon-to-unmount-mq";
650 const char *mount_dir_basename = path_basename(mount_dir);
654 unmount_to_daemon_mq_name = strcat_dup(slash, mount_dir_basename,
656 if (!unmount_to_daemon_mq_name) {
657 ERROR("Out of memory");
658 return WIMLIB_ERR_NOMEM;
660 daemon_to_unmount_mq_name = strcat_dup(slash, mount_dir_basename,
662 if (!daemon_to_unmount_mq_name) {
663 ERROR("Out of memory");
664 ret = WIMLIB_ERR_NOMEM;
668 remove_trailing_slashes(unmount_to_daemon_mq_name);
669 remove_trailing_slashes(daemon_to_unmount_mq_name);
670 s_slashes_underscores_g(unmount_to_daemon_mq_name + 1);
671 s_slashes_underscores_g(daemon_to_unmount_mq_name + 1);
674 flags = O_RDONLY | O_CREAT;
676 flags = O_WRONLY | O_CREAT;
678 unmount_to_daemon_mq = mq_open(unmount_to_daemon_mq_name, flags,
681 if (unmount_to_daemon_mq == -1) {
682 ERROR_WITH_ERRNO("mq_open()");
683 ret = WIMLIB_ERR_MQUEUE;
688 flags = O_WRONLY | O_CREAT;
690 flags = O_RDONLY | O_CREAT;
692 daemon_to_unmount_mq = mq_open(daemon_to_unmount_mq_name, flags,
695 if (daemon_to_unmount_mq == -1) {
696 ERROR_WITH_ERRNO("mq_open()");
697 ret = WIMLIB_ERR_MQUEUE;
702 mq_close(unmount_to_daemon_mq);
703 mq_unlink(unmount_to_daemon_mq_name);
705 FREE(daemon_to_unmount_mq_name);
707 FREE(unmount_to_daemon_mq_name);
711 static int mq_get_msgsize(mqd_t mq)
713 static const char *msgsize_max_file = "/proc/sys/fs/mqueue/msgsize_max";
718 if (mq_getattr(unmount_to_daemon_mq, &attr) == 0) {
719 msgsize = attr.mq_msgsize;
721 ERROR_WITH_ERRNO("mq_getattr()");
722 ERROR("Attempting to read %s", msgsize_max_file);
723 fp = fopen(msgsize_max_file, "rb");
725 if (fscanf(fp, "%d", &msgsize) != 1) {
726 ERROR("Assuming message size of 8192");
731 ERROR_WITH_ERRNO("Failed to open the file `%s'",
733 ERROR("Assuming message size of 8192");
740 /* Closes the message queues, which are allocated in static variables */
741 static void close_message_queues()
743 mq_close(unmount_to_daemon_mq);
744 mq_close(daemon_to_unmount_mq);
745 mq_unlink(unmount_to_daemon_mq_name);
746 mq_unlink(daemon_to_unmount_mq_name);
749 static int wimfs_access(const char *path, int mask)
751 /* XXX Permissions not implemented */
755 /* Closes the staging file descriptor associated with the lookup table entry, if
757 static int close_lte_fds(struct lookup_table_entry *lte)
759 for (u16 i = 0, j = 0; j < lte->num_opened_fds; i++) {
760 if (lte->fds[i] && lte->fds[i]->staging_fd != -1) {
761 wimlib_assert(lte->resource_location == RESOURCE_IN_STAGING_FILE);
762 wimlib_assert(lte->staging_file_name);
763 if (close(lte->fds[i]->staging_fd) != 0) {
764 ERROR_WITH_ERRNO("Failed close file `%s'",
765 lte->staging_file_name);
766 return WIMLIB_ERR_WRITE;
774 static void lte_list_change_lte_ptr(struct lookup_table_entry *lte,
775 struct lookup_table_entry *newptr)
777 struct list_head *pos;
778 struct stream_list_head *head;
779 list_for_each(pos, <e->lte_group_list) {
780 head = container_of(pos, struct stream_list_head, list);
781 if (head->type == STREAM_TYPE_ADS) {
782 struct ads_entry *ads_entry;
783 ads_entry = container_of(head, struct ads_entry, lte_group_list);
785 ads_entry->lte = newptr;
787 wimlib_assert(head->type == STREAM_TYPE_NORMAL);
789 struct dentry *dentry;
790 dentry = container_of(head, struct dentry, lte_group_list);
792 dentry->lte = newptr;
798 static int update_lte_of_staging_file(struct lookup_table_entry *lte,
799 struct lookup_table *table)
801 struct lookup_table_entry *duplicate_lte;
803 u8 hash[SHA1_HASH_SIZE];
806 wimlib_assert(lte->resource_location == RESOURCE_IN_STAGING_FILE);
807 wimlib_assert(lte->staging_file_name);
809 ret = sha1sum(lte->staging_file_name, hash);
813 lookup_table_unlink(table, lte);
815 duplicate_lte = __lookup_resource(table, hash);
818 /* Merge duplicate lookup table entries */
820 lte_list_change_lte_ptr(lte, duplicate_lte);
821 duplicate_lte->refcnt += lte->refcnt;
822 list_splice(<e->lte_group_list,
823 &duplicate_lte->lte_group_list);
825 free_lookup_table_entry(lte);
827 if (stat(lte->staging_file_name, &stbuf) != 0) {
828 ERROR_WITH_ERRNO("Failed to stat `%s'", lte->staging_file_name);
829 return WIMLIB_ERR_STAT;
831 wimlib_assert(<e->file_on_disk == <e->staging_file_name);
832 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
833 copy_hash(lte->hash, hash);
834 lte->resource_entry.original_size = stbuf.st_size;
835 lte->resource_entry.size = stbuf.st_size;
836 lookup_table_insert(table, lte);
842 /* Overwrites the WIM file, with changes saved. */
843 static int rebuild_wim(WIMStruct *w, bool check_integrity)
846 struct lookup_table_entry *lte, *tmp;
848 /* Close all the staging file descriptors. */
849 DEBUG("Closing all staging file descriptors.");
850 list_for_each_entry(lte, &staging_list, staging_list) {
851 ret = close_lte_fds(lte);
856 /* Calculate SHA1 checksums for all staging files, and merge unnecessary
857 * lookup table entries. */
858 DEBUG("Calculating SHA1 checksums for all new staging files.");
859 list_for_each_entry_safe(lte, tmp, &staging_list, staging_list) {
860 ret = update_lte_of_staging_file(lte, w->lookup_table);
867 xml_update_image_info(w, w->current_image);
869 ret = wimlib_overwrite(w, check_integrity);
871 ERROR("Failed to commit changes");
877 /* Called when the filesystem is unmounted. */
878 static void wimfs_destroy(void *p)
880 /* For read-write mounts, the `imagex unmount' command, which is
881 * running in a separate process and is executing the
882 * wimlib_unmount() function, will send this process a byte
883 * through a message queue that indicates whether the --commit
884 * option was specified or not. */
887 struct timespec timeout;
889 ssize_t bytes_received;
892 char check_integrity;
895 ret = open_message_queues(true);
899 msgsize = mq_get_msgsize(unmount_to_daemon_mq);
904 /* Wait at most 3 seconds before giving up and discarding changes. */
905 gettimeofday(&now, NULL);
906 timeout.tv_sec = now.tv_sec + 3;
907 timeout.tv_nsec = now.tv_usec * 1000;
908 DEBUG("Waiting for message telling us whether to commit or not, and "
909 "whether to include integrity checks.");
911 bytes_received = mq_timedreceive(unmount_to_daemon_mq, msg,
912 msgsize, NULL, &timeout);
914 check_integrity = msg[1];
915 if (bytes_received == -1) {
916 if (errno == ETIMEDOUT) {
919 ERROR_WITH_ERRNO("mq_timedreceive()");
921 ERROR("Not committing.");
923 DEBUG("Received message: [%d %d]", msg[0], msg[1]);
927 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
929 status = chdir(working_directory);
931 ERROR_WITH_ERRNO("chdir()");
932 status = WIMLIB_ERR_NOTDIR;
935 status = rebuild_wim(w, (check_integrity != 0));
937 ret = delete_staging_dir();
939 ERROR_WITH_ERRNO("Failed to delete the staging "
945 DEBUG("Read-only mount");
948 DEBUG("Sending status %u", status);
949 ret = mq_send(daemon_to_unmount_mq, &status, 1, 1);
951 ERROR_WITH_ERRNO("Failed to send status to unmount process");
952 close_message_queues();
955 static int wimfs_fallocate(const char *path, int mode,
956 off_t offset, off_t len, struct fuse_file_info *fi)
958 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
959 wimlib_assert(fd->staging_fd != -1);
960 return fallocate(fd->staging_fd, mode, offset, len);
963 static int wimfs_fgetattr(const char *path, struct stat *stbuf,
964 struct fuse_file_info *fi)
966 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
967 return dentry_to_stbuf(fd->dentry, stbuf);
970 static int wimfs_ftruncate(const char *path, off_t size,
971 struct fuse_file_info *fi)
973 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
974 int ret = ftruncate(fd->staging_fd, size);
977 fd->lte->resource_entry.original_size = size;
982 * Fills in a `struct stat' that corresponds to a file or directory in the WIM.
984 static int wimfs_getattr(const char *path, struct stat *stbuf)
986 const char *stream_name;
988 struct dentry *dentry;
991 ret = lookup_resource(w, path,
992 get_lookup_flags() | LOOKUP_FLAG_DIRECTORY_OK,
993 &dentry, NULL, NULL);
996 return dentry_to_stbuf(dentry, stbuf);
999 /* Read an alternate data stream through the XATTR interface, or get its size */
1000 static int wimfs_getxattr(const char *path, const char *name, char *value,
1004 struct dentry *dentry;
1005 struct ads_entry *ads_entry;
1007 struct lookup_table_entry *lte;
1009 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1012 if (memcmp(name, "user.", 5) != 0)
1016 dentry = get_dentry(w, path);
1019 ads_entry = dentry_get_ads_entry(dentry, name);
1023 lte = ads_entry->lte;
1024 res_size = wim_resource_size(lte);
1028 if (res_size > size)
1030 ret = read_full_wim_resource(lte, value);
1036 /* Create a hard link */
1037 static int wimfs_link(const char *to, const char *from)
1039 struct dentry *to_dentry, *from_dentry, *from_dentry_parent;
1040 const char *link_name;
1042 to_dentry = get_dentry(w, to);
1045 if (!dentry_is_regular_file(to_dentry))
1048 from_dentry_parent = get_parent_dentry(w, from);
1049 if (!from_dentry_parent)
1051 if (!dentry_is_directory(from_dentry_parent))
1054 link_name = path_basename(from);
1055 if (get_dentry_child_with_name(from_dentry_parent, link_name))
1057 from_dentry = clone_dentry(to_dentry);
1060 if (change_dentry_name(from_dentry, link_name) != 0) {
1065 /* Add the new dentry to the dentry list for the link group */
1066 list_add(&from_dentry->link_group_list, &to_dentry->link_group_list);
1068 /* Increment reference counts for the unnamed file stream and all
1069 * alternate data streams. */
1070 if (from_dentry->lte) {
1071 list_add(&from_dentry->lte_group_list.list,
1072 &to_dentry->lte_group_list.list);
1073 from_dentry->lte->refcnt++;
1075 for (u16 i = 0; i < from_dentry->num_ads; i++) {
1076 struct ads_entry *ads_entry = &from_dentry->ads_entries[i];
1078 ads_entry->lte->refcnt++;
1081 /* The ADS entries are owned by another dentry. */
1082 from_dentry->ads_entries_status = ADS_ENTRIES_USER;
1084 link_dentry(from_dentry, from_dentry_parent);
1088 static int wimfs_listxattr(const char *path, char *list, size_t size)
1090 struct dentry *dentry;
1095 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1098 /* List alternate data streams, or get the list size */
1100 ret = lookup_resource(w, path, get_lookup_flags(), &dentry, NULL, NULL);
1105 for (i = 0; i < dentry->num_ads; i++)
1106 needed_size += dentry->ads_entries[i].stream_name_utf8_len + 6;
1109 for (i = 0; i < dentry->num_ads; i++) {
1110 needed_size = dentry->ads_entries[i].stream_name_utf8_len + 6;
1111 if (needed_size > size)
1113 p += sprintf(p, "user.%s",
1114 dentry->ads_entries[i].stream_name_utf8) + 1;
1115 size -= needed_size;
1122 * Create a directory in the WIM.
1123 * @mode is currently ignored.
1125 static int wimfs_mkdir(const char *path, mode_t mode)
1127 struct dentry *parent;
1128 struct dentry *newdir;
1129 const char *basename;
1131 parent = get_parent_dentry(w, path);
1135 if (!dentry_is_directory(parent))
1138 basename = path_basename(path);
1139 if (get_dentry_child_with_name(parent, basename))
1142 newdir = new_dentry(basename);
1143 newdir->attributes |= FILE_ATTRIBUTE_DIRECTORY;
1144 newdir->resolved = true;
1145 newdir->hard_link = next_link_group_id++;
1146 link_dentry(newdir, parent);
1151 /* Creates a regular file. */
1152 static int wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
1154 const char *stream_name;
1155 const char *file_name;
1156 if ((mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
1157 && (stream_name = path_stream_name(path))) {
1158 /* Make an alternate data stream */
1159 struct ads_entry *new_entry;
1160 struct dentry *dentry;
1162 char *p = (char*)stream_name - 1;
1163 wimlib_assert(*p == ':');
1166 dentry = get_dentry(w, path);
1167 if (!dentry || !dentry_is_regular_file(dentry))
1169 if (dentry_get_ads_entry(dentry, stream_name))
1171 new_entry = dentry_add_ads(dentry, stream_name);
1175 struct dentry *dentry, *parent;
1176 const char *basename;
1178 /* Make a normal file (not an alternate data stream) */
1180 /* Make sure that the parent of @path exists and is a directory, and
1181 * that the dentry named by @path does not already exist. */
1182 parent = get_parent_dentry(w, path);
1185 if (!dentry_is_directory(parent))
1188 basename = path_basename(path);
1189 if (get_dentry_child_with_name(parent, path))
1192 dentry = new_dentry(basename);
1195 dentry->resolved = true;
1196 dentry->hard_link = next_link_group_id++;
1197 dentry->lte_group_list.type = STREAM_TYPE_NORMAL;
1198 INIT_LIST_HEAD(&dentry->lte_group_list.list);
1199 link_dentry(dentry, parent);
1206 static int wimfs_open(const char *path, struct fuse_file_info *fi)
1208 struct dentry *dentry;
1209 struct lookup_table_entry *lte;
1212 struct wimlib_fd *fd;
1213 unsigned stream_idx;
1215 ret = lookup_resource(w, path, get_lookup_flags(), &dentry, <e,
1221 /* Empty file with no lookup-table entry. This is fine if it's
1222 * a read-only filesystem. Otherwise we need to create a lookup
1223 * table entry so that we can keep track of the file descriptors
1224 * (this is important in case someone opens the file for
1226 if (!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
1231 ret = extract_resource_to_staging_dir(dentry, stream_idx,
1237 ret = alloc_wimlib_fd(lte, &fd);
1241 fd->dentry = dentry;
1243 /* The file resource may be in the staging directory (read-write
1244 * mounts only) or in the WIM. If it's in the staging
1245 * directory, we need to open a native file descriptor for the
1246 * corresponding file. Otherwise, we can read the file resource
1247 * directly from the WIM file if we are opening it read-only,
1248 * but we need to extract the resource to the staging directory
1249 * if we are opening it writable. */
1250 if (flags_writable(fi->flags) &&
1251 lte->resource_location != RESOURCE_IN_STAGING_FILE) {
1252 ret = extract_resource_to_staging_dir(dentry, stream_idx, <e,
1253 lte->resource_entry.original_size);
1257 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1258 fd->staging_fd = open(lte->staging_file_name, fi->flags);
1259 if (fd->staging_fd == -1) {
1260 close_wimlib_fd(fd);
1264 fi->fh = (uint64_t)fd;
1268 /* Opens a directory. */
1269 static int wimfs_opendir(const char *path, struct fuse_file_info *fi)
1271 struct dentry *dentry;
1273 dentry = get_dentry(w, path);
1276 if (!dentry_is_directory(dentry))
1278 dentry->num_times_opened++;
1279 fi->fh = (uint64_t)dentry;
1285 * Read data from a file in the WIM or in the staging directory.
1287 static int wimfs_read(const char *path, char *buf, size_t size,
1288 off_t offset, struct fuse_file_info *fi)
1290 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
1293 /* Empty file with no lookup table entry on read-only mounted
1295 wimlib_assert(!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE));
1299 wimlib_assert(fd->lte);
1301 if (fd->lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1302 /* Read from staging file */
1304 wimlib_assert(fd->lte->staging_file_name);
1305 wimlib_assert(fd->staging_fd != -1);
1308 DEBUG("Seek to offset %zu", offset);
1310 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1312 ret = read(fd->staging_fd, buf, size);
1319 const struct resource_entry *res_entry;
1321 res_entry = &fd->lte->resource_entry;
1323 if (offset > res_entry->original_size)
1326 size = min(size, res_entry->original_size - offset);
1328 if (read_wim_resource(fd->lte, buf, size, offset, false) != 0)
1334 /* Fills in the entries of the directory specified by @path using the
1335 * FUSE-provided function @filler. */
1336 static int wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
1337 off_t offset, struct fuse_file_info *fi)
1339 struct dentry *parent, *child;
1341 parent = (struct dentry*)fi->fh;
1342 wimlib_assert(parent);
1343 child = parent->children;
1345 filler(buf, ".", NULL, 0);
1346 filler(buf, "..", NULL, 0);
1352 if (filler(buf, child->file_name_utf8, NULL, 0))
1354 child = child->next;
1355 } while (child != parent->children);
1360 static int wimfs_readlink(const char *path, char *buf, size_t buf_len)
1362 struct dentry *dentry = get_dentry(w, path);
1366 if (!dentry_is_symlink(dentry))
1369 ret = dentry_readlink(dentry, buf, buf_len, w);
1376 static int wimfs_release(const char *path, struct fuse_file_info *fi)
1379 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
1382 /* Empty file with no lookup table entry on read-only mounted
1384 wimlib_assert(!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE));
1388 if (flags_writable(fi->flags) && fd->dentry) {
1389 u64 now = get_wim_timestamp();
1390 fd->dentry->last_access_time = now;
1391 fd->dentry->last_write_time = now;
1394 return close_wimlib_fd(fd);
1397 static int wimfs_releasedir(const char *path, struct fuse_file_info *fi)
1399 struct dentry *dentry = (struct dentry *)fi->fh;
1401 wimlib_assert(dentry);
1402 wimlib_assert(dentry->num_times_opened);
1403 if (--dentry->num_times_opened == 0)
1404 free_dentry(dentry);
1408 /* Remove an alternate data stream through the XATTR interface */
1409 static int wimfs_removexattr(const char *path, const char *name)
1411 struct dentry *dentry;
1412 struct ads_entry *ads_entry;
1414 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1417 if (memcmp(name, "user.", 5) != 0)
1421 dentry = get_dentry(w, path);
1425 ads_entry = dentry_get_ads_entry(dentry, name);
1428 remove_ads(dentry, ads_entry, w->lookup_table);
1432 /* Renames a file or directory. See rename (3) */
1433 static int wimfs_rename(const char *from, const char *to)
1437 struct dentry *parent_of_dst;
1438 char *file_name_utf16 = NULL, *file_name_utf8 = NULL;
1439 u16 file_name_utf16_len, file_name_utf8_len;
1442 /* This rename() implementation currently only supports actual files
1443 * (not alternate data streams) */
1445 src = get_dentry(w, from);
1449 dst = get_dentry(w, to);
1452 ret = get_names(&file_name_utf16, &file_name_utf8,
1453 &file_name_utf16_len, &file_name_utf8_len,
1459 /* Destination file exists */
1461 if (src == dst) /* Same file */
1464 if (!dentry_is_directory(src)) {
1465 /* Cannot rename non-directory to directory. */
1466 if (dentry_is_directory(dst))
1469 /* Cannot rename directory to a non-directory or a non-empty
1471 if (!dentry_is_directory(dst))
1473 if (dst->children != NULL)
1476 parent_of_dst = dst->parent;
1477 remove_dentry(dst, w->lookup_table);
1479 /* Destination does not exist */
1480 parent_of_dst = get_parent_dentry(w, to);
1484 if (!dentry_is_directory(parent_of_dst))
1488 FREE(src->file_name);
1489 FREE(src->file_name_utf8);
1490 src->file_name = file_name_utf16;
1491 src->file_name_utf8 = file_name_utf8;
1492 src->file_name_len = file_name_utf16_len;
1493 src->file_name_utf8_len = file_name_utf8_len;
1496 link_dentry(src, parent_of_dst);
1500 /* Remove a directory */
1501 static int wimfs_rmdir(const char *path)
1503 struct dentry *dentry;
1505 dentry = get_dentry(w, path);
1509 if (!dentry_is_empty_directory(dentry))
1512 unlink_dentry(dentry);
1513 if (dentry->num_times_opened == 0)
1514 free_dentry(dentry);
1518 /* Write an alternate data stream through the XATTR interface */
1519 static int wimfs_setxattr(const char *path, const char *name,
1520 const char *value, size_t size, int flags)
1522 struct dentry *dentry;
1523 struct ads_entry *existing_ads_entry;
1524 struct ads_entry *new_ads_entry;
1525 struct lookup_table_entry *existing_lte;
1526 struct lookup_table_entry *lte;
1527 u8 value_hash[SHA1_HASH_SIZE];
1531 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1534 dentry = get_dentry(w, path);
1537 existing_ads_entry = dentry_get_ads_entry(dentry, name);
1538 if (existing_ads_entry) {
1539 if (flags & XATTR_CREATE)
1541 remove_ads(dentry, existing_ads_entry, w->lookup_table);
1543 if (flags & XATTR_REPLACE)
1546 new_ads_entry = dentry_add_ads(dentry, name);
1550 sha1_buffer(value, size, value_hash);
1552 existing_lte = __lookup_resource(w->lookup_table, value_hash);
1559 lte = new_lookup_table_entry();
1562 value_copy = MALLOC(size);
1567 lte->resource_location = RESOURCE_IN_ATTACHED_BUFFER;
1568 lte->attached_buffer = value_copy;
1569 lte->resource_entry.original_size = size;
1570 lte->resource_entry.size = size;
1571 lte->resource_entry.flags = 0;
1572 copy_hash(lte->hash, value_hash);
1573 lookup_table_insert(w->lookup_table, lte);
1575 new_ads_entry->lte = lte;
1579 static int wimfs_symlink(const char *to, const char *from)
1581 struct dentry *dentry_parent, *dentry;
1582 const char *link_name;
1583 struct lookup_table_entry *lte;
1585 dentry_parent = get_parent_dentry(w, from);
1588 if (!dentry_is_directory(dentry_parent))
1591 link_name = path_basename(from);
1593 if (get_dentry_child_with_name(dentry_parent, link_name))
1595 dentry = new_dentry(link_name);
1599 dentry->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
1600 dentry->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
1601 dentry->hard_link = next_link_group_id++;
1603 if (dentry_set_symlink(dentry, to, w->lookup_table, <e) != 0)
1604 goto out_free_dentry;
1608 dentry->ads_entries[1].lte_group_list.type = STREAM_TYPE_ADS;
1609 list_add(&dentry->ads_entries[1].lte_group_list.list,
1610 <e->lte_group_list);
1611 wimlib_assert(dentry->resolved);
1613 link_dentry(dentry, dentry_parent);
1616 free_dentry(dentry);
1621 /* Reduce the size of a file */
1622 static int wimfs_truncate(const char *path, off_t size)
1624 struct dentry *dentry;
1625 struct lookup_table_entry *lte;
1627 unsigned stream_idx;
1629 ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1635 if (!lte) /* Already a zero-length file */
1638 if (lte->staging_file_name) {
1639 ret = truncate(lte->staging_file_name, size);
1642 lte->resource_entry.original_size = size;
1644 /* File in WIM. Extract it to the staging directory, but only
1645 * the first @size bytes of it. */
1646 ret = extract_resource_to_staging_dir(dentry, stream_idx,
1649 dentry_update_all_timestamps(dentry);
1653 /* Remove a regular file */
1654 static int wimfs_unlink(const char *path)
1656 struct dentry *dentry;
1657 struct lookup_table_entry *lte;
1660 unsigned stream_idx;
1662 ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1668 if (stream_idx == 0) {
1669 /* We are removing the full dentry including all alternate data
1671 remove_dentry(dentry, w->lookup_table);
1673 /* We are removing an alternate data stream. */
1674 remove_ads(dentry, &dentry->ads_entries[stream_idx - 1],
1677 /* Beware: The lookup table entry(s) may still be referenced by users
1678 * that have opened the corresponding streams. They are freed later in
1679 * wimfs_release() when the last file user has closed the stream. */
1684 * Change the timestamp on a file dentry.
1686 * Note that alternate data streams do not have their own timestamps.
1688 static int wimfs_utimens(const char *path, const struct timespec tv[2])
1690 struct dentry *dentry = get_dentry(w, path);
1693 if (tv[0].tv_nsec != UTIME_OMIT) {
1694 if (tv[0].tv_nsec == UTIME_NOW)
1695 dentry->last_access_time = get_wim_timestamp();
1697 dentry->last_access_time = timespec_to_wim_timestamp(&tv[0]);
1699 if (tv[1].tv_nsec != UTIME_OMIT) {
1700 if (tv[1].tv_nsec == UTIME_NOW)
1701 dentry->last_write_time = get_wim_timestamp();
1703 dentry->last_write_time = timespec_to_wim_timestamp(&tv[1]);
1708 /* Writes to a file in the WIM filesystem.
1709 * It may be an alternate data stream, but here we don't even notice because we
1710 * just get a lookup table entry. */
1711 static int wimfs_write(const char *path, const char *buf, size_t size,
1712 off_t offset, struct fuse_file_info *fi)
1714 struct wimlib_fd *fd = (struct wimlib_fd*)fi->fh;
1718 wimlib_assert(fd->lte);
1719 wimlib_assert(fd->lte->staging_file_name);
1720 wimlib_assert(fd->staging_fd != -1);
1722 /* Seek to the requested position */
1723 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1726 /* Write the data. */
1727 ret = write(fd->staging_fd, buf, size);
1734 static struct fuse_operations wimfs_operations = {
1735 .access = wimfs_access,
1736 .destroy = wimfs_destroy,
1737 .fallocate = wimfs_fallocate,
1738 .fgetattr = wimfs_fgetattr,
1739 .ftruncate = wimfs_ftruncate,
1740 .getattr = wimfs_getattr,
1741 .getxattr = wimfs_getxattr,
1743 .listxattr = wimfs_listxattr,
1744 .mkdir = wimfs_mkdir,
1745 .mknod = wimfs_mknod,
1747 .opendir = wimfs_opendir,
1749 .readdir = wimfs_readdir,
1750 .readlink = wimfs_readlink,
1751 .release = wimfs_release,
1752 .releasedir = wimfs_releasedir,
1753 .removexattr = wimfs_removexattr,
1754 .rename = wimfs_rename,
1755 .rmdir = wimfs_rmdir,
1756 .setxattr = wimfs_setxattr,
1757 .symlink = wimfs_symlink,
1758 .truncate = wimfs_truncate,
1759 .unlink = wimfs_unlink,
1760 .utimens = wimfs_utimens,
1761 .write = wimfs_write,
1765 static int check_lte_refcnt(struct lookup_table_entry *lte, void *ignore)
1767 size_t lte_group_size = 0;
1768 struct list_head *cur;
1769 list_for_each(cur, <e->lte_group_list)
1771 if (lte_group_size > lte->refcnt) {
1772 #ifdef ENABLE_ERROR_MESSAGES
1773 ERROR("The following lookup table entry has a reference count "
1774 "of %u, but", lte->refcnt);
1775 ERROR("We found %u references to it", lte_group_size);
1776 print_lookup_table_entry(lte);
1778 return WIMLIB_ERR_INVALID_DENTRY;
1783 /* Mounts a WIM file. */
1784 WIMLIBAPI int wimlib_mount(WIMStruct *wim, int image, const char *dir,
1792 DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
1793 wim, image, dir, flags);
1796 return WIMLIB_ERR_INVALID_PARAM;
1798 ret = wimlib_select_image(wim, image);
1803 DEBUG("Selected image %d", image);
1805 next_link_group_id = assign_link_groups(wim->image_metadata[image - 1].lgt);
1807 /* Resolve all the lookup table entries of the dentry tree */
1808 for_dentry_in_tree(wim_root_dentry(wim), dentry_resolve_ltes,
1811 ret = for_lookup_table_entry(wim->lookup_table, check_lte_refcnt, NULL);
1815 if (flags & WIMLIB_MOUNT_FLAG_READWRITE)
1816 wim_get_current_image_metadata(wim)->modified = true;
1818 if (!(flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
1819 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
1820 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
1821 flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
1824 working_directory = getcwd(NULL, 0);
1825 if (!working_directory) {
1826 ERROR_WITH_ERRNO("Could not determine current directory");
1827 return WIMLIB_ERR_NOTDIR;
1832 return WIMLIB_ERR_NOMEM;
1834 argv[argc++] = "imagex";
1836 argv[argc++] = "-s"; /* disable multi-threaded operation */
1838 if (flags & WIMLIB_MOUNT_FLAG_DEBUG)
1839 argv[argc++] = "-d";
1842 * We provide the use_ino option because we are going to assign inode
1843 * numbers oursides. We've already numbered the hard link groups with
1844 * unique numbers with the assign_link_groups() function, and the static
1845 * variable next_link_group_id is set to the next available link group
1846 * ID that we will assign to new dentries.
1848 char optstring[256] = "use_ino";
1849 argv[argc++] = "-o";
1850 argv[argc++] = optstring;
1851 if ((flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
1852 /* Read-write mount. Make the staging directory */
1854 if (!staging_dir_name) {
1856 return WIMLIB_ERR_MKDIR;
1859 /* Read-only mount */
1860 strcat(optstring, ",ro");
1867 DEBUG("FUSE command line (argc = %d): ", argc);
1868 for (i = 0; i < argc; i++) {
1869 fputs(argv[i], stdout);
1877 /* Set static variables. */
1879 mount_flags = flags;
1881 ret = fuse_main(argc, argv, &wimfs_operations, NULL);
1883 return (ret == 0) ? 0 : WIMLIB_ERR_FUSE;
1888 * Unmounts the WIM file that was previously mounted on @dir by using
1891 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
1898 struct timespec timeout;
1902 /* Execute `fusermount -u', which is installed setuid root, to unmount
1905 * FUSE does not yet implement synchronous unmounts. This means that
1906 * fusermount -u will return before the filesystem daemon returns from
1907 * wimfs_destroy(). This is partly what we want, because we need to
1908 * send a message from this process to the filesystem daemon telling
1909 * whether --commit was specified or not. However, after that, the
1910 * unmount process must wait for the filesystem daemon to finish writing
1917 ERROR_WITH_ERRNO("Failed to fork()");
1918 return WIMLIB_ERR_FORK;
1921 execlp("fusermount", "fusermount", "-u", dir, NULL);
1922 ERROR_WITH_ERRNO("Failed to execute `fusermount'");
1923 return WIMLIB_ERR_FUSERMOUNT;
1926 ret = waitpid(pid, &status, 0);
1928 ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
1930 return WIMLIB_ERR_FUSERMOUNT;
1934 ERROR("fusermount exited with status %d", status);
1935 return WIMLIB_ERR_FUSERMOUNT;
1938 /* Open message queues between the unmount process and the
1939 * filesystem daemon. */
1940 ret = open_message_queues(false);
1944 /* Send a message to the filesystem saying whether to commit or
1946 msg[0] = (flags & WIMLIB_UNMOUNT_FLAG_COMMIT) ? 1 : 0;
1947 msg[1] = (flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY) ? 1 : 0;
1949 DEBUG("Sending message: %s, %s",
1950 (msg[0] == 0) ? "don't commit" : "commit",
1951 (msg[1] == 0) ? "don't check" : "check");
1952 ret = mq_send(unmount_to_daemon_mq, msg, 2, 1);
1954 ERROR("Failed to notify filesystem daemon whether we want to "
1955 "commit changes or not");
1956 close_message_queues();
1957 return WIMLIB_ERR_MQUEUE;
1960 /* Wait for a message from the filesytem daemon indicating whether the
1961 * filesystem was unmounted successfully (0) or an error occurred (1).
1962 * This may take a long time if a big WIM file needs to be rewritten. */
1964 /* Wait at most 600??? seconds before giving up and returning false.
1965 * Either it's a really big WIM file, or (more likely) the
1966 * filesystem daemon has crashed or failed for some reason.
1968 * XXX come up with some method to determine if the filesystem
1969 * daemon has really crashed or not.
1971 * XXX Idea: have mount daemon write its PID into the WIM file header?
1974 gettimeofday(&now, NULL);
1975 timeout.tv_sec = now.tv_sec + 600;
1976 timeout.tv_nsec = now.tv_usec * 1000;
1978 msgsize = mq_get_msgsize(daemon_to_unmount_mq);
1979 char mailbox[msgsize];
1982 DEBUG("Waiting for message telling us whether the unmount was "
1983 "successful or not.");
1984 ret = mq_timedreceive(daemon_to_unmount_mq, mailbox, msgsize,
1987 close_message_queues();
1989 if (errno_save == ETIMEDOUT) {
1990 ERROR("Timed out- probably the filesystem daemon "
1991 "crashed and the WIM was not written "
1993 return WIMLIB_ERR_TIMEOUT;
1995 ERROR("mq_receive(): %s", strerror(errno_save));
1996 return WIMLIB_ERR_MQUEUE;
2000 DEBUG("Received message: %s",
2001 (mailbox[0] == 0) ? "Unmount OK" : "Unmount Failed");
2002 if (mailbox[0] != 0)
2003 ERROR("Unmount failed");
2007 #else /* WITH_FUSE */
2010 static inline int mount_unsupported_error()
2012 ERROR("WIMLIB was compiled with --without-fuse, which disables support "
2013 "for mounting WIMs.");
2014 return WIMLIB_ERR_UNSUPPORTED;
2017 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
2019 return mount_unsupported_error();
2022 WIMLIBAPI int wimlib_mount(WIMStruct *wim_p, int image, const char *dir,
2025 return mount_unsupported_error();
2028 #endif /* WITH_FUSE */