From: Joel Rosdahl Date: Mon, 12 Mar 2018 20:48:59 +0000 (+0100) Subject: Add x_try_unlink utility function X-Git-Tag: v3.4.2~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=dfddac1030db943cac9257e339b1489191f9180f;p=thirdparty%2Fccache.git Add x_try_unlink utility function It behaves like x_unlink but doesn’t log a failure if the file is missing. --- diff --git a/src/ccache.h b/src/ccache.h index b4df27217..a4fd28ab9 100644 --- a/src/ccache.h +++ b/src/ccache.h @@ -191,6 +191,7 @@ void x_exit(int status) ATTR_NORETURN; int x_rename(const char *oldpath, const char *newpath); int tmp_unlink(const char *path); int x_unlink(const char *path); +int x_try_unlink(const char *path); #ifndef _WIN32 char *x_readlink(const char *path); #endif diff --git a/src/util.c b/src/util.c index b89a33e8a..e7258728f 100644 --- a/src/util.c +++ b/src/util.c @@ -1473,9 +1473,8 @@ tmp_unlink(const char *path) return rc; } -// Remove path, NFS safe. -int -x_unlink(const char *path) +static int +do_x_unlink(const char *path, bool log_failure) { int saved_errno = 0; @@ -1483,7 +1482,6 @@ x_unlink(const char *path) // file. We don't care if the temp file is trashed, so it's always safe to // unlink it first. char *tmp_name = format("%s.rm.%s", path, tmp_string()); - cc_log("Unlink %s via %s", path, tmp_name); int result = 0; if (x_rename(path, tmp_name) == -1) { @@ -1500,14 +1498,31 @@ x_unlink(const char *path) } out: - free(tmp_name); - if (result) { - cc_log("x_unlink failed: %s", strerror(saved_errno)); + if (result == 0 || log_failure) { + cc_log("Unlink %s via %s", path, tmp_name); + if (result != 0 && log_failure) { + cc_log("x_unlink failed: %s", strerror(saved_errno)); + } } + free(tmp_name); errno = saved_errno; return result; } +// Remove path, NFS safe, log both successes and failures. +int +x_unlink(const char *path) +{ + return do_x_unlink(path, true); +} + +// Remove path, NFS safe, only log successes. +int +x_try_unlink(const char *path) +{ + return do_x_unlink(path, false); +} + #ifndef _WIN32 // Like readlink() but returns the string or NULL on failure. Caller frees. char *