Debian Codegraph @

util/util.go

raw
1 /*
2 * util.go - Various helpers used throughout fscrypt
3 *
4 * Copyright 2017 Google Inc.
5 * Author: Joe Richey ([email protected])
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
8 * use this file except in compliance with the License. You may obtain a copy of
9 * the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16 * License for the specific language governing permissions and limitations under
17 * the License.
18 */
19
20 // Package util contains useful components for simplifying Go code.
21 //
22 // The package contains common error types (errors.go) and functions for
23 // converting arrays to pointers.
24 package util
25
26 import (
27 "bufio"
28 "fmt"
29 "log"
30 "os"
31 "os/user"
32 "strconv"
33 "unsafe"
34
35 "golang.org/x/sys/unix"
36 )
37
38 // Ptr converts a Go byte array to a pointer to the start of the array.
39 func Ptr(slice []byte) unsafe.Pointer {
40 if len(slice) == 0 {
41 return nil
42 }
43 return unsafe.Pointer(&slice[0])
44 }
45
46 // ByteSlice takes a pointer to some data and views it as a slice of bytes.
47 // Note, indexing into this slice is unsafe.
48 func ByteSlice(ptr unsafe.Pointer) []byte {
49 // Slice must fit in the smallest address space go supports.
50 return (*[1 << 30]byte)(ptr)[:]
51 }
52
53 // PointerSlice takes a pointer to an array of pointers and views it as a slice
54 // of pointers. Note, indexing into this slice is unsafe.
55 func PointerSlice(ptr unsafe.Pointer) []unsafe.Pointer {
56 // Slice must fit in the smallest address space go supports.
57 return (*[1 << 28]unsafe.Pointer)(ptr)[:]
58 }
59
60 // Index returns the first index i such that inVal == inArray[i].
61 // ok is true if we find a match, false otherwise.
62 func Index(inVal int64, inArray []int64) (index int, ok bool) {
63 for index, val := range inArray {
64 if val == inVal {
65 return index, true
66 }
67 }
68 return 0, false
69 }
70
71 // Lookup finds inVal in inArray and returns the corresponding element in
72 // outArray. Specifically, if inVal == inArray[i], outVal == outArray[i].
73 // ok is true if we find a match, false otherwise.
74 func Lookup(inVal int64, inArray, outArray []int64) (outVal int64, ok bool) {
75 index, ok := Index(inVal, inArray)
76 if !ok {
77 return 0, false
78 }
79 return outArray[index], true
80 }
81
82 // MinInt returns the lesser of a and b.
83 func MinInt(a, b int) int {
84 if a < b {
85 return a
86 }
87 return b
88 }
89
90 // MaxInt returns the greater of a and b.
91 func MaxInt(a, b int) int {
92 if a > b {
93 return a
94 }
95 return b
96 }
97
98 // MinInt64 returns the lesser of a and b.
99 func MinInt64(a, b int64) int64 {
100 if a < b {
101 return a
102 }
103 return b
104 }
105
106 // ReadLine returns a line of input from standard input. An empty string is
107 // returned if the user didn't insert anything or on error.
108 func ReadLine() (string, error) {
109 scanner := bufio.NewScanner(os.Stdin)
110 scanner.Scan()
111 return scanner.Text(), scanner.Err()
112 }
113
114 // AtoiOrPanic converts a string to an int or it panics. Should only be used in
115 // situations where the input MUST be a decimal number.
116 func AtoiOrPanic(input string) int {
117 i, err := strconv.Atoi(input)
118 if err != nil {
119 panic(err)
120 }
121 return i
122 }
123
124 // UserFromUID returns the User corresponding to the given user id.
125 func UserFromUID(uid int64) (*user.User, error) {
126 return user.LookupId(strconv.FormatInt(uid, 10))
127 }
128
129 // EffectiveUser returns the user entry corresponding to the effective user.
130 func EffectiveUser() (*user.User, error) {
131 return UserFromUID(int64(os.Geteuid()))
132 }
133
134 // IsUserRoot checks if the effective user is root.
135 func IsUserRoot() bool {
136 return os.Geteuid() == 0
137 }
138
139 // Chown changes the owner of a File to a User.
140 func Chown(file *os.File, user *user.User) error {
141 uid := AtoiOrPanic(user.Uid)
142 gid := AtoiOrPanic(user.Gid)
143 return file.Chown(uid, gid)
144 }
145
146 // IsKernelVersionAtLeast returns true if the Linux kernel version is at least
147 // major.minor. If something goes wrong it assumes false.
148 func IsKernelVersionAtLeast(major, minor int) bool {
149 var uname unix.Utsname
150 if err := unix.Uname(&uname); err != nil {
151 log.Printf("Uname failed [%v], assuming old kernel", err)
152 return false
153 }
154 release := string(uname.Release[:])
155 log.Printf("Kernel version is %s", release)
156 var actualMajor, actualMinor int
157 if n, _ := fmt.Sscanf(release, "%d.%d", &actualMajor, &actualMinor); n != 2 {
158 log.Printf("Unrecognized uname format %q, assuming old kernel", release)
159 return false
160 }
161 return actualMajor > major ||
162 (actualMajor == major && actualMinor >= minor)
163 }