blob: 43be7f6e8e5de60dab04b0bec3d46c2444c6e6b8 [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
Sunitha Harish3e919b52020-10-13 01:21:48 -05003#pragma once
4
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08005#include "logging.hpp"
Sunitha Harish3e919b52020-10-13 01:21:48 -05006
7#include <filesystem>
8#include <fstream>
9
10namespace crow
11{
12namespace ibm_utils
13{
14
Ed Tanous26ccae32023-02-16 10:28:44 -080015inline bool createDirectory(std::string_view path)
Sunitha Harish3e919b52020-10-13 01:21:48 -050016{
17 // Create persistent directory
18 std::error_code ec;
19
Ed Tanous62598e32023-07-17 17:06:25 -070020 BMCWEB_LOG_DEBUG("Creating persistent directory : {}", path);
Sunitha Harish3e919b52020-10-13 01:21:48 -050021
22 bool dirCreated = std::filesystem::create_directories(path, ec);
23
24 if (ec)
25 {
Ed Tanous62598e32023-07-17 17:06:25 -070026 BMCWEB_LOG_ERROR("Failed to create persistent directory : {}", path);
Sunitha Harish3e919b52020-10-13 01:21:48 -050027 return false;
28 }
29
30 if (dirCreated)
31 {
32 // set the permission of the directory to 700
Ed Tanous62598e32023-07-17 17:06:25 -070033 BMCWEB_LOG_DEBUG("Setting the permission to 700");
Sunitha Harish3e919b52020-10-13 01:21:48 -050034 std::filesystem::perms permission = std::filesystem::perms::owner_all;
35 std::filesystem::permissions(path, permission);
36 }
37 else
38 {
Ed Tanous62598e32023-07-17 17:06:25 -070039 BMCWEB_LOG_DEBUG("{} already exists", path);
Sunitha Harish3e919b52020-10-13 01:21:48 -050040 }
41 return true;
42}
43
44} // namespace ibm_utils
45} // namespace crow