From fd30d2ba530d069efe43c6acf9e0ca2ba30820d3 Mon Sep 17 00:00:00 2001 From: Ivan Trubach Date: Tue, 18 Aug 2020 01:10:13 +0300 Subject: [PATCH 1/2] Added util.Stat that returns os.FileInfo --- helper/chroot/chroot.go | 19 +++++++++++++++++++ util/util.go | 9 +++++++++ 2 files changed, 28 insertions(+) diff --git a/helper/chroot/chroot.go b/helper/chroot/chroot.go index 8b44e78..da5da74 100644 --- a/helper/chroot/chroot.go +++ b/helper/chroot/chroot.go @@ -7,6 +7,7 @@ import ( "github.com/go-git/go-billy/v5" "github.com/go-git/go-billy/v5/helper/polyfill" + "github.com/go-git/go-billy/v5/util" ) // ChrootHelper is a helper to implement billy.Chroot. @@ -222,6 +223,15 @@ func (fs *ChrootHelper) Capabilities() billy.Capability { return billy.Capabilities(fs.underlying) } +type fileInfo struct { + os.FileInfo + name string +} + +func (fi *fileInfo) Name() string { + return fi.name +} + type file struct { billy.File name string @@ -240,3 +250,12 @@ func newFile(fs billy.Filesystem, f billy.File, filename string) billy.File { func (f *file) Name() string { return f.name } + +func (f *file) Stat() (os.FileInfo, error) { + fi, err := util.Stat(f.File) + if err != nil { + return fi, err + } + name := f.Name() + return &fileInfo{fi, name}, nil +} diff --git a/util/util.go b/util/util.go index 34c1d9e..645ee35 100644 --- a/util/util.go +++ b/util/util.go @@ -207,6 +207,15 @@ func TempDir(fs billy.Dir, dir, prefix string) (name string, err error) { return } +// Stat returns the os.FileInfo structure describing file. +func Stat(f billy.File) (os.FileInfo, error) { + fi, ok := f.(interface{ Stat() (os.FileInfo, error) }) + if !ok { + return nil, billy.ErrNotSupported + } + return fi.Stat() +} + type underlying interface { Underlying() billy.Basic } From 4055a2947b210a6d63a766690539237198fa52c4 Mon Sep 17 00:00:00 2001 From: Ivan Trubach Date: Tue, 18 Aug 2020 02:24:14 +0300 Subject: [PATCH 2/2] Move Stat from util to root billy package --- fs.go | 9 +++++++++ helper/chroot/chroot.go | 3 +-- util/util.go | 9 --------- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/fs.go b/fs.go index a9efccd..d0f7fc8 100644 --- a/fs.go +++ b/fs.go @@ -200,3 +200,12 @@ func CapabilityCheck(fs Basic, capabilities Capability) bool { fsCaps := Capabilities(fs) return fsCaps&capabilities == capabilities } + +// Stat returns the os.FileInfo structure describing file. +func Stat(f File) (os.FileInfo, error) { + fi, ok := f.(interface{ Stat() (os.FileInfo, error) }) + if !ok { + return nil, ErrNotSupported + } + return fi.Stat() +} diff --git a/helper/chroot/chroot.go b/helper/chroot/chroot.go index da5da74..8321914 100644 --- a/helper/chroot/chroot.go +++ b/helper/chroot/chroot.go @@ -7,7 +7,6 @@ import ( "github.com/go-git/go-billy/v5" "github.com/go-git/go-billy/v5/helper/polyfill" - "github.com/go-git/go-billy/v5/util" ) // ChrootHelper is a helper to implement billy.Chroot. @@ -252,7 +251,7 @@ func (f *file) Name() string { } func (f *file) Stat() (os.FileInfo, error) { - fi, err := util.Stat(f.File) + fi, err := billy.Stat(f.File) if err != nil { return fi, err } diff --git a/util/util.go b/util/util.go index 645ee35..34c1d9e 100644 --- a/util/util.go +++ b/util/util.go @@ -207,15 +207,6 @@ func TempDir(fs billy.Dir, dir, prefix string) (name string, err error) { return } -// Stat returns the os.FileInfo structure describing file. -func Stat(f billy.File) (os.FileInfo, error) { - fi, ok := f.(interface{ Stat() (os.FileInfo, error) }) - if !ok { - return nil, billy.ErrNotSupported - } - return fi.Stat() -} - type underlying interface { Underlying() billy.Basic }