I need to write bytes1 to file1 and bytes2 to file2 and to make sure  I will not catch no space left exception during writings.
Or maybe someone knows how sql databases implement integrity of their files?
I need to write bytes1 to file1 and bytes2 to file2 and to make sure  I will not catch no space left exception during writings.
Or maybe someone knows how sql databases implement integrity of their files?
 
    
    I've found a way to implement transaction but don't know pitfalls of this method. The key element is truncate syscall which will allow us to implement a rollback logic. Here is a pseudocode:
file1Pos = file1.tell()
file2Pos = file2.tell()
err = file1.write(bytes1)
if err != nil {
    // rollback to previous position
    file1.truncate(file1Pos)
    // The file offset is not changed after truncation
    file1.seek(file1Pos, SEEK_SET)
}
err = file2.write(bytes2)
if err != nil {
    file1.truncate(file1Pos)
    file1.seek(file1Pos, SEEK_SET)
    file2.truncate(file2Pos)
    file2.seek(file2Pos, SEEK_SET)
}
 
    
    According to Is file append atomic in UNIX? a single write is atomic on linux if the file is opened with O_DIRECT .  There might be more developments out there if you search for "atomic write".
