/
file_linux.go
56 lines (46 loc) · 1017 Bytes
/
file_linux.go
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
//go:build linux
package storage
import (
"syscall"
)
type linuxFile struct {
fd int
filename string
}
func (f *linuxFile) Close() error {
if f.fd != -1 {
if err := syscall.Close(f.fd); err != nil {
return err
}
f.fd = -1
}
return nil
}
func (f *linuxFile) Read(p []byte) (n int, err error) {
return syscall.Read(f.fd, p)
}
func (f *linuxFile) Write(p []byte) (n int, err error) {
return syscall.Write(f.fd, p)
}
func (f *linuxFile) Seek(offset int64, whence int) (int64, error) {
return syscall.Seek(f.fd, offset, whence)
}
func (f *linuxFile) Flush() error {
return syscall.Fsync(f.fd)
}
func (f *linuxFile) Path() string {
return f.filename
}
func OpenFile(filename string, readonly bool) (File, error) {
var access int
if readonly {
access = syscall.O_RDONLY
} else {
access = syscall.O_RDWR
}
fd, err := syscall.Open(filename, syscall.O_CLOEXEC|syscall.O_CREAT|access, 0666)
if err != nil {
return nil, err
}
return &linuxFile{fd: fd, filename: filename}, nil
}