blob: aaf10629ad1ec15f0ca68888d404317910dc64f1 [file] [log] [blame]
Vishwanatha Subbanna070a3e42017-09-06 11:40:45 +05301#pragma once
2
3#include <stdio.h>
Patrick Williams9638afb2021-02-22 17:16:24 -06004
Gunnar Mills703131f2020-10-28 14:26:33 -05005#include <filesystem>
Vishwanatha Subbanna070a3e42017-09-06 11:40:45 +05306namespace phosphor
7{
8namespace user
9{
10
Vishwanatha Subbanna070a3e42017-09-06 11:40:45 +053011/** @class File
12 * @brief Responsible for handling file pointer
13 * Needed by putspent(3)
14 */
15class File
16{
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053017 private:
18 /** @brief handler for operating on file */
19 FILE* fp = NULL;
Vishwanatha Subbanna070a3e42017-09-06 11:40:45 +053020
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053021 /** @brief File name. Needed in the case where the temp
22 * needs to be removed
23 */
24 const std::string& name;
Vishwanatha Subbanna070a3e42017-09-06 11:40:45 +053025
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053026 /** @brief Should the file be removed at exit */
27 bool removeOnExit = false;
Vishwanatha Subbanna070a3e42017-09-06 11:40:45 +053028
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053029 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 Subbanna070a3e42017-09-06 11:40:45 +053035
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053036 /** @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,
43 bool removeOnExit = false) :
44 name(name),
45 removeOnExit(removeOnExit)
46 {
47 fp = fopen(name.c_str(), mode.c_str());
48 }
49
50 /** @brief Opens file using provided file descriptor
51 *
52 * @param[in] fd - File descriptor
53 * @param[in] name - File name
54 * @param[in] mode - File open mode
55 * @param[in] removeOnExit - File to be removed at exit or no
56 */
57 File(int fd, const std::string& name, const std::string& mode,
58 bool removeOnExit = false) :
59 name(name),
60 removeOnExit(removeOnExit)
61 {
62 fp = fdopen(fd, mode.c_str());
63 }
64
65 ~File()
66 {
67 if (fp)
Vishwanatha Subbanna070a3e42017-09-06 11:40:45 +053068 {
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053069 fclose(fp);
Vishwanatha Subbanna070a3e42017-09-06 11:40:45 +053070 }
71
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053072 // Needed for exception safety
Gunnar Mills703131f2020-10-28 14:26:33 -050073 if (removeOnExit && std::filesystem::exists(name))
Richard Marian Thomaiyar1f5a0022017-12-16 15:11:47 +053074 {
Gunnar Mills703131f2020-10-28 14:26:33 -050075 std::filesystem::remove(name);
Richard Marian Thomaiyar1f5a0022017-12-16 15:11:47 +053076 }
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053077 }
Richard Marian Thomaiyar1f5a0022017-12-16 15:11:47 +053078
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053079 auto operator()()
80 {
81 return fp;
82 }
Vishwanatha Subbanna070a3e42017-09-06 11:40:45 +053083};
84
85} // namespace user
86} // namespace phosphor