From 0dd3316c16a1e4f07c74ea92e15b51bfe6d086b6 Mon Sep 17 00:00:00 2001 From: Henry Date: Wed, 15 Oct 2014 11:58:25 +0200 Subject: [PATCH 1/4] rewrote HOME dir lookup (no cgo) --- README.md | 2 -- util/util.go | 10 ++-------- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index dc3d82cb489..194c760f8e8 100644 --- a/README.md +++ b/README.md @@ -22,8 +22,6 @@ NOTES: all dependencies. * Package managers often contain out-of-date `golang` packages. Compilation from source is recommended. -* go-ipfs depends on cgo. In case you've disabled cgo, you'll need to - compile with `CGO_ENABLED=1` * If you are interested in development, please install the development dependencies as well. * **WARNING: older versions of OSX FUSE (for Mac OS X) can cause kernel panics when mounting!** diff --git a/util/util.go b/util/util.go index 0351d86f8bf..36853c44f8d 100644 --- a/util/util.go +++ b/util/util.go @@ -4,7 +4,7 @@ import ( "errors" "io" "math/rand" - "os/user" + "os" "path/filepath" "strings" "time" @@ -35,13 +35,7 @@ var ErrNotFound = ds.ErrNotFound // TildeExpansion expands a filename, which may begin with a tilde. func TildeExpansion(filename string) (string, error) { if strings.HasPrefix(filename, "~/") { - usr, err := user.Current() - if err != nil { - return "", err - } - - dir := usr.HomeDir + "/" - filename = strings.Replace(filename, "~/", dir, 1) + filename = strings.Replace(filename, "~", os.Getenv("HOME"), 1) } return filename, nil } From c9236dd8e1e1859072fdec3b552ebddd5b9661c0 Mon Sep 17 00:00:00 2001 From: Henry Date: Wed, 15 Oct 2014 12:21:16 +0200 Subject: [PATCH 2/4] use mitchellh/go-homedir instead of simple HOME lookup --- util/util.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/util/util.go b/util/util.go index 36853c44f8d..5ebbb958f71 100644 --- a/util/util.go +++ b/util/util.go @@ -4,12 +4,12 @@ import ( "errors" "io" "math/rand" - "os" "path/filepath" "strings" "time" ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go" + "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/mitchellh/go-homedir" ) func init() { @@ -35,7 +35,11 @@ var ErrNotFound = ds.ErrNotFound // TildeExpansion expands a filename, which may begin with a tilde. func TildeExpansion(filename string) (string, error) { if strings.HasPrefix(filename, "~/") { - filename = strings.Replace(filename, "~", os.Getenv("HOME"), 1) + var err error + filename, err = homedir.Expand(filename) + if err != nil { + return "", err + } } return filename, nil } From 320ac339bb2be194cb41d0c6e6ed363c06663138 Mon Sep 17 00:00:00 2001 From: Henry Date: Wed, 15 Oct 2014 12:27:53 +0200 Subject: [PATCH 3/4] checkin github.com/mitchellh/go-homedir" --- Godeps/Godeps.json | 4 + .../github.com/mitchellh/go-homedir/LICENSE | 21 +++++ .../github.com/mitchellh/go-homedir/README.md | 14 ++++ .../mitchellh/go-homedir/homedir.go | 83 +++++++++++++++++++ .../mitchellh/go-homedir/homedir_test.go | 77 +++++++++++++++++ 5 files changed, 199 insertions(+) create mode 100644 Godeps/_workspace/src/github.com/mitchellh/go-homedir/LICENSE create mode 100644 Godeps/_workspace/src/github.com/mitchellh/go-homedir/README.md create mode 100644 Godeps/_workspace/src/github.com/mitchellh/go-homedir/homedir.go create mode 100644 Godeps/_workspace/src/github.com/mitchellh/go-homedir/homedir_test.go diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index 901dd011e38..71690234faf 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -92,6 +92,10 @@ "Comment": "0.1.0-5-g1976046", "Rev": "1976046c2b0db0b668791b3e541d76a38b7c1af7" }, + { + "ImportPath": "github.com/mitchellh/go-homedir", + "Rev": "7d2d8c8a4e078ce3c58736ab521a40b37a504c52" + }, { "ImportPath": "github.com/op/go-logging", "Rev": "3df864a88c7f005e676db4f026a4fe2f14929be3" diff --git a/Godeps/_workspace/src/github.com/mitchellh/go-homedir/LICENSE b/Godeps/_workspace/src/github.com/mitchellh/go-homedir/LICENSE new file mode 100644 index 00000000000..f9c841a51e0 --- /dev/null +++ b/Godeps/_workspace/src/github.com/mitchellh/go-homedir/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2013 Mitchell Hashimoto + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/Godeps/_workspace/src/github.com/mitchellh/go-homedir/README.md b/Godeps/_workspace/src/github.com/mitchellh/go-homedir/README.md new file mode 100644 index 00000000000..d70706d5b35 --- /dev/null +++ b/Godeps/_workspace/src/github.com/mitchellh/go-homedir/README.md @@ -0,0 +1,14 @@ +# go-homedir + +This is a Go library for detecting the user's home directory without +the use of cgo, so the library can be used in cross-compilation environments. + +Usage is incredibly simple, just call `homedir.Dir()` to get the home directory +for a user, and `homedir.Expand()` to expand the `~` in a path to the home +directory. + +**Why not just use `os/user`?** The built-in `os/user` package requires +cgo on Darwin systems. This means that any Go code that uses that package +cannot cross compile. But 99% of the time the use for `os/user` is just to +retrieve the home directory, which we can do for the current user without +cgo. This library does that, enabling cross-compilation. diff --git a/Godeps/_workspace/src/github.com/mitchellh/go-homedir/homedir.go b/Godeps/_workspace/src/github.com/mitchellh/go-homedir/homedir.go new file mode 100644 index 00000000000..b6d35027b74 --- /dev/null +++ b/Godeps/_workspace/src/github.com/mitchellh/go-homedir/homedir.go @@ -0,0 +1,83 @@ +package homedir + +import ( + "bytes" + "errors" + "os" + "os/exec" + "runtime" + "strings" +) + +// Dir returns the home directory for the executing user. +// +// This uses an OS-specific method for discovering the home directory. +// An error is returned if a home directory cannot be detected. +func Dir() (string, error) { + if runtime.GOOS == "windows" { + return dirWindows() + } + + // Unix-like system, so just assume Unix + return dirUnix() +} + +// Expand expands the path to include the home directory if the path +// is prefixed with `~`. If it isn't prefixed with `~`, the path is +// returned as-is. +func Expand(path string) (string, error) { + if len(path) == 0 { + return path, nil + } + + if path[0] != '~' { + return path, nil + } + + if len(path) > 1 && path[1] != '/' && path[1] != '\\' { + return "", errors.New("cannot expand user-specific home dir") + } + + dir, err := Dir() + if err != nil { + return "", err + } + + return dir + path[1:], nil +} + +func dirUnix() (string, error) { + // First prefer the HOME environmental variable + if home := os.Getenv("HOME"); home != "" { + return home, nil + } + + // If that fails, try the shell + var stdout bytes.Buffer + cmd := exec.Command("sh", "-c", "eval echo ~$USER") + cmd.Stdout = &stdout + if err := cmd.Run(); err != nil { + return "", err + } + + result := strings.TrimSpace(stdout.String()) + if result == "" { + return "", errors.New("blank output when reading home directory") + } + + return result, nil +} + +func dirWindows() (string, error) { + drive := os.Getenv("HOMEDRIVE") + path := os.Getenv("HOMEPATH") + home := drive + path + if drive == "" || path == "" { + home = os.Getenv("USERPROFILE") + } + if home == "" { + return "", errors.New("HOMEDRIVE, HOMEPATH, and USERPROFILE are blank") + } + + return home, nil +} diff --git a/Godeps/_workspace/src/github.com/mitchellh/go-homedir/homedir_test.go b/Godeps/_workspace/src/github.com/mitchellh/go-homedir/homedir_test.go new file mode 100644 index 00000000000..89e74c37846 --- /dev/null +++ b/Godeps/_workspace/src/github.com/mitchellh/go-homedir/homedir_test.go @@ -0,0 +1,77 @@ +package homedir + +import ( + "fmt" + "os/user" + "testing" +) + +func TestDir(t *testing.T) { + u, err := user.Current() + if err != nil { + t.Fatalf("err: %s", err) + } + + dir, err := Dir() + if err != nil { + t.Fatalf("err: %s", err) + } + + if u.HomeDir != dir { + t.Fatalf("%#v != %#v", u.HomeDir, dir) + } +} + +func TestExpand(t *testing.T) { + u, err := user.Current() + if err != nil { + t.Fatalf("err: %s", err) + } + + cases := []struct { + Input string + Output string + Err bool + }{ + { + "/foo", + "/foo", + false, + }, + + { + "~/foo", + fmt.Sprintf("%s/foo", u.HomeDir), + false, + }, + + { + "", + "", + false, + }, + + { + "~", + u.HomeDir, + false, + }, + + { + "~foo/foo", + "", + true, + }, + } + + for _, tc := range cases { + actual, err := Expand(tc.Input) + if (err != nil) != tc.Err { + t.Fatalf("Input: %#v\n\nErr: %s", tc.Input, err) + } + + if actual != tc.Output { + t.Fatalf("Input: %#v\n\nOutput: %#v", tc.Input, actual) + } + } +} From 960996d16665522195244ac294abb4a481c83a91 Mon Sep 17 00:00:00 2001 From: Henry Date: Wed, 15 Oct 2014 13:13:43 +0200 Subject: [PATCH 4/4] simplify TildeExpansion() --- util/util.go | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/util/util.go b/util/util.go index 5ebbb958f71..1a24acdb428 100644 --- a/util/util.go +++ b/util/util.go @@ -5,7 +5,6 @@ import ( "io" "math/rand" "path/filepath" - "strings" "time" ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go" @@ -34,14 +33,7 @@ var ErrNotFound = ds.ErrNotFound // TildeExpansion expands a filename, which may begin with a tilde. func TildeExpansion(filename string) (string, error) { - if strings.HasPrefix(filename, "~/") { - var err error - filename, err = homedir.Expand(filename) - if err != nil { - return "", err - } - } - return filename, nil + return homedir.Expand(filename) } // ExpandPathnames takes a set of paths and turns them into absolute paths