aboutsummaryrefslogtreecommitdiff
path: root/filesystem/path.go
diff options
context:
space:
mode:
authorEric Biggers <ebiggers@google.com>2019-10-29 00:04:39 -0700
committerEric Biggers <ebiggers@google.com>2019-10-30 09:11:29 -0700
commitd9d2b32f9fa9e39b154b71b2abc9eda43d5aaa3c (patch)
tree74a26e4fc5219e98c990579248e76d7a6ccc6369 /filesystem/path.go
parentc7b963bfa76b9541e01493d21fef3e3596ef9904 (diff)
filesystem: add device number utilities
Add a utility type and functions for handling device numbers.
Diffstat (limited to 'filesystem/path.go')
-rw-r--r--filesystem/path.go26
1 files changed, 26 insertions, 0 deletions
diff --git a/filesystem/path.go b/filesystem/path.go
index cfc3dc0..a99b743 100644
--- a/filesystem/path.go
+++ b/filesystem/path.go
@@ -20,6 +20,7 @@
package filesystem
import (
+ "fmt"
"log"
"os"
"path/filepath"
@@ -99,3 +100,28 @@ func isRegularFile(path string) bool {
info, err := loggedStat(path)
return err == nil && info.Mode().IsRegular()
}
+
+// DeviceNumber represents a combined major:minor device number.
+type DeviceNumber uint64
+
+func (num DeviceNumber) String() string {
+ return fmt.Sprintf("%d:%d", unix.Major(uint64(num)), unix.Minor(uint64(num)))
+}
+
+func newDeviceNumberFromString(str string) (DeviceNumber, error) {
+ var major, minor uint32
+ if count, _ := fmt.Sscanf(str, "%d:%d", &major, &minor); count != 2 {
+ return 0, errors.Errorf("invalid device number string %q", str)
+ }
+ return DeviceNumber(unix.Mkdev(major, minor)), nil
+}
+
+// getDeviceNumber returns the device number of the device node at the given
+// path. If there is a symlink at the path, it is dereferenced.
+func getDeviceNumber(path string) (DeviceNumber, error) {
+ var stat unix.Stat_t
+ if err := unix.Stat(path, &stat); err != nil {
+ return 0, err
+ }
+ return DeviceNumber(stat.Rdev), nil
+}