utils/filedescriptor: add helper to query if the fd is readable

This commit is contained in:
Xaver Hugl 2023-07-24 23:33:04 +02:00
parent 7c24242300
commit a64e43e6b1
2 changed files with 13 additions and 0 deletions

View file

@ -9,6 +9,7 @@
#include "filedescriptor.h"
#include <fcntl.h>
#include <sys/poll.h>
#include <unistd.h>
#include <utility>
@ -72,4 +73,14 @@ FileDescriptor FileDescriptor::duplicate() const
return {};
}
}
bool FileDescriptor::isReadable() const
{
pollfd fd = {
.fd = m_fd,
.events = POLLIN,
.revents = 0,
};
return poll(&fd, 1, 0) && (fd.revents & (POLLIN | POLLNVAL)) != 0;
}
}

View file

@ -28,6 +28,8 @@ public:
void reset();
FileDescriptor duplicate() const;
bool isReadable() const;
private:
int m_fd = -1;
};