mirror of
https://github.com/opencontainers/runc.git
synced 2026-07-11 14:13:58 +08:00
c39f87a47a
Using errors.Unwrap() is not the best thing to do, since it returns nil in case of an error which was not wrapped. More to say, errors package provides more elegant ways to check for underlying errors, such as errors.As() and errors.Is(). This reverts commitf8e138855d, reversing changes made to6ca9d8e6da. Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
61 lines
1.3 KiB
Go
61 lines
1.3 KiB
Go
// +build linux
|
|
|
|
package fscommon
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"os"
|
|
"syscall"
|
|
|
|
securejoin "github.com/cyphar/filepath-securejoin"
|
|
"github.com/pkg/errors"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
func WriteFile(dir, file, data string) error {
|
|
if dir == "" {
|
|
return errors.Errorf("no directory specified for %s", file)
|
|
}
|
|
path, err := securejoin.SecureJoin(dir, file)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := retryingWriteFile(path, []byte(data), 0700); err != nil {
|
|
return errors.Wrapf(err, "failed to write %q to %q", data, path)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ReadFile(dir, file string) (string, error) {
|
|
if dir == "" {
|
|
return "", errors.Errorf("no directory specified for %s", file)
|
|
}
|
|
path, err := securejoin.SecureJoin(dir, file)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
data, err := ioutil.ReadFile(path)
|
|
return string(data), err
|
|
}
|
|
|
|
func retryingWriteFile(filename string, data []byte, perm os.FileMode) error {
|
|
for {
|
|
err := ioutil.WriteFile(filename, data, perm)
|
|
if isInterruptedWriteFile(err) {
|
|
logrus.Infof("interrupted while writing %s to %s", string(data), filename)
|
|
continue
|
|
}
|
|
return err
|
|
}
|
|
}
|
|
|
|
func isInterruptedWriteFile(err error) bool {
|
|
if patherr, ok := err.(*os.PathError); ok {
|
|
errno, ok2 := patherr.Err.(syscall.Errno)
|
|
if ok2 && errno == syscall.EINTR {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|