write out full spec instead of just the disk id
License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>
Kevin Atkinson committed
Jul 7, 2017 at 14:48 UTC
3bbe06513283f7f97aba851f00b9d19373c42162
1 file changed
+43
-15
repo/fsrepo/fsrepo.go
+43
-15
@@ -1,7 +1,7 @@
1
package fsrepo
2
3
import (
4
- "bytes"
4
+ "encoding/json"
5
"errors"
6
"fmt"
7
"io"
@@ -370,14 +370,14 @@ func (r *FSRepo) openDatastore() error {
370
}
371
diskId := dsc.DiskId()
372
373
- oldDiskId, err := r.readDiskId()
373
+ oldId, _, err := r.readSpec()
374
if err == nil {
375
- if oldDiskId != diskId {
375
+ if oldId != diskId {
376
return fmt.Errorf("Datastore configuration of '%s' does not match what is on disk '%s'",
377
- oldDiskId, diskId)
377
+ oldId, diskId)
378
}
379
} else if os.IsNotExist(err) {
380
- err := r.writeDiskId(diskId)
380
+ err := r.writeSpec(diskId, r.config.Datastore.Spec)
381
if err != nil {
382
return err
383
}
@@ -398,27 +398,55 @@ func (r *FSRepo) openDatastore() error {
398
return nil
399
}
400
401
-var DiskIdFn = "dsid"
401
+var SpecFn = "spec"
402
403
-func (r *FSRepo) readDiskId() (string, error) {
404
- fn, err := config.Path(r.path, DiskIdFn)
403
+func (r *FSRepo) readSpec() (string, map[string]interface{}, error) {
404
+ fn, err := config.Path(r.path, SpecFn)
405
if err != nil {
406
- return "", err
406
+ return "", nil, err
407
}
408
b, err := ioutil.ReadFile(fn)
409
if err != nil {
410
- return "", err
410
+ return "", nil, err
411
}
412
- b = bytes.TrimSpace(b)
413
- return string(b), nil
412
+ idspec := make(map[string]interface{})
413
+ err = json.Unmarshal(b, &idspec)
414
+ if err != nil {
415
+ return "", nil, err
416
+ }
417
+ id, ok := idspec["id"].(string)
418
+ if !ok {
419
+ return "", nil, fmt.Errorf("could not retrieve 'id' field from spec file")
420
+ }
421
+ spec, ok := idspec["spec"].(map[string]interface{})
422
+ if !ok {
423
+ return "", nil, fmt.Errorf("could not retrieve 'spec' field from spec file")
424
+ }
425
+
426
+ dsc, err := AnyDatastoreConfig(spec)
427
+ if err != nil {
428
+ return "", nil, err
429
+ }
430
+ computedId := dsc.DiskId()
431
+ if computedId != id {
432
+ return "", nil, fmt.Errorf("bad spec file, computed id (%s) does not match given (%s)",
433
+ computedId, id)
434
+ }
435
+
436
+ return id, spec, nil
437
}
438
416
-func (r *FSRepo) writeDiskId(newId string) error {
417
- fn, err := config.Path(r.path, DiskIdFn)
439
+func (r *FSRepo) writeSpec(id string, spec map[string]interface{}) error {
440
+ fn, err := config.Path(r.path, SpecFn)
441
if err != nil {
442
return err
443
}
421
- err = ioutil.WriteFile(fn, []byte(newId), 0666)
444
+ idspec := map[string]interface{}{
445
+ "id": id,
446
+ "spec": spec,
447
+ }
448
+ b, err := json.Marshal(idspec)
449
+ err = ioutil.WriteFile(fn, b, 0666)
450
if err != nil {
451
return err
452
}