1
0
mirror of https://github.com/moparisthebest/curl synced 2025-02-28 09:21:50 -05:00

rename: a new file for Curl_rename()

And make the cookie save function use it.
This commit is contained in:
Daniel Stenberg 2020-02-17 22:55:34 +01:00
parent b834890a3f
commit 330f133224
No known key found for this signature in database
GPG Key ID: 5CC908FDB71E12C2
4 changed files with 93 additions and 28 deletions

View File

@ -63,7 +63,7 @@ LIB_CFILES = file.c timeval.c base64.c hostip.c progress.c formdata.c \
curl_multibyte.c hostcheck.c conncache.c dotdot.c \ curl_multibyte.c hostcheck.c conncache.c dotdot.c \
x509asn1.c http2.c smb.c curl_endian.c curl_des.c system_win32.c \ x509asn1.c http2.c smb.c curl_endian.c curl_des.c system_win32.c \
mime.c sha256.c setopt.c curl_path.c curl_ctype.c curl_range.c psl.c \ mime.c sha256.c setopt.c curl_path.c curl_ctype.c curl_range.c psl.c \
doh.c urlapi.c curl_get_line.c altsvc.c socketpair.c doh.c urlapi.c curl_get_line.c altsvc.c socketpair.c rename.c
LIB_HFILES = arpa_telnet.h netrc.h file.h timeval.h hostip.h progress.h \ LIB_HFILES = arpa_telnet.h netrc.h file.h timeval.h hostip.h progress.h \
formdata.h cookie.h http.h sendf.h ftp.h url.h dict.h if2ip.h \ formdata.h cookie.h http.h sendf.h ftp.h url.h dict.h if2ip.h \
@ -84,7 +84,7 @@ LIB_HFILES = arpa_telnet.h netrc.h file.h timeval.h hostip.h progress.h \
x509asn1.h http2.h sigpipe.h smb.h curl_endian.h curl_des.h \ x509asn1.h http2.h sigpipe.h smb.h curl_endian.h curl_des.h \
curl_printf.h system_win32.h rand.h mime.h curl_sha256.h setopt.h \ curl_printf.h system_win32.h rand.h mime.h curl_sha256.h setopt.h \
curl_path.h curl_ctype.h curl_range.h psl.h doh.h urlapi-int.h \ curl_path.h curl_ctype.h curl_range.h psl.h doh.h urlapi-int.h \
curl_get_line.h altsvc.h quic.h socketpair.h curl_get_line.h altsvc.h quic.h socketpair.h rename.h
LIB_RCFILES = libcurl.rc LIB_RCFILES = libcurl.rc

View File

@ -98,6 +98,7 @@ Example set of cookies:
#include "inet_pton.h" #include "inet_pton.h"
#include "parsedate.h" #include "parsedate.h"
#include "rand.h" #include "rand.h"
#include "rename.h"
/* The last 3 #include files should be in this order */ /* The last 3 #include files should be in this order */
#include "curl_printf.h" #include "curl_printf.h"
@ -1494,31 +1495,6 @@ static char *get_netscape_format(const struct Cookie *co)
co->value?co->value:""); co->value?co->value:"");
} }
/* return 0 on success, 1 on error */
static int xrename(const char *oldpath, const char *newpath)
{
#ifdef WIN32
/* rename() on Windows doesn't overwrite, so we can't use it here.
MoveFileExA() will overwrite and is usually atomic, however it fails
when there are open handles to the file. */
const int max_wait_ms = 1000;
struct curltime start = Curl_now();
for(;;) {
timediff_t diff;
if(MoveFileExA(oldpath, newpath, MOVEFILE_REPLACE_EXISTING))
break;
diff = Curl_timediff(Curl_now(), start);
if(diff < 0 || diff > max_wait_ms)
return 1;
Sleep(1);
}
#else
if(rename(oldpath, newpath))
return 1;
#endif
return 0;
}
/* /*
* cookie_output() * cookie_output()
* *
@ -1606,7 +1582,7 @@ static int cookie_output(struct Curl_easy *data,
if(out && !use_stdout) { if(out && !use_stdout) {
fclose(out); fclose(out);
out = NULL; out = NULL;
if(xrename(tempstore, filename)) { if(Curl_rename(tempstore, filename)) {
unlink(tempstore); unlink(tempstore);
goto error; goto error;
} }

62
lib/rename.c Normal file
View File

@ -0,0 +1,62 @@
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.haxx.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
***************************************************************************/
#include "rename.h"
#include "curl_setup.h"
#if (!defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)) || \
defined(USE_ALTSVC)
#include "timeval.h"
/* The last 3 #include files should be in this order */
#include "curl_printf.h"
#include "curl_memory.h"
#include "memdebug.h"
/* return 0 on success, 1 on error */
int Curl_rename(const char *oldpath, const char *newpath)
{
#ifdef WIN32
/* rename() on Windows doesn't overwrite, so we can't use it here.
MoveFileExA() will overwrite and is usually atomic, however it fails
when there are open handles to the file. */
const int max_wait_ms = 1000;
struct curltime start = Curl_now();
for(;;) {
timediff_t diff;
if(MoveFileExA(oldpath, newpath, MOVEFILE_REPLACE_EXISTING))
break;
diff = Curl_timediff(Curl_now(), start);
if(diff < 0 || diff > max_wait_ms)
return 1;
Sleep(1);
}
#else
if(rename(oldpath, newpath))
return 1;
#endif
return 0;
}
#endif

27
lib/rename.h Normal file
View File

@ -0,0 +1,27 @@
#ifndef HEADER_CURL_RENAME_H
#define HEADER_CURL_RENAME_H
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.haxx.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
***************************************************************************/
int Curl_rename(const char *oldpath, const char *newpath);
#endif /* HEADER_CURL_RENAME_H */