+/* Next hard link group ID to be assigned. These are also used as the inode
+ * numbers. */
+static u64 next_link_group_id;
+
+
+static inline int get_lookup_flags()
+{
+ if (mount_flags & WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_WINDOWS)
+ return LOOKUP_FLAG_ADS_OK;
+ else
+ return 0;
+}
+
+static inline int flags_writable(int open_flags)
+{
+ return open_flags & (O_RDWR | O_WRONLY);
+}
+
+static int alloc_wimlib_fd(struct lookup_table_entry *lte,
+ struct wimlib_fd **fd_ret)
+{
+ static const u16 fds_per_alloc = 8;
+ static const u16 max_fds = 0xffff;
+
+ if (lte->num_opened_fds == lte->num_allocated_fds) {
+ struct wimlib_fd **fds;
+ u16 num_new_fds;
+
+ if (lte->num_allocated_fds == max_fds)
+ return -EMFILE;
+ num_new_fds = min(fds_per_alloc, max_fds - lte->num_allocated_fds);
+
+ fds = CALLOC(lte->num_allocated_fds + num_new_fds,
+ sizeof(lte->fds[0]));
+ if (!fds)
+ return -ENOMEM;
+ memcpy(fds, lte->fds,
+ lte->num_allocated_fds * sizeof(lte->fds[0]));
+ FREE(lte->fds);
+ lte->fds = fds;
+ lte->num_allocated_fds += num_new_fds;
+ }
+ for (u16 i = 0; ; i++) {
+ if (!lte->fds[i]) {
+ struct wimlib_fd *fd = CALLOC(1, sizeof(*fd));
+ if (!fd)
+ return -ENOMEM;
+ fd->staging_fd = -1;
+ fd->idx = i;
+ fd->lte = lte;
+ lte->fds[i] = fd;
+ lte->num_opened_fds++;
+ *fd_ret = fd;
+ return 0;
+ }
+ }
+}
+
+static int close_wimlib_fd(struct wimlib_fd *fd)
+{
+ struct lookup_table_entry *lte = fd->lte;
+
+ wimlib_assert(lte);
+ wimlib_assert(lte->num_opened_fds);
+
+ if (lte->staging_file_name) {
+ wimlib_assert(fd->staging_fd != -1);
+ if (close(fd->staging_fd) != 0)
+ return -errno;
+ }
+ if (--lte->num_opened_fds == 0 && lte->refcnt == 0) {
+ if (lte->staging_file_name)
+ unlink(lte->staging_file_name);
+ free_lookup_table_entry(lte);
+ }
+ lte->fds[fd->idx] = NULL;
+ FREE(fd);
+ return 0;
+}
+
+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++;
+ }
+}
+
+/* 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 {
+ 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);
+ *lte = new_lte;
+ return 0;
+out_delete_staging_file:
+ unlink(staging_file_name);
+ FREE(staging_file_name);
+ return ret;
+}