1.1 --- a/libe2access/include/e2access/image.h Wed Feb 16 18:38:11 2022 +0100
1.2 +++ b/libe2access/include/e2access/image.h Wed Feb 16 19:28:42 2022 +0100
1.3 @@ -61,7 +61,8 @@
1.4 errcode_t image_inode(ext2_filsys fs, const char *pathname,
1.5 struct ext2_inode *inode);
1.6
1.7 -errcode_t image_inode_decrement(ext2_filsys fs, ext2_ino_t ino);
1.8 +errcode_t image_inode_refcount_update(ext2_filsys fs, ext2_ino_t ino,
1.9 + int change);
1.10
1.11 errcode_t image_list_dir(ext2_filsys fs, const char *path,
1.12 int (*proc)(struct ext2_dir_entry *, int, int, char *,
2.1 --- a/libe2access/lib/src/image.c Wed Feb 16 18:38:11 2022 +0100
2.2 +++ b/libe2access/lib/src/image.c Wed Feb 16 19:28:42 2022 +0100
2.3 @@ -324,30 +324,26 @@
2.4 return ext2fs_read_inode(fs, ino, inode);
2.5 }
2.6
2.7 -/* Decrement the reference count on an inode. */
2.8 +/* Update the reference count for the given inode. */
2.9
2.10 -errcode_t image_inode_decrement(ext2_filsys fs, ext2_ino_t ino)
2.11 +errcode_t image_inode_refcount_update(ext2_filsys fs, ext2_ino_t ino, int change)
2.12 {
2.13 - struct ext2_inode_large inode;
2.14 - errcode_t retval;
2.15 -
2.16 - retval = ext2fs_read_inode_full(fs, ino,
2.17 - (struct ext2_inode *) &inode,
2.18 - sizeof(inode));
2.19 + struct ext2_inode inode;
2.20 + errcode_t retval = ext2fs_read_inode(fs, ino, &inode);
2.21
2.22 if (retval)
2.23 return retval;
2.24
2.25 /* NOTE: FUSE implementation tests for > 1. */
2.26
2.27 - if (inode.i_links_count)
2.28 - inode.i_links_count--;
2.29 + if ((change < 0) && (inode.i_links_count + change < 0))
2.30 + inode.i_links_count = 0;
2.31 + else
2.32 + inode.i_links_count += change;
2.33
2.34 /* NOTE: Update modification time. */
2.35 -
2.36 - return ext2fs_write_inode_full(fs, ino,
2.37 - (struct ext2_inode *) &inode,
2.38 - sizeof(inode));
2.39 +
2.40 + return ext2fs_write_inode(fs, ino, &inode);
2.41 }
2.42
2.43 /* List a directory in the filesystem image. */
2.44 @@ -541,7 +537,7 @@
2.45 /* Decrement the parent reference count for directories. */
2.46
2.47 if (!retval && !inode.i_links_count && ino_parent)
2.48 - retval = image_inode_decrement(fs, ino_parent);
2.49 + retval = image_inode_refcount_update(fs, ino_parent, -1);
2.50
2.51 return retval;
2.52 }
2.53 @@ -561,7 +557,7 @@
2.54 ext2_ino_t target_parent, const char *target_basename)
2.55 {
2.56 errcode_t retval;
2.57 - struct ext2_inode source_inode, source_parent_inode, target_parent_inode;
2.58 + struct ext2_inode source_inode;
2.59
2.60 /* NOTE: Should check for space. */
2.61
2.62 @@ -584,14 +580,7 @@
2.63 {
2.64 /* Update the link count for the target. */
2.65
2.66 - retval = ext2fs_read_inode(fs, target_parent, &target_parent_inode);
2.67 -
2.68 - if (retval)
2.69 - return retval;
2.70 -
2.71 - target_parent_inode.i_links_count++;
2.72 -
2.73 - retval = ext2fs_write_inode(fs, target_parent, &target_parent_inode);
2.74 + retval = image_inode_refcount_update(fs, target_parent, 1);
2.75
2.76 if (retval)
2.77 return retval;
2.78 @@ -604,14 +593,7 @@
2.79
2.80 /* Update the link count for the source. */
2.81
2.82 - retval = ext2fs_read_inode(fs, source_parent, &source_parent_inode);
2.83 -
2.84 - if (retval)
2.85 - return retval;
2.86 -
2.87 - source_parent_inode.i_links_count--;
2.88 -
2.89 - retval = ext2fs_write_inode(fs, source_parent, &source_parent_inode);
2.90 + retval = image_inode_refcount_update(fs, source_parent, -1);
2.91
2.92 if (retval)
2.93 return retval;