-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStream.cpp
More file actions
190 lines (153 loc) · 5.13 KB
/
Stream.cpp
File metadata and controls
190 lines (153 loc) · 5.13 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*******************************************************************************
* libunix++: C++ wrapper for Linux system calls
* Generic stream operations
*
* © 2019—2025, Sauron <libunixpp@saur0n.science>
******************************************************************************/
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/sendfile.h>
#include <sys/stat.h>
#include "exception.hppi"
#include "Stream.hpp"
using namespace upp;
using std::initializer_list;
class StdStream : public Stream {
public:
StdStream(Stream::Standard no) : Stream(int(no)) {}
};
static StdStream sin(Stream::IN), sout(Stream::OUT), serr(Stream::ERR);
/******************************************************************************/
Stream::Stream(Stream &&other) : fd(other.fd) {
other.fd=-1;
}
Stream::Stream(const Stream &other) : fd(dup(other.fd)) {
THROW_SYSTEM_ERROR_IF(fd<0);
}
Stream::Stream(const Stream &other, int newfd) :
Stream(dup2(other.fd, newfd)) {}
Stream::Stream(const Stream &other, int newfd, int flags) :
Stream(dup3(other.fd, newfd, flags)) {}
Stream::~Stream() {
close(fd);
}
off_t Stream::seek(off_t offset, int whence) {
off_t result=::lseek(fd, offset, whence);
THROW_SYSTEM_ERROR_IF(result<0);
return result;
}
size_t Stream::read(void * buffer, size_t count) {
NORMAL_IO_WRAPPER(ssize_t, size_t, ::read(fd, buffer, count));
}
size_t Stream::read(AtomicBool &interrupted, void * buffer, size_t count) {
INTERRUPTED_IO_WRAPPER(ssize_t, size_t, ::read(fd, buffer, count));
}
size_t Stream::read(void * buffer, size_t count, off_t offset) {
NORMAL_IO_WRAPPER(ssize_t, size_t, ::pread(fd, buffer, count, offset));
}
size_t Stream::read(AtomicBool &interrupted, void * buffer, size_t count, off_t offset) {
INTERRUPTED_IO_WRAPPER(ssize_t, size_t, ::pread(fd, buffer, count, offset));
}
size_t Stream::read(const initializer_list<struct iovec> &vec) {
ssize_t result=::readv(fd, vec.begin(), vec.size());
THROW_SYSTEM_ERROR_IF(result<0);
return size_t(result);
}
size_t Stream::read(const initializer_list<struct iovec> &vec, off_t offset) {
ssize_t result=::preadv(fd, vec.begin(), vec.size(), offset);
THROW_SYSTEM_ERROR_IF(result<0);
return size_t(result);
}
size_t Stream::read(const initializer_list<struct iovec> &vec, off_t offset,
int flags) {
ssize_t result=::preadv2(fd, vec.begin(), vec.size(), offset, flags);
THROW_SYSTEM_ERROR_IF(result<0);
return size_t(result);
}
size_t Stream::write(const void * buffer, size_t count) {
ssize_t result=::write(fd, buffer, count);
THROW_SYSTEM_ERROR_IF(result<0);
return size_t(result);
}
size_t Stream::write(const void * buffer, size_t count, off_t offset) {
ssize_t result=::pwrite(fd, buffer, count, offset);
THROW_SYSTEM_ERROR_IF(result<0);
return size_t(result);
}
size_t Stream::write(const initializer_list<struct iovec> &vec) {
ssize_t result=::writev(fd, vec.begin(), vec.size());
THROW_SYSTEM_ERROR_IF(result<0);
return size_t(result);
}
size_t Stream::write(const initializer_list<struct iovec> &vec, off_t offset) {
ssize_t result=::pwritev(fd, vec.begin(), vec.size(), offset);
THROW_SYSTEM_ERROR_IF(result<0);
return size_t(result);
}
size_t Stream::write(const initializer_list<struct iovec> &vec, off_t offset, int flags) {
ssize_t result=::pwritev2(fd, vec.begin(), vec.size(), offset, flags);
THROW_SYSTEM_ERROR_IF(result<0);
return size_t(result);
}
void Stream::stat(struct stat * statbuf) {
int result=fstat(fd, statbuf);
THROW_SYSTEM_ERROR_IF(result<0);
}
unsigned Stream::getFlags() {
return fcntl(F_GETFD);
}
void Stream::setFlags(unsigned flags) {
fcntl(F_SETFD, flags);
}
unsigned Stream::getStatusFlags() {
return fcntl(F_GETFL);
}
void Stream::setStatusFlags(unsigned flags) {
fcntl(F_SETFL, flags);
}
int Stream::getOwner() {
return fcntl(fd, F_GETOWN);
}
size_t Stream::sendfile(Stream &from, size_t count) {
ssize_t result=::sendfile(fd, from.fd, nullptr, count);
THROW_SYSTEM_ERROR_IF(result<0);
return size_t(result);
}
size_t Stream::sendfile(Stream &from, off_t &offset, size_t count) {
ssize_t result=::sendfile(fd, from.fd, &offset, count);
THROW_SYSTEM_ERROR_IF(result<0);
return size_t(result);
}
Stream &Stream::get(Standard no) {
if (no==IN)
return sin;
else if (no==OUT)
return sout;
else if (no==ERR)
return serr;
else
throw std::out_of_range("no");
}
Stream::Stream(int fd) : fd(fd) {
THROW_SYSTEM_ERROR_IF(fd<0);
}
unsigned Stream::ioctl(int request, void * argp) {
int result=::ioctl(fd, request, argp);
THROW_SYSTEM_ERROR_IF(result<0);
return unsigned(result);
}
unsigned Stream::ioctl(int request, int argp) {
int result=::ioctl(fd, request, argp);
THROW_SYSTEM_ERROR_IF(result<0);
return unsigned(result);
}
unsigned Stream::fcntl(int cmd) {
int result=::fcntl(fd, cmd);
THROW_SYSTEM_ERROR_IF(result<0);
return unsigned(result);
}
unsigned Stream::fcntl(int cmd, int arg) {
int result=::fcntl(fd, cmd, arg);
THROW_SYSTEM_ERROR_IF(result<0);
return unsigned(result);
}