MPD 0.20.6

- Main Page
- Namespaces
- Data Structures
- Files

- File List
- Globals

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) {
74 fd = _fd;
75 }
76
77 int Steal() {
78 assert(IsDefined());
79
80 int _fd = fd;
81 fd = -1;
82 return _fd;
83 }
84
85 void SetUndefined() {
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);
94 bool OpenReadOnly(const char *pathname);
95
96 #ifndef WIN32
97 bool OpenNonBlocking(const char *pathname);
98
99 static bool CreatePipe(FileDescriptor &r, FileDescriptor &w);
100
104 void SetNonBlocking();
105
109 void SetBlocking();
110
114 bool Duplicate(int new_fd) const {
115 return ::dup2(Get(), new_fd) == 0;
116 }
117 #endif
118
119 #ifdef USE_EVENTFD
120 bool CreateEventFD(unsigned initval=0);
121 #endif
122
123 #ifdef USE_SIGNALFD
124 bool CreateSignalFD(const sigset_t *mask);
125 #endif
126
127 #ifdef HAVE_INOTIFY_INIT
128 bool CreateInotify();
129 #endif
130
136 bool Close() {
137 return ::close(Steal()) == 0;
138 }
139
143 bool Rewind();
144
145 off_t Seek(off_t offset) {
146 return lseek(Get(), offset, SEEK_SET);
147 }
148
149 off_t Skip(off_t offset) {
150 return lseek(Get(), offset, SEEK_CUR);
151 }
152
153 gcc_pure
154 off_t Tell() const {
155 return lseek(Get(), 0, SEEK_CUR);
156 }
157
161 gcc_pure
162 off_t GetSize() const;
163
164 ssize_t Read(void *buffer, size_t length) {
165 return ::read(fd, buffer, length);
166 }
167
168 ssize_t Write(const void *buffer, size_t length) {
169 return ::write(fd, buffer, length);
170 }
171
172 #ifndef WIN32
173 int Poll(short events, int timeout) const;
174
175 int WaitReadable(int timeout) const;
176 int WaitWritable(int timeout) const;
177 #endif
178 };
179
180 #endif
check.h
FileDescriptor::SetUndefined
void SetUndefined()
Definition: FileDescriptor.hxx:85
FileDescriptor::WaitWritable
int WaitWritable(int timeout) const
FileDescriptor::FileDescriptor
constexpr FileDescriptor(int _fd)
Definition: FileDescriptor.hxx:55
FileDescriptor::IsDefined
constexpr bool IsDefined() const
Definition: FileDescriptor.hxx:61
FileDescriptor::operator==
constexpr bool operator==(FileDescriptor other) const
Definition: FileDescriptor.hxx:57
FileDescriptor::Open
bool Open(const char *pathname, int flags, mode_t mode=0666)
FileDescriptor::Seek
off_t Seek(off_t offset)
Definition: FileDescriptor.hxx:145
FileDescriptor::Read
ssize_t Read(void *buffer, size_t length)
Definition: FileDescriptor.hxx:164
FileDescriptor::Write
ssize_t Write(const void *buffer, size_t length)
Definition: FileDescriptor.hxx:168
FileDescriptor::GetSize
gcc_pure off_t GetSize() const
Returns the size of the file in bytes, or -1 on error.
FileDescriptor::CreatePipe
static bool CreatePipe(FileDescriptor &r, FileDescriptor &w)
FileDescriptor::Skip
off_t Skip(off_t offset)
Definition: FileDescriptor.hxx:149
FileDescriptor::Poll
int Poll(short events, int timeout) const
FileDescriptor::fd
int fd
Definition: FileDescriptor.hxx:51
FileDescriptor::Tell
gcc_pure off_t Tell() const
Definition: FileDescriptor.hxx:154
Compiler.h
FileDescriptor::OpenNonBlocking
bool OpenNonBlocking(const char *pathname)
FileDescriptor::OpenReadOnly
bool OpenReadOnly(const char *pathname)
FileDescriptor::Duplicate
bool Duplicate(int new_fd) const
Duplicate the file descriptor onto the given file descriptor.
Definition: FileDescriptor.hxx:114
FileDescriptor::Set
void Set(int _fd)
Definition: FileDescriptor.hxx:73
FileDescriptor::SetBlocking
void SetBlocking()
Enable blocking mode on this file descriptor.
FileDescriptor::SetNonBlocking
void SetNonBlocking()
Enable non-blocking mode on this file descriptor.
FileDescriptor::Undefined
static constexpr FileDescriptor Undefined()
Definition: FileDescriptor.hxx:89
FileDescriptor::FileDescriptor
FileDescriptor()=default
FileDescriptor::Close
bool Close()
Close the file descriptor.
Definition: FileDescriptor.hxx:136
FileDescriptor::Steal
int Steal()
Definition: FileDescriptor.hxx:77
FileDescriptor
An OO wrapper for a UNIX file descriptor.
Definition: FileDescriptor.hxx:49
gcc_pure
#define gcc_pure
Definition: Compiler.h:116
FileDescriptor::Rewind
bool Rewind()
Rewind the pointer to the beginning of the file.
FileDescriptor::WaitReadable
int WaitReadable(int timeout) const
FileDescriptor::Get
constexpr int Get() const
Returns the file descriptor.
Definition: FileDescriptor.hxx:69