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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
|
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include "Handle.h"
#include "config.h"
Handle::Handle(const Handle & nh) : h(nh.h >= 0 ? dup(nh.h) : nh.h), closed(false), nonblock(false), zfile(0), z(0) {
// cerr << "Duplication of handle " << nh.h << " to " << h << endl;
if ((h >= 0) && (nh.z)) {
SetZ(nh.z);
}
}
Handle::~Handle() {
// cerr << "Destroying handle " << h << endl;
close();
}
Handle::Handle(int nh) : h(nh), closed(false), zfile(0), z(0) {
// cerr << "Initialising handle " << h << endl;
}
int Handle::GetHandle() {
return h;
}
ssize_t Handle::write(const void *buf, size_t count) throw (GeneralException) {
ssize_t r, tr = 0;
bool done, full = false;
do {
done = true;
errno = 0;
if ((r = uwrite(buf, count)) < 0) {
if ((!errno) || (errno == EAGAIN) || (errno == EINTR)) {
// Avant de déclarer une erreur, on vérifie si ce n'est pas un
// problème lié au fait qu'il n'y a plus de place libre. Cela peut
// arriver si l'on agit sur un pipe ou un handle. Nous
// attendons encore une fois avant de déclarer l'erreur,
// grace au drapeau full.
if (full) {
throw IOException(GetName(), IO_WRITE, count);
} else {
done = false;
full = true;
if (nonblock) {
throw IOAgain();
} else {
sleep(1);
}
}
} else {
throw IOException(GetName(), IO_WRITE, count);
}
} else if (((size_t) r) != count) {
full = done = false;
((char *)buf) += r;
tr += r;
}
} while (!done);
return r + tr;
}
ssize_t Handle::read(void *buf, size_t count) throw (GeneralException) {
ssize_t r;
errno = 0;
if ((r = uread(buf, count)) < 0) {
if ((!errno) || (errno == EAGAIN) || (errno == EINTR)) {
// Avant de déclarer une erreur, on vérifie si ce n'est pas un
// problème lié au fait qu'il n'y a plus d'octets.
throw IOAgain();
} else {
throw IOException(GetName(), IO_READ, count);
}
}
if (!r) {
close();
}
return r;
}
bool Handle::IsClosed(void) {
return closed;
}
bool Handle::IsNonBlock(void) {
return nonblock;
}
void Handle::SetNonBlock(void) {
if ((h >= 0) || !nonblock) {
fcntl(h, F_SETFL, O_NONBLOCK);
}
nonblock = true;
}
Handle & operator<<(Handle & h, const String & s) {
const char * p;
p = s.to_charp();
h.write(p, strlen(p));
return h;
}
Handle & operator>>(Handle & h, String & s) {
char t[BUFSIZ];
int i = 0, r;
while ((r = h.read(&(t[i]), 1)) && (i != (BUFSIZ - 1))) {
// Il y a souvent des \r\n dans les sockets par exemple,
// ou bien en lisant des fichiers au format MS-DOS. On
// ignore le \r pour ne garder que le \n, standard sous Unix.
if (t[i] == '\r') {
continue;
}
if (t[i] == '\n') {
break;
} else {
i++;
}
}
t[i] = '\0';
s = t;
return h;
}
void Handle::close() throw (GeneralException) {
if (IsClosed()) {
return;
}
if (h >= 0) {
if (z) {
// cerr << "Performing gzclose on handle " << h << endl;
int err = gzclose(zfile);
// cerr << "gzclose returned " << err << endl;
if (err) {
if (err == Z_ERRNO) {
throw GeneralException(String(_("Error during close: ")) + strerror(errno));
} else {
throw GeneralException(_("Error in zlib during gzclose."));
}
}
} else {
int err = ::close(h);
if (err) {
throw GeneralException(String(_("Error during close: ")) + strerror(errno));
}
}
}
h = -1;
closed = 1;
}
bool Handle::CanRead(void) {
return false;
}
bool Handle::CanWrite(void) {
return false;
}
String Handle::GetName(void) {
return _("Bare Handle - should not happend");
}
ssize_t Handle::GetSize(void) {
return -1;
}
time_t Handle::GetModif(void) {
return -1;
}
bool Handle::CanWatch(void) {
return true;
}
void Handle::Dup(const Handle & H) {
close();
if (H.h >= 0) {
h = dup(H.h);
}
}
void Handle::SetZ(int az) throw (GeneralException) {
if (z) {
throw GeneralException(_("Can't SetZ a Handle twice."));
}
z = az;
if (h < 0) {
throw GeneralException(_("Can't SetZ a virtual Handle."));
}
if (z) {
char format[4];
int index = 0;
if (CanRead()) {
format[index++] = 'r';
}
if (CanWrite()) {
format[index++] = 'w';
}
format[index++] = (char) (z + '0');
format[index] = 0;
// cerr << "Performing gzdopen on handle " << h << " with mode \"" << format << "\"\n";
if (!(zfile = gzdopen(h, format))) {
throw GeneralException(_("Was not able to gzdopen."));
}
}
}
ssize_t Handle::uwrite(const void * buf, size_t count) throw (GeneralException) {
if (z) {
// cerr << "Performing gzwrite of " << count << " byte for handle " << h << endl;
int err = gzwrite(zfile, buf, count);
// cerr << "gzwrite returned " << err << endl;
if (err == 0) {
const char * m = gzerror(zfile, &err);
if (err == Z_ERRNO) {
return -1;
} else {
throw GeneralException(String(_("Error in zlib during gzwrite: ")) + m);
}
}
return err;
} else {
return ::write(h, buf, count);
}
}
ssize_t Handle::uread(void * buf, size_t count) {
if (z) {
// cerr << "Performing gzread of " << count << " byte for handle " << h << endl;
int err = gzread(zfile, buf, count);
// cerr << "gzwrite returned " << err << endl;
if (err == -1) {
gzerror(zfile, &err);
if (err == Z_ERRNO) {
return -1;
} else {
return 0;
}
}
return err;
} else {
return ::read(h, buf, count);
}
}
|