Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dchest/safefile
Go package safefile implements safe "atomic" saving of files.
https://github.com/dchest/safefile
Last synced: 3 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 (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2023-12-05T15:03:09.000Z (12 months ago)
- Last Synced: 2024-11-09T19:50:35.131Z (11 days ago)
- Language: Go
- Homepage:
- Size: 208 KB
- Stars: 83
- Watchers: 4
- Forks: 9
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# safefile
[![Build Status](https://travis-ci.org/dchest/safefile.svg)](https://travis-ci.org/dchest/safefile) [![Windows Build status](https://ci.appveyor.com/api/projects/status/owlifxeekg75t2ho?svg=true)](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
```