summaryrefslogtreecommitdiff
path: root/lib/altsvc.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/altsvc.c')
-rw-r--r--lib/altsvc.c38
1 files changed, 11 insertions, 27 deletions
diff --git a/lib/altsvc.c b/lib/altsvc.c
index 4ab77fdfc..610969a97 100644
--- a/lib/altsvc.c
+++ b/lib/altsvc.c
@@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
- * Copyright (C) 2019 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
+ * Copyright (C) 2019 - 2021, 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
@@ -34,8 +34,7 @@
#include "parsedate.h"
#include "sendf.h"
#include "warnless.h"
-#include "rand.h"
-#include "rename.h"
+#include "openlock.h"
/* The last 3 #include files should be in this order */
#include "curl_printf.h"
@@ -322,15 +321,12 @@ void Curl_altsvc_cleanup(struct altsvcinfo **altsvcp)
/*
* Curl_altsvc_save() writes the altsvc cache to a file.
*/
-CURLcode Curl_altsvc_save(struct Curl_easy *data,
- struct altsvcinfo *altsvc, const char *file)
+CURLcode Curl_altsvc_save(struct altsvcinfo *altsvc, const char *file)
{
struct Curl_llist_element *e;
struct Curl_llist_element *n;
+ struct openlock o;
CURLcode result = CURLE_OK;
- FILE *out;
- char *tempstore;
- unsigned char randsuffix[9];
if(!altsvc)
/* no cache activated */
@@ -344,35 +340,23 @@ CURLcode Curl_altsvc_save(struct Curl_easy *data,
/* marked as read-only, no file or zero length file name */
return CURLE_OK;
- if(Curl_rand_hex(data, randsuffix, sizeof(randsuffix)))
- return CURLE_FAILED_INIT;
-
- tempstore = aprintf("%s.%s.tmp", file, randsuffix);
- if(!tempstore)
- return CURLE_OUT_OF_MEMORY;
-
- out = fopen(tempstore, FOPEN_WRITETEXT);
- if(!out)
- result = CURLE_WRITE_ERROR;
+ result = Curl_openlock(file, &o);
+ if(result)
+ goto error;
else {
fputs("# Your alt-svc cache. https://curl.se/docs/alt-svc.html\n"
"# This file was generated by libcurl! Edit at your own risk.\n",
- out);
+ o.out);
for(e = altsvc->list.head; e; e = n) {
struct altsvc *as = e->ptr;
n = e->next;
- result = altsvc_out(as, out);
+ result = altsvc_out(as, o.out);
if(result)
break;
}
- fclose(out);
- if(!result && Curl_rename(tempstore, file))
- result = CURLE_WRITE_ERROR;
-
- if(result)
- unlink(tempstore);
}
- free(tempstore);
+ error:
+ Curl_openunlock(&o);
return result;
}