@cryptotaxi247 / kubo / commits / ce62bed82

Write the spec file during initialization only.

License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>

Kevin Atkinson committed Jul 14, 2017 at 05:07 UTC ce62bed82e66343122b3b67325bf71203a36ba11
3 files changed +36 -28
repo/fsrepo/datastores.go
+6 -3
@@ -31,14 +31,17 @@ type DatastoreConfig interface {
31 Create(path string) (repo.Datastore, error)
32 }
33
34 -func (spec DiskSpec) String() string {
34 +func (spec DiskSpec) Bytes() []byte {
35 b, err := json.Marshal(spec)
36 if err != nil {
37 // should not happen
38 panic(err)
39 }
40 - b = bytes.TrimSpace(b)
41 - return string(b)
40 + return bytes.TrimSpace(b)
41 +}
42 +
43 +func (spec DiskSpec) String() string {
44 + return string(spec.Bytes())
45 }
46
47 var datastores map[string]ConfigFromMap
repo/fsrepo/fsrepo.go
+29 -24
@@ -240,9 +240,29 @@ func initConfig(path string, conf *config.Config) error {
240 if err := serialize.WriteConfigFile(configFilename, conf); err != nil {
241 return err
242 }
243 +
244 return nil
245 }
246
247 +func initSpec(path string, conf map[string]interface{}) error {
248 + fn, err := config.Path(path, SpecFn)
249 + if err != nil {
250 + return err
251 + }
252 +
253 + if util.FileExists(fn) {
254 + return nil
255 + }
256 +
257 + dsc, err := AnyDatastoreConfig(conf)
258 + if err != nil {
259 + return err
260 + }
261 + bytes := dsc.DiskSpec().Bytes()
262 +
263 + return ioutil.WriteFile(fn, bytes, 0600)
264 +}
265 +
266 // Init initializes a new FSRepo at the given path with the provided config.
267 // TODO add support for custom datastores.
268 func Init(repoPath string, conf *config.Config) error {
@@ -260,6 +280,10 @@ func Init(repoPath string, conf *config.Config) error {
280 return err
281 }
282
283 + if err := initSpec(repoPath, conf.Datastore.Spec); err != nil {
284 + return err
285 + }
286 +
287 if err := mfsr.RepoPath(repoPath).WriteVersion(RepoVersion); err != nil {
288 return err
289 }
@@ -370,19 +394,13 @@ func (r *FSRepo) openDatastore() error {
394 spec := dsc.DiskSpec()
395
396 oldSpec, err := r.readSpec()
373 - if err == nil {
374 - if oldSpec != spec.String() {
375 - return fmt.Errorf("Datastore configuration of '%s' does not match what is on disk '%s'",
376 - oldSpec, spec.String())
377 - }
378 - } else if os.IsNotExist(err) {
379 - err := r.writeSpec(spec.String())
380 - if err != nil {
381 - return err
382 - }
383 - } else {
397 + if err != nil {
398 return err
399 }
400 + if oldSpec != spec.String() {
401 + return fmt.Errorf("Datastore configuration of '%s' does not match what is on disk '%s'",
402 + oldSpec, spec.String())
403 + }
404
405 d, err := dsc.Create(r.path)
406 if err != nil {
@@ -411,19 +429,6 @@ func (r *FSRepo) readSpec() (string, error) {
429 return strings.TrimSpace(string(b)), nil
430 }
431
414 -func (r *FSRepo) writeSpec(spec string) error {
415 - fn, err := config.Path(r.path, SpecFn)
416 - if err != nil {
417 - return err
418 - }
419 - b := []byte(spec)
420 - err = ioutil.WriteFile(fn, b, 0600)
421 - if err != nil {
422 - return err
423 - }
424 - return nil
425 -}
426 -
432 // Close closes the FSRepo, releasing held resources.
433 func (r *FSRepo) Close() error {
434 packageLock.Lock()
repo/fsrepo/fsrepo_test.go
+1 -1
@@ -25,7 +25,7 @@ func TestInitIdempotence(t *testing.T) {
25 t.Parallel()
26 path := testRepoPath("", t)
27 for i := 0; i < 10; i++ {
28 - assert.Nil(Init(path, &config.Config{}), t, "multiple calls to init should succeed")
28 + assert.Nil(Init(path, &config.Config{Datastore: config.DefaultDatastoreConfig()}), t, "multiple calls to init should succeed")
29 }
30 }
31