FILETIME FileLib::getFileTime(SYSTEMTIME &systemTime, bool asLocalTimeZone) { FILETIME fileTime; if (asLocalTimeZone) { TIME_ZONE_INFORMATION tzInfo; const DWORD tzType = GetTimeZoneInformation(&tzInfo); if (tzType == TIME_ZONE_ID_INVALID) { UTIL_THROW_PLATFORM_ERROR(NULL); } SYSTEMTIME utcSystemTime; if (!TzSpecificLocalTimeToSystemTime( &tzInfo, &systemTime, &utcSystemTime)) { UTIL_THROW_PLATFORM_ERROR(NULL); } if (!SystemTimeToFileTime(&utcSystemTime, &fileTime)) { UTIL_THROW_PLATFORM_ERROR(NULL); } } else { if (!SystemTimeToFileTime(&systemTime, &fileTime)) { UTIL_THROW_PLATFORM_ERROR(NULL); } } return fileTime; }
void FileLib::getFileSystemStatus( const char8_t *path, FileSystemStatus *status) { #ifdef _WIN32 std::wstring pathStr; CodeConverter(Code::UTF8, Code::WCHAR_T)(path, pathStr); ULARGE_INTEGER availableBytes; ULARGE_INTEGER totalBytes; ULARGE_INTEGER freeBytes; if (!GetDiskFreeSpaceExW(pathStr.c_str(), &availableBytes, &totalBytes, &freeBytes)) { UTIL_THROW_PLATFORM_ERROR(NULL); } SYSTEM_INFO systemInfo; GetSystemInfo(&systemInfo); const DWORD blockSize = systemInfo.dwPageSize; status->blockSize_ = blockSize; status->fragmentSize_ = blockSize; status->blockCount_ = totalBytes.QuadPart / blockSize; status->freeBlockCount_ = freeBytes.QuadPart / blockSize; status->availableBlockCount_ = availableBytes.QuadPart / blockSize; #else std::string pathStr; CodeConverter(Code::UTF8, Code::CHAR)(path, pathStr); struct statvfs stvBuf; if (statvfs(pathStr.c_str(), &stvBuf) != 0) { UTIL_THROW_PLATFORM_ERROR(NULL); } status->blockSize_ = stvBuf.f_bsize; status->fragmentSize_ = stvBuf.f_frsize; status->blockCount_ = stvBuf.f_blocks; status->freeBlockCount_ = stvBuf.f_bfree; status->availableBlockCount_ = stvBuf.f_bavail; status->iNodeCount_ = stvBuf.f_files; status->freeINodeCount_ = stvBuf.f_ffree; status->availableINodeCount_ = stvBuf.f_favail; status->id_ = stvBuf.f_fsid; status->flags_ = stvBuf.f_flag; status->maxFileNameSize_ = stvBuf.f_namemax; #endif }
tm FileLib::getTM(int64_t unixTime, bool asLocalTimeZone) { if (unixTime < 0) { UTIL_THROW_UTIL_ERROR(CODE_INVALID_PARAMETER, "Negative unix time"); } time_t unixTimeSec = static_cast<time_t>(unixTime / 1000); tm time; if (asLocalTimeZone) { if (localtime_r(&unixTimeSec, &time) == NULL) { UTIL_THROW_PLATFORM_ERROR(NULL); } } else { if (gmtime_r(&unixTimeSec, &time) == NULL) { UTIL_THROW_PLATFORM_ERROR(NULL); } } return time; }
timespec FileLib::calculateTimeoutSpec(clockid_t clockId, uint32_t timeoutMillis) { timespec ts; if (0 != clock_gettime(clockId, &ts)) { UTIL_THROW_PLATFORM_ERROR(NULL); } const long nanosPerSec = 1000 * 1000 * 1000; const long nanosPerMilli = 1000 * 1000; ts.tv_sec += static_cast<time_t>(timeoutMillis / 1000); ts.tv_nsec += static_cast<long>(timeoutMillis % 1000) * nanosPerMilli; const long sec = ts.tv_nsec / nanosPerSec; assert(0 <= sec && sec <= 1); ts.tv_sec += static_cast<time_t>(sec); ts.tv_nsec -= sec * nanosPerSec; return ts; }