diff --git a/commands.go b/commands.go index a4c5a4ef43..d56f77aab2 100644 --- a/commands.go +++ b/commands.go @@ -9,6 +9,7 @@ import ( "github.com/dotcloud/docker/future" "github.com/dotcloud/docker/rcli" "io" + "math/rand" "net/http" "net/url" "path" @@ -795,7 +796,7 @@ func (srv *Server) CmdRun(stdin io.ReadCloser, stdout io.Writer, args ...string) } func NewServer() (*Server, error) { - future.Seed() + rand.Seed(time.Now().UTC().UnixNano()) // if err != nil { // return nil, err // } diff --git a/container.go b/container.go index 80109ee1af..04bb1bf0b0 100644 --- a/container.go +++ b/container.go @@ -4,7 +4,6 @@ import ( "encoding/json" "errors" "fmt" - "github.com/dotcloud/docker/future" "github.com/dotcloud/docker/graph" "github.com/kr/pty" "io" @@ -66,8 +65,8 @@ type NetworkSettings struct { } func GenerateId() string { - future.Seed() - return future.RandomId() + return graph.GenerateId() // Re-use the same code to generate container and image IDs + // (this might change when image Ids become content-based) } func (container *Container) Cmd() *exec.Cmd { diff --git a/future/future.go b/future/future.go index dc85115719..c4b30fba41 100644 --- a/future/future.go +++ b/future/future.go @@ -1,35 +1,10 @@ package future import ( - "bytes" - "crypto/sha256" "fmt" "io" - "math/rand" - "time" ) -func Seed() { - rand.Seed(time.Now().UTC().UnixNano()) -} - -func ComputeId(content io.Reader) (string, error) { - h := sha256.New() - if _, err := io.Copy(h, content); err != nil { - return "", err - } - return fmt.Sprintf("%x", h.Sum(nil)[:8]), nil -} - -func randomBytes() io.Reader { - return bytes.NewBuffer([]byte(fmt.Sprintf("%x", rand.Int()))) -} - -func RandomId() string { - id, _ := ComputeId(randomBytes()) // can't fail - return id -} - func Go(f func() error) chan error { ch := make(chan error) go func() { diff --git a/graph/image.go b/graph/image.go index 98bdc8874b..210ba7cedb 100644 --- a/graph/image.go +++ b/graph/image.go @@ -1,10 +1,13 @@ package graph import ( + "bytes" + "crypto/sha256" "encoding/json" "fmt" - "github.com/dotcloud/docker/future" + "io" "io/ioutil" + "math/rand" "os" "path" "strings" @@ -157,8 +160,20 @@ func ValidateId(id string) error { } func GenerateId() string { - future.Seed() - return future.RandomId() + // FIXME: don't seed every time + rand.Seed(time.Now().UTC().UnixNano()) + randomBytes := bytes.NewBuffer([]byte(fmt.Sprintf("%x", rand.Int()))) + id, _ := ComputeId(randomBytes) // can't fail + return id +} + +// ComputeId reads from `content` until EOF, then returns a SHA of what it read, as a string. +func ComputeId(content io.Reader) (string, error) { + h := sha256.New() + if _, err := io.Copy(h, content); err != nil { + return "", err + } + return fmt.Sprintf("%x", h.Sum(nil)[:8]), nil } // Image includes convenience proxy functions to its graph