v / vlib / os
Raw file | 191 loc (160 sloc) | 3.14 KB | Latest commit hash 3aeb6179b
1module os
2
3pub fn mkdir(path string, params MkdirParams) ! {
4 $if js_node {
5 if path == '.' {
6 return
7 }
8 #$fs.mkdirSync(path.valueOf())
9
10 return
11 } $else {
12 return error('could not create folder')
13 }
14}
15
16pub fn is_dir(path string) bool {
17 res := false
18 $if js_node {
19 #res.val = $fs.existsSync(path,str) && $fs.lstatSync(path.str).isDirectory()
20 }
21 return res
22}
23
24pub fn is_link(path string) bool {
25 res := false
26 $if js_node {
27 #res.val = $fs.existsSync(path.str) && $fs.lstatSync(path.str).isSymbolicLink()
28 }
29 return res
30}
31
32struct PathKind {
33 is_dir bool
34 is_link bool
35}
36
37fn kind_of_existing_path(path string) PathKind {
38 is_link := false
39 is_dir := false
40 $if js_node {
41 #is_link.val = $fs.existsSync(path.str) && $fs.lstatSync(path.str).isSymbolicLink()
42 #is_dir.val = $fs.existsSync(path,str) && $fs.lstatSync(path.str).isDirectory()
43 }
44 return PathKind{
45 is_dir: is_dir
46 is_link: is_link
47 }
48}
49
50pub fn exists(path string) bool {
51 res := false
52 $if js_node {
53 #res.val = $fs.existsSync(path.str)
54 }
55 return res
56}
57
58pub fn ls(path string) ![]string {
59 if !is_dir(path) {
60 return error('ls(): cannot open dir ${dir}')
61 }
62
63 result := []string{}
64 $if js_node {
65 #let i = 0
66 #$fs.readdirSync(path.str).forEach((path) => result.arr[i++] = new string(path))
67 }
68 return result
69}
70
71pub fn get_raw_line() string {
72 return ''
73}
74
75pub fn executable() string {
76 return ''
77}
78
79pub fn is_executable(path string) bool {
80 eprintln('TODO: There is no isExecutable on fs.stats')
81 return false
82}
83
84pub fn rmdir(path string) ! {
85 $if js_node {
86 err := ''
87 #try {
88 #$fs.rmdirSync(path.str)
89 #return;
90 #} catch (e) {
91 #err.str = 'Failed to remove "' + path.str + '": ' + e.toString()
92 #}
93
94 return error(err)
95 }
96}
97
98pub fn rm(path string) ! {
99 $if js_node {
100 err := ''
101 #try {
102 #$fs.rmSync(path.str)
103 #return;
104 #} catch (e) {
105 #err.str = 'Failed to remove "' + path.str + '": ' + e.toString()
106 #}
107
108 return error(err)
109 }
110}
111
112pub fn cp(src string, dst string) ! {
113 $if js_node {
114 err := ''
115 #try {
116 #$fs.cpSync(src.str,dst.str);
117 #return;
118 #} catch (e) {
119 #err.str = 'failed to copy ' + src.str + ' to ' + dst.str + ': ' + e.toString();
120 #}
121
122 return error(err)
123 }
124}
125
126pub fn rename(src string, dst string) ! {
127 $if js_node {
128 err := ''
129 #try {
130 #$fs.renameSync(src.str,dst.str);
131 #return;
132 #} catch (e) {
133 #err.str = 'failed to rename ' + src.str + ' to ' + dst.str + ': ' + e.toString();
134 #}
135
136 return error(err)
137 }
138}
139
140pub fn read_file(s string) !string {
141 mut err := ''
142 err = err
143 res := ''
144 #try {
145 #res.str = $fs.readFileSync(s.str).toString()
146 #} catch (e) {
147 #err.str = 'Failed to read file: ' + e.toString()
148 #return error(err)
149 #}
150
151 return res
152}
153
154pub fn getwd() string {
155 res := ''
156 #res.str = $process.cwd()
157
158 return res
159}
160
161pub fn getuid() int {
162 res := 0
163 #if (process.getuid) res.val = process.getuid();
164
165 return res
166}
167
168pub fn execvp(cmd string, args []string) ! {
169 panic('os.execvp() is not available on JS backend')
170}
171
172pub fn stdin_resume() {
173 #$process.stdin.resume();
174}
175
176pub fn is_readable(path string) bool {
177 $if js_node {
178 res := false
179 #try { res.val = $fs.accessSync(path.str,$fs.constants.R_OK); } catch { res.val = false; }
180
181 return res
182 } $else {
183 return false
184 }
185}
186
187// get_long_path has no meaning for *nix, but has for windows, where `c:\folder\some~1` for example
188// can be the equivalent of `c:\folder\some spa ces`. On *nix, it just returns a copy of the input path.
189fn get_long_path(path string) !string {
190 return path
191}