Skip to content

Commit

Permalink
Properly analyze and cleanup old motion detect and debug frames
Browse files Browse the repository at this point in the history
  • Loading branch information
kala13x committed May 22, 2023
1 parent 0b0422e commit aefd31f
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 7 deletions.
46 changes: 46 additions & 0 deletions server/bc-cleaner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,50 @@ bool bc_remove_dir_if_empty(const std::string& path)
}

return true;
}

/*
* bc_remove_directory - Recursively remove directory. Argument
* path is the directory path. On success, zero is returned.
* On error, -1 is returned and errno is set appropriately.
*/
int bc_remove_directory(const std::string& path)
{
/* Check if path exists */
struct stat statbuf = {0};
int retval = stat(path.c_str(), &statbuf);
if (retval < 0) return -1;

/* Open directory */
DIR *dir = opendir(path.c_str());
if (dir)
{
struct dirent *entry;
retval = 0;

while (!retval && (entry = readdir(dir)))
{
int xretval = -1;

/* Skip the names "." and ".." as we don't want to recurse on them. */
if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) continue;
std::string new_path = path + "/" + std::string(entry->d_name);

if (!stat(new_path.c_str(), &statbuf))
{
if (!S_ISDIR(statbuf.st_mode)) xretval = unlink(new_path.c_str());
else xretval = bc_remove_directory(new_path.c_str());
}

retval = xretval;
}

/* Close dir pointer */
closedir(dir);
}

if (!retval)
retval = rmdir(path.c_str());

return retval;
}
1 change: 1 addition & 0 deletions server/bc-cleaner.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ std::string bc_get_dir_path(const std::string& path);

bool bc_is_dir_empty(const std::string& path);
bool bc_remove_dir_if_empty(const std::string& path);
int bc_remove_directory(const std::string& path);

#endif /* __BC_CLEANER__ */
76 changes: 69 additions & 7 deletions server/bc-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ static void bc_cleanup_media_retry()
static int bc_media_is_archived(const char *filepath)
{
int archived = 0;
BC_DB_RES dbres = bc_db_get_table("SELECT * FROM Media WHERE archive=1 AND "
BC_DB_RES dbres = bc_db_get_table("SELECT * FROM Media WHERE archive=1 AND "
"filepath='%s'", filepath);

if (!dbres) {
Expand All @@ -735,6 +735,40 @@ static int bc_media_is_archived(const char *filepath)
return archived;
}

static int bc_cleanup_motion_debug_folder(bc_oldest_media_t &ctx, const std::string& full_path, const std::string& time_path)
{
bc_time rec_time;
int id = 0;

int count = sscanf(time_path.c_str(), "%04d/%02d/%02d/%06d/%02d-%02d-%02d",
(int*)&rec_time.year, (int*)&rec_time.month, (int*)&rec_time.day, (int*)&id,
(int*)&rec_time.hour, (int*)&rec_time.min, (int*)&rec_time.sec);

if (count != 7) {
bc_log(Error, "Can not parse date from path: %s", time_path.c_str());
ctx.errors++;
return -1;
}

bc_time old_time;
old_time.year = ctx.year;
old_time.month = ctx.month;
old_time.day = ctx.day;
old_time.hour = ctx.hour;
old_time.min = ctx.min;
old_time.sec = ctx.sec;

/* Check if found entry is older than oldest media record in DB */
bool entry_is_old = bc_compare_time(rec_time, old_time);

if (entry_is_old) {
bc_remove_directory(full_path);
return 1;
}

return 0;
}

/*
Recursively read media directory and delete every untracked file.
This may take a while but will only happen at startup once.
Expand Down Expand Up @@ -773,23 +807,50 @@ static int bc_recursive_cleanup_untracked_media(bc_oldest_media_t &ctx, std::str
continue;
}

std::size_t debug_pos = full_path.find(".debug");
std::size_t motion_pos = full_path.find(".motion");

if (debug_pos != std::string::npos ||
motion_pos != std::string::npos)
{
std::string sub_path = full_path.substr(0, debug_pos);
std::string time_path = sub_path.substr(sub_path.size() - 26, std::string::npos);

if (S_ISDIR(statbuf.st_mode))
bc_cleanup_motion_debug_folder(ctx, full_path, time_path);

entry = readdir(pdir);
continue;
}

if (S_ISDIR(statbuf.st_mode)) { // Recursive read directory
bc_recursive_cleanup_untracked_media(ctx, full_path);
bc_remove_dir_if_empty(full_path);
entry = readdir(pdir);
continue;
}

if (entry_name.compare(entry_name.size()-3, 3, "mkv") &&
entry_name.compare(entry_name.size()-3, 3, "mp4") &&
entry_name.compare(entry_name.size()-3, 3, "jpg")) {
if (entry_name.compare(entry_name.size()-4, 4, ".mkv") &&
entry_name.compare(entry_name.size()-4, 4, ".mp4") &&
entry_name.compare(entry_name.size()-4, 4, ".jpg")) {
/* Not a bc media file, leave it unchanged */
entry = readdir(pdir);
ctx.others++;
continue;
}

std::string timed_path = full_path.substr(full_path.size() - 30, 26);
size_t time_len = 26;
size_t suffix_len = 4;

if (!entry_name.compare(entry_name.size()-11, 11, "-motion.jpg") ||
!entry_name.compare(entry_name.size()-11, 11, "-motion.mp4") ||
!entry_name.compare(entry_name.size()-11, 11, "-motion.mkv"))
{
/* Not a bc media file, leave it unchanged */
suffix_len = 11;
}

std::string timed_path = full_path.substr(full_path.size() - (time_len + suffix_len), time_len);
bc_time rec_time;
int id = 0;

Expand All @@ -798,7 +859,9 @@ static int bc_recursive_cleanup_untracked_media(bc_oldest_media_t &ctx, std::str
(int*)&rec_time.hour, (int*)&rec_time.min, (int*)&rec_time.sec);

if (count != 7) {
bc_log(Error, "Can not parse date from media file: %s", timed_path.c_str());
bc_log(Error, "Can not parse date from media file: %s (%s)",
full_path.c_str(), timed_path.c_str());

entry = readdir(pdir);
ctx.errors++;
continue;
Expand All @@ -814,7 +877,6 @@ static int bc_recursive_cleanup_untracked_media(bc_oldest_media_t &ctx, std::str

/* Check if found entry is older than oldest media record in DB */
bool entry_is_old = bc_compare_time(rec_time, old_time);
bc_log(Info, "Recording is old: %s", entry_is_old?"true":"false"); // kala >> temporary

if (entry_is_old) {
std::string video_file = full_path;
Expand Down

0 comments on commit aefd31f

Please sign in to comment.