+#include <utime.h>
+
+#ifdef ENABLE_XATTR
+#include <attr/xattr.h>
+#endif
+
+/* File descriptor to a file open on the WIM filesystem. */
+struct wimlib_fd {
+ struct inode *f_inode;
+ struct lookup_table_entry *f_lte;
+ int staging_fd;
+ u16 idx;
+ u32 stream_id;
+};
+
+struct wimfs_context {
+ /* The WIMStruct for the mounted WIM. */
+ WIMStruct *wim;
+
+ /* Working directory when `imagex mount' is run. */
+ char *working_directory;
+
+ /* Name of the staging directory for a read-write mount. Whenever a new file is
+ * created, it is done so in the staging directory. Furthermore, whenever a
+ * file in the WIM is modified, it is extracted to the staging directory. If
+ * changes are commited when the WIM is unmounted, the file resources are merged
+ * in from the staging directory when writing the new WIM. */
+ char *staging_dir_name;
+ size_t staging_dir_name_len;
+
+ /* Flags passed to wimlib_mount(). */
+ int mount_flags;
+
+ /* Next inode number to be assigned. */
+ u64 next_ino;
+
+ /* List of lookup table entries in the staging directory */
+ struct list_head staging_list;
+
+ /* Name and message queue descriptors for message queues between the filesystem
+ * daemon process and the unmount process. These are used when the filesystem
+ * is unmounted and the process running wimlib_mount() (i.e. the `imagex
+ * unmount' command) needs to communicate with the filesystem daemon running
+ * fuse_main() (i.e. that spawned by the `imagex mount' or `imagex mountrw'
+ * commands */
+ char *unmount_to_daemon_mq_name;
+ char *daemon_to_unmount_mq_name;
+ mqd_t unmount_to_daemon_mq;
+ mqd_t daemon_to_unmount_mq;
+};
+
+static void init_wimfs_context(struct wimfs_context *ctx)
+{
+ memset(ctx, 0, sizeof(*ctx));
+ ctx->unmount_to_daemon_mq = (mqd_t)-1;
+ ctx->daemon_to_unmount_mq = (mqd_t)-1;
+ INIT_LIST_HEAD(&ctx->staging_list);
+}
+
+static inline struct wimfs_context *wimfs_get_context()
+{
+ return (struct wimfs_context*)fuse_get_context()->private_data;
+}
+
+static inline WIMStruct *wimfs_get_WIMStruct()
+{
+ return wimfs_get_context()->wim;
+}
+
+static inline int get_lookup_flags(const struct wimfs_context *ctx)
+{
+ if (ctx->mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
+ return LOOKUP_FLAG_ADS_OK;
+ else
+ return 0;
+}
+
+/* Returns nonzero if write permission is requested on the file open flags */
+static inline int flags_writable(int open_flags)
+{
+ return open_flags & (O_RDWR | O_WRONLY);
+}
+
+/*
+ * Allocate a file descriptor for a stream.
+ *
+ * @inode: inode containing the stream we're opening
+ * @stream_id: ID of the stream we're opening
+ * @lte: Lookup table entry for the stream (may be NULL)
+ * @fd_ret: Return the allocated file descriptor if successful.
+ *
+ * Return 0 iff successful or error code if unsuccessful.
+ */
+static int alloc_wimlib_fd(struct inode *inode,
+ u32 stream_id,
+ struct lookup_table_entry *lte,
+ struct wimlib_fd **fd_ret)
+{
+ static const u16 fds_per_alloc = 8;
+ static const u16 max_fds = 0xffff;
+
+ DEBUG("Allocating fd for stream ID %u from inode %lx (open = %u, allocated = %u)",
+ stream_id, inode->ino, inode->num_opened_fds,
+ inode->num_allocated_fds);
+
+ if (inode->num_opened_fds == inode->num_allocated_fds) {
+ struct wimlib_fd **fds;
+ u16 num_new_fds;
+
+ if (inode->num_allocated_fds == max_fds)
+ return -EMFILE;
+ num_new_fds = min(fds_per_alloc,
+ max_fds - inode->num_allocated_fds);
+
+ fds = REALLOC(inode->fds,
+ (inode->num_allocated_fds + num_new_fds) *
+ sizeof(inode->fds[0]));
+ if (!fds)
+ return -ENOMEM;
+ memset(&fds[inode->num_allocated_fds], 0,
+ num_new_fds * sizeof(fds[0]));
+ inode->fds = fds;
+ inode->num_allocated_fds += num_new_fds;
+ }
+ for (u16 i = 0; ; i++) {
+ if (!inode->fds[i]) {
+ struct wimlib_fd *fd = CALLOC(1, sizeof(*fd));
+ if (!fd)
+ return -ENOMEM;
+ fd->f_inode = inode;
+ fd->f_lte = lte;
+ fd->staging_fd = -1;
+ fd->idx = i;
+ fd->stream_id = stream_id;
+ *fd_ret = fd;
+ inode->fds[i] = fd;
+ inode->num_opened_fds++;
+ if (lte)
+ lte->num_opened_fds++;
+ DEBUG("Allocated fd");
+ return 0;
+ }
+ }
+}
+
+static void inode_put_fd(struct inode *inode, struct wimlib_fd *fd)
+{
+ wimlib_assert(fd);
+ wimlib_assert(inode);
+ wimlib_assert(fd->f_inode == inode);
+ wimlib_assert(inode->num_opened_fds);
+ wimlib_assert(fd->idx < inode->num_opened_fds);
+ wimlib_assert(inode->fds[fd->idx] == fd);
+
+ inode->fds[fd->idx] = NULL;
+ FREE(fd);
+ if (--inode->num_opened_fds == 0 && inode->link_count == 0)
+ free_inode(inode);
+}
+
+static int lte_put_fd(struct lookup_table_entry *lte, struct wimlib_fd *fd)
+{
+ wimlib_assert(fd);
+ wimlib_assert(fd->f_lte == lte);
+
+ if (!lte) /* Empty stream with no lookup table entry */
+ return 0;
+
+ /* Close staging file descriptor if needed. */
+
+ if (lte->resource_location == RESOURCE_IN_STAGING_FILE
+ && fd->staging_fd != -1)
+ {
+ if (close(fd->staging_fd) != 0) {
+ ERROR_WITH_ERRNO("Failed to close staging file");
+ return -errno;
+ }
+ }
+ lte_decrement_num_opened_fds(lte);
+ return 0;
+}
+
+/* Close a file descriptor. */
+static int close_wimlib_fd(struct wimlib_fd *fd)
+{
+ int ret;
+ wimlib_assert(fd);
+ DEBUG("Closing fd (inode = %lu, opened = %u, allocated = %u)",
+ fd->f_inode->ino, fd->f_inode->num_opened_fds,
+ fd->f_inode->num_allocated_fds);
+ ret = lte_put_fd(fd->f_lte, fd);
+ if (ret != 0)
+ return ret;
+
+ inode_put_fd(fd->f_inode, fd);
+ return 0;
+}
+
+/* Remove a dentry; i.e. remove a reference to the corresponding inode.
+ *
+ * If there are no remaining references to the inode either through dentries or
+ * open file descriptors, the inode is freed. Otherwise, the inode is not
+ * removed, but the dentry is unlinked and freed.
+ *
+ * Either way, all lookup table entries referenced by the inode have their
+ * reference count decremented. If a lookup table entry has no open file
+ * descriptors and no references remaining, it is freed, and the staging file is
+ * unlinked.
+ */
+static void remove_dentry(struct dentry *dentry,
+ struct lookup_table *lookup_table)
+{
+ struct inode *inode = dentry->d_inode;
+ struct lookup_table_entry *lte;
+ unsigned i;
+
+ for (i = 0; i <= inode->num_ads; i++) {
+ lte = inode_stream_lte_resolved(inode, i);
+ if (lte)
+ lte_decrement_refcnt(lte, lookup_table);
+ }
+ unlink_dentry(dentry);
+ put_dentry(dentry);
+}
+
+/* Creates a new staging file and returns its file descriptor opened for
+ * writing.
+ *
+ * @name_ret: A location into which the a pointer to the newly allocated name of
+ * the staging file is stored.
+ * @return: The file descriptor for the new file. Returns -1 and sets errno on
+ * error, for any reason possible from the creat() function.
+ */
+static int create_staging_file(char **name_ret, int open_flags,
+ struct wimfs_context *ctx)
+{
+ size_t name_len;
+ char *name;
+ struct stat stbuf;
+ int fd;
+ int errno_save;
+
+ name_len = ctx->staging_dir_name_len + 1 + SHA1_HASH_SIZE;
+ name = MALLOC(name_len + 1);
+ if (!name) {
+ errno = ENOMEM;
+ return -1;
+ }
+
+ do {
+
+ memcpy(name, ctx->staging_dir_name, ctx->staging_dir_name_len);
+ name[ctx->staging_dir_name_len] = '/';
+ randomize_char_array_with_alnum(name + ctx->staging_dir_name_len + 1,
+ SHA1_HASH_SIZE);
+ name[name_len] = '\0';
+