MPD: system/FileDescriptor.hxx Source File

MPD 0.20.18

FileDescriptor.hxx
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2012-2015 Max Kellermann <max.kellermann@gmail.com>
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the
14 * distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27 * OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #ifndef FILE_DESCRIPTOR_HXX
31 #define FILE_DESCRIPTOR_HXX
32
33 #include "check.h"
34 #include "Compiler.h"
35
36 #include <assert.h>
37 #include <unistd.h>
38 #include <sys/types.h>
39
40 #ifdef USE_SIGNALFD
41 #include <signal.h>
42 #endif
43
49 class FileDescriptor {
50 protected:
51 int fd;
52
53 public:
54 FileDescriptor() = default;
55 explicit constexpr FileDescriptor(int _fd):fd(_fd) {}
56
57 constexpr bool operator==(FileDescriptor other) const {
58 return fd == other.fd;
59 }
60
61 constexpr bool IsDefined() const {
62 return fd >= 0;
63 }
64
69 constexpr int Get() const {
70 return fd;
71 }
72
73 void Set(int _fd) noexcept {
74 fd = _fd;
75 }
76
77 int Steal() noexcept {
78 assert(IsDefined());
79
80 int _fd = fd;
81 fd = -1;
82 return _fd;
83 }
84
85 void SetUndefined() noexcept {
86 fd = -1;
87 }
88
89 static constexpr FileDescriptor Undefined() {
90 return FileDescriptor(-1);
91 }
92
93 bool Open(const char *pathname, int flags, mode_t mode=0666) noexcept;
94 bool OpenReadOnly(const char *pathname) noexcept;
95
96 #ifndef _WIN32
97 bool OpenNonBlocking(const char *pathname) noexcept;
98
99 static bool CreatePipe(FileDescriptor &r, FileDescriptor &w) noexcept;
100
104 void SetNonBlocking() noexcept;
105
109 void SetBlocking() noexcept;
110
114 bool Duplicate(int new_fd) const noexcept {
115 return ::dup2(Get(), new_fd) == 0;
116 }
117 #endif
118
119 #ifdef USE_EVENTFD
120 bool CreateEventFD(unsigned initval=0) noexcept;
121 #endif
122
123 #ifdef USE_SIGNALFD
124 bool CreateSignalFD(const sigset_t *mask) noexcept;
125 #endif
126
127 #ifdef HAVE_INOTIFY_INIT
128 bool CreateInotify() noexcept;
129 #endif
130
136 bool Close() noexcept {
137 return ::close(Steal()) == 0;
138 }
139
143 bool Rewind() noexcept;
144
145 off_t Seek(off_t offset) noexcept {
146 return lseek(Get(), offset, SEEK_SET);
147 }
148
149 off_t Skip(off_t offset) noexcept {
150 return lseek(Get(), offset, SEEK_CUR);
151 }
152
153 gcc_pure
154 off_t Tell() const noexcept {
155 return lseek(Get(), 0, SEEK_CUR);
156 }
157
161 gcc_pure
162 off_t GetSize() const noexcept;
163
164 ssize_t Read(void *buffer, size_t length) noexcept {
165 return ::read(fd, buffer, length);
166 }
167
168 ssize_t Write(const void *buffer, size_t length) noexcept {
169 return ::write(fd, buffer, length);
170 }
171
172 #ifndef _WIN32
173 int Poll(short events, int timeout) const noexcept;
174
175 int WaitReadable(int timeout) const noexcept;
176 int WaitWritable(int timeout) const noexcept;
177 #endif
178 };
179
180 #endif
FileDescriptor::Tell
gcc_pure off_t Tell() const noexcept
Definition: FileDescriptor.hxx:154
check.h
FileDescriptor::Set
void Set(int _fd) noexcept
Definition: FileDescriptor.hxx:73
FileDescriptor::FileDescriptor
constexpr FileDescriptor(int _fd)
Definition: FileDescriptor.hxx:55
FileDescriptor::GetSize
gcc_pure off_t GetSize() const noexcept
Returns the size of the file in bytes, or -1 on error.
FileDescriptor::CreatePipe
static bool CreatePipe(FileDescriptor &r, FileDescriptor &w) noexcept
FileDescriptor::Skip
off_t Skip(off_t offset) noexcept
Definition: FileDescriptor.hxx:149
FileDescriptor::SetUndefined
void SetUndefined() noexcept
Definition: FileDescriptor.hxx:85
FileDescriptor::Seek
off_t Seek(off_t offset) noexcept
Definition: FileDescriptor.hxx:145
FileDescriptor::Poll
int Poll(short events, int timeout) const noexcept
FileDescriptor::Close
bool Close() noexcept
Close the file descriptor.
Definition: FileDescriptor.hxx:136
FileDescriptor::WaitReadable
int WaitReadable(int timeout) const noexcept
FileDescriptor::fd
int fd
Definition: FileDescriptor.hxx:51
FileDescriptor::OpenReadOnly
bool OpenReadOnly(const char *pathname) noexcept
FileDescriptor::Write
ssize_t Write(const void *buffer, size_t length) noexcept
Definition: FileDescriptor.hxx:168
Compiler.h
FileDescriptor::IsDefined
constexpr bool IsDefined() const
Definition: FileDescriptor.hxx:61
FileDescriptor::Rewind
bool Rewind() noexcept
Rewind the pointer to the beginning of the file.
FileDescriptor::OpenNonBlocking
bool OpenNonBlocking(const char *pathname) noexcept
FileDescriptor::Open
bool Open(const char *pathname, int flags, mode_t mode=0666) noexcept
FileDescriptor::Duplicate
bool Duplicate(int new_fd) const noexcept
Duplicate the file descriptor onto the given file descriptor.
Definition: FileDescriptor.hxx:114
FileDescriptor::WaitWritable
int WaitWritable(int timeout) const noexcept
FileDescriptor::Steal
int Steal() noexcept
Definition: FileDescriptor.hxx:77
FileDescriptor::Undefined
static constexpr FileDescriptor Undefined()
Definition: FileDescriptor.hxx:89
FileDescriptor::operator==
constexpr bool operator==(FileDescriptor other) const
Definition: FileDescriptor.hxx:57
FileDescriptor::FileDescriptor
FileDescriptor()=default
FileDescriptor
An OO wrapper for a UNIX file descriptor.
Definition: FileDescriptor.hxx:49
gcc_pure
#define gcc_pure
Definition: Compiler.h:116
FileDescriptor::SetNonBlocking
void SetNonBlocking() noexcept
Enable non-blocking mode on this file descriptor.
FileDescriptor::SetBlocking
void SetBlocking() noexcept
Enable blocking mode on this file descriptor.
FileDescriptor::Read
ssize_t Read(void *buffer, size_t length) noexcept
Definition: FileDescriptor.hxx:164
FileDescriptor::Get
constexpr int Get() const
Returns the file descriptor.
Definition: FileDescriptor.hxx:69