libhv icon indicating copy to clipboard operation
libhv copied to clipboard

关于Windows下中文路径的问题

Open KivinChiu opened this issue 1 year ago • 2 comments

我因为一些原因,必须在中文Windows下运行http服务,使用libhv时发现所有文件相关的方法是直接使用的标准库的方法,比如hv_exists、hlog的open这些,这些方法在中文windows下,由于系统的locale设置默认是中文936,系统会认为路径是gbk编码而不是utf8,导致无法读取中文路径,所有文件相关操作应该使用宽字符版本才可以正确操作。以hlog.c的打开日志文件句柄为例: #if (defined(OS_WIN) || defined(WIN32)) //修正中文windows路径操作 wchar_t wstr[MAX_PATH]; MultiByteToWideChar(CP_UTF8, 0, logger->cur_logfile, -1, wstr, sizeof(wstr) / sizeof(wstr[0])); logger->fp = wfopen(wstr, L"a"); #else logger->fp = fopen(logger->cur_logfile, "a"); #endif 又比如hbase中的hv_exists:

bool hv_exists(const char* path) {
    if (!path || path[0] == '\0') return false;
#if (defined(WIN32) || defined(_WIN32) || defined(OS_WIN))
    wchar_t wstr[MAX_PATH] = {0};
    MultiByteToWideChar(CP_UTF8, 0, path, -1, wstr, sizeof(wstr) / sizeof(wstr[0]));
    DWORD dwAttrib = GetFileAttributesW(wstr);
    return (dwAttrib != INVALID_FILE_ATTRIBUTES);
#else
    struct stat buffer;
    return (stat(path, &buffer) == 0);
#endif
}

KivinChiu avatar Apr 05 '24 14:04 KivinChiu

没有考虑windows下中文路径问题,如果你适配好了,欢迎提PR

ithewei avatar Apr 07 '24 08:04 ithewei

已经提了PR,坐等大神接受。反正老外也不会关注中文问题,索性全部用中文了哈

xth avatar May 29 '24 02:05 xth