github.com/jspc/eggos@v0.5.1-0.20221028160421-556c75c878a5/fs/chdir/chdir.go (about) 1 package chdir 2 3 import ( 4 "errors" 5 "os" 6 "path/filepath" 7 "time" 8 9 "github.com/spf13/afero" 10 ) 11 12 type Chdirfs struct { 13 dir string 14 backend afero.Fs 15 } 16 17 func New(backend afero.Fs) *Chdirfs { 18 return &Chdirfs{ 19 dir: "/", 20 backend: backend, 21 } 22 } 23 24 func (c *Chdirfs) Chdir(dir string) error { 25 name := c.name(dir) 26 fi, err := c.backend.Stat(name) 27 if err != nil { 28 return err 29 } 30 if !fi.IsDir() { 31 return errors.New("not a dir") 32 } 33 c.dir = name 34 return nil 35 } 36 37 func (c *Chdirfs) name(name string) string { 38 if filepath.IsAbs(name) { 39 return name 40 } 41 return filepath.Join(c.dir, name) 42 } 43 44 // Create creates a file in the filesystem, returning the file and an 45 // error, if any happens. 46 func (c *Chdirfs) Create(name string) (afero.File, error) { 47 return c.backend.Create(c.name(name)) 48 } 49 50 // Mkdir creates a directory in the filesystem, return an error if any 51 // happens. 52 func (c *Chdirfs) Mkdir(name string, perm os.FileMode) error { 53 return c.backend.Mkdir(c.name(name), perm) 54 } 55 56 // MkdirAll creates a directory path and all parents that does not exist 57 // yet. 58 func (c *Chdirfs) MkdirAll(path string, perm os.FileMode) error { 59 return c.backend.MkdirAll(c.name(path), perm) 60 } 61 62 // Open opens a file, returning it or an error, if any happens. 63 func (c *Chdirfs) Open(name string) (afero.File, error) { 64 return c.backend.Open(c.name(name)) 65 } 66 67 // OpenFile opens a file using the given flags and the given mode. 68 func (c *Chdirfs) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error) { 69 return c.backend.OpenFile(c.name(name), flag, perm) 70 } 71 72 // Remove removes a file identified by name, returning an error, if any 73 // happens. 74 func (c *Chdirfs) Remove(name string) error { 75 return c.backend.Remove(c.name(name)) 76 } 77 78 // RemoveAll removes a directory path and any children it contains. It 79 // does not fail if the path does not exist (return nil). 80 func (c *Chdirfs) RemoveAll(path string) error { 81 return c.backend.RemoveAll(c.name(path)) 82 } 83 84 // Rename renames a file. 85 func (c *Chdirfs) Rename(oldname string, newname string) error { 86 return c.backend.Rename(c.name(oldname), c.name(newname)) 87 } 88 89 // Stat returns a FileInfo describing the named file, or an error, if any 90 // happens. 91 func (c *Chdirfs) Stat(name string) (os.FileInfo, error) { 92 return c.backend.Stat(c.name(name)) 93 } 94 95 // The name of this FileSystem 96 func (c *Chdirfs) Name() string { 97 return "chdirfs" 98 } 99 100 //Chmod changes the mode of the named file to mode. 101 func (c *Chdirfs) Chmod(name string, mode os.FileMode) error { 102 return c.backend.Chmod(c.name(name), mode) 103 } 104 105 //Chtimes changes the access and modification times of the named file 106 func (c *Chdirfs) Chtimes(name string, atime time.Time, mtime time.Time) error { 107 return c.backend.Chtimes(c.name(name), atime, mtime) 108 }