+/* Next hard link group ID to be assigned. These are also used as the inode
+ * numbers. */
+static u64 next_link_group_id;
+
+/* List of lookup table entries in the staging directory */
+static LIST_HEAD(staging_list);
+
+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);
+}
+
+/* Transfers file attributes from a struct dentry to a `stat' buffer. */
+int dentry_to_stbuf(const struct dentry *dentry, struct stat *stbuf)
+{
+ struct lookup_table_entry *lte;
+
+ if (dentry_is_symlink(dentry))
+ stbuf->st_mode = S_IFLNK | 0777;
+ else if (dentry_is_directory(dentry))
+ stbuf->st_mode = S_IFDIR | 0755;
+ else
+ stbuf->st_mode = S_IFREG | 0644;
+
+ stbuf->st_ino = (ino_t)dentry->hard_link;
+
+ stbuf->st_nlink = dentry_link_group_size(dentry);
+ stbuf->st_uid = getuid();
+ stbuf->st_gid = getgid();
+
+ /* Use the size of the unnamed (default) file stream. */
+ if ((lte = dentry_lte(dentry))) {
+ if (lte->staging_file_name) {
+ struct stat native_stat;
+ if (stat(lte->staging_file_name, &native_stat) != 0)
+ return -errno;
+ stbuf->st_size = native_stat.st_size;
+ } else {
+ stbuf->st_size = lte->resource_entry.original_size;
+ }
+ } else {
+ stbuf->st_size = 0;
+ }
+
+ stbuf->st_atime = ms_timestamp_to_unix(dentry->last_access_time);
+ stbuf->st_mtime = ms_timestamp_to_unix(dentry->last_write_time);
+ stbuf->st_ctime = ms_timestamp_to_unix(dentry->creation_time);
+ stbuf->st_blocks = (stbuf->st_size + 511) / 512;
+ return 0;
+}
+
+/* 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;
+}
+
+/* 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->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->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;
+ }
+ }
+ 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)
+{
+ 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;
+ do {
+ struct dentry *d;
+ d = container_of(pos, struct dentry, link_group_list);
+ lte_transfer_dentry(new_lte, d);
+
+ pos = pos->next;
+ } while (pos != &dentry->link_group_list);
+ } else {
+ struct ads_entry *ads_entry;
+ ads_entry = &dentry->ads_entries[stream_idx - 1];
+ lte_transfer_ads_entry(new_lte, ads_entry);
+ }
+}
+
+/*
+ * 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
+ * - Transfer stream entries from the old lte's list to the new lte's list.
+ *
+ * *lte is permitted to be NULL, in which case there is no old lookup table
+ * entry.
+ */
+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;
+
+ 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_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. */
+ 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;
+ 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;
+}