-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisk_interface.go
238 lines (211 loc) · 5.81 KB
/
disk_interface.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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Copyright 2011 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package nin
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
"syscall"
)
// FileReader is an interface for reading files from disk.
//
// See DiskInterface for details. This base offers the minimum interface needed
// just to read files.
type FileReader interface {
// ReadFile reads a file and returns its content.
//
// Unlike os.ReadFile(), if the content is not empty, it appends a zero byte
// at the end of the slice.
ReadFile(path string) ([]byte, error)
}
// DiskInterface is an interface for accessing the disk.
//
// Abstract so it can be mocked out for tests. The real implementation is
// RealDiskInterface.
type DiskInterface interface {
FileReader
// Stat stat()'s a file, returning the mtime, or 0 if missing and -1 on
// other errors.
Stat(path string) (TimeStamp, error)
// MakeDir creates a directory, returning false on failure.
MakeDir(path string) error
// WriteFile creates a file, with the specified name and contents
WriteFile(path, contents string) error
// RemoveFile removes the file named path.
//
// It should return an error that matches os.IsNotExist() if the file was not
// present.
RemoveFile(path string) error
}
type dirCache map[string]TimeStamp
type cache map[string]dirCache
func dirName(path string) string {
return filepath.Dir(path)
/*
pathSeparators := "\\/"
end := pathSeparators + len(pathSeparators) - 1
slashPos := path.findLastOf(pathSeparators)
if slashPos == -1 {
return "" // Nothing to do.
}
for slashPos > 0 && find(pathSeparators, end, path[slashPos-1]) != end {
slashPos--
}
return path[0:slashPos]
*/
}
func statSingleFile(path string) (TimeStamp, error) {
s, err := os.Stat(path)
if err != nil {
// See TestDiskInterfaceTest_StatMissingFile for rationale for ENOTDIR
// check.
if os.IsNotExist(err) || errors.Unwrap(err) == syscall.ENOTDIR {
return 0, nil
}
return -1, err
}
return TimeStamp(s.ModTime().UnixMicro()), nil
}
func statAllFilesInDir(dir string, stamps map[string]TimeStamp) error {
f, err := os.Open(dir)
if err != nil {
return err
}
d, err := f.Readdir(0)
if err != nil {
_ = f.Close()
return err
}
for _, i := range d {
if !i.IsDir() {
stamps[i.Name()] = TimeStamp(i.ModTime().UnixMicro())
}
}
return f.Close()
}
// MakeDirs create all the parent directories for path; like mkdir -p
// `basename path`.
func MakeDirs(d DiskInterface, path string) error {
dir := dirName(path)
if dir == path || dir == "." || dir == "" {
return nil // Reached root; assume it's there.
}
mtime, err := d.Stat(dir)
if mtime < 0 {
return err
}
if mtime > 0 {
return nil // Exists already; we're done.
}
// Directory doesn't exist. Try creating its parent first.
if err := MakeDirs(d, dir); err != nil {
return err
}
return d.MakeDir(dir)
}
//
// RealDiskInterface is the implementation of DiskInterface that actually hits
// the disk.
type RealDiskInterface struct {
// Whether stat information can be cached.
useCache bool
// TODO: Neither a map nor a hashmap seems ideal here. If the statcache
// works out, come up with a better data structure.
cache cache
}
// MSDN: "Naming Files, Paths, and Namespaces"
// http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
const maxPath = 260
// Stat implements DiskInterface.
func (r *RealDiskInterface) Stat(path string) (TimeStamp, error) {
defer metricRecord("node stat")()
if runtime.GOOS == "windows" {
if path != "" && path[0] != '\\' && len(path) >= maxPath {
return -1, fmt.Errorf("Stat(%s): Filename longer than %d characters", path, maxPath)
}
if !r.useCache {
return statSingleFile(path)
}
dir := dirName(path)
o := 0
if dir != "" {
o = len(dir) + 1
}
base := path[o:]
if base == ".." {
// statAllFilesInDir does not report any information for base = "..".
base = "."
dir = path
}
dir = strings.ToLower(dir)
base = strings.ToLower(base)
ci, ok := r.cache[dir]
if !ok {
ci = dirCache{}
r.cache[dir] = ci
s := "."
if dir != "" {
s = dir
}
if err := statAllFilesInDir(s, ci); err != nil {
delete(r.cache, dir)
return -1, err
}
}
return ci[base], nil
}
return statSingleFile(path)
}
// WriteFile implements DiskInterface.
func (r *RealDiskInterface) WriteFile(path string, contents string) error {
return ioutil.WriteFile(path, unsafeByteSlice(contents), 0o666)
}
// MakeDir implements DiskInterface.
func (r *RealDiskInterface) MakeDir(path string) error {
return os.Mkdir(path, 0o777)
}
// ReadFile implements DiskInterface.
func (r *RealDiskInterface) ReadFile(path string) ([]byte, error) {
c, err := ioutil.ReadFile(path)
if err == nil {
if len(c) != 0 {
// ioutil.ReadFile() is guaranteed to have an extra byte in the slice,
// (ab)use it.
c = c[:len(c)+1]
}
return c, nil
}
return nil, err
}
// RemoveFile implements DiskInterface.
func (r *RealDiskInterface) RemoveFile(path string) error {
return os.Remove(path)
}
// AllowStatCache sets whether stat information can be cached.
//
// Only has an effect on Windows.
func (r *RealDiskInterface) AllowStatCache(allow bool) {
if runtime.GOOS == "windows" {
r.useCache = allow
if !r.useCache {
r.cache = nil
} else if r.cache == nil {
r.cache = cache{}
}
}
}