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