Skip to content

Commit c409c63

Browse files
committed
shim: WritePidFile & WriteAddress use atomicfile
Signed-off-by: Samuel Karp <[email protected]>
1 parent 3c4a1ab commit c409c63

1 file changed

Lines changed: 12 additions & 12 deletions

File tree

runtime/v2/shim/util.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@ import (
2828
"strings"
2929
"time"
3030

31+
"github.com/containerd/ttrpc"
32+
"github.com/containerd/typeurl/v2"
33+
exec "golang.org/x/sys/execabs"
34+
3135
"github.com/containerd/containerd/errdefs"
3236
"github.com/containerd/containerd/namespaces"
37+
"github.com/containerd/containerd/pkg/atomicfile"
3338
"github.com/containerd/containerd/protobuf/proto"
3439
"github.com/containerd/containerd/protobuf/types"
35-
"github.com/containerd/ttrpc"
36-
"github.com/containerd/typeurl/v2"
37-
exec "golang.org/x/sys/execabs"
3840
)
3941

4042
type CommandConfig struct {
@@ -124,17 +126,16 @@ func WritePidFile(path string, pid int) error {
124126
if err != nil {
125127
return err
126128
}
127-
tempPath := filepath.Join(filepath.Dir(path), fmt.Sprintf(".%s", filepath.Base(path)))
128-
f, err := os.OpenFile(tempPath, os.O_RDWR|os.O_CREATE|os.O_EXCL|os.O_SYNC, 0666)
129+
f, err := atomicfile.New(path, 0o666)
129130
if err != nil {
130131
return err
131132
}
132133
_, err = fmt.Fprintf(f, "%d", pid)
133-
f.Close()
134134
if err != nil {
135+
f.Cancel()
135136
return err
136137
}
137-
return os.Rename(tempPath, path)
138+
return f.Close()
138139
}
139140

140141
// WriteAddress writes a address file atomically
@@ -143,17 +144,16 @@ func WriteAddress(path, address string) error {
143144
if err != nil {
144145
return err
145146
}
146-
tempPath := filepath.Join(filepath.Dir(path), fmt.Sprintf(".%s", filepath.Base(path)))
147-
f, err := os.OpenFile(tempPath, os.O_RDWR|os.O_CREATE|os.O_EXCL|os.O_SYNC, 0666)
147+
f, err := atomicfile.New(path, 0o666)
148148
if err != nil {
149149
return err
150150
}
151-
_, err = f.WriteString(address)
152-
f.Close()
151+
_, err = f.Write([]byte(address))
153152
if err != nil {
153+
f.Cancel()
154154
return err
155155
}
156-
return os.Rename(tempPath, path)
156+
return f.Close()
157157
}
158158

159159
// ErrNoAddress is returned when the address file has no content

0 commit comments

Comments
 (0)