+ if (inode->num_opened_fds == inode->num_allocated_fds) {
+ struct wimlib_fd **fds;
+ u16 num_new_fds;
+
+ if (inode->num_allocated_fds == max_fds) {
+ ret = -EMFILE;
+ goto out;
+ }
+ 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) {
+ ret = -ENOMEM;
+ goto out;
+ }
+ 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) {
+ ret = -ENOMEM;
+ break;
+ }
+ 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 && !readonly)
+ lte->num_opened_fds++;
+ DEBUG("Allocated fd (idx = %u)", fd->idx);
+ ret = 0;
+ break;
+ }
+ }
+out:
+ pthread_mutex_unlock(&inode->i_mutex);
+ return ret;
+}
+
+static void inode_put_fd(struct inode *inode, struct wimlib_fd *fd)
+{
+ wimlib_assert(fd != NULL);
+ wimlib_assert(inode != NULL);
+
+ pthread_mutex_lock(&inode->i_mutex);
+
+ wimlib_assert(fd->f_inode == inode);
+ wimlib_assert(inode->num_opened_fds != 0);
+ wimlib_assert(fd->idx < inode->num_allocated_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) {
+ pthread_mutex_unlock(&inode->i_mutex);
+ free_inode(inode);
+ } else {
+ pthread_mutex_unlock(&inode->i_mutex);
+ }
+}
+
+static int lte_put_fd(struct lookup_table_entry *lte, struct wimlib_fd *fd)
+{
+ wimlib_assert(fd != NULL);
+ 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 != NULL);
+ 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;
+
+ /* 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,
+ struct wimfs_context *ctx)
+{
+ 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, ctx);
+ 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(ctx->wim->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 = %u, 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("%hu 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(ctx->wim->lookup_table, new_lte);
+ list_add(&new_lte->staging_list, &ctx->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++;
+ }
+ }
+ free_lookup_table_entry(new_lte);
+out_delete_staging_file:
+ unlink(staging_file_name);
+ FREE(staging_file_name);
+ return ret;
+}
+
+/*
+ * Creates a randomly named staging directory and saves its name in the
+ * filesystem context structure.
+ */
+static int make_staging_dir(struct wimfs_context *ctx,
+ const char *user_prefix)
+{
+ static const size_t random_suffix_len = 10;
+ static const char *common_suffix = ".staging";
+ static const size_t common_suffix_len = 8;
+
+ char *staging_dir_name = NULL;
+ size_t staging_dir_name_len;
+ size_t prefix_len;
+ const char *wim_basename;
+ char *real_user_prefix = NULL;
+ int ret;
+
+ if (user_prefix) {
+ real_user_prefix = realpath(user_prefix, NULL);
+ if (!real_user_prefix) {
+ ERROR_WITH_ERRNO("Could not resolve `%s'",
+ real_user_prefix);
+ ret = WIMLIB_ERR_NOTDIR;
+ goto out;
+ }
+ wim_basename = path_basename(ctx->wim->filename);
+ prefix_len = strlen(real_user_prefix) + 1 + strlen(wim_basename);
+ } else {
+ prefix_len = strlen(ctx->wim->filename);
+ }
+
+ staging_dir_name_len = prefix_len + common_suffix_len + random_suffix_len;