]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Clean up long-lived temporary files with an exit function
authorJoel Rosdahl <joel@rosdahl.net>
Wed, 14 Jul 2010 17:40:35 +0000 (19:40 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Wed, 14 Jul 2010 17:40:35 +0000 (19:40 +0200)
ccache.c

index 556d6de4014ba1633db5c72313e336fbad972737..7678ba524eabb5aed8cfb7f42bee103b1c031529 100644 (file)
--- a/ccache.c
+++ b/ccache.c
@@ -253,22 +253,6 @@ static void failed(void)
 {
        char *e;
 
-       /* delete intermediate pre-processor file if needed */
-       if (i_tmpfile) {
-               if (!direct_i_file) {
-                       unlink(i_tmpfile);
-               }
-               free(i_tmpfile);
-               i_tmpfile = NULL;
-       }
-
-       /* delete the cpp stderr file if necessary */
-       if (cpp_stderr) {
-               unlink(cpp_stderr);
-               free(cpp_stderr);
-               cpp_stderr = NULL;
-       }
-
        /* strip any local args */
        args_strip(orig_args, "--ccache-");
 
@@ -287,6 +271,26 @@ static void failed(void)
        fatal("%s: execv returned (%s)", orig_args->argv[0], strerror(errno));
 }
 
+static void
+clean_up_tmp_files()
+{
+       /* delete intermediate pre-processor file if needed */
+       if (i_tmpfile) {
+               if (!direct_i_file) {
+                       unlink(i_tmpfile);
+               }
+               free(i_tmpfile);
+               i_tmpfile = NULL;
+       }
+
+       /* delete the cpp stderr file if necessary */
+       if (cpp_stderr) {
+               unlink(cpp_stderr);
+               free(cpp_stderr);
+               cpp_stderr = NULL;
+       }
+}
+
 /*
  * Transform a name to a full path into the cache directory, creating needed
  * sublevels if needed. Caller frees.
@@ -679,10 +683,7 @@ static void to_cache(ARGS *args)
                close(fd_real_stderr);
                close(fd_result);
                unlink(tmp_stderr2);
-               unlink(cpp_stderr);
                free(tmp_stderr2);
-               free(cpp_stderr);
-               cpp_stderr = NULL;
        }
 
        if (status != 0) {
@@ -701,9 +702,6 @@ static void to_cache(ARGS *args)
                                copy_fd(fd, 2);
                                close(fd);
                                unlink(tmp_stderr);
-                               if (i_tmpfile && !direct_i_file) {
-                                       unlink(i_tmpfile);
-                               }
                                exit(status);
                        }
                }
@@ -1221,22 +1219,6 @@ static void from_cache(enum fromcache_call_mode mode, int put_object_in_manifest
                }
        }
 
-       /* get rid of the intermediate preprocessor file */
-       if (i_tmpfile) {
-               if (!direct_i_file) {
-                       unlink(i_tmpfile);
-               }
-               free(i_tmpfile);
-               i_tmpfile = NULL;
-       }
-
-       /* Delete the cpp stderr file if necessary. */
-       if (cpp_stderr) {
-               unlink(cpp_stderr);
-               free(cpp_stderr);
-               cpp_stderr = NULL;
-       }
-
        /* Send the stderr, if any. */
        fd_stderr = open(cached_stderr, O_RDONLY | O_BINARY);
        if (fd_stderr != -1) {
@@ -2137,6 +2119,7 @@ int main(int argc, char *argv[])
 
        exitfn_init();
        exitfn_add_nullary(stats_flush);
+       exitfn_add_nullary(clean_up_tmp_files);
 
        /* check for logging early so cc_log messages start working ASAP */
        cache_logfile = getenv("CCACHE_LOGFILE");