fix(file_store): remove writes on partial failure)
This commit is contained in:
parent
81ba03bb3d
commit
df1f29b7ef
3 changed files with 14 additions and 6 deletions
|
|
@ -9,7 +9,6 @@ import (
|
||||||
_ "modernc.org/sqlite"
|
_ "modernc.org/sqlite"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
server := server.NewServer()
|
server := server.NewServer()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -41,9 +41,7 @@ func (s *Server) Run(r io.Reader, w io.Writer) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
s.handle(w, req)
|
s.handle(w, req)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return scanner.Err()
|
return scanner.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,14 +21,24 @@ func (fs *FileStore) Stage(path string, data []byte) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fs *FileStore) Flush() error {
|
func (fs *FileStore) Flush() error {
|
||||||
|
originalCommittedLen := len(fs.committed)
|
||||||
|
var writtenPaths []string
|
||||||
for _, file := range fs.staged {
|
for _, file := range fs.staged {
|
||||||
|
|
||||||
if fs.fileExists(file.path) {
|
if fs.fileExists(file.path) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := os.WriteFile(file.path, file.data, 0o644); err != nil {
|
if err := os.WriteFile(file.path, file.data, 0o644); err != nil {
|
||||||
|
for _, path := range writtenPaths {
|
||||||
|
_ = os.Remove(path)
|
||||||
|
}
|
||||||
|
fs.committed = fs.committed[:originalCommittedLen]
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.committed = append(fs.committed, file.path)
|
fs.committed = append(fs.committed, file.path)
|
||||||
|
writtenPaths = append(writtenPaths, file.path)
|
||||||
}
|
}
|
||||||
fs.staged = nil
|
fs.staged = nil
|
||||||
return nil
|
return nil
|
||||||
|
|
@ -48,3 +58,4 @@ func (fs *FileStore) fileExists(path string) bool {
|
||||||
_, err := os.Stat(path)
|
_, err := os.Stat(path)
|
||||||
return err == nil
|
return err == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue