Skip to content

Commit cda170c

Browse files
committed
save: use renameio
I checked in strace that the save handler now results in a rename syscall, and the specific syscall is renameat thanks to the *os.Root API: [pid 596578] close(9</home/michael/tmp/garden/.link.md4944995753028614592>) = 0 [pid 596578] renameat(4</home/michael/tmp/garden>, ".link.md4944995753028614592", 4</home/michael/tmp/garden>, "link.md") = 0
1 parent f551be6 commit cda170c

3 files changed

Lines changed: 9 additions & 8 deletions

File tree

‎go.mod‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/BurntSushi/toml v1.4.1-0.20240526193622-a339e1f7089c
77
github.com/fsnotify/fsnotify v1.8.0
88
github.com/google/go-cmp v0.6.0
9+
github.com/google/renameio/v2 v2.0.2
910
github.com/yuin/goldmark v1.7.8
1011
go.abhg.dev/goldmark/wikilink v0.5.0
1112
golang.org/x/image v0.23.0

‎go.sum‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/
88
github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
99
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
1010
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
11+
github.com/google/renameio/v2 v2.0.2 h1:qKZs+tfn+arruZZhQ7TKC/ergJunuJicWS6gLDt/dGw=
12+
github.com/google/renameio/v2 v2.0.2/go.mod h1:OX+G6WHHpHq3NVj7cAOleLOwJfcQ1s3uUJQCrr78SWo=
1113
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1214
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1315
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

‎internal/bull/save.go‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package bull
33
import (
44
"fmt"
55
"net/http"
6-
"os"
76
"path/filepath"
87
"strings"
8+
9+
"github.com/google/renameio/v2"
910
)
1011

1112
func (b *bullServer) save(w http.ResponseWriter, r *http.Request) error {
@@ -26,9 +27,6 @@ func (b *bullServer) save(w http.ResponseWriter, r *http.Request) error {
2627
pageName := pageFromURL(r)
2728
possibilities := page2files(pageName)
2829

29-
// TODO(go1.25): use https://github.com/google/renameio/ to make writes
30-
// safer once Go 1.25 ships os.Root.Rename.
31-
3230
var firstFn string
3331
for _, fn := range possibilities {
3432
_, err := b.content.Stat(fn)
@@ -45,15 +43,15 @@ func (b *bullServer) save(w http.ResponseWriter, r *http.Request) error {
4543
if err := mkdirAll(b.content, filepath.Dir(firstFn), 0755); err != nil {
4644
return err
4745
}
48-
f, err := b.content.OpenFile(firstFn, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
46+
pf, err := renameio.NewPendingFile(firstFn, renameio.WithRoot(b.content))
4947
if err != nil {
5048
return err
5149
}
52-
defer f.Close()
53-
if _, err := f.Write([]byte(md)); err != nil {
50+
defer pf.Cleanup()
51+
if _, err := pf.Write([]byte(md)); err != nil {
5452
return err
5553
}
56-
if err := f.Close(); err != nil {
54+
if err := pf.CloseAtomicallyReplace(); err != nil {
5755
return err
5856
}
5957

0 commit comments

Comments
 (0)