Browse code

moved GenerateId() to the graph package

Solomon Hykes authored on 2013/03/21 17:07:07
Showing 4 changed files
... ...
@@ -9,6 +9,7 @@ import (
9 9
 	"github.com/dotcloud/docker/future"
10 10
 	"github.com/dotcloud/docker/rcli"
11 11
 	"io"
12
+	"math/rand"
12 13
 	"net/http"
13 14
 	"net/url"
14 15
 	"path"
... ...
@@ -795,7 +796,7 @@ func (srv *Server) CmdRun(stdin io.ReadCloser, stdout io.Writer, args ...string)
795 795
 }
796 796
 
797 797
 func NewServer() (*Server, error) {
798
-	future.Seed()
798
+	rand.Seed(time.Now().UTC().UnixNano())
799 799
 	// if err != nil {
800 800
 	// 	return nil, err
801 801
 	// }
... ...
@@ -4,7 +4,6 @@ import (
4 4
 	"encoding/json"
5 5
 	"errors"
6 6
 	"fmt"
7
-	"github.com/dotcloud/docker/future"
8 7
 	"github.com/dotcloud/docker/graph"
9 8
 	"github.com/kr/pty"
10 9
 	"io"
... ...
@@ -66,8 +65,8 @@ type NetworkSettings struct {
66 66
 }
67 67
 
68 68
 func GenerateId() string {
69
-	future.Seed()
70
-	return future.RandomId()
69
+	return graph.GenerateId() // Re-use the same code to generate container and image IDs
70
+	// (this might change when image Ids become content-based)
71 71
 }
72 72
 
73 73
 func (container *Container) Cmd() *exec.Cmd {
... ...
@@ -1,35 +1,10 @@
1 1
 package future
2 2
 
3 3
 import (
4
-	"bytes"
5
-	"crypto/sha256"
6 4
 	"fmt"
7 5
 	"io"
8
-	"math/rand"
9
-	"time"
10 6
 )
11 7
 
12
-func Seed() {
13
-	rand.Seed(time.Now().UTC().UnixNano())
14
-}
15
-
16
-func ComputeId(content io.Reader) (string, error) {
17
-	h := sha256.New()
18
-	if _, err := io.Copy(h, content); err != nil {
19
-		return "", err
20
-	}
21
-	return fmt.Sprintf("%x", h.Sum(nil)[:8]), nil
22
-}
23
-
24
-func randomBytes() io.Reader {
25
-	return bytes.NewBuffer([]byte(fmt.Sprintf("%x", rand.Int())))
26
-}
27
-
28
-func RandomId() string {
29
-	id, _ := ComputeId(randomBytes()) // can't fail
30
-	return id
31
-}
32
-
33 8
 func Go(f func() error) chan error {
34 9
 	ch := make(chan error)
35 10
 	go func() {
... ...
@@ -1,10 +1,13 @@
1 1
 package graph
2 2
 
3 3
 import (
4
+	"bytes"
5
+	"crypto/sha256"
4 6
 	"encoding/json"
5 7
 	"fmt"
6
-	"github.com/dotcloud/docker/future"
8
+	"io"
7 9
 	"io/ioutil"
10
+	"math/rand"
8 11
 	"os"
9 12
 	"path"
10 13
 	"strings"
... ...
@@ -157,8 +160,20 @@ func ValidateId(id string) error {
157 157
 }
158 158
 
159 159
 func GenerateId() string {
160
-	future.Seed()
161
-	return future.RandomId()
160
+	// FIXME: don't seed every time
161
+	rand.Seed(time.Now().UTC().UnixNano())
162
+	randomBytes := bytes.NewBuffer([]byte(fmt.Sprintf("%x", rand.Int())))
163
+	id, _ := ComputeId(randomBytes) // can't fail
164
+	return id
165
+}
166
+
167
+// ComputeId reads from `content` until EOF, then returns a SHA of what it read, as a string.
168
+func ComputeId(content io.Reader) (string, error) {
169
+	h := sha256.New()
170
+	if _, err := io.Copy(h, content); err != nil {
171
+		return "", err
172
+	}
173
+	return fmt.Sprintf("%x", h.Sum(nil)[:8]), nil
162 174
 }
163 175
 
164 176
 // Image includes convenience proxy functions to its graph