Changeset 5484


Ignore:
Timestamp:
May 31, 2017, 1:48:49 PM (21 months ago)
Author:
cameron
Message:

Handle non-existent files, directories=read mode, -s

Location:
icGREP/icgrep-devel/icgrep
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/grep_engine.cpp

    r5483 r5484  
    3535#include <sys/stat.h>
    3636#include <fcntl.h>
     37#include <errno.h>
     38#include <mutex>
    3739
    3840#ifdef CUDA_ENABLED
     
    4648namespace grep {
    4749
     50static std::stringstream * resultStrs = nullptr;
     51static std::vector<std::string> inputFiles;
     52static std::vector<std::string> linePrefix;
     53static bool grepMatchFound;
    4854
    4955size_t * startPoints = nullptr;
    5056size_t * accumBytes = nullptr;
     57
     58
     59std::mutex count_mutex;
     60size_t fileCount;
     61
     62// DoGrep thread function.
     63void *DoGrepThreadFunction(void *args)
     64{
     65    size_t fileIdx;
     66    grep::GrepEngine * grepEngine = (grep::GrepEngine *)args;
     67
     68    count_mutex.lock();
     69    fileIdx = fileCount;
     70    fileCount++;
     71    count_mutex.unlock();
     72
     73    while (fileIdx < inputFiles.size()) {
     74        size_t grepResult = grepEngine->doGrep(inputFiles[fileIdx], fileIdx);
     75       
     76        count_mutex.lock();
     77        if (grepResult > 0) grepMatchFound = true;
     78        fileIdx = fileCount;
     79        fileCount++;
     80        count_mutex.unlock();
     81        if (QuietMode && grepMatchFound) pthread_exit(nullptr);
     82    }
     83
     84    pthread_exit(nullptr);
     85}
     86
    5187
    5288
     
    98134
    99135uint64_t GrepEngine::doGrep(const std::string & fileName, const uint32_t fileIdx) const {
     136    struct stat sb;
    100137    const int32_t fd = open(fileName.c_str(), O_RDONLY);
    101138    if (LLVM_UNLIKELY(fd == -1)) {
     139        if (!NoMessagesFlag  && !(Mode == QuietMode)) {
     140            if (errno == EACCES) {
     141                resultStrs[fileIdx] << "icgrep: " << fileName << ": Permission denied.\n";
     142            }
     143            else if (errno == ENOENT) {
     144                resultStrs[fileIdx] << "icgrep: " << fileName << ": No such file.\n";
     145            }
     146            else {
     147                resultStrs[fileIdx] << "icgrep: " << fileName << ": Failed.\n";
     148            }
     149        }
     150        return 0;
     151    }
     152    if (stat(fileName.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode)) {
     153        if (!NoMessagesFlag  && !(Mode == QuietMode)) {
     154            resultStrs[fileIdx] << "icgrep: " << fileName << ": Is a directory.\n";
     155        }
     156        close(fd);
    102157        return 0;
    103158    }
     
    111166    typedef uint64_t (*GrepFunctionType)(int32_t fileDescriptor, const uint32_t fileIdx);
    112167    auto f = reinterpret_cast<GrepFunctionType>(mGrepDriver->getMain());
    113     return f(fileDescriptor, fileIdx);
    114 }
    115 
    116 static int * total_count;
    117 static std::stringstream * resultStrs = nullptr;
    118 static std::vector<std::string> inputFiles;
    119 static std::vector<std::string> linePrefix;
     168   
     169    uint64_t grepResult = f(fileDescriptor, fileIdx);
     170    if (grepResult > 0) grepMatchFound = true;
     171    else if ((Mode == NormalMode) && !resultStrs[fileIdx].str().empty()) grepMatchFound = true;
     172   
     173    if (Mode == CountOnly) {
     174        resultStrs[fileIdx] << linePrefix[fileIdx] << grepResult << "\n";
     175    }
     176    else if (Mode == FilesWithMatch || Mode == FilesWithoutMatch ) {
     177        size_t requiredCount = Mode == FilesWithMatch ? 1 : 0;
     178        if (grepResult == requiredCount) {
     179            resultStrs[fileIdx] << linePrefix[fileIdx];
     180        }
     181    }
     182    else if (Mode == QuietMode) {
     183        if (grepMatchFound) exit(MatchFoundExitCode);
     184    }
     185    return grepResult;
     186}
    120187
    121188void initFileResult(std::vector<std::string> filenames){
     189    grepMatchFound = false;
    122190    const int n = filenames.size();
    123191    linePrefix.resize(n);
     
    125193        WithFilenameFlag = true;
    126194    }
    127     std::string fileSuffix;
    128     if (WithFilenameFlag) {
     195    std::string fileSuffix = "";
     196    bool setLinePrefix = WithFilenameFlag || (Mode == FilesWithMatch) || (Mode == FilesWithoutMatch);
     197    if (setLinePrefix) {
    129198        if (NullFlag) {
    130199            fileSuffix = std::string("\0", 1);
     
    133202            fileSuffix = "\t:";
    134203        }
    135         else {
     204        else if (Mode == CountOnly) {
    136205            fileSuffix = ":";
     206        }
     207        else if ((Mode == FilesWithMatch) || (Mode == FilesWithoutMatch)) {
     208            fileSuffix = "\n";
    137209        }
    138210    }
    139211    inputFiles = filenames;
    140212    resultStrs = new std::stringstream[n];
    141     total_count = new int[n];
    142213    for (unsigned i = 0; i < inputFiles.size(); ++i) {
    143         if (WithFilenameFlag) {
     214        if (setLinePrefix) {
    144215            if (inputFiles[i] == "-") {
    145216                linePrefix[i] = LabelFlag + fileSuffix;
     
    149220            }
    150221        }
    151         total_count[i] = 0;
    152222    }
    153223}
     
    214284}
    215285
    216 void PrintResult(GrepModeType grepMode, std::vector<size_t> & total_CountOnly){
    217     if (grepMode == NormalMode) {
    218         int returnCode = MatchNotFoundExitCode;
    219         for (unsigned i = 0; i < inputFiles.size(); ++i){
    220             std::cout << resultStrs[i].str();
    221             if (!resultStrs[i].str().empty()) returnCode = MatchFoundExitCode;
    222         }
    223         exit(returnCode);
    224     }
    225     if (grepMode == CountOnly) {
    226         size_t total = 0;
    227         if (!WithFilenameFlag) {
    228             for (unsigned i = 0; i < inputFiles.size(); ++i) {
    229                 std::cout << total_CountOnly[i] << std::endl;
    230                 total += total_CountOnly[i];
    231             }
    232         } else {
    233             for (unsigned i = 0; i < inputFiles.size(); ++i){
    234                 std::cout << linePrefix[i] << total_CountOnly[i] << std::endl;
    235                 total += total_CountOnly[i];
    236             };
    237         }
    238         exit(total == 0 ? MatchNotFoundExitCode : MatchFoundExitCode);
    239     }
    240     else if (grepMode == FilesWithMatch || grepMode == FilesWithoutMatch ) {
    241         size_t total = 0;
    242         size_t requiredCount = grepMode == FilesWithMatch ? 1 : 0;
    243         for (unsigned i = 0; i < inputFiles.size(); ++i) {
    244             if (total_CountOnly[i] == requiredCount) {
    245                 std::cout << linePrefix[i];
    246                 if (!NullFlag) {
    247                     std::cout << "\n";
    248                 }
    249             }
    250             total += total_CountOnly[i];
    251         }
    252         exit(total == 0 ? MatchNotFoundExitCode : MatchFoundExitCode);
    253     } else /* QuietMode */ {
    254         for (unsigned i = 0; i < inputFiles.size(); ++i){
    255             if (total_CountOnly[i] > 0) exit(MatchFoundExitCode);
    256         }
    257         exit(MatchNotFoundExitCode);
    258     }
     286void PrintResults(){
     287   
     288    for (unsigned i = 0; i < inputFiles.size(); ++i){
     289        std::cout << resultStrs[i].str();
     290    }
     291    exit(grepMatchFound ? MatchFoundExitCode : MatchNotFoundExitCode);
    259292}
    260293
  • icGREP/icgrep-devel/icgrep/grep_engine.h

    r5481 r5484  
    2020namespace grep {
    2121
     22// Thread function only.
     23void *DoGrepThreadFunction(void *args);
     24   
    2225class GrepEngine {
    2326public:
     
    3437    void doGrep(const std::string & fileName) const;
    3538
    36         uint64_t doGrep(const std::string & fileName, const uint32_t fileIdx) const;
     39    uint64_t doGrep(const std::string & fileName, const uint32_t fileIdx) const;
    3740
    3841    uint64_t doGrep(const int32_t fileDescriptor, const uint32_t fileIdx) const;
     
    4548void initFileResult(std::vector<std::string> filenames);
    4649   
    47 void PrintResult(GrepModeType grepMode, std::vector<size_t> & total_CountOnly);
     50void PrintResults();
    4851   
    4952
  • icGREP/icgrep-devel/icgrep/grep_interface.cpp

    r5483 r5484  
    287287        llvm::report_fatal_error("Sorry, -include is not yet supported.\n");
    288288    }   
    289     if (NoMessagesFlag) {
    290         llvm::report_fatal_error("Sorry, -s is not yet supported.\n");
    291     }
    292289    if (ByteOffsetFlag) {
    293290        llvm::report_fatal_error("Sorry, -b is not yet supported.\n");
  • icGREP/icgrep-devel/icgrep/icgrep.cpp

    r5480 r5484  
    2424#include <re/re_toolchain.h>
    2525#include <pablo/pablo_toolchain.h>
    26 #include <mutex>
    2726#include <boost/filesystem.hpp>
    2827#include <iostream> // MEEE
     
    108107
    109108    return REs;
    110 }
    111 
    112 std::vector<size_t> total_Count;
    113 std::mutex count_mutex;
    114 size_t fileCount;
    115 void *DoGrep(void *args)
    116 {
    117     size_t fileIdx;
    118     grep::GrepEngine * grepEngine = (grep::GrepEngine *)args;
    119 
    120     count_mutex.lock();
    121     fileIdx = fileCount;
    122     fileCount++;
    123     count_mutex.unlock();
    124 
    125     while (fileIdx < allFiles.size()) {
    126         total_Count[fileIdx] = grepEngine->doGrep(allFiles[fileIdx], fileIdx);
    127        
    128         count_mutex.lock();
    129         fileIdx = fileCount;
    130         fileCount++;
    131         count_mutex.unlock();
    132     }
    133 
    134     pthread_exit(nullptr);
    135109}
    136110
     
    212186        allFiles = { "-" };
    213187        grep::initFileResult(allFiles);
    214         total_Count.resize(1);
    215         total_Count[0] = grepEngine.doGrep(STDIN_FILENO, 0);
     188        grepEngine.doGrep(STDIN_FILENO, 0);
    216189
    217190    } else {
     
    230203
    231204        grep::initFileResult(allFiles);
    232         total_Count.resize(allFiles.size());
    233205
    234206        if (Threads <= 1) {
    235207            for (unsigned i = 0; i != allFiles.size(); ++i) {
    236                 total_Count[i] = grepEngine.doGrep(allFiles[i], i);
     208                grepEngine.doGrep(allFiles[i], i);
    237209            }
    238210        } else if (Threads > 1) {
     
    241213
    242214            for(unsigned long i = 0; i < numOfThreads; ++i){
    243                 const int rc = pthread_create(&threads[i], nullptr, DoGrep, (void *)&grepEngine);
     215                const int rc = pthread_create(&threads[i], nullptr, grep::DoGrepThreadFunction, (void *)&grepEngine);
    244216                if (rc) {
    245217                    llvm::report_fatal_error("Failed to create thread: code " + std::to_string(rc));
     
    257229    }
    258230   
    259     grep::PrintResult(grep::Mode, total_Count);
     231    grep::PrintResults();
    260232   
    261233    return 0;
Note: See TracChangeset for help on using the changeset viewer.