Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
file-descriptor.hh
Go to the documentation of this file.
1#pragma once
11
12#include "nix/util/error.hh"
13#include "nix/util/result.hh"
15
16#ifdef _WIN32
17# define WIN32_LEAN_AND_MEAN
18# include <windows.h>
19# include <io.h>
20#endif
21
22#include <sys/types.h>
23
24#include <span>
25#include <cstddef>
26#include <type_traits>
27
28namespace nix {
29
30struct Sink;
31struct Source;
32
37#ifdef _WIN32
38 HANDLE
39#else
40 int
41#endif
42 ;
43
44const Descriptor INVALID_DESCRIPTOR =
45#ifdef _WIN32
46 INVALID_HANDLE_VALUE
47#else
48 -1
49#endif
50 ;
51
57static inline Descriptor toDescriptor(int fd)
58{
59#ifdef _WIN32
60 return reinterpret_cast<HANDLE>(_get_osfhandle(fd));
61#else
62 return fd;
63#endif
64}
65
69std::string readFile(Descriptor fd);
70
82size_t read(Descriptor fd, std::span<std::byte> buffer);
83
95size_t write(Descriptor fd, std::span<const std::byte> buffer, bool allowInterrupts);
96
106std::make_unsigned_t<off_t> getFileSize(Descriptor fd);
107
120size_t readOffset(Descriptor fd, off_t offset, std::span<std::byte> buffer);
121
131void copyFdRange(Descriptor fd, off_t offset, size_t nbytes, Sink & sink, bool tryCoW = false);
132
137void readFull(Descriptor fd, char * buf, size_t count);
138
139void writeFull(Descriptor fd, std::string_view s, bool allowInterrupts = true);
140
151std::string readLine(Descriptor fd, bool eofOk = false, char terminator = '\n');
152
156void writeLine(Descriptor fd, std::string s);
157
161void syncDescriptor(Descriptor fd);
162
167{
171 std::optional<std::make_unsigned_t<off_t>> expectedSize = {};
172
173#ifndef _WIN32
177 bool block = true;
178#endif
179};
180
185{
190 std::make_unsigned_t<off_t> size = 0;
191
195 bool expected = false;
196
197#ifndef _WIN32
201 bool block = true;
202#endif
203};
204
211std::string drainFD(Descriptor fd, DrainFdOpts opts = {});
212
220void drainFD(Descriptor fd, Sink & sink, DrainFdSinkOpts opts = {});
221
225[[gnu::always_inline]]
227{
228#ifndef _WIN32
229 return STDIN_FILENO;
230#else
231 return GetStdHandle(STD_INPUT_HANDLE);
232#endif
233}
234
238[[gnu::always_inline]]
240{
241#ifndef _WIN32
242 return STDOUT_FILENO;
243#else
244 return GetStdHandle(STD_OUTPUT_HANDLE);
245#endif
246}
247
251[[gnu::always_inline]]
253{
254#ifndef _WIN32
255 return STDERR_FILENO;
256#else
257 return GetStdHandle(STD_ERROR_HANDLE);
258#endif
259}
260
264class AutoCloseFD
265{
266 Descriptor fd;
267public:
268 AutoCloseFD() noexcept
269 : fd(INVALID_DESCRIPTOR)
270 {
271 }
272
273 AutoCloseFD(Descriptor fd) noexcept
274 : fd(fd)
275 {
276 }
277
278 AutoCloseFD(const AutoCloseFD & fd) = delete;
279 AutoCloseFD(AutoCloseFD && fd) noexcept;
280 ~AutoCloseFD();
281 AutoCloseFD & operator=(const AutoCloseFD & fd) = delete;
282 // NOLINTNEXTLINE(performance-noexcept-move-constructor) - technically can throw because of close()
283 AutoCloseFD & operator=(AutoCloseFD && fd);
284
285 Descriptor get() const noexcept
286 {
287 return fd;
288 }
289
290 explicit operator bool() const noexcept
291 {
292 return fd != INVALID_DESCRIPTOR;
293 }
294
295 Descriptor release() noexcept;
296 void close();
297
301 void fsync() const
302 {
303 if (fd != INVALID_DESCRIPTOR)
305 }
306
312 void startFsync() const;
313};
314
321AutoCloseFD dupDescriptor(Descriptor fd);
322
328AutoCloseFD openNullFD();
329
330class Pipe
331{
332public:
333 AutoCloseFD readSide, writeSide;
334
335 void create(
336#ifndef _WIN32
337 bool nonBlocking = false
338#endif
339 );
340
341 void close();
342};
343
354void closeOnExec(Descriptor fd);
355
356#ifndef _WIN32 // Not needed on Windows, where we don't fork
357namespace unix {
358
363void closeExtraFDs();
364
370void closeExtraFDs(std::span<const Descriptor> keepExtra);
371
377{
378 Pipe pipe;
379
380 void create();
381
385 void notify();
386
390 void drain();
391};
392
393} // namespace unix
394#else
395namespace windows {
396
404void setHandleInheritability(Descriptor fd, bool inherit);
405
406} // namespace windows
407#endif
408
409MakeError(EndOfFile, Error);
410
421Result<off_t, NativeError> seek(Descriptor fd, off_t offset, int whence);
422
423} // namespace nix
Definition file-descriptor.hh:265
void startFsync() const
Definition file-descriptor.cc:289
void fsync() const
Definition file-descriptor.hh:301
Definition file-descriptor.hh:331
This file defines two main structs/classes used in nix error handling.
Descriptor getStandardError()
Definition file-descriptor.hh:252
int Descriptor
Definition file-descriptor.hh:36
Descriptor getStandardOutput()
Definition file-descriptor.hh:239
void syncDescriptor(Descriptor fd)
Definition file-descriptor.cc:243
Descriptor getStandardInput()
Definition file-descriptor.hh:226
void closeOnExec(Descriptor fd)
Definition file-descriptor.cc:228
Definition file-descriptor.hh:185
bool expected
Definition file-descriptor.hh:195
bool block
Definition file-descriptor.hh:201
std::make_unsigned_t< off_t > size
Definition file-descriptor.hh:190
Definition file-descriptor.hh:167
std::optional< std::make_unsigned_t< off_t > > expectedSize
Definition file-descriptor.hh:171
bool block
Definition file-descriptor.hh:177
Definition serialise.hh:18
Definition serialise.hh:95
Definition file-descriptor.hh:377
void notify()
Definition file-descriptor.cc:261
void drain()
Definition file-descriptor.cc:273
Errors from the operating system: SystemError, and its subclasses for the native error codes,...