If path is specified and not empty, will try to setup server environment and load existing metadata
| 472 | |
| 473 | /// If path is specified and not empty, will try to setup server environment and load existing metadata |
| 474 | void LocalServer::tryInitPath() |
| 475 | { |
| 476 | std::string path; |
| 477 | |
| 478 | if (getClientConfiguration().has("path")) |
| 479 | { |
| 480 | /// User-supplied path. |
| 481 | path = getClientConfiguration().getString("path"); |
| 482 | Poco::trimInPlace(path); |
| 483 | |
| 484 | if (path.empty()) |
| 485 | { |
| 486 | throw Exception(ErrorCodes::BAD_ARGUMENTS, |
| 487 | "Cannot work with empty storage path that is explicitly specified" |
| 488 | " by the --path option. Please check the program options and" |
| 489 | " correct the --path."); |
| 490 | } |
| 491 | } |
| 492 | else |
| 493 | { |
| 494 | /// The user requested to use a temporary path - use a unique path in the system temporary directory |
| 495 | /// (or in the current dir if a temporary doesn't exist) |
| 496 | LoggerRawPtr log = &logger(); |
| 497 | std::filesystem::path parent_folder; |
| 498 | std::filesystem::path default_path; |
| 499 | |
| 500 | try |
| 501 | { |
| 502 | /// Try to guess a tmp folder name, and check if it's a directory (throw an exception otherwise). |
| 503 | parent_folder = std::filesystem::temp_directory_path(); |
| 504 | |
| 505 | } |
| 506 | catch (const fs::filesystem_error & e) |
| 507 | { |
| 508 | // The tmp folder doesn't exist? Is it a misconfiguration? Or chroot? |
| 509 | LOG_DEBUG(log, "Can not get temporary folder: {}", e.what()); |
| 510 | parent_folder = std::filesystem::current_path(); |
| 511 | |
| 512 | (void)std::filesystem::is_directory(parent_folder); // checks the path is accessible (may throw on I/O error) |
| 513 | LOG_DEBUG(log, "Will create working directory inside current directory: {}", parent_folder.string()); |
| 514 | } |
| 515 | |
| 516 | /// we can have another clickhouse-local running simultaneously, even with the same PID (for ex. - several dockers mounting the same folder) |
| 517 | /// or it can be some leftovers from other clickhouse-local runs |
| 518 | /// as we can't accurately distinguish those situations we don't touch any existent folders |
| 519 | /// we just try to pick some free name for our working folder |
| 520 | |
| 521 | default_path = parent_folder / fmt::format("clickhouse-local-{}", UUIDHelpers::generateV4()); |
| 522 | |
| 523 | if (fs::exists(default_path)) |
| 524 | throw Exception(ErrorCodes::FILE_ALREADY_EXISTS, "Unsuccessful attempt to set up the working directory: {} already exists.", default_path.string()); |
| 525 | |
| 526 | /// The directory can be created lazily during the runtime. |
| 527 | temporary_directory_to_delete = default_path; |
| 528 | |
| 529 | path = default_path.string(); |
| 530 | LOG_DEBUG(log, "Working directory will be created as needed: {}", path); |
| 531 | } |
nothing calls this directly
no test coverage detected