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

add CloserListerAt #577

Merged
merged 1 commit into from
Apr 10, 2024
Merged
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
2 changes: 2 additions & 0 deletions request-interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ type NameLookupFileLister interface {
//
// If a populated entry implements [FileInfoExtendedData], extended attributes will also be returned to the client.
//
// The request server code will call Close() on ListerAt if an io.Closer type assertion succeeds.
//
// Note in cases of an error, the error text will be sent to the client.
type ListerAt interface {
ListAt([]os.FileInfo, int64) (int, error)
Expand Down
31 changes: 31 additions & 0 deletions request-server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,37 @@ func TestRequestReaddir(t *testing.T) {
checkRequestServerAllocator(t, p)
}

type testListerAtCloser struct {
isClosed bool
}

func (l *testListerAtCloser) ListAt([]os.FileInfo, int64) (int, error) {
return 0, io.EOF
}

func (l *testListerAtCloser) Close() error {
l.isClosed = true
return nil
}

func TestRequestServerListerAtCloser(t *testing.T) {
p := clientRequestServerPair(t)
defer p.Close()

handle, err := p.cli.opendir(context.Background(), "/")
require.NoError(t, err)
require.Len(t, p.svr.openRequests, 1)
req, ok := p.svr.getRequest(handle)
require.True(t, ok)
listerAt := &testListerAtCloser{}
req.setListerAt(listerAt)
assert.NotNil(t, req.state.getListerAt())
err = p.cli.close(handle)
assert.NoError(t, err)
require.Len(t, p.svr.openRequests, 0)
assert.True(t, listerAt.isClosed)
}

func TestRequestStatVFS(t *testing.T) {
if runtime.GOOS != "linux" && runtime.GOOS != "darwin" {
t.Skip("StatVFS is implemented on linux and darwin")
Expand Down
20 changes: 18 additions & 2 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,22 @@ func (s *state) getListerAt() ListerAt {
return s.listerAt
}

func (s *state) closeListerAt() error {
s.mu.Lock()
defer s.mu.Unlock()

var err error

if s.listerAt != nil {
if c, ok := s.listerAt.(io.Closer); ok {
err = c.Close()
}
s.listerAt = nil
}

return err
}

// Request contains the data and state for the incoming service request.
type Request struct {
// Get, Put, Setstat, Stat, Rename, Remove
Expand Down Expand Up @@ -230,9 +246,9 @@ func (r *Request) close() error {
}
}()

rd, wr, rw := r.getAllReaderWriters()
err := r.state.closeListerAt()

var err error
rd, wr, rw := r.getAllReaderWriters()

// Close errors on a Writer are far more likely to be the important one.
// As they can be information that there was a loss of data.
Expand Down