The app is hard crashing now but I was able to determine the cause this
time. Must functions in filesystem tend to abort if the filesystem
object doesn't exists. Added protection where needed to prevent crashing
This commit is contained in:
Maurice ONeal 2022-12-13 21:13:10 -05:00
parent 954fdfba0b
commit a5ee164b4e
4 changed files with 37 additions and 23 deletions

View File

@ -52,24 +52,22 @@ bool createDirTree(const string &full_path)
return ret; return ret;
} }
bool fileExists(const string& name)
{
return access(name.c_str(), F_OK) != -1;
}
vector<string> lsFilesInDir(const string &path, const string &ext) vector<string> lsFilesInDir(const string &path, const string &ext)
{ {
vector<string> names; vector<string> 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 (entry.is_regular_file())
if (ext.empty() || name.ends_with(ext))
{ {
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<string> lsDirsInDir(const string &path)
{ {
vector<string> names; vector<string> 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); 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[0]).c_str());
remove(string(cleanDir(dirPath) + "/" + names[1]).c_str()); remove(string(cleanDir(dirPath) + "/" + names[1]).c_str());
@ -233,8 +238,11 @@ bool rdConf(shared_t *share)
if (share->init) if (share->init)
{ {
remove_all(share->buffDir.c_str()); if (exists(share->buffDir))
{
remove_all(share->buffDir);
}
share->init = false; share->init = false;
} }

View File

@ -35,7 +35,7 @@ using namespace cv;
using namespace std; using namespace std;
using namespace std::filesystem; using namespace std::filesystem;
#define APP_VER "1.5.t8" #define APP_VER "1.5.t9"
#define APP_NAME "Motion Watch" #define APP_NAME "Motion Watch"
struct shared_t struct shared_t
@ -76,7 +76,6 @@ string cleanDir(const string &path);
string parseForParam(const string &arg, int argc, char** argv, bool argOnly); string parseForParam(const string &arg, int argc, char** argv, bool argOnly);
bool createDir(const string &dir); bool createDir(const string &dir);
bool createDirTree(const string &full_path); bool createDirTree(const string &full_path);
bool fileExists(const string& name);
void enforceMaxDays(const string &dirPath, shared_t *share); void enforceMaxDays(const string &dirPath, shared_t *share);
void enforceMaxClips(const string &dirPath, shared_t *share); void enforceMaxClips(const string &dirPath, shared_t *share);
void rdLine(const string &param, const string &line, string *value); void rdLine(const string &param, const string &line, string *value);

View File

@ -24,9 +24,12 @@ void detLog(const string &line, shared_t *share)
void enforceMaxLogSize(const string &filePath, 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 (!fileObj.is_open())
{ {
if (!fileExists(filePath)) if (!exists(filePath))
{ {
system(string("touch " + filePath).c_str()); system(string("touch " + filePath).c_str());
} }

View File

@ -45,7 +45,11 @@ void detectLoop(shared_t *share)
else else
{ {
detLog("no motion detected in file: " + fullPath + " removing it.", share); detLog("no motion detected in file: " + fullPath + " removing it.", share);
remove(fullPath.c_str());
if (exists(fullPath))
{
remove(fullPath);
}
} }
} }
else else