summaryrefslogtreecommitdiff
path: root/cmd/btrfs-rec/util.go
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2023-01-25 17:41:28 -0700
committerLuke Shumaker <lukeshu@lukeshu.com>2023-01-25 17:41:28 -0700
commitd6d19b3a8f66f27f78fd2c6fa02b32fb2a88e9ad (patch)
tree65c8fb097c58a17480be9650bc662fe0fe207dbb /cmd/btrfs-rec/util.go
parent9c5d61133af93ddf279e355a06d4c1ae78c568b3 (diff)
Move btrfs-rec's logging RuneScanner to a new `streamio` package
Diffstat (limited to 'cmd/btrfs-rec/util.go')
-rw-r--r--cmd/btrfs-rec/util.go61
1 files changed, 2 insertions, 59 deletions
diff --git a/cmd/btrfs-rec/util.go b/cmd/btrfs-rec/util.go
index 9a0d60c..b2497b2 100644
--- a/cmd/btrfs-rec/util.go
+++ b/cmd/btrfs-rec/util.go
@@ -9,77 +9,20 @@ import (
"context"
"io"
"os"
- "time"
"git.lukeshu.com/go/lowmemjson"
"github.com/datawire/dlib/dlog"
- "git.lukeshu.com/btrfs-progs-ng/lib/textui"
+ "git.lukeshu.com/btrfs-progs-ng/lib/streamio"
)
-type runeScanner struct {
- ctx context.Context //nolint:containedctx // For detecting shutdown from methods
- progress textui.Portion[int64]
- progressWriter *textui.Progress[textui.Portion[int64]]
- unreadCnt uint64
- reader *bufio.Reader
- closer io.Closer
-}
-
-func newRuneScanner(ctx context.Context, fh *os.File) (*runeScanner, error) {
- fi, err := fh.Stat()
- if err != nil {
- return nil, err
- }
- ret := &runeScanner{
- ctx: ctx,
- progress: textui.Portion[int64]{
- D: fi.Size(),
- },
- progressWriter: textui.NewProgress[textui.Portion[int64]](ctx, dlog.LogLevelInfo, textui.Tunable(1*time.Second)),
- reader: bufio.NewReader(fh),
- closer: fh,
- }
- return ret, nil
-}
-
-func (rs *runeScanner) ReadRune() (r rune, size int, err error) {
- if err := rs.ctx.Err(); err != nil {
- return 0, 0, err
- }
- r, size, err = rs.reader.ReadRune()
- if rs.unreadCnt > 0 {
- rs.unreadCnt--
- } else {
- rs.progress.N += int64(size)
- rs.progressWriter.Set(rs.progress)
- }
- return
-}
-
-func (rs *runeScanner) UnreadRune() error {
- if err := rs.ctx.Err(); err != nil {
- return err
- }
- if err := rs.reader.UnreadRune(); err != nil {
- return err
- }
- rs.unreadCnt++
- return nil
-}
-
-func (rs *runeScanner) Close() error {
- rs.progressWriter.Done()
- return rs.closer.Close()
-}
-
func readJSONFile[T any](ctx context.Context, filename string) (T, error) {
fh, err := os.Open(filename)
if err != nil {
var zero T
return zero, err
}
- buf, err := newRuneScanner(dlog.WithField(ctx, "btrfs.read-json-file", filename), fh)
+ buf, err := streamio.NewRuneScanner(dlog.WithField(ctx, "btrfs.read-json-file", filename), fh)
defer func() {
_ = buf.Close()
}()