aboutsummaryrefslogtreecommitdiffhomepage
path: root/container/vfs/mountinfo.go
diff options
context:
space:
mode:
authorOphestra <cat@gensokyo.uk>2025-08-29 21:51:31 +0900
committerOphestra <cat@gensokyo.uk>2025-08-29 21:51:31 +0900
commit50972096cd8bffd9b17dbdf656ec34c48edb8f86 (patch)
tree1ad00bbf65f59539aa9bd0b3e1f42b89b7ca3fcb /container/vfs/mountinfo.go
parent905b9f9785263297611a61facc8b6bdf4c3e257f (diff)
container/vfs: wrap decoder errors
This passes line information and handles strconv errors so it reads better. Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'container/vfs/mountinfo.go')
-rw-r--r--container/vfs/mountinfo.go35
1 files changed, 33 insertions, 2 deletions
diff --git a/container/vfs/mountinfo.go b/container/vfs/mountinfo.go
index bcb3063f..5f9f2257 100644
--- a/container/vfs/mountinfo.go
+++ b/container/vfs/mountinfo.go
@@ -24,6 +24,32 @@ var (
ErrMountInfoSep = errors.New("bad optional fields separator")
)
+type DecoderError struct {
+ Op string
+ Line int
+ Err error
+}
+
+func (e *DecoderError) Unwrap() error { return e.Err }
+func (e *DecoderError) Error() string {
+ var s string
+
+ var numError *strconv.NumError
+ switch {
+ case errors.As(e.Err, &numError) && numError != nil:
+ s = "numeric field " + strconv.Quote(numError.Num) + " " + numError.Err.Error()
+
+ default:
+ s = e.Err.Error()
+ }
+
+ var atLine string
+ if e.Line >= 0 {
+ atLine = " at line " + strconv.Itoa(e.Line)
+ }
+ return e.Op + " mountinfo" + atLine + ": " + s
+}
+
type (
// A MountInfoDecoder reads and decodes proc_pid_mountinfo(5) entries from an input stream.
MountInfoDecoder struct {
@@ -32,6 +58,7 @@ type (
current *MountInfo
parseErr error
+ curLine int
complete bool
}
@@ -132,9 +159,12 @@ func (d *MountInfoDecoder) Entries() iter.Seq[*MountInfoEntry] {
func (d *MountInfoDecoder) Err() error {
if err := d.s.Err(); err != nil {
- return err
+ return &DecoderError{"scan", d.curLine, err}
+ }
+ if d.parseErr != nil {
+ return &DecoderError{"parse", d.curLine, d.parseErr}
}
- return d.parseErr
+ return nil
}
func (d *MountInfoDecoder) scan() bool {
@@ -160,6 +190,7 @@ func (d *MountInfoDecoder) scan() bool {
d.current.Next = m
d.current = d.current.Next
}
+ d.curLine++
return true
}