Skip to content

Commit 624410f

Browse files
committed
Add skip rules for removing files for phys restore
1 parent 2fce3b0 commit 624410f

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

pbm/restore/physical.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ func (r *PhysRestore) close(noerr, cleanup bool) {
248248
}
249249
} else if cleanup { // clean-up dbpath on err if needed (cluster is done or partlyDone)
250250
r.log.Debug("clean-up dbpath")
251-
err := removeAll(r.dbpath, nil, r.log)
251+
err := removeAll(r.dbpath, r.log, getInternlLogFileSkipRule())
252252
if err != nil {
253253
r.log.Error("flush dbpath %s: %v", r.dbpath, err)
254254
}
@@ -398,7 +398,7 @@ func (r *PhysRestore) migrateDBDirToFallbackDir() error {
398398
// moves all content from fallback path.
399399
func (r *PhysRestore) migrateFromFallbackDirToDBDir() error {
400400
r.log.Debug("clean-up dbpath")
401-
err := removeAll(r.dbpath, []string{fallbackDir}, r.log)
401+
err := removeAll(r.dbpath, r.log, getFallbackSyncFileSkipRule(), getInternlLogFileSkipRule())
402402
if err != nil {
403403
r.log.Error("flush dbpath %s: %v", r.dbpath, err)
404404
return errors.Wrap(err, "remove all from dbpath")
@@ -2588,7 +2588,7 @@ func moveAll(fromDir, toDir string, toIgnore []string, l log.LogEvent) error {
25882588
return nil
25892589
}
25902590

2591-
func removeAll(dir string, toIgnore []string, l log.LogEvent) error {
2591+
func removeAll(dir string, l log.LogEvent, fileSkipRules ...fileSkipRule) error {
25922592
d, err := os.Open(dir)
25932593
if err != nil {
25942594
return errors.Wrap(err, "open dir")
@@ -2600,7 +2600,7 @@ func removeAll(dir string, toIgnore []string, l log.LogEvent) error {
26002600
return errors.Wrap(err, "read file names")
26012601
}
26022602
for _, n := range names {
2603-
if isInternalMongoLog(n) || slices.Contains(toIgnore, n) {
2603+
if isFileToSkip(n, fileSkipRules...) {
26042604
continue
26052605
}
26062606
err = os.RemoveAll(filepath.Join(dir, n))
@@ -2618,6 +2618,32 @@ func isInternalMongoLog(f string) bool {
26182618
return strings.HasPrefix(f, internalMongodLog)
26192619
}
26202620

2621+
type fileSkipRule func(string) bool
2622+
2623+
// isFileToSkip for the given file name f and the given set of skip rules: skipRules,
2624+
// function returns true if at least one rule is satisfied. In case when all rules
2625+
// are false it returns false, which means that file shouldn't be skipped.
2626+
func isFileToSkip(f string, skipRules ...fileSkipRule) bool {
2627+
for _, rule := range skipRules {
2628+
if rule(f) {
2629+
return true
2630+
}
2631+
}
2632+
return false
2633+
}
2634+
2635+
func getInternlLogFileSkipRule() fileSkipRule {
2636+
return func(f string) bool {
2637+
return isInternalMongoLog(f)
2638+
}
2639+
}
2640+
2641+
func getFallbackSyncFileSkipRule() fileSkipRule {
2642+
return func(f string) bool {
2643+
return f == fallbackDir
2644+
}
2645+
}
2646+
26212647
func majmin(v string) string {
26222648
if len(v) == 0 {
26232649
return v

0 commit comments

Comments
 (0)