blob: 31f0a2e8020d51b4b1741a92f9b390a4dcd884b7 [file] [log] [blame]
Sunitha Harish3e919b52020-10-13 01:21:48 -05001#pragma once
2
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08003#include "logging.hpp"
Sunitha Harish3e919b52020-10-13 01:21:48 -05004
5#include <filesystem>
6#include <fstream>
7
8namespace crow
9{
10namespace ibm_utils
11{
12
Ed Tanous26ccae32023-02-16 10:28:44 -080013inline bool createDirectory(std::string_view path)
Sunitha Harish3e919b52020-10-13 01:21:48 -050014{
15 // Create persistent directory
16 std::error_code ec;
17
Ed Tanous62598e32023-07-17 17:06:25 -070018 BMCWEB_LOG_DEBUG("Creating persistent directory : {}", path);
Sunitha Harish3e919b52020-10-13 01:21:48 -050019
20 bool dirCreated = std::filesystem::create_directories(path, ec);
21
22 if (ec)
23 {
Ed Tanous62598e32023-07-17 17:06:25 -070024 BMCWEB_LOG_ERROR("Failed to create persistent directory : {}", path);
Sunitha Harish3e919b52020-10-13 01:21:48 -050025 return false;
26 }
27
28 if (dirCreated)
29 {
30 // set the permission of the directory to 700
Ed Tanous62598e32023-07-17 17:06:25 -070031 BMCWEB_LOG_DEBUG("Setting the permission to 700");
Sunitha Harish3e919b52020-10-13 01:21:48 -050032 std::filesystem::perms permission = std::filesystem::perms::owner_all;
33 std::filesystem::permissions(path, permission);
34 }
35 else
36 {
Ed Tanous62598e32023-07-17 17:06:25 -070037 BMCWEB_LOG_DEBUG("{} already exists", path);
Sunitha Harish3e919b52020-10-13 01:21:48 -050038 }
39 return true;
40}
41
42} // namespace ibm_utils
43} // namespace crow