-
Notifications
You must be signed in to change notification settings - Fork 342
/
local_fs_os.go
174 lines (136 loc) · 4.14 KB
/
local_fs_os.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
package localfs
import (
"context"
"io"
"os"
"path/filepath"
"runtime"
"strings"
"syscall"
"github.com/pkg/errors"
"github.com/kopia/kopia/fs"
)
type filesystemDirectoryIterator struct {
dirHandle *os.File
childPrefix string
currentIndex int
currentBatch []os.DirEntry
}
func (it *filesystemDirectoryIterator) Next(ctx context.Context) (fs.Entry, error) {
for {
// we're at the end of the current batch, fetch the next batch
if it.currentIndex >= len(it.currentBatch) {
batch, err := it.dirHandle.ReadDir(numEntriesToRead)
if err != nil && !errors.Is(err, io.EOF) {
// stop iteration
return nil, err //nolint:wrapcheck
}
it.currentIndex = 0
it.currentBatch = batch
// got empty batch
if len(batch) == 0 {
return nil, nil
}
}
n := it.currentIndex
it.currentIndex++
e, err := toDirEntryOrNil(it.currentBatch[n], it.childPrefix)
if err != nil {
// stop iteration
return nil, err
}
if e == nil {
// go to the next item
continue
}
return e, nil
}
}
func (it *filesystemDirectoryIterator) Close() {
it.dirHandle.Close() //nolint:errcheck
}
func (fsd *filesystemDirectory) Iterate(ctx context.Context) (fs.DirectoryIterator, error) {
fullPath := fsd.fullPath()
f, direrr := os.Open(fullPath) //nolint:gosec
if direrr != nil {
return nil, errors.Wrap(direrr, "unable to read directory")
}
childPrefix := fullPath + string(filepath.Separator)
return &filesystemDirectoryIterator{dirHandle: f, childPrefix: childPrefix}, nil
}
func (fsd *filesystemDirectory) Child(ctx context.Context, name string) (fs.Entry, error) {
fullPath := fsd.fullPath()
st, err := os.Lstat(filepath.Join(fullPath, name))
if err != nil {
if os.IsNotExist(err) {
return nil, fs.ErrEntryNotFound
}
return nil, errors.Wrap(err, "unable to get child")
}
return entryFromDirEntry(st, fullPath+string(filepath.Separator)), nil
}
func toDirEntryOrNil(dirEntry os.DirEntry, prefix string) (fs.Entry, error) {
fi, err := os.Lstat(prefix + dirEntry.Name())
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, errors.Wrap(err, "error reading directory")
}
return entryFromDirEntry(fi, prefix), nil
}
// NewEntry returns fs.Entry for the specified path, the result will be one of supported entry types: fs.File, fs.Directory, fs.Symlink
// or fs.UnsupportedEntry.
func NewEntry(path string) (fs.Entry, error) {
path = filepath.Clean(path)
fi, err := os.Lstat(path)
if err != nil {
// Paths such as `\\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy01`
// cause os.Lstat to fail with "Incorrect function" error unless they
// end with a separator. Retry the operation with the separator added.
var e syscall.Errno
//nolint:goconst
if runtime.GOOS == "windows" &&
!strings.HasSuffix(path, string(filepath.Separator)) &&
errors.As(err, &e) && e == 1 {
fi, err = os.Lstat(path + string(filepath.Separator))
}
if err != nil {
return nil, errors.Wrap(err, "unable to determine entry type")
}
}
if path == "/" {
return entryFromDirEntry(fi, ""), nil
}
return entryFromDirEntry(fi, dirPrefix(path)), nil
}
func entryFromDirEntry(fi os.FileInfo, prefix string) fs.Entry {
isplaceholder := strings.HasSuffix(fi.Name(), ShallowEntrySuffix)
maskedmode := fi.Mode() & os.ModeType
switch {
case maskedmode == os.ModeDir && !isplaceholder:
return newFilesystemDirectory(newEntry(fi, prefix))
case maskedmode == os.ModeDir && isplaceholder:
return newShallowFilesystemDirectory(newEntry(fi, prefix))
case maskedmode == os.ModeSymlink && !isplaceholder:
return newFilesystemSymlink(newEntry(fi, prefix))
case maskedmode == 0 && !isplaceholder:
return newFilesystemFile(newEntry(fi, prefix))
case maskedmode == 0 && isplaceholder:
return newShallowFilesystemFile(newEntry(fi, prefix))
default:
return newFilesystemErrorEntry(newEntry(fi, prefix), fs.ErrUnknown)
}
}
var _ os.FileInfo = (*filesystemEntry)(nil)
func newEntry(fi os.FileInfo, prefix string) filesystemEntry {
return filesystemEntry{
TrimShallowSuffix(fi.Name()),
fi.Size(),
fi.ModTime().UnixNano(),
fi.Mode(),
platformSpecificOwnerInfo(fi),
platformSpecificDeviceInfo(fi),
prefix,
}
}