-
Notifications
You must be signed in to change notification settings - Fork 0
/
ro_file.go
154 lines (134 loc) · 2.82 KB
/
ro_file.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
package gitfs
import (
"bytes"
"errors"
"io"
"os"
"syscall"
"gopkg.in/src-d/go-git.v4/plumbing/filemode"
"gopkg.in/src-d/go-git.v4/plumbing/object"
)
type gitfile struct {
fs *Gitfs
entry *object.TreeEntry
rdr io.ReadCloser
skr io.ReadSeeker
readDirCount int
}
func (g *gitfile) Close() error {
if g.rdr != nil {
defer func() { g.rdr = nil }()
return g.rdr.Close()
}
return nil
}
func (g *gitfile) Name() string {
return g.entry.Name
}
func (g *gitfile) initReader() error {
if g.rdr != nil {
return nil
}
bo, err := g.fs.repo.BlobObject(g.entry.Hash)
if err != nil {
return err
}
rdr, err := bo.Reader()
if err != nil {
return err
}
g.rdr = rdr
return nil
}
func (g *gitfile) initSeeker() error {
var buf bytes.Buffer
if err := g.initReader(); err != nil {
return err
}
if g.skr != nil {
if _, err := io.Copy(&buf, g); err != nil {
return err
}
}
g.skr = bytes.NewReader(buf.Bytes())
return nil
}
func (g *gitfile) Read(u []uint8) (int, error) {
if g.skr != nil {
return g.skr.Read(u)
}
if err := g.initReader(); err != nil {
return 0, err
}
return g.rdr.Read(u)
}
func (g *gitfile) ReadAt(u []uint8, i int64) (int, error) {
if err := g.initSeeker(); err != nil {
return 0, err
}
_, err := g.skr.Seek(i, io.SeekStart)
if err != nil {
return 0, err
}
return g.skr.Read(u)
}
func (g *gitfile) patherr(op string, err error) error {
return &os.PathError{Op: op, Path: g.entry.Name, Err: err}
}
func (g *gitfile) Readdir(n int) ([]os.FileInfo, error) {
if g.entry.Mode != filemode.Dir {
return nil, g.patherr("readdir", errors.New("not a directory"))
}
tree, err := g.fs.tree.Tree(g.entry.Name)
if err != nil {
return nil, g.patherr("readdir", err)
}
if n > 0 {
if len(tree.Entries)-g.readDirCount <= 0 {
return nil, io.EOF
}
var outlen int
entries := tree.Entries[g.readDirCount:]
if len(entries) < n {
outlen = len(entries)
} else {
outlen = n
}
res := make([]os.FileInfo, outlen)
for i := range res {
res[i] = &fileinfo{&entries[i]}
}
g.readDirCount += outlen
return res, nil
}
return nil, nil
}
func (g *gitfile) Readdirnames(i int) ([]string, error) {
fi, err := g.Readdir(i)
res := make([]string, len(fi))
for idx := range fi {
res[idx] = fi[idx].Name()
}
return res, err
}
func (g *gitfile) Seek(i int64, i1 int) (int64, error) {
return g.skr.Seek(i, i1)
}
func (g *gitfile) Stat() (os.FileInfo, error) {
return &fileinfo{g.entry}, nil
}
func (g *gitfile) Sync() error {
return nil
}
func (g *gitfile) Truncate(i int64) error {
return syscall.EPERM
}
func (g *gitfile) Write(u []uint8) (int, error) {
return 0, syscall.EPERM
}
func (g *gitfile) WriteAt(u []uint8, i int64) (int, error) {
return 0, syscall.EPERM
}
func (g *gitfile) WriteString(s string) (int, error) {
return 0, syscall.EPERM
}