ChimeraTK-DeviceAccess  03.18.00
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 #include <sys/stat.h>
8 
9 #include <cstring>
10 #include <fcntl.h>
11 #include <iostream>
12 #include <utility>
13 
14 namespace ChimeraTK {
15 
16  DeviceFile::DeviceFile(const std::string& deviceFilePath, int flags)
17  : _path{deviceFilePath}, _fd{::open(_path.c_str(), flags)} {
18 #ifdef _DEBUG
19  std::cout << "XDMA: opening device file " << _path << std::endl;
20 #endif
21  if(_fd < 0) {
22  throw runtime_error(_strerror("Cannot open device: "));
23  }
24  }
25 
26  DeviceFile::DeviceFile(DeviceFile&& d) : _path(std::move(d._path)), _fd(std::exchange(d._fd, 0)) {}
27 
29  if(_fd > 0) {
30 #ifdef _DEBUG
31  std::cout << "XDMA: closing device file " << _path << std::endl;
32 #endif
33  ::close(_fd);
34  }
35  }
36 
37  std::string DeviceFile::_strerror(const std::string& msg) const {
38  char tmp[255];
39  return msg + _path + ": " + ::strerror_r(errno, tmp, sizeof(tmp));
40  }
41 
42  DeviceFile::operator int() const {
43  return _fd;
44  }
45 
46  std::string DeviceFile::name() const {
47  return _path;
48  }
49 
50  bool DeviceFile::goodState() const {
51  struct stat s {};
52  if(fstat(_fd, &s) != 0) {
53  return false;
54  }
55  // check whether device file was deleted since opened
56  return s.st_nlink > 0;
57  }
58 
59 } // namespace ChimeraTK
ChimeraTK::DeviceFile::DeviceFile
DeviceFile()=delete
DeviceFile.h
ChimeraTK::DeviceFile::goodState
bool goodState() const
Definition: DeviceFile.cc:50
ChimeraTK::DeviceFile::name
std::string name() const
Definition: DeviceFile.cc:46
ChimeraTK::DeviceFile
Definition: DeviceFile.h:12
Exception.h
ChimeraTK
Definition: DummyBackend.h:16
ChimeraTK::DeviceFile::~DeviceFile
virtual ~DeviceFile()
Definition: DeviceFile.cc:28