| 1 | // CheckEventLog.cpp : Defines the entry point for the DLL application. |
|---|
| 2 | // |
|---|
| 3 | |
|---|
| 4 | #include "stdafx.h" |
|---|
| 5 | #include "CheckDisk.h" |
|---|
| 6 | #include <strEx.h> |
|---|
| 7 | #include <time.h> |
|---|
| 8 | |
|---|
| 9 | CheckDisk gCheckDisk; |
|---|
| 10 | |
|---|
| 11 | BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) |
|---|
| 12 | { |
|---|
| 13 | NSCModuleWrapper::wrapDllMain(hModule, ul_reason_for_call); |
|---|
| 14 | return TRUE; |
|---|
| 15 | } |
|---|
| 16 | |
|---|
| 17 | CheckDisk::CheckDisk() { |
|---|
| 18 | } |
|---|
| 19 | CheckDisk::~CheckDisk() { |
|---|
| 20 | } |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | bool CheckDisk::loadModule() { |
|---|
| 24 | return true; |
|---|
| 25 | } |
|---|
| 26 | bool CheckDisk::unloadModule() { |
|---|
| 27 | return true; |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | std::string CheckDisk::getModuleName() { |
|---|
| 31 | return "CheckDisk Various Disk related checks."; |
|---|
| 32 | } |
|---|
| 33 | NSCModuleWrapper::module_version CheckDisk::getModuleVersion() { |
|---|
| 34 | NSCModuleWrapper::module_version version = {0, 0, 1 }; |
|---|
| 35 | return version; |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | bool CheckDisk::hasCommandHandler() { |
|---|
| 39 | return true; |
|---|
| 40 | } |
|---|
| 41 | bool CheckDisk::hasMessageHandler() { |
|---|
| 42 | return false; |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | typedef std::unary_function<const WIN32_FIND_DATA&, bool> baseClass; |
|---|
| 46 | struct GetSize : public baseClass |
|---|
| 47 | { |
|---|
| 48 | GetSize() : size(0) { } |
|---|
| 49 | result_type operator()(argument_type wfd) { |
|---|
| 50 | if (!(wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)) { |
|---|
| 51 | size += (wfd.nFileSizeHigh * ((long long)MAXDWORD+1)) + (long long)wfd.nFileSizeLow; |
|---|
| 52 | NSC_LOG_ERROR_STD(wfd.cFileName + ": " + strEx::itos((wfd.nFileSizeHigh * ((long long)MAXDWORD+1)) + (long long)wfd.nFileSizeLow) + " --- " + strEx::itos(size)); |
|---|
| 53 | } |
|---|
| 54 | return true; |
|---|
| 55 | } |
|---|
| 56 | inline long long getSize() { |
|---|
| 57 | return size; |
|---|
| 58 | } |
|---|
| 59 | private: |
|---|
| 60 | long long size; |
|---|
| 61 | }; |
|---|
| 62 | |
|---|
| 63 | void RecursiveScanDirectory(std::string dir, GetSize & f) { |
|---|
| 64 | std::string baseDir; |
|---|
| 65 | std::string::size_type pos = dir.find_last_of('\\'); |
|---|
| 66 | if (pos == std::string::npos) |
|---|
| 67 | return; |
|---|
| 68 | baseDir = dir.substr(0, pos); |
|---|
| 69 | |
|---|
| 70 | WIN32_FIND_DATA wfd; |
|---|
| 71 | HANDLE hFind = FindFirstFile(dir.c_str(), &wfd); |
|---|
| 72 | if (hFind != INVALID_HANDLE_VALUE) { |
|---|
| 73 | do { |
|---|
| 74 | if (!f(wfd)) |
|---|
| 75 | break; |
|---|
| 76 | if ((wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY) { |
|---|
| 77 | if ( (strcmp(wfd.cFileName, ".") != 0) && (strcmp(wfd.cFileName, "..") != 0) ) |
|---|
| 78 | RecursiveScanDirectory(baseDir + "\\" + wfd.cFileName + "\\*.*", f); |
|---|
| 79 | } |
|---|
| 80 | } while (FindNextFile(hFind, &wfd)); |
|---|
| 81 | } |
|---|
| 82 | FindClose(hFind); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | // CheckDirectorySize&<Directory>&<Directory>... |
|---|
| 87 | // request: CheckDirectorySize&<warning>&<max>&<Directory>&<Directory>... |
|---|
| 88 | // Return: <return state>&<return string>... |
|---|
| 89 | // <return state> 0 - No errors |
|---|
| 90 | // 1 - Unknown |
|---|
| 91 | // 2 - Errors |
|---|
| 92 | // <return string> <directory> <size mb> ... |
|---|
| 93 | // test: CheckDirectorySize&1024M&4096M&c:\WINDOWS\*.* |
|---|
| 94 | // CheckDirectorySize |
|---|
| 95 | #define BUFFER_SIZE 1024*64 |
|---|
| 96 | |
|---|
| 97 | std::string CheckDisk::handleCommand(const std::string command, const unsigned int argLen, char **char_args) { |
|---|
| 98 | std::string ret; |
|---|
| 99 | if (command == "CheckDirectorySize") { |
|---|
| 100 | std::list<std::string> args = NSCHelper::makelist(argLen, char_args); |
|---|
| 101 | if (args.size() < 3) |
|---|
| 102 | return "Missing argument(s)." + strEx::itos((int)args.size()); |
|---|
| 103 | int iWarn = strEx::stoi64_as_BKMG(args.front()); args.pop_front(); |
|---|
| 104 | int iMax = strEx::stoi64_as_BKMG(args.front()); args.pop_front(); |
|---|
| 105 | |
|---|
| 106 | std::list<std::string>::const_iterator cit; |
|---|
| 107 | for (cit=args.begin();cit!=args.end();++cit) { |
|---|
| 108 | GetSize size; |
|---|
| 109 | RecursiveScanDirectory(*cit, size); |
|---|
| 110 | if (!ret.empty()) |
|---|
| 111 | ret += ", "; |
|---|
| 112 | ret += (*cit) + ": " + strEx::itos_as_BKMG(size.getSize()); |
|---|
| 113 | } |
|---|
| 114 | } |
|---|
| 115 | return ret; |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | NSC_WRAPPERS_MAIN_DEF(gCheckDisk); |
|---|
| 120 | NSC_WRAPPERS_IGNORE_MSG_DEF(); |
|---|
| 121 | NSC_WRAPPERS_HANDLE_CMD_DEF(gCheckDisk); |
|---|