Commit e5eafa29 authored by Guido van Rossum's avatar Guido van Rossum

Two improvements to large file support:

- In _portable_ftell(), try fgetpos() before ftello() and ftell64().
  I ran into a situation on a 64-bit capable Linux where the C
  library's ftello() and ftell64() returned negative numbers despite
  fpos_t and off_t both being 64-bit types; fgetpos() did the right
  thing.

- Define a new typedef, Py_off_t, which is either fpos_t or off_t,
  depending on which one is 64 bits.  This removes the need for a lot
  of #ifdefs later on.  (XXX Should this be moved to pyport.h?  That
  file currently seems oblivious to large fille support, so for now
  I'll leave it here where it's needed.)
parent 818cfe69
...@@ -212,14 +212,18 @@ file_close(PyFileObject *f, PyObject *args) ...@@ -212,14 +212,18 @@ file_close(PyFileObject *f, PyObject *args)
} }
/* a portable fseek() function /* An 8-byte off_t-like type */
return 0 on success, non-zero on failure (with errno set) */
int
#if defined(HAVE_LARGEFILE_SUPPORT) && SIZEOF_OFF_T < 8 && SIZEOF_FPOS_T >= 8 #if defined(HAVE_LARGEFILE_SUPPORT) && SIZEOF_OFF_T < 8 && SIZEOF_FPOS_T >= 8
_portable_fseek(FILE *fp, fpos_t offset, int whence) typedef fpos_t Py_off_t;
#else #else
_portable_fseek(FILE *fp, off_t offset, int whence) typedef off_t Py_off_t;
#endif #endif
/* a portable fseek() function
return 0 on success, non-zero on failure (with errno set) */
int
_portable_fseek(FILE *fp, Py_off_t offset, int whence)
{ {
#if defined(HAVE_FSEEKO) #if defined(HAVE_FSEEKO)
return fseeko(fp, offset, whence); return fseeko(fp, offset, whence);
...@@ -253,22 +257,18 @@ _portable_fseek(FILE *fp, off_t offset, int whence) ...@@ -253,22 +257,18 @@ _portable_fseek(FILE *fp, off_t offset, int whence)
/* a portable ftell() function /* a portable ftell() function
Return -1 on failure with errno set appropriately, current file Return -1 on failure with errno set appropriately, current file
position on success */ position on success */
#if defined(HAVE_LARGEFILE_SUPPORT) && SIZEOF_OFF_T < 8 && SIZEOF_FPOS_T >= 8 Py_off_t
fpos_t
#else
off_t
#endif
_portable_ftell(FILE* fp) _portable_ftell(FILE* fp)
{ {
#if defined(HAVE_FTELLO) && defined(HAVE_LARGEFILE_SUPPORT) #if SIZEOF_FPOS_T >= 8 && defined(HAVE_LARGEFILE_SUPPORT)
return ftello(fp);
#elif defined(HAVE_FTELL64) && defined(HAVE_LARGEFILE_SUPPORT)
return ftell64(fp);
#elif SIZEOF_FPOS_T >= 8 && defined(HAVE_LARGEFILE_SUPPORT)
fpos_t pos; fpos_t pos;
if (fgetpos(fp, &pos) != 0) if (fgetpos(fp, &pos) != 0)
return -1; return -1;
return pos; return pos;
#elif defined(HAVE_FTELLO) && defined(HAVE_LARGEFILE_SUPPORT)
return ftello(fp);
#elif defined(HAVE_FTELL64) && defined(HAVE_LARGEFILE_SUPPORT)
return ftell64(fp);
#else #else
return ftell(fp); return ftell(fp);
#endif #endif
...@@ -280,11 +280,7 @@ file_seek(PyFileObject *f, PyObject *args) ...@@ -280,11 +280,7 @@ file_seek(PyFileObject *f, PyObject *args)
{ {
int whence; int whence;
int ret; int ret;
#if defined(HAVE_LARGEFILE_SUPPORT) && SIZEOF_OFF_T < 8 && SIZEOF_FPOS_T >= 8 Py_off_t offset;
fpos_t offset, pos;
#else
off_t offset;
#endif /* !MS_WIN64 */
PyObject *offobj; PyObject *offobj;
if (f->f_fp == NULL) if (f->f_fp == NULL)
...@@ -321,11 +317,7 @@ static PyObject * ...@@ -321,11 +317,7 @@ static PyObject *
file_truncate(PyFileObject *f, PyObject *args) file_truncate(PyFileObject *f, PyObject *args)
{ {
int ret; int ret;
#if defined(HAVE_LARGEFILE_SUPPORT) && SIZEOF_OFF_T < 8 && SIZEOF_FPOS_T >= 8 Py_off_t newsize;
fpos_t newsize;
#else
off_t newsize;
#endif
PyObject *newsizeobj; PyObject *newsizeobj;
if (f->f_fp == NULL) if (f->f_fp == NULL)
...@@ -396,11 +388,7 @@ onioerror: ...@@ -396,11 +388,7 @@ onioerror:
static PyObject * static PyObject *
file_tell(PyFileObject *f, PyObject *args) file_tell(PyFileObject *f, PyObject *args)
{ {
#if defined(HAVE_LARGEFILE_SUPPORT) && SIZEOF_OFF_T < 8 && SIZEOF_FPOS_T >= 8 Py_off_t pos;
fpos_t pos;
#else
off_t pos;
#endif
if (f->f_fp == NULL) if (f->f_fp == NULL)
return err_closed(); return err_closed();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment