]> wimlib.net Git - wimlib/blobdiff - src/mount.c
update_lte_of_staging_file() fix
[wimlib] / src / mount.c
index d5f43718d85833311514481feaa08897893aeab4..0da8eeeacd5c589b611c45aa36d3c81421b02b55 100644 (file)
@@ -54,7 +54,7 @@
 /* File descriptor to a file open on the WIM filesystem. */
 struct wimlib_fd {
        struct inode *f_inode;
-       struct lookup_table_entry *lte;
+       struct lookup_table_entry *f_lte;
        int staging_fd;
        u16 idx;
        u32 stream_id;
@@ -147,7 +147,7 @@ static int alloc_wimlib_fd(struct inode *inode,
                        if (!fd)
                                return -ENOMEM;
                        fd->f_inode    = inode;
-                       fd->lte        = lte;
+                       fd->f_lte      = lte;
                        fd->staging_fd = -1;
                        fd->idx        = i;
                        fd->stream_id  = stream_id;
@@ -180,7 +180,7 @@ static void inode_put_fd(struct inode *inode, struct wimlib_fd *fd)
 static int lte_put_fd(struct lookup_table_entry *lte, struct wimlib_fd *fd)
 {
        wimlib_assert(fd);
-       wimlib_assert(fd->lte == lte);
+       wimlib_assert(fd->f_lte == lte);
 
        if (!lte) /* Empty stream with no lookup table entry */
                return 0;
@@ -188,9 +188,9 @@ static int lte_put_fd(struct lookup_table_entry *lte, struct wimlib_fd *fd)
        /* Close staging file descriptor if needed. */
        wimlib_assert(lte->num_opened_fds);
 
-       if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
-               wimlib_assert(lte->staging_file_name);
-               wimlib_assert(fd->staging_fd != -1);
+       if (lte->resource_location == RESOURCE_IN_STAGING_FILE
+            && fd->staging_fd != -1)
+       {
                if (close(fd->staging_fd) != 0) {
                        ERROR_WITH_ERRNO("Failed to close staging file");
                        return -errno;
@@ -208,7 +208,7 @@ static int close_wimlib_fd(struct wimlib_fd *fd)
        DEBUG("Closing fd (inode = %lu, opened = %u, allocated = %u)",
              fd->f_inode->ino, fd->f_inode->num_opened_fds,
              fd->f_inode->num_allocated_fds);
-       ret = lte_put_fd(fd->lte, fd);
+       ret = lte_put_fd(fd->f_lte, fd);
        if (ret != 0)
                return ret;
 
@@ -385,8 +385,9 @@ static int extract_resource_to_staging_dir(struct inode *inode,
                        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;
+                                       wimlib_assert(fd->f_lte == old_lte);
+                                       wimlib_assert(fd->staging_fd == -1);
+                                       fd->f_lte = new_lte;
                                        new_lte->num_opened_fds++;
                                        fd->staging_fd = open(staging_file_name, O_RDONLY);
                                        if (fd->staging_fd == -1) {
@@ -407,9 +408,9 @@ static int extract_resource_to_staging_dir(struct inode *inode,
 
        new_lte->resource_entry.original_size = size;
        new_lte->refcnt                       = inode->link_count;
-       new_lte->staging_file_name            = staging_file_name;
        new_lte->resource_location            = RESOURCE_IN_STAGING_FILE;
-       new_lte->lte_inode                        = inode;
+       new_lte->staging_file_name            = staging_file_name;
+       new_lte->lte_inode                    = inode;
        random_hash(new_lte->hash);
 
        if (stream_id == 0)
@@ -426,8 +427,8 @@ static int extract_resource_to_staging_dir(struct inode *inode,
 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 && fd->stream_id == stream_id && fd->f_lte == new_lte) {
+                       fd->f_lte = old_lte;
                        if (fd->staging_fd != -1) {
                                close(fd->staging_fd);
                                fd->staging_fd = -1;
@@ -673,6 +674,22 @@ static int wimfs_access(const char *path, int mask)
        return 0;
 }
 
+static void inode_update_lte_ptr(struct inode *inode,
+                                struct lookup_table_entry *old_lte,
+                                struct lookup_table_entry *new_lte)
+{
+       if (inode->lte == old_lte) {
+               inode->lte = new_lte;
+       } else {
+               for (unsigned i = 0; i < inode->num_ads; i++) {
+                       if (inode->ads_entries[i].lte == old_lte) {
+                               inode->ads_entries[i].lte = new_lte;
+                               break;
+                       }
+               }
+       }
+}
+
 static int update_lte_of_staging_file(struct lookup_table_entry *lte,
                                      struct lookup_table *table)
 {
@@ -696,27 +713,34 @@ static int update_lte_of_staging_file(struct lookup_table_entry *lte,
                /* Merge duplicate lookup table entries */
                duplicate_lte->refcnt += lte->refcnt;
                list_del(&lte->staging_list);
+               inode_update_lte_ptr(lte->lte_inode, lte, duplicate_lte);
                free_lookup_table_entry(lte);
        } else {
                if (stat(lte->staging_file_name, &stbuf) != 0) {
                        ERROR_WITH_ERRNO("Failed to stat `%s'", lte->staging_file_name);
                        return WIMLIB_ERR_STAT;
                }
-               wimlib_assert(&lte->file_on_disk == &lte->staging_file_name);
-               lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
-               copy_hash(lte->hash, hash);
-               lte->resource_entry.original_size = stbuf.st_size;
-               lte->resource_entry.size = stbuf.st_size;
-               lte->lte_inode = NULL;
-               lookup_table_insert(table, lte);
+               if (stbuf.st_size == 0) {
+                       /* Zero-length stream.  No lookup table entry needed. */
+                       inode_update_lte_ptr(lte->lte_inode, lte, NULL);
+                       free_lookup_table_entry(lte);
+               } else {
+                       wimlib_assert(&lte->file_on_disk == &lte->staging_file_name);
+                       lte->resource_entry.original_size = stbuf.st_size;
+                       lte->resource_entry.size = stbuf.st_size;
+                       lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
+                       lte->lte_inode = NULL;
+                       copy_hash(lte->hash, hash);
+                       lookup_table_insert(table, lte);
+               }
        }
-
        return 0;
 }
 
 static int inode_close_fds(struct inode *inode)
 {
-       for (u16 i = 0, j = 0; j < inode->num_opened_fds; i++) {
+       u16 num_opened_fds = inode->num_opened_fds;
+       for (u16 i = 0, j = 0; j < num_opened_fds; i++) {
                struct wimlib_fd *fd = inode->fds[i];
                if (fd) {
                        wimlib_assert(fd->f_inode == inode);
@@ -729,11 +753,6 @@ static int inode_close_fds(struct inode *inode)
        return 0;
 }
 
-/*static int dentry_close_fds(struct dentry *dentry, void *ignore)*/
-/*{*/
-       /*return inode_close_fds(dentry->d_inode);*/
-/*}*/
-
 /* Overwrites the WIM file, with changes saved. */
 static int rebuild_wim(WIMStruct *w, bool check_integrity)
 {
@@ -747,9 +766,6 @@ static int rebuild_wim(WIMStruct *w, bool check_integrity)
                if (ret != 0)
                        return ret;
        }
-       /*ret = for_dentry_in_tree(wim_root_dentry(w), dentry_close_fds, NULL);*/
-       /*if (ret != 0)*/
-               /*return ret;*/
 
        DEBUG("Calculating SHA1 checksums for all new staging files.");
        list_for_each_entry_safe(lte, tmp, &staging_list, staging_list) {
@@ -861,7 +877,7 @@ static int wimfs_fgetattr(const char *path, struct stat *stbuf,
                          struct fuse_file_info *fi)
 {
        struct wimlib_fd *fd = (struct wimlib_fd*)(uintptr_t)fi->fh;
-       return inode_to_stbuf(fd->f_inode, fd->lte, stbuf);
+       return inode_to_stbuf(fd->f_inode, fd->f_lte, stbuf);
 }
 
 static int wimfs_ftruncate(const char *path, off_t size,
@@ -871,8 +887,8 @@ static int wimfs_ftruncate(const char *path, off_t size,
        int ret = ftruncate(fd->staging_fd, size);
        if (ret != 0)
                return ret;
-       if (fd->lte && size < fd->lte->resource_entry.original_size)
-               fd->lte->resource_entry.original_size = size;
+       if (fd->f_lte && size < fd->f_lte->resource_entry.original_size)
+               fd->f_lte->resource_entry.original_size = size;
        return 0;
 }
 
@@ -1042,7 +1058,6 @@ static int wimfs_mkdir(const char *path, mode_t mode)
        return 0;
 }
 
-
 /* Creates a regular file. */
 static int wimfs_mknod(const char *path, mode_t mode, dev_t rdev)
 {
@@ -1177,13 +1192,13 @@ static int wimfs_read(const char *path, char *buf, size_t size,
        if (!fd)
                return -EBADF;
 
-       if (!fd->lte) /* Empty stream with no lookup table entry */
+       if (!fd->f_lte) /* Empty stream with no lookup table entry */
                return 0;
 
-       if (fd->lte->resource_location == RESOURCE_IN_STAGING_FILE) {
+       if (fd->f_lte->resource_location == RESOURCE_IN_STAGING_FILE) {
                /* Read from staging file */
 
-               wimlib_assert(fd->lte->staging_file_name);
+               wimlib_assert(fd->f_lte->staging_file_name);
                wimlib_assert(fd->staging_fd != -1);
 
                ssize_t ret;
@@ -1198,14 +1213,14 @@ static int wimfs_read(const char *path, char *buf, size_t size,
        } else {
                /* Read from WIM */
 
-               u64 res_size = wim_resource_size(fd->lte);
+               u64 res_size = wim_resource_size(fd->f_lte);
                
                if (offset > res_size)
                        return -EOVERFLOW;
 
                size = min(size, res_size - offset);
 
-               if (read_wim_resource(fd->lte, (u8*)buf,
+               if (read_wim_resource(fd->f_lte, (u8*)buf,
                                      size, offset, false) != 0)
                        return -EIO;
                return size;
@@ -1610,8 +1625,8 @@ static int wimfs_write(const char *path, const char *buf, size_t size,
        if (!fd)
                return -EBADF;
 
-       wimlib_assert(fd->lte);
-       wimlib_assert(fd->lte->staging_file_name);
+       wimlib_assert(fd->f_lte);
+       wimlib_assert(fd->f_lte->staging_file_name);
        wimlib_assert(fd->staging_fd != -1);
        wimlib_assert(fd->f_inode);