Skip to content

Commit d75bf78

Browse files
samuelkarpthaJeztah
authored andcommitted
ctr: update WritePidFile to use atomicfile
Signed-off-by: Samuel Karp <samuelkarp@google.com> (cherry picked from commit ab53652) Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 5f70b23 commit d75bf78

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

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

@@ -259,15 +261,14 @@ func WritePidFile(path string, pid int) error {
259261
if err != nil {
260262
return err
261263
}
262-
tempPath := filepath.Join(filepath.Dir(path), fmt.Sprintf(".%s", filepath.Base(path)))
263-
f, err := os.OpenFile(tempPath, os.O_RDWR|os.O_CREATE|os.O_EXCL|os.O_SYNC, 0666)
264+
f, err := atomicfile.New(path, 0o666)
264265
if err != nil {
265266
return err
266267
}
267268
_, err = fmt.Fprintf(f, "%d", pid)
268-
f.Close()
269269
if err != nil {
270+
f.Cancel()
270271
return err
271272
}
272-
return os.Rename(tempPath, path)
273+
return f.Close()
273274
}

0 commit comments

Comments
 (0)