2018-08-02 14:09:51 -04:00
|
|
|
package fsutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
2019-04-03 00:55:22 -04:00
|
|
|
"io/ioutil"
|
2018-08-02 14:09:51 -04:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2019-04-03 00:55:22 -04:00
|
|
|
"sort"
|
2018-08-02 14:09:51 -04:00
|
|
|
"strings"
|
2020-11-12 21:14:57 -05:00
|
|
|
"syscall"
|
2018-08-02 14:09:51 -04:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2018-10-05 05:04:52 -04:00
|
|
|
"github.com/tonistiigi/fsutil/types"
|
2018-08-02 14:09:51 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type FS interface {
|
|
|
|
Walk(context.Context, filepath.WalkFunc) error
|
|
|
|
Open(string) (io.ReadCloser, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFS(root string, opt *WalkOpt) FS {
|
|
|
|
return &fs{
|
|
|
|
root: root,
|
|
|
|
opt: opt,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type fs struct {
|
|
|
|
root string
|
|
|
|
opt *WalkOpt
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *fs) Walk(ctx context.Context, fn filepath.WalkFunc) error {
|
|
|
|
return Walk(ctx, fs.root, fs.opt, fn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *fs) Open(p string) (io.ReadCloser, error) {
|
2020-11-12 21:14:57 -05:00
|
|
|
rc, err := os.Open(filepath.Join(fs.root, p))
|
|
|
|
return rc, errors.WithStack(err)
|
2018-08-02 14:09:51 -04:00
|
|
|
}
|
|
|
|
|
2019-04-03 00:55:22 -04:00
|
|
|
type Dir struct {
|
|
|
|
Stat types.Stat
|
|
|
|
FS FS
|
|
|
|
}
|
|
|
|
|
|
|
|
func SubDirFS(dirs []Dir) (FS, error) {
|
|
|
|
sort.Slice(dirs, func(i, j int) bool {
|
|
|
|
return dirs[i].Stat.Path < dirs[j].Stat.Path
|
|
|
|
})
|
|
|
|
m := map[string]Dir{}
|
|
|
|
for _, d := range dirs {
|
|
|
|
if path.Base(d.Stat.Path) != d.Stat.Path {
|
2020-11-12 21:14:57 -05:00
|
|
|
return nil, errors.WithStack(&os.PathError{Path: d.Stat.Path, Err: syscall.EISDIR, Op: "invalid path"})
|
2019-04-03 00:55:22 -04:00
|
|
|
}
|
|
|
|
if _, ok := m[d.Stat.Path]; ok {
|
2020-11-12 21:14:57 -05:00
|
|
|
return nil, errors.WithStack(&os.PathError{Path: d.Stat.Path, Err: syscall.EEXIST, Op: "duplicate path"})
|
2019-04-03 00:55:22 -04:00
|
|
|
}
|
|
|
|
m[d.Stat.Path] = d
|
|
|
|
}
|
|
|
|
return &subDirFS{m: m, dirs: dirs}, nil
|
2018-08-02 14:09:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type subDirFS struct {
|
2019-04-03 00:55:22 -04:00
|
|
|
m map[string]Dir
|
|
|
|
dirs []Dir
|
2018-08-02 14:09:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *subDirFS) Walk(ctx context.Context, fn filepath.WalkFunc) error {
|
2019-04-03 00:55:22 -04:00
|
|
|
for _, d := range fs.dirs {
|
|
|
|
fi := &StatInfo{Stat: &d.Stat}
|
|
|
|
if !fi.IsDir() {
|
2020-11-12 21:14:57 -05:00
|
|
|
return errors.WithStack(&os.PathError{Path: d.Stat.Path, Err: syscall.ENOTDIR, Op: "walk subdir"})
|
2019-04-03 00:55:22 -04:00
|
|
|
}
|
|
|
|
if err := fn(d.Stat.Path, fi, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := d.FS.Walk(ctx, func(p string, fi os.FileInfo, err error) error {
|
|
|
|
stat, ok := fi.Sys().(*types.Stat)
|
|
|
|
if !ok {
|
2020-11-12 21:14:57 -05:00
|
|
|
return errors.WithStack(&os.PathError{Path: d.Stat.Path, Err: syscall.EBADMSG, Op: "fileinfo without stat info"})
|
2019-04-03 00:55:22 -04:00
|
|
|
}
|
|
|
|
stat.Path = path.Join(d.Stat.Path, stat.Path)
|
|
|
|
if stat.Linkname != "" {
|
|
|
|
if fi.Mode()&os.ModeSymlink != 0 {
|
|
|
|
if strings.HasPrefix(stat.Linkname, "/") {
|
|
|
|
stat.Linkname = path.Join("/"+d.Stat.Path, stat.Linkname)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
stat.Linkname = path.Join(d.Stat.Path, stat.Linkname)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fn(filepath.Join(d.Stat.Path, p), &StatInfo{stat}, nil)
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-08-02 14:09:51 -04:00
|
|
|
}
|
2019-04-03 00:55:22 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *subDirFS) Open(p string) (io.ReadCloser, error) {
|
|
|
|
parts := strings.SplitN(filepath.Clean(p), string(filepath.Separator), 2)
|
|
|
|
if len(parts) == 0 {
|
|
|
|
return ioutil.NopCloser(&emptyReader{}), nil
|
2018-08-02 14:09:51 -04:00
|
|
|
}
|
2019-04-03 00:55:22 -04:00
|
|
|
d, ok := fs.m[parts[0]]
|
|
|
|
if !ok {
|
2020-11-12 21:14:57 -05:00
|
|
|
return nil, errors.WithStack(&os.PathError{Path: parts[0], Err: syscall.ENOENT, Op: "open"})
|
2018-08-02 14:09:51 -04:00
|
|
|
}
|
2019-04-03 00:55:22 -04:00
|
|
|
return d.FS.Open(parts[1])
|
2018-08-02 14:09:51 -04:00
|
|
|
}
|
|
|
|
|
2019-04-03 00:55:22 -04:00
|
|
|
type emptyReader struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*emptyReader) Read([]byte) (int, error) {
|
|
|
|
return 0, io.EOF
|
2018-08-02 14:09:51 -04:00
|
|
|
}
|