Skip to content

Commit 30103a1

Browse files
committed
Bugfix: Correctly handle directories in store path for backups
1 parent 3847e6e commit 30103a1

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

internal/app/store/store.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/golang/protobuf/jsonpb"
77
"github.com/pkg/errors"
88
"os"
9+
"path/filepath"
910
"time"
1011
)
1112

@@ -103,15 +104,15 @@ func (s *Store) Add(entry *statemachine.StateChange) error {
103104

104105
_, err = s.file.WriteString(jsonState)
105106
if err != nil {
106-
return errors.Wrap(err, "Could not write to store")
107+
return errors.Wrapf(err, "Could not write '%v' to store", jsonState)
107108
}
108109
_, err = s.file.WriteString("\n")
109110
if err != nil {
110-
return errors.Wrap(err, "Could not write to store")
111+
return errors.Wrap(err, "Could not write '\n' to store")
111112
}
112113
err = s.file.Sync()
113114
if err != nil {
114-
return errors.Wrap(err, "Could not write to store")
115+
return errors.Wrap(err, "Could not sync store")
115116
}
116117
return nil
117118
}
@@ -121,9 +122,13 @@ func (s *Store) Reset() error {
121122
if err := s.Close(); err != nil {
122123
return errors.Wrap(err, "Could not close current store file")
123124
}
124-
backupFile := time.Now().Format("2006-01-02_15-04-05-MST") + "_" + s.filename
125-
if err := os.Rename(s.filename, backupFile); err != nil {
126-
return errors.Wrap(err, "Could not rename store file")
125+
if len(s.entries) > 0 {
126+
base := filepath.Base(s.filename)
127+
path := filepath.Dir(s.filename)
128+
backupFile := filepath.Join(path, time.Now().Format("2006-01-02_15-04-05-MST")+"_"+base)
129+
if err := os.Rename(s.filename, backupFile); err != nil {
130+
return errors.Wrap(err, "Could not rename store file")
131+
}
127132
}
128133
if err := s.Open(); err != nil {
129134
return errors.Wrap(err, "Could not reopen new empty store")

0 commit comments

Comments
 (0)