aboutsummaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorJoe Richey <joerichey@google.com>2017-03-02 10:38:33 -0800
committerJoe Richey joerichey@google.com <joerichey@google.com>2017-05-02 13:39:18 -0700
commita683ab55245aa44ada5059f8e9816adbd94198ff (patch)
tree13b966a777803e1b2a870b89219c3440259e9ffb /util
parent06e86bde8886aca3e1cd8cbc948cff7a103f97ab (diff)
metadata: get and set policies from go
This commit adds in the ability to get and set policy data from go using the GetPolicy and SetPolicy functions. This is done via a patch of the x/sys/unix package that exposes the filesystem encryption structures. Note that not all the fields of the PolicyData protocol buffer are needed to get and set policies. The wrapped_policy_keys are not used and will be written and read by other components of fscrypt. To run the policy tests, the environment variable BASE_TEST_DIR must be set to a directory for testing on a filesystem that supports encryption. Change-Id: I13b1d983356845f3ffc1945cedf53234218f32e5
Diffstat (limited to 'util')
-rw-r--r--util/errors.go15
-rw-r--r--util/util.go22
2 files changed, 37 insertions, 0 deletions
diff --git a/util/errors.go b/util/errors.go
index aafeadd..bd63ac8 100644
--- a/util/errors.go
+++ b/util/errors.go
@@ -22,6 +22,7 @@ package util
import (
"fmt"
"log"
+ "os"
)
// InvalidInputF creates an error that should indicate either bad input from a
@@ -48,3 +49,17 @@ func NeverError(err error) {
log.Panicf("NeverError() check failed: %v", err)
}
}
+
+// UnderlyingError returns the underlying error for known os error types.
+// From: src/os/error.go
+func UnderlyingError(err error) error {
+ switch err := err.(type) {
+ case *os.PathError:
+ return err.Err
+ case *os.LinkError:
+ return err.Err
+ case *os.SyscallError:
+ return err.Err
+ }
+ return err
+}
diff --git a/util/util.go b/util/util.go
index 9439d0e..7574f35 100644
--- a/util/util.go
+++ b/util/util.go
@@ -31,3 +31,25 @@ import (
func Ptr(slice []byte) unsafe.Pointer {
return unsafe.Pointer(&slice[0])
}
+
+// Index returns the first index i such that inVal == inArray[i].
+// ok is true if we find a match, false otherwise.
+func Index(inVal int64, inArray []int64) (index int, ok bool) {
+ for index, val := range inArray {
+ if val == inVal {
+ return index, true
+ }
+ }
+ return 0, false
+}
+
+// Lookup finds inVal in inArray and returns the corresponding element in
+// outArray. Specifically, if inVal == inArray[i], outVal == outArray[i].
+// ok is true if we find a match, false otherwise.
+func Lookup(inVal int64, inArray, outArray []int64) (outVal int64, ok bool) {
+ index, ok := Index(inVal, inArray)
+ if !ok {
+ return 0, false
+ }
+ return outArray[index], true
+}