tractor.dev/toolkit-go@v0.0.0-20241010005851-214d91207d07/engine/fs/osfs/osfs.go (about)

     1  package osfs
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"time"
     7  
     8  	"tractor.dev/toolkit-go/engine/fs"
     9  	"tractor.dev/toolkit-go/engine/fs/workingpathfs"
    10  )
    11  
    12  type FS struct{}
    13  
    14  func New() *FS {
    15  	return &FS{}
    16  }
    17  
    18  func Dir(dir string) *workingpathfs.FS {
    19  	return workingpathfs.New(New(), dir)
    20  }
    21  
    22  func (FS) Create(name string) (fs.File, error) {
    23  	f, e := os.Create(fsToUnixPath(name))
    24  	if f == nil {
    25  		// while this looks strange, we need to return a bare nil (of type nil) not
    26  		// a nil value of type *os.File or nil won't be nil
    27  		return nil, e
    28  	}
    29  	return f, e
    30  }
    31  
    32  func (FS) Mkdir(name string, perm fs.FileMode) error {
    33  	return os.Mkdir(fsToUnixPath(name), perm)
    34  }
    35  
    36  func (FS) MkdirAll(path string, perm fs.FileMode) error {
    37  	return os.MkdirAll(path, perm)
    38  }
    39  
    40  func (FS) Open(name string) (fs.File, error) {
    41  	f, e := os.Open(fsToUnixPath(name))
    42  	if f == nil {
    43  		// while this looks strange, we need to return a bare nil (of type nil) not
    44  		// a nil value of type *os.File or nil won't be nil
    45  		return nil, e
    46  	}
    47  	return f, e
    48  }
    49  
    50  func (FS) OpenFile(name string, flag int, perm fs.FileMode) (fs.File, error) {
    51  	f, e := os.OpenFile(fsToUnixPath(name), flag, perm)
    52  	if f == nil {
    53  		// while this looks strange, we need to return a bare nil (of type nil) not
    54  		// a nil value of type *os.File or nil won't be nil
    55  		return nil, e
    56  	}
    57  	return f, e
    58  }
    59  
    60  func (FS) Remove(name string) error {
    61  	return os.Remove(fsToUnixPath(name))
    62  }
    63  
    64  func (FS) RemoveAll(path string) error {
    65  	return os.RemoveAll(fsToUnixPath(path))
    66  }
    67  
    68  func (FS) Rename(oldname, newname string) error {
    69  	return os.Rename(fsToUnixPath(oldname), fsToUnixPath(newname))
    70  }
    71  
    72  func (FS) Stat(name string) (fs.FileInfo, error) {
    73  	return os.Stat(fsToUnixPath(name))
    74  }
    75  
    76  func (FS) Chmod(name string, mode fs.FileMode) error {
    77  	return os.Chmod(fsToUnixPath(name), mode)
    78  }
    79  
    80  func (FS) Chown(name string, uid, gid int) error {
    81  	return os.Chown(fsToUnixPath(name), uid, gid)
    82  }
    83  
    84  func (FS) Chtimes(name string, atime time.Time, mtime time.Time) error {
    85  	return os.Chtimes(fsToUnixPath(name), atime, mtime)
    86  }
    87  
    88  // Converts an `io/fs` path to a Unix path.
    89  // Assumes path is absolute already
    90  func fsToUnixPath(path string) string {
    91  	if !filepath.IsAbs(path) {
    92  		path = "/" + path
    93  	}
    94  	return filepath.Clean(path)
    95  }