diff --git a/lib/fs/fat/file.cpp b/lib/fs/fat/file.cpp index 6ccc688f0..86167c593 100644 --- a/lib/fs/fat/file.cpp +++ b/lib/fs/fat/file.cpp @@ -194,9 +194,11 @@ ssize_t fat_file::read_file_priv(void *_buf, const off_t offset, size_t len) { if (offset_within_sector == fs_->info().bytes_per_sector) { offset_within_sector = 0; // push the iterator forward to next sector - err = fbi.next_sector(); - if (err < 0) { - return err; + if (buf_offset < len) { + err = fbi.next_sector(); + if (err < 0) { + return err; + } } } } diff --git a/lib/fs/fat/test/mkblk b/lib/fs/fat/test/mkblk index 01c434af6..8c0288935 100755 --- a/lib/fs/fat/test/mkblk +++ b/lib/fs/fat/test/mkblk @@ -31,6 +31,8 @@ for i in blk.bin.*; do mmd -i $i dir.c mcopy -i $i hello.txt ::dir.a/long_filename_hello.txt mcopy -i $i largefile ::largefile + mcopy -i $i test_4kb.bin ::test_4kb.bin + mcopy -i $i test_8kb.bin ::test_8kb.bin # add a bunch of entries to the root dir that cause it # to spill over to a few extra sectors diff --git a/lib/fs/fat/test/test.cpp b/lib/fs/fat/test/test.cpp index dd9315b63..043ea084d 100644 --- a/lib/fs/fat/test/test.cpp +++ b/lib/fs/fat/test/test.cpp @@ -26,6 +26,8 @@ // pull in a few test files into rodata to test against INCFILE(test_file_hello, test_file_hello_size, LOCAL_DIR "/hello.txt"); INCFILE(test_file_license, test_file_license_size, LOCAL_DIR "/LICENSE"); +INCFILE(test_file_4kb, test_file_4kb_size, LOCAL_DIR "/test_4kb.bin"); +INCFILE(test_file_8kb, test_file_8kb_size, LOCAL_DIR "/test_8kb.bin"); namespace { @@ -171,6 +173,8 @@ bool test_fat_read_file() { EXPECT_TRUE(test_file_read(test_path "/long_filename_hello.txt", test_file_hello, test_file_hello_size)); EXPECT_TRUE(test_file_read(test_path "/a_very_long_filename_hello_that_uses_at_least_a_few_entries.txt", test_file_hello, test_file_hello_size)); EXPECT_TRUE(test_file_read(test_path "/dir.a/long_filename_hello.txt", test_file_hello, test_file_hello_size)); + EXPECT_TRUE(test_file_read(test_path "/test_4kb.bin", test_file_4kb, test_file_4kb_size)); + EXPECT_TRUE(test_file_read(test_path "/test_8kb.bin", test_file_8kb, test_file_8kb_size)); // unmount the fs unmount_cleanup.cancel(); diff --git a/lib/fs/fat/test/test_4kb.bin b/lib/fs/fat/test/test_4kb.bin new file mode 100644 index 000000000..02a0169ef Binary files /dev/null and b/lib/fs/fat/test/test_4kb.bin differ diff --git a/lib/fs/fat/test/test_8kb.bin b/lib/fs/fat/test/test_8kb.bin new file mode 100644 index 000000000..62a763466 Binary files /dev/null and b/lib/fs/fat/test/test_8kb.bin differ