nachos.kernel.filesys
Class Directory

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

 class Directory
extends java.lang.Object

This class class defines a UNIX-like "directory". Each entry in the directory describes a file, and where to find it on disk. The directory is a table of fixed length entries; each entry represents a single file, and contains the file name, and the location of the file header on disk. The fixed size of each directory entry means that we have the restriction of a fixed maximum size for file names. Also, this implementation has the restriction that the size of the directory cannot expand. In other words, once all the entries in the directory are used, no more files can be created. Fixing this is one of the parts to the assignment. The directory data structure can be stored in memory, or on disk. When it is on disk, it is stored as a regular Nachos file. The constructor initializes a directory structure in memory; the fetchFrom/writeBack operations shuffle the directory information from/to disk. We assume mutual exclusion is provided by the caller.


Field Summary
private  DirectoryEntry[] table
          Table of pairs: file name/file header location.
private  int tableSize
          Number of entries in the directory.
 
Constructor Summary
Directory(int size)
          Initialize a directory; initially, the directory is completely empty.
 
Method Summary
(package private)  boolean add(java.lang.String name, int newSector)
          Add a file into the directory.
(package private)  void fetchFrom(OpenFile file)
          Read the contents of the directory from disk.
(package private)  int find(java.lang.String name)
          Look up file name in directory, and return the disk sector number where the file's header is stored.
private  int findIndex(java.lang.String name)
          Look up file name in directory, and return its location in the table of directory entries.
(package private)  void list()
          List all the file names in the directory (for debugging).
(package private)  void print()
          List all the file names in the directory, their FileHeader locations, and the contents of each file (for debugging).
(package private)  boolean remove(java.lang.String name)
          Remove a file name from the directory.
(package private)  void writeBack(OpenFile file)
          Write any modifications to the directory back to disk
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

tableSize

private int tableSize
Number of entries in the directory.


table

private DirectoryEntry[] table
Table of pairs: file name/file header location.

Constructor Detail

Directory

Directory(int size)
Initialize a directory; initially, the directory is completely empty. If the disk is being formatted, an empty directory is all we need, but otherwise, we need to call FetchFrom in order to initialize it from disk.

Parameters:
size - The number of entries in the directory.
Method Detail

fetchFrom

void fetchFrom(OpenFile file)
Read the contents of the directory from disk.

Parameters:
file - The file containing the directory contents.

writeBack

void writeBack(OpenFile file)
Write any modifications to the directory back to disk

Parameters:
file - The file to contain the new directory contents.

findIndex

private int findIndex(java.lang.String name)
Look up file name in directory, and return its location in the table of directory entries. Return -1 if the name isn't in the directory.

Parameters:
name - The file name to look up.
Returns:
The index of the entry in the table, if present, otherwise -1.

find

int find(java.lang.String name)
Look up file name in directory, and return the disk sector number where the file's header is stored. Return -1 if the name isn't in the directory.

Parameters:
name - The file name to look up.
Returns:
The disk sector number where the file's header is stored, if the entry was found, otherwise -1.

add

boolean add(java.lang.String name,
            int newSector)
Add a file into the directory. Return TRUE if successful; return FALSE if the file name is already in the directory, or if the directory is completely full, and has no more space for additional file names, or if the file name cannot be represented in the number of bytes available in a directory entry.

Parameters:
name - The name of the file being added.
newSector - The disk sector containing the added file's header.
Returns:
true if the file was successfully added, otherwise false.

remove

boolean remove(java.lang.String name)
Remove a file name from the directory. Return TRUE if successful; return FALSE if the file isn't in the directory.

Parameters:
name - The file name to be removed.

list

void list()
List all the file names in the directory (for debugging).


print

void print()
List all the file names in the directory, their FileHeader locations, and the contents of each file (for debugging).