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


Golang filepath.EvalSymlinks函数代码示例

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


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

示例1: shouldRead

func (f *Filesystem) shouldRead(filePath string, fi os.FileInfo) (bool, error) {
	if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
		link, err := filepath.EvalSymlinks(filePath)
		if err != nil {
			jww.ERROR.Printf("Cannot read symbolic link '%s', error was: %s", filePath, err)
			return false, nil
		}
		linkfi, err := os.Stat(link)
		if err != nil {
			jww.ERROR.Printf("Cannot stat '%s', error was: %s", link, err)
			return false, nil
		}
		if !linkfi.Mode().IsRegular() {
			jww.ERROR.Printf("Symbolic links for directories not supported, skipping '%s'", filePath)
		}
		return false, nil
	}

	if fi.IsDir() {
		if f.avoid(filePath) || isNonProcessablePath(filePath) {
			return false, filepath.SkipDir
		}
		return false, nil
	}

	if isNonProcessablePath(filePath) {
		return false, nil
	}
	return true, nil
}
开发者ID:yanwushuang,项目名称:pango,代码行数:30,代码来源:filesystem.go

示例2: Symlink

func (m *MetadataEncoder) Symlink(path string, fi os.FileInfo) error {
	_, err := m.e.Encode(TypeSymlink)
	if err != nil {
		return err
	}

	var link string
	if filepath.IsAbs(path) {
		link, err = filepath.EvalSymlinks(path)
		if err != nil {
			return err
		}
	} else {
		link, err = filepath.EvalSymlinks(path)
		if err != nil {
			return err
		}
		link, err = filepath.Rel(path, link)
		if err != nil {
			return err
		}
	}

	_, err = m.e.Encode(Symlink{
		Name: path,
		Link: link,
	})
	if err != nil {
		return err
	}

	return nil
}
开发者ID:jcvernaleo,项目名称:acdb,代码行数:33,代码来源:metadata.go

示例3: TestLinkDoUndoCurrentSymlink

func (s *linkSuite) TestLinkDoUndoCurrentSymlink(c *C) {
	const yaml = `name: hello
version: 1.0
`

	info := snaptest.MockSnap(c, yaml, &snap.SideInfo{Revision: snap.R(11)})

	err := s.be.LinkSnap(info)
	c.Assert(err, IsNil)

	mountDir := info.MountDir()
	dataDir := info.DataDir()
	currentActiveSymlink := filepath.Join(mountDir, "..", "current")
	currentActiveDir, err := filepath.EvalSymlinks(currentActiveSymlink)
	c.Assert(err, IsNil)
	c.Assert(currentActiveDir, Equals, mountDir)

	currentDataSymlink := filepath.Join(dataDir, "..", "current")
	currentDataDir, err := filepath.EvalSymlinks(currentDataSymlink)
	c.Assert(err, IsNil)
	c.Assert(currentDataDir, Equals, dataDir)

	// undo will remove the symlinks
	err = s.be.UnlinkSnap(info, &s.nullProgress)
	c.Assert(err, IsNil)

	c.Check(osutil.FileExists(currentActiveSymlink), Equals, false)
	c.Check(osutil.FileExists(currentDataSymlink), Equals, false)

}
开发者ID:niemeyer,项目名称:snapd,代码行数:30,代码来源:link_test.go

示例4: StoreName

func (p ExpBase) StoreName() map[string]string {
	names := make(map[string]string)
	names["exp"], _ = filepath.EvalSymlinks(p.ExpDir())
	names["data"], _ = filepath.EvalSymlinks(p.DataDir())
	names["param"], _ = filepath.EvalSymlinks(p.ParamDir())
	return names
}
开发者ID:rosrad,项目名称:grid,代码行数:7,代码来源:path-base.go

示例5: GetLogReader

// GetLogReader returns a reader for the specified filename. In
// restricted mode, the filename must be the base name of a file in
// this process's log directory (this is safe for cases when the
// filename comes from external sources, such as the admin UI via
// HTTP). In unrestricted mode any path is allowed, with the added
// feature that relative paths will be searched in both the current
// directory and this process's log directory.
func GetLogReader(filename string, restricted bool) (io.ReadCloser, error) {
	if !restricted {
		if resolved, err := filepath.EvalSymlinks(filename); err == nil {
			if verifyFile(resolved) == nil {
				return os.Open(resolved)
			}
		}
	}
	// Verify there are no path separators in a restricted-mode pathname.
	if restricted && filepath.Base(filename) != filename {
		return nil, fmt.Errorf("pathnames must be basenames only: %s", filename)
	}
	if !filepath.IsAbs(filename) {
		filename = filepath.Join(*logDir, filename)
	}
	if !restricted {
		var err error
		filename, err = filepath.EvalSymlinks(filename)
		if err != nil {
			return nil, err
		}
	}
	if err := verifyFile(filename); err != nil {
		return nil, err
	}
	return os.Open(filename)
}
开发者ID:kaustubhkurve,项目名称:cockroach,代码行数:34,代码来源:file.go

示例6: StoreName

func (conf *PathConf) StoreName() map[string]string {
	names := make(map[string]string)
	names["exp"], _ = filepath.EvalSymlinks(conf.Exp)
	names["data"], _ = filepath.EvalSymlinks(conf.FeatData)
	names["param"], _ = filepath.EvalSymlinks(conf.FeatParam)
	return names
}
开发者ID:postfix,项目名称:go-kaldi,代码行数:7,代码来源:conf.go

示例7: TestEndToEndSpecialEntries

func TestEndToEndSpecialEntries(t *testing.T) {
	tc := NewTestCase(t)
	defer tc.Clean()

	readlink, _ := filepath.EvalSymlinks(tc.FindBin("readlink"))
	req := WorkRequest{
		Binary: readlink,
		Argv:   []string{"readlink", "proc/self/exe"},
		Env:    testEnv(),
		Dir:    "/",
	}
	rep := tc.Run(req)

	if rep.Exit.ExitStatus() != 0 {
		t.Fatalf("readlink should exit cleanly. Rep %v", rep)
	}

	out, _ := filepath.EvalSymlinks(strings.TrimRight(rep.Stdout, "\n"))
	if out != readlink {
		t.Errorf("proc/self/exe point to wrong location: got %q, expect %q", out, readlink)
	}

	req = WorkRequest{
		Binary: tc.FindBin("ls"),
		Argv:   []string{"ls", "proc/misc"},
		Env:    testEnv(),
		Dir:    "/",
	}
	rep = tc.Run(req)
	if rep.Exit.ExitStatus() == 0 {
		t.Fatalf("ls should have failed", rep)
	}
}
开发者ID:lht,项目名称:termite,代码行数:33,代码来源:worker_test.go

示例8: TestSetActive

func (s *SnapTestSuite) TestSetActive(c *C) {
	makeTwoTestSnaps(c, snap.TypeApp)

	path, err := filepath.EvalSymlinks(filepath.Join(dirs.SnapSnapsDir, fooComposedName, "current"))
	c.Assert(err, IsNil)
	c.Check(path, Equals, filepath.Join(dirs.SnapSnapsDir, fooComposedName, "2.0"))

	path, err = filepath.EvalSymlinks(filepath.Join(dirs.SnapDataDir, fooComposedName, "current"))
	c.Assert(err, IsNil)
	c.Check(path, Equals, filepath.Join(dirs.SnapDataDir, fooComposedName, "2.0"))

	meter := &MockProgressMeter{}

	// setActive has some ugly print
	devnull, err := os.Open(os.DevNull)
	c.Assert(err, IsNil)
	oldStdout := os.Stdout
	os.Stdout = devnull
	defer func() {
		os.Stdout = oldStdout
	}()

	err = makeSnapActiveByNameAndVersion("foo", "1.0", meter)
	c.Assert(err, IsNil)
	path, _ = filepath.EvalSymlinks(filepath.Join(dirs.SnapSnapsDir, fooComposedName, "current"))
	c.Check(path, Equals, filepath.Join(dirs.SnapSnapsDir, fooComposedName, "1.0"))
	path, _ = filepath.EvalSymlinks(filepath.Join(dirs.SnapDataDir, fooComposedName, "current"))
	c.Check(path, Equals, filepath.Join(dirs.SnapDataDir, fooComposedName, "1.0"))
}
开发者ID:robert-ancell,项目名称:snapd,代码行数:29,代码来源:set_test.go

示例9: Symlink

func (fs osFileSystem) Symlink(oldPath, newPath string) (err error) {
	fs.logger.Debug(fs.logTag, "Symlinking oldPath %s with newPath %s", oldPath, newPath)

	actualOldPath, err := filepath.EvalSymlinks(oldPath)
	if err != nil {
		err = bosherr.WrapError(err, "Evaluating symlinks for %s", oldPath)
		return
	}

	existingTargetedPath, err := filepath.EvalSymlinks(newPath)
	if err == nil {
		if existingTargetedPath == actualOldPath {
			return
		}

		err = os.Remove(newPath)
		if err != nil {
			err = bosherr.WrapError(err, "Failed to delete symlimk at %s", newPath)
			return
		}
	}

	containingDir := filepath.Dir(newPath)
	if !fs.FileExists(containingDir) {
		fs.MkdirAll(containingDir, os.FileMode(0700))
	}

	return os.Symlink(oldPath, newPath)
}
开发者ID:Jane4PKU,项目名称:bosh,代码行数:29,代码来源:os_file_system.go

示例10: TestCanGetPath

func TestCanGetPath(t *testing.T) {
	t.Parallel()
	base, rm := TempDir()
	defer rm()

	s := New(0, NOW, time.Second/10, CF_FILEEVENTS, base)
	s.Start()
	defer s.Close()

	withNew(base, func(dummyfile string) {
		select {
		case events := <-s.Chan:
			events = getEvents(base, events)
			if len(events) != 1 {
				t.Errorf("Expected 1 event, got %#v.", len(events))
			}
			fullpath, _ := filepath.Abs(dummyfile)
			fullpath, _ = filepath.EvalSymlinks(fullpath)
			evPath, _ := filepath.EvalSymlinks(events[0].Path)
			if evPath != fullpath {
				t.Errorf("Expected %#v to equal %#v.", evPath, fullpath)
			}
		case <-time.After(time.Minute):
			t.Errorf("timed out")
		}
	})
}
开发者ID:zchee,项目名称:go.fsevents,代码行数:27,代码来源:watcher_test.go

示例11: ResolveHostSourcePath

// ResolveHostSourcePath decides real path need to be copied with parameters such as
// whether to follow symbol link or not, if followLink is true, resolvedPath will return
// link target of any symbol link file, else it will only resolve symlink of directory
// but return symbol link file itself without resolving.
func ResolveHostSourcePath(path string, followLink bool) (resolvedPath, rebaseName string, err error) {
	if followLink {
		resolvedPath, err = filepath.EvalSymlinks(path)
		if err != nil {
			return
		}

		resolvedPath, rebaseName = GetRebaseName(path, resolvedPath)
	} else {
		dirPath, basePath := filepath.Split(path)

		// if not follow symbol link, then resolve symbol link of parent dir
		var resolvedDirPath string
		resolvedDirPath, err = filepath.EvalSymlinks(dirPath)
		if err != nil {
			return
		}
		// resolvedDirPath will have been cleaned (no trailing path separators) so
		// we can manually join it with the base path element.
		resolvedPath = resolvedDirPath + string(filepath.Separator) + basePath
		if hasTrailingPathSeparator(path) && filepath.Base(path) != filepath.Base(resolvedPath) {
			rebaseName = filepath.Base(path)
		}
	}
	return resolvedPath, rebaseName, nil
}
开发者ID:Chandra-TechPassionate,项目名称:docker,代码行数:30,代码来源:copy.go

示例12: shouldTraverse

// shouldTraverse reports whether the symlink fi should, found in dir,
// should be followed.  It makes sure symlinks were never visited
// before to avoid symlink loops.
func shouldTraverse(dir string, fi os.FileInfo) bool {
	path := filepath.Join(dir, fi.Name())
	target, err := filepath.EvalSymlinks(path)
	if err != nil {
		if !os.IsNotExist(err) {
			fmt.Fprintln(os.Stderr, err)
		}
		return false
	}
	ts, err := os.Stat(target)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return false
	}
	if !ts.IsDir() {
		return false
	}

	realParent, err := filepath.EvalSymlinks(dir)
	if err != nil {
		fmt.Fprint(os.Stderr, err)
		return false
	}
	realPath := filepath.Join(realParent, fi.Name())
	visitedSymlinks.Lock()
	defer visitedSymlinks.Unlock()
	if visitedSymlinks.m == nil {
		visitedSymlinks.m = make(map[string]struct{})
	}
	if _, ok := visitedSymlinks.m[realPath]; ok {
		return false
	}
	visitedSymlinks.m[realPath] = struct{}{}
	return true
}
开发者ID:zeebo,项目名称:tools,代码行数:38,代码来源:fix.go

示例13: TestEvalSymlinksCanonicalNames

// TestEvalSymlinksCanonicalNames verify that EvalSymlinks
// returns "canonical" path names on windows.
func TestEvalSymlinksCanonicalNames(t *testing.T) {
	tmp, err := ioutil.TempDir("", "evalsymlinkcanonical")
	if err != nil {
		t.Fatal("creating temp dir:", err)
	}
	defer os.RemoveAll(tmp)

	// ioutil.TempDir might return "non-canonical" name.
	cTmpName, err := filepath.EvalSymlinks(tmp)
	if err != nil {
		t.Errorf("EvalSymlinks(%q) error: %v", tmp, err)
	}

	dirs := []string{
		"test",
		"test/dir",
		"testing_long_dir",
		"TEST2",
	}

	for _, d := range dirs {
		dir := filepath.Join(cTmpName, d)
		err := os.Mkdir(dir, 0755)
		if err != nil {
			t.Fatal(err)
		}
		cname, err := filepath.EvalSymlinks(dir)
		if err != nil {
			t.Errorf("EvalSymlinks(%q) error: %v", dir, err)
			continue
		}
		if dir != cname {
			t.Errorf("EvalSymlinks(%q) returns %q, but should return %q", dir, cname, dir)
			continue
		}
		// test non-canonical names
		test := strings.ToUpper(dir)
		p, err := filepath.EvalSymlinks(test)
		if err != nil {
			t.Errorf("EvalSymlinks(%q) error: %v", test, err)
			continue
		}
		if p != cname {
			t.Errorf("EvalSymlinks(%q) returns %q, but should return %q", test, p, cname)
			continue
		}
		// another test
		test = strings.ToLower(dir)
		p, err = filepath.EvalSymlinks(test)
		if err != nil {
			t.Errorf("EvalSymlinks(%q) error: %v", test, err)
			continue
		}
		if p != cname {
			t.Errorf("EvalSymlinks(%q) returns %q, but should return %q", test, p, cname)
			continue
		}
	}
}
开发者ID:danny8002,项目名称:go,代码行数:61,代码来源:path_windows_test.go

示例14: AppConfigPath

func AppConfigPath(cmd *cobra.Command) (string, error) {
	if configFile, err := cmd.Flags().GetString("filename"); err == nil && configFile != "" {
		fmt.Printf("using config file %s\n", configFile)
		return filepath.EvalSymlinks(configFile)
	}

	return filepath.EvalSymlinks(".ranch.yaml")
}
开发者ID:goodeggs,项目名称:platform,代码行数:8,代码来源:app.go

示例15: hwmonName

func (c *hwMonCollector) hwmonName(dir string) (string, error) {
	// generate a name for a sensor path

	// sensor numbering depends on the order of linux module loading and
	// is thus unstable.
	// However the path of the device has to be stable:
	// - /sys/devices/<bus>/<device>
	// Some hardware monitors have a "name" file that exports a human
	// readbale name that can be used.

	// human readable names would be bat0 or coretemp, while a path string
	// could be platform_applesmc.768

	// preference 1: construct a name based on device name, always unique

	devicePath, devErr := filepath.EvalSymlinks(path.Join(dir, "device"))
	if devErr == nil {
		devPathPrefix, devName := path.Split(devicePath)
		_, devType := path.Split(strings.TrimRight(devPathPrefix, "/"))

		cleanDevName := cleanMetricName(devName)
		cleanDevType := cleanMetricName(devType)

		if cleanDevType != "" && cleanDevName != "" {
			return cleanDevType + "_" + cleanDevName, nil
		}

		if cleanDevName != "" {
			return cleanDevName, nil
		}
	}

	// preference 2: is there a name file
	sysnameRaw, nameErr := ioutil.ReadFile(path.Join(dir, "name"))
	if nameErr == nil && string(sysnameRaw) != "" {
		cleanName := cleanMetricName(string(sysnameRaw))
		if cleanName != "" {
			return cleanName, nil
		}
	}

	// it looks bad, name and device don't provide enough information
	// return a hwmon[0-9]* name

	realDir, err := filepath.EvalSymlinks(dir)
	if err != nil {
		return "", err
	}

	// take the last path element, this will be hwmonX
	_, name := path.Split(realDir)
	cleanName := cleanMetricName(name)
	if cleanName != "" {
		return cleanName, nil
	}
	return "", errors.New("Could not derive a monitoring name for " + dir)
}
开发者ID:prometheus,项目名称:node_exporter,代码行数:57,代码来源:hwmon_linux.go


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