Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于Windows下中文路径的问题 #514

Open
KivinChiu opened this issue Apr 5, 2024 · 1 comment
Open

关于Windows下中文路径的问题 #514

KivinChiu opened this issue Apr 5, 2024 · 1 comment

Comments

@KivinChiu
Copy link

我因为一些原因,必须在中文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
}
@ithewei
Copy link
Owner

ithewei commented Apr 7, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants