forked from oliwur/directory_stat_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dirstatFileAge.go
52 lines (47 loc) · 1017 Bytes
/
dirstatFileAge.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
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"io/ioutil"
"os"
"path/filepath"
"time"
)
func getModTime(file string) int64 {
info, err := os.Stat(file)
if err != nil {
return time.Now().Unix()
}
return info.ModTime().Unix()
}
func getOldestFileModTimestamp(dir string, recursive bool) int64 {
if recursive {
return getOldestAgeInDirRecursively(dir)
} else {
return getOldestAgeInDir(dir)
}
}
func getOldestAgeInDirRecursively(dir string) int64 {
var oldestTs int64 = time.Now().Unix()
_ = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if info != nil && !info.IsDir() {
ts := getModTime(path)
if ts < oldestTs {
oldestTs = ts
}
}
return nil
})
return oldestTs
}
func getOldestAgeInDir(dir string) int64 {
var files, _ = ioutil.ReadDir(dir)
var oldestTs int64 = time.Now().Unix()
for _, file := range files {
if !file.IsDir() {
ts := getModTime(dir + string(os.PathSeparator) + file.Name())
if ts < oldestTs {
oldestTs = ts
}
}
}
return oldestTs
}