Skip to content

Commit

Permalink
fatfs: prevent illegal operation to avoid volume corruption
Browse files Browse the repository at this point in the history
Signed-off-by: Cheng <[email protected]>
  • Loading branch information
Cheng-Li1 committed Oct 11, 2024
1 parent 87e0c1b commit 69bcb99
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
5 changes: 5 additions & 0 deletions components/fs/fat/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ void print_sector_data(uint8_t *buffer, unsigned long size) {
_Static_assert(BLK_QUEUE_SIZE_CLI_FAT >= FAT_WORKER_THREAD_NUM,
"The size of queue between fs and blk should be at least the size of FAT_WORKER_THREAD_NUM");

// TODO: The FF_FS_LOCK is meant to prevent illegal behavior from the client, e.g. open a file and remove the file before closing it
// However, the illegal operations can ideatically be sanitized on an upper layer
_Static_assert(FF_FS_LOCK >= (FAT_MAX_OPENED_DIRNUM + FAT_MAX_OPENED_FILENUM),
"FF_FS_LOCK should be equal or larger than max opened dir number and max opened file number combined");

void init(void) {
// Init the block device queue
// Have to make sure who initialize this SDDF queue
Expand Down
2 changes: 1 addition & 1 deletion dep/ff15/ffconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@
*/


#define FF_FS_LOCK 0
#define FF_FS_LOCK 48
/* The option FF_FS_LOCK switches file lock function to control duplicated file open
/ and illegal operation to open objects. This option must be 0 when FF_FS_READONLY
/ is 1.
Expand Down
26 changes: 13 additions & 13 deletions examples/fileio/fs_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ def test_environment(path):
raise AssertionError(f"Test failed: Directory '{final_dir}' already exists.")
else:
os.mkdir(final_dir)
print(f"Test environment set up: '{final_dir}' created successfully.")

return final_dir

Expand Down Expand Up @@ -112,21 +111,21 @@ def test_write_and_read_back_complex(directory):
]

try:
# Write the poem to the file line by line and read back each line to verify
# Step 1: Write the poem to the file line by line
with open(test_file, "w") as f:
for index, line in enumerate(poem_lines):
for line in poem_lines:
f.write(line + "\n")
# print(f"Line {index + 1} written to file.")
f.flush() # Ensure all content is written to disk

# Read the line back immediately to verify
f.flush() # Ensure the content is written to the file
with open(test_file, "r") as fr:
lines = fr.readlines()
read_back_line = lines[-1].strip() # Read the last line written
# Step 2: Open the file for reading after writing is complete
with open(test_file, "r") as f:
read_back_lines = [line.strip() for line in f.readlines()] # Read all lines and strip newlines

assert read_back_line == line, (
f"Test failed at line {index + 1}: Expected: '{line}', Got: '{read_back_line}'"
)
# Step 3: Verify each line matches the expected poem lines
for index, line in enumerate(poem_lines):
assert read_back_lines[index] == line, (
f"Test failed at line {index + 1}: Expected: '{line}', Got: '{read_back_lines[index]}'"
)

# Increment success count if all lines are verified
success_count += 1
Expand All @@ -136,10 +135,11 @@ def test_write_and_read_back_complex(directory):
fail_count += 1

finally:
# Cleanup
# Cleanup: Remove the test file
if path_exists(test_file):
os.remove(test_file)


def test_mkdir_and_remove(directory):
"""Test creating directories with various names and removing them."""
global success_count, fail_count
Expand Down

0 comments on commit 69bcb99

Please sign in to comment.