/
modkey_unix.go
41 lines (34 loc) · 950 Bytes
/
modkey_unix.go
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
//go:build darwin || freebsd || linux
// +build darwin freebsd linux
package fs
import (
"time"
"golang.org/x/sys/unix"
)
func modKey(path string) (ModKey, error) {
stat := unix.Stat_t{}
if err := unix.Stat(path, &stat); err != nil {
return ModKey{}, err
}
// We can't detect changes if the file system zeros out the modification time
if stat.Mtim.Sec == 0 && stat.Mtim.Nsec == 0 {
return ModKey{}, modKeyUnusable
}
// Don't generate a modification key if the file is too new
now, err := unix.TimeToTimespec(time.Now())
if err != nil {
return ModKey{}, err
}
mtimeSec := stat.Mtim.Sec + modKeySafetyGap
if mtimeSec > now.Sec || (mtimeSec == now.Sec && stat.Mtim.Nsec > now.Nsec) {
return ModKey{}, modKeyUnusable
}
return ModKey{
inode: stat.Ino,
size: stat.Size,
mtime_sec: int64(stat.Mtim.Sec),
mtime_nsec: int64(stat.Mtim.Nsec),
mode: uint32(stat.Mode),
uid: stat.Uid,
}, nil
}