github.com/sunvim/utils@v0.1.0/fs/sub.go (about) 1 package fs 2 3 import ( 4 "os" 5 "path/filepath" 6 ) 7 8 // Sub returns a new file system rooted at dir. 9 func Sub(fsys FileSystem, dir string) FileSystem { 10 return &subFS{ 11 fsys: fsys, 12 root: dir, 13 } 14 } 15 16 type subFS struct { 17 fsys FileSystem 18 root string 19 } 20 21 func (fs *subFS) OpenFile(name string, flag int, perm os.FileMode) (File, error) { 22 subName := filepath.Join(fs.root, name) 23 return fs.fsys.OpenFile(subName, flag, perm) 24 } 25 26 func (fs *subFS) Stat(name string) (os.FileInfo, error) { 27 subName := filepath.Join(fs.root, name) 28 return fs.fsys.Stat(subName) 29 } 30 31 func (fs *subFS) Remove(name string) error { 32 subName := filepath.Join(fs.root, name) 33 return fs.fsys.Remove(subName) 34 } 35 36 func (fs *subFS) Rename(oldpath, newpath string) error { 37 subOldpath := filepath.Join(fs.root, oldpath) 38 subNewpath := filepath.Join(fs.root, newpath) 39 return fs.fsys.Rename(subOldpath, subNewpath) 40 } 41 42 func (fs *subFS) ReadDir(name string) ([]os.FileInfo, error) { 43 subName := filepath.Join(fs.root, name) 44 return fs.fsys.ReadDir(subName) 45 } 46 47 func (fs *subFS) CreateLockFile(name string, perm os.FileMode) (LockFile, bool, error) { 48 subName := filepath.Join(fs.root, name) 49 return fs.fsys.CreateLockFile(subName, perm) 50 } 51 52 var _ FileSystem = &subFS{}