From ab536522288071503f60ad4c5256decf0d993f6c Mon Sep 17 00:00:00 2001 From: Samuel Karp Date: Wed, 31 May 2023 23:23:09 -0700 Subject: [PATCH] ctr: update WritePidFile to use atomicfile Signed-off-by: Samuel Karp --- cmd/ctr/commands/commands.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/cmd/ctr/commands/commands.go b/cmd/ctr/commands/commands.go index bfac57986..521e03739 100644 --- a/cmd/ctr/commands/commands.go +++ b/cmd/ctr/commands/commands.go @@ -24,6 +24,8 @@ import ( "strings" "github.com/containerd/containerd/defaults" + "github.com/containerd/containerd/pkg/atomicfile" + "github.com/urfave/cli" ) @@ -272,15 +274,14 @@ func WritePidFile(path string, pid int) error { if err != nil { return err } - tempPath := filepath.Join(filepath.Dir(path), fmt.Sprintf(".%s", filepath.Base(path))) - f, err := os.OpenFile(tempPath, os.O_RDWR|os.O_CREATE|os.O_EXCL|os.O_SYNC, 0666) + f, err := atomicfile.New(path, 0o666) if err != nil { return err } _, err = fmt.Fprintf(f, "%d", pid) - f.Close() if err != nil { + f.Cancel() return err } - return os.Rename(tempPath, path) + return f.Close() }