From 942cd318940b12e1c13b17e47de62ac56944e8ea Mon Sep 17 00:00:00 2001 From: Javi Fontan Date: Mon, 17 Dec 2018 22:27:58 +0100 Subject: [PATCH 1/5] Add ReadDir and fix test directory deletion Signed-off-by: Javi Fontan --- filesystem.go | 26 ++++++++++++++++++++++---- filesystem_test.go | 5 +++-- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/filesystem.go b/filesystem.go index 1b4f59f..6905cbc 100644 --- a/filesystem.go +++ b/filesystem.go @@ -1,7 +1,6 @@ package gluster import ( - "fmt" "os" "path/filepath" @@ -135,10 +134,29 @@ func (g *FS) Join(elem ...string) string { return filepath.Join(elem...) } -// ReadDir is not implemented by the underlying library. Added so billy.Dir -// is implemented as it is needed by tests. +// ReadDir implements billy.Dir interface. func (g *FS) ReadDir(path string) ([]os.FileInfo, error) { - return nil, fmt.Errorf("ReadDir not implemented") + d, err := g.v.Open(path) + if err != nil { + return nil, err + } + + defer d.Close() + + files, err := d.Readdir(0) + if err != nil { + return nil, err + } + + // gluster Readdir returns also "." and ".." + clean := make([]os.FileInfo, 0, len(files)) + for _, f := range files { + if f.Name() != "." && f.Name() != ".." { + clean = append(clean, f) + } + } + + return clean, nil } // MkdirAll implements billy.Dir interface. diff --git a/filesystem_test.go b/filesystem_test.go index 69572f7..f9906a0 100644 --- a/filesystem_test.go +++ b/filesystem_test.go @@ -1,7 +1,6 @@ package gluster import ( - "os" "testing" . "gopkg.in/check.v1" @@ -16,6 +15,7 @@ var _ = Suite(&FilesystemSuite{}) type FilesystemSuite struct { test.BasicSuite + test.DirSuite FS *FS tmp string @@ -30,11 +30,12 @@ func (s *FilesystemSuite) SetUpTest(c *C) { tmp := chroot.New(fs, s.tmp) s.BasicSuite.FS = tmp + s.DirSuite.FS = tmp } func (s *FilesystemSuite) TearDownTest(c *C) { if s.FS != nil { - err := os.RemoveAll(s.tmp) + err := util.RemoveAll(s.FS, s.tmp) c.Assert(err, IsNil) err = s.FS.Close() From ec43ee567b0239913b97872c1f8d89ba92034fa0 Mon Sep 17 00:00:00 2001 From: Javi Fontan Date: Fri, 21 Dec 2018 18:06:35 +0100 Subject: [PATCH 2/5] travis: specify go-billy version for testing It fixes a problem with directory permissions in test suite. Signed-off-by: Javi Fontan --- .travis.yml | 2 ++ Gopkg.testing | 4 ++++ 2 files changed, 6 insertions(+) create mode 100644 Gopkg.testing diff --git a/.travis.yml b/.travis.yml index 50350b8..a10b6cb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,6 +19,8 @@ before_script: - make container script: + - cp Gopkg.testing Gopkg.toml + - make godep - make dependencies - make test-coverage - make codecov diff --git a/Gopkg.testing b/Gopkg.testing new file mode 100644 index 0000000..5af3129 --- /dev/null +++ b/Gopkg.testing @@ -0,0 +1,4 @@ +[[constraint]] + name = "gopkg.in/src-d/go-billy.v4" + source = "github.com/src-d/go-billy" + revision = "40f7491e8a4a5cf7ab1d17f7679d94938ca4a714" From b87a1d5877367c4096206a8a26bd9de0a22f6a83 Mon Sep 17 00:00:00 2001 From: Javi Fontan Date: Fri, 21 Dec 2018 18:18:30 +0100 Subject: [PATCH 3/5] readme: add Readdir to the list of supported functions Signed-off-by: Javi Fontan --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index c071fae..d7fc264 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ So far, following actions are implemented: * `Stat` * `Rename` * `Remove` +* `Readdir` * `MkdirAll` For more information head to the [documentation](https://godoc.org/gopkg.in/src-d/go-billy-gluster.v0) From 84379ad5dcd876854066bc8ca8a42da8912dd59a Mon Sep 17 00:00:00 2001 From: Javi Fontan Date: Thu, 3 Jan 2019 12:46:29 +0100 Subject: [PATCH 4/5] Fix bug deleting directories Also fixed deletion of test dirs Signed-off-by: Javi Fontan --- filesystem.go | 14 +++++++++++++- filesystem_test.go | 11 +++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/filesystem.go b/filesystem.go index 6905cbc..13493ef 100644 --- a/filesystem.go +++ b/filesystem.go @@ -3,6 +3,7 @@ package gluster import ( "os" "path/filepath" + "syscall" "github.com/gluster/gogfapi/gfapi" "gopkg.in/src-d/go-billy.v4" @@ -126,7 +127,18 @@ func (g *FS) Rename(oldpath string, newpath string) error { // Remove implements billy.Basic interface. func (g *FS) Remove(filename string) error { - return g.v.Unlink(filename) + err := g.v.Unlink(filename) + if err == nil { + return nil + } + + if e, ok := err.(*os.PathError); ok { + if e.Err == syscall.EISDIR { + return g.v.Rmdir(filename) + } + } + + return err } // Join implements billy.Basic interface. diff --git a/filesystem_test.go b/filesystem_test.go index f9906a0..dd94bce 100644 --- a/filesystem_test.go +++ b/filesystem_test.go @@ -24,6 +24,7 @@ type FilesystemSuite struct { func (s *FilesystemSuite) SetUpTest(c *C) { fs, err := New("localhost", "billy") c.Assert(err, IsNil) + s.FS = fs s.tmp, err = util.TempDir(fs, "", "billy") c.Assert(err, IsNil) @@ -42,3 +43,13 @@ func (s *FilesystemSuite) TearDownTest(c *C) { c.Assert(err, IsNil) } } + +func (s *FilesystemSuite) TestReaddirEmpty(c *C) { + fs := s.DirSuite.FS + err := fs.MkdirAll("test", 0777) + c.Assert(err, IsNil) + + files, err := fs.ReadDir("test") + c.Assert(err, IsNil) + c.Assert(len(files), Equals, 0) +} From b445dff9892172d39146ea438266012e712a05ca Mon Sep 17 00:00:00 2001 From: Javi Fontan Date: Thu, 3 Jan 2019 12:50:14 +0100 Subject: [PATCH 5/5] Improve Readdir so it does not copy the whole file list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit By: Kuba Podgórski Signed-off-by: Javi Fontan --- filesystem.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/filesystem.go b/filesystem.go index 13493ef..9268ff5 100644 --- a/filesystem.go +++ b/filesystem.go @@ -161,14 +161,17 @@ func (g *FS) ReadDir(path string) ([]os.FileInfo, error) { } // gluster Readdir returns also "." and ".." - clean := make([]os.FileInfo, 0, len(files)) - for _, f := range files { - if f.Name() != "." && f.Name() != ".." { - clean = append(clean, f) + n := len(files) + for i := 0; i < n; i++ { + if files[i].Name() == "." || files[i].Name() == ".." { + // swap with the last element + files[i], files[n-1] = files[n-1], files[i] + n-- + i-- } } - return clean, nil + return files[:n], nil } // MkdirAll implements billy.Dir interface.