v / vlib / os
Raw file | 186 loc (155 sloc) | 4.1 KB | Latest commit hash 2083e6b04
1module os
2
3$if js_node {
4 #var $fs = require('fs');
5 #var $path = require('path');
6 #var tty = require('tty')
7}
8
9pub const (
10 path_delimiter = get_path_delimiter()
11 path_separator = get_path_separator()
12 args = []string{}
13)
14
15const executable_suffixes = ['']
16
17fn get_path_delimiter() string {
18 delimiter := ':'
19 $if js_node {
20 #delimiter.str = $path.delimiter
21 }
22 return delimiter
23}
24
25fn get_path_separator() string {
26 separator := '/'
27 $if js_node {
28 #separator.str = $path.sep
29 }
30 return separator
31}
32
33fn init() {
34 $if js_node {
35 #$process.argv.forEach(function(val,index) { os__args.arr[index] = new string(val); })
36 }
37}
38
39// real_path returns the full absolute path for fpath, with all relative ../../, symlinks and so on resolved.
40// See http://pubs.opengroup.org/onlinepubs/9699919799/functions/realpath.html
41// Also https://insanecoding.blogspot.com/2007/11/pathmax-simply-isnt.html
42// and https://insanecoding.blogspot.com/2007/11/implementing-realpath-in-c.html
43// Note: this particular rabbit hole is *deep* ...
44pub fn real_path(fpath string) string {
45 $if js_node {
46 mut res := ''
47 #res = new string( $fs.realpathSync(fpath))
48
49 return res
50 } $else {
51 return fpath
52 }
53}
54
55// flush will flush the stdout buffer.
56pub fn flush() {
57 $if js_node {
58 #$process.stdout.write('')
59 }
60}
61
62pub fn getpid() int {
63 res := 0
64 #res.val = $process.pid
65
66 return res
67}
68
69// chmod change file access attributes of `path` to `mode`.
70// Octals like `0o600` can be used.
71pub fn chmod(path string, mode int) ! {
72 $if js_node {
73 #try {
74 #$fs.chmodSync(''+path,mode.valueOf())
75 #} catch (error) {
76 #return error_with_code(new string("chmod failed: " + error.message),new int(error.code))
77 #}
78 } $else {
79 return error('os.chmod() is available only for NodeJS')
80 }
81}
82
83// chown changes the owner and group attributes of `path` to `owner` and `group`.
84// Octals like `0o600` can be used.
85pub fn chown(path string, owner int, group int) ! {
86 $if js_node {
87 #try {
88 #$fs.chownSync(''+path,owner.valueOf(),group.valueOf())
89 #} catch (error) { return error_with_code(new string("chown failed: " + error.message),new int(error.code)) }
90 } $else {
91 return error('os.chown() is available only for NodeJS')
92 }
93}
94
95pub fn temp_dir() string {
96 mut res := ''
97 $if js_node {
98 #res = new string($os.tmpdir())
99 }
100 return res
101}
102
103pub fn home_dir() string {
104 mut res := ''
105 $if js_node {
106 #res = new string($os.homedir())
107 }
108 return res
109}
110
111// join_path returns a path as string from input string parameter(s).
112pub fn join_path(base string, dirs ...string) string {
113 mut result := []string{}
114 result << base.trim_right('\\/')
115 for d in dirs {
116 result << d
117 }
118 mut path_sep := ''
119 #path_sep = $path.sep;
120
121 res := result.join(path_sep)
122 return res
123}
124
125pub fn join_path_single(base string, elem string) string {
126 // TODO: deprecate this
127 return join_path(base, elem)
128}
129
130pub fn execute(cmd string) Result {
131 mut exit_code := 0
132 mut stdout := ''
133 #let commands = cmd.str.split(' ');
134 #let output = $child_process.spawnSync(commands[0],commands.slice(1,commands.length));
135 #exit_code = new int(output.status)
136 #stdout = new string(output.stdout + '')
137
138 return Result{
139 exit_code: exit_code
140 output: stdout
141 }
142}
143
144pub fn system(cmd string) int {
145 exit_code := 0
146 #let commands = cmd.str.split(' ');
147 #exit_code.val = $child_process.execSync(commands[0],commands.slice(1,commands.length));
148
149 return exit_code
150}
151
152pub fn is_atty(fd int) int {
153 res := 0
154 #res.val = +tty.isatty(fd.val)
155
156 return res
157}
158
159pub fn glob(patterns ...string) ![]string {
160 panic('not yet implemented')
161 return error('not yet implemented')
162}
163
164pub fn write_file_array(path string, buffer array) ! {
165 mut f := create(path)!
166 f.write_array(buffer)!
167 f.close()
168}
169
170pub fn chdir(s string) ! {
171 #try { $process.chdir(s.str); } catch (e) { return error(new string('' + s)) }
172}
173
174pub fn file_last_mod_unix(path string) int {
175 mtime := 0
176 #mtime.val = Math.floor($fs.lstatSync(path.str).mtime.getTime() / 1000)
177
178 return mtime
179}
180
181pub fn ensure_folder_is_writable(path string) ! {
182 fpath := join_path(path, 'some_newfile')
183 #try { $fs.writeFileSync(fpath); $fs.unlinkSync(fpath) } catch(e) { return error(new string('could not write to ' + path)) }
184
185 _ := fpath
186}