-
Notifications
You must be signed in to change notification settings - Fork 350
/
local_fs.go
162 lines (126 loc) · 3.35 KB
/
local_fs.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
package localfs
import (
"context"
"os"
"path/filepath"
"time"
"github.com/pkg/errors"
"github.com/kopia/kopia/fs"
)
const numEntriesToRead = 100 // number of directory entries to read in one shot
type filesystemEntry struct {
name string
size int64
mtimeNanos int64
mode os.FileMode
owner fs.OwnerInfo
device fs.DeviceInfo
prefix string
}
func (e *filesystemEntry) Name() string {
return e.name
}
func (e *filesystemEntry) IsDir() bool {
return e.mode.IsDir()
}
func (e *filesystemEntry) Mode() os.FileMode {
return e.mode
}
func (e *filesystemEntry) Size() int64 {
return e.size
}
func (e *filesystemEntry) ModTime() time.Time {
return time.Unix(0, e.mtimeNanos)
}
func (e *filesystemEntry) Sys() interface{} {
return nil
}
func (e *filesystemEntry) fullPath() string {
return e.prefix + e.Name()
}
func (e *filesystemEntry) Owner() fs.OwnerInfo {
return e.owner
}
func (e *filesystemEntry) Device() fs.DeviceInfo {
return e.device
}
func (e *filesystemEntry) LocalFilesystemPath() string {
return e.fullPath()
}
type filesystemDirectory struct {
filesystemEntry
}
type filesystemSymlink struct {
filesystemEntry
}
type filesystemFile struct {
filesystemEntry
}
type filesystemErrorEntry struct {
filesystemEntry
err error
}
func (fsd *filesystemDirectory) SupportsMultipleIterations() bool {
return true
}
func (fsd *filesystemDirectory) Size() int64 {
// force directory size to always be zero
return 0
}
type fileWithMetadata struct {
*os.File
}
func (f *fileWithMetadata) Entry() (fs.Entry, error) {
fi, err := f.Stat()
if err != nil {
return nil, errors.Wrap(err, "unable to stat() local file")
}
return newFilesystemFile(newEntry(fi, dirPrefix(f.Name()))), nil
}
func (fsf *filesystemFile) Open(ctx context.Context) (fs.Reader, error) {
f, err := os.Open(fsf.fullPath())
if err != nil {
return nil, errors.Wrap(err, "unable to open local file")
}
return &fileWithMetadata{f}, nil
}
func (fsl *filesystemSymlink) Readlink(ctx context.Context) (string, error) {
//nolint:wrapcheck
return os.Readlink(fsl.fullPath())
}
func (e *filesystemErrorEntry) ErrorInfo() error {
return e.err
}
// dirPrefix returns the directory prefix for a given path - the initial part of the path up to and including the final slash (or backslash on Windows).
// this is similar to filepath.Dir() except dirPrefix("\\foo\bar") == "\\foo\", which is unsupported in filepath.
func dirPrefix(s string) string {
for i := len(s) - 1; i >= 0; i-- {
if s[i] == filepath.Separator || s[i] == '/' {
return s[0 : i+1]
}
}
return ""
}
// Directory returns fs.Directory for the specified path.
func Directory(path string) (fs.Directory, error) {
e, err := NewEntry(path)
if err != nil {
return nil, err
}
switch e := e.(type) {
case *filesystemDirectory:
return e, nil
case *filesystemSymlink:
// it's a symbolic link, possibly to a directory, it may work or we may get a ReadDir() error.
// this is apparently how VSS mounted snapshots appear on Windows and attempts to os.Readlink() fail on them.
return newFilesystemDirectory(e.filesystemEntry), nil
default:
return nil, errors.Errorf("not a directory: %v (was %T)", path, e)
}
}
var (
_ fs.Directory = (*filesystemDirectory)(nil)
_ fs.File = (*filesystemFile)(nil)
_ fs.Symlink = (*filesystemSymlink)(nil)
_ fs.ErrorEntry = (*filesystemErrorEntry)(nil)
)