FileDescriptor.hxx 6.2 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 150
#ifdef _WIN32
	void EnableCloseOnExec() noexcept {}
	void DisableCloseOnExec() noexcept {}
#else
151 152 153
	static bool CreatePipeNonBlock(FileDescriptor &r,
				       FileDescriptor &w) noexcept;

154 155 156
	/**
	 * Enable non-blocking mode on this file descriptor.
	 */
157
	void SetNonBlocking() noexcept;
158 159 160 161

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

164 165 166 167 168 169 170 171 172 173 174 175
	/**
	 * 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;

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

	/**
	 * 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.
	 */
189
	bool CheckDuplicate(FileDescriptor new_fd) noexcept;
190 191
#endif

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

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

	/**
	 * Rewind the pointer to the beginning of the file.
	 */
210
	bool Rewind() noexcept;
211

212
	off_t Seek(off_t offset) noexcept {
213 214 215
		return lseek(Get(), offset, SEEK_SET);
	}

216
	off_t Skip(off_t offset) noexcept {
217 218 219
		return lseek(Get(), offset, SEEK_CUR);
	}

220
	[[gnu::pure]]
221
	off_t Tell() const noexcept {
222 223 224
		return lseek(Get(), 0, SEEK_CUR);
	}

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

231
	ssize_t Read(void *buffer, std::size_t length) noexcept {
232 233 234
		return ::read(fd, buffer, length);
	}

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

241
	ssize_t Write(const void *buffer, std::size_t length) noexcept {
242 243 244
		return ::write(fd, buffer, length);
	}

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

251
#ifndef _WIN32
252
	int Poll(short events, int timeout) const noexcept;
253

254 255
	int WaitReadable(int timeout) const noexcept;
	int WaitWritable(int timeout) const noexcept;
256

257
	[[gnu::pure]]
258
	bool IsReadyForWriting() const noexcept;
259 260 261 262
#endif
};

#endif