| 1 | package fsutil |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "io" |
| 6 | gofs "io/fs" |
| 7 | "os" |
| 8 | "path" |
| 9 | "path/filepath" |
| 10 | "sort" |
| 11 | "strings" |
| 12 | "syscall" |
| 13 | "time" |
| 14 | |
| 15 | "github.com/pkg/errors" |
| 16 | "github.com/tonistiigi/fsutil/types" |
| 17 | ) |
| 18 | |
| 19 | type FS interface { |
| 20 | Walk(context.Context, string, gofs.WalkDirFunc) error |
| 21 | Open(string) (io.ReadCloser, error) |
| 22 | } |
| 23 | |
| 24 | // NewFS creates a new FS from a root directory on the host filesystem. |
| 25 | func NewFS(root string) (FS, error) { |
| 26 | root, err := filepath.EvalSymlinks(root) |
| 27 | if err != nil { |
| 28 | return nil, errors.WithStack(&os.PathError{Op: "resolve", Path: root, Err: err}) |
| 29 | } |
| 30 | fi, err := os.Stat(root) |
| 31 | if err != nil { |
| 32 | return nil, err |
| 33 | } |
| 34 | if !fi.IsDir() { |
| 35 | return nil, errors.WithStack(&os.PathError{Op: "stat", Path: root, Err: syscall.ENOTDIR}) |
| 36 | } |
| 37 | |
| 38 | return &fs{ |
| 39 | root: root, |
| 40 | }, nil |
| 41 | } |
| 42 | |
| 43 | type fs struct { |
| 44 | root string |
| 45 | } |
| 46 | |
| 47 | func (fs *fs) Walk(ctx context.Context, target string, fn gofs.WalkDirFunc) error { |
| 48 | seenFiles := make(map[uint64]string) |
| 49 | return filepath.WalkDir(filepath.Join(fs.root, target), func(path string, dirEntry gofs.DirEntry, walkErr error) (retErr error) { |
| 50 | defer func() { |
| 51 | if retErr != nil && isNotExist(retErr) { |
| 52 | retErr = filepath.SkipDir |
| 53 | } |
| 54 | }() |
| 55 | |
| 56 | origpath := path |
| 57 | path, err := filepath.Rel(fs.root, path) |
| 58 | if err != nil { |
| 59 | return err |
| 60 | } |
| 61 | // Skip root |
| 62 | if path == "." { |
| 63 | return nil |
| 64 | } |
| 65 | |
| 66 | var entry gofs.DirEntry |
| 67 | if dirEntry != nil { |
| 68 | entry = &DirEntryInfo{ |
| 69 | path: path, |
| 70 | origpath: origpath, |
| 71 | entry: dirEntry, |
| 72 | seenFiles: seenFiles, |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | select { |
| 77 | case <-ctx.Done(): |
| 78 | return ctx.Err() |
| 79 | default: |
| 80 | if err := fn(path, entry, walkErr); err != nil { |
| 81 | return err |
| 82 | } |
| 83 | } |
| 84 | return nil |
| 85 | }) |
| 86 | } |
| 87 | |
| 88 | func (fs *fs) Open(p string) (io.ReadCloser, error) { |
| 89 | rc, err := os.Open(filepath.Join(fs.root, p)) |
| 90 | return rc, errors.WithStack(err) |
| 91 | } |
| 92 | |
| 93 | type Dir struct { |
| 94 | Stat *types.Stat |
| 95 | FS FS |
| 96 | } |
| 97 | |
| 98 | func SubDirFS(dirs []Dir) (FS, error) { |
| 99 | sort.Slice(dirs, func(i, j int) bool { |
| 100 | return dirs[i].Stat.Path < dirs[j].Stat.Path |
| 101 | }) |
| 102 | m := map[string]Dir{} |
| 103 | for _, d := range dirs { |
| 104 | if path.Base(d.Stat.Path) != d.Stat.Path { |
| 105 | return nil, errors.WithStack(&os.PathError{Path: d.Stat.Path, Err: syscall.EISDIR, Op: "invalid path"}) |
| 106 | } |
| 107 | if _, ok := m[d.Stat.Path]; ok { |
| 108 | return nil, errors.WithStack(&os.PathError{Path: d.Stat.Path, Err: syscall.EEXIST, Op: "duplicate path"}) |
| 109 | } |
| 110 | m[d.Stat.Path] = d |
| 111 | } |
| 112 | return &subDirFS{m: m, dirs: dirs}, nil |
| 113 | } |
| 114 | |
| 115 | type subDirFS struct { |
| 116 | m map[string]Dir |
| 117 | dirs []Dir |
| 118 | } |
| 119 | |
| 120 | func (fs *subDirFS) Walk(ctx context.Context, target string, fn gofs.WalkDirFunc) error { |
| 121 | first, rest, _ := strings.Cut(target, string(filepath.Separator)) |
| 122 | |
| 123 | for _, d := range fs.dirs { |
| 124 | if first != "" && first != d.Stat.Path { |
| 125 | continue |
| 126 | } |
| 127 | |
| 128 | fi := &StatInfo{d.Stat.Clone()} |
| 129 | if !fi.IsDir() { |
| 130 | return errors.WithStack(&os.PathError{Path: d.Stat.Path, Err: syscall.ENOTDIR, Op: "walk subdir"}) |
| 131 | } |
| 132 | dStat := d.Stat.Clone() |
| 133 | if err := fn(d.Stat.Path, &DirEntryInfo{Stat: dStat}, nil); err != nil { |
| 134 | return err |
| 135 | } |
| 136 | if err := d.FS.Walk(ctx, rest, func(p string, entry gofs.DirEntry, err error) error { |
| 137 | if err != nil { |
| 138 | return err |
| 139 | } |
| 140 | |
| 141 | fi, err := entry.Info() |
| 142 | if err != nil { |
| 143 | return err |
| 144 | } |
| 145 | stat, ok := fi.Sys().(*types.Stat) |
| 146 | if !ok { |
| 147 | return errors.WithStack(&os.PathError{Path: d.Stat.Path, Err: syscall.EBADMSG, Op: "fileinfo without stat info"}) |
| 148 | } |
| 149 | stat.Path = path.Join(d.Stat.Path, stat.Path) |
| 150 | if stat.Linkname != "" { |
| 151 | if fi.Mode()&os.ModeSymlink != 0 { |
| 152 | if strings.HasPrefix(stat.Linkname, "/") { |
| 153 | stat.Linkname = path.Join("/"+d.Stat.Path, stat.Linkname) |
| 154 | } |
| 155 | } else { |
| 156 | stat.Linkname = path.Join(d.Stat.Path, stat.Linkname) |
| 157 | } |
| 158 | } |
| 159 | return fn(filepath.Join(d.Stat.Path, p), &DirEntryInfo{Stat: stat}, nil) |
| 160 | }); err != nil { |
| 161 | return err |
| 162 | } |
| 163 | } |
| 164 | return nil |
| 165 | } |
| 166 | |
| 167 | func (fs *subDirFS) Open(p string) (io.ReadCloser, error) { |
| 168 | parts := strings.SplitN(filepath.Clean(p), string(filepath.Separator), 2) |
| 169 | if len(parts) == 0 { |
| 170 | return io.NopCloser(&emptyReader{}), nil |
| 171 | } |
| 172 | d, ok := fs.m[parts[0]] |
| 173 | if !ok { |
| 174 | return nil, errors.WithStack(&os.PathError{Path: parts[0], Err: syscall.ENOENT, Op: "open"}) |
| 175 | } |
| 176 | return d.FS.Open(parts[1]) |
| 177 | } |
| 178 | |
| 179 | type emptyReader struct{} |
| 180 | |
| 181 | func (*emptyReader) Read([]byte) (int, error) { |
| 182 | return 0, io.EOF |
| 183 | } |
| 184 | |
| 185 | type StatInfo struct { |
| 186 | *types.Stat |
| 187 | } |
| 188 | |
| 189 | func (s *StatInfo) Name() string { |
| 190 | return filepath.Base(s.Stat.Path) |
| 191 | } |
| 192 | |
| 193 | func (s *StatInfo) Size() int64 { |
| 194 | return s.Stat.Size |
| 195 | } |
| 196 | |
| 197 | func (s *StatInfo) Mode() os.FileMode { |
| 198 | return os.FileMode(s.Stat.Mode) |
| 199 | } |
| 200 | |
| 201 | func (s *StatInfo) ModTime() time.Time { |
| 202 | return time.Unix(s.Stat.ModTime/1e9, s.Stat.ModTime%1e9) |
| 203 | } |
| 204 | |
| 205 | func (s *StatInfo) IsDir() bool { |
| 206 | return s.Mode().IsDir() |
| 207 | } |
| 208 | |
| 209 | func (s *StatInfo) Sys() interface{} { |
| 210 | return s.Stat |
| 211 | } |
| 212 | |
| 213 | type DirEntryInfo struct { |
| 214 | *types.Stat |
| 215 | |
| 216 | entry gofs.DirEntry |
| 217 | path string |
| 218 | origpath string |
| 219 | seenFiles map[uint64]string |
| 220 | } |
| 221 | |
| 222 | func (s *DirEntryInfo) Name() string { |
| 223 | if s.Stat != nil { |
| 224 | return filepath.Base(s.Stat.Path) |
| 225 | } |
| 226 | return s.entry.Name() |
| 227 | } |
| 228 | |
| 229 | func (s *DirEntryInfo) IsDir() bool { |
| 230 | if s.Stat != nil { |
| 231 | return s.Stat.IsDir() |
| 232 | } |
| 233 | return s.entry.IsDir() |
| 234 | } |
| 235 | |
| 236 | func (s *DirEntryInfo) Type() gofs.FileMode { |
| 237 | if s.Stat != nil { |
| 238 | return gofs.FileMode(s.Stat.Mode) |
| 239 | } |
| 240 | return s.entry.Type() |
| 241 | } |
| 242 | |
| 243 | func (s *DirEntryInfo) Info() (gofs.FileInfo, error) { |
| 244 | if s.Stat == nil { |
| 245 | fi, err := s.entry.Info() |
| 246 | if err != nil { |
| 247 | return nil, err |
| 248 | } |
| 249 | stat, err := mkstat(s.origpath, s.path, fi, s.seenFiles) |
| 250 | if err != nil { |
| 251 | return nil, err |
| 252 | } |
| 253 | s.Stat = stat |
| 254 | } |
| 255 | |
| 256 | st := s.Stat.Clone() |
| 257 | return &StatInfo{st}, nil |
| 258 | } |