From f86a938a8a7388b776014f9b4c9227628065aea6 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Sun, 19 Apr 2020 17:51:51 -0700 Subject: [PATCH] ircd::magic: Add conf item for magic file path; default to $MAGIC from environment. #138 --- include/ircd/ircd.h | 2 +- ircd/magic.cc | 39 +++++++++++++++++++++++++++++++-------- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/include/ircd/ircd.h b/include/ircd/ircd.h index 0f256a60d..c42a2cad1 100644 --- a/include/ircd/ircd.h +++ b/include/ircd/ircd.h @@ -80,8 +80,8 @@ #include "fmt.h" #include "http.h" #include "http2/http2.h" -#include "magic.h" #include "conf.h" +#include "magic.h" #include "stats.h" #include "prof/prof.h" #include "fs/fs.h" diff --git a/ircd/magic.cc b/ircd/magic.cc index 1467194f2..76b0f25cd 100644 --- a/ircd/magic.cc +++ b/ircd/magic.cc @@ -12,7 +12,7 @@ namespace ircd::magic { - extern const char *const fallback_paths[] + extern const char *const fallback_paths[]; extern magic_t cookie; extern int flags; @@ -35,6 +35,13 @@ ircd::magic::version_abi "magic", info::versions::ABI, ::magic_version() }; +decltype(ircd::magic::file_path) +ircd::magic::file_path +{ + { "name", "ircd.magic.file" }, + { "default", getenv("MAGIC") }, +}; + decltype(ircd::magic::fallback_paths) ircd::magic::fallback_paths { @@ -61,16 +68,32 @@ ircd::magic::flags ircd::magic::init::init() { + const auto load + { + [](const string_view &path) + { + return !empty(path) + && fs::exists(path) + && magic_check(cookie, fs::path_cstr(path)) == 0 + && magic_load(cookie, fs::path_cstr(path)) == 0 + ; + } + }; + version_check(); - if(unlikely(!(cookie = ::magic_open(flags)))) - throw error{"magic_open() failed"}; + throw error + { + "magic_open() failed" + }; - for(const char *const *path{paths}; *path; ++path) - if(fs::exists(*path)) - if(magic_check(cookie, *path) == 0) - if(magic_load(cookie, *path) == 0) - return; + // Try the conf item first + if(load(string_view(file_path))) + return; + + for(const char *const *path{fallback_paths}; *path; ++path) + if(load(*path)) + return; throw error {