forked from bazil/bazil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.go
151 lines (133 loc) · 3.34 KB
/
fs.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
package fs
import (
"encoding/binary"
"errors"
"crypto/rand"
"bazil.org/bazil/cas/chunks"
"bazil.org/bazil/fs/inodes"
"bazil.org/bazil/fs/wire"
"bazil.org/bazil/tokens"
"bazil.org/fuse"
"bazil.org/fuse/fs"
"github.com/boltdb/bolt"
"github.com/gogo/protobuf/proto"
"golang.org/x/net/context"
)
type Volume struct {
db *bolt.DB
volID VolumeID
chunkStore chunks.Store
}
var _ = fs.FS(&Volume{})
var _ = fs.FSIniter(&Volume{})
var _ = fs.FSInodeGenerator(&Volume{})
var bucketVolume = []byte(tokens.BucketVolume)
var bucketVolName = []byte(tokens.BucketVolName)
var bucketDir = []byte("dir")
var bucketInode = []byte("inode")
var bucketSnap = []byte("snap")
func (v *Volume) bucket(tx *bolt.Tx) *bolt.Bucket {
b := tx.Bucket(bucketVolume)
b = b.Bucket(v.volID.Bytes())
return b
}
// Open returns a FUSE filesystem instance serving content from the
// given volume. The result can be passed to bazil.org/fuse/fs#Serve
// to start serving file access requests from the kernel.
//
// Caller guarantees volume ID exists at least as long as requests are
// served for this file system.
func Open(db *bolt.DB, chunkStore chunks.Store, volumeID *VolumeID) (*Volume, error) {
fs := &Volume{}
fs.db = db
fs.volID = *volumeID
fs.chunkStore = chunkStore
return fs, nil
}
// Create a new volume.
func Create(db *bolt.DB, volumeName string) error {
// uniqueness of id is guaranteed by tx.CreateBucket refusing to
// create the per-volume buckets on collision. this leads to an
// ugly error, but it's boil-the-oceans rare
id, err := RandomVolumeID()
if err != nil {
return err
}
err = db.Update(func(tx *bolt.Tx) error {
{
bucket := tx.Bucket(bucketVolName)
key := []byte(volumeName)
exists := bucket.Get(key)
if exists != nil {
return errors.New("volume name exists already")
}
var secret [32]byte
if _, err := rand.Read(secret[:]); err != nil {
return err
}
volConf := wire.VolumeConfig{
VolumeID: id.Bytes(),
Storage: wire.KV{
Local: &wire.KV_Local{
Secret: secret[:],
},
},
}
buf, err := proto.Marshal(&volConf)
if err != nil {
return err
}
err = bucket.Put(key, buf)
if err != nil {
return err
}
}
bucket := tx.Bucket(bucketVolume)
if bucket, err = bucket.CreateBucket(id.Bytes()); err != nil {
return err
}
if _, err := bucket.CreateBucket(bucketDir); err != nil {
return err
}
if _, err := bucket.CreateBucket(bucketInode); err != nil {
return err
}
if _, err := bucket.CreateBucket(bucketSnap); err != nil {
return err
}
return nil
})
if err != nil {
return err
}
return nil
}
func (f *Volume) Init(ctx context.Context, req *fuse.InitRequest, resp *fuse.InitResponse) error {
resp.MaxReadahead = 32 * 1024 * 1024
resp.Flags |= fuse.InitAsyncRead
return nil
}
func (v *Volume) Root() (fs.Node, error) {
d := &dir{
inode: tokens.InodeRoot,
parent: nil,
fs: v,
active: make(map[string]node),
}
return d, nil
}
func (*Volume) GenerateInode(parent uint64, name string) uint64 {
return inodes.Dynamic(parent, name)
}
var _ = fs.FSInodeGenerator(&Volume{})
func pathToKey(parentInode uint64, name string) []byte {
buf := make([]byte, 8+len(name))
binary.BigEndian.PutUint64(buf, parentInode)
copy(buf[8:], name)
return buf
}
type node interface {
fs.Node
marshal() (*wire.Dirent, error)
setName(name string)
}