Skip to content

Commit

Permalink
More fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
uditagarwal97 committed Dec 9, 2024
1 parent 6f151d3 commit 145e81f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 31 deletions.
1 change: 1 addition & 0 deletions sycl/include/sycl/detail/os_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ class __SYCL_EXPORT OSUtil {
getFilesWithAccessTime(const std::string &Path);

static size_t DirSizeVar;
static std::vector<std::pair<time_t, std::string>> Files;
};

} // namespace detail
Expand Down
63 changes: 32 additions & 31 deletions sycl/source/detail/os_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,13 @@ size_t OSUtil::getDirectorySize(const std::string &Path) {
// Use ftw for Linux and darwin as they support posix.
#if defined(__SYCL_RT_OS_LINUX) || defined(__SYCL_RT_OS_DARWIN)
auto SumSize = []([[maybe_unused]] const char *Fpath,
const struct stat *StatBuf, [[maybe_unused]] int TypeFlag) {
const struct stat *StatBuf, int TypeFlag) {
if (TypeFlag == FTW_F)
DirSizeVar += StatBuf->st_size;
return 0;
};

if (ftw(Path.c_str(),SumSize, 1) == -1)
if (ftw(Path.c_str(), SumSize, 1) == -1)
std::cerr << "Failed to get directory size: " << Path << std::endl;
#endif

Expand All @@ -302,43 +302,44 @@ size_t OSUtil::getDirectorySize(const std::string &Path) {
// Get size of file in bytes.
size_t OSUtil::getFileSize(const std::string &Path) {
size_t Size = 0;
#if __GNUC__ && __GNUC__ < 8
// Should we worry about this case?
assert(false && "getFileSize is not implemented for GCC < 8");
#else
std::filesystem::path FilePath(Path);
if (std::filesystem::exists(FilePath) &&
std::filesystem::is_regular_file(FilePath))
Size = std::filesystem::file_size(FilePath);
#endif

// For POSIX, use stats to get file size.
#if defined(__SYCL_RT_OS_LINUX) || defined(__SYCL_RT_OS_DARWIN)
struct stat StatBuf;
if (stat(Path.c_str(), &StatBuf) == 0)
Size = StatBuf.st_size;

// For Windows, use GetFileAttributesEx to get file size.
#elif defined(__SYCL_RT_OS_WINDOWS)
WIN32_FILE_ATTRIBUTE_DATA FileData;
if (GetFileAttributesEx(Path.c_str(), GetFileExInfoStandard, &FileData))
Size = (static_cast<size_t>(FileData.nFileSizeHigh) << 32) |
FileData.nFileSizeLow;
#endif // __SYCL_RT_OS

return Size;
}

std::vector<std::pair<time_t, std::string>> OSUtil::Files = {};
// Get list of all files in the directory along with its last access time.
std::vector<std::pair<time_t, std::string>>
OSUtil::getFilesWithAccessTime(const std::string &Path) {
std::vector<std::pair<time_t, std::string>> Files;
#if __GNUC__ && __GNUC__ < 8
// Should we worry about this case?
assert(false && "getFilesWithAccessTime is not implemented for GCC < 8");
#else
for (const auto &entry :
std::filesystem::recursive_directory_iterator(Path)) {
if (entry.is_regular_file()) {

Files.clear();

// Use ftw for posix.
#if defined(__SYCL_RT_OS_LINUX) || defined(__SYCL_RT_OS_DARWIN)
struct stat StatBuf;
if (stat(entry.path().c_str(), &StatBuf) == 0)
Files.push_back({StatBuf.st_atime, entry.path().string()});
#elif defined(__SYCL_RT_OS_WINDOWS)
WIN32_FILE_ATTRIBUTE_DATA FileData;
if (GetFileAttributesEx(entry.path().c_str(), GetFileExInfoStandard,
&FileData))
Files.push_back(
{FileData.ftLastAccessTime.dwLowDateTime, entry.path().string()});
#endif // __SYCL_RT_OS
}
}
auto GetFiles = [](const char *Fpath, const struct stat *StatBuf,
int TypeFlag) {
if (TypeFlag == FTW_F)
Files.push_back({StatBuf->st_atime, std::string(Fpath)});
return 0;
};

if (ftw(Path.c_str(), GetFiles, 1) == -1)
std::cerr << "Failed to get files with access time: " << Path << std::endl;
#endif

return Files;
}

Expand Down

0 comments on commit 145e81f

Please sign in to comment.