mirror of
https://github.com/ceph/ceph-csi.git
synced 2025-06-13 02:33:34 +00:00
vendor: vendor fscrypt integration dependencies
Signed-off-by: Marcel Lauhoff <marcel.lauhoff@suse.com>
This commit is contained in:
committed by
mergify[bot]
parent
cfea8d7562
commit
f8faffac89
135
vendor/github.com/google/fscrypt/util/errors.go
generated
vendored
Normal file
135
vendor/github.com/google/fscrypt/util/errors.go
generated
vendored
Normal file
@ -0,0 +1,135 @@
|
||||
/*
|
||||
* errors.go - Custom errors and error functions used by fscrypt
|
||||
*
|
||||
* Copyright 2017 Google Inc.
|
||||
* Author: Joe Richey (joerichey@google.com)
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of
|
||||
* the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations under
|
||||
* the License.
|
||||
*/
|
||||
|
||||
package util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// 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
|
||||
// do all of the error checking at the end. For example:
|
||||
//
|
||||
// r := NewErrReader(reader)
|
||||
// r.Read(foo)
|
||||
// r.Read(bar)
|
||||
// r.Read(baz)
|
||||
// if r.Err() != nil {
|
||||
// // Handle error
|
||||
// }
|
||||
//
|
||||
// Taken from https://blog.golang.org/errors-are-values by Rob Pike.
|
||||
type ErrReader struct {
|
||||
r io.Reader
|
||||
err error
|
||||
}
|
||||
|
||||
// NewErrReader creates an ErrReader which wraps the provided reader.
|
||||
func NewErrReader(reader io.Reader) *ErrReader {
|
||||
return &ErrReader{r: reader, err: nil}
|
||||
}
|
||||
|
||||
// Read runs ReadFull on the wrapped reader if no errors have occurred.
|
||||
// Otherwise, the previous error is just returned and no reads are attempted.
|
||||
func (e *ErrReader) Read(p []byte) (n int, err error) {
|
||||
if e.err == nil {
|
||||
n, e.err = io.ReadFull(e.r, p)
|
||||
}
|
||||
return n, e.err
|
||||
}
|
||||
|
||||
// Err returns the first encountered err (or nil if no errors occurred).
|
||||
func (e *ErrReader) Err() error {
|
||||
return e.err
|
||||
}
|
||||
|
||||
// ErrWriter works exactly like ErrReader, except with io.Writer.
|
||||
type ErrWriter struct {
|
||||
w io.Writer
|
||||
err error
|
||||
}
|
||||
|
||||
// NewErrWriter creates an ErrWriter which wraps the provided writer.
|
||||
func NewErrWriter(writer io.Writer) *ErrWriter {
|
||||
return &ErrWriter{w: writer, err: nil}
|
||||
}
|
||||
|
||||
// Write runs the wrapped writer's Write if no errors have occurred. Otherwise,
|
||||
// the previous error is just returned and no writes are attempted.
|
||||
func (e *ErrWriter) Write(p []byte) (n int, err error) {
|
||||
if e.err == nil {
|
||||
n, e.err = e.w.Write(p)
|
||||
}
|
||||
return n, e.err
|
||||
}
|
||||
|
||||
// Err returns the first encountered err (or nil if no errors occurred).
|
||||
func (e *ErrWriter) Err() error {
|
||||
return e.err
|
||||
}
|
||||
|
||||
// CheckValidLength returns an invalid length error if expected != actual
|
||||
func CheckValidLength(expected, actual int) error {
|
||||
if expected == actual {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("expected length of %d, got %d", expected, actual)
|
||||
}
|
||||
|
||||
// SystemError is an error that should indicate something has gone wrong in the
|
||||
// underlying system (syscall failure, bad ioctl, etc...).
|
||||
type SystemError string
|
||||
|
||||
func (s SystemError) Error() string {
|
||||
return "system error: " + string(s)
|
||||
}
|
||||
|
||||
// NeverError panics if a non-nil error is passed in. It should be used to check
|
||||
// for logic errors, not to handle recoverable errors.
|
||||
func NeverError(err error) {
|
||||
if err != nil {
|
||||
log.Panicf("NeverError() check failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
// testEnvVarName is the name of 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
|
||||
}
|
163
vendor/github.com/google/fscrypt/util/util.go
generated
vendored
Normal file
163
vendor/github.com/google/fscrypt/util/util.go
generated
vendored
Normal file
@ -0,0 +1,163 @@
|
||||
/*
|
||||
* util.go - Various helpers used throughout fscrypt
|
||||
*
|
||||
* Copyright 2017 Google Inc.
|
||||
* Author: Joe Richey (joerichey@google.com)
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of
|
||||
* the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations under
|
||||
* the License.
|
||||
*/
|
||||
|
||||
// Package util contains useful components for simplifying Go code.
|
||||
//
|
||||
// The package contains common error types (errors.go) and functions for
|
||||
// converting arrays to pointers.
|
||||
package util
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/user"
|
||||
"strconv"
|
||||
"unsafe"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// Ptr converts a Go byte array to a pointer to the start of the array.
|
||||
func Ptr(slice []byte) unsafe.Pointer {
|
||||
if len(slice) == 0 {
|
||||
return nil
|
||||
}
|
||||
return unsafe.Pointer(&slice[0])
|
||||
}
|
||||
|
||||
// ByteSlice takes a pointer to some data and views it as a slice of bytes.
|
||||
// Note, indexing into this slice is unsafe.
|
||||
func ByteSlice(ptr unsafe.Pointer) []byte {
|
||||
// Slice must fit in the smallest address space go supports.
|
||||
return (*[1 << 30]byte)(ptr)[:]
|
||||
}
|
||||
|
||||
// PointerSlice takes a pointer to an array of pointers and views it as a slice
|
||||
// of pointers. Note, indexing into this slice is unsafe.
|
||||
func PointerSlice(ptr unsafe.Pointer) []unsafe.Pointer {
|
||||
// Slice must fit in the smallest address space go supports.
|
||||
return (*[1 << 28]unsafe.Pointer)(ptr)[:]
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// MinInt returns the lesser of a and b.
|
||||
func MinInt(a, b int) int {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// MaxInt returns the greater of a and b.
|
||||
func MaxInt(a, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// MinInt64 returns the lesser of a and b.
|
||||
func MinInt64(a, b int64) int64 {
|
||||
if a < b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// ReadLine returns a line of input from standard input. An empty string is
|
||||
// returned if the user didn't insert anything or on error.
|
||||
func ReadLine() (string, error) {
|
||||
scanner := bufio.NewScanner(os.Stdin)
|
||||
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
|
||||
}
|
||||
|
||||
// UserFromUID returns the User corresponding to the given user id.
|
||||
func UserFromUID(uid int64) (*user.User, error) {
|
||||
return user.LookupId(strconv.FormatInt(uid, 10))
|
||||
}
|
||||
|
||||
// EffectiveUser returns the user entry corresponding to the effective user.
|
||||
func EffectiveUser() (*user.User, error) {
|
||||
return UserFromUID(int64(os.Geteuid()))
|
||||
}
|
||||
|
||||
// IsUserRoot checks if the effective user is root.
|
||||
func IsUserRoot() bool {
|
||||
return os.Geteuid() == 0
|
||||
}
|
||||
|
||||
// Chown changes the owner of a File to a User.
|
||||
func Chown(file *os.File, user *user.User) error {
|
||||
uid := AtoiOrPanic(user.Uid)
|
||||
gid := AtoiOrPanic(user.Gid)
|
||||
return file.Chown(uid, gid)
|
||||
}
|
||||
|
||||
// IsKernelVersionAtLeast returns true if the Linux kernel version is at least
|
||||
// major.minor. If something goes wrong it assumes false.
|
||||
func IsKernelVersionAtLeast(major, minor int) bool {
|
||||
var uname unix.Utsname
|
||||
if err := unix.Uname(&uname); err != nil {
|
||||
log.Printf("Uname failed [%v], assuming old kernel", err)
|
||||
return false
|
||||
}
|
||||
release := string(uname.Release[:])
|
||||
log.Printf("Kernel version is %s", release)
|
||||
var actualMajor, actualMinor int
|
||||
if n, _ := fmt.Sscanf(release, "%d.%d", &actualMajor, &actualMinor); n != 2 {
|
||||
log.Printf("Unrecognized uname format %q, assuming old kernel", release)
|
||||
return false
|
||||
}
|
||||
return actualMajor > major ||
|
||||
(actualMajor == major && actualMinor >= minor)
|
||||
}
|
Reference in New Issue
Block a user