-
Notifications
You must be signed in to change notification settings - Fork 7
/
extrafs.go
54 lines (44 loc) · 977 Bytes
/
extrafs.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
package extrafs
import (
"io/fs"
"os"
"path/filepath"
)
/*
Go does not currently support symlinks in io/fs.
We work around this by wrapping the fs.FS returned by os.DirFS with our own type which bolts on the ReadLinkFS
*/
type OSFS interface {
fs.FS
fs.StatFS
}
type ReadLinkFS interface {
ResolveSymlink(name, dir string) (string, error)
}
type FS interface {
OSFS
ReadLinkFS
}
type filesystem struct {
root string
underlying OSFS
}
func OSDir(path string) FS {
return &filesystem{
root: path,
underlying: os.DirFS(path).(OSFS),
}
}
func (f *filesystem) Open(name string) (fs.File, error) {
return f.underlying.Open(name)
}
func (f *filesystem) Stat(name string) (fs.FileInfo, error) {
return f.underlying.Stat(name)
}
func (f *filesystem) ResolveSymlink(name, dir string) (string, error) {
link, err := os.Readlink(filepath.Join(f.root, dir, name))
if err == nil {
return filepath.Join(dir, link), nil
}
return name, nil
}