mirror of
https://github.com/python/cpython.git
synced 2025-11-01 02:38:53 +00:00
Issue #23152: Implement _Py_fstat() to support files larger than 2 GB on Windows.
fstat() may fail with EOVERFLOW on files larger than 2 GB because the file size type is an signed 32-bit integer.
This commit is contained in:
parent
18d1924987
commit
f2f373f593
12 changed files with 242 additions and 180 deletions
|
|
@ -350,8 +350,8 @@ static int win32_can_symlink = 0;
|
|||
#ifdef MS_WINDOWS
|
||||
# define STAT win32_stat
|
||||
# define LSTAT win32_lstat
|
||||
# define FSTAT win32_fstat
|
||||
# define STRUCT_STAT struct win32_stat
|
||||
# define FSTAT _Py_fstat
|
||||
# define STRUCT_STAT struct _Py_stat_struct
|
||||
#else
|
||||
# define STAT stat
|
||||
# define LSTAT lstat
|
||||
|
|
@ -1469,73 +1469,6 @@ struct win32_stat{
|
|||
unsigned long st_file_attributes;
|
||||
};
|
||||
|
||||
static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
|
||||
|
||||
static void
|
||||
FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
|
||||
{
|
||||
/* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
|
||||
/* Cannot simply cast and dereference in_ptr,
|
||||
since it might not be aligned properly */
|
||||
__int64 in;
|
||||
memcpy(&in, in_ptr, sizeof(in));
|
||||
*nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
|
||||
*time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
|
||||
}
|
||||
|
||||
static void
|
||||
time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
|
||||
{
|
||||
/* XXX endianness */
|
||||
__int64 out;
|
||||
out = time_in + secs_between_epochs;
|
||||
out = out * 10000000 + nsec_in / 100;
|
||||
memcpy(out_ptr, &out, sizeof(out));
|
||||
}
|
||||
|
||||
/* Below, we *know* that ugo+r is 0444 */
|
||||
#if _S_IREAD != 0400
|
||||
#error Unsupported C library
|
||||
#endif
|
||||
static int
|
||||
attributes_to_mode(DWORD attr)
|
||||
{
|
||||
int m = 0;
|
||||
if (attr & FILE_ATTRIBUTE_DIRECTORY)
|
||||
m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
|
||||
else
|
||||
m |= _S_IFREG;
|
||||
if (attr & FILE_ATTRIBUTE_READONLY)
|
||||
m |= 0444;
|
||||
else
|
||||
m |= 0666;
|
||||
return m;
|
||||
}
|
||||
|
||||
static int
|
||||
attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct win32_stat *result)
|
||||
{
|
||||
memset(result, 0, sizeof(*result));
|
||||
result->st_mode = attributes_to_mode(info->dwFileAttributes);
|
||||
result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
|
||||
result->st_dev = info->dwVolumeSerialNumber;
|
||||
result->st_rdev = result->st_dev;
|
||||
FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
|
||||
FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
|
||||
FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
|
||||
result->st_nlink = info->nNumberOfLinks;
|
||||
result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow;
|
||||
if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
|
||||
/* first clear the S_IFMT bits */
|
||||
result->st_mode ^= (result->st_mode & S_IFMT);
|
||||
/* now set the bits that make this a symlink */
|
||||
result->st_mode |= S_IFLNK;
|
||||
}
|
||||
result->st_file_attributes = info->dwFileAttributes;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static BOOL
|
||||
attributes_from_dir(LPCSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
|
||||
{
|
||||
|
|
@ -1645,11 +1578,15 @@ get_target_path(HANDLE hdl, wchar_t **target_path)
|
|||
return TRUE;
|
||||
}
|
||||
|
||||
/* defined in fileutils.c */
|
||||
int
|
||||
attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct _Py_stat_struct *result);
|
||||
|
||||
static int
|
||||
win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
|
||||
win32_xstat_impl_w(const wchar_t *path, struct _Py_stat_struct *result,
|
||||
BOOL traverse);
|
||||
static int
|
||||
win32_xstat_impl(const char *path, struct win32_stat *result,
|
||||
win32_xstat_impl(const char *path, struct _Py_stat_struct *result,
|
||||
BOOL traverse)
|
||||
{
|
||||
int code;
|
||||
|
|
@ -1745,7 +1682,7 @@ win32_xstat_impl(const char *path, struct win32_stat *result,
|
|||
}
|
||||
|
||||
static int
|
||||
win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
|
||||
win32_xstat_impl_w(const wchar_t *path, struct _Py_stat_struct *result,
|
||||
BOOL traverse)
|
||||
{
|
||||
int code;
|
||||
|
|
@ -1841,7 +1778,7 @@ win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
|
|||
}
|
||||
|
||||
static int
|
||||
win32_xstat(const char *path, struct win32_stat *result, BOOL traverse)
|
||||
win32_xstat(const char *path, struct _Py_stat_struct *result, BOOL traverse)
|
||||
{
|
||||
/* Protocol violation: we explicitly clear errno, instead of
|
||||
setting it to a POSIX error. Callers should use GetLastError. */
|
||||
|
|
@ -1851,7 +1788,7 @@ win32_xstat(const char *path, struct win32_stat *result, BOOL traverse)
|
|||
}
|
||||
|
||||
static int
|
||||
win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse)
|
||||
win32_xstat_w(const wchar_t *path, struct _Py_stat_struct *result, BOOL traverse)
|
||||
{
|
||||
/* Protocol violation: we explicitly clear errno, instead of
|
||||
setting it to a POSIX error. Callers should use GetLastError. */
|
||||
|
|
@ -1873,80 +1810,29 @@ win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse)
|
|||
The _w represent Unicode equivalents of the aforementioned ANSI functions. */
|
||||
|
||||
static int
|
||||
win32_lstat(const char* path, struct win32_stat *result)
|
||||
win32_lstat(const char* path, struct _Py_stat_struct *result)
|
||||
{
|
||||
return win32_xstat(path, result, FALSE);
|
||||
}
|
||||
|
||||
static int
|
||||
win32_lstat_w(const wchar_t* path, struct win32_stat *result)
|
||||
win32_lstat_w(const wchar_t* path, struct _Py_stat_struct *result)
|
||||
{
|
||||
return win32_xstat_w(path, result, FALSE);
|
||||
}
|
||||
|
||||
static int
|
||||
win32_stat(const char* path, struct win32_stat *result)
|
||||
win32_stat(const char* path, struct _Py_stat_struct *result)
|
||||
{
|
||||
return win32_xstat(path, result, TRUE);
|
||||
}
|
||||
|
||||
static int
|
||||
win32_stat_w(const wchar_t* path, struct win32_stat *result)
|
||||
win32_stat_w(const wchar_t* path, struct _Py_stat_struct *result)
|
||||
{
|
||||
return win32_xstat_w(path, result, TRUE);
|
||||
}
|
||||
|
||||
static int
|
||||
win32_fstat(int file_number, struct win32_stat *result)
|
||||
{
|
||||
BY_HANDLE_FILE_INFORMATION info;
|
||||
HANDLE h;
|
||||
int type;
|
||||
|
||||
if (!_PyVerify_fd(file_number))
|
||||
h = INVALID_HANDLE_VALUE;
|
||||
else
|
||||
h = (HANDLE)_get_osfhandle(file_number);
|
||||
|
||||
/* Protocol violation: we explicitly clear errno, instead of
|
||||
setting it to a POSIX error. Callers should use GetLastError. */
|
||||
errno = 0;
|
||||
|
||||
if (h == INVALID_HANDLE_VALUE) {
|
||||
/* This is really a C library error (invalid file handle).
|
||||
We set the Win32 error to the closes one matching. */
|
||||
SetLastError(ERROR_INVALID_HANDLE);
|
||||
return -1;
|
||||
}
|
||||
memset(result, 0, sizeof(*result));
|
||||
|
||||
type = GetFileType(h);
|
||||
if (type == FILE_TYPE_UNKNOWN) {
|
||||
DWORD error = GetLastError();
|
||||
if (error != 0) {
|
||||
return -1;
|
||||
}
|
||||
/* else: valid but unknown file */
|
||||
}
|
||||
|
||||
if (type != FILE_TYPE_DISK) {
|
||||
if (type == FILE_TYPE_CHAR)
|
||||
result->st_mode = _S_IFCHR;
|
||||
else if (type == FILE_TYPE_PIPE)
|
||||
result->st_mode = _S_IFIFO;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!GetFileInformationByHandle(h, &info)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
attribute_data_to_stat(&info, 0, result);
|
||||
/* specific to fstat() */
|
||||
result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif /* MS_WINDOWS */
|
||||
|
||||
PyDoc_STRVAR(stat_result__doc__,
|
||||
|
|
@ -6333,6 +6219,11 @@ exit:
|
|||
return return_value;
|
||||
}
|
||||
|
||||
#ifdef MS_WINDOWS
|
||||
void
|
||||
time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr);
|
||||
#endif
|
||||
|
||||
static PyObject *
|
||||
os_utime_impl(PyModuleDef *module, path_t *path, PyObject *times, PyObject *ns, int dir_fd, int follow_symlinks)
|
||||
/*[clinic end generated code: output=891489c35cc68c5d input=1f18c17d5941aa82]*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue