Repository: lucy-charmonizer Updated Branches: refs/heads/repeat_remove 09e2d9a21 -> 16cedceaf
Try to rename continuously for 1 second. Before spinning on remove(), spin on rename(). Project: http://git-wip-us.apache.org/repos/asf/lucy-charmonizer/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy-charmonizer/commit/16cedcea Tree: http://git-wip-us.apache.org/repos/asf/lucy-charmonizer/tree/16cedcea Diff: http://git-wip-us.apache.org/repos/asf/lucy-charmonizer/diff/16cedcea Branch: refs/heads/repeat_remove Commit: 16cedceafd4c76e7e6ba74957581c9bff35496c5 Parents: 09e2d9a Author: Marvin Humphrey <[email protected]> Authored: Tue Jul 22 22:17:37 2014 +0000 Committer: Marvin Humphrey <[email protected]> Committed: Tue Jul 22 22:17:37 2014 +0000 ---------------------------------------------------------------------- src/Charmonizer/Core/OperatingSystem.c | 35 ++++++++++++++++------------- 1 file changed, 19 insertions(+), 16 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy-charmonizer/blob/16cedcea/src/Charmonizer/Core/OperatingSystem.c ---------------------------------------------------------------------- diff --git a/src/Charmonizer/Core/OperatingSystem.c b/src/Charmonizer/Core/OperatingSystem.c index 3f0743d..6e1910f 100644 --- a/src/Charmonizer/Core/OperatingSystem.c +++ b/src/Charmonizer/Core/OperatingSystem.c @@ -152,7 +152,7 @@ chaz_OS_remove(const char *name) { size_t name_len = strlen(name); size_t i; char *temp_name = (char*)malloc(name_len + num_random_chars + 1); - const char *working_name; + const char *working_name = name; clock_t start, now; strcpy(temp_name, name); @@ -161,24 +161,27 @@ chaz_OS_remove(const char *name) { } temp_name[name_len+num_random_chars] = '\0'; - if (rename(name, temp_name) == 0) { - working_name = temp_name; - } - else if (errno == ENOENT) { - /* No such file or directory, so no point in trying to remove it. - * (Technically ENOENT is POSIX but hopefully this works.) */ - free(temp_name); - return 0; - } - else { - /* Error during rename, remove using old name. */ - working_name = name; - } - - /* Try over and over again for around 1 second to delete the file. + /* Try over and over again for around 1 second to rename the file. * Ideally we would sleep between attempts, but sleep functionality is not * portable. */ start = now = clock(); + while (now - start < CLOCKS_PER_SEC) { + now = clock(); + if (!rename(name, temp_name)) { + /* The rename succeeded. */ + working_name = temp_name; + break; + } + else if (errno == ENOENT) { + /* No such file or directory, so no point in trying to remove it. + * (Technically ENOENT is POSIX but hopefully this works.) */ + free(temp_name); + return 0; + } + } + + /* Try over and over again for around 1 second to delete the file. */ + start = now = clock(); while (!retval && now - start < CLOCKS_PER_SEC) { now = clock(); retval = !remove(working_name);
