From d72a54aae2892985988379bb757b216fa2130ea9 Mon Sep 17 00:00:00 2001 From: Jeronimo Irazabal Date: Mon, 31 Oct 2022 14:55:06 -0300 Subject: [PATCH] chore(embedded/appendable): sync directories Signed-off-by: Jeronimo Irazabal chore(embedded/appendable): fileutils methods Signed-off-by: Jeronimo Irazabal --- embedded/appendable/appendable.go | 22 ------------- embedded/appendable/fileutils/fileutils.go | 27 +++++++++++++++ .../appendable/fileutils/fileutils_unix.go | 33 +++++++++++++++++++ .../appendable/fileutils/fileutils_windows.go | 24 ++++++++++++++ embedded/appendable/multiapp/multi_app.go | 14 +++++++- embedded/appendable/singleapp/single_app.go | 3 +- 6 files changed, 99 insertions(+), 24 deletions(-) create mode 100644 embedded/appendable/fileutils/fileutils.go create mode 100644 embedded/appendable/fileutils/fileutils_unix.go create mode 100644 embedded/appendable/fileutils/fileutils_windows.go diff --git a/embedded/appendable/appendable.go b/embedded/appendable/appendable.go index 50d4a58257..0856a4a52b 100644 --- a/embedded/appendable/appendable.go +++ b/embedded/appendable/appendable.go @@ -20,7 +20,6 @@ import ( "compress/flate" "crypto/sha256" "io" - "os" ) const DefaultCompressionFormat = NoCompression @@ -74,24 +73,3 @@ func Checksum(rAt io.ReaderAt, off, n int64) (checksum [sha256.Size]byte, err er return checksum, nil } - -func SyncPaths(paths ...string) error { - for _, path := range paths { - err := SyncPath(path) - if err != nil { - return err - } - } - return nil -} - -func SyncPath(path string) error { - f, err := os.Open(path) - if err != nil { - return err - } - - defer f.Close() - - return f.Sync() -} diff --git a/embedded/appendable/fileutils/fileutils.go b/embedded/appendable/fileutils/fileutils.go new file mode 100644 index 0000000000..a6a337975c --- /dev/null +++ b/embedded/appendable/fileutils/fileutils.go @@ -0,0 +1,27 @@ +/* +Copyright 2022 Codenotary Inc. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package fileutils + +func SyncDir(paths ...string) error { + for _, path := range paths { + err := syncDir(path) + if err != nil { + return err + } + } + return nil +} diff --git a/embedded/appendable/fileutils/fileutils_unix.go b/embedded/appendable/fileutils/fileutils_unix.go new file mode 100644 index 0000000000..cce124d5ae --- /dev/null +++ b/embedded/appendable/fileutils/fileutils_unix.go @@ -0,0 +1,33 @@ +//go:build !windows +// +build !windows + +/* +Copyright 2022 Codenotary Inc. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package fileutils + +import "os" + +func syncDir(path string) error { + f, err := os.Open(path) + if err != nil { + return err + } + + defer f.Close() + + return f.Sync() +} diff --git a/embedded/appendable/fileutils/fileutils_windows.go b/embedded/appendable/fileutils/fileutils_windows.go new file mode 100644 index 0000000000..28d836494f --- /dev/null +++ b/embedded/appendable/fileutils/fileutils_windows.go @@ -0,0 +1,24 @@ +//go:build windows +// +build windows + +/* +Copyright 2022 Codenotary Inc. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package fileutils + +func syncDir(path string) error { + return nil +} diff --git a/embedded/appendable/multiapp/multi_app.go b/embedded/appendable/multiapp/multi_app.go index e1f3938864..63bdf0344d 100644 --- a/embedded/appendable/multiapp/multi_app.go +++ b/embedded/appendable/multiapp/multi_app.go @@ -29,6 +29,7 @@ import ( "sync" "github.com/codenotary/immudb/embedded/appendable" + "github.com/codenotary/immudb/embedded/appendable/fileutils" "github.com/codenotary/immudb/embedded/appendable/singleapp" "github.com/codenotary/immudb/embedded/cache" ) @@ -141,7 +142,7 @@ func OpenWithHooks(path string, hooks MultiFileAppendableHooks, opts *Options) ( return nil, err } - err = appendable.SyncPaths(path, filepath.Dir(path)) + err = fileutils.SyncDir(path, filepath.Dir(path)) if err != nil { return nil, err } @@ -460,6 +461,8 @@ func (mf *MultiFileAppendable) DiscardUpto(off int64) error { appID := appendableID(off, mf.fileSize) + var dirSyncNeeded bool + for i := int64(0); i < appID; i++ { if i == mf.currAppID { break @@ -478,6 +481,15 @@ func (mf *MultiFileAppendable) DiscardUpto(off int64) error { if err != nil && !os.IsNotExist(err) { return err } + + dirSyncNeeded = true + } + + if dirSyncNeeded { + err := fileutils.SyncDir(mf.path) + if err != nil { + return err + } } return nil diff --git a/embedded/appendable/singleapp/single_app.go b/embedded/appendable/singleapp/single_app.go index 633b8bea95..5426ae713c 100644 --- a/embedded/appendable/singleapp/single_app.go +++ b/embedded/appendable/singleapp/single_app.go @@ -32,6 +32,7 @@ import ( "sync" "github.com/codenotary/immudb/embedded/appendable" + "github.com/codenotary/immudb/embedded/appendable/fileutils" ) var ErrorPathIsNotADirectory = errors.New("singleapp: path is not a directory") @@ -139,7 +140,7 @@ func Open(fileName string, opts *Options) (*AppendableFile, error) { return nil, err } - err = appendable.SyncPath(filepath.Dir(fileName)) + err = fileutils.SyncDir(filepath.Dir(fileName)) if err != nil { return nil, err }