-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
114 lines (95 loc) · 1.72 KB
/
main.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
package gorofs
import (
"archive/zip"
"fmt"
"io"
"os"
"time"
)
type raw struct {
buf []byte
}
func (r *raw) ReadAt(b []byte, off int64) (n int, err error) {
if off > int64(len(r.buf)) {
return 0, fmt.Errorf("Offset %v beyond EOF at %v", off, len(r.buf))
}
n = copy(b, r.buf[int(off):])
if n < len(b) {
err = io.EOF
}
return
}
type file struct {
io.ReadCloser
zf *zip.File
}
func (f *file) Name() string {
return f.zf.Name
}
func (f *file) ModTime() time.Time {
return f.zf.FileHeader.ModTime()
}
func (f *file) Mode() os.FileMode {
m := f.zf.FileHeader.Mode()
return m
}
func (f *file) IsDir() bool {
return false
}
func (f *file) Size() int64 {
return int64(f.zf.UncompressedSize)
}
func (f *file) Stat() (fi os.FileInfo, err error) {
return f, nil
}
func (f *file) Sys() (interface{}) {
return f
}
var defaultFs *Rofs
func Register(buf []byte) (err error) {
fs, err := NewROFS(buf)
if err != nil {
return
}
defaultFs = fs
return
}
func Open(name string) (ReadStatCloser, error) {
return defaultFs.Open(name)
}
type Rofs struct {
reader *zip.Reader
}
func NewROFS(buf []byte) (fs *Rofs, err error) {
data := &raw{buf: buf}
r, err := zip.NewReader(data, int64(len(data.buf)))
if err != nil {
return
}
fs = &Rofs{reader: r}
return
}
func (fs *Rofs) Open(name string) (f ReadStatCloser, err error) {
f, err = os.Open(name)
if err == nil {
return
}
if fs == nil {
return nil, fmt.Errorf("No fs registered")
}
for _, fz := range fs.reader.File {
if fz.Name == name {
fi, err := fz.Open()
if err != nil {
continue
}
f = &file{fi, fz}
return f, nil
}
}
return nil, fmt.Errorf("File not found")
}
type ReadStatCloser interface {
io.ReadCloser
Stat() (os.FileInfo, error)
}