]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Add x_try_unlink utility function
authorJoel Rosdahl <joel@rosdahl.net>
Mon, 12 Mar 2018 20:48:59 +0000 (21:48 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Tue, 13 Mar 2018 13:36:42 +0000 (14:36 +0100)
It behaves like x_unlink but doesn’t log a failure if the file is
missing.

src/ccache.h
src/util.c

index b4df272174d30457023c53326f1394fa4778cf05..a4fd28ab9bf56e49f71349bb6652c074dd839f13 100644 (file)
@@ -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
index b89a33e8aba3c90312b44f8f9064241914fe9e59..e7258728fbdefa5502804feefe679892f47aea54 100644 (file)
@@ -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 *