1
0
mirror of https://github.com/coreos/ignition.git synced 2026-02-05 06:47:12 +01:00

renameio: pin v2.0.1 && re-vendor

This commit is contained in:
Steven Presti
2026-01-13 14:52:28 -05:00
parent f5a807a58e
commit 15316f62d0
6 changed files with 27 additions and 132 deletions

6
go.mod
View File

@@ -1,6 +1,8 @@
module github.com/coreos/ignition/v2
go 1.25
go 1.24.0
toolchain go1.24.1
require (
cloud.google.com/go/compute/metadata v0.9.0
@@ -17,7 +19,7 @@ require (
github.com/coreos/go-semver v0.3.1
github.com/coreos/go-systemd/v22 v22.6.0
github.com/coreos/vcontext v0.0.0-20230201181013-d72178a18687
github.com/google/renameio/v2 v2.0.2
github.com/google/renameio/v2 v2.0.1
github.com/google/uuid v1.6.0
github.com/mdlayher/vsock v1.2.1
github.com/mitchellh/copystructure v1.2.0

4
go.sum
View File

@@ -129,8 +129,8 @@ github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/google/martian/v3 v3.3.3 h1:DIhPTQrbPkgs2yJYdXU/eNACCG5DVQjySNRNlflZ9Fc=
github.com/google/martian/v3 v3.3.3/go.mod h1:iEPrYcgCF7jA9OtScMFQyAlZZ4YXTKEtJ1E6RWzmBA0=
github.com/google/renameio/v2 v2.0.2 h1:qKZs+tfn+arruZZhQ7TKC/ergJunuJicWS6gLDt/dGw=
github.com/google/renameio/v2 v2.0.2/go.mod h1:OX+G6WHHpHq3NVj7cAOleLOwJfcQ1s3uUJQCrr78SWo=
github.com/google/renameio/v2 v2.0.1 h1:HyOM6qd9gF9sf15AvhbptGHUnaLTpEI9akAFFU3VyW0=
github.com/google/renameio/v2 v2.0.1/go.mod h1:BtmJXm5YlszgC+TD4HOEEUFgkJP3nLxehU6hfe7jRt4=
github.com/google/s2a-go v0.1.9 h1:LGD7gtMgezd8a/Xak7mEWL0PjoTQFvpRudN895yqKW0=
github.com/google/s2a-go v0.1.9/go.mod h1:YA0Ei2ZQL3acow2O62kdp9UlnvMmU7kA6Eutn0dXayM=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=

View File

@@ -13,6 +13,7 @@
// limitations under the License.
//go:build !windows
// +build !windows
package renameio
@@ -85,14 +86,3 @@ func WithReplaceOnClose() Option {
c.renameOnClose = true
})
}
// WithRoot specifies a root directory to use when working with files.
// See [os.Root] and https://go.dev/blog/osroot for more details.
//
// When WithRoot is used, WithTempDir (and the $TMPDIR environment variable) are
// ignored, as temporary files must be created in the specified root directory.
func WithRoot(root *os.Root) Option {
return optionFunc(func(c *config) {
c.root = root
})
}

View File

@@ -13,11 +13,13 @@
// limitations under the License.
//go:build !windows
// +build !windows
package renameio
import (
"math/rand/v2"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"strconv"
@@ -27,10 +29,10 @@ import (
const defaultPerm os.FileMode = 0o600
// nextrandom is a function generating a random number.
var nextrandom = rand.Int64
var nextrandom = rand.Int63
// openTempFile creates a randomly named file and returns an open handle. It is
// similar to os.CreateTemp except that the directory must be given, the file
// similar to ioutil.TempFile except that the directory must be given, the file
// permissions can be controlled and patterns in the name are not supported.
// The name is always suffixed with a random number.
func openTempFile(dir, name string, perm os.FileMode) (*os.File, error) {
@@ -56,33 +58,6 @@ func openTempFile(dir, name string, perm os.FileMode) (*os.File, error) {
}
}
// openTempFileRoot creates a randomly named file in root and returns an open
// handle. It is similar to os.CreateTemp except that the directory must be
// given, the file permissions can be controlled and patterns in the name are
// not supported. The name is always suffixed with a random number.
func openTempFileRoot(root *os.Root, name string, perm os.FileMode) (string, *os.File, error) {
prefix := name
for attempt := 0; ; {
// Generate a reasonably random name which is unlikely to already
// exist. O_EXCL ensures that existing files generate an error.
name := prefix + strconv.FormatInt(nextrandom(), 10)
f, err := root.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, perm)
if !os.IsExist(err) {
return name, f, err
}
if attempt++; attempt > 10000 {
return "", nil, &os.PathError{
Op: "tempfile",
Path: name,
Err: os.ErrExist,
}
}
}
}
// TempDir checks whether os.TempDir() can be used as a temporary directory for
// later atomically replacing files within dest. If no (os.TempDir() resides on
// a different mount point), dest is returned.
@@ -108,7 +83,7 @@ func tempDir(dir, dest string) string {
// the TMPDIR environment variable.
tmpdir := os.TempDir()
testsrc, err := os.CreateTemp(tmpdir, "."+filepath.Base(dest))
testsrc, err := ioutil.TempFile(tmpdir, "."+filepath.Base(dest))
if err != nil {
return fallback
}
@@ -120,7 +95,7 @@ func tempDir(dir, dest string) string {
}()
testsrc.Close()
testdest, err := os.CreateTemp(filepath.Dir(dest), "."+filepath.Base(dest))
testdest, err := ioutil.TempFile(filepath.Dir(dest), "."+filepath.Base(dest))
if err != nil {
return fallback
}
@@ -143,8 +118,6 @@ type PendingFile struct {
done bool
closed bool
replaceOnClose bool
root *os.Root
tmpname string
}
// Cleanup is a no-op if CloseAtomicallyReplace succeeded, and otherwise closes
@@ -161,14 +134,8 @@ func (t *PendingFile) Cleanup() error {
if !t.closed {
closeErr = t.File.Close()
}
if t.root != nil {
if err := t.root.Remove(t.tmpname); err != nil {
return err
}
} else {
if err := os.Remove(t.Name()); err != nil {
return err
}
if err := os.Remove(t.Name()); err != nil {
return err
}
t.done = true
return closeErr
@@ -196,14 +163,8 @@ func (t *PendingFile) CloseAtomicallyReplace() error {
if err := t.File.Close(); err != nil {
return err
}
if t.root != nil {
if err := t.root.Rename(t.tmpname, t.path); err != nil {
return err
}
} else {
if err := os.Rename(t.Name(), t.path); err != nil {
return err
}
if err := os.Rename(t.Name(), t.path); err != nil {
return err
}
t.done = true
return nil
@@ -239,7 +200,6 @@ type config struct {
ignoreUmask bool
chmod *os.FileMode
renameOnClose bool
root *os.Root
}
// NewPendingFile creates a temporary file destined to atomically creating or
@@ -267,15 +227,8 @@ func NewPendingFile(path string, opts ...Option) (*PendingFile, error) {
}
if cfg.attemptPermCopy {
var existing os.FileInfo
var err error
if cfg.root != nil {
existing, err = cfg.root.Lstat(cfg.path)
} else {
existing, err = os.Lstat(cfg.path)
}
// Try to determine permissions from an existing file.
if err == nil && existing.Mode().IsRegular() {
if existing, err := os.Lstat(cfg.path); err == nil && existing.Mode().IsRegular() {
perm := existing.Mode() & os.ModePerm
cfg.chmod = &perm
@@ -287,14 +240,7 @@ func NewPendingFile(path string, opts ...Option) (*PendingFile, error) {
}
}
var f *os.File
var err error
var tmpname string
if cfg.root != nil {
tmpname, f, err = openTempFileRoot(cfg.root, "."+filepath.Base(cfg.path), cfg.createPerm)
} else {
f, err = openTempFile(tempDir(cfg.dir, cfg.path), "."+filepath.Base(cfg.path), cfg.createPerm)
}
f, err := openTempFile(tempDir(cfg.dir, cfg.path), "."+filepath.Base(cfg.path), cfg.createPerm)
if err != nil {
return nil, err
}
@@ -309,13 +255,7 @@ func NewPendingFile(path string, opts ...Option) (*PendingFile, error) {
}
}
return &PendingFile{
File: f,
path: cfg.path,
replaceOnClose: cfg.renameOnClose,
root: cfg.root,
tmpname: tmpname,
}, nil
return &PendingFile{File: f, path: cfg.path, replaceOnClose: cfg.renameOnClose}, nil
}
// Symlink wraps os.Symlink, replacing an existing symlink with the same name
@@ -327,9 +267,9 @@ func Symlink(oldname, newname string) error {
return err
}
// We need to use os.MkdirTemp, as we cannot overwrite a os.CreateTemp file,
// We need to use ioutil.TempDir, as we cannot overwrite a ioutil.TempFile,
// and removing+symlinking creates a TOCTOU race.
d, err := os.MkdirTemp(filepath.Dir(newname), "."+filepath.Base(newname))
d, err := ioutil.TempDir(filepath.Dir(newname), "."+filepath.Base(newname))
if err != nil {
return err
}
@@ -352,41 +292,3 @@ func Symlink(oldname, newname string) error {
cleanup = false
return os.RemoveAll(d)
}
// SymlinkRoot wraps os.Symlink, replacing an existing symlink with the same
// name atomically (os.Symlink fails when newname already exists, at least on
// Linux).
func SymlinkRoot(root *os.Root, oldname, newname string) error {
// Fast path: if newname does not exist yet, we can skip the whole dance
// below.
if err := root.Symlink(oldname, newname); err == nil || !os.IsExist(err) {
return err
}
// We need to use os.MkdirTemp, as we cannot overwrite a os.CreateTemp file,
// and removing+symlinking creates a TOCTOU race.
//
// There is no os.Root-compatible os.MkdirTemp, so we use the path directly.
d, err := os.MkdirTemp(root.Name(), "."+filepath.Base(newname))
if err != nil {
return err
}
cleanup := true
defer func() {
if cleanup {
os.RemoveAll(d)
}
}()
symlink := filepath.Join(filepath.Base(d), "tmp.symlink")
if err := root.Symlink(oldname, symlink); err != nil {
return err
}
if err := root.Rename(symlink, newname); err != nil {
return err
}
cleanup = false
return os.RemoveAll(d)
}

View File

@@ -13,12 +13,13 @@
// limitations under the License.
//go:build !windows
// +build !windows
package renameio
import "os"
// WriteFile mirrors os.WriteFile, replacing an existing file with the same
// WriteFile mirrors ioutil.WriteFile, replacing an existing file with the same
// name atomically.
func WriteFile(filename string, data []byte, perm os.FileMode, opts ...Option) error {
opts = append([]Option{

4
vendor/modules.txt vendored
View File

@@ -334,8 +334,8 @@ github.com/godbus/dbus/v5
# github.com/golang-jwt/jwt/v5 v5.3.0
## explicit; go 1.21
github.com/golang-jwt/jwt/v5
# github.com/google/renameio/v2 v2.0.2
## explicit; go 1.25
# github.com/google/renameio/v2 v2.0.1
## explicit; go 1.13
github.com/google/renameio/v2
# github.com/google/s2a-go v0.1.9
## explicit; go 1.20