From 38ec0f3c4d53fbc006971ae2eeea4bc6b495d0b4 Mon Sep 17 00:00:00 2001 From: Ramiro Polla Date: Thu, 15 Jul 2010 14:28:55 -0300 Subject: [PATCH] Run stat() before open() on some files to allow bailing out earlier --- ccache.c | 17 +++++++++-------- hashutil.c | 16 +++++++--------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/ccache.c b/ccache.c index 7678ba524..dc21fa1b4 100644 --- a/ccache.c +++ b/ccache.c @@ -350,20 +350,21 @@ static void remember_include_file(char *path, size_t path_len) goto ignore; } - /* Let's hash the include file. */ - fd = open(path, O_RDONLY|O_BINARY); - if (fd == -1) { - cc_log("Failed to open include file %s", path); - goto failure; - } - if (fstat(fd, &st) != 0) { - cc_log("Failed to fstat include file %s", path); + if (stat(path, &st) != 0) { + cc_log("Failed to stat include file %s", path); goto failure; } if (S_ISDIR(st.st_mode)) { /* Ignore directory, typically $PWD. */ goto ignore; } + + /* Let's hash the include file. */ + fd = open(path, O_RDONLY|O_BINARY); + if (fd == -1) { + cc_log("Failed to open include file %s", path); + goto failure; + } if (!(sloppiness & SLOPPY_INCLUDE_FILE_MTIME) && st.st_mtime >= time_of_compilation) { cc_log("Include file %s too new", path); diff --git a/hashutil.c b/hashutil.c index b2aa15dfc..2b745b362 100644 --- a/hashutil.c +++ b/hashutil.c @@ -204,20 +204,18 @@ hash_source_code_file(struct mdfour *hash, const char *path) char *data; int result; - fd = open(path, O_RDONLY|O_BINARY); - if (fd == -1) { - cc_log("Failed to open %s", path); - return HASH_SOURCE_CODE_ERROR; - } - if (fstat(fd, &st) == -1) { - cc_log("Failed to fstat %s", path); - close(fd); + if (stat(path, &st) == -1) { + cc_log("Failed to stat %s", path); return HASH_SOURCE_CODE_ERROR; } if (st.st_size == 0) { - close(fd); return HASH_SOURCE_CODE_OK; } + fd = open(path, O_RDONLY|O_BINARY); + if (fd == -1) { + cc_log("Failed to open %s", path); + return HASH_SOURCE_CODE_ERROR; + } data = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); close(fd); if (data == (void *)-1) { -- 2.47.3