+static void remove_dentry(struct dentry *dentry,
+ struct lookup_table *lookup_table)
+{
+ const u8 *hash = dentry->hash;
+ u16 i = 0;
+ struct lookup_table_entry *lte;
+ while (1) {
+ lte = lookup_table_decrement_refcnt(lookup_table, hash);
+ if (lte && lte->num_opened_fds)
+ for (u16 i = 0; i < lte->num_allocated_fds; i++)
+ if (lte->fds[i] && lte->fds[i]->dentry == dentry)
+ lte->fds[i]->dentry = NULL;
+ if (i == dentry->num_ads)
+ break;
+ hash = dentry->ads_entries[i].hash;
+ i++;
+ }
+
+ unlink_dentry(dentry);
+ put_dentry(dentry);
+}
+
+static void dentry_increment_lookup_table_refcnts(struct dentry *dentry,
+ struct lookup_table *lookup_table)
+{
+ u16 i = 0;
+ const u8 *hash = dentry->hash;
+ struct lookup_table_entry *lte;
+ while (1) {
+ lte = __lookup_resource(lookup_table, hash);
+ if (lte)
+ lte->refcnt++;
+ if (i == dentry->num_ads)
+ break;
+ hash = dentry->ads_entries[i].hash;
+ i++;
+ }
+}
+
+/* Change the hash value of the main or alternate file stream in a hard link
+ * group. This needs to be done if the hash of the corresponding lookup table
+ * entry was changed. */
+static void link_group_set_stream_hash(struct dentry *dentry,
+ unsigned stream_idx,
+ const u8 new_hash[])
+{
+ struct list_head *head, *cur;
+
+ if (stream_idx == 0) {
+ head = &dentry->link_group_list;
+ cur = head;
+ do {
+ dentry = container_of(cur, struct dentry, link_group_list);
+ memcpy(dentry->hash, new_hash, WIM_HASH_SIZE);
+ cur = cur->next;
+ } while (cur != head);
+ } else {
+ /* Dentries in the link group share their alternate stream
+ * entries. */
+ wimlib_assert(stream_idx <= dentry->num_ads);
+ memcpy(dentry->ads_entries[stream_idx - 1].hash, new_hash,
+ WIM_HASH_SIZE);
+ }
+}
+
+/* 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 + WIM_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,
+ WIM_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.
+ *
+ * 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
+ */
+static int extract_resource_to_staging_dir(struct dentry *dentry,
+ 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;
+
+ 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_resource_to_fd(w, &old_lte->resource_entry, 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_remove(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. */
+ wimlib_assert(old_lte->refcnt > link_group_size);
+
+ new_lte = new_lookup_table_entry();
+ if (!new_lte) {
+ ret = -ENOMEM;
+ goto out_delete_staging_file;
+ }
+
+ u16 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->hard_link ==
+ dentry->hard_link)
+ {
+ 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);
+ ret = -ENOMEM;
+ goto out_delete_staging_file;
+ }
+ for (u16 i = 0, j = 0; ; i++) {
+ if (old_lte->fds[i] &&
+ old_lte->fds[i]->dentry->hard_link ==
+ dentry->hard_link)
+ {
+ 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;
+ }
+ }
+ old_lte->refcnt -= link_group_size;
+ old_lte->num_opened_fds -= num_transferred_fds;
+ new_lte->num_opened_fds = num_transferred_fds;
+ new_lte->num_allocated_fds = num_transferred_fds;
+ }
+ } 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;
+ }
+ }
+ new_lte->resource_entry.original_size = size;
+ new_lte->refcnt = link_group_size;
+ randomize_byte_array(new_lte->hash, WIM_HASH_SIZE);
+ new_lte->staging_file_name = staging_file_name;
+
+ 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;
+}
+