Search code examples
cposix

Remove contents from front of file c


I'm implementing an appending log file for a server process. It writes some number of operations into a log, and then at various points writes a "checkpoint" record.

I currently have a FILE pointer opened in appending mode on the file, for the actual writing of the file.

What I would like to do is, at various points, "dump" the front of the log from the disk record, up to the point of a given checkpoint. I can find the relevant byte offsets I would need to modify.

My question is: is there a graceful way to do this, similar to truncate, but for the front of the file? I can take the option of writing what I want to keep into a temporary file and then swapping them, but if there's a simple operation that doesn't necessarily involve the process of re-scanning and writing into a separate file, I'd love to know.

Thanks for the help!


Solution

  • My question is: is there a graceful way to do this, similar to truncate, but for the front of the file?

    No.

    You'd have to atomically copy the data you want to remain in the file to the front of the file, then truncate it to its new length.

    Simply put, that can't be done atomically, leaving you open to possible corruption or loss of data.

    And that doesn't even get into whether or not the process(es) appending to the file would all be able to gracefully handle that happening to the file while they have it open. Depending on the actual specifics of how the file is opened (see the various oflag values in the POSIX open() documentation), the process(es) writing to the file may or may not be able to handle the operation, even if you could do it atomically.

    Don't reinvent proper logging systems. They already handle things like this.