]> git.netwichtig.de Git - user/henk/code/inspircd.git/commitdiff
Close files opened by configreader
authordanieldg <danieldg@e03df62e-2008-0410-955e-edbf42e46eb7>
Mon, 19 Oct 2009 04:09:30 +0000 (04:09 +0000)
committerdanieldg <danieldg@e03df62e-2008-0410-955e-edbf42e46eb7>
Mon, 19 Oct 2009 04:09:30 +0000 (04:09 +0000)
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@11915 e03df62e-2008-0410-955e-edbf42e46eb7

include/logger.h
src/configreader.cpp
src/logger.cpp
src/mode.cpp

index 4914e86a2fe992acd3e90281bbf7609b72b52d3f..e3b7ee0a5b8c119559f9fbe26b2aa885a5554f8c 100644 (file)
@@ -147,18 +147,8 @@ class CoreExport LogManager
 
  public:
 
-       LogManager()
-       {
-               noforkstream = NULL;
-               Logging = false;
-       }
-
-       ~LogManager()
-       {
-               if (noforkstream)
-                       delete noforkstream;
-               Logging = true;
-       }
+       LogManager();
+       ~LogManager();
 
        /** Sets up the logstream for -nofork. Called by InspIRCd::OpenLog() and LogManager::OpenFileLogs().
         * First time called it creates the nofork stream and stores it in noforkstream. Each call thereafter just readds it to GlobalLogStreams
index 80d123842e1756b4c77a48d1fd00e27db0914a51..545ff86c2e3ddcc6e308cf45f6183c3f3041f248 100644 (file)
@@ -203,7 +203,9 @@ struct Parser
                nextword(name);
 
                int spc = next();
-               if (!isspace(spc))
+               if (spc == '>')
+                       unget(spc);
+               else if (!isspace(spc))
                        throw CoreException("Invalid character in tag name");
 
                if (name.empty())
@@ -303,6 +305,20 @@ void ParseStack::DoInclude(ConfigTag* tag, int flags)
        }
 }
 
+/** RAII wrapper on FILE* to close files on exceptions */
+struct FileWrapper
+{
+       FILE* const f;
+       FileWrapper(FILE* file) : f(file) {}
+       operator bool() { return f; }
+       operator FILE*() { return f; }
+       ~FileWrapper()
+       {
+               if (f)
+                       fclose(f);
+       }
+};
+
 bool ParseStack::ParseFile(const std::string& name, int flags)
 {
        ServerInstance->Logs->Log("CONFIG", DEBUG, "Reading file %s", name.c_str());
@@ -316,7 +332,7 @@ bool ParseStack::ParseFile(const std::string& name, int flags)
 
        /* It's not already included, add it to the list of files we've loaded */
 
-       FILE* file = fopen(name.c_str(), "r");
+       FileWrapper file(fopen(name.c_str(), "r"));
        if (!file)
                throw CoreException("Could not read \"" + name + "\" for include");
 
@@ -340,7 +356,7 @@ bool ParseStack::ParseExec(const std::string& name, int flags)
 
        /* It's not already included, add it to the list of files we've loaded */
 
-       FILE* file = popen(name.c_str(), "r");
+       FileWrapper file(popen(name.c_str(), "r"));
        if (!file)
                throw CoreException("Could not open executable \"" + name + "\" for include");
 
@@ -1245,31 +1261,23 @@ bool ServerConfig::ReadFile(file_cache &F, const std::string& fname)
        if (fname.empty())
                return false;
 
-       FILE* file = NULL;
        char linebuf[MAXBUF];
 
        F.clear();
 
-       if (!FileExists(fname.c_str()))
-               return false;
-       file = fopen(fname.c_str(), "r");
+       FileWrapper file(fopen(fname.c_str(), "r"));
 
-       if (file)
+       if (!file)
+               return false;
+       while (!feof(file))
        {
-               while (!feof(file))
-               {
-                       if (fgets(linebuf, sizeof(linebuf), file))
-                               linebuf[strlen(linebuf)-1] = 0;
-                       else
-                               *linebuf = 0;
-
-                       F.push_back(*linebuf ? linebuf : " ");
-               }
+               if (fgets(linebuf, sizeof(linebuf), file))
+                       linebuf[strlen(linebuf)-1] = 0;
+               else
+                       *linebuf = 0;
 
-               fclose(file);
+               F.push_back(*linebuf ? linebuf : " ");
        }
-       else
-               return false;
 
        return true;
 }
index be28bf836dab939f210105ecfde73be013034c71..19e07e78f64188b5b08e2724ced28d08f853409d 100644 (file)
  *
  */
 
+LogManager::LogManager()
+{
+       noforkstream = NULL;
+       Logging = false;
+}
+
+LogManager::~LogManager()
+{
+       if (noforkstream)
+       {
+               ServerInstance->Logs = this;
+               delete noforkstream;
+               ServerInstance->Logs = NULL;
+       }
+}
+
 void LogManager::SetupNoFork()
 {
        if (!noforkstream)
index 47553f238294278fa8ebc4f166044943a9e87d7c..65a027e900ac35d5e99d1d7f230419be48c290de 100644 (file)
@@ -55,13 +55,14 @@ ModeHandler::ModeHandler(Module* Creator, const std::string& Name, char modelett
 
 CullResult ModeHandler::cull()
 {
-       ServerInstance->Modes->DelMode(this);
+       if (ServerInstance->Modes)
+               ServerInstance->Modes->DelMode(this);
        return classbase::cull();
 }
 
 ModeHandler::~ModeHandler()
 {
-       if (ServerInstance && ServerInstance->Modes->FindMode(mode, m_type) == this)
+       if (ServerInstance && ServerInstance->Modes && ServerInstance->Modes->FindMode(mode, m_type) == this)
                ServerInstance->Logs->Log("MODE", DEBUG, "ERROR: Destructor for mode %c called while not culled", mode);
 }
 
@@ -1026,7 +1027,7 @@ ModeParser::ModeParser()
 
 ModeParser::~ModeParser()
 {
-       ModeHandler* mh = ServerInstance->Modes->FindMode('h', MODETYPE_CHANNEL);
+       ModeHandler* mh = FindMode('h', MODETYPE_CHANNEL);
        if (mh)
        {
                mh->cull();