-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathfs.go
207 lines (175 loc) · 4.82 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
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
// Package fs is a simple Datastore implementation that stores keys
// as directories and files, mirroring the key. That is, the key
// "/foo/bar" is stored as file "PATH/foo/bar/.dsobject".
//
// This means key some segments will not work. For example, the
// following keys will result in unwanted behavior:
//
// - "/foo/./bar"
// - "/foo/../bar"
// - "/foo\x00bar"
//
// Keys that only differ in case may be confused with each other on
// case insensitive file systems, for example in OS X.
//
// This package is intended for exploratory use, where the user would
// examine the file system manually, and should only be used with
// human-friendly, trusted keys. You have been warned.
package examples
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"strings"
ds "github.com/ipfs/go-datastore"
query "github.com/ipfs/go-datastore/query"
)
var ObjectKeySuffix = ".dsobject"
// Datastore uses a uses a file per key to store values.
type Datastore struct {
path string
}
var _ ds.Datastore = (*Datastore)(nil)
var _ ds.Batching = (*Datastore)(nil)
var _ ds.PersistentDatastore = (*Datastore)(nil)
// NewDatastore returns a new fs Datastore at given `path`
func NewDatastore(path string) (ds.Datastore, error) {
if !isDir(path) {
return nil, fmt.Errorf("failed to find directory at: %v (file? perms?)", path)
}
return &Datastore{path: path}, nil
}
// KeyFilename returns the filename associated with `key`
func (d *Datastore) KeyFilename(key ds.Key) string {
return filepath.Join(d.path, key.String(), ObjectKeySuffix)
}
// Put stores the given value.
func (d *Datastore) Put(ctx context.Context, key ds.Key, value []byte) (err error) {
fn := d.KeyFilename(key)
// mkdirall above.
err = os.MkdirAll(filepath.Dir(fn), 0755)
if err != nil {
return err
}
return os.WriteFile(fn, value, 0666)
}
// Sync would ensure that any previous Puts under the prefix are written to disk.
// However, they already are.
func (d *Datastore) Sync(ctx context.Context, prefix ds.Key) error {
return nil
}
// Get returns the value for given key
func (d *Datastore) Get(ctx context.Context, key ds.Key) (value []byte, err error) {
fn := d.KeyFilename(key)
if !isFile(fn) {
return nil, ds.ErrNotFound
}
return os.ReadFile(fn)
}
// Has returns whether the datastore has a value for a given key
func (d *Datastore) Has(ctx context.Context, key ds.Key) (exists bool, err error) {
return ds.GetBackedHas(ctx, d, key)
}
func (d *Datastore) GetSize(ctx context.Context, key ds.Key) (size int, err error) {
return ds.GetBackedSize(ctx, d, key)
}
// Delete removes the value for given key
func (d *Datastore) Delete(ctx context.Context, key ds.Key) (err error) {
fn := d.KeyFilename(key)
if !isFile(fn) {
return nil
}
err = os.Remove(fn)
if os.IsNotExist(err) {
err = nil // idempotent
}
return err
}
// Query implements Datastore.Query
func (d *Datastore) Query(ctx context.Context, q query.Query) (query.Results, error) {
results := make(chan query.Result)
walkFn := func(path string, info os.FileInfo, _ error) error {
// remove ds path prefix
relPath, err := filepath.Rel(d.path, path)
if err == nil {
path = filepath.ToSlash(relPath)
}
if !info.IsDir() {
path = strings.TrimSuffix(path, ObjectKeySuffix)
var result query.Result
key := ds.NewKey(path)
result.Entry.Key = key.String()
if !q.KeysOnly {
result.Entry.Value, result.Error = d.Get(ctx, key)
}
results <- result
}
return nil
}
go func() {
filepath.Walk(d.path, walkFn)
close(results)
}()
r := query.ResultsWithContext(q, func(ctx context.Context, out chan<- query.Result) {
loop:
for {
select {
case <-ctx.Done(): // client told us to close early
break loop
case e, more := <-results:
if !more {
return
}
select {
case out <- e:
case <-ctx.Done(): // client told us to close early
break loop
}
}
}
// Drain results on cancel so writer is unblocked.
for range results {
}
})
r = query.NaiveQueryApply(q, r)
return r, nil
}
// isDir returns whether given path is a directory
func isDir(path string) bool {
finfo, err := os.Stat(path)
if err != nil {
return false
}
return finfo.IsDir()
}
// isFile returns whether given path is a file
func isFile(path string) bool {
finfo, err := os.Stat(path)
if err != nil {
return false
}
return !finfo.IsDir()
}
func (d *Datastore) Close() error {
return nil
}
func (d *Datastore) Batch(ctx context.Context) (ds.Batch, error) {
return ds.NewBasicBatch(d), nil
}
// DiskUsage returns the disk size used by the datastore in bytes.
func (d *Datastore) DiskUsage(ctx context.Context) (uint64, error) {
var du uint64
err := filepath.Walk(d.path, func(p string, f os.FileInfo, err error) error {
if err != nil {
log.Println(err)
return err
}
if f != nil && f.Mode().IsRegular() {
du += uint64(f.Size())
}
return nil
})
return du, err
}