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"
34 #include "lookup_table.h"
37 #include "timestamp.h"
41 #define FUSE_USE_VERSION 26
51 #include <attr/xattr.h>
54 /* File descriptor to a file open on the WIM filesystem. */
56 /* Index of this file descriptor in the lookup table entry's table of
60 /* Index of the stream we've opened from a dentry */
63 /* Native file descriptor to the staging file */
66 /* Pointer to lookup table entry that has the table of file descriptors
67 * containing this file descriptor */
68 struct lookup_table_entry *lte;
71 struct dentry *dentry;
72 struct ads_entry *ads_entry;
75 /* Hard link group ID of the dentry containing the stream that was
76 * opened to produce this file descriptor.
78 * This is always valid even if @dentry is NULL. It can be used to
79 * identify file descriptors corresponding to the same hard-linked
80 * stream, even if the specific dentry under which the stream was opened
81 * has been unlinked. */
85 static inline struct dentry *fd_dentry(const struct wimlib_fd *fd)
87 if (fd->stream_idx == 0)
90 return fd->ads_entry ? fd->ads_entry->dentry : NULL;
93 /* The WIMStruct for the mounted WIM. */
96 /* Working directory when `imagex mount' is run. */
97 static const char *working_directory;
99 /* Name of the staging directory for a read-write mount. Whenever a new file is
100 * created, it is done so in the staging directory. Furthermore, whenever a
101 * file in the WIM is modified, it is extracted to the staging directory. If
102 * changes are commited when the WIM is unmounted, the file resources are merged
103 * in from the staging directory when writing the new WIM. */
104 static char *staging_dir_name;
105 static size_t staging_dir_name_len;
107 /* Flags passed to wimlib_mount(). */
108 static int mount_flags;
110 /* Name of the directory on which the WIM file is mounted. */
111 static const char *mount_dir;
113 /* Next hard link group ID to be assigned. These are also used as the inode
115 static u64 next_link_group_id;
117 /* List of lookup table entries in the staging directory */
118 static LIST_HEAD(staging_list);
120 static inline int get_lookup_flags()
122 if (mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
123 return LOOKUP_FLAG_ADS_OK;
128 /* Returns nonzero if write permission is requested on the file open flags */
129 static inline int flags_writable(int open_flags)
131 return open_flags & (O_RDWR | O_WRONLY);
135 * Allocate a file descriptor for a lookup table entry
137 static int alloc_wimlib_fd(struct lookup_table_entry *lte,
138 struct wimlib_fd **fd_ret)
140 static const u16 fds_per_alloc = 8;
141 static const u16 max_fds = 0xffff;
143 if (lte->num_opened_fds == lte->num_allocated_fds) {
144 struct wimlib_fd **fds;
147 if (lte->num_allocated_fds == max_fds)
149 num_new_fds = min(fds_per_alloc, max_fds - lte->num_allocated_fds);
151 fds = REALLOC(lte->fds, (lte->num_allocated_fds + num_new_fds) *
152 sizeof(lte->fds[0]));
155 memset(&fds[lte->num_allocated_fds], 0,
156 num_new_fds * sizeof(fds[0]));
158 lte->num_allocated_fds += num_new_fds;
160 for (u16 i = 0; ; i++) {
162 struct wimlib_fd *fd = CALLOC(1, sizeof(*fd));
169 lte->num_opened_fds++;
176 static int close_wimlib_fd(struct wimlib_fd *fd)
178 struct lookup_table_entry *lte = fd->lte;
181 wimlib_assert(lte->num_opened_fds);
183 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
184 wimlib_assert(lte->staging_file_name);
185 wimlib_assert(fd->staging_fd != -1);
186 if (close(fd->staging_fd) != 0)
189 if (--lte->num_opened_fds == 0 && lte->refcnt == 0) {
190 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
191 wimlib_assert(lte->staging_file_name);
192 unlink(lte->staging_file_name);
194 free_lookup_table_entry(lte);
196 wimlib_assert(lte->fds[fd->idx] == fd);
197 lte->fds[fd->idx] = NULL;
202 /* Remove a dentry and all its alternate file streams */
203 static void remove_dentry(struct dentry *dentry,
204 struct lookup_table *lookup_table)
206 wimlib_assert(dentry);
207 wimlib_assert(dentry->resolved);
209 if (dentry->ads_entries_status == ADS_ENTRIES_OWNER) {
210 struct dentry *new_owner;
211 list_for_each_entry(new_owner, &dentry->link_group_list,
214 if (new_owner->ads_entries_status == ADS_ENTRIES_USER) {
215 new_owner->ads_entries_status = ADS_ENTRIES_OWNER;
216 for (u16 i = 0; i < dentry->num_ads; i++)
217 dentry->ads_entries[i]->dentry = new_owner;
221 dentry->ads_entries_status = ADS_ENTRIES_USER;
223 list_del(&dentry->link_group_list);
224 list_del(&dentry->lte_group_list);
227 struct lookup_table_entry *lte = dentry->lte;
229 lte = lte_decrement_refcnt(lte, lookup_table);
230 if (lte && lte->num_opened_fds)
231 for (u16 j = 0; j < lte->num_allocated_fds; j++)
233 lte->fds[j]->link_group_id == dentry->link_group_id
234 && ((lte->fds[j].stream_idx == 0 &&
235 lte->fds[j].dentry == dentry) ||
236 (lte->fds[j].stream_idx &&
237 lte->fds[j].ads_entry &&
238 lte->fds[j].ads_entry->dentry == dentry)))
239 lte->fds[j].dentry = NULL;
240 if (i == dentry->num_ads)
242 lte = dentry->ads_entries[i].lte;
246 unlink_dentry(dentry);
250 static void remove_ads(struct dentry *dentry,
251 struct ads_entry *ads_entry,
252 struct lookup_table *lookup_table)
254 struct lookup_table_entry *lte;
256 wimlib_assert(dentry->resolved);
257 wimlib_assert(ads_entry - dentry->ads_entries < dentry->num_ads);
259 lte = lte_decrement_refcnt(ads_entry->lte, lookup_table);
261 for (u16 i = 0; i < lte->num_allocated_fds; i++)
262 if (lte->fds[i] && lte->fds[i]->dentry == dentry)
263 lte->fds[i]->dentry = NULL;
265 /* Fix up file descriptor stream indexes */
266 for (u16 i = ads_entry - dentry->ads_entries + 1; i < dentry->num_ads; i++) {
267 struct lookup_table_entry *lte = ads_entry[i].lte;
269 for (u16 open_fd_idx = 0, fd_idx = 0;
270 open_fd_idx < lte->num_opened_fds; fd_idx++)
272 if (lte->fds[fd_idx]) {
274 if (lte->fds[fd_idx]->dentry == dentry
275 && lte->fds[fd_idx]->stream_idx > idx)
276 lte->fds[fd_idx]->stream_id
281 dentry_remove_ads(dentry, ads_entry);
284 /* Transfers file attributes from a struct dentry to a `stat' buffer. */
285 int dentry_to_stbuf(const struct dentry *dentry, struct stat *stbuf)
287 struct lookup_table_entry *lte;
289 if (dentry_is_symlink(dentry))
290 stbuf->st_mode = S_IFLNK | 0777;
291 else if (dentry_is_directory(dentry))
292 stbuf->st_mode = S_IFDIR | 0755;
294 stbuf->st_mode = S_IFREG | 0644;
296 stbuf->st_ino = (ino_t)dentry->link_group_id;
298 stbuf->st_nlink = dentry_link_group_size(dentry);
299 stbuf->st_uid = getuid();
300 stbuf->st_gid = getgid();
302 /* Use the size of the unnamed (default) file stream. */
303 lte = dentry_unnamed_lte_resolved(dentry);
305 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
306 wimlib_assert(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE);
307 wimlib_assert(lte->staging_file_name);
308 struct stat native_stat;
309 if (stat(lte->staging_file_name, &native_stat) != 0) {
310 DEBUG("Failed to stat `%s': %m",
311 lte->staging_file_name);
314 stbuf->st_size = native_stat.st_size;
316 stbuf->st_size = lte->resource_entry.original_size;
322 stbuf->st_atime = wim_timestamp_to_unix(dentry->last_access_time);
323 stbuf->st_mtime = wim_timestamp_to_unix(dentry->last_write_time);
324 stbuf->st_ctime = wim_timestamp_to_unix(dentry->creation_time);
325 stbuf->st_blocks = (stbuf->st_size + 511) / 512;
329 /* Creates a new staging file and returns its file descriptor opened for
332 * @name_ret: A location into which the a pointer to the newly allocated name of
333 * the staging file is stored.
334 * @return: The file descriptor for the new file. Returns -1 and sets errno on
335 * error, for any reason possible from the creat() function.
337 static int create_staging_file(char **name_ret, int open_flags)
345 name_len = staging_dir_name_len + 1 + SHA1_HASH_SIZE;
346 name = MALLOC(name_len + 1);
354 memcpy(name, staging_dir_name, staging_dir_name_len);
355 name[staging_dir_name_len] = '/';
356 randomize_char_array_with_alnum(name + staging_dir_name_len + 1,
358 name[name_len] = '\0';
361 /* Just in case, verify that the randomly generated name doesn't name an
362 * existing file, and try again if so */
363 } while (stat(name, &stbuf) == 0);
369 /* doesn't exist--- ok */
371 DEBUG("Creating staging file `%s'", name);
373 fd = open(name, open_flags | O_CREAT | O_TRUNC, 0600);
385 * Removes open file descriptors from a lookup table entry @old_lte where the
386 * removed file descriptors have opened the corresponding file resource in the
387 * context of a dentry in the hard link group @link_group and a stream at index
388 * @stream_idx. These file descriptors are extracted and placed in a new lookup
389 * table entry, which is returned.
391 * Note we need to examine the link_group_id of each file descriptor and not
392 * dentry->link_group_id of each file descriptor, since dentry may be NULL in
393 * the case of an un-linked dentry.
395 static struct lookup_table_entry *
396 lte_extract_fds(struct lookup_table_entry *old_lte, u64 link_group_id,
399 u16 num_transferred_fds;
400 struct lookup_table_entry *new_lte;
402 new_lte = new_lookup_table_entry();
406 num_transferred_fds = 0;
407 for (u16 i = 0; i < old_lte->num_allocated_fds; i++)
409 && old_lte->fds[i]->link_group_id == link_group_id
410 && old_lte->fds[i]->stream_idx == stream_idx)
411 num_transferred_fds++;
412 DEBUG("Transferring %u file descriptors",
413 num_transferred_fds);
414 new_lte->fds = MALLOC(num_transferred_fds * sizeof(new_lte->fds[0]));
419 for (u16 i = 0, j = 0; ; i++) {
421 && old_lte->fds[i]->link_group_id == link_group_id
422 && old_lte->fds[i]->stream_idx == stream_idx) {
423 struct wimlib_fd *fd = old_lte->fds[i];
424 old_lte->fds[i] = NULL;
427 new_lte->fds[j] = fd;
428 if (++j == num_transferred_fds)
432 DEBUG("old_lte: %u fds open; new_lte: %u fds open",
433 old_lte->num_opened_fds, new_lte->num_opened_fds);
434 old_lte->num_opened_fds -= num_transferred_fds;
435 new_lte->num_opened_fds = num_transferred_fds;
436 new_lte->num_allocated_fds = num_transferred_fds;
441 * Transfers an alternate data stream entry to a new lookup table entry
443 static void lte_transfer_ads_entry(struct lookup_table_entry *new_lte,
444 struct ads_entry *ads_entry)
446 list_del(&ads_entry->lte_group_list.list);
447 list_add(&ads_entry->lte_group_list.list, &new_lte->lte_group_list);
448 ads_entry->lte = new_lte;
452 * Transfers a dentry to a new lookup table entry
454 static void lte_transfer_dentry(struct lookup_table_entry *new_lte,
455 struct dentry *dentry)
457 wimlib_assert(dentry->lte_group_list.list.next);
458 wimlib_assert(new_lte->lte_group_list.next);
459 list_del(&dentry->lte_group_list.list);
460 list_add(&dentry->lte_group_list.list, &new_lte->lte_group_list);
461 dentry->lte = new_lte;
464 static void lte_transfer_stream_entries(struct lookup_table_entry *new_lte,
465 struct dentry *dentry,
468 INIT_LIST_HEAD(&new_lte->lte_group_list);
469 if (stream_idx == 0) {
470 struct list_head *pos = &dentry->link_group_list;
473 d = container_of(pos, struct dentry, link_group_list);
474 wimlib_assert(d->link_group_id == dentry->link_group_id);
475 lte_transfer_dentry(new_lte, d);
477 } while (pos != &dentry->link_group_list);
479 struct ads_entry *ads_entry;
480 wimlib_assert(stream_idx <= dentry->num_ads);
481 ads_entry = &dentry->ads_entries[stream_idx - 1];
482 lte_transfer_ads_entry(new_lte, ads_entry);
487 * Extract a WIM resource to the staging directory.
489 * @dentry, @stream_idx: The stream on whose behalf we are modifying the lookup
490 * table entry (these may be more streams than this that reference the lookup
493 * @lte: Pointer to pointer to the lookup table entry for the stream we need to
494 * extract, or NULL if there was no lookup table entry present for the stream
496 * @size: Number of bytes of the stream we want to extract (this supports the
497 * wimfs_truncate() function).
499 static int extract_resource_to_staging_dir(struct dentry *dentry,
501 struct lookup_table_entry **lte,
504 char *staging_file_name;
507 struct lookup_table_entry *old_lte, *new_lte;
508 size_t link_group_size;
512 * - Create a staging file for the WIM resource
513 * - Extract the resource to it
514 * - Create a new lte for the file resource
515 * - Transfer fds from the old lte to the new lte, but only if they share the
516 * same hard link group as this dentry. If there is no old lte, then this
517 * step does not need to be done
518 * - Transfer stream entries from the old lte's list to the new lte's list. If
519 * there is no old lte, we instead transfer entries for the hard link group.
521 * Note: *lte is permitted to be NULL, in which case there is no old
522 * lookup table entry.
525 DEBUG("Extracting dentry `%s' stream %u to staging directory",
526 dentry->full_path_utf8, stream_idx);
529 fd = create_staging_file(&staging_file_name, O_WRONLY);
534 ret = extract_wim_resource_to_fd(old_lte, fd, size);
537 if (ret != 0 || close(fd) != 0) {
543 goto out_delete_staging_file;
546 link_group_size = dentry_link_group_size(dentry);
549 wimlib_assert(old_lte->resource_location == RESOURCE_IN_WIM);
550 if (link_group_size == old_lte->refcnt) {
551 /* The reference count of the existing lookup table
552 * entry is the same as the size of the hard link group
553 * associated with the dentry; therefore, ALL the
554 * references to the lookup table entry correspond to
555 * the stream we're trying to extract. So the lookup
556 * table entry can be re-used.
558 DEBUG("Re-using lookup table entry");
559 lookup_table_unlink(w->lookup_table, old_lte);
562 DEBUG("Splitting lookup table entry "
563 "(link_group_size = %zu, lte refcnt = %u)",
564 link_group_size, old_lte->refcnt);
565 /* The stream we are going to change does not account
566 * for all the references to the lookup table entry.
567 * The other references to the lookup table entry may be
568 * from different hard link groups or from other streams
569 * in the same hard link group.
572 wimlib_assert(old_lte->refcnt > link_group_size);
574 /* First, find the old lookup table entry's file
575 * descriptors that were opened in the context of the
576 * stream we're going to change, and allocate a new
577 * lookup table entry containing those file descriptors.
579 new_lte = lte_extract_fds(old_lte, link_group_id,
583 goto out_delete_staging_file;
586 lte_transfer_stream_entries(new_lte, dentry, stream_idx);
587 old_lte->refcnt -= link_group_size;
590 /* No old_lte was supplied, so the resource had no lookup table
591 * entry before (it must be an empty resource) */
592 new_lte = new_lookup_table_entry();
595 goto out_delete_staging_file;
597 lte_transfer_stream_entries(new_lte, dentry, stream_idx);
599 new_lte->resource_entry.original_size = size;
600 new_lte->refcnt = link_group_size;
601 random_hash(new_lte->hash);
602 new_lte->staging_file_name = staging_file_name;
603 new_lte->resource_location = RESOURCE_IN_STAGING_FILE;
605 lookup_table_insert(w->lookup_table, new_lte);
606 list_add(&new_lte->staging_list, &staging_list);
609 out_delete_staging_file:
610 unlink(staging_file_name);
611 FREE(staging_file_name);
616 * Creates a randomly named staging directory and returns its name into the
617 * static variable staging_dir_name.
619 * If the staging directory cannot be created, staging_dir_name is set to NULL.
621 static void make_staging_dir()
623 /* XXX Give the user an option of where to stage files */
625 static char prefix[] = "wimlib-staging-";
626 static const size_t prefix_len = 15;
627 static const size_t suffix_len = 10;
629 size_t pwd_len = strlen(working_directory);
631 staging_dir_name_len = pwd_len + 1 + prefix_len + suffix_len;
633 staging_dir_name = MALLOC(staging_dir_name_len + 1);
634 if (!staging_dir_name) {
635 ERROR("Out of memory");
639 memcpy(staging_dir_name, working_directory, pwd_len);
640 staging_dir_name[pwd_len] = '/';
641 memcpy(staging_dir_name + pwd_len + 1, prefix, prefix_len);
642 randomize_char_array_with_alnum(staging_dir_name + pwd_len + 1 + prefix_len,
644 staging_dir_name[staging_dir_name_len] = '\0';
646 if (mkdir(staging_dir_name, 0700) != 0) {
647 ERROR_WITH_ERRNO("Failed to create temporary directory `%s'",
649 FREE(staging_dir_name);
650 staging_dir_name = NULL;
654 static int remove_file_or_directory(const char *fpath, const struct stat *sb,
655 int typeflag, struct FTW *ftwbuf)
657 if (remove(fpath) == 0)
660 return WIMLIB_ERR_DELETE_STAGING_DIR;
665 * Deletes the staging directory and all the files contained in it.
667 static int delete_staging_dir()
671 ret = nftw(staging_dir_name, remove_file_or_directory,10, FTW_DEPTH);
672 staging_dir_name = NULL;
676 /* Name and message queue descriptors for message queues between the filesystem
677 * daemon process and the unmount process. These are used when the filesystem
678 * is unmounted and the process running wimlib_mount() (i.e. the `imagex
679 * unmount' command) needs to communicate with the filesystem daemon running
680 * fuse_main() (i.e. that spawned by the `imagex mount' or `imagex mountrw'
682 static char *unmount_to_daemon_mq_name;
683 static char *daemon_to_unmount_mq_name;
684 static mqd_t unmount_to_daemon_mq;
685 static mqd_t daemon_to_unmount_mq;
687 /* Simple function that returns the concatenation of 4 strings. */
688 static char *strcat_dup(const char *s1, const char *s2, const char *s3,
691 size_t len = strlen(s1) + strlen(s2) + strlen(s3) + strlen(s4) + 1;
692 char *p = MALLOC(len);
698 return strcat(p, s4);
701 /* Removes trailing forward slashes in a string. */
702 static void remove_trailing_slashes(char *s)
704 long len = strlen(s);
705 for (long i = len - 1; i >= 1; i--) {
713 /* Changes forward slashes to underscores in a string. */
714 static void s_slashes_underscores_g(char *s)
724 * Opens two POSIX message queue: one for sending messages from the unmount
725 * process to the daemon process, and one to go the other way. The names of the
726 * message queues, which must be system-wide unique, are be based on the mount
727 * point. (There of course is still a possibility of a collision if one were to
728 * unmount two identically named directories simultaneously...)
730 * @daemon specifies whether the calling process is the filesystem daemon or the
733 static int open_message_queues(bool daemon)
735 static const char *slash = "/";
736 static const char *prefix = "wimlib-";
737 static const char *u2d_suffix = "unmount-to-daemon-mq";
738 static const char *d2u_suffix = "daemon-to-unmount-mq";
740 const char *mount_dir_basename = path_basename(mount_dir);
744 unmount_to_daemon_mq_name = strcat_dup(slash, mount_dir_basename,
746 if (!unmount_to_daemon_mq_name) {
747 ERROR("Out of memory");
748 return WIMLIB_ERR_NOMEM;
750 daemon_to_unmount_mq_name = strcat_dup(slash, mount_dir_basename,
752 if (!daemon_to_unmount_mq_name) {
753 ERROR("Out of memory");
754 ret = WIMLIB_ERR_NOMEM;
758 remove_trailing_slashes(unmount_to_daemon_mq_name);
759 remove_trailing_slashes(daemon_to_unmount_mq_name);
760 s_slashes_underscores_g(unmount_to_daemon_mq_name + 1);
761 s_slashes_underscores_g(daemon_to_unmount_mq_name + 1);
764 flags = O_RDONLY | O_CREAT;
766 flags = O_WRONLY | O_CREAT;
768 unmount_to_daemon_mq = mq_open(unmount_to_daemon_mq_name, flags,
771 if (unmount_to_daemon_mq == (mqd_t)-1) {
772 ERROR_WITH_ERRNO("mq_open()");
773 ret = WIMLIB_ERR_MQUEUE;
778 flags = O_WRONLY | O_CREAT;
780 flags = O_RDONLY | O_CREAT;
782 daemon_to_unmount_mq = mq_open(daemon_to_unmount_mq_name, flags,
785 if (daemon_to_unmount_mq == (mqd_t)-1) {
786 ERROR_WITH_ERRNO("mq_open()");
787 ret = WIMLIB_ERR_MQUEUE;
792 mq_close(unmount_to_daemon_mq);
793 mq_unlink(unmount_to_daemon_mq_name);
795 FREE(daemon_to_unmount_mq_name);
797 FREE(unmount_to_daemon_mq_name);
801 static int mq_get_msgsize(mqd_t mq)
803 static const char *msgsize_max_file = "/proc/sys/fs/mqueue/msgsize_max";
808 if (mq_getattr(unmount_to_daemon_mq, &attr) == 0) {
809 msgsize = attr.mq_msgsize;
811 ERROR_WITH_ERRNO("mq_getattr()");
812 ERROR("Attempting to read %s", msgsize_max_file);
813 fp = fopen(msgsize_max_file, "rb");
815 if (fscanf(fp, "%d", &msgsize) != 1) {
816 ERROR("Assuming message size of 8192");
821 ERROR_WITH_ERRNO("Failed to open the file `%s'",
823 ERROR("Assuming message size of 8192");
830 /* Closes the message queues, which are allocated in static variables */
831 static void close_message_queues()
833 mq_close(unmount_to_daemon_mq);
834 mq_close(daemon_to_unmount_mq);
835 mq_unlink(unmount_to_daemon_mq_name);
836 mq_unlink(daemon_to_unmount_mq_name);
839 static int wimfs_access(const char *path, int mask)
841 /* XXX Permissions not implemented */
845 /* Closes the staging file descriptor associated with the lookup table entry, if
847 static int close_lte_fds(struct lookup_table_entry *lte)
849 for (u16 i = 0, j = 0; j < lte->num_opened_fds; i++) {
850 if (lte->fds[i] && lte->fds[i]->staging_fd != -1) {
851 wimlib_assert(lte->resource_location == RESOURCE_IN_STAGING_FILE);
852 wimlib_assert(lte->staging_file_name);
853 if (close(lte->fds[i]->staging_fd) != 0) {
854 ERROR_WITH_ERRNO("Failed close file `%s'",
855 lte->staging_file_name);
856 return WIMLIB_ERR_WRITE;
864 static void lte_list_change_lte_ptr(struct lookup_table_entry *lte,
865 struct lookup_table_entry *newptr)
867 struct list_head *pos;
868 struct stream_list_head *head;
869 list_for_each(pos, <e->lte_group_list) {
870 head = container_of(pos, struct stream_list_head, list);
871 if (head->type == STREAM_TYPE_ADS) {
872 struct ads_entry *ads_entry;
873 ads_entry = container_of(head, struct ads_entry, lte_group_list);
875 ads_entry->lte = newptr;
877 wimlib_assert(head->type == STREAM_TYPE_NORMAL);
879 struct dentry *dentry;
880 dentry = container_of(head, struct dentry, lte_group_list);
882 dentry->lte = newptr;
888 static int update_lte_of_staging_file(struct lookup_table_entry *lte,
889 struct lookup_table *table)
891 struct lookup_table_entry *duplicate_lte;
893 u8 hash[SHA1_HASH_SIZE];
896 wimlib_assert(lte->resource_location == RESOURCE_IN_STAGING_FILE);
897 wimlib_assert(lte->staging_file_name);
899 ret = sha1sum(lte->staging_file_name, hash);
903 lookup_table_unlink(table, lte);
905 duplicate_lte = __lookup_resource(table, hash);
908 /* Merge duplicate lookup table entries */
910 lte_list_change_lte_ptr(lte, duplicate_lte);
911 duplicate_lte->refcnt += lte->refcnt;
912 list_splice(<e->lte_group_list,
913 &duplicate_lte->lte_group_list);
915 free_lookup_table_entry(lte);
917 if (stat(lte->staging_file_name, &stbuf) != 0) {
918 ERROR_WITH_ERRNO("Failed to stat `%s'", lte->staging_file_name);
919 return WIMLIB_ERR_STAT;
921 wimlib_assert(<e->file_on_disk == <e->staging_file_name);
922 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
923 copy_hash(lte->hash, hash);
924 lte->resource_entry.original_size = stbuf.st_size;
925 lte->resource_entry.size = stbuf.st_size;
926 lookup_table_insert(table, lte);
932 /* Overwrites the WIM file, with changes saved. */
933 static int rebuild_wim(WIMStruct *w, bool check_integrity)
936 struct lookup_table_entry *lte, *tmp;
938 /* Close all the staging file descriptors. */
939 DEBUG("Closing all staging file descriptors.");
940 list_for_each_entry(lte, &staging_list, staging_list) {
941 ret = close_lte_fds(lte);
946 /* Calculate SHA1 checksums for all staging files, and merge unnecessary
947 * lookup table entries. */
948 DEBUG("Calculating SHA1 checksums for all new staging files.");
949 list_for_each_entry_safe(lte, tmp, &staging_list, staging_list) {
950 ret = update_lte_of_staging_file(lte, w->lookup_table);
955 xml_update_image_info(w, w->current_image);
957 ret = wimlib_overwrite(w, check_integrity);
959 ERROR("Failed to commit changes");
965 /* Called when the filesystem is unmounted. */
966 static void wimfs_destroy(void *p)
968 /* For read-write mounts, the `imagex unmount' command, which is
969 * running in a separate process and is executing the
970 * wimlib_unmount() function, will send this process a byte
971 * through a message queue that indicates whether the --commit
972 * option was specified or not. */
975 struct timespec timeout;
977 ssize_t bytes_received;
980 char check_integrity;
983 ret = open_message_queues(true);
987 msgsize = mq_get_msgsize(unmount_to_daemon_mq);
992 /* Wait at most 3 seconds before giving up and discarding changes. */
993 gettimeofday(&now, NULL);
994 timeout.tv_sec = now.tv_sec + 3;
995 timeout.tv_nsec = now.tv_usec * 1000;
996 DEBUG("Waiting for message telling us whether to commit or not, and "
997 "whether to include integrity checks.");
999 bytes_received = mq_timedreceive(unmount_to_daemon_mq, msg,
1000 msgsize, NULL, &timeout);
1002 check_integrity = msg[1];
1003 if (bytes_received == -1) {
1004 if (errno == ETIMEDOUT) {
1005 ERROR("Timed out.");
1007 ERROR_WITH_ERRNO("mq_timedreceive()");
1009 ERROR("Not committing.");
1011 DEBUG("Received message: [%d %d]", msg[0], msg[1]);
1015 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1017 status = chdir(working_directory);
1019 ERROR_WITH_ERRNO("chdir()");
1020 status = WIMLIB_ERR_NOTDIR;
1023 status = rebuild_wim(w, (check_integrity != 0));
1025 ret = delete_staging_dir();
1027 ERROR_WITH_ERRNO("Failed to delete the staging "
1033 DEBUG("Read-only mount");
1036 DEBUG("Sending status %u", status);
1037 ret = mq_send(daemon_to_unmount_mq, &status, 1, 1);
1039 ERROR_WITH_ERRNO("Failed to send status to unmount process");
1040 close_message_queues();
1044 static int wimfs_fallocate(const char *path, int mode,
1045 off_t offset, off_t len, struct fuse_file_info *fi)
1047 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1048 wimlib_assert(fd->staging_fd != -1);
1049 return fallocate(fd->staging_fd, mode, offset, len);
1052 static int wimfs_fgetattr(const char *path, struct stat *stbuf,
1053 struct fuse_file_info *fi)
1055 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1056 return dentry_to_stbuf(fd->dentry, stbuf);
1060 static int wimfs_ftruncate(const char *path, off_t size,
1061 struct fuse_file_info *fi)
1063 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1064 int ret = ftruncate(fd->staging_fd, size);
1067 fd->lte->resource_entry.original_size = size;
1072 * Fills in a `struct stat' that corresponds to a file or directory in the WIM.
1074 static int wimfs_getattr(const char *path, struct stat *stbuf)
1076 struct dentry *dentry;
1079 ret = lookup_resource(w, path,
1080 get_lookup_flags() | LOOKUP_FLAG_DIRECTORY_OK,
1081 &dentry, NULL, NULL);
1084 return dentry_to_stbuf(dentry, stbuf);
1088 /* Read an alternate data stream through the XATTR interface, or get its size */
1089 static int wimfs_getxattr(const char *path, const char *name, char *value,
1093 struct dentry *dentry;
1094 struct ads_entry *ads_entry;
1096 struct lookup_table_entry *lte;
1098 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1101 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1105 dentry = get_dentry(w, path);
1108 ads_entry = dentry_get_ads_entry(dentry, name);
1112 lte = ads_entry->lte;
1113 res_size = wim_resource_size(lte);
1117 if (res_size > size)
1119 ret = read_full_wim_resource(lte, (u8*)value);
1126 /* Create a hard link */
1127 static int wimfs_link(const char *to, const char *from)
1129 struct dentry *to_dentry, *from_dentry, *from_dentry_parent;
1130 const char *link_name;
1132 to_dentry = get_dentry(w, to);
1135 if (!dentry_is_regular_file(to_dentry))
1138 from_dentry_parent = get_parent_dentry(w, from);
1139 if (!from_dentry_parent)
1141 if (!dentry_is_directory(from_dentry_parent))
1144 link_name = path_basename(from);
1145 if (get_dentry_child_with_name(from_dentry_parent, link_name))
1147 from_dentry = clone_dentry(to_dentry);
1150 if (change_dentry_name(from_dentry, link_name) != 0) {
1155 /* Add the new dentry to the dentry list for the link group */
1156 list_add(&from_dentry->link_group_list, &to_dentry->link_group_list);
1158 /* Increment reference counts for the unnamed file stream and all
1159 * alternate data streams. */
1160 if (from_dentry->lte) {
1161 list_add(&from_dentry->lte_group_list.list,
1162 &to_dentry->lte_group_list.list);
1163 from_dentry->lte->refcnt++;
1165 for (u16 i = 0; i < from_dentry->num_ads; i++) {
1166 struct ads_entry *ads_entry = &from_dentry->ads_entries[i];
1168 ads_entry->lte->refcnt++;
1171 /* The ADS entries are owned by another dentry. */
1172 from_dentry->ads_entries_status = ADS_ENTRIES_USER;
1174 link_dentry(from_dentry, from_dentry_parent);
1179 static int wimfs_listxattr(const char *path, char *list, size_t size)
1181 struct dentry *dentry;
1186 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1189 /* List alternate data streams, or get the list size */
1191 ret = lookup_resource(w, path, get_lookup_flags(), &dentry, NULL, NULL);
1196 for (i = 0; i < dentry->num_ads; i++)
1197 needed_size += dentry->ads_entries[i].stream_name_utf8_len + 6;
1200 for (i = 0; i < dentry->num_ads; i++) {
1201 needed_size = dentry->ads_entries[i].stream_name_utf8_len + 6;
1202 if (needed_size > size)
1204 p += sprintf(p, "user.%s",
1205 dentry->ads_entries[i].stream_name_utf8) + 1;
1206 size -= needed_size;
1214 * Create a directory in the WIM.
1215 * @mode is currently ignored.
1217 static int wimfs_mkdir(const char *path, mode_t mode)
1219 struct dentry *parent;
1220 struct dentry *newdir;
1221 const char *basename;
1223 parent = get_parent_dentry(w, path);
1227 if (!dentry_is_directory(parent))
1230 basename = path_basename(path);
1231 if (get_dentry_child_with_name(parent, basename))
1234 newdir = new_dentry(basename);
1235 newdir->attributes |= FILE_ATTRIBUTE_DIRECTORY;
1236 newdir->resolved = true;
1237 newdir->link_group_id = next_link_group_id++;
1238 link_dentry(newdir, parent);
1243 /* Creates a regular file. */
1244 static int wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
1246 const char *stream_name;
1247 if ((mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
1248 && (stream_name = path_stream_name(path))) {
1249 /* Make an alternate data stream */
1250 struct ads_entry *new_entry;
1251 struct dentry *dentry;
1253 char *p = (char*)stream_name - 1;
1254 wimlib_assert(*p == ':');
1257 dentry = get_dentry(w, path);
1258 if (!dentry || !dentry_is_regular_file(dentry))
1260 if (dentry_get_ads_entry(dentry, stream_name))
1262 new_entry = dentry_add_ads(dentry, stream_name);
1266 struct dentry *dentry, *parent;
1267 const char *basename;
1269 /* Make a normal file (not an alternate data stream) */
1271 /* Make sure that the parent of @path exists and is a directory, and
1272 * that the dentry named by @path does not already exist. */
1273 parent = get_parent_dentry(w, path);
1276 if (!dentry_is_directory(parent))
1279 basename = path_basename(path);
1280 if (get_dentry_child_with_name(parent, path))
1283 dentry = new_dentry(basename);
1286 dentry->resolved = true;
1287 dentry->link_group_id = next_link_group_id++;
1288 dentry->lte_group_list.type = STREAM_TYPE_NORMAL;
1289 INIT_LIST_HEAD(&dentry->lte_group_list.list);
1290 link_dentry(dentry, parent);
1297 static int wimfs_open(const char *path, struct fuse_file_info *fi)
1299 struct dentry *dentry;
1300 struct lookup_table_entry *lte;
1302 struct wimlib_fd *fd;
1303 unsigned stream_idx;
1305 ret = lookup_resource(w, path, get_lookup_flags(), &dentry, <e,
1311 /* Empty file with no lookup-table entry. This is fine if it's
1312 * a read-only filesystem. Otherwise we need to create a lookup
1313 * table entry so that we can keep track of the file descriptors
1314 * (this is important in case someone opens the file for
1316 if (!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
1321 ret = extract_resource_to_staging_dir(dentry, stream_idx,
1327 ret = alloc_wimlib_fd(lte, &fd);
1331 fd->dentry = dentry;
1332 fd->stream_idx = stream_idx;
1333 fd->link_group_id = dentry->link_group_id;
1335 /* The file resource may be in the staging directory (read-write
1336 * mounts only) or in the WIM. If it's in the staging
1337 * directory, we need to open a native file descriptor for the
1338 * corresponding file. Otherwise, we can read the file resource
1339 * directly from the WIM file if we are opening it read-only,
1340 * but we need to extract the resource to the staging directory
1341 * if we are opening it writable. */
1342 if (flags_writable(fi->flags) &&
1343 lte->resource_location != RESOURCE_IN_STAGING_FILE) {
1344 ret = extract_resource_to_staging_dir(dentry, stream_idx, <e,
1345 lte->resource_entry.original_size);
1349 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1350 fd->staging_fd = open(lte->staging_file_name, fi->flags);
1351 if (fd->staging_fd == -1) {
1352 close_wimlib_fd(fd);
1356 fi->fh = (uintptr_t)fd;
1360 /* Opens a directory. */
1361 static int wimfs_opendir(const char *path, struct fuse_file_info *fi)
1363 struct dentry *dentry;
1365 dentry = get_dentry(w, path);
1368 if (!dentry_is_directory(dentry))
1370 dentry->num_times_opened++;
1371 fi->fh = (uintptr_t)dentry;
1377 * Read data from a file in the WIM or in the staging directory.
1379 static int wimfs_read(const char *path, char *buf, size_t size,
1380 off_t offset, 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 wimlib_assert(fd->lte);
1393 if (fd->lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1394 /* Read from staging file */
1396 wimlib_assert(fd->lte->staging_file_name);
1397 wimlib_assert(fd->staging_fd != -1);
1400 DEBUG("Seek to offset %zu", offset);
1402 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1404 ret = read(fd->staging_fd, buf, size);
1411 const struct resource_entry *res_entry;
1413 res_entry = &fd->lte->resource_entry;
1415 if (offset > res_entry->original_size)
1418 size = min(size, res_entry->original_size - offset);
1420 if (read_wim_resource(fd->lte, (u8*)buf,
1421 size, offset, false) != 0)
1427 /* Fills in the entries of the directory specified by @path using the
1428 * FUSE-provided function @filler. */
1429 static int wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
1430 off_t offset, struct fuse_file_info *fi)
1432 struct dentry *parent, *child;
1434 parent = (struct dentry*)(uintptr_t)fi->fh;
1435 wimlib_assert(parent);
1436 child = parent->children;
1438 filler(buf, ".", NULL, 0);
1439 filler(buf, "..", NULL, 0);
1445 if (filler(buf, child->file_name_utf8, NULL, 0))
1447 child = child->next;
1448 } while (child != parent->children);
1453 static int wimfs_readlink(const char *path, char *buf, size_t buf_len)
1455 struct dentry *dentry = get_dentry(w, path);
1459 if (!dentry_is_symlink(dentry))
1462 ret = dentry_readlink(dentry, buf, buf_len, w);
1468 /* Creation time, write time, access time */
1470 dentry_link_group_set_times(struct dentry *dentry, u64 times[3])
1472 struct dentry *cur = dentry;
1475 cur->creation_time = times[0];
1477 cur->last_write_time = times[1];
1479 cur->last_access_time = times[2];
1480 } while ((cur = container_of(dentry->link_group_list.next,
1482 link_group_list)) != dentry);
1486 dentry_link_group_update_times(struct dentry *dentry)
1488 u64 now = get_wim_timestamp();
1489 u64 times[3] = {now, now, now};
1490 dentry_link_group_set_times(dentry, times);
1494 static int wimfs_release(const char *path, struct fuse_file_info *fi)
1496 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1499 /* Empty file with no lookup table entry on read-only mounted
1501 wimlib_assert(!(mount_flags & WIMLIB_MOUNT_FLAG_READWRITE));
1505 return close_wimlib_fd(fd);
1508 static int wimfs_releasedir(const char *path, struct fuse_file_info *fi)
1510 struct dentry *dentry = (struct dentry *)(uintptr_t)fi->fh;
1512 wimlib_assert(dentry);
1513 wimlib_assert(dentry->num_times_opened);
1514 if (--dentry->num_times_opened == 0)
1515 free_dentry(dentry);
1520 /* Remove an alternate data stream through the XATTR interface */
1521 static int wimfs_removexattr(const char *path, const char *name)
1523 struct dentry *dentry;
1524 struct ads_entry *ads_entry;
1525 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1528 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1532 dentry = get_dentry(w, path);
1536 ads_entry = dentry_get_ads_entry(dentry, name);
1539 remove_ads(dentry, ads_entry, w->lookup_table);
1544 /* Renames a file or directory. See rename (3) */
1545 static int wimfs_rename(const char *from, const char *to)
1549 struct dentry *parent_of_dst;
1550 char *file_name_utf16 = NULL, *file_name_utf8 = NULL;
1551 u16 file_name_utf16_len, file_name_utf8_len;
1554 /* This rename() implementation currently only supports actual files
1555 * (not alternate data streams) */
1557 src = get_dentry(w, from);
1561 dst = get_dentry(w, to);
1564 ret = get_names(&file_name_utf16, &file_name_utf8,
1565 &file_name_utf16_len, &file_name_utf8_len,
1571 /* Destination file exists */
1573 if (src == dst) /* Same file */
1576 if (!dentry_is_directory(src)) {
1577 /* Cannot rename non-directory to directory. */
1578 if (dentry_is_directory(dst))
1581 /* Cannot rename directory to a non-directory or a non-empty
1583 if (!dentry_is_directory(dst))
1585 if (dst->children != NULL)
1588 parent_of_dst = dst->parent;
1589 remove_dentry(dst, w->lookup_table);
1591 /* Destination does not exist */
1592 parent_of_dst = get_parent_dentry(w, to);
1596 if (!dentry_is_directory(parent_of_dst))
1600 FREE(src->file_name);
1601 FREE(src->file_name_utf8);
1602 src->file_name = file_name_utf16;
1603 src->file_name_utf8 = file_name_utf8;
1604 src->file_name_len = file_name_utf16_len;
1605 src->file_name_utf8_len = file_name_utf8_len;
1608 link_dentry(src, parent_of_dst);
1612 /* Remove a directory */
1613 static int wimfs_rmdir(const char *path)
1615 struct dentry *dentry;
1617 dentry = get_dentry(w, path);
1621 if (!dentry_is_empty_directory(dentry))
1624 unlink_dentry(dentry);
1625 if (dentry->num_times_opened == 0)
1626 free_dentry(dentry);
1631 /* Write an alternate data stream through the XATTR interface */
1632 static int wimfs_setxattr(const char *path, const char *name,
1633 const char *value, size_t size, int flags)
1635 struct dentry *dentry;
1636 struct ads_entry *existing_ads_entry;
1637 struct ads_entry *new_ads_entry;
1638 struct lookup_table_entry *existing_lte;
1639 struct lookup_table_entry *lte;
1640 u8 value_hash[SHA1_HASH_SIZE];
1642 if (!(mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1645 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1649 dentry = get_dentry(w, path);
1652 existing_ads_entry = dentry_get_ads_entry(dentry, name);
1653 if (existing_ads_entry) {
1654 if (flags & XATTR_CREATE)
1656 remove_ads(dentry, existing_ads_entry, w->lookup_table);
1658 if (flags & XATTR_REPLACE)
1661 new_ads_entry = dentry_add_ads(dentry, name);
1665 sha1_buffer((const u8*)value, size, value_hash);
1667 existing_lte = __lookup_resource(w->lookup_table, value_hash);
1674 lte = new_lookup_table_entry();
1677 value_copy = MALLOC(size);
1682 memcpy(value_copy, value, size);
1683 lte->resource_location = RESOURCE_IN_ATTACHED_BUFFER;
1684 lte->attached_buffer = value_copy;
1685 lte->resource_entry.original_size = size;
1686 lte->resource_entry.size = size;
1687 lte->resource_entry.flags = 0;
1688 copy_hash(lte->hash, value_hash);
1689 lookup_table_insert(w->lookup_table, lte);
1691 new_ads_entry->lte = lte;
1696 static int wimfs_symlink(const char *to, const char *from)
1698 struct dentry *dentry_parent, *dentry;
1699 const char *link_name;
1700 struct lookup_table_entry *lte;
1702 dentry_parent = get_parent_dentry(w, from);
1705 if (!dentry_is_directory(dentry_parent))
1708 link_name = path_basename(from);
1710 if (get_dentry_child_with_name(dentry_parent, link_name))
1712 dentry = new_dentry(link_name);
1716 dentry->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
1717 dentry->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
1718 dentry->link_group_id = next_link_group_id++;
1720 if (dentry_set_symlink(dentry, to, w->lookup_table, <e) != 0)
1721 goto out_free_dentry;
1725 dentry->ads_entries[1].lte_group_list.type = STREAM_TYPE_ADS;
1726 list_add(&dentry->ads_entries[1].lte_group_list.list,
1727 <e->lte_group_list);
1728 wimlib_assert(dentry->resolved);
1730 link_dentry(dentry, dentry_parent);
1733 free_dentry(dentry);
1738 /* Reduce the size of a file */
1739 static int wimfs_truncate(const char *path, off_t size)
1741 struct dentry *dentry;
1742 struct lookup_table_entry *lte;
1744 unsigned stream_idx;
1746 ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1752 if (!lte) /* Already a zero-length file */
1755 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1756 wimlib_assert(lte->staging_file_name);
1757 ret = truncate(lte->staging_file_name, size);
1760 lte->resource_entry.original_size = size;
1762 wimlib_assert(lte->resource_location == RESOURCE_IN_WIM);
1763 /* File in WIM. Extract it to the staging directory, but only
1764 * the first @size bytes of it. */
1765 ret = extract_resource_to_staging_dir(dentry, stream_idx,
1768 dentry_link_group_update_times(dentry);
1772 /* Remove a regular file */
1773 static int wimfs_unlink(const char *path)
1775 struct dentry *dentry;
1776 struct lookup_table_entry *lte;
1778 unsigned stream_idx;
1780 ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
1786 if (stream_idx == 0) {
1787 /* We are removing the full dentry including all alternate data
1789 remove_dentry(dentry, w->lookup_table);
1791 /* We are removing an alternate data stream. */
1792 remove_ads(dentry, &dentry->ads_entries[stream_idx - 1],
1795 /* Beware: The lookup table entry(s) may still be referenced by users
1796 * that have opened the corresponding streams. They are freed later in
1797 * wimfs_release() when the last file user has closed the stream. */
1801 #ifdef HAVE_UTIMENSAT
1803 * Change the timestamp on a file dentry.
1805 * Note that alternate data streams do not have their own timestamps.
1807 static int wimfs_utimens(const char *path, const struct timespec tv[2])
1809 struct dentry *dentry = get_dentry(w, path);
1812 u64 times[3] = {0, 0, 0};
1813 if (tv[0].tv_nsec != UTIME_OMIT) {
1814 if (tv[0].tv_nsec == UTIME_NOW)
1815 times[2] = get_wim_timestamp();
1817 times[2] = timespec_to_wim_timestamp(&tv[0]);
1819 if (tv[1].tv_nsec != UTIME_OMIT) {
1820 if (tv[1].tv_nsec == UTIME_NOW)
1821 times[1] = get_wim_timestamp();
1823 times[1] = timespec_to_wim_timestamp(&tv[1]);
1825 dentry_link_group_set_times(dentry, times);
1829 static int wimfs_utime(const char *path, struct utimbuf *times)
1831 struct dentry *dentry = get_dentry(w, path);
1836 wim_times[1] = unix_timestamp_to_wim(times->modtime);
1837 wim_times[2] = unix_timestamp_to_wim(times->actime);
1838 dentry_link_group_set_times(dentry, wim_times);
1843 /* Writes to a file in the WIM filesystem.
1844 * It may be an alternate data stream, but here we don't even notice because we
1845 * just get a lookup table entry. */
1846 static int wimfs_write(const char *path, const char *buf, size_t size,
1847 off_t offset, struct fuse_file_info *fi)
1849 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1853 wimlib_assert(fd->lte);
1854 wimlib_assert(fd->lte->staging_file_name);
1855 wimlib_assert(fd->staging_fd != -1);
1857 /* Seek to the requested position */
1858 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1861 /* Write the data. */
1862 ret = write(fd->staging_fd, buf, size);
1867 u64 now = get_wim_timestamp();
1868 u64 times[3] = {0, now, now};
1869 dentry_link_group_set_times(fd->dentry, times);
1875 static struct fuse_operations wimfs_operations = {
1876 .access = wimfs_access,
1877 .destroy = wimfs_destroy,
1879 .fallocate = wimfs_fallocate,
1880 .fgetattr = wimfs_fgetattr,
1882 .ftruncate = wimfs_ftruncate,
1883 .getattr = wimfs_getattr,
1885 .getxattr = wimfs_getxattr,
1889 .listxattr = wimfs_listxattr,
1891 .mkdir = wimfs_mkdir,
1892 .mknod = wimfs_mknod,
1894 .opendir = wimfs_opendir,
1896 .readdir = wimfs_readdir,
1897 .readlink = wimfs_readlink,
1898 .release = wimfs_release,
1899 .releasedir = wimfs_releasedir,
1901 .removexattr = wimfs_removexattr,
1903 .rename = wimfs_rename,
1904 .rmdir = wimfs_rmdir,
1906 .setxattr = wimfs_setxattr,
1908 .symlink = wimfs_symlink,
1909 .truncate = wimfs_truncate,
1910 .unlink = wimfs_unlink,
1911 #ifdef HAVE_UTIMENSAT
1912 .utimens = wimfs_utimens,
1914 .utime = wimfs_utime,
1916 .write = wimfs_write,
1920 static int check_lte_refcnt(struct lookup_table_entry *lte, void *ignore)
1922 size_t lte_group_size = 0;
1923 struct list_head *cur;
1924 list_for_each(cur, <e->lte_group_list)
1926 if (lte_group_size > lte->refcnt) {
1927 #ifdef ENABLE_ERROR_MESSAGES
1928 struct dentry *example_dentry;
1929 struct list_head *next;
1930 struct stream_list_head *head;
1931 WARNING("The following lookup table entry has a reference count "
1932 "of %u, but", lte->refcnt);
1933 WARNING("We found %zu references to it", lte_group_size);
1934 next = lte->lte_group_list.next;
1935 head = container_of(next, struct stream_list_head, list);
1936 if (head->type == STREAM_TYPE_NORMAL) {
1937 example_dentry = container_of(head, struct dentry,
1939 WARNING("(One dentry referencing it is at `%s')",
1940 example_dentry->full_path_utf8);
1942 print_lookup_table_entry(lte);
1944 /* Guess what! install.wim for Windows 8 contains a stream with
1945 * 2 dentries referencing it, but the lookup table entry has
1946 * reference count of 1. So we will need to handle this case
1947 * and not just make it be an error... I'm just setting the
1948 * reference count to the number of references we found. */
1951 lte->refcnt = lte_group_size;
1952 WARNING("Fixing reference count");
1954 return WIMLIB_ERR_INVALID_DENTRY;
1960 /* Mounts a WIM file. */
1961 WIMLIBAPI int wimlib_mount(WIMStruct *wim, int image, const char *dir,
1962 int flags, WIMStruct **additional_swms,
1963 unsigned num_additional_swms)
1969 struct lookup_table *joined_tab, *wim_tab_save;
1971 DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
1972 wim, image, dir, flags);
1975 return WIMLIB_ERR_INVALID_PARAM;
1977 ret = verify_swm_set(wim, additional_swms, num_additional_swms);
1981 if (num_additional_swms) {
1982 ret = new_joined_lookup_table(wim, additional_swms,
1983 num_additional_swms,
1987 wim_tab_save = wim->lookup_table;
1988 wim->lookup_table = joined_tab;
1991 ret = wimlib_select_image(wim, image);
1996 DEBUG("Selected image %d", image);
1998 next_link_group_id = assign_link_group_ids(wim->image_metadata[image - 1].lgt);
2000 DEBUG("Resolving lookup table entries");
2001 /* Resolve all the lookup table entries of the dentry tree */
2002 for_dentry_in_tree(wim_root_dentry(wim), dentry_resolve_ltes,
2005 DEBUG("Checking lookup table entry reference counts");
2007 ret = for_lookup_table_entry(wim->lookup_table, check_lte_refcnt, NULL);
2011 if (flags & WIMLIB_MOUNT_FLAG_READWRITE)
2012 wim_get_current_image_metadata(wim)->modified = true;
2014 if (!(flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
2015 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
2016 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
2017 flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
2019 DEBUG("Getting current directory");
2022 working_directory = getcwd(NULL, 0);
2023 if (!working_directory) {
2024 ERROR_WITH_ERRNO("Could not determine current directory");
2025 ret = WIMLIB_ERR_NOTDIR;
2029 DEBUG("Closing POSIX message queues");
2030 /* XXX hack to get rid of the message queues if they already exist for
2031 * some reason (maybe left over from a previous mount that wasn't
2032 * unmounted correctly) */
2033 ret = open_message_queues(true);
2036 close_message_queues();
2038 DEBUG("Preparing arguments to fuse_main()");
2043 ret = WIMLIB_ERR_NOMEM;
2047 argv[argc++] = "imagex";
2049 argv[argc++] = "-s"; /* disable multi-threaded operation */
2051 if (flags & WIMLIB_MOUNT_FLAG_DEBUG)
2052 argv[argc++] = "-d";
2055 * We provide the use_ino option because we are going to assign inode
2056 * numbers oursides. We've already numbered the hard link groups with
2057 * unique numbers with the assign_link_groups() function, and the static
2058 * variable next_link_group_id is set to the next available link group
2059 * ID that we will assign to new dentries.
2061 char optstring[256] = "use_ino";
2062 argv[argc++] = "-o";
2063 argv[argc++] = optstring;
2064 if ((flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
2065 /* Read-write mount. Make the staging directory */
2067 if (!staging_dir_name) {
2069 ret = WIMLIB_ERR_MKDIR;
2073 /* Read-only mount */
2074 strcat(optstring, ",ro");
2076 strcat(optstring, ",subtype=wimfs,attr_timeout=0");
2082 DEBUG("FUSE command line (argc = %d): ", argc);
2083 for (i = 0; i < argc; i++) {
2084 fputs(argv[i], stdout);
2092 /* Set static variables. */
2094 mount_flags = flags;
2096 ret = fuse_main(argc, argv, &wimfs_operations, NULL);
2098 ret = WIMLIB_ERR_FUSE;
2100 if (num_additional_swms) {
2101 free_lookup_table(wim->lookup_table);
2102 wim->lookup_table = wim_tab_save;
2109 * Unmounts the WIM file that was previously mounted on @dir by using
2112 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
2119 struct timespec timeout;
2125 /* Open message queues between the unmount process and the
2126 * filesystem daemon. */
2127 ret = open_message_queues(false);
2131 /* Send a message to the filesystem saying whether to commit or
2133 msg[0] = (flags & WIMLIB_UNMOUNT_FLAG_COMMIT) ? 1 : 0;
2134 msg[1] = (flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY) ? 1 : 0;
2136 DEBUG("Sending message: %s, %s",
2137 (msg[0] == 0) ? "don't commit" : "commit",
2138 (msg[1] == 0) ? "don't check" : "check");
2139 ret = mq_send(unmount_to_daemon_mq, msg, 2, 1);
2141 ERROR("Failed to notify filesystem daemon whether we want to "
2142 "commit changes or not");
2143 close_message_queues();
2144 return WIMLIB_ERR_MQUEUE;
2147 /* Execute `fusermount -u', which is installed setuid root, to unmount
2150 * FUSE does not yet implement synchronous unmounts. This means that
2151 * fusermount -u will return before the filesystem daemon returns from
2152 * wimfs_destroy(). This is partly what we want, because we need to
2153 * send a message from this process to the filesystem daemon telling
2154 * whether --commit was specified or not. However, after that, the
2155 * unmount process must wait for the filesystem daemon to finish writing
2162 ERROR_WITH_ERRNO("Failed to fork()");
2163 return WIMLIB_ERR_FORK;
2166 execlp("fusermount", "fusermount", "-u", dir, NULL);
2167 ERROR_WITH_ERRNO("Failed to execute `fusermount'");
2168 exit(WIMLIB_ERR_FUSERMOUNT);
2171 ret = wait(&status);
2173 ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
2175 return WIMLIB_ERR_FUSERMOUNT;
2179 ERROR("fusermount exited with status %d", status);
2181 /* Try again, but with the `umount' program. This is required
2182 * on other FUSE implementations such as FreeBSD's that do not
2183 * have a `fusermount' program. */
2187 ERROR_WITH_ERRNO("Failed to fork()");
2188 return WIMLIB_ERR_FORK;
2191 execlp("umount", "umount", dir, NULL);
2192 ERROR_WITH_ERRNO("Failed to execute `umount'");
2193 exit(WIMLIB_ERR_FUSERMOUNT);
2196 ret = wait(&status);
2198 ERROR_WITH_ERRNO("Failed to wait for `umount' process to "
2200 return WIMLIB_ERR_FUSERMOUNT;
2203 ERROR("`umount' exited with failure status");
2204 return WIMLIB_ERR_FUSERMOUNT;
2209 /* Wait for a message from the filesytem daemon indicating whether the
2210 * filesystem was unmounted successfully (0) or an error occurred (1).
2211 * This may take a long time if a big WIM file needs to be rewritten. */
2213 /* Wait at most 600??? seconds before giving up and returning false.
2214 * Either it's a really big WIM file, or (more likely) the
2215 * filesystem daemon has crashed or failed for some reason.
2217 * XXX come up with some method to determine if the filesystem
2218 * daemon has really crashed or not.
2220 * XXX Idea: have mount daemon write its PID into the WIM file header?
2223 gettimeofday(&now, NULL);
2224 timeout.tv_sec = now.tv_sec + 600;
2225 timeout.tv_nsec = now.tv_usec * 1000;
2227 msgsize = mq_get_msgsize(daemon_to_unmount_mq);
2228 char mailbox[msgsize];
2231 DEBUG("Waiting for message telling us whether the unmount was "
2232 "successful or not.");
2233 ret = mq_timedreceive(daemon_to_unmount_mq, mailbox, msgsize,
2236 close_message_queues();
2238 if (errno_save == ETIMEDOUT) {
2239 ERROR("Timed out- probably the filesystem daemon "
2240 "crashed and the WIM was not written "
2242 return WIMLIB_ERR_TIMEOUT;
2244 ERROR("mq_receive(): %s", strerror(errno_save));
2245 return WIMLIB_ERR_MQUEUE;
2249 DEBUG("Received message: %s",
2250 (mailbox[0] == 0) ? "Unmount OK" : "Unmount Failed");
2251 if (mailbox[0] != 0)
2252 ERROR("Unmount failed");
2256 #else /* WITH_FUSE */
2259 static inline int mount_unsupported_error()
2261 ERROR("WIMLIB was compiled with --without-fuse, which disables support "
2262 "for mounting WIMs.");
2263 return WIMLIB_ERR_UNSUPPORTED;
2266 WIMLIBAPI int wimlib_unmount(const char *dir, int flags)
2268 return mount_unsupported_error();
2271 WIMLIBAPI int wimlib_mount(WIMStruct *wim_p, int image, const char *dir,
2272 int flags, WIMStruct **additional_swms,
2273 unsigned num_additional_swms)
2275 return mount_unsupported_error();
2278 #endif /* WITH_FUSE */