+ 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)
+{
+ /* XXX Give the user an option of where to stage files */
+
+ static const char prefix[] = "wimlib-staging-";
+ static const size_t prefix_len = sizeof(prefix) - 1;
+ static const size_t random_suffix_len = 10;
+
+ size_t pwd_len = strlen(ctx->working_directory);
+
+ ctx->staging_dir_name_len = pwd_len + 1 + prefix_len + random_suffix_len;
+
+ ctx->staging_dir_name = MALLOC(ctx->staging_dir_name_len + 1);
+ if (!ctx->staging_dir_name)
+ return WIMLIB_ERR_NOMEM;
+
+ memcpy(ctx->staging_dir_name, ctx->working_directory, pwd_len);
+ ctx->staging_dir_name[pwd_len] = '/';
+ memcpy(ctx->staging_dir_name + pwd_len + 1, prefix, prefix_len);
+ randomize_char_array_with_alnum(ctx->staging_dir_name + pwd_len +
+ 1 + prefix_len, random_suffix_len);
+ ctx->staging_dir_name[ctx->staging_dir_name_len] = '\0';
+
+ if (mkdir(ctx->staging_dir_name, 0700) != 0) {
+ ERROR_WITH_ERRNO("Failed to create temporary directory `%s'",
+ ctx->staging_dir_name);
+ FREE(ctx->staging_dir_name);
+ ctx->staging_dir_name = NULL;
+ return WIMLIB_ERR_MKDIR;
+ }
+ return 0;