Skip to content

Commit 3092870

Browse files
committed
Fix compilation issues with MSVC
1 parent 084e2d1 commit 3092870

File tree

2 files changed

+48
-69
lines changed

2 files changed

+48
-69
lines changed

sycl/include/sycl/detail/os_util.hpp

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,6 @@ class __SYCL_EXPORT OSUtil {
8989
return !stat(Path.c_str(), &Stat);
9090
#endif
9191
}
92-
93-
private:
94-
// These static variables will be used by ftw POSIX function to
95-
// calculate directory size and get files with access time. Other
96-
// option is to make these global variables but that will pollute
97-
// the sycl::detail namespace.
98-
// QUESTION: Should we make these global variables? Or implement ftw-like
99-
// function ourself for directory iteration?
100-
static size_t DirSizeVar;
101-
static std::vector<std::pair<time_t, std::string>> Files;
102-
103-
// Friendship is required to access private static variables.
104-
friend size_t getDirectorySize(const std::string &Path);
105-
friend std::vector<std::pair<time_t, std::string>>
106-
getFilesWithAccessTime(const std::string &Path);
10792
};
10893

10994
// These functions are not a part of OSUtils class to prevent
@@ -116,7 +101,7 @@ size_t getDirectorySize(const std::string &Path);
116101
size_t getFileSize(const std::string &Path);
117102

118103
// Get list of all files in the directory along with its last access time.
119-
std::vector<std::pair<time_t, std::string>>
104+
std::vector<std::pair<uint64_t, std::string>>
120105
getFilesWithAccessTime(const std::string &Path);
121106

122107
} // namespace detail

sycl/source/detail/os_util.cpp

Lines changed: 47 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,16 @@
1111
#include <cassert>
1212
#include <iostream>
1313
#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;
1622
#else
17-
#include <filesystem> // C++ 17 std::create_directories
23+
#error "OSUtils requires C++ filesystem support"
1824
#endif
1925

2026
#if defined(__SYCL_RT_OS_LINUX)
@@ -27,7 +33,6 @@
2733
#include <cstring>
2834
#include <dlfcn.h>
2935
#include <fstream>
30-
#include <ftw.h> // for ftw - file tree walk
3136
#include <libgen.h> // for dirname
3237
#include <link.h>
3338
#include <linux/limits.h> // for PATH_MAX
@@ -278,69 +283,58 @@ int OSUtil::makeDir(const char *Dir) {
278283
return 0;
279284
}
280285

281-
size_t OSUtil::DirSizeVar = 0;
282286
// Get size of a directory in bytes.
283287
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+
}
284297

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;
300299
}
301300

302301
// Get size of file in bytes.
303302
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));
321304
}
322305

323-
std::vector<std::pair<time_t, std::string>> OSUtil::Files = {};
324306
// 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>>
326308
getFilesWithAccessTime(const std::string &Path) {
309+
std::vector<std::pair<uint64_t, std::string>> Files = {};
327310

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.
331315
#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+
}
342336

343-
return OSUtil::Files;
337+
return Files;
344338
}
345339

346340
} // namespace detail

0 commit comments

Comments
 (0)