mirror of
https://github.com/containerd/containerd.git
synced 2026-08-12 22:16:27 +00:00
ctr: update WritePidFile to use atomicfile
Signed-off-by: Samuel Karp <samuelkarp@google.com>
(cherry picked from commit ab53652228)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
committed by
Sebastiaan van Stijn
parent
ae70213003
commit
7353c02869
@@ -24,6 +24,8 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/containerd/containerd/defaults"
|
||||
"github.com/containerd/containerd/pkg/atomicfile"
|
||||
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
@@ -268,15 +270,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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user