* #14569: yamlparser: guarantee that the file exists for the duration of yamlparser's lifetime

Before it was possible that it was deleted in between the time it was
checked and the time the YamlParser was created.
This commit is contained in:
Tristan Matthews
2012-08-17 12:06:47 -04:00
parent 94cc6fdb16
commit 551f3ce03e
4 changed files with 14 additions and 27 deletions

View File

@ -40,8 +40,7 @@
namespace Conf {
YamlParser::YamlParser(const char *file) : filename_(file)
, fd_(0)
YamlParser::YamlParser(FILE *fd) : fd_(fd)
, parser_()
, events_()
, eventNumber_(0)
@ -58,7 +57,6 @@ YamlParser::YamlParser(const char *file) : filename_(file)
, voiplinkNode_(NULL)
, shortcutNode_(NULL)
{
fd_ = fopen(filename_.c_str(), "rb");
if (!fd_)
throw YamlParserException("Could not open file descriptor");
@ -129,10 +127,8 @@ YamlParser::getShortcutNode()
YamlParser::~YamlParser()
{
if (fd_) {
fclose(fd_);
if (fd_)
yaml_parser_delete(&parser_);
}
for (int i = 0; i < eventNumber_; ++i)
yaml_event_delete(&events_[i]);

View File

@ -51,8 +51,7 @@ typedef std::vector<yaml_event_t> YamlEventVector;
class YamlParserException : public std::runtime_error {
public:
YamlParserException(const std::string& str="") :
std::runtime_error("YamlParserException occured: " + str) {}
YamlParserException(const char *err) : std::runtime_error(err) {}
};
@ -60,7 +59,7 @@ class YamlParser {
public:
YamlParser(const char *file);
YamlParser(FILE *fd);
~YamlParser();
@ -107,11 +106,6 @@ class YamlParser {
void mainNativeDataMapping(MappingNode *map);
/**
* Configuration file name
*/
std::string filename_;
/**
* Configuration file descriptor
*/

View File

@ -103,26 +103,20 @@ void ManagerImpl::init(const std::string &config_file)
DEBUG("Configuration file path: %s", path_.c_str());
try {
std::fstream testFileExistence(path_.c_str(), std::fstream::in);
bool fileExist = testFileExistence.good();
testFileExistence.close();
FILE *file = fopen(path_.c_str(), "rb");
if(fileExist) {
Conf::YamlParser parser(path_.c_str());
if (file) {
Conf::YamlParser parser(file);
parser.serializeEvents();
parser.composeEvents();
parser.constructNativeData();
loadAccountMap(parser);
}
else {
fclose(file);
} else {
WARN("Config file not found: creating default account map");
loadDefaultAccountMap();
}
}
catch (const Conf::YamlParserException &e) {
ERROR("%s", e.what());
}
catch(std::fstream::failure &e) {
} catch (const Conf::YamlParserException &e) {
ERROR("%s", e.what());
}

View File

@ -91,10 +91,13 @@ void ConfigurationTest::testInitAudioDriver()
void ConfigurationTest::testYamlParser()
{
try {
Conf::YamlParser parser("ymlParser.yml");
FILE *file = fopen("ymlParser.yml", "rb");
Conf::YamlParser parser(file);
parser.serializeEvents();
parser.composeEvents();
parser.constructNativeData();
if (file)
fclose(file);
} catch (const Conf::YamlParserException &e) {
ERROR("ConfigTree: %s", e.what());
}