nachos.kernel.filesys
Class OpenFileReal

java.lang.Object
  extended by nachos.kernel.filesys.OpenFileReal
All Implemented Interfaces:
OpenFile

 class OpenFileReal
extends java.lang.Object
implements OpenFile

This is a class for managing an open Nachos file. As in UNIX, a file must be open before we can read or write to it. Once we're all done, we can close it (in Nachos, by deleting the OpenFile data structure). Also as in UNIX, for convenience, we keep the file header in memory while the file is open. Supports opening, closing, reading and writing to individual files. The operations supported are similar to the UNIX ones. There are two implementations. This is the "real" implementation, that turns these operations into read and write disk sector requests. In this baseline implementation of the file system, we don't worry about concurrent accesses to the file system by different threads -- this is part of the assignment.


Field Summary
private  FileHeader hdr
          Cached copy of the header for this file.
private  int seekPosition
          Current position within the file.
 
Constructor Summary
OpenFileReal(int sector)
          Open a Nachos file for reading and writing.
 
Method Summary
 int close()
          Close the file, releasing any resources held in kernel memory.
 long length()
          Determine the number of bytes in the file.
 int read(byte[] into, int index, int numBytes)
          Read a portion of a file, starting from seekPosition.
 int readAt(byte[] into, int index, int numBytes, long position)
          Read a portion of a file, starting at "position".
 void seek(long position)
          Change the current location within the open file -- the point at which the next Read or Write will start from.
 int write(byte[] from, int index, int numBytes)
          Write a portion of a file, starting from seekPosition.
 int writeAt(byte[] from, int index, int numBytes, long position)
          Write a portion of a file, starting at "position".
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

hdr

private FileHeader hdr
Cached copy of the header for this file.


seekPosition

private int seekPosition
Current position within the file.

Constructor Detail

OpenFileReal

OpenFileReal(int sector)
Open a Nachos file for reading and writing. Bring the file header into memory while the file is open. This constructor is not public, because users of the filesystem should be using the methods of the FileSystem class to obtain an OpenFile.

Parameters:
sector - The location on disk of the file header for this file.
Method Detail

seek

public void seek(long position)
Change the current location within the open file -- the point at which the next Read or Write will start from.

Specified by:
seek in interface OpenFile
Parameters:
position - -- the location within the file for the next Read/Write.

read

public int read(byte[] into,
                int index,
                int numBytes)
Read a portion of a file, starting from seekPosition. Return the number of bytes actually read, and as a side effect, increment the current position within the file. Implemented using the more primitive ReadAt.

Specified by:
read in interface OpenFile
Parameters:
into - The buffer to contain the data to be read from disk .
index - Position in the buffer at which to begin placing data.
numBytes - The number of bytes to transfer.
Returns:
The number of bytes actually read (0 if error).

write

public int write(byte[] from,
                 int index,
                 int numBytes)
Write a portion of a file, starting from seekPosition. Return the number of bytes actually written, and as a side effect, increment the current position within the file. Implemented using the more primitive WriteAt.

Specified by:
write in interface OpenFile
Parameters:
from - The buffer containing the data to be written to disk .
index - Position in the buffer at which to begin taking data.
numBytes - The number of bytes to transfer.
Returns:
The number of bytes actually written (0 if error).

readAt

public int readAt(byte[] into,
                  int index,
                  int numBytes,
                  long position)
Read a portion of a file, starting at "position". Return the number of bytes actually read, but has no side effects. There is no guarantee the request starts or ends on an even disk sector boundary; however the disk only knows how to read a whole disk sector at a time. Thus: We read in all of the full or partial sectors that are part of the request, but we only copy the part we are interested in.

Specified by:
readAt in interface OpenFile
Parameters:
into - The buffer to contain the data to be read from disk.
index - Position in the buffer at which to begin placing data.
numBytes - The number of bytes to transfer.
position - The offset within the file of the first byte to be read/written.
Returns:
The number of bytes actually read (0 if error).

writeAt

public int writeAt(byte[] from,
                   int index,
                   int numBytes,
                   long position)
Write a portion of a file, starting at "position". Return the number of bytes actually written, but has no side effects (except that Write modifies the file, of course). There is no guarantee the request starts or ends on an even disk sector boundary; however the disk only knows how to write a whole disk sector at a time. Thus: We must first read in any sectors that will be partially written, so that we don't overwrite the unmodified portion. We then copy in the data that will be modified, and write back all the full or partial sectors that are part of the request.

Specified by:
writeAt in interface OpenFile
Parameters:
from - The buffer containing the data to be written to disk.
index - Position in the buffer at which to begin placing data.
numBytes - The number of bytes to transfer.
position - The offset within the file of the first byte to be read/written.
Returns:
The number of bytes actually written (0 in case of an error).

length

public long length()
Determine the number of bytes in the file.

Specified by:
length in interface OpenFile
Returns:
the length of the file in bytes.

close

public int close()
Close the file, releasing any resources held in kernel memory. Subsequent attempts to access the file will fail.

Specified by:
close in interface OpenFile
Returns:
0 if an error occurred while closing the file, otherwise nonzero.