event/SocketMonitor: use class SocketDescriptor

This commit is contained in:
Max Kellermann
2017-08-10 18:25:22 +02:00
parent fcfc8bacc0
commit 492b20a89d
21 changed files with 80 additions and 83 deletions

View File

@@ -38,7 +38,8 @@ InotifySource::OnSocketReady(gcc_unused unsigned flags)
static_assert(sizeof(buffer) >= sizeof(struct inotify_event) + NAME_MAX + 1,
"inotify buffer too small");
ssize_t nbytes = read(Get(), buffer, sizeof(buffer));
auto ifd = Get().ToFileDescriptor();
ssize_t nbytes = ifd.Read(buffer, sizeof(buffer));
if (nbytes < 0)
FatalSystemError("Failed to read from inotify");
if (nbytes == 0)
@@ -67,19 +68,20 @@ InotifySource::OnSocketReady(gcc_unused unsigned flags)
return true;
}
static int
static FileDescriptor
InotifyInit()
{
FileDescriptor fd;
if (!fd.CreateInotify())
throw MakeErrno("inotify_init() has failed");
return fd.Get();
return fd;
}
InotifySource::InotifySource(EventLoop &_loop,
mpd_inotify_callback_t _callback, void *_ctx)
:SocketMonitor(InotifyInit(), _loop),
:SocketMonitor(SocketDescriptor::FromFileDescriptor(InotifyInit()),
_loop),
callback(_callback), callback_ctx(_ctx)
{
ScheduleRead();
@@ -88,7 +90,8 @@ InotifySource::InotifySource(EventLoop &_loop,
int
InotifySource::Add(const char *path_fs, unsigned mask)
{
int wd = inotify_add_watch(Get(), path_fs, mask);
auto ifd = Get().ToFileDescriptor();
int wd = inotify_add_watch(ifd.Get(), path_fs, mask);
if (wd < 0)
throw MakeErrno("inotify_add_watch() has failed");
@@ -98,7 +101,8 @@ InotifySource::Add(const char *path_fs, unsigned mask)
void
InotifySource::Remove(unsigned wd)
{
int ret = inotify_rm_watch(Get(), wd);
auto ifd = Get().ToFileDescriptor();
int ret = inotify_rm_watch(ifd.Get(), wd);
if (ret < 0 && errno != EINVAL)
LogErrno(inotify_domain, "inotify_rm_watch() has failed");