From: Joel Rosdahl Date: Thu, 19 Mar 2015 20:27:26 +0000 (+0100) Subject: Introduce logging wrappers for stat/lstat/fstat functions X-Git-Tag: v3.2.2~13 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8deb0011b98ad8e61fe8047566608174190ae58a;p=thirdparty%2Fccache.git Introduce logging wrappers for stat/lstat/fstat functions --- diff --git a/ccache.c b/ccache.c index 41bda17c8..4c6cbe028 100644 --- a/ccache.c +++ b/ccache.c @@ -310,8 +310,7 @@ clean_up_internal_tempdir(void) struct stat st; time_t now = time(NULL); - stat(conf->cache_dir, &st); - if (st.st_mtime + 3600 >= now) { + if (x_stat(conf->cache_dir, &st) != 0 || st.st_mtime + 3600 >= now) { /* No cleanup needed. */ return; } @@ -331,7 +330,7 @@ clean_up_internal_tempdir(void) } path = format("%s/%s", temp_dir(), entry->d_name); - if (lstat(path, &st) == 0 && st.st_mtime + 3600 < now) { + if (x_lstat(path, &st) == 0 && st.st_mtime + 3600 < now) { tmp_unlink(path); } free(path); @@ -422,8 +421,7 @@ remember_include_file(char *path, struct mdfour *cpp_hash) goto ignore; #endif - if (stat(path, &st) != 0) { - cc_log("Failed to stat include file %s: %s", path, strerror(errno)); + if (x_stat(path, &st) != 0) { goto failure; } if (S_ISDIR(st.st_mode)) { @@ -688,8 +686,7 @@ put_file_in_cache(const char *source, const char *dest) failed(); } cc_log("Stored in cache: %s -> %s", source, dest); - if (stat(dest, &st) != 0) { - cc_log("Failed to stat %s: %s", dest, strerror(errno)); + if (x_stat(dest, &st) != 0) { stats_update(STATS_ERROR); failed(); } @@ -767,10 +764,8 @@ void update_manifest_file(void) if (manifest_put(manifest_path, cached_obj_hash, included_files)) { cc_log("Added object file hash to %s", manifest_path); update_mtime(manifest_path); - if (stat(manifest_path, &st) == 0) { + if (x_stat(manifest_path, &st) == 0) { stats_update_size(file_size(&st) - old_size, old_size == 0 ? 1 : 0); - } else { - cc_log("Failed to stat %s: %s", manifest_path, strerror(errno)); } } else { cc_log("Failed to add object file hash to %s", manifest_path); @@ -815,9 +810,8 @@ to_cache(struct args *args) status = execute(args->argv, tmp_stdout_fd, tmp_stderr_fd); args_pop(args, 3); - if (stat(tmp_stdout, &st) != 0) { + if (x_stat(tmp_stdout, &st) != 0) { /* The stdout file was removed - cleanup in progress? Better bail out. */ - cc_log("%s not found: %s", tmp_stdout, strerror(errno)); stats_update(STATS_MISSING); tmp_unlink(tmp_stdout); tmp_unlink(tmp_stderr); @@ -892,7 +886,7 @@ to_cache(struct args *args) failed(); } - if (stat(output_obj, &st) != 0) { + if (x_stat(output_obj, &st) != 0) { cc_log("Compiler didn't produce an object file"); stats_update(STATS_NOOUTPUT); failed(); @@ -903,8 +897,7 @@ to_cache(struct args *args) failed(); } - if (stat(tmp_stderr, &st) != 0) { - cc_log("Failed to stat %s: %s", tmp_stderr, strerror(errno)); + if (x_stat(tmp_stderr, &st) != 0) { stats_update(STATS_ERROR); failed(); } @@ -918,14 +911,9 @@ to_cache(struct args *args) failed(); } cc_log("Stored in cache: %s", cached_stderr); - if (conf->compression) { - /* The file was compressed, so obtain the size again. */ - if (stat(cached_stderr, &st) == 0) { - stats_update_size(file_size(&st), 1); - } else { - cc_log("Failed to stat %s: %s", cached_stderr, strerror(errno)); - } - } else { + if (!conf->compression + /* If the file was compressed, obtain the size again: */ + || (conf->compression && x_stat(cached_stderr, &st) == 0)) { stats_update_size(file_size(&st), 1); } } else { @@ -937,8 +925,7 @@ to_cache(struct args *args) } if (output_dia) { - if (stat(output_dia, &st) != 0) { - cc_log("Failed to stat %s: %s", output_dia, strerror(errno)); + if (x_stat(output_dia, &st) != 0) { stats_update(STATS_ERROR); failed(); } @@ -1191,8 +1178,7 @@ calculate_common_hash(struct args *args, struct mdfour *hash) full_path = full_path_win_ext; #endif - if (stat(full_path, &st) != 0) { - cc_log("Couldn't stat compiler %s: %s", args->argv[0], strerror(errno)); + if (x_stat(full_path, &st) != 0) { stats_update(STATS_COMPILER); failed(); } @@ -1341,7 +1327,7 @@ calculate_object_hash(struct args *args, struct mdfour *hash, int direct_mode) } else if (str_startswith(args->argv[i], "--specs=")) { p = args->argv[i] + 8; } - if (p && stat(p, &st) == 0) { + if (p && x_stat(p, &st) == 0) { /* If given an explicit specs file, then hash that file, but don't include the path to it in the hash. */ hash_delimiter(hash, "specs"); @@ -1350,7 +1336,7 @@ calculate_object_hash(struct args *args, struct mdfour *hash, int direct_mode) } if (str_startswith(args->argv[i], "-fplugin=") - && stat(args->argv[i] + 9, &st) == 0) { + && x_stat(args->argv[i] + 9, &st) == 0) { hash_delimiter(hash, "plugin"); hash_compiler(hash, &st, args->argv[i] + 9, false); continue; @@ -1360,7 +1346,7 @@ calculate_object_hash(struct args *args, struct mdfour *hash, int direct_mode) && i + 3 < args->argc && str_eq(args->argv[i+1], "-load") && str_eq(args->argv[i+2], "-Xclang") - && stat(args->argv[i+3], &st) == 0) { + && x_stat(args->argv[i+3], &st) == 0) { hash_delimiter(hash, "plugin"); hash_compiler(hash, &st, args->argv[i+3], false); continue; diff --git a/ccache.h b/ccache.h index 9d35a553e..6028f46c6 100644 --- a/ccache.h +++ b/ccache.h @@ -136,6 +136,9 @@ void *x_malloc(size_t size); void *x_calloc(size_t nmemb, size_t size); void *x_realloc(void *ptr, size_t size); void x_unsetenv(const char *name); +int x_fstat(int fd, struct stat *buf); +int x_lstat(const char *pathname, struct stat *buf); +int x_stat(const char *pathname, struct stat *buf); void traverse(const char *dir, void (*fn)(const char *, struct stat *)); char *basename(const char *path); char *dirname(const char *path); diff --git a/cleanup.c b/cleanup.c index ee872ccbf..db2ac7f8d 100644 --- a/cleanup.c +++ b/cleanup.c @@ -1,6 +1,6 @@ /* * Copyright (C) 2002-2006 Andrew Tridgell - * Copyright (C) 2009-2014 Joel Rosdahl + * Copyright (C) 2009-2015 Joel Rosdahl * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free @@ -122,7 +122,7 @@ delete_sibling_file(const char *base, const char *extension) if (lstat(path, &st) == 0) { delete_file(path, file_size(&st)); } else if (errno != ENOENT) { - cc_log("Failed to stat %s (%s)", path, strerror(errno)); + cc_log("Failed to stat %s: %s", path, strerror(errno)); } free(path); } diff --git a/manifest.c b/manifest.c index 6eb53fd92..1dcf83f68 100644 --- a/manifest.c +++ b/manifest.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2014 Joel Rosdahl + * Copyright (C) 2009-2015 Joel Rosdahl * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free @@ -393,8 +393,7 @@ verify_object(struct conf *conf, struct manifest *mf, struct object *obj, st = hashtable_search(hashed_files, path); if (!st) { struct stat file_stat; - if (stat(path, &file_stat) == -1) { - cc_log("Failed to stat include file %s: %s", path, strerror(errno)); + if (x_stat(path, &file_stat) != 0) { return 0; } st = x_malloc(sizeof(*st)); diff --git a/util.c b/util.c index 1743ed673..8b5370510 100644 --- a/util.c +++ b/util.c @@ -300,8 +300,7 @@ copy_file(const char *src, const char *dest, int compress_level) * occupy an entire filesystem block, even for empty files. * Turn off compression for empty files to save some space. */ - if (fstat(fd_in, &st) != 0) { - cc_log("fstat error: %s", strerror(errno)); + if (x_fstat(fd_in, &st) != 0) { goto error; } if (file_size(&st) == 0) { @@ -791,6 +790,39 @@ void x_unsetenv(const char *name) #endif } +/* Like fstat() but also call cc_log on failure. */ +int +x_fstat(int fd, struct stat *buf) +{ + int result = fstat(fd, buf); + if (result != 0) { + cc_log("Failed to fstat fd %d: %s", fd, strerror(errno)); + } + return result; +} + +/* Like lstat() but also call cc_log on failure. */ +int +x_lstat(const char *pathname, struct stat *buf) +{ + int result = lstat(pathname, buf); + if (result != 0) { + cc_log("Failed to lstat %s: %s", pathname, strerror(errno)); + } + return result; +} + +/* Like stat() but also call cc_log on failure. */ +int +x_stat(const char *pathname, struct stat *buf) +{ + int result = stat(pathname, buf); + if (result != 0) { + cc_log("Failed to stat %s: %s", pathname, strerror(errno)); + } + return result; +} + /* * Construct a string according to the format and store it in *ptr. The * original *ptr is then freed. @@ -1510,7 +1542,7 @@ read_file(const char *path, size_t size_hint, char **data, size_t *size) if (size_hint == 0) { struct stat st; - if (stat(path, &st) == 0) { + if (x_stat(path, &st) == 0) { size_hint = st.st_size; } }