Files
ViperOS/vpsdk/fs.vp
T

138 lines
3.9 KiB
Plaintext

from . import syscall as syscall
define O_RDONLY = 0x01
define O_WRONLY = 0x02
define O_RDWR = 0x03
define O_CREAT = 0x04
define O_TRUNC = 0x08
define O_APPEND = 0x30
define SEEK_SET = 0
define SEEK_CUR = 1
define SEEK_END = 2
define DT_UNKNOWN = 0
define DT_FILE = 1
define DT_DIR = 2
def open(path: const str, flags: uint32) -> int:
return int(syscall._syscall3(uint64(syscall.OPEN), uint64(path), uint64(flags), 0))
def close(fd: int) -> int:
return int(syscall._syscall1(uint64(syscall.CLOSE), uint64(fd)))
def read(fd: int, buf: rawptr, count: uint32) -> int:
return int(syscall._syscall3(uint64(syscall.READ), uint64(fd), uint64(buf), uint64(count)))
def write(fd: int, buf: rawptr, count: uint32) -> int:
return int(syscall._syscall3(uint64(syscall.WRITE), uint64(fd), uint64(buf), uint64(count)))
def seek(fd: int, offset: uint32) -> int:
return int(syscall._syscall3(uint64(syscall.SEEK), uint64(fd), uint64(offset), 0))
def tell(fd: int) -> uint32:
return uint32(syscall._syscall1(uint64(syscall.TELL), uint64(fd)))
def fsize(fd: int) -> uint32:
return uint32(syscall._syscall1(uint64(syscall.SIZE), uint64(fd)))
def mkdir(path: const str) -> int:
return int(syscall._syscall3(uint64(syscall.MKDIR), uint64(path), 0, 0))
def remove(path: const str) -> int:
return int(syscall._syscall3(uint64(syscall.REMOVE), uint64(path), 0, 0))
def opendir(path: const str) -> int:
return int(syscall._syscall3(uint64(syscall.OPENDIR), uint64(path), 0, 0))
def readdir(dd: int, info: rawptr) -> int:
return int(syscall._syscall3(uint64(syscall.READDIR), uint64(dd), uint64(info), 0))
def closedir(dd: int) -> int:
return int(syscall._syscall1(uint64(syscall.CLOSEDIR), uint64(dd)))
def stat(path: const str, info: rawptr) -> int:
return int(syscall._syscall3(uint64(syscall.STAT), uint64(path), uint64(info), 0))
class FileIO:
_fd: int
_pos: uint32
_size: uint32
_flags: uint32
_closed: int
def __init__(self, fd: int, flags: uint32):
self._fd = fd
self._flags = flags
self._pos = 0
self._size = fsize(fd)
self._closed = 0
def read(self, buf: rawptr, count: uint32) -> int:
if self._closed: return -1
n: int = read(self._fd, buf, count)
if n > 0:
self._pos = self._pos + uint32(n)
return n
def write(self, buf: rawptr, count: uint32) -> int:
if self._closed: return -1
n: int = write(self._fd, buf, count)
if n > 0:
self._pos = self._pos + uint32(n)
if self._pos > self._size:
self._size = self._pos
return n
def seek(self, offset: uint32) -> int:
if self._closed: return -1
res: int = seek(self._fd, offset)
if res == 0:
self._pos = offset
return res
def tell(self) -> uint32:
return self._pos
def size(self) -> uint32:
return self._size
def close(self) -> int:
if self._closed: return 0
self._closed = 1
return close(self._fd)
def is_closed(self) -> int:
return self._closed
class DirIO:
_dd: int
_closed: int
def __init__(self, dd: int):
self._dd = dd
self._closed = 0
def read(self, info: rawptr) -> int:
if self._closed: return -1
return readdir(self._dd, info)
def close(self) -> int:
if self._closed: return 0
self._closed = 1
return closedir(self._dd)
def is_closed(self) -> int:
return self._closed
def open_file(path: const str, flags: uint32) -> FileIO:
fd: int = open(path, flags)
if fd < 0: return None
return FileIO(fd, flags)
def open_dir(path: const str) -> DirIO:
dd: int = opendir(path)
if dd < 0: return None
return DirIO(dd)