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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
package cache
import (
"bufio"
"code.laria.me/petrific/objects"
"fmt"
"io"
"os"
"strconv"
"strings"
"time"
)
type Cache interface {
PathUpdated(path string) (mtime time.Time, id objects.ObjectId, ok bool)
SetPathUpdated(path string, mtime time.Time, id objects.ObjectId)
Close() error
}
type NopCache struct{}
func (NopCache) PathUpdated(_ string) (_ time.Time, _ objects.ObjectId, ok bool) {
ok = false
return
}
func (NopCache) SetPathUpdated(_ string, _ time.Time, _ objects.ObjectId) {}
func (NopCache) Close() error { return nil }
type fileCacheEntry struct {
mtime time.Time
id objects.ObjectId
}
type FileCache struct {
cache map[string]fileCacheEntry
location string
}
func (fc FileCache) PathUpdated(path string) (time.Time, objects.ObjectId, bool) {
entry, ok := fc.cache[path]
return entry.mtime, entry.id, ok
}
func (fc FileCache) SetPathUpdated(path string, mtime time.Time, id objects.ObjectId) {
fc.cache[path] = fileCacheEntry{mtime, id}
}
func NewFileCache(location string) FileCache {
return FileCache{make(map[string]fileCacheEntry), location}
}
func escapeName(name string) string {
name = strings.Replace(name, "\\", "\\\\", -1)
name = strings.Replace(name, "\n", "\\n", -1)
return name
}
func unescapeName(name string) string {
name = strings.Replace(name, "\\n", "\n", -1)
name = strings.Replace(name, "\\\\", "\\", -1)
return name
}
func (fc FileCache) dump(w io.Writer) error {
for path, entry := range fc.cache {
if _, err := fmt.Fprintf(
w,
"%s %d %d %s\n",
entry.id,
entry.mtime.Unix(),
entry.mtime.Nanosecond(),
escapeName(path),
); err != nil {
return err
}
}
return nil
}
func (fc FileCache) load(r io.Reader) error {
scanner := bufio.NewScanner(r)
for scanner.Scan() {
parts := strings.SplitN(scanner.Text(), " ", 4)
if len(parts) != 4 {
return fmt.Errorf("Could not load FileCache: Expected 4 entries, got %d", len(parts))
}
id, err := objects.ParseObjectId(parts[0])
if err != nil {
return err
}
sec, err := strconv.ParseInt(parts[1], 10, 64)
if err != nil {
return err
}
nsec, err := strconv.ParseInt(parts[2], 10, 64)
if err != nil {
return err
}
fc.cache[unescapeName(parts[3])] = fileCacheEntry{time.Unix(sec, nsec), id}
}
return scanner.Err()
}
func (fc FileCache) Load() error {
f, err := os.Open(fc.location)
switch {
case os.IsNotExist(err):
return nil
case err != nil:
return err
default:
}
defer f.Close()
return fc.load(f)
}
func (fc FileCache) Close() error {
f, err := os.Create(fc.location)
if err != nil {
return err
}
defer f.Close()
return fc.dump(f)
}
|