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 1 commit
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
11 changes: 3 additions & 8 deletions cephfs/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,6 @@ 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 is implemented using ReadDir. If any of the calls to ReadDir returns
Expand All @@ -204,11 +199,11 @@ type dirEntries []*DirEntry
// 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 @@ -222,7 +217,7 @@ func (dir *Directory) List() (dirEntries, error) {
}

// Names returns a slice of only the name fields from dir entries.
func (entries dirEntries) Names() []string {
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
8 changes: 4 additions & 4 deletions cephfs/directory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func TestDirectoryList(t *testing.T) {
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 @@ -139,7 +139,7 @@ func TestDirectoryList(t *testing.T) {
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 @@ -151,15 +151,15 @@ func TestDirectoryList(t *testing.T) {
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()
assert.NoError(t, err)
assert.Greater(t, len(entries), 1)
found = entries.Names()
found = Names(entries)
assert.Subset(t, found, subdirs)
})
}
Expand Down