diff options
author | AppaRao Puli <apparao.puli@intel.com> | 2018-05-17 10:28:48 +0530 |
---|---|---|
committer | Richard Marian Thomaiyar <richard.marian.thomaiyar@linux.intel.com> | 2018-11-13 11:26:09 +0530 |
commit | b29b5ab3b9ad812949f3621a4422fde8d7c1c8d2 (patch) | |
tree | e25e81565b04c8513af25e3ad7376a03a685cdbb /user_channel/file.hpp | |
parent | 4654d99fda30800024c56b085ec9dcf2ac26c417 (diff) | |
download | phosphor-host-ipmid-b29b5ab3b9ad812949f3621a4422fde8d7c1c8d2.tar.gz phosphor-host-ipmid-b29b5ab3b9ad812949f3621a4422fde8d7c1c8d2.zip |
Handling delete password entry from ipmi-pass
API to Handle the delete password entry from
ipmi-pass encrypted file when user gets deleted
by any interface
Change-Id: I692a81b166b53d6fc981fdb85ce5d6980887560b
Signed-off-by: AppaRao Puli <apparao.puli@intel.com>
Signed-off-by: Richard Marian Thomaiyar <richard.marian.thomaiyar@linux.intel.com>
Diffstat (limited to 'user_channel/file.hpp')
-rw-r--r-- | user_channel/file.hpp | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/user_channel/file.hpp b/user_channel/file.hpp new file mode 100644 index 0000000..2c537b1 --- /dev/null +++ b/user_channel/file.hpp @@ -0,0 +1,88 @@ +#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()); + } + + /** @brief Opens file using provided file descriptor + * + * @param[in] fd - File descriptor + * @param[in] name - File name + * @param[in] mode - File open mode + * @param[in] removeOnExit - File to be removed at exit or no + */ + File(int fd, const std::string& name, const std::string& mode, + bool removeOnExit = false) : + name(name), + removeOnExit(removeOnExit) + { + fp = fdopen(fd, 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 |