diff options
| author | Joseph Richey <joerichey94@gmail.com> | 2017-10-19 02:21:08 -0700 |
|---|---|---|
| committer | Joseph Richey <joerichey94@gmail.com> | 2017-10-19 02:22:29 -0700 |
| commit | 0d4d11af2883bac66ecc427cdde37aa995b93ca8 (patch) | |
| tree | be426d602b210d91a3e0121ad4757dad8fef44cd /util | |
| parent | 18925df4483ccf5c48dfb4314f85c1b37a1cbe81 (diff) | |
Random changesext4
Diffstat (limited to 'util')
| -rw-r--r-- | util/errors.go | 41 | ||||
| -rw-r--r-- | util/users.go | 8 |
2 files changed, 30 insertions, 19 deletions
diff --git a/util/errors.go b/util/errors.go index f10569e..f0b9403 100644 --- a/util/errors.go +++ b/util/errors.go @@ -28,6 +28,28 @@ import ( "github.com/pkg/errors" ) +var ( + // ErrNotRoot indicates the action is restricted to the superuser. + ErrNotRoot = errors.New("only root can perform this action") + // ErrSkipIntegration indicates integration tests shouldn't be run. + ErrSkipIntegration = errors.New("skipping integration test") +) + +// testEnvVarName is the name on an environment variable that should be set to +// an empty mountpoint. This is only used for integration tests. If not set, +// integration tests are skipped. +var testEnvVarName = "TEST_FILESYSTEM_ROOT" + +// TestRoot returns a the root of a filesystem specified by testEnvVarName. This +// function is only used for integration tests. +func TestRoot() (string, error) { + path := os.Getenv(testEnvVarName) + if path == "" { + return "", ErrSkipIntegration + } + return path, nil +} + // ErrReader wraps an io.Reader, passing along calls to Read() until a read // fails. Then, the error is stored, and all subsequent calls to Read() do // nothing. This allows you to write code which has many subsequent reads and @@ -114,22 +136,3 @@ func NeverError(err error) { log.Panicf("NeverError() check failed: %v", err) } } - -var ( - // testEnvVarName is the name on an environment variable that should be - // set to an empty mountpoint. This is only used for integration tests. - // If not set, integration tests are skipped. - testEnvVarName = "TEST_FILESYSTEM_ROOT" - // ErrSkipIntegration indicates integration tests shouldn't be run. - ErrSkipIntegration = errors.New("skipping integration test") -) - -// TestRoot returns a the root of a filesystem specified by testEnvVarName. This -// function is only used for integration tests. -func TestRoot() (string, error) { - path := os.Getenv(testEnvVarName) - if path == "" { - return "", ErrSkipIntegration - } - return path, nil -} diff --git a/util/users.go b/util/users.go index 92affa8..49abd32 100644 --- a/util/users.go +++ b/util/users.go @@ -48,3 +48,11 @@ func GetUser(uid int) *user.User { func CurrentUser() *user.User { return GetUser(CurrentUserID()) } + +// CheckIfRoot returns ErrNotRoot if the current user is not the root user. +func CheckIfRoot() error { + if id := CurrentUserID(); id != 0 { + return ErrNotRoot + } + return nil +} |