Matt Spinler | 4e97ebe | 2017-02-28 10:02:16 -0600 | [diff] [blame] | 1 | /** |
Patrick Venture | e84b4dd | 2018-11-01 16:06:31 -0700 | [diff] [blame] | 2 | * Copyright (C) 2017 IBM Corporation |
Matt Spinler | 4e97ebe | 2017-02-28 10:02:16 -0600 | [diff] [blame] | 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 | */ |
Matt Spinler | a231ceb | 2017-10-04 11:26:09 -0500 | [diff] [blame] | 16 | #include "filedescriptor.hpp" |
Matt Spinler | 4e97ebe | 2017-02-28 10:02:16 -0600 | [diff] [blame] | 17 | |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 18 | #include <unistd.h> |
| 19 | |
| 20 | #include <phosphor-logging/elog-errors.hpp> |
| 21 | #include <phosphor-logging/elog.hpp> |
| 22 | #include <stdexcept> |
| 23 | #include <xyz/openbmc_project/Common/File/error.hpp> |
| 24 | |
Matt Spinler | 4e97ebe | 2017-02-28 10:02:16 -0600 | [diff] [blame] | 25 | namespace openpower |
| 26 | { |
Matt Spinler | c3bffed | 2017-03-10 09:05:30 -0600 | [diff] [blame] | 27 | namespace util |
Matt Spinler | 4e97ebe | 2017-02-28 10:02:16 -0600 | [diff] [blame] | 28 | { |
| 29 | |
Matt Spinler | a231ceb | 2017-10-04 11:26:09 -0500 | [diff] [blame] | 30 | namespace file_error = sdbusplus::xyz::openbmc_project::Common::File::Error; |
| 31 | |
Matt Spinler | 4e97ebe | 2017-02-28 10:02:16 -0600 | [diff] [blame] | 32 | FileDescriptor::FileDescriptor(const std::string& path) |
| 33 | { |
Dhruvaraj Subhashchandran | 18b0786 | 2017-04-26 07:13:35 -0500 | [diff] [blame] | 34 | using namespace phosphor::logging; |
| 35 | |
Matt Spinler | 4e97ebe | 2017-02-28 10:02:16 -0600 | [diff] [blame] | 36 | fd = open(path.c_str(), O_RDWR | O_SYNC); |
| 37 | |
| 38 | if (fd < 0) |
| 39 | { |
Matt Spinler | a231ceb | 2017-10-04 11:26:09 -0500 | [diff] [blame] | 40 | using metadata = xyz::openbmc_project::Common::File::Open; |
| 41 | |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 42 | elog<file_error::Open>(metadata::ERRNO(errno), |
| 43 | metadata::PATH(path.c_str())); |
Matt Spinler | 4e97ebe | 2017-02-28 10:02:16 -0600 | [diff] [blame] | 44 | } |
| 45 | } |
| 46 | |
Matt Spinler | 4e97ebe | 2017-02-28 10:02:16 -0600 | [diff] [blame] | 47 | FileDescriptor::~FileDescriptor() |
| 48 | { |
| 49 | if (fd >= 0) |
| 50 | { |
| 51 | close(fd); |
| 52 | } |
| 53 | } |
| 54 | |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 55 | } // namespace util |
| 56 | } // namespace openpower |