/
os_nix.c.v
534 lines (471 loc) · 11.8 KB
/
os_nix.c.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
module os
import strings
#include <dirent.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/utsname.h>
#include <sys/types.h>
#include <utime.h>
// path_separator is the platform specific separator string, used between the folders
// and filenames in a path. It is '/' on POSIX, and '\\' on Windows.
pub const path_separator = '/'
// path_delimiter is the platform specific delimiter string, used between the paths
// in environment variables like PATH. It is ':' on POSIX, and ';' on Windows.
pub const path_delimiter = ':'
// path_devnull is a platform-specific file path of the null device.
// It is '/dev/null' on POSIX, and r'\\.\nul' on Windows.
pub const path_devnull = '/dev/null'
const executable_suffixes = ['']
const stdin_value = 0
const stdout_value = 1
const stderr_value = 2
// (Must be realized in Syscall) (Must be specified)
// ref: http://www.ccfit.nsu.ru/~deviv/courses/unix/unix/ng7c229.html
pub const s_ifmt = 0xF000 // type of file
pub const s_ifdir = 0x4000 // directory
pub const s_ifreg = 0x8000 // regular file
pub const s_iflnk = 0xa000 // link
pub const s_isuid = 0o4000 // SUID
pub const s_isgid = 0o2000 // SGID
pub const s_isvtx = 0o1000 // Sticky
pub const s_irusr = 0o0400 // Read by owner
pub const s_iwusr = 0o0200 // Write by owner
pub const s_ixusr = 0o0100 // Execute by owner
pub const s_irgrp = 0o0040 // Read by group
pub const s_iwgrp = 0o0020 // Write by group
pub const s_ixgrp = 0o0010 // Execute by group
pub const s_iroth = 0o0004 // Read by others
pub const s_iwoth = 0o0002 // Write by others
pub const s_ixoth = 0o0001
fn C.utime(&char, voidptr) int
fn C.uname(name voidptr) int
fn C.symlink(&char, &char) int
fn C.link(&char, &char) int
fn C.gethostname(&char, int) int
// Note: not available on Android fn C.getlogin_r(&char, int) int
fn C.getlogin() &char
fn C.getppid() int
fn C.getgid() int
fn C.getegid() int
enum GlobMatch {
exact
ends_with
starts_with
start_and_ends_with
contains
any
}
fn glob_match(dir string, pattern string, next_pattern string, mut matches []string) []string {
mut subdirs := []string{}
if is_file(dir) {
return subdirs
}
mut files := ls(dir) or { return subdirs }
mut mode := GlobMatch.exact
mut pat := pattern
if pat == '*' {
mode = GlobMatch.any
if next_pattern != pattern && next_pattern != '' {
for file in files {
if is_dir('${dir}/${file}') {
subdirs << '${dir}/${file}'
}
}
return subdirs
}
}
if pat == '**' {
files = walk_ext(dir, '')
pat = next_pattern
}
if pat.starts_with('*') {
mode = .ends_with
pat = pat[1..]
}
if pat.ends_with('*') {
mode = if mode == .ends_with { GlobMatch.contains } else { GlobMatch.starts_with }
pat = pat[..pat.len - 1]
}
if pat.contains('*') {
mode = .start_and_ends_with
}
for file in files {
mut fpath := file
f := if file.contains(os.path_separator) {
pathwalk := file.split(os.path_separator)
pathwalk[pathwalk.len - 1]
} else {
fpath = if dir == '.' { file } else { '${dir}/${file}' }
file
}
if f in ['.', '..'] || f == '' {
continue
}
hit := match mode {
.any {
true
}
.exact {
f == pat
}
.starts_with {
f.starts_with(pat)
}
.ends_with {
f.ends_with(pat)
}
.start_and_ends_with {
p := pat.split('*')
f.starts_with(p[0]) && f.ends_with(p[1])
}
.contains {
f.contains(pat)
}
}
if hit {
if is_dir(fpath) {
subdirs << fpath
if next_pattern == pattern && next_pattern != '' {
matches << '${fpath}${os.path_separator}'
}
} else {
matches << fpath
}
}
}
return subdirs
}
fn native_glob_pattern(pattern string, mut matches []string) ! {
steps := pattern.split(os.path_separator)
mut cwd := if pattern.starts_with(os.path_separator) { os.path_separator } else { '.' }
mut subdirs := [cwd]
for i := 0; i < steps.len; i++ {
step := steps[i]
step2 := if i + 1 == steps.len { step } else { steps[i + 1] }
if step == '' {
continue
}
if is_dir('${cwd}${os.path_separator}${step}') {
dd := if cwd == '/' {
step
} else {
if cwd == '.' || cwd == '' {
step
} else {
if step == '.' || step == '/' { cwd } else { '${cwd}/${step}' }
}
}
if i + 1 != steps.len {
if dd !in subdirs {
subdirs << dd
}
}
}
mut subs := []string{}
for sd in subdirs {
d := if cwd == '/' {
sd
} else {
if cwd == '.' || cwd == '' {
sd
} else {
if sd == '.' || sd == '/' { cwd } else { '${cwd}/${sd}' }
}
}
subs << glob_match(d.replace('//', '/'), step, step2, mut matches)
}
subdirs = subs.clone()
}
}
pub fn utime(path string, actime int, modtime int) ! {
mut u := C.utimbuf{actime, modtime}
if C.utime(&char(path.str), voidptr(&u)) != 0 {
return error_with_code(posix_get_error_msg(C.errno), C.errno)
}
}
// uname returns information about the platform on which the program is running
// For example:
// os.Uname{
// sysname: 'Linux'
// nodename: 'nemesis'
// release: '5.15.0-57-generic'
// version: '#63~20.04.1-Ubuntu SMP Wed Nov 30 13:40:16 UTC 2022'
// machine: 'x86_64'
// }
// where the fields have the following meaning:
// sysname is the name of this implementation of the operating system
// nodename is the name of this node within an implementation-dependent communications network
// release is the current release level of this implementation
// version is the current version level of this release
// machine is the name of the hardware type, on which the system is running
// See also https://pubs.opengroup.org/onlinepubs/7908799/xsh/sysutsname.h.html
pub fn uname() Uname {
mut u := Uname{}
utsize := sizeof(C.utsname)
unsafe {
x := malloc_noscan(int(utsize))
d := &C.utsname(x)
if C.uname(d) == 0 {
u.sysname = cstring_to_vstring(d.sysname)
u.nodename = cstring_to_vstring(d.nodename)
u.release = cstring_to_vstring(d.release)
u.version = cstring_to_vstring(d.version)
u.machine = cstring_to_vstring(d.machine)
}
free(d)
}
return u
}
pub fn hostname() !string {
mut hstnme := ''
size := 256
mut buf := unsafe { &char(malloc_noscan(size)) }
if C.gethostname(buf, size) == 0 {
hstnme = unsafe { cstring_to_vstring(buf) }
unsafe { free(buf) }
return hstnme
}
return error(posix_get_error_msg(C.errno))
}
pub fn loginname() !string {
x := C.getlogin()
if !isnil(x) {
return unsafe { cstring_to_vstring(x) }
}
return error(posix_get_error_msg(C.errno))
}
fn init_os_args(argc int, argv &&u8) []string {
mut args_ := []string{len: argc}
for i in 0 .. argc {
args_[i] = unsafe { tos_clone(argv[i]) }
}
return args_
}
pub fn ls(path string) ![]string {
if path.len == 0 {
return error('ls() expects a folder, not an empty string')
}
mut res := []string{cap: 50}
dir := unsafe { C.opendir(&char(path.str)) }
if isnil(dir) {
return error('ls() couldnt open dir "${path}"')
}
mut ent := &C.dirent(unsafe { nil })
// mut ent := &C.dirent{!}
for {
ent = C.readdir(dir)
if isnil(ent) {
break
}
unsafe {
bptr := &u8(&ent.d_name[0])
if bptr[0] == 0 || (bptr[0] == `.` && bptr[1] == 0)
|| (bptr[0] == `.` && bptr[1] == `.` && bptr[2] == 0) {
continue
}
res << tos_clone(bptr)
}
}
C.closedir(dir)
return res
}
// mkdir creates a new directory with the specified path.
pub fn mkdir(path string, params MkdirParams) ! {
if path == '.' {
return
}
apath := real_path(path)
r := unsafe { C.mkdir(&char(apath.str), params.mode) }
if r == -1 {
return error(posix_get_error_msg(C.errno))
}
}
// execute starts the specified command, waits for it to complete, and returns its output.
@[manualfree]
pub fn execute(cmd string) Result {
// if cmd.contains(';') || cmd.contains('&&') || cmd.contains('||') || cmd.contains('\n') {
// return Result{ exit_code: -1, output: ';, &&, || and \\n are not allowed in shell commands' }
// }
pcmd := if cmd.contains('2>') { cmd.clone() } else { '${cmd} 2>&1' }
defer {
unsafe { pcmd.free() }
}
f := vpopen(pcmd)
if isnil(f) {
return Result{
exit_code: -1
output: 'exec("${cmd}") failed'
}
}
fd := fileno(f)
mut res := strings.new_builder(1024)
defer {
unsafe { res.free() }
}
buf := [4096]u8{}
unsafe {
pbuf := &buf[0]
for {
len := C.read(fd, pbuf, 4096)
if len == 0 {
break
}
res.write_ptr(pbuf, len)
}
}
soutput := res.str()
exit_code := vpclose(f)
return Result{
exit_code: exit_code
output: soutput
}
}
// raw_execute does the same as `execute` on Unix platforms.
// On Windows raw_execute starts the specified command, waits for it to complete, and returns its output.
// It's marked as `unsafe` to help emphasize the problems that may arise by allowing, for example,
// user provided escape sequences.
@[unsafe]
pub fn raw_execute(cmd string) Result {
return execute(cmd)
}
@[manualfree]
pub fn (mut c Command) start() ! {
pcmd := c.path + ' 2>&1'
defer {
unsafe { pcmd.free() }
}
c.f = vpopen(pcmd)
if isnil(c.f) {
return error('exec("${c.path}") failed')
}
}
@[manualfree]
pub fn (mut c Command) read_line() string {
buf := [4096]u8{}
mut res := strings.new_builder(1024)
defer {
unsafe { res.free() }
}
unsafe {
bufbp := &buf[0]
for C.fgets(&char(bufbp), 4096, c.f) != 0 {
len := vstrlen(bufbp)
for i in 0 .. len {
if bufbp[i] == `\n` {
res.write_ptr(bufbp, i)
final := res.str()
return final
}
}
res.write_ptr(bufbp, len)
}
}
c.eof = true
final := res.str()
return final
}
pub fn (mut c Command) close() ! {
c.exit_code = vpclose(c.f)
if c.exit_code == 127 {
return error_with_code('error', 127)
}
}
pub fn symlink(origin string, target string) ! {
res := C.symlink(&char(origin.str), &char(target.str))
if res == 0 {
return
}
return error(posix_get_error_msg(C.errno))
}
pub fn link(origin string, target string) ! {
res := C.link(&char(origin.str), &char(target.str))
if res == 0 {
return
}
return error(posix_get_error_msg(C.errno))
}
// get_error_msg return error code representation in string.
pub fn get_error_msg(code int) string {
return posix_get_error_msg(code)
}
pub fn (mut f File) close() {
if !f.is_opened {
return
}
f.is_opened = false
C.fflush(f.cfile)
C.fclose(f.cfile)
}
fn C.mkstemp(stemplate &u8) int
// ensure_folder_is_writable checks that `folder` exists, and is writable to the process
// by creating an empty file in it, then deleting it.
@[manualfree]
pub fn ensure_folder_is_writable(folder string) ! {
if !exists(folder) {
return error_with_code('`${folder}` does not exist', 1)
}
if !is_dir(folder) {
return error_with_code('`${folder}` is not a folder', 2)
}
tmp_perm_check := join_path_single(folder, 'XXXXXX')
defer {
unsafe { tmp_perm_check.free() }
}
unsafe {
x := C.mkstemp(&char(tmp_perm_check.str))
if -1 == x {
return error_with_code('folder `${folder}` is not writable', 3)
}
C.close(x)
}
rm(tmp_perm_check)!
}
@[inline]
pub fn getpid() int {
return C.getpid()
}
@[inline]
pub fn getppid() int {
return C.getppid()
}
@[inline]
pub fn getuid() int {
return C.getuid()
}
@[inline]
pub fn geteuid() int {
return C.geteuid()
}
@[inline]
pub fn getgid() int {
return C.getgid()
}
@[inline]
pub fn getegid() int {
return C.getegid()
}
// Turns the given bit on or off, depending on the `enable` parameter
pub fn posix_set_permission_bit(path_s string, mode u32, enable bool) {
mut s := C.stat{}
mut new_mode := u32(0)
path := &char(path_s.str)
unsafe {
C.stat(path, &s)
new_mode = s.st_mode
}
match enable {
true { new_mode |= mode }
false { new_mode &= (0o7777 - mode) }
}
C.chmod(path, int(new_mode))
}
// get_long_path has no meaning for *nix, but has for windows, where `c:\folder\some~1` for example
// can be the equivalent of `c:\folder\some spa ces`. On *nix, it just returns a copy of the input path.
fn get_long_path(path string) !string {
return path
}
fn C.sysconf(name int) i64
// page_size returns the page size in bytes.
pub fn page_size() int {
return int(C.sysconf(C._SC_PAGESIZE))
}