aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNatanael Copa <ncopa@alpinelinux.org>2015-09-24 09:13:11 +0200
committerNatanael Copa <ncopa@alpinelinux.org>2015-09-24 09:13:11 +0200
commitc6b7670ec3aab4f65d480230014bad51d7f4bdd9 (patch)
treeb784138c6cfbcc18beb56fd59262d7bec9a5cbd2
parentedb3c808c174c8a386d2c976457c2803fa1a90f1 (diff)
downloadmkinitfs-c6b7670ec3aab4f65d480230014bad51d7f4bdd9.tar.bz2
mkinitfs-c6b7670ec3aab4f65d480230014bad51d7f4bdd9.tar.xz
nlplug-findfs: refactor recursing dir tree
we need a more general recurse function so we can search for boot repos and similar.
-rw-r--r--nlplug-findfs.c43
1 files changed, 28 insertions, 15 deletions
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;
}