2011-04-02 05:26:33 +00:00
|
|
|
/*
|
|
|
|
Copyright 2011 Google 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.
|
|
|
|
*/
|
|
|
|
|
2013-07-08 04:12:18 +00:00
|
|
|
// Package osutil provides operating system-specific path information,
|
|
|
|
// and other utility functions.
|
2011-04-02 05:26:33 +00:00
|
|
|
package osutil
|
|
|
|
|
|
|
|
import (
|
2013-07-31 22:06:13 +00:00
|
|
|
"log"
|
2011-04-02 05:26:33 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2011-06-07 20:25:44 +00:00
|
|
|
"runtime"
|
2011-06-24 20:02:51 +00:00
|
|
|
"strings"
|
2012-04-22 23:19:21 +00:00
|
|
|
"sync"
|
2011-04-02 05:26:33 +00:00
|
|
|
)
|
|
|
|
|
2013-07-08 04:12:18 +00:00
|
|
|
// HomeDir returns the path to the user's home directory.
|
|
|
|
// It returns the empty string if the value isn't known.
|
2011-04-02 05:26:33 +00:00
|
|
|
func HomeDir() string {
|
2011-06-07 20:25:44 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
return os.Getenv("HOMEPATH")
|
|
|
|
}
|
2011-04-02 05:26:33 +00:00
|
|
|
return os.Getenv("HOME")
|
|
|
|
}
|
|
|
|
|
2013-08-28 13:53:58 +00:00
|
|
|
func Username() string {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
return os.Getenv("USERNAME")
|
|
|
|
}
|
|
|
|
return os.Getenv("USER")
|
|
|
|
}
|
|
|
|
|
2012-04-22 23:19:21 +00:00
|
|
|
var cacheDirOnce sync.Once
|
|
|
|
|
2011-09-17 00:56:49 +00:00
|
|
|
func CacheDir() string {
|
2012-04-22 23:19:21 +00:00
|
|
|
cacheDirOnce.Do(makeCacheDir)
|
|
|
|
return cacheDir()
|
|
|
|
}
|
|
|
|
|
|
|
|
func cacheDir() string {
|
2013-01-27 21:24:50 +00:00
|
|
|
if d := os.Getenv("CAMLI_CACHE_DIR"); d != "" {
|
|
|
|
return d
|
|
|
|
}
|
2012-04-18 14:44:08 +00:00
|
|
|
switch runtime.GOOS {
|
|
|
|
case "darwin":
|
|
|
|
return filepath.Join(HomeDir(), "Library", "Caches", "Camlistore")
|
|
|
|
case "windows":
|
2013-08-04 21:21:36 +00:00
|
|
|
// Per http://technet.microsoft.com/en-us/library/cc749104(v=ws.10).aspx
|
|
|
|
// these should both exist. But that page overwhelms me. Just try them
|
|
|
|
// both. This seems to work.
|
|
|
|
for _, ev := range []string{"TEMP", "TMP"} {
|
|
|
|
if v := os.Getenv(ev); v != "" {
|
|
|
|
return filepath.Join(v, "Camlistore")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
panic("No Windows TEMP or TMP environment variables found; please file a bug report.")
|
2011-09-17 00:56:49 +00:00
|
|
|
}
|
2012-04-22 23:19:21 +00:00
|
|
|
return filepath.Join(HomeDir(), ".cache", "camlistore")
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeCacheDir() {
|
2013-07-31 22:06:13 +00:00
|
|
|
err := os.MkdirAll(cacheDir(), 0700)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Could not create cacheDir %v: %v", cacheDir(), err)
|
|
|
|
}
|
2011-09-17 00:56:49 +00:00
|
|
|
}
|
|
|
|
|
2013-01-11 00:16:10 +00:00
|
|
|
func CamliVarDir() string {
|
2012-03-19 20:07:46 +00:00
|
|
|
switch runtime.GOOS {
|
|
|
|
case "windows":
|
2013-01-11 00:16:10 +00:00
|
|
|
return filepath.Join(os.Getenv("APPDATA"), "Camlistore")
|
2012-03-19 20:07:46 +00:00
|
|
|
case "darwin":
|
2013-01-11 00:16:10 +00:00
|
|
|
return filepath.Join(HomeDir(), "Library", "Camlistore")
|
2012-03-19 20:07:46 +00:00
|
|
|
}
|
2013-01-11 00:16:10 +00:00
|
|
|
return filepath.Join(HomeDir(), "var", "camlistore")
|
|
|
|
}
|
|
|
|
|
|
|
|
func CamliBlobRoot() string {
|
|
|
|
return filepath.Join(CamliVarDir(), "blobs")
|
2012-03-19 20:07:46 +00:00
|
|
|
}
|
|
|
|
|
2011-04-02 05:26:33 +00:00
|
|
|
func CamliConfigDir() string {
|
2011-05-16 04:55:46 +00:00
|
|
|
if p := os.Getenv("CAMLI_CONFIG_DIR"); p != "" {
|
|
|
|
return p
|
|
|
|
}
|
2011-06-07 20:25:44 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
2012-03-19 20:07:46 +00:00
|
|
|
return filepath.Join(os.Getenv("APPDATA"), "Camlistore")
|
2011-06-07 20:25:44 +00:00
|
|
|
}
|
2013-06-21 23:30:20 +00:00
|
|
|
if xdg := os.Getenv("XDG_CONFIG_HOME"); xdg != "" {
|
|
|
|
return filepath.Join(xdg, "camlistore")
|
|
|
|
}
|
|
|
|
return filepath.Join(HomeDir(), ".config", "camlistore")
|
2011-04-02 05:26:33 +00:00
|
|
|
}
|
|
|
|
|
2011-04-03 15:07:40 +00:00
|
|
|
func UserServerConfigPath() string {
|
2012-03-03 22:27:17 +00:00
|
|
|
return filepath.Join(CamliConfigDir(), "server-config.json")
|
2011-04-03 15:07:40 +00:00
|
|
|
}
|
|
|
|
|
2013-06-21 14:10:40 +00:00
|
|
|
func UserClientConfigPath() string {
|
2013-06-21 23:30:20 +00:00
|
|
|
return filepath.Join(CamliConfigDir(), "client-config.json")
|
2013-06-21 14:10:40 +00:00
|
|
|
}
|
|
|
|
|
2012-04-12 23:54:57 +00:00
|
|
|
func IdentitySecretRing() string {
|
|
|
|
return filepath.Join(CamliConfigDir(), "identity-secring.gpg")
|
|
|
|
}
|
|
|
|
|
2013-08-23 19:30:01 +00:00
|
|
|
// KeyBlobsDir returns the path to the directory containing
|
|
|
|
// the blob(s) for the public gpg key(s)
|
|
|
|
func KeyBlobsDir() string {
|
|
|
|
return filepath.Join(CamliConfigDir(), "keyblobs")
|
|
|
|
}
|
|
|
|
|
2013-06-21 14:10:40 +00:00
|
|
|
// Find the correct absolute path corresponding to a relative path,
|
2011-06-24 20:02:51 +00:00
|
|
|
// searching the following sequence of directories:
|
|
|
|
// 1. Working Directory
|
|
|
|
// 2. CAMLI_CONFIG_DIR (deprecated, will complain if this is on env)
|
|
|
|
// 3. (windows only) APPDATA/camli
|
|
|
|
// 4. All directories in CAMLI_INCLUDE_PATH (standard PATH form for OS)
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
func FindCamliInclude(configFile string) (absPath string, err error) {
|
2011-06-24 20:02:51 +00:00
|
|
|
// Try to open as absolute / relative to CWD
|
|
|
|
_, err = os.Stat(configFile)
|
|
|
|
if err == nil {
|
2011-07-02 16:09:50 +00:00
|
|
|
return configFile, nil
|
2011-06-24 20:02:51 +00:00
|
|
|
}
|
|
|
|
if filepath.IsAbs(configFile) {
|
|
|
|
// End of the line for absolute path
|
2011-07-02 16:09:50 +00:00
|
|
|
return "", err
|
2011-06-24 20:02:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try the config dir
|
2011-07-02 16:09:50 +00:00
|
|
|
configDir := CamliConfigDir()
|
2011-06-24 20:02:51 +00:00
|
|
|
if _, err = os.Stat(filepath.Join(configDir, configFile)); err == nil {
|
|
|
|
return filepath.Join(configDir, configFile), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, search CAMLI_INCLUDE_PATH
|
2011-07-02 16:09:50 +00:00
|
|
|
p := os.Getenv("CAMLI_INCLUDE_PATH")
|
2011-06-24 20:02:51 +00:00
|
|
|
for _, d := range strings.Split(p, string(filepath.ListSeparator)) {
|
|
|
|
if _, err = os.Stat(filepath.Join(d, configFile)); err == nil {
|
|
|
|
return filepath.Join(d, configFile), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
return "", os.ErrNotExist
|
2011-06-24 20:02:51 +00:00
|
|
|
}
|
2012-12-12 17:04:05 +00:00
|
|
|
|
|
|
|
// GoPackagePath returns the path to the provided Go package's
|
|
|
|
// source directory.
|
|
|
|
// pkg may be a path prefix without any *.go files.
|
|
|
|
// The error is os.ErrNotExist if GOPATH is unset or the directory
|
|
|
|
// doesn't exist in any GOPATH component.
|
|
|
|
func GoPackagePath(pkg string) (path string, err error) {
|
|
|
|
gp := os.Getenv("GOPATH")
|
|
|
|
if gp == "" {
|
|
|
|
return path, os.ErrNotExist
|
|
|
|
}
|
2013-06-18 00:12:22 +00:00
|
|
|
for _, p := range filepath.SplitList(gp) {
|
|
|
|
dir := filepath.Join(p, "src", filepath.FromSlash(pkg))
|
2012-12-12 17:04:05 +00:00
|
|
|
fi, err := os.Stat(dir)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if !fi.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return dir, nil
|
|
|
|
}
|
|
|
|
return path, os.ErrNotExist
|
|
|
|
}
|