blob: a067e0b9a383274866cdc5d0101fe98a3c893fe6 [file] [log] [blame]
Zane Shelley6e365872020-10-23 22:00:05 -05001#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
12namespace util
13{
14
15FFDCFile::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{std::string{"Unable to open FFDC file: "} +
23 strerror(errno)};
24 }
25
26 // Store file descriptor in FileDescriptor object
27 descriptor.set(fd);
28}
29
30void 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{std::string{"Unable to close FFDC file: "} +
37 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