fix unicode issue that ie.ReadNetwork fails if the exe's folder conta… (#12720)

* fix unicode issue that ie.ReadNetwork fails if the exe's folder contains Chinese characters

* fix clang format issue

* fix clang format issue

* modify '/' to file_separator in get_directory

* fix ci issue

* fix test failure on windows

* fix test issue

Co-authored-by: Ilya Lavrenov <ilya.lavrenov@intel.com>
This commit is contained in:
Sun Xiaoxia
2022-08-30 16:12:37 +08:00
committed by GitHub
parent c5588f6f46
commit 756659b624
4 changed files with 94 additions and 0 deletions

View File

@@ -194,6 +194,9 @@ inline FilePath to_file_path(const std::string& path) {
return string_to_wstring(path);
}
std::wstring get_directory(const std::wstring& path);
std::wstring path_join_w(const std::vector<std::wstring>& paths);
#else
using FilePath = std::string;

View File

@@ -87,6 +87,18 @@ std::string ov::util::get_directory(const std::string& s) {
return rc;
}
#ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT
std::wstring ov::util::get_directory(const std::wstring& s) {
std::wstring rc = s;
auto pos = s.find_last_of(ov::util::FileTraits<wchar_t>::file_separator);
if (pos != std::wstring::npos) {
rc = s.substr(0, pos);
return rc;
}
return rc;
}
#endif
namespace {
std::string join_paths(const std::string& s1, const std::string& s2) {
@@ -108,6 +120,28 @@ std::string join_paths(const std::string& s1, const std::string& s2) {
}
return rc;
}
#ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT
std::wstring join_paths(const std::wstring& s1, const std::wstring& s2) {
std::wstring rc;
if (s2.size() > 0) {
if (s2[0] == '/') {
rc = s2;
} else if (s1.size() > 0) {
rc = s1;
if (rc[rc.size() - 1] != '/') {
rc += '/';
}
rc += s2;
} else {
rc = s2;
}
} else {
rc = s1;
}
return rc;
}
#endif
} // namespace
std::string ov::util::path_join(const std::vector<std::string>& paths) {
@@ -122,6 +156,20 @@ std::string ov::util::path_join(const std::vector<std::string>& paths) {
return result;
}
#ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT
std::wstring ov::util::path_join_w(const std::vector<std::wstring>& paths) {
std::wstring result;
if (paths.empty()) {
return result;
}
result = paths[0];
for (size_t i = 1; i < paths.size(); i++) {
result = join_paths(result, paths[i]);
}
return result;
}
#endif
#ifndef _WIN32
static void iterate_files_worker(const std::string& path,
const std::function<void(const std::string& file, bool is_dir)>& func,
@@ -174,6 +222,32 @@ void ov::util::iterate_files(const std::string& path,
std::vector<std::string> files;
std::vector<std::string> dirs;
#ifdef _WIN32
# ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT
std::wstring pathw = string_to_wstring(path);
std::wstring file_match = path_join_w({pathw, L"*"});
WIN32_FIND_DATAW data;
HANDLE hFind = FindFirstFileW(file_match.c_str(), &data);
if (hFind != INVALID_HANDLE_VALUE) {
do {
bool is_dir = data.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY;
if (is_dir) {
if (std::wstring(data.cFileName) != L"." && std::wstring(data.cFileName) != L"..") {
std::wstring dir_pathw = path_join_w({pathw, data.cFileName});
std::string dir_path = wstring_to_string(dir_pathw);
if (recurse) {
iterate_files(dir_path, func, recurse);
}
func(dir_path, true);
}
} else {
std::wstring file_namew = path_join_w({pathw, data.cFileName});
std::string file_name = wstring_to_string(file_namew);
func(file_name, false);
}
} while (FindNextFileW(hFind, &data));
FindClose(hFind);
}
# else
std::string file_match = path_join({path, "*"});
WIN32_FIND_DATAA data;
HANDLE hFind = FindFirstFileA(file_match.c_str(), &data);
@@ -195,6 +269,7 @@ void ov::util::iterate_files(const std::string& path,
} while (FindNextFileA(hFind, &data));
FindClose(hFind);
}
# endif
#else
iterate_files_worker(
path,

View File

@@ -135,7 +135,11 @@ bool PluginInfo::load() {
bool PluginInfo::load_internal() {
std::shared_ptr<void> so;
try {
#ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT
so = ov::util::load_shared_object(ov::util::string_to_wstring(m_file_path).c_str());
#else
so = ov::util::load_shared_object(m_file_path.c_str());
#endif
} catch (const std::exception& ex) {
OPENVINO_DEBUG << "Error loading FrontEnd '" << m_file_path << "': " << ex.what() << std::endl;
return false;

View File

@@ -30,6 +30,17 @@ namespace {
static std::string _get_frontend_library_path() {
#ifdef _WIN32
# ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT
WCHAR ie_library_path[MAX_PATH];
HMODULE hm = NULL;
if (!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
reinterpret_cast<LPCWSTR>(ov::frontend::get_frontend_library_path),
&hm)) {
FRONT_END_INITIALIZATION_CHECK(false, "GetModuleHandle returned ", GetLastError());
}
GetModuleFileNameW(hm, (LPWSTR)ie_library_path, sizeof(ie_library_path) / sizeof(ie_library_path[0]));
return ov::util::wstring_to_string(ov::util::get_directory(std::wstring(ie_library_path)));
# else
CHAR ie_library_path[MAX_PATH];
HMODULE hm = NULL;
if (!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
@@ -39,6 +50,7 @@ static std::string _get_frontend_library_path() {
}
GetModuleFileNameA(hm, (LPSTR)ie_library_path, sizeof(ie_library_path));
return ov::util::get_directory(std::string(ie_library_path));
# endif
#elif defined(__APPLE__) || defined(__linux__)
Dl_info info;
dladdr(reinterpret_cast<void*>(ov::frontend::get_frontend_library_path), &info);