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"
36 #include "buffer_io.h"
37 #include "timestamp.h"
42 #define FUSE_USE_VERSION 26
52 #include <attr/xattr.h>
55 /* File descriptor to a file open on the WIM filesystem. */
57 struct inode *f_inode;
58 struct lookup_table_entry *f_lte;
64 struct wimfs_context {
65 /* The WIMStruct for the mounted WIM. */
68 /* Name of the staging directory for a read-write mount. Whenever a new file is
69 * created, it is done so in the staging directory. Furthermore, whenever a
70 * file in the WIM is modified, it is extracted to the staging directory. If
71 * changes are commited when the WIM is unmounted, the file resources are merged
72 * in from the staging directory when writing the new WIM. */
73 char *staging_dir_name;
74 size_t staging_dir_name_len;
76 /* Flags passed to wimlib_mount(). */
79 int default_lookup_flags;
81 /* Next inode number to be assigned. */
84 /* List of lookup table entries in the staging directory */
85 struct list_head staging_list;
87 /* Name and message queue descriptors for message queues between the filesystem
88 * daemon process and the unmount process. These are used when the filesystem
89 * is unmounted and the process running wimlib_mount() (i.e. the `imagex
90 * unmount' command) needs to communicate with the filesystem daemon running
91 * fuse_main() (i.e. that spawned by the `imagex mount' or `imagex mountrw'
93 char *unmount_to_daemon_mq_name;
94 char *daemon_to_unmount_mq_name;
95 mqd_t unmount_to_daemon_mq;
96 mqd_t daemon_to_unmount_mq;
99 static void init_wimfs_context(struct wimfs_context *ctx)
101 memset(ctx, 0, sizeof(*ctx));
102 ctx->unmount_to_daemon_mq = (mqd_t)-1;
103 ctx->daemon_to_unmount_mq = (mqd_t)-1;
104 INIT_LIST_HEAD(&ctx->staging_list);
107 static inline struct wimfs_context *wimfs_get_context()
109 return (struct wimfs_context*)fuse_get_context()->private_data;
112 static inline WIMStruct *wimfs_get_WIMStruct()
114 return wimfs_get_context()->wim;
117 static inline bool wimfs_ctx_readonly(const struct wimfs_context *ctx)
119 return (ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) == 0;
122 static inline int get_lookup_flags(const struct wimfs_context *ctx)
124 return ctx->default_lookup_flags;
127 /* Returns nonzero if write permission is requested on the file open flags */
128 static inline int flags_writable(int open_flags)
130 return open_flags & (O_RDWR | O_WRONLY);
134 * Allocate a file descriptor for a stream.
136 * @inode: inode containing the stream we're opening
137 * @stream_id: ID of the stream we're opening
138 * @lte: Lookup table entry for the stream (may be NULL)
139 * @fd_ret: Return the allocated file descriptor if successful.
140 * @readonly: True if this is a read-only mount.
142 * Return 0 iff successful or error code if unsuccessful.
144 static int alloc_wimlib_fd(struct inode *inode,
146 struct lookup_table_entry *lte,
147 struct wimlib_fd **fd_ret,
150 static const u16 fds_per_alloc = 8;
151 static const u16 max_fds = 0xffff;
154 pthread_mutex_lock(&inode->i_mutex);
156 DEBUG("Allocating fd for stream ID %u from inode %lx (open = %u, allocated = %u)",
157 stream_id, inode->ino, inode->num_opened_fds,
158 inode->num_allocated_fds);
160 if (inode->num_opened_fds == inode->num_allocated_fds) {
161 struct wimlib_fd **fds;
164 if (inode->num_allocated_fds == max_fds) {
168 num_new_fds = min(fds_per_alloc,
169 max_fds - inode->num_allocated_fds);
171 fds = REALLOC(inode->fds,
172 (inode->num_allocated_fds + num_new_fds) *
173 sizeof(inode->fds[0]));
178 memset(&fds[inode->num_allocated_fds], 0,
179 num_new_fds * sizeof(fds[0]));
181 inode->num_allocated_fds += num_new_fds;
183 for (u16 i = 0; ; i++) {
184 if (!inode->fds[i]) {
185 struct wimlib_fd *fd = CALLOC(1, sizeof(*fd));
194 fd->stream_id = stream_id;
197 inode->num_opened_fds++;
198 if (lte && !readonly)
199 lte->num_opened_fds++;
200 DEBUG("Allocated fd (idx = %u)", fd->idx);
206 pthread_mutex_unlock(&inode->i_mutex);
210 static void inode_put_fd(struct inode *inode, struct wimlib_fd *fd)
212 wimlib_assert(inode != NULL);
214 pthread_mutex_lock(&inode->i_mutex);
216 wimlib_assert(fd->f_inode == inode);
217 wimlib_assert(inode->num_opened_fds != 0);
218 wimlib_assert(fd->idx < inode->num_allocated_fds);
219 wimlib_assert(inode->fds[fd->idx] == fd);
221 inode->fds[fd->idx] = NULL;
223 if (--inode->num_opened_fds == 0 && inode->link_count == 0) {
224 pthread_mutex_unlock(&inode->i_mutex);
227 pthread_mutex_unlock(&inode->i_mutex);
231 static int lte_put_fd(struct lookup_table_entry *lte, struct wimlib_fd *fd)
233 wimlib_assert(fd->f_lte == lte);
235 if (!lte) /* Empty stream with no lookup table entry */
238 /* Close staging file descriptor if needed. */
240 if (lte->resource_location == RESOURCE_IN_STAGING_FILE
241 && fd->staging_fd != -1)
243 if (close(fd->staging_fd) != 0) {
244 ERROR_WITH_ERRNO("Failed to close staging file");
248 lte_decrement_num_opened_fds(lte);
252 /* Close a file descriptor. */
253 static int close_wimlib_fd(struct wimlib_fd *fd)
256 DEBUG("Closing fd (inode = %lu, opened = %u, allocated = %u)",
257 fd->f_inode->ino, fd->f_inode->num_opened_fds,
258 fd->f_inode->num_allocated_fds);
259 ret = lte_put_fd(fd->f_lte, fd);
263 inode_put_fd(fd->f_inode, fd);
267 static int create_dentry(struct wimfs_context *ctx, const char *path,
268 struct dentry **dentry_ret)
270 struct dentry *parent;
272 const char *basename;
274 parent = get_parent_dentry(ctx->wim, path);
278 if (!dentry_is_directory(parent))
281 basename = path_basename(path);
282 if (get_dentry_child_with_name(parent, basename))
285 new = new_dentry_with_inode(basename);
289 new->d_inode->resolved = 1;
290 new->d_inode->ino = ctx->next_ino++;
291 dentry_add_child(parent, new);
296 /* Remove a dentry; i.e. remove a reference to the corresponding inode.
298 * If there are no remaining references to the inode either through dentries or
299 * open file descriptors, the inode is freed. Otherwise, the inode is not
300 * removed, but the dentry is unlinked and freed.
302 * Either way, all lookup table entries referenced by the inode have their
303 * reference count decremented. If a lookup table entry has no open file
304 * descriptors and no references remaining, it is freed, and the staging file is
307 static void remove_dentry(struct dentry *dentry,
308 struct lookup_table *lookup_table)
310 struct inode *inode = dentry->d_inode;
311 struct lookup_table_entry *lte;
314 for (i = 0; i <= inode->num_ads; i++) {
315 lte = inode_stream_lte_resolved(inode, i);
317 lte_decrement_refcnt(lte, lookup_table);
319 unlink_dentry(dentry);
323 /* Transfers file attributes from a struct inode to a `stat' buffer.
325 * The lookup table entry tells us which stream in the inode we are statting.
326 * For a named data stream, everything returned is the same as the unnamed data
327 * stream except possibly the size and block count. */
328 static int inode_to_stbuf(const struct inode *inode,
329 struct lookup_table_entry *lte, struct stat *stbuf)
331 if (inode_is_symlink(inode))
332 stbuf->st_mode = S_IFLNK | 0777;
333 else if (inode_is_directory(inode))
334 stbuf->st_mode = S_IFDIR | 0755;
336 stbuf->st_mode = S_IFREG | 0755;
338 stbuf->st_ino = (ino_t)inode->ino;
339 stbuf->st_nlink = inode->link_count;
340 stbuf->st_uid = getuid();
341 stbuf->st_gid = getgid();
344 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
345 struct stat native_stat;
346 if (stat(lte->staging_file_name, &native_stat) != 0) {
347 DEBUG("Failed to stat `%s': %m",
348 lte->staging_file_name);
351 stbuf->st_size = native_stat.st_size;
353 stbuf->st_size = wim_resource_size(lte);
359 stbuf->st_atime = wim_timestamp_to_unix(inode->last_access_time);
360 stbuf->st_mtime = wim_timestamp_to_unix(inode->last_write_time);
361 stbuf->st_ctime = wim_timestamp_to_unix(inode->creation_time);
362 stbuf->st_blocks = (stbuf->st_size + 511) / 512;
366 /* Creates a new staging file and returns its file descriptor opened for
369 * @name_ret: A location into which the a pointer to the newly allocated name of
370 * the staging file is stored.
371 * @return: The file descriptor for the new file. Returns -1 and sets errno on
372 * error, for any reason possible from the creat() function.
374 static int create_staging_file(char **name_ret, int open_flags,
375 struct wimfs_context *ctx)
383 name_len = ctx->staging_dir_name_len + 1 + SHA1_HASH_SIZE;
384 name = MALLOC(name_len + 1);
392 memcpy(name, ctx->staging_dir_name, ctx->staging_dir_name_len);
393 name[ctx->staging_dir_name_len] = '/';
394 randomize_char_array_with_alnum(name + ctx->staging_dir_name_len + 1,
396 name[name_len] = '\0';
399 /* Just in case, verify that the randomly generated name doesn't name an
400 * existing file, and try again if so */
401 } while (stat(name, &stbuf) == 0);
407 /* doesn't exist--- ok */
409 DEBUG("Creating staging file `%s'", name);
411 fd = open(name, open_flags | O_CREAT | O_TRUNC, 0600);
423 * Extract a WIM resource to the staging directory.
425 * @inode: Inode that contains the stream we are extracting
427 * @stream_id: Identifier for the stream (it stays constant even if the indices
428 * of the stream entries are changed)
430 * @lte: Pointer to pointer to the lookup table entry for the stream we need to
431 * extract, or NULL if there was no lookup table entry present for the stream
433 * @size: Number of bytes of the stream we want to extract (this supports the
434 * wimfs_truncate() function). It may be more than the actual stream length, in
435 * which case the extra space is filled with zeroes.
437 * @ctx: Context for the WIM filesystem.
439 * Returns 0 on success or a negative error code on failure.
441 static int extract_resource_to_staging_dir(struct inode *inode,
443 struct lookup_table_entry **lte,
445 struct wimfs_context *ctx)
447 char *staging_file_name;
450 struct lookup_table_entry *old_lte, *new_lte;
454 DEBUG("Extracting resource to staging dir: inode %"PRIu64", "
455 "stream id %"PRIu32, inode->ino, stream_id);
459 wimlib_assert(old_lte == NULL ||
460 old_lte->resource_location != RESOURCE_IN_STAGING_FILE);
462 /* Create the staging file */
463 fd = create_staging_file(&staging_file_name, O_WRONLY, ctx);
467 /* Extract the stream to the staging file (possibly truncated) */
469 extract_size = min(wim_resource_size(old_lte), size);
470 ret = extract_wim_resource_to_fd(old_lte, fd, extract_size);
476 /* In the case of truncate() to more than the file length, extend the
477 * file with zeroes by calling ftruncate() on the underlying staging
479 if (ret == 0 && size > extract_size)
480 ret = ftruncate(fd, size);
482 /* Close the staging file descriptor and check for errors. If there's
483 * an error, unlink the staging file. */
484 if (ret != 0 || close(fd) != 0) {
490 goto out_delete_staging_file;
493 /* Now deal with the lookup table entries. We may be able to re-use the
494 * existing entry, but we may have to create a new one instead. */
496 if (old_lte && inode->link_count == old_lte->refcnt) {
497 /* The reference count of the existing lookup table entry is the
498 * same as the link count of the inode that contains the stream
499 * we're opening. Therefore, ALL the references to the lookup
500 * table entry correspond to the stream we're trying to extract,
501 * so the lookup table entry can be re-used. */
502 DEBUG("Re-using lookup table entry");
503 lookup_table_unlink(ctx->wim->lookup_table, old_lte);
507 /* There's an existing lookup table entry, but its
508 * reference count is greater than the link count for
509 * the inode containing a stream we're opening.
510 * Therefore, we need to split the lookup table entry.
512 wimlib_assert(old_lte->refcnt > inode->link_count);
513 DEBUG("Splitting lookup table entry "
514 "(inode->link_count = %u, old_lte->refcnt = %u)",
515 inode->link_count, old_lte->refcnt);
518 new_lte = new_lookup_table_entry();
521 goto out_delete_staging_file;
524 /* There may already be open file descriptors to this stream if
525 * it's previously been opened read-only, but just now we're
526 * opening it read-write. Identify those file descriptors and
527 * change their lookup table entry pointers to point to the new
528 * lookup table entry, and open staging file descriptors for
531 * At the same time, we need to count the number of these opened
532 * file descriptors to the new lookup table entry. If there's
533 * an old lookup table entry, this number needs to be subtracted
534 * from the fd's opened to the old entry. */
535 for (u16 i = 0, j = 0; j < inode->num_opened_fds; i++) {
536 struct wimlib_fd *fd = inode->fds[i];
538 if (fd->stream_id == stream_id) {
539 wimlib_assert(fd->f_lte == old_lte);
540 wimlib_assert(fd->staging_fd == -1);
542 new_lte->num_opened_fds++;
543 fd->staging_fd = open(staging_file_name, O_RDONLY);
544 if (fd->staging_fd == -1) {
546 goto out_revert_fd_changes;
552 DEBUG("%hu fd's were already opened to the file we extracted",
553 new_lte->num_opened_fds);
555 old_lte->num_opened_fds -= new_lte->num_opened_fds;
556 old_lte->refcnt -= inode->link_count;
560 new_lte->refcnt = inode->link_count;
561 new_lte->resource_location = RESOURCE_IN_STAGING_FILE;
562 new_lte->staging_file_name = staging_file_name;
563 new_lte->lte_inode = inode;
564 random_hash(new_lte->hash);
567 inode->lte = new_lte;
569 for (u16 i = 0; i < inode->num_ads; i++)
570 if (inode->ads_entries[i].stream_id == stream_id)
571 inode->ads_entries[i].lte = new_lte;
573 lookup_table_insert(ctx->wim->lookup_table, new_lte);
574 list_add(&new_lte->staging_list, &ctx->staging_list);
577 out_revert_fd_changes:
578 for (u16 i = 0, j = 0; j < new_lte->num_opened_fds; i++) {
579 struct wimlib_fd *fd = inode->fds[i];
580 if (fd && fd->stream_id == stream_id && fd->f_lte == new_lte) {
582 if (fd->staging_fd != -1) {
583 close(fd->staging_fd);
589 free_lookup_table_entry(new_lte);
590 out_delete_staging_file:
591 unlink(staging_file_name);
592 FREE(staging_file_name);
597 * Creates a randomly named staging directory and saves its name in the
598 * filesystem context structure.
600 static int make_staging_dir(struct wimfs_context *ctx, const char *user_prefix)
602 static const size_t random_suffix_len = 10;
603 static const char *common_suffix = ".staging";
604 static const size_t common_suffix_len = 8;
606 char *staging_dir_name = NULL;
607 size_t staging_dir_name_len;
609 const char *wim_basename;
610 char *real_user_prefix = NULL;
614 real_user_prefix = realpath(user_prefix, NULL);
615 if (!real_user_prefix) {
616 ERROR_WITH_ERRNO("Could not resolve `%s'",
618 ret = WIMLIB_ERR_NOTDIR;
621 wim_basename = path_basename(ctx->wim->filename);
622 prefix_len = strlen(real_user_prefix) + 1 + strlen(wim_basename);
624 prefix_len = strlen(ctx->wim->filename);
627 staging_dir_name_len = prefix_len + common_suffix_len + random_suffix_len;
629 staging_dir_name = MALLOC(staging_dir_name_len + 1);
630 if (!staging_dir_name) {
631 ret = WIMLIB_ERR_NOMEM;
635 if (real_user_prefix)
636 sprintf(staging_dir_name, "%s/%s", real_user_prefix, wim_basename);
638 strcpy(staging_dir_name, ctx->wim->filename);
640 strcat(staging_dir_name, common_suffix);
642 randomize_char_array_with_alnum(staging_dir_name + prefix_len + common_suffix_len,
645 staging_dir_name[staging_dir_name_len] = '\0';
647 if (mkdir(staging_dir_name, 0700) != 0) {
648 ERROR_WITH_ERRNO("Failed to create temporary directory `%s'",
650 ret = WIMLIB_ERR_MKDIR;
655 FREE(real_user_prefix);
657 ctx->staging_dir_name = staging_dir_name;
658 ctx->staging_dir_name_len = staging_dir_name_len;
660 FREE(staging_dir_name);
665 static int remove_file_or_directory(const char *fpath, const struct stat *sb,
666 int typeflag, struct FTW *ftwbuf)
668 if (remove(fpath) == 0)
671 ERROR_WITH_ERRNO("Cannot remove `%s'", fpath);
672 return WIMLIB_ERR_DELETE_STAGING_DIR;
678 * Deletes the staging directory and all the files contained in it.
680 static int delete_staging_dir(struct wimfs_context *ctx)
683 ret = nftw(ctx->staging_dir_name, remove_file_or_directory,
685 FREE(ctx->staging_dir_name);
686 ctx->staging_dir_name = NULL;
691 /* Simple function that returns the concatenation of 2 strings. */
692 static char *strcat_dup(const char *s1, const char *s2, size_t max_len)
694 size_t len = strlen(s1) + strlen(s2);
697 char *p = MALLOC(len + 1);
700 snprintf(p, len + 1, "%s%s", s1, s2);
704 static int set_message_queue_names(struct wimfs_context *ctx,
705 const char *mount_dir)
707 static const char *u2d_prefix = "/wimlib-unmount-to-daemon-mq";
708 static const char *d2u_prefix = "/wimlib-daemon-to-unmount-mq";
713 dir_path = realpath(mount_dir, NULL);
715 ERROR_WITH_ERRNO("Failed to resolve path \"%s\"", mount_dir);
717 return WIMLIB_ERR_NOMEM;
719 return WIMLIB_ERR_NOTDIR;
729 ctx->unmount_to_daemon_mq_name = strcat_dup(u2d_prefix, dir_path,
731 if (!ctx->unmount_to_daemon_mq_name) {
732 ret = WIMLIB_ERR_NOMEM;
733 goto out_free_dir_path;
735 ctx->daemon_to_unmount_mq_name = strcat_dup(d2u_prefix, dir_path,
737 if (!ctx->daemon_to_unmount_mq_name) {
738 ret = WIMLIB_ERR_NOMEM;
739 goto out_free_unmount_to_daemon_mq_name;
743 goto out_free_dir_path;
744 out_free_unmount_to_daemon_mq_name:
745 FREE(ctx->unmount_to_daemon_mq_name);
746 ctx->unmount_to_daemon_mq_name = NULL;
752 static void free_message_queue_names(struct wimfs_context *ctx)
754 FREE(ctx->unmount_to_daemon_mq_name);
755 FREE(ctx->daemon_to_unmount_mq_name);
756 ctx->unmount_to_daemon_mq_name = NULL;
757 ctx->daemon_to_unmount_mq_name = NULL;
761 * Opens two POSIX message queue: one for sending messages from the unmount
762 * process to the daemon process, and one to go the other way. The names of the
763 * message queues, which must be system-wide unique, are be based on the mount
764 * point. (There of course is still a possibility of a collision if one were to
765 * unmount two identically named directories simultaneously...)
767 * @daemon specifies whether the calling process is the filesystem daemon or the
770 static int open_message_queues(struct wimfs_context *ctx, bool daemon)
772 int unmount_to_daemon_mq_flags = O_WRONLY | O_CREAT;
773 int daemon_to_unmount_mq_flags = O_RDONLY | O_CREAT;
776 swap(unmount_to_daemon_mq_flags, daemon_to_unmount_mq_flags);
778 DEBUG("Opening message queue \"%s\"", ctx->unmount_to_daemon_mq_name);
779 ctx->unmount_to_daemon_mq = mq_open(ctx->unmount_to_daemon_mq_name,
780 unmount_to_daemon_mq_flags, 0700, NULL);
782 if (ctx->unmount_to_daemon_mq == (mqd_t)-1) {
783 ERROR_WITH_ERRNO("mq_open()");
784 return WIMLIB_ERR_MQUEUE;
787 DEBUG("Opening message queue \"%s\"", ctx->daemon_to_unmount_mq_name);
788 ctx->daemon_to_unmount_mq = mq_open(ctx->daemon_to_unmount_mq_name,
789 daemon_to_unmount_mq_flags, 0700, NULL);
791 if (ctx->daemon_to_unmount_mq == (mqd_t)-1) {
792 ERROR_WITH_ERRNO("mq_open()");
793 mq_close(ctx->unmount_to_daemon_mq);
794 mq_unlink(ctx->unmount_to_daemon_mq_name);
795 ctx->unmount_to_daemon_mq = (mqd_t)-1;
796 return WIMLIB_ERR_MQUEUE;
801 /* Try to determine the maximum message size of a message queue. The return
802 * value is the maximum message size, or a guess of 8192 bytes if it cannot be
804 static long mq_get_msgsize(mqd_t mq)
806 static const char *msgsize_max_file = "/proc/sys/fs/mqueue/msgsize_max";
811 if (mq_getattr(mq, &attr) == 0) {
812 msgsize = attr.mq_msgsize;
814 ERROR_WITH_ERRNO("mq_getattr()");
815 ERROR("Attempting to read %s", msgsize_max_file);
816 fp = fopen(msgsize_max_file, "rb");
818 if (fscanf(fp, "%ld", &msgsize) != 1) {
819 ERROR("Assuming message size of 8192");
824 ERROR_WITH_ERRNO("Failed to open the file `%s'",
826 ERROR("Assuming message size of 8192");
833 static int get_mailbox(mqd_t mq, long needed_msgsize, long *msgsize_ret,
839 msgsize = mq_get_msgsize(mq);
841 if (msgsize < needed_msgsize) {
842 ERROR("Message queue max size must be at least %ld!",
844 return WIMLIB_ERR_MQUEUE;
847 mailbox = MALLOC(msgsize);
849 ERROR("Failed to allocate %ld bytes for mailbox", msgsize);
850 return WIMLIB_ERR_NOMEM;
852 *msgsize_ret = msgsize;
853 *mailbox_ret = mailbox;
857 static void unlink_message_queues(struct wimfs_context *ctx)
859 mq_unlink(ctx->unmount_to_daemon_mq_name);
860 mq_unlink(ctx->daemon_to_unmount_mq_name);
863 /* Closes the message queues, which are allocated in static variables */
864 static void close_message_queues(struct wimfs_context *ctx)
866 mq_close(ctx->unmount_to_daemon_mq);
867 ctx->unmount_to_daemon_mq = (mqd_t)(-1);
868 mq_close(ctx->daemon_to_unmount_mq);
869 ctx->daemon_to_unmount_mq = (mqd_t)(-1);
870 unlink_message_queues(ctx);
873 struct unmount_message_header {
879 struct unmount_request {
880 struct unmount_message_header hdr;
886 struct unmount_response {
887 struct unmount_message_header hdr;
894 static void inode_update_lte_ptr(struct inode *inode,
895 struct lookup_table_entry *old_lte,
896 struct lookup_table_entry *new_lte)
898 if (inode->lte == old_lte) {
899 inode->lte = new_lte;
901 for (unsigned i = 0; i < inode->num_ads; i++) {
902 if (inode->ads_entries[i].lte == old_lte) {
903 inode->ads_entries[i].lte = new_lte;
910 static int update_lte_of_staging_file(struct lookup_table_entry *lte,
911 struct lookup_table *table)
913 struct lookup_table_entry *duplicate_lte;
915 u8 hash[SHA1_HASH_SIZE];
918 ret = sha1sum(lte->staging_file_name, hash);
921 lookup_table_unlink(table, lte);
922 duplicate_lte = __lookup_resource(table, hash);
924 /* Merge duplicate lookup table entries */
925 duplicate_lte->refcnt += lte->refcnt;
926 inode_update_lte_ptr(lte->lte_inode, lte, duplicate_lte);
927 free_lookup_table_entry(lte);
929 if (stat(lte->staging_file_name, &stbuf) != 0) {
930 ERROR_WITH_ERRNO("Failed to stat `%s'", lte->staging_file_name);
931 return WIMLIB_ERR_STAT;
933 if (stbuf.st_size == 0) {
934 /* Zero-length stream. No lookup table entry needed. */
935 inode_update_lte_ptr(lte->lte_inode, lte, NULL);
936 free_lookup_table_entry(lte);
938 wimlib_assert(<e->file_on_disk == <e->staging_file_name);
939 lte->resource_entry.original_size = stbuf.st_size;
940 lte->resource_entry.size = stbuf.st_size;
941 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
942 lte->file_on_disk_fp = NULL;
943 copy_hash(lte->hash, hash);
944 lookup_table_insert(table, lte);
950 static int inode_close_fds(struct inode *inode)
952 u16 num_opened_fds = inode->num_opened_fds;
953 for (u16 i = 0, j = 0; j < num_opened_fds; i++) {
954 struct wimlib_fd *fd = inode->fds[i];
956 wimlib_assert(fd->f_inode == inode);
957 int ret = close_wimlib_fd(fd);
966 /* Overwrites the WIM file, with changes saved. */
967 static int rebuild_wim(struct wimfs_context *ctx, int write_flags)
970 struct lookup_table_entry *lte, *tmp;
971 WIMStruct *w = ctx->wim;
973 DEBUG("Closing all staging file descriptors.");
974 list_for_each_entry_safe(lte, tmp, &ctx->staging_list, staging_list) {
975 ret = inode_close_fds(lte->lte_inode);
980 DEBUG("Calculating SHA1 checksums for all new staging files.");
981 list_for_each_entry(lte, &ctx->staging_list, staging_list) {
982 ret = update_lte_of_staging_file(lte, w->lookup_table);
987 xml_update_image_info(w, w->current_image);
988 ret = wimlib_overwrite(w, write_flags, 0, NULL);
990 ERROR("Failed to commit changes to mounted WIM image");
994 /* Send a message to the filesystem daemon saying whether to commit or not. */
995 static int send_unmount_request(struct wimfs_context *ctx, int unmount_flags)
998 struct unmount_request msg;
1000 msg.hdr.min_version = WIMLIB_MAKEVERSION(1, 2, 0);
1001 msg.hdr.cur_version = WIMLIB_VERSION_CODE;
1002 msg.hdr.data_size = sizeof(msg.data);
1003 msg.data.unmount_flags = unmount_flags;
1005 ret = mq_send(ctx->unmount_to_daemon_mq, (void*)&msg,
1008 ERROR_WITH_ERRNO("Failed to notify filesystem daemon whether "
1009 "we want to commit changes or not");
1010 return WIMLIB_ERR_MQUEUE;
1015 static int receive_unmount_request(struct wimfs_context *ctx,
1021 struct timespec timeout;
1022 ssize_t bytes_received;
1023 struct unmount_request *msg;
1026 ret = get_mailbox(ctx->unmount_to_daemon_mq,
1027 sizeof(struct unmount_request),
1028 &msgsize, &mailbox);
1032 /* Wait at most 3 seconds before giving up and discarding changes. */
1033 gettimeofday(&now, NULL);
1034 timeout.tv_sec = now.tv_sec + 3;
1035 timeout.tv_nsec = now.tv_usec * 1000;
1037 bytes_received = mq_timedreceive(ctx->unmount_to_daemon_mq, mailbox,
1038 msgsize, NULL, &timeout);
1040 if (bytes_received == -1) {
1041 ERROR_WITH_ERRNO("mq_timedreceive()");
1042 ERROR("Not committing.");
1043 ret = WIMLIB_ERR_MQUEUE;
1044 } else if (bytes_received < sizeof(struct unmount_message_header) ||
1045 bytes_received != sizeof(struct unmount_message_header) + msg->hdr.data_size) {
1046 ERROR("Received invalid unmount request");
1047 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1048 } else if (WIMLIB_VERSION_CODE < msg->hdr.min_version) {
1049 ERROR("Cannot understand the unmount request. "
1050 "Please upgrade wimlib to at least v%d.%d.%d",
1051 WIMLIB_GET_MAJOR_VERSION(msg->hdr.min_version),
1052 WIMLIB_GET_MINOR_VERSION(msg->hdr.min_version),
1053 WIMLIB_GET_PATCH_VERSION(msg->hdr.min_version));
1054 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1056 *unmount_flags = msg->data.unmount_flags;
1063 static void send_unmount_response(struct wimfs_context *ctx, int status)
1065 struct unmount_response response;
1066 response.hdr.min_version = WIMLIB_MAKEVERSION(1, 2, 0);
1067 response.hdr.cur_version = WIMLIB_VERSION_CODE;
1068 response.hdr.data_size = sizeof(response.data);
1069 response.data.status = status;
1070 response.data.mount_flags = ctx->mount_flags;
1071 if (mq_send(ctx->daemon_to_unmount_mq, (void*)&response,
1072 sizeof(response), 1))
1073 ERROR_WITH_ERRNO("Failed to send status to unmount process");
1076 /* Wait for a message from the filesytem daemon indicating whether the
1077 * filesystem was unmounted successfully. This may
1078 * take a long time if a big WIM file needs to be rewritten.
1080 * Wait at most 600??? seconds before giving up and returning an error. Either
1081 * it's a really big WIM file, or (more likely) the filesystem daemon has
1082 * crashed or failed for some reason.
1084 * XXX come up with some method to determine if the filesystem daemon has really
1087 * XXX Idea: have mount daemon write its PID into the WIM file header? No, this
1088 * wouldn't work because we know the directory but not the WIM file...
1090 static int receive_unmount_response(struct wimfs_context *ctx)
1095 struct timespec timeout;
1096 ssize_t bytes_received;
1098 struct unmount_response *msg;
1100 ret = get_mailbox(ctx->daemon_to_unmount_mq,
1101 sizeof(struct unmount_response), &msgsize, &mailbox);
1105 DEBUG("Waiting for message telling us whether the unmount was "
1106 "successful or not.");
1108 gettimeofday(&now, NULL);
1109 timeout.tv_sec = now.tv_sec + 600;
1110 timeout.tv_nsec = now.tv_usec * 1000;
1111 bytes_received = mq_timedreceive(ctx->daemon_to_unmount_mq,
1112 mailbox, msgsize, NULL, &timeout);
1114 if (bytes_received == -1) {
1115 if (errno == ETIMEDOUT) {
1116 ERROR("Timed out- probably the filesystem daemon "
1117 "crashed and the WIM was not written "
1119 ret = WIMLIB_ERR_TIMEOUT;
1121 ERROR_WITH_ERRNO("mq_receive()");
1122 ret = WIMLIB_ERR_MQUEUE;
1124 } else if (bytes_received < sizeof(struct unmount_message_header) ||
1125 bytes_received != sizeof(struct unmount_message_header) + msg->hdr.data_size ||
1126 WIMLIB_VERSION_CODE < msg->hdr.min_version) {
1127 ERROR("Received invalid unmount response");
1128 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1129 } else if (WIMLIB_VERSION_CODE < msg->hdr.min_version) {
1130 ERROR("Cannot understand the unmount response. "
1131 "Please upgrade wimlib to at least v%d.%d.%d",
1132 WIMLIB_GET_MAJOR_VERSION(msg->hdr.min_version),
1133 WIMLIB_GET_MINOR_VERSION(msg->hdr.min_version),
1134 WIMLIB_GET_PATCH_VERSION(msg->hdr.min_version));
1135 ret = WIMLIB_ERR_INVALID_UNMOUNT_MESSAGE;
1137 ret = msg->data.status;
1139 ERROR("A failure status (%d) was returned "
1140 "by the filesystem daemon", ret);
1147 /* Execute `fusermount -u', which is installed setuid root, to unmount the WIM.
1149 * FUSE does not yet implement synchronous unmounts. This means that fusermount
1150 * -u will return before the filesystem daemon returns from wimfs_destroy().
1151 * This is partly what we want, because we need to send a message from this
1152 * process to the filesystem daemon telling whether --commit was specified or
1153 * not. However, after that, the unmount process must wait for the filesystem
1154 * daemon to finish writing the WIM file.
1156 static int execute_fusermount(const char *dir)
1164 ERROR_WITH_ERRNO("Failed to fork()");
1165 return WIMLIB_ERR_FORK;
1169 execlp("fusermount", "fusermount", "-u", dir, NULL);
1170 ERROR_WITH_ERRNO("Failed to execute `fusermount'");
1171 exit(WIMLIB_ERR_FUSERMOUNT);
1175 ret = wait(&status);
1177 ERROR_WITH_ERRNO("Failed to wait for fusermount process to "
1179 return WIMLIB_ERR_FUSERMOUNT;
1183 if (status == WIMLIB_ERR_FUSERMOUNT)
1184 ERROR("Could not find the `fusermount' program");
1186 ERROR("fusermount exited with status %d", status);
1188 /* Try again, but with the `umount' program. This is required
1189 * on other FUSE implementations such as FreeBSD's that do not
1190 * have a `fusermount' program. */
1194 ERROR_WITH_ERRNO("Failed to fork()");
1195 return WIMLIB_ERR_FORK;
1199 execlp("umount", "umount", dir, NULL);
1200 ERROR_WITH_ERRNO("Failed to execute `umount'");
1201 exit(WIMLIB_ERR_FUSERMOUNT);
1205 ret = wait(&status);
1207 ERROR_WITH_ERRNO("Failed to wait for `umount' process to "
1209 return WIMLIB_ERR_FUSERMOUNT;
1212 ERROR("`umount' exited with failure status");
1213 return WIMLIB_ERR_FUSERMOUNT;
1219 static int wimfs_access(const char *path, int mask)
1221 /* Permissions not implemented */
1225 static int wimfs_chmod(const char *path, mode_t mask)
1227 struct dentry *dentry;
1228 struct wimfs_context *ctx = wimfs_get_context();
1229 struct inode *inode;
1233 ret = lookup_resource(ctx->wim, path,
1234 get_lookup_flags(ctx) | LOOKUP_FLAG_DIRECTORY_OK,
1235 &dentry, NULL, NULL);
1238 inode = dentry->d_inode;
1239 inode_to_stbuf(inode, NULL, &stbuf);
1240 if (mask == stbuf.st_mode)
1246 /* Called when the filesystem is unmounted. */
1247 static void wimfs_destroy(void *p)
1249 /* The `imagex unmount' command, which is running in a separate process
1250 * and is executing the wimlib_unmount() function, will send this
1251 * process a `struct unmount_request' through a message queue that
1252 * specifies some unmount options, such as other to commit changes or
1257 struct wimfs_context *ctx;
1260 ctx = wimfs_get_context();
1261 if (open_message_queues(ctx, true))
1264 ret = receive_unmount_request(ctx, &unmount_flags);
1267 if (ctx->mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
1268 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_COMMIT) {
1270 if (unmount_flags & WIMLIB_UNMOUNT_FLAG_CHECK_INTEGRITY)
1271 write_flags = WIMLIB_WRITE_FLAG_CHECK_INTEGRITY;
1274 status = rebuild_wim(ctx, write_flags);
1276 ret = delete_staging_dir(ctx);
1278 ERROR("Failed to delete the staging directory");
1283 DEBUG("Read-only mount");
1286 send_unmount_response(ctx, status);
1287 close_message_queues(ctx);
1291 static int wimfs_fallocate(const char *path, int mode,
1292 off_t offset, off_t len, struct fuse_file_info *fi)
1294 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1295 wimlib_assert(fd->staging_fd != -1);
1296 return fallocate(fd->staging_fd, mode, offset, len);
1301 static int wimfs_fgetattr(const char *path, struct stat *stbuf,
1302 struct fuse_file_info *fi)
1304 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1305 return inode_to_stbuf(fd->f_inode, fd->f_lte, stbuf);
1308 static int wimfs_ftruncate(const char *path, off_t size,
1309 struct fuse_file_info *fi)
1311 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1312 int ret = ftruncate(fd->staging_fd, size);
1315 if (fd->f_lte && size < fd->f_lte->resource_entry.original_size)
1316 fd->f_lte->resource_entry.original_size = size;
1321 * Fills in a `struct stat' that corresponds to a file or directory in the WIM.
1323 static int wimfs_getattr(const char *path, struct stat *stbuf)
1325 struct dentry *dentry;
1326 struct lookup_table_entry *lte;
1328 struct wimfs_context *ctx = wimfs_get_context();
1330 ret = lookup_resource(ctx->wim, path,
1331 get_lookup_flags(ctx) | LOOKUP_FLAG_DIRECTORY_OK,
1332 &dentry, <e, NULL);
1335 return inode_to_stbuf(dentry->d_inode, lte, stbuf);
1339 /* Read an alternate data stream through the XATTR interface, or get its size */
1340 static int wimfs_getxattr(const char *path, const char *name, char *value,
1344 struct inode *inode;
1345 struct ads_entry *ads_entry;
1347 struct lookup_table_entry *lte;
1348 struct wimfs_context *ctx = wimfs_get_context();
1350 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1353 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1357 inode = wim_pathname_to_inode(ctx->wim, path);
1361 ads_entry = inode_get_ads_entry(inode, name, NULL);
1365 lte = ads_entry->lte;
1366 res_size = wim_resource_size(lte);
1371 if (res_size > size)
1374 ret = read_full_wim_resource(lte, (u8*)value,
1375 WIMLIB_RESOURCE_FLAG_MULTITHREADED);
1383 /* Create a hard link */
1384 static int wimfs_link(const char *to, const char *from)
1386 struct dentry *from_dentry, *from_dentry_parent;
1387 const char *link_name;
1388 struct inode *inode;
1389 struct lookup_table_entry *lte;
1390 WIMStruct *w = wimfs_get_WIMStruct();
1393 inode = wim_pathname_to_inode(w, to);
1397 if (inode->attributes & FILE_ATTRIBUTE_REPARSE_POINT)
1400 if (inode->attributes & FILE_ATTRIBUTE_DIRECTORY)
1403 from_dentry_parent = get_parent_dentry(w, from);
1404 if (!from_dentry_parent)
1406 if (!dentry_is_directory(from_dentry_parent))
1409 link_name = path_basename(from);
1410 if (get_dentry_child_with_name(from_dentry_parent, link_name))
1412 from_dentry = new_dentry(link_name);
1416 inode_add_dentry(from_dentry, inode);
1417 from_dentry->d_inode = inode;
1418 inode->link_count++;
1421 inode->lte->refcnt++;
1422 for (i = 0; i <= inode->num_ads; i++) {
1423 lte = inode_stream_lte_resolved(inode, i);
1427 dentry_add_child(from_dentry_parent, from_dentry);
1432 static int wimfs_listxattr(const char *path, char *list, size_t size)
1435 struct inode *inode;
1436 struct wimfs_context *ctx = wimfs_get_context();
1440 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1443 /* List alternate data streams, or get the list size */
1445 inode = wim_pathname_to_inode(ctx->wim, path);
1451 for (i = 0; i < inode->num_ads; i++)
1452 needed_size += inode->ads_entries[i].stream_name_utf8_len + 6;
1456 for (i = 0; i < inode->num_ads; i++) {
1457 needed_size = inode->ads_entries[i].stream_name_utf8_len + 6;
1458 if (needed_size > size)
1460 p += sprintf(p, "user.%s",
1461 inode->ads_entries[i].stream_name_utf8) + 1;
1462 size -= needed_size;
1470 /* Create a directory in the WIM.
1471 * @mode is currently ignored. */
1472 static int wimfs_mkdir(const char *path, mode_t mode)
1474 struct dentry *dentry;
1477 ret = create_dentry(wimfs_get_context(), path, &dentry);
1479 dentry->d_inode->attributes = FILE_ATTRIBUTE_DIRECTORY;
1483 /* Create a regular file in the WIM.
1484 * @mode is currently ignored. */
1485 static int wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
1487 const char *stream_name;
1488 struct wimfs_context *ctx = wimfs_get_context();
1489 if ((ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
1490 && (stream_name = path_stream_name(path))) {
1491 /* Make an alternate data stream */
1492 struct ads_entry *new_entry;
1493 struct inode *inode;
1495 char *p = (char*)stream_name - 1;
1496 wimlib_assert(*p == ':');
1499 inode = wim_pathname_to_inode(ctx->wim, path);
1502 if (inode->attributes &
1503 (FILE_ATTRIBUTE_REPARSE_POINT | FILE_ATTRIBUTE_DIRECTORY))
1505 if (inode_get_ads_entry(inode, stream_name, NULL))
1507 new_entry = inode_add_ads(inode, stream_name);
1512 struct dentry *dentry;
1515 /* Make a normal file (not an alternate data stream) */
1516 ret = create_dentry(ctx, path, &dentry);
1518 dentry->d_inode->attributes = FILE_ATTRIBUTE_NORMAL;
1525 static int wimfs_open(const char *path, struct fuse_file_info *fi)
1527 struct dentry *dentry;
1528 struct lookup_table_entry *lte;
1530 struct wimlib_fd *fd;
1531 struct inode *inode;
1534 struct wimfs_context *ctx = wimfs_get_context();
1536 ret = lookup_resource(ctx->wim, path, get_lookup_flags(ctx),
1537 &dentry, <e, &stream_idx);
1541 inode = dentry->d_inode;
1543 if (stream_idx == 0)
1546 stream_id = inode->ads_entries[stream_idx - 1].stream_id;
1548 /* The file resource may be in the staging directory (read-write mounts
1549 * only) or in the WIM. If it's in the staging directory, we need to
1550 * open a native file descriptor for the corresponding file. Otherwise,
1551 * we can read the file resource directly from the WIM file if we are
1552 * opening it read-only, but we need to extract the resource to the
1553 * staging directory if we are opening it writable. */
1555 if (flags_writable(fi->flags) &&
1556 (!lte || lte->resource_location != RESOURCE_IN_STAGING_FILE)) {
1557 u64 size = (lte) ? wim_resource_size(lte) : 0;
1558 ret = extract_resource_to_staging_dir(inode, stream_id,
1564 ret = alloc_wimlib_fd(inode, stream_id, lte, &fd,
1565 wimfs_ctx_readonly(ctx));
1569 if (lte && lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1570 fd->staging_fd = open(lte->staging_file_name, fi->flags);
1571 if (fd->staging_fd == -1) {
1572 int errno_save = errno;
1573 close_wimlib_fd(fd);
1577 fi->fh = (uintptr_t)fd;
1581 /* Opens a directory. */
1582 static int wimfs_opendir(const char *path, struct fuse_file_info *fi)
1584 struct inode *inode;
1586 struct wimlib_fd *fd = NULL;
1587 struct wimfs_context *ctx = wimfs_get_context();
1588 WIMStruct *w = ctx->wim;
1590 inode = wim_pathname_to_inode(w, path);
1593 if (!inode_is_directory(inode))
1595 ret = alloc_wimlib_fd(inode, 0, NULL, &fd, wimfs_ctx_readonly(ctx));
1596 fi->fh = (uintptr_t)fd;
1602 * Read data from a file in the WIM or in the staging directory.
1604 static int wimfs_read(const char *path, char *buf, size_t size,
1605 off_t offset, struct fuse_file_info *fi)
1607 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1613 if (!fd->f_lte) /* Empty stream with no lookup table entry */
1616 if (fd->f_lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1617 /* Read from staging file */
1619 wimlib_assert(fd->f_lte->staging_file_name);
1620 wimlib_assert(fd->staging_fd != -1);
1622 DEBUG("Seek to offset %zu", offset);
1624 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
1626 ret = read(fd->staging_fd, buf, size);
1632 u64 res_size = wim_resource_size(fd->f_lte);
1633 if (offset > res_size)
1635 size = min(size, res_size - offset);
1636 if (read_wim_resource(fd->f_lte, (u8*)buf,
1638 WIMLIB_RESOURCE_FLAG_MULTITHREADED) != 0)
1644 struct fill_params {
1646 fuse_fill_dir_t filler;
1649 static int dentry_fuse_fill(struct dentry *dentry, void *arg)
1651 struct fill_params *fill_params = arg;
1652 return fill_params->filler(fill_params->buf, dentry->file_name_utf8,
1656 /* Fills in the entries of the directory specified by @path using the
1657 * FUSE-provided function @filler. */
1658 static int wimfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
1659 off_t offset, struct fuse_file_info *fi)
1661 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1662 struct inode *inode;
1667 inode = fd->f_inode;
1669 struct fill_params fill_params = {
1674 filler(buf, ".", NULL, 0);
1675 filler(buf, "..", NULL, 0);
1677 return for_dentry_in_rbtree(inode->children.rb_node,
1678 dentry_fuse_fill, &fill_params);
1682 static int wimfs_readlink(const char *path, char *buf, size_t buf_len)
1684 struct wimfs_context *ctx = wimfs_get_context();
1685 struct inode *inode = wim_pathname_to_inode(ctx->wim, path);
1689 if (!inode_is_symlink(inode))
1692 ret = inode_readlink(inode, buf, buf_len, ctx->wim,
1693 WIMLIB_RESOURCE_FLAG_MULTITHREADED);
1700 static int wimfs_release(const char *path, struct fuse_file_info *fi)
1702 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1703 return close_wimlib_fd(fd);
1706 /* Close a directory */
1707 static int wimfs_releasedir(const char *path, struct fuse_file_info *fi)
1709 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
1710 return close_wimlib_fd(fd);
1714 /* Remove an alternate data stream through the XATTR interface */
1715 static int wimfs_removexattr(const char *path, const char *name)
1717 struct inode *inode;
1718 struct ads_entry *ads_entry;
1720 struct wimfs_context *ctx = wimfs_get_context();
1722 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1725 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1729 inode = wim_pathname_to_inode(ctx->wim, path);
1733 ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
1736 inode_remove_ads(inode, ads_idx, ctx->wim->lookup_table);
1741 /* Renames a file or directory. See rename (3) */
1742 static int wimfs_rename(const char *from, const char *to)
1746 struct dentry *parent_of_dst;
1747 char *file_name_utf16 = NULL, *file_name_utf8 = NULL;
1748 u16 file_name_utf16_len, file_name_utf8_len;
1749 WIMStruct *w = wimfs_get_WIMStruct();
1752 /* This rename() implementation currently only supports actual files
1753 * (not alternate data streams) */
1755 src = get_dentry(w, from);
1759 dst = get_dentry(w, to);
1761 ret = get_names(&file_name_utf16, &file_name_utf8,
1762 &file_name_utf16_len, &file_name_utf8_len,
1768 /* Destination file exists */
1770 if (src == dst) /* Same file */
1773 if (!dentry_is_directory(src)) {
1774 /* Cannot rename non-directory to directory. */
1775 if (dentry_is_directory(dst))
1778 /* Cannot rename directory to a non-directory or a non-empty
1780 if (!dentry_is_directory(dst))
1782 if (inode_has_children(dst->d_inode))
1785 parent_of_dst = dst->parent;
1786 remove_dentry(dst, w->lookup_table);
1788 /* Destination does not exist */
1789 parent_of_dst = get_parent_dentry(w, to);
1793 if (!dentry_is_directory(parent_of_dst))
1797 FREE(src->file_name);
1798 FREE(src->file_name_utf8);
1799 src->file_name = file_name_utf16;
1800 src->file_name_utf8 = file_name_utf8;
1801 src->file_name_len = file_name_utf16_len;
1802 src->file_name_utf8_len = file_name_utf8_len;
1805 dentry_add_child(parent_of_dst, src);
1809 /* Remove a directory */
1810 static int wimfs_rmdir(const char *path)
1812 struct dentry *dentry;
1813 WIMStruct *w = wimfs_get_WIMStruct();
1815 dentry = get_dentry(w, path);
1819 if (!dentry_is_empty_directory(dentry))
1822 remove_dentry(dentry, w->lookup_table);
1827 /* Write an alternate data stream through the XATTR interface */
1828 static int wimfs_setxattr(const char *path, const char *name,
1829 const char *value, size_t size, int flags)
1831 struct ads_entry *existing_ads_entry;
1832 struct ads_entry *new_ads_entry;
1833 struct lookup_table_entry *existing_lte;
1834 struct lookup_table_entry *lte;
1835 struct inode *inode;
1836 u8 value_hash[SHA1_HASH_SIZE];
1838 struct wimfs_context *ctx = wimfs_get_context();
1840 if (!(ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR))
1843 if (strlen(name) < 5 || memcmp(name, "user.", 5) != 0)
1847 inode = wim_pathname_to_inode(ctx->wim, path);
1851 existing_ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
1852 if (existing_ads_entry) {
1853 if (flags & XATTR_CREATE)
1855 inode_remove_ads(inode, ads_idx, ctx->wim->lookup_table);
1857 if (flags & XATTR_REPLACE)
1860 new_ads_entry = inode_add_ads(inode, name);
1864 sha1_buffer((const u8*)value, size, value_hash);
1866 existing_lte = __lookup_resource(ctx->wim->lookup_table, value_hash);
1873 lte = new_lookup_table_entry();
1876 value_copy = MALLOC(size);
1881 memcpy(value_copy, value, size);
1882 lte->resource_location = RESOURCE_IN_ATTACHED_BUFFER;
1883 lte->attached_buffer = value_copy;
1884 lte->resource_entry.original_size = size;
1885 lte->resource_entry.size = size;
1886 lte->resource_entry.flags = 0;
1887 copy_hash(lte->hash, value_hash);
1888 lookup_table_insert(ctx->wim->lookup_table, lte);
1890 new_ads_entry->lte = lte;
1895 static int wimfs_symlink(const char *to, const char *from)
1897 struct wimfs_context *ctx = wimfs_get_context();
1898 struct dentry *dentry;
1901 ret = create_dentry(ctx, from, &dentry);
1903 dentry->d_inode->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
1904 dentry->d_inode->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
1905 if (inode_set_symlink(dentry->d_inode, to,
1906 ctx->wim->lookup_table, NULL))
1908 unlink_dentry(dentry);
1909 free_dentry(dentry);
1917 /* Reduce the size of a file */
1918 static int wimfs_truncate(const char *path, off_t size)
1920 struct dentry *dentry;
1921 struct lookup_table_entry *lte;
1925 struct inode *inode;
1926 struct wimfs_context *ctx = wimfs_get_context();
1928 ret = lookup_resource(ctx->wim, path, get_lookup_flags(ctx),
1929 &dentry, <e, &stream_idx);
1934 if (lte == NULL && size == 0)
1937 inode = dentry->d_inode;
1938 if (stream_idx == 0)
1941 stream_id = inode->ads_entries[stream_idx - 1].stream_id;
1943 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
1944 ret = truncate(lte->staging_file_name, size);
1948 /* File in WIM. Extract it to the staging directory, but only
1949 * the first @size bytes of it. */
1950 ret = extract_resource_to_staging_dir(inode, stream_id,
1956 /* Unlink a non-directory or alternate data stream */
1957 static int wimfs_unlink(const char *path)
1959 struct dentry *dentry;
1960 struct lookup_table_entry *lte;
1963 struct wimfs_context *ctx = wimfs_get_context();
1965 ret = lookup_resource(ctx->wim, path, get_lookup_flags(ctx),
1966 &dentry, <e, &stream_idx);
1971 if (stream_idx == 0)
1972 remove_dentry(dentry, ctx->wim->lookup_table);
1974 inode_remove_ads(dentry->d_inode, stream_idx - 1,
1975 ctx->wim->lookup_table);
1979 #ifdef HAVE_UTIMENSAT
1981 * Change the timestamp on a file dentry.
1983 * Note that alternate data streams do not have their own timestamps.
1985 static int wimfs_utimens(const char *path, const struct timespec tv[2])
1987 struct dentry *dentry;
1988 struct inode *inode;
1989 WIMStruct *w = wimfs_get_WIMStruct();
1991 dentry = get_dentry(w, path);
1994 inode = dentry->d_inode;
1996 if (tv[0].tv_nsec != UTIME_OMIT) {
1997 if (tv[0].tv_nsec == UTIME_NOW)
1998 inode->last_access_time = get_wim_timestamp();
2000 inode->last_access_time = timespec_to_wim_timestamp(&tv[0]);
2002 if (tv[1].tv_nsec != UTIME_OMIT) {
2003 if (tv[1].tv_nsec == UTIME_NOW)
2004 inode->last_write_time = get_wim_timestamp();
2006 inode->last_write_time = timespec_to_wim_timestamp(&tv[1]);
2011 static int wimfs_utime(const char *path, struct utimbuf *times)
2013 struct dentry *dentry;
2014 struct inode *inode;
2015 WIMStruct *w = wimfs_get_WIMStruct();
2017 dentry = get_dentry(w, path);
2020 inode = dentry->d_inode;
2022 inode->last_write_time = unix_timestamp_to_wim(times->modtime);
2023 inode->last_access_time = unix_timestamp_to_wim(times->actime);
2028 /* Writes to a file in the WIM filesystem.
2029 * It may be an alternate data stream, but here we don't even notice because we
2030 * just get a lookup table entry. */
2031 static int wimfs_write(const char *path, const char *buf, size_t size,
2032 off_t offset, struct fuse_file_info *fi)
2034 struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
2041 wimlib_assert(fd->f_lte);
2042 wimlib_assert(fd->f_lte->staging_file_name);
2043 wimlib_assert(fd->staging_fd != -1);
2044 wimlib_assert(fd->f_inode);
2046 /* Seek to the requested position */
2047 if (lseek(fd->staging_fd, offset, SEEK_SET) == -1)
2050 /* Write the data. */
2051 ret = write(fd->staging_fd, buf, size);
2055 now = get_wim_timestamp();
2056 fd->f_inode->last_write_time = now;
2057 fd->f_inode->last_access_time = now;
2061 static struct fuse_operations wimfs_operations = {
2062 .access = wimfs_access,
2063 .chmod = wimfs_chmod,
2064 .destroy = wimfs_destroy,
2066 .fallocate = wimfs_fallocate,
2068 .fgetattr = wimfs_fgetattr,
2069 .ftruncate = wimfs_ftruncate,
2070 .getattr = wimfs_getattr,
2072 .getxattr = wimfs_getxattr,
2076 .listxattr = wimfs_listxattr,
2078 .mkdir = wimfs_mkdir,
2079 .mknod = wimfs_mknod,
2081 .opendir = wimfs_opendir,
2083 .readdir = wimfs_readdir,
2084 .readlink = wimfs_readlink,
2085 .release = wimfs_release,
2086 .releasedir = wimfs_releasedir,
2088 .removexattr = wimfs_removexattr,
2090 .rename = wimfs_rename,
2091 .rmdir = wimfs_rmdir,
2093 .setxattr = wimfs_setxattr,
2095 .symlink = wimfs_symlink,
2096 .truncate = wimfs_truncate,
2097 .unlink = wimfs_unlink,
2098 #ifdef HAVE_UTIMENSAT
2099 .utimens = wimfs_utimens,
2101 .utime = wimfs_utime,
2103 .write = wimfs_write,
2105 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 8)
2106 .flag_nullpath_ok = 1,
2108 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 9)
2110 .flag_utime_omit_ok = 1,
2115 /* Mounts an image from a WIM file. */
2116 WIMLIBAPI int wimlib_mount_image(WIMStruct *wim, int image, const char *dir,
2117 int mount_flags, WIMStruct **additional_swms,
2118 unsigned num_additional_swms,
2119 const char *staging_dir)
2125 struct lookup_table *joined_tab, *wim_tab_save;
2126 struct image_metadata *imd;
2127 struct wimfs_context ctx;
2128 struct hlist_node *cur_node;
2129 struct inode *inode;
2131 DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
2132 wim, image, dir, mount_flags);
2135 return WIMLIB_ERR_INVALID_PARAM;
2137 ret = verify_swm_set(wim, additional_swms, num_additional_swms);
2141 if ((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) && (wim->hdr.total_parts != 1)) {
2142 ERROR("Cannot mount a split WIM read-write");
2143 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
2146 if (num_additional_swms) {
2147 ret = new_joined_lookup_table(wim, additional_swms,
2148 num_additional_swms,
2152 wim_tab_save = wim->lookup_table;
2153 wim->lookup_table = joined_tab;
2156 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2157 ret = wim_run_full_verifications(wim);
2162 ret = select_wim_image(wim, image);
2167 imd = &wim->image_metadata[image - 1];
2169 DEBUG("Selected image %d", image);
2171 if (imd->root_dentry->refcnt != 1) {
2172 ERROR("Cannot mount image that was just exported with "
2173 "wimlib_export_image()");
2174 ret = WIMLIB_ERR_INVALID_PARAM;
2178 if (imd->modified) {
2179 ERROR("Cannot mount image that was added "
2180 "with wimlib_add_image()");
2181 ret = WIMLIB_ERR_INVALID_PARAM;
2185 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2186 ret = lock_wim(wim->fp, wim->filename);
2191 if (!(mount_flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
2192 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |
2193 WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)))
2194 mount_flags |= WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR;
2197 DEBUG("Initializing struct wimfs_context");
2198 init_wimfs_context(&ctx);
2200 ctx.mount_flags = mount_flags;
2202 if (mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
2203 ctx.default_lookup_flags = LOOKUP_FLAG_ADS_OK;
2205 DEBUG("Unlinking message queues in case they already exist");
2206 ret = set_message_queue_names(&ctx, dir);
2209 unlink_message_queues(&ctx);
2211 DEBUG("Preparing arguments to fuse_main()");
2213 dir_copy = STRDUP(dir);
2215 goto out_free_message_queue_names;
2217 argv[argc++] = "imagex";
2218 argv[argc++] = dir_copy;
2220 /* disable multi-threaded operation for read-write mounts */
2221 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)
2222 argv[argc++] = "-s";
2224 if (mount_flags & WIMLIB_MOUNT_FLAG_DEBUG)
2225 argv[argc++] = "-d";
2228 * We provide the use_ino option because we are going to assign inode
2229 * numbers oursides. The inodes will be given unique numbers in the
2230 * assign_inode_numbers() function, and the static variable @next_ino is
2231 * set to the next available inode number.
2233 char optstring[256] =
2237 #if FUSE_MAJOR_VERSION > 2 || (FUSE_MAJOR_VERSION == 2 && FUSE_MINOR_VERSION >= 8)
2241 argv[argc++] = "-o";
2242 argv[argc++] = optstring;
2243 if ((mount_flags & WIMLIB_MOUNT_FLAG_READWRITE)) {
2244 /* Read-write mount. Make the staging directory */
2245 ret = make_staging_dir(&ctx, staging_dir);
2247 goto out_free_dir_copy;
2249 /* Read-only mount */
2250 strcat(optstring, ",ro");
2257 DEBUG("FUSE command line (argc = %d): ", argc);
2258 for (i = 0; i < argc; i++) {
2259 fputs(argv[i], stdout);
2267 /* Mark dentry tree as modified if read-write mount. */
2268 if (mount_flags & WIMLIB_MOUNT_FLAG_READWRITE) {
2270 imd->has_been_mounted_rw = 1;
2273 /* Resolve the lookup table entries for every inode in the image, and
2274 * assign inode numbers */
2275 DEBUG("Resolving lookup table entries and assigning inode numbers");
2278 hlist_for_each_entry(inode, cur_node, &imd->inode_list, hlist) {
2279 inode_resolve_ltes(inode, wim->lookup_table);
2280 inode->ino = ctx.next_ino++;
2282 /*ctx.next_ino = assign_inode_numbers(&imd->inode_list);*/
2283 DEBUG("(next_ino = %"PRIu64")", ctx.next_ino);
2285 DEBUG("Calling fuse_main()");
2287 ret = fuse_main(argc, argv, &wimfs_operations, &ctx);
2289 DEBUG("Returned from fuse_main() (ret = %d)", ret);
2291 ret = WIMLIB_ERR_FUSE;
2294 out_free_message_queue_names:
2295 free_message_queue_names(&ctx);
2297 if (num_additional_swms) {
2298 free_lookup_table(wim->lookup_table);
2299 wim->lookup_table = wim_tab_save;
2305 * Unmounts the WIM file that was previously mounted on @dir by using
2306 * wimlib_mount_image().
2308 WIMLIBAPI int wimlib_unmount_image(const char *dir, int unmount_flags,
2309 wimlib_progress_func_t progress_func)
2312 struct wimfs_context ctx;
2314 init_wimfs_context(&ctx);
2316 ret = set_message_queue_names(&ctx, dir);
2320 ret = open_message_queues(&ctx, false);
2322 goto out_free_message_queue_names;
2324 ret = send_unmount_request(&ctx, unmount_flags);
2326 goto out_close_message_queues;
2328 ret = execute_fusermount(dir);
2330 goto out_close_message_queues;
2331 ret = receive_unmount_response(&ctx);
2332 out_close_message_queues:
2333 close_message_queues(&ctx);
2334 out_free_message_queue_names:
2335 free_message_queue_names(&ctx);
2340 #else /* WITH_FUSE */
2343 static inline int mount_unsupported_error()
2345 ERROR("wimlib was compiled with --without-fuse, which disables support "
2346 "for mounting WIMs.");
2347 return WIMLIB_ERR_UNSUPPORTED;
2350 WIMLIBAPI int wimlib_unmount_image(const char *dir, int unmount_flags,
2351 wimlib_progress_func_t progress_func)
2353 return mount_unsupported_error();
2356 WIMLIBAPI int wimlib_mount_image(WIMStruct *wim_p, int image, const char *dir,
2357 int mount_flags, WIMStruct **additional_swms,
2358 unsigned num_additional_swms,
2359 const char *staging_dir)
2361 return mount_unsupported_error();
2364 #endif /* WITH_FUSE */