From c6b7670ec3aab4f65d480230014bad51d7f4bdd9 Mon Sep 17 00:00:00 2001 From: Natanael Copa Date: Thu, 24 Sep 2015 09:13:11 +0200 Subject: nlplug-findfs: refactor recursing dir tree we need a more general recurse function so we can search for boot repos and similar. --- nlplug-findfs.c | 43 ++++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) (limited to 'nlplug-findfs.c') diff --git a/nlplug-findfs.c b/nlplug-findfs.c index f7fa0ca..b934fda 100644 --- a/nlplug-findfs.c +++ b/nlplug-findfs.c @@ -379,7 +379,12 @@ int process_uevent(char *buf, const size_t len, struct ueventconf *conf) return dispatch_uevent(&ev, conf); } -void trigger_events(const char *dir) +struct recurse_opts { + const char *searchname; + void (*callback)(const char *); +}; + +void recurse_dir(const char *dir, struct recurse_opts *opts) { DIR *d = opendir(dir); struct dirent *entry; @@ -389,32 +394,40 @@ void trigger_events(const char *dir) while ((entry = readdir(d)) != NULL) { char path[PATH_MAX]; - if (!(entry->d_type & DT_DIR) \ - && strcmp(entry->d_name, "uevent") != 0) - continue; - - if (entry->d_name[0] == '.') + if (entry->d_type & DT_DIR) { + if (entry->d_name[0] == '.') + continue; + } else if (opts->searchname + && strcmp(entry->d_name, opts->searchname) != 0) { continue; + } snprintf(path, sizeof(path), "%s/%s", dir, entry->d_name); - if (entry->d_type & DT_DIR) - trigger_events(path); - else { - int fd = open(path, O_WRONLY); - write(fd, "add", 3); - close(fd); - } + recurse_dir(path, opts); + else + opts->callback(path); } closedir(d); } +void trigger_uevent_cb(const char *path) +{ + int fd = open(path, O_WRONLY); + write(fd, "add", 3); + close(fd); +} + void *trigger_thread(void *data) { int fd = *(int *)data; uint64_t ok = 1; - trigger_events("/sys/bus"); - trigger_events("/sys/devices"); + struct recurse_opts opts = { + .searchname = "uevent", + .callback = trigger_uevent_cb, + }; + recurse_dir("/sys/bus", &opts); + recurse_dir("/sys/devices", &opts); write(fd, &ok, sizeof(ok)); return NULL; } -- cgit v1.2.3