Files
ViperOS/vpsdk/fs.vp
T
TermiNexus 5b4bab59cc chore(ViperOS): 合并落库内核侧在途改动并移除 vpsdk wiki 拷贝
VKernel / vpsdk / Apps / Libs 共 30 个源文件:内核与 SDK 侧的新语法适配及编译修正(含 bootinfo.vp / main.vp 位宽改 uint64、gfx.vp 前向引用改指针、devfs.vp 指针解引用赋值、elf.vp 跨模块函数取值等)。删除 vpsdk/wiki/*.md(14 文件,约 -8100 行):与仓库根 wiki 重复的拷贝,统一以根 wiki 为唯一来源。
2026-10-07 19:04:33 +08:00

142 lines
3.9 KiB
Plaintext

import t, c
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: t.CPtr, count: uint32) -> int:
return int(syscall._syscall3(uint64(syscall.READ), uint64(fd), uint64(buf), uint64(count)))
def write(fd: int, buf: t.CPtr, 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: t.CPtr) -> 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: t.CPtr) -> int:
return int(syscall._syscall3(uint64(syscall.STAT), uint64(path), uint64(info), 0))
@t.Object
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: t.CPtr, 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: t.CPtr, 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
@t.Object
class DirIO:
_dd: int
_closed: int
def __init__(self, dd: int):
self._dd = dd
self._closed = 0
def read(self, info: t.CPtr) -> 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)