diff --git a/config.go b/config.go index 59854e8f..9dbb5f5a 100644 --- a/config.go +++ b/config.go @@ -22,6 +22,7 @@ import ( "runtime" "github.com/minio/minio-xl/pkg/probe" + "github.com/minio/minio/pkg/user" ) // mcCustomConfigDir contains the whole path to config dir. Only access via get/set functions. @@ -40,16 +41,16 @@ func getMcConfigDir() (string, *probe.Error) { if mcCustomConfigDir != "" { return mcCustomConfigDir, nil } - u, err := userCurrent() - if err != nil { - return "", err.Trace() + homeDir, e := user.HomeDir() + if e != nil { + return "", probe.NewError(e) } // For windows the path is slightly different switch runtime.GOOS { case "windows": - return filepath.Join(u.HomeDir, globalMCConfigWindowsDir), nil + return filepath.Join(homeDir, globalMCConfigWindowsDir), nil default: - return filepath.Join(u.HomeDir, globalMCConfigDir), nil + return filepath.Join(homeDir, globalMCConfigDir), nil } } diff --git a/utils.go b/utils.go index c8afa881..2d910ed3 100644 --- a/utils.go +++ b/utils.go @@ -18,14 +18,8 @@ package main import ( "math/rand" - "os" - "os/user" "path/filepath" - "runtime" - "strconv" "time" - - "github.com/minio/minio-xl/pkg/probe" ) var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") @@ -46,32 +40,3 @@ func isBucketVirtualStyle(host string) bool { googleVirtual, _ := filepath.Match("*.storage.googleapis.com", host) return s3Virtual || googleVirtual } - -// Workaround for docker images with fully static binary and 32bit linux operating systems. -// For static binaries NSS library will not be a part of the static binary hence user.Current() fails. -// For 32bit linux CGO is not enabled so it will not provide linux specific codebase. -// This code is taken from the minio project: -// (https://github.com/minio/minio/blob/8046b24b975a26d659bda12797a30631bb427e8f/utils.go#L60-L85) -func userCurrent() (*user.User, *probe.Error) { - if os.Getenv("DOCKERIMAGE") == "1" { - wd, err := os.Getwd() - if err != nil { - return nil, probe.NewError(err) - } - return &user.User{Uid: "0", Gid: "0", Username: "root", Name: "root", HomeDir: wd}, nil - } - if runtime.GOARCH == "386" && runtime.GOOS == "linux" { - return &user.User{ - Uid: strconv.Itoa(os.Getuid()), - Gid: strconv.Itoa(os.Getgid()), - Username: os.Getenv("USER"), - Name: os.Getenv("USER"), - HomeDir: os.Getenv("HOME"), - }, nil - } - user, err := user.Current() - if err != nil { - return nil, probe.NewError(err) - } - return user, nil -} diff --git a/vendor/github.com/minio/minio/pkg/user/user.go b/vendor/github.com/minio/minio/pkg/user/user.go new file mode 100644 index 00000000..ee94ef5c --- /dev/null +++ b/vendor/github.com/minio/minio/pkg/user/user.go @@ -0,0 +1,65 @@ +/* + * Minio Cloud Storage, (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 user + +import ( + "os" + "os/user" + "runtime" + "strconv" +) + +// Current is a portable implementation to determine the current user. +// Golang's user.Current does not work reliably under docker or 32bit linux +// +// Two issues this code handles :- +// +// Docker Container - For static binaries NSS library will not be a part of the static binary hence user.Current() fails. +// Linux Intel 32 bit - CGO is not enabled so it will not link with NSS library. +// +func Current() (*user.User, error) { + if os.Getenv("DOCKERIMAGE") == "1" { + wd, err := os.Getwd() + if err != nil { + return nil, err + } + return &user.User{Uid: "0", Gid: "0", Username: "root", Name: "root", HomeDir: wd}, nil + } + if runtime.GOARCH == "386" && runtime.GOOS == "linux" { + return &user.User{ + Uid: strconv.Itoa(os.Getuid()), + Gid: strconv.Itoa(os.Getgid()), + Username: os.Getenv("USER"), + Name: os.Getenv("USER"), + HomeDir: os.Getenv("HOME"), + }, nil + } + user, e := user.Current() + if e != nil { + return nil, e + } + return user, nil +} + +// HomeDir - return current home directory. +func HomeDir() (string, error) { + user, err := Current() + if err != nil { + return "", err + } + return user.HomeDir, nil +} diff --git a/vendor/github.com/minio/minio/pkg/user/user_test.go b/vendor/github.com/minio/minio/pkg/user/user_test.go new file mode 100644 index 00000000..1ab2e0b0 --- /dev/null +++ b/vendor/github.com/minio/minio/pkg/user/user_test.go @@ -0,0 +1,41 @@ +/* + * Minio Cloud Storage, (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 user_test + +import ( + "testing" + + "github.com/minio/minio/pkg/user" + + . "gopkg.in/check.v1" +) + +func Test(t *testing.T) { TestingT(t) } + +type MySuite struct{} + +var _ = Suite(&MySuite{}) + +func (s *MySuite) TestUser(c *C) { + _, err := user.Current() + c.Assert(err, IsNil) +} + +func (s *MySuite) TestHomeDir(c *C) { + _, err := user.HomeDir() + c.Assert(err, IsNil) +} diff --git a/vendor/vendor.json b/vendor/vendor.json index b3379f51..e2fc297d 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -52,6 +52,11 @@ "revision": "bed528d5696872b03dd32d63599f5b9ed82cb361", "revisionTime": "2015-12-03T01:11:14-08:00" }, + { + "path": "github.com/minio/minio/pkg/user", + "revision": "71876688282283e8af374bd8592c7d33c547df4d", + "revisionTime": "2015-12-07T14:59:57-08:00" + }, { "path": "github.com/minio/pb", "revision": "6a97c3b0da073f974e0b8d1ac1bc42c0a7adca46",