| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| #include <ctype.h>
|
| #include <direct.h>
|
| #include <errno.h>
|
| #include <fcntl.h>
|
| #include <io.h>
|
| #include <share.h>
|
| #include <stdlib.h>
|
| #include <string.h>
|
| #include <sys/stat.h>
|
| #include <sys/types.h>
|
| #include <corecrt_internal_time.h>
|
| #include <corecrt_internal_lowio.h>
|
| #include <corecrt_internal_win32_buffer.h>
|
|
|
| #include "faststat.h"
|
|
|
| #ifdef ENABLE_FAST_STAT
|
|
|
| #include <winnt.h>
|
| #include <winioctl.h>
|
|
|
| #ifndef MAXUSHORT
|
| #define MAXUSHORT 0xffff
|
| #endif
|
|
|
| #endif
|
|
|
| namespace
|
| {
|
| struct file_handle_traits
|
| {
|
| typedef int type;
|
|
|
| inline static bool close(_In_ type const fh) throw()
|
| {
|
| _close(fh);
|
| return true;
|
| }
|
|
|
| inline static type get_invalid_value() throw()
|
| {
|
| return -1;
|
| }
|
| };
|
|
|
| struct find_handle_traits
|
| {
|
| typedef HANDLE type;
|
|
|
| static bool close(_In_ type const handle) throw()
|
| {
|
| return ::FindClose(handle) != FALSE;
|
| }
|
|
|
| static type get_invalid_value() throw()
|
| {
|
| return INVALID_HANDLE_VALUE;
|
| }
|
| };
|
|
|
|
|
|
|
| typedef __crt_unique_handle_t<file_handle_traits> scoped_file_handle;
|
| typedef __crt_unique_handle_t<find_handle_traits> unique_find_handle;
|
| }
|
|
|
| static bool __cdecl is_slash(wchar_t const c) throw()
|
| {
|
| return c == L'\\' || c == L'/';
|
| }
|
|
|
| static bool __cdecl compute_size(BY_HANDLE_FILE_INFORMATION const& file_info, long& size) throw()
|
| {
|
| size = 0;
|
| _VALIDATE_RETURN_NOEXC(file_info.nFileSizeHigh == 0 && file_info.nFileSizeLow <= LONG_MAX, EOVERFLOW, false);
|
|
|
| size = static_cast<long>(file_info.nFileSizeLow);
|
| return true;
|
| }
|
|
|
| static bool __cdecl compute_size(BY_HANDLE_FILE_INFORMATION const& file_info, __int64& size) throw()
|
| {
|
| size = 0;
|
| _VALIDATE_RETURN_NOEXC(file_info.nFileSizeHigh <= LONG_MAX, EOVERFLOW, false);
|
|
|
| size = static_cast<__int64>(
|
| static_cast<unsigned __int64>(file_info.nFileSizeHigh) * 0x100000000i64 +
|
| static_cast<unsigned __int64>(file_info.nFileSizeLow));
|
| return true;
|
| }
|
|
|
| #ifdef ENABLE_FAST_STAT
|
|
|
| static bool __cdecl compute_size(LARGE_INTEGER const& file_size, long& size) throw()
|
| {
|
| size = 0;
|
| _VALIDATE_RETURN_NOEXC(file_size.HighPart == 0 && file_size.LowPart <= LONG_MAX, EOVERFLOW, false);
|
|
|
| size = static_cast<long>(file_size.LowPart);
|
| return true;
|
| }
|
|
|
| static bool __cdecl compute_size(LARGE_INTEGER const& file_size, __int64& size) throw()
|
| {
|
| size = 0;
|
| _VALIDATE_RETURN_NOEXC(file_size.HighPart <= LONG_MAX, EOVERFLOW, false);
|
|
|
| size = static_cast<__int64>(
|
| static_cast<unsigned __int64>(file_size.HighPart) * 0x100000000i64 +
|
| static_cast<unsigned __int64>(file_size.LowPart));
|
| return true;
|
| }
|
|
|
| #endif
|
|
|
| _Success_(return != 0)
|
| static wchar_t* __cdecl call_wfullpath(
|
| _Out_writes_z_(buffer_size) wchar_t* const buffer,
|
| wchar_t const* const path,
|
| size_t const buffer_size,
|
| _Inout_ wchar_t** const buffer_result
|
| ) throw()
|
| {
|
| errno_t const saved_errno = errno;
|
| errno = 0;
|
|
|
| wchar_t* const result = _wfullpath(buffer, path, buffer_size);
|
| if (result != nullptr)
|
| {
|
| errno = saved_errno;
|
| return result;
|
| }
|
|
|
| if (errno != ERANGE)
|
| return nullptr;
|
|
|
| errno = saved_errno;
|
|
|
| *buffer_result = _wfullpath(nullptr, path, 0);
|
| return *buffer_result;
|
| }
|
|
|
| static bool __cdecl has_executable_extension(wchar_t const* const path) throw()
|
| {
|
| if (!path)
|
| {
|
| return false;
|
| }
|
|
|
| wchar_t const* const last_dot = wcsrchr(path, L'.');
|
| if (!last_dot)
|
| {
|
| return false;
|
| }
|
|
|
| if (_wcsicmp(last_dot, L".exe") != 0 &&
|
| _wcsicmp(last_dot, L".cmd") != 0 &&
|
| _wcsicmp(last_dot, L".bat") != 0 &&
|
| _wcsicmp(last_dot, L".com") != 0)
|
| {
|
| return false;
|
| }
|
|
|
| return true;
|
| }
|
|
|
| static bool __cdecl is_root_or_empty(wchar_t const* const path) throw()
|
| {
|
| if (!path)
|
| {
|
| return false;
|
| }
|
|
|
| bool const has_drive_letter_and_colon = __ascii_iswalpha(path[0]) && path[1] == L':';
|
| wchar_t const* const path_start = has_drive_letter_and_colon
|
| ? path + 2
|
| : path;
|
|
|
| if (path_start[0] == L'\0')
|
| {
|
| return true;
|
| }
|
|
|
| if (is_slash(path_start[0]) && path_start[1] == L'\0')
|
| {
|
| return true;
|
| }
|
|
|
| return false;
|
| }
|
|
|
| static unsigned short __cdecl convert_to_stat_mode(
|
| int const attributes,
|
| wchar_t const* const path
|
| ) throw()
|
| {
|
| unsigned const os_mode = attributes & 0xff;
|
|
|
|
|
|
|
| bool const is_directory = (os_mode & FILE_ATTRIBUTE_DIRECTORY) != 0;
|
|
|
| unsigned short stat_mode = is_directory || is_root_or_empty(path)
|
| ? _S_IFDIR | _S_IEXEC
|
| : _S_IFREG;
|
|
|
|
|
| stat_mode |= (os_mode & FILE_ATTRIBUTE_READONLY) != 0
|
| ? _S_IREAD
|
| : _S_IREAD | _S_IWRITE;
|
|
|
|
|
| if (has_executable_extension(path))
|
| stat_mode |= _S_IEXEC;
|
|
|
|
|
| stat_mode |= (stat_mode & 0700) >> 3;
|
| stat_mode |= (stat_mode & 0700) >> 6;
|
|
|
| return stat_mode;
|
| }
|
|
|
|
|
| static bool __cdecl get_drive_number_from_path(wchar_t const* const path, int& drive_number) throw()
|
| {
|
| drive_number = 0;
|
|
|
|
|
|
|
|
|
| if (__ascii_iswalpha(path[0]) && path[1] == L':')
|
| {
|
|
|
|
|
| if (path[2] == L'\0')
|
| {
|
| __acrt_errno_map_os_error(ERROR_FILE_NOT_FOUND);
|
| return false;
|
| }
|
|
|
| drive_number = __ascii_towlower(path[0]) - L'a' + 1;
|
| }
|
| else
|
| {
|
| drive_number = _getdrive();
|
| }
|
|
|
| return true;
|
| }
|
|
|
| static bool __cdecl is_root_unc_name(wchar_t const* const path) throw()
|
| {
|
|
|
| if (wcslen(path) < 5)
|
| return 0;
|
|
|
|
|
| if (!is_slash(path[0]) || !is_slash(path[1]) || is_slash(path[2]))
|
| return 0;
|
|
|
|
|
| wchar_t const* p = path + 2;
|
| while (*++p)
|
| {
|
| if (is_slash(*p))
|
| break;
|
| }
|
|
|
|
|
| if (p[0] == L'\0' || p[1] == L'\0')
|
| return 0;
|
|
|
|
|
| while (*++p)
|
| {
|
| if (is_slash(*p))
|
| break;
|
| }
|
|
|
|
|
| if (p[0] == L'\0' || p[1] == L'\0')
|
| return 1;
|
|
|
| return 0;
|
| }
|
|
|
| static bool __cdecl is_usable_drive_or_unc_root(wchar_t const* const path) throw()
|
| {
|
| if (wcspbrk(path, L"./\\") == nullptr)
|
| return false;
|
|
|
| wchar_t full_path_buffer[_MAX_PATH];
|
| __crt_unique_heap_ptr<wchar_t, __crt_public_free_policy> full_path_pointer;
|
| wchar_t* const full_path = call_wfullpath(
|
| full_path_buffer,
|
| path,
|
| _MAX_PATH,
|
| full_path_pointer.get_address_of());
|
|
|
| if (full_path == nullptr)
|
| return false;
|
|
|
|
|
|
|
| if (wcslen(full_path) != 3 && !is_root_unc_name(full_path))
|
| return false;
|
|
|
| if (GetDriveTypeW(path) <= 1)
|
| return false;
|
|
|
| return true;
|
| }
|
|
|
| template <typename TimeType>
|
| static TimeType __cdecl convert_filetime_to_time_t(
|
| FILETIME const file_time,
|
| TimeType const fallback_time
|
| ) throw()
|
| {
|
| using time_traits = __crt_time_time_t_traits<TimeType>;
|
|
|
| if (file_time.dwLowDateTime == 0 && file_time.dwHighDateTime == 0)
|
| {
|
| return fallback_time;
|
| }
|
|
|
| SYSTEMTIME system_time;
|
| SYSTEMTIME local_time;
|
| if (!FileTimeToSystemTime(&file_time, &system_time) ||
|
| !SystemTimeToTzSpecificLocalTime(nullptr, &system_time, &local_time))
|
| {
|
|
|
|
|
| return -1;
|
| }
|
|
|
|
|
|
|
|
|
|
|
| return time_traits::loctotime(
|
| local_time.wYear,
|
| local_time.wMonth,
|
| local_time.wDay,
|
| local_time.wHour,
|
| local_time.wMinute,
|
| local_time.wSecond,
|
| -1);
|
| }
|
|
|
| #ifdef ENABLE_FAST_STAT
|
|
|
| template <typename TimeType>
|
| static TimeType __cdecl convert_filetime_to_time_t(
|
| const LARGE_INTEGER& file_time_int,
|
| const TimeType& fallback_time
|
| ) throw()
|
| {
|
| FILETIME file_time;
|
| file_time.dwHighDateTime = file_time_int.HighPart;
|
| file_time.dwLowDateTime = file_time_int.LowPart;
|
| return convert_filetime_to_time_t(file_time, fallback_time);
|
| }
|
|
|
| #endif
|
|
|
| template <typename StatStruct>
|
| static bool __cdecl common_stat_handle_file_not_opened(
|
| wchar_t const* const path,
|
| StatStruct& result
|
| ) throw()
|
| {
|
| using time_traits = __crt_time_time_t_traits<decltype(result.st_mtime)>;
|
|
|
| if (!is_usable_drive_or_unc_root(path))
|
| {
|
| __acrt_errno_map_os_error(ERROR_FILE_NOT_FOUND);
|
| return false;
|
| }
|
|
|
|
|
| result.st_mode = convert_to_stat_mode(FILE_ATTRIBUTE_DIRECTORY, path);
|
| result.st_nlink = 1;
|
|
|
|
|
| int drive_number{};
|
| if (!get_drive_number_from_path(path, drive_number))
|
| {
|
| return false;
|
| }
|
|
|
| result.st_rdev = static_cast<_dev_t>(drive_number - 1);
|
| result.st_dev = static_cast<_dev_t>(drive_number - 1);
|
|
|
| result.st_mtime = time_traits::loctotime(1980, 1, 1, 0, 0, 0, -1);
|
| result.st_atime = result.st_mtime;
|
| result.st_ctime = result.st_mtime;
|
| return true;
|
| }
|
|
|
| template <typename StatStruct>
|
| static bool __cdecl common_stat_handle_file_opened(
|
| wchar_t const* const path,
|
| int const fh,
|
| HANDLE const handle,
|
| StatStruct& result
|
| ) throw()
|
| {
|
| using time_type = decltype(result.st_mtime);
|
|
|
|
|
| int const file_type = GetFileType(handle) & ~FILE_TYPE_REMOTE;
|
|
|
| if (file_type == FILE_TYPE_DISK)
|
| {
|
|
|
| }
|
| else if (file_type == FILE_TYPE_CHAR || file_type == FILE_TYPE_PIPE)
|
| {
|
|
|
|
|
|
|
| result.st_mode = file_type == FILE_TYPE_CHAR
|
| ? _S_IFCHR
|
| : _S_IFIFO;
|
|
|
| result.st_nlink = 1;
|
|
|
| result.st_rdev = static_cast<unsigned>(fh);
|
| result.st_dev = static_cast<unsigned>(fh);
|
|
|
| if (file_type != FILE_TYPE_CHAR)
|
| {
|
| unsigned long available;
|
| if (PeekNamedPipe(handle, nullptr, 0, nullptr, &available, nullptr))
|
| {
|
| result.st_size = static_cast<_off_t>(available);
|
| }
|
| }
|
|
|
| return true;
|
| }
|
| else if (file_type == FILE_TYPE_UNKNOWN)
|
| {
|
| errno = EBADF;
|
| return false;
|
| }
|
| else
|
| {
|
|
|
|
|
| __acrt_errno_map_os_error(GetLastError());
|
| return false;
|
| }
|
|
|
|
|
| result.st_nlink = 1;
|
|
|
| if (path)
|
| {
|
|
|
| int drive_number{};
|
| if (!get_drive_number_from_path(path, drive_number))
|
| {
|
| return false;
|
| }
|
|
|
| result.st_rdev = static_cast<_dev_t>(drive_number - 1);
|
| result.st_dev = static_cast<_dev_t>(drive_number - 1);
|
| }
|
|
|
| BY_HANDLE_FILE_INFORMATION file_info{};
|
| if (!GetFileInformationByHandle(handle, &file_info))
|
| {
|
| __acrt_errno_map_os_error(GetLastError());
|
| return false;
|
| }
|
|
|
| result.st_mode = convert_to_stat_mode(file_info.dwFileAttributes, path);
|
|
|
| result.st_mtime = convert_filetime_to_time_t(file_info.ftLastWriteTime, static_cast<time_type>(0));
|
| result.st_atime = convert_filetime_to_time_t(file_info.ftLastAccessTime, result.st_mtime);
|
| result.st_ctime = convert_filetime_to_time_t(file_info.ftCreationTime, result.st_mtime);
|
|
|
| if (!compute_size(file_info, result.st_size))
|
| {
|
| return false;
|
| }
|
|
|
| return true;
|
| }
|
|
|
| #ifdef ENABLE_FAST_STAT
|
|
|
| template <typename StatStruct>
|
| static bool __cdecl common_stat_by_path(
|
| wchar_t const* const path,
|
| StatStruct& result
|
| ) throw()
|
| {
|
| using time_type = decltype(result.st_mtime);
|
|
|
| LARGE_INTEGER file_size;
|
| LARGE_INTEGER creation_time;
|
| LARGE_INTEGER last_access_time;
|
| LARGE_INTEGER last_write_time;
|
| unsigned long device_type;
|
| unsigned long number_of_links;
|
| unsigned long file_attributes;
|
| bool status = false;
|
|
|
| if ((path != nullptr) &&
|
| GetFileInfoByName(path,
|
| device_type,
|
| number_of_links,
|
| file_attributes,
|
| creation_time.QuadPart,
|
| last_access_time.QuadPart,
|
| last_write_time.QuadPart,
|
| file_size.QuadPart))
|
| {
|
| do
|
| {
|
|
|
| bool use_file_info;
|
| switch (device_type)
|
| {
|
| case FILE_DEVICE_DISK:
|
| case FILE_DEVICE_DISK_FILE_SYSTEM:
|
| case FILE_DEVICE_VIRTUAL_DISK:
|
| use_file_info = true;
|
| break;
|
|
|
| default:
|
| use_file_info = false;
|
| break;
|
| }
|
| if (!use_file_info)
|
| {
|
| break;
|
| }
|
|
|
| if (!compute_size(file_size, result.st_size))
|
| {
|
| break;
|
| }
|
|
|
|
|
| int drive_number{};
|
| if (!get_drive_number_from_path(path, drive_number))
|
| {
|
| break;
|
| }
|
|
|
| result.st_nlink = number_of_links > MAXUSHORT ? MAXUSHORT : (USHORT) number_of_links;
|
| result.st_rdev = static_cast<_dev_t>(drive_number - 1);
|
| result.st_dev = static_cast<_dev_t>(drive_number - 1);
|
| result.st_mode = convert_to_stat_mode(file_attributes, path);
|
| result.st_mtime = convert_filetime_to_time_t(last_write_time, static_cast<time_type>(0));
|
| result.st_atime = convert_filetime_to_time_t(last_access_time, result.st_mtime);
|
| result.st_ctime = convert_filetime_to_time_t(creation_time, result.st_mtime);
|
|
|
| status = true;
|
|
|
| } while (false);
|
| }
|
|
|
| return status;
|
| }
|
|
|
| #endif
|
|
|
| template <typename StatStruct>
|
| static int __cdecl common_stat(
|
| wchar_t const* const path,
|
| StatStruct* const result
|
| ) throw()
|
| {
|
| _VALIDATE_CLEAR_OSSERR_RETURN(result != nullptr, EINVAL, -1);
|
| *result = StatStruct{};
|
|
|
| _VALIDATE_CLEAR_OSSERR_RETURN(path != nullptr, EINVAL, -1);
|
|
|
| #ifdef ENABLE_FAST_STAT
|
|
|
|
|
|
|
| if (common_stat_by_path(path, *result))
|
| {
|
| return 0;
|
| }
|
|
|
| #endif
|
|
|
| __crt_unique_handle const file_handle(CreateFileW(
|
| path,
|
| FILE_READ_ATTRIBUTES,
|
| FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
|
| nullptr,
|
| OPEN_EXISTING,
|
| FILE_FLAG_BACKUP_SEMANTICS,
|
| nullptr));
|
|
|
| if (file_handle)
|
| {
|
| if (!common_stat_handle_file_opened(path, -1, file_handle.get(), *result))
|
| {
|
| *result = StatStruct{};
|
| return -1;
|
| }
|
| }
|
| else
|
| {
|
| if (!common_stat_handle_file_not_opened(path, *result))
|
| {
|
| *result = StatStruct{};
|
| return -1;
|
| }
|
| }
|
|
|
| return 0;
|
| }
|
|
|
|
|
|
|
| template <typename StatStruct>
|
| static int __cdecl common_stat(
|
| char const* const path,
|
| StatStruct* const result
|
| ) throw()
|
| {
|
| if (path == nullptr) {
|
| return common_stat(static_cast<wchar_t const*>(nullptr), result);
|
| }
|
|
|
| __crt_internal_win32_buffer<wchar_t> wide_path;
|
|
|
| errno_t const cvt = __acrt_mbs_to_wcs_cp(path, wide_path, __acrt_get_utf8_acp_compatibility_codepage());
|
|
|
| if (cvt != 0) {
|
| return -1;
|
| }
|
|
|
| return common_stat(wide_path.data(), result);
|
| }
|
|
|
|
|
|
|
| extern "C" int __cdecl _stat32(char const* const path, struct _stat32* const result)
|
| {
|
| return common_stat(path, result);
|
| }
|
|
|
| extern "C" int __cdecl _stat32i64(char const* const path, struct _stat32i64* const result)
|
| {
|
| return common_stat(path, result);
|
| }
|
|
|
| extern "C" int __cdecl _stat64(char const* const path, struct _stat64* const result)
|
| {
|
| return common_stat(path, result);
|
| }
|
|
|
| extern "C" int __cdecl _stat64i32(char const* const path, struct _stat64i32* const result)
|
| {
|
| return common_stat(path, result);
|
| }
|
|
|
| extern "C" int __cdecl _wstat32(wchar_t const* const path, struct _stat32* const result)
|
| {
|
| return common_stat(path, result);
|
| }
|
|
|
| extern "C" int __cdecl _wstat32i64(wchar_t const* const path, struct _stat32i64* const result)
|
| {
|
| return common_stat(path, result);
|
| }
|
|
|
| extern "C" int __cdecl _wstat64(wchar_t const* const path, struct _stat64* const result)
|
| {
|
| return common_stat(path, result);
|
| }
|
|
|
| extern "C" int __cdecl _wstat64i32(wchar_t const* const path, struct _stat64i32* const result)
|
| {
|
| return common_stat(path, result);
|
| }
|
|
|
|
|
|
|
| template <typename StatStruct>
|
| static int __cdecl common_fstat(int const fh, StatStruct* const result) throw()
|
| {
|
| _VALIDATE_CLEAR_OSSERR_RETURN(result != nullptr, EINVAL, -1);
|
| *result = StatStruct{};
|
|
|
| _CHECK_FH_CLEAR_OSSERR_RETURN(fh, EBADF, -1);
|
| _VALIDATE_CLEAR_OSSERR_RETURN(fh >= 0 && fh < _nhandle, EBADF, -1);
|
| _VALIDATE_CLEAR_OSSERR_RETURN(_osfile(fh) & FOPEN, EBADF, -1);
|
|
|
| return __acrt_lowio_lock_fh_and_call(fh, [&]()
|
| {
|
| if ((_osfile(fh) & FOPEN) == 0)
|
| {
|
| errno = EBADF;
|
| _ASSERTE(("Invalid file descriptor. File possibly closed by a different thread",0));
|
| return -1;
|
| }
|
|
|
| if (!common_stat_handle_file_opened(nullptr, fh, reinterpret_cast<HANDLE>(_osfhnd(fh)), *result))
|
| {
|
| *result = StatStruct{};
|
| return -1;
|
| }
|
|
|
| return 0;
|
| });
|
| }
|
|
|
| extern "C" int __cdecl _fstat32(int const fh, struct _stat32* const result)
|
| {
|
| return common_fstat(fh, result);
|
| }
|
|
|
| extern "C" int __cdecl _fstat32i64(int const fh, struct _stat32i64* const result)
|
| {
|
| return common_fstat(fh, result);
|
| }
|
|
|
| extern "C" int __cdecl _fstat64(int const fh, struct _stat64* const result)
|
| {
|
| return common_fstat(fh, result);
|
| }
|
|
|
| extern "C" int __cdecl _fstat64i32(int const fh, struct _stat64i32* const result)
|
| {
|
| return common_fstat(fh, result);
|
| }
|
|
|