diff options
-rw-r--r-- | Include/fileutils.h | 8 | ||||
-rw-r--r-- | Modules/_io/fileio.c | 10 | ||||
-rw-r--r-- | Modules/main.c | 6 | ||||
-rw-r--r-- | Modules/mmapmodule.c | 25 | ||||
-rw-r--r-- | Modules/posixmodule.c | 2 | ||||
-rw-r--r-- | Modules/signalmodule.c | 10 | ||||
-rw-r--r-- | Programs/_freeze_importlib.c | 6 | ||||
-rw-r--r-- | Python/dynload_shlib.c | 14 | ||||
-rw-r--r-- | Python/fileutils.c | 58 | ||||
-rw-r--r-- | Python/marshal.c | 2 | ||||
-rw-r--r-- | Python/random.c | 3 | ||||
-rw-r--r-- | Python/sysmodule.c | 2 |
12 files changed, 89 insertions, 57 deletions
diff --git a/Include/fileutils.h b/Include/fileutils.h index 93a9297..4fd3172 100644 --- a/Include/fileutils.h +++ b/Include/fileutils.h @@ -41,12 +41,16 @@ struct _Py_stat_struct { PyAPI_FUNC(int) _Py_fstat( int fd, - struct _Py_stat_struct *stat); + struct _Py_stat_struct *status); + +PyAPI_FUNC(int) _Py_fstat_noraise( + int fd, + struct _Py_stat_struct *status); #endif /* Py_LIMITED_API */ PyAPI_FUNC(int) _Py_stat( PyObject *path, - struct stat *statbuf); + struct stat *status); #ifndef Py_LIMITED_API PyAPI_FUNC(int) _Py_open( diff --git a/Modules/_io/fileio.c b/Modules/_io/fileio.c index 595f99e..b56a9c3 100644 --- a/Modules/_io/fileio.c +++ b/Modules/_io/fileio.c @@ -399,10 +399,8 @@ fileio_init(PyObject *oself, PyObject *args, PyObject *kwds) } self->blksize = DEFAULT_BUFFER_SIZE; - if (_Py_fstat(self->fd, &fdfstat) < 0) { - PyErr_SetFromErrno(PyExc_OSError); + if (_Py_fstat(self->fd, &fdfstat) < 0) goto error; - } #if defined(S_ISDIR) && defined(EISDIR) /* On Unix, open will succeed for directories. In Python, there should be no file objects referring to @@ -589,7 +587,7 @@ new_buffersize(fileio *self, size_t currentsize) static PyObject * fileio_readall(fileio *self) { - struct _Py_stat_struct st; + struct _Py_stat_struct status; Py_off_t pos, end; PyObject *result; Py_ssize_t bytes_read = 0; @@ -606,8 +604,8 @@ fileio_readall(fileio *self) #else pos = lseek(self->fd, 0L, SEEK_CUR); #endif - if (_Py_fstat(self->fd, &st) == 0) - end = st.st_size; + if (_Py_fstat_noraise(self->fd, &status) == 0) + end = status.st_size; else end = (Py_off_t)-1; diff --git a/Modules/main.c b/Modules/main.c index 74e512b..2a9ea28 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -753,9 +753,11 @@ Py_Main(int argc, wchar_t **argv) } { struct _Py_stat_struct sb; - if (_Py_fstat(fileno(fp), &sb) == 0 && + if (_Py_fstat_noraise(fileno(fp), &sb) == 0 && S_ISDIR(sb.st_mode)) { - fprintf(stderr, "%ls: '%ls' is a directory, cannot continue\n", argv[0], filename); + fprintf(stderr, + "%ls: '%ls' is a directory, cannot continue\n", + argv[0], filename); fclose(fp); return 1; } diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c index 25056a4..e2ed5f9 100644 --- a/Modules/mmapmodule.c +++ b/Modules/mmapmodule.c @@ -465,15 +465,13 @@ mmap_size_method(mmap_object *self, #ifdef UNIX { - struct _Py_stat_struct buf; - if (-1 == _Py_fstat(self->fd, &buf)) { - PyErr_SetFromErrno(PyExc_OSError); + struct _Py_stat_struct status; + if (_Py_fstat(self->fd, &status) == -1) return NULL; - } #ifdef HAVE_LARGEFILE_SUPPORT - return PyLong_FromLongLong(buf.st_size); + return PyLong_FromLongLong(status.st_size); #else - return PyLong_FromLong(buf.st_size); + return PyLong_FromLong(status.st_size); #endif } #endif /* UNIX */ @@ -1112,7 +1110,7 @@ _GetMapSize(PyObject *o, const char* param) static PyObject * new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict) { - struct _Py_stat_struct st; + struct _Py_stat_struct status; mmap_object *m_obj; PyObject *map_size_obj = NULL; Py_ssize_t map_size; @@ -1177,25 +1175,26 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict) if (fd != -1) (void)fcntl(fd, F_FULLFSYNC); #endif - if (fd != -1 && _Py_fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) { + if (fd != -1 && _Py_fstat_noraise(fd, &status) == 0 + && S_ISREG(status.st_mode)) { if (map_size == 0) { - if (st.st_size == 0) { + if (status.st_size == 0) { PyErr_SetString(PyExc_ValueError, "cannot mmap an empty file"); return NULL; } - if (offset >= st.st_size) { + if (offset >= status.st_size) { PyErr_SetString(PyExc_ValueError, "mmap offset is greater than file size"); return NULL; } - if (st.st_size - offset > PY_SSIZE_T_MAX) { + if (status.st_size - offset > PY_SSIZE_T_MAX) { PyErr_SetString(PyExc_ValueError, "mmap length is too large"); return NULL; } - map_size = (Py_ssize_t) (st.st_size - offset); - } else if (offset + map_size > st.st_size) { + map_size = (Py_ssize_t) (status.st_size - offset); + } else if (offset + map_size > status.st_size) { PyErr_SetString(PyExc_ValueError, "mmap length is greater than file size"); return NULL; diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 801305f..ef69a45 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -351,7 +351,7 @@ static int win32_can_symlink = 0; #ifdef MS_WINDOWS # define STAT win32_stat # define LSTAT win32_lstat -# define FSTAT _Py_fstat +# define FSTAT _Py_fstat_noraise # define STRUCT_STAT struct _Py_stat_struct #else # define STAT stat diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c index 1b3589d..3081562 100644 --- a/Modules/signalmodule.c +++ b/Modules/signalmodule.c @@ -503,7 +503,7 @@ signal_siginterrupt(PyObject *self, PyObject *args) static PyObject * signal_set_wakeup_fd(PyObject *self, PyObject *args) { - struct _Py_stat_struct st; + struct _Py_stat_struct status; #ifdef MS_WINDOWS PyObject *fdobj; SOCKET_T sockfd, old_sockfd; @@ -559,10 +559,8 @@ signal_set_wakeup_fd(PyObject *self, PyObject *args) return NULL; } - if (_Py_fstat(fd, &st) != 0) { - PyErr_SetExcFromWindowsErr(PyExc_OSError, GetLastError()); + if (_Py_fstat(fd, &status) != 0) return NULL; - } /* on Windows, a file cannot be set to non-blocking mode */ } @@ -591,10 +589,8 @@ signal_set_wakeup_fd(PyObject *self, PyObject *args) return NULL; } - if (_Py_fstat(fd, &st) != 0) { - PyErr_SetFromErrno(PyExc_OSError); + if (_Py_fstat(fd, &status) != 0) return NULL; - } blocking = _Py_get_blocking(fd); if (blocking < 0) diff --git a/Programs/_freeze_importlib.c b/Programs/_freeze_importlib.c index 31b3d31..b72fcf4 100644 --- a/Programs/_freeze_importlib.c +++ b/Programs/_freeze_importlib.c @@ -35,7 +35,7 @@ main(int argc, char *argv[]) { char *inpath, *outpath; FILE *infile = NULL, *outfile = NULL; - struct _Py_stat_struct st; + struct _Py_stat_struct status; size_t text_size, data_size, n; char *text = NULL; unsigned char *data; @@ -54,11 +54,11 @@ main(int argc, char *argv[]) fprintf(stderr, "cannot open '%s' for reading\n", inpath); goto error; } - if (_Py_fstat(fileno(infile), &st)) { + if (_Py_fstat_noraise(fileno(infile), &status)) { fprintf(stderr, "cannot fstat '%s'\n", inpath); goto error; } - text_size = st.st_size; + text_size = status.st_size; text = (char *) malloc(text_size + 1); if (text == NULL) { fprintf(stderr, "could not allocate %ld bytes\n", (long) text_size); diff --git a/Python/dynload_shlib.c b/Python/dynload_shlib.c index 659adac..1a467fd 100644 --- a/Python/dynload_shlib.c +++ b/Python/dynload_shlib.c @@ -71,22 +71,20 @@ dl_funcptr _PyImport_GetDynLoadFunc(const char *shortname, if (fp != NULL) { int i; - struct _Py_stat_struct statb; - if (_Py_fstat(fileno(fp), &statb) == -1) { - PyErr_SetFromErrno(PyExc_IOError); + struct _Py_stat_struct status; + if (_Py_fstat(fileno(fp), &status) == -1) return NULL; - } for (i = 0; i < nhandles; i++) { - if (statb.st_dev == handles[i].dev && - statb.st_ino == handles[i].ino) { + if (status.st_dev == handles[i].dev && + status.st_ino == handles[i].ino) { p = (dl_funcptr) dlsym(handles[i].handle, funcname); return p; } } if (nhandles < 128) { - handles[nhandles].dev = statb.st_dev; - handles[nhandles].ino = statb.st_ino; + handles[nhandles].dev = status.st_dev; + handles[nhandles].ino = status.st_ino; } } diff --git a/Python/fileutils.c b/Python/fileutils.c index e6d3154..daaad2a 100644 --- a/Python/fileutils.c +++ b/Python/fileutils.c @@ -565,7 +565,8 @@ attributes_to_mode(DWORD attr) } void -_Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct _Py_stat_struct *result) +_Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, + struct _Py_stat_struct *result) { memset(result, 0, sizeof(*result)); result->st_mode = attributes_to_mode(info->dwFileAttributes); @@ -595,9 +596,12 @@ _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, files larger than 2 GB. fstat() may fail with EOVERFLOW on files larger than 2 GB because the file size type is an signed 32-bit integer: see issue #23152. - */ + + On Windows, set the last Windows error and return nonzero on error. On + POSIX, set errno and return nonzero on error. Fill status and return 0 on + success. */ int -_Py_fstat(int fd, struct _Py_stat_struct *result) +_Py_fstat_noraise(int fd, struct _Py_stat_struct *status) { #ifdef MS_WINDOWS BY_HANDLE_FILE_INFORMATION info; @@ -619,22 +623,21 @@ _Py_fstat(int fd, struct _Py_stat_struct *result) SetLastError(ERROR_INVALID_HANDLE); return -1; } - memset(result, 0, sizeof(*result)); + memset(status, 0, sizeof(*status)); type = GetFileType(h); if (type == FILE_TYPE_UNKNOWN) { DWORD error = GetLastError(); - if (error != 0) { + 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; + status->st_mode = _S_IFCHR; else if (type == FILE_TYPE_PIPE) - result->st_mode = _S_IFIFO; + status->st_mode = _S_IFIFO; return 0; } @@ -642,15 +645,48 @@ _Py_fstat(int fd, struct _Py_stat_struct *result) return -1; } - _Py_attribute_data_to_stat(&info, 0, result); + _Py_attribute_data_to_stat(&info, 0, status); /* specific to fstat() */ - result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; + status->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow; return 0; #else - return fstat(fd, result); + return fstat(fd, status); #endif } +/* Return information about a file. + + On POSIX, use fstat(). + + On Windows, use GetFileType() and GetFileInformationByHandle() which support + files larger than 2 GB. fstat() may fail with EOVERFLOW on files larger + than 2 GB because the file size type is an signed 32-bit integer: see issue + #23152. + + Raise an exception and return -1 on error. On Windows, set the last Windows + error on error. On POSIX, set errno on error. Fill status and return 0 on + success. + + The GIL must be held. */ +int +_Py_fstat(int fd, struct _Py_stat_struct *status) +{ + int res; + + Py_BEGIN_ALLOW_THREADS + res = _Py_fstat_noraise(fd, status); + Py_END_ALLOW_THREADS + + if (res != 0) { +#ifdef MS_WINDOWS + PyErr_SetFromWindowsErr(0); +#else + PyErr_SetFromErrno(PyExc_OSError); +#endif + return -1; + } + return 0; +} /* Call _wstat() on Windows, or encode the path to the filesystem encoding and call stat() otherwise. Only fill st_mode attribute on Windows. diff --git a/Python/marshal.c b/Python/marshal.c index 3472882..f89cd04 100644 --- a/Python/marshal.c +++ b/Python/marshal.c @@ -1486,7 +1486,7 @@ static off_t getfilesize(FILE *fp) { struct _Py_stat_struct st; - if (_Py_fstat(fileno(fp), &st) != 0) + if (_Py_fstat_noraise(fileno(fp), &st) != 0) return -1; #if SIZEOF_OFF_T == 4 else if (st.st_size >= INT_MAX) diff --git a/Python/random.c b/Python/random.c index a281829..a4eba3c 100644 --- a/Python/random.c +++ b/Python/random.c @@ -221,7 +221,7 @@ dev_urandom_python(char *buffer, Py_ssize_t size) if (urandom_cache.fd >= 0) { /* Does the fd point to the same thing as before? (issue #21207) */ - if (_Py_fstat(urandom_cache.fd, &st) + if (_Py_fstat_noraise(urandom_cache.fd, &st) || st.st_dev != urandom_cache.st_dev || st.st_ino != urandom_cache.st_ino) { /* Something changed: forget the cached fd (but don't close it, @@ -250,7 +250,6 @@ dev_urandom_python(char *buffer, Py_ssize_t size) } else { if (_Py_fstat(fd, &st)) { - PyErr_SetFromErrno(PyExc_OSError); close(fd); return -1; } diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 471389c..9ec2521 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -1690,7 +1690,7 @@ _PySys_Init(void) #if !defined(MS_WINDOWS) { struct _Py_stat_struct sb; - if (_Py_fstat(fileno(stdin), &sb) == 0 && + if (_Py_fstat_noraise(fileno(stdin), &sb) == 0 && S_ISDIR(sb.st_mode)) { /* There's nothing more we can do. */ /* Py_FatalError() will core dump, so just exit. */ |