sandbox: wrap error with its own text message
All checks were successful
Test / Create distribution (push) Successful in 26s
Test / Fortify (push) Successful in 2m40s
Test / Fpkg (push) Successful in 3m33s
Test / Data race detector (push) Successful in 4m24s
Test / Flake checks (push) Successful in 57s

PathError has a pretty good text message, many of them are wrapped with its own text message. This change adds a function to do just that to improve readability.

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
Ophestra 2025-03-25 19:42:20 +09:00
parent 33940265a6
commit f86d868274
Signed by: cat
SSH Key Fingerprint: SHA256:gQ67O0enBZ7UdZypgtspB2FDM1g3GVw8nX0XSdcFw8Q
4 changed files with 28 additions and 21 deletions

View File

@ -25,7 +25,7 @@ func (p *procPaths) bindMount(source, target string, flags uintptr, eq bool) err
var targetFinal string
if v, err := filepath.EvalSymlinks(target); err != nil {
return msg.WrapErr(err, err.Error())
return wrapErrSelf(err)
} else {
targetFinal = v
if targetFinal != target {
@ -45,7 +45,7 @@ func (p *procPaths) bindMount(source, target string, flags uintptr, eq bool) err
fmt.Sprintf("cannot open %q:", targetFinal))
}
if v, err := os.Readlink(p.fd(destFd)); err != nil {
return msg.WrapErr(err, err.Error())
return wrapErrSelf(err)
} else if err = syscall.Close(destFd); err != nil {
return wrapErrSuffix(err,
fmt.Sprintf("cannot close %q:", targetFinal))
@ -102,7 +102,7 @@ func remountWithFlags(n *vfs.MountInfoNode, mf uintptr) error {
func mountTmpfs(fsname, name string, size int, perm os.FileMode) error {
target := toSysroot(name)
if err := os.MkdirAll(target, parentPerm(perm)); err != nil {
return msg.WrapErr(err, err.Error())
return wrapErrSelf(err)
}
opt := fmt.Sprintf("mode=%#o", perm)
if size > 0 {

View File

@ -17,3 +17,10 @@ func wrapErrSuffix(err error, a ...any) error {
}
return msg.WrapErr(err, append(a, err)...)
}
func wrapErrSelf(err error) error {
if err == nil {
return nil
}
return msg.WrapErr(err, err.Error())
}

View File

@ -32,16 +32,16 @@ func toHost(name string) string {
func createFile(name string, perm, pperm os.FileMode, content []byte) error {
if err := os.MkdirAll(path.Dir(name), pperm); err != nil {
return msg.WrapErr(err, err.Error())
return wrapErrSelf(err)
}
f, err := os.OpenFile(name, syscall.O_CREAT|syscall.O_EXCL|syscall.O_WRONLY, perm)
if err != nil {
return msg.WrapErr(err, err.Error())
return wrapErrSelf(err)
}
if content != nil {
_, err = f.Write(content)
if err != nil {
err = msg.WrapErr(err, err.Error())
err = wrapErrSelf(err)
}
}
return errors.Join(f.Close(), err)
@ -78,7 +78,7 @@ func (p *procPaths) stdout() string { return p.self + "/fd/1" }
func (p *procPaths) fd(fd int) string { return p.self + "/fd/" + strconv.Itoa(fd) }
func (p *procPaths) mountinfo(f func(d *vfs.MountInfoDecoder) error) error {
if r, err := os.Open(p.self + "/mountinfo"); err != nil {
return msg.WrapErr(err, err.Error())
return wrapErrSelf(err)
} else {
d := vfs.NewMountInfoDecoder(r)
err0 := f(d)

View File

@ -38,7 +38,7 @@ func (b *BindMount) early(*Params) error {
b.SourceFinal = "\x00"
return nil
}
return msg.WrapErr(err, err.Error())
return wrapErrSelf(err)
} else {
b.SourceFinal = v
return nil
@ -65,10 +65,10 @@ func (b *BindMount) apply(*Params) error {
// this perm value emulates bwrap behaviour as it clears bits from 0755 based on
// op->perms which is never set for any bind setup op so always results in 0700
if fi, err := os.Stat(source); err != nil {
return msg.WrapErr(err, err.Error())
return wrapErrSelf(err)
} else if fi.IsDir() {
if err = os.MkdirAll(target, 0700); err != nil {
return msg.WrapErr(err, err.Error())
return wrapErrSelf(err)
}
} else if err = ensureFile(target, 0444, 0700); err != nil {
return err
@ -114,7 +114,7 @@ func (p MountProc) apply(*Params) error {
target := toSysroot(v)
if err := os.MkdirAll(target, 0755); err != nil {
return msg.WrapErr(err, err.Error())
return wrapErrSelf(err)
}
return wrapErrSuffix(syscall.Mount("proc", target, "proc",
syscall.MS_NOSUID|syscall.MS_NOEXEC|syscall.MS_NODEV, ""),
@ -167,7 +167,7 @@ func (d MountDev) apply(params *Params) error {
"/proc/self/fd/"+string(rune(i+'0')),
path.Join(target, name),
); err != nil {
return msg.WrapErr(err, err.Error())
return wrapErrSelf(err)
}
}
for _, pair := range [][2]string{
@ -176,14 +176,14 @@ func (d MountDev) apply(params *Params) error {
{"pts/ptmx", "ptmx"},
} {
if err := os.Symlink(pair[0], path.Join(target, pair[1])); err != nil {
return msg.WrapErr(err, err.Error())
return wrapErrSelf(err)
}
}
devPtsPath := path.Join(target, "pts")
for _, name := range []string{path.Join(target, "shm"), devPtsPath} {
if err := os.Mkdir(name, 0755); err != nil {
return msg.WrapErr(err, err.Error())
return wrapErrSelf(err)
}
}
@ -205,7 +205,7 @@ func (d MountDev) apply(params *Params) error {
return err
}
if name, err := os.Readlink(hostProc.stdout()); err != nil {
return msg.WrapErr(err, err.Error())
return wrapErrSelf(err)
} else if err = hostProc.bindMount(
toHost(name),
consolePath,
@ -244,7 +244,7 @@ func (m MountMqueue) apply(*Params) error {
target := toSysroot(v)
if err := os.MkdirAll(target, 0755); err != nil {
return msg.WrapErr(err, err.Error())
return wrapErrSelf(err)
}
return wrapErrSuffix(syscall.Mount("mqueue", target, "mqueue",
syscall.MS_NOSUID|syscall.MS_NOEXEC|syscall.MS_NODEV, ""),
@ -304,10 +304,10 @@ func (l *Symlink) apply(*Params) error {
target := toSysroot(l[1])
if err := os.MkdirAll(path.Dir(target), 0755); err != nil {
return msg.WrapErr(err, err.Error())
return wrapErrSelf(err)
}
if err := os.Symlink(l[0], target); err != nil {
return msg.WrapErr(err, err.Error())
return wrapErrSelf(err)
}
return nil
}
@ -336,7 +336,7 @@ func (m *Mkdir) apply(*Params) error {
}
if err := os.MkdirAll(toSysroot(m.Path), m.Perm); err != nil {
return msg.WrapErr(err, err.Error())
return wrapErrSelf(err)
}
return nil
}
@ -366,7 +366,7 @@ func (t *Tmpfile) apply(*Params) error {
var tmpPath string
if f, err := os.CreateTemp("/", "tmp.*"); err != nil {
return msg.WrapErr(err, err.Error())
return wrapErrSelf(err)
} else if _, err = f.Write(t.Data); err != nil {
return wrapErrSuffix(err,
"cannot write to intermediate file:")
@ -388,7 +388,7 @@ func (t *Tmpfile) apply(*Params) error {
); err != nil {
return err
} else if err = os.Remove(tmpPath); err != nil {
return msg.WrapErr(err, err.Error())
return wrapErrSelf(err)
}
return nil
}