Skip to content

Commit 7353c02

Browse files
samuelkarpthaJeztah
authored andcommitted
ctr: update WritePidFile to use atomicfile
Signed-off-by: Samuel Karp <[email protected]> (cherry picked from commit ab53652) Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent ae70213 commit 7353c02

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

cmd/ctr/commands/commands.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424
"strings"
2525

2626
"github.com/containerd/containerd/defaults"
27+
"github.com/containerd/containerd/pkg/atomicfile"
28+
2729
"github.com/urfave/cli"
2830
)
2931

@@ -268,15 +270,14 @@ func WritePidFile(path string, pid int) error {
268270
if err != nil {
269271
return err
270272
}
271-
tempPath := filepath.Join(filepath.Dir(path), fmt.Sprintf(".%s", filepath.Base(path)))
272-
f, err := os.OpenFile(tempPath, os.O_RDWR|os.O_CREATE|os.O_EXCL|os.O_SYNC, 0666)
273+
f, err := atomicfile.New(path, 0o666)
273274
if err != nil {
274275
return err
275276
}
276277
_, err = fmt.Fprintf(f, "%d", pid)
277-
f.Close()
278278
if err != nil {
279+
f.Cancel()
279280
return err
280281
}
281-
return os.Rename(tempPath, path)
282+
return f.Close()
282283
}

0 commit comments

Comments
 (0)