Commit 4bd67bc2 authored by Max Kellermann's avatar Max Kellermann

db/update/InotifySource: migrate from class Error to C++ exceptions

parent 15607495
...@@ -20,9 +20,9 @@ ...@@ -20,9 +20,9 @@
#include "config.h" #include "config.h"
#include "InotifySource.hxx" #include "InotifySource.hxx"
#include "InotifyDomain.hxx" #include "InotifyDomain.hxx"
#include "util/Error.hxx"
#include "system/FileDescriptor.hxx" #include "system/FileDescriptor.hxx"
#include "system/FatalError.hxx" #include "system/FatalError.hxx"
#include "system/Error.hxx"
#include "Log.hxx" #include "Log.hxx"
#include <sys/inotify.h> #include <sys/inotify.h>
...@@ -67,37 +67,30 @@ InotifySource::OnSocketReady(gcc_unused unsigned flags) ...@@ -67,37 +67,30 @@ InotifySource::OnSocketReady(gcc_unused unsigned flags)
return true; return true;
} }
inline static int
InotifySource::InotifySource(EventLoop &_loop, InotifyInit()
mpd_inotify_callback_t _callback, void *_ctx,
FileDescriptor _fd)
:SocketMonitor(_fd.Get(), _loop),
callback(_callback), callback_ctx(_ctx)
{ {
ScheduleRead(); FileDescriptor fd;
if (!fd.CreateInotify())
throw MakeErrno("inotify_init() has failed");
return fd.Get();
} }
InotifySource * InotifySource::InotifySource(EventLoop &_loop,
InotifySource::Create(EventLoop &loop, mpd_inotify_callback_t _callback, void *_ctx)
mpd_inotify_callback_t callback, void *callback_ctx, :SocketMonitor(InotifyInit(), _loop),
Error &error) callback(_callback), callback_ctx(_ctx)
{ {
FileDescriptor fd; ScheduleRead();
if (!fd.CreateInotify()) {
error.SetErrno("inotify_init() has failed");
return nullptr;
}
return new InotifySource(loop, callback, callback_ctx, fd);
} }
int int
InotifySource::Add(const char *path_fs, unsigned mask, Error &error) InotifySource::Add(const char *path_fs, unsigned mask)
{ {
int wd = inotify_add_watch(Get(), path_fs, mask); int wd = inotify_add_watch(Get(), path_fs, mask);
if (wd < 0) if (wd < 0)
error.SetErrno("inotify_add_watch() has failed"); throw MakeErrno("inotify_add_watch() has failed");
return wd; return wd;
} }
......
...@@ -23,7 +23,6 @@ ...@@ -23,7 +23,6 @@
#include "event/SocketMonitor.hxx" #include "event/SocketMonitor.hxx"
#include "Compiler.h" #include "Compiler.h"
class Error;
class FileDescriptor; class FileDescriptor;
typedef void (*mpd_inotify_callback_t)(int wd, unsigned mask, typedef void (*mpd_inotify_callback_t)(int wd, unsigned mask,
...@@ -33,33 +32,31 @@ class InotifySource final : private SocketMonitor { ...@@ -33,33 +32,31 @@ class InotifySource final : private SocketMonitor {
mpd_inotify_callback_t callback; mpd_inotify_callback_t callback;
void *callback_ctx; void *callback_ctx;
InotifySource(EventLoop &_loop,
mpd_inotify_callback_t callback, void *ctx,
FileDescriptor fd);
public: public:
~InotifySource() {
Close();
}
/** /**
* Creates a new inotify source and registers it in the * Creates a new inotify source and registers it in the
* #EventLoop. * #EventLoop.
* *
* Throws #std::system_error on error.
*
* @param callback a callback invoked for events received from * @param callback a callback invoked for events received from
* the kernel * the kernel
*/ */
static InotifySource *Create(EventLoop &_loop, InotifySource(EventLoop &_loop,
mpd_inotify_callback_t callback, mpd_inotify_callback_t callback, void *ctx);
void *ctx,
Error &error); ~InotifySource() {
Close();
}
/** /**
* Adds a path to the notify list. * Adds a path to the notify list.
* *
* @return a watch descriptor or -1 on error * Throws #std::system_error on error.
*
* @return a watch descriptor
*/ */
int Add(const char *path_fs, unsigned mask, Error &error); int Add(const char *path_fs, unsigned mask);
/** /**
* Removes a path from the notify list. * Removes a path from the notify list.
......
...@@ -25,7 +25,6 @@ ...@@ -25,7 +25,6 @@
#include "storage/StorageInterface.hxx" #include "storage/StorageInterface.hxx"
#include "fs/AllocatedPath.hxx" #include "fs/AllocatedPath.hxx"
#include "fs/FileInfo.hxx" #include "fs/FileInfo.hxx"
#include "util/Error.hxx"
#include "Log.hxx" #include "Log.hxx"
#include <string> #include <string>
...@@ -157,7 +156,6 @@ static void ...@@ -157,7 +156,6 @@ static void
recursive_watch_subdirectories(WatchDirectory *directory, recursive_watch_subdirectories(WatchDirectory *directory,
const AllocatedPath &path_fs, unsigned depth) const AllocatedPath &path_fs, unsigned depth)
{ {
Error error;
DIR *dir; DIR *dir;
struct dirent *ent; struct dirent *ent;
...@@ -187,22 +185,23 @@ recursive_watch_subdirectories(WatchDirectory *directory, ...@@ -187,22 +185,23 @@ recursive_watch_subdirectories(WatchDirectory *directory,
AllocatedPath::Build(path_fs, ent->d_name); AllocatedPath::Build(path_fs, ent->d_name);
FileInfo fi; FileInfo fi;
if (!GetFileInfo(child_path_fs, fi, error)) { try {
LogError(error); fi = FileInfo(child_path_fs);
error.Clear(); } catch (const std::runtime_error &e) {
LogError(e);
continue; continue;
} }
if (!fi.IsDirectory()) if (!fi.IsDirectory())
continue; continue;
ret = inotify_source->Add(child_path_fs.c_str(), IN_MASK, try {
error); ret = inotify_source->Add(child_path_fs.c_str(),
if (ret < 0) { IN_MASK);
FormatError(error, } catch (const std::runtime_error &e) {
FormatError(e,
"Failed to register %s", "Failed to register %s",
child_path_fs.c_str()); child_path_fs.c_str());
error.Clear();
continue; continue;
} }
...@@ -299,20 +298,22 @@ mpd_inotify_init(EventLoop &loop, Storage &storage, UpdateService &update, ...@@ -299,20 +298,22 @@ mpd_inotify_init(EventLoop &loop, Storage &storage, UpdateService &update,
return; return;
} }
Error error; try {
inotify_source = InotifySource::Create(loop, inotify_source = new InotifySource(loop,
mpd_inotify_callback, nullptr, mpd_inotify_callback,
error); nullptr);
if (inotify_source == nullptr) { } catch (const std::runtime_error &e) {
LogError(error); LogError(e);
return; return;
} }
inotify_max_depth = max_depth; inotify_max_depth = max_depth;
int descriptor = inotify_source->Add(path.c_str(), IN_MASK, error); int descriptor;
if (descriptor < 0) { try {
LogError(error); descriptor = inotify_source->Add(path.c_str(), IN_MASK);
} catch (const std::runtime_error &e) {
LogError(e);
delete inotify_source; delete inotify_source;
inotify_source = nullptr; inotify_source = nullptr;
return; return;
......
...@@ -21,7 +21,6 @@ ...@@ -21,7 +21,6 @@
#include "ShutdownHandler.hxx" #include "ShutdownHandler.hxx"
#include "db/update/InotifySource.hxx" #include "db/update/InotifySource.hxx"
#include "event/Loop.hxx" #include "event/Loop.hxx"
#include "util/Error.hxx"
#include "Log.hxx" #include "Log.hxx"
#include <sys/inotify.h> #include <sys/inotify.h>
...@@ -41,7 +40,7 @@ my_inotify_callback(gcc_unused int wd, unsigned mask, ...@@ -41,7 +40,7 @@ my_inotify_callback(gcc_unused int wd, unsigned mask,
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ try {
const char *path; const char *path;
if (argc != 2) { if (argc != 2) {
...@@ -54,24 +53,13 @@ int main(int argc, char **argv) ...@@ -54,24 +53,13 @@ int main(int argc, char **argv)
EventLoop event_loop; EventLoop event_loop;
const ShutdownHandler shutdown_handler(event_loop); const ShutdownHandler shutdown_handler(event_loop);
Error error; InotifySource source(event_loop, my_inotify_callback, nullptr);
InotifySource *source = InotifySource::Create(event_loop, source.Add(path, IN_MASK);
my_inotify_callback,
nullptr, error);
if (source == NULL) {
LogError(error);
return EXIT_FAILURE;
}
int descriptor = source->Add(path, IN_MASK, error);
if (descriptor < 0) {
delete source;
LogError(error);
return EXIT_FAILURE;
}
event_loop.Run(); event_loop.Run();
delete source;
return EXIT_SUCCESS; return EXIT_SUCCESS;
} catch (const std::runtime_error &e) {
LogError(e);
return EXIT_FAILURE;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment