wired: Use l_dir_watch for monitoring storage directory

This commit is contained in:
Marcel Holtmann 2018-10-04 23:12:04 +02:00
parent f16e671e62
commit 70f58f640d
1 changed files with 48 additions and 36 deletions

View File

@ -41,7 +41,7 @@ struct network {
}; };
static struct l_queue *network_list; static struct l_queue *network_list;
static struct l_fswatch *storage_watch; static struct l_dir_watch *storage_watch;
static char *network_name_from_filename(const char *filename) static char *network_name_from_filename(const char *filename)
{ {
@ -78,7 +78,7 @@ static bool network_match(const void *a, const void *b)
const struct network *net = a; const struct network *net = a;
const char *name = b; const char *name = b;
return strcmp(net->name, name); return !strcmp(net->name, name);
} }
static struct network *network_lookup(const char *name) static struct network *network_lookup(const char *name)
@ -86,6 +86,37 @@ static struct network *network_lookup(const char *name)
return l_queue_find(network_list, network_match, name); return l_queue_find(network_list, network_match, name);
} }
static void network_create(const char *name)
{
struct network *net;
net = network_lookup(name);
if (!net) {
net = network_new(name);
l_queue_push_tail(network_list, net);
}
}
static void network_remove(const char *name)
{
struct network *net;
net = l_queue_remove_if(network_list, network_match, name);
if (net)
network_free(net);
}
static void network_reload(const char *name)
{
struct network *net;
net = network_lookup(name);
if (!net)
return;
l_debug("Refresh network '%s'", net->name);
}
struct l_settings *network_lookup_security(const char *network) struct l_settings *network_lookup_security(const char *network)
{ {
struct l_settings *conf; struct l_settings *conf;
@ -104,47 +135,28 @@ struct l_settings *network_lookup_security(const char *network)
return conf; return conf;
} }
static void network_storage_watch_cb(struct l_fswatch *watch, static void network_storage_watch_cb(const char *filename,
const char *filename, enum l_dir_watch_event event,
enum l_fswatch_event event,
void *user_data) void *user_data)
{ {
struct network *net;
char *name; char *name;
/*
* Ignore notifications for the actual directory, we can't do
* anything about some of them anyway. Only react to
* notifications for files in the storage directory.
*/
if (!filename)
return;
name = network_name_from_filename(filename); name = network_name_from_filename(filename);
if (!name) if (!name)
return; return;
switch (event) { switch (event) {
case L_FSWATCH_EVENT_DELETE: case L_DIR_WATCH_EVENT_CREATED:
case L_FSWATCH_EVENT_MOVE: network_create(name);
case L_FSWATCH_EVENT_MODIFY: break;
case L_FSWATCH_EVENT_ATTRIB: case L_DIR_WATCH_EVENT_REMOVED:
case L_FSWATCH_EVENT_CREATE: network_remove(name);
/* break;
* For now treat all the operations the same. E.g. they may case L_DIR_WATCH_EVENT_MODIFIED:
* result in the removal of the network (file moved out, not network_reload(name);
* readable or invalid) or the creation of a new network (file break;
* created, permissions granted, syntax fixed, etc.) case L_DIR_WATCH_EVENT_ACCESSED:
* so we always need to re-read the file. break;
*/
net = network_lookup(name);
if (!net) {
net = network_new(name);
l_queue_push_tail(network_list, net);
} else {
l_free(net->name);
net->name = l_strdup(name);
}
} }
l_free(name); l_free(name);
@ -188,7 +200,7 @@ bool network_init(void)
closedir(dir); closedir(dir);
storage_watch = l_fswatch_new(WIRED_STORAGEDIR, storage_watch = l_dir_watch_new(WIRED_STORAGEDIR,
network_storage_watch_cb, NULL, network_storage_watch_cb, NULL,
network_storage_watch_destroy); network_storage_watch_destroy);
@ -197,7 +209,7 @@ bool network_init(void)
void network_exit(void) void network_exit(void)
{ {
l_fswatch_destroy(storage_watch); l_dir_watch_destroy(storage_watch);
l_queue_destroy(network_list, network_free); l_queue_destroy(network_list, network_free);
network_list = NULL; network_list = NULL;