tool: move tool_ftruncate64 to tool_util.c

... and the prototype to tool_setup.h, to make them both available more
widely and accurately.

Follow-up to 00bef95946

Fixes #13458
Closes #13459
This commit is contained in:
Daniel Stenberg 2024-04-24 10:35:45 +02:00
parent 03cf1c7b8c
commit 9fc4b2c78c
No known key found for this signature in database
GPG Key ID: 5CC908FDB71E12C2
4 changed files with 42 additions and 41 deletions

View File

@ -90,32 +90,3 @@ int tool_seek_cb(void *userdata, curl_off_t offset, int whence)
return CURL_SEEKFUNC_OK;
}
#ifdef USE_TOOL_FTRUNCATE
#ifdef _WIN32_WCE
/* 64-bit lseek-like function unavailable */
# undef _lseeki64
# define _lseeki64(hnd,ofs,whence) lseek(hnd,ofs,whence)
# undef _get_osfhandle
# define _get_osfhandle(fd) (fd)
#endif
/*
* Truncate a file handle at a 64-bit position 'where'.
*/
int tool_ftruncate64(int fd, curl_off_t where)
{
intptr_t handle = _get_osfhandle(fd);
if(_lseeki64(fd, where, SEEK_SET) < 0)
return -1;
if(!SetEndOfFile((HANDLE)handle))
return -1;
return 0;
}
#endif /* USE_TOOL_FTRUNCATE */

View File

@ -25,18 +25,6 @@
***************************************************************************/
#include "tool_setup.h"
#if defined(_WIN32) && !defined(HAVE_FTRUNCATE)
int tool_ftruncate64(int fd, curl_off_t where);
#undef ftruncate
#define ftruncate(fd,where) tool_ftruncate64(fd,where)
#define HAVE_FTRUNCATE 1
#define USE_TOOL_FTRUNCATE 1
#endif /* _WIN32 && ! HAVE_FTRUNCATE */
/*
** callback for CURLOPT_SEEKFUNCTION
*/

View File

@ -74,4 +74,17 @@ extern bool tool_isVistaOrGreater;
extern bool tool_term_has_bold;
#endif
#if defined(_WIN32) && !defined(HAVE_FTRUNCATE)
int tool_ftruncate64(int fd, curl_off_t where);
#undef ftruncate
#define ftruncate(fd,where) tool_ftruncate64(fd,where)
#define HAVE_FTRUNCATE 1
#define USE_TOOL_FTRUNCATE 1
#endif /* _WIN32 && ! HAVE_FTRUNCATE */
#endif /* HEADER_CURL_TOOL_SETUP_H */

View File

@ -159,3 +159,32 @@ int struplocompare4sort(const void *p1, const void *p2)
{
return struplocompare(* (char * const *) p1, * (char * const *) p2);
}
#ifdef USE_TOOL_FTRUNCATE
#ifdef _WIN32_WCE
/* 64-bit lseek-like function unavailable */
# undef _lseeki64
# define _lseeki64(hnd,ofs,whence) lseek(hnd,ofs,whence)
# undef _get_osfhandle
# define _get_osfhandle(fd) (fd)
#endif
/*
* Truncate a file handle at a 64-bit position 'where'.
*/
int tool_ftruncate64(int fd, curl_off_t where)
{
intptr_t handle = _get_osfhandle(fd);
if(_lseeki64(fd, where, SEEK_SET) < 0)
return -1;
if(!SetEndOfFile((HANDLE)handle))
return -1;
return 0;
}
#endif /* USE_TOOL_FTRUNCATE */