-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
libct: wrap unix.Mount/Unmount errors
Errors returned by unix are bare. In some cases it's impossible to find out what went wrong because there's is not enough context. Add a mountError type (mostly copy-pasted from github.com/moby/sys/mount), and mount/unmount helpers. Use these where appropriate, and convert error checks to use errors.Is. Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
- Loading branch information
Showing
5 changed files
with
130 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package libcontainer | ||
|
||
import ( | ||
"strconv" | ||
|
||
"golang.org/x/sys/unix" | ||
) | ||
|
||
// mountError holds an error from a failed mount or unmount operation. | ||
type mountError struct { | ||
op string | ||
source string | ||
target string | ||
procfd string | ||
flags uintptr | ||
data string | ||
err error | ||
} | ||
|
||
// Error provides a string error representation. | ||
func (e *mountError) Error() string { | ||
out := e.op + " " | ||
|
||
if e.source != "" { | ||
out += e.source + ":" + e.target | ||
} else { | ||
out += e.target | ||
} | ||
if e.procfd != "" { | ||
out += " (via " + e.procfd + ")" | ||
} | ||
|
||
if e.flags != uintptr(0) { | ||
out += ", flags: 0x" + strconv.FormatUint(uint64(e.flags), 16) | ||
} | ||
if e.data != "" { | ||
out += ", data: " + e.data | ||
} | ||
|
||
out += ": " + e.err.Error() | ||
return out | ||
} | ||
|
||
// Cause returns the underlying cause of the error. | ||
// This is a convention used by github.com/pkg/errors. | ||
func (e *mountError) Cause() error { | ||
return e.err | ||
} | ||
|
||
// Unwrap returns the underlying error. | ||
// This is a convention used by Go 1.13+ standard library. | ||
func (e *mountError) Unwrap() error { | ||
return e.err | ||
} | ||
|
||
// mount is a simple unix.Mount wrapper. If procfd is not empty, it is | ||
// used instead of target (and the target is only used in an error). | ||
func mount(device, target, procfd, fstype string, flags uintptr, data string) error { | ||
if err := unix.Mount(device, target, fstype, flags, data); err != nil { | ||
return &mountError{ | ||
op: "mount", | ||
source: device, | ||
target: target, | ||
procfd: procfd, | ||
flags: flags, | ||
data: data, | ||
err: err, | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// unmount is a simple unix.Unmount wrapper. | ||
func unmount(target string, flags int) error { | ||
err := unix.Unmount(target, flags) | ||
if err != nil { | ||
return &mountError{ | ||
op: "unmount", | ||
target: target, | ||
flags: uintptr(flags), | ||
err: err, | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters