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,15 +52,12 @@ bool createDirTree(const string &full_path)
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> names;
if (exists(path))
{
for (auto &entry : directory_iterator(path))
{
if (entry.is_regular_file())
@ -73,6 +70,7 @@ vector<string> lsFilesInDir(const string &path, const string &ext)
}
}
}
}
sort(names.begin(), names.end());
@ -83,6 +81,8 @@ vector<string> lsDirsInDir(const string &path)
{
vector<string> names;
if (exists(path))
{
for (auto &entry : directory_iterator(path))
{
if (entry.is_directory())
@ -90,6 +90,7 @@ vector<string> lsDirsInDir(const string &path)
names.push_back(entry.path().filename().string());
}
}
}
sort(names.begin(), names.end());
@ -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,7 +238,10 @@ 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;
}

View File

@ -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 &param, const string &line, string *value);

View File

@ -23,18 +23,21 @@ void detLog(const string &line, shared_t *share)
}
void enforceMaxLogSize(const string &filePath, shared_t *share)
{
if (exists(filePath))
{
if (file_size(filePath) >= share->maxLogSize)
{
remove(filePath);
}
}
}
void initLogFile(const string &filePath, ofstream &fileObj)
{
if (!fileObj.is_open())
{
if (!fileExists(filePath))
if (!exists(filePath))
{
system(string("touch " + filePath).c_str());
}

View File

@ -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