src: drop detecting and redefining system symbol ftruncate

Introduce `toolx_ftruncate()` macro and map it to existing replacements
for non-mingw-w64 Windows and DJGPP, or to `ftruncate` otherwise.

Follow-up to 6041b9b11b #21109

Closes #21130
This commit is contained in:
Viktor Szakats 2026-03-27 21:51:43 +01:00
parent 335dc0e3c5
commit 20b18a43f6
No known key found for this signature in database
14 changed files with 17 additions and 135 deletions

View File

@ -88,7 +88,6 @@ elseif(CYGWIN OR
set(HAVE_FSETXATTR_5 1) set(HAVE_FSETXATTR_5 1)
set(HAVE_FSETXATTR_6 0) set(HAVE_FSETXATTR_6 0)
endif() endif()
set(HAVE_FTRUNCATE 1)
set(HAVE_GETADDRINFO 1) set(HAVE_GETADDRINFO 1)
if(CMAKE_SYSTEM_NAME STREQUAL "OpenBSD") if(CMAKE_SYSTEM_NAME STREQUAL "OpenBSD")
set(HAVE_GETADDRINFO_THREADSAFE 0) set(HAVE_GETADDRINFO_THREADSAFE 0)

View File

@ -29,7 +29,6 @@ if(MINGW)
set(HAVE_BASENAME 1) set(HAVE_BASENAME 1)
set(HAVE_BOOL_T 1) # = HAVE_STDBOOL_H set(HAVE_BOOL_T 1) # = HAVE_STDBOOL_H
set(HAVE_DIRENT_H 1) set(HAVE_DIRENT_H 1)
set(HAVE_FTRUNCATE 1)
set(HAVE_GETTIMEOFDAY 1) set(HAVE_GETTIMEOFDAY 1)
set(HAVE_LIBGEN_H 1) set(HAVE_LIBGEN_H 1)
set(HAVE_OPENDIR 1) set(HAVE_OPENDIR 1)
@ -42,7 +41,6 @@ if(MINGW)
set(HAVE_UTIME_H 1) # wrapper to sys/utime.h set(HAVE_UTIME_H 1) # wrapper to sys/utime.h
else() else()
set(HAVE_DIRENT_H 0) set(HAVE_DIRENT_H 0)
set(HAVE_FTRUNCATE 0)
set(HAVE_GETTIMEOFDAY 0) set(HAVE_GETTIMEOFDAY 0)
set(HAVE_LIBGEN_H 0) set(HAVE_LIBGEN_H 0)
set(HAVE_OPENDIR 0) set(HAVE_OPENDIR 0)

View File

@ -1637,7 +1637,6 @@ check_symbol_exists("freeaddrinfo" "${CURL_INCLUDES}" HAVE_FREEADDRINFO) # w
check_function_exists("pipe" HAVE_PIPE) check_function_exists("pipe" HAVE_PIPE)
check_function_exists("pipe2" HAVE_PIPE2) check_function_exists("pipe2" HAVE_PIPE2)
check_function_exists("eventfd" HAVE_EVENTFD) check_function_exists("eventfd" HAVE_EVENTFD)
check_symbol_exists("ftruncate" "unistd.h" HAVE_FTRUNCATE)
check_symbol_exists("getpeername" "${CURL_INCLUDES}" HAVE_GETPEERNAME) # winsock2.h unistd.h proto/bsdsocket.h check_symbol_exists("getpeername" "${CURL_INCLUDES}" HAVE_GETPEERNAME) # winsock2.h unistd.h proto/bsdsocket.h
check_symbol_exists("getsockname" "${CURL_INCLUDES}" HAVE_GETSOCKNAME) # winsock2.h unistd.h proto/bsdsocket.h check_symbol_exists("getsockname" "${CURL_INCLUDES}" HAVE_GETSOCKNAME) # winsock2.h unistd.h proto/bsdsocket.h
check_function_exists("getrlimit" HAVE_GETRLIMIT) check_function_exists("getrlimit" HAVE_GETRLIMIT)

View File

@ -4086,7 +4086,6 @@ CURL_CHECK_FUNC_CLOSESOCKET_CAMEL
CURL_CHECK_FUNC_FCNTL CURL_CHECK_FUNC_FCNTL
CURL_CHECK_FUNC_FREEADDRINFO CURL_CHECK_FUNC_FREEADDRINFO
CURL_CHECK_FUNC_FSETXATTR CURL_CHECK_FUNC_FSETXATTR
CURL_CHECK_FUNC_FTRUNCATE
CURL_CHECK_FUNC_GETADDRINFO CURL_CHECK_FUNC_GETADDRINFO
CURL_CHECK_FUNC_GETHOSTBYNAME_R CURL_CHECK_FUNC_GETHOSTBYNAME_R
CURL_CHECK_FUNC_GETHOSTNAME CURL_CHECK_FUNC_GETHOSTNAME

View File

@ -51,7 +51,6 @@
#define HAVE_SYS_UTIME_H 1 #define HAVE_SYS_UTIME_H 1
#define HAVE_SYS_IOCTL_H 1 #define HAVE_SYS_IOCTL_H 1
#define HAVE_ALARM 1 #define HAVE_ALARM 1
#define HAVE_FTRUNCATE 1
#define HAVE_UTIME 1 #define HAVE_UTIME 1
#define HAVE_SELECT 1 #define HAVE_SELECT 1
#define HAVE_SOCKET 1 #define HAVE_SOCKET 1

View File

@ -69,9 +69,6 @@
/* Define to 1 if you have the freeaddrinfo function. */ /* Define to 1 if you have the freeaddrinfo function. */
#define HAVE_FREEADDRINFO 1 #define HAVE_FREEADDRINFO 1
/* Define to 1 if you have the ftruncate function. */
#define HAVE_FTRUNCATE 1
/* Define to 1 if you have a working getaddrinfo function. */ /* Define to 1 if you have a working getaddrinfo function. */
#define HAVE_GETADDRINFO 1 #define HAVE_GETADDRINFO 1

View File

@ -48,9 +48,6 @@
/* Define if you have the <fcntl.h> header file. */ /* Define if you have the <fcntl.h> header file. */
#define HAVE_FCNTL_H #define HAVE_FCNTL_H
/* Define if you have the `ftruncate' function. */
#define HAVE_FTRUNCATE
/* Define if getaddrinfo exists and works */ /* Define if getaddrinfo exists and works */
#define HAVE_GETADDRINFO #define HAVE_GETADDRINFO

View File

@ -86,11 +86,6 @@
/* Define if you have the closesocket function. */ /* Define if you have the closesocket function. */
#define HAVE_CLOSESOCKET 1 #define HAVE_CLOSESOCKET 1
/* Define if you have the ftruncate function. */
#ifdef __MINGW32__
#define HAVE_FTRUNCATE 1
#endif
/* Define to 1 if you have the `getpeername' function. */ /* Define to 1 if you have the `getpeername' function. */
#define HAVE_GETPEERNAME 1 #define HAVE_GETPEERNAME 1

View File

@ -252,9 +252,6 @@
/* Define to 1 if you have the fseeko declaration. */ /* Define to 1 if you have the fseeko declaration. */
#cmakedefine HAVE_DECL_FSEEKO 1 #cmakedefine HAVE_DECL_FSEEKO 1
/* Define to 1 if you have the ftruncate function. */
#cmakedefine HAVE_FTRUNCATE 1
/* Define to 1 if you have a working getaddrinfo function. */ /* Define to 1 if you have a working getaddrinfo function. */
#cmakedefine HAVE_GETADDRINFO 1 #cmakedefine HAVE_GETADDRINFO 1

View File

@ -1172,91 +1172,6 @@ AC_DEFUN([CURL_CHECK_FUNC_FSETXATTR], [
]) ])
dnl CURL_CHECK_FUNC_FTRUNCATE
dnl -------------------------------------------------
dnl Verify if ftruncate is available, prototyped, and
dnl can be compiled. If all of these are true, and
dnl usage has not been previously disallowed with
dnl shell variable curl_disallow_ftruncate, then
dnl HAVE_FTRUNCATE will be defined.
AC_DEFUN([CURL_CHECK_FUNC_FTRUNCATE], [
AC_REQUIRE([CURL_INCLUDES_UNISTD])dnl
#
tst_links_ftruncate="unknown"
tst_proto_ftruncate="unknown"
tst_compi_ftruncate="unknown"
tst_allow_ftruncate="unknown"
#
AC_MSG_CHECKING([if ftruncate can be linked])
AC_LINK_IFELSE([
AC_LANG_FUNC_LINK_TRY([ftruncate])
],[
AC_MSG_RESULT([yes])
tst_links_ftruncate="yes"
],[
AC_MSG_RESULT([no])
tst_links_ftruncate="no"
])
#
if test "$tst_links_ftruncate" = "yes"; then
AC_MSG_CHECKING([if ftruncate is prototyped])
AC_EGREP_CPP([ftruncate],[
$curl_includes_unistd
],[
AC_MSG_RESULT([yes])
tst_proto_ftruncate="yes"
],[
AC_MSG_RESULT([no])
tst_proto_ftruncate="no"
])
fi
#
if test "$tst_proto_ftruncate" = "yes"; then
AC_MSG_CHECKING([if ftruncate is compilable])
AC_COMPILE_IFELSE([
AC_LANG_PROGRAM([[
$curl_includes_unistd
]],[[
if(ftruncate(0, 0) != 0)
return 1;
]])
],[
AC_MSG_RESULT([yes])
tst_compi_ftruncate="yes"
],[
AC_MSG_RESULT([no])
tst_compi_ftruncate="no"
])
fi
#
if test "$tst_compi_ftruncate" = "yes"; then
AC_MSG_CHECKING([if ftruncate usage allowed])
if test "x$curl_disallow_ftruncate" != "xyes"; then
AC_MSG_RESULT([yes])
tst_allow_ftruncate="yes"
else
AC_MSG_RESULT([no])
tst_allow_ftruncate="no"
fi
fi
#
AC_MSG_CHECKING([if ftruncate might be used])
if test "$tst_links_ftruncate" = "yes" &&
test "$tst_proto_ftruncate" = "yes" &&
test "$tst_compi_ftruncate" = "yes" &&
test "$tst_allow_ftruncate" = "yes"; then
AC_MSG_RESULT([yes])
AC_DEFINE_UNQUOTED(HAVE_FTRUNCATE, 1,
[Define to 1 if you have the ftruncate function.])
curl_cv_func_ftruncate="yes"
else
AC_MSG_RESULT([no])
curl_cv_func_ftruncate="no"
fi
])
dnl CURL_CHECK_FUNC_GETADDRINFO dnl CURL_CHECK_FUNC_GETADDRINFO
dnl ------------------------------------------------- dnl -------------------------------------------------
dnl Verify if getaddrinfo is available, prototyped, can dnl Verify if getaddrinfo is available, prototyped, can

View File

@ -270,7 +270,7 @@ static size_t save_etag(const char *etag_h, const char *endp,
if((fd != -1) && if((fd != -1) &&
!curlx_fstat(fd, &file) && !curlx_fstat(fd, &file) &&
(S_ISREG(file.st_mode) && (S_ISREG(file.st_mode) &&
ftruncate(fd, 0))) toolx_ftruncate(fd, 0)))
return CURL_WRITEFUNC_ERROR; return CURL_WRITEFUNC_ERROR;
fwrite(etag_h, 1, etag_length, etag_save->stream); fwrite(etag_h, 1, etag_length, etag_save->stream);

View File

@ -559,7 +559,7 @@ static CURLcode retrycheck(struct OperationConfig *config,
notef("Throwing away %" CURL_FORMAT_CURL_OFF_T " bytes", outs->bytes); notef("Throwing away %" CURL_FORMAT_CURL_OFF_T " bytes", outs->bytes);
/* truncate file at the position where we started appending */ /* truncate file at the position where we started appending */
if(ftruncate(fileno(outs->stream), outs->init)) { if(toolx_ftruncate(fileno(outs->stream), outs->init)) {
/* when truncate fails, we cannot append as then we /* when truncate fails, we cannot append as then we
create something strange, bail out */ create something strange, bail out */
errorf("Failed to truncate file"); errorf("Failed to truncate file");

View File

@ -94,26 +94,16 @@ extern FILE *tool_stderr;
#ifdef _WIN32 #ifdef _WIN32
/* set in init_terminal() */ /* set in init_terminal() */
extern bool tool_term_has_bold; extern bool tool_term_has_bold;
#endif
#ifndef HAVE_FTRUNCATE #if defined(_WIN32) && !defined(__MINGW32__)
int toolx_ftruncate_win32(int fd, curl_off_t where);
int tool_ftruncate64(int fd, curl_off_t where); #define toolx_ftruncate toolx_ftruncate_win32
#elif defined(__DJGPP__)
#undef ftruncate int toolx_ftruncate_djgpp(int fd, curl_off_t where);
#define ftruncate(fd, where) tool_ftruncate64(fd, where) #define toolx_ftruncate toolx_ftruncate_djgpp
#else
#define HAVE_FTRUNCATE 1 #define toolx_ftruncate ftruncate
#define USE_TOOL_FTRUNCATE 1
#endif /* !HAVE_FTRUNCATE */
#endif /* _WIN32 */
#ifdef __DJGPP__
int msdos_ftruncate(int fd, curl_off_t where);
#undef HAVE_FTRUNCATE
#undef ftruncate /* to be sure */
#define ftruncate(fd, where) msdos_ftruncate(fd, where)
#define USE_MSDOS_FTRUNCATE 1
#endif #endif
#ifdef CURL_CA_EMBED #ifdef CURL_CA_EMBED

View File

@ -78,11 +78,11 @@ int struplocompare4sort(const void *p1, const void *p2)
return struplocompare(*(char * const *)p1, *(char * const *)p2); return struplocompare(*(char * const *)p1, *(char * const *)p2);
} }
#ifdef USE_TOOL_FTRUNCATE #if defined(_WIN32) && !defined(__MINGW32__)
/* /*
* Truncate a file handle at a 64-bit position 'where'. * Truncate a file handle at a 64-bit position 'where'.
*/ */
int tool_ftruncate64(int fd, curl_off_t where) int toolx_ftruncate_win32(int fd, curl_off_t where)
{ {
intptr_t handle = _get_osfhandle(fd); intptr_t handle = _get_osfhandle(fd);
@ -94,21 +94,18 @@ int tool_ftruncate64(int fd, curl_off_t where)
return 0; return 0;
} }
#endif /* USE_TOOL_FTRUNCATE */ #elif defined(__DJGPP__)
#ifdef USE_MSDOS_FTRUNCATE
/* /*
* Only supports 'off_t' (signed 32 bit) as file size. * Only supports 'off_t' (signed 32 bit) as file size.
*/ */
int msdos_ftruncate(int fd, curl_off_t where) int toolx_ftruncate_djgpp(int fd, curl_off_t where)
{ {
if(where > INT_MAX) if(where > INT_MAX)
return -1; return -1;
/* avoid using the macro for this */ return ftruncate(fd, (off_t)where);
return (ftruncate)(fd, (off_t) where);
} }
#endif /* USE_MSDOS_FTRUNCATE */ #endif
#ifdef _WIN32 #ifdef _WIN32
FILE *tool_execpath(const char *filename, char **pathp) FILE *tool_execpath(const char *filename, char **pathp)