Search code examples
cfileblocklarge-data

Handle blockwise writing to a file in C


I'm currently on a project what has to handle writing of large number of bytes to a file. I use the low-level C function write(int, const void *, size_t). To handle lots of bytes at once, I split the data into blocks of the block size that's given by the system:

struct stat b_stat;
stat("/", &b_stat);
blksize_t block_size = b_stat.st_blksize;

The project manages important data, so I have to check for every error. My problem is, how I should proceed when I'm in the middle of the writing process and one block throws an error. The previous data is already written to the file and the low-level C functions does not include something like an "undo"-function.

Do you have any idea on how I should proceed in that sample of case?


Solution

  • If you are "appending" to the file (each new block ends up after the previous one) you can truncate the file after the last correct block in case of error.

    If you overwrite old data with new data, your only option is to use temporary file for writing and if that succeeds you rename the file with old data to some temporary name (to have a backup), rename the file with new data to the "right" name and if all of that succeeds you delete the backup of old data.