2014-02-19 00:56:11 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/hex"
|
|
|
|
"io"
|
2014-02-25 05:11:52 +00:00
|
|
|
"path/filepath"
|
2014-02-19 00:56:11 +00:00
|
|
|
)
|
|
|
|
|
2014-02-20 06:43:40 +00:00
|
|
|
// GenerateRandomName returns a new name joined with a prefix. This size
|
|
|
|
// specified is used to truncate the randomly generated value
|
2014-02-19 23:54:53 +00:00
|
|
|
func GenerateRandomName(prefix string, size int) (string, error) {
|
|
|
|
id := make([]byte, 32)
|
2014-02-19 00:56:11 +00:00
|
|
|
if _, err := io.ReadFull(rand.Reader, id); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2014-02-19 23:54:53 +00:00
|
|
|
return prefix + hex.EncodeToString(id)[:size], nil
|
2014-02-19 00:56:11 +00:00
|
|
|
}
|
2014-02-25 05:11:52 +00: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)
|
|
|
|
}
|