diff --git a/src/common/file_system/local_file_system.cpp b/src/common/file_system/local_file_system.cpp index 8abe57ea73..c2a830b7aa 100644 --- a/src/common/file_system/local_file_system.cpp +++ b/src/common/file_system/local_file_system.cpp @@ -222,7 +222,7 @@ void LocalFileSystem::createDir(const std::string& dir) const { auto directoryToCreate = dir; if (directoryToCreate.ends_with('/') #if defined(_WIN32) - || directoryToCreate.ends_with('\\') + || directoryToCreate.ends_with('\\') #endif ) { // This is a known issue with std::filesystem::create_directories. (link: @@ -261,7 +261,7 @@ bool isSubdirectory(const std::filesystem::path& base, const std::filesystem::pa // Size check for a "." result. // If the path starts with "..", it's not a subdirectory. return !relative.empty() && !(relative.starts_with("..")); - + } catch (const std::filesystem::filesystem_error& e) { // Handle errors, e.g., if paths don't exist std::cerr << "Filesystem error: " << e.what() << std::endl; @@ -271,7 +271,6 @@ bool isSubdirectory(const std::filesystem::path& base, const std::filesystem::pa return false; } - void LocalFileSystem::removeFileIfExists(const std::string& path) { if (!fileOrPathExists(path)) { return; diff --git a/src/include/storage/storage_utils.h b/src/include/storage/storage_utils.h index fa77d041ed..f169cc8bbe 100644 --- a/src/include/storage/storage_utils.h +++ b/src/include/storage/storage_utils.h @@ -7,10 +7,10 @@ #include "common/file_system/virtual_file_system.h" #include "common/null_mask.h" #include "common/types/types.h" -#include "storage/db_file_id.h" -#include "main/db_config.h" #include "main/client_context.h" +#include "main/db_config.h" #include "main/settings.h" +#include "storage/db_file_id.h" namespace kuzu { namespace storage { @@ -145,12 +145,13 @@ class StorageUtils { auto fullPath = path; // Handle '~' for home directory expansion if (path.starts_with('~')) { - fullPath = - context->getCurrentSetting(main::HomeDirectorySetting::name).getValue() + - fullPath.substr(1); + fullPath = context->getCurrentSetting(main::HomeDirectorySetting::name) + .getValue() + + fullPath.substr(1); } // Normalize the path to resolve '.' and '..' - std::filesystem::path normalizedPath = std::filesystem::absolute(fullPath).lexically_normal(); + std::filesystem::path normalizedPath = + std::filesystem::absolute(fullPath).lexically_normal(); return normalizedPath.string(); } diff --git a/test/c_api/database_test.cpp b/test/c_api/database_test.cpp index a5eaa2a372..1595cfad3b 100644 --- a/test/c_api/database_test.cpp +++ b/test/c_api/database_test.cpp @@ -1,9 +1,10 @@ +#include + #include "c_api/kuzu.h" +#include "common/exception/io.h" +#include "common/file_system/virtual_file_system.h" #include "graph_test/api_graph_test.h" #include "gtest/gtest.h" -#include "common/file_system/virtual_file_system.h" -#include "common/exception/io.h" -#include using namespace kuzu::main; using namespace kuzu::testing; @@ -115,7 +116,8 @@ TEST_F(CApiDatabaseTest, VirtualFileSystemDeleteFiles) { vfs.removeFileIfExists("/tmp/test1"); } catch (const kuzu::common::IOException& e) { // Expected behavior - EXPECT_STREQ(e.what(), "IO exception: Error: Path /tmp/test1 is not within the allowed home directory /tmp/dbHome/"); + EXPECT_STREQ(e.what(), "IO exception: Error: Path /tmp/test1 is not within the allowed " + "home directory /tmp/dbHome/"); } vfs.removeFileIfExists("/tmp/dbHome/test1"); @@ -140,7 +142,8 @@ TEST_F(CApiDatabaseTest, VirtualFileSystemDeleteFilesWithHome) { vfs.removeFileIfExists("~/tmp/test1"); } catch (const kuzu::common::IOException& e) { // Expected behavior - EXPECT_STREQ(e.what(), "IO exception: Error: Path ~/tmp/test1 is not within the allowed home directory ~/tmp/dbHome/"); + EXPECT_STREQ(e.what(), "IO exception: Error: Path ~/tmp/test1 is not within the allowed " + "home directory ~/tmp/dbHome/"); } // Attempt to delete files outside the home directory (should error) @@ -148,7 +151,8 @@ TEST_F(CApiDatabaseTest, VirtualFileSystemDeleteFilesWithHome) { vfs.removeFileIfExists("~"); } catch (const kuzu::common::IOException& e) { // Expected behavior - EXPECT_STREQ(e.what(), "IO exception: Error: Path ~ is not within the allowed home directory ~/tmp/dbHome/"); + EXPECT_STREQ(e.what(), + "IO exception: Error: Path ~ is not within the allowed home directory ~/tmp/dbHome/"); } vfs.removeFileIfExists("~/tmp/dbHome/test1"); @@ -174,42 +178,48 @@ TEST_F(CApiDatabaseTest, VirtualFileSystemDeleteFilesEdgeCases) { vfs.removeFileIfExists("/tmp/dbHome/../test2"); } catch (const kuzu::common::IOException& e) { // Expected behavior - EXPECT_STREQ(e.what(), "IO exception: Error: Path /tmp/dbHome/../test2 is not within the allowed home directory /tmp/dbHome/"); + EXPECT_STREQ(e.what(), "IO exception: Error: Path /tmp/dbHome/../test2 is not within the " + "allowed home directory /tmp/dbHome/"); } try { vfs.removeFileIfExists("/tmp"); } catch (const kuzu::common::IOException& e) { // Expected behavior - EXPECT_STREQ(e.what(), "IO exception: Error: Path /tmp is not within the allowed home directory /tmp/dbHome/"); + EXPECT_STREQ(e.what(), + "IO exception: Error: Path /tmp is not within the allowed home directory /tmp/dbHome/"); } try { vfs.removeFileIfExists("/tmp/"); } catch (const kuzu::common::IOException& e) { // Expected behavior - EXPECT_STREQ(e.what(), "IO exception: Error: Path /tmp/ is not within the allowed home directory /tmp/dbHome/"); + EXPECT_STREQ(e.what(), "IO exception: Error: Path /tmp/ is not within the allowed home " + "directory /tmp/dbHome/"); } try { vfs.removeFileIfExists("/tmp//////////////////"); } catch (const kuzu::common::IOException& e) { // Expected behavior - EXPECT_STREQ(e.what(), "IO exception: Error: Path /tmp////////////////// is not within the allowed home directory /tmp/dbHome/"); + EXPECT_STREQ(e.what(), "IO exception: Error: Path /tmp////////////////// is not within the " + "allowed home directory /tmp/dbHome/"); } try { vfs.removeFileIfExists("/tmp/./.././"); } catch (const kuzu::common::IOException& e) { // Expected behavior - EXPECT_STREQ(e.what(), "IO exception: Error: Path /tmp/./.././ is not within the allowed home directory /tmp/dbHome/"); + EXPECT_STREQ(e.what(), "IO exception: Error: Path /tmp/./.././ is not within the allowed " + "home directory /tmp/dbHome/"); } try { vfs.removeFileIfExists("/"); } catch (const kuzu::common::IOException& e) { // Expected behavior - EXPECT_STREQ(e.what(), "IO exception: Error: Path / is not within the allowed home directory /tmp/dbHome/"); + EXPECT_STREQ(e.what(), + "IO exception: Error: Path / is not within the allowed home directory /tmp/dbHome/"); } vfs.removeFileIfExists("/tmp/dbHome/test2"); @@ -240,7 +250,8 @@ TEST_F(CApiDatabaseTest, VirtualFileSystemDeleteFilesWindowsPaths) { vfs.removeFileIfExists("C:\\test1"); FAIL() << "Expected exception for path outside home directory."; } catch (const kuzu::common::IOException& e) { - EXPECT_STREQ(e.what(), "IO exception: Error: Path C:\\test1 is not within the allowed home directory C:\\Desktop\\dir"); + EXPECT_STREQ(e.what(), "IO exception: Error: Path C:\\test1 is not within the allowed home " + "directory C:\\Desktop\\dir"); } // Attempt to delete file inside the home directory with mixed separators (should succeed)