From 7c207c7a72a5c7c3fefd65c6eb646928e7839c63 Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Tue, 2 Sep 2003 18:51:28 +0000 Subject: [PATCH] Next step in refactoring. --- shred.h | 11 ++---- shredtree.c | 100 ++++------------------------------------------------ 2 files changed, 9 insertions(+), 102 deletions(-) diff --git a/shred.h b/shred.h index 0eba471..cd1199e 100644 --- a/shred.h +++ b/shred.h @@ -35,13 +35,8 @@ extern int rws; extern int debug; extern int shredsize; -/* globally visible data; chunk_buffer should be a malloc area set by the caller */ -extern int file_count, chunk_count; -extern struct hash_t *chunk_buffer; - -/* functions */ -extern void generate_shredfile(const char *, FILE *); -extern struct sorthash_t *generate_shredlist(const int, const char *argv[]); -extern void shredreport(int argc, char *argv[]); +/* shredtree.c functions */ +extern char **sorted_file_list(const char *, int *); +extern void shredfile(const char *, void (*hook)(struct hash_t, const char *)); /* shred.h ends here */ diff --git a/shredtree.c b/shredtree.c index f0123d5..263cd2e 100644 --- a/shredtree.c +++ b/shredtree.c @@ -16,11 +16,6 @@ int rws = 0; int debug = 0; int shredsize = 5; -/* globally visible data */ -int file_count, chunk_count, sort_count; -struct hash_t *chunk_buffer; -struct sorthash_t *sort_buffer; - struct item { char *file; @@ -29,6 +24,8 @@ struct item dummy; static struct item *head = &dummy; +static int file_count; + static int eligible(const char *file) /* is the specified file eligible to be compared? */ { @@ -105,36 +102,7 @@ static struct hash_t emit_chunk(shred *display, int linecount) return(out); } -/* - * We differentiate these two functions in order to avaid the memory - * overhead of the extra (char *) when assembling shred lists to be written - * to a file. This is only worth bothering with because the data sets - * can get quite large. - */ - -static void filehook(struct hash_t hash, const char *file) -/* hook to store only hashes */ -{ - chunk_buffer = (struct hash_t *)realloc(chunk_buffer, - sizeof(struct hash_t) * (chunk_count+1)); - - chunk_buffer[chunk_count++] = hash; -} - - -static void corehook(struct hash_t hash, const char *file) -/* hook to store hash and file */ -{ - sort_buffer = (struct sorthash_t *)realloc(sort_buffer, - sizeof(struct sorthash_t) * (sort_count+1)); - - sort_buffer[sort_count].hash = hash; - sort_buffer[sort_count].file = (char *)file; - sort_count++; -} - - -static void shredfile(const char *file, +void shredfile(const char *file, void (*hook)(struct hash_t, const char *)) /* emit hash section for specified file */ { @@ -203,12 +171,13 @@ static int stringsort(void *a, void *b) return strcmp(*(char **)a, *(char **)b); } -static char **sorted_file_list(const char *tree) +char **sorted_file_list(const char *tree, int *fc) /* generate a sorted list of files under the given tree */ { char **place, **list; /* make file list */ + file_count = 0; ftw(tree, treewalker, 16); /* now that we know the length, copy into an array */ @@ -220,65 +189,8 @@ static char **sorted_file_list(const char *tree) qsort(list, file_count, sizeof(char *), stringsort); /* caller is responsible for freeing this */ + *fc = file_count; return(list); } -void generate_shredfile(const char *tree, FILE *ofp) -/* generate shred file for given tree */ -{ - char **place, **list; - u_int32_t netfile_count; - - list = sorted_file_list(tree); - - fputs("#SHIF-A 1.0\n", ofp); - fputs("Generator-Program: shredtree 1.0\n", ofp); - fputs("Hash-Method: MD5\n", ofp); - fprintf(ofp, "Normalization: %s\n", rws ? "remove_whitespace" : "none"); - fprintf(ofp, "Shred-Size: %d\n", shredsize); - fputs("%%\n", ofp); - - netfile_count = htonl(file_count); - fwrite(&netfile_count, sizeof(u_int32_t), 1, ofp); - for (place = list; place < list + file_count; place++) - { - linenum_t net_chunks; - - /* shredfile adds data to chunk_buffer */ - chunk_buffer = (struct hash_t *)calloc(sizeof(struct hash_t), 1); - chunk_count = 0; - shredfile(*place, filehook); - - /* the actual output */ - fputs(*place, ofp); - fputc('\n', ofp); - net_chunks = TONET(chunk_count); - fwrite((char *)&net_chunks, sizeof(linenum_t), 1, ofp); - fwrite(chunk_buffer, sizeof(struct hash_t), chunk_count, ofp); - free(chunk_buffer); - } -} - -struct sorthash_t *generate_shredlist(const int argc, const char *argv[]) -/* generate an in-core list of sorthash structures */ -{ - int i; - - sort_buffer = (struct sorthash_t*)calloc(sizeof(struct sorthash_t),1); - sort_count = 0; - - for (i = 0; i < argc; i++) - { - char **place, **list; - - list = sorted_file_list(argv[i]); - for (place = list; place < list + file_count; place++) - shredfile(*place, corehook); - free(list); - } - - /* caller is responsible for freeing this */ - return(sort_buffer); -} - /* shredtree.c ends here */ -- 2.43.0