+/* 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;
+}
+
+/*
+ * Removes open file descriptors from a lookup table entry @old_lte where the
+ * file descriptors have opened the corresponding file resource in the context
+ * of the hard link group @link_group; these file descriptors are extracted and
+ * placed in a new lookup table entry, which is returned.
+ */
+static struct lookup_table_entry *
+lte_extract_fds(struct lookup_table_entry *old_lte, u64 link_group)
+{
+ int ret;
+ u16 num_transferred_fds;
+ struct lookup_table_entry *new_lte;
+
+ new_lte = new_lookup_table_entry();
+ if (!new_lte)
+ return NULL;
+
+ num_transferred_fds = 0;
+ for (u16 i = 0; i < old_lte->num_allocated_fds; i++)
+ if (old_lte->fds[i] && old_lte->fds[i]->dentry &&
+ old_lte->fds[i]->dentry->hard_link == link_group)
+ num_transferred_fds++;
+ DEBUG("Transferring %u file descriptors",
+ num_transferred_fds);
+ new_lte->fds = MALLOC(num_transferred_fds * sizeof(new_lte->fds[0]));
+ if (!new_lte->fds) {
+ FREE(new_lte);
+ return NULL;
+ }
+ for (u16 i = 0, j = 0; ; i++) {
+ if (old_lte->fds[i] && old_lte->fds[i]->dentry &&
+ old_lte->fds[i]->dentry->hard_link == link_group) {
+ struct wimlib_fd *fd = old_lte->fds[i];
+ old_lte->fds[i] = NULL;
+ fd->lte = new_lte;
+ fd->idx = j;
+ new_lte->fds[j] = fd;
+ if (++j == num_transferred_fds)
+ break;
+ }
+ }
+ DEBUG("old_lte: %u fds open; new_lte: %u fds open",
+ old_lte->num_opened_fds, new_lte->num_opened_fds);
+ old_lte->num_opened_fds -= num_transferred_fds;
+ new_lte->num_opened_fds = num_transferred_fds;
+ new_lte->num_allocated_fds = num_transferred_fds;
+ return new_lte;
+}
+
+/*
+ * Transfers an alternate data stream entry to a new lookup table entry
+ */
+static void lte_transfer_ads_entry(struct lookup_table_entry *new_lte,
+ struct ads_entry *ads_entry)
+{
+ list_del(&ads_entry->lte_group_list.list);
+ list_add(&ads_entry->lte_group_list.list, &new_lte->lte_group_list);
+ ads_entry->lte = new_lte;
+}
+
+/*
+ * Transfers a dentry to a new lookup table entry
+ */
+static void lte_transfer_dentry(struct lookup_table_entry *new_lte,
+ struct dentry *dentry)
+{
+ wimlib_assert(dentry->lte_group_list.list.next);
+ wimlib_assert(new_lte->lte_group_list.next);
+ list_del(&dentry->lte_group_list.list);
+ list_add(&dentry->lte_group_list.list, &new_lte->lte_group_list);
+ dentry->lte = new_lte;
+}
+
+static void lte_transfer_stream_entries(struct lookup_table_entry *new_lte,
+ struct dentry *dentry,
+ unsigned stream_idx)
+{
+ INIT_LIST_HEAD(&new_lte->lte_group_list);
+ if (stream_idx == 0) {
+ struct list_head *pos = &dentry->link_group_list;
+ do {
+ struct dentry *d;
+ d = container_of(pos, struct dentry, link_group_list);
+ wimlib_assert(d->hard_link == dentry->hard_link);
+ lte_transfer_dentry(new_lte, d);
+ pos = pos->next;
+ } while (pos != &dentry->link_group_list);
+ } else {
+ struct ads_entry *ads_entry;
+ wimlib_assert(stream_idx <= dentry->num_ads);
+ ads_entry = &dentry->ads_entries[stream_idx - 1];
+ lte_transfer_ads_entry(new_lte, ads_entry);
+ }
+}
+
+/*
+ * Extract a WIM resource to the staging directory.
+ *
+ * @dentry, @stream_idx: The stream on whose behalf we are modifying the lookup
+ * table entry (these may be more streams than this that reference the lookup
+ * table entry)
+ *
+ * @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 dentry *dentry,
+ unsigned stream_idx,
+ struct lookup_table_entry **lte,
+ off_t size)
+{
+ char *staging_file_name;
+ int ret;
+ int fd;
+ struct lookup_table_entry *old_lte, *new_lte;
+ size_t link_group_size;
+
+ /*
+ * We need to:
+ * - Create a staging file for the WIM resource
+ * - Extract the resource to it
+ * - Create a new lte for the file resource
+ * - Transfer fds from the old lte to the new lte, but only if they share the
+ * same hard link group as this dentry. If there is no old lte, then this
+ * step does not need to be done
+ * - Transfer stream entries from the old lte's list to the new lte's list. If
+ * there is no old lte, we instead transfer entries for the hard link group.
+ *
+ * Note: *lte is permitted to be NULL, in which case there is no old
+ * lookup table entry.
+ */
+
+ DEBUG("Extracting resource `%s' to staging directory", dentry->full_path_utf8);
+
+ 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;
+ }
+
+ link_group_size = dentry_link_group_size(dentry);
+
+ if (old_lte) {
+ if (link_group_size == old_lte->refcnt) {
+ /* This hard link group is the only user of the lookup
+ * table entry, so we can re-use it. */
+ DEBUG("Re-using lookup table entry");
+ lookup_table_unlink(w->lookup_table, old_lte);
+ new_lte = old_lte;
+ } else {
+ DEBUG("Splitting lookup table entry "
+ "(link_group_size = %u, lte refcnt = %u)",
+ link_group_size, old_lte->refcnt);
+ /* Split a hard link group away from the "lookup table
+ * entry" hard link group (i.e. we had two hard link
+ * groups that were identical, but now we are changing
+ * one of them) */
+
+ /* XXX
+ * The ADS really complicate things here and not
+ * everything is going to work correctly yet. For
+ * example it could be the same that a file contains two
+ * file streams that are identical and therefore share
+ * the same lookup table entry despite the fact that the
+ * streams themselves are not hardlinked.
+ * XXX*/
+ wimlib_assert(old_lte->refcnt > link_group_size);
+
+ new_lte = lte_extract_fds(old_lte, dentry->hard_link);
+ if (!new_lte) {
+ ret = -ENOMEM;
+ goto out_delete_staging_file;
+ }
+
+ lte_transfer_stream_entries(new_lte, dentry, stream_idx);
+ old_lte->refcnt -= link_group_size;
+ }
+ } else {
+ /* No old_lte was supplied, so the resource had no lookup table
+ * entry before (it must be an empty resource) */
+ new_lte = new_lookup_table_entry();
+ if (!new_lte) {
+ ret = -ENOMEM;
+ goto out_delete_staging_file;
+ }
+ lte_transfer_stream_entries(new_lte, dentry, stream_idx);
+ }
+ new_lte->resource_entry.original_size = size;
+ new_lte->refcnt = link_group_size;
+ random_hash(new_lte->hash);
+ if (new_lte->staging_file_name)
+ FREE(new_lte->staging_file_name);
+ new_lte->staging_file_name = staging_file_name;
+ new_lte->resource_location = RESOURCE_IN_STAGING_FILE;
+
+ lookup_table_insert(w->lookup_table, new_lte);
+ list_add(&new_lte->staging_list, &staging_list);
+ *lte = new_lte;
+ return 0;
+out_delete_staging_file:
+ unlink(staging_file_name);
+ FREE(staging_file_name);
+ return ret;
+}
+