c++filesystemsfstreamofstreamapfs

Detecting insufficient space during ofstream write, stream.fail() failed to work


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;
        }
    }
}

Solution

  • After taking @john's advice and use POSIX write and setting my own buffer to 4MB and write to disk size/4MB time, the problem was solved. Here's the code

    int file = open("run1",O_WRONLY);
    
    char zero[4000000] = {0};
    for (long unsigned int i = 0; i < (size/4000000); ++i) { // while space left, write
        write(file,&zero, sizeof(zero)* sizeof(char));
    }
    

    While I'm still not sure about what exactly caused my problem, I can assume with confidence that it has something to do with fstream default buffer size. Maybe the buffer was greater than space remain therefore cannot be written to file, however that does not explain why stream.fail() didn't catch it. Still the problem is fixed.