|
| 1 | +package fs |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "io/fs" |
| 6 | + "path/filepath" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +type SubFS struct { |
| 11 | + FS FullFS |
| 12 | + Root string |
| 13 | +} |
| 14 | + |
| 15 | +func (s *SubFS) Open(path string) (fs.File, error) { |
| 16 | + if !fs.ValidPath(path) { |
| 17 | + return nil, &fs.PathError{Op: "open", Path: path, Err: fs.ErrInvalid} |
| 18 | + } |
| 19 | + fullPath := filepath.Join(s.Root, path) |
| 20 | + return s.FS.Open(fullPath) |
| 21 | +} |
| 22 | + |
| 23 | +func (s *SubFS) OpenReaderAt(path string) (File, error) { |
| 24 | + fullPath := filepath.Join(s.Root, path) |
| 25 | + return s.FS.OpenReaderAt(fullPath) |
| 26 | +} |
| 27 | + |
| 28 | +func (s *SubFS) OpenFile(name string, flag int, perm fs.FileMode) (File, error) { |
| 29 | + fullPath := filepath.Join(s.Root, name) |
| 30 | + return s.FS.OpenFile(fullPath, flag, perm) |
| 31 | +} |
| 32 | +func (s *SubFS) Create(name string) (File, error) { |
| 33 | + fullPath := filepath.Join(s.Root, name) |
| 34 | + return s.FS.Create(fullPath) |
| 35 | +} |
| 36 | + |
| 37 | +func (s *SubFS) ReadFile(name string) ([]byte, error) { |
| 38 | + fullPath := filepath.Join(s.Root, name) |
| 39 | + return s.FS.ReadFile(fullPath) |
| 40 | +} |
| 41 | +func (s *SubFS) WriteFile(name string, b []byte, mode fs.FileMode) error { |
| 42 | + fullPath := filepath.Join(s.Root, name) |
| 43 | + return s.FS.WriteFile(fullPath, b, mode) |
| 44 | +} |
| 45 | + |
| 46 | +func (s *SubFS) Mkdir(path string, perm fs.FileMode) error { |
| 47 | + fullPath := filepath.Join(s.Root, path) |
| 48 | + return s.FS.Mkdir(fullPath, perm) |
| 49 | +} |
| 50 | +func (s *SubFS) MkdirAll(path string, perm fs.FileMode) error { |
| 51 | + fullPath := filepath.Join(s.Root, path) |
| 52 | + return s.FS.MkdirAll(fullPath, perm) |
| 53 | +} |
| 54 | +func (s *SubFS) ReadDir(name string) ([]fs.DirEntry, error) { |
| 55 | + fullPath := filepath.Join(s.Root, name) |
| 56 | + return s.FS.ReadDir(fullPath) |
| 57 | +} |
| 58 | + |
| 59 | +func (s *SubFS) Stat(path string) (fs.FileInfo, error) { |
| 60 | + fullPath := filepath.Join(s.Root, path) |
| 61 | + return s.FS.Stat(fullPath) |
| 62 | +} |
| 63 | +func (s *SubFS) Lstat(path string) (fs.FileInfo, error) { |
| 64 | + fullPath := filepath.Join(s.Root, path) |
| 65 | + return s.FS.Lstat(fullPath) |
| 66 | +} |
| 67 | + |
| 68 | +func (s *SubFS) Remove(name string) error { |
| 69 | + fullPath := filepath.Join(s.Root, name) |
| 70 | + return s.FS.Remove(fullPath) |
| 71 | +} |
| 72 | +func (s *SubFS) Chmod(path string, perm fs.FileMode) error { |
| 73 | + fullPath := filepath.Join(s.Root, path) |
| 74 | + return s.FS.Chmod(fullPath, perm) |
| 75 | +} |
| 76 | +func (s *SubFS) Chown(path string, uid int, gid int) error { |
| 77 | + fullPath := filepath.Join(s.Root, path) |
| 78 | + return s.FS.Chown(fullPath, uid, gid) |
| 79 | +} |
| 80 | +func (s *SubFS) Chtimes(path string, atime time.Time, mtime time.Time) error { |
| 81 | + fullPath := filepath.Join(s.Root, path) |
| 82 | + return s.FS.Chtimes(fullPath, atime, mtime) |
| 83 | +} |
| 84 | + |
| 85 | +func (s *SubFS) Symlink(oldname, newname string) error { |
| 86 | + return s.FS.Symlink(oldname, newname) |
| 87 | +} |
| 88 | +func (s *SubFS) Link(oldname, newname string) error { |
| 89 | + return s.FS.Link(oldname, newname) |
| 90 | +} |
| 91 | +func (s *SubFS) Readlink(name string) (string, error) { |
| 92 | + fullPath := filepath.Join(s.Root, name) |
| 93 | + return s.FS.Readlink(fullPath) |
| 94 | +} |
| 95 | + |
| 96 | +func (s *SubFS) Mknod(path string, mode uint32, dev int) error { |
| 97 | + fullPath := filepath.Join(s.Root, path) |
| 98 | + return s.FS.Mknod(fullPath, mode, dev) |
| 99 | +} |
| 100 | +func (s *SubFS) Readnod(path string) (int, error) { |
| 101 | + fullPath := filepath.Join(s.Root, path) |
| 102 | + return s.FS.Readnod(fullPath) |
| 103 | +} |
| 104 | + |
| 105 | +func (s *SubFS) SetXattr(path string, attr string, data []byte) error { |
| 106 | + fullPath := filepath.Join(s.Root, path) |
| 107 | + return s.FS.SetXattr(fullPath, attr, data) |
| 108 | +} |
| 109 | +func (s *SubFS) GetXattr(path string, attr string) ([]byte, error) { |
| 110 | + fullPath := filepath.Join(s.Root, path) |
| 111 | + return s.FS.GetXattr(fullPath, attr) |
| 112 | +} |
| 113 | + |
| 114 | +func (s *SubFS) RemoveXattr(path string, attr string) error { |
| 115 | + fullPath := filepath.Join(s.Root, path) |
| 116 | + return s.FS.RemoveXattr(fullPath, attr) |
| 117 | +} |
| 118 | + |
| 119 | +func (s *SubFS) ListXattrs(path string) (map[string][]byte, error) { |
| 120 | + fullPath := filepath.Join(s.Root, path) |
| 121 | + return s.FS.ListXattrs(fullPath) |
| 122 | +} |
| 123 | + |
| 124 | +func (s *SubFS) Sub(path string) (FullFS, error) { |
| 125 | + if !fs.ValidPath(path) { |
| 126 | + return nil, &fs.PathError{Op: "sub", Path: path, Err: fs.ErrInvalid} |
| 127 | + } |
| 128 | + |
| 129 | + cleanPath := filepath.Clean(path) |
| 130 | + |
| 131 | + if cleanPath == "." { |
| 132 | + return s, nil |
| 133 | + } |
| 134 | + |
| 135 | + fullPath := filepath.Join(s.Root, cleanPath) |
| 136 | + info, err := s.FS.Stat(fullPath) |
| 137 | + if err != nil { |
| 138 | + return nil, err |
| 139 | + } |
| 140 | + if !info.IsDir() { |
| 141 | + return nil, errors.New("not a directory") |
| 142 | + } |
| 143 | + |
| 144 | + return &SubFS{ |
| 145 | + FS: s.FS, |
| 146 | + Root: fullPath, |
| 147 | + }, nil |
| 148 | +} |
0 commit comments