github.com/sunvim/utils@v0.1.0/fs/os.go (about)

     1  package fs
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  )
     7  
     8  type osFS struct{}
     9  
    10  // OS is a file system backed by the os package.
    11  var OS FileSystem = &osFS{}
    12  
    13  func (fs *osFS) OpenFile(name string, flag int, perm os.FileMode) (File, error) {
    14  	f, err := os.OpenFile(name, flag, perm)
    15  	if err != nil {
    16  		return nil, err
    17  	}
    18  	return &osFile{File: f}, nil
    19  }
    20  
    21  func (fs *osFS) CreateLockFile(name string, perm os.FileMode) (LockFile, bool, error) {
    22  	return createLockFile(name, perm)
    23  }
    24  
    25  func (fs *osFS) Stat(name string) (os.FileInfo, error) {
    26  	return os.Stat(name)
    27  }
    28  
    29  func (fs *osFS) Remove(name string) error {
    30  	return os.Remove(name)
    31  }
    32  
    33  func (fs *osFS) Rename(oldpath, newpath string) error {
    34  	return os.Rename(oldpath, newpath)
    35  }
    36  
    37  func (fs *osFS) ReadDir(name string) ([]os.FileInfo, error) {
    38  	return ioutil.ReadDir(name)
    39  }
    40  
    41  type osFile struct {
    42  	*os.File
    43  }
    44  
    45  func (f *osFile) Slice(start int64, end int64) ([]byte, error) {
    46  	buf := make([]byte, end-start)
    47  	_, err := f.ReadAt(buf, start)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  	return buf, nil
    52  }
    53  
    54  type osLockFile struct {
    55  	*os.File
    56  	path string
    57  }
    58  
    59  func (f *osLockFile) Unlock() error {
    60  	if err := os.Remove(f.path); err != nil {
    61  		return err
    62  	}
    63  	return f.Close()
    64  }