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