xref: /openbmc/openpower-hw-diags/util/ffdc_file.cpp (revision a0c724d3d425032213dbd48247d93cc76d61a331)
16e36587aSZane Shelley #include "util/ffdc_file.hpp"
26e36587aSZane Shelley 
36e36587aSZane Shelley #include <errno.h>     // for errno
46e36587aSZane Shelley #include <fcntl.h>     // for open()
56e36587aSZane Shelley #include <string.h>    // for strerror()
66e36587aSZane Shelley #include <sys/stat.h>  // for open()
76e36587aSZane Shelley #include <sys/types.h> // for open()
86e36587aSZane Shelley 
96e36587aSZane Shelley #include <stdexcept>
106e36587aSZane Shelley #include <string>
116e36587aSZane Shelley 
126e36587aSZane Shelley namespace util
136e36587aSZane Shelley {
146e36587aSZane Shelley 
FFDCFile(FFDCFormat format,uint8_t subType,uint8_t version)156e36587aSZane Shelley FFDCFile::FFDCFile(FFDCFormat format, uint8_t subType, uint8_t version) :
166e36587aSZane Shelley     format{format}, subType{subType}, version{version}
176e36587aSZane Shelley {
186e36587aSZane Shelley     // Open the temporary file for both reading and writing
196e36587aSZane Shelley     int fd = open(tempFile.getPath().c_str(), O_RDWR);
206e36587aSZane Shelley     if (fd == -1)
216e36587aSZane Shelley     {
22*a0c724d3SPatrick Williams         throw std::runtime_error{
23*a0c724d3SPatrick Williams             std::string{"Unable to open FFDC file: "} + strerror(errno)};
246e36587aSZane Shelley     }
256e36587aSZane Shelley 
266e36587aSZane Shelley     // Store file descriptor in FileDescriptor object
276e36587aSZane Shelley     descriptor.set(fd);
286e36587aSZane Shelley }
296e36587aSZane Shelley 
remove()306e36587aSZane Shelley void FFDCFile::remove()
316e36587aSZane Shelley {
326e36587aSZane Shelley     // Close file descriptor.  Does nothing if descriptor was already closed.
336e36587aSZane Shelley     // Returns -1 if close failed.
346e36587aSZane Shelley     if (descriptor.close() == -1)
356e36587aSZane Shelley     {
36*a0c724d3SPatrick Williams         throw std::runtime_error{
37*a0c724d3SPatrick Williams             std::string{"Unable to close FFDC file: "} + strerror(errno)};
386e36587aSZane Shelley     }
396e36587aSZane Shelley 
406e36587aSZane Shelley     // Delete temporary file.  Does nothing if file was already deleted.
416e36587aSZane Shelley     // Throws an exception if the deletion failed.
426e36587aSZane Shelley     tempFile.remove();
436e36587aSZane Shelley }
446e36587aSZane Shelley 
456e36587aSZane Shelley } // namespace util
46