lib/backup: remove flock.lock file in empty dirs

This fixes an issue when VictoriaMetrics doesn't see the restored data after the following operations:

1. Stop VictoriaMetrics.
2. Delete `<-storageDataPath>` dir.
3. Start VictoriaMetrics, then stop it.
4. Restore data from backup with `vmrestore`.
5. Start VictoriaMetrics.

`vmrestore` didn't delete properly empty dirs in `<-storageDataPath>/indexdb` because of the remaining `flock.lock` files in these dirs.
This commit is contained in:
Aliaksandr Valialkin 2019-11-28 13:36:02 +02:00
parent a6d7179286
commit a7125a5b7b

View File

@ -173,6 +173,7 @@ func removeEmptyDirsInternal(d *os.File) (bool, error) {
return false, fmt.Errorf("cannot read directory contents in %q: %s", dir, err)
}
dirEntries := 0
hasFlock := false
for _, fi := range fis {
name := fi.Name()
if name == "." || name == ".." {
@ -191,6 +192,10 @@ func removeEmptyDirsInternal(d *os.File) (bool, error) {
continue
}
if fi.Mode()&os.ModeSymlink != os.ModeSymlink {
if name == "flock.lock" {
hasFlock = true
continue
}
// Skip plain files.
dirEntries++
continue
@ -244,6 +249,12 @@ func removeEmptyDirsInternal(d *os.File) (bool, error) {
return false, nil
}
logger.Infof("removing empty dir %q", dir)
if hasFlock {
flockFilepath := dir + "/flock.lock"
if err := os.Remove(flockFilepath); err != nil {
return false, fmt.Errorf("cannot remove %q: %s", flockFilepath, err)
}
}
if err := os.Remove(dir); err != nil {
return false, fmt.Errorf("cannot remove %q: %s", dir, err)
}