aboutsummaryrefslogtreecommitdiff
path: root/contrib/statvsfstat
diff options
context:
space:
mode:
authorJakob Unterwurzacher2019-11-16 21:19:08 +0100
committerJakob Unterwurzacher2019-11-16 21:19:08 +0100
commit7e9e63b43024919fb5ea26939027680d11b49df4 (patch)
treed42e647920127ece1b7feb6e008b3285cf3b0990 /contrib/statvsfstat
parentf7ddae24eda373e8680ffb9d4b7ef74dea36f60f (diff)
contrib/statvsfstat: add statvsfstat tool
Cli tool to compare the result of Stat() and Fstat().
Diffstat (limited to 'contrib/statvsfstat')
-rw-r--r--contrib/statvsfstat/.gitignore1
-rw-r--r--contrib/statvsfstat/statvsfstat.go53
2 files changed, 54 insertions, 0 deletions
diff --git a/contrib/statvsfstat/.gitignore b/contrib/statvsfstat/.gitignore
new file mode 100644
index 0000000..7f17e5f
--- /dev/null
+++ b/contrib/statvsfstat/.gitignore
@@ -0,0 +1 @@
+/statvsfstat
diff --git a/contrib/statvsfstat/statvsfstat.go b/contrib/statvsfstat/statvsfstat.go
new file mode 100644
index 0000000..e5159ce
--- /dev/null
+++ b/contrib/statvsfstat/statvsfstat.go
@@ -0,0 +1,53 @@
+package main
+
+import (
+ "flag"
+ "fmt"
+ "os"
+
+ "golang.org/x/sys/unix"
+)
+
+const (
+ myName = "statvsfstat"
+)
+
+func main() {
+ flag.Usage = func() {
+ fmt.Fprintf(os.Stderr, "Usage: %s PATH\n", myName)
+ fmt.Fprintf(os.Stderr, "Dump the stat and fstat information for PATH to the console, JSON format.\n")
+ os.Exit(1)
+ }
+ flag.Parse()
+ if flag.NArg() != 1 {
+ flag.Usage()
+ }
+ path := flag.Arg(0)
+
+ var st unix.Stat_t
+ err := unix.Stat(path, &st)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "stat syscall returned error: %v\n", err)
+ os.Exit(4)
+ }
+
+ fd, err := unix.Open(path, unix.O_RDONLY, 0)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "open syscall returned error: %v\n", err)
+ os.Exit(3)
+ }
+ var fst unix.Stat_t
+ err = unix.Fstat(fd, &fst)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "fstat syscall returned error: %v\n", err)
+ os.Exit(2)
+ }
+
+ fmt.Printf("stat result: %#v\n", st)
+ fmt.Printf("fstat result: %#v\n", fst)
+ if st == fst {
+ fmt.Println("results are identical")
+ } else {
+ fmt.Println("results differ")
+ }
+}