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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
|
#+private
package os2
import "base:runtime"
import "core:io"
import "core:time"
import "core:sync"
import "core:sys/linux"
// Most implementations will EINVAL at some point when doing big writes.
// In practice a read/write call would probably never read/write these big buffers all at once,
// which is why the number of bytes is returned and why there are procs that will call this in a
// loop for you.
// We set a max of 1GB to keep alignment and to be safe.
MAX_RW :: 1 << 30
File_Impl :: struct {
file: File,
name: string,
fd: linux.Fd,
allocator: runtime.Allocator,
buffer: []byte,
rw_mutex: sync.RW_Mutex, // read write calls
p_mutex: sync.Mutex, // pread pwrite calls
}
_stdin := File{
stream = {
procedure = _file_stream_proc,
},
fstat = _fstat,
}
_stdout := File{
stream = {
procedure = _file_stream_proc,
},
fstat = _fstat,
}
_stderr := File{
stream = {
procedure = _file_stream_proc,
},
fstat = _fstat,
}
@init
_standard_stream_init :: proc "contextless" () {
new_std :: proc "contextless" (impl: ^File_Impl, fd: linux.Fd, name: string) -> ^File {
impl.file.impl = impl
impl.fd = linux.Fd(fd)
impl.allocator = runtime.nil_allocator()
impl.name = name
impl.file.stream = {
data = impl,
procedure = _file_stream_proc,
}
impl.file.fstat = _fstat
return &impl.file
}
@(static) files: [3]File_Impl
stdin = new_std(&files[0], 0, "/proc/self/fd/0")
stdout = new_std(&files[1], 1, "/proc/self/fd/1")
stderr = new_std(&files[2], 2, "/proc/self/fd/2")
}
_open :: proc(name: string, flags: File_Flags, perm: Permissions) -> (f: ^File, err: Error) {
temp_allocator := TEMP_ALLOCATOR_GUARD({})
name_cstr := clone_to_cstring(name, temp_allocator) or_return
// Just default to using O_NOCTTY because needing to open a controlling
// terminal would be incredibly rare. This has no effect on files while
// allowing us to open serial devices.
sys_flags: linux.Open_Flags = {.NOCTTY, .CLOEXEC}
when size_of(rawptr) == 4 {
sys_flags += {.LARGEFILE}
}
switch flags & (O_RDONLY|O_WRONLY|O_RDWR) {
case O_RDONLY:
case O_WRONLY: sys_flags += {.WRONLY}
case O_RDWR: sys_flags += {.RDWR}
}
if .Append in flags { sys_flags += {.APPEND} }
if .Create in flags { sys_flags += {.CREAT} }
if .Excl in flags { sys_flags += {.EXCL} }
if .Sync in flags { sys_flags += {.DSYNC} }
if .Trunc in flags { sys_flags += {.TRUNC} }
if .Inheritable in flags { sys_flags -= {.CLOEXEC} }
fd, errno := linux.open(name_cstr, sys_flags, transmute(linux.Mode)transmute(u32)perm)
if errno != .NONE {
return nil, _get_platform_error(errno)
}
return _new_file(uintptr(fd), name, file_allocator())
}
_new_file :: proc(fd: uintptr, _: string, allocator: runtime.Allocator) -> (f: ^File, err: Error) {
impl := new(File_Impl, allocator) or_return
defer if err != nil {
free(impl, allocator)
}
impl.file.impl = impl
impl.fd = linux.Fd(fd)
impl.allocator = allocator
impl.name = _get_full_path(impl.fd, impl.allocator) or_return
impl.file.stream = {
data = impl,
procedure = _file_stream_proc,
}
impl.file.fstat = _fstat
return &impl.file, nil
}
_clone :: proc(f: ^File) -> (clone: ^File, err: Error) {
if f == nil || f.impl == nil {
return
}
fd := (^File_Impl)(f.impl).fd
clonefd, errno := linux.dup(fd)
if errno != nil {
err = _get_platform_error(errno)
return
}
defer if err != nil { linux.close(clonefd) }
return _new_file(uintptr(clonefd), "", file_allocator())
}
@(require_results)
_open_buffered :: proc(name: string, buffer_size: uint, flags := File_Flags{.Read}, perm: Permissions) -> (f: ^File, err: Error) {
assert(buffer_size > 0)
f, err = _open(name, flags, perm)
if f != nil && err == nil {
impl := (^File_Impl)(f.impl)
impl.buffer = make([]byte, buffer_size, file_allocator())
f.stream.procedure = _file_stream_buffered_proc
}
return
}
_destroy :: proc(f: ^File_Impl) -> Error {
if f == nil {
return nil
}
a := f.allocator
err0 := delete(f.name, a)
err1 := delete(f.buffer, a)
err2 := free(f, a)
err0 or_return
err1 or_return
err2 or_return
return nil
}
_close :: proc(f: ^File_Impl) -> Error {
if f == nil{
return nil
}
errno := linux.close(f.fd)
if errno == .EBADF { // avoid possible double free
return _get_platform_error(errno)
}
_destroy(f)
return _get_platform_error(errno)
}
_fd :: proc(f: ^File) -> uintptr {
if f == nil || f.impl == nil {
return ~uintptr(0)
}
impl := (^File_Impl)(f.impl)
return uintptr(impl.fd)
}
_name :: proc(f: ^File) -> string {
return (^File_Impl)(f.impl).name if f != nil && f.impl != nil else ""
}
_seek :: proc(f: ^File_Impl, offset: i64, whence: io.Seek_From) -> (ret: i64, err: Error) {
// We have to handle this here, because Linux returns EINVAL for both
// invalid offsets and invalid whences.
switch whence {
case .Start, .Current, .End:
break
case:
return 0, .Invalid_Whence
}
n, errno := linux.lseek(f.fd, offset, linux.Seek_Whence(whence))
#partial switch errno {
case .EINVAL:
return 0, .Invalid_Offset
case .NONE:
return n, nil
case:
return 0, _get_platform_error(errno)
}
}
_read :: proc(f: ^File_Impl, p: []byte) -> (i64, Error) {
if len(p) <= 0 {
return 0, nil
}
n, errno := linux.read(f.fd, p[:min(len(p), MAX_RW)])
if errno != .NONE {
return 0, _get_platform_error(errno)
}
return i64(n), io.Error.EOF if n == 0 else nil
}
_read_at :: proc(f: ^File_Impl, p: []byte, offset: i64) -> (i64, Error) {
if len(p) <= 0 {
return 0, nil
}
if offset < 0 {
return 0, .Invalid_Offset
}
n, errno := linux.pread(f.fd, p[:min(len(p), MAX_RW)], offset)
if errno != .NONE {
return 0, _get_platform_error(errno)
}
if n == 0 {
return 0, .EOF
}
return i64(n), nil
}
_write :: proc(f: ^File_Impl, p: []byte) -> (nt: i64, err: Error) {
p := p
for len(p) > 0 {
n, errno := linux.write(f.fd, p[:min(len(p), MAX_RW)])
if errno != .NONE {
err = _get_platform_error(errno)
return
}
p = p[n:]
nt += i64(n)
}
return
}
_write_at :: proc(f: ^File_Impl, p: []byte, offset: i64) -> (nt: i64, err: Error) {
if offset < 0 {
return 0, .Invalid_Offset
}
p := p
offset := offset
for len(p) > 0 {
n, errno := linux.pwrite(f.fd, p[:min(len(p), MAX_RW)], offset)
if errno != .NONE {
err = _get_platform_error(errno)
return
}
p = p[n:]
nt += i64(n)
offset += i64(n)
}
return
}
@(no_sanitize_memory)
_file_size :: proc(f: ^File_Impl) -> (n: i64, err: Error) {
// TODO: Identify 0-sized "pseudo" files and return No_Size. This would
// eliminate the need for the _read_entire_pseudo_file procs.
s: linux.Stat = ---
errno := linux.fstat(f.fd, &s)
if errno != .NONE {
return 0, _get_platform_error(errno)
}
if s.mode & linux.S_IFMT == linux.S_IFREG {
return i64(s.size), nil
}
return 0, .No_Size
}
_sync :: proc(f: ^File) -> Error {
impl := (^File_Impl)(f.impl)
return _get_platform_error(linux.fsync(impl.fd))
}
_flush :: proc(f: ^File_Impl) -> Error {
return _get_platform_error(linux.fsync(f.fd))
}
_truncate :: proc(f: ^File, size: i64) -> Error {
impl := (^File_Impl)(f.impl)
return _get_platform_error(linux.ftruncate(impl.fd, size))
}
_remove :: proc(name: string) -> Error {
temp_allocator := TEMP_ALLOCATOR_GUARD({})
name_cstr := clone_to_cstring(name, temp_allocator) or_return
if fd, errno := linux.open(name_cstr, _OPENDIR_FLAGS + {.NOFOLLOW}); errno == .NONE {
linux.close(fd)
return _get_platform_error(linux.rmdir(name_cstr))
}
return _get_platform_error(linux.unlink(name_cstr))
}
_rename :: proc(old_name, new_name: string) -> Error {
temp_allocator := TEMP_ALLOCATOR_GUARD({})
old_name_cstr := clone_to_cstring(old_name, temp_allocator) or_return
new_name_cstr := clone_to_cstring(new_name, temp_allocator) or_return
return _get_platform_error(linux.rename(old_name_cstr, new_name_cstr))
}
_link :: proc(old_name, new_name: string) -> Error {
temp_allocator := TEMP_ALLOCATOR_GUARD({})
old_name_cstr := clone_to_cstring(old_name, temp_allocator) or_return
new_name_cstr := clone_to_cstring(new_name, temp_allocator) or_return
return _get_platform_error(linux.link(old_name_cstr, new_name_cstr))
}
_symlink :: proc(old_name, new_name: string) -> Error {
temp_allocator := TEMP_ALLOCATOR_GUARD({})
old_name_cstr := clone_to_cstring(old_name, temp_allocator) or_return
new_name_cstr := clone_to_cstring(new_name, temp_allocator) or_return
return _get_platform_error(linux.symlink(old_name_cstr, new_name_cstr))
}
_read_link_cstr :: proc(name_cstr: cstring, allocator: runtime.Allocator) -> (string, Error) {
bufsz : uint = 256
buf := make([]byte, bufsz, allocator)
for {
sz, errno := linux.readlink(name_cstr, buf[:])
if errno != .NONE {
delete(buf, allocator)
return "", _get_platform_error(errno)
} else if sz == int(bufsz) {
bufsz *= 2
delete(buf, allocator)
buf = make([]byte, bufsz, allocator)
} else {
return string(buf[:sz]), nil
}
}
}
_read_link :: proc(name: string, allocator: runtime.Allocator) -> (s: string, e: Error) {
temp_allocator := TEMP_ALLOCATOR_GUARD({ allocator })
name_cstr := clone_to_cstring(name, temp_allocator) or_return
return _read_link_cstr(name_cstr, allocator)
}
_chdir :: proc(name: string) -> Error {
temp_allocator := TEMP_ALLOCATOR_GUARD({})
name_cstr := clone_to_cstring(name, temp_allocator) or_return
return _get_platform_error(linux.chdir(name_cstr))
}
_fchdir :: proc(f: ^File) -> Error {
impl := (^File_Impl)(f.impl)
return _get_platform_error(linux.fchdir(impl.fd))
}
_chmod :: proc(name: string, mode: Permissions) -> Error {
temp_allocator := TEMP_ALLOCATOR_GUARD({})
name_cstr := clone_to_cstring(name, temp_allocator) or_return
return _get_platform_error(linux.chmod(name_cstr, transmute(linux.Mode)transmute(u32)mode))
}
_fchmod :: proc(f: ^File, mode: Permissions) -> Error {
impl := (^File_Impl)(f.impl)
return _get_platform_error(linux.fchmod(impl.fd, transmute(linux.Mode)transmute(u32)mode))
}
// NOTE: will throw error without super user priviledges
_chown :: proc(name: string, uid, gid: int) -> Error {
temp_allocator := TEMP_ALLOCATOR_GUARD({})
name_cstr := clone_to_cstring(name, temp_allocator) or_return
return _get_platform_error(linux.chown(name_cstr, linux.Uid(uid), linux.Gid(gid)))
}
// NOTE: will throw error without super user priviledges
_lchown :: proc(name: string, uid, gid: int) -> Error {
temp_allocator := TEMP_ALLOCATOR_GUARD({})
name_cstr := clone_to_cstring(name, temp_allocator) or_return
return _get_platform_error(linux.lchown(name_cstr, linux.Uid(uid), linux.Gid(gid)))
}
// NOTE: will throw error without super user priviledges
_fchown :: proc(f: ^File, uid, gid: int) -> Error {
impl := (^File_Impl)(f.impl)
return _get_platform_error(linux.fchown(impl.fd, linux.Uid(uid), linux.Gid(gid)))
}
_chtimes :: proc(name: string, atime, mtime: time.Time) -> Error {
temp_allocator := TEMP_ALLOCATOR_GUARD({})
name_cstr := clone_to_cstring(name, temp_allocator) or_return
times := [2]linux.Time_Spec {
{
uint(atime._nsec) / uint(time.Second),
uint(atime._nsec) % uint(time.Second),
},
{
uint(mtime._nsec) / uint(time.Second),
uint(mtime._nsec) % uint(time.Second),
},
}
return _get_platform_error(linux.utimensat(linux.AT_FDCWD, name_cstr, ×[0], nil))
}
_fchtimes :: proc(f: ^File, atime, mtime: time.Time) -> Error {
times := [2]linux.Time_Spec {
{
uint(atime._nsec) / uint(time.Second),
uint(atime._nsec) % uint(time.Second),
},
{
uint(mtime._nsec) / uint(time.Second),
uint(mtime._nsec) % uint(time.Second),
},
}
impl := (^File_Impl)(f.impl)
return _get_platform_error(linux.utimensat(impl.fd, nil, ×[0], nil))
}
_exists :: proc(name: string) -> bool {
temp_allocator := TEMP_ALLOCATOR_GUARD({})
name_cstr, _ := clone_to_cstring(name, temp_allocator)
return linux.access(name_cstr, linux.F_OK) == .NONE
}
/* For reading Linux system files that stat to size 0 */
_read_entire_pseudo_file :: proc { _read_entire_pseudo_file_string, _read_entire_pseudo_file_cstring }
_read_entire_pseudo_file_string :: proc(name: string, allocator: runtime.Allocator) -> (b: []u8, e: Error) {
temp_allocator := TEMP_ALLOCATOR_GUARD({ allocator })
name_cstr := clone_to_cstring(name, temp_allocator) or_return
return _read_entire_pseudo_file_cstring(name_cstr, allocator)
}
_read_entire_pseudo_file_cstring :: proc(name: cstring, allocator: runtime.Allocator) -> ([]u8, Error) {
fd, errno := linux.open(name, {})
if errno != .NONE {
return nil, _get_platform_error(errno)
}
defer linux.close(fd)
BUF_SIZE_STEP :: 128
contents := make([dynamic]u8, 0, BUF_SIZE_STEP, allocator)
n: int
i: int
for {
resize(&contents, i + BUF_SIZE_STEP)
n, errno = linux.read(fd, contents[i:i+BUF_SIZE_STEP])
if errno != .NONE {
delete(contents)
return nil, _get_platform_error(errno)
}
if n < BUF_SIZE_STEP {
break
}
i += BUF_SIZE_STEP
}
resize(&contents, i + n)
return contents[:], nil
}
@(private="package")
_file_stream_proc :: proc(stream_data: rawptr, mode: io.Stream_Mode, p: []byte, offset: i64, whence: io.Seek_From) -> (n: i64, err: io.Error) {
f := (^File_Impl)(stream_data)
ferr: Error
switch mode {
case .Read:
n, ferr = _read(f, p)
err = error_to_io_error(ferr)
return
case .Read_At:
n, ferr = _read_at(f, p, offset)
err = error_to_io_error(ferr)
return
case .Write:
n, ferr = _write(f, p)
err = error_to_io_error(ferr)
return
case .Write_At:
n, ferr = _write_at(f, p, offset)
err = error_to_io_error(ferr)
return
case .Seek:
n, ferr = _seek(f, offset, whence)
err = error_to_io_error(ferr)
return
case .Size:
n, ferr = _file_size(f)
err = error_to_io_error(ferr)
return
case .Flush:
ferr = _flush(f)
err = error_to_io_error(ferr)
return
case .Close, .Destroy:
ferr = _close(f)
err = error_to_io_error(ferr)
return
case .Query:
return io.query_utility({.Read, .Read_At, .Write, .Write_At, .Seek, .Size, .Flush, .Close, .Destroy, .Query})
}
return 0, .Empty
}
@(private="package")
_file_stream_buffered_proc :: proc(stream_data: rawptr, mode: io.Stream_Mode, p: []byte, offset: i64, whence: io.Seek_From) -> (n: i64, err: io.Error) {
f := (^File_Impl)(stream_data)
ferr: Error
switch mode {
case .Read:
n, ferr = _read(f, p)
err = error_to_io_error(ferr)
return
case .Read_At:
n, ferr = _read_at(f, p, offset)
err = error_to_io_error(ferr)
return
case .Write:
n, ferr = _write(f, p)
err = error_to_io_error(ferr)
return
case .Write_At:
n, ferr = _write_at(f, p, offset)
err = error_to_io_error(ferr)
return
case .Seek:
n, ferr = _seek(f, offset, whence)
err = error_to_io_error(ferr)
return
case .Size:
n, ferr = _file_size(f)
err = error_to_io_error(ferr)
return
case .Flush:
ferr = _flush(f)
err = error_to_io_error(ferr)
return
case .Close, .Destroy:
ferr = _close(f)
err = error_to_io_error(ferr)
return
case .Query:
return io.query_utility({.Read, .Read_At, .Write, .Write_At, .Seek, .Size, .Flush, .Close, .Destroy, .Query})
}
return 0, .Empty
}
|