summaryrefslogtreecommitdiffhomepage
path: root/pkg/sentry
diff options
context:
space:
mode:
authorgVisor bot <gvisor-bot@google.com>2020-08-21 17:24:45 -0700
committergVisor bot <gvisor-bot@google.com>2020-08-21 17:26:38 -0700
commit46d11a767ccfcd6f7af0d10ccefc08ec9e2df5f4 (patch)
tree1ce05e8dfdcb1e535fc4ac2bb33ca395cb113362 /pkg/sentry
parentbd3383a7e25b426fc6aa5a28fc8ffa6fe7fa333e (diff)
Provide fdReader/Writer for FileDescription
fdReader/Writer implements io.Reader/Writer so that they can be passed to Merkle tree library. PiperOrigin-RevId: 327901376
Diffstat (limited to 'pkg/sentry')
-rw-r--r--pkg/sentry/vfs/file_description.go28
1 files changed, 28 insertions, 0 deletions
diff --git a/pkg/sentry/vfs/file_description.go b/pkg/sentry/vfs/file_description.go
index d3abe28ee..33910e095 100644
--- a/pkg/sentry/vfs/file_description.go
+++ b/pkg/sentry/vfs/file_description.go
@@ -847,3 +847,31 @@ func (fd *FileDescription) SetAsyncHandler(newHandler func() FileAsync) FileAsyn
}
return fd.asyncHandler
}
+
+// FileReadWriteSeeker is a helper struct to pass a FileDescription as
+// io.Reader/io.Writer/io.ReadSeeker/etc.
+type FileReadWriteSeeker struct {
+ Fd *FileDescription
+ Ctx context.Context
+ ROpts ReadOptions
+ WOpts WriteOptions
+}
+
+// Read implements io.ReadWriteSeeker.Read.
+func (f *FileReadWriteSeeker) Read(p []byte) (int, error) {
+ dst := usermem.BytesIOSequence(p)
+ ret, err := f.Fd.Read(f.Ctx, dst, f.ROpts)
+ return int(ret), err
+}
+
+// Seek implements io.ReadWriteSeeker.Seek.
+func (f *FileReadWriteSeeker) Seek(offset int64, whence int) (int64, error) {
+ return f.Fd.Seek(f.Ctx, offset, int32(whence))
+}
+
+// Write implements io.ReadWriteSeeker.Write.
+func (f *FileReadWriteSeeker) Write(p []byte) (int, error) {
+ buf := usermem.BytesIOSequence(p)
+ ret, err := f.Fd.Write(f.Ctx, buf, f.WOpts)
+ return int(ret), err
+}