1.1 --- a/libe2access/host/test_listing.c Wed Feb 16 18:21:01 2022 +0100
1.2 +++ b/libe2access/host/test_listing.c Wed Feb 16 18:38:11 2022 +0100
1.3 @@ -133,6 +133,7 @@
1.4
1.5 list_dir_data.fs = fs;
1.6 list_dir_data.filename = NULL;
1.7 + list_dir_data.link_count = 0;
1.8
1.9 image_list_dir(fs, "", image_list_dir_proc, &list_dir_data);
1.10 image_list_dir(fs, "", image_list_dir_proc, &list_dir_data);
2.1 --- a/libe2access/include/e2access/utils.h Wed Feb 16 18:21:01 2022 +0100
2.2 +++ b/libe2access/include/e2access/utils.h Wed Feb 16 18:38:11 2022 +0100
2.3 @@ -32,6 +32,7 @@
2.4 {
2.5 ext2_filsys fs; /* filesystem */
2.6 const char *filename; /* single filename to show */
2.7 + int link_count; /* flag to show link counts */
2.8 };
2.9
2.10 errcode_t utils_list_dir(ext2_filsys fs, const char *path);
3.1 --- a/libe2access/lib/src/image.c Wed Feb 16 18:21:01 2022 +0100
3.2 +++ b/libe2access/lib/src/image.c Wed Feb 16 18:38:11 2022 +0100
3.3 @@ -482,8 +482,9 @@
3.4 struct ext2_inode_large inode;
3.5 ext2_ino_t ino_parent = 0;
3.6 errcode_t retval;
3.7 + int isdir = _image_isdir(fs, ino);
3.8
3.9 - if (_image_isdir(fs, ino))
3.10 + if (isdir)
3.11 {
3.12 retval = image_dir_get_parent(fs, ino, &ino_parent);
3.13 if (retval)
3.14 @@ -502,10 +503,13 @@
3.15 return 0;
3.16
3.17 /* Decrement the reference count. With no more references to the inode,
3.18 - remove its resources. */
3.19 + remove its resources. Directories appear to need a double decrement. */
3.20
3.21 inode.i_links_count--;
3.22
3.23 + if (isdir && inode.i_links_count)
3.24 + inode.i_links_count--;
3.25 +
3.26 if (!inode.i_links_count)
3.27 {
3.28 /* NOTE: Update deletion time. */
4.1 --- a/libe2access/lib/src/utils.c Wed Feb 16 18:21:01 2022 +0100
4.2 +++ b/libe2access/lib/src/utils.c Wed Feb 16 18:38:11 2022 +0100
4.3 @@ -44,6 +44,10 @@
4.4 else
4.5 data.filename = NULL;
4.6
4.7 + /* Do not show link counts by default. */
4.8 +
4.9 + data.link_count = 0;
4.10 +
4.11 return image_list_dir(fs, path, utils_list_dir_proc, &data);
4.12 }
4.13
4.14 @@ -76,6 +80,11 @@
4.15 inode.i_gid,
4.16 EXT2_I_SIZE(&inode));
4.17
4.18 + /* Show link counts if requested. */
4.19 +
4.20 + if (data->link_count)
4.21 + printf("%6d ", inode.i_links_count);
4.22 +
4.23 /* Output the name which is presumably not necessarily null-terminated. */
4.24
4.25 fwrite(dirent->name, sizeof(char), ext2fs_dirent_name_len(dirent), stdout);