2023-10-25 00:43:36 -04:00
|
|
|
#pragma once
|
|
|
|
///@file
|
|
|
|
|
|
|
|
#include "types.hh"
|
|
|
|
#include "error.hh"
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
struct Sink;
|
|
|
|
struct Source;
|
|
|
|
|
2024-03-29 15:59:14 -04:00
|
|
|
/**
|
|
|
|
* Operating System capability
|
|
|
|
*/
|
|
|
|
typedef int Descriptor;
|
|
|
|
|
|
|
|
const Descriptor INVALID_DESCRIPTOR = -1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a native `Descriptor` to a POSIX file descriptor
|
|
|
|
*
|
|
|
|
* This is a no-op except on Windows.
|
|
|
|
*/
|
|
|
|
static inline Descriptor toDescriptor(int fd)
|
|
|
|
{
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a POSIX file descriptor to a native `Descriptor`
|
|
|
|
*
|
|
|
|
* This is a no-op except on Windows.
|
|
|
|
*/
|
|
|
|
static inline int fromDescriptor(Descriptor fd, int flags)
|
|
|
|
{
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2023-10-25 00:43:36 -04:00
|
|
|
/**
|
|
|
|
* Read the contents of a resource into a string.
|
|
|
|
*/
|
2024-03-29 15:59:14 -04:00
|
|
|
std::string readFile(Descriptor fd);
|
2023-10-25 00:43:36 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrappers arount read()/write() that read/write exactly the
|
|
|
|
* requested number of bytes.
|
|
|
|
*/
|
2024-03-29 15:59:14 -04:00
|
|
|
void readFull(Descriptor fd, char * buf, size_t count);
|
2023-10-25 00:43:36 -04:00
|
|
|
|
2024-03-29 15:59:14 -04:00
|
|
|
void writeFull(Descriptor fd, std::string_view s, bool allowInterrupts = true);
|
2023-10-25 00:43:36 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Read a line from a file descriptor.
|
|
|
|
*/
|
2024-03-29 15:59:14 -04:00
|
|
|
std::string readLine(Descriptor fd);
|
2023-10-25 00:43:36 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Write a line to a file descriptor.
|
|
|
|
*/
|
2024-03-29 15:59:14 -04:00
|
|
|
void writeLine(Descriptor fd, std::string s);
|
2023-10-25 00:43:36 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Read a file descriptor until EOF occurs.
|
|
|
|
*/
|
2024-03-29 15:59:14 -04:00
|
|
|
std::string drainFD(Descriptor fd, bool block = true, const size_t reserveSize=0);
|
2023-10-25 00:43:36 -04:00
|
|
|
|
2024-03-29 15:59:14 -04:00
|
|
|
void drainFD(Descriptor fd, Sink & sink, bool block = true);
|
|
|
|
|
|
|
|
[[gnu::always_inline]]
|
|
|
|
inline Descriptor getStandardOut() {
|
|
|
|
return STDOUT_FILENO;
|
|
|
|
}
|
2023-10-25 00:43:36 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Automatic cleanup of resources.
|
|
|
|
*/
|
|
|
|
class AutoCloseFD
|
|
|
|
{
|
2024-03-29 15:59:14 -04:00
|
|
|
Descriptor fd;
|
2023-10-25 00:43:36 -04:00
|
|
|
public:
|
|
|
|
AutoCloseFD();
|
2024-03-29 15:59:14 -04:00
|
|
|
AutoCloseFD(Descriptor fd);
|
2023-10-25 00:43:36 -04:00
|
|
|
AutoCloseFD(const AutoCloseFD & fd) = delete;
|
|
|
|
AutoCloseFD(AutoCloseFD&& fd);
|
|
|
|
~AutoCloseFD();
|
|
|
|
AutoCloseFD& operator =(const AutoCloseFD & fd) = delete;
|
|
|
|
AutoCloseFD& operator =(AutoCloseFD&& fd);
|
2024-03-29 15:59:14 -04:00
|
|
|
Descriptor get() const;
|
2023-10-25 00:43:36 -04:00
|
|
|
explicit operator bool() const;
|
2024-03-29 15:59:14 -04:00
|
|
|
Descriptor release();
|
2023-10-25 00:43:36 -04:00
|
|
|
void close();
|
|
|
|
void fsync();
|
|
|
|
};
|
|
|
|
|
|
|
|
class Pipe
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AutoCloseFD readSide, writeSide;
|
|
|
|
void create();
|
|
|
|
void close();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Close all file descriptors except those listed in the given set.
|
|
|
|
* Good practice in child processes.
|
|
|
|
*/
|
2024-03-29 15:59:14 -04:00
|
|
|
void closeMostFDs(const std::set<Descriptor> & exceptions);
|
2023-10-25 00:43:36 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the close-on-exec flag for the given file descriptor.
|
|
|
|
*/
|
2024-03-29 15:59:14 -04:00
|
|
|
void closeOnExec(Descriptor fd);
|
2023-10-25 00:43:36 -04:00
|
|
|
|
|
|
|
MakeError(EndOfFile, Error);
|
|
|
|
|
|
|
|
}
|