Ed Tanous | 40e9b92 | 2024-09-10 13:50:16 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright OpenBMC Authors |
Sunitha Harish | 3e919b5 | 2020-10-13 01:21:48 -0500 | [diff] [blame] | 3 | #pragma once |
| 4 | |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 5 | #include "logging.hpp" |
Sunitha Harish | 3e919b5 | 2020-10-13 01:21:48 -0500 | [diff] [blame] | 6 | |
| 7 | #include <filesystem> |
Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 8 | #include <string_view> |
| 9 | #include <system_error> |
Sunitha Harish | 3e919b5 | 2020-10-13 01:21:48 -0500 | [diff] [blame] | 10 | |
| 11 | namespace crow |
| 12 | { |
| 13 | namespace ibm_utils |
| 14 | { |
| 15 | |
Ed Tanous | 26ccae3 | 2023-02-16 10:28:44 -0800 | [diff] [blame] | 16 | inline bool createDirectory(std::string_view path) |
Sunitha Harish | 3e919b5 | 2020-10-13 01:21:48 -0500 | [diff] [blame] | 17 | { |
| 18 | // Create persistent directory |
| 19 | std::error_code ec; |
| 20 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 21 | BMCWEB_LOG_DEBUG("Creating persistent directory : {}", path); |
Sunitha Harish | 3e919b5 | 2020-10-13 01:21:48 -0500 | [diff] [blame] | 22 | |
| 23 | bool dirCreated = std::filesystem::create_directories(path, ec); |
| 24 | |
| 25 | if (ec) |
| 26 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 27 | BMCWEB_LOG_ERROR("Failed to create persistent directory : {}", path); |
Sunitha Harish | 3e919b5 | 2020-10-13 01:21:48 -0500 | [diff] [blame] | 28 | return false; |
| 29 | } |
| 30 | |
| 31 | if (dirCreated) |
| 32 | { |
| 33 | // set the permission of the directory to 700 |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 34 | BMCWEB_LOG_DEBUG("Setting the permission to 700"); |
Sunitha Harish | 3e919b5 | 2020-10-13 01:21:48 -0500 | [diff] [blame] | 35 | std::filesystem::perms permission = std::filesystem::perms::owner_all; |
| 36 | std::filesystem::permissions(path, permission); |
| 37 | } |
| 38 | else |
| 39 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 40 | BMCWEB_LOG_DEBUG("{} already exists", path); |
Sunitha Harish | 3e919b5 | 2020-10-13 01:21:48 -0500 | [diff] [blame] | 41 | } |
| 42 | return true; |
| 43 | } |
| 44 | |
| 45 | } // namespace ibm_utils |
| 46 | } // namespace crow |