Skip to content
Snippets Groups Projects
Commit 1f40894a authored by Raphael.Olande's avatar Raphael.Olande
Browse files

task 1

parent 7936cf1a
No related branches found
No related tags found
No related merge requests found
from myfs_1 import (
_get_num_files, _set_num_files, NoFreeSpace, NoFile, EmptyFile,
HEADER_START, FILE_ENTRY_SIZE, FILE_BLOCK_SIZE, MAX_FILES
)
def save(f, filename, content):
"""
Saves a file with the given content to the file system, locating the
first available block, and ensures that the file is not empty.
Args:
f (file object): The file object representing the file system.
filename (str): The name of the file to be saved.
content (str): The file content to be saved.
Raises:
NoFreeSpace: If no free space is available to store the file.
EmptyFile: If the content is empty.
"""
if not content:
raise EmptyFile("Not allowed to store an empty file")
num_existing = _get_num_files(f)
# Look for the first available slot in the file table
for new_fileno in range(MAX_FILES):
f.seek(HEADER_START + FILE_ENTRY_SIZE * (new_fileno + 1))
name_entry = f.read(FILE_ENTRY_SIZE).decode("ascii").strip('\0')
if not name_entry: # Empty entry found
break
else:
# If no empty block is found, raise NoFreeSpace
raise NoFreeSpace(f"No free space available, max files: {MAX_FILES}")
# Update the file count only if we're using a new slot
if new_fileno >= num_existing:
_set_num_files(f, num_existing + 1)
# Write filename to file table in the located free entry
f.seek(HEADER_START + FILE_ENTRY_SIZE * (new_fileno + 1))
f.write(filename.encode('ascii')[:FILE_ENTRY_SIZE])
# Write content to the file block, truncate if needed
f.seek(HEADER_START + FILE_BLOCK_SIZE * (new_fileno + 1))
f.write(content.encode('ascii')[:FILE_BLOCK_SIZE])
test1.fs 0 → 100644
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment