Skip to content

Commit 4d25dd6

Browse files
authored
[core] Migrate some file utils use fs path (#32882)
### Details: Migrate file utils to use std path class: - file_exists - make_plugin_library_name - get_plugin_path - get_compiled_plugin_path Remove not used/required utils functions - remove is_absolute_file_path - remove to_file_path ### Tickets: - CVS-176714 --------- Signed-off-by: Pawel Raasz <pawel.raasz@intel.com>
1 parent 5d95073 commit 4d25dd6

File tree

10 files changed

+199
-266
lines changed

10 files changed

+199
-266
lines changed

src/bindings/c/tests/test_model_repo.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ std::string generate_test_xml_file() {
4141
plugin_xml_file << ov::util::FileTraits<char>::library_prefix();
4242
plugin_xml_file << "mock_engine";
4343
plugin_xml_file << OV_BUILD_POSTFIX;
44-
plugin_xml_file << ov::util::FileTraits<char>::dot_symbol;
45-
plugin_xml_file << ov::util::FileTraits<char>::library_ext();
44+
plugin_xml_file << ov::util::library_extension().string();
4645
plugin_xml_file << "\" name=\"CUSTOM\">\n";
4746
plugin_xml_file << " </plugin>\n";
4847
plugin_xml_file << " </plugins>\n";

src/common/util/include/openvino/util/file_util.hpp

Lines changed: 79 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,50 @@ struct FileTraits<wchar_t> {
8080
}
8181
};
8282

83+
inline std::filesystem::path library_prefix() {
84+
#if defined(_WIN32) && (defined(__MINGW32__) || defined(__MINGW64__))
85+
return {"lib"};
86+
#elif defined(_WIN32)
87+
return {};
88+
#else
89+
return {"lib"};
90+
#endif
91+
}
92+
inline std::filesystem::path library_extension() {
93+
#ifdef _WIN32
94+
return {".dll"};
95+
#else
96+
return {".so"};
97+
#endif
98+
}
99+
100+
/**
101+
* @brief Creates std::filesystem::path provided by source.
102+
*
103+
* The purpose of this function is to hide platform specific issue with path creation like on Windows create from
104+
* literal std::string with unicode characters can lead to different path name than expected.
105+
*
106+
* @param source Source to create path. Supported types are same as for std::filesystem::path constructor.
107+
* @return std::filesystem::path object.
108+
*/
109+
template <class Source>
110+
std::filesystem::path make_path(Source&& source) {
111+
if constexpr (std::is_same_v<std::add_const_t<std::remove_pointer_t<std::decay_t<Source>>>, const wchar_t>) {
112+
return {std::wstring(std::forward<Source>(source))};
113+
} else if constexpr (std::is_same_v<std::filesystem::path::string_type, std::wstring> &&
114+
std::is_same_v<std::decay_t<Source>, std::string>) {
115+
return {ov::util::string_to_wstring(std::forward<Source>(source))};
116+
} else {
117+
return {std::forward<Source>(source)};
118+
}
119+
}
120+
83121
/**
84122
* @brief Convert path as char string to to a single-byte chain.
85123
* @param path Path as char string.
86124
* @return Reference to input path (no conversion).
87125
*/
88-
template <class Path,
89-
typename std::enable_if<std::is_same<typename std::decay<Path>::type, std::string>::value>::type* = nullptr>
126+
template <class Path, typename std::enable_if_t<std::is_same_v<std::decay_t<Path>, std::string>>* = nullptr>
90127
const std::string& path_to_string(const Path& path) {
91128
return path;
92129
}
@@ -97,14 +134,23 @@ const std::string& path_to_string(const Path& path) {
97134
* @param path Path as wide-char string.
98135
* @return A char string
99136
*/
100-
template <class Path,
101-
typename std::enable_if<std::is_same<typename std::decay<Path>::type, std::wstring>::value>::type* = nullptr>
137+
template <class Path, typename std::enable_if_t<std::is_same_v<std::decay_t<Path>, std::wstring>>* = nullptr>
102138
std::string path_to_string(const Path& path) {
103139
return ov::util::wstring_to_string(path);
104140
}
105141

106142
#endif
107143

144+
/**
145+
* @brief Convert std::filesystem::path single-byte chain.
146+
* Function resolve issue when path create from std::string which contains unicode characters.
147+
* @param path Path.
148+
* @return A char string.
149+
*/
150+
inline auto path_to_string(const std::filesystem::path& path) -> decltype(path_to_string(path.native())) {
151+
return ov::util::path_to_string(path.native());
152+
}
153+
108154
/// \brief Remove path components which would allow traversing up a directory tree.
109155
/// \param path A path to file
110156
/// \return A sanitized path
@@ -122,14 +168,6 @@ std::string get_file_name(const std::string& path);
122168
*/
123169
std::string get_absolute_file_path(const std::string& path);
124170

125-
/**
126-
* @brief Interface function to check path to file is absolute or not
127-
* @param path - path to file, can be relative to current working directory
128-
* @return True if path is absolute and False otherwise
129-
* @throw runtime_error if any error occurred
130-
*/
131-
bool is_absolute_file_path(const std::string& path);
132-
133171
/**
134172
* @brief Interface function to create directories recursively by given path
135173
* @param path - path to file, can be relative to current working directory
@@ -172,47 +210,27 @@ inline int64_t file_size(const char* path) {
172210
#endif
173211

174212
/**
175-
* @brief Returns file size for file
176-
* @param[in] path The file name
177-
* @return file size
213+
* @brief Tests whether file exists at given path.
214+
* @param[in] path The file path.
215+
* @return True if file exists, false otherwise.
216+
* @{
178217
*/
179-
inline bool file_exists(const char* path) {
180-
#if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT) && defined(_WIN32)
181-
std::wstring widefilename = ov::util::string_to_wstring(path);
182-
const wchar_t* file_name = widefilename.c_str();
183-
#elif defined(__ANDROID__) || defined(ANDROID)
184-
std::string file_name = path;
185-
std::string::size_type pos = file_name.find('!');
186-
if (pos != std::string::npos) {
187-
file_name = file_name.substr(0, pos);
188-
}
218+
inline bool file_exists(const std::filesystem::path& path) noexcept {
219+
#if defined(__ANDROID__) || defined(ANDROID)
220+
const auto pos = path.native().find('!');
221+
const auto f_status = (pos == std::string::npos) ? std::filesystem::status(path)
222+
: std::filesystem::status(path.native().substr(0, pos));
189223
#else
190-
const char* file_name = path;
224+
const auto f_status = std::filesystem::status(path);
191225
#endif
192-
std::ifstream in(file_name, std::ios_base::binary | std::ios_base::ate);
193-
return in.good();
226+
return std::filesystem::exists(f_status) && !std::filesystem::is_directory(f_status);
194227
}
195228

196-
#ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT
197-
198-
/**
199-
* @brief Returns true if file exists
200-
* @param[in] path The file name
201-
* @return true if file exists
202-
*/
203-
inline bool file_exists(const std::wstring& path) {
204-
return file_exists(wstring_to_string(path).c_str());
205-
}
206-
#endif // OPENVINO_ENABLE_UNICODE_PATH_SUPPORT
207-
208-
/**
209-
* @brief Returns true if file exists
210-
* @param[in] path The file name
211-
* @return true if file exists
212-
*/
213-
inline bool file_exists(const std::string& path) {
214-
return file_exists(path.c_str());
229+
template <class T>
230+
inline bool file_exists(const std::basic_string<T>& path) noexcept {
231+
return file_exists(make_path(path));
215232
}
233+
/** @} */
216234

217235
std::string get_file_ext(const std::string& path);
218236
std::filesystem::path get_directory(const std::filesystem::path& path);
@@ -236,16 +254,6 @@ using FilePath = std::filesystem::path::string_type;
236254
inline std::string from_file_path(const std::filesystem::path& path) {
237255
return path.string();
238256
}
239-
240-
// TODO: remove this function after all calls use Path
241-
inline FilePath to_file_path(const std::filesystem::path& path) {
242-
#if defined(_WIN32) && defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT)
243-
return ov::util::string_to_wstring(path.string());
244-
#else
245-
return path.native();
246-
#endif
247-
}
248-
249257
#ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT
250258

251259
/**
@@ -277,22 +285,31 @@ inline std::basic_string<C> make_plugin_library_name(const std::basic_string<C>&
277285
FileTraits<C>::library_ext();
278286
}
279287

288+
inline std::filesystem::path make_plugin_library_name(const std::filesystem::path& lib_name) {
289+
return library_prefix().concat(lib_name.filename().native()).concat(library_extension().native());
290+
}
291+
292+
inline std::filesystem::path make_plugin_library_name(const std::filesystem::path& dir_path,
293+
const std::filesystem::path& lib_name) {
294+
return dir_path / make_plugin_library_name(lib_name);
295+
}
296+
280297
/**
281298
* @brief Format plugin path (canonicalize, complete to absolute or complete to file name) for further
282299
* dynamic loading by OS
283300
* @param plugin - Path (absolute or relative) or name of a plugin. Depending on platform, `plugin` is wrapped with
284301
* shared library suffix and prefix to identify library full name
285302
* @return absolute path or file name with extension (to be found in ENV)
286303
*/
287-
FilePath get_plugin_path(const std::string& plugin);
304+
std::filesystem::path get_plugin_path(const std::filesystem::path& plugin);
288305

289306
/**
290307
* @brief Find the plugins which are located together with OV library
291308
* @param plugin - Path (absolute or relative) or name of a plugin. Depending on platform, `plugin` is wrapped with
292309
* shared library suffix and prefix to identify library full name
293310
* @return absolute path or file name with extension (to be found in ENV)
294311
*/
295-
FilePath get_compiled_plugin_path(const std::string& plugin);
312+
std::filesystem::path get_compiled_plugin_path(const std::filesystem::path& plugin);
296313

297314
/**
298315
* @brief Format plugin path (canonicalize, complete to absolute or complete to file name) for further
@@ -303,7 +320,9 @@ FilePath get_compiled_plugin_path(const std::string& plugin);
303320
* @param as_abs_only - Bool value, allows return file names or not
304321
* @return absolute path or file name with extension (to be found in ENV)
305322
*/
306-
FilePath get_plugin_path(const std::string& plugin, const std::string& xml_path, bool as_abs_only = false);
323+
std::filesystem::path get_plugin_path(const std::filesystem::path& plugin,
324+
const std::filesystem::path& xml_path,
325+
bool as_abs_only = false);
307326

308327
/**
309328
* @brief load binary data from file
@@ -345,27 +364,5 @@ inline std::basic_string<C> make_path(const std::basic_string<C>& folder, const
345364
return file;
346365
return folder + ov::util::FileTraits<C>::file_separator + file;
347366
}
348-
349-
/**
350-
* @brief Creates std::filesystem::path provided by source.
351-
*
352-
* The purpose of this function is to hide platform specific issue with path creation like on Windows create from
353-
* literal std::string with unicode characters can lead to different path name than expected.
354-
*
355-
* @param source Source to create path. Supported types are same as for std::filesystem::path constructor.
356-
* @return std::filesystem::path object.
357-
*/
358-
template <class Source>
359-
std::filesystem::path make_path(const Source& source) {
360-
if constexpr (std::is_same_v<std::decay_t<Source>, wchar_t*>) {
361-
return {std::wstring(source)};
362-
} else if constexpr (std::is_same_v<std::filesystem::path::value_type, std::wstring::value_type> &&
363-
std::is_same_v<Source, std::string>) {
364-
return {ov::util::string_to_wstring(source)};
365-
} else {
366-
return {source};
367-
}
368-
}
369-
370367
} // namespace util
371368
} // namespace ov

0 commit comments

Comments
 (0)