From f9ffe6f417dd739162fcad0fa465b4789d64f8a4 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Mon, 28 Mar 2011 21:05:39 +0200 Subject: [PATCH] Move m_sHomePath from CZNC to CFile Thanks to this change, linking the following object files produces no unresolved symbols: FileUtils.o Utils.o ZNCString.o MD5.o SHA256.o The idea here is that ZNC is a little better modularized. Signed-off-by: Uli Schlachter --- FileUtils.cpp | 29 ++++++++++++++++++++++++++--- FileUtils.h | 5 +++++ znc.cpp | 24 ++---------------------- znc.h | 3 +-- 4 files changed, 34 insertions(+), 27 deletions(-) diff --git a/FileUtils.cpp b/FileUtils.cpp index 6a8bd16c..34877914 100644 --- a/FileUtils.cpp +++ b/FileUtils.cpp @@ -7,12 +7,12 @@ */ #include "FileUtils.h" -#include "znc.h" #include "Utils.h" #include #include #include #include +#include #ifndef HAVE_LSTAT # define lstat(a, b) stat(a, b) @@ -22,6 +22,8 @@ # define O_BINARY 0 #endif +CString CFile::m_sHomePath; + CFile::CFile() { m_iFD = -1; } @@ -38,7 +40,7 @@ CFile::~CFile() { void CFile::SetFileName(const CString& sLongName) { if (sLongName.Left(2) == "~/") { - m_sLongName = CZNC::Get().GetHomePath() + sLongName.substr(1); + m_sLongName = CFile::GetHomePath() + sLongName.substr(1); } else m_sLongName = sLongName; @@ -426,11 +428,32 @@ CString CFile::GetDir() const { return sDir; } +void CFile::InitHomePath(const CString& sFallback) { + const char *home = getenv("HOME"); + + m_sHomePath.clear(); + if (home) { + m_sHomePath = home; + } + + if (m_sHomePath.empty()) { + const struct passwd* pUserInfo = getpwuid(getuid()); + + if (pUserInfo) { + m_sHomePath = pUserInfo->pw_dir; + } + } + + if (m_sHomePath.empty()) { + m_sHomePath = sFallback; + } +} + CString CDir::ChangeDir(const CString& sPath, const CString& sAdd, const CString& sHome) { CString sHomeDir(sHome); if (sHomeDir.empty()) { - sHomeDir = CZNC::Get().GetHomePath(); + sHomeDir = CFile::GetHomePath(); } if (sAdd == "~") { diff --git a/FileUtils.h b/FileUtils.h index 3be7165e..a3c061bb 100644 --- a/FileUtils.h +++ b/FileUtils.h @@ -120,6 +120,9 @@ public: CString GetShortName() const; CString GetDir() const; + static void InitHomePath(const CString& sFallback); + static const CString& GetHomePath() { return m_sHomePath; } + private: // fcntl() locking wrapper bool Lock(int iType, bool bBlocking); @@ -127,6 +130,8 @@ private: CString m_sBuffer; int m_iFD; + static CString m_sHomePath; + protected: CString m_sLongName; //!< Absolute filename (m_sPath + "/" + m_sShortName) CString m_sShortName; //!< Filename alone, without path diff --git a/znc.cpp b/znc.cpp index 07465290..b4e9e56a 100644 --- a/znc.cpp +++ b/znc.cpp @@ -12,7 +12,6 @@ #include "Server.h" #include "User.h" #include "Listener.h" -#include #include namespace @@ -418,8 +417,6 @@ bool CZNC::AllowConnectionFrom(const CString& sIP) const { } void CZNC::InitDirs(const CString& sArgvPath, const CString& sDataDir) { - char *home; - // If the bin was not ran from the current directory, we need to add that dir onto our cwd CString::size_type uPos = sArgvPath.rfind('/'); if (uPos == CString::npos) @@ -428,27 +425,10 @@ void CZNC::InitDirs(const CString& sArgvPath, const CString& sDataDir) { m_sCurPath = CDir::ChangeDir("./", sArgvPath.Left(uPos), ""); // Try to set the user's home dir, default to binpath on failure - home = getenv("HOME"); - - m_sHomePath.clear(); - if (home) { - m_sHomePath = home; - } - - if (m_sHomePath.empty()) { - struct passwd* pUserInfo = getpwuid(getuid()); - - if (pUserInfo) { - m_sHomePath = pUserInfo->pw_dir; - } - } - - if (m_sHomePath.empty()) { - m_sHomePath = m_sCurPath; - } + CFile::InitHomePath(m_sCurPath); if (sDataDir.empty()) { - m_sZNCPath = m_sHomePath + "/.znc"; + m_sZNCPath = CFile::GetHomePath() + "/.znc"; } else { m_sZNCPath = sDataDir; } diff --git a/znc.h b/znc.h index cb46ed54..5b334975 100644 --- a/znc.h +++ b/znc.h @@ -99,7 +99,7 @@ public: CString GetSkinName() const { return m_sSkinName; } const CString& GetStatusPrefix() const { return m_sStatusPrefix; } const CString& GetCurPath() const { if (!CFile::Exists(m_sCurPath)) { CDir::MakeDir(m_sCurPath); } return m_sCurPath; } - const CString& GetHomePath() const { if (!CFile::Exists(m_sHomePath)) { CDir::MakeDir(m_sHomePath); } return m_sHomePath; } + const CString& GetHomePath() const { return CFile::GetHomePath(); } const CString& GetZNCPath() const { if (!CFile::Exists(m_sZNCPath)) { CDir::MakeDir(m_sZNCPath); } return m_sZNCPath; } CString GetConfPath(bool bAllowMkDir = true) const; CString GetUserPath() const; @@ -165,7 +165,6 @@ protected: CSockManager m_Manager; CString m_sCurPath; - CString m_sHomePath; CString m_sZNCPath; CString m_sConfigFile;