+/*
+ * 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';
+
+
+ /* 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;