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<uint64_t>(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);
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*));
#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)
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"
$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
$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
}