mirror of
https://github.com/anope/anope.git
synced 2026-06-30 07:16:38 +02:00
Allow using absolute paths in more places.
This commit is contained in:
@@ -579,6 +579,28 @@ namespace Anope
|
||||
|
||||
/** Update the current time. */
|
||||
extern CoreExport void UpdateTime();
|
||||
|
||||
/** Expands a path fragment that is relative to the base directory.
|
||||
* @param base The base directory that it is relative to.
|
||||
* @param fragment The fragment to expand.
|
||||
*/
|
||||
extern CoreExport Anope::string Expand(const Anope::string &base, const Anope::string &fragment);
|
||||
|
||||
/** Expands a config path. */
|
||||
inline auto ExpandConfig(const Anope::string &path) { return Expand(ConfigDir, path); }
|
||||
|
||||
/** Expands a data path. */
|
||||
inline auto ExpandData(const Anope::string &path) { return Expand(DataDir, path); }
|
||||
|
||||
/** Expands a locale path. */
|
||||
inline auto ExpandLocale(const Anope::string &path) { return Expand(LocaleDir, path); }
|
||||
|
||||
/** Expands a log path. */
|
||||
inline auto ExpandLog(const Anope::string &path) { return Expand(LogDir, path); }
|
||||
|
||||
/** Expands a module path. */
|
||||
inline auto ExpandModule(const Anope::string &path) { return Expand(ModuleDir, path); }
|
||||
|
||||
}
|
||||
|
||||
/** sepstream allows for splitting token separated lists.
|
||||
|
||||
@@ -1290,7 +1290,7 @@ public:
|
||||
|
||||
EventReturn OnLoadDatabase() override
|
||||
{
|
||||
const auto dbname = Anope::DataDir + "/" + Config->GetModule(this)->Get<const Anope::string>("database", "atheme.db");
|
||||
const auto dbname = Anope::ExpandData(Config->GetModule(this)->Get<const Anope::string>("database", "atheme.db"));
|
||||
std::ifstream fd(dbname.str());
|
||||
if (!fd.is_open())
|
||||
{
|
||||
|
||||
@@ -130,8 +130,8 @@ class DBFlatFile final
|
||||
|
||||
for (const auto &db : dbs)
|
||||
{
|
||||
const Anope::string &oldname = Anope::DataDir + "/" + db;
|
||||
Anope::string newname = Anope::DataDir + "/backups/" + db + "-" + Anope::ToString(tm->tm_year + 1900) + Anope::printf("-%02i-", tm->tm_mon + 1) + Anope::printf("%02i", tm->tm_mday);
|
||||
const auto oldname = Anope::ExpandData(db);
|
||||
const auto newname = Anope::ExpandData("backups/" + db + "-" + Anope::ToString(tm->tm_year + 1900) + Anope::printf("-%02i-", tm->tm_mon + 1) + Anope::printf("%02i", tm->tm_mday));
|
||||
|
||||
/* Backup already exists or no database to backup */
|
||||
if (Anope::IsFile(newname) || !Anope::IsFile(oldname))
|
||||
@@ -216,7 +216,7 @@ public:
|
||||
{
|
||||
std::set<Anope::string> tried_dbs;
|
||||
|
||||
const Anope::string &db_name = Anope::DataDir + "/" + Config->GetModule(this)->Get<const Anope::string>("database", "anope.db");
|
||||
const auto db_name = Anope::ExpandData(Config->GetModule(this)->Get<const Anope::string>("database", "anope.db"));
|
||||
|
||||
std::fstream fd(db_name.c_str(), std::ios_base::in | std::ios_base::binary);
|
||||
if (!fd.is_open())
|
||||
@@ -296,9 +296,9 @@ public:
|
||||
|
||||
Anope::string db_name;
|
||||
if (s_type->GetOwner())
|
||||
db_name = Anope::DataDir + "/module_" + s_type->GetOwner()->name + ".db";
|
||||
db_name = Anope::ExpandData("module_" + s_type->GetOwner()->name + ".db");
|
||||
else
|
||||
db_name = Anope::DataDir + "/" + Config->GetModule(this)->Get<const Anope::string>("database", "anope.db");
|
||||
db_name = Anope::ExpandData(Config->GetModule(this)->Get<const Anope::string>("database", "anope.db"));
|
||||
|
||||
std::fstream *fs = databases[s_type->GetOwner()] = new std::fstream((db_name + ".tmp").c_str(), std::ios_base::out | std::ios_base::trunc | std::ios_base::binary);
|
||||
|
||||
@@ -325,7 +325,7 @@ public:
|
||||
|
||||
for (auto &[mod, f] : databases)
|
||||
{
|
||||
const Anope::string &db_name = Anope::DataDir + "/" + (mod ? (mod->name + ".db") : Config->GetModule(this)->Get<const Anope::string>("database", "anope.db"));
|
||||
const auto db_name = Anope::ExpandData((mod ? (mod->name + ".db") : Config->GetModule(this)->Get<const Anope::string>("database", "anope.db")));
|
||||
|
||||
if (!f->is_open() || !f->good())
|
||||
{
|
||||
@@ -367,9 +367,9 @@ public:
|
||||
|
||||
Anope::string db_name;
|
||||
if (stype->GetOwner())
|
||||
db_name = Anope::DataDir + "/module_" + stype->GetOwner()->name + ".db";
|
||||
db_name = Anope::ExpandData("module_" + stype->GetOwner()->name + ".db");
|
||||
else
|
||||
db_name = Anope::DataDir + "/" + Config->GetModule(this)->Get<const Anope::string>("database", "anope.db");
|
||||
db_name = Anope::ExpandData(Config->GetModule(this)->Get<const Anope::string>("database", "anope.db"));
|
||||
|
||||
std::fstream fd(db_name.c_str(), std::ios_base::in | std::ios_base::binary);
|
||||
if (!fd.is_open())
|
||||
|
||||
@@ -321,7 +321,7 @@ static dbFILE *open_db_read(const char *service, const char *filename, int versi
|
||||
int myversion;
|
||||
|
||||
f = new dbFILE;
|
||||
strscpy(f->filename, (Anope::DataDir + "/" + filename).c_str(), sizeof(f->filename));
|
||||
strscpy(f->filename, Anope::ExpandData(filename).c_str(), sizeof(f->filename));
|
||||
f->mode = 'r';
|
||||
fp = fopen(f->filename, "rb");
|
||||
if (!fp)
|
||||
|
||||
@@ -118,8 +118,7 @@ public:
|
||||
|
||||
if (this->SQLiteServices.find(connname) == this->SQLiteServices.end())
|
||||
{
|
||||
Anope::string database = Anope::DataDir + "/" + block->Get<const Anope::string>("database", "anope");
|
||||
|
||||
auto database = Anope::ExpandData(block->Get<const Anope::string>("database", "anope"));
|
||||
try
|
||||
{
|
||||
auto *ss = new SQLiteService(this, connname, database);
|
||||
|
||||
@@ -24,7 +24,7 @@ class CommandOSLogSearch final
|
||||
|
||||
strftime(timestamp, sizeof(timestamp), "%Y%m%d", tm);
|
||||
|
||||
return Anope::LogDir + "/" + file + "." + timestamp;
|
||||
return Anope::ExpandLog(file + "." + timestamp);
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
@@ -61,7 +61,7 @@ public:
|
||||
Configuration::Block *block = Config->GetModule(this);
|
||||
provider_name = block->Get<const Anope::string>("server", "httpd/main");
|
||||
template_name = block->Get<const Anope::string>("template", "default");
|
||||
template_base = Anope::DataDir + "/modules/webcpanel/templates/" + template_name;
|
||||
template_base = Anope::ExpandData("modules/webcpanel/templates/" + template_name);
|
||||
page_title = block->Get<const Anope::string>("title", "Anope IRC Services");
|
||||
|
||||
provider = ServiceReference<HTTPProvider>("HTTPProvider", provider_name);
|
||||
|
||||
+2
-2
@@ -697,7 +697,7 @@ const Anope::string &File::GetName() const
|
||||
|
||||
Anope::string File::GetPath() const
|
||||
{
|
||||
return (this->executable ? "" : Anope::ConfigDir + "/") + this->name;
|
||||
return this->executable ? this->name : Anope::ExpandConfig(this->name);
|
||||
}
|
||||
|
||||
bool File::IsOpen() const
|
||||
@@ -708,7 +708,7 @@ bool File::IsOpen() const
|
||||
bool File::Open()
|
||||
{
|
||||
this->Close();
|
||||
this->fp = (this->executable ? popen(this->name.c_str(), "r") : fopen((Anope::ConfigDir + "/" + this->name).c_str(), "r"));
|
||||
this->fp = (this->executable ? popen(GetPath().c_str(), "r") : fopen(GetPath().c_str(), "r"));
|
||||
return this->fp != NULL;
|
||||
}
|
||||
|
||||
|
||||
+2
-5
@@ -417,12 +417,9 @@ bool Anope::Init(int ac, char **av)
|
||||
}
|
||||
|
||||
Log(LOG_TERMINAL) << "Anope " << Anope::Version() << ", " << Anope::VersionBuildString();
|
||||
Log(LOG_TERMINAL) << "Using configuration file " << Anope::ExpandConfig(ServicesConf.GetName());
|
||||
|
||||
#ifdef _WIN32
|
||||
Log(LOG_TERMINAL) << "Using configuration file " << Anope::ConfigDir << "\\" << ServicesConf.GetName();
|
||||
#else
|
||||
Log(LOG_TERMINAL) << "Using configuration file " << Anope::ConfigDir << "/" << ServicesConf.GetName();
|
||||
|
||||
#ifndef _WIN32
|
||||
/* Fork to background */
|
||||
if (!Anope::NoFork)
|
||||
{
|
||||
|
||||
+1
-1
@@ -52,7 +52,7 @@ static inline Anope::string CreateLogName(const Anope::string &file, time_t t =
|
||||
tm *tm = localtime(&t);
|
||||
strftime(timestamp, sizeof(timestamp), "%Y%m%d", tm);
|
||||
|
||||
return Anope::LogDir + "/" + file + "." + timestamp;
|
||||
return Anope::ExpandLog(file + "." + timestamp);
|
||||
}
|
||||
|
||||
LogFile::LogFile(const Anope::string &name) : filename(name), stream(name.c_str(), std::ios_base::out | std::ios_base::app)
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
#include <climits>
|
||||
#include <numeric>
|
||||
#include <random>
|
||||
#include <filesystem>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#ifndef _WIN32
|
||||
@@ -808,3 +809,26 @@ void Anope::UpdateTime()
|
||||
CurTimeNs = tv.tv_usec * 1000;
|
||||
#endif
|
||||
}
|
||||
|
||||
Anope::string Anope::Expand(const Anope::string& base, const Anope::string& fragment)
|
||||
{
|
||||
// The fragment is an absolute path, don't modify it.
|
||||
if (std::filesystem::path(fragment.str()).is_absolute())
|
||||
return fragment;
|
||||
|
||||
#ifdef _WIN32
|
||||
static constexpr const char separator = '\\';
|
||||
#else
|
||||
static constexpr const char separator = '/';
|
||||
#endif
|
||||
|
||||
// The fragment is relative to a home directory, expand that.
|
||||
if (!fragment.compare(0, 2, "~/", 2))
|
||||
{
|
||||
const auto *homedir = getenv("HOME");
|
||||
if (homedir && *homedir)
|
||||
return Anope::printf("%s%c%s", homedir, separator, fragment.c_str() + 2);
|
||||
}
|
||||
|
||||
return Anope::printf("%s%c%s", base.c_str(), separator, fragment.c_str());
|
||||
}
|
||||
|
||||
+1
-1
@@ -46,7 +46,7 @@ Module::Module(const Anope::string &modname, const Anope::string &, ModType modt
|
||||
Anope::string lang;
|
||||
sepstream(language, '.').GetToken(lang);
|
||||
|
||||
if (Anope::IsFile(Anope::LocaleDir + "/" + lang + "/LC_MESSAGES/" + modname + ".mo"))
|
||||
if (Anope::IsFile(Anope::ExpandLocale(lang + "/LC_MESSAGES/" + modname + ".mo")))
|
||||
{
|
||||
if (!bindtextdomain(this->name.c_str(), Anope::LocaleDir.c_str()))
|
||||
Log() << "Error calling bindtextdomain, " << Anope::LastError();
|
||||
|
||||
@@ -27,7 +27,7 @@ std::vector<Module *> ModuleManager::EventHandlers[I_SIZE];
|
||||
#ifdef _WIN32
|
||||
void ModuleManager::CleanupRuntimeDirectory()
|
||||
{
|
||||
Anope::string dirbuf = Anope::DataDir + "/runtime";
|
||||
Anope::string dirbuf = Anope::ExpandData("runtime");
|
||||
|
||||
Log(LOG_DEBUG) << "Cleaning out Module run time directory (" << dirbuf << ") - this may take a moment, please wait";
|
||||
try
|
||||
@@ -55,7 +55,7 @@ void ModuleManager::CleanupRuntimeDirectory()
|
||||
*/
|
||||
static ModuleReturn moduleCopyFile(const Anope::string &name, Anope::string &output)
|
||||
{
|
||||
Anope::string input = Anope::ModuleDir + "/modules/" + name + DLL_EXT;
|
||||
const auto input = Anope::ExpandModule("modules/" + name + DLL_EXT);
|
||||
|
||||
struct stat s;
|
||||
if (stat(input.c_str(), &s) == -1)
|
||||
@@ -133,7 +133,7 @@ ModuleReturn ModuleManager::LoadModule(const Anope::string &modname, User *u)
|
||||
|
||||
#ifdef _WIN32
|
||||
/* Generate the filename for the temporary copy of the module */
|
||||
Anope::string pbuf = Anope::DataDir + "/runtime/" + modname + DLL_EXT ".XXXXXX";
|
||||
const auto pbuf = Anope::ExpandData("runtime/" + modname + DLL_EXT ".XXXXXX");
|
||||
|
||||
/* Don't skip return value checking! -GD */
|
||||
ModuleReturn ret = moduleCopyFile(modname, pbuf);
|
||||
@@ -146,7 +146,7 @@ ModuleReturn ModuleManager::LoadModule(const Anope::string &modname, User *u)
|
||||
return ret;
|
||||
}
|
||||
#else
|
||||
Anope::string pbuf = Anope::ModuleDir + "/modules/" + modname + DLL_EXT;
|
||||
const auto pbuf = Anope::ExpandModule("modules/" + modname + DLL_EXT);
|
||||
#endif
|
||||
|
||||
dlerror();
|
||||
|
||||
Reference in New Issue
Block a user