curlx: add curlx_rename(), fix to support long filenames on Windows

Move existing `Curl_rename()` `rename()` wrapper from lib to
curlx/fopen, and make it a curlx macro/function. To allow using
the local worker function to fixup long filenames on Windows.

Then fix the Windows-specific rename implementation to support long
filenames. This operation may happen when using a cookie jar, HSTS cache
or alt-svc cache, via libcurl or the curl tool.

Before this patch, when passing a long filename to the above options,
a `<random>.tmp` file was left on the disk without renaming it to the
filename passed to curl. There was also 1 second delay for each
attempted rename operation.

Also:
- checksrc: ban raw `rename()` and `MoveFileEx*()` functions.
- Note: `Curl_rename()` returned 1 on failure before this patch, while
  `curlx_rename()` returns -1 after, to match POSIX `rename()`.

Refs:
https://learn.microsoft.com/windows/win32/api/winbase/nf-winbase-movefileexa
https://learn.microsoft.com/windows/win32/fileio/maximum-file-path-limitation

Ref: #20040

Closes #20042
This commit is contained in:
Viktor Szakats 2025-12-20 01:44:46 +01:00
parent 424cef6733
commit 6d0ee7b17b
No known key found for this signature in database
GPG key ID: B5ABD165E2AEF201
10 changed files with 81 additions and 104 deletions

View file

@ -35,7 +35,6 @@
#include "parsedate.h"
#include "sendf.h"
#include "curlx/warnless.h"
#include "rename.h"
#include "strdup.h"
#include "curlx/inet_pton.h"
#include "curlx/strparse.h"
@ -379,7 +378,7 @@ CURLcode Curl_altsvc_save(struct Curl_easy *data,
break;
}
curlx_fclose(out);
if(!result && tempstore && Curl_rename(tempstore, file))
if(!result && tempstore && curlx_rename(tempstore, file))
result = CURLE_WRITE_ERROR;
if(result && tempstore)