-
Notifications
You must be signed in to change notification settings - Fork 0
/
ghfs.go
213 lines (191 loc) · 4.04 KB
/
ghfs.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
package ghfs
import (
"github.com/pkg/errors"
"io"
"io/ioutil"
"net/http"
"os"
"strings"
"syscall"
"time"
g "github.com/gogits/git"
)
type ghfsDir struct {
tree *g.Tree
fi os.FileInfo
scanner *g.TreeScanner
}
// Implement http.File on a git tree
func NewDir(tree *g.Tree, fi os.FileInfo) (http.File, error) {
scanner, err := tree.Scanner()
if err != nil {
return nil, errors.Wrap(err, "Cannot open scanner.")
}
return &ghfsDir{tree: tree, fi: fi, scanner: scanner}, nil
}
func (d *ghfsDir) Read([]byte) (int, error) {
return 0, io.EOF
}
func (d *ghfsDir) Close() error {
return nil
}
func (d *ghfsDir) Readdir(count int) ([]os.FileInfo, error) {
ret := []os.FileInfo{}
for c := 0; count <= 0 || count > c; c++ {
if !d.scanner.Scan() {
break
}
ret = append(ret, d.scanner.TreeEntry())
}
return ret, d.scanner.Err()
}
func (d *ghfsDir) Seek(int64, int) (int64, error) {
return 0, syscall.EISDIR
}
func (d *ghfsDir) Stat() (os.FileInfo, error) {
return d.fi, nil
}
type rootFileInfo struct{}
func (r rootFileInfo) Name() string {
return ""
}
func (r rootFileInfo) Size() int64 {
return 0
}
func (r rootFileInfo) Mode() os.FileMode {
return os.ModeDir | 0755
}
func (r rootFileInfo) ModTime() time.Time {
return time.Now()
}
func (r rootFileInfo) IsDir() bool {
return true
}
func (r rootFileInfo) Sys() interface{} {
return nil
}
type modTimeFileInfo struct {
os.FileInfo
modTime time.Time
}
func (m modTimeFileInfo) ModTime() time.Time {
return m.modTime
}
type ghfsFile struct {
entry *g.TreeEntry
fi os.FileInfo
rc io.ReadCloser
off int64
atEnd bool
}
// Implement http.File on a git blob
func NewFile(entry *g.TreeEntry, fi os.FileInfo) (http.File, error) {
return &ghfsFile{entry: entry, fi: fi}, nil
}
func (f *ghfsFile) Read(buf []byte) (int, error) {
var err error
if f.rc == nil {
f.rc, err = f.entry.Blob().Data()
if err != nil {
return 0, errors.Wrap(err, "Cannot get Data()")
}
}
if f.atEnd {
return 0, io.EOF
}
n, err := f.rc.Read(buf)
f.off += int64(n)
return n, err
}
func (f *ghfsFile) Close() error {
var ret error
if f.rc != nil {
ret = f.rc.Close()
f.rc = nil
}
f.off = 0
return ret
}
func (f *ghfsFile) Readdir(count int) ([]os.FileInfo, error) {
return nil, os.ErrInvalid
}
func (f *ghfsFile) Seek(offset int64, whence int) (int64, error) {
var noff int64
if whence == io.SeekCurrent && f.atEnd {
whence = io.SeekEnd
}
switch whence {
case io.SeekStart:
noff = offset
case io.SeekCurrent:
noff = f.off + offset
case io.SeekEnd:
if offset == 0 {
f.atEnd = true
return f.entry.Size(), nil
}
noff = f.entry.Size() + offset
default:
return 0, errors.New("Invalid argument for whence")
}
f.atEnd = false
switch {
case noff < 0:
return 0, errors.New("Invalid offset")
case noff < f.off:
f.Close()
return io.CopyN(ioutil.Discard, f, noff)
case noff >= f.off:
_, err := io.CopyN(ioutil.Discard, f, noff-f.off)
return f.off, err
default:
panic("Unreachable")
}
}
func (f *ghfsFile) Stat() (os.FileInfo, error) {
return f.fi, nil
}
// Implement a http.Filesystem for a git Tree
type ghfs struct {
commit *g.Commit
tree *g.Tree
}
// Serve git tree from commit. Optionally from subtree
func FromCommit(commit *g.Commit, tree ...*g.Tree) http.FileSystem {
var t *g.Tree
if len(tree) == 0 {
t = &commit.Tree
} else {
t = tree[0]
}
return ghfs{commit, t}
}
func (fs ghfs) Open(name string) (http.File, error) {
var entry *g.TreeEntry
name = strings.TrimPrefix(name, "/")
if name == "" {
return NewDir(fs.tree, rootFileInfo{})
} else {
var err error
entry, err = fs.tree.GetTreeEntryByPath(name)
switch {
case err == g.ErrNotExist:
return nil, os.ErrNotExist
case err != nil:
return nil, errors.Wrap(err, "Cannot get entry.")
}
}
fi := modTimeFileInfo{entry, fs.commit.Author.When}
switch entry.Type {
case g.ObjectTree:
stree, err := fs.tree.SubTree(name)
if err != nil {
return nil, err
}
return NewDir(stree, fi)
case g.ObjectBlob:
return NewFile(entry, fi)
default:
return nil, errors.New("Invalid type")
}
}