From 0d4d11af2883bac66ecc427cdde37aa995b93ca8 Mon Sep 17 00:00:00 2001 From: Joseph Richey Date: Thu, 19 Oct 2017 02:21:08 -0700 Subject: Random changes --- util/errors.go | 41 ++++++++++++++++++++++------------------- util/users.go | 8 ++++++++ 2 files changed, 30 insertions(+), 19 deletions(-) (limited to 'util') 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 +} -- cgit v1.2.3