Shawn McCarney | de5434d | 2024-05-22 14:07:44 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2024 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "temporary_subdirectory.hpp" |
| 18 | |
| 19 | #include <errno.h> // for errno |
| 20 | #include <stdlib.h> // for mkdtemp() |
| 21 | #include <string.h> // for strerror() |
| 22 | |
| 23 | #include <stdexcept> |
| 24 | #include <string> |
| 25 | |
| 26 | namespace phosphor::power::util |
| 27 | { |
| 28 | |
| 29 | namespace fs = std::filesystem; |
| 30 | |
| 31 | TemporarySubDirectory::TemporarySubDirectory() |
| 32 | { |
| 33 | // Build template path required by mkdtemp() |
Patrick Williams | f540219 | 2024-08-16 15:20:53 -0400 | [diff] [blame^] | 34 | std::string templatePath = |
| 35 | fs::temp_directory_path() / "phosphor-power-XXXXXX"; |
Shawn McCarney | de5434d | 2024-05-22 14:07:44 -0500 | [diff] [blame] | 36 | |
| 37 | // Generate unique subdirectory name and create it. The XXXXXX characters |
| 38 | // are replaced by mkdtemp() to make the subdirectory name unique. |
| 39 | char* retVal = mkdtemp(templatePath.data()); |
| 40 | if (retVal == nullptr) |
| 41 | { |
| 42 | throw std::runtime_error{ |
| 43 | std::string{"Unable to create temporary subdirectory: "} + |
| 44 | strerror(errno)}; |
| 45 | } |
| 46 | |
| 47 | // Store path to temporary subdirectory |
| 48 | path = templatePath; |
| 49 | } |
| 50 | |
| 51 | TemporarySubDirectory& |
| 52 | TemporarySubDirectory::operator=(TemporarySubDirectory&& subdirectory) |
| 53 | { |
| 54 | // Verify not assigning object to itself (a = std::move(a)) |
| 55 | if (this != &subdirectory) |
| 56 | { |
| 57 | // Delete temporary subdirectory owned by this object |
| 58 | remove(); |
| 59 | |
| 60 | // Move subdirectory path from other object, transferring ownership |
| 61 | path = std::move(subdirectory.path); |
| 62 | |
| 63 | // Clear path in other object; after move path is in unspecified state |
| 64 | subdirectory.path.clear(); |
| 65 | } |
| 66 | return *this; |
| 67 | } |
| 68 | |
| 69 | void TemporarySubDirectory::remove() |
| 70 | { |
| 71 | if (!path.empty()) |
| 72 | { |
| 73 | // Delete temporary subdirectory from file system |
| 74 | fs::remove_all(path); |
| 75 | |
| 76 | // Clear path to indicate subdirectory has been deleted |
| 77 | path.clear(); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | } // namespace phosphor::power::util |