Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix create dir ending with slash #3369

Merged
merged 4 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/common/file_system/local_file_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,25 @@ void LocalFileSystem::createDir(const std::string& dir) const {
throw IOException(stringFormat("Directory {} already exists.", dir));
// LCOV_EXCL_STOP
}
if (!std::filesystem::create_directories(dir)) {
auto directoryToCreate = dir;
if (directoryToCreate.ends_with('/')) {
// This is a known issue with std::filesystem::create_directories. (link:
// https://github.com/llvm/llvm-project/issues/60634). We have to manually remove the
// last '/' if the path ends with '/'.
directoryToCreate = directoryToCreate.substr(0, directoryToCreate.size() - 1);
}
std::error_code errCode;
if (!std::filesystem::create_directories(directoryToCreate, errCode)) {
// LCOV_EXCL_START
throw IOException(
stringFormat("Directory {} cannot be created. Check if it exists and remove it.",
directoryToCreate));
// LCOV_EXCL_STOP
}
if (errCode) {
// LCOV_EXCL_START
throw IOException(stringFormat(
"Directory {} cannot be created. Check if it exists and remove it.", dir));
throw IOException(stringFormat("Failed to create directory: {}, error message: {}.",
dir, errCode.message()));
// LCOV_EXCL_STOP
}
} catch (std::exception& e) {
Expand Down
7 changes: 6 additions & 1 deletion test/c_api/database_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,13 @@ TEST_F(CApiDatabaseTest, CreationInvalidPath) {
}

TEST_F(CApiDatabaseTest, CreationHomeDir) {
auto databasePathCStr = (char*)"~/ku_test.db";
auto databasePathCStr = (char*)"~/ku_test.db/";
auto database = kuzu_database_init(databasePathCStr, kuzu_default_system_config());
auto connection = kuzu_connection_init(database);
auto homePath =
getClientContext(*(Connection*)(connection->_connection))->getClientConfig()->homeDirectory;
ASSERT_NE(database, nullptr);
kuzu_connection_destroy(connection);
kuzu_database_destroy(database);
std::filesystem::remove_all(homePath + "/ku_test.db");
}
6 changes: 2 additions & 4 deletions tools/python_api/test/test_exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ def test_exception(conn_db_readonly: ConnDB) -> None:

def test_db_path_exception() -> None:
path = ""
if sys.platform == "win32":
error_message = "Failed to create directory"
else:
error_message = "filesystem error"
error_message = ("IO exception: Failed to create directory due to: IO exception: Directory cannot be created. "
"Check if it exists and remove it.")
with pytest.raises(RuntimeError, match=error_message):
kuzu.Database(path)

Expand Down
Loading