InotifyQueue: convert to a class
This commit is contained in:
parent
8e3982dd42
commit
c8b408beae
|
@ -23,9 +23,6 @@
|
||||||
#include "Main.hxx"
|
#include "Main.hxx"
|
||||||
#include "event/Loop.hxx"
|
#include "event/Loop.hxx"
|
||||||
|
|
||||||
#include <list>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
#include <glib.h>
|
#include <glib.h>
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
@ -42,28 +39,19 @@ enum {
|
||||||
INOTIFY_UPDATE_DELAY_S = 5,
|
INOTIFY_UPDATE_DELAY_S = 5,
|
||||||
};
|
};
|
||||||
|
|
||||||
static std::list<std::string> inotify_queue;
|
InotifyQueue::~InotifyQueue()
|
||||||
static guint queue_source_id;
|
|
||||||
|
|
||||||
void
|
|
||||||
mpd_inotify_queue_init(void)
|
|
||||||
{
|
{
|
||||||
|
if (source_id != 0)
|
||||||
|
g_source_remove(source_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
inline bool
|
||||||
mpd_inotify_queue_finish(void)
|
InotifyQueue::Run()
|
||||||
{
|
|
||||||
if (queue_source_id != 0)
|
|
||||||
g_source_remove(queue_source_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
static gboolean
|
|
||||||
mpd_inotify_run_update(G_GNUC_UNUSED gpointer data)
|
|
||||||
{
|
{
|
||||||
unsigned id;
|
unsigned id;
|
||||||
|
|
||||||
while (!inotify_queue.empty()) {
|
while (!queue.empty()) {
|
||||||
const char *uri_utf8 = inotify_queue.front().c_str();
|
const char *uri_utf8 = queue.front().c_str();
|
||||||
|
|
||||||
id = update_enqueue(uri_utf8, false);
|
id = update_enqueue(uri_utf8, false);
|
||||||
if (id == 0)
|
if (id == 0)
|
||||||
|
@ -72,14 +60,21 @@ mpd_inotify_run_update(G_GNUC_UNUSED gpointer data)
|
||||||
|
|
||||||
g_debug("updating '%s' job=%u", uri_utf8, id);
|
g_debug("updating '%s' job=%u", uri_utf8, id);
|
||||||
|
|
||||||
inotify_queue.pop_front();
|
queue.pop_front();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* done, remove the timer event by returning false */
|
/* done, remove the timer event by returning false */
|
||||||
queue_source_id = 0;
|
source_id = 0;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
gboolean
|
||||||
|
InotifyQueue::Run(gpointer data)
|
||||||
|
{
|
||||||
|
InotifyQueue &queue = *(InotifyQueue *)data;
|
||||||
|
return queue.Run();
|
||||||
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
path_in(const char *path, const char *possible_parent)
|
path_in(const char *path, const char *possible_parent)
|
||||||
{
|
{
|
||||||
|
@ -91,16 +86,14 @@ path_in(const char *path, const char *possible_parent)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
mpd_inotify_enqueue(const char *uri_utf8)
|
InotifyQueue::Enqueue(const char *uri_utf8)
|
||||||
{
|
{
|
||||||
if (queue_source_id != 0)
|
if (source_id != 0)
|
||||||
g_source_remove(queue_source_id);
|
g_source_remove(source_id);
|
||||||
queue_source_id = main_loop->AddTimeoutSeconds(INOTIFY_UPDATE_DELAY_S,
|
source_id = main_loop->AddTimeoutSeconds(INOTIFY_UPDATE_DELAY_S,
|
||||||
mpd_inotify_run_update,
|
Run, nullptr);
|
||||||
nullptr);
|
|
||||||
|
|
||||||
for (auto i = inotify_queue.begin(), end = inotify_queue.end();
|
for (auto i = queue.begin(), end = queue.end(); i != end;) {
|
||||||
i != end;) {
|
|
||||||
const char *current_uri = i->c_str();
|
const char *current_uri = i->c_str();
|
||||||
|
|
||||||
if (path_in(uri_utf8, current_uri))
|
if (path_in(uri_utf8, current_uri))
|
||||||
|
@ -111,10 +104,10 @@ mpd_inotify_enqueue(const char *uri_utf8)
|
||||||
/* existing path is a sub-path of the new
|
/* existing path is a sub-path of the new
|
||||||
path; we can dequeue the existing path and
|
path; we can dequeue the existing path and
|
||||||
update the new path instead */
|
update the new path instead */
|
||||||
i = inotify_queue.erase(i);
|
i = queue.erase(i);
|
||||||
else
|
else
|
||||||
++i;
|
++i;
|
||||||
}
|
}
|
||||||
|
|
||||||
inotify_queue.emplace_back(uri_utf8);
|
queue.emplace_back(uri_utf8);
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,13 +20,23 @@
|
||||||
#ifndef MPD_INOTIFY_QUEUE_HXX
|
#ifndef MPD_INOTIFY_QUEUE_HXX
|
||||||
#define MPD_INOTIFY_QUEUE_HXX
|
#define MPD_INOTIFY_QUEUE_HXX
|
||||||
|
|
||||||
void
|
#include <glib.h>
|
||||||
mpd_inotify_queue_init(void);
|
|
||||||
|
|
||||||
void
|
#include <list>
|
||||||
mpd_inotify_queue_finish(void);
|
#include <string>
|
||||||
|
|
||||||
void
|
class InotifyQueue {
|
||||||
mpd_inotify_enqueue(const char *uri_utf8);
|
std::list<std::string> queue;
|
||||||
|
guint source_id;
|
||||||
|
|
||||||
|
public:
|
||||||
|
~InotifyQueue();
|
||||||
|
|
||||||
|
void Enqueue(const char *uri_utf8);
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool Run();
|
||||||
|
static gboolean Run(gpointer ctx);
|
||||||
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -57,6 +57,7 @@ struct watch_directory {
|
||||||
};
|
};
|
||||||
|
|
||||||
static InotifySource *inotify_source;
|
static InotifySource *inotify_source;
|
||||||
|
static InotifyQueue *inotify_queue;
|
||||||
|
|
||||||
static unsigned inotify_max_depth;
|
static unsigned inotify_max_depth;
|
||||||
static struct watch_directory inotify_root;
|
static struct watch_directory inotify_root;
|
||||||
|
@ -295,7 +296,7 @@ mpd_inotify_callback(int wd, unsigned mask,
|
||||||
: g_strdup("");
|
: g_strdup("");
|
||||||
|
|
||||||
if (uri_utf8 != NULL) {
|
if (uri_utf8 != NULL) {
|
||||||
mpd_inotify_enqueue(uri_utf8);
|
inotify_queue->Enqueue(uri_utf8);
|
||||||
g_free(uri_utf8);
|
g_free(uri_utf8);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -341,7 +342,7 @@ mpd_inotify_init(unsigned max_depth)
|
||||||
|
|
||||||
recursive_watch_subdirectories(&inotify_root, path, 0);
|
recursive_watch_subdirectories(&inotify_root, path, 0);
|
||||||
|
|
||||||
mpd_inotify_queue_init();
|
inotify_queue = new InotifyQueue();
|
||||||
|
|
||||||
g_debug("watching music directory");
|
g_debug("watching music directory");
|
||||||
}
|
}
|
||||||
|
@ -367,7 +368,7 @@ mpd_inotify_finish(void)
|
||||||
if (inotify_source == NULL)
|
if (inotify_source == NULL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
mpd_inotify_queue_finish();
|
delete inotify_queue;
|
||||||
delete inotify_source;
|
delete inotify_source;
|
||||||
|
|
||||||
g_tree_foreach(inotify_directories, free_watch_directory, NULL);
|
g_tree_foreach(inotify_directories, free_watch_directory, NULL);
|
||||||
|
|
Loading…
Reference in New Issue