blob: 2819791fc41564c02f10f5b7c5f75e1c736ce6c6 [file] [log] [blame]
Vishwanatha Subbanna070a3e42017-09-06 11:40:45 +05301#pragma once
2
3#include <stdio.h>
Gunnar Mills703131f2020-10-28 14:26:33 -05004#include <filesystem>
Vishwanatha Subbanna070a3e42017-09-06 11:40:45 +05305namespace phosphor
6{
7namespace user
8{
9
Vishwanatha Subbanna070a3e42017-09-06 11:40:45 +053010/** @class File
11 * @brief Responsible for handling file pointer
12 * Needed by putspent(3)
13 */
14class File
15{
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053016 private:
17 /** @brief handler for operating on file */
18 FILE* fp = NULL;
Vishwanatha Subbanna070a3e42017-09-06 11:40:45 +053019
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053020 /** @brief File name. Needed in the case where the temp
21 * needs to be removed
22 */
23 const std::string& name;
Vishwanatha Subbanna070a3e42017-09-06 11:40:45 +053024
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053025 /** @brief Should the file be removed at exit */
26 bool removeOnExit = false;
Vishwanatha Subbanna070a3e42017-09-06 11:40:45 +053027
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053028 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 Subbanna070a3e42017-09-06 11:40:45 +053034
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053035 /** @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 Subbanna070a3e42017-09-06 11:40:45 +053067 {
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053068 fclose(fp);
Vishwanatha Subbanna070a3e42017-09-06 11:40:45 +053069 }
70
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053071 // Needed for exception safety
Gunnar Mills703131f2020-10-28 14:26:33 -050072 if (removeOnExit && std::filesystem::exists(name))
Richard Marian Thomaiyar1f5a0022017-12-16 15:11:47 +053073 {
Gunnar Mills703131f2020-10-28 14:26:33 -050074 std::filesystem::remove(name);
Richard Marian Thomaiyar1f5a0022017-12-16 15:11:47 +053075 }
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053076 }
Richard Marian Thomaiyar1f5a0022017-12-16 15:11:47 +053077
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053078 auto operator()()
79 {
80 return fp;
81 }
Vishwanatha Subbanna070a3e42017-09-06 11:40:45 +053082};
83
84} // namespace user
85} // namespace phosphor