diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2023-01-05 19:48:27 -0700 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2023-01-05 19:48:27 -0700 |
commit | cd01485ec126c103fa9ab718685d184b70d0e1ff (patch) | |
tree | 0c003455bf0795a04e4fb198d2e85e2c7b63ffb3 /cmd/btrfs-rec/util.go | |
parent | f416f777b2c2cac095e851e6799020f91e34aed1 (diff) | |
parent | 3b385f26973e45b4c2e2f3ebf9d52ab0131cff5e (diff) |
Merge branch 'lukeshu/rebuild-nodes-take4'
Diffstat (limited to 'cmd/btrfs-rec/util.go')
-rw-r--r-- | cmd/btrfs-rec/util.go | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/cmd/btrfs-rec/util.go b/cmd/btrfs-rec/util.go index ffc03cc..9a0d60c 100644 --- a/cmd/btrfs-rec/util.go +++ b/cmd/btrfs-rec/util.go @@ -18,6 +18,7 @@ import ( ) 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 @@ -31,6 +32,7 @@ func newRuneScanner(ctx context.Context, fh *os.File) (*runeScanner, error) { return nil, err } ret := &runeScanner{ + ctx: ctx, progress: textui.Portion[int64]{ D: fi.Size(), }, @@ -42,6 +44,9 @@ func newRuneScanner(ctx context.Context, fh *os.File) (*runeScanner, error) { } 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-- @@ -53,8 +58,14 @@ func (rs *runeScanner) ReadRune() (r rune, size int, err error) { } 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 rs.reader.UnreadRune() + return nil } func (rs *runeScanner) Close() error { @@ -69,6 +80,9 @@ func readJSONFile[T any](ctx context.Context, filename string) (T, error) { return zero, err } buf, err := newRuneScanner(dlog.WithField(ctx, "btrfs.read-json-file", filename), fh) + defer func() { + _ = buf.Close() + }() if err != nil { var zero T return zero, err @@ -78,7 +92,6 @@ func readJSONFile[T any](ctx context.Context, filename string) (T, error) { var zero T return zero, err } - _ = buf.Close() return ret, nil } |