2014-11-08 08:21:39 +08:00
|
|
|
package sysfs
|
2014-10-31 04:41:24 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2014-12-31 22:12:25 +08:00
|
|
|
// A File represents basic IO interactions with the underlying file system
|
2014-10-31 04:41:24 +08:00
|
|
|
type File interface {
|
|
|
|
Write(b []byte) (n int, err error)
|
|
|
|
WriteString(s string) (ret int, err error)
|
|
|
|
Sync() (err error)
|
|
|
|
Read(b []byte) (n int, err error)
|
|
|
|
ReadAt(b []byte, off int64) (n int, err error)
|
2016-02-19 04:08:48 +08:00
|
|
|
Seek(offset int64, whence int) (ret int64, err error)
|
2014-10-31 04:41:24 +08:00
|
|
|
Fd() uintptr
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
2014-12-31 22:12:25 +08:00
|
|
|
// Filesystem opens files and returns either a native file system or user defined
|
2014-10-31 04:41:24 +08:00
|
|
|
type Filesystem interface {
|
|
|
|
OpenFile(name string, flag int, perm os.FileMode) (file File, err error)
|
2017-03-14 07:11:06 +08:00
|
|
|
Stat(name string) (os.FileInfo, error)
|
2014-10-31 04:41:24 +08:00
|
|
|
}
|
|
|
|
|
2014-12-31 22:12:25 +08:00
|
|
|
// NativeFilesystem represents the native file system implementation
|
2014-11-08 08:21:39 +08:00
|
|
|
type NativeFilesystem struct{}
|
2014-10-31 04:41:24 +08:00
|
|
|
|
|
|
|
// Default to the host filesystem.
|
|
|
|
var fs Filesystem = &NativeFilesystem{}
|
|
|
|
|
2014-12-31 22:12:25 +08:00
|
|
|
// SetFilesystem sets the filesystem implementation.
|
2014-10-31 04:41:24 +08:00
|
|
|
func SetFilesystem(f Filesystem) {
|
|
|
|
fs = f
|
|
|
|
}
|
|
|
|
|
2014-12-31 22:12:25 +08:00
|
|
|
// OpenFile calls os.OpenFile().
|
2014-10-31 04:41:24 +08:00
|
|
|
func (fs *NativeFilesystem) OpenFile(name string, flag int, perm os.FileMode) (file File, err error) {
|
|
|
|
return os.OpenFile(name, flag, perm)
|
|
|
|
}
|
|
|
|
|
2017-03-14 07:11:06 +08:00
|
|
|
// Stat calls os.Stat()
|
|
|
|
func (fs *NativeFilesystem) Stat(name string) (os.FileInfo, error) {
|
|
|
|
return os.Stat(name)
|
|
|
|
}
|
|
|
|
|
2014-12-31 22:12:25 +08:00
|
|
|
// OpenFile calls either the NativeFilesystem or user defined OpenFile
|
2014-10-31 04:41:24 +08:00
|
|
|
func OpenFile(name string, flag int, perm os.FileMode) (file File, err error) {
|
|
|
|
return fs.OpenFile(name, flag, perm)
|
|
|
|
}
|
2017-03-14 07:11:06 +08:00
|
|
|
|
|
|
|
// Stat call either the NativeFilesystem of user defined Stat
|
|
|
|
func Stat(name string) (os.FileInfo, error) {
|
|
|
|
return fs.Stat(name)
|
|
|
|
}
|