+/* Next inode number to be assigned. */
+static u64 next_ino;
+
+/* List of lookup table entries in the staging directory */
+static LIST_HEAD(staging_list);
+
+static inline int get_lookup_flags()
+{
+ if (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, w->lookup_table);
+ 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)
+{
+ size_t name_len;
+ char *name;
+ struct stat stbuf;
+ int fd;
+ int errno_save;
+
+ name_len = staging_dir_name_len + 1 + SHA1_HASH_SIZE;
+ name = MALLOC(name_len + 1);
+ if (!name) {
+ errno = ENOMEM;
+ return -1;
+ }
+
+ do {
+
+ memcpy(name, staging_dir_name, staging_dir_name_len);
+ name[staging_dir_name_len] = '/';
+ randomize_char_array_with_alnum(name + staging_dir_name_len + 1,
+ SHA1_HASH_SIZE);
+ name[name_len] = '\0';
+
+
+ /* Just in case, verify that the randomly generated name doesn't name an
+ * existing file, and try again if so */
+ } while (stat(name, &stbuf) == 0);
+
+ if (errno != ENOENT)
+ /* other error! */
+ return -1;
+
+ /* doesn't exist--- ok */
+
+ DEBUG("Creating staging file `%s'", name);
+
+ fd = open(name, open_flags | O_CREAT | O_TRUNC, 0600);
+ if (fd == -1) {
+ errno_save = errno;
+ FREE(name);
+ errno = errno_save;
+ } else {
+ *name_ret = name;
+ }
+ return fd;
+}
+
+/*
+ * Extract a WIM resource to the staging directory.
+ *
+ * @inode: Inode that contains the stream we are extracting
+ *
+ * @stream_id: Identifier for the stream (it stays constant even if the indices
+ * of the stream entries are changed)
+ *
+ * @lte: Pointer to pointer to the lookup table entry for the stream we need to
+ * extract, or NULL if there was no lookup table entry present for the stream
+ *
+ * @size: Number of bytes of the stream we want to extract (this supports the
+ * wimfs_truncate() function).
+ */
+static int extract_resource_to_staging_dir(struct inode *inode,
+ u32 stream_id,
+ struct lookup_table_entry **lte,
+ off_t size)
+{
+ char *staging_file_name;
+ int ret;
+ int fd;
+ struct lookup_table_entry *old_lte, *new_lte;
+
+ DEBUG("Extracting resource to staging dir: inode %"PRIu64", "
+ "stream id %"PRIu32, inode->ino, stream_id);
+
+ old_lte = *lte;
+ fd = create_staging_file(&staging_file_name, O_WRONLY);
+ if (fd == -1)
+ return -errno;
+
+ if (old_lte)
+ ret = extract_wim_resource_to_fd(old_lte, fd, size);
+ else
+ ret = 0;
+ if (ret != 0 || close(fd) != 0) {
+ if (errno != 0)
+ ret = -errno;
+ else
+ ret = -EIO;
+ close(fd);
+ goto out_delete_staging_file;
+ }
+
+ if (old_lte && inode->link_count == old_lte->refcnt) {
+ /* The reference count of the existing lookup table
+ * entry is the same as the link count of the inode that
+ * contains the stream we're opening. Therefore, ALL
+ * the references to the lookup table entry correspond
+ * to the stream we're trying to extract, so the lookup
+ * table entry can be re-used. */
+ DEBUG("Re-using lookup table entry");
+ lookup_table_unlink(w->lookup_table, old_lte);
+ new_lte = old_lte;
+ } else {
+ if (old_lte) {
+ /* There's an existing lookup table entry, but its
+ * reference count is creater than the link count for
+ * the inode containing a stream we're opening.
+ * Therefore, we need to split the lookup table entry.
+ * */
+ wimlib_assert(old_lte->refcnt > inode->link_count);
+ DEBUG("Splitting lookup table entry "
+ "(inode->link_count = %zu, old_lte->refcnt = %u)",
+ inode->link_count, old_lte->refcnt);
+
+ }
+
+ new_lte = new_lookup_table_entry();
+ if (!new_lte) {
+ ret = -ENOMEM;
+ goto out_delete_staging_file;
+ }
+
+ /* There may already be open file descriptors to this stream if
+ * it's previously been opened read-only, but just now we're
+ * opening it read-write. Identify those file descriptors and
+ * change their lookup table entry pointers to point to the new
+ * lookup table entry, and open staging file descriptors for
+ * them.
+ *
+ * At the same time, we need to count the number of these opened
+ * file descriptors to the new lookup table entry. If there's
+ * an old lookup table entry, this number needs to be subtracted
+ * from the fd's opened to the old entry. */
+ for (u16 i = 0, j = 0; j < inode->num_opened_fds; i++) {
+ struct wimlib_fd *fd = inode->fds[i];
+ if (fd) {
+ if (fd->stream_id == stream_id) {
+ wimlib_assert(fd->f_lte == old_lte);
+ wimlib_assert(fd->staging_fd == -1);
+ fd->f_lte = new_lte;
+ new_lte->num_opened_fds++;
+ fd->staging_fd = open(staging_file_name, O_RDONLY);
+ if (fd->staging_fd == -1) {
+ ret = -errno;
+ goto out_revert_fd_changes;
+ }
+ }
+ j++;
+ }
+ }
+ DEBUG("%zu fd's were already opened to the file we extracted",
+ new_lte->num_opened_fds);
+ if (old_lte) {
+ old_lte->num_opened_fds -= new_lte->num_opened_fds;
+ old_lte->refcnt -= inode->link_count;
+ }
+ }
+
+ new_lte->resource_entry.original_size = size;
+ new_lte->refcnt = inode->link_count;
+ new_lte->resource_location = RESOURCE_IN_STAGING_FILE;
+ new_lte->staging_file_name = staging_file_name;
+ new_lte->lte_inode = inode;
+ random_hash(new_lte->hash);
+
+ if (stream_id == 0)
+ inode->lte = new_lte;
+ else
+ for (u16 i = 0; i < inode->num_ads; i++)
+ if (inode->ads_entries[i].stream_id == stream_id)
+ inode->ads_entries[i].lte = new_lte;
+
+ lookup_table_insert(w->lookup_table, new_lte);
+ list_add(&new_lte->staging_list, &staging_list);
+ *lte = new_lte;
+ return 0;
+out_revert_fd_changes:
+ for (u16 i = 0, j = 0; j < new_lte->num_opened_fds; i++) {
+ struct wimlib_fd *fd = inode->fds[i];
+ if (fd && fd->stream_id == stream_id && fd->f_lte == new_lte) {
+ fd->f_lte = old_lte;
+ if (fd->staging_fd != -1) {
+ close(fd->staging_fd);
+ fd->staging_fd = -1;
+ }
+ j++;
+ }
+ }
+out_free_new_lte:
+ free_lookup_table_entry(new_lte);
+out_delete_staging_file:
+ unlink(staging_file_name);
+ FREE(staging_file_name);
+ return ret;
+}