https://github.com/dchest/safefile
Go package safefile implements safe "atomic" saving of files.
https://github.com/dchest/safefile
Last synced: 19 days ago
JSON representation
Go package safefile implements safe "atomic" saving of files.
- Host: GitHub
- URL: https://github.com/dchest/safefile
- Owner: dchest
- License: bsd-2-clause
- Created: 2013-11-27T12:08:51.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2023-12-05T15:03:09.000Z (over 1 year ago)
- Last Synced: 2025-05-20T01:11:16.044Z (about 2 months ago)
- Language: Go
- Homepage:
- Size: 208 KB
- Stars: 81
- Watchers: 3
- Forks: 9
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# safefile
[](https://travis-ci.org/dchest/safefile) [](https://ci.appveyor.com/project/dchest/safefile)
Go package safefile implements safe "atomic" saving of files.
Instead of truncating and overwriting the destination file, it creates a
temporary file in the same directory, writes to it, and then renames the
temporary file to the original name when calling Commit.### Installation
```
$ go get github.com/dchest/safefile
```### Documentation
### Example
```go
f, err := safefile.Create("/home/ken/report.txt", 0644)
if err != nil {
// ...
}
// Created temporary file /home/ken/sf-ppcyksu5hyw2mfec.tmpdefer f.Close()
_, err = io.WriteString(f, "Hello world")
if err != nil {
// ...
}
// Wrote "Hello world" to /home/ken/sf-ppcyksu5hyw2mfec.tmperr = f.Commit()
if err != nil {
// ...
}
// Renamed /home/ken/sf-ppcyksu5hyw2mfec.tmp to /home/ken/report.txt
```