fixing crashes, adding threadsafety and changing welcome.,php
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
#ifndef SCAN_CPP
|
||||
#define SCAN_CPP
|
||||
#include "scan.h"
|
||||
#include <windows.h>
|
||||
#include <iostream>
|
||||
@@ -13,14 +15,21 @@
|
||||
#include "log.h"
|
||||
#include "virus_ctrl.h"
|
||||
#include "app_ctrl.h"
|
||||
#ifndef SCAN_CPP
|
||||
#define SCAN_CPP
|
||||
#include <mutex> // Include the mutex header
|
||||
|
||||
// Define mutexes for thread synchronization
|
||||
std::mutex fileHandlesMutex;
|
||||
std::mutex mappingHandlesMutex;
|
||||
std::mutex fileDataMutex;
|
||||
std::mutex cntMutex;
|
||||
std::mutex numThreadsMutex;
|
||||
|
||||
std::unordered_map<std::string, HANDLE> fileHandles;
|
||||
std::unordered_map<std::string, HANDLE> mappingHandles;
|
||||
std::unordered_map<std::string, char*> fileData;
|
||||
|
||||
int cnt = 0;
|
||||
unsigned int num_threads=0;
|
||||
unsigned int num_threads = 0;
|
||||
|
||||
//load all the db files into memory
|
||||
int initialize(const std::string& folderPath) {
|
||||
@@ -32,7 +41,7 @@ int initialize(const std::string& folderPath) {
|
||||
}
|
||||
|
||||
// Create the filename based on the naming convention
|
||||
std::string filename = folderPath +"\\" + firstChar + secondChar + ".jdbf";
|
||||
std::string filename = folderPath + "\\" + firstChar + secondChar + ".jdbf";
|
||||
//printf("Loading %s\n", filename.c_str());
|
||||
|
||||
// Open the file
|
||||
@@ -45,7 +54,7 @@ int initialize(const std::string& folderPath) {
|
||||
// Create the file mapping
|
||||
HANDLE hMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
|
||||
if (hMapping == NULL) {
|
||||
// log(LOGLEVEL::ERR, "[initialize()]: Error creating database file mapping: ", filename);
|
||||
// log(LOGLEVEL::ERR, "[initialize()]: Error creating database file mapping: ", filename);
|
||||
CloseHandle(hFile);
|
||||
return 2;
|
||||
}
|
||||
@@ -60,15 +69,23 @@ int initialize(const std::string& folderPath) {
|
||||
}
|
||||
|
||||
// Store the handles in the global maps
|
||||
fileHandles[filename] = hFile;
|
||||
mappingHandles[filename] = hMapping;
|
||||
fileData[filename] = fileDataPtr;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(fileHandlesMutex);
|
||||
fileHandles[filename] = hFile;
|
||||
}
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mappingHandlesMutex);
|
||||
mappingHandles[filename] = hMapping;
|
||||
}
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(fileDataMutex);
|
||||
fileData[filename] = fileDataPtr;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
// Call this function when you are done using the file mappings
|
||||
void cleanup() {
|
||||
for (const auto& entry : fileHandles) {
|
||||
@@ -78,31 +95,59 @@ void cleanup() {
|
||||
}
|
||||
|
||||
// Clear the global maps
|
||||
fileHandles.clear();
|
||||
mappingHandles.clear();
|
||||
fileData.clear();
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(fileHandlesMutex);
|
||||
fileHandles.clear();
|
||||
}
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mappingHandlesMutex);
|
||||
mappingHandles.clear();
|
||||
}
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(fileDataMutex);
|
||||
fileData.clear();
|
||||
}
|
||||
}
|
||||
|
||||
//the latest and fastest version of searching a hash by now
|
||||
int search_hash(const std::string& dbname_, const std::string& hash_, const std::string& filepath_) {
|
||||
// Check if the file mapping is already open for the given filename
|
||||
thread_local std::string dbname (dbname_);
|
||||
thread_local std::string hash (hash_);
|
||||
thread_local std::string filepath (filepath_);
|
||||
std::string dbname;
|
||||
std::string hash;
|
||||
std::string filepath;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(fileHandlesMutex);
|
||||
dbname = dbname_;
|
||||
}
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(fileDataMutex);
|
||||
hash = hash_;
|
||||
}
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mappingHandlesMutex);
|
||||
filepath = filepath_;
|
||||
}
|
||||
|
||||
thread_local auto fileIter = fileHandles.find(dbname);
|
||||
if (fileIter == fileHandles.end() and dbname_.find("c:.jdbf") == std::string::npos) {
|
||||
auto fileIter = fileHandles.find(dbname);
|
||||
if (fileIter == fileHandles.end() && dbname_.find("c:.jdbf") == std::string::npos) {
|
||||
log(LOGLEVEL::ERR, "[search_hash()]: File mapping not initialized for ", dbname);
|
||||
return 2;
|
||||
}else if (fileIter == fileHandles.end()) {
|
||||
}
|
||||
else if (fileIter == fileHandles.end()) {
|
||||
return 2;
|
||||
}
|
||||
|
||||
// Use fileData for subsequent searches
|
||||
thread_local DWORD fileSize = GetFileSize(fileHandles[dbname], NULL);
|
||||
thread_local std::string fileContent(fileData[dbname], fileSize);
|
||||
DWORD fileSize;
|
||||
std::string fileContent;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(fileDataMutex);
|
||||
fileSize = GetFileSize(fileHandles[dbname], NULL);
|
||||
fileContent = std::string(fileData[dbname], fileSize);
|
||||
}
|
||||
|
||||
// Search for the specific string in the file content
|
||||
thread_local size_t foundPos = fileContent.find(hash);
|
||||
size_t foundPos = fileContent.find(hash);
|
||||
if (foundPos != std::string::npos) {
|
||||
//log(LOGLEVEL::VIRUS, "[search_hash()]: Found virus: ", hash, " in file: ", filepath);
|
||||
virus_ctrl_store(filepath.c_str(), hash.c_str(), hash.c_str());
|
||||
@@ -112,6 +157,10 @@ int search_hash(const std::string& dbname_, const std::string& hash_, const std:
|
||||
}
|
||||
return 0; // Not found
|
||||
}
|
||||
|
||||
// Rest of the code remains unchanged...
|
||||
|
||||
|
||||
bool file_exists(const std::string& filePath) {
|
||||
DWORD fileAttributes = GetFileAttributes(filePath.c_str());
|
||||
|
||||
@@ -131,7 +180,7 @@ void scan_folder(const std::string& directory) {
|
||||
HANDLE hFind = FindFirstFile(search_path.c_str(), &find_file_data);
|
||||
|
||||
if (hFind == INVALID_HANDLE_VALUE) {
|
||||
log(LOGLEVEL::WARN, "[scan_folder()]: Could not open directory: ", search_path.c_str() , " while scanning files inside directory.");
|
||||
log(LOGLEVEL::WARN, "[scan_folder()]: Could not open directory: ", search_path.c_str(), " while scanning files inside directory.");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -147,15 +196,17 @@ void scan_folder(const std::string& directory) {
|
||||
scan_folder(full_path);
|
||||
}
|
||||
else {
|
||||
//action scanfile_t will start the trheads for scanning the hashes
|
||||
//action_scanfile_t(full_path.c_str());
|
||||
//action scanfile_t will start the trheads for scanning the hashes
|
||||
//action_scanfile_t(full_path.c_str());
|
||||
|
||||
//do multithreading here
|
||||
//do multithreading here
|
||||
while (num_threads >= std::thread::hardware_concurrency()) {
|
||||
Sleep(10);
|
||||
}
|
||||
Sleep(10);
|
||||
}
|
||||
//log(LOGLEVEL::INFO_NOSEND, "[scan_folder()]: Scanning file: ", full_path, "threads: ",num_threads);
|
||||
std::thread scan_thread(scan_file_t, full_path);
|
||||
scan_thread.detach();
|
||||
//Sleep(1);
|
||||
|
||||
cnt++;
|
||||
if (cnt % 100 == 0) {
|
||||
@@ -171,13 +222,13 @@ void scan_folder(const std::string& directory) {
|
||||
|
||||
|
||||
//for singlethreaded scans
|
||||
void action_scanfile(const char*filepath) {
|
||||
void action_scanfile(const char* filepath) {
|
||||
thread_init();
|
||||
char* db_path = new char[300];
|
||||
|
||||
//log(LOGLEVEL::INFO, "[action_scanfile_t()]: Scanning file: ", filepath);
|
||||
if (strlen(filepath) == 0 or strcmp("", filepath) == 0 or file_exists(filepath) == false) {
|
||||
thread_shutdown();
|
||||
thread_shutdown();
|
||||
delete[] db_path;
|
||||
return; //no filepath given or file not accessible
|
||||
}
|
||||
@@ -192,20 +243,26 @@ void action_scanfile(const char*filepath) {
|
||||
delete[] db_path;
|
||||
thread_shutdown();
|
||||
}
|
||||
void action_scanfolder(const char*folderpath) {
|
||||
thread_init();
|
||||
void action_scanfolder(const char* folderpath) {
|
||||
thread_init();
|
||||
cnt = 0;
|
||||
thread_local std::string folderpath_ (folderpath);
|
||||
scan_folder(folderpath_);
|
||||
thread_shutdown();
|
||||
thread_local std::string folderpath_(folderpath);
|
||||
scan_folder(folderpath_);
|
||||
thread_shutdown();
|
||||
}
|
||||
|
||||
void scan_file_t(const std::string& filepath_) {
|
||||
num_threads++;
|
||||
thread_local const std::string filepath (filepath_);
|
||||
thread_local const std::string filepath(filepath_);
|
||||
thread_local char* db_path = new char[300];
|
||||
thread_local char* hash = new char[300];
|
||||
strcpy_s(hash,295 ,md5_file_t(filepath).c_str());
|
||||
thread_local std::string hash_(md5_file_t(filepath));
|
||||
if (strlen(hash_.c_str()) < 290)
|
||||
strcpy_s(hash, 295, hash_.c_str());
|
||||
else{
|
||||
strcpy_s(hash, 295, "");
|
||||
log(LOGLEVEL::ERR_NOSEND, "[scan_file_t()]: Could not calculate hash for file: ", filepath);
|
||||
}
|
||||
sprintf_s(db_path, 295, "%s\\%c%c.jdbf", DB_DIR, hash[0], hash[1]);
|
||||
search_hash(db_path, hash, filepath);
|
||||
num_threads--;
|
||||
|
||||
Reference in New Issue
Block a user