]> git.apps.os.sepia.ceph.com Git - fscrypt.git/commitdiff
util: Added parsing and effective user functions
authorJoseph Richey <joerichey94@gmail.com>
Wed, 30 Aug 2017 11:49:39 +0000 (04:49 -0700)
committerJoe Richey <joerichey@google.com>
Wed, 30 Aug 2017 22:35:32 +0000 (15:35 -0700)
util/util.go

index c02ea0e577d4f6b5f2ada76b88d716efee7a9baf..3de4a1ad58b965bf1dc4141a109567e4b038c1d2 100644 (file)
@@ -27,6 +27,8 @@ import (
        "bufio"
        "math"
        "os"
+       "os/user"
+       "strconv"
        "unsafe"
 )
 
@@ -105,3 +107,23 @@ func ReadLine() (string, error) {
        scanner.Scan()
        return scanner.Text(), scanner.Err()
 }
+
+// AtoiOrPanic converts a string to an int or it panics. Should only be used in
+// situations where the input MUST be a decimal number.
+func AtoiOrPanic(input string) int {
+       i, err := strconv.Atoi(input)
+       if err != nil {
+               panic(err)
+       }
+       return i
+}
+
+// EffectiveUser returns the user entry corresponding to the effective user.
+func EffectiveUser() (*user.User, error) {
+       return user.LookupId(strconv.Itoa(os.Geteuid()))
+}
+
+// IsUserRoot checks if the effective user is root.
+func IsUserRoot() bool {
+       return os.Geteuid() == 0
+}