[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[gnurl] 126/282: rename: a new file for Curl_rename()
From: |
gnunet |
Subject: |
[gnurl] 126/282: rename: a new file for Curl_rename() |
Date: |
Wed, 01 Apr 2020 14:29:51 +0200 |
This is an automated email from the git hooks/post-receive script.
ng0 pushed a commit to branch master
in repository gnurl.
commit 330f133224af18c65b9325d9b6502e07b4f09f6b
Author: Daniel Stenberg <address@hidden>
AuthorDate: Mon Feb 17 22:55:34 2020 +0100
rename: a new file for Curl_rename()
And make the cookie save function use it.
---
lib/Makefile.inc | 4 ++--
lib/cookie.c | 28 ++-----------------------
lib/rename.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
lib/rename.h | 27 ++++++++++++++++++++++++
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, <address@hidden>, 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, <address@hidden>, 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 */
--
To stop receiving notification emails like this one, please contact
address@hidden.
- [gnurl] 113/282: strerror: Increase STRERROR_LEN 128 -> 256, (continued)
- [gnurl] 113/282: strerror: Increase STRERROR_LEN 128 -> 256, gnunet, 2020/04/01
- [gnurl] 107/282: smtp: Simplify the MAIL command and avoid a duplication of send strings, gnunet, 2020/04/01
- [gnurl] 111/282: easy: remove dead code, gnunet, 2020/04/01
- [gnurl] 108/282: RELEASE-NOTES: synced, gnunet, 2020/04/01
- [gnurl] 120/282: CURLINFO_COOKIELIST.3: Fix example, gnunet, 2020/04/01
- [gnurl] 121/282: ftp: convert 'sock_accepted' to a plain boolean, gnunet, 2020/04/01
- [gnurl] 116/282: tool_home: Fix the copyright year being out of date, gnunet, 2020/04/01
- [gnurl] 114/282: strerror.h: Copyright year out of date, gnunet, 2020/04/01
- [gnurl] 115/282: tool_homedir: Change GetEnv() to use libcurl's curl_getenv(), gnunet, 2020/04/01
- [gnurl] 118/282: TODO: CURLOPT_SSL_CTX_FUNCTION for LDAPS, gnunet, 2020/04/01
- [gnurl] 126/282: rename: a new file for Curl_rename(),
gnunet <=
- [gnurl] 124/282: RELEASE-NOTES: synced, gnunet, 2020/04/01
- [gnurl] 110/282: create-dirs.d: mention the mode, gnunet, 2020/04/01
- [gnurl] 109/282: CURLOPT_ALTSVC_CTRL.3: fix the DEFAULT wording, gnunet, 2020/04/01
- [gnurl] 117/282: azure: disable brotli on the macos debug-builds, gnunet, 2020/04/01
- [gnurl] 125/282: cookies: make saving atomic with a rename, gnunet, 2020/04/01
- [gnurl] 122/282: multi: if Curl_readwrite sets 'comeback' use expire, not loop, gnunet, 2020/04/01
- [gnurl] 119/282: TODO: Paged searches on LDAP server, gnunet, 2020/04/01
- [gnurl] 133/282: HTTP-COOKIES: mention that a trailing newline is required, gnunet, 2020/04/01
- [gnurl] 130/282: SOCKS: fix typo in printf formatting, gnunet, 2020/04/01
- [gnurl] 127/282: altsvc: make saving the cache an atomic operation, gnunet, 2020/04/01