gobot.io/x/gobot@v1.16.0/sysfs/fs.go (about)

     1  package sysfs
     2  
     3  import (
     4  	"os"
     5  )
     6  
     7  // A File represents basic IO interactions with the underlying file system
     8  type File interface {
     9  	Write(b []byte) (n int, err error)
    10  	WriteString(s string) (ret int, err error)
    11  	Sync() (err error)
    12  	Read(b []byte) (n int, err error)
    13  	ReadAt(b []byte, off int64) (n int, err error)
    14  	Seek(offset int64, whence int) (ret int64, err error)
    15  	Fd() uintptr
    16  	Close() error
    17  }
    18  
    19  // Filesystem opens files and returns either a native file system or user defined
    20  type Filesystem interface {
    21  	OpenFile(name string, flag int, perm os.FileMode) (file File, err error)
    22  	Stat(name string) (os.FileInfo, error)
    23  }
    24  
    25  // NativeFilesystem represents the native file system implementation
    26  type NativeFilesystem struct{}
    27  
    28  // Default to the host filesystem.
    29  var fs Filesystem = &NativeFilesystem{}
    30  
    31  // SetFilesystem sets the filesystem implementation.
    32  func SetFilesystem(f Filesystem) {
    33  	fs = f
    34  }
    35  
    36  // OpenFile calls os.OpenFile().
    37  func (fs *NativeFilesystem) OpenFile(name string, flag int, perm os.FileMode) (file File, err error) {
    38  	return os.OpenFile(name, flag, perm)
    39  }
    40  
    41  // Stat calls os.Stat()
    42  func (fs *NativeFilesystem) Stat(name string) (os.FileInfo, error) {
    43  	return os.Stat(name)
    44  }
    45  
    46  // OpenFile calls either the NativeFilesystem or user defined OpenFile
    47  func OpenFile(name string, flag int, perm os.FileMode) (file File, err error) {
    48  	return fs.OpenFile(name, flag, perm)
    49  }
    50  
    51  // Stat call either the NativeFilesystem of user defined Stat
    52  func Stat(name string) (os.FileInfo, error) {
    53  	return fs.Stat(name)
    54  }