-
Notifications
You must be signed in to change notification settings - Fork 97
/
client_fs.go
120 lines (95 loc) · 1.97 KB
/
client_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
// +build go1.16
package smb2
import (
"io/fs"
)
type wfs struct {
root string
share *Share
}
func (s *Share) DirFS(dirname string) fs.FS {
return &wfs{
root: normPath(dirname),
share: s,
}
}
func (fs *wfs) path(name string) string {
name = normPath(name)
if fs.root != "" {
if name != "" {
name = fs.root + "\\" + name
} else {
name = fs.root
}
}
return name
}
func (fs *wfs) pattern(pattern string) string {
pattern = normPattern(pattern)
if fs.root != "" {
pattern = fs.root + "\\" + pattern
}
return pattern
}
func (fs *wfs) Open(name string) (fs.File, error) {
file, err := fs.share.Open(fs.path(name))
if err != nil {
return nil, err
}
return &wfile{file}, nil
}
func (fs *wfs) Stat(name string) (fs.FileInfo, error) {
return fs.share.Stat(fs.path(name))
}
func (fs *wfs) ReadFile(name string) ([]byte, error) {
return fs.share.ReadFile(fs.path(name))
}
func (fs *wfs) Glob(pattern string) (matches []string, err error) {
matches, err = fs.share.Glob(fs.pattern(pattern))
if err != nil {
return nil, err
}
if fs.root == "" {
return matches, nil
}
for i, match := range matches {
matches[i] = match[len(fs.root)+1:]
}
return matches, nil
}
// dirInfo is a DirEntry based on a FileInfo.
type dirInfo struct {
fileInfo fs.FileInfo
}
func (di dirInfo) IsDir() bool {
return di.fileInfo.IsDir()
}
func (di dirInfo) Type() fs.FileMode {
return di.fileInfo.Mode().Type()
}
func (di dirInfo) Info() (fs.FileInfo, error) {
return di.fileInfo, nil
}
func (di dirInfo) Name() string {
return di.fileInfo.Name()
}
func fileInfoToDirEntry(info fs.FileInfo) fs.DirEntry {
if info == nil {
return nil
}
return dirInfo{fileInfo: info}
}
type wfile struct {
*File
}
func (f *wfile) ReadDir(n int) (dirents []fs.DirEntry, err error) {
infos, err := f.Readdir(n)
if err != nil {
return nil, err
}
dirents = make([]fs.DirEntry, len(infos))
for i, info := range infos {
dirents[i] = fileInfoToDirEntry(info)
}
return dirents, nil
}