Question: Personal file system in Python How to submit the assignment? Submit 1 file for each of the assignments, including the sub - assignments in assignment

Personal file system in Python
How to submit the assignment?
Submit 1 file for each of the assignments, including the sub-assignments in assignment 4. Everything is delivered on codegrade.
The assignments are progression-based. This means that you must use the file you received from task 1 and continue with it in task 2. Perhaps the most practical thing to do is to make a copy of task 1 when you have finished and then work on task 2 from there. And so on.
Handed out code
In this task, you will be given code that simulates a simple file system. Run main.py to generate the file system with some sample files.
The file system as a whole is stored in a file myfilesystem.fs and is divided into blocks. The first block has header information about the file system, and the rest are blocks used for file content. Read the comments and documentation in the code to better understand the code. It is also possible to open the file system as raw data by using the command xxd myfilesystem.fs in the terminal. You will then see an overview of the file system in hexadecimal format.
In main.py, some tests have been added that you can run after each task to check if your code works as expected.
ATTENTION: Keep as much of the original structure as possible, i.e. do not delete or change functions and function names. As you work on the task, you also need to consider the effect of your changes on the functionality of the file system as a whole. For example, changing one function can have effects on another.
Task 1: Better use of free blocks
deliver file: myfs_1.py
Try to understand how save() decides where to save a new file. Consider why this is not ideal considering that files can be deleted. Change the code so that it always finds the first available block to write to. This should work in conjunction with remove(), which deletes files. Make sure you still get the same error message if you don't have room to save more files. In addition, saving an empty file should not be allowed, and you should use the error message EmptyFile when this is the case.
Task 2: Better loading of files
deliver file: myfs_2.py
The way load() is implemented, what happens to files that have valid \0 bytes at the end of the content? We want to change load() so that it reads as many bytes as the file actually contains, and not the whole block. Then we solve that problem.
This will require us to store the file size in the file's header. Think about how much of a file entry you need to use for this purpose, and update FILENAME_SIZE accordingly. After that, change save() so that it saves the file size in the header, and change load() so that it reads the correct number of bytes from the block.
Also change find_fileno() so that it works with the new change.
Finally, since we are now storing file sizes, we can create a helper function find_filesize(f, fileno) that takes the file number returned by find_fileno() and returns the size of the file that belongs to the file number.
I cant figure out how to implement and pass test 1, or if the test is supposed to pass
def save(f, filename, content):
"""
Saves a file with the given content to the file system.
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.
"""
num_existing =_get_num_files(f)
new_fileno = num_existing +1
_set_num_files(f, new_fileno)
if new_fileno >= MAX_FILES:
| raise NoFreeSpace(f"No free space available, max files: {MAX_FILES}")
write filename in ftable next free entry, truncate if needed
f.seek(HEADER_START + FILE_ENTRY_SIZE * new_fileno)
f.write(filename.encode('ascii')[:FILE_ENTRY_SIZE])
write content to file block, truncate if needed
f.seek(HEADER_START + FILE_BLOCK_SIZE * new_fileno)
f.write(content.encode('ascii')[:FILE_BLOCK_SIZE])
def load(f, filename):
"""
Load the content of a file from a custom filesystem.
Args:
f (file object): The file object representing the file system.
filename (str): The name of the file to load.
Returns:
|"|" str: The content of the file as a string.
fileno = find_fileno(f, filename)
f.seek(HEADER_START + FILE_BLOCK_SIZE *(fileno+1))
content = f.read(FILE_BLOCK_SIZE)
content = content.decode("ascii").strip('\0')
return content
Personal file system in Python How to submit the

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!