aboutsummaryrefslogtreecommitdiff
path: root/contrib/statvsfstat/statvsfstat.go
blob: e5159ce1da3b5a368565934b41455f3eedbc019b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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")
	}
}