Vishwanatha Subbanna | 070a3e4 | 2017-09-06 11:40:45 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <stdio.h> |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 4 | |
Gunnar Mills | 703131f | 2020-10-28 14:26:33 -0500 | [diff] [blame] | 5 | #include <filesystem> |
Vishwanatha Subbanna | 070a3e4 | 2017-09-06 11:40:45 +0530 | [diff] [blame] | 6 | namespace phosphor |
| 7 | { |
| 8 | namespace user |
| 9 | { |
| 10 | |
Vishwanatha Subbanna | 070a3e4 | 2017-09-06 11:40:45 +0530 | [diff] [blame] | 11 | /** @class File |
| 12 | * @brief Responsible for handling file pointer |
| 13 | * Needed by putspent(3) |
| 14 | */ |
| 15 | class File |
| 16 | { |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 17 | private: |
| 18 | /** @brief handler for operating on file */ |
| 19 | FILE* fp = NULL; |
Vishwanatha Subbanna | 070a3e4 | 2017-09-06 11:40:45 +0530 | [diff] [blame] | 20 | |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 21 | /** @brief File name. Needed in the case where the temp |
| 22 | * needs to be removed |
| 23 | */ |
| 24 | const std::string& name; |
Vishwanatha Subbanna | 070a3e4 | 2017-09-06 11:40:45 +0530 | [diff] [blame] | 25 | |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 26 | /** @brief Should the file be removed at exit */ |
| 27 | bool removeOnExit = false; |
Vishwanatha Subbanna | 070a3e4 | 2017-09-06 11:40:45 +0530 | [diff] [blame] | 28 | |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 29 | public: |
| 30 | File() = delete; |
| 31 | File(const File&) = delete; |
| 32 | File& operator=(const File&) = delete; |
| 33 | File(File&&) = delete; |
| 34 | File& operator=(File&&) = delete; |
Vishwanatha Subbanna | 070a3e4 | 2017-09-06 11:40:45 +0530 | [diff] [blame] | 35 | |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 36 | /** @brief Opens file and uses it to do file operation |
| 37 | * |
| 38 | * @param[in] name - File name |
| 39 | * @param[in] mode - File open mode |
| 40 | * @param[in] removeOnExit - File to be removed at exit or no |
| 41 | */ |
| 42 | File(const std::string& name, const std::string& mode, |
Patrick Williams | 16c2b68 | 2024-08-16 15:20:56 -0400 | [diff] [blame^] | 43 | bool removeOnExit = false) : name(name), removeOnExit(removeOnExit) |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 44 | { |
| 45 | fp = fopen(name.c_str(), mode.c_str()); |
| 46 | } |
| 47 | |
| 48 | /** @brief Opens file using provided file descriptor |
| 49 | * |
| 50 | * @param[in] fd - File descriptor |
| 51 | * @param[in] name - File name |
| 52 | * @param[in] mode - File open mode |
| 53 | * @param[in] removeOnExit - File to be removed at exit or no |
| 54 | */ |
| 55 | File(int fd, const std::string& name, const std::string& mode, |
Patrick Williams | 16c2b68 | 2024-08-16 15:20:56 -0400 | [diff] [blame^] | 56 | bool removeOnExit = false) : name(name), removeOnExit(removeOnExit) |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 57 | { |
| 58 | fp = fdopen(fd, mode.c_str()); |
| 59 | } |
| 60 | |
| 61 | ~File() |
| 62 | { |
| 63 | if (fp) |
Vishwanatha Subbanna | 070a3e4 | 2017-09-06 11:40:45 +0530 | [diff] [blame] | 64 | { |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 65 | fclose(fp); |
Vishwanatha Subbanna | 070a3e4 | 2017-09-06 11:40:45 +0530 | [diff] [blame] | 66 | } |
| 67 | |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 68 | // Needed for exception safety |
Gunnar Mills | 703131f | 2020-10-28 14:26:33 -0500 | [diff] [blame] | 69 | if (removeOnExit && std::filesystem::exists(name)) |
Richard Marian Thomaiyar | 1f5a002 | 2017-12-16 15:11:47 +0530 | [diff] [blame] | 70 | { |
Gunnar Mills | 703131f | 2020-10-28 14:26:33 -0500 | [diff] [blame] | 71 | std::filesystem::remove(name); |
Richard Marian Thomaiyar | 1f5a002 | 2017-12-16 15:11:47 +0530 | [diff] [blame] | 72 | } |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 73 | } |
Richard Marian Thomaiyar | 1f5a002 | 2017-12-16 15:11:47 +0530 | [diff] [blame] | 74 | |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 75 | auto operator()() |
| 76 | { |
| 77 | return fp; |
| 78 | } |
Vishwanatha Subbanna | 070a3e4 | 2017-09-06 11:40:45 +0530 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | } // namespace user |
| 82 | } // namespace phosphor |