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> |
| 8 | #include <fstream> |
| 9 | |
| 10 | namespace crow |
| 11 | { |
| 12 | namespace ibm_utils |
| 13 | { |
| 14 | |
Ed Tanous | 26ccae3 | 2023-02-16 10:28:44 -0800 | [diff] [blame] | 15 | inline bool createDirectory(std::string_view path) |
Sunitha Harish | 3e919b5 | 2020-10-13 01:21:48 -0500 | [diff] [blame] | 16 | { |
| 17 | // Create persistent directory |
| 18 | std::error_code ec; |
| 19 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 20 | BMCWEB_LOG_DEBUG("Creating persistent directory : {}", path); |
Sunitha Harish | 3e919b5 | 2020-10-13 01:21:48 -0500 | [diff] [blame] | 21 | |
| 22 | bool dirCreated = std::filesystem::create_directories(path, ec); |
| 23 | |
| 24 | if (ec) |
| 25 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 26 | BMCWEB_LOG_ERROR("Failed to create persistent directory : {}", path); |
Sunitha Harish | 3e919b5 | 2020-10-13 01:21:48 -0500 | [diff] [blame] | 27 | return false; |
| 28 | } |
| 29 | |
| 30 | if (dirCreated) |
| 31 | { |
| 32 | // set the permission of the directory to 700 |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 33 | BMCWEB_LOG_DEBUG("Setting the permission to 700"); |
Sunitha Harish | 3e919b5 | 2020-10-13 01:21:48 -0500 | [diff] [blame] | 34 | std::filesystem::perms permission = std::filesystem::perms::owner_all; |
| 35 | std::filesystem::permissions(path, permission); |
| 36 | } |
| 37 | else |
| 38 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 39 | BMCWEB_LOG_DEBUG("{} already exists", path); |
Sunitha Harish | 3e919b5 | 2020-10-13 01:21:48 -0500 | [diff] [blame] | 40 | } |
| 41 | return true; |
| 42 | } |
| 43 | |
| 44 | } // namespace ibm_utils |
| 45 | } // namespace crow |