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