1 #include "util/ffdc_file.hpp"
2 
3 #include <errno.h>     // for errno
4 #include <fcntl.h>     // for open()
5 #include <string.h>    // for strerror()
6 #include <sys/stat.h>  // for open()
7 #include <sys/types.h> // for open()
8 
9 #include <stdexcept>
10 #include <string>
11 
12 namespace util
13 {
14 
FFDCFile(FFDCFormat format,uint8_t subType,uint8_t version)15 FFDCFile::FFDCFile(FFDCFormat format, uint8_t subType, uint8_t version) :
16     format{format}, subType{subType}, version{version}
17 {
18     // Open the temporary file for both reading and writing
19     int fd = open(tempFile.getPath().c_str(), O_RDWR);
20     if (fd == -1)
21     {
22         throw std::runtime_error{
23             std::string{"Unable to open FFDC file: "} + strerror(errno)};
24     }
25 
26     // Store file descriptor in FileDescriptor object
27     descriptor.set(fd);
28 }
29 
remove()30 void FFDCFile::remove()
31 {
32     // Close file descriptor.  Does nothing if descriptor was already closed.
33     // Returns -1 if close failed.
34     if (descriptor.close() == -1)
35     {
36         throw std::runtime_error{
37             std::string{"Unable to close FFDC file: "} + strerror(errno)};
38     }
39 
40     // Delete temporary file.  Does nothing if file was already deleted.
41     // Throws an exception if the deletion failed.
42     tempFile.remove();
43 }
44 
45 } // namespace util
46