- commit
- 0b62aa3
- parent
- 28603ad
- author
- Eric Bower
- date
- 2025-06-29 10:00:57 -0400 EDT
fix(pgs): fs adapter atomically replace files
3 files changed,
+10,
-5
M
go.mod
+1,
-0
1@@ -32,6 +32,7 @@ require (
2 github.com/darkweak/storages/core v0.0.14
3 github.com/gkampitakis/go-snaps v0.5.7
4 github.com/google/go-cmp v0.7.0
5+ github.com/google/renameio/v2 v2.0.0
6 github.com/google/uuid v1.6.0
7 github.com/gorilla/feeds v1.2.0
8 github.com/gorilla/websocket v1.5.3
M
go.sum
+2,
-0
1@@ -404,6 +404,8 @@ github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OI
2 github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
3 github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e h1:ijClszYn+mADRFY17kjQEVQ1XRhq2/JR1M3sGqeJoxs=
4 github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e/go.mod h1:boTsfXsheKC2y+lKOCMpSfarhxDeIzfZG1jqGcPl3cA=
5+github.com/google/renameio/v2 v2.0.0 h1:UifI23ZTGY8Tt29JbYFiuyIU3eX+RNFtUwefq9qAhxg=
6+github.com/google/renameio/v2 v2.0.0/go.mod h1:BtmJXm5YlszgC+TD4HOEEUFgkJP3nLxehU6hfe7jRt4=
7 github.com/google/s2a-go v0.1.9 h1:LGD7gtMgezd8a/Xak7mEWL0PjoTQFvpRudN895yqKW0=
8 github.com/google/s2a-go v0.1.9/go.mod h1:YA0Ei2ZQL3acow2O62kdp9UlnvMmU7kA6Eutn0dXayM=
9 github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
+7,
-5
1@@ -14,6 +14,7 @@ import (
2 "strings"
3 "time"
4
5+ "github.com/google/renameio/v2"
6 "github.com/picosh/pico/pkg/send/utils"
7 "github.com/picosh/pico/pkg/shared/mime"
8 putils "github.com/picosh/utils"
9@@ -152,19 +153,20 @@ func (s *StorageFS) PutObject(bucket Bucket, fpath string, contents io.Reader, e
10 if err != nil {
11 return "", 0, err
12 }
13- f, err := os.OpenFile(loc, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
14+ out, err := renameio.NewPendingFile(loc)
15 if err != nil {
16 return "", 0, err
17 }
18- defer func() {
19- _ = f.Close()
20- }()
21
22- size, err := io.Copy(f, contents)
23+ size, err := io.Copy(out, contents)
24 if err != nil {
25 return "", 0, err
26 }
27
28+ if err := out.CloseAtomicallyReplace(); err != nil {
29+ return "", 0, err
30+ }
31+
32 if entry.Mtime > 0 {
33 uTime := time.Unix(entry.Mtime, 0)
34 _ = os.Chtimes(loc, uTime, uTime)