Skip to content

Commit

Permalink
support linux
Browse files Browse the repository at this point in the history
  • Loading branch information
hyan23 committed Sep 9, 2022
1 parent 634f4ac commit 7cb675e
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions storage/file_linux.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,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
}

0 comments on commit 7cb675e

Please sign in to comment.