• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.gitignoreH A D20-Sep-2015266

LICENSEH A D20-Sep-20151.1 KiB

README.mdH A D20-Sep-20151.4 KiB

atomic.goH A D20-Sep-20151.8 KiB

file_unix.goH A D20-Sep-2015329

file_windows.goH A D20-Sep-2015991

zfile_windows.goH A D20-Sep-2015618

README.md

1# atomic
2    import "github.com/natefinch/atomic"
3atomic is a go package for atomic file writing
4
5By default, writing to a file in go (and generally any language) can fail
6partway through... you then have a partially written file, which probably was
7truncated when the write began, and bam, now you've lost data.
8
9This go package avoids this problem, by writing first to a temp file, and then
10overwriting the target file in an atomic way.  This is easy on linux, os.Rename
11just is atomic.  However, on Windows, os.Rename is not atomic, and so bad things
12can happen.  By wrapping the windows API moveFileEx, we can ensure that the move
13is atomic, and we can be safe in knowing that either the move succeeds entirely,
14or neither file will be modified.
15
16
17## func ReplaceFile
18``` go
19func ReplaceFile(source, destination string) error
20```
21ReplaceFile atomically replaces the destination file or directory with the
22source.  It is guaranteed to either replace the target file entirely, or not
23change either file.
24
25
26## func WriteFile
27``` go
28func WriteFile(filename string, r io.Reader) (err error)
29```
30WriteFile atomically writes the contents of r to the specified filepath.  If
31an error occurs, the target file is guaranteed to be either fully written, or
32not written at all.  WriteFile overwrites any file that exists at the
33location (but only if the write fully succeeds, otherwise the existing file
34is unmodified).
35
36