1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

moved GenerateId() to the graph package

This commit is contained in:
Solomon Hykes 2013-03-21 01:07:07 -07:00
parent d7c5d060c4
commit 0208b6accd
4 changed files with 22 additions and 32 deletions

View file

@ -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
// }

View file

@ -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 {

View file

@ -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() {

View file

@ -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