Skip to content

Commit

Permalink
Merge pull request #2 from jfontan/readdir
Browse files Browse the repository at this point in the history
Add ReadDir and fix test directory deletion
  • Loading branch information
jfontan committed Jan 3, 2019
2 parents c3fa76f + b445dff commit 130898d
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Expand Up @@ -19,6 +19,8 @@ before_script:
- make container

script:
- cp Gopkg.testing Gopkg.toml
- make godep
- make dependencies
- make test-coverage
- make codecov
4 changes: 4 additions & 0 deletions Gopkg.testing
@@ -0,0 +1,4 @@
[[constraint]]
name = "gopkg.in/src-d/go-billy.v4"
source = "github.com/src-d/go-billy"
revision = "40f7491e8a4a5cf7ab1d17f7679d94938ca4a714"
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -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)
Expand Down
43 changes: 38 additions & 5 deletions filesystem.go
@@ -1,9 +1,9 @@
package gluster

import (
"fmt"
"os"
"path/filepath"
"syscall"

"github.com/gluster/gogfapi/gfapi"
"gopkg.in/src-d/go-billy.v4"
Expand Down Expand Up @@ -127,18 +127,51 @@ 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.
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 ".."
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 files[:n], nil
}

// MkdirAll implements billy.Dir interface.
Expand Down
16 changes: 14 additions & 2 deletions filesystem_test.go
@@ -1,7 +1,6 @@
package gluster

import (
"os"
"testing"

. "gopkg.in/check.v1"
Expand All @@ -16,6 +15,7 @@ var _ = Suite(&FilesystemSuite{})

type FilesystemSuite struct {
test.BasicSuite
test.DirSuite

FS *FS
tmp string
Expand All @@ -24,20 +24,32 @@ 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)

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()
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)
}

0 comments on commit 130898d

Please sign in to comment.