2014-02-19 08:56:11 +08:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/hex"
|
|
|
|
"io"
|
2014-02-25 13:11:52 +08:00
|
|
|
"path/filepath"
|
2014-06-13 21:56:58 +08:00
|
|
|
"syscall"
|
2014-02-19 08:56:11 +08:00
|
|
|
)
|
|
|
|
|
2015-02-07 11:16:11 +08:00
|
|
|
const (
|
|
|
|
exitSignalOffset = 128
|
|
|
|
)
|
|
|
|
|
2014-02-20 14:43:40 +08:00
|
|
|
// GenerateRandomName returns a new name joined with a prefix. This size
|
|
|
|
// specified is used to truncate the randomly generated value
|
2014-02-20 07:54:53 +08:00
|
|
|
func GenerateRandomName(prefix string, size int) (string, error) {
|
|
|
|
id := make([]byte, 32)
|
2014-02-19 08:56:11 +08:00
|
|
|
if _, err := io.ReadFull(rand.Reader, id); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-05-30 11:50:15 +08:00
|
|
|
if size > 64 {
|
|
|
|
size = 64
|
|
|
|
}
|
2014-02-20 07:54:53 +08:00
|
|
|
return prefix + hex.EncodeToString(id)[:size], nil
|
2014-02-19 08:56:11 +08:00
|
|
|
}
|
2014-02-25 13:11:52 +08:00
|
|
|
|
|
|
|
// ResolveRootfs ensures that the current working directory is
|
|
|
|
// not a symlink and returns the absolute path to the rootfs
|
|
|
|
func ResolveRootfs(uncleanRootfs string) (string, error) {
|
|
|
|
rootfs, err := filepath.Abs(uncleanRootfs)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return filepath.EvalSymlinks(rootfs)
|
|
|
|
}
|
2014-06-13 21:56:58 +08:00
|
|
|
|
2015-02-07 11:16:11 +08:00
|
|
|
// ExitStatus returns the correct exit status for a process based on if it
|
2015-10-22 21:38:03 +08:00
|
|
|
// was signaled or exited cleanly.
|
2015-02-07 11:16:11 +08:00
|
|
|
func ExitStatus(status syscall.WaitStatus) int {
|
|
|
|
if status.Signaled() {
|
|
|
|
return exitSignalOffset + int(status.Signal())
|
|
|
|
}
|
|
|
|
return status.ExitStatus()
|
|
|
|
}
|