ext4l: Add ls command support

Implement directory listing (ls) for the ext4l filesystem driver. This
includes path resolution with symlink following (limited to 8 levels to
prevent loops).

Add ext4l_ls() which uses ext4_lookup() for path resolution and
ext4_readdir() for directory enumeration. The dir_context actor callback
formats and prints each directory entry.

Export ext4_lookup() from namei.c and add declarations to ext4.h.

Add test_ls to the Python test suite, which creates a test file using
debugfs and verifies it appears in the directory listing with the
correct size.

Co-developed-by: Claude Opus 4.5 <noreply@anthropic.com>

Cover-letter:
ext4l: Add support for listing directoties (Part H)
This series adds directory-listing support to the ext4l filesystem
driver. It exports a few required functions from the Linux ext4 code,
fixes the dir_emit() stub to properly call the actor callback, and
implements ext4l_ls() with path resolution and symlink following.
END

Signed-off-by: Simon Glass <simon.glass@canonical.com>
This commit is contained in:
Simon Glass
2025-12-22 15:08:32 -07:00
parent 8bafb79e32
commit 60788f5431
7 changed files with 381 additions and 2 deletions

View File

@@ -79,3 +79,32 @@ static int fs_test_ext4l_msgs_norun(struct unit_test_state *uts)
}
FS_TEST_ARGS(fs_test_ext4l_msgs_norun, UTF_SCAN_FDT | UTF_CONSOLE | UTF_MANUAL,
{ "fs_image", UT_ARG_STR });
/**
* fs_test_ext4l_ls_norun() - Test ext4l ls command
*
* This test verifies that the ext4l driver can list directory contents.
*
* Arguments:
* fs_image: Path to the ext4 filesystem image
*/
static int fs_test_ext4l_ls_norun(struct unit_test_state *uts)
{
const char *fs_image = ut_str(EXT4L_ARG_IMAGE);
ut_assertnonnull(fs_image);
ut_assertok(run_commandf("host bind 0 %s", fs_image));
console_record_reset_enable();
ut_assertok(run_commandf("ls host 0"));
/*
* The Python test adds testfile.txt (12 bytes) to the image.
* Directory entries appear in hash order which varies between runs.
* Verify the file entry appears with correct size (12 bytes).
*/
ut_assert_skip_to_line(" 12 testfile.txt");
ut_assert_console_end();
return 0;
}
FS_TEST_ARGS(fs_test_ext4l_ls_norun, UTF_SCAN_FDT | UTF_CONSOLE | UTF_MANUAL,
{ "fs_image", UT_ARG_STR });

View File

@@ -10,6 +10,7 @@ Test ext4l filesystem probing via C unit test.
import os
from subprocess import CalledProcessError, check_call
from tempfile import NamedTemporaryFile
import pytest
@@ -35,6 +36,17 @@ class TestExt4l:
check_call(f'dd if=/dev/zero of={image_path} bs=1M count=64 2>/dev/null',
shell=True)
check_call(f'mkfs.ext4 -q {image_path}', shell=True)
# Add a test file using debugfs (no mount required)
with NamedTemporaryFile(mode='w', delete=False) as tmp:
tmp.write('hello world\n')
tmp_path = tmp.name
try:
check_call(f'debugfs -w {image_path} '
f'-R "write {tmp_path} testfile.txt" 2>/dev/null',
shell=True)
finally:
os.unlink(tmp_path)
except CalledProcessError:
pytest.skip('Failed to create ext4 image')
@@ -57,3 +69,10 @@ class TestExt4l:
output = ubman.run_command(
f'ut -f fs fs_test_ext4l_msgs_norun fs_image={ext4_image}')
assert 'failures: 0' in output
def test_ls(self, ubman, ext4_image):
"""Test that ext4l can list directory contents."""
with ubman.log.section('Test ext4l ls'):
output = ubman.run_command(
f'ut -f fs fs_test_ext4l_ls_norun fs_image={ext4_image}')
assert 'failures: 0' in output