mirror of
https://github.com/matrix-construct/construct
synced 2025-03-16 22:41:46 +01:00
ircd: Move magic out of fs::.
This commit is contained in:
parent
fbafdaa851
commit
7142cbf735
9 changed files with 190 additions and 185 deletions
|
@ -87,7 +87,6 @@ namespace ircd::fs
|
|||
extern aio *aioctx;
|
||||
}
|
||||
|
||||
#include "magic.h"
|
||||
#include "read.h"
|
||||
#include "write.h"
|
||||
|
||||
|
|
|
@ -9,11 +9,13 @@
|
|||
// full license for this software is available in the LICENSE file.
|
||||
|
||||
#pragma once
|
||||
#define HAVE_IRCD_FS_MAGIC_H
|
||||
#define HAVE_IRCD_MAGIC_H
|
||||
|
||||
namespace ircd::fs::magic
|
||||
namespace ircd::magic
|
||||
{
|
||||
IRCD_EXCEPTION(fs::error, error)
|
||||
struct init;
|
||||
|
||||
IRCD_EXCEPTION(ircd::error, error)
|
||||
|
||||
int version();
|
||||
|
||||
|
@ -23,3 +25,9 @@ namespace ircd::fs::magic
|
|||
string_view extensions(const mutable_buffer &out, const const_buffer &);
|
||||
string_view description(const mutable_buffer &out, const const_buffer &);
|
||||
}
|
||||
|
||||
struct ircd::magic::init
|
||||
{
|
||||
init();
|
||||
~init() noexcept;
|
||||
};
|
|
@ -217,6 +217,7 @@ namespace ircd
|
|||
#include "openssl.h"
|
||||
#include "http.h"
|
||||
#include "fmt.h"
|
||||
#include "magics.h"
|
||||
#include "fs/fs.h"
|
||||
#include "ios.h"
|
||||
#include "ctx/ctx.h"
|
||||
|
|
|
@ -85,6 +85,7 @@ libircd_la_SOURCES = \
|
|||
base.cc \
|
||||
parse.cc \
|
||||
openssl.cc \
|
||||
magic.cc \
|
||||
fs.cc \
|
||||
ctx.cc \
|
||||
rfc3986.cc \
|
||||
|
|
179
ircd/fs.cc
179
ircd/fs.cc
|
@ -8,7 +8,6 @@
|
|||
// copyright notice and this permission notice is present in all copies. The
|
||||
// full license for this software is available in the LICENSE file.
|
||||
|
||||
#include <RB_INC_MAGIC_H
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <ircd/asio.h>
|
||||
|
||||
|
@ -25,12 +24,6 @@ namespace ircd::fs
|
|||
extern const std::array<ent, num_of<index>()> paths;
|
||||
}
|
||||
|
||||
namespace ircd::fs::magic
|
||||
{
|
||||
static void init();
|
||||
static void fini();
|
||||
}
|
||||
|
||||
/// Non-null when aio is available for use
|
||||
decltype(ircd::fs::aioctx)
|
||||
ircd::fs::aioctx
|
||||
|
@ -52,8 +45,6 @@ ircd::fs::paths
|
|||
|
||||
ircd::fs::init::init()
|
||||
{
|
||||
magic::init();
|
||||
|
||||
#ifdef IRCD_USE_AIO
|
||||
assert(!aioctx);
|
||||
aioctx = new aio{};
|
||||
|
@ -74,7 +65,6 @@ noexcept
|
|||
#endif
|
||||
|
||||
assert(!aioctx);
|
||||
magic::fini();
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -355,172 +345,3 @@ catch(const std::out_of_range &e)
|
|||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// fs/magic.h
|
||||
//
|
||||
|
||||
namespace ircd::fs::magic
|
||||
{
|
||||
magic_t cookie;
|
||||
|
||||
// magic_getflags() may not be available; this will supplement for our
|
||||
// tracking of their flags state.
|
||||
int flags{MAGIC_NONE};
|
||||
|
||||
static void throw_on_error(const magic_t &);
|
||||
static void set_flags(const magic_t &, const int &flags);
|
||||
static string_view call(const magic_t &, const int &flags, const mutable_buffer &, const std::function<const char *()> &);
|
||||
static string_view call(const magic_t &, const int &flags, const mutable_buffer &, const const_buffer &);
|
||||
}
|
||||
|
||||
void
|
||||
ircd::fs::magic::init()
|
||||
{
|
||||
if(unlikely(!(cookie = ::magic_open(flags))))
|
||||
throw error{"magic_open() failed"};
|
||||
|
||||
//TODO: conf; TODO: windows
|
||||
static const char *const paths[]
|
||||
{
|
||||
"/usr/local/share/misc/magic.mgc",
|
||||
"/usr/share/misc/magic.mgc",
|
||||
nullptr
|
||||
};
|
||||
|
||||
for(const char *const *path{paths}; *path; ++path)
|
||||
if(magic_check(cookie, *path) == 0)
|
||||
if(magic_load(cookie, *path) == 0)
|
||||
return;
|
||||
|
||||
throw error
|
||||
{
|
||||
"Failed to open any magic database"
|
||||
};
|
||||
}
|
||||
|
||||
void
|
||||
ircd::fs::magic::fini()
|
||||
{
|
||||
::magic_close(cookie);
|
||||
}
|
||||
|
||||
ircd::string_view
|
||||
ircd::fs::magic::description(const mutable_buffer &out,
|
||||
const const_buffer &buffer)
|
||||
{
|
||||
return call(cookie, MAGIC_NONE, out, buffer);
|
||||
}
|
||||
|
||||
ircd::string_view
|
||||
ircd::fs::magic::extensions(const mutable_buffer &out,
|
||||
const const_buffer &buffer)
|
||||
{
|
||||
return call(cookie, MAGIC_EXTENSION, out, buffer);
|
||||
}
|
||||
|
||||
ircd::string_view
|
||||
ircd::fs::magic::mime_encoding(const mutable_buffer &out,
|
||||
const const_buffer &buffer)
|
||||
{
|
||||
return call(cookie, MAGIC_MIME_ENCODING, out, buffer);
|
||||
}
|
||||
|
||||
ircd::string_view
|
||||
ircd::fs::magic::mime_type(const mutable_buffer &out,
|
||||
const const_buffer &buffer)
|
||||
{
|
||||
return call(cookie, MAGIC_MIME_TYPE, out, buffer);
|
||||
}
|
||||
|
||||
ircd::string_view
|
||||
ircd::fs::magic::mime(const mutable_buffer &out,
|
||||
const const_buffer &buffer)
|
||||
{
|
||||
return call(cookie, MAGIC_MIME, out, buffer);
|
||||
}
|
||||
|
||||
ircd::string_view
|
||||
ircd::fs::magic::call(const magic_t &cookie,
|
||||
const int &flags,
|
||||
const mutable_buffer &out,
|
||||
const const_buffer &buffer)
|
||||
{
|
||||
return call(cookie, flags, out, [&cookie, &buffer]
|
||||
{
|
||||
return magic_buffer(cookie, data(buffer), size(buffer));
|
||||
});
|
||||
}
|
||||
|
||||
ircd::string_view
|
||||
ircd::fs::magic::call(const magic_t &cookie,
|
||||
const int &ours,
|
||||
const mutable_buffer &out,
|
||||
const std::function<const char *()> &closure)
|
||||
{
|
||||
const auto &theirs{magic::flags};
|
||||
const unwind reset{[&theirs, &cookie]
|
||||
{
|
||||
set_flags(cookie, theirs);
|
||||
}};
|
||||
|
||||
set_flags(cookie, ours);
|
||||
string_view str
|
||||
{
|
||||
closure()
|
||||
};
|
||||
|
||||
if(!str)
|
||||
{
|
||||
throw_on_error(cookie);
|
||||
str = "application/octet-stream"_sv;
|
||||
}
|
||||
|
||||
return { data(out), copy(out, str) };
|
||||
}
|
||||
|
||||
void
|
||||
ircd::fs::magic::set_flags(const magic_t &cookie,
|
||||
const int &flags)
|
||||
{
|
||||
if(magic_setflags(cookie, flags) == -1)
|
||||
throw_on_error(cookie);
|
||||
}
|
||||
|
||||
void
|
||||
ircd::fs::magic::throw_on_error(const magic_t &cookie)
|
||||
{
|
||||
const char *const errstr
|
||||
{
|
||||
::magic_error(cookie)
|
||||
};
|
||||
|
||||
if(errstr)
|
||||
throw error
|
||||
{
|
||||
"%s", errstr
|
||||
};
|
||||
|
||||
assert(magic_errno(cookie) == 0);
|
||||
}
|
||||
|
||||
int
|
||||
ircd::fs::magic::version()
|
||||
{
|
||||
return ::magic_version();
|
||||
}
|
||||
|
||||
__attribute__((constructor))
|
||||
static void
|
||||
ircd_fs_magic_version_check()
|
||||
{
|
||||
if(unlikely(MAGIC_VERSION != ircd::fs::magic::version()))
|
||||
{
|
||||
fprintf(stderr, "FATAL: linked libmagic version %d != compiled magic.h version %d.\n",
|
||||
ircd::fs::magic::version(),
|
||||
MAGIC_VERSION);
|
||||
|
||||
std::terminate();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -38,7 +38,7 @@ ircd::info::init()
|
|||
db::version,
|
||||
nacl::version(),
|
||||
openssl::version(),
|
||||
fs::magic::version()
|
||||
magic::version()
|
||||
};
|
||||
|
||||
// This message flashes posix information about the system and platform IRCd
|
||||
|
|
|
@ -211,6 +211,7 @@ try
|
|||
// also occur in ircd::init() or static initialization itself if either are
|
||||
// more appropriate.
|
||||
|
||||
magic::init _magic_; // libmagic
|
||||
fs::init _fs_; // Local filesystem
|
||||
ctx::ole::init _ole_; // Thread OffLoad Engine
|
||||
nacl::init _nacl_; // nacl crypto
|
||||
|
|
174
ircd/magic.cc
Normal file
174
ircd/magic.cc
Normal file
|
@ -0,0 +1,174 @@
|
|||
// Matrix Construct
|
||||
//
|
||||
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
||||
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice is present in all copies. The
|
||||
// full license for this software is available in the LICENSE file.
|
||||
|
||||
#include <RB_INC_MAGIC_H
|
||||
|
||||
namespace ircd::magic
|
||||
{
|
||||
magic_t cookie;
|
||||
|
||||
// magic_getflags() may not be available; this will supplement for our
|
||||
// tracking of their flags state.
|
||||
int flags{MAGIC_NONE};
|
||||
|
||||
static void throw_on_error(const magic_t &);
|
||||
static void set_flags(const magic_t &, const int &flags);
|
||||
static string_view call(const magic_t &, const int &flags, const mutable_buffer &, const std::function<const char *()> &);
|
||||
static string_view call(const magic_t &, const int &flags, const mutable_buffer &, const const_buffer &);
|
||||
}
|
||||
|
||||
ircd::magic::init::init()
|
||||
{
|
||||
if(unlikely(!(cookie = ::magic_open(flags))))
|
||||
throw error{"magic_open() failed"};
|
||||
|
||||
//TODO: conf; TODO: windows
|
||||
static const char *const paths[]
|
||||
{
|
||||
"/usr/local/share/misc/magic.mgc",
|
||||
"/usr/share/misc/magic.mgc",
|
||||
nullptr
|
||||
};
|
||||
|
||||
for(const char *const *path{paths}; *path; ++path)
|
||||
if(magic_check(cookie, *path) == 0)
|
||||
if(magic_load(cookie, *path) == 0)
|
||||
return;
|
||||
|
||||
throw error
|
||||
{
|
||||
"Failed to open any magic database"
|
||||
};
|
||||
}
|
||||
|
||||
ircd::magic::init::~init()
|
||||
noexcept
|
||||
{
|
||||
::magic_close(cookie);
|
||||
}
|
||||
|
||||
ircd::string_view
|
||||
ircd::magic::description(const mutable_buffer &out,
|
||||
const const_buffer &buffer)
|
||||
{
|
||||
return call(cookie, MAGIC_NONE, out, buffer);
|
||||
}
|
||||
|
||||
ircd::string_view
|
||||
ircd::magic::extensions(const mutable_buffer &out,
|
||||
const const_buffer &buffer)
|
||||
{
|
||||
return call(cookie, MAGIC_EXTENSION, out, buffer);
|
||||
}
|
||||
|
||||
ircd::string_view
|
||||
ircd::magic::mime_encoding(const mutable_buffer &out,
|
||||
const const_buffer &buffer)
|
||||
{
|
||||
return call(cookie, MAGIC_MIME_ENCODING, out, buffer);
|
||||
}
|
||||
|
||||
ircd::string_view
|
||||
ircd::magic::mime_type(const mutable_buffer &out,
|
||||
const const_buffer &buffer)
|
||||
{
|
||||
return call(cookie, MAGIC_MIME_TYPE, out, buffer);
|
||||
}
|
||||
|
||||
ircd::string_view
|
||||
ircd::magic::mime(const mutable_buffer &out,
|
||||
const const_buffer &buffer)
|
||||
{
|
||||
return call(cookie, MAGIC_MIME, out, buffer);
|
||||
}
|
||||
|
||||
ircd::string_view
|
||||
ircd::magic::call(const magic_t &cookie,
|
||||
const int &flags,
|
||||
const mutable_buffer &out,
|
||||
const const_buffer &buffer)
|
||||
{
|
||||
return call(cookie, flags, out, [&cookie, &buffer]
|
||||
{
|
||||
return magic_buffer(cookie, data(buffer), size(buffer));
|
||||
});
|
||||
}
|
||||
|
||||
ircd::string_view
|
||||
ircd::magic::call(const magic_t &cookie,
|
||||
const int &ours,
|
||||
const mutable_buffer &out,
|
||||
const std::function<const char *()> &closure)
|
||||
{
|
||||
const auto &theirs{magic::flags};
|
||||
const unwind reset{[&theirs, &cookie]
|
||||
{
|
||||
set_flags(cookie, theirs);
|
||||
}};
|
||||
|
||||
set_flags(cookie, ours);
|
||||
string_view str
|
||||
{
|
||||
closure()
|
||||
};
|
||||
|
||||
if(!str)
|
||||
{
|
||||
throw_on_error(cookie);
|
||||
str = "application/octet-stream"_sv;
|
||||
}
|
||||
|
||||
return { data(out), copy(out, str) };
|
||||
}
|
||||
|
||||
void
|
||||
ircd::magic::set_flags(const magic_t &cookie,
|
||||
const int &flags)
|
||||
{
|
||||
if(magic_setflags(cookie, flags) == -1)
|
||||
throw_on_error(cookie);
|
||||
}
|
||||
|
||||
void
|
||||
ircd::magic::throw_on_error(const magic_t &cookie)
|
||||
{
|
||||
const char *const errstr
|
||||
{
|
||||
::magic_error(cookie)
|
||||
};
|
||||
|
||||
if(errstr)
|
||||
throw error
|
||||
{
|
||||
"%s", errstr
|
||||
};
|
||||
|
||||
assert(magic_errno(cookie) == 0);
|
||||
}
|
||||
|
||||
int
|
||||
ircd::magic::version()
|
||||
{
|
||||
return ::magic_version();
|
||||
}
|
||||
|
||||
__attribute__((constructor))
|
||||
static void
|
||||
ircd_magic_version_check()
|
||||
{
|
||||
if(unlikely(MAGIC_VERSION != ircd::magic::version()))
|
||||
{
|
||||
fprintf(stderr, "FATAL: linked libmagic version %d != compiled magic.h version %d.\n",
|
||||
ircd::magic::version(),
|
||||
MAGIC_VERSION);
|
||||
|
||||
std::terminate();
|
||||
}
|
||||
}
|
|
@ -71,7 +71,7 @@ get_root(client &client, const resource::request &request)
|
|||
case hash("txt"): content_type = "text/plain; charset=utf-8"; break;
|
||||
default:
|
||||
{
|
||||
content_type = fs::magic::mime(content_type_buf, string_view{content});
|
||||
content_type = magic::mime(content_type_buf, string_view{content});
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue