Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/fs/access_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async test "exists" {
inspect(
try? ignore(@fs.open("moon.mod.json.not_exist", mode=ReadOnly)),
content=(
#|Err(OSError("@fs.open(): No such file or directory"))
#|Err(OSError("@fs.open(): \"moon.mod.json.not_exist\": No such file or directory"))
),
)
}
Expand Down
8 changes: 6 additions & 2 deletions src/fs/dir_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
async test "read_all" {
let dir = @fs.opendir("src/fs")
defer dir.close()
let list = dir.read_all()
let list = dir
.read_all()
.filter_map(x => if x is "target" { None } else { Some(x) })
list.sort()
@json.inspect(list, content=[
"fs.mbt", "stub.c", "dir.mbt", "README.md", "utils.mbt", "dir_test.mbt", "eof_test.mbt",
Expand All @@ -33,7 +35,9 @@ async test "as_dir" {
guard dir_file.kind() is Directory
let dir = dir_file.as_dir()
defer dir.close()
let list = dir.read_all()
let list = dir
.read_all()
.filter_map(x => if x is "target" { None } else { Some(x) })
list.sort()
let kinds = []
for file in list {
Expand Down
74 changes: 57 additions & 17 deletions src/internal/event_loop/fs.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@ pub async fn open(
mode~ : Int,
context~ : String,
) -> Int {
let path = @encoding/utf8.encode(path)
perform_job_in_worker(JobForWorker::open(path, flags, mode), context~)
let path_bytes = @encoding/utf8.encode(path)
let job = JobForWorker::open(path_bytes, flags, mode)
perform_job_in_worker(job, context~) catch {
@os_error.OSError(errno, context~) =>
raise @os_error.OSError(errno, context="\{context}: \{repr(path)}")
err => raise err
}
}

///|
Expand All @@ -37,19 +42,27 @@ pub async fn stat(
follow_symlink~ : Bool,
context~ : String,
) -> @fd_util.Stat {
let path = @encoding/utf8.encode(path)
let path_bytes = @encoding/utf8.encode(path)
let stat = @fd_util.Stat::new()
let _ = perform_job_in_worker(
JobForWorker::stat(path, stat, follow_symlink~),
context~,
)
let job = JobForWorker::stat(path_bytes, stat, follow_symlink~)
try perform_job_in_worker(job, context~) catch {
@os_error.OSError(errno, context~) =>
raise @os_error.OSError(errno, context="\{context}: \{repr(path)}")
err => raise err
} noraise {
_ => ()
}
stat
}

///|
pub async fn access(path : StringView, amode~ : Int, context~ : String) -> Int {
let path = @encoding/utf8.encode(path)
perform_job_in_worker(JobForWorker::access(path, amode), context~)
let path_bytes = @encoding/utf8.encode(path)
perform_job_in_worker(JobForWorker::access(path_bytes, amode), context~) catch {
@os_error.OSError(errno, context~) =>
raise @os_error.OSError(errno, context="\{context}: \{repr(path)}")
err => raise err
}
}

///|
Expand All @@ -59,20 +72,40 @@ pub async fn fsync(fd : Int, only_data~ : Bool, context~ : String) -> Unit {

///|
pub async fn remove(path : StringView, context~ : String) -> Unit {
let path = @encoding/utf8.encode(path)
perform_job_in_worker(JobForWorker::remove(path), context~) |> ignore
let path_bytes = @encoding/utf8.encode(path)
try perform_job_in_worker(JobForWorker::remove(path_bytes), context~) catch {
@os_error.OSError(errno, context~) =>
raise @os_error.OSError(errno, context="\{context}: \{repr(path)}")
err => raise err
} noraise {
_ => ()
}
}

///|
pub async fn mkdir(path : StringView, mode~ : Int, context~ : String) -> Unit {
let path = @encoding/utf8.encode(path)
perform_job_in_worker(JobForWorker::mkdir(path, mode), context~) |> ignore
let path_bytes = @encoding/utf8.encode(path)
try
perform_job_in_worker(JobForWorker::mkdir(path_bytes, mode), context~)
catch {
@os_error.OSError(errno, context~) =>
raise @os_error.OSError(errno, context="\{context}: \{repr(path)}")
err => raise err
} noraise {
_ => ()
}
}

///|
pub async fn rmdir(path : StringView, context~ : String) -> Unit {
let path = @encoding/utf8.encode(path)
perform_job_in_worker(JobForWorker::rmdir(path), context~) |> ignore
let path_bytes = @encoding/utf8.encode(path)
try perform_job_in_worker(JobForWorker::rmdir(path_bytes), context~) catch {
@os_error.OSError(errno, context~) =>
raise @os_error.OSError(errno, context="\{context}: \{repr(path)}")
err => raise err
} noraise {
_ => ()
}
}

///|
Expand All @@ -87,9 +120,16 @@ pub async fn readdir(dir : Directory, context~ : String) -> DirectoryEntry {

///|
pub async fn realpath(path : StringView, context~ : String) -> Bytes {
let path = @encoding/utf8.encode(path)
let path_bytes = @encoding/utf8.encode(path)
let out = @ref.new(@c_buffer.null)
let _ = perform_job_in_worker(JobForWorker::realpath(path, out), context~)
let job = JobForWorker::realpath(path_bytes, out)
try perform_job_in_worker(job, context~) catch {
@os_error.OSError(errno, context~) =>
raise @os_error.OSError(errno, context="\{context}: \{repr(path)}")
err => raise err
} noraise {
_ => ()
}
let c_path = out.val
defer c_path.free()
let len = c_path.strlen()
Expand Down
10 changes: 7 additions & 3 deletions src/internal/event_loop/network.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,17 @@ pub async fn getaddrinfo(
host : StringView,
context~ : String,
) -> Result[AddrInfo, String] {
let host = @encoding/utf8.encode(host)
let host_bytes = @encoding/utf8.encode(host)
let out = AddrInfoRef::new()
let ret = perform_job_in_worker(
JobForWorker::getaddrinfo(host, out),
JobForWorker::getaddrinfo(host_bytes, out),
allow_cancel=true,
context~,
)
) catch {
@os_error.OSError(errno, context~) =>
raise @os_error.OSError(errno, context="\{context}: \{repr(host)}")
err => raise err
}
if ret != 0 {
let c_str = gai_strerror(ret)
let len = c_str.strlen()
Expand Down