Nix 2.26.3
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
3
4#include "types.hh"
5#include "error.hh"
6
7#ifdef _WIN32
8# define WIN32_LEAN_AND_MEAN
9# include <windows.h>
10#endif
11
12namespace nix {
13
14struct Sink;
15struct Source;
16
21#if _WIN32
22 HANDLE
23#else
24 int
25#endif
26 ;
27
28const Descriptor INVALID_DESCRIPTOR =
29#if _WIN32
30 INVALID_HANDLE_VALUE
31#else
32 -1
33#endif
34 ;
35
41static inline Descriptor toDescriptor(int fd)
42{
43#ifdef _WIN32
44 return reinterpret_cast<HANDLE>(_get_osfhandle(fd));
45#else
46 return fd;
47#endif
48}
49
56static inline int fromDescriptorReadOnly(Descriptor fd)
57{
58#ifdef _WIN32
59 return _open_osfhandle(reinterpret_cast<intptr_t>(fd), _O_RDONLY);
60#else
61 return fd;
62#endif
63}
64
68std::string readFile(Descriptor fd);
69
74void readFull(Descriptor fd, char * buf, size_t count);
75
76void writeFull(Descriptor fd, std::string_view s, bool allowInterrupts = true);
77
86std::string readLine(Descriptor fd, bool eofOk = false);
87
91void writeLine(Descriptor fd, std::string s);
92
96std::string drainFD(Descriptor fd, bool block = true, const size_t reserveSize=0);
97
101void drainFD(
103 , Sink & sink
104#ifndef _WIN32
105 , bool block = true
106#endif
107 );
108
112[[gnu::always_inline]]
114{
115#ifndef _WIN32
116 return STDIN_FILENO;
117#else
118 return GetStdHandle(STD_INPUT_HANDLE);
119#endif
120}
121
125[[gnu::always_inline]]
127{
128#ifndef _WIN32
129 return STDOUT_FILENO;
130#else
131 return GetStdHandle(STD_OUTPUT_HANDLE);
132#endif
133}
134
138[[gnu::always_inline]]
140{
141#ifndef _WIN32
142 return STDERR_FILENO;
143#else
144 return GetStdHandle(STD_ERROR_HANDLE);
145#endif
146}
147
151class AutoCloseFD
152{
153 Descriptor fd;
154public:
155 AutoCloseFD();
156 AutoCloseFD(Descriptor fd);
157 AutoCloseFD(const AutoCloseFD & fd) = delete;
158 AutoCloseFD(AutoCloseFD&& fd) noexcept;
159 ~AutoCloseFD();
160 AutoCloseFD& operator =(const AutoCloseFD & fd) = delete;
161 AutoCloseFD& operator =(AutoCloseFD&& fd);
162 Descriptor get() const;
163 explicit operator bool() const;
164 Descriptor release();
165 void close();
166
170 void fsync() const;
171
177 void startFsync() const;
178};
179
180class Pipe
181{
182public:
183 AutoCloseFD readSide, writeSide;
184 void create();
185 void close();
186};
187
188#ifndef _WIN32 // Not needed on Windows, where we don't fork
189namespace unix {
190
196
201
202} // namespace unix
203#endif
204
205#if defined(_WIN32) && _WIN32_WINNT >= 0x0600
206namespace windows {
207
208Path handleToPath(Descriptor handle);
209std::wstring handleToFileName(Descriptor handle);
210
211} // namespace windows
212#endif
213
214MakeError(EndOfFile, Error);
215
216}
Definition file-descriptor.hh:152
void startFsync() const
Definition file-descriptor.cc:117
void fsync() const
Definition file-descriptor.cc:97
Definition file-descriptor.hh:181
This file defines two main structs/classes used in nix error handling.
Descriptor getStandardError()
Definition file-descriptor.hh:139
int Descriptor
Definition file-descriptor.hh:20
std::string readLine(Descriptor fd, bool eofOk=false)
void closeExtraFDs()
Descriptor getStandardOutput()
Definition file-descriptor.hh:126
void closeOnExec(Descriptor fd)
Descriptor getStandardInput()
Definition file-descriptor.hh:113
void readFull(Descriptor fd, char *buf, size_t count)
return STDIN_FILENO
Definition lexer.l:3018
return STDOUT_FILENO
Definition lexer.l:3031
return s
Definition lexer.l:459
return STDERR_FILENO
Definition lexer.l:3044
size_t & count
Definition lexer.l:7012
std::string readFile() const
return fd
Definition lexer.l:2948
Definition serialise.hh:20
Definition serialise.hh:68
std::string Path
Definition types.hh:22