当前位置: 首页>>代码示例>>Golang>>正文


Golang os.IsPathSeparator函数代码示例

本文整理汇总了Golang中os.IsPathSeparator函数的典型用法代码示例。如果您正苦于以下问题:Golang IsPathSeparator函数的具体用法?Golang IsPathSeparator怎么用?Golang IsPathSeparator使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了IsPathSeparator函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: fileMatchRegexp

// append a path separator if needed and escape regexp meta characters
func fileMatchRegexp(logRoot, fileMatch string) *regexp.Regexp {
	if !os.IsPathSeparator(logRoot[len(logRoot)-1]) && !os.IsPathSeparator(fileMatch[0]) {
		logRoot += string(os.PathSeparator)
	}

	return regexp.MustCompile("^" + regexp.QuoteMeta(logRoot) + fileMatch)
}
开发者ID:orangemi,项目名称:heka,代码行数:8,代码来源:filehandling.go

示例2: walkLinks

func walkLinks(path string, linksWalked *int) (string, error) {
	switch dir, file := Split(path); {
	case dir == "":
		newpath, _, err := walkLink(file, linksWalked)
		return newpath, err
	case file == "":
		if isDriveLetter(dir) {
			return dir, nil
		}
		if os.IsPathSeparator(dir[len(dir)-1]) {
			if isRoot(dir) {
				return dir, nil
			}
			return walkLinks(dir[:len(dir)-1], linksWalked)
		}
		newpath, _, err := walkLink(dir, linksWalked)
		return newpath, err
	default:
		newdir, err := walkLinks(dir, linksWalked)
		if err != nil {
			return "", err
		}
		newpath, islink, err := walkLink(Join(newdir, file), linksWalked)
		if err != nil {
			return "", err
		}
		if !islink {
			return newpath, nil
		}
		if IsAbs(newpath) || os.IsPathSeparator(newpath[0]) {
			return newpath, nil
		}
		return Join(newdir, newpath), nil
	}
}
开发者ID:RajibTheKing,项目名称:gcc,代码行数:35,代码来源:symlink.go

示例3: mrtFileOpen

func mrtFileOpen(filename string, interval uint64) (*os.File, error) {
	realname := filename
	if interval != 0 {
		realname = time.Now().Format(filename)
	}

	i := len(realname)
	for i > 0 && os.IsPathSeparator(realname[i-1]) {
		// skip trailing path separators
		i--
	}
	j := i

	for j > 0 && !os.IsPathSeparator(realname[j-1]) {
		j--
	}

	if j > 0 {
		if err := os.MkdirAll(realname[0:j-1], 0755); err != nil {
			log.Warn(err)
			return nil, err
		}
	}

	file, err := os.OpenFile(realname, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0644)
	if err != nil {
		log.Warn(err)
	}
	return file, err
}
开发者ID:mfeed,项目名称:gobgp,代码行数:30,代码来源:watcher.go

示例4: FindAndParseIndexYAML

// FindAndParseIndexYAML walks up from the directory specified by path until it
// finds a `index.yaml` or `index.yml` file. If an index YAML file
// is found, it opens and parses the file, and returns all the indexes found.
// If path is a relative path, it is converted into an absolute path
// relative to the calling test file. To determine the path of the calling test
// file, FindAndParseIndexYAML walks upto a maximum of 100 call stack frames
// looking for a file ending with `_test.go`.
//
// FindAndParseIndexYAML returns a non-nil error if the root of the drive is
// reached without finding an index YAML file, if there was
// an error reading the found index YAML file, or if the calling test file could
// not be located in the case of a relative path argument.
func FindAndParseIndexYAML(path string) ([]*IndexDefinition, error) {
	var currentDir string

	if filepath.IsAbs(path) {
		currentDir = path
	} else {
		testPath, err := getCallingTestFilePath(100)
		if err != nil {
			return nil, err
		}
		currentDir = filepath.Join(filepath.Dir(testPath), path)
	}

	isRoot := func(dir string) bool {
		parentDir := filepath.Dir(dir)
		return os.IsPathSeparator(dir[len(dir)-1]) && os.IsPathSeparator(parentDir[len(parentDir)-1])
	}

	for {
		for _, filename := range []string{"index.yml", "index.yaml"} {
			file, err := os.Open(filepath.Join(currentDir, filename))
			if err == nil {
				defer file.Close()
				return ParseIndexYAML(file)
			}
		}

		if isRoot(currentDir) {
			return nil, fmt.Errorf("datastore: failed to find index YAML file")
		}

		currentDir = filepath.Dir(currentDir)
	}
}
开发者ID:tetrafolium,项目名称:gae,代码行数:46,代码来源:datastore.go

示例5: simplifyFilePath

func simplifyFilePath(path string) string {
	pathLen := len(path)
	if strings.HasPrefix(path, goroot) && pathLen > len(goroot) {
		return path[len(goroot):]
	}
	for _, tmpPath := range sourcePaths {
		var check string
		if len(tmpPath) > 1 && os.IsPathSeparator(tmpPath[0]) {
			check = tmpPath[1:]
		} else {
			check = tmpPath
		}
		if strings.HasPrefix(path, check) && pathLen > len(check) {
			var src, pkg string
			src = path + "src" + string(os.PathSeparator)
			pkg = path + "pkg" + string(os.PathSeparator)
			if strings.HasPrefix(path, src) && pathLen > len(src) {
				return path[len(src):]
			} else if strings.HasPrefix(path, pkg) && pathLen > len(pkg) {
				return path[len(pkg):]
			} else {
				return path[len(check):]
			}
		}
	}
	if pathLen > 1 && os.IsPathSeparator(path[0]) {
		path = path[1:]
	}
	return path
}
开发者ID:pettyjamesm,项目名称:bugsnag-go,代码行数:30,代码来源:errors.go

示例6: MkdirAll

func (c *Client) MkdirAll(path string) error {
	dir, err := c.Stat(path)
	if err == nil {
		if dir.IsDir() {
			return nil
		}
	}
	i := len(path)
	for i > 0 && os.IsPathSeparator(path[i-1]) { // Skip trailing path separator.
		i--
	}
	j := i
	for j > 0 && !os.IsPathSeparator(path[j-1]) { // Scan backward over element.
		j--
	}
	if j > 1 {
		// Create parent
		err = c.MkdirAll(path[0 : j-1])
		if err != nil {
			return err
		}
	}
	err = c.Mkdir(path)
	if err != nil {
		return err
	}

	return nil

}
开发者ID:lostz,项目名称:sftp,代码行数:30,代码来源:client.go

示例7: mkdirAll

func mkdirAll(path string, hdr Metadata) (stack []string, topMTime time.Time, err error) {
	// Following code derives from the golang standard library, so you can consider it BSDish if you like.
	// Our changes are licensed under Apache for the sake of overall license simplicity of the project.
	// Ref: https://github.com/golang/go/blob/883bc6ed0ea815293fe6309d66f967ea60630e87/src/os/path.go#L12

	// Fast path: if we can tell whether path is a directory or file, stop with success or error.
	dir, err := os.Stat(path)
	if err == nil {
		if dir.IsDir() {
			return nil, dir.ModTime(), nil
		}
		return nil, dir.ModTime(), &os.PathError{"mkdir", path, syscall.ENOTDIR}
	}

	// Slow path: make sure parent exists and then call Mkdir for path.
	i := len(path)
	for i > 0 && os.IsPathSeparator(path[i-1]) { // Skip trailing path separator.
		i--
	}

	j := i
	for j > 0 && !os.IsPathSeparator(path[j-1]) { // Scan backward over element.
		j--
	}

	if j > 1 {
		// Create parent
		stack, topMTime, err = mkdirAll(path[0:j-1], hdr)
		if err != nil {
			return stack, topMTime, err
		}
	}

	// Parent now exists; invoke Mkdir and use its result.
	err = os.Mkdir(path, 0755)
	if err != nil {
		// Handle arguments like "foo/." by
		// double-checking that directory doesn't exist.
		dir, err1 := os.Lstat(path)
		if err1 == nil && dir.IsDir() {
			return stack, topMTime, nil
		}
		return stack, topMTime, err
	}
	stack = append(stack, path)

	// Apply standardizations.
	if err := os.Lchown(path, hdr.Uid, hdr.Gid); err != nil {
		return stack, topMTime, err
	}
	if err := os.Chmod(path, hdr.FileMode()); err != nil {
		return stack, topMTime, err
	}
	// Except for time, because as usual with dirs, that requires walking backwards again at the end.
	// That'll be done one function out.

	return stack, topMTime, nil
}
开发者ID:kkroening,项目名称:repeatr,代码行数:58,代码来源:emplacements.go

示例8: walkSymlinks

func walkSymlinks(path string) (string, error) {
	const maxIter = 255
	originalPath := path
	// consume path by taking each frontmost path element,
	// expanding it if it's a symlink, and appending it to b
	var b bytes.Buffer
	for n := 0; path != ""; n++ {
		if n > maxIter {
			return "", errors.New("EvalSymlinks: too many links in " + originalPath)
		}

		// find next path component, p
		var i = -1
		for j, c := range path {
			if c < utf8RuneSelf && os.IsPathSeparator(uint8(c)) {
				i = j
				break
			}
		}
		var p string
		if i == -1 {
			p, path = path, ""
		} else {
			p, path = path[:i], path[i+1:]
		}

		if p == "" {
			if b.Len() == 0 {
				// must be absolute path
				b.WriteRune(Separator)
			}
			continue
		}

		fi, err := os.Lstat(b.String() + p)
		if err != nil {
			return "", err
		}
		if fi.Mode()&os.ModeSymlink == 0 {
			b.WriteString(p)
			if path != "" || (b.Len() == 2 && len(p) == 2 && p[1] == ':') {
				b.WriteRune(Separator)
			}
			continue
		}

		// it's a symlink, put it at the front of path
		dest, err := os.Readlink(b.String() + p)
		if err != nil {
			return "", err
		}
		if IsAbs(dest) || os.IsPathSeparator(dest[0]) {
			b.Reset()
		}
		path = dest + string(Separator) + path
	}
	return Clean(b.String()), nil
}
开发者ID:pjump,项目名称:gcc,代码行数:58,代码来源:symlink.go

示例9: mkdirall

// mkdirall is a custom version of os.MkdirAll modified for use on Windows
// so that it is both volume path aware, and can create a directory with
// a DACL.
func mkdirall(path string, adminAndLocalSystem bool) error {
	if re := regexp.MustCompile(`^\\\\\?\\Volume{[a-z0-9-]+}$`); re.MatchString(path) {
		return nil
	}

	// The rest of this method is largely copied from os.MkdirAll and should be kept
	// as-is to ensure compatibility.

	// Fast path: if we can tell whether path is a directory or file, stop with success or error.
	dir, err := os.Stat(path)
	if err == nil {
		if dir.IsDir() {
			return nil
		}
		return &os.PathError{
			Op:   "mkdir",
			Path: path,
			Err:  syscall.ENOTDIR,
		}
	}

	// Slow path: make sure parent exists and then call Mkdir for path.
	i := len(path)
	for i > 0 && os.IsPathSeparator(path[i-1]) { // Skip trailing path separator.
		i--
	}

	j := i
	for j > 0 && !os.IsPathSeparator(path[j-1]) { // Scan backward over element.
		j--
	}

	if j > 1 {
		// Create parent
		err = mkdirall(path[0:j-1], false)
		if err != nil {
			return err
		}
	}

	// Parent now exists; invoke os.Mkdir or mkdirWithACL and use its result.
	if adminAndLocalSystem {
		err = mkdirWithACL(path)
	} else {
		err = os.Mkdir(path, 0)
	}

	if err != nil {
		// Handle arguments like "foo/." by
		// double-checking that directory doesn't exist.
		dir, err1 := os.Lstat(path)
		if err1 == nil && dir.IsDir() {
			return nil
		}
		return err
	}
	return nil
}
开发者ID:djs55,项目名称:docker,代码行数:61,代码来源:filesys_windows.go

示例10: mkdirAllWithOwner

// Like os.MkdirAll but new components created have the given UID and GID.
func mkdirAllWithOwner(absPath string, perm os.FileMode, uid, gid int) error {
	// From os/path.go.
	// Fast path: if we can tell whether path is a directory or file, stop with success or error.
	dir, err := os.Stat(absPath)
	if err == nil {
		if dir.IsDir() {
			return nil
		}
		return &os.PathError{"mkdir", absPath, syscall.ENOTDIR}
	}

	// Slow path: make sure parent exists and then call Mkdir for path.
	i := len(absPath)
	for i > 0 && os.IsPathSeparator(absPath[i-1]) { // Skip trailing path separator.
		i--
	}

	j := i
	for j > 0 && !os.IsPathSeparator(absPath[j-1]) { // Scan backward over element.
		j--
	}

	if j > 1 {
		// Create parent
		err = mkdirAllWithOwner(absPath[0:j-1], perm, uid, gid)
		if err != nil {
			return err
		}
	}

	// Parent now exists; invoke Mkdir and use its result.
	err = os.Mkdir(absPath, perm)
	if err != nil {
		// Handle arguments like "foo/." by double-checking that directory
		// doesn't exist.
		dir, err1 := os.Lstat(absPath)
		if err1 == nil && dir.IsDir() {
			return nil
		}

		return err
	}

	if uid >= 0 || gid >= 0 {
		if uid < 0 {
			uid = os.Getuid()
		}
		if gid < 0 {
			gid = os.Getgid()
		}
		err = os.Lchown(absPath, uid, gid) // ignore errors in case we aren't root
		log.Errore(err, "cannot chown ", absPath)
	}

	return nil
}
开发者ID:joneskoo,项目名称:acmetool-kapsi,代码行数:57,代码来源:mkdir.go

示例11: removeSlashes

/**
 * Remove a string's leading and trailing slashes
 *
 * @param *string
 */
func removeSlashes(s *string) {
	for {
		if os.IsPathSeparator(((*s)[len(*s)-1 : len(*s)])[0]) {
			*s = (*s)[0 : len(*s)-1]
		} else if os.IsPathSeparator(((*s)[0:1])[0]) {
			*s = (*s)[1:len(*s)]
		} else {
			break
		}
	}
}
开发者ID:Adar,项目名称:go-supervise,代码行数:16,代码来源:helpers.go

示例12: isRoot

// isRoot returns true if path is root of file system
// (`/` on unix and `/`, `\`, `c:\` or `c:/` on windows).
func isRoot(path string) bool {
	if runtime.GOOS != "windows" {
		return path == "/"
	}
	switch len(path) {
	case 1:
		return os.IsPathSeparator(path[0])
	case 3:
		return path[1] == ':' && os.IsPathSeparator(path[2])
	}
	return false
}
开发者ID:fjballest,项目名称:golang,代码行数:14,代码来源:symlink.go

示例13: GetParentDir

// GetParentDir calculate the parent dir of param path, no matter whether path exists.
func GetParentDir(path string) string {
	i := len(path)
	for i > 0 && os.IsPathSeparator(path[i-1]) { // Skip trailing path separator.
		i--
	}

	j := i
	for j > 0 && !os.IsPathSeparator(path[j-1]) { // Scan backward over element.
		j--
	}

	return path[0 : j-1]
}
开发者ID:jackwanger,项目名称:cloud-base,代码行数:14,代码来源:util.go

示例14: MkdirAllOwner

func MkdirAllOwner(path string, perm os.FileMode, uid int, gid int) error {
	// This function is a slightly modified version of MkdirAll from the Go standard library.
	// https://golang.org/src/os/path.go?s=488:535#L9

	// Fast path: if we can tell whether path is a directory or file, stop with success or error.
	dir, err := os.Stat(path)
	if err == nil {
		if dir.IsDir() {
			return nil
		}
		return fmt.Errorf("path exists but isn't a directory")
	}

	// Slow path: make sure parent exists and then call Mkdir for path.
	i := len(path)
	for i > 0 && os.IsPathSeparator(path[i-1]) { // Skip trailing path separator.
		i--
	}

	j := i
	for j > 0 && !os.IsPathSeparator(path[j-1]) { // Scan backward over element.
		j--
	}

	if j > 1 {
		// Create parent
		err = MkdirAllOwner(path[0:j-1], perm, uid, gid)
		if err != nil {
			return err
		}
	}

	// Parent now exists; invoke Mkdir and use its result.
	err = os.Mkdir(path, perm)

	err_chown := os.Chown(path, uid, gid)
	if err_chown != nil {
		return err_chown
	}

	if err != nil {
		// Handle arguments like "foo/." by
		// double-checking that directory doesn't exist.
		dir, err1 := os.Lstat(path)
		if err1 == nil && dir.IsDir() {
			return nil
		}
		return err
	}
	return nil
}
开发者ID:akshaykarle,项目名称:lxd,代码行数:51,代码来源:util.go

示例15: mkdirAll

// mkdirAll creates a directory named path,
// along with any necessary parents, and returns nil,
// or else returns an error. The permission bits perm are used
// for all directories that mkdirAll creates. If path is already
// a directory, mkdirAll does nothing and returns nil.
func mkdirAll(path string, perm os.FileMode) error {
	path = preparePath(path)
	// Fast path: if we can tell whether path is a directory or file, stop with success or error.
	dir, err := os.Stat(path)
	if err == nil {
		if dir.IsDir() {
			return nil
		}
		return &os.PathError{
			Op:   "mkdir",
			Path: path,
			Err:  syscall.ENOTDIR,
		}
	}

	// Slow path: make sure parent exists and then call Mkdir for path.
	i := len(path)
	for i > 0 && os.IsPathSeparator(path[i-1]) { // Skip trailing path separator.
		i--
	}

	j := i
	for j > 0 && !os.IsPathSeparator(path[j-1]) { // Scan backward over element.
		j--
	}

	if j > 1 {
		// Create parent
		parent := path[0 : j-1]
		if parent != filepath.VolumeName(parent) {
			err = mkdirAll(parent, perm)
			if err != nil {
				return err
			}
		}
	}

	// Parent now exists; invoke Mkdir and use its result.
	err = os.Mkdir(path, perm)
	if err != nil {
		// Handle arguments like "foo/." by
		// double-checking that directory doesn't exist.
		dir, err1 := os.Lstat(path)
		if err1 == nil && dir.IsDir() {
			return nil
		}
		return err
	}
	return nil
}
开发者ID:hackintoshrao,项目名称:minio,代码行数:55,代码来源:posix-utils_windows.go


注:本文中的os.IsPathSeparator函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。