nfs fix close file on write
Some checks failed
docker / build-docker (linux/amd64) (push) Successful in 1m40s
docker / build-docker (linux/arm64) (push) Failing after 3h11m48s

This commit is contained in:
royalcat 2024-06-19 10:50:22 +03:00
parent 77d19e08fc
commit 0f7238c6a8
4 changed files with 4 additions and 6 deletions

View file

@ -26,10 +26,6 @@ type Filesystem interface {
// it if it already exists. If successful, methods on the returned File can // it if it already exists. If successful, methods on the returned File can
// be used for I/O; the associated file descriptor has mode O_RDWR. // be used for I/O; the associated file descriptor has mode O_RDWR.
Create(ctx context.Context, filename string) (File, error) Create(ctx context.Context, filename string) (File, error)
// Open opens the named file for reading. If successful, methods on the
// returned file can be used for reading; the associated file descriptor has
// mode O_RDONLY.
Open(ctx context.Context, filename string) (File, error)
// OpenFile is the generalized open call; most users will use Open or Create // OpenFile is the generalized open call; most users will use Open or Create
// instead. It opens the named file with specified flag (O_RDONLY etc.) and // instead. It opens the named file with specified flag (O_RDONLY etc.) and
// perm, (0666 etc.) if applicable. If successful, methods on the returned // perm, (0666 etc.) if applicable. If successful, methods on the returned

View file

@ -42,7 +42,7 @@ func onRead(ctx context.Context, w *response, userHandle Handler) error {
return &NFSStatusError{NFSStatusStale, err} return &NFSStatusError{NFSStatusStale, err}
} }
fh, err := fs.Open(ctx, fs.Join(path...)) fh, err := fs.OpenFile(ctx, fs.Join(path...), os.O_RDONLY, 0)
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
return &NFSStatusError{NFSStatusNoEnt, err} return &NFSStatusError{NFSStatusNoEnt, err}

View file

@ -72,6 +72,7 @@ func onWrite(ctx context.Context, w *response, userHandle Handler) error {
if err != nil { if err != nil {
return &NFSStatusError{NFSStatusAccess, err} return &NFSStatusError{NFSStatusAccess, err}
} }
defer file.Close(ctx)
if req.Offset > 0 { if req.Offset > 0 {
if _, err := file.Seek(int64(req.Offset), io.SeekStart); err != nil { if _, err := file.Seek(int64(req.Offset), io.SeekStart); err != nil {
return &NFSStatusError{NFSStatusIO, err} return &NFSStatusError{NFSStatusIO, err}

View file

@ -5,6 +5,7 @@ import (
"context" "context"
"fmt" "fmt"
"net" "net"
"os"
"reflect" "reflect"
"sort" "sort"
"testing" "testing"
@ -86,7 +87,7 @@ func TestNFS(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
mf, _ := mem.Open(ctx, "/helloworld.txt") mf, _ := mem.OpenFile(ctx, "/helloworld.txt", os.O_RDONLY, 0)
buf := make([]byte, len(b)) buf := make([]byte, len(b))
if _, err = mf.Read(ctx, buf[:]); err != nil { if _, err = mf.Read(ctx, buf[:]); err != nil {
t.Fatal(err) t.Fatal(err)