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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package osutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
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":
|
2011-09-17 00:56:49 +00:00
|
|
|
panic("CacheDir not implemented on OS == " + runtime.GOOS)
|
|
|
|
}
|
2012-04-22 23:19:21 +00:00
|
|
|
return filepath.Join(HomeDir(), ".cache", "camlistore")
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeCacheDir() {
|
|
|
|
os.Mkdir(cacheDir(), 0700)
|
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
|
|
|
}
|
2012-03-19 20:07:46 +00:00
|
|
|
return filepath.Join(HomeDir(), ".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
|
|
|
}
|
|
|
|
|
2012-04-12 23:54:57 +00:00
|
|
|
func IdentitySecretRing() string {
|
|
|
|
return filepath.Join(CamliConfigDir(), "identity-secring.gpg")
|
|
|
|
}
|
|
|
|
|
2011-06-24 20:02:51 +00:00
|
|
|
// Find the correct absolute path corresponding to a relative path,
|
|
|
|
// 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
|
|
|
|
|
|
|
func envVarSplitChar() string {
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "windows":
|
|
|
|
return ";"
|
|
|
|
case "plan9":
|
|
|
|
panic("unsupported")
|
|
|
|
}
|
|
|
|
return ":"
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
for _, p := range strings.Split(gp, envVarSplitChar()) {
|
|
|
|
dir := filepath.Join(p, "src", pkg)
|
|
|
|
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
|
|
|
|
}
|