ChimeraTK-DeviceAccess 03.25.00
Loading...
Searching...
No Matches
DeviceFile.cc
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
4#include "DeviceFile.h"
5
6#include "Exception.h"
7
8#include <sys/stat.h>
9
10#include <fcntl.h>
11
12#include <cstring>
13#include <iostream>
14#include <utility>
15
16namespace ChimeraTK {
17
18 DeviceFile::DeviceFile(const std::string& deviceFilePath, int flags)
19 : _path{deviceFilePath}, _fd{::open(_path.c_str(), flags)} {
20#ifdef _DEBUG
21 std::cout << "XDMA: opening device file " << _path << std::endl;
22#endif
23 if(_fd < 0) {
24 throw runtime_error(_strerror("Cannot open device: "));
25 }
26 }
27
28 DeviceFile::DeviceFile(DeviceFile&& d) : _path(std::move(d._path)), _fd(std::exchange(d._fd, 0)) {}
29
31 if(_fd > 0) {
32#ifdef _DEBUG
33 std::cout << "XDMA: closing device file " << _path << std::endl;
34#endif
35 ::close(_fd);
36 }
37 }
38
39 std::string DeviceFile::_strerror(const std::string& msg) const {
40 char tmp[255];
41 return msg + _path + ": " + ::strerror_r(errno, tmp, sizeof(tmp));
42 }
43
44 DeviceFile::operator int() const {
45 return _fd;
46 }
47
48 std::string DeviceFile::name() const {
49 return _path;
50 }
51
52 bool DeviceFile::goodState() const {
53 struct stat s{};
54 if(fstat(_fd, &s) != 0) {
55 return false;
56 }
57 // check whether device file was deleted since opened
58 return s.st_nlink > 0;
59 }
60
61} // namespace ChimeraTK
bool goodState() const
Definition DeviceFile.cc:52
std::string name() const
Definition DeviceFile.cc:48
Exception thrown when a runtime error has occured.
Definition Exception.h:18
STL namespace.