fix(file_store): remove writes on partial failure)

This commit is contained in:
Kristian Borgwarth 2026-04-02 19:01:49 +02:00
parent 81ba03bb3d
commit df1f29b7ef
3 changed files with 14 additions and 6 deletions

View file

@ -9,12 +9,11 @@ import (
_ "modernc.org/sqlite"
)
func main() {
server := server.NewServer()
server.Register("initialize", handlers.InitializeHandler{})
server.Register("initialize", handlers.InitializeHandler{})
if err := server.Run(os.Stdin, os.Stdout); err != nil {
slog.Error("server error", "error", err)
}

View file

@ -41,9 +41,7 @@ func (s *Server) Run(r io.Reader, w io.Writer) error {
}
s.handle(w, req)
}
return scanner.Err()
}

View file

@ -3,7 +3,7 @@ package store
import "os"
type FileStore struct {
staged []stagedFile
staged []stagedFile
committed []string
}
@ -21,14 +21,24 @@ func (fs *FileStore) Stage(path string, data []byte) {
}
func (fs *FileStore) Flush() error {
originalCommittedLen := len(fs.committed)
var writtenPaths []string
for _, file := range fs.staged {
if fs.fileExists(file.path) {
continue
}
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
}
fs.committed = append(fs.committed, file.path)
writtenPaths = append(writtenPaths, file.path)
}
fs.staged = nil
return nil
@ -48,3 +58,4 @@ func (fs *FileStore) fileExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}