-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosfs_windows.go
245 lines (203 loc) · 5.17 KB
/
osfs_windows.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
239
240
241
242
243
244
245
package osfs
import (
"errors"
"io"
"io/fs"
"math/bits"
"os"
"regexp"
"strings"
"time"
"golang.org/x/sys/windows"
)
var ErrIsDirectory = errors.New("is a directory")
var drivePathRe = regexp.MustCompile(`^[a-zA-Z]:$`)
var windowsPathRe = regexp.MustCompile(`^[a-zA-Z]:`)
func fsToOsPath(fs_name string) (string, error) {
if !fs.ValidPath(fs_name) {
return "", fs.ErrInvalid
}
if fs_name == "." {
return "", fs.ErrNotExist
}
if !windowsPathRe.MatchString(fs_name) {
return "", fs.ErrNotExist
}
win_name := strings.ReplaceAll(fs_name, "/", `\`)
if drivePathRe.MatchString(win_name) {
win_name = win_name + `\`
}
return win_name, nil
}
func (f OsFS) Open(fs_name string) (fs.File, error) {
if !fs.ValidPath(fs_name) {
return nil, &fs.PathError{Op: "open", Path: fs_name, Err: fs.ErrInvalid}
}
if fs_name == "." {
return newRootFile()
}
if !windowsPathRe.MatchString(fs_name) {
return nil, &fs.PathError{Op: "open", Path: fs_name, Err: fs.ErrNotExist}
}
os_name, err := fsToOsPath(fs_name)
if err != nil {
return nil, &fs.PathError{Op: "open", Path: fs_name, Err: err}
}
return os.Open(os_name)
}
func (f OsFS) Stat(fs_name string) (fs.FileInfo, error) {
if !fs.ValidPath(fs_name) {
return nil, &fs.PathError{Op: "stat", Path: fs_name, Err: fs.ErrInvalid}
}
if fs_name == "." {
return dummyDirInfo("."), nil
}
if !windowsPathRe.MatchString(fs_name) {
return nil, &fs.PathError{Op: "stat", Path: fs_name, Err: fs.ErrNotExist}
}
if drivePathRe.MatchString(fs_name) {
drv := strings.ToUpper(fs_name)[0]
if !found_drive(drv) {
return nil, &fs.PathError{Op: "stat", Path: fs_name, Err: fs.ErrNotExist}
}
return dummyDirInfo(fs_name), nil
}
os_name, err := fsToOsPath(fs_name)
if err != nil {
return nil, &fs.PathError{Op: "stat", Path: fs_name, Err: err}
}
return os.Stat(os_name)
}
func (f OsFS) ReadFile(fs_name string) ([]byte, error) {
if !fs.ValidPath(fs_name) {
return nil, &fs.PathError{Op: "readfile", Path: fs_name, Err: fs.ErrInvalid}
}
if fs_name == "." {
return nil, &fs.PathError{Op: "readfile", Path: fs_name, Err: ErrIsDirectory}
}
if !windowsPathRe.MatchString(fs_name) {
return nil, &fs.PathError{Op: "readfile", Path: fs_name, Err: fs.ErrNotExist}
}
if drivePathRe.MatchString(fs_name) {
return nil, &fs.PathError{Op: "readfile", Path: fs_name, Err: ErrIsDirectory}
}
os_name, err := fsToOsPath(fs_name)
if err != nil {
return nil, &fs.PathError{Op: "readfile", Path: fs_name, Err: err}
}
return os.ReadFile(os_name)
}
func (f OsFS) ReadDir(fs_name string) ([]fs.DirEntry, error) {
if !fs.ValidPath(fs_name) {
return nil, &fs.PathError{Op: "readdir", Path: fs_name, Err: fs.ErrInvalid}
}
if fs_name == "." {
dent, err := get_drives()
if err != nil {
return nil, &fs.PathError{Op: "readddir", Path: ".", Err: err}
}
return dent, nil
}
if !windowsPathRe.MatchString(fs_name) {
return nil, &fs.PathError{Op: "readdir", Path: fs_name, Err: fs.ErrNotExist}
}
os_name, err := fsToOsPath(fs_name)
if err != nil {
return nil, &fs.PathError{Op: "readdir", Path: fs_name, Err: err}
}
return os.ReadDir(os_name)
}
type rootFile struct {
drives []fs.DirEntry
offset int
}
func newRootFile() (*rootFile, error) {
dent, err := get_drives()
if err != nil {
return nil, &fs.PathError{Op: "open", Path: ".", Err: err}
}
return &rootFile{drives: dent, offset: 0}, nil
}
func (rf *rootFile) Close() error {
return nil
}
func (rf *rootFile) Stat() (fs.FileInfo, error) {
return dummyDirInfo("."), nil
}
func (rf *rootFile) Read([]byte) (int, error) {
return 0, &fs.PathError{Op: "read", Path: ".", Err: ErrIsDirectory}
}
func (rf *rootFile) ReadDir(count int) ([]fs.DirEntry, error) {
n := len(rf.drives) - rf.offset
if n == 0 {
if count <= 0 {
return nil, nil
}
return nil, io.EOF
}
if count > 0 && n > count {
n = count
}
list := make([]fs.DirEntry, n)
for i := range list {
list[i] = rf.drives[rf.offset+i]
}
rf.offset += n
return list, nil
}
type dummyDirInfo string
func (dfi dummyDirInfo) Name() string {
return string(dfi)
}
func (dfi dummyDirInfo) Size() int64 {
return 0
}
func (dfi dummyDirInfo) Mode() fs.FileMode {
return fs.ModeDir
}
func (dfi dummyDirInfo) ModTime() time.Time {
return time.Time{}
}
func (dfi dummyDirInfo) IsDir() bool {
return true
}
func (dfi dummyDirInfo) Sys() any {
return nil
}
type driveDirEntry string
func (dde driveDirEntry) Name() string {
return string(dde)
}
func (dde driveDirEntry) IsDir() bool {
return true
}
func (dde driveDirEntry) Type() fs.FileMode {
return fs.ModeDir
}
func (dde driveDirEntry) Info() (fs.FileInfo, error) {
return dummyDirInfo(dde), nil
}
func found_drive(drv byte) bool {
drv_bit, err := windows.GetLogicalDrives()
if err != nil {
return false
}
if drv < byte('A') || drv > byte('Z') {
return false
}
return (drv_bit>>(drv-byte('A')))&0x1 != 0
}
func get_drives() ([]fs.DirEntry, error) {
drv_bit, err := windows.GetLogicalDrives()
if err != nil {
return nil, err
}
drives := make([]fs.DirEntry, 0, bits.OnesCount32(drv_bit))
for d := byte('A'); d <= byte('Z'); d++ {
if drv_bit&0x1 != 0 {
drives = append(drives, driveDirEntry(string([]byte{d, ':'})))
}
drv_bit >>= 1
}
return drives, err
}