blob: 7f528d649161a7348390f218299551d55f0464ac [file] [log] [blame]
Shawn McCarneyf3633f62020-09-02 10:34:52 -05001/**
2 * Copyright © 2020 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "ffdc_file.hpp"
18
19#include <errno.h> // for errno
20#include <fcntl.h> // for open()
21#include <string.h> // for strerror()
22#include <sys/stat.h> // for open()
23#include <sys/types.h> // for open()
24
25#include <stdexcept>
26#include <string>
27
28namespace phosphor::power::regulators
29{
30
31FFDCFile::FFDCFile(FFDCFormat format, uint8_t subType, uint8_t version) :
32 format{format}, subType{subType}, version{version}
33{
34 // Open the temporary file for both reading and writing
35 int fd = open(tempFile.getPath().c_str(), O_RDWR);
36 if (fd == -1)
37 {
38 throw std::runtime_error{std::string{"Unable to open FFDC file: "} +
39 strerror(errno)};
40 }
41
42 // Store file descriptor in FileDescriptor object
43 descriptor.set(fd);
44}
45
46void FFDCFile::remove()
47{
48 // Close file descriptor. Does nothing if descriptor was already closed.
49 // Returns -1 if close failed.
50 if (descriptor.close() == -1)
51 {
52 throw std::runtime_error{std::string{"Unable to close FFDC file: "} +
53 strerror(errno)};
54 }
55
56 // Delete temporary file. Does nothing if file was already deleted.
57 // Throws an exception if the deletion failed.
58 tempFile.remove();
59}
60
61} // namespace phosphor::power::regulators