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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
// file : butl/fdstream.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2016 Code Synthesis Ltd
// license : MIT; see accompanying LICENSE file
#include <butl/fdstream>
#ifndef _WIN32
# include <fcntl.h> // open(), O_*
# include <unistd.h> // close(), read(), write()
# include <sys/stat.h> // S_I*
#else
# include <io.h> // _close(), _read(), _write(), _setmode(), _sopen()
# include <share.h> // _SH_DENYNO
# include <stdio.h> // _fileno(), stdin, stdout, stderr
# include <fcntl.h> // _O_*
# include <sys/stat.h> // S_I*
#endif
#include <system_error>
using namespace std;
namespace butl
{
// fdbuf
//
fdbuf::
~fdbuf () {close ();}
void fdbuf::
open (int fd)
{
close ();
fd_ = fd;
setg (buf_, buf_, buf_);
setp (buf_, buf_ + sizeof (buf_) - 1); // Keep space for overflow's char.
}
void fdbuf::
close ()
{
if (is_open ())
{
if (!fdclose (fd_))
throw system_error (errno, system_category ());
fd_ = -1;
}
}
streamsize fdbuf::
showmanyc ()
{
return is_open () ? static_cast<streamsize> (egptr () - gptr ()) : -1;
}
fdbuf::int_type fdbuf::
underflow ()
{
int_type r (traits_type::eof ());
if (is_open ())
{
if (gptr () < egptr () || load ())
r = traits_type::to_int_type (*gptr ());
}
return r;
}
bool fdbuf::
load ()
{
#ifndef _WIN32
ssize_t n (read (fd_, buf_, sizeof (buf_)));
#else
int n (_read (fd_, buf_, sizeof (buf_)));
#endif
if (n == -1)
throw system_error (errno, system_category ());
setg (buf_, buf_, buf_ + n);
return n != 0;
}
fdbuf::int_type fdbuf::
overflow (int_type c)
{
int_type r (traits_type::eof ());
if (is_open () && c != traits_type::eof ())
{
// Store last character in the space we reserved in open(). Note
// that pbump() doesn't do any checks.
//
*pptr () = traits_type::to_char_type (c);
pbump (1);
if (save ())
r = c;
}
return r;
}
int fdbuf::
sync ()
{
return is_open () && save () ? 0 : -1;
}
bool fdbuf::
save ()
{
size_t n (pptr () - pbase ());
if (n != 0)
{
#ifndef _WIN32
ssize_t m (write (fd_, buf_, n));
#else
int m (_write (fd_, buf_, n));
#endif
if (m == -1)
throw system_error (errno, system_category ());
if (n != static_cast<size_t> (m))
return false;
setp (buf_, buf_ + sizeof (buf_) - 1);
}
return true;
}
// fdstream_base
//
fdstream_base::
fdstream_base (int fd, fdtranslate m)
: fdstream_base (fd) // Delegate.
{
// Note that here we rely on fdstream_base() (and fdbuf() which it calls)
// to note read from the file.
//
fdmode (fd, m);
}
// Utility functions
//
int
fdopen (const path& f, fdopen_mode m, permissions p)
{
mode_t pf (S_IREAD | S_IWRITE | S_IEXEC);
#ifdef S_IRWXG
pf |= S_IRWXG;
#endif
#ifdef S_IRWXO
pf |= S_IRWXO;
#endif
pf &= static_cast<mode_t> (p);
// Return true if the open mode contains a specific flag.
//
auto mode = [m](fdopen_mode flag) -> bool {return (m & flag) == flag;};
int of (0);
bool in (mode (fdopen_mode::in));
bool out (mode (fdopen_mode::out));
#ifndef _WIN32
if (in && out)
of |= O_RDWR;
else if (in)
of |= O_RDONLY;
else if (out)
of |= O_WRONLY;
if (out)
{
if (mode (fdopen_mode::append))
of |= O_APPEND;
if (mode (fdopen_mode::truncate))
of |= O_TRUNC;
}
if (mode (fdopen_mode::create))
{
of |= O_CREAT;
if (mode (fdopen_mode::exclusive))
of |= O_EXCL;
}
#ifdef O_LARGEFILE
of |= O_LARGEFILE;
#endif
int fd (open (f.string ().c_str (), of, pf));
#else
if (in && out)
of |= _O_RDWR;
else if (in)
of |= _O_RDONLY;
else if (out)
of |= _O_WRONLY;
if (out)
{
if (mode (fdopen_mode::append))
of |= _O_APPEND;
if (mode (fdopen_mode::truncate))
of |= _O_TRUNC;
}
if (mode (fdopen_mode::create))
{
of |= _O_CREAT;
if (mode (fdopen_mode::exclusive))
of |= _O_EXCL;
}
of |= mode (fdopen_mode::binary) ? _O_BINARY : _O_TEXT;
// According to Microsoft _sopen() should not change the permissions of an
// existing file. Meanwhile it does if we pass them (reproduced on Windows
// XP, 7, and 8). And we must pass them if we have _O_CREATE. So we need
// to take care of preserving the permissions ourselves. Note that Wine's
// implementation of _sopen() works properly.
//
bool pass_perm (of & _O_CREAT);
if (pass_perm && file_exists (f))
{
// If the _O_CREAT flag is set then we need to clear it so that we can
// omit the permissions. But if the _O_EXCL flag is set as well we can't
// do that as fdopen() wouldn't fail as expected.
//
if (of & _O_EXCL)
throw system_error (EEXIST, system_category ());
of &= ~_O_CREAT;
pass_perm = false;
}
int fd (pass_perm
? _sopen (f.string ().c_str (), of, _SH_DENYNO, pf)
: _sopen (f.string ().c_str (), of, _SH_DENYNO));
#endif
if (fd == -1)
throw system_error (errno, system_category ());
return fd;
}
#ifndef _WIN32
bool
fdclose (int fd) noexcept
{
return close (fd) == 0;
}
int
fdnull () noexcept
{
return open ("/dev/null", O_RDWR);
}
fdtranslate
fdmode (int, fdtranslate)
{
return fdtranslate::binary;
}
fdtranslate
stdin_fdmode (fdtranslate)
{
return fdtranslate::binary;
}
fdtranslate
stdout_fdmode (fdtranslate)
{
return fdtranslate::binary;
}
fdtranslate
stderr_fdmode (fdtranslate)
{
return fdtranslate::binary;
}
#else
bool
fdclose (int fd) noexcept
{
return _close (fd) == 0;
}
int
fdnull () noexcept
{
return _sopen ("nul", _O_RDWR, _SH_DENYNO);
}
fdtranslate
fdmode (int fd, fdtranslate m)
{
int r (_setmode (fd, m == fdtranslate::binary ? _O_BINARY : _O_TEXT));
if (r == -1)
throw system_error (errno, system_category ());
return (r & _O_BINARY) == _O_BINARY
? fdtranslate::binary
: fdtranslate::text;
}
fdtranslate
stdin_fdmode (fdtranslate m)
{
int fd (_fileno (stdin));
if (fd == -1)
throw system_error (errno, system_category ());
return fdmode (fd, m);
}
fdtranslate
stdout_fdmode (fdtranslate m)
{
int fd (_fileno (stdout));
if (fd == -1)
throw system_error (errno, system_category ());
return fdmode (fd, m);
}
fdtranslate
stderr_fdmode (fdtranslate m)
{
int fd (_fileno (stderr));
if (fd == -1)
throw system_error (errno, system_category ());
return fdmode (fd, m);
}
#endif
}
|