1

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?

2
  • You probably should not bother that much. Disk I/O is very slow w.r.t. CPU. You don't explain what data are you handling and how is the file organized, and on which operating system & file system. If you don't write sequentially, did you consider sqlite, or gdbm, or databases like mongodb or postgresql ? Commented Nov 8, 2014 at 13:24
  • 1
    BTW, you should not stat the root directory / but the directory containing the file you are writing to (or the written file itself). They probably will be on different filesystems! Commented Nov 8, 2014 at 13:25

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.