forked from bazil/zipfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
226 lines (190 loc) · 4.43 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
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
package main
import (
"archive/zip"
"flag"
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
"bazil.org/fuse"
"bazil.org/fuse/fs"
"golang.org/x/net/context"
)
// We assume the zip file contains entries for directories too.
var progName = filepath.Base(os.Args[0])
func usage() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", progName)
fmt.Fprintf(os.Stderr, " %s ZIP MOUNTPOINT\n", progName)
flag.PrintDefaults()
}
func main() {
log.SetFlags(0)
log.SetPrefix(progName + ": ")
flag.Usage = usage
flag.Parse()
if flag.NArg() != 2 {
usage()
os.Exit(2)
}
path := flag.Arg(0)
mountpoint := flag.Arg(1)
if err := mount(path, mountpoint); err != nil {
log.Fatal(err)
}
}
func mount(path, mountpoint string) error {
archive, err := zip.OpenReader(path)
if err != nil {
return err
}
defer archive.Close()
c, err := fuse.Mount(mountpoint)
if err != nil {
return err
}
defer c.Close()
filesys := &FS{
archive: &archive.Reader,
}
if err := fs.Serve(c, filesys); err != nil {
return err
}
// check if the mount process has an error to report
<-c.Ready
if err := c.MountError; err != nil {
return err
}
return nil
}
type FS struct {
archive *zip.Reader
}
var _ fs.FS = (*FS)(nil)
func (f *FS) Root() (fs.Node, error) {
n := &Dir{
archive: f.archive,
}
return n, nil
}
type Dir struct {
archive *zip.Reader
// nil for the root directory, which has no entry in the zip
file *zip.File
}
var _ fs.Node = (*Dir)(nil)
func zipAttr(f *zip.File, a *fuse.Attr) {
a.Size = f.UncompressedSize64
a.Mode = f.Mode()
a.Mtime = f.ModTime()
a.Ctime = f.ModTime()
a.Crtime = f.ModTime()
}
func (d *Dir) Attr(ctx context.Context, a *fuse.Attr) error {
if d.file == nil {
// root directory
a.Mode = os.ModeDir | 0755
return nil
}
zipAttr(d.file, a)
return nil
}
var _ = fs.NodeRequestLookuper(&Dir{})
func (d *Dir) Lookup(ctx context.Context, req *fuse.LookupRequest, resp *fuse.LookupResponse) (fs.Node, error) {
path := req.Name
if d.file != nil {
path = d.file.Name + path
}
for _, f := range d.archive.File {
switch {
case f.Name == path:
child := &File{
file: f,
}
return child, nil
case f.Name[:len(f.Name)-1] == path && f.Name[len(f.Name)-1] == '/':
child := &Dir{
archive: d.archive,
file: f,
}
return child, nil
}
}
return nil, fuse.ENOENT
}
var _ = fs.HandleReadDirAller(&Dir{})
func (d *Dir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
prefix := ""
if d.file != nil {
prefix = d.file.Name
}
var res []fuse.Dirent
for _, f := range d.archive.File {
if !strings.HasPrefix(f.Name, prefix) {
continue
}
name := f.Name[len(prefix):]
if name == "" {
// the dir itself, not a child
continue
}
if strings.ContainsRune(name[:len(name)-1], '/') {
// contains slash in the middle -> is in a deeper subdir
continue
}
var de fuse.Dirent
if name[len(name)-1] == '/' {
// directory
name = name[:len(name)-1]
de.Type = fuse.DT_Dir
}
de.Name = name
res = append(res, de)
}
return res, nil
}
type File struct {
file *zip.File
}
var _ fs.Node = (*File)(nil)
func (f *File) Attr(ctx context.Context, a *fuse.Attr) error {
zipAttr(f.file, a)
return nil
}
var _ = fs.NodeOpener(&File{})
func (f *File) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) {
r, err := f.file.Open()
if err != nil {
return nil, err
}
// individual entries inside a zip file are not seekable
resp.Flags |= fuse.OpenNonSeekable
return &FileHandle{r: r}, nil
}
type FileHandle struct {
r io.ReadCloser
}
var _ fs.Handle = (*FileHandle)(nil)
var _ fs.HandleReleaser = (*FileHandle)(nil)
func (fh *FileHandle) Release(ctx context.Context, req *fuse.ReleaseRequest) error {
return fh.r.Close()
}
var _ = fs.HandleReader(&FileHandle{})
func (fh *FileHandle) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
// We don't actually enforce Offset to match where previous read
// ended. Maybe we should, but that would mean'd we need to track
// it. The kernel *should* do it for us, based on the
// fuse.OpenNonSeekable flag.
//
// One exception to the above is if we fail to fully populate a
// page cache page; a read into page cache is always page aligned.
// Make sure we never serve a partial read, to avoid that.
buf := make([]byte, req.Size)
n, err := io.ReadFull(fh.r, buf)
if err == io.ErrUnexpectedEOF || err == io.EOF {
err = nil
}
resp.Data = buf[:n]
return err
}