| 1 | diff -ruN a/request-server.go b/request-server.go |
| 2 | |
| 3 | |
| 4 | @@ -287,8 +287,9 @@ |
| 5 | rpkt = statusFromError(pkt.ID, EBADF) |
| 6 | } else { |
| 7 | request = &Request{ |
| 8 | - Method: "Stat", |
| 9 | - Filepath: cleanPathWithBase(rs.startDirectory, request.Filepath), |
| 10 | + Method: "Stat", |
| 11 | + Filepath: cleanPathWithBase(rs.startDirectory, request.Filepath), |
| 12 | + handleFile: request.opened(), |
| 13 | } |
| 14 | rpkt = request.call(rs.Handlers, pkt, rs.pktMgr.alloc, orderID, rs.maxTxPacket) |
| 15 | } |
| 16 | @@ -299,8 +300,9 @@ |
| 17 | rpkt = statusFromError(pkt.ID, EBADF) |
| 18 | } else { |
| 19 | request = &Request{ |
| 20 | - Method: "Setstat", |
| 21 | - Filepath: cleanPathWithBase(rs.startDirectory, request.Filepath), |
| 22 | + Method: "Setstat", |
| 23 | + Filepath: cleanPathWithBase(rs.startDirectory, request.Filepath), |
| 24 | + handleFile: request.opened(), |
| 25 | } |
| 26 | rpkt = request.call(rs.Handlers, pkt, rs.pktMgr.alloc, orderID, rs.maxTxPacket) |
| 27 | } |
| 28 | diff -ruN a/request-server_test.go b/request-server_test.go |
| 29 | |
| 30 | |
| 31 | @@ -1045,3 +1045,47 @@ |
| 32 | cleanPath(bslash+"a"+bslash+bslash+"b"+bslash+bslash+"c"+bslash)) |
| 33 | assert.Equal(t, "/C:/a", cleanPath("C:"+bslash+"a")) |
| 34 | } |
| 35 | + |
| 36 | +type handleFileRecorder struct { |
| 37 | + FileLister |
| 38 | + FileCmder |
| 39 | + got []any |
| 40 | +} |
| 41 | + |
| 42 | +func (h *handleFileRecorder) Filelist(r *Request) (ListerAt, error) { |
| 43 | + if r.Method == "Stat" { |
| 44 | + h.got = append(h.got, r.HandleFile()) |
| 45 | + } |
| 46 | + return h.FileLister.Filelist(r) |
| 47 | +} |
| 48 | + |
| 49 | +func (h *handleFileRecorder) Filecmd(r *Request) error { |
| 50 | + if r.Method == "Setstat" { |
| 51 | + h.got = append(h.got, r.HandleFile()) |
| 52 | + } |
| 53 | + return h.FileCmder.Filecmd(r) |
| 54 | +} |
| 55 | + |
| 56 | +func TestRequestHandleFile(t *testing.T) { |
| 57 | + mem := InMemHandler() |
| 58 | + rec := &handleFileRecorder{FileLister: mem.FileList, FileCmder: mem.FileCmd} |
| 59 | + p := clientRequestServerPairWithHandlers(t, Handlers{FileGet: mem.FileGet, FilePut: mem.FilePut, FileCmd: rec, FileList: rec}) |
| 60 | + defer p.Close() |
| 61 | + |
| 62 | + f, err := p.cli.Create("/a") |
| 63 | + require.NoError(t, err) |
| 64 | + defer f.Close() |
| 65 | + _, err = f.Stat() |
| 66 | + require.NoError(t, err) |
| 67 | + require.NoError(t, f.Truncate(0)) |
| 68 | + _, err = p.cli.Stat("/a") |
| 69 | + require.NoError(t, err) |
| 70 | + require.NoError(t, p.cli.Truncate("/a", 0)) |
| 71 | + |
| 72 | + require.Len(t, rec.got, 4) |
| 73 | + assert.NotNil(t, rec.got[0], "FSTAT") |
| 74 | + assert.NotNil(t, rec.got[1], "FSETSTAT") |
| 75 | + assert.Same(t, rec.got[0], rec.got[1]) |
| 76 | + assert.Nil(t, rec.got[2], "STAT") |
| 77 | + assert.Nil(t, rec.got[3], "SETSTAT") |
| 78 | +} |
| 79 | diff -ruN a/request.go b/request.go |
| 80 | |
| 81 | |
| 82 | @@ -148,6 +148,9 @@ |
| 83 | Target string // for renames and sym-links |
| 84 | handle string |
| 85 | |
| 86 | + // what the handlers returned for the handle of a FSTAT or FSETSTAT |
| 87 | + handleFile any |
| 88 | + |
| 89 | // reader/writer/readdir from handlers |
| 90 | state |
| 91 | |
| 92 | @@ -176,6 +179,8 @@ |
| 93 | Target: r.Target, |
| 94 | handle: r.handle, |
| 95 | |
| 96 | + handleFile: r.handleFile, |
| 97 | + |
| 98 | state: r.state.copy(), |
| 99 | |
| 100 | ctx: r.ctx, |
| 101 | @@ -183,6 +188,34 @@ |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | +// HandleFile returns, for the Stat request of a FSTAT and the Setstat request of a FSETSTAT, |
| 106 | +// what the handlers returned when the handle was opened: the io.ReaderAt, io.WriterAt, |
| 107 | +// WriterAtReaderAt or ListerAt. It returns nil for other requests. |
| 108 | +// |
| 109 | +// Filepath is the path the handle was opened with, which may now be another file, |
| 110 | +// or none, if the file was renamed or removed since. Handlers that can should act |
| 111 | +// on the returned value instead. |
| 112 | +func (r *Request) HandleFile() any { |
| 113 | + return r.handleFile |
| 114 | +} |
| 115 | + |
| 116 | +// opened returns what the handlers returned when r was opened, or nil. |
| 117 | +func (s *state) opened() any { |
| 118 | + rd, wr, rw := s.getAllReaderWriters() |
| 119 | + switch { |
| 120 | + case rw != nil: |
| 121 | + return rw |
| 122 | + case wr != nil: |
| 123 | + return wr |
| 124 | + case rd != nil: |
| 125 | + return rd |
| 126 | + } |
| 127 | + if la := s.getListerAt(); la != nil { |
| 128 | + return la |
| 129 | + } |
| 130 | + return nil |
| 131 | +} |
| 132 | + |
| 133 | // New Request initialized based on packet data |
| 134 | func requestFromPacket(ctx context.Context, pkt hasPath, baseDir string) *Request { |
| 135 | request := &Request{ |