/
file.go
115 lines (89 loc) · 1.83 KB
/
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
package sivafs
import (
"io"
"os"
"path/filepath"
"gopkg.in/src-d/go-billy.v4"
"gopkg.in/src-d/go-siva.v1"
)
type file struct {
name string
closeNotify func() error
isClosed bool
w siva.Writer
r *io.SectionReader
}
func newFile(filename string, w siva.Writer, closeNotify func() error) billy.File {
return &file{
name: filepath.FromSlash(filename),
closeNotify: closeNotify,
w: w,
}
}
func openFile(filename string, r *io.SectionReader) billy.File {
return &file{
name: filepath.FromSlash(filename),
r: r,
}
}
func (f *file) Name() string {
return f.name
}
func (f *file) Read(p []byte) (int, error) {
if f.isClosed {
return 0, os.ErrClosed
}
if f.r == nil {
return 0, ErrWriteOnlyFile
}
return f.r.Read(p)
}
func (f *file) ReadAt(b []byte, off int64) (int, error) {
if f.isClosed {
return 0, os.ErrClosed
}
if f.r == nil {
return 0, ErrWriteOnlyFile
}
return f.r.ReadAt(b, off)
}
func (f *file) Seek(offset int64, whence int) (int64, error) {
if f.isClosed {
return 0, os.ErrClosed
}
if f.r == nil {
return 0, ErrNonSeekableFile
}
return f.r.Seek(offset, whence)
}
func (f *file) Write(p []byte) (int, error) {
if f.isClosed {
return 0, os.ErrClosed
}
if f.w == nil {
return 0, ErrReadOnlyFile
}
return f.w.Write(p)
}
func (f *file) Close() error {
if f.isClosed {
return os.ErrClosed
}
defer func() { f.isClosed = true }()
if f.closeNotify == nil {
return nil
}
return f.closeNotify()
}
// Lock is a no-op. It's not implemented in the underlying siva library.
func (f *file) Lock() error {
return nil
}
// Unlock is a no-op. It's not implemented in the underlying siva library.
func (f *file) Unlock() error {
return nil
}
// Truncate is not supported by siva files.
func (f *file) Truncate(size int64) error {
return billy.ErrNotSupported
}