@@ -22,6 +22,7 @@ package checkpoint
2222
2323import (
2424 "fmt"
25+ "io"
2526 "io/ioutil"
2627 "os"
2728 "path/filepath"
@@ -88,7 +89,10 @@ func NewCheckpoint(file string, maxUpdates int, interval time.Duration) (*Checkp
8889 save : make (chan JournalState , 1 ),
8990 }
9091
91- c .file = paths .Resolve (paths .Data , c .file )
92+ err := c .findRegistryFile ()
93+ if err != nil {
94+ return nil , fmt .Errorf ("error locating the proper registry file: %+v" , err )
95+ }
9296
9397 // Minimum batch size.
9498 if c .maxUpdates < 1 {
@@ -123,6 +127,53 @@ func NewCheckpoint(file string, maxUpdates int, interval time.Duration) (*Checkp
123127 return c , nil
124128}
125129
130+ // Previously the registry file was written to the root folder. It was fixed on
131+ // 7.x but not on 6.x. Thus, migration is needed, so users avoid losing state info.
132+ func (c * Checkpoint ) findRegistryFile () error {
133+ migratedPath := paths .Resolve (paths .Data , c .file )
134+
135+ fs , err := os .Stat (c .file )
136+ if os .IsNotExist (err ) {
137+ c .file = migratedPath
138+ return nil
139+ } else if err != nil {
140+ return fmt .Errorf ("error accessing previous registry file: %+v" , err )
141+ }
142+
143+ f , err := os .Open (c .file )
144+ if err != nil {
145+ return err
146+ }
147+ defer f .Close ()
148+
149+ target , err := os .OpenFile (migratedPath , os .O_WRONLY | os .O_CREATE | os .O_TRUNC , fs .Mode ())
150+ if err != nil {
151+ return err
152+ }
153+ defer target .Close ()
154+
155+ if _ , err := io .Copy (target , f ); err != nil {
156+ return err
157+ }
158+
159+ err = target .Sync ()
160+ if err != nil {
161+ return fmt .Errorf ("error while syncing new registry file to disk: %+v" , err )
162+ }
163+
164+ c .file = migratedPath
165+
166+ p := filepath .Dir (migratedPath )
167+ pf , err := os .Open (p )
168+ if err != nil {
169+ return nil
170+ }
171+ defer pf .Close ()
172+ pf .Sync ()
173+
174+ return nil
175+ }
176+
126177// run is worker loop that reads incoming state information from the save
127178// channel and persists it when the number of changes reaches maxEvents or
128179// the amount of time since the last disk write reaches flushInterval.
0 commit comments