import ctypes
import os


def load_lib(lib_path):
    """
    加载共享库文件
    Load shared library file
    Args:
        lib_path (str): 库文件路径,如： resource/db/ksycopg2/libkci.so.5
    """
    if not os.path.exists(lib_path):
        raise FileNotFoundError(f"Library file not found: {lib_path}")

    # 检查文件权限
    if not os.access(lib_path, os.R_OK):
        raise PermissionError(f"No read permission for library: {lib_path}")

    # 加载库文件
    lib = ctypes.CDLL(lib_path, mode=ctypes.RTLD_GLOBAL)
    return lib