Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export cephfs List and Names functions #326

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions cephfs/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,24 +191,19 @@ func (dir *Directory) RewindDir() {
C.ceph_rewinddir(dir.mount.mount, dir.dir)
}

// dirEntries provides a convenient wrapper around slices of DirEntry items.
// For example, use the Names() call to easily get only the names from a
// DirEntry slice.
type dirEntries []*DirEntry

// list returns all the contents of a directory as a dirEntries slice.
// List returns all the contents of a directory as a dirEntries slice.
//
// list is implemented using ReadDir. If any of the calls to ReadDir returns
// List is implemented using ReadDir. If any of the calls to ReadDir returns
// an error List will return an error. However, all previous entries
// collected will still be returned. Callers of this function may want to check
// the entries return value even when an error is returned.
// List rewinds the handle every time it is called to get a full
// listing of directory contents.
func (dir *Directory) list() (dirEntries, error) {
func (dir *Directory) List() ([]*DirEntry, error) {
var (
err error
entry *DirEntry
entries = make(dirEntries, 0)
entries = make([]*DirEntry, 0)
)
dir.RewindDir()
for {
Expand All @@ -221,8 +216,8 @@ func (dir *Directory) list() (dirEntries, error) {
return entries, err
}

// names returns a slice of only the name fields from dir entries.
func (entries dirEntries) names() []string {
// Names returns a slice of only the name fields from dir entries.
func Names(entries []*DirEntry) []string {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're going to generalize the function we should probably make it generalize for both the readdir (DirEntry) and readdir_plus (DirEntryPlus) cases. We can probably do something like:

type NamedEntry interface {
    Name() string
}

func Names(entries []NamedEntry) []string {
   // ... impl ...
}

names := make([]string, len(entries))
for i, v := range entries {
names[i] = v.Name()
Expand Down
16 changes: 8 additions & 8 deletions cephfs/directory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ func TestDirectoryList(t *testing.T) {
assert.NotNil(t, dir)
defer func() { assert.NoError(t, dir.Close()) }()

entries, err := dir.list()
entries, err := dir.List()
assert.NoError(t, err)
assert.Greater(t, len(entries), 1)
found := entries.names()
found := Names(entries)
assert.Contains(t, found, "base")
})
t.Run("dir1", func(t *testing.T) {
Expand All @@ -136,10 +136,10 @@ func TestDirectoryList(t *testing.T) {
assert.NotNil(t, dir)
defer func() { assert.NoError(t, dir.Close()) }()

entries, err := dir.list()
entries, err := dir.List()
assert.NoError(t, err)
assert.Greater(t, len(entries), 1)
found := entries.names()
found := Names(entries)
assert.Subset(t, found, subdirs)
})
t.Run("dir1Twice", func(t *testing.T) {
Expand All @@ -148,18 +148,18 @@ func TestDirectoryList(t *testing.T) {
assert.NotNil(t, dir)
defer func() { assert.NoError(t, dir.Close()) }()

entries, err := dir.list()
entries, err := dir.List()
assert.NoError(t, err)
assert.Greater(t, len(entries), 1)
found := entries.names()
found := Names(entries)
assert.Subset(t, found, subdirs)

// verify that calling list gives a complete list
// even after being used for the same directory already
entries, err = dir.list()
entries, err = dir.List()
assert.NoError(t, err)
assert.Greater(t, len(entries), 1)
found = entries.names()
found = Names(entries)
assert.Subset(t, found, subdirs)
})
}
Expand Down