comments for better understanding

This commit is contained in:
Hendrik Schutter 2022-08-22 09:47:06 +02:00
parent 0d72829f61
commit 8e15a537cf
6 changed files with 1208 additions and 31 deletions

View File

@ -22,12 +22,13 @@ public:
FROZEN FROZEN
} state; } state;
struct { struct
{
time_t u32ShredTimeDelta; 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 ulWrittenBytes;
} sShredSpeed; } sShredSpeed;
bool bWasShredded = false; bool bWasShredded = false;
bool bWasDeleteted = false; bool bWasDeleteted = false;

View File

@ -93,6 +93,8 @@ private:
inline void tfng_encrypt_rawblk(TFNG_UNIT_TYPE *O, const TFNG_UNIT_TYPE *I, const TFNG_UNIT_TYPE *K); inline void tfng_encrypt_rawblk(TFNG_UNIT_TYPE *O, const TFNG_UNIT_TYPE *I, const TFNG_UNIT_TYPE *K);
inline void tfnge_emit(void *dst, size_t szdst, struct tfnge_stream *tfe); inline void tfnge_emit(void *dst, size_t szdst, struct tfnge_stream *tfe);
void printRandomBufferChecksum(char* buf, size_t szdst);
}; };
#endif // SHRED_H_ #endif // SHRED_H_

1144
reHDD_random.log Normal file

File diff suppressed because it is too large Load Diff

BIN
reHDD_speed_test.ods Normal file

Binary file not shown.

View File

@ -60,8 +60,8 @@ void Shred::shredDrive(Drive* drive, int* ipSignalFd)
#ifndef DRYRUN #ifndef DRYRUN
struct stat st; struct stat st;
char *buf, *s, *d, rc = 0; char *bufRandomData, *s, *d, rc = 0;
int f, rsf; int filehandleDrive, filehandleRandom;
int xret = 0, pat = 0, last = 0, special = 0, iIteration = 0; int xret = 0, pat = 0, last = 0, special = 0, iIteration = 0;
size_t blksz = 0, x, y; size_t blksz = 0, x, y;
size_t l, ll = NOSIZE; size_t l, ll = NOSIZE;
@ -79,8 +79,8 @@ void Shred::shredDrive(Drive* drive, int* ipSignalFd)
d32Percent = 0U; d32Percent = 0U;
rsf = open(randsrc, O_RDONLY | O_LARGEFILE); filehandleRandom = open(randsrc, O_RDONLY | O_LARGEFILE);
if (rsf == -1) if (filehandleRandom == -1)
{ {
perror(randsrc); perror(randsrc);
exit(1); exit(1);
@ -88,7 +88,10 @@ void Shred::shredDrive(Drive* drive, int* ipSignalFd)
special = pat = 0; special = pat = 0;
iIteration = SHRED_ITERATIONS; iIteration = SHRED_ITERATIONS;
if (verbose) fprintf(stderr, "destroying %s ...\n", cpDrivePath); if (verbose)
{
fprintf(stderr, "destroying %s ...\n", cpDrivePath);
}
if (stat(cpDrivePath, &st) == -1) if (stat(cpDrivePath, &st) == -1)
{ {
@ -96,10 +99,20 @@ void Shred::shredDrive(Drive* drive, int* ipSignalFd)
XRET(1); XRET(1);
goto _return; goto _return;
} }
if (!blksz) blksz = (size_t)st.st_blksize; if (!blksz)
{
blksz = (size_t)st.st_blksize;
Logger::logThis()->info("Optimal block size for I/O: " + to_string(blksz));
}
else l = ll = st.st_size; else
if (l == 0 && !S_ISREG(st.st_mode)) special = 1; {
l = ll = st.st_size;
}
if (l == 0 && !S_ISREG(st.st_mode))
{
special = 1;
}
memset(&st, 0, sizeof(struct stat)); memset(&st, 0, sizeof(struct stat));
if (force) if (chmod(cpDrivePath, S_IRUSR|S_IWUSR) == -1) if (force) if (chmod(cpDrivePath, S_IRUSR|S_IWUSR) == -1)
@ -107,35 +120,36 @@ void Shred::shredDrive(Drive* drive, int* ipSignalFd)
perror(cpDrivePath); perror(cpDrivePath);
XRET(1); XRET(1);
} }
f = open(cpDrivePath, O_WRONLY | O_LARGEFILE | O_NOCTTY | syncio); filehandleDrive = open(cpDrivePath, O_WRONLY | O_LARGEFILE | O_NOCTTY | syncio);
if (f == -1) if (filehandleDrive == -1)
{ {
XRET(1); XRET(1);
perror(cpDrivePath); perror(cpDrivePath);
goto _return; goto _return;
} }
buf = (char*) malloc(blksz); bufRandomData = (char*) malloc(blksz);
if (!buf) if (!bufRandomData)
{ {
perror("malloc"); perror("malloc");
XRET(2); XRET(2);
fprintf(stderr, "Continuing with fixed buffer (%zu bytes long)\n", sizeof(sfbuf)); fprintf(stderr, "Continuing with fixed buffer (%zu bytes long)\n", sizeof(sfbuf));
buf = sfbuf; bufRandomData = sfbuf;
blksz = sizeof(sfbuf); blksz = sizeof(sfbuf);
} }
memset(buf, 0, blksz); memset(bufRandomData, 0, blksz);
if (read(rsf, buf, blksz) <= 0) fprintf(stderr, "%s: read 0 bytes (wanted %zu)\n", randsrc, blksz); if (read(filehandleRandom, bufRandomData, blksz) <= 0) fprintf(stderr, "%s: read 0 bytes (wanted %zu)\n", randsrc, blksz);
tfnge_init(&tfnge, buf); tfnge_init(&tfnge, bufRandomData);
//iteration loop //iteration loop
while (iIteration) while (iIteration)
{ {
lseek(f, 0L, SEEK_SET); lseek(filehandleDrive, 0L, SEEK_SET);
if (iIteration <= 1 && zrf) if (iIteration <= 1 && zrf)
{ {
Logger::logThis()->info("pat=1");
pat = 1; pat = 1;
rc = 0; rc = 0;
} }
@ -145,9 +159,10 @@ void Shred::shredDrive(Drive* drive, int* ipSignalFd)
} }
else if (!alwaysrand) else if (!alwaysrand)
{ {
if (read(rsf, &rc, 1) <= 0) fprintf(stderr, "%s: read 0 bytes (wanted 1)\n", randsrc); if (read(filehandleRandom, &rc, 1) <= 0) fprintf(stderr, "%s: read 0 bytes (wanted 1)\n", randsrc);
pat = rc%2; pat = rc%2;
if (read(rsf, &rc, 1) <= 0) fprintf(stderr, "%s: read 0 bytes (wanted 1)\n", randsrc); if (read(filehandleRandom, &rc, 1) <= 0) fprintf(stderr, "%s: read 0 bytes (wanted 1)\n", randsrc);
} }
else pat = 0; else pat = 0;
@ -193,11 +208,12 @@ void Shred::shredDrive(Drive* drive, int* ipSignalFd)
if (!pat) if (!pat)
{ {
tfnge_emit(buf, blksz, &tfnge); // Logger::logThis()->info("tfnge_emit");
//tfnge_emit(bufRandomData, blksz, &tfnge);
} }
else else
{ {
memset(buf, rc, blksz); memset(bufRandomData, rc, blksz);
} }
if (l <= blksz && !special) if (l <= blksz && !special)
@ -205,7 +221,10 @@ void Shred::shredDrive(Drive* drive, int* ipSignalFd)
last = 1; last = 1;
} }
errno = 0; errno = 0;
size_t write_return = write(f, buf, (noround && last) ? l : blksz);
printRandomBufferChecksum(bufRandomData, (noround && last) ? l : blksz);
size_t write_return = write(filehandleDrive, bufRandomData, (noround && last) ? l : blksz);
ulSpeedMetricBytesWritten += write_return; ulSpeedMetricBytesWritten += write_return;
l -= write_return; l -= write_return;
if (errno) if (errno)
@ -223,14 +242,14 @@ void Shred::shredDrive(Drive* drive, int* ipSignalFd)
} }
// write block loop end // write block loop end
l = ll; l = ll;
fdatasync(f); fdatasync(filehandleDrive);
iIteration--; iIteration--;
} //iteration loop end } //iteration loop end
if (rmf) if (rmf)
{ {
close(f); close(filehandleDrive);
f = open(cpDrivePath, O_WRONLY | O_TRUNC | O_LARGEFILE | O_NOCTTY | syncio); filehandleDrive = open(cpDrivePath, O_WRONLY | O_TRUNC | O_LARGEFILE | O_NOCTTY | syncio);
if (verbose) fprintf(stderr, "removing %s ...\n", cpDrivePath); if (verbose) fprintf(stderr, "removing %s ...\n", cpDrivePath);
x = strnlen(cpDrivePath, sizeof(sfbuf)/2); x = strnlen(cpDrivePath, sizeof(sfbuf)/2);
s = sfbuf+(sizeof(sfbuf)/2); s = sfbuf+(sizeof(sfbuf)/2);
@ -290,12 +309,12 @@ void Shred::shredDrive(Drive* drive, int* ipSignalFd)
} }
tfnge_emit(NULL, 0, &tfnge); tfnge_emit(NULL, 0, &tfnge);
if (buf && buf != sfbuf) free(buf); if (bufRandomData && bufRandomData != sfbuf) free(bufRandomData);
if (f != -1) close(f); if (filehandleDrive != -1) close(filehandleDrive);
_return: _return:
optind++; optind++;
close(rsf); close(filehandleRandom);
#endif #endif
if(drive->state == Drive::SHRED_ACTIVE) if(drive->state == Drive::SHRED_ACTIVE)
{ {
@ -313,6 +332,16 @@ double Shred::calcProgress()
return ((((double)blockcount/(double)blockcount_max))*100); return ((((double)blockcount/(double)blockcount_max))*100);
} }
void Shred::printRandomBufferChecksum(char* buf, size_t szdst)
{
uint8_t u8Checksum = 0U;
for (size_t i = 0; i < szdst; i++)
{
u8Checksum += buf[i];
}
Logger::logThis()->info("random: " + to_string(u8Checksum));
}
void Shred::tfnge_init_iv(struct tfnge_stream *tfe, const void *key, const void *iv) void Shred::tfnge_init_iv(struct tfnge_stream *tfe, const void *key, const void *iv)
{ {

View File

@ -463,7 +463,8 @@ string TUI::formatTimeDuration(time_t u32Duration)
return out.str(); return out.str();
} }
string TUI::formatSpeed(time_t u32ShredTimeDelta, unsigned long ulWrittenBytes){ string TUI::formatSpeed(time_t u32ShredTimeDelta, unsigned long ulWrittenBytes)
{
std::ostringstream out; std::ostringstream out;
double dDeltaSec = ((double)((u32ShredTimeDelta)/1000000000.0)); //convert nano in sec double dDeltaSec = ((double)((u32ShredTimeDelta)/1000000000.0)); //convert nano in sec
double speed = ((ulWrittenBytes/1000000.0)/dDeltaSec); double speed = ((ulWrittenBytes/1000000.0)/dDeltaSec);