-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
vfs.go
80 lines (64 loc) · 1.31 KB
/
vfs.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
package cxgo
import (
"io"
"os"
"strings"
"time"
"modernc.org/cc/v3"
"github.com/gotranspile/cxgo/libs"
)
var timeRun = time.Now()
func newIncludeFS(c *libs.Env) cc.Filesystem {
return includeFS{c: c}
}
type includeFS struct {
c *libs.Env
}
func (fs includeFS) content(path string, sys bool) (string, error) {
if !sys {
if _, ok := fs.c.Map[strings.TrimPrefix(path, libs.IncludePath+"/")]; !ok {
return "", os.ErrNotExist
}
}
l, ok := fs.c.NewLibrary(path)
if !ok {
return "", os.ErrNotExist
}
return l.Header, nil
}
func (fs includeFS) Stat(path string, sys bool) (os.FileInfo, error) {
data, err := fs.content(path, sys)
if err != nil {
return nil, err
}
return includeFI{name: path, data: data}, nil
}
func (fs includeFS) Open(path string, sys bool) (io.ReadCloser, error) {
data, err := fs.content(path, sys)
if err != nil {
return nil, err
}
return io.NopCloser(strings.NewReader(data)), nil
}
type includeFI struct {
name string
data string
}
func (fi includeFI) Name() string {
return fi.name
}
func (fi includeFI) Size() int64 {
return int64(len(fi.data))
}
func (fi includeFI) Mode() os.FileMode {
return 0
}
func (fi includeFI) ModTime() time.Time {
return timeRun
}
func (fi includeFI) IsDir() bool {
return false
}
func (fi includeFI) Sys() interface{} {
return fi
}