← Commit list

6041b9b11b

Daniel Stenberg
Daniel Stenberg
2026-03-27
SUCCESS Duration: 333s

Subject

src: use ftruncate() unconditionally

Binary

No binary (build failed)

Architecture & Compiler Flags

Architecture: x86_64-linux-gnu Compiler: gcc (Ubuntu 11.4.0-1ubuntu1~22.04.3) 11.4.0

—

Build Log

(log yok)

Commit Diff GitHub →

src/tool_cb_hdr.c+0 -6
@@ -272,15 +272,9 @@ static size_t save_etag(const char *etag_h, const char *endp,
/*
* Truncate regular files to avoid stale etag content.
*/
-#ifdef HAVE_FTRUNCATE
if(ftruncate(fileno(etag_save->stream), 0))
return CURL_WRITEFUNC_ERROR;
-#else
- if(fseek(etag_save->stream, 0, SEEK_SET))
- return CURL_WRITEFUNC_ERROR;
-#endif
}
-
fwrite(etag_h, 1, etag_length, etag_save->stream);
/* terminate with newline */
fputc('\n', etag_save->stream);
src/tool_operate.c+2 -6
@@ -558,7 +558,7 @@ static CURLcode retrycheck(struct OperationConfig *config,
fflush(outs->stream);
notef("Throwing away %" CURL_FORMAT_CURL_OFF_T " bytes", outs->bytes);
/* truncate file at the position where we started appending */
-#if defined(HAVE_FTRUNCATE) && !defined(__DJGPP__) && !defined(__AMIGA__)
+
if(ftruncate(fileno(outs->stream), outs->init)) {
/* when truncate fails, we cannot append as then we
create something strange, bail out */
@@ -568,11 +568,7 @@ static CURLcode retrycheck(struct OperationConfig *config,
/* now seek to the end of the file, the position where we
truncated the file in a large file-safe way */
rc = fseek(outs->stream, 0, SEEK_END);
-#else
- /* ftruncate is not available, so reposition the file to the location
- we would have truncated it. */
- rc = curlx_fseek(outs->stream, outs->init, SEEK_SET);
-#endif
+
if(rc) {
errorf("Failed seeking to end of file");
return CURLE_WRITE_ERROR;
src/tool_setup.h+8 -0
@@ -108,6 +108,14 @@ int tool_ftruncate64(int fd, curl_off_t where);
#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
+
#ifdef CURL_CA_EMBED
extern const unsigned char curl_ca_embed[];
#endif
src/tool_util.c+13 -0
@@ -96,6 +96,19 @@ int tool_ftruncate64(int fd, curl_off_t where)
}
#endif /* USE_TOOL_FTRUNCATE */
+#ifdef USE_MSDOS_FTRUNCATE
+/*
+ * Only supports 'off_t' (signed 32 bit) as file size.
+ */
+int msdos_ftruncate(int fd, curl_off_t where)
+{
+ if(where > INT_MAX)
+ return -1;
+
+ return ftruncate(fd, (off_t) where);
+}
+#endif /* USE_MSDOS_FTRUNCATE */
+
#ifdef _WIN32
FILE *tool_execpath(const char *filename, char **pathp)
{