识别和显示日志消息中的文件类型

sunninghao_branch
snh 1 month ago
parent 831fb4611e
commit f31505947f

@ -64,14 +64,16 @@ class Magic:
return magic_file(self.cookie, filename) return magic_file(self.cookie, filename)
def __del__(self): def __del__(self):
# during shutdown magic_close may have been cleared already # 析构函数,确保在对象被垃圾回收时关闭 libmagic cookie
if self.cookie and magic_close: if self.cookie and magic_close:
magic_close(self.cookie) magic_close(self.cookie)
self.cookie = None self.cookie = None
# 全局变量用于保存默认和MIME magic对象
_magic_mime = None _magic_mime = None
_magic = None _magic = None
# 获取默认和MIME magic对象的函数
def _get_magic_mime(): def _get_magic_mime():
global _magic_mime global _magic_mime
if not _magic_mime: if not _magic_mime:
@ -90,6 +92,7 @@ def _get_magic_type(mime):
else: else:
return _get_magic() return _get_magic()
# 公共函数,用于识别文件和缓冲区
def from_file(filename, mime=False): def from_file(filename, mime=False):
m = _get_magic_type(mime) m = _get_magic_type(mime)
return m.from_file(filename) return m.from_file(filename)
@ -98,6 +101,7 @@ def from_buffer(buffer, mime=False):
m = _get_magic_type(mime) m = _get_magic_type(mime)
return m.from_buffer(buffer) return m.from_buffer(buffer)
# 使用 ctypes 导入 libmagic 库
try: try:
libmagic = None libmagic = None
@ -106,7 +110,7 @@ try:
from ctypes import c_char_p, c_int, c_size_t, c_void_p from ctypes import c_char_p, c_int, c_size_t, c_void_p
# Let's try to find magic or magic1 # 尝试找到 libmagic 库
dll = ctypes.util.find_library('magic') or ctypes.util.find_library('magic1') dll = ctypes.util.find_library('magic') or ctypes.util.find_library('magic1')
# This is necessary because find_library returns None if it doesn't find the library # This is necessary because find_library returns None if it doesn't find the library
@ -116,6 +120,7 @@ try:
except WindowsError: except WindowsError:
pass pass
# 如果没有找到,尝试平台特定的路径
if not libmagic or not libmagic._name: if not libmagic or not libmagic._name:
platform_to_lib = {'darwin': ['/opt/local/lib/libmagic.dylib', platform_to_lib = {'darwin': ['/opt/local/lib/libmagic.dylib',
'/usr/local/lib/libmagic.dylib', '/usr/local/lib/libmagic.dylib',
@ -126,11 +131,13 @@ try:
libmagic = ctypes.CDLL(dll) libmagic = ctypes.CDLL(dll)
except OSError: except OSError:
pass pass
# 如果仍然没有找到,抛出 ImportError
if not libmagic or not libmagic._name: if not libmagic or not libmagic._name:
# It is better to raise an ImportError since we are importing magic module # It is better to raise an ImportError since we are importing magic module
raise ImportError('failed to find libmagic. Check your installation') raise ImportError('failed to find libmagic. Check your installation')
# 定义 magic_t 类型和错误检查函数
magic_t = ctypes.c_void_p magic_t = ctypes.c_void_p
def errorcheck(result, func, args): def errorcheck(result, func, args):
@ -145,6 +152,7 @@ try:
return None return None
return filename.encode(sys.getfilesystemencoding()) return filename.encode(sys.getfilesystemencoding())
# 使用 ctypes 定义 libmagic 函数
magic_open = libmagic.magic_open magic_open = libmagic.magic_open
magic_open.restype = magic_t magic_open.restype = magic_t
magic_open.argtypes = [c_int] magic_open.argtypes = [c_int]
@ -198,28 +206,31 @@ try:
magic_compile.restype = c_int magic_compile.restype = c_int
magic_compile.argtypes = [magic_t, c_char_p] magic_compile.argtypes = [magic_t, c_char_p]
# 如果 libmagic 无法导入,定义回退函数
except (ImportError, OSError): except (ImportError, OSError):
from_file = from_buffer = lambda *args, **kwargs: MAGIC_UNKNOWN_FILETYPE from_file = from_buffer = lambda *args, **kwargs: MAGIC_UNKNOWN_FILETYPE
MAGIC_NONE = 0x000000 # No flags
MAGIC_DEBUG = 0x000001 # Turn on debugging # 定义 libmagic 标志常量
MAGIC_SYMLINK = 0x000002 # Follow symlinks MAGIC_NONE = 0x000000 # 无标志
MAGIC_COMPRESS = 0x000004 # Check inside compressed files MAGIC_DEBUG = 0x000001 # 打开调试
MAGIC_DEVICES = 0x000008 # Look at the contents of devices MAGIC_SYMLINK = 0x000002 # 跟随符号链接
MAGIC_MIME = 0x000010 # Return a mime string MAGIC_COMPRESS = 0x000004 # 检查压缩文件内部
MAGIC_MIME_ENCODING = 0x000400 # Return the MIME encoding MAGIC_DEVICES = 0x000008 # 查看设备内容
MAGIC_CONTINUE = 0x000020 # Return all matches MAGIC_MIME = 0x000010 # 返回 MIME 字符串
MAGIC_CHECK = 0x000040 # Print warnings to stderr MAGIC_MIME_ENCODING = 0x000400 # 返回 MIME 编码
MAGIC_PRESERVE_ATIME = 0x000080 # Restore access time on exit MAGIC_CONTINUE = 0x000020 # 返回所有匹配项
MAGIC_RAW = 0x000100 # Don't translate unprintable chars MAGIC_CHECK = 0x000040 # 打印警告到标准错误
MAGIC_ERROR = 0x000200 # Handle ENOENT etc as real errors MAGIC_PRESERVE_ATIME = 0x000080 # 退出时恢复访问时间
MAGIC_NO_CHECK_COMPRESS = 0x001000 # Don't check for compressed files MAGIC_RAW = 0x000100 # 不转换不可打印字符
MAGIC_NO_CHECK_TAR = 0x002000 # Don't check for tar files MAGIC_ERROR = 0x000200 # 将 ENOENT 等视为真实错误
MAGIC_NO_CHECK_SOFT = 0x004000 # Don't check magic entries MAGIC_NO_CHECK_COMPRESS = 0x001000 # 不检查压缩文件
MAGIC_NO_CHECK_APPTYPE = 0x008000 # Don't check application type MAGIC_NO_CHECK_TAR = 0x002000 # 不检查 tar 文件
MAGIC_NO_CHECK_ELF = 0x010000 # Don't check for elf details MAGIC_NO_CHECK_SOFT = 0x004000 # 不检查 magic 条目
MAGIC_NO_CHECK_ASCII = 0x020000 # Don't check for ascii files MAGIC_NO_CHECK_APPTYPE = 0x008000 # 不检查应用程序类型
MAGIC_NO_CHECK_TROFF = 0x040000 # Don't check ascii/troff MAGIC_NO_CHECK_ELF = 0x010000 # 不检查 elf 详细信息
MAGIC_NO_CHECK_FORTRAN = 0x080000 # Don't check ascii/fortran MAGIC_NO_CHECK_ASCII = 0x020000 # 不检查 ascii 文件
MAGIC_NO_CHECK_TOKENS = 0x100000 # Don't check ascii/tokens MAGIC_NO_CHECK_TROFF = 0x040000 # 不检查 ascii/troff
MAGIC_NO_CHECK_FORTRAN = 0x080000 # 不检查 ascii/fortran
MAGIC_NO_CHECK_TOKENS = 0x100000 # 不检查 ascii/tokens
MAGIC_UNKNOWN_FILETYPE = b"unknown" MAGIC_UNKNOWN_FILETYPE = b"unknown"

Loading…
Cancel
Save