forked from keybase/kbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpfs.go
164 lines (147 loc) · 3.2 KB
/
httpfs.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
package libfs
import (
"net/http"
"os"
"github.com/keybase/kbfs/libkbfs"
"github.com/pkg/errors"
)
type dir struct {
fs *FS
dirname string
node libkbfs.Node
}
// Readdir reads children from d.
func (d *dir) Readdir(count int) (fis []os.FileInfo, err error) {
d.fs.log.CDebugf(d.fs.ctx, "ReadDir %s", count)
defer func() {
d.fs.deferLog.CDebugf(d.fs.ctx, "ReadDir done: %+v", err)
err = translateErr(err)
}()
return d.fs.readDir(d.node)
}
// fileOrDir is a wrapper around billy FS types that satisfies http.File, which
// is either a file or a dir.
type fileOrDir struct {
file *File
dir *dir
ei libkbfs.EntryInfo
}
var _ http.File = fileOrDir{}
// fileOrDir implements the http.File interface.
func (fod fileOrDir) Read(p []byte) (n int, err error) {
defer func() {
if err != nil {
err = translateErr(err)
}
}()
if fod.file == nil {
return 0, libkbfs.NotFileError{}
}
return fod.file.Read(p)
}
// Close implements the http.File interface.
func (fod fileOrDir) Close() (err error) {
defer func() {
if err != nil {
err = translateErr(err)
}
}()
if fod.file != nil {
err = fod.file.Close()
}
if fod.dir != nil {
fod.dir.node = nil
}
fod.file = nil
fod.dir = nil
return err
}
// Seek implements the http.File interface.
func (fod fileOrDir) Seek(offset int64, whence int) (n int64, err error) {
defer func() {
if err != nil {
err = translateErr(err)
}
}()
if fod.file == nil {
return 0, libkbfs.NotFileError{}
}
return fod.file.Seek(offset, whence)
}
// Readdir implements the http.File interface.
func (fod fileOrDir) Readdir(count int) (fis []os.FileInfo, err error) {
defer func() {
if err != nil {
err = translateErr(err)
}
}()
if fod.dir == nil {
return nil, libkbfs.NotDirError{}
}
return fod.dir.Readdir(count)
}
// Stat implements the http.File interface.
func (fod fileOrDir) Stat() (fi os.FileInfo, err error) {
defer func() {
if err != nil {
err = translateErr(err)
}
}()
if fod.file != nil {
return &FileInfo{
fs: fod.file.fs,
ei: fod.ei,
node: fod.file.node,
name: fod.file.node.GetBasename(),
}, nil
} else if fod.dir != nil {
return &FileInfo{
fs: fod.dir.fs,
ei: fod.ei,
node: fod.dir.node,
name: fod.dir.node.GetBasename(),
}, nil
}
return nil, errors.New("invalid fod")
}
// httpFileSystem is a simple wrapper around *FS that satisfies http.FileSystem
// interface.
type httpFileSystem struct {
fs *FS
}
var _ http.FileSystem = httpFileSystem{}
// Open implements the http.FileSystem interface.
func (hfs httpFileSystem) Open(filename string) (entry http.File, err error) {
hfs.fs.log.CDebugf(
hfs.fs.ctx, "hfs.Open %s", filename)
defer func() {
hfs.fs.deferLog.CDebugf(hfs.fs.ctx, "hfs.Open done: %+v", err)
if err != nil {
err = translateErr(err)
}
}()
n, ei, err := hfs.fs.lookupOrCreateEntry(filename, os.O_RDONLY, 0600)
if err != nil {
return fileOrDir{}, err
}
if ei.Type.IsFile() {
return fileOrDir{
file: &File{
fs: hfs.fs,
filename: n.GetBasename(),
node: n,
readOnly: true,
offset: 0,
},
ei: ei,
}, nil
}
return fileOrDir{
dir: &dir{
fs: hfs.fs,
dirname: n.GetBasename(),
node: n,
},
ei: ei,
}, nil
}