Refactoring: move hash_*_ignoring_comments to hash_include_file_* in testutil

This commit is contained in:
Joel Rosdahl 2010-04-25 15:44:18 +02:00
parent fca0348c46
commit b2dd02bbdc
7 changed files with 151 additions and 164 deletions

View File

@ -23,12 +23,12 @@ libs = @LIBS@ -lm
sources = \
ccache.c mdfour.c hash.c execute.c util.c args.c stats.c \
cleanup.c snprintf.c unify.c manifest.c hashtable.c hashtable_itr.c \
murmurhashneutral2.c hashutil.c comments.c getopt_long.c
murmurhashneutral2.c hashutil.c getopt_long.c
all_sources = $(sources) @extra_sources@
headers = \
ccache.h hashtable.h hashtable_itr.h hashtable_private.h hashutil.h \
manifest.h mdfour.h murmurhashneutral2.h comments.h getopt_long.h
manifest.h mdfour.h murmurhashneutral2.h getopt_long.h
objs = $(all_sources:.c=.o)

View File

@ -25,7 +25,6 @@
#include "hashtable_itr.h"
#include "hashutil.h"
#include "manifest.h"
#include "comments.h"
#include <sys/types.h>
#include <sys/stat.h>
@ -343,7 +342,7 @@ static void remember_include_file(char *path, size_t path_len)
}
hash_start(&fhash);
hash_string_ignoring_comments(&fhash, data, st.st_size);
hash_include_file_string(&fhash, data, st.st_size);
h = x_malloc(sizeof(*h));
hash_result_as_bytes(&fhash, h->hash);
@ -861,7 +860,7 @@ static int find_hash(ARGS *args, enum findhash_call_mode mode)
switch (mode) {
case FINDHASH_DIRECT_MODE:
if (!hash_file_ignoring_comments(&hash, input_file)) {
if (!hash_include_file(&hash, input_file)) {
cc_log("Failed to hash %s", input_file);
failed();
}

View File

@ -1,145 +0,0 @@
/*
* Copyright (C) 2009 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
* Software Foundation; either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "ccache.h"
#include "comments.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#define HASH(ch) \
do { \
hashbuf[hashbuflen] = ch; \
hashbuflen++; \
if (hashbuflen == sizeof(hashbuf)) { \
hash_buffer(hash, hashbuf, sizeof(hashbuf)); \
hashbuflen = 0; \
} \
} while (0)
void hash_string_ignoring_comments(
struct mdfour *hash, const char *str, size_t len)
{
const char *p;
const char *end;
char hashbuf[64];
size_t hashbuflen = 0;
p = str;
end = str + len;
while (1) {
if (p >= end) {
goto end;
}
switch (*p) {
case '/':
if (p+1 == end) {
break;
}
switch (*(p+1)) {
case '*':
HASH(' '); /* Don't paste tokens together when
* removing the comment. */
p += 2;
while (p+1 < end
&& (*p != '*' || *(p+1) != '/')) {
if (*p == '\n') {
/* Keep line numbers. */
HASH('\n');
}
p++;
}
if (p+1 == end) {
goto end;
}
p += 2;
continue;
case '/':
p += 2;
while (p < end
&& (*p != '\n' || *(p-1) == '\\')) {
p++;
}
continue;
default:
break;
}
break;
case '"':
HASH(*p);
p++;
while (p < end && (*p != '"' || *(p-1) == '\\')) {
HASH(*p);
p++;
}
if (p == end) {
goto end;
}
break;
default:
break;
}
HASH(*p);
p++;
}
end:
hash_buffer(hash, hashbuf, hashbuflen);
}
/*
* Add contents of a file to a hash, but don't hash comments. Returns 1 on
* success, otherwise 0.
*/
int hash_file_ignoring_comments(struct mdfour *hash, const char *path)
{
int fd;
struct stat st;
char *data;
fd = open(path, O_RDONLY);
if (fd == -1) {
return 0;
}
if (fstat(fd, &st) == -1) {
close(fd);
return 0;
}
if (st.st_size == 0) {
close(fd);
return 1;
}
data = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
close(fd);
if (data == (void *)-1) {
return 0;
}
hash_string_ignoring_comments(hash, data, st.st_size);
munmap(data, st.st_size);
return 1;
}

View File

@ -1,10 +0,0 @@
#ifndef COMMENTS_H
#define COMMENTS_H
#include "mdfour.h"
void hash_string_ignoring_comments(
struct mdfour *hash, const char *str, size_t len);
int hash_file_ignoring_comments(struct mdfour *hash, const char *path);
#endif

View File

@ -1,8 +1,29 @@
#include <string.h>
/*
* Copyright (C) 2009-2010 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
* Software Foundation; either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "ccache.h"
#include "hashutil.h"
#include "murmurhashneutral2.h"
#include <string.h>
#include <sys/mman.h>
#include <fcntl.h>
unsigned int hash_from_string(void *str)
{
return murmurhashneutral2(str, strlen((const char *)str), 0);
@ -18,3 +39,122 @@ int file_hashes_equal(struct file_hash *fh1, struct file_hash *fh2)
return memcmp(fh1->hash, fh2->hash, 16) == 0
&& fh1->size == fh2->size;
}
#define HASH(ch) \
do { \
hashbuf[hashbuflen] = ch; \
hashbuflen++; \
if (hashbuflen == sizeof(hashbuf)) { \
hash_buffer(hash, hashbuf, sizeof(hashbuf)); \
hashbuflen = 0; \
} \
} while (0)
void hash_include_file_string(
struct mdfour *hash, const char *str, size_t len)
{
const char *p;
const char *end;
char hashbuf[64];
size_t hashbuflen = 0;
p = str;
end = str + len;
while (1) {
if (p >= end) {
goto end;
}
switch (*p) {
case '/':
if (p+1 == end) {
break;
}
switch (*(p+1)) {
case '*':
HASH(' '); /* Don't paste tokens together when
* removing the comment. */
p += 2;
while (p+1 < end
&& (*p != '*' || *(p+1) != '/')) {
if (*p == '\n') {
/* Keep line numbers. */
HASH('\n');
}
p++;
}
if (p+1 == end) {
goto end;
}
p += 2;
continue;
case '/':
p += 2;
while (p < end
&& (*p != '\n' || *(p-1) == '\\')) {
p++;
}
continue;
default:
break;
}
break;
case '"':
HASH(*p);
p++;
while (p < end && (*p != '"' || *(p-1) == '\\')) {
HASH(*p);
p++;
}
if (p == end) {
goto end;
}
break;
default:
break;
}
HASH(*p);
p++;
}
end:
hash_buffer(hash, hashbuf, hashbuflen);
}
/*
* Add contents of a file to a hash, but don't hash comments. Returns 1 on
* success, otherwise 0.
*/
int hash_include_file(struct mdfour *hash, const char *path)
{
int fd;
struct stat st;
char *data;
fd = open(path, O_RDONLY);
if (fd == -1) {
return 0;
}
if (fstat(fd, &st) == -1) {
close(fd);
return 0;
}
if (st.st_size == 0) {
close(fd);
return 1;
}
data = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
close(fd);
if (data == (void *)-1) {
return 0;
}
hash_include_file_string(hash, data, st.st_size);
munmap(data, st.st_size);
return 1;
}

View File

@ -1,6 +1,7 @@
#ifndef HASHUTIL_H
#define HASHUTIL_H
#include <mdfour.h>
#include <inttypes.h>
struct file_hash
@ -13,4 +14,8 @@ unsigned int hash_from_string(void *str);
int strings_equal(void *str1, void *str2);
int file_hashes_equal(struct file_hash *fh1, struct file_hash *fh2);
void hash_include_file_string(
struct mdfour *hash, const char *str, size_t len);
int hash_include_file(struct mdfour *hash, const char *path);
#endif

View File

@ -21,7 +21,6 @@
#include "hashutil.h"
#include "manifest.h"
#include "murmurhashneutral2.h"
#include "comments.h"
#include <sys/types.h>
#include <sys/stat.h>
@ -371,8 +370,7 @@ static int verify_object(struct manifest *mf, struct object *obj,
if (!actual) {
actual = x_malloc(sizeof(*actual));
hash_start(&hash);
if (!hash_file_ignoring_comments(
&hash, mf->files[fi->index])) {
if (!hash_include_file(&hash, mf->files[fi->index])) {
cc_log("Failed hashing %s",
mf->files[fi->index]);
free(actual);