Your task in this assignment is to implement a simulated file system. The simulated file system (simfs) is stored in a single Unix file. There
is only one directory in this file system, and there are limits on the number of files (MAXFILES) it can store and the amount of space
(MAXBLOCKS) it can occupy. The key file system structures are already defined; your job is to add functions: the ability to create a file, delete
a file, write to a file, and read from a file.
In completing this assignment, we expect you will learn about how file systems are implemented, will become familiar with both binary
and ASCII file I/O, and will become comfortable using arrays of structs and characters.
Please read the next section, about the simfs structure, very carefully. The specification for the file system is necessarily detailed, and for
your program to work with simfs-formatted files, it’ll need to conform precisely to the spec.
simfs should be thought of as an array of blocks. Each block is a contiguous chunk of BLOCKSIZE bytes. Metadata (described below) is
stored at the beginning of the file in the first (or first few) blocks. The simulated file system, simfs, contains two types of metadata: file
entries (fentries) and file nodes (fnodes).
A fentry contains:
file name: An array to store a name of maximum length 11.
size: An unsigned short integer giving the size of the actual file. Note that a file might not use all of the space in the blocks
allocated to it.
firstblock: An index into the array of fnodes. The specified fnode knows where the first block of data in the file is stored and how
to get information about the next (second) block of data.
An fnode contains:
blockindex: The index of the data block storing the file data associated with this fnode. The magnitude of the index is always the
index of the fnode in its array; it’s negative if the data block is not in use.
nextblock: An index into the array of fnodes. The specified fnode contains info about the next block of data in the file. This value
is -1 if there is no next block in the file.
There are a fixed number of fentries and a fixed number of fnodes, so it’s possible to calculate how much space they will take. The array
of fentries is stored at the very beginning of the file, followed immediately after by the array of fnodes. The number of blocks required
to store the fentries and fnodes depends on how many of them there are (MAXFILES and MAXBLOCKS). See simfstypes.h for the definitions of
these constants as well as for the structs defined in the previous bullets.
For example, suppose the maximum number of files is 4 and the maximum number of fnodes is 6. The block size is set to 128. The fentry
and fnode arrays take 88 bytes ((16*4) + (4 * 6) = 88), so they can fit in a single block. As a result, the first fnode is in use (block 0
contains metadata), and file data begins in the second block (index 1). The real file containing the simulated file system would be
arranged as shown in the following diagram. Please note that the boxes are not scaled according to the number of bytes they occupy.