Skip to content

Commit

Permalink
Fix create dir ending with slash (#3369)
Browse files Browse the repository at this point in the history
  • Loading branch information
acquamarin committed Apr 24, 2024
1 parent 49aa2b7 commit 64574da
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
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

0 comments on commit 64574da

Please sign in to comment.