Newer
Older
Marc Vef
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
Copyright 2018-2020, Barcelona Supercomputing Center (BSC), Spain
Copyright 2015-2020, Johannes Gutenberg Universitaet Mainz, Germany
This software was partially supported by the
EC H2020 funded project NEXTGenIO (Project ID: 671951, www.nextgenio.eu).
This software was partially supported by the
ADA-FS project under the SPPEXA project funded by the DFG.
SPDX-License-Identifier: MIT
*/
#ifndef GEKKOFS_DAEMON_FILE_HANDLE_HPP
#define GEKKOFS_DAEMON_FILE_HANDLE_HPP
#include <daemon/backend/data/data_module.hpp>
extern "C" {
#include <unistd.h>
}
namespace gkfs {
namespace data {
/**
* File handle to encapsulate a file descriptor, allowing RAII closing of the file descriptor
*/
class FileHandle {
private:
constexpr static const int init_value{-1};
int fd_{init_value};
std::string path_{};
public:
FileHandle() = default;
explicit FileHandle(int fd, std::string path) noexcept :
fd_(fd) {}
FileHandle(FileHandle&& rhs) = default;
FileHandle(const FileHandle& other) = delete;
FileHandle& operator=(FileHandle&& rhs) = default;
FileHandle& operator=(const FileHandle& other) = delete;
explicit operator bool() const noexcept {
return valid();
}
bool operator!() const noexcept {
return !valid();
}
bool
valid() const noexcept {
return fd_ != init_value;
}
int
native() const noexcept {
return fd_;
}
/**
* Closes file descriptor and resets it to initial value
* @return
*/
bool close() noexcept {
if (fd_ != init_value) {
if (::close(fd_) < 0) {
GKFS_DATA_MOD->log()->warn("{}() Failed to close file descriptor '{}' path '{}' errno '{}'", __func__,
fd_, path_, ::strerror(errno));
return false;
}
}
fd_ = init_value;
return true;
}
~FileHandle() {
if (fd_ != init_value)
close();
}
};
} // namespace data
} // namespace gkfs
#endif //GEKKOFS_DAEMON_FILE_HANDLE_HPP