-
Notifications
You must be signed in to change notification settings - Fork 1
/
file_descriptor.hpp
100 lines (85 loc) · 1.98 KB
/
file_descriptor.hpp
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
100
#ifndef FLUKS_FILE_DESCRIPTOR_HPP
#define FLUKS_FILE_DESCRIPTOR_HPP
namespace fluks {
class File_descriptor {
public:
File_descriptor(int fd) noexcept : _fd(fd) {}
File_descriptor(File_descriptor &&rhs) noexcept : _fd(rhs._fd) {
rhs._fd = -1;
}
~File_descriptor() noexcept {
try {
close();
} catch (...) {}
}
/** \throw std::system_error */
File_descriptor &operator=(File_descriptor &&rhs) {
close();
_fd = rhs._fd;
rhs._fd = -1;
return *this;
}
/** \throw std::system_error */
void close() {
if (_fd == -1) return;
if (::close(_fd) == -1)
throw_errno(errno);
_fd = -1;
}
/** \throw std::system_error */
size_t read(void *buf, size_t bytes) {
if (_fd == -1)
throw std::runtime_exception("illegal state");
ssize_t rbytes = ::read(_fd, buf, bytes);
if (rbytes < 0)
throw_errno(errno);
return static_cast<size_t>(rbytes);
}
/** \throw std::system_error */
size_t read_all(void *buf, size_t bytes) {
uint8_t *pos = buf;
size_t remaining = bytes;
while (remaining) {
size_t rbytes = read(pos, remaining);
if (!rbytes)
return bytes - remaining;
remaining -= rbytes;
pos += rbytes;
}
return bytes;
}
/** \throw std::system_error */
void seek(off_t pos, int whence) {
if (_fd == -1)
throw std::runtime_exception("illegal state");
if (::lseek(_fd, pos, whence) == static_cast<off_t>(-1))
throw_errno(errno);
}
/** \throw std::system_error */
size_t write(const void *buf, size_t bytes) {
if (_fd == -1)
throw std::runtime_exception("illegal state");
ssize_t wbytes = ::write(_fd, buf, bytes);
if (wbytes < 0)
throw_errno(errno);
return static_cast<size_t>(wbytes);
}
/** \throw std::system_error */
size_t write_all(const void *buf, size_t bytes) {
uint8_t *pos = buf;
size_t remaining = bytes;
while (remaining) {
size_t wbytes = write(pos, remaining);
remaining -= wbytes;
pos += wbytes;
}
return bytes;
}
int operator() noexcept {
return _fd;
}
private:
int _fd;
};
}
#endif