diff options
Diffstat (limited to 'lib/hsts.c')
-rw-r--r-- | lib/hsts.c | 34 |
1 files changed, 10 insertions, 24 deletions
diff --git a/lib/hsts.c b/lib/hsts.c index ef166f196..5923671e9 100644 --- a/lib/hsts.c +++ b/lib/hsts.c @@ -35,9 +35,7 @@ #include "sendf.h" #include "strtoofft.h" #include "parsedate.h" -#include "rand.h" -#include "rename.h" -#include "strtoofft.h" +#include "openlock.h" /* The last 3 #include files should be in this order */ #include "curl_printf.h" @@ -319,9 +317,8 @@ CURLcode Curl_hsts_save(struct Curl_easy *data, struct hsts *h, struct Curl_llist_element *e; struct Curl_llist_element *n; CURLcode result = CURLE_OK; - FILE *out; - char *tempstore; - unsigned char randsuffix[9]; + FILE *out = NULL; + struct openlock o; if(!h) /* no cache activated */ @@ -335,17 +332,9 @@ CURLcode Curl_hsts_save(struct Curl_easy *data, struct hsts *h, /* marked as read-only, no file or zero length file name */ goto skipsave; - 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; - else { + result = Curl_openlock(file, &o); + if(!result) { + out = o.out; fputs("# Your HSTS cache. https://curl.se/docs/hsts.html\n" "# This file was generated by libcurl! Edit at your own risk.\n", out); @@ -356,14 +345,8 @@ CURLcode Curl_hsts_save(struct Curl_easy *data, struct hsts *h, if(result) break; } - fclose(out); - if(!result && Curl_rename(tempstore, file)) - result = CURLE_WRITE_ERROR; - - if(result) - unlink(tempstore); } - free(tempstore); + skipsave: if(data->set.hsts_write) { /* if there's a write callback */ @@ -380,6 +363,9 @@ CURLcode Curl_hsts_save(struct Curl_easy *data, struct hsts *h, i.index++; } } + if(out) + Curl_openunlock(&o); + return result; } |