1    let fopen (name : CString) (mode : CString) : CStream
2    let mkdir (path : CString) (mode : u32) : int
3    
4    object Fs =
5        def modified_time "kd_modified_time" (path : CString) : Time
6        let rename (from : CString) (to : CString) : int
7        def path_exists "kd_path_exists" (path : CString) : bool
8        def is_directory "kd_is_directory" (path : CString) : bool
9        let read_file "kd_read_file" (path : CString) (size : mut ptr u32) : MutPtr<u8>
10       def write_file "kd_write_file" (path : CString) (text : String)
11       def remove (path : CString) : int
12       let list_files "kd_list_files" (path : CString) : mem std/List<String>
13   
14   def modified_time (path : String) =
15       let c_path @owner = path.to_c_string
16       Fs.modified_time c_path
17   
18   def exists (path : String) =
19       let c_path @owner = path.to_c_string
20       Fs.path_exists c_path
21   
22   def is_directory (path : String) =
23       let c_path @owner = path.to_c_string
24       Fs.is_directory c_path
25   
26   let read_file (path : String) =
27        let c_path @owner = path.to_c_string
28        let mut size = 0
29        let ptr = Fs.read_file c_path size@mut_ptr
30        if ptr == null then
31            fail "cannot read $path"
32   
33        (ptr, size)
34   
35   def read_text (path : String) =
36       let (ptr, size) = read_file path
37       String ptr.as<Ptr<Char>> size
38   
39   def read_bytes (path : String) =
40       let (ptr, size) = read_file path
41       MutArray<u8>.from ptr size
42   
43   def write_file (path : String) (text : String) =
44       let c_path @owner = path.to_c_string
45       Fs.write_file c_path text
46   
47   def list_files (path : String) =
48       let c_path @owner = path.to_c_string
49       let std_paths : mem std/List<String> = Fs.list_files c_path
50       let size = std_paths.size as i32
51       let paths = List<String>.new
52       for i = 0 until size do
53           let s = std_paths[i]
54           paths.add s
55   
56       paths
57   
58   def remove_file (path : String) =
59       let c_path @owner = path.to_c_string
60       let result = Fs.remove c_path
61       assert result == 0
62   
63   def remove_directory (path : String) =
64       let c_path @owner = path.to_c_string
65       let result = Fs.remove c_path
66       assert result == 0
67   
68   def create_file (path : String) =
69       let c_path @owner = path.to_c_string
70       let ptr = fopen c_path "w"
71       let result = sys/fclose ptr
72       assert result == 0
73   
74   def create_path (path : String)  =
75       let c_path @owner = path.to_c_string
76       let result = mkdir c_path 493
77       assert result == 0
78   
79   def rename (from : String) (to : String) =
80       let c_from @owner = from.to_c_string
81       let c_to @owner = to.to_c_string
82   
83       let result = Fs.rename c_from c_to
84       assert result == 0
85