summaryrefslogtreecommitdiff
path: root/lib/diskio/file_state.go
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2022-07-13 22:38:43 -0600
committerLuke Shumaker <lukeshu@lukeshu.com>2022-07-14 01:09:20 -0600
commit7153fc9379dd5910c688925ccd2e0a03b9551a6d (patch)
treed1fd8edb223c130cc12c0ef2af3c6ae5130afda5 /lib/diskio/file_state.go
parent913acf193bfac666cec68e8c3fb13829a7a0c794 (diff)
Buffer FS IO
Diffstat (limited to 'lib/diskio/file_state.go')
-rw-r--r--lib/diskio/file_state.go36
1 files changed, 36 insertions, 0 deletions
diff --git a/lib/diskio/file_state.go b/lib/diskio/file_state.go
new file mode 100644
index 0000000..63ec53b
--- /dev/null
+++ b/lib/diskio/file_state.go
@@ -0,0 +1,36 @@
+// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
+//
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+package diskio
+
+type statefulFile[A ~int64] struct {
+ inner File[A]
+ pos A
+}
+
+var _ File[assertAddr] = (*statefulFile[assertAddr])(nil)
+
+func NewStatefulFile[A ~int64](file File[A]) *statefulFile[A] {
+ return &statefulFile[A]{
+ inner: file,
+ }
+}
+
+func (sf *statefulFile[A]) Name() string { return sf.inner.Name() }
+func (sf *statefulFile[A]) Size() A { return sf.inner.Size() }
+func (sf *statefulFile[A]) Close() error { return sf.inner.Close() }
+func (sf *statefulFile[A]) ReadAt(dat []byte, off A) (int, error) { return sf.inner.ReadAt(dat, off) }
+func (sf *statefulFile[A]) WriteAt(dat []byte, off A) (int, error) { return sf.inner.WriteAt(dat, off) }
+
+func (sf *statefulFile[A]) Read(dat []byte) (n int, err error) {
+ n, err = sf.ReadAt(dat, sf.pos)
+ sf.pos += A(n)
+ return n, err
+}
+
+func (sf *statefulFile[A]) ReadByte() (byte, error) {
+ var dat [1]byte
+ _, err := sf.Read(dat[:])
+ return dat[0], err
+}