I am currently writing a implementation of DOD5220.22-M. The piece of code below is for writing binary zero to a file until the disk is completely full.
So the problem is using statvfs, it detects 3800158208 bytes (around 3.82gb) of writable space. However, the write process will stop at 3.77gb, and it will just stuck there forever. The drive is formatted to APFS just before this operation, so it is completely empty.
As you can see, I try to use stream.fail() to detect such error but this does not seems to work at all. System is MacOS running g++8 with C++17
What am I missing? Is the statvfs detecting more writable space than there are or am I doing something wrong?
Also is there a way that I can write without checking stream.fail() every iteration?
Thanks in advance.
{
statvfs("/Volumes/SECUREERASE", &space); // get space
size = space.f_frsize * space.f_bavail;
char zero = 0;
for (int i = 0; i < size; ++i){ // while space left, write
file.write(&zero, sizeof(char));
if(file.fail()){
break;
}
}
}