ext4l: Add size() support

Add ext4l_size() function to retrieve the size of a file or directory.
Wire it into the filesystem operations table in fs_legacy.c.

Signed-off-by: Simon Glass <sjg@chromium.org>

Co-developed-by: Claude Opus 4.5 <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
This commit is contained in:
Simon Glass
2025-12-22 18:24:26 -07:00
parent 0e9c88b170
commit 8810a02743
5 changed files with 65 additions and 1 deletions

View File

@@ -649,6 +649,20 @@ int ext4l_exists(const char *filename)
return 1;
}
int ext4l_size(const char *filename, loff_t *sizep)
{
struct inode *inode;
int ret;
ret = ext4l_resolve_path(filename, &inode);
if (ret)
return ret;
*sizep = inode->i_size;
return 0;
}
void ext4l_close(void)
{
if (ext4l_open_dirs > 0)

View File

@@ -267,7 +267,7 @@ static struct fstype_info fstypes[] = {
.close = ext4l_close,
.ls = ext4l_ls,
.exists = ext4l_exists,
.size = fs_size_unsupported,
.size = ext4l_size,
.read = fs_read_unsupported,
.write = fs_write_unsupported,
.uuid = fs_uuid_unsupported,

View File

@@ -46,6 +46,15 @@ int ext4l_ls(const char *dirname);
*/
int ext4l_exists(const char *filename);
/**
* ext4l_size() - Get the size of a file
*
* @filename: Path to file
* @sizep: Returns the file size
* Return: 0 on success, negative on error
*/
int ext4l_size(const char *filename, loff_t *sizep);
/**
* ext4l_get_uuid() - Get the filesystem UUID
*

View File

@@ -11,6 +11,7 @@
#include <ext4l.h>
#include <fs.h>
#include <fs_legacy.h>
#include <linux/sizes.h>
#include <u-boot/uuid.h>
#include <test/test.h>
#include <test/ut.h>
@@ -223,3 +224,36 @@ static int fs_test_ext4l_exists_norun(struct unit_test_state *uts)
}
FS_TEST_ARGS(fs_test_ext4l_exists_norun, UTF_SCAN_FDT | UTF_CONSOLE |
UTF_MANUAL, { "fs_image", UT_ARG_STR });
/**
* fs_test_ext4l_size_norun() - Test ext4l_size function
*
* Verifies that ext4l_size correctly reports file size.
*
* Arguments:
* fs_image: Path to the ext4 filesystem image
*/
static int fs_test_ext4l_size_norun(struct unit_test_state *uts)
{
const char *fs_image = ut_str(EXT4L_ARG_IMAGE);
loff_t size;
ut_assertnonnull(fs_image);
ut_assertok(run_commandf("host bind 0 %s", fs_image));
ut_assertok(fs_set_blk_dev("host", "0", FS_TYPE_ANY));
/* Test root directory size - one block on a 4K block filesystem */
ut_assertok(ext4l_size("/", &size));
ut_asserteq(SZ_4K, size);
/* Test file size - testfile.txt contains "hello world\n" */
ut_assertok(ext4l_size("/testfile.txt", &size));
ut_asserteq(12, size);
/* Test non-existent path returns -ENOENT */
ut_asserteq(-ENOENT, ext4l_size("/no/such/path", &size));
return 0;
}
FS_TEST_ARGS(fs_test_ext4l_size_norun, UTF_SCAN_FDT | UTF_CONSOLE | UTF_MANUAL,
{ "fs_image", UT_ARG_STR });

View File

@@ -103,3 +103,10 @@ class TestExt4l:
output = ubman.run_command(
f'ut -f fs fs_test_ext4l_exists_norun fs_image={ext4_image}')
assert 'failures: 0' in output
def test_size(self, ubman, ext4_image):
"""Test that ext4l_size reports file size correctly."""
with ubman.log.section('Test ext4l size'):
output = ubman.run_command(
f'ut -f fs fs_test_ext4l_size_norun fs_image={ext4_image}')
assert 'failures: 0' in output