Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
Ensure boltDB pathname is constructed uniformly
Browse files Browse the repository at this point in the history
Keep the concatenation of the prefix and filename inside the boltdb
package, so we can't get it wrong outside.
  • Loading branch information
bboreham committed Apr 11, 2017
1 parent bc47866 commit 5f6c5f6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
12 changes: 9 additions & 3 deletions db/boltdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@ var (

const (
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)
if err != nil {
return nil, fmt.Errorf("[boltDB] Unable to open %s: %s", dbPathname, err)
Expand All @@ -38,8 +43,9 @@ func NewBoltDB(dbPathname string) (*BoltDB, error) {
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}
dbPathname := Pathname(dbPrefix)
db, err := bolt.Open(dbPathname, 0660, &options)
if err != nil {
return nil, fmt.Errorf("[boltDB] Unable to open %s: %s", dbPathname, err)
Expand Down
9 changes: 4 additions & 5 deletions net/uuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ func getSystemUUID(hostRoot string) ([]byte, error) {
return append(machineid, uuid...), nil
}

func getPersistedPeerName(pathName string) (mesh.PeerName, error) {
d, err := db.NewBoltDBReadOnly(pathName)
func getPersistedPeerName(dbPrefix string) (mesh.PeerName, error) {
d, err := db.NewBoltDBReadOnly(dbPrefix)
if err != nil {
return mesh.UnknownPeerName, err
}
Expand All @@ -56,9 +56,8 @@ func getPersistedPeerName(pathName string) (mesh.PeerName, error) {
func GetSystemPeerName(dbPrefix, hostRoot string) (string, error) {
// Check if we have a persisted name that matches the old-style ID for this host
if oldUUID, err := getOldStyleSystemUUID(); err == nil {
pathName := dbPrefix + db.FileName
if _, err := os.Stat(pathName); err == nil {
persistedPeerName, err := getPersistedPeerName(pathName)
if _, err := os.Stat(db.Pathname(dbPrefix)); err == nil {
persistedPeerName, err := getPersistedPeerName(dbPrefix)
if err != nil && !os.IsNotExist(err) {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion prog/weaver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func main() {
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)
defer db.Close()

Expand Down

0 comments on commit 5f6c5f6

Please sign in to comment.