static const u16 fds_per_alloc = 8;
static const u16 max_fds = 0xffff;
+ DEBUG("Allocating fd for stream ID %u from inode %lx (open = %u, allocated = %u)",
+ stream_id, inode->ino, inode->num_opened_fds,
+ inode->num_allocated_fds);
+
if (inode->num_opened_fds == inode->num_allocated_fds) {
struct wimlib_fd **fds;
u16 num_new_fds;
*fd_ret = fd;
inode->fds[i] = fd;
inode->num_opened_fds++;
- lte->num_opened_fds++;
+ if (lte)
+ lte->num_opened_fds++;
+ DEBUG("Allocated fd");
return 0;
}
}
static int close_wimlib_fd(struct wimlib_fd *fd)
{
int ret;
+ DEBUG("Closing fd (inode = %lu, opened = %u, allocated = %u)",
+ fd->inode->ino, fd->inode->num_opened_fds,
+ fd->inode->num_allocated_fds);
ret = lte_put_fd(fd->lte, fd);
if (ret != 0)
return ret;
struct lookup_table_entry *lte;
unsigned i;
- for (i = 0; <= inode->num_ads; i++) {
+ for (i = 0; i <= inode->num_ads; i++) {
lte = inode_stream_lte_resolved(inode, i);
if (lte)
lte_decrement_refcnt(lte, lookup_table);
*
* @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
*
goto out_delete_staging_file;
}
- if (old_lte) {
- wimlib_assert(old_lte->resource_location == RESOURCE_IN_WIM);
- if (inode->link_count == old_lte->refcnt) {
- /* The reference count of the existing lookup table
- * entry is the same as the size of the hard link group
- * associated with the dentry; 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(w->lookup_table, old_lte);
- new_lte = old_lte;
- } else {
+ 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(w->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 = %zu, lte refcnt = %u)",
+ "(inode->link_count = %zu, old_lte->refcnt = %u)",
inode->link_count, old_lte->refcnt);
- wimlib_assert(old_lte->refcnt > inode->link_count);
+ }
- new_lte = new_lookup_table_entry();
- if (!new_lte) {
- ret = -ENOMEM;
- goto out_delete_staging_file;
- }
- old_lte->refcnt -= inode->link_count;
+ new_lte = new_lookup_table_entry();
+ if (!new_lte) {
+ ret = -ENOMEM;
+ goto out_delete_staging_file;
+ }
- for (u16 i = 0, j = 0; j < inode->num_opened_fds; i++) {
- if (inode->fds[i]) {
- if (inode->fds[i]->stream_id == stream_id) {
- wimlib_assert(inode->fds[i]->lte == old_lte);
- inode->fds[i]->lte = new_lte;
+ /* 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->lte == old_lte);
+ fd->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++;
}
+ j++;
}
}
- } else {
- /* No old_lte was supplied, so the resource had no lookup table
- * entry before (it must be an empty resource) */
- wimlib_assert(inode->num_opened_fds == 0);
- new_lte = new_lookup_table_entry();
- if (!new_lte) {
- ret = -ENOMEM;
- goto out_delete_staging_file;
+ DEBUG("%zu 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;
random_hash(new_lte->hash);
new_lte->staging_file_name = staging_file_name;
new_lte->resource_location = RESOURCE_IN_STAGING_FILE;
+ 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(w->lookup_table, new_lte);
list_add(&new_lte->staging_list, &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->lte == new_lte) {
+ fd->lte = old_lte;
+ if (fd->staging_fd != -1) {
+ close(fd->staging_fd);
+ fd->staging_fd = -1;
+ }
+ j++;
+ }
+ }
+out_free_new_lte:
+ free_lookup_table_entry(new_lte);
out_delete_staging_file:
unlink(staging_file_name);
FREE(staging_file_name);
* opening it read-only, but we need to extract the resource to the
* staging directory if we are opening it writable. */
if (flags_writable(fi->flags) &&
- lte->resource_location != RESOURCE_IN_STAGING_FILE) {
+ (!lte || lte->resource_location != RESOURCE_IN_STAGING_FILE)) {
+ u64 size = (lte) ? wim_resource_size(lte) : 0;
ret = extract_resource_to_staging_dir(inode, stream_id,
- <e,
- wim_resource_size(lte));
+ <e, size);
if (ret != 0)
return ret;
}
if (ret != 0)
return ret;
- if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
+ if (lte && lte->resource_location == RESOURCE_IN_STAGING_FILE) {
fd->staging_fd = open(lte->staging_file_name, fi->flags);
if (fd->staging_fd == -1) {
close_wimlib_fd(fd);
/* Opens a directory. */
static int wimfs_opendir(const char *path, struct fuse_file_info *fi)
{
- struct dentry *dentry;
struct inode *inode;
int ret;
struct wimlib_fd *fd = NULL;
- dentry = get_dentry(w, path);
- if (!dentry)
+ inode = wim_pathname_to_inode(w, path);
+ if (!inode)
return -ENOENT;
-
- inode = dentry->inode;
-
if (!inode_is_directory(inode))
return -ENOTDIR;
ret = alloc_wimlib_fd(inode, 0, NULL, &fd);
static int wimfs_setxattr(const char *path, const char *name,
const char *value, size_t size, int flags)
{
- struct dentry *dentry;
struct ads_entry *existing_ads_entry;
struct ads_entry *new_ads_entry;
struct lookup_table_entry *existing_lte;
return -ENOATTR;
name += 5;
- dentry = get_dentry(w, path);
- if (!dentry)
+ inode = wim_pathname_to_inode(w, path);
+ if (!inode)
return -ENOENT;
- inode = dentry->inode;
existing_ads_entry = inode_get_ads_entry(inode, name, &ads_idx);
if (existing_ads_entry) {
if (flags & XATTR_CREATE)
return -EEXIST;
- inode_remove_ads(dentry->inode, ads_idx, w->lookup_table);
+ inode_remove_ads(inode, ads_idx, w->lookup_table);
} else {
if (flags & XATTR_REPLACE)
return -ENOATTR;
}
- new_ads_entry = inode_add_ads(dentry, name);
+ new_ads_entry = inode_add_ads(inode, name);
if (!new_ads_entry)
return -ENOMEM;
{
struct dentry *dentry_parent, *dentry;
const char *link_name;
- struct lookup_table_entry *lte;
+ struct inode *inode;
dentry_parent = get_parent_dentry(w, from);
if (!dentry_parent)
if (get_dentry_child_with_name(dentry_parent, link_name))
return -EEXIST;
- dentry = new_dentry(link_name);
+ dentry = new_dentry_with_inode(link_name);
if (!dentry)
return -ENOMEM;
+ inode = dentry->inode;
- dentry->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
- dentry->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
- dentry->link_group_id = next_link_group_id++;
+ inode->attributes = FILE_ATTRIBUTE_REPARSE_POINT;
+ inode->reparse_tag = WIM_IO_REPARSE_TAG_SYMLINK;
+ inode->ino = next_ino++;
- if (dentry_set_symlink(dentry, to, w->lookup_table, <e) != 0)
+ if (inode_set_symlink(inode, to, w->lookup_table, NULL) != 0)
goto out_free_dentry;
- wimlib_assert(lte);
-
- dentry->ads_entries[1].lte_group_list.type = STREAM_TYPE_ADS;
- list_add(&dentry->ads_entries[1].lte_group_list.list,
- <e->lte_group_list);
- wimlib_assert(dentry->resolved);
-
link_dentry(dentry, dentry_parent);
return 0;
out_free_dentry:
struct dentry *dentry;
struct lookup_table_entry *lte;
int ret;
- unsigned stream_idx;
+ u16 stream_idx;
+ u32 stream_id;
+ struct inode *inode;
ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
<e, &stream_idx);
if (!lte) /* Already a zero-length file */
return 0;
+ inode = dentry->inode;
+
+ if (stream_idx == 0)
+ stream_id = 0;
+ else
+ stream_id = inode->ads_entries[stream_idx - 1]->stream_id;
+
if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
wimlib_assert(lte->staging_file_name);
ret = truncate(lte->staging_file_name, size);
wimlib_assert(lte->resource_location == RESOURCE_IN_WIM);
/* File in WIM. Extract it to the staging directory, but only
* the first @size bytes of it. */
- ret = extract_resource_to_staging_dir(dentry, stream_idx,
+ ret = extract_resource_to_staging_dir(inode, stream_id,
<e, size);
}
return ret;
struct lookup_table_entry *lte;
struct inode *inode;
int ret;
- unsigned stream_idx;
+ u16 stream_idx;
unsigned i;
ret = lookup_resource(w, path, get_lookup_flags(), &dentry,
};
-static int check_lte_refcnt(struct lookup_table_entry *lte, void *ignore)
-{
- size_t lte_group_size = 0;
- struct list_head *cur;
- list_for_each(cur, <e->lte_group_list)
- lte_group_size++;
- if (lte_group_size > lte->refcnt) {
-#ifdef ENABLE_ERROR_MESSAGES
- struct dentry *example_dentry;
- struct list_head *next;
- struct stream_list_head *head;
- WARNING("The following lookup table entry has a reference count "
- "of %u, but", lte->refcnt);
- WARNING("We found %zu references to it", lte_group_size);
- next = lte->lte_group_list.next;
- head = container_of(next, struct stream_list_head, list);
- if (head->type == STREAM_TYPE_NORMAL) {
- example_dentry = container_of(head, struct dentry,
- lte_group_list);
- WARNING("(One dentry referencing it is at `%s')",
- example_dentry->full_path_utf8);
- }
- print_lookup_table_entry(lte);
-#endif
- /* Guess what! install.wim for Windows 8 contains a stream with
- * 2 dentries referencing it, but the lookup table entry has
- * reference count of 1. So we will need to handle this case
- * and not just make it be an error... I'm just setting the
- * reference count to the number of references we found. */
-
- #if 1
- lte->refcnt = lte_group_size;
- WARNING("Fixing reference count");
- #else
- return WIMLIB_ERR_INVALID_DENTRY;
- #endif
- }
- return 0;
-}
-
/* Mounts a WIM file. */
WIMLIBAPI int wimlib_mount(WIMStruct *wim, int image, const char *dir,
int flags, WIMStruct **additional_swms,
int ret;
char *p;
struct lookup_table *joined_tab, *wim_tab_save;
+ struct image_metadata *imd;
DEBUG("Mount: wim = %p, image = %d, dir = %s, flags = %d, ",
wim, image, dir, flags);
if (ret != 0)
goto out;
+ imd = &wim->image_metadata[image - 1];
+
DEBUG("Selected image %d", image);
- next_link_group_id = assign_link_group_ids(wim->image_metadata[image - 1].lgt);
+ next_ino = assign_inode_numbers(&imd->inode_list);
+
+ DEBUG("(next_ino = %"PRIu64")", next_ino);
- DEBUG("Resolving lookup table entries");
/* Resolve all the lookup table entries of the dentry tree */
- for_dentry_in_tree(wim_root_dentry(wim), dentry_resolve_ltes,
+ DEBUG("Resolving lookup table entries");
+ for_dentry_in_tree(imd->root_dentry, dentry_resolve_ltes,
wim->lookup_table);
- DEBUG("Checking lookup table entry reference counts");
-
- ret = for_lookup_table_entry(wim->lookup_table, check_lte_refcnt, NULL);
- if (ret != 0)
- goto out;
-
if (flags & WIMLIB_MOUNT_FLAG_READWRITE)
- wim_get_current_image_metadata(wim)->modified = true;
+ imd->modified = true;
if (!(flags & (WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_NONE |
WIMLIB_MOUNT_FLAG_STREAM_INTERFACE_XATTR |