From: Joel Rosdahl Date: Sun, 13 Oct 2019 14:28:24 +0000 (+0200) Subject: Remove now superfluous size check of uncompressed files X-Git-Tag: v4.0~737 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=f861c38d8312216e6c9dd2838e9bd33712a33311;p=thirdparty%2Fccache.git Remove now superfluous size check of uncompressed files Uncompressed cache files have a trailing checksum as well; this wasn’t the case earlier in the format design process. --- diff --git a/src/CacheEntryReader.cpp b/src/CacheEntryReader.cpp index 8014fe8e1..59da8562c 100644 --- a/src/CacheEntryReader.cpp +++ b/src/CacheEntryReader.cpp @@ -50,21 +50,6 @@ CacheEntryReader::CacheEntryReader(FILE* stream, throw Error(fmt::format( "Unknown version (actual {}, expected {})", m_version, expected_version)); } - if (m_compression_type == Compression::Type::none) { - // Since we have the size available, let's use it as a super primitive - // consistency check for the non-compressed case. (A real checksum is used - // for compressed data.) - struct stat st; - if (x_fstat(fileno(stream), &st) != 0) { - throw Error(fmt::format("Failed to fstat: {}", strerror(errno))); - } - if (static_cast(st.st_size) != m_content_size) { - throw Error(fmt::format( - "Bad uncompressed file size (actual {} bytes, expected {} bytes)", - st.st_size, - m_content_size)); - } - } m_checksum.update(header_bytes, sizeof(header_bytes)); m_decompressor = Decompressor::create_from_type(m_compression_type, stream); diff --git a/src/ccache.hpp b/src/ccache.hpp index 76d9ce1f9..e7cfd7244 100644 --- a/src/ccache.hpp +++ b/src/ccache.hpp @@ -175,7 +175,6 @@ void* x_calloc(size_t nmemb, size_t size); void* x_realloc(void* ptr, size_t size); void x_setenv(const char* name, const char* value); 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*)); diff --git a/src/legacy_util.cpp b/src/legacy_util.cpp index 943175994..1c5171c50 100644 --- a/src/legacy_util.cpp +++ b/src/legacy_util.cpp @@ -725,17 +725,6 @@ 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) diff --git a/test/suites/base.bash b/test/suites/base.bash index 827fbe154..f8d2d9cae 100644 --- a/test/suites/base.bash +++ b/test/suites/base.bash @@ -267,6 +267,32 @@ base_tests() { test_failed "Result file seems to be uncompressed" fi + # ------------------------------------------------------------------------- + TEST "Corrupt result file" + + $CCACHE_COMPILE -c test1.c + expect_stat 'cache hit (preprocessed)' 0 + expect_stat 'cache miss' 1 + expect_stat 'files in cache' 1 + + $CCACHE_COMPILE -c test1.c + expect_stat 'cache hit (preprocessed)' 1 + expect_stat 'cache miss' 1 + expect_stat 'files in cache' 1 + + result_file=$(find $CCACHE_DIR -name '*.result') + printf foo | dd of=$result_file bs=3 count=1 seek=20 conv=notrunc >&/dev/null + + $CCACHE_COMPILE -c test1.c + expect_stat 'cache hit (preprocessed)' 1 + expect_stat 'cache miss' 2 + expect_stat 'files in cache' 1 + + $CCACHE_COMPILE -c test1.c + expect_stat 'cache hit (preprocessed)' 2 + expect_stat 'cache miss' 2 + expect_stat 'files in cache' 1 + # ------------------------------------------------------------------------- TEST "CCACHE_DISABLE" diff --git a/test/suites/no_compression.bash b/test/suites/no_compression.bash index c7ff6f816..1ca81a63c 100644 --- a/test/suites/no_compression.bash +++ b/test/suites/no_compression.bash @@ -12,7 +12,7 @@ SUITE_no_compression() { $REAL_COMPILER -c -o reference_test.o test.c $CCACHE_COMPILE -c test.c - expect_stat 'cache hit (preprocessed)' 0 + expect_stat 'cache hit (direct)' 0 expect_stat 'cache miss' 1 expect_stat 'files in cache' 2 expect_equal_object_files reference_test.o test.o @@ -51,4 +51,30 @@ SUITE_no_compression() { $CCACHE_COMPILE -c test.c expect_stat 'cache hit (direct)' 2 expect_stat 'cache miss' 1 + + # ------------------------------------------------------------------------- + TEST "Corrupt result file" + + $CCACHE_COMPILE -c test.c + expect_stat 'cache hit (direct)' 0 + expect_stat 'cache miss' 1 + expect_stat 'files in cache' 2 + + $CCACHE_COMPILE -c test.c + expect_stat 'cache hit (direct)' 1 + expect_stat 'cache miss' 1 + expect_stat 'files in cache' 2 + + result_file=$(find $CCACHE_DIR -name '*.result') + printf foo | dd of=$result_file bs=3 count=1 seek=20 conv=notrunc >&/dev/null + + $CCACHE_COMPILE -c test.c + expect_stat 'cache hit (direct)' 1 + expect_stat 'cache miss' 2 + expect_stat 'files in cache' 2 + + $CCACHE_COMPILE -c test.c + expect_stat 'cache hit (direct)' 2 + expect_stat 'cache miss' 2 + expect_stat 'files in cache' 2 }