Skip to content

Commit 96c46a1

Browse files
Check only directories name during cache init (#1484)
Instead of checking whole path check only top-level directories names and produce error if there are some unexpected one. Nested directories in the `lost` directory are allowed for now. Relates-To: OLPEDGE-2868 Signed-off-by: Andrey Kashcheev <ext-andrey.kashcheev@here.com>
1 parent 1062239 commit 96c46a1

File tree

4 files changed

+53
-52
lines changed

4 files changed

+53
-52
lines changed

olp-cpp-sdk-core/include/olp/core/utils/Dir.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,11 @@ class CORE_API Dir {
148148
static bool IsReadOnly(const std::string& path);
149149

150150
/**
151-
* @brief Iterates through all directories in the provided path and calls the
152-
* provided callback function for each directory.
151+
* @brief Iterates through top-level directories in the provided path and
152+
* calls the provided callback function for each directory.
153153
*
154154
* @param path The path of the root directory.
155-
* @param path_fn The callback function.
155+
* @param path_fn The callback function with a relative path.
156156
*/
157157
static void ForEachDirectory(const std::string& path, PathCallback path_fn);
158158
};

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ namespace cache {
4242

4343
namespace {
4444
constexpr auto kLogTag = "DiskCache";
45-
constexpr auto kLevelDbLostFolder = "/lost";
45+
constexpr auto kLevelDbLostFolder = "lost";
4646
constexpr auto kMaxL0Files = 4;
4747

4848
leveldb::Slice ToLeveldbSlice(const std::string& slice) {
@@ -54,11 +54,11 @@ static bool RepairCache(const std::string& data_path) {
5454
auto status = leveldb::RepairDB(data_path, leveldb::Options());
5555
if (status.ok()) {
5656
OLP_SDK_LOG_INFO(kLogTag, "RepairCache: repaired - " << data_path);
57-
const auto lost_folder_path = data_path + kLevelDbLostFolder;
57+
const auto lost_folder_path = data_path + '/' + kLevelDbLostFolder;
5858
if (utils::Dir::Exists(lost_folder_path)) {
5959
OLP_SDK_LOG_INFO_F(
6060
kLogTag, "RepairCache: some data may have been lost - deleting '%s'",
61-
kLevelDbLostFolder);
61+
lost_folder_path.c_str());
6262
utils::Dir::Remove(lost_folder_path);
6363
}
6464
return true;
@@ -205,14 +205,14 @@ OpenResult DiskCache::Open(const std::string& data_path,
205205
}
206206

207207
// Check cache path for unexpected directories
208-
const std::vector<std::string> expected_dirs = {disk_cache_path_ +
209-
kLevelDbLostFolder};
208+
const std::vector<std::string> expected_dirs = {kLevelDbLostFolder};
210209
bool unexpected_dirs = false;
211210
utils::Dir::ForEachDirectory(disk_cache_path_, [&](const std::string& dir) {
212211
if (std::find(expected_dirs.begin(), expected_dirs.end(), dir) ==
213212
expected_dirs.end()) {
214-
OLP_SDK_LOG_WARNING_F(
215-
kLogTag, "Open: unexpected directory found, path='%s'", dir.c_str());
213+
OLP_SDK_LOG_WARNING_F(kLogTag,
214+
"Open: unexpected directory found, path='%s/%s'",
215+
disk_cache_path_.c_str(), dir.c_str());
216216
unexpected_dirs = true;
217217
}
218218
});

olp-cpp-sdk-core/src/utils/Dir.cpp

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -546,9 +546,7 @@ void Dir::ForEachDirectory(const std::string& path, PathCallback path_fn) {
546546
}
547547

548548
if (find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
549-
current_path = path + "\\" + find_data.cFileName;
550-
path_fn(current_path);
551-
ForEachDirectory(current_path, path_fn);
549+
path_fn(find_data.cFileName);
552550
}
553551
} while (FindNextFileA(handle, &find_data) != 0);
554552

@@ -557,50 +555,39 @@ void Dir::ForEachDirectory(const std::string& path, PathCallback path_fn) {
557555

558556
#else
559557

560-
std::queue<std::string> recursive_queue;
561-
recursive_queue.push(path);
562-
563-
auto iterate_directory = [&](const std::string& path) {
564-
auto* dir = ::opendir(path.c_str());
565-
if (dir == nullptr) {
566-
return;
567-
}
558+
auto* dir = ::opendir(path.c_str());
559+
if (dir == nullptr) {
560+
return;
561+
}
568562

569-
struct ::dirent* entry = nullptr;
570-
while ((entry = ::readdir(dir)) != nullptr) {
571-
const char* entry_name = entry->d_name;
563+
struct ::dirent* entry = nullptr;
564+
while ((entry = ::readdir(dir)) != nullptr) {
565+
const char* entry_name = entry->d_name;
572566

573-
if (strcmp(entry_name, ".") == 0 || strcmp(entry_name, "..") == 0) {
574-
continue;
575-
}
567+
if (strcmp(entry_name, ".") == 0 || strcmp(entry_name, "..") == 0) {
568+
continue;
569+
}
576570

577-
std::string full_path = path + "/" + entry_name;
571+
std::string full_path = path + "/" + entry_name;
578572

579573
#ifdef __APPLE__
580-
struct ::stat path_stat;
581-
if (::lstat(full_path.c_str(), &path_stat) == 0) {
574+
struct ::stat path_stat;
575+
if (::lstat(full_path.c_str(), &path_stat) == 0) {
582576
#else
583-
struct ::stat64 path_stat;
584-
if (::lstat64(full_path.c_str(), &path_stat) == 0) {
577+
struct ::stat64 path_stat;
578+
if (::lstat64(full_path.c_str(), &path_stat) == 0) {
585579
#endif
586-
if (S_ISDIR(path_stat.st_mode)) {
587-
path_fn(full_path);
588-
recursive_queue.push(std::move(full_path));
589-
}
590-
} else if (errno != ENOENT) {
591-
// Ignore ENOENT errors as its a common case, e.g. cache compaction.
592-
break;
580+
if (S_ISDIR(path_stat.st_mode)) {
581+
path_fn(entry_name);
593582
}
583+
} else if (errno != ENOENT) {
584+
// Ignore ENOENT errors as its a common case, e.g. cache compaction.
585+
break;
594586
}
595-
596-
::closedir(dir);
597-
};
598-
599-
while (!recursive_queue.empty()) {
600-
iterate_directory(recursive_queue.front());
601-
recursive_queue.pop();
602587
}
603588

589+
::closedir(dir);
590+
604591
#endif
605592
}
606593

olp-cpp-sdk-core/tests/cache/DefaultCacheTest.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -686,20 +686,19 @@ TEST(DefaultCacheTest, OpenTypeCache) {
686686
ASSERT_FALSE(cache.Contains(key2));
687687
}
688688

689-
{
690-
SCOPED_TRACE("Additional directories");
691-
689+
auto additional_dirs_test = [](const std::string& mutable_path,
690+
const std::string& protected_path) {
692691
constexpr auto kExpectedDirResult =
693692
olp::cache::DefaultCache::StorageOpenResult::Success;
694693
constexpr auto kUnexpectedDirResult =
695694
olp::cache::DefaultCache::StorageOpenResult::OpenDiskPathFailure;
696695

697696
auto scenarios = {
697+
std::make_tuple("/lost/tmp", kExpectedDirResult),
698698
std::make_tuple("/lost", kExpectedDirResult),
699-
std::make_tuple("/lost/tmp", kUnexpectedDirResult),
700699
std::make_tuple("/found", kUnexpectedDirResult),
701-
std::make_tuple("/ARCHIVES", kUnexpectedDirResult),
702-
std::make_tuple("/ARCHIVES/removed", kUnexpectedDirResult)};
700+
std::make_tuple("/ARCHIVES/removed", kUnexpectedDirResult),
701+
std::make_tuple("/ARCHIVES", kUnexpectedDirResult)};
703702

704703
for (auto& scenario : scenarios) {
705704
{
@@ -732,6 +731,21 @@ TEST(DefaultCacheTest, OpenTypeCache) {
732731
olp::utils::Dir::Remove(dir_path);
733732
}
734733
}
734+
};
735+
736+
{
737+
SCOPED_TRACE("Additional directories");
738+
739+
additional_dirs_test(mutable_path, protected_path);
740+
}
741+
742+
{
743+
SCOPED_TRACE("Additional directories, relative paths");
744+
745+
std::string mutable_relative_path = mutable_path + "/../mutable_cache";
746+
std::string protected_relative_path =
747+
protected_path + "/../protected_cache";
748+
additional_dirs_test(mutable_relative_path, protected_relative_path);
735749
}
736750
}
737751

0 commit comments

Comments
 (0)