| 1 |
/* |
| 2 |
* path.go - Utility functions for dealing with filesystem paths |
| 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 filesystem |
| 21 |
|
| 22 |
import ( |
| 23 |
"fmt" |
| 24 |
"log" |
| 25 |
"os" |
| 26 |
"path/filepath" |
| 27 |
|
| 28 |
"golang.org/x/sys/unix" |
| 29 |
|
| 30 |
"github.com/pkg/errors" |
| 31 |
) |
| 32 |
|
| 33 |
// OpenFileOverridingUmask calls os.OpenFile but with the umask overridden so |
| 34 |
// that no permission bits are masked out if the file is created. |
| 35 |
func OpenFileOverridingUmask(name string, flag int, perm os.FileMode) (*os.File, error) { |
| 36 |
oldMask := unix.Umask(0) |
| 37 |
defer unix.Umask(oldMask) |
| 38 |
return os.OpenFile(name, flag, perm) |
| 39 |
} |
| 40 |
|
| 41 |
// canonicalizePath turns path into an absolute path without symlinks. |
| 42 |
func canonicalizePath(path string) (string, error) { |
| 43 |
path, err := filepath.Abs(path) |
| 44 |
if err != nil { |
| 45 |
return "", err |
| 46 |
} |
| 47 |
path, err = filepath.EvalSymlinks(path) |
| 48 |
|
| 49 |
// Get a better error if we have an invalid path |
| 50 |
if pathErr, ok := err.(*os.PathError); ok { |
| 51 |
err = errors.Wrap(pathErr.Err, pathErr.Path) |
| 52 |
} |
| 53 |
|
| 54 |
return path, err |
| 55 |
} |
| 56 |
|
| 57 |
// loggedStat runs os.Stat, but it logs the error if stat returns any error |
| 58 |
// other than nil or IsNotExist. |
| 59 |
func loggedStat(name string) (os.FileInfo, error) { |
| 60 |
info, err := os.Stat(name) |
| 61 |
if err != nil && !os.IsNotExist(err) { |
| 62 |
log.Print(err) |
| 63 |
} |
| 64 |
return info, err |
| 65 |
} |
| 66 |
|
| 67 |
// loggedLstat runs os.Lstat (doesn't dereference trailing symlink), but it logs |
| 68 |
// the error if lstat returns any error other than nil or IsNotExist. |
| 69 |
func loggedLstat(name string) (os.FileInfo, error) { |
| 70 |
info, err := os.Lstat(name) |
| 71 |
if err != nil && !os.IsNotExist(err) { |
| 72 |
log.Print(err) |
| 73 |
} |
| 74 |
return info, err |
| 75 |
} |
| 76 |
|
| 77 |
// isDir returns true if the path exists and is that of a directory. |
| 78 |
func isDir(path string) bool { |
| 79 |
info, err := loggedStat(path) |
| 80 |
return err == nil && info.IsDir() |
| 81 |
} |
| 82 |
|
| 83 |
// isRegularFile returns true if the path exists and is that of a regular file. |
| 84 |
func isRegularFile(path string) bool { |
| 85 |
info, err := loggedStat(path) |
| 86 |
return err == nil && info.Mode().IsRegular() |
| 87 |
} |
| 88 |
|
| 89 |
// HaveReadAccessTo returns true if the process has read access to a file or |
| 90 |
// directory, without actually opening it. |
| 91 |
func HaveReadAccessTo(path string) bool { |
| 92 |
return unix.Access(path, unix.R_OK) == nil |
| 93 |
} |
| 94 |
|
| 95 |
// DeviceNumber represents a combined major:minor device number. |
| 96 |
type DeviceNumber uint64 |
| 97 |
|
| 98 |
func (num DeviceNumber) String() string { |
| 99 |
return fmt.Sprintf("%d:%d", unix.Major(uint64(num)), unix.Minor(uint64(num))) |
| 100 |
} |
| 101 |
|
| 102 |
func newDeviceNumberFromString(str string) (DeviceNumber, error) { |
| 103 |
var major, minor uint32 |
| 104 |
if count, _ := fmt.Sscanf(str, "%d:%d", &major, &minor); count != 2 { |
| 105 |
return 0, errors.Errorf("invalid device number string %q", str) |
| 106 |
} |
| 107 |
return DeviceNumber(unix.Mkdev(major, minor)), nil |
| 108 |
} |
| 109 |
|
| 110 |
// getDeviceNumber returns the device number of the device node at the given |
| 111 |
// path. If there is a symlink at the path, it is dereferenced. |
| 112 |
func getDeviceNumber(path string) (DeviceNumber, error) { |
| 113 |
var stat unix.Stat_t |
| 114 |
if err := unix.Stat(path, &stat); err != nil { |
| 115 |
return 0, err |
| 116 |
} |
| 117 |
return DeviceNumber(stat.Rdev), nil |
| 118 |
} |
| 119 |
|
| 120 |
// getNumberOfContainingDevice returns the device number of the filesystem which |
| 121 |
// contains the given file. If the file is a symlink, it is not dereferenced. |
| 122 |
func getNumberOfContainingDevice(path string) (DeviceNumber, error) { |
| 123 |
var stat unix.Stat_t |
| 124 |
if err := unix.Lstat(path, &stat); err != nil { |
| 125 |
return 0, err |
| 126 |
} |
| 127 |
return DeviceNumber(stat.Dev), nil |
| 128 |
} |