Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/natefinch/atomic
atomic is a go package for atomic file writing
https://github.com/natefinch/atomic
Last synced: 8 days ago
JSON representation
atomic is a go package for atomic file writing
- Host: GitHub
- URL: https://github.com/natefinch/atomic
- Owner: natefinch
- License: mit
- Created: 2015-05-16T20:51:00.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2022-05-24T09:05:55.000Z (over 2 years ago)
- Last Synced: 2024-10-12T06:44:01.421Z (27 days ago)
- Language: Go
- Size: 10.7 KB
- Stars: 199
- Watchers: 5
- Forks: 14
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# atomic
import "github.com/natefinch/atomic"
atomic is a go package for atomic file writingBy default, writing to a file in go (and generally any language) can fail
partway through... you then have a partially written file, which probably was
truncated when the write began, and bam, now you've lost data.This go package avoids this problem, by writing first to a temp file, and then
overwriting the target file in an atomic way. This is easy on linux, os.Rename
just is atomic. However, on Windows, os.Rename is not atomic, and so bad things
can happen. By wrapping the windows API moveFileEx, we can ensure that the move
is atomic, and we can be safe in knowing that either the move succeeds entirely,
or neither file will be modified.## func ReplaceFile
``` go
func ReplaceFile(source, destination string) error
```
ReplaceFile atomically replaces the destination file or directory with the
source. It is guaranteed to either replace the target file entirely, or not
change either file.## func WriteFile
``` go
func WriteFile(filename string, r io.Reader) (err error)
```
WriteFile atomically writes the contents of r to the specified filepath. If
an error occurs, the target file is guaranteed to be either fully written, or
not written at all. WriteFile overwrites any file that exists at the
location (but only if the write fully succeeds, otherwise the existing file
is unmodified).