Prechádzať zdrojové kódy

moved GenerateId() to the graph package

Solomon Hykes 12 rokov pred
rodič
commit
0208b6accd
4 zmenil súbory, kde vykonal 22 pridanie a 32 odobranie
  1. 2 1
      commands.go
  2. 2 3
      container.go
  3. 0 25
      future/future.go
  4. 18 3
      graph/image.go

+ 2 - 1
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
 	// }

+ 2 - 3
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 {

+ 0 - 25
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() {

+ 18 - 3
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