FileDescriptor.hxx 6.27 KB
Newer Older
1
/*
2
 * Copyright 2012-2020 Max Kellermann <max.kellermann@gmail.com>
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
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the
 * distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
 * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef FILE_DESCRIPTOR_HXX
#define FILE_DESCRIPTOR_HXX

33
#include <cstddef>
34 35
#include <utility>

36 37 38
#include <unistd.h>
#include <sys/types.h>

39
#ifdef __linux__
40
#include <csignal>
41 42
#endif

43 44 45 46
#ifdef _WIN32
#include <wchar.h>
#endif

47 48 49
/**
 * An OO wrapper for a UNIX file descriptor.
 *
50 51
 * This class is unmanaged and trivial; for a managed version, see
 * #UniqueFileDescriptor.
52 53 54 55 56 57 58
 */
class FileDescriptor {
protected:
	int fd;

public:
	FileDescriptor() = default;
59
	explicit constexpr FileDescriptor(int _fd) noexcept:fd(_fd) {}
60

61
	constexpr bool operator==(FileDescriptor other) const noexcept {
62 63 64
		return fd == other.fd;
	}

65 66 67 68
	constexpr bool operator!=(FileDescriptor other) const noexcept {
		return !(*this == other);
	}

69
	constexpr bool IsDefined() const noexcept {
70 71 72
		return fd >= 0;
	}

73 74 75 76
#ifndef _WIN32
	/**
	 * Ask the kernel whether this is a valid file descriptor.
	 */
77
	[[gnu::pure]]
78
	bool IsValid() const noexcept;
79

80 81 82
	/**
	 * Ask the kernel whether this is a regular file.
	 */
83
	[[gnu::pure]]
84 85
	bool IsRegularFile() const noexcept;

86 87 88
	/**
	 * Ask the kernel whether this is a pipe.
	 */
89
	[[gnu::pure]]
90 91 92 93 94
	bool IsPipe() const noexcept;

	/**
	 * Ask the kernel whether this is a socket descriptor.
	 */
95
	[[gnu::pure]]
96
	bool IsSocket() const noexcept;
97 98
#endif

99 100 101 102
	/**
	 * Returns the file descriptor.  This may only be called if
	 * IsDefined() returns true.
	 */
103
	constexpr int Get() const noexcept {
104 105 106
		return fd;
	}

107
	void Set(int _fd) noexcept {
108 109 110
		fd = _fd;
	}

111
	int Steal() noexcept {
112
		return std::exchange(fd, -1);
113 114
	}

115
	void SetUndefined() noexcept {
116 117 118
		fd = -1;
	}

119
	static constexpr FileDescriptor Undefined() noexcept {
120 121 122
		return FileDescriptor(-1);
	}

123
#ifdef __linux__
124 125 126 127
	bool Open(FileDescriptor dir, const char *pathname,
		  int flags, mode_t mode=0666) noexcept;
#endif

128
	bool Open(const char *pathname, int flags, mode_t mode=0666) noexcept;
129 130 131 132 133

#ifdef _WIN32
	bool Open(const wchar_t *pathname, int flags, mode_t mode=0666) noexcept;
#endif

134
	bool OpenReadOnly(const char *pathname) noexcept;
135

136
#ifndef _WIN32
137
	bool OpenNonBlocking(const char *pathname) noexcept;
138
#endif
139

140 141 142 143 144
#ifdef __linux__
	static bool CreatePipe(FileDescriptor &r, FileDescriptor &w,
			       int flags) noexcept;
#endif

145
	static bool CreatePipe(FileDescriptor &r, FileDescriptor &w) noexcept;
146

147 148 149
#ifdef _WIN32
	void EnableCloseOnExec() noexcept {}
	void DisableCloseOnExec() noexcept {}
150
	void SetBinaryMode() noexcept;
151
#else
152 153 154
	static bool CreatePipeNonBlock(FileDescriptor &r,
				       FileDescriptor &w) noexcept;

155 156
	void SetBinaryMode() noexcept {}

157 158 159
	/**
	 * Enable non-blocking mode on this file descriptor.
	 */
160
	void SetNonBlocking() noexcept;
161 162 163 164

	/**
	 * Enable blocking mode on this file descriptor.
	 */
165
	void SetBlocking() noexcept;
166

167 168 169 170 171 172 173 174 175 176 177 178
	/**
	 * Auto-close this file descriptor when a new program is
	 * executed.
	 */
	void EnableCloseOnExec() noexcept;

	/**
	 * Do not auto-close this file descriptor when a new program
	 * is executed.
	 */
	void DisableCloseOnExec() noexcept;

179 180 181
	/**
	 * Duplicate the file descriptor onto the given file descriptor.
	 */
182
	bool Duplicate(FileDescriptor new_fd) const noexcept {
183
		return ::dup2(Get(), new_fd.Get()) != -1;
184
	}
185 186 187 188 189 190 191

	/**
	 * Similar to Duplicate(), but if destination and source file
	 * descriptor are equal, clear the close-on-exec flag.  Use
	 * this method to inject file descriptors into a new child
	 * process, to be used by a newly executed program.
	 */
192
	bool CheckDuplicate(FileDescriptor new_fd) noexcept;
193 194
#endif

195
#ifdef __linux__
196 197 198
	bool CreateEventFD(unsigned initval=0) noexcept;
	bool CreateSignalFD(const sigset_t *mask) noexcept;
	bool CreateInotify() noexcept;
199 200 201
#endif

	/**
202
	 * Close the file descriptor.  It should not be called on an
203 204 205
	 * "undefined" object.  After this call, IsDefined() is guaranteed
	 * to return false, and this object may be reused.
	 */
206
	bool Close() noexcept {
207
		return ::close(Steal()) == 0;
208 209 210 211 212
	}

	/**
	 * Rewind the pointer to the beginning of the file.
	 */
213
	bool Rewind() noexcept;
214

215
	off_t Seek(off_t offset) noexcept {
216 217 218
		return lseek(Get(), offset, SEEK_SET);
	}

219
	off_t Skip(off_t offset) noexcept {
220 221 222
		return lseek(Get(), offset, SEEK_CUR);
	}

223
	[[gnu::pure]]
224
	off_t Tell() const noexcept {
225 226 227
		return lseek(Get(), 0, SEEK_CUR);
	}

228 229 230
	/**
	 * Returns the size of the file in bytes, or -1 on error.
	 */
231
	[[gnu::pure]]
232
	off_t GetSize() const noexcept;
233

234
	ssize_t Read(void *buffer, std::size_t length) noexcept {
235 236 237
		return ::read(fd, buffer, length);
	}

238 239 240 241
	/**
	 * Read until all of the given buffer has been filled.  Throws
	 * on error.
	 */
242
	void FullRead(void *buffer, std::size_t length);
243

244
	ssize_t Write(const void *buffer, std::size_t length) noexcept {
245 246 247
		return ::write(fd, buffer, length);
	}

248 249 250 251
	/**
	 * Write until all of the given buffer has been written.
	 * Throws on error.
	 */
252
	void FullWrite(const void *buffer, std::size_t length);
253

254
#ifndef _WIN32
255
	int Poll(short events, int timeout) const noexcept;
256

257 258
	int WaitReadable(int timeout) const noexcept;
	int WaitWritable(int timeout) const noexcept;
259

260
	[[gnu::pure]]
261
	bool IsReadyForWriting() const noexcept;
262 263 264 265
#endif
};

#endif