fs.oak
← Standard library
See on GitHub ↗
ReadBufSize := 4096
fn readFileSync(path) {
evt := open(path, :readonly)
if evt.type {
:error -> ?
_ -> {
fd := evt.fd
fn sub(file, offset) {
evt := read(fd, offset, ReadBufSize)
if evt.type {
:error -> {
close(fd)
?
}
_ -> if len(evt.data) {
ReadBufSize -> sub(
file << evt.data
offset + ReadBufSize
)
_ -> {
close(fd)
file << evt.data
}
}
}
}
sub('', 0)
}
}
}
fn readFileAsync(path, withFile) with open(path, :readonly) fn(evt) if evt.type {
:error -> withFile(?)
_ -> {
fd := evt.fd
fn sub(file, offset) with read(fd, offset, ReadBufSize) fn(evt) if evt.type {
:error -> with close(fd) fn {
withFile(?)
}
_ -> if len(evt.data) {
ReadBufSize -> sub(
file << evt.data
offset + ReadBufSize
)
_ -> with close(fd) fn {
withFile(file << evt.data)
}
}
}
sub('', 0)
}
}
fn readFile(path, withFile) if withFile {
? -> readFileSync(path)
_ -> readFileAsync(path, withFile)
}
fn writeFileSyncWithFlag(path, file, flag) {
evt := open(path, flag)
if evt.type {
:error -> ?
_ -> {
fd := evt.fd
{
evt := write(fd, 0, file)
close(fd)
if evt.type {
:error -> ?
_ -> true
}
}
}
}
}
fn writeFileAsyncWithFlag(path, file, flag, withEnd) with open(path, flag) fn(evt) if evt.type {
:error -> withEnd(?)
_ -> with write(fd := evt.fd, 0, file) fn(evt) if evt.type {
:error -> with close(fd) fn {
withEnd(?)
}
_ -> with close(fd) fn {
withEnd(true)
}
}
}
fn writeFile(path, file, withEnd) if withEnd {
? -> writeFileSyncWithFlag(path, file, :truncate)
_ -> writeFileAsyncWithFlag(path, file, :truncate, withEnd)
}
fn appendFile(path, file, withEnd) if withEnd {
? -> writeFileSyncWithFlag(path, file, :append)
_ -> writeFileAsyncWithFlag(path, file, :append, withEnd)
}
fn statFileSync(path) {
evt := stat(path)
if evt.type {
:error -> ?
_ -> evt.data
}
}
fn statFileAsync(path, withStat) with stat(path) fn(evt) if evt.type {
:error -> withStat(?)
_ -> withStat(evt.data)
}
fn statFile(path, withStat) if withStat {
? -> statFileSync(path)
_ -> statFileAsync(path, withStat)
}
fn listFilesSync(path) {
evt := ls(path)
if evt.type {
:error -> ?
_ -> evt.data
}
}
fn listFilesAsync(path, withFiles) with ls(path) fn(evt) if evt.type {
:error -> withFiles(?)
_ -> withFiles(evt.data)
}
fn listFiles(path, withFiles) if withFiles {
? -> listFilesSync(path)
_ -> listFilesAsync(path, withFiles)
}