nachos.kernel.filesys
Class OpenFileStub

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

 class OpenFileStub
extends java.lang.Object
implements OpenFile

This "stub" class implements file operations for Nachos by simply passing the filesystem operations through to the native filesystem on the host platform. This is provided in case the multiprogramming and virtual memory assignments (which make use of the file system) are done before the file system assignment.


Field Summary
private static boolean BLOCKING_FILESYS
          Do file operations cause the calling thread to block waiting for the operation to complete? Blocking is simulated by calling Scheduler.yield; more realistically, the thread would wait for an interrupt to inform it that the operation is complete.
private  long currentOffset
          The current file position.
private  java.io.RandomAccessFile file
          The underlying file on the host platform.
 
Constructor Summary
OpenFileStub(java.io.RandomAccessFile f)
          Open a file.
 
Method Summary
 int close()
          Close the file, releasing any resources held in kernel memory.
 long length()
          Determine the number of bytes in the file (this interface is simpler than the UNIX idiom -- lseek to end of file, tell, lseek back).
 int read(byte[] into, int index, int numBytes)
          Read bytes from the file, starting at the implicit position.
 int readAt(byte[] into, int index, int numBytes, long position)
          Read bytes from the file, bypassing the implicit position.
 void seek(long position)
          Set the position from which to start reading/writing -- UNIX lseek
 int write(byte[] from, int index, int numBytes)
          Write bytes to the file, starting at the implicit position.
 int writeAt(byte[] from, int index, int numBytes, long position)
          Write bytes to the file, bypassing the implicit position.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BLOCKING_FILESYS

private static final boolean BLOCKING_FILESYS
Do file operations cause the calling thread to block waiting for the operation to complete? Blocking is simulated by calling Scheduler.yield; more realistically, the thread would wait for an interrupt to inform it that the operation is complete.

See Also:
Constant Field Values

file

private java.io.RandomAccessFile file
The underlying file on the host platform.


currentOffset

private long currentOffset
The current file position.

Constructor Detail

OpenFileStub

OpenFileStub(java.io.RandomAccessFile f)
Open a file. This constructor is not exported outside the package, because users of the filesystem should be using the methods of the FileSystem class to obtain an OpenFile.

Parameters:
f - The underlying file on the host filesystem.
Method Detail

seek

public void seek(long position)
Set the position from which to start reading/writing -- UNIX lseek

Specified by:
seek in interface OpenFile
Parameters:
position - The desired position in the file, in bytes from the start of file.

readAt

public int readAt(byte[] into,
                  int index,
                  int numBytes,
                  long position)
Read bytes from the file, bypassing the implicit position.

Specified by:
readAt in interface OpenFile
Parameters:
into - Buffer into which to read data.
index - Starting position in the buffer.
numBytes - Number of bytes desired.
position - Starting position in the file.
Returns:
The number of bytes actually read (0 in case of an error).

writeAt

public int writeAt(byte[] from,
                   int index,
                   int numBytes,
                   long position)
Write bytes to the file, bypassing the implicit position.

Specified by:
writeAt in interface OpenFile
Parameters:
from - Buffer containing the data to be written.
index - Starting position in the buffer.
numBytes - Number of bytes to write.
position - Starting position in the file.
Returns:
The number of bytes actually written (0 in case of an error).

read

public int read(byte[] into,
                int index,
                int numBytes)
Read bytes from the file, starting at the implicit position. The position is incremented by the number of bytes read.

Specified by:
read in interface OpenFile
Parameters:
into - Buffer into which to read data.
index - Starting position in the buffer.
numBytes - Number of bytes desired.
Returns:
The number of bytes actually read (0 in case of an error).

write

public int write(byte[] from,
                 int index,
                 int numBytes)
Write bytes to the file, starting at the implicit position. The position is incremented by the number of bytes read.

Specified by:
write in interface OpenFile
Parameters:
from - Buffer containing the data to be written.
index - Starting position in the buffer.
numBytes - Number of bytes to write.
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 (this interface is simpler than the UNIX idiom -- lseek to end of file, tell, lseek back).

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.