|
11 | 11 | #include <cassert> |
12 | 12 | #include <iostream> |
13 | 13 | #include <limits> |
14 | | -#if __GNUC__ && __GNUC__ < 8 |
15 | | -// Don't include <filesystem> for GCC versions less than 8 |
| 14 | + |
| 15 | +// For GCC versions less than 8, use experimental/filesystem. |
| 16 | +#if defined(__has_include) && __has_include(<filesystem>) |
| 17 | +#include <filesystem> |
| 18 | +namespace fs = std::filesystem; |
| 19 | +#elif defined(__has_include) && __has_include(<experimental/filesystem>) |
| 20 | +#include <experimental/filesystem> |
| 21 | +namespace fs = std::experimental::filesystem; |
16 | 22 | #else |
17 | | -#include <filesystem> // C++ 17 std::create_directories |
| 23 | +#error "OSUtils requires C++ filesystem support" |
18 | 24 | #endif |
19 | 25 |
|
20 | 26 | #if defined(__SYCL_RT_OS_LINUX) |
|
27 | 33 | #include <cstring> |
28 | 34 | #include <dlfcn.h> |
29 | 35 | #include <fstream> |
30 | | -#include <ftw.h> // for ftw - file tree walk |
31 | 36 | #include <libgen.h> // for dirname |
32 | 37 | #include <link.h> |
33 | 38 | #include <linux/limits.h> // for PATH_MAX |
@@ -278,69 +283,58 @@ int OSUtil::makeDir(const char *Dir) { |
278 | 283 | return 0; |
279 | 284 | } |
280 | 285 |
|
281 | | -size_t OSUtil::DirSizeVar = 0; |
282 | 286 | // Get size of a directory in bytes. |
283 | 287 | size_t getDirectorySize(const std::string &Path) { |
| 288 | + size_t DirSizeVar = 0; |
| 289 | + |
| 290 | + // using fs::recursive_directory_iterator. |
| 291 | + for (const auto &Entry : fs::recursive_directory_iterator(Path)) { |
| 292 | + // Don't check file with .lock extension. |
| 293 | + if (fs::is_regular_file(Entry.path()) && |
| 294 | + Entry.path().extension() != ".lock") |
| 295 | + DirSizeVar += getFileSize(Entry.path().string()); |
| 296 | + } |
284 | 297 |
|
285 | | - OSUtil::DirSizeVar = 0; |
286 | | -// Use ftw for Linux and darwin as they support posix. |
287 | | -#if defined(__SYCL_RT_OS_LINUX) || defined(__SYCL_RT_OS_DARWIN) |
288 | | - auto SumSize = []([[maybe_unused]] const char *Fpath, |
289 | | - const struct stat *StatBuf, int TypeFlag) { |
290 | | - if (TypeFlag == FTW_F) |
291 | | - OSUtil::DirSizeVar += StatBuf->st_size; |
292 | | - return 0; |
293 | | - }; |
294 | | - |
295 | | - if (ftw(Path.c_str(), SumSize, 1) == -1) |
296 | | - std::cerr << "Failed to get directory size: " << Path << std::endl; |
297 | | -#endif |
298 | | - |
299 | | - return OSUtil::DirSizeVar; |
| 298 | + return DirSizeVar; |
300 | 299 | } |
301 | 300 |
|
302 | 301 | // Get size of file in bytes. |
303 | 302 | size_t getFileSize(const std::string &Path) { |
304 | | - size_t Size = 0; |
305 | | - |
306 | | - // For POSIX, use stats to get file size. |
307 | | -#if defined(__SYCL_RT_OS_LINUX) || defined(__SYCL_RT_OS_DARWIN) |
308 | | - struct stat StatBuf; |
309 | | - if (stat(Path.c_str(), &StatBuf) == 0) |
310 | | - Size = StatBuf.st_size; |
311 | | - |
312 | | -#elif defined(__SYCL_RT_OS_WINDOWS) |
313 | | - // For Windows, use GetFileAttributesEx to get file size. |
314 | | - WIN32_FILE_ATTRIBUTE_DATA FileData; |
315 | | - if (GetFileAttributesEx(Path.c_str(), GetFileExInfoStandard, &FileData)) |
316 | | - Size = (static_cast<size_t>(FileData.nFileSizeHigh) << 32) | |
317 | | - FileData.nFileSizeLow; |
318 | | -#endif // __SYCL_RT_OS |
319 | | - |
320 | | - return Size; |
| 303 | + return static_cast<size_t>(fs::file_size(Path)); |
321 | 304 | } |
322 | 305 |
|
323 | | -std::vector<std::pair<time_t, std::string>> OSUtil::Files = {}; |
324 | 306 | // Get list of all files in the directory along with its last access time. |
325 | | -std::vector<std::pair<time_t, std::string>> |
| 307 | +std::vector<std::pair<uint64_t, std::string>> |
326 | 308 | getFilesWithAccessTime(const std::string &Path) { |
| 309 | + std::vector<std::pair<uint64_t, std::string>> Files = {}; |
327 | 310 |
|
328 | | - OSUtil::Files.clear(); |
329 | | - |
330 | | -// Use ftw for posix. |
| 311 | + // using fs::recursive_directory_iterator. |
| 312 | + for (const auto &Entry : fs::recursive_directory_iterator(Path)) { |
| 313 | + if (fs::is_regular_file(Entry.path())) { |
| 314 | +// For Linux and Darwin, use stats. |
331 | 315 | #if defined(__SYCL_RT_OS_LINUX) || defined(__SYCL_RT_OS_DARWIN) |
332 | | - auto GetFiles = [](const char *Fpath, const struct stat *StatBuf, |
333 | | - int TypeFlag) { |
334 | | - if (TypeFlag == FTW_F) |
335 | | - OSUtil::Files.push_back({StatBuf->st_atime, std::string(Fpath)}); |
336 | | - return 0; |
337 | | - }; |
338 | | - |
339 | | - if (ftw(Path.c_str(), GetFiles, 1) == -1) |
340 | | - std::cerr << "Failed to get files with access time: " << Path << std::endl; |
341 | | -#endif |
| 316 | + struct stat StatBuf; |
| 317 | + if (stat(Entry.path().c_str(), &StatBuf) == 0) |
| 318 | + Files.push_back({StatBuf.st_atime, Entry.path().string()}); |
| 319 | +#elif defined(__SYCL_RT_OS_WINDOWS) |
| 320 | + // For Windows, use GetFileAttributesEx to get file size. |
| 321 | + WIN32_FILE_ATTRIBUTE_DATA FileData; |
| 322 | + // Convert to wise string. |
| 323 | + char *path = new char[Entry.path().string().length() + 1]; |
| 324 | + strcpy(path, Entry.path().string().c_str()); |
| 325 | + if (GetFileAttributesEx(path, GetFileExInfoStandard, &FileData)) { |
| 326 | + // Convert FILETIME to uint64_t. |
| 327 | + ULARGE_INTEGER Time; |
| 328 | + Time.LowPart = FileData.ftLastAccessTime.dwLowDateTime; |
| 329 | + Time.HighPart = FileData.ftLastAccessTime.dwHighDateTime; |
| 330 | + Files.push_back({Time.QuadPart, Entry.path().string()}); |
| 331 | + } |
| 332 | + free(path); |
| 333 | +#endif // __SYCL_RT_OS |
| 334 | + } |
| 335 | + } |
342 | 336 |
|
343 | | - return OSUtil::Files; |
| 337 | + return Files; |
344 | 338 | } |
345 | 339 |
|
346 | 340 | } // namespace detail |
|
0 commit comments