int nrefs;
// Configuration
- int configfd;
+ nw_configd* configd;
nw_config* config;
// Event Loop
*/
static int nw_daemon_config_open(nw_daemon* daemon, const char* path) {
- // Open the directory
- daemon->configfd = open(path, O_DIRECTORY);
- if (daemon->configfd < 0) {
- ERROR("Could not open %s: %m\n", path);
- return -errno;
- }
-
- return 0;
-}
-
-FILE* nw_daemon_config_fopen(nw_daemon* daemon, const char* path, const char* mode) {
- // Open the file
- int fd = openat(daemon->configfd, path, 0);
- if (fd < 0) {
- ERROR("Could not open configuration file %s: %m\n", path);
- return NULL;
- }
-
- // Return a file handle
- return fdopen(fd, mode);
-}
+ int r;
-DIR* nw_daemon_config_opendir(nw_daemon* daemon, const char* path) {
- int fd = openat(daemon->configfd, path, O_DIRECTORY);
- if (fd < 0) {
- ERROR("Could not open configuration directory %s: %m\n", path);
- return NULL;
- }
+ // Open the configuration directory
+ r = nw_configd_create(&daemon->configd, path);
+ if (r < 0)
+ return r;
- return fdopendir(fd);
+ return 0;
}
static int nw_daemon_parse_argv(nw_daemon* daemon, int argc, char* argv[]) {
}
static int nw_daemon_load_config(nw_daemon* daemon) {
- FILE* f = NULL;
int r;
// If no configuration path has been opened yet, we will open something
- if (!daemon->configfd) {
+ if (!daemon->configd) {
r = nw_daemon_config_open(daemon, CONFIG_DIR);
if (r < 0)
- goto ERROR;
+ return r;
}
// Open the configuration file
- f = nw_daemon_config_fopen(daemon, "settings", "r");
- if (!f) {
- r = -errno;
- goto ERROR;
- }
-
- // Create configuration
- r = nw_config_create(&daemon->config, f);
- if (r < 0)
- goto ERROR;
-
-ERROR:
- if (f)
- fclose(f);
-
- return r;
+ return nw_configd_open_config(&daemon->config, daemon->configd, "settings");
}
static int nw_start_device_monitor(nw_daemon* daemon) {
// Cleanup common objects
nw_daemon_cleanup(daemon);
- if (daemon->configfd > 0)
- close(daemon->configfd);
+ if (daemon->configd)
+ nw_configd_unref(daemon->configd);
if (daemon->stats_collector_event)
sd_event_source_unref(daemon->stats_collector_event);
if (daemon->bus)
return 0;
}
+nw_configd* nw_daemon_configd(nw_daemon* daemon, const char* path) {
+ if (!daemon->configd)
+ return NULL;
+
+ if (path)
+ return nw_configd_descend(daemon->configd, path);
+
+ return nw_configd_ref(daemon->configd);
+}
+
/*
Bus
*/