summaryrefslogtreecommitdiffstats
path: root/file.hpp
diff options
context:
space:
mode:
authorVishwanatha Subbanna <vishwa@linux.vnet.ibm.com>2017-09-06 11:40:45 +0530
committerVishwanatha Subbanna <vishwa@linux.vnet.ibm.com>2017-10-12 23:36:37 +0530
commit070a3e49fdd8a6ecd5c71f6ee018ebe89ccd7a1f (patch)
tree7de0f5d48d4e408472194e369395ef563053eab5 /file.hpp
parentbdb298f744cc71421f0c2e6f50254f933c6bb3cc (diff)
downloadphosphor-user-manager-070a3e49fdd8a6ecd5c71f6ee018ebe89ccd7a1f.tar.gz
phosphor-user-manager-070a3e49fdd8a6ecd5c71f6ee018ebe89ccd7a1f.zip
Update shadow password file with new password
Change-Id: Ida7c1aba6f17ac6f006f159d08e2638808f3a54c Signed-off-by: Vishwanatha Subbanna <vishwa@linux.vnet.ibm.com>
Diffstat (limited to 'file.hpp')
-rw-r--r--file.hpp73
1 files changed, 73 insertions, 0 deletions
diff --git a/file.hpp b/file.hpp
new file mode 100644
index 0000000..ae5cf85
--- /dev/null
+++ b/file.hpp
@@ -0,0 +1,73 @@
+#pragma once
+
+#include <stdio.h>
+#include <experimental/filesystem>
+namespace phosphor
+{
+namespace user
+{
+
+namespace fs = std::experimental::filesystem;
+
+/** @class File
+ * @brief Responsible for handling file pointer
+ * Needed by putspent(3)
+ */
+class File
+{
+ private:
+ /** @brief handler for operating on file */
+ FILE *fp = NULL;
+
+ /** @brief File name. Needed in the case where the temp
+ * needs to be removed
+ */
+ const std::string& name;
+
+ /** @brief Should the file be removed at exit */
+ bool removeOnExit = false;
+
+ public:
+ File() = delete;
+ File(const File&) = delete;
+ File& operator=(const File&) = delete;
+ File(File&&) = delete;
+ File& operator=(File&&) = delete;
+
+ /** @brief Opens file and uses it to do file operation
+ *
+ * @param[in] name - File name
+ * @param[in] mode - File open mode
+ * @param[in] removeOnExit - File to be removed at exit or no
+ */
+ File(const std::string& name,
+ const std::string& mode,
+ bool removeOnExit = false) :
+ name(name),
+ removeOnExit(removeOnExit)
+ {
+ fp = fopen(name.c_str(), mode.c_str());
+ }
+
+ ~File()
+ {
+ if (fp)
+ {
+ fclose(fp);
+ }
+
+ // Needed for exception safety
+ if (removeOnExit && fs::exists(name))
+ {
+ fs::remove(name);
+ }
+ }
+
+ auto operator()()
+ {
+ return fp;
+ }
+};
+
+} // namespace user
+} // namespace phosphor
OpenPOWER on IntegriCloud