aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Stenberg <daniel@haxx.se>2020-02-17 22:55:34 +0100
committerDaniel Stenberg <daniel@haxx.se>2020-02-18 07:49:15 +0100
commit330f133224af18c65b9325d9b6502e07b4f09f6b (patch)
tree7e68b7bbfb84fc9b1cb7f280bbe142cc1cf1592c
parentb834890a3fa3f525cd8ef4e99554cdb4558d7e1b (diff)
rename: a new file for Curl_rename()
And make the cookie save function use it.
-rw-r--r--lib/Makefile.inc4
-rw-r--r--lib/cookie.c28
-rw-r--r--lib/rename.c62
-rw-r--r--lib/rename.h27
4 files changed, 93 insertions, 28 deletions
diff --git a/lib/Makefile.inc b/lib/Makefile.inc
index 2b99aa320..46ded90bb 100644
--- a/lib/Makefile.inc
+++ b/lib/Makefile.inc
@@ -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 \
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 \
- 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 \
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 \
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_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
diff --git a/lib/cookie.c b/lib/cookie.c
index 7ae90ea27..2dfdc733f 100644
--- a/lib/cookie.c
+++ b/lib/cookie.c
@@ -98,6 +98,7 @@ Example set of cookies:
#include "inet_pton.h"
#include "parsedate.h"
#include "rand.h"
+#include "rename.h"
/* The last 3 #include files should be in this order */
#include "curl_printf.h"
@@ -1494,31 +1495,6 @@ static char *get_netscape_format(const struct Cookie *co)
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()
*
@@ -1606,7 +1582,7 @@ static int cookie_output(struct Curl_easy *data,
if(out && !use_stdout) {
fclose(out);
out = NULL;
- if(xrename(tempstore, filename)) {
+ if(Curl_rename(tempstore, filename)) {
unlink(tempstore);
goto error;
}
diff --git a/lib/rename.c b/lib/rename.c
new file mode 100644
index 000000000..bb170d3cc
--- /dev/null
+++ b/lib/rename.c
@@ -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
diff --git a/lib/rename.h b/lib/rename.h
new file mode 100644
index 000000000..d7442c844
--- /dev/null
+++ b/lib/rename.h
@@ -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 */