1.1 --- a/libe2access/lib/src/image.c Tue Jan 04 19:22:01 2022 +0100
1.2 +++ b/libe2access/lib/src/image.c Tue Jan 04 23:27:53 2022 +0100
1.3 @@ -195,7 +195,6 @@
1.4 void *),
1.5 void *data)
1.6 {
1.7 - const char *path_orig = path;
1.8 char *buf;
1.9 ext2_ino_t ino;
1.10 errcode_t retval;
1.11 @@ -344,6 +343,70 @@
1.12 return 0;
1.13 }
1.14
1.15 +/* Remove an inode. */
1.16 +
1.17 +errcode_t image_remove_by_inode(ext2_filsys fs, ext2_ino_t ino)
1.18 +{
1.19 + struct ext2_inode_large inode;
1.20 + errcode_t err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *) &inode,
1.21 + sizeof(inode));
1.22 +
1.23 + /* Handle invalid inodes, ignore unreferenced inodes. */
1.24 +
1.25 + if (err)
1.26 + return err;
1.27 +
1.28 + if (!inode.i_links_count)
1.29 + return 0;
1.30 +
1.31 + /* Decrement the reference count. With no more references to the inode,
1.32 + remove its resources. */
1.33 +
1.34 + inode.i_links_count--;
1.35 +
1.36 + if (!inode.i_links_count)
1.37 + {
1.38 + err = ext2fs_free_ext_attr(fs, ino, &inode);
1.39 +
1.40 + if (!err)
1.41 + {
1.42 + /* Deallocate blocks, if appropriate. ~0ULL as the end represents
1.43 + truncation. */
1.44 +
1.45 + if (ext2fs_inode_has_valid_blocks2(fs, (struct ext2_inode *) &inode))
1.46 + {
1.47 + err = ext2fs_punch(fs, ino, (struct ext2_inode *) &inode, NULL,
1.48 + 0, ~0ULL);
1.49 +
1.50 + /* Update allocation statistics. */
1.51 +
1.52 + if (!err)
1.53 + ext2fs_inode_alloc_stats2(fs, ino, -1,
1.54 + LINUX_S_ISDIR(inode.i_mode));
1.55 + }
1.56 + }
1.57 + }
1.58 +
1.59 + return ext2fs_write_inode_full(fs, ino, (struct ext2_inode *) &inode,
1.60 + sizeof(inode));
1.61 +}
1.62 +
1.63 +/* Unlink a directory entry by name. */
1.64 +
1.65 +errcode_t image_unlink_by_name(ext2_filsys fs, ext2_ino_t ino_parent,
1.66 + const char *basename)
1.67 +{
1.68 + return ext2fs_unlink(fs, ino_parent, basename, 0, 0);
1.69 +}
1.70 +
1.71 +/* Unlink a directory entry by inode number. */
1.72 +
1.73 +errcode_t image_unlink_by_inode(ext2_filsys fs, ext2_ino_t ino_parent,
1.74 + ext2_ino_t ino)
1.75 +{
1.76 + return ext2fs_unlink(fs, ino_parent, 0, ino, 0);
1.77 +}
1.78 +
1.79 /* Test object types in the filesystem image. */
1.80
1.81 int _image_isdir(ext2_filsys fs, ext2_ino_t ino)