Class Kernel

java.lang.Object
   |
   +----Kernel

public class Kernel
extends Object
Simulates a unix-like file system. Provides basic directory and file operations and implements them in terms of the underlying disk block structures.

Author:
Ray Ontko

Variable Index

 o EACCES
Permission denied.
 o EBADF
Bad file number.
 o EEXIST
File exists.
 o EFBIG
File too large.
 o EINVAL
Invalid argument.
 o EISDIR
Is a directory.
 o EMFILE
Too many open files.
 o EMLINK
Too many links.
 o ENFILE
File table overflow.
 o ENOENT
No such file or directory.
 o ENOSPC
No space left on device.
 o ENOTDIR
Not a directory.
 o EPERM
Not owner.
 o EROFS
Read-only file system.
 o EXDEV
Cross-device link.
 o MAX_OPEN_FILE_SYSTEMS
 o O_RDONLY
Open with read-only access.
 o O_RDWR
Open for read or write access.
 o O_WRONLY
Open with write-only access.
 o openFileSystems
 o PROGRAM_NAME
The name this program uses when displaying any error messages it generates internally.
 o ROOT_FILE_SYSTEM
 o S_IEXEC
User has execute permission
 o S_IFBLK
Block Special
 o S_IFCHR
Character special
 o S_IFDIR
Directory
 o S_IFMPB
Multiplexed block special
 o S_IFMPC
Multiplexed character special
 o S_IFMT
File type mask
 o S_IFREG
Regular file
 o S_IREAD
User has read permission
 o S_IRGRP
Group has read permission
 o S_IROTH
Others have read permission
 o S_IRUSR
User has read permission
 o S_IRWXG
Group has read, write and execute permission
 o S_IRWXO
Others have read, write and execute permission
 o S_IRWXU
User (file owner) has read, write and execute permission
 o S_ISGID
Set group id on execution
 o S_ISUID
Set user id on execution
 o S_ISVTX
Save swapped text even after use
 o S_IWGRP
Group has write permission
 o S_IWOTH
Others have write permisson
 o S_IWRITE
User has write permission
 o S_IWUSR
User has write permission
 o S_IXGRP
Group has execute permission
 o S_IXOTH
Others have execute permission
 o S_IXUSR
User has execute permission
 o sys_errlist
The array of kernel error messages.
 o sys_nerr
Number of errors messages defined in sys_errlist

Simulates unix system variable:

   int sys_nerr;
 

Constructor Index

 o Kernel()

Method Index

 o close(int)
Closes the specified file descriptor.
 o creat(String, short)
Creates a file or directory with the specified mode.
 o exit(int)
Terminate the current "process".
 o finalize(int)
End the simulation and exit.
 o fstat(int, Stat)
Obtain information for an open file.
 o getErrno()
Get the value of errno for the current process.
 o initialize()
Initialize the file simulator kernel.
 o lseek(int, int, int)
Set the current file pointer for a file.
 o open(String, int)
Opens a file or directory for reading, writing, or both reading and writing.
 o perror(String)
Prints a system error message.
 o read(int, byte[], int)
Read bytes from a file.
 o readdir(int, DirectoryEntry)
Reads a directory entry from a file descriptor for an open directory.
 o setErrno(int)
Set the value of errno for the current process.
 o stat(String, Stat)
Obtain information about a named file.
 o sync()
First commits inodes to buffers, and then buffers to disk.
 o write(int, byte[], int)
Write bytes to a file.
 o writedir(int, DirectoryEntry)
Writes a directory entry from a file descriptor for an open directory.

Variables

 o PROGRAM_NAME
 public static final String PROGRAM_NAME
The name this program uses when displaying any error messages it generates internally.

 o EPERM
 public static final int EPERM
Not owner.

 o ENOENT
 public static final int ENOENT
No such file or directory.

 o EBADF
 public static final int EBADF
Bad file number.

 o EACCES
 public static final int EACCES
Permission denied.

 o EEXIST
 public static final int EEXIST
File exists.

 o EXDEV
 public static final int EXDEV
Cross-device link.

 o ENOTDIR
 public static final int ENOTDIR
Not a directory.

 o EISDIR
 public static final int EISDIR
Is a directory.

 o EINVAL
 public static final int EINVAL
Invalid argument.

 o ENFILE
 public static final int ENFILE
File table overflow.

 o EMFILE
 public static final int EMFILE
Too many open files.

 o EFBIG
 public static final int EFBIG
File too large.

 o ENOSPC
 public static final int ENOSPC
No space left on device.

 o EROFS
 public static final int EROFS
Read-only file system.

 o EMLINK
 public static final int EMLINK
Too many links.

 o sys_nerr
 public static final int sys_nerr
Number of errors messages defined in sys_errlist

Simulates unix system variable:

   int sys_nerr;
 

 o sys_errlist
 public static final String sys_errlist[]
The array of kernel error messages.

Simulates unix system variable:

   const char *sys_errlist[];
 

 o S_IFMT
 public static final short S_IFMT
File type mask

 o S_IFREG
 public static final short S_IFREG
Regular file

 o S_IFMPB
 public static final short S_IFMPB
Multiplexed block special

 o S_IFBLK
 public static final short S_IFBLK
Block Special

 o S_IFDIR
 public static final short S_IFDIR
Directory

 o S_IFMPC
 public static final short S_IFMPC
Multiplexed character special

 o S_IFCHR
 public static final short S_IFCHR
Character special

 o S_ISUID
 public static final short S_ISUID
Set user id on execution

 o S_ISGID
 public static final short S_ISGID
Set group id on execution

 o S_ISVTX
 public static final short S_ISVTX
Save swapped text even after use

 o S_IRWXU
 public static final short S_IRWXU
User (file owner) has read, write and execute permission

 o S_IRUSR
 public static final short S_IRUSR
User has read permission

 o S_IREAD
 public static final short S_IREAD
User has read permission

 o S_IWUSR
 public static final short S_IWUSR
User has write permission

 o S_IWRITE
 public static final short S_IWRITE
User has write permission

 o S_IXUSR
 public static final short S_IXUSR
User has execute permission

 o S_IEXEC
 public static final short S_IEXEC
User has execute permission

 o S_IRWXG
 public static final short S_IRWXG
Group has read, write and execute permission

 o S_IRGRP
 public static final short S_IRGRP
Group has read permission

 o S_IWGRP
 public static final short S_IWGRP
Group has write permission

 o S_IXGRP
 public static final short S_IXGRP
Group has execute permission

 o S_IRWXO
 public static final short S_IRWXO
Others have read, write and execute permission

 o S_IROTH
 public static final short S_IROTH
Others have read permission

 o S_IWOTH
 public static final short S_IWOTH
Others have write permisson

 o S_IXOTH
 public static final short S_IXOTH
Others have execute permission

 o O_RDONLY
 public static final int O_RDONLY
Open with read-only access.

 o O_WRONLY
 public static final int O_WRONLY
Open with write-only access.

 o O_RDWR
 public static final int O_RDWR
Open for read or write access.

 o MAX_OPEN_FILE_SYSTEMS
 public static int MAX_OPEN_FILE_SYSTEMS
 o openFileSystems
 public static FileSystem openFileSystems[]
 o ROOT_FILE_SYSTEM
 public static short ROOT_FILE_SYSTEM

Constructors

 o Kernel
 public Kernel()

Methods

 o perror
 public static void perror(String s)
Prints a system error message. The actual text written to stderr is the given string, followed by a colon, a space, the message text, and a newline. It is customary to give the name of the program as the argument to perror.

Parameters:
s - the program name
 o setErrno
 public static void setErrno(int newErrno)
Set the value of errno for the current process.

Simulates the unix variable:

   extern int errno ;
 

See Also:
getErrno
 o getErrno
 public static int getErrno()
Get the value of errno for the current process.

Simulates the unix variable:

   extern int errno ;
 

See Also:
setErrno
 o close
 public static int close(int fd)
Closes the specified file descriptor.

Simulates the unix system call:

   int close(int fd);
 

Parameters:
fd - the file descriptor of the file to close
Returns:
Zero if the file is closed; -1 if the file descriptor is invalid.
 o creat
 public static int creat(String pathname,
                         short mode) throws Exception
Creates a file or directory with the specified mode.

Creates a new file or prepares to rewrite an existing file. If the file does not exist, it is given the mode specified. If the file does exist, it is truncated to length zero. The file is opened for writing and its file descriptor is returned.

Simulates the unix system call:

   int creat(const char *pathname, mode_t mode);
 

Parameters:
pathname - the name of the file or directory to create
mode - the file or directory protection mode for the new file
Returns:
the file descriptor (a non-negative integer); -1 if a needed directory is not searchable, if the file does not exist and the directory in which it is to be created is not writable, if the file does exist and is unwritable, if the file is a directory, or if there are already too many open files.
Throws: Exception
if any underlying action causes an exception to be thrown
 o exit
 public static void exit(int status) throws Exception
Terminate the current "process". Any open files will be closed.

Simulates the unix system call:

   exit(int status);
 

Note: If this is the last process to terminate, this method calls finalize().

Parameters:
status - the exit status
Throws: Exception
if any underlying Exception is thrown
 o lseek
 public static int lseek(int fd,
                         int offset,
                         int whence)
Set the current file pointer for a file. The current file position is updated based on the values of offset and whence. If whence is 0, the new position is offset bytes from the beginning of the file. If whence is 1, the new position is the current position plus the value of offset. If whence is 2, the new position is the size of the file plus the offset value. Note that offset may be negative if whence is 1 or 2, as long as the resulting position is not less than zero. It is valid to position past the end of the file, but it is not valid to read past the end of the file.

Simulates the unix system call:

   lseek( int filedes , int offset , int whence );
 

Parameters:
fd - the file descriptor
offset - the offset
whence - 0 = from beginning of file; 1 = from current position ; 2 = from end of file
 o open
 public static int open(String pathname,
                        int flags) throws Exception
Opens a file or directory for reading, writing, or both reading and writing.

The file is positioned at the beginning (byte 0). The returned file descriptor must be used for subsequent calls for other input and output functions on the file.

Simulates the unix system call:

   int open(const char *pathname, int flags );
 

Parameters:
pathname - the name of the file or directory to create
flags - the flags to use when opening the file: O_RDONLY, O_WRONLY, or O_RDWR.
Returns:
the file descriptor (a non-negative integer); -1 if the file does not exist, if one of the necessary directories does not exist or is unreadable, if the file is not readable (resp. writable), or if too many files are open.
Throws: Exception
if any underlying action causes an exception to be thrown
 o read
 public static int read(int fd,
                        byte buf[],
                        int count) throws Exception
Read bytes from a file.

Simulates the unix system call:

   int read(int fd, void *buf, size_t count);
 

Parameters:
fd - the file descriptor of a file open for reading
buf - an array of bytes into which bytes are read
count - the number of bytes to read from the file
Returns:
the number of bytes actually read; or -1 if an error occurs.
Throws: Exception
if any underlying action causes Exception to be thrown
 o readdir
 public static int readdir(int fd,
                           DirectoryEntry dirp) throws Exception
Reads a directory entry from a file descriptor for an open directory.

Simulates the unix system call:

   int readdir(unsigned int fd, struct dirent *dirp ) ;
 
Note that count is ignored in the unix call.

Parameters:
fd - the file descriptor for the directory being read
dirp - the directory entry into which data should be copied
Returns:
number of bytes read; 0 if end of directory; -1 if the file descriptor is invalid, or if the file is not opened for read access.
Throws: Exception
if any underlying action causes Exception to be thrown
 o fstat
 public static int fstat(int fd,
                         Stat buf) throws Exception
Obtain information for an open file.

Simulates the unix system call:

   int fstat(int filedes, struct stat *buf);
 

Throws: Exception
if any underlying action causes Exception to be thrown
 o stat
 public static int stat(String name,
                        Stat buf) throws Exception
Obtain information about a named file.

Simulates the unix system call:

   int stat(const char *name, struct stat *buf);
 

Throws: Exception
if any underlying action causes Exception to be thrown
 o sync
 public static void sync()
First commits inodes to buffers, and then buffers to disk.

Simulates unix system call:

   int sync(void);
 

 o write
 public static int write(int fd,
                         byte buf[],
                         int count) throws Exception
Write bytes to a file.

Simulates the unix system call:

   int write(int fd, const void *buf, size_t count);
 

Throws: Exception
if any underlying action causes Exception to be thrown
 o writedir
 public static int writedir(int fd,
                            DirectoryEntry dirp) throws Exception
Writes a directory entry from a file descriptor for an open directory.

Simulates the unix system call:

   int readdir(unsigned int fd, struct dirent *dirp ) ;
 
Note that count is ignored in the unix call.

Parameters:
fd - the file descriptor for the directory being read
dirp - the directory entry into which data should be copied
Returns:
number of bytes read; 0 if end of directory; -1 if the file descriptor is invalid, or if the file is not opened for read access.
Throws: Exception
if any underlying action causes Exception to be thrown
 o initialize
 public static void initialize()
Initialize the file simulator kernel. This should be the first call in any simulation program. You can think of this as the method which "boots" the kernel. This method opens the "filesys.conf" file (or the file named by the system property "filesys.conf") and reads any properties given in that file, including the filesystem.root.filename and filesystem.root.mode ("r", "rw").

 o finalize
 public static void finalize(int status) throws Exception
End the simulation and exit. Terminates any remaining "processes", flushes all file system blocks to "disk", and exit the simulation program. This method is generally called by exit() when the last process terminates. However, it may also be called directly to gracefully end the simlation.

Parameters:
status - the status to use with System.exit()
Throws: Exception
if any underlying operation causes and exception to be thrown.