Skip to content

DVR-187: implement thread-safe storage manager with background scanning

Hosseini requested to merge DVR-187-Overwrite-Record-is-not-Working into master

This commit introduces a comprehensive storage management system for the DVR firmware that automatically manages disk space by removing oldest files when storage thresholds are exceeded.

  • Automatic disk space monitoring using disk_percent() API

  • File cleanup when usage exceeds SER_USED_PER (85%) threshold

  • Thread-safe operations with proper mutex protection

  • Support for multiple storage manager instances (multiple disks)

  • Real-time file monitoring using inotify for CREATE/MOVED_TO/DELETE/MOVED_FROM events

  • Background directory scanning for existing files

  • Deduplication to prevent duplicate entries from scan + inotify

  • Sorted linked lists maintained by modification time (oldest first)

  • Background scanning prevents blocking system startup

  • Array + qsort approach for O(n log n) scanning vs O(n²) insertion sort

  • No stat() calls during background scan for improved performance

  • Removal tracking during background scan to handle concurrent file deletions

  • Self-deletion tracking to ignore inotify events from storage_manager_remove_oldest

  • Removal tracking during background scan to handle operator file deletions

  • Thread-safe linked list operations with proper mutex protection

  • storage_manager_init(): Initialize with storage base path

  • storage_manager_cleanup(): Cleanup all resources

  • storage_manager_is_scan_completed(): Check background scan status

  • storage_manager_remove_oldest(): Remove oldest file (returns deleted path)

  • storage_manager_get_usage_percent(): Get current disk usage

  • storage_manager_add_file(): Manually add file to tracking

  • file_entry_t: Linked list node with filepath and modification time

  • removed_file_t: Temporary tracking for files removed during background scan

  • storage_manager_t: Main context with separate video/image file lists

  • Monitor thread: Handles inotify events in real-time

  • Background scan thread: Scans existing directories without blocking

  • Mutex protection for all shared data structures

  • Graceful handling of background scan thread creation failures

  • Proper cleanup of all allocated resources

  • Comprehensive error codes for different failure scenarios

  • Handles FAT32 filesystem limitations (linear directory scanning)

  • Robust against operator file manipulations during operation

  • Proper handling of open files (skips deletion if file is open)

  • Memory leak free in all code paths

This implementation provides a production-ready storage management solution that ensures the DVR system never runs out of disk space while maintaining high performance and reliability.

Merge request reports

Loading