File: modkey_unix.go

package info (click to toggle)
golang-github-evanw-esbuild 0.25.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,184 kB
  • sloc: javascript: 28,602; makefile: 856; sh: 17
file content (41 lines) | stat: -rw-r--r-- 950 bytes parent folder | download | duplicates (3)
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
}