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