blob: 068027ffe683a974cd3e18386ad7d10c1248bd04 [file] [log] [blame]
Matt Spinler4e97ebe2017-02-28 10:02:16 -06001/**
Patrick Venturee84b4dd2018-11-01 16:06:31 -07002 * Copyright (C) 2017 IBM Corporation
Matt Spinler4e97ebe2017-02-28 10:02:16 -06003 *
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 Spinlera231ceb2017-10-04 11:26:09 -050016#include "filedescriptor.hpp"
Matt Spinler4e97ebe2017-02-28 10:02:16 -060017
Patrick Venturef78d9042018-11-01 15:39:53 -070018#include <unistd.h>
19
20#include <phosphor-logging/elog-errors.hpp>
21#include <phosphor-logging/elog.hpp>
Patrick Venturef78d9042018-11-01 15:39:53 -070022#include <xyz/openbmc_project/Common/File/error.hpp>
23
Brad Bishop5e5d4452020-10-27 19:46:13 -040024#include <stdexcept>
25
Matt Spinler4e97ebe2017-02-28 10:02:16 -060026namespace openpower
27{
Matt Spinlerc3bffed2017-03-10 09:05:30 -060028namespace util
Matt Spinler4e97ebe2017-02-28 10:02:16 -060029{
30
Matt Spinlera231ceb2017-10-04 11:26:09 -050031namespace file_error = sdbusplus::xyz::openbmc_project::Common::File::Error;
32
Matt Spinler4e97ebe2017-02-28 10:02:16 -060033FileDescriptor::FileDescriptor(const std::string& path)
34{
Dhruvaraj Subhashchandran18b07862017-04-26 07:13:35 -050035 using namespace phosphor::logging;
36
Matt Spinler4e97ebe2017-02-28 10:02:16 -060037 fd = open(path.c_str(), O_RDWR | O_SYNC);
38
39 if (fd < 0)
40 {
Matt Spinlera231ceb2017-10-04 11:26:09 -050041 using metadata = xyz::openbmc_project::Common::File::Open;
42
Patrick Venturef78d9042018-11-01 15:39:53 -070043 elog<file_error::Open>(metadata::ERRNO(errno),
44 metadata::PATH(path.c_str()));
Matt Spinler4e97ebe2017-02-28 10:02:16 -060045 }
46}
47
Matt Spinler4e97ebe2017-02-28 10:02:16 -060048FileDescriptor::~FileDescriptor()
49{
50 if (fd >= 0)
51 {
52 close(fd);
53 }
54}
55
Patrick Venturef78d9042018-11-01 15:39:53 -070056} // namespace util
57} // namespace openpower