diff options
author | Jay Satiro <raysatiro@yahoo.com> | 2020-02-17 02:30:59 -0500 |
---|---|---|
committer | Jay Satiro <raysatiro@yahoo.com> | 2020-02-17 02:30:59 -0500 |
commit | 0c1de413c0757535846400dcd954194179cd2aa4 (patch) | |
tree | 14eff4b31ba597586d7145ebcaccbe14aee0e3b5 | |
parent | ead1e2492f880adf675003c0c73c28cb97dce537 (diff) | |
download | curl-bagder/cookie-atomic.tar.gz |
squashme: improvementsbagder/cookie-atomic
- fix windows wait time (don't rely on MoveFileEx to return right away)
- use tmp extension (eg: outfile.<randhex>.tmp)
- unlink tmpfile on fail (don't want to pollute)
-rw-r--r-- | lib/cookie.c | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/lib/cookie.c b/lib/cookie.c index bf2afc006..7ae90ea27 100644 --- a/lib/cookie.c +++ b/lib/cookie.c @@ -1501,20 +1501,24 @@ static int xrename(const char *oldpath, const char *newpath) /* 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. */ - int i = 0; - int max_wait_ms = 1000; - for(i = 0; i < max_wait_ms; ++i, Sleep(1)) { + 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); } - if(i == max_wait_ms) - return 1; #else if(rename(oldpath, newpath)) return 1; #endif return 0; } + /* * cookie_output() * @@ -1550,7 +1554,7 @@ static int cookie_output(struct Curl_easy *data, if(Curl_rand_hex(data, randsuffix, sizeof(randsuffix))) return 2; - tempstore = aprintf("%s.%s", filename, randsuffix); + tempstore = aprintf("%s.%s.tmp", filename, randsuffix); if(!tempstore) return 1; @@ -1602,8 +1606,10 @@ static int cookie_output(struct Curl_easy *data, if(out && !use_stdout) { fclose(out); out = NULL; - if(xrename(tempstore, filename)) + if(xrename(tempstore, filename)) { + unlink(tempstore); goto error; + } } goto cleanup; |