Skip to content

Commit 5e0cdae

Browse files
committed
move file stat utils to @fd_util
1 parent ea9ba79 commit 5e0cdae

File tree

10 files changed

+47
-55
lines changed

10 files changed

+47
-55
lines changed

src/fs/dir.mbt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ pub async fn rmdir(path : StringView, recursive? : Bool = false) -> Unit {
3131
let base = path.to_string()
3232
for file in readdir(base) {
3333
let child_path = base + "/" + file
34-
let stat = Stat::from_path(child_path, follow_symlink=false, context~)
35-
if stat.kind() is Directory {
34+
let stat = @event_loop.stat(child_path, follow_symlink=false, context~)
35+
if FileKind::from_int(stat.kind()) is Directory {
3636
rmdir(child_path, recursive=true)
3737
} else {
3838
@event_loop.remove(child_path, context~)

src/fs/dir_test.mbt

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,7 @@ async test "read_all" {
1818
defer dir.close()
1919
let list = dir.read_all()
2020
list.sort()
21-
@json.inspect(list, content=[
22-
"fs.mbt", "stub.c", "dir.mbt", "stat.mbt", "README.md", "utils.mbt", "dir_test.mbt",
23-
"eof_test.mbt", "README.mbt.md", "moon.pkg.json", "seek_test.mbt", "stat_test.mbt",
24-
"walk_test.mbt", "mkdir_test.mbt", "access_test.mbt", "create_test.mbt", "read_all_test.mbt",
25-
"realpath_test.mbt", "pkg.generated.mbti", "text_file_test.mbt", "timestamp_test.mbt",
26-
])
21+
@json.inspect(list, content=(["fs.mbt","stub.c","dir.mbt","README.md","utils.mbt","dir_test.mbt","eof_test.mbt","README.mbt.md","moon.pkg.json","seek_test.mbt","stat_test.mbt","walk_test.mbt","mkdir_test.mbt","access_test.mbt","create_test.mbt","read_all_test.mbt","realpath_test.mbt","pkg.generated.mbti","text_file_test.mbt","timestamp_test.mbt"]))
2722
}
2823

2924
///|
@@ -46,13 +41,5 @@ async test "as_dir" {
4641
assert_eq(kind, @fs.kind(path))
4742
kinds.push("\{file}: \{kind}")
4843
}
49-
@json.inspect(kinds, content=[
50-
"fs.mbt: Regular", "stub.c: Regular", "dir.mbt: Regular", "stat.mbt: Regular",
51-
"README.md: Regular", "utils.mbt: Regular", "dir_test.mbt: Regular", "eof_test.mbt: Regular",
52-
"README.mbt.md: Regular", "moon.pkg.json: Regular", "seek_test.mbt: Regular",
53-
"stat_test.mbt: Regular", "walk_test.mbt: Regular", "mkdir_test.mbt: Regular",
54-
"access_test.mbt: Regular", "create_test.mbt: Regular", "read_all_test.mbt: Regular",
55-
"realpath_test.mbt: Regular", "pkg.generated.mbti: Regular", "text_file_test.mbt: Regular",
56-
"timestamp_test.mbt: Regular",
57-
])
44+
@json.inspect(kinds, content=(["fs.mbt: Regular","stub.c: Regular","dir.mbt: Regular","README.md: Regular","utils.mbt: Regular","dir_test.mbt: Regular","eof_test.mbt: Regular","README.mbt.md: Regular","moon.pkg.json: Regular","seek_test.mbt: Regular","stat_test.mbt: Regular","walk_test.mbt: Regular","mkdir_test.mbt: Regular","access_test.mbt: Regular","create_test.mbt: Regular","read_all_test.mbt: Regular","realpath_test.mbt: Regular","pkg.generated.mbti: Regular","text_file_test.mbt: Regular","timestamp_test.mbt: Regular"]))
5845
}

src/fs/fs.mbt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
///|
16+
using @fd_util {type Stat}
17+
1518
///|
1619
pub(all) enum FileKind {
1720
Unknown = 0
@@ -24,6 +27,9 @@ pub(all) enum FileKind {
2427
CharDevice = 7
2528
} derive(Eq, Show)
2629

30+
///|
31+
fn FileKind::from_int(x : Int) -> FileKind = "%identity"
32+
2733
///|
2834
#valtype
2935
struct File {
@@ -115,7 +121,8 @@ pub async fn open(
115121
mode=user_mode,
116122
context="@fs.open()",
117123
)
118-
{ fd, kind: Stat::from_fd(fd, context="@fs.open()").kind() }
124+
let kind = FileKind::from_int(Stat::from_fd(fd, context="@fs.open()").kind())
125+
{ fd, kind }
119126
}
120127

121128
///|

src/fs/moon.pkg.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"moonbitlang/async/os_error",
44
"moonbitlang/async/io",
55
"moonbitlang/async/internal/event_loop",
6+
"moonbitlang/async/internal/fd_util",
67
"moonbitlang/async",
78
"moonbitlang/async/semaphore"
89
],

src/fs/utils.mbt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
/// If `path` is a symbolic link and `follow_symlink` is `true` (`true` by default),
1818
/// the kind of the target of the link will be returned.
1919
pub async fn kind(path : StringView, follow_symlink? : Bool = true) -> FileKind {
20-
Stat::from_path(path, follow_symlink~, context="@fs.kind()").kind()
20+
@event_loop.stat(path, follow_symlink~, context="@fs.kind()").kind()
21+
|> FileKind::from_int
2122
}
2223

2324
///|
@@ -31,7 +32,7 @@ pub async fn atime(
3132
path : StringView,
3233
follow_symlink? : Bool = true,
3334
) -> (Int64, Int) {
34-
Stat::from_path(path, follow_symlink~, context="@fs.atime()").atime()
35+
@event_loop.stat(path, follow_symlink~, context="@fs.atime()").atime()
3536
}
3637

3738
///|
@@ -45,7 +46,7 @@ pub async fn mtime(
4546
path : StringView,
4647
follow_symlink? : Bool = true,
4748
) -> (Int64, Int) {
48-
Stat::from_path(path, follow_symlink~, context="@fs.mtime()").mtime()
49+
@event_loop.stat(path, follow_symlink~, context="@fs.mtime()").mtime()
4950
}
5051

5152
///|
@@ -59,7 +60,7 @@ pub async fn ctime(
5960
path : StringView,
6061
follow_symlink? : Bool = true,
6162
) -> (Int64, Int) {
62-
Stat::from_path(path, follow_symlink~, context="@fs.ctime()").ctime()
63+
@event_loop.stat(path, follow_symlink~, context="@fs.ctime()").ctime()
6364
}
6465

6566
///|

src/internal/event_loop/fs.mbt

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,14 @@ pub async fn open(
3131
perform_job_in_worker(JobForWorker::open(path, flags, mode), context~)
3232
}
3333

34-
///|
35-
struct Stat(FixedArray[Byte])
36-
37-
///|
38-
extern "C" fn sizeof_stat() -> Int = "moonbitlang_async_sizeof_stat"
39-
40-
///|
41-
pub fn Stat::new() -> Stat {
42-
FixedArray::make(sizeof_stat(), b'\x00')
43-
}
44-
4534
///|
4635
pub async fn stat(
4736
path : StringView,
4837
follow_symlink~ : Bool,
4938
context~ : String,
50-
) -> Stat {
39+
) -> @fd_util.Stat {
5140
let path = @encoding/utf8.encode(path)
52-
let stat = Stat::new()
41+
let stat = @fd_util.Stat::new()
5342
let _ = perform_job_in_worker(
5443
JobForWorker::stat(path, stat, follow_symlink~),
5544
context~,

src/internal/event_loop/pkg.generated.mbti

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// Generated using `moon info`, DON'T EDIT IT
22
package "moonbitlang/async/internal/event_loop"
33

4+
import(
5+
"moonbitlang/async/internal/fd_util"
6+
)
7+
48
// Values
59
async fn accept(Int, Bytes, context~ : String) -> Int
610

@@ -38,7 +42,7 @@ async fn sleep(Int) -> Unit
3842

3943
async fn spawn(Bytes, FixedArray[Bytes?], env~ : FixedArray[Bytes?], stdin~ : Int, stdout~ : Int, stderr~ : Int, cwd~ : Bytes?, context~ : String) -> Int
4044

41-
async fn stat(StringView, follow_symlink~ : Bool, context~ : String) -> Stat
45+
async fn stat(StringView, follow_symlink~ : Bool, context~ : String) -> @fd_util.Stat
4246

4347
async fn wait_pid(Int) -> Unit
4448

@@ -58,9 +62,6 @@ pub type Directory
5862
#external
5963
pub type DirectoryEntry
6064

61-
type Stat
62-
fn Stat::new() -> Self
63-
6465
// Type aliases
6566

6667
// Traits

src/internal/event_loop/thread_pool.mbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ extern "C" fn JobForWorker::open(
7878
#owned(path, out)
7979
extern "C" fn JobForWorker::stat(
8080
path : Bytes,
81-
out : Stat,
81+
out : @fd_util.Stat,
8282
follow_symlink~ : Bool,
8383
) -> JobForWorker = "moonbitlang_async_make_stat_job"
8484

src/internal/fd_util/pkg.generated.mbti

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ fn set_nonblocking(Int, context~ : String) -> Unit raise
1717
// Errors
1818

1919
// Types and methods
20+
type Stat
21+
fn Stat::atime(Self) -> (Int64, Int)
22+
fn Stat::ctime(Self) -> (Int64, Int)
23+
fn Stat::from_fd(Int, context~ : String) -> Self raise
24+
fn Stat::kind(Self) -> Int
25+
fn Stat::mtime(Self) -> (Int64, Int)
26+
fn Stat::new() -> Self
2027

2128
// Type aliases
2229

src/fs/stat.mbt renamed to src/internal/fd_util/stat.mbt

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,32 @@
1313
// limitations under the License.
1414

1515
///|
16-
typealias @event_loop.Stat
16+
struct Stat(FixedArray[Byte])
17+
18+
///|
19+
extern "C" fn sizeof_stat() -> Int = "moonbitlang_async_sizeof_stat"
20+
21+
///|
22+
pub fn Stat::new() -> Stat {
23+
FixedArray::make(sizeof_stat(), b'\x00')
24+
}
1725

1826
///|
1927
#borrow(stat)
2028
extern "C" fn Stat::from_fd_ffi(fd : Int, stat : Stat) -> Int = "fstat"
2129

2230
///|
23-
fn Stat::from_fd(fd : Int, context~ : String) -> Stat raise {
31+
pub fn Stat::from_fd(fd : Int, context~ : String) -> Stat raise {
2432
let stat = Stat::new()
2533
if Stat::from_fd_ffi(fd, stat) < 0 {
2634
@os_error.check_errno(context)
2735
}
2836
stat
2937
}
3038

31-
///|
32-
async fn Stat::from_path(
33-
path : StringView,
34-
follow_symlink~ : Bool,
35-
context~ : String,
36-
) -> Stat {
37-
@event_loop.stat(path, follow_symlink~, context~)
38-
}
39-
4039
///|
4140
#borrow(stat)
42-
extern "C" fn Stat::kind(stat : Stat) -> FileKind = "moonbitlang_async_file_kind_from_stat"
41+
pub extern "C" fn Stat::kind(stat : Stat) -> Int = "moonbitlang_async_file_kind_from_stat"
4342

4443
///|
4544
#borrow(stat, sec_out, nsec_out)
@@ -50,7 +49,7 @@ extern "C" fn Stat::atime_ffi(
5049
) = "moonbitlang_async_atime_from_stat"
5150

5251
///|
53-
fn Stat::atime(stat : Stat) -> (Int64, Int) {
52+
pub fn Stat::atime(stat : Stat) -> (Int64, Int) {
5453
let sec = @ref.new(0L)
5554
let nsec = @ref.new(0)
5655
stat.atime_ffi(sec, nsec)
@@ -66,7 +65,7 @@ extern "C" fn Stat::mtime_ffi(
6665
) = "moonbitlang_async_mtime_from_stat"
6766

6867
///|
69-
fn Stat::mtime(stat : Stat) -> (Int64, Int) {
68+
pub fn Stat::mtime(stat : Stat) -> (Int64, Int) {
7069
let sec = @ref.new(0L)
7170
let nsec = @ref.new(0)
7271
stat.mtime_ffi(sec, nsec)
@@ -82,7 +81,7 @@ extern "C" fn Stat::ctime_ffi(
8281
) = "moonbitlang_async_ctime_from_stat"
8382

8483
///|
85-
fn Stat::ctime(stat : Stat) -> (Int64, Int) {
84+
pub fn Stat::ctime(stat : Stat) -> (Int64, Int) {
8685
let sec = @ref.new(0L)
8786
let nsec = @ref.new(0)
8887
stat.ctime_ffi(sec, nsec)

0 commit comments

Comments
 (0)