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
} state;
struct {
struct
{
time_t u32ShredTimeDelta;
std::chrono::time_point<std::chrono::system_clock> chronoShredTimestamp;
unsigned long ulWrittenBytes;
} sShredSpeed;
bool bWasShredded = 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 tfnge_emit(void *dst, size_t szdst, struct tfnge_stream *tfe);
void printRandomBufferChecksum(char* buf, size_t szdst);
};
#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
struct stat st;
char *buf, *s, *d, rc = 0;
int f, rsf;
char *bufRandomData, *s, *d, rc = 0;
int filehandleDrive, filehandleRandom;
int xret = 0, pat = 0, last = 0, special = 0, iIteration = 0;
size_t blksz = 0, x, y;
size_t l, ll = NOSIZE;
@ -79,8 +79,8 @@ void Shred::shredDrive(Drive* drive, int* ipSignalFd)
d32Percent = 0U;
rsf = open(randsrc, O_RDONLY | O_LARGEFILE);
if (rsf == -1)
filehandleRandom = open(randsrc, O_RDONLY | O_LARGEFILE);
if (filehandleRandom == -1)
{
perror(randsrc);
exit(1);
@ -88,7 +88,10 @@ void Shred::shredDrive(Drive* drive, int* ipSignalFd)
special = pat = 0;
iIteration = SHRED_ITERATIONS;
if (verbose) fprintf(stderr, "destroying %s ...\n", cpDrivePath);
if (verbose)
{
fprintf(stderr, "destroying %s ...\n", cpDrivePath);
}
if (stat(cpDrivePath, &st) == -1)
{
@ -96,10 +99,20 @@ void Shred::shredDrive(Drive* drive, int* ipSignalFd)
XRET(1);
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;
if (l == 0 && !S_ISREG(st.st_mode)) special = 1;
else
{
l = ll = st.st_size;
}
if (l == 0 && !S_ISREG(st.st_mode))
{
special = 1;
}
memset(&st, 0, sizeof(struct stat));
if (force) if (chmod(cpDrivePath, S_IRUSR|S_IWUSR) == -1)
@ -107,35 +120,36 @@ void Shred::shredDrive(Drive* drive, int* ipSignalFd)
perror(cpDrivePath);
XRET(1);
}
f = open(cpDrivePath, O_WRONLY | O_LARGEFILE | O_NOCTTY | syncio);
if (f == -1)
filehandleDrive = open(cpDrivePath, O_WRONLY | O_LARGEFILE | O_NOCTTY | syncio);
if (filehandleDrive == -1)
{
XRET(1);
perror(cpDrivePath);
goto _return;
}
buf = (char*) malloc(blksz);
if (!buf)
bufRandomData = (char*) malloc(blksz);
if (!bufRandomData)
{
perror("malloc");
XRET(2);
fprintf(stderr, "Continuing with fixed buffer (%zu bytes long)\n", sizeof(sfbuf));
buf = sfbuf;
bufRandomData = 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);
tfnge_init(&tfnge, buf);
if (read(filehandleRandom, bufRandomData, blksz) <= 0) fprintf(stderr, "%s: read 0 bytes (wanted %zu)\n", randsrc, blksz);
tfnge_init(&tfnge, bufRandomData);
//iteration loop
while (iIteration)
{
lseek(f, 0L, SEEK_SET);
lseek(filehandleDrive, 0L, SEEK_SET);
if (iIteration <= 1 && zrf)
{
Logger::logThis()->info("pat=1");
pat = 1;
rc = 0;
}
@ -145,9 +159,10 @@ void Shred::shredDrive(Drive* drive, int* ipSignalFd)
}
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;
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;
@ -193,11 +208,12 @@ void Shred::shredDrive(Drive* drive, int* ipSignalFd)
if (!pat)
{
tfnge_emit(buf, blksz, &tfnge);
// Logger::logThis()->info("tfnge_emit");
//tfnge_emit(bufRandomData, blksz, &tfnge);
}
else
{
memset(buf, rc, blksz);
memset(bufRandomData, rc, blksz);
}
if (l <= blksz && !special)
@ -205,7 +221,10 @@ void Shred::shredDrive(Drive* drive, int* ipSignalFd)
last = 1;
}
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;
l -= write_return;
if (errno)
@ -223,14 +242,14 @@ void Shred::shredDrive(Drive* drive, int* ipSignalFd)
}
// write block loop end
l = ll;
fdatasync(f);
fdatasync(filehandleDrive);
iIteration--;
} //iteration loop end
if (rmf)
{
close(f);
f = open(cpDrivePath, O_WRONLY | O_TRUNC | O_LARGEFILE | O_NOCTTY | syncio);
close(filehandleDrive);
filehandleDrive = open(cpDrivePath, O_WRONLY | O_TRUNC | O_LARGEFILE | O_NOCTTY | syncio);
if (verbose) fprintf(stderr, "removing %s ...\n", cpDrivePath);
x = strnlen(cpDrivePath, sizeof(sfbuf)/2);
s = sfbuf+(sizeof(sfbuf)/2);
@ -290,12 +309,12 @@ void Shred::shredDrive(Drive* drive, int* ipSignalFd)
}
tfnge_emit(NULL, 0, &tfnge);
if (buf && buf != sfbuf) free(buf);
if (f != -1) close(f);
if (bufRandomData && bufRandomData != sfbuf) free(bufRandomData);
if (filehandleDrive != -1) close(filehandleDrive);
_return:
optind++;
close(rsf);
close(filehandleRandom);
#endif
if(drive->state == Drive::SHRED_ACTIVE)
{
@ -313,6 +332,16 @@ double Shred::calcProgress()
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)
{

View File

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