Skip to content

Commit

Permalink
combine the writes in AtomFile
Browse files Browse the repository at this point in the history
  • Loading branch information
pragmatrix committed Mar 14, 2018
1 parent f19f94c commit f4133f6
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions FunToolbox/AtomFile.fs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
module FunToolbox.AtomFile
/// Atomic file operations that guarantees that file accesses are atomic as
/// observed from different operating processes on the same machine.
///
/// Note that the functions herein try to reduce the corruption probability
/// in case of power losses to a minimum by flushing the file streams to disk
/// after a write, but ultimately can not guarantee it.
module FunToolbox.AtomFile

open System.IO
open System.Text
Expand Down Expand Up @@ -55,13 +61,9 @@ module private Helper =
let setData (fs: FileStream) (data: byte[] option) =
fs |> truncate
if data.IsSome then
try
fs |> write Header.data
fs |> write data.Value
with _ ->
// be sure file is empty when writing fails.
fs |> truncate
reraise()
let combined = Array.append Header.data data.Value
fs |> write combined
// fs.Flush(true)

/// Writes contents to an atomic file. Creates it, if it's not existing.
let write fn data =
Expand Down

0 comments on commit f4133f6

Please sign in to comment.