xref: /openbmc/bmcweb/include/ibm/utils.hpp (revision d78572018fc2022091ff8b8eb5a7fef2172ba3d6)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3 #pragma once
4 
5 #include "logging.hpp"
6 
7 #include <filesystem>
8 #include <string_view>
9 #include <system_error>
10 
11 namespace crow
12 {
13 namespace ibm_utils
14 {
15 
createDirectory(std::string_view path)16 inline bool createDirectory(std::string_view path)
17 {
18     // Create persistent directory
19     std::error_code ec;
20 
21     BMCWEB_LOG_DEBUG("Creating persistent directory : {}", path);
22 
23     bool dirCreated = std::filesystem::create_directories(path, ec);
24 
25     if (ec)
26     {
27         BMCWEB_LOG_ERROR("Failed to create persistent directory : {}", path);
28         return false;
29     }
30 
31     if (dirCreated)
32     {
33         // set the permission of the directory to 700
34         BMCWEB_LOG_DEBUG("Setting the permission to 700");
35         std::filesystem::perms permission = std::filesystem::perms::owner_all;
36         std::filesystem::permissions(path, permission);
37     }
38     else
39     {
40         BMCWEB_LOG_DEBUG("{} already exists", path);
41     }
42     return true;
43 }
44 
45 } // namespace ibm_utils
46 } // namespace crow
47