diff options
author | Lee Campbell <leecam@google.com> | 2015-07-24 16:57:14 -0700 |
---|---|---|
committer | Lee Campbell <leecam@google.com> | 2015-07-27 14:56:03 -0700 |
commit | f13b1b31399aa501514eb9beeef303d1ae2e0e14 (patch) | |
tree | 67eee7556d9a1f0ccbdfe9f1108c12645010cf42 /init/init_parser.cpp | |
parent | 5f3b05ad14db2a4ebc9eb25567bc951d465b3151 (diff) |
init: Adding support to import directories
Support added so init scripts can now import directories.
BUG: 22721249
Change-Id: I02b566bfb50ea84469f1ea0c6ad205435a1df286
TEST: Tested importing a folder on arm64 emulator
Diffstat (limited to 'init/init_parser.cpp')
-rw-r--r-- | init/init_parser.cpp | 36 |
1 files changed, 33 insertions, 3 deletions
diff --git a/init/init_parser.cpp b/init/init_parser.cpp index 86e9ce68f..460f5acd2 100644 --- a/init/init_parser.cpp +++ b/init/init_parser.cpp @@ -15,6 +15,7 @@ */ #include <ctype.h> +#include <dirent.h> #include <errno.h> #include <fcntl.h> #include <inttypes.h> @@ -32,6 +33,7 @@ #include "property_service.h" #include "util.h" +#include <base/stringprintf.h> #include <cutils/iosched_policy.h> #include <cutils/list.h> @@ -385,15 +387,15 @@ static void parse_config(const char *fn, const std::string& data) parser_done: list_for_each(node, &import_list) { struct import* import = node_to_item(node, struct import, list); - if (!init_parse_config_file(import->filename)) { + if (!init_parse_config(import->filename)) { ERROR("could not import file '%s' from '%s': %s\n", import->filename, fn, strerror(errno)); } } } -bool init_parse_config_file(const char* path) { - INFO("Parsing %s...\n", path); +static bool init_parse_config_file(const char* path) { + INFO("Parsing file %s...\n", path); Timer t; std::string data; if (!read_file(path, &data)) { @@ -408,6 +410,34 @@ bool init_parse_config_file(const char* path) { return true; } +static bool init_parse_config_dir(const char* path) { + INFO("Parsing directory %s...\n", path); + std::unique_ptr<DIR, int(*)(DIR*)> config_dir(opendir(path), closedir); + if (!config_dir) { + ERROR("Could not import directory '%s'\n", path); + return false; + } + dirent* current_file; + while ((current_file = readdir(config_dir.get()))) { + std::string current_path = + android::base::StringPrintf("%s/%s", path, current_file->d_name); + // Ignore directories and only process regular files. + if (current_file->d_type == DT_REG) { + if (!init_parse_config_file(current_path.c_str())) { + ERROR("could not import file '%s'\n", current_path.c_str()); + } + } + } + return true; +} + +bool init_parse_config(const char* path) { + if (is_dir(path)) { + return init_parse_config_dir(path); + } + return init_parse_config_file(path); +} + static int valid_name(const char *name) { if (strlen(name) > 16) { |