github.com/weaveworks/common@v0.0.0-20230728070032-dd9e68f319d5/fs/fs.go (about)

     1  package fs
     2  
     3  import (
     4  	"io"
     5  	"io/ioutil"
     6  	"os"
     7  	"syscall"
     8  )
     9  
    10  // Interface is the filesystem interface type.
    11  type Interface interface {
    12  	ReadDir(string) ([]os.FileInfo, error)
    13  	ReadDirNames(string) ([]string, error)
    14  	ReadDirCount(string) (int, error)
    15  	ReadFile(string) ([]byte, error)
    16  	Lstat(string, *syscall.Stat_t) error
    17  	Stat(string, *syscall.Stat_t) error
    18  	Open(string) (io.ReadWriteCloser, error)
    19  }
    20  
    21  type realFS struct{}
    22  
    23  // FS is the way you should access the filesystem.
    24  var fs Interface = realFS{}
    25  
    26  func (realFS) ReadDir(path string) ([]os.FileInfo, error) {
    27  	return ioutil.ReadDir(path)
    28  }
    29  
    30  func (realFS) ReadDirNames(path string) ([]string, error) {
    31  	fh, err := os.Open(path)
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  	defer fh.Close()
    36  	return fh.Readdirnames(-1)
    37  }
    38  
    39  func (realFS) ReadFile(path string) ([]byte, error) {
    40  	return ioutil.ReadFile(path)
    41  }
    42  
    43  func (realFS) Lstat(path string, stat *syscall.Stat_t) error {
    44  	return syscall.Lstat(path, stat)
    45  }
    46  
    47  func (realFS) Stat(path string, stat *syscall.Stat_t) error {
    48  	return syscall.Stat(path, stat)
    49  }
    50  
    51  func (realFS) Open(path string) (io.ReadWriteCloser, error) {
    52  	return os.Open(path)
    53  }
    54  
    55  // trampolines here to allow users to do fs.ReadDir etc
    56  
    57  // ReadDir see ioutil.ReadDir
    58  func ReadDir(path string) ([]os.FileInfo, error) {
    59  	return fs.ReadDir(path)
    60  }
    61  
    62  // ReadDirNames see os.File.ReadDirNames
    63  func ReadDirNames(path string) ([]string, error) {
    64  	return fs.ReadDirNames(path)
    65  }
    66  
    67  // ReadDirCount is an optimized way to call len(ReadDirNames)
    68  func ReadDirCount(path string) (int, error) {
    69  	return fs.ReadDirCount(path)
    70  }
    71  
    72  // ReadFile see ioutil.ReadFile
    73  func ReadFile(path string) ([]byte, error) {
    74  	return fs.ReadFile(path)
    75  }
    76  
    77  // Lstat see syscall.Lstat
    78  func Lstat(path string, stat *syscall.Stat_t) error {
    79  	return fs.Lstat(path, stat)
    80  }
    81  
    82  // Stat see syscall.Stat
    83  func Stat(path string, stat *syscall.Stat_t) error {
    84  	return fs.Stat(path, stat)
    85  }
    86  
    87  // Open see os.Open
    88  func Open(path string) (io.ReadWriteCloser, error) {
    89  	return fs.Open(path)
    90  }
    91  
    92  // Mock is used to switch out the filesystem for a mock.
    93  func Mock(mock Interface) {
    94  	fs = mock
    95  }
    96  
    97  // Restore puts back the real filesystem.
    98  func Restore() {
    99  	fs = realFS{}
   100  }