From 2b0276bd6cacb43aab7e0c52ab5b070d66a0c393 Mon Sep 17 00:00:00 2001 From: Krishna Srinivas Date: Wed, 30 Sep 2015 00:49:16 -0700 Subject: [PATCH] rename newSID to randomID --- session-v2.go | 2 +- session.go | 14 -------------- session_test.go | 2 +- utils.go | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 36 insertions(+), 16 deletions(-) create mode 100644 utils.go diff --git a/session-v2.go b/session-v2.go index 910d5335..76ff25aa 100644 --- a/session-v2.go +++ b/session-v2.go @@ -114,7 +114,7 @@ func newSessionV2() *sessionV2 { s.Header.CommandArgs = nil s.Header.When = time.Now().UTC() s.mutex = new(sync.Mutex) - s.SessionID = newSID(8) + s.SessionID = randomID(8) sessionDataFile, perr := getSessionDataFile(s.SessionID) fatalIf(perr.Trace(s.SessionID), "Unable to create session data file \""+sessionDataFile+"\".") diff --git a/session.go b/session.go index 994e8d94..62fad31a 100644 --- a/session.go +++ b/session.go @@ -17,11 +17,9 @@ package main import ( - "math/rand" "os" "path/filepath" "strings" - "time" "github.com/minio/minio/pkg/probe" ) @@ -63,18 +61,6 @@ func isSessionDirExists() bool { return true } -var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") - -// newSID generates a random session id of regular lower case and uppercase english characters -func newSID(n int) string { - rand.Seed(time.Now().UTC().UnixNano()) - sid := make([]rune, n) - for i := range sid { - sid[i] = letters[rand.Intn(len(letters))] - } - return string(sid) -} - func getSessionFile(sid string) (string, *probe.Error) { sessionDir, err := getSessionDir() if err != nil { diff --git a/session_test.go b/session_test.go index 9aa92b9c..8560375c 100644 --- a/session_test.go +++ b/session_test.go @@ -26,7 +26,7 @@ import ( func (s *TestSuite) TestValidSessionID(c *C) { validSid := regexp.MustCompile("^[a-zA-Z]+$") - sid := newSID(8) + sid := randomID(8) c.Assert(len(sid), Equals, 8) c.Assert(validSid.MatchString(sid), Equals, true) } diff --git a/utils.go b/utils.go new file mode 100644 index 00000000..6ada2fc6 --- /dev/null +++ b/utils.go @@ -0,0 +1,34 @@ +/* + * Minio Client (C) 2015 Minio, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package main + +import ( + "math/rand" + "time" +) + +var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") + +// randomID generates a random id of regular lower case and uppercase english characters +func randomID(n int) string { + rand.Seed(time.Now().UTC().UnixNano()) + sid := make([]rune, n) + for i := range sid { + sid[i] = letters[rand.Intn(len(letters))] + } + return string(sid) +}