1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
#include "config/configcompiler.hpp"
#include "config/configitem.hpp"
#include "base/logger.hpp"
#include "base/utility.hpp"
#include "base/loader.hpp"
#include "base/context.hpp"
#include "base/exception.hpp"
#include <fstream>
using namespace icinga;
std::vector<String> ConfigCompiler::m_IncludeSearchDirs;
std::mutex ConfigCompiler::m_ZoneDirsMutex;
std::map<String, std::vector<ZoneFragment> > ConfigCompiler::m_ZoneDirs;
/**
* Constructor for the ConfigCompiler class.
*
* @param path The path of the configuration file (or another name that
* identifies the source of the configuration text).
* @param input Input stream for the configuration file.
* @param zone The zone.
*/
ConfigCompiler::ConfigCompiler(String path, std::istream *input,
String zone, String package)
: m_Path(std::move(path)), m_Input(input), m_Zone(std::move(zone)),
m_Package(std::move(package)), m_Eof(false), m_OpenBraces(0)
{
InitializeScanner();
}
/**
* Destructor for the ConfigCompiler class.
*/
ConfigCompiler::~ConfigCompiler()
{
DestroyScanner();
}
/**
* Reads data from the input stream. Used internally by the lexer.
*
* @param buffer Where to store data.
* @param max_size The maximum number of bytes to read from the stream.
* @returns The actual number of bytes read.
*/
size_t ConfigCompiler::ReadInput(char *buffer, size_t max_size)
{
m_Input->read(buffer, max_size);
return static_cast<size_t>(m_Input->gcount());
}
/**
* Retrieves the scanner object.
*
* @returns The scanner object.
*/
void *ConfigCompiler::GetScanner() const
{
return m_Scanner;
}
/**
* Retrieves the path for the input file.
*
* @returns The path.
*/
const char *ConfigCompiler::GetPath() const
{
return m_Path.CStr();
}
void ConfigCompiler::SetZone(const String& zone)
{
m_Zone = zone;
}
String ConfigCompiler::GetZone() const
{
return m_Zone;
}
void ConfigCompiler::SetPackage(const String& package)
{
m_Package = package;
}
String ConfigCompiler::GetPackage() const
{
return m_Package;
}
void ConfigCompiler::CollectIncludes(std::vector<std::unique_ptr<Expression> >& expressions,
const String& file, const String& zone, const String& package)
{
try {
expressions.emplace_back(CompileFile(file, zone, package));
} catch (const std::exception& ex) {
Log(LogWarning, "ConfigCompiler")
<< "Cannot compile file '"
<< file << "': " << DiagnosticInformation(ex);
}
}
/**
* Handles an include directive.
*
* @param relativeBath The path this include is relative to.
* @param path The path from the include directive.
* @param search Whether to search global include dirs.
* @param debuginfo Debug information.
*/
std::unique_ptr<Expression> ConfigCompiler::HandleInclude(const String& relativeBase, const String& path,
bool search, const String& zone, const String& package, const DebugInfo& debuginfo)
{
String upath;
if (search || (IsAbsolutePath(path)))
upath = path;
else
upath = relativeBase + "/" + path;
String includePath = upath;
if (search) {
for (const String& dir : m_IncludeSearchDirs) {
String spath = dir + "/" + path;
if (Utility::PathExists(spath)) {
includePath = spath;
break;
}
}
}
std::vector<std::unique_ptr<Expression> > expressions;
auto funcCallback = [&expressions, zone, package](const String& file) { CollectIncludes(expressions, file, zone, package); };
if (!Utility::Glob(includePath, funcCallback, GlobFile) && includePath.FindFirstOf("*?") == String::NPos) {
std::ostringstream msgbuf;
msgbuf << "Include file '" + path + "' does not exist";
BOOST_THROW_EXCEPTION(ScriptError(msgbuf.str(), debuginfo));
}
std::unique_ptr<DictExpression> expr{new DictExpression(std::move(expressions))};
expr->MakeInline();
return std::move(expr);
}
/**
* Handles recursive includes.
*
* @param relativeBase The path this include is relative to.
* @param path The directory path.
* @param pattern The file pattern.
* @param debuginfo Debug information.
*/
std::unique_ptr<Expression> ConfigCompiler::HandleIncludeRecursive(const String& relativeBase, const String& path,
const String& pattern, const String& zone, const String& package, const DebugInfo&)
{
String ppath;
if (IsAbsolutePath(path))
ppath = path;
else
ppath = relativeBase + "/" + path;
std::vector<std::unique_ptr<Expression> > expressions;
Utility::GlobRecursive(ppath, pattern, [&expressions, zone, package](const String& file) {
CollectIncludes(expressions, file, zone, package);
}, GlobFile);
std::unique_ptr<DictExpression> dict{new DictExpression(std::move(expressions))};
dict->MakeInline();
return std::move(dict);
}
void ConfigCompiler::HandleIncludeZone(const String& relativeBase, const String& tag, const String& path, const String& pattern, const String& package, std::vector<std::unique_ptr<Expression> >& expressions)
{
String zoneName = Utility::BaseName(path);
String ppath;
if (IsAbsolutePath(path))
ppath = path;
else
ppath = relativeBase + "/" + path;
RegisterZoneDir(tag, ppath, zoneName);
Utility::GlobRecursive(ppath, pattern, [&expressions, zoneName, package](const String& file) {
CollectIncludes(expressions, file, zoneName, package);
}, GlobFile);
}
/**
* Handles zone includes.
*
* @param relativeBase The path this include is relative to.
* @param tag The tag name.
* @param path The directory path.
* @param pattern The file pattern.
* @param debuginfo Debug information.
*/
std::unique_ptr<Expression> ConfigCompiler::HandleIncludeZones(const String& relativeBase, const String& tag,
const String& path, const String& pattern, const String& package, const DebugInfo&)
{
String ppath;
String newRelativeBase = relativeBase;
if (IsAbsolutePath(path))
ppath = path;
else {
ppath = relativeBase + "/" + path;
newRelativeBase = ".";
}
std::vector<std::unique_ptr<Expression> > expressions;
Utility::Glob(ppath + "/*", [newRelativeBase, tag, pattern, package, &expressions](const String& path) {
HandleIncludeZone(newRelativeBase, tag, path, pattern, package, expressions);
}, GlobDirectory);
return std::unique_ptr<Expression>(new DictExpression(std::move(expressions)));
}
/**
* Compiles a stream.
*
* @param path A name identifying the stream.
* @param stream The input stream.
* @returns Configuration items.
*/
std::unique_ptr<Expression> ConfigCompiler::CompileStream(const String& path,
std::istream *stream, const String& zone, const String& package)
{
CONTEXT("Compiling configuration stream with name '" << path << "'");
stream->exceptions(std::istream::badbit);
ConfigCompiler ctx(path, stream, zone, package);
try {
return ctx.Compile();
} catch (const ScriptError& ex) {
return std::unique_ptr<Expression>(new ThrowExpression(MakeLiteral(ex.what()), ex.IsIncompleteExpression(), ex.GetDebugInfo()));
} catch (const std::exception& ex) {
return std::unique_ptr<Expression>(new ThrowExpression(MakeLiteral(DiagnosticInformation(ex)), false));
}
}
/**
* Compiles a file.
*
* @param path The path.
* @returns Configuration items.
*/
std::unique_ptr<Expression> ConfigCompiler::CompileFile(const String& path, const String& zone,
const String& package)
{
CONTEXT("Compiling configuration file '" << path << "'");
std::ifstream stream(path.CStr(), std::ifstream::in);
if (!stream)
BOOST_THROW_EXCEPTION(posix_error()
<< boost::errinfo_api_function("std::ifstream::open")
<< boost::errinfo_errno(errno)
<< boost::errinfo_file_name(path));
Log(LogNotice, "ConfigCompiler")
<< "Compiling config file: " << path;
return CompileStream(path, &stream, zone, package);
}
/**
* Compiles a snippet of text.
*
* @param path A name identifying the text.
* @param text The text.
* @returns Configuration items.
*/
std::unique_ptr<Expression> ConfigCompiler::CompileText(const String& path, const String& text,
const String& zone, const String& package)
{
std::stringstream stream(text);
return CompileStream(path, &stream, zone, package);
}
/**
* Adds a directory to the list of include search dirs.
*
* @param dir The new dir.
*/
void ConfigCompiler::AddIncludeSearchDir(const String& dir)
{
Log(LogInformation, "ConfigCompiler")
<< "Adding include search dir: " << dir;
m_IncludeSearchDirs.push_back(dir);
}
std::vector<ZoneFragment> ConfigCompiler::GetZoneDirs(const String& zone)
{
std::unique_lock<std::mutex> lock(m_ZoneDirsMutex);
auto it = m_ZoneDirs.find(zone);
if (it == m_ZoneDirs.end())
return std::vector<ZoneFragment>();
else
return it->second;
}
void ConfigCompiler::RegisterZoneDir(const String& tag, const String& ppath, const String& zoneName)
{
ZoneFragment zf;
zf.Tag = tag;
zf.Path = ppath;
std::unique_lock<std::mutex> lock(m_ZoneDirsMutex);
m_ZoneDirs[zoneName].push_back(zf);
}
bool ConfigCompiler::HasZoneConfigAuthority(const String& zoneName)
{
std::vector<ZoneFragment> zoneDirs = m_ZoneDirs[zoneName];
bool empty = zoneDirs.empty();
if (!empty) {
std::vector<String> paths;
paths.reserve(zoneDirs.size());
for (const ZoneFragment& zf : zoneDirs) {
paths.push_back(zf.Path);
}
Log(LogNotice, "ConfigCompiler")
<< "Registered authoritative config directories for zone '" << zoneName << "': " << Utility::NaturalJoin(paths);
}
return !empty;
}
bool ConfigCompiler::IsAbsolutePath(const String& path)
{
#ifndef _WIN32
return (path.GetLength() > 0 && path[0] == '/');
#else /* _WIN32 */
return !PathIsRelative(path.CStr());
#endif /* _WIN32 */
}
void ConfigCompiler::AddImport(const Expression::Ptr& import)
{
m_Imports.push_back(import);
}
std::vector<Expression::Ptr> ConfigCompiler::GetImports() const
{
return m_Imports;
}
|