From 20b18a43f6dd8ff3a1ae72c2774f469a74224fc7 Mon Sep 17 00:00:00 2001 From: Viktor Szakats Date: Fri, 27 Mar 2026 21:51:43 +0100 Subject: [PATCH] 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 6041b9b11b904c64305eb6c3f456b83288c1f323 #21109 Closes #21130 --- CMake/unix-cache.cmake | 1 - CMake/win32-cache.cmake | 2 - CMakeLists.txt | 1 - configure.ac | 1 - lib/config-mac.h | 1 - lib/config-os400.h | 3 -- lib/config-riscos.h | 3 -- lib/config-win32.h | 5 --- lib/curl_config-cmake.h.in | 3 -- m4/curl-functions.m4 | 85 -------------------------------------- src/tool_cb_hdr.c | 2 +- src/tool_operate.c | 2 +- src/tool_setup.h | 28 ++++--------- src/tool_util.c | 15 +++---- 14 files changed, 17 insertions(+), 135 deletions(-) diff --git a/CMake/unix-cache.cmake b/CMake/unix-cache.cmake index 51bfa17d29..e8a7df1cbc 100644 --- a/CMake/unix-cache.cmake +++ b/CMake/unix-cache.cmake @@ -88,7 +88,6 @@ elseif(CYGWIN OR set(HAVE_FSETXATTR_5 1) set(HAVE_FSETXATTR_6 0) endif() -set(HAVE_FTRUNCATE 1) set(HAVE_GETADDRINFO 1) if(CMAKE_SYSTEM_NAME STREQUAL "OpenBSD") set(HAVE_GETADDRINFO_THREADSAFE 0) diff --git a/CMake/win32-cache.cmake b/CMake/win32-cache.cmake index 77535a6367..bd9bb6e3fc 100644 --- a/CMake/win32-cache.cmake +++ b/CMake/win32-cache.cmake @@ -29,7 +29,6 @@ if(MINGW) set(HAVE_BASENAME 1) set(HAVE_BOOL_T 1) # = HAVE_STDBOOL_H set(HAVE_DIRENT_H 1) - set(HAVE_FTRUNCATE 1) set(HAVE_GETTIMEOFDAY 1) set(HAVE_LIBGEN_H 1) set(HAVE_OPENDIR 1) @@ -42,7 +41,6 @@ if(MINGW) set(HAVE_UTIME_H 1) # wrapper to sys/utime.h else() set(HAVE_DIRENT_H 0) - set(HAVE_FTRUNCATE 0) set(HAVE_GETTIMEOFDAY 0) set(HAVE_LIBGEN_H 0) set(HAVE_OPENDIR 0) diff --git a/CMakeLists.txt b/CMakeLists.txt index a0a4a016d6..c75571acb7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1637,7 +1637,6 @@ check_symbol_exists("freeaddrinfo" "${CURL_INCLUDES}" HAVE_FREEADDRINFO) # w check_function_exists("pipe" HAVE_PIPE) check_function_exists("pipe2" HAVE_PIPE2) 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("getsockname" "${CURL_INCLUDES}" HAVE_GETSOCKNAME) # winsock2.h unistd.h proto/bsdsocket.h check_function_exists("getrlimit" HAVE_GETRLIMIT) diff --git a/configure.ac b/configure.ac index 762cb59b70..bdd316190a 100644 --- a/configure.ac +++ b/configure.ac @@ -4086,7 +4086,6 @@ CURL_CHECK_FUNC_CLOSESOCKET_CAMEL CURL_CHECK_FUNC_FCNTL CURL_CHECK_FUNC_FREEADDRINFO CURL_CHECK_FUNC_FSETXATTR -CURL_CHECK_FUNC_FTRUNCATE CURL_CHECK_FUNC_GETADDRINFO CURL_CHECK_FUNC_GETHOSTBYNAME_R CURL_CHECK_FUNC_GETHOSTNAME diff --git a/lib/config-mac.h b/lib/config-mac.h index e9806b4e37..fd9d3e7b60 100644 --- a/lib/config-mac.h +++ b/lib/config-mac.h @@ -51,7 +51,6 @@ #define HAVE_SYS_UTIME_H 1 #define HAVE_SYS_IOCTL_H 1 #define HAVE_ALARM 1 -#define HAVE_FTRUNCATE 1 #define HAVE_UTIME 1 #define HAVE_SELECT 1 #define HAVE_SOCKET 1 diff --git a/lib/config-os400.h b/lib/config-os400.h index 4b922df733..2ae1424b28 100644 --- a/lib/config-os400.h +++ b/lib/config-os400.h @@ -69,9 +69,6 @@ /* Define to 1 if you have the freeaddrinfo function. */ #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 HAVE_GETADDRINFO 1 diff --git a/lib/config-riscos.h b/lib/config-riscos.h index 879be03d52..afff218a76 100644 --- a/lib/config-riscos.h +++ b/lib/config-riscos.h @@ -48,9 +48,6 @@ /* Define if you have the header file. */ #define HAVE_FCNTL_H -/* Define if you have the `ftruncate' function. */ -#define HAVE_FTRUNCATE - /* Define if getaddrinfo exists and works */ #define HAVE_GETADDRINFO diff --git a/lib/config-win32.h b/lib/config-win32.h index 4126726716..f73e2ab0a1 100644 --- a/lib/config-win32.h +++ b/lib/config-win32.h @@ -86,11 +86,6 @@ /* Define if you have the closesocket function. */ #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 HAVE_GETPEERNAME 1 diff --git a/lib/curl_config-cmake.h.in b/lib/curl_config-cmake.h.in index 17a2aabde4..34f163a743 100644 --- a/lib/curl_config-cmake.h.in +++ b/lib/curl_config-cmake.h.in @@ -252,9 +252,6 @@ /* Define to 1 if you have the fseeko declaration. */ #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. */ #cmakedefine HAVE_GETADDRINFO 1 diff --git a/m4/curl-functions.m4 b/m4/curl-functions.m4 index f4fbe3ad78..e59bf50a5a 100644 --- a/m4/curl-functions.m4 +++ b/m4/curl-functions.m4 @@ -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 ------------------------------------------------- dnl Verify if getaddrinfo is available, prototyped, can diff --git a/src/tool_cb_hdr.c b/src/tool_cb_hdr.c index 747a8ac2c6..c51637a8b0 100644 --- a/src/tool_cb_hdr.c +++ b/src/tool_cb_hdr.c @@ -270,7 +270,7 @@ static size_t save_etag(const char *etag_h, const char *endp, if((fd != -1) && !curlx_fstat(fd, &file) && (S_ISREG(file.st_mode) && - ftruncate(fd, 0))) + toolx_ftruncate(fd, 0))) return CURL_WRITEFUNC_ERROR; fwrite(etag_h, 1, etag_length, etag_save->stream); diff --git a/src/tool_operate.c b/src/tool_operate.c index 8f0e6c0b9e..6e07c5e769 100644 --- a/src/tool_operate.c +++ b/src/tool_operate.c @@ -559,7 +559,7 @@ static CURLcode retrycheck(struct OperationConfig *config, notef("Throwing away %" CURL_FORMAT_CURL_OFF_T " bytes", outs->bytes); /* 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 create something strange, bail out */ errorf("Failed to truncate file"); diff --git a/src/tool_setup.h b/src/tool_setup.h index 7a8c47347f..7a5f597bbb 100644 --- a/src/tool_setup.h +++ b/src/tool_setup.h @@ -94,26 +94,16 @@ extern FILE *tool_stderr; #ifdef _WIN32 /* set in init_terminal() */ extern bool tool_term_has_bold; +#endif -#ifndef 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 /* !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 +#if defined(_WIN32) && !defined(__MINGW32__) +int toolx_ftruncate_win32(int fd, curl_off_t where); +#define toolx_ftruncate toolx_ftruncate_win32 +#elif defined(__DJGPP__) +int toolx_ftruncate_djgpp(int fd, curl_off_t where); +#define toolx_ftruncate toolx_ftruncate_djgpp +#else +#define toolx_ftruncate ftruncate #endif #ifdef CURL_CA_EMBED diff --git a/src/tool_util.c b/src/tool_util.c index ce235e3bd8..b5838f8564 100644 --- a/src/tool_util.c +++ b/src/tool_util.c @@ -78,11 +78,11 @@ int struplocompare4sort(const void *p1, const void *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'. */ -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); @@ -94,21 +94,18 @@ int tool_ftruncate64(int fd, curl_off_t where) return 0; } -#endif /* USE_TOOL_FTRUNCATE */ - -#ifdef USE_MSDOS_FTRUNCATE +#elif defined(__DJGPP__) /* * 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) 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 FILE *tool_execpath(const char *filename, char **pathp)