Merge pull request 'fix/atomic-drive-members' (#83) from fix/atomic-drive-members into bugfix/ai-static-analysis

Reviewed-on: #83
This commit is contained in:
2025-12-10 21:21:15 +01:00
9 changed files with 185 additions and 67 deletions

View File

@ -14,7 +14,7 @@ class Drive
{
public:
enum TaskState
enum class TaskState
{
NONE,
SHRED_SELECTED,
@ -23,23 +23,28 @@ public:
DELETE_SELECTED,
DELETE_ACTIVE,
FROZEN
} state;
};
enum ConnectionType
enum class ConnectionType
{
UNKNOWN,
USB,
SATA,
NVME
} connectionType;
};
struct
struct ShredSpeed
{
time_t u32ShredTimeDelta;
std::chrono::time_point<std::chrono::system_clock> chronoShredTimestamp;
std::chrono::time_point<std::chrono::system_clock>
chronoShredTimestamp;
unsigned long ulWrittenBytes;
unsigned long ulSpeedMetricBytesWritten;
} sShredSpeed;
};
std::atomic<TaskState> state;
std::atomic<ConnectionType> connectionType;
std::atomic<ShredSpeed> sShredSpeed;
bool bWasShredded = false; // all shred iterations done
bool bWasShredStarted = false; // shred was atleast once started
@ -73,6 +78,18 @@ private:
protected:
public:
// Copy constructor
Drive(const Drive &other);
// Copy assignment operator
Drive &operator=(const Drive &other);
// Move constructor
Drive(Drive &&other) noexcept;
// Move assignment operator
Drive &operator=(Drive &&other) noexcept;
Drive(std::string path)
{
this->sPath = path;

View File

@ -56,6 +56,7 @@
#include <sstream>
#include <iomanip>
#include <signal.h>
#include <atomic>
#include "drive.h"
#include "smart.h"
#include "shred.h"

View File

@ -67,7 +67,7 @@ private:
static void centerTitle(WINDOW *pwin, const char *title);
static WINDOW *createOverViewWindow(int iXSize, int iYSize);
static WINDOW *createDetailViewWindow(int iXSize, int iYSize, int iXStart, Drive drive);
static WINDOW *createDetailViewWindow(int iXSize, int iYSize, int iXStart, Drive &drive);
static WINDOW *overwriteDetailViewWindow(int iXSize, int iYSize, int iXStart);
static WINDOW *createEntryWindow(int iXSize, int iYSize, int iXStart, int iYStart, int iListIndex, std::string sModelFamily, std::string sSerial, std::string sCapacity, std::string sState, std::string sTime, std::string sSpeed, std::string sTemp, std::string sConnection, bool bSelected);
static WINDOW *createSystemStats(int iXSize, int iYSize, int iXStart, int iYStart);
@ -77,7 +77,7 @@ private:
static WINDOW *createSmartWarning(int iXSize, int iYSize, int iXStart, int iYStart, std::string sPath, uint32_t u32PowerOnHours, uint32_t u32PowerCycles, uint32_t u32ErrorCount, uint32_t u32Temperature);
static WINDOW *createZeroChecksumWarning(int iXSize, int iYSize, int iXStart, int iYStart, std::string sPath, std::string sModelFamily, std::string sModelName, std::string sSerial, uint32_t u32Checksum);
void displaySelectedDrive(Drive drive, int stdscrX, int stdscrY);
void displaySelectedDrive(Drive &drive, int stdscrX, int stdscrY);
std::string formatTimeDuration(time_t u32Duration);
std::string formatSpeed(time_t u32ShredTimeDelta, unsigned long ulWrittenBytes);
static void vTruncateText(std::string *psText, uint16_t u16MaxLenght);

View File

@ -18,7 +18,7 @@ DCOMPILE_FLAGS = -D DEBUG
# Add additional include paths
INCLUDES = include
# General linker settings
LINK_FLAGS = -Llib -lpthread -lncurses -ltfng
LINK_FLAGS = -Llib -lpthread -lncurses -ltfng -latomic
# Doc
DOCDIR = doc

View File

@ -8,6 +8,94 @@
#include "../include/reHDD.h"
using namespace std;
// Copy constructor
Drive::Drive(const Drive &other)
: state(other.state.load()),
connectionType(other.connectionType.load()),
sShredSpeed(other.sShredSpeed.load()),
bWasShredded(other.bWasShredded),
bWasShredStarted(other.bWasShredStarted),
bWasChecked(other.bWasChecked),
bWasDeleted(other.bWasDeleted),
bIsOffline(other.bIsOffline),
u32DriveChecksumAfterShredding(other.u32DriveChecksumAfterShredding),
sPath(other.sPath),
u32Timestamp(other.u32Timestamp),
d32TaskPercentage(other.d32TaskPercentage),
u32TimestampTaskStart(other.u32TimestampTaskStart),
u32TaskDuration(other.u32TaskDuration),
sSmartData(other.sSmartData)
{
}
// Copy assignment operator
Drive &Drive::operator=(const Drive &other)
{
if (this != &other)
{
state = other.state.load();
connectionType = other.connectionType.load();
sShredSpeed = other.sShredSpeed.load();
bWasShredded = other.bWasShredded;
bWasShredStarted = other.bWasShredStarted;
bWasChecked = other.bWasChecked;
bWasDeleted = other.bWasDeleted;
bIsOffline = other.bIsOffline;
u32DriveChecksumAfterShredding = other.u32DriveChecksumAfterShredding;
sPath = other.sPath;
u32Timestamp = other.u32Timestamp;
d32TaskPercentage = other.d32TaskPercentage;
u32TimestampTaskStart = other.u32TimestampTaskStart;
u32TaskDuration = other.u32TaskDuration;
sSmartData = other.sSmartData;
}
return *this;
}
// Move constructor
Drive::Drive(Drive &&other) noexcept
: state(other.state.load()),
connectionType(other.connectionType.load()),
sShredSpeed(other.sShredSpeed.load()),
bWasShredded(other.bWasShredded),
bWasShredStarted(other.bWasShredStarted),
bWasChecked(other.bWasChecked),
bWasDeleted(other.bWasDeleted),
bIsOffline(other.bIsOffline),
u32DriveChecksumAfterShredding(other.u32DriveChecksumAfterShredding),
sPath(std::move(other.sPath)),
u32Timestamp(other.u32Timestamp),
d32TaskPercentage(other.d32TaskPercentage),
u32TimestampTaskStart(other.u32TimestampTaskStart),
u32TaskDuration(other.u32TaskDuration),
sSmartData(std::move(other.sSmartData))
{
}
// Move assignment operator
Drive &Drive::operator=(Drive &&other) noexcept
{
if (this != &other)
{
state = other.state.load();
connectionType = other.connectionType.load();
sShredSpeed = other.sShredSpeed.load();
bWasShredded = other.bWasShredded;
bWasShredStarted = other.bWasShredStarted;
bWasChecked = other.bWasChecked;
bWasDeleted = other.bWasDeleted;
bIsOffline = other.bIsOffline;
u32DriveChecksumAfterShredding = other.u32DriveChecksumAfterShredding;
sPath = std::move(other.sPath);
u32Timestamp = other.u32Timestamp;
d32TaskPercentage = other.d32TaskPercentage;
u32TimestampTaskStart = other.u32TimestampTaskStart;
u32TaskDuration = other.u32TaskDuration;
sSmartData = std::move(other.sSmartData);
}
return *this;
}
string Drive::getPath(void)
{
return sPath;
@ -205,6 +293,6 @@ void Drive::checkFrozenDrive(void)
Logger::logThis()->warning("Drive Frozen: " + this->getModelName() + " " + this->getSerial());
this->bWasDeleted = false;
this->bWasShredded = false;
this->state = Drive::FROZEN;
this->state = Drive::TaskState::FROZEN;
}
}

View File

@ -7,7 +7,6 @@
#include "../include/reHDD.h"
bool Printer::instanceFlag = false;
Printer *Printer::single = NULL;
@ -57,16 +56,16 @@ void Printer::print(Drive *drive)
switch (drive->connectionType)
{
case Drive::USB:
case Drive::ConnectionType::USB:
strncpy(msgQueueData.driveData.caDriveConnectionType, "usb", STR_BUFFER_SIZE);
break;
case Drive::SATA:
case Drive::ConnectionType::SATA:
strncpy(msgQueueData.driveData.caDriveConnectionType, "sata", STR_BUFFER_SIZE);
break;
case Drive::NVME:
case Drive::ConnectionType::NVME:
strncpy(msgQueueData.driveData.caDriveConnectionType, "nvme", STR_BUFFER_SIZE);
break;
case Drive::UNKNOWN:
case Drive::ConnectionType::UNKNOWN:
default:
strncpy(msgQueueData.driveData.caDriveConnectionType, "na", STR_BUFFER_SIZE);
}

View File

@ -133,7 +133,7 @@ void reHDD::ThreadCheckFrozenDrives()
mxDrives.lock();
for (auto it = begin(listDrives); it != end(listDrives); ++it)
{
if (it->state == Drive::SHRED_ACTIVE)
if (it->state == Drive::TaskState::SHRED_ACTIVE)
{
it->checkFrozenDrive();
}
@ -175,9 +175,9 @@ void reHDD::ThreadUserInput()
if (tmpSelectedDrive != nullptr)
{
if (tmpSelectedDrive->state == Drive::NONE)
if (tmpSelectedDrive->state == Drive::TaskState::NONE)
{
tmpSelectedDrive->state = Drive::DELETE_SELECTED;
tmpSelectedDrive->state = Drive::TaskState::DELETE_SELECTED;
}
}
@ -187,9 +187,9 @@ void reHDD::ThreadUserInput()
// cout << "Shred" << endl;
if (tmpSelectedDrive != nullptr)
{
if (tmpSelectedDrive->state == Drive::NONE)
if (tmpSelectedDrive->state == Drive::TaskState::NONE)
{
tmpSelectedDrive->state = Drive::SHRED_SELECTED;
tmpSelectedDrive->state = Drive::TaskState::SHRED_SELECTED;
}
}
ui->updateTUI(&listDrives, u16SelectedEntry);
@ -302,7 +302,7 @@ void reHDD::filterNewDrives(list<Drive> *plistOldDrives, list<Drive> *plistNewDr
{
// cout << "offline drive found: " << itOld->getPath() << endl;
Logger::logThis()->warning("Mark offline drive found: " + itOld->getPath());
itOld->state = Drive::NONE; // clear state --> shred task will terminate
itOld->state = Drive::TaskState::NONE; // clear state --> shred task will terminate
}
}
@ -340,27 +340,27 @@ void reHDD::searchDrives(std::list<Drive> *plistDrives)
continue;
Drive tmpDrive("/dev/" + devName);
tmpDrive.state = Drive::NONE;
tmpDrive.state = Drive::TaskState::NONE;
tmpDrive.bIsOffline = false;
// Set connection type
if (transport == "sata")
tmpDrive.connectionType = Drive::SATA;
tmpDrive.connectionType = Drive::ConnectionType::SATA;
else if (transport == "usb")
tmpDrive.connectionType = Drive::USB;
tmpDrive.connectionType = Drive::ConnectionType::USB;
else if (transport == "nvme")
tmpDrive.connectionType = Drive::NVME;
tmpDrive.connectionType = Drive::ConnectionType::NVME;
else
tmpDrive.connectionType = Drive::UNKNOWN;
tmpDrive.connectionType = Drive::ConnectionType::UNKNOWN;
plistDrives->push_back(tmpDrive);
Logger::logThis()->info(
"Drive found: " + tmpDrive.getPath() +
" (type: " +
(tmpDrive.connectionType == Drive::USB ? "USB" : tmpDrive.connectionType == Drive::SATA ? "SATA"
: tmpDrive.connectionType == Drive::NVME ? "NVME"
: "UNKNOWN") +
(tmpDrive.connectionType == Drive::ConnectionType::USB ? "USB" : tmpDrive.connectionType == Drive::ConnectionType::SATA ? "SATA"
: tmpDrive.connectionType == Drive::ConnectionType::NVME ? "NVME"
: "UNKNOWN") +
")");
}
@ -478,7 +478,7 @@ void reHDD::startShredAllDrives(list<Drive> *plistDrives)
mxDrives.lock();
for (it = plistDrives->begin(); it != plistDrives->end(); ++it)
{
if (it->state == Drive::NONE)
if (it->state == Drive::TaskState::NONE)
{
Drive *pTmpDrive = iterator_to_pointer<Drive, std::list<Drive>::iterator>(it);
#ifdef LOG_LEVEL_HIGH
@ -505,9 +505,9 @@ void reHDD::stopShredAllDrives(list<Drive> *plistDrives)
for (it = plistDrives->begin(); it != plistDrives->end(); ++it)
{
if (it->state == Drive::SHRED_ACTIVE || it->state == Drive::DELETE_ACTIVE)
if (it->state == Drive::TaskState::SHRED_ACTIVE || it->state == Drive::TaskState::DELETE_ACTIVE)
{
it->state = Drive::NONE;
it->state = Drive::TaskState::NONE;
Logger::logThis()->info("Abort-Shred-Signal for: " + it->getModelName() + "-" + it->getSerial());
// task for drive is running --> remove selection
}
@ -566,18 +566,20 @@ void reHDD::updateShredMetrics(list<Drive> *plistDrives)
list<Drive>::iterator it;
for (it = plistDrives->begin(); it != plistDrives->end(); ++it)
{
if (it->state == Drive::SHRED_ACTIVE)
if (it->state == Drive::TaskState::SHRED_ACTIVE)
{
Drive *pTmpDrive = iterator_to_pointer<Drive, std::list<Drive>::iterator>(it);
// set metrics for calculating shred speed
std::chrono::time_point<std::chrono::system_clock> chronoCurrentTimestamp = std::chrono::system_clock::now();
time_t u32ShredTimeDelta = (chronoCurrentTimestamp - pTmpDrive->sShredSpeed.chronoShredTimestamp).count();
auto shredSpeed = pTmpDrive->sShredSpeed.load();
time_t u32ShredTimeDelta = (chronoCurrentTimestamp - shredSpeed.chronoShredTimestamp).count();
if (u32ShredTimeDelta > METRIC_THRESHOLD)
{
pTmpDrive->sShredSpeed.u32ShredTimeDelta = u32ShredTimeDelta;
pTmpDrive->sShredSpeed.chronoShredTimestamp = std::chrono::system_clock::now();
pTmpDrive->sShredSpeed.ulWrittenBytes = pTmpDrive->sShredSpeed.ulSpeedMetricBytesWritten;
pTmpDrive->sShredSpeed.ulSpeedMetricBytesWritten = 0U;
shredSpeed.u32ShredTimeDelta = u32ShredTimeDelta;
shredSpeed.chronoShredTimestamp = std::chrono::system_clock::now();
shredSpeed.ulWrittenBytes = shredSpeed.ulSpeedMetricBytesWritten;
shredSpeed.ulSpeedMetricBytesWritten = 0U;
pTmpDrive->sShredSpeed.store(shredSpeed);
}
}
}
@ -675,9 +677,9 @@ void reHDD::handleAbort()
Drive *tmpSelectedDrive = getSelectedDrive();
if (tmpSelectedDrive != nullptr)
{
if (tmpSelectedDrive->state == Drive::SHRED_ACTIVE || tmpSelectedDrive->state == Drive::DELETE_ACTIVE)
if (tmpSelectedDrive->state == Drive::TaskState::SHRED_ACTIVE || tmpSelectedDrive->state == Drive::TaskState::DELETE_ACTIVE)
{
tmpSelectedDrive->state = Drive::NONE;
tmpSelectedDrive->state = Drive::TaskState::NONE;
Logger::logThis()->info("Abort-Shred-Signal for: " + tmpSelectedDrive->getModelName() + "-" + tmpSelectedDrive->getSerial());
// task for drive is running --> remove selection
}

View File

@ -42,7 +42,7 @@ int Shred::shredDrive(Drive *drive, int *ipSignalFd)
#ifdef DRYRUN
for (int i = 0; i <= 500; i++)
{
if (drive->state != Drive::SHRED_ACTIVE)
if (drive->state.load() != Drive::TaskState::SHRED_ACTIVE)
{
return 0;
}
@ -202,9 +202,9 @@ int Shred::shredDrive(Drive *drive, int *ipSignalFd)
cleanup();
if ((drive->state == Drive::SHRED_ACTIVE) || (drive->state == Drive::CHECK_ACTIVE))
if ((drive->state.load() == Drive::TaskState::SHRED_ACTIVE) || (drive->state.load() == Drive::TaskState::CHECK_ACTIVE))
{
drive->state = Drive::NONE;
drive->state = Drive::TaskState::NONE;
drive->setTaskPercentage(0.0);
Printer::getPrinter()->print(drive);
Logger::logThis()->info("Finished shred/check for: " + drive->getModelName() + "-" + drive->getSerial());
@ -286,7 +286,9 @@ unsigned int Shred::uiCalcChecksum(fileDescriptor file, Drive *drive, int *ipSig
ulDriveByteCounter += iReadBytes;
ulDriveByteOverallCount += iReadBytes;
d32Percent = this->calcProgress();
drive->sShredSpeed.ulSpeedMetricBytesWritten += iReadBytes;
auto shredSpeed = drive->sShredSpeed.load();
shredSpeed.ulSpeedMetricBytesWritten += iReadBytes;
drive->sShredSpeed.store(shredSpeed);
#ifdef LOG_LEVEL_HIGH
Logger::logThis()->info("Shred-Task (Checksum): ByteCount: " + to_string(ulDriveByteCounter) + " - progress: " + to_string(d32Percent) + " - Drive: " + drive->getSerial());

View File

@ -99,9 +99,9 @@ void TUI::updateTUI(list<Drive> *plistDrives, uint8_t u8SelectedEntry)
string sSpeed = " ";
string sTime = " ";
string sTemp = it->sTemperatureToText();
string sConnection = (it->connectionType == Drive::USB ? "USB" : it->connectionType == Drive::SATA ? "SATA"
: it->connectionType == Drive::NVME ? "NVME"
: "");
string sConnection = (it->connectionType == Drive::ConnectionType::USB ? "USB" : it->connectionType == Drive::ConnectionType::SATA ? "SATA"
: it->connectionType == Drive::ConnectionType::NVME ? "NVME"
: "");
bool bSelectedEntry = false;
@ -122,30 +122,39 @@ void TUI::updateTUI(list<Drive> *plistDrives, uint8_t u8SelectedEntry)
switch (it->state)
{
case Drive::SHRED_ACTIVE:
case Drive::TaskState::SHRED_ACTIVE:
{
stream << fixed << setprecision(3) << (it->getTaskPercentage());
sState = "Shredding: " + stream.str() + "%";
it->calculateTaskDuration();
sTime = this->formatTimeDuration(it->getTaskDuration());
sSpeed = this->formatSpeed(it->sShredSpeed.u32ShredTimeDelta, it->sShredSpeed.ulWrittenBytes);
auto shredSpeed = it->sShredSpeed.load();
sSpeed = this->formatSpeed(shredSpeed.u32ShredTimeDelta, shredSpeed.ulWrittenBytes);
break;
case Drive::CHECK_ACTIVE:
}
case Drive::TaskState::CHECK_ACTIVE:
{
stream << fixed << setprecision(3) << (it->getTaskPercentage());
sState = "Checking: " + stream.str() + "%";
it->calculateTaskDuration();
sTime = this->formatTimeDuration(it->getTaskDuration());
sSpeed = this->formatSpeed(it->sShredSpeed.u32ShredTimeDelta, it->sShredSpeed.ulWrittenBytes);
auto shredSpeed = it->sShredSpeed.load();
sSpeed = this->formatSpeed(shredSpeed.u32ShredTimeDelta, shredSpeed.ulWrittenBytes);
break;
case Drive::DELETE_ACTIVE:
}
case Drive::TaskState::DELETE_ACTIVE:
{
sState = "Deleting ...";
it->calculateTaskDuration();
sTime = this->formatTimeDuration(it->getTaskDuration());
break;
case Drive::NONE:
case Drive::SHRED_SELECTED:
case Drive::DELETE_SELECTED:
}
case Drive::TaskState::NONE:
case Drive::TaskState::SHRED_SELECTED:
case Drive::TaskState::DELETE_SELECTED:
{
if (it->bWasDeleted)
{
sState = "DELETED"; // mark drive as deleted previously
@ -172,9 +181,9 @@ void TUI::updateTUI(list<Drive> *plistDrives, uint8_t u8SelectedEntry)
wrefresh(dialog);
}
#endif
break;
case Drive::FROZEN:
}
case Drive::TaskState::FROZEN:
stream << fixed << setprecision(3) << (it->getTaskPercentage());
#ifdef FROZEN_ALERT
if (bSelectedEntry)
@ -290,7 +299,7 @@ WINDOW *TUI::createOverViewWindow(int iXSize, int iYSize)
return newWindow;
}
WINDOW *TUI::createDetailViewWindow(int iXSize, int iYSize, int iXStart, Drive drive)
WINDOW *TUI::createDetailViewWindow(int iXSize, int iYSize, int iXStart, Drive &drive)
{
WINDOW *newWindow;
newWindow = newwin(iYSize, iXSize, 1, iXStart);
@ -633,7 +642,7 @@ void TUI::vTruncateText(string *psText, uint16_t u16MaxLenght)
}
}
void TUI::displaySelectedDrive(Drive drive, int stdscrX, int stdscrY)
void TUI::displaySelectedDrive(Drive &drive, int stdscrX, int stdscrY)
{
struct MenuState menustate;
static bool dialogIsActive;
@ -646,27 +655,27 @@ void TUI::displaySelectedDrive(Drive drive, int stdscrX, int stdscrY)
// set menustate based on drive state
switch (drive.state)
{
case Drive::NONE: // no task running or selected for this drive
case Drive::TaskState::NONE: // no task running or selected for this drive
menustate.bShred = true;
menustate.bDelete = true;
break;
case Drive::DELETE_ACTIVE: // delete task running for this drive
case Drive::TaskState::DELETE_ACTIVE: // delete task running for this drive
menustate.bAbort = true;
break;
case Drive::SHRED_ACTIVE: // shred task running for this drive
case Drive::TaskState::SHRED_ACTIVE: // shred task running for this drive
menustate.bAbort = true;
break;
case Drive::CHECK_ACTIVE: // check task running for this drive
case Drive::TaskState::CHECK_ACTIVE: // check task running for this drive
menustate.bAbort = true;
break;
case Drive::DELETE_SELECTED: // delete task selected for this drive
case Drive::TaskState::DELETE_SELECTED: // delete task selected for this drive
menustate.bConfirmDelete = true;
break;
case Drive::SHRED_SELECTED: // shred task selected for this drive
case Drive::TaskState::SHRED_SELECTED: // shred task selected for this drive
menustate.bConfirmShred = true;
break;
default: