From 6463dbba07e3f336a556c9b6870fc3f32462ba84 Mon Sep 17 00:00:00 2001 From: Mark Bates Date: Sun, 1 Sep 2019 16:18:39 -0400 Subject: [PATCH] guarded by monkeys --- fs/file.go | 2 +- fs/fs.go | 3 +++ fs/fstest/suite.go | 1 - fs/hdfs/file.go | 47 +++++++++++++++++++++++----------------------- fs/hdfs/hdfs.go | 44 ++++++++++++++++++++++++++----------------- fs/memfs/file.go | 4 ++-- fs/memfs/memfs.go | 12 ++++++++++++ 7 files changed, 69 insertions(+), 44 deletions(-) diff --git a/fs/file.go b/fs/file.go index d558b7b..428c51f 100644 --- a/fs/file.go +++ b/fs/file.go @@ -9,7 +9,7 @@ import ( type File interface { Close() error - FilePath() string + Abs() (string, error) Info() here.Info Name() string Open(name string) (http.File, error) diff --git a/fs/fs.go b/fs/fs.go index 5985f04..c881d78 100644 --- a/fs/fs.go +++ b/fs/fs.go @@ -9,6 +9,9 @@ import ( type FileSystem interface { Parse(p string) (Path, error) + Abs(string) (string, error) + AbsPath(Path) (string, error) + Current() (here.Info, error) Info(p string) (here.Info, error) diff --git a/fs/fstest/suite.go b/fs/fstest/suite.go index ddeef75..e1a5793 100644 --- a/fs/fstest/suite.go +++ b/fs/fstest/suite.go @@ -196,7 +196,6 @@ func (s *FileSystem) Test_Stat(t *testing.T) { pt, err := s.Parse(tt.in) r.NoError(err) - r.Fail(pt.String()) f, err := s.Create(tt.in) r.NoError(err) _, err = io.Copy(f, strings.NewReader("!"+pt.String())) diff --git a/fs/hdfs/file.go b/fs/hdfs/file.go index f2b366c..82f455e 100644 --- a/fs/hdfs/file.go +++ b/fs/hdfs/file.go @@ -3,7 +3,6 @@ package hdfs import ( "net/http" "os" - "strings" "github.com/markbates/pkger/fs" "github.com/markbates/pkger/here" @@ -13,22 +12,18 @@ var _ fs.File = &File{} type File struct { *os.File - filePath string - info *fs.FileInfo - her here.Info - path fs.Path - fs fs.FileSystem + info *fs.FileInfo + her here.Info + path fs.Path + fs fs.FileSystem } func NewFile(fx fs.FileSystem, osf *os.File) (*File, error) { - cur, err := fx.Current() + pt, err := fx.Parse(osf.Name()) if err != nil { return nil, err } - pt := fs.Path{ - Name: strings.TrimPrefix(osf.Name(), cur.Dir), - } info, err := osf.Stat() if err != nil { @@ -36,10 +31,9 @@ func NewFile(fx fs.FileSystem, osf *os.File) (*File, error) { } f := &File{ - File: osf, - filePath: info.Name(), - path: pt, - fs: fx, + File: osf, + path: pt, + fs: fx, } f.info = fs.WithName(pt.Name, info) @@ -55,8 +49,8 @@ func (f *File) Close() error { return f.File.Close() } -func (f *File) FilePath() string { - return f.filePath +func (f *File) Abs() (string, error) { + return f.fs.AbsPath(f.path) } func (f *File) Info() here.Info { @@ -76,12 +70,19 @@ func (f *File) Path() fs.Path { } func (f *File) Stat() (os.FileInfo, error) { - if f.info == nil { - info, err := os.Stat(f.filePath) - if err != nil { - return nil, err - } - f.info = fs.NewFileInfo(info) + if f.info != nil { + return f.info, nil } - return f.info, nil + + abs, err := f.Abs() + if err != nil { + return nil, err + } + + info, err := os.Stat(abs) + if err != nil { + return nil, err + } + f.info = fs.NewFileInfo(info) + return info, nil } diff --git a/fs/hdfs/hdfs.go b/fs/hdfs/hdfs.go index e9c41f4..1bcc889 100644 --- a/fs/hdfs/hdfs.go +++ b/fs/hdfs/hdfs.go @@ -20,6 +20,25 @@ type FS struct { current here.Info } +func (f *FS) Abs(p string) (string, error) { + pt, err := f.Parse(p) + if err != nil { + return "", err + } + return f.AbsPath(pt) +} + +func (f *FS) AbsPath(pt fs.Path) (string, error) { + if pt.Pkg == f.current.ImportPath { + return filepath.Join(f.current.Dir, pt.Name), nil + } + info, err := f.Info(pt.Pkg) + if err != nil { + return "", err + } + return filepath.Join(info.Dir, pt.Name), nil +} + func New() (*FS, error) { info, err := here.Current() if err != nil { @@ -35,7 +54,7 @@ func New() (*FS, error) { } func (fx *FS) Create(name string) (fs.File, error) { - name, err := fx.locate(name) + name, err := fx.Abs(name) if err != nil { return nil, err } @@ -68,7 +87,7 @@ func (f *FS) Info(p string) (here.Info, error) { } func (f *FS) MkdirAll(p string, perm os.FileMode) error { - p, err := f.locate(p) + p, err := f.Abs(p) if err != nil { return err } @@ -76,7 +95,7 @@ func (f *FS) MkdirAll(p string, perm os.FileMode) error { } func (fx *FS) Open(name string) (fs.File, error) { - name, err := fx.locate(name) + name, err := fx.Abs(name) if err != nil { return nil, err } @@ -92,7 +111,7 @@ func (f *FS) Parse(p string) (fs.Path, error) { } func (f *FS) ReadFile(s string) ([]byte, error) { - s, err := f.locate(s) + s, err := f.Abs(s) if err != nil { return nil, err } @@ -100,7 +119,7 @@ func (f *FS) ReadFile(s string) ([]byte, error) { } func (f *FS) Stat(name string) (os.FileInfo, error) { - name, err := f.locate(name) + name, err := f.Abs(name) if err != nil { return nil, err } @@ -108,7 +127,7 @@ func (f *FS) Stat(name string) (os.FileInfo, error) { } func (f *FS) Walk(p string, wf filepath.WalkFunc) error { - fp, err := f.locate(p) + fp, err := f.Abs(p) if err != nil { return err } @@ -133,17 +152,8 @@ func (f *FS) Walk(p string, wf filepath.WalkFunc) error { return err } -func (f *FS) locate(p string) (string, error) { - pt, err := f.Parse(p) - if err != nil { - return p, err - } - p = f.current.FilePath(pt.Name) - return p, nil -} - func (fx *FS) Remove(name string) error { - name, err := fx.locate(name) + name, err := fx.Abs(name) if err != nil { return err } @@ -151,7 +161,7 @@ func (fx *FS) Remove(name string) error { } func (fx *FS) RemoveAll(name string) error { - name, err := fx.locate(name) + name, err := fx.Abs(name) if err != nil { return err } diff --git a/fs/memfs/file.go b/fs/memfs/file.go index 612c12b..0ff8834 100644 --- a/fs/memfs/file.go +++ b/fs/memfs/file.go @@ -98,8 +98,8 @@ func (f File) Name() string { return f.info.Name() } -func (f File) FilePath() string { - return f.her.FilePath(f.Name()) +func (f File) Abs() (string, error) { + return f.fs.AbsPath(f.Path()) } func (f File) Path() fs.Path { diff --git a/fs/memfs/memfs.go b/fs/memfs/memfs.go index e254141..7abb9a5 100644 --- a/fs/memfs/memfs.go +++ b/fs/memfs/memfs.go @@ -38,6 +38,18 @@ type FS struct { current here.Info } +func (f *FS) Abs(p string) (string, error) { + pt, err := f.Parse(p) + if err != nil { + return "", err + } + return f.AbsPath(pt) +} + +func (f *FS) AbsPath(pt fs.Path) (string, error) { + return pt.String(), nil +} + func (f *FS) Current() (here.Info, error) { return f.current, nil }