Changeset 1415
- Timestamp:
- 10/13/07 23:25:29 (11 months ago)
- Files:
-
- nebula/trunk/src/cluster.c (modified) (1 diff)
- nebula/trunk/src/nebula.c (modified) (10 diffs)
- nebula/trunk/src/util.c (modified) (2 diffs)
- nebula/trunk/src/util.h (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
nebula/trunk/src/cluster.c
r1414 r1415 128 128 129 129 130 // hashq_free(cl2->hq, 0, NULL); 131 free(cl2->hq); 130 hashq_free(cl2->hq, 0, NULL); 132 131 cluster_free(cl2, 0, NULL); 133 132 num_of_clusters--; nebula/trunk/src/nebula.c
r1414 r1415 49 49 int main(int argc, char *argv[]) { 50 50 int i, qsize, show_progress; 51 u_char *content, *tmpbuf;51 u_char time_sort, *content, *tmpbuf; 52 52 FILE *md5sum_file, *spamsum_file; 53 char option, *curfile; 54 struct dirent *dir_entry; 55 struct stat statbuf; 53 char option, *dirname; 56 54 hash *tmp_hash; 57 55 qelem *cur_hq, *tmp_hq; 58 56 cluster *cl; 59 57 double score; 60 DIR *dirp;61 58 trie_node *t; 62 59 bstring bstr; 60 int n; 61 struct dirent** namelist; 63 62 64 63 65 64 spamsum_file = NULL; 66 65 md5sum_file = NULL; 67 dir p= NULL;66 dirname = NULL; 68 67 69 68 outlierq = NULL; … … 78 77 79 78 /* default values for parameters */ 79 time_sort = 0; // don't process files chronologically 80 80 verbose = 0; // don't be verbose 81 81 show_progress = 0; // don't show progress dots … … 88 88 89 89 // process args 90 while((option = getopt(argc, argv, "c:q: plvd:r:h?")) > 0) {90 while((option = getopt(argc, argv, "c:q:tplvd:r:h?")) > 0) { 91 91 switch(option) { 92 92 case 'c': … … 98 98 break; 99 99 case 'd': 100 if ((dirp = opendir(optarg)) == NULL) { 101 fprintf(stderr, "Error - Unable to open directory %s: %m.\n", optarg); 102 exit(EXIT_FAILURE); 103 } 104 if (chdir(optarg) != 0) { 105 fprintf(stderr, "Error - Unable to change into directory %s: %m.\n", optarg); 100 if (chdir(dirname = optarg) != 0) { 101 fprintf(stderr, "Error - Unable to change into directory %s: %m.\n", dirname); 106 102 exit(EXIT_FAILURE); 107 103 } … … 109 105 case 'l': 110 106 list_files = 1; 107 break; 108 case 'p': 109 show_progress = 1; 110 break; 111 case 'q': 112 outlierq_max = atoi(optarg); 113 if (outlierq_max < 1) { 114 fprintf(stderr, "Error - Outlier queue size must be a non-negative value.\n"); 115 exit(EXIT_FAILURE); 116 } 111 117 break; 112 118 case 'r': … … 117 123 } 118 124 break; 119 case 'q': 120 outlierq_max = atoi(optarg); 121 if (outlierq_max < 1) { 122 fprintf(stderr, "Error - Outlier queue size must be a non-negative value.\n"); 123 exit(EXIT_FAILURE); 124 } 125 break; 126 case 'p': 127 show_progress = 1; 125 case 't': 126 time_sort = 1; 128 127 break; 129 128 case 'v': … … 143 142 outlierq = hashq_new(); 144 143 145 if (!dirp) { 144 if (dirname) { 145 printf("scanning directory... "); 146 fflush(stdout); 147 if (time_sort) { 148 if ((n = scandir(dirname, &namelist, regular_file, timesort)) == -1) { 149 fprintf(stderr, "Error - Unable to read directory entries: %m.\n"); 150 exit(EXIT_FAILURE); 151 } 152 } else { 153 if ((n = scandir(dirname, &namelist, regular_file, alphasort)) == -1) { 154 fprintf(stderr, "Error - Unable to read directory entries: %m.\n"); 155 exit(EXIT_FAILURE); 156 } 157 } 158 total_files = n; 159 } else { 146 160 if ((argc - optind) < 1) usage(argv[0], EXIT_FAILURE); 147 161 i = optind; 148 } 149 150 if (dirp) { 151 while ((dir_entry = readdir(dirp)) != NULL) if (dir_entry->d_type == 8) total_files++; 152 rewinddir(dirp); 153 printf("processing %u files.\n", (unsigned int) total_files); 154 } 155 162 total_files = argc - i; 163 if (time_sort) printf("Only directories (-d) can be processed chronologically (-t).\n"); 164 } 165 166 167 printf("processing %u files.\n", (unsigned int) total_files); 156 168 if (!verbose && show_progress) { 157 169 printf("files processed: "); … … 159 171 } 160 172 161 // process files 162 for (;;) { 163 // get next file content 164 if (dirp) { 165 if ((dir_entry = readdir(dirp)) == NULL) break; 166 curfile = dir_entry->d_name; 167 memset(&statbuf, 0, sizeof(struct stat)); 168 if (stat(curfile, &statbuf) != 0) { 169 fprintf(stderr, "Error - Unable to get file status for %s: %m.\n", curfile); 170 exit(EXIT_FAILURE); 171 } 172 if (!S_ISREG(statbuf.st_mode)) continue; 173 } else { 174 if (i >= argc) break; // all command line argument filenames processed? 175 curfile = argv[i]; 176 } 177 178 bstr = bstr_map(curfile); 173 for (n=0; n<total_files; n++) { 174 bstr = bstr_map(namelist[n]->d_name); 179 175 num_of_files++; 180 176 181 if (verbose) printf(" processing file %s.\n", curfile);177 if (verbose) printf(" processing file %s.\n", namelist[n]->d_name); 182 178 else if (show_progress) { 183 179 printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"); … … 214 210 ((hash*)t->data)->cnt++; 215 211 // set filename 216 if ((((hash*)t->data)->filename = strdup( curfile)) == NULL) {212 if ((((hash*)t->data)->filename = strdup(namelist[n]->d_name)) == NULL) { 217 213 fprintf(stderr, "Error - Unable to allocate memory: %m.\n"); 218 214 exit(EXIT_FAILURE); … … 279 275 bstr_unmap(bstr); 280 276 if (verbose) printf("\n"); 281 282 if (!dirp) i++; 283 } 284 285 if (dirp) closedir(dirp); 277 } 286 278 287 279 cleanup(); nebula/trunk/src/util.c
r1414 r1415 19 19 */ 20 20 21 #include <dirent.h> 21 22 #include <fcntl.h> 22 23 #include <stdlib.h> … … 74 75 } 75 76 77 78 int regular_file(const struct dirent *e) { 79 struct stat sta; 80 memset(&sta, 0, sizeof(struct stat)); 81 82 if (stat(e->d_name, &sta) != 0) { 83 fprintf(stderr, "Error - Unable to get file status for %s: %m.\n", e->d_name); 84 exit(EXIT_FAILURE); 85 } 86 87 return(S_ISREG(sta.st_mode)); 88 } 89 90 91 int timesort(const void *a, const void *b) { 92 struct dirent **A, **B; 93 struct stat sta, stb; 94 memset(&sta, 0, sizeof(struct stat)); 95 memset(&stb, 0, sizeof(struct stat)); 96 97 A = (struct dirent **) a; 98 B = (struct dirent **) b; 99 100 if ((stat((*A)->d_name, &sta) != 0) || (stat((*B)->d_name, &stb) != 0)) { 101 fprintf(stderr, "Error - Unable to get file status for %s, %s: %m.\n", (*A)->d_name, (*B)->d_name); 102 exit(EXIT_FAILURE); 103 } 104 105 if (sta.st_ctime < stb.st_ctime) return(1); 106 if (sta.st_ctime > stb.st_ctime) return(-1); 107 return(0); 108 } 109 110 76 111 void cleanup(void) { 77 112 // free data structures nebula/trunk/src/util.h
r1414 r1415 26 26 #endif 27 27 28 #include <dirent.h> 29 28 30 typedef struct bstring { 29 31 u_int32_t len; … … 33 35 bstring bstr_map(const char* filename); 34 36 void bstr_unmap(bstring bstr); 37 int regular_file(const struct dirent *e); 38 int timesort(const void *a, const void *b); 35 39 void cleanup(void); 36 40
