예제 #1
0
/*
  Returns absolute path of executing file.
  WARNING: this must be called before
  changing the current directory
*/
func discoverExecName() string {
	if DEBUG {
		log.Print("Debug: discoverExecName\n")
	}

	f := os.Args[0]
	if path.IsAbs(f) {
		return f
	}

	wd, err := os.Getwd()
	if err != nil {
		panic(fmt.Sprintf("Getwd failed: %s", err))
	}

	_, err = os.Stat(f)
	if err == nil { // relative file exists
		return path.Clean(path.Join(wd, f))
	} // not exists? lookup in path

	f2, err := exec.LookPath(f)
	if err != nil {
		panic(fmt.Sprintf("lookpath failed: %s", err))
	}

	if path.IsAbs(f2) {
		return f2
	}

	return path.Clean(path.Join(wd, f2))
}
예제 #2
0
func main() {
	flag.StringVar(&etcdctlPath, "etcdctl-path", "/usr/bin/etcdctl", "absolute path to etcdctl executable")
	flag.StringVar(&etcdPath, "etcd-path", "/usr/bin/etcd2", "absolute path to etcd2 executable")
	flag.StringVar(&etcdRestoreDir, "etcd-restore-dir", "/var/lib/etcd2-restore", "absolute path to etcd2 restore dir")
	flag.StringVar(&etcdName, "etcd-name", "default", "name of etcd2 node")
	flag.StringVar(&etcdPeerUrls, "etcd-peer-urls", "", "advertise peer urls")

	flag.Parse()

	if etcdPeerUrls == "" {
		panic("must set -etcd-peer-urls")
	}

	if finfo, err := os.Stat(etcdRestoreDir); err != nil {
		panic(err)
	} else {
		if !finfo.IsDir() {
			panic(fmt.Errorf("%s is not a directory", etcdRestoreDir))
		}
	}

	if !path.IsAbs(etcdctlPath) {
		panic(fmt.Sprintf("etcdctl-path %s is not absolute", etcdctlPath))
	}

	if !path.IsAbs(etcdPath) {
		panic(fmt.Sprintf("etcd-path %s is not absolute", etcdPath))
	}

	if err := restoreEtcd(); err != nil {
		panic(err)
	}
}
예제 #3
0
// posixRel returns a relative path that is lexically equivalent to targpath when
// joined to basepath with an intervening separator.
//
// That is, Join(basepath, Rel(basepath, targpath)) is equivalent to targpath itself.
// On success, the returned path will always be relative to basepath,
// even if basepath and targpath share no elements.
// An error is returned if targpath can't be made relative to basepath or if
// knowing the current working directory would be necessary to compute it.
//
// Copy-pasted & slightly edited from Go's lib path/filepath/path.go .
//
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
func posixRel(basepath, targpath string) (string, error) {
	base := path.Clean(basepath)
	targ := path.Clean(targpath)
	if targ == base {
		return ".", nil
	}
	if base == "." {
		base = ""
	}
	if path.IsAbs(base) != path.IsAbs(targ) {
		return "", errors.New("Rel: can't make " + targ + " relative to " + base)
	}
	// Position base[b0:bi] and targ[t0:ti] at the first differing elements.
	bl := len(base)
	tl := len(targ)
	var b0, bi, t0, ti int
	for {
		for bi < bl && base[bi] != '/' {
			bi++
		}
		for ti < tl && targ[ti] != '/' {
			ti++
		}
		if targ[t0:ti] != base[b0:bi] {
			break
		}
		if bi < bl {
			bi++
		}
		if ti < tl {
			ti++
		}
		b0 = bi
		t0 = ti
	}
	if base[b0:bi] == ".." {
		return "", errors.New("Rel: can't make " + targ + " relative to " + base)
	}
	if b0 != bl {
		// Base elements left. Must go up before going down.
		seps := strings.Count(base[b0:bl], string('/'))
		size := 2 + seps*3
		if tl != t0 {
			size += 1 + tl - t0
		}
		buf := make([]byte, size)
		n := copy(buf, "..")
		for i := 0; i < seps; i++ {
			buf[n] = '/'
			copy(buf[n+1:], "..")
			n += 3
		}
		if t0 != tl {
			buf[n] = '/'
			copy(buf[n+1:], targ[t0:])
		}
		return string(buf), nil
	}
	return targ[t0:], nil
}
예제 #4
0
/*
  Returns absolute path of executing file.
  WARNING: this must be called before
  changing the current directory
*/
func (be *BdsExec) discoverExecName() string {
	if DEBUG {
		log.Printf("Debug: discoverExecName (%s)\n", be.args[0])
	}

	f := be.args[0]

	if path.IsAbs(f) {
		return f
	}

	wd, err := os.Getwd()
	if err != nil {
		panic(fmt.Sprintf("discoverExecName: Getwd failed '%s'", err))
	}

	_, err = os.Stat(f)
	if err == nil {
		// Relative file exists
		return path.Clean(path.Join(wd, f))
	}

	f2, err := exec.LookPath(f)
	if err != nil {
		panic(fmt.Sprintf("discoverExecName: Lookpath failed '%s'", err))
	}

	if path.IsAbs(f2) {
		return f2
	}

	return path.Clean(path.Join(wd, f2))
}
예제 #5
0
파일: path.go 프로젝트: CarlChenCC/examples
func main() {
	wd, err := os.Getwd()
	if err != nil {
		panic(err)
	}

	fmt.Println("working directory: ", wd)

	fmt.Println("base: ", path.Base(wd))
	fmt.Println("dir: ", path.Dir(wd))
	fmt.Println("ext: ", path.Ext(wd+"howdy.test"))
	fmt.Println("abs: ", path.IsAbs(wd))
	fmt.Println("abs: ", path.IsAbs("howdy/../../"))

	dirty := "////howdy////lots/of//slashes//yeah/"
	fmt.Println("dirty: ", dirty)
	fmt.Println("clean: ", path.Clean(dirty))

	fmt.Println("joined: ", path.Join("one", "two", "three", "four"))

	dir, file := path.Split(wd + "/lala.txt")
	fmt.Println("dir: ", dir, " file: ", file)

	// Hmmmm, I wonder if path works with URLs.
	var base string
	url := "http://example.com/test/file.txt"
	base, file = path.Split(url)
	fmt.Printf("Got base %v and file %v\n", base, file)

	// Looks like the double slash after http: gets messed up by path.Dir.
	fmt.Printf("Base is %v\nDir is %v\n", path.Base(url), path.Dir(url))
}
예제 #6
0
func TestAbs(t *testing.T) {
	fmt.Println("Ejecutanto test de Abs...")
	falseExpected := path.IsAbs("./some/path")
	trueExpected := path.IsAbs("/dev/nul")
	if falseExpected == true || trueExpected == false {
		t.Error("Fallo en test de Abs.")
	}
}
예제 #7
0
// getBinlogSize returns the total size of the binlog files
func (m *MySQLBinlogGrowth) getBinlogSize(binLog string, dataDir string) (size int64, err error) {
	// Read the binlog.index file
	// It contains a list of log files, one per line
	file, err := os.Open(binLog)
	if err != nil {
		m.log.Warn(fmt.Sprintf("Cannot open index file %s : %s", binLog, err))
		return
	}
	defer file.Close()

	// Read the file line by line
	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		fileName := scanner.Text()
		// If the fileName is not an absolute path, then it's relative to the datadir directory
		if !path.IsAbs(fileName) {
			fileName = path.Join(dataDir, fileName)
		}

		fileSize, err := getFileSize(fileName)
		if err != nil {
			m.log.Warn(err)
		}
		size += fileSize

	}

	if scanErr := scanner.Err(); scanErr != nil {
		m.log.Warn("There was an error reading the index file")
		return
	}
	return
}
예제 #8
0
파일: kbfspath.go 프로젝트: gozes/kbfs-beta
func split(pathStr string) ([]string, error) {
	cleanPath := path.Clean(pathStr)
	if !path.IsAbs(cleanPath) {
		return nil, fmt.Errorf("split: %s is not an absolute path", pathStr)
	}
	return splitHelper(cleanPath), nil
}
예제 #9
0
func zipPath(name string) (string, error) {
	name = path.Clean(name)
	if !path.IsAbs(name) {
		return "", fmt.Errorf("stat: not an absolute path: %s", name)
	}
	return name[1:], nil // strip leading '/'
}
예제 #10
0
// getBinlogPath read and parse the my.cnf config file and returns the path to the binlog file and datadir.
func (m *MySQLBinlogGrowth) getBinlogPath() (binLog string, dataDir string) {
	// read my.cnf config file
	config, err := configparser.Read(m.myCnfPath)
	if err != nil {
		m.log.Error(err)
		return
	}

	section, err := config.Section("mysqld")
	if err != nil {
		m.log.Error("mysqld section missing in ", m.myCnfPath)
		return
	}

	binLog = section.ValueOf("log-bin")
	if binLog == "" {
		m.log.Error("log-bin value missing from ", m.myCnfPath)
		return
	}

	dataDir = section.ValueOf("datadir")
	if dataDir == "" {
		m.log.Error("datadir value missing from ", m.myCnfPath)
		return
	}

	// If the log-bin value is a relative path then it's based on datadir
	if !path.IsAbs(binLog) {
		binLog = path.Join(dataDir, binLog)
	}
	return
}
예제 #11
0
// validatePath validates a single path, returning an error if the path is
// invalid.  paths may not:
//
// 1. be absolute
// 2. contain '..' as an element
// 3. start with '..'
// 4. contain filenames larger than 255 characters
// 5. be longer than 4096 characters
func validatePath(targetPath string) error {
	// TODO: somehow unify this with the similar api validation,
	// validateVolumeSourcePath; the error semantics are just different enough
	// from this that it was time-prohibitive trying to find the right
	// refactoring to re-use.
	if targetPath == "" {
		return fmt.Errorf("invalid path: must not be empty: %q", targetPath)
	}
	if path.IsAbs(targetPath) {
		return fmt.Errorf("invalid path: must be relative path: %s", targetPath)
	}

	if len(targetPath) > maxPathLength {
		return fmt.Errorf("invalid path: must be less than %d characters", maxPathLength)
	}

	items := strings.Split(targetPath, string(os.PathSeparator))
	for _, item := range items {
		if item == ".." {
			return fmt.Errorf("invalid path: must not contain '..': %s", targetPath)
		}
		if len(item) > maxFileNameLength {
			return fmt.Errorf("invalid path: filenames must be less than %d characters", maxFileNameLength)
		}
	}
	if strings.HasPrefix(items[0], "..") && len(items[0]) > 2 {
		return fmt.Errorf("invalid path: must not start with '..': %s", targetPath)
	}

	return nil
}
예제 #12
0
func getCommand(r *ReplYell) {
	shellState := getShellState()
	key := ""

	switch len(r.Args) {
	case 1:
		key = r.Args[0]
	case 0:
		key = ""
		fmt.Println("No key set: Getting pwd " + shellState.pwd)
	default:
		fmt.Println("Need one key - multiple key retrieval TODO")
		return
	}

	if !path.IsAbs(key) {
		key = path.Join(shellState.pwd, key)
	} else {
		key = path.Clean(key)
	}

	resp, err := shellState.kapi.Get(context.TODO(), key, nil)

	if err != nil {
		fmt.Println(err)
		return
	}
	printResponseKey(resp)

}
예제 #13
0
// findConfig returns the filename of the
// config. It searches parent directories
// if it can't find any of the config
// filenames in the current directory.
func findConfig(location string) string {
	configFiles := configFilenames(location)
	// Absolute path to config given
	if len(location) > 0 && path.IsAbs(location) {
		if _, err := os.Stat(location); err == nil {
			return location
		}
	} else { // Relative config
		configPath, _ := os.Getwd()
		for {
			for _, f := range configFiles {
				// the root path is a `/` but others don't have a trailing `/`
				filename := strings.TrimSuffix(configPath, "/") + "/" + f
				if _, err := os.Stat(filename); err == nil {
					return filename
				}
			}
			// loop only if we haven't yet reached the root
			if parentPath := path.Dir(configPath); len(parentPath) != len(configPath) {
				configPath = parentPath
			} else {
				break
			}
		}
	}
	panic(StatusError{fmt.Errorf("No configuration found %v", configFiles), 78})
}
예제 #14
0
func (bh *BoxHandler) PopulateBoxes(directories []string, port *int, hostname *string) {
	log.Println("Populating boxes..")
	absolutedirectories := []string{}
	for _, d := range directories {
		if !path.IsAbs(d) {
			wd, _ := os.Getwd()
			absolute := path.Clean(path.Join(wd, d))
			absolutedirectories = append(absolutedirectories, absolute)
		} else {
			absolutedirectories = append(absolutedirectories, d)
		}
	}
	bh.Directories = absolutedirectories
	boxfiles := getBoxList(absolutedirectories)
	boxdata := bh.getBoxData(boxfiles)
	bh.createBoxes(boxdata, *port, hostname)

	for _, boxinfo := range bh.Boxes {
		for boxname, box := range boxinfo {
			for _, version := range box.Versions {
				for _, provider := range version.Providers {
					log.Println("Found " + box.Username + "/" + boxname + "/" + version.Version + "/" + provider.Name)
				}
			}
		}
	}
}
예제 #15
0
파일: saxer.go 프로젝트: tcw/saxer
func abs(name string) (string, error) {
	if path.IsAbs(name) {
		return name, nil
	}
	wd, err := os.Getwd()
	return path.Join(wd, name), err
}
예제 #16
0
파일: file.go 프로젝트: xgfone/go-tools
// Get the absolute filepath, based on built executable file.
func RealPath(fp string) (string, error) {
	if path.IsAbs(fp) {
		return fp, nil
	}
	wd, err := os.Getwd()
	return path.Join(wd, fp), err
}
예제 #17
0
파일: volume.go 프로젝트: crowley-io/pack
func isAbsolutePath(t, p string) error {
	if !path.IsAbs(p) {
		return fmt.Errorf("%s path '%s' is not an absolute path", t, p)
	}

	return nil
}
예제 #18
0
파일: gl4d0s.go 프로젝트: BullyBoy/sectalks
func join(dir, file string) (string, error) {
	pth := path.Join(dir, file)

	fi, err := os.Lstat(pth)
	if err != nil {
		panic("could not Lstat file")
	}

	// This stops absolute links from working, but garbage ones should still work.
	// HINT[2]: "There are three types of symlinks. Those who care about the directories around them and those who don't. And then there's the garbage ones. symlink(2) is stupid."
	if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
		link, err := os.Readlink(pth)
		if err != nil {
			panic("could not Readlink a link")
		}

		// Block relative symlinks, so they have to use /../../../../magic.
		// Seriously though, why is symlink(2) *that* bad?
		if !path.IsAbs(link) {
			return "", fmt.Errorf("relative symlink detected")
		}

		pth = path.Join(dir, link)
	}

	pth = path.Clean(pth)
	return pth, nil
}
예제 #19
0
파일: rush.go 프로젝트: u-root/u-root
func doArgs(cmds []*Command) error {
	for _, c := range cmds {
		globargv := []string{}
		for _, v := range c.args {
			if v.mod == "ENV" {
				e := v.val
				if !path.IsAbs(v.val) {
					e = path.Join(envDir, e)
				}
				b, err := ioutil.ReadFile(e)
				if err != nil {
					return err
				}
				// It goes in as one argument. Not sure if this is what we want
				// but it gets very weird to start splitting it on spaces. Or maybe not?
				globargv = append(globargv, string(b))
			} else if globs, err := filepath.Glob(v.val); err == nil && len(globs) > 0 {
				globargv = append(globargv, globs...)
			} else {
				globargv = append(globargv, v.val)
			}
		}

		c.cmd = globargv[0]
		c.argv = globargv[1:]
	}
	return nil
}
예제 #20
0
파일: config.go 프로젝트: jesper/crane
func (c *config) MacSync(name string) MacSync {
	parts := strings.Split(name, ":")
	if !path.IsAbs(parts[0]) {
		parts[0] = c.path + "/" + parts[0]
	}
	return c.macSyncMap[strings.Join(parts, ":")]
}
예제 #21
0
파일: zipfs.go 프로젝트: RoneyThomas/mgodoc
func zipPath(name string) string {
	name = path.Clean(name)
	if !path.IsAbs(name) {
		panic(fmt.Sprintf("stat: not an absolute path: %s", name))
	}
	return name[1:] // strip leading '/'
}
예제 #22
0
//loadConfig unmarshals config for current application mode
func loadConfig() {
	data, err := ioutil.ReadFile("./config/config.json")
	if err != nil {
		panic(err)
	}
	configs := &Configs{}
	if err := json.Unmarshal(data, configs); err != nil {
		panic(err)
	}
	switch GetMode() {
	case DebugMode:
		config = &configs.Debug
	case ReleaseMode:
		config = &configs.Release
	case TestMode:
		config = &configs.Test
	}
	if !path.IsAbs(config.Public) {
		workingDir, err := os.Getwd()
		if err != nil {
			panic(err)
		}
		config.Public = path.Join(workingDir, config.Public)
	}
	config.Uploads = path.Join(config.Public, "uploads")
}
예제 #23
0
파일: deceive.go 프로젝트: paultag/deceive
func GetConfig() Deceive {
	conf := Deceive{
		Host:   "localhost",
		Port:   1984,
		Cert:   "/etc/deceive/deceive.crt",
		Key:    "/etc/deceive/deceive.key",
		CaCert: "/etc/deceive/ca.crt",
		Root:   "/var/lib/deceive/",
	}
	flags, err := config.LoadFlags("deceive", &conf)
	if err != nil {
		panic(err)
	}
	flags.Parse(os.Args[1:])

	if !path.IsAbs(conf.Root) {
		cwd, err := os.Getwd()
		if err != nil {
			panic(err)
		}
		conf.Root = path.Clean(path.Join(cwd, conf.Root))
	}

	return conf
}
예제 #24
0
파일: app.go 프로젝트: Clarifai/kubernetes
func (a *App) assertValid() error {
	if err := a.Exec.assertValid(); err != nil {
		return err
	}
	if a.User == "" {
		return errors.New(`user is required`)
	}
	if a.Group == "" {
		return errors.New(`group is required`)
	}
	if !path.IsAbs(a.WorkingDirectory) && a.WorkingDirectory != "" {
		return errors.New("workingDirectory must be an absolute path")
	}
	eh := make(map[string]bool)
	for _, e := range a.EventHandlers {
		name := e.Name
		if eh[name] {
			return fmt.Errorf("Only one eventHandler of name %q allowed", name)
		}
		eh[name] = true
	}
	if err := a.Environment.assertValid(); err != nil {
		return err
	}
	return nil
}
예제 #25
0
파일: file.go 프로젝트: kennylixi/com
// get absolute filepath, based on built executable file
func RealPath(file string) (string, error) {
	if path.IsAbs(file) {
		return file, nil
	}
	wd, err := os.Getwd()
	return path.Join(wd, file), err
}
func (s *executor) createVolumes() ([]string, []string, error) {
	var binds, volumesFrom []string

	for _, volume := range s.Config.Docker.Volumes {
		s.addVolume(&binds, &volumesFrom, volume)
	}

	// Cache Git sources:
	// take path of the projects directory,
	// because we use `rm -rf` which could remove the mounted volume
	parentDir := path.Dir(s.Build.FullProjectDir())

	// Caching is supported only for absolute and non-root paths
	if path.IsAbs(parentDir) && parentDir != "/" {
		if s.Build.GetGitStrategy() == common.GitFetch && !s.Config.Docker.DisableCache {
			// create persistent cache container
			s.addVolume(&binds, &volumesFrom, parentDir)
		} else {
			// create temporary cache container
			container, _ := s.createCacheVolume("", parentDir)
			if container != nil {
				s.caches = append(s.caches, container)
				volumesFrom = append(volumesFrom, container.ID)
			}
		}
	}

	return binds, volumesFrom, nil
}
예제 #27
0
파일: config.go 프로젝트: nelsonsilva/hugo
func (c *Config) findConfigFile(configFileName string) (string, error) {

	if configFileName == "" { // config not specified, let's search
		if b, _ := exists(c.GetAbsPath("config.json")); b {
			return c.GetAbsPath("config.json"), nil
		}

		if b, _ := exists(c.GetAbsPath("config.toml")); b {
			return c.GetAbsPath("config.toml"), nil
		}

		if b, _ := exists(c.GetAbsPath("config.yaml")); b {
			return c.GetAbsPath("config.yaml"), nil
		}

		return "", fmt.Errorf("config file not found in: %s", c.GetPath())

	} else {
		// If the full path is given, just use that
		if path.IsAbs(configFileName) {
			return configFileName, nil
		}

		// Else check the local directory
		t := c.GetAbsPath(configFileName)
		if b, _ := exists(t); b {
			return t, nil
		} else {
			return "", fmt.Errorf("config file not found at: %s", t)
		}
	}
}
예제 #28
0
// GetLogReader returns a reader for the specified filename. Any
// external requests (say from the admin UI via HTTP) must specify
// allowAbsolute as false to prevent leakage of non-log
// files. Absolute filenames are allowed for the case of the cockroach "log"
// command, which provides human readable output from an arbitrary file,
// and is intended to be run locally in a terminal.
func GetLogReader(filename string, allowAbsolute bool) (io.ReadCloser, error) {
	if path.IsAbs(filename) {
		if !allowAbsolute {
			return nil, fmt.Errorf("absolute pathnames are forbidden: %s", filename)
		}
		if verifyFile(filename) == nil {
			return os.Open(filename)
		}
	}
	// Verify there are no path separators in the a non-absolute pathname.
	if path.Base(filename) != filename {
		return nil, fmt.Errorf("pathnames must be basenames only: %s", filename)
	}
	if !logFileRE.MatchString(filename) {
		return nil, fmt.Errorf("filename is not a cockroach log file: %s", filename)
	}
	var reader io.ReadCloser
	var err error
	filename = path.Join(*logDir, filename)
	if verifyFile(filename) == nil {
		reader, err = os.Open(filename)
		if err == nil {
			return reader, err
		}
	}
	return nil, err
}
예제 #29
0
파일: config.go 프로젝트: kiyoto/ik
func handleInclude(reader LineReader, context *parserContext, attrValue string) error {
	url_, err := url.Parse(attrValue)
	if err != nil {
		return err
	}
	if url_.Scheme == "file" || url_.Path == attrValue {
		var abspathPattern string
		if path.IsAbs(url_.Path) {
			abspathPattern = url_.Path
		} else {
			abspathPattern = path.Join(context.opener.BasePath(), url_.Path)
		}
		files, err := Glob(context.opener.FileSystem(), abspathPattern)
		if err != nil {
			return err
		}
		for _, file := range files {
			newReader, err := context.opener.NewLineReader(file)
			if err != nil {
				return err
			}
			defer newReader.Close()
			parseConfig(newReader, &parserContext{
				tag:     context.tag,
				tagArgs: context.tagArgs,
				elems:   context.elems,
				attrs:   context.attrs,
				opener:  context.opener.NewOpener(path.Dir(file)),
			})
		}
		return nil
	} else {
		return errors.New("Not implemented!")
	}
}
예제 #30
0
파일: paths.go 프로젝트: xiekeyang/hdfs
// normalizePaths parses the hosts out of HDFS URLs, and turns relative paths
// into absolute ones (by appending /user/<user>). If multiple HDFS urls with
// differing hosts are passed in, it returns an error.
func normalizePaths(paths []string) ([]string, string, error) {
	namenode := ""
	cleanPaths := make([]string, 0, len(paths))

	for _, rawurl := range paths {
		url, err := url.Parse(rawurl)
		if err != nil {
			return nil, "", err
		}

		if url.Host != "" {
			if namenode != "" && namenode != url.Host {
				return nil, "", errMultipleNamenodeUrls
			}

			namenode = url.Host
		}

		p := path.Clean(url.Path)
		if !path.IsAbs(url.Path) {
			p = path.Join(rootPath, p)
		}

		cleanPaths = append(cleanPaths, p)
	}

	return cleanPaths, namenode, nil
}