Ensure boltDB pathname is constructed uniformly

Keep the concatenation of the prefix and filename inside the boltdb
package, so we can't get it wrong outside.
This commit is contained in:
Bryan Boreham committed 2017-04-11 15:45:04 +00:00
1 parent bc47866880
commit 5f6c5f6692
3 files changed
+14 -9

No files matched your search

+9 -3
View File
@@ -26,10 +26,15 @@ var (
const ( const (
NameIdent = "peername" NameIdent = "peername"
FileName = "data.db" fileName = "data.db"
) )
func NewBoltDB(dbPathname string) (*BoltDB, error) { func Pathname(dbPrefix string) string {
return dbPrefix + fileName
}
func NewBoltDB(dbPrefix string) (*BoltDB, error) {
dbPathname := Pathname(dbPrefix)
db, err := bolt.Open(dbPathname, 0660, nil) db, err := bolt.Open(dbPathname, 0660, nil)
if err != nil { if err != nil {
return nil, fmt.Errorf("[boltDB] Unable to open %s: %s", dbPathname, err) return nil, fmt.Errorf("[boltDB] Unable to open %s: %s", dbPathname, err)
@@ -38,8 +43,9 @@ func NewBoltDB(dbPathname string) (*BoltDB, error) {
return &BoltDB{db: db}, err return &BoltDB{db: db}, err
} }
func NewBoltDBReadOnly(dbPathname string) (*BoltDB, error) { func NewBoltDBReadOnly(dbPrefix string) (*BoltDB, error) {
options := bolt.Options{Timeout: time.Millisecond * 50, ReadOnly: true} options := bolt.Options{Timeout: time.Millisecond * 50, ReadOnly: true}
dbPathname := Pathname(dbPrefix)
db, err := bolt.Open(dbPathname, 0660, &options) db, err := bolt.Open(dbPathname, 0660, &options)
if err != nil { if err != nil {
return nil, fmt.Errorf("[boltDB] Unable to open %s: %s", dbPathname, err) return nil, fmt.Errorf("[boltDB] Unable to open %s: %s", dbPathname, err)
+4 -5
View File
@@ -36,8 +36,8 @@ func getSystemUUID(hostRoot string) ([]byte, error) {
return append(machineid, uuid...), nil return append(machineid, uuid...), nil
} }
func getPersistedPeerName(pathName string) (mesh.PeerName, error) { func getPersistedPeerName(dbPrefix string) (mesh.PeerName, error) {
d, err := db.NewBoltDBReadOnly(pathName) d, err := db.NewBoltDBReadOnly(dbPrefix)
if err != nil { if err != nil {
return mesh.UnknownPeerName, err return mesh.UnknownPeerName, err
} }
@@ -56,9 +56,8 @@ func getPersistedPeerName(pathName string) (mesh.PeerName, error) {
func GetSystemPeerName(dbPrefix, hostRoot string) (string, error) { func GetSystemPeerName(dbPrefix, hostRoot string) (string, error) {
// Check if we have a persisted name that matches the old-style ID for this host // Check if we have a persisted name that matches the old-style ID for this host
if oldUUID, err := getOldStyleSystemUUID(); err == nil { if oldUUID, err := getOldStyleSystemUUID(); err == nil {
pathName := dbPrefix + db.FileName if _, err := os.Stat(db.Pathname(dbPrefix)); err == nil {
if _, err := os.Stat(pathName); err == nil { persistedPeerName, err := getPersistedPeerName(dbPrefix)
persistedPeerName, err := getPersistedPeerName(pathName)
if err != nil && !os.IsNotExist(err) { if err != nil && !os.IsNotExist(err) {
return "", err return "", err
} }
+1 -1
View File
@@ -260,7 +260,7 @@ func main() {
Log.Fatalf("--awsvpc mode is not compatible with the --password option") Log.Fatalf("--awsvpc mode is not compatible with the --password option")
} }
db, err := db.NewBoltDB(dbPrefix + db.FileName) db, err := db.NewBoltDB(dbPrefix)
checkFatal(err) checkFatal(err)
defer db.Close() defer db.Close()