diff --git a/src/common.cpp b/src/common.cpp index 2a0f82f..71d5f70 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -52,24 +52,22 @@ bool createDirTree(const string &full_path) return ret; } -bool fileExists(const string& name) -{ - return access(name.c_str(), F_OK) != -1; -} - vector lsFilesInDir(const string &path, const string &ext) { vector names; - for (auto &entry : directory_iterator(path)) + if (exists(path)) { - if (entry.is_regular_file()) + for (auto &entry : directory_iterator(path)) { - auto name = entry.path().filename().string(); - - if (ext.empty() || name.ends_with(ext)) + if (entry.is_regular_file()) { - names.push_back(name); + auto name = entry.path().filename().string(); + + if (ext.empty() || name.ends_with(ext)) + { + names.push_back(name); + } } } } @@ -83,11 +81,14 @@ vector lsDirsInDir(const string &path) { vector names; - for (auto &entry : directory_iterator(path)) + if (exists(path)) { - if (entry.is_directory()) + for (auto &entry : directory_iterator(path)) { - names.push_back(entry.path().filename().string()); + if (entry.is_directory()) + { + names.push_back(entry.path().filename().string()); + } } } @@ -112,7 +113,11 @@ void enforceMaxClips(const string &dirPath, shared_t *share) { auto names = lsDirsInDir(dirPath); - while ((names.size() * 3) > ((share->maxClips - 1) * 3)) + // note: this function assumes all video clips are accompanied by + // .html and .jpg files of the same name, hence why it *3 + // the max names in the directory and *3 the file deletion. + + while (names.size() > ((share->maxClips - 1) * 3)) { remove(string(cleanDir(dirPath) + "/" + names[0]).c_str()); remove(string(cleanDir(dirPath) + "/" + names[1]).c_str()); @@ -233,8 +238,11 @@ bool rdConf(shared_t *share) if (share->init) { - remove_all(share->buffDir.c_str()); - + if (exists(share->buffDir)) + { + remove_all(share->buffDir); + } + share->init = false; } diff --git a/src/common.h b/src/common.h index 34b9301..d673bfc 100644 --- a/src/common.h +++ b/src/common.h @@ -35,7 +35,7 @@ using namespace cv; using namespace std; using namespace std::filesystem; -#define APP_VER "1.5.t8" +#define APP_VER "1.5.t9" #define APP_NAME "Motion Watch" struct shared_t @@ -76,7 +76,6 @@ string cleanDir(const string &path); string parseForParam(const string &arg, int argc, char** argv, bool argOnly); bool createDir(const string &dir); bool createDirTree(const string &full_path); -bool fileExists(const string& name); void enforceMaxDays(const string &dirPath, shared_t *share); void enforceMaxClips(const string &dirPath, shared_t *share); void rdLine(const string ¶m, const string &line, string *value); diff --git a/src/logger.cpp b/src/logger.cpp index 279232c..bbecd07 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -24,9 +24,12 @@ void detLog(const string &line, shared_t *share) void enforceMaxLogSize(const string &filePath, shared_t *share) { - if (file_size(filePath) >= share->maxLogSize) + if (exists(filePath)) { - remove(filePath); + if (file_size(filePath) >= share->maxLogSize) + { + remove(filePath); + } } } @@ -34,7 +37,7 @@ void initLogFile(const string &filePath, ofstream &fileObj) { if (!fileObj.is_open()) { - if (!fileExists(filePath)) + if (!exists(filePath)) { system(string("touch " + filePath).c_str()); } diff --git a/src/main.cpp b/src/main.cpp index a0c8b64..4113479 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -45,7 +45,11 @@ void detectLoop(shared_t *share) else { detLog("no motion detected in file: " + fullPath + " removing it.", share); - remove(fullPath.c_str()); + + if (exists(fullPath)) + { + remove(fullPath); + } } } else