Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Atomically write byte[] to file

Tags:

java

file

stream

(This is a hypothetical question since it's very broad, and workarounds exist for specific cases.)

Is it possible to atomically write a byte[] to a file (as FileOutputStream or FileWriter?

If writing fails, then it's unacceptable that part of the array is written. For example, if the array is 1,000,000 bytes and the disk is full after 500,000 bytes, then no bytes should be written to the file, or the changes should somehow be rolled back. This should even be the case if a medium is physically disconnected mid-write.

Assume that the maximum size of the array is known.

like image 917
Mark Jeronimus Avatar asked May 18 '26 18:05

Mark Jeronimus


1 Answers

Atomic writes to files are not possible. Operating systems don't support it, and since they don't, programming language libraries can't do it either.

The best you are going to get with a files in a conventional file system is atomic file renaming; i.e.

  1. write new file into same file system as the old one

  2. use FileDescriptor.sync() to ensure that new file is written

  3. rename the new file over the old one; e.g. using

    java.nio.file.Files.move(Path source, Path target, 
                             CopyOption... options) 
    

    with CopyOptions ATOMIC_MOVE. According to the javadocs, this may not be supported, but if it isn't supported you should get an exception.

But note that the atomicity is implemented in the OS, and if the OS cannot give strong enough guarantees, you are out of luck.

(One issue is what might happen in the event of a hard disk error. If the disk dies completely, then atomicity is moot. But if the OS is still able to read data from the disk after the failure, then the outcome may depend on the OS'es ability to repair a possibly inconsistent file system.)

like image 55
Stephen C Avatar answered May 20 '26 08:05

Stephen C