Don't hash .gch files twice; hash the hash instead

This commit is contained in:
Joel Rosdahl 2010-09-05 21:43:06 +02:00
parent 1d5e983325
commit a914a97462
3 changed files with 16 additions and 34 deletions

View File

@ -284,12 +284,12 @@ get_path_in_cache(const char *name, const char *suffix)
static void
remember_include_file(char *path, size_t path_len, struct mdfour *cpp_hash)
{
struct file_hash *h;
struct mdfour fhash;
struct stat st;
char *source = NULL;
size_t size;
int result;
bool is_pch;
if (path_len >= 2 && (path[0] == '<' && path[path_len - 1] == '>')) {
/* Typically <built-in> or <command-line>. */
@ -324,12 +324,21 @@ remember_include_file(char *path, size_t path_len, struct mdfour *cpp_hash)
hash_start(&fhash);
if (is_precompiled_header(path)) {
hash_file2(&fhash, cpp_hash, path);
is_pch = is_precompiled_header(path);
if (is_pch) {
struct file_hash pch_hash;
if (!hash_file(&fhash, path)) {
goto failure;
}
hash_result_as_bytes(&fhash, pch_hash.hash);
pch_hash.size = fhash.totalN;
hash_delimiter(cpp_hash, "pch_hash");
hash_buffer(cpp_hash, pch_hash.hash, sizeof(pch_hash.hash));
}
if (enable_direct) {
if (!is_precompiled_header(path)) {
struct file_hash *h;
if (!is_pch) { /* else: the file has already been hashed. */
if (st.st_size > 0) {
if (!read_file(path, st.st_size, &source, &size)) {
goto failure;

View File

@ -92,9 +92,7 @@ void hash_delimiter(struct mdfour *md, const char* type);
void hash_string(struct mdfour *md, const char *s);
void hash_int(struct mdfour *md, int x);
bool hash_fd(struct mdfour *md, int fd);
bool hash_fd2(struct mdfour *md1, struct mdfour *md2, int fd);
bool hash_file(struct mdfour *md, const char *fname);
bool hash_file2(struct mdfour *md1, struct mdfour *md2, const char *fname);
/* ------------------------------------------------------------------------- */
/* util.c */

29
hash.c
View File

@ -95,27 +95,12 @@ hash_int(struct mdfour *md, int x)
*/
bool
hash_fd(struct mdfour *md, int fd)
{
return hash_fd2(md, NULL, fd);
}
/*
* Add contents of an open file to the hash. Returns true on success, otherwise
* false.
*/
bool
hash_fd2(struct mdfour *md1, struct mdfour *md2, int fd)
{
char buf[16384];
ssize_t n;
while ((n = read(fd, buf, sizeof(buf))) > 0) {
if (md1) {
hash_buffer(md1, buf, n);
}
if (md2) {
hash_buffer(md2, buf, n);
}
hash_buffer(md, buf, n);
}
return n == 0;
}
@ -126,16 +111,6 @@ hash_fd2(struct mdfour *md1, struct mdfour *md2, int fd)
*/
bool
hash_file(struct mdfour *md, const char *fname)
{
return hash_file2(md, NULL, fname);
}
/*
* Add contents of a file to two hash sums. Returns true on success, otherwise
* false.
*/
bool
hash_file2(struct mdfour *md1, struct mdfour *md2, const char *fname)
{
int fd;
bool ret;
@ -145,7 +120,7 @@ hash_file2(struct mdfour *md1, struct mdfour *md2, const char *fname)
return false;
}
ret = hash_fd2(md1, md2, fd);
ret = hash_fd(md, fd);
close(fd);
return ret;
}