Skip to content

Commit 0c6ffbb

Browse files
Ensure path exists before cache creation (#1276)
Path should exist in order to verify permissions and use proper environment (read-write or read-only) for the cache Relates-To: OLPEDGE-2664 Signed-off-by: Andrey Kashcheev <[email protected]>
1 parent d01f000 commit 0c6ffbb

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

olp-cpp-sdk-core/src/cache/DefaultCacheImpl.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -833,27 +833,34 @@ DefaultCache::StorageOpenResult DefaultCacheImpl::SetupProtectedCache() {
833833
StorageSettings protected_storage_settings;
834834
protected_storage_settings.max_file_size = 32 * 1024 * 1024;
835835

836-
// In case user requested read-write acccess we will try to open protected
836+
const auto& disk_path_protected = *settings_.disk_path_protected;
837+
838+
// In case user requested read-write access we will try to open protected
837839
// cache as read-write also to prevent high RAM usage when cache is recovering
838840
// from uncompacted close.
839841
OpenOptions open_mode = settings_.openOptions;
840842
bool is_read_only = (settings_.openOptions & ReadOnly) == ReadOnly;
841843
if (!is_read_only) {
842-
if (utils::Dir::IsReadOnly(settings_.disk_path_protected.get())) {
844+
// It is necessary to create target directory here since 'IsReadOnly'
845+
// routine doesn't work correctly if the path doesn't exist.
846+
// Intentionally ignore possible error.
847+
utils::Dir::Create(disk_path_protected);
848+
849+
if (utils::Dir::IsReadOnly(disk_path_protected)) {
843850
OLP_SDK_LOG_INFO_F(kLogTag,
844851
"R/W permission missing, opening protected cache in "
845852
"r/o mode, disk_path_protected='%s'",
846-
settings_.disk_path_protected.get().c_str());
853+
disk_path_protected.c_str());
847854
open_mode = static_cast<OpenOptions>(open_mode | OpenOptions::ReadOnly);
848855
}
849856
}
850857

851-
auto status = protected_cache_->Open(
852-
settings_.disk_path_protected.get(), settings_.disk_path_protected.get(),
853-
protected_storage_settings, open_mode, false);
858+
auto status =
859+
protected_cache_->Open(disk_path_protected, disk_path_protected,
860+
protected_storage_settings, open_mode, false);
854861
if (status != OpenResult::Success) {
855862
OLP_SDK_LOG_ERROR_F(kLogTag, "Failed to open protected cache %s",
856-
settings_.disk_path_protected.get().c_str());
863+
disk_path_protected.c_str());
857864

858865
protected_cache_.reset();
859866
return ToStorageOpenResult(status);

0 commit comments

Comments
 (0)