nachos.kernel.filesys
Class FileHeader

java.lang.Object
  extended by nachos.kernel.filesys.FileHeader

 class FileHeader
extends java.lang.Object

This class defines the Nachos "file header" (in UNIX terms, the "i-node"), describing where on disk to find all of the data in the file. The file header is organized as a simple table of pointers to data blocks. The file header data structure can be stored in memory or on disk. When it is on disk, it is stored in a single sector -- this means that we assume the size of this data structure to be the same as one disk sector. Without indirect addressing, this limits the maximum file length to just under 4K bytes. The file header is used to locate where on disk the file's data is stored. We implement this as a fixed size table of pointers -- each entry in the table points to the disk sector containing that portion of the file data (in other words, there are no indirect or doubly indirect blocks). The table size is chosen so that the file header will be just big enough to fit in one disk sector, Unlike in a real system, we do not keep track of file permissions, ownership, last modification date, etc., in the file header. A file header can be initialized in two ways: for a new file, by modifying the in-memory data structure to point to the newly allocated data blocks; for a file already on disk, by reading the file header from disk.


Field Summary
private  int[] dataSectors
          Disk sector numbers for each data block in the file.
private static int MaxFileSize
          Maximum file size that can be represented in the baseline system.
private  int numBytes
          Number of bytes in the file.
private static int NumDirect
          Number of pointers to data blocks stored in a file header.
private  int numSectors
          Number of data sectors in the file.
 
Constructor Summary
FileHeader()
          Allocate a new "in-core" file header.
 
Method Summary
(package private)  boolean allocate(BitMap freeMap, int fileSize)
          Initialize a fresh file header for a newly created file.
(package private)  int byteToSector(int offset)
          Calculate which disk sector is storing a particular byte within the file.
(package private)  void deallocate(BitMap freeMap)
          De-allocate all the space allocated for data blocks for this file.
private  void externalize(byte[] buffer, int pos)
          Export the fields of this FileHeader object to a buffer in a format suitable for writing to the disk.
(package private)  void fetchFrom(int sector)
          Fetch contents of file header from disk.
(package private)  int fileLength()
          Retrieve the number of bytes in the file.
private  void internalize(byte[] buffer, int pos)
          Initialize the fields of this FileHeader object using data read from the disk.
(package private)  void print()
          Print the contents of the file header, and the contents of all the data blocks pointed to by the file header.
private static int sizeOf()
          Calculate the size of the on-disk representation of a FileHeader.
(package private)  void writeBack(int sector)
          Write the modified contents of the file header back to disk.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NumDirect

private static final int NumDirect
Number of pointers to data blocks stored in a file header.

See Also:
Constant Field Values

MaxFileSize

private static final int MaxFileSize
Maximum file size that can be represented in the baseline system.

See Also:
Constant Field Values

numBytes

private int numBytes
Number of bytes in the file.


numSectors

private int numSectors
Number of data sectors in the file.


dataSectors

private int[] dataSectors
Disk sector numbers for each data block in the file.

Constructor Detail

FileHeader

FileHeader()
Allocate a new "in-core" file header.

Method Detail

sizeOf

private static int sizeOf()
Calculate the size of the on-disk representation of a FileHeader. This should be exactly equal to one disk sector.

Returns:
the number of bytes it takes to store a FileHeader on the disk.

internalize

private void internalize(byte[] buffer,
                         int pos)
Initialize the fields of this FileHeader object using data read from the disk.

Parameters:
buffer - A buffer holding the data read from the disk.
pos - Position in the buffer at which to start.

externalize

private void externalize(byte[] buffer,
                         int pos)
Export the fields of this FileHeader object to a buffer in a format suitable for writing to the disk.

Parameters:
buffer - A buffer into which to place the exported data.
pos - Position in the buffer at which to start.

allocate

boolean allocate(BitMap freeMap,
                 int fileSize)
Initialize a fresh file header for a newly created file. Allocate data blocks for the file out of the map of free disk blocks. Return FALSE if there are not enough free blocks to accomodate the new file.

Parameters:
freeMap - is the bit map of free disk sectors.
fileSize - is size of the new file.

deallocate

void deallocate(BitMap freeMap)
De-allocate all the space allocated for data blocks for this file.

Parameters:
freeMap - is the bit map of free disk sectors.

fetchFrom

void fetchFrom(int sector)
Fetch contents of file header from disk.

Parameters:
sector - is the disk sector containing the file header.

writeBack

void writeBack(int sector)
Write the modified contents of the file header back to disk.

Parameters:
sector - is the disk sector to contain the file header.

byteToSector

int byteToSector(int offset)
Calculate which disk sector is storing a particular byte within the file. This is essentially a translation from a virtual address (the offset in the file) to a physical address (the sector where the data at the offset is stored).

Parameters:
offset - The location within the file of the byte in question.
Returns:
the disk sector number storing the specified byte.

fileLength

int fileLength()
Retrieve the number of bytes in the file.

Returns:
the number of bytes in the file.

print

void print()
Print the contents of the file header, and the contents of all the data blocks pointed to by the file header.