We load the whole file into memory and then parse it. Although this is not * the optimal approach, it does mean that most of this code can be shared with * the bufferlist loading function. Since bufferlists are always in-memory, the * load_from_buffer interface works well for them. * In general, configuration files should be a few kilobytes at maximum, so * loading the whole configuration in
| 106 | * loading the whole configuration into memory shouldn't be a problem. |
| 107 | */ |
| 108 | int ConfFile::parse_file(const std::string &fname, |
| 109 | std::ostream *warnings) |
| 110 | { |
| 111 | clear(); |
| 112 | try { |
| 113 | if (auto file_size = fs::file_size(fname); file_size > MAX_CONFIG_FILE_SZ) { |
| 114 | *warnings << __func__ << ": config file '" << fname |
| 115 | << "' is " << file_size << " bytes, " |
| 116 | << "but the maximum is " << MAX_CONFIG_FILE_SZ; |
| 117 | return -EINVAL; |
| 118 | } |
| 119 | } catch (const fs::filesystem_error& e) { |
| 120 | std::error_code ec; |
| 121 | auto is_other = fs::is_other(fname, ec); |
| 122 | if (!ec && is_other) { |
| 123 | // /dev/null? |
| 124 | return 0; |
| 125 | } else { |
| 126 | *warnings << __func__ << ": " << e.what(); |
| 127 | return -e.code().value(); |
| 128 | } |
| 129 | } |
| 130 | std::ifstream ifs{fname}; |
| 131 | std::string buffer{std::istreambuf_iterator<char>(ifs), |
| 132 | std::istreambuf_iterator<char>()}; |
| 133 | if (parse_buffer(buffer, warnings)) { |
| 134 | return 0; |
| 135 | } else { |
| 136 | return -EINVAL; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | namespace { |
| 141 |