ctr: update WritePidFile to use atomicfile
Signed-off-by: Samuel Karp <samuelkarp@google.com>
This commit is contained in:
parent
c409c631ca
commit
ab53652228
@ -24,6 +24,8 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/containerd/containerd/defaults"
|
"github.com/containerd/containerd/defaults"
|
||||||
|
"github.com/containerd/containerd/pkg/atomicfile"
|
||||||
|
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -272,15 +274,14 @@ func WritePidFile(path string, pid int) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
tempPath := filepath.Join(filepath.Dir(path), fmt.Sprintf(".%s", filepath.Base(path)))
|
f, err := atomicfile.New(path, 0o666)
|
||||||
f, err := os.OpenFile(tempPath, os.O_RDWR|os.O_CREATE|os.O_EXCL|os.O_SYNC, 0666)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
_, err = fmt.Fprintf(f, "%d", pid)
|
_, err = fmt.Fprintf(f, "%d", pid)
|
||||||
f.Close()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
f.Cancel()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return os.Rename(tempPath, path)
|
return f.Close()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user