diff --git a/dev/devcam/env.go b/dev/devcam/env.go index 0880933a1..b421aac8f 100644 --- a/dev/devcam/env.go +++ b/dev/devcam/env.go @@ -119,4 +119,5 @@ func (e *Env) SetCamdevVars(altkey bool) { e.Set("CAMLI_SECRET_RING", secring) e.Set("CAMLI_KEYID", identity) e.Set("CAMLI_PUBKEY_BLOBREF", pubKeyRef.String()) + e.Set("CAMLI_KV_VERIFY", "true") } diff --git a/dev/devcam/server.go b/dev/devcam/server.go index 0f032a436..9edc76856 100644 --- a/dev/devcam/server.go +++ b/dev/devcam/server.go @@ -213,7 +213,6 @@ func (c *serverCmd) setEnvVars() error { default: setenv("CAMLI_KVINDEX_ENABLED", "true") setenv("CAMLI_INDEXER_PATH", "/index-kv/") - setenv("CAMLI_KV_VERIFY", "true") if c.root == "" { panic("no root set") } diff --git a/pkg/kvutil/kvutil.go b/pkg/kvutil/kvutil.go index 1c0fcb56d..0fd6c7811 100644 --- a/pkg/kvutil/kvutil.go +++ b/pkg/kvutil/kvutil.go @@ -19,8 +19,10 @@ limitations under the License. package kvutil import ( + "fmt" "io" "os" + "strconv" "camlistore.org/third_party/github.com/camlistore/lock" "camlistore.org/third_party/github.com/cznic/kv" @@ -28,19 +30,35 @@ import ( // Open opens the named kv DB file for reading/writing. It // creates the file if it does not exist yet. -func Open(filePath string, opts *kv.Options) (*kv.DB, error) { - // TODO(mpl): use it in index pkg and such +func Open(dbFile string, opts *kv.Options) (*kv.DB, error) { createOpen := kv.Open - if _, err := os.Stat(filePath); os.IsNotExist(err) { + verb := "opening" + if _, err := os.Stat(dbFile); os.IsNotExist(err) { createOpen = kv.Create + verb = "creating" } if opts == nil { opts = &kv.Options{} } if opts.Locker == nil { - opts.Locker = func(fullPath string) (io.Closer, error) { - return lock.Lock(filePath + ".lock") + opts.Locker = func(dbFile string) (io.Closer, error) { + lkfile := dbFile + ".lock" + cl, err := lock.Lock(lkfile) + if err != nil { + return nil, fmt.Errorf("failed to acquire lock on %s: %v", lkfile, err) + } + return cl, nil } } - return createOpen(filePath, opts) + if v, _ := strconv.ParseBool(os.Getenv("CAMLI_KV_VERIFY")); v { + opts.VerifyDbBeforeOpen = true + opts.VerifyDbAfterOpen = true + opts.VerifyDbBeforeClose = true + opts.VerifyDbAfterClose = true + } + db, err := createOpen(dbFile, opts) + if err != nil { + return nil, fmt.Errorf("error %s %s: %v", verb, dbFile, err) + } + return db, nil } diff --git a/pkg/sorted/kvfile/kvfile.go b/pkg/sorted/kvfile/kvfile.go index 9db5eaaae..88871cdef 100644 --- a/pkg/sorted/kvfile/kvfile.go +++ b/pkg/sorted/kvfile/kvfile.go @@ -26,13 +26,12 @@ import ( "io" "log" "os" - "strconv" "sync" "camlistore.org/pkg/jsonconfig" + "camlistore.org/pkg/kvutil" "camlistore.org/pkg/sorted" - "camlistore.org/third_party/github.com/camlistore/lock" "camlistore.org/third_party/github.com/cznic/kv" ) @@ -55,31 +54,10 @@ func NewKeyValue(cfg jsonconfig.Obj) (sorted.KeyValue, error) { if err := cfg.Validate(); err != nil { return nil, err } - createOpen := kv.Open - verb := "opening" - if _, err := os.Stat(file); os.IsNotExist(err) { - createOpen = kv.Create - verb = "creating" - } - opts := &kv.Options{ - Locker: func(dbname string) (io.Closer, error) { - lkfile := dbname + ".lock" - cl, err := lock.Lock(lkfile) - if err != nil { - return nil, fmt.Errorf("failed to acquire lock on %s: %v", lkfile, err) - } - return cl, nil - }, - } - if v, _ := strconv.ParseBool(os.Getenv("CAMLI_KV_VERIFY")); v { - opts.VerifyDbBeforeOpen = true - opts.VerifyDbAfterOpen = true - opts.VerifyDbBeforeClose = true - opts.VerifyDbAfterClose = true - } - db, err := createOpen(file, opts) + opts := &kv.Options{} + db, err := kvutil.Open(file, opts) if err != nil { - return nil, fmt.Errorf("error %s %s: %v", verb, file, err) + return nil, err } is := &kvis{ db: db,