-
Notifications
You must be signed in to change notification settings - Fork 1
/
file.go
97 lines (73 loc) · 1.66 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
package bltfs
import (
"os"
"time"
"hpt.space/bltfs/proto"
)
type entryStat struct {
e *proto.Entry
}
// Size is part of the os.FileInfo interface
func (es *entryStat) Size() int64 {
if x, ok := es.e.Elem.(*proto.Entry_File); ok {
return int64(x.File.Length)
}
panic("Size() not implemented for directories")
}
// IsDir is part of the os.FileInfo interface
func (es *entryStat) IsDir() bool {
_, ok := es.e.Elem.(*proto.Entry_Dir)
return ok
}
func (es *entryStat) Mode() os.FileMode { return os.ModePerm }
func (es *entryStat) ModTime() time.Time { return time.Unix(0, es.e.ModifyTime) }
func (es *entryStat) Sys() interface{} { return es.e }
func (es *entryStat) Name() string { return es.e.Name }
type fileOptions struct {
noBatch bool
}
type FileOption func(*fileOptions)
func WithNoBatch() FileOption {
return func(o *fileOptions) {
o.noBatch = true
}
}
type File struct {
id uint64
rw *synchronizedWriter
idx *index
path string
fopts fileOptions
}
func (f *File) String() string {
return f.path
}
func (s *Store) Create(path string, opts ...FileOption) (*File, error) {
return s.Open(path, opts...)
}
func (s *Store) Open(path string, opts ...FileOption) (*File, error) {
f := &File{
rw: s.rw,
idx: s.idx,
path: path,
}
for _, opt := range opts {
opt(&f.fopts)
}
return f, nil
}
func (f *File) Name() string {
return f.path
}
func (f *File) Close() error {
return nil
}
func (f *File) Write(p []byte) (n int, err error) {
return f.rw.Write(p)
}
func (f *File) Read(p []byte) (n int, err error) {
return f.rw.Read(p)
}
func (f *File) Seek(offset int64, whence int) (ret int64, err error) {
panic("not implemented")
}