diff --git a/src/helperFunctions/magic.py b/src/helperFunctions/magic.py new file mode 100644 index 0000000000..15c3013f2a --- /dev/null +++ b/src/helperFunctions/magic.py @@ -0,0 +1,38 @@ +"""This is a wrapper around pymagic. +It aims to provide the same API but with the ability to load multiple magic +files in the default api. +""" +import magic as pymagic +from ctypes import c_int, c_char_p + +from helperFunctions.fileSystem import get_src_dir + +_magic_getpath = pymagic.libmagic.magic_getpath +_magic_getpath.restype = c_char_p +_magic_getpath.argtypes = [c_char_p, c_int] + +_sys_magic = _magic_getpath(None, 1) +_fact_magic = f'{get_src_dir()}/bin/fact.mgc' +_internal_symlink_magic = f'{get_src_dir()}/bin/internal_symlink.mgc' + +_magic_by_mime = {} + + +def _get_magic(mime: bool) -> pymagic.Magic: + if mime not in _magic_by_mime: + _magic_by_mime[mime] = pymagic.Magic( + mime=mime, + magic_file=f'{_internal_symlink_magic}:{_fact_magic}:{_sys_magic}', + ) + + return _magic_by_mime[mime] + + +def from_file(filename, mime=False): + """A wrapper for pymagic's ``magic.Magic.from_file``""" + return _get_magic(mime).from_file(filename) + + +def from_buffer(filename, mime=False): + """A wrapper for pymagic's ``magic.Magic.from_buffer``""" + return _get_magic(mime).from_buffer(filename) diff --git a/src/install/common.py b/src/install/common.py index 2e0427c6ac..fed7a3d953 100644 --- a/src/install/common.py +++ b/src/install/common.py @@ -46,6 +46,11 @@ def main(distribution): BIN_DIR.mkdir(exist_ok=True) + with OperateInDirectory('../../src/bin'): + run_cmd_with_logging('wget https://github.com/maringuu/fact-mime-database/releases/download/v0.1.0/fact.mgc') + run_cmd_with_logging(f'file -C -m {INSTALL_DIR / "internal_symlink_magic"}') + run_cmd_with_logging(f'mv {INSTALL_DIR / "internal_symlink_magic.mgc"} {BIN_DIR}') + apt_packages_path = INSTALL_DIR / 'apt-pkgs-common.txt' dnf_packages_path = INSTALL_DIR / 'dnf-pkgs-common.txt' diff --git a/src/install/frontend.py b/src/install/frontend.py index 0f29f8b7e6..0f75a18863 100644 --- a/src/install/frontend.py +++ b/src/install/frontend.py @@ -144,6 +144,9 @@ def main(skip_docker, radare, nginx, distribution): pkgs = read_package_list_from_file(INSTALL_DIR / 'dnf-pkgs-frontend.txt') dnf_install_packages(*pkgs) + with OperateInDirectory('../../src/bin'): + run_cmd_with_logging('wget https://github.com/maringuu/fact-mime-database/releases/download/v0.1.0/fact.mgc') + # flask-security is not maintained anymore and replaced by flask-security-too. # Since python package naming conflicts are not resolved automatically, we remove flask-security manually. pip = 'pip' if is_virtualenv() else 'sudo -EH pip3' diff --git a/src/install/internal_symlink_magic b/src/install/internal_symlink_magic new file mode 100644 index 0000000000..5bb311d268 --- /dev/null +++ b/src/install/internal_symlink_magic @@ -0,0 +1,6 @@ +# ====================== faf internal ====================== + +# ---- faf internal link representation ---- +0 string symbolic\ link\ -> symbolic link +>17 string x to '%s' +!:mime inode/symlink