Changeset 1415

Show
Ignore:
Timestamp:
10/13/07 23:25:29 (11 months ago)
Author:
till
Message:

nebula
- process input from directory chronologically when -t is given

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • nebula/trunk/src/cluster.c

    r1414 r1415  
    128128 
    129129 
    130 //              hashq_free(cl2->hq, 0, NULL); 
    131                 free(cl2->hq); 
     130                hashq_free(cl2->hq, 0, NULL); 
    132131                cluster_free(cl2, 0, NULL); 
    133132                num_of_clusters--; 
  • nebula/trunk/src/nebula.c

    r1414 r1415  
    4949int main(int argc, char *argv[]) { 
    5050        int             i, qsize, show_progress; 
    51         u_char          *content, *tmpbuf; 
     51        u_char          time_sort, *content, *tmpbuf; 
    5252        FILE            *md5sum_file, *spamsum_file; 
    53         char            option, *curfile; 
    54         struct dirent   *dir_entry; 
    55         struct stat     statbuf; 
     53        char            option, *dirname; 
    5654        hash            *tmp_hash; 
    5755        qelem           *cur_hq, *tmp_hq; 
    5856        cluster         *cl; 
    5957        double          score; 
    60         DIR             *dirp; 
    6158        trie_node       *t; 
    6259        bstring         bstr; 
     60        int             n; 
     61        struct dirent** namelist; 
    6362 
    6463 
    6564        spamsum_file    = NULL; 
    6665        md5sum_file     = NULL; 
    67         dirp          = NULL; 
     66        dirname               = NULL; 
    6867 
    6968        outlierq        = NULL; 
     
    7877 
    7978        /* default values for parameters */ 
     79        time_sort               = 0;    // don't process files chronologically 
    8080        verbose                 = 0;    // don't be verbose 
    8181        show_progress           = 0;    // don't show progress dots 
     
    8888 
    8989        // 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) { 
    9191                switch(option) { 
    9292                        case 'c': 
     
    9898                                break; 
    9999                        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); 
    106102                                        exit(EXIT_FAILURE); 
    107103                                } 
     
    109105                        case 'l': 
    110106                                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                                } 
    111117                                break; 
    112118                        case 'r': 
     
    117123                                } 
    118124                                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; 
    128127                                break; 
    129128                        case 'v': 
     
    143142        outlierq = hashq_new(); 
    144143 
    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 { 
    146160                if ((argc - optind) < 1) usage(argv[0], EXIT_FAILURE); 
    147161                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); 
    156168        if (!verbose && show_progress) { 
    157169                printf("files processed:                       "); 
     
    159171        } 
    160172 
    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); 
    179175                num_of_files++; 
    180176 
    181                 if (verbose) printf("  processing file %s.\n", curfile); 
     177                if (verbose) printf("  processing file %s.\n", namelist[n]->d_name); 
    182178                else if (show_progress) { 
    183179                        printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"); 
     
    214210                        ((hash*)t->data)->cnt++; 
    215211                        // set filename 
    216                         if ((((hash*)t->data)->filename = strdup(curfile)) == NULL) { 
     212                        if ((((hash*)t->data)->filename = strdup(namelist[n]->d_name)) == NULL) { 
    217213                                fprintf(stderr, "Error - Unable to allocate memory: %m.\n"); 
    218214                                exit(EXIT_FAILURE); 
     
    279275                bstr_unmap(bstr); 
    280276                if (verbose) printf("\n"); 
    281  
    282                 if (!dirp) i++; 
    283         } 
    284  
    285         if (dirp) closedir(dirp); 
     277        } 
    286278 
    287279        cleanup(); 
  • nebula/trunk/src/util.c

    r1414 r1415  
    1919 */ 
    2020 
     21#include <dirent.h> 
    2122#include <fcntl.h> 
    2223#include <stdlib.h> 
     
    7475} 
    7576 
     77 
     78int 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 
     91int 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 
    76111void cleanup(void) { 
    77112        // free data structures 
  • nebula/trunk/src/util.h

    r1414 r1415  
    2626#endif 
    2727 
     28#include <dirent.h> 
     29 
    2830typedef struct bstring { 
    2931        u_int32_t len; 
     
    3335bstring bstr_map(const char* filename); 
    3436void bstr_unmap(bstring bstr); 
     37int regular_file(const struct dirent *e); 
     38int timesort(const void *a, const void *b); 
    3539void cleanup(void); 
    3640