From 3fa4c28f6b6f775bd35d59da489ef544e9a2b32f Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Tue, 11 Jun 2019 23:03:14 +0300 Subject: [PATCH] lib/fs: make sure the created directory remains visible in the fs in the event of power loss Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/63 --- lib/fs/fs.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/fs/fs.go b/lib/fs/fs.go index adda41301..dddb496a7 100644 --- a/lib/fs/fs.go +++ b/lib/fs/fs.go @@ -124,7 +124,7 @@ func MkdirAllIfNotExist(path string) error { if IsPathExist(path) { return nil } - return os.MkdirAll(path, 0755) + return mkdirSync(path) } // MkdirAllFailIfExist creates the given path dir if it isn't exist. @@ -134,7 +134,18 @@ func MkdirAllFailIfExist(path string) error { if IsPathExist(path) { return fmt.Errorf("the %q already exists", path) } - return os.MkdirAll(path, 0755) + return mkdirSync(path) +} + +func mkdirSync(path string) error { + if err := os.MkdirAll(path, 0755); err != nil { + return err + } + // Sync the parent directory, so the created directory becomes visible + // in the fs after power loss. + parentDirPath := filepath.Dir(path) + SyncPath(parentDirPath) + return nil } // RemoveDirContents removes all the contents of the given dir it it exists. @@ -255,7 +266,7 @@ func init() { // HardLinkFiles makes hard links for all the files from srcDir in dstDir. func HardLinkFiles(srcDir, dstDir string) error { - if err := os.MkdirAll(dstDir, 0755); err != nil { + if err := mkdirSync(dstDir); err != nil { return fmt.Errorf("cannot create dstDir=%q: %s", dstDir, err) }