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
-
EACCES
- Permission denied.
-
EBADF
- Bad file number.
-
EEXIST
- File exists.
-
EFBIG
- File too large.
-
EINVAL
- Invalid argument.
-
EISDIR
- Is a directory.
-
EMFILE
- Too many open files.
-
EMLINK
- Too many links.
-
ENFILE
- File table overflow.
-
ENOENT
- No such file or directory.
-
ENOSPC
- No space left on device.
-
ENOTDIR
- Not a directory.
-
EPERM
- Not owner.
-
EROFS
- Read-only file system.
-
EXDEV
- Cross-device link.
-
MAX_OPEN_FILE_SYSTEMS
-
-
O_RDONLY
- Open with read-only access.
-
O_RDWR
- Open for read or write access.
-
O_WRONLY
- Open with write-only access.
-
openFileSystems
-
-
PROGRAM_NAME
- The name this program uses when displaying any error messages
it generates internally.
-
ROOT_FILE_SYSTEM
-
-
S_IEXEC
- User has execute permission
-
S_IFBLK
- Block Special
-
S_IFCHR
- Character special
-
S_IFDIR
- Directory
-
S_IFMPB
- Multiplexed block special
-
S_IFMPC
- Multiplexed character special
-
S_IFMT
- File type mask
-
S_IFREG
- Regular file
-
S_IREAD
- User has read permission
-
S_IRGRP
- Group has read permission
-
S_IROTH
- Others have read permission
-
S_IRUSR
- User has read permission
-
S_IRWXG
- Group has read, write and execute permission
-
S_IRWXO
- Others have read, write and execute permission
-
S_IRWXU
- User (file owner) has read, write and execute permission
-
S_ISGID
- Set group id on execution
-
S_ISUID
- Set user id on execution
-
S_ISVTX
- Save swapped text even after use
-
S_IWGRP
- Group has write permission
-
S_IWOTH
- Others have write permisson
-
S_IWRITE
- User has write permission
-
S_IWUSR
- User has write permission
-
S_IXGRP
- Group has execute permission
-
S_IXOTH
- Others have execute permission
-
S_IXUSR
- User has execute permission
-
sys_errlist
- The array of kernel error messages.
-
sys_nerr
- Number of errors messages defined in sys_errlist
Simulates unix system variable:
int sys_nerr;
-
Kernel()
-
-
close(int)
- Closes the specified file descriptor.
-
creat(String, short)
- Creates a file or directory with the specified mode.
-
exit(int)
- Terminate the current "process".
-
finalize(int)
- End the simulation and exit.
-
fstat(int, Stat)
- Obtain information for an open file.
-
getErrno()
- Get the value of errno for the current process.
-
initialize()
- Initialize the file simulator kernel.
-
lseek(int, int, int)
- Set the current file pointer for a file.
-
open(String, int)
- Opens a file or directory for reading, writing, or
both reading and writing.
-
perror(String)
- Prints a system error message.
-
read(int, byte[], int)
- Read bytes from a file.
-
readdir(int, DirectoryEntry)
- Reads a directory entry from a file descriptor for an open directory.
-
setErrno(int)
- Set the value of errno for the current process.
-
stat(String, Stat)
- Obtain information about a named file.
-
sync()
- First commits inodes to buffers, and then buffers to disk.
-
write(int, byte[], int)
- Write bytes to a file.
-
writedir(int, DirectoryEntry)
- Writes a directory entry from a file descriptor for an
open directory.
PROGRAM_NAME
public static final String PROGRAM_NAME
- The name this program uses when displaying any error messages
it generates internally.
EPERM
public static final int EPERM
- Not owner.
ENOENT
public static final int ENOENT
- No such file or directory.
EBADF
public static final int EBADF
- Bad file number.
EACCES
public static final int EACCES
- Permission denied.
EEXIST
public static final int EEXIST
- File exists.
EXDEV
public static final int EXDEV
- Cross-device link.
ENOTDIR
public static final int ENOTDIR
- Not a directory.
EISDIR
public static final int EISDIR
- Is a directory.
EINVAL
public static final int EINVAL
- Invalid argument.
ENFILE
public static final int ENFILE
- File table overflow.
EMFILE
public static final int EMFILE
- Too many open files.
EFBIG
public static final int EFBIG
- File too large.
ENOSPC
public static final int ENOSPC
- No space left on device.
EROFS
public static final int EROFS
- Read-only file system.
EMLINK
public static final int EMLINK
- Too many links.
sys_nerr
public static final int sys_nerr
- Number of errors messages defined in sys_errlist
Simulates unix system variable:
int sys_nerr;
sys_errlist
public static final String sys_errlist[]
- The array of kernel error messages.
Simulates unix system variable:
const char *sys_errlist[];
S_IFMT
public static final short S_IFMT
- File type mask
S_IFREG
public static final short S_IFREG
- Regular file
S_IFMPB
public static final short S_IFMPB
- Multiplexed block special
S_IFBLK
public static final short S_IFBLK
- Block Special
S_IFDIR
public static final short S_IFDIR
- Directory
S_IFMPC
public static final short S_IFMPC
- Multiplexed character special
S_IFCHR
public static final short S_IFCHR
- Character special
S_ISUID
public static final short S_ISUID
- Set user id on execution
S_ISGID
public static final short S_ISGID
- Set group id on execution
S_ISVTX
public static final short S_ISVTX
- Save swapped text even after use
S_IRWXU
public static final short S_IRWXU
- User (file owner) has read, write and execute permission
S_IRUSR
public static final short S_IRUSR
- User has read permission
S_IREAD
public static final short S_IREAD
- User has read permission
S_IWUSR
public static final short S_IWUSR
- User has write permission
S_IWRITE
public static final short S_IWRITE
- User has write permission
S_IXUSR
public static final short S_IXUSR
- User has execute permission
S_IEXEC
public static final short S_IEXEC
- User has execute permission
S_IRWXG
public static final short S_IRWXG
- Group has read, write and execute permission
S_IRGRP
public static final short S_IRGRP
- Group has read permission
S_IWGRP
public static final short S_IWGRP
- Group has write permission
S_IXGRP
public static final short S_IXGRP
- Group has execute permission
S_IRWXO
public static final short S_IRWXO
- Others have read, write and execute permission
S_IROTH
public static final short S_IROTH
- Others have read permission
S_IWOTH
public static final short S_IWOTH
- Others have write permisson
S_IXOTH
public static final short S_IXOTH
- Others have execute permission
O_RDONLY
public static final int O_RDONLY
- Open with read-only access.
O_WRONLY
public static final int O_WRONLY
- Open with write-only access.
O_RDWR
public static final int O_RDWR
- Open for read or write access.
MAX_OPEN_FILE_SYSTEMS
public static int MAX_OPEN_FILE_SYSTEMS
openFileSystems
public static FileSystem openFileSystems[]
ROOT_FILE_SYSTEM
public static short ROOT_FILE_SYSTEM
Kernel
public Kernel()
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
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
getErrno
public static int getErrno()
- Get the value of errno for the current process.
Simulates the unix variable:
extern int errno ;
- See Also:
- setErrno
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.
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
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
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
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
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
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
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
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
sync
public static void sync()
- First commits inodes to buffers, and then buffers to disk.
Simulates unix system call:
int sync(void);
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
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
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").
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.