From: tho Date: Thu, 18 Feb 2010 14:47:56 +0000 (+0000) Subject: Utils/Console: fixed parsing ConfigFile if file is empty X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=8ce5d6817ae748496d7db06ff4b35ad496fa3d21;p=senf.git Utils/Console: fixed parsing ConfigFile if file is empty Utils: made SystemException::anyOf() const git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1580 270642c3-0616-0410-b53a-bc976706d245 --- diff --git a/senf/Utils/Console/Config.test.cc b/senf/Utils/Console/Config.test.cc index 2e830da..2e8973d 100644 --- a/senf/Utils/Console/Config.test.cc +++ b/senf/Utils/Console/Config.test.cc @@ -63,6 +63,20 @@ namespace { } +SENF_AUTO_UNIT_TEST(configBundle_empty) +{ + TempFile cfg ("test.cfg"); + cfg << TempFile::close; + + senf::console::ScopedDirectory<> root; + root.add("fun2", senf::console::factory::Command(&fun2)); + + senf::console::ConfigBundle bundle(root); + bundle.add( senf::console::FileConfig("/tmp/test.cfg")); + + SENF_CHECK_NO_THROW( bundle.parse() ); +} + SENF_AUTO_UNIT_TEST(configBundle) { namespace fty = senf::console::factory; diff --git a/senf/Utils/Console/ConfigFile.cci b/senf/Utils/Console/ConfigFile.cci index cff5ced..6b8bf6a 100644 --- a/senf/Utils/Console/ConfigFile.cci +++ b/senf/Utils/Console/ConfigFile.cci @@ -48,7 +48,7 @@ senf::console::detail::ConfigFileSource::ignoreMissing() } prefix_ senf::console::detail::ConfigFileSource::ConfigFileSource(std::string const & filename) - : filename_ (filename), ignoreMissing_ (true) + : filename_ (filename), ignoreMissing_ (false) {} #endif diff --git a/senf/Utils/Console/ConfigFile.test.cc b/senf/Utils/Console/ConfigFile.test.cc index 8369e66..8cb3e30 100644 --- a/senf/Utils/Console/ConfigFile.test.cc +++ b/senf/Utils/Console/ConfigFile.test.cc @@ -62,6 +62,16 @@ namespace { } +#define SENF_CHECK_THROW_SYSTEMEXCEPTION( expr, errorNumber) \ + try { \ + BOOST_TEST_PASSPOINT(); \ + expr; \ + BOOST_ERROR( "senf::SystemException is expected"); \ + } catch( senf::SystemException const & ex ) { \ + BOOST_CHECK( ex.anyOf( errorNumber)); \ + } \ + + SENF_AUTO_UNIT_TEST(configFile) { namespace fty = senf::console::factory; @@ -93,9 +103,18 @@ SENF_AUTO_UNIT_TEST(configFile) { senf::console::ConfigFile cfg ("i don't exist"); + SENF_CHECK_THROW_SYSTEMEXCEPTION( + cfg.parse(), ENOENT); cfg.ignoreMissing(); SENF_CHECK_NO_THROW( cfg.parse() ); } + { + if (getuid() != 0 && boost::filesystem::exists("/etc/shadow")) { + senf::console::ConfigFile cfg ("/etc/shadow"); + SENF_CHECK_THROW_SYSTEMEXCEPTION( + cfg.parse(), EACCES); + } + } } SENF_AUTO_UNIT_TEST(configFileRestrict) diff --git a/senf/Utils/Console/Parse.cc b/senf/Utils/Console/Parse.cc index 40a61fc..5cd4e4c 100644 --- a/senf/Utils/Console/Parse.cc +++ b/senf/Utils/Console/Parse.cc @@ -28,6 +28,7 @@ // Custom includes #include +#include #include #include #include @@ -368,8 +369,21 @@ prefix_ void senf::console::CommandParser::parse(std::string const & command, Ca prefix_ void senf::console::CommandParser::parseFile(std::string const & filename, Callback cb) { + // file_iterator sets errno to EINVAL and returns error when file size is 0 + // so we check the file size before + struct stat statBuf; + if (stat( filename.c_str(), &statBuf) != 0) + throw SystemException(errno SENF_EXC_DEBUGINFO); + if (statBuf.st_size == 0) return; boost::spirit::file_iterator<> i (filename); - if (!i) throw SystemException(ENOENT SENF_EXC_DEBUGINFO); + if (!i) { + if (errno == 0) + // hmm.. errno==0 but the file_iterator is false; something is wrong but we + // do not know what exactly, so we throw a SystemeException with EINVAL + throw SystemException(EINVAL SENF_EXC_DEBUGINFO); + else + throw SystemException(errno SENF_EXC_DEBUGINFO); + } boost::spirit::file_iterator<> const i_end (i.make_end()); parseLoop(i, i_end, filename, cb); } diff --git a/senf/Utils/Exception.cci b/senf/Utils/Exception.cci index 6f7e13d..6e61b52 100644 --- a/senf/Utils/Exception.cci +++ b/senf/Utils/Exception.cci @@ -103,6 +103,7 @@ prefix_ char const * senf::SystemException::errorString() prefix_ bool senf::SystemException::anyOf(int c0, int c1, int c2, int c3, int c4, int c5, int c6, int c7, int c8, int c9) + const { return (c0 && code_ == c0) || diff --git a/senf/Utils/Exception.hh b/senf/Utils/Exception.hh index c6f3257..1bed5ce 100644 --- a/senf/Utils/Exception.hh +++ b/senf/Utils/Exception.hh @@ -295,11 +295,9 @@ namespace senf { char const * errorString() const; ///< Error string (\c strerror() value) bool anyOf(int c0, int c1=0, int c2=0, int c3=0, int c4=0, int c5=0, - int c6=0, int c7=0, int c8=0, int c9=0); + int c6=0, int c7=0, int c8=0, int c9=0) const; ///< \c true, if errorNumber() is one of \a c0 ... \a c9 - - private: void init(std::string const & descr, int code _SENF_EXC_DEBUG_ARGS_ND);