/
store.go
339 lines (312 loc) · 10.6 KB
/
store.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package ocflv1
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"path"
"github.com/srerickson/ocfl-go"
"github.com/srerickson/ocfl-go/extension"
"github.com/srerickson/ocfl-go/validation"
)
var (
ErrLayoutUndefined = errors.New("storage root layout is undefined")
ErrEmptyDirs = errors.New("storage root includes empty directories")
ErrNonObject = errors.New("storage root includes files that aren't part of an object")
ErrObjectVersion = errors.New("storage root includes objects with higher OCFL version than the storage root")
)
// Store represents an existing OCFL v1.x Storage Root.
type Store struct {
Layout extension.Layout
fsys ocfl.FS
rootDir string // storage root directory
config storeConfig
spec ocfl.Spec
layoutErr error // error from ReadLayout()
}
// store layout represent ocfl_layout.json file
type storeConfig map[string]string
type InitStoreConf struct {
Spec ocfl.Spec
Description string
Layout extension.Layout
Extensions []extension.Extension
}
// InitStore initializes a new OCFL v1.x storage root on fsys at root.
func InitStore(ctx context.Context, fsys ocfl.WriteFS, root string, conf *InitStoreConf) error {
if conf == nil {
conf = &InitStoreConf{}
}
if conf.Spec.Empty() {
conf.Spec = defaultSpec
}
if !ocflVerSupported[conf.Spec] {
return fmt.Errorf("%s: %w", conf.Spec, ErrOCFLVersion)
}
// default to 0002-flat-direct layout
if conf.Layout == nil {
conf.Layout = &extension.LayoutFlatDirect{}
}
decl := ocfl.Namaste{
Type: ocfl.NamasteTypeStore,
Version: conf.Spec,
}
entries, err := fsys.ReadDir(ctx, root)
if err != nil {
if !errors.Is(err, fs.ErrNotExist) {
return err
}
}
if len(entries) > 0 {
return fmt.Errorf("directory '%s' is not empty", root)
}
if err := ocfl.WriteDeclaration(ctx, fsys, root, decl); err != nil {
return err
}
if _, err := ocfl.WriteSpecFile(ctx, fsys, root, conf.Spec); err != nil {
return err
}
layt := newStoreConfig(conf.Description, conf.Layout)
if err := writeStoreConfig(ctx, fsys, root, layt); err != nil {
return err
}
for _, e := range append(conf.Extensions, conf.Layout) {
if err := writeExtensionConfig(ctx, fsys, root, e); err != nil {
return err
}
}
return nil
}
// Commit creates or updates the object with the given id using the contents of stage. The returned
// error is always a CommitError
func (s Store) Commit(ctx context.Context, id string, stage *ocfl.Stage, opts ...CommitOption) error {
writeFS, ok := s.fsys.(ocfl.WriteFS)
if !ok {
return &CommitError{Err: fmt.Errorf("storage root backend is read-only")}
}
if s.Layout == nil {
err := fmt.Errorf("commit requires a storage root layout: %w", ErrLayoutUndefined)
return &CommitError{Err: err}
}
objPath, err := s.Layout.Resolve(id)
if err != nil {
return &CommitError{Err: fmt.Errorf("cannot commit id '%s': %w", id, err)}
}
// include the storage root spec so it can be checked during commit.
opts = append(opts, withStoreSpec(s.spec))
return Commit(ctx, writeFS, path.Join(s.rootDir, objPath), id, stage, opts...)
}
// GetStore returns a *Store for the OCFL Storage Root at root in fsys. The path
// root must be a directory/prefix with storage root declaration file.
func GetStore(ctx context.Context, fsys ocfl.FS, root string) (*Store, error) {
// Don't use fs.ReadDir here as we would with GetObject because storage
// roots might have huge numbers of directory entries. Instead, open storage
// root declarations until we find one (or return error)
var ocflVer ocfl.Spec
for _, s := range []ocfl.Spec{ocflv1_1, ocflv1_0} {
decl := ocfl.Namaste{Type: ocfl.NamasteTypeStore, Version: s}.Name()
if err := ocfl.ReadNamaste(ctx, fsys, path.Join(root, decl)); err != nil {
if errors.Is(err, fs.ErrNotExist) {
continue
}
return nil, fmt.Errorf("reading storage root delaration: %w", err)
}
ocflVer = s
break
}
if ocflVer.Empty() {
return nil, fmt.Errorf("missing storage root declaration: %w", ocfl.ErrNoNamaste)
}
str := &Store{
fsys: fsys,
rootDir: root,
spec: ocflVer,
}
cfg := storeConfig{}
err := readStoreConfig(ctx, fsys, root, &cfg)
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return nil, err
}
if err == nil {
str.config = cfg
// if ReadLayout fails, we don't return the error here. The store can
// still be used, however, the error should be returned by ResolveID()
// or other methods requiring the layout from the configuration.
str.ReadLayout(ctx)
}
return str, nil
}
// Root returns the Store's ocfl.FS and root directory.
func (s *Store) Root() (ocfl.FS, string) {
return s.fsys, s.rootDir
}
// Descriptions returns the description set in the storage root's
// ocfl_layout.json file, or an empty string if the description is undefined
func (s *Store) Description() string {
if s.config == nil {
return ""
}
return s.config[descriptionKey]
}
// LayoutName returns the extension name set in the storage root's
// ocfl_layout.json file, or an empty string if the name is not set.
func (s *Store) LayoutName() string {
if s.config == nil {
return ""
}
return s.config[extensionKey]
}
// Spec returns the ocfl.Spec defined in the storage root's declaration.
func (s *Store) Spec() ocfl.Spec {
return s.spec
}
// Objects iterates over over the OCFL Object in the storage root and calls fn
// for each. If an error is encountered while loading the object, the error is
// passed to fn. If fn returns an error the iteration process terminates.
func (s Store) Objects(ctx context.Context, fn func(*Object, error) error) error {
sel := ocfl.PathSelector{
Dir: s.rootDir,
SkipDirFn: func(n string) bool { return n == path.Join(s.rootDir, extensionsDir) },
}
return Objects(ctx, s.fsys, sel, fn)
}
// Validate performs complete validation on the store
func (s *Store) Validate(ctx context.Context, opts ...ValidationOption) *validation.Result {
return ValidateStore(ctx, s.fsys, s.rootDir, opts...)
}
// ResolveID resolves the storage path for the given id relative using the
// storage root's layout extension
func (s *Store) ResolveID(id string) (string, error) {
if s.layoutErr != nil {
return "", s.layoutErr
}
if s.Layout == nil {
return "", ErrLayoutUndefined
}
return s.Layout.Resolve(id)
}
// GetObjectPath returns an Object for the given path relative to the storage root.
func (s *Store) GetObjectPath(ctx context.Context, p string) (*Object, error) {
return GetObject(ctx, s.fsys, path.Join(s.rootDir, p))
}
// GetObject returns the OCFL object using the store's layout
// extension (if defined). The store layout is set during GetStore() if the
// storage root includes an ocfl_layout.json file. Otherwise, it can be set
// using, SetLayout().
func (s *Store) GetObject(ctx context.Context, id string) (*Object, error) {
pth, err := s.ResolveID(id)
if err != nil {
return nil, err
}
return GetObject(ctx, s.fsys, path.Join(s.rootDir, pth))
}
// ObjectExists returns true if an object with the given ID exists in the store
func (s *Store) ObjectExists(ctx context.Context, id string) (bool, error) {
_, err := s.GetObject(ctx, id)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return false, nil
}
return false, err
}
return true, nil
}
// ReadLayout resolves the layout extension defined in ocfl_layout.json and
// loads its configuration file (if present) from the store's extensions
// directory. The store's active layout is set to the new layout. If no error is
// returned, subsequent calls to ResolveID() will resolve object ids using the
// new layout.
func (s *Store) ReadLayout(ctx context.Context) error {
s.layoutErr = nil
name := s.LayoutName()
if name == "" {
s.layoutErr = ErrLayoutUndefined
return s.layoutErr
}
layout, err := readLayout(ctx, s.fsys, s.rootDir, name)
if err != nil {
s.layoutErr = err
return s.layoutErr
}
s.Layout = layout
return nil
}
func readLayout(ctx context.Context, fsys ocfl.FS, root string, name string) (extension.Layout, error) {
ext, err := readExtensionConfig(ctx, fsys, root, name)
if err != nil {
return nil, err
}
layout, ok := ext.(extension.Layout)
if !ok {
return nil, extension.ErrNotLayout
}
return layout, nil
}
// readExtensionConfig reads the extension config file for ext in the storage root's
// extensions directory. The value is unmarshalled into the value pointed to by
// ext. If the extension config does not exist, nil is returned.
func readExtensionConfig(ctx context.Context, fsys ocfl.FS, root string, name string) (extension.Extension, error) {
confPath := path.Join(root, extensionsDir, name, extensionConfigFile)
f, err := fsys.OpenFile(ctx, confPath)
if err != nil {
if !errors.Is(err, fs.ErrNotExist) {
return nil, fmt.Errorf("openning config for extension %s: %w", name, err)
}
return nil, nil
}
defer f.Close()
b, err := io.ReadAll(f)
if err != nil {
return nil, fmt.Errorf("reading config for extension %s: %w", name, err)
}
return extension.Unmarshal(b)
}
// writeExtensionConfig writes the configuration files for the ext to the
// extensions directory in the storage root with at root.
func writeExtensionConfig(ctx context.Context, fsys ocfl.WriteFS, root string, config extension.Extension) error {
confPath := path.Join(root, extensionsDir, config.Name(), extensionConfigFile)
b, err := json.Marshal(config)
if err != nil {
return fmt.Errorf("encoding config for extension %s: %w", config.Name(), err)
}
_, err = fsys.Write(ctx, confPath, bytes.NewBuffer(b))
if err != nil {
return fmt.Errorf("writing config for extension %s: %w", config.Name(), err)
}
return nil
}
func newStoreConfig(description string, layout extension.Layout) storeConfig {
return map[string]string{
descriptionKey: description,
extensionKey: layout.Name(),
}
}
// readStoreConfig reads the `ocfl_layout.json` files in the storage root
// and unmarshals into the value pointed to by layout
func readStoreConfig(ctx context.Context, fsys ocfl.FS, root string, layout *storeConfig) error {
f, err := fsys.OpenFile(ctx, path.Join(root, layoutName))
if err != nil {
return fmt.Errorf("reading %s: %w", layoutName, err)
}
defer f.Close()
if err := json.NewDecoder(f).Decode(layout); err != nil {
return fmt.Errorf("decoding %s: %w", layoutName, err)
}
return nil
}
// writeStoreConfig marshals the value pointe to by layout and writes the result to
// the `ocfl_layout.json` files in the storage root.
func writeStoreConfig(ctx context.Context, fsys ocfl.WriteFS, root string, layout storeConfig) error {
b, err := json.Marshal(layout)
if err != nil {
return fmt.Errorf("encoding %s: %w", layoutName, err)
}
_, err = fsys.Write(ctx, path.Join(root, layoutName), bytes.NewBuffer(b))
if err != nil {
return fmt.Errorf("writing %s: %w", layoutName, err)
}
return nil
}