當前位置: 首頁>>代碼示例>>Golang>>正文


Golang os.Symlink函數代碼示例

本文整理匯總了Golang中os.Symlink函數的典型用法代碼示例。如果您正苦於以下問題:Golang Symlink函數的具體用法?Golang Symlink怎麽用?Golang Symlink使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了Symlink函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: TestRunDeviceSymlink

// TestRunDeviceSymlink checks run with device that follows symlink (#13840)
func (s *DockerSuite) TestRunDeviceSymlink(c *check.C) {
	testRequires(c, DaemonIsLinux, NotUserNamespace, NotArm, SameHostDaemon)
	if _, err := os.Stat("/dev/zero"); err != nil {
		c.Skip("Host does not have /dev/zero")
	}

	// Create a temporary directory to create symlink
	tmpDir, err := ioutil.TempDir("", "docker_device_follow_symlink_tests")
	c.Assert(err, checker.IsNil)

	defer os.RemoveAll(tmpDir)

	// Create a symbolic link to /dev/zero
	symZero := filepath.Join(tmpDir, "zero")
	err = os.Symlink("/dev/zero", symZero)
	c.Assert(err, checker.IsNil)

	// Create a temporary file "temp" inside tmpDir, write some data to "tmpDir/temp",
	// then create a symlink "tmpDir/file" to the temporary file "tmpDir/temp".
	tmpFile := filepath.Join(tmpDir, "temp")
	err = ioutil.WriteFile(tmpFile, []byte("temp"), 0666)
	c.Assert(err, checker.IsNil)
	symFile := filepath.Join(tmpDir, "file")
	err = os.Symlink(tmpFile, symFile)
	c.Assert(err, checker.IsNil)

	// md5sum of 'dd if=/dev/zero bs=4K count=8' is bb7df04e1b0a2570657527a7e108ae23
	out, _ := dockerCmd(c, "run", "--device", symZero+":/dev/symzero", "busybox", "sh", "-c", "dd if=/dev/symzero bs=4K count=8 | md5sum")
	c.Assert(strings.Trim(out, "\r\n"), checker.Contains, "bb7df04e1b0a2570657527a7e108ae23", check.Commentf("expected output bb7df04e1b0a2570657527a7e108ae23"))

	// symlink "tmpDir/file" to a file "tmpDir/temp" will result in an error as it is not a device.
	out, _, err = dockerCmdWithError("run", "--device", symFile+":/dev/symzero", "busybox", "sh", "-c", "dd if=/dev/symzero bs=4K count=8 | md5sum")
	c.Assert(err, check.NotNil)
	c.Assert(strings.Trim(out, "\r\n"), checker.Contains, "not a device node", check.Commentf("expected output 'not a device node'"))
}
開發者ID:ailispaw,項目名稱:docker,代碼行數:36,代碼來源:docker_cli_run_unix_test.go

示例2: main

func main() {

	flag.Parse()
	if randomDelay != nil && *randomDelay != "" {
		if dur, err := time.ParseDuration(*randomDelay); err != nil {
			log.Fatalf("Cannot parse duration '%s': %v", randomDelay, err)
		} else {
			rdur := time.Duration(randInt64(dur.Nanoseconds()))
			log.Printf("Waiting for %s of %s", rdur.String(), dur.String())
			time.Sleep(rdur)
		}
	}

	log.Printf("Updating geoip database at %s from %s via %s", *directory, *sourceHost, *protocol)
	if err := getClientIp(); err != nil {
		log.Fatalf("Can't get client IP: %v", err)
	}
	for _, p := range strings.Split(*productIds, ",") {
		getProduct(p)
	}
	if *dolinks {
		log.Printf("Making legacy links in %s", *directory)
		os.Symlink(path.Join(*directory, "GeoLiteCity.dat"), path.Join(*directory, "GeoIPCity.dat"))
		os.Symlink(path.Join(*directory, "GeoLiteCountry.dat"), path.Join(*directory, "GeoIP.dat"))
	}
	log.Printf("Done\n")
}
開發者ID:abligh,項目名稱:geoipupdate,代碼行數:27,代碼來源:geoipupdate.go

示例3: TestCreationChecks

func TestCreationChecks(t *testing.T) {
	wd, clean := setup(t)
	defer clean()

	err := os.Mkdir(wd+"/store/foo", 0755)
	CheckSuccess(err)
	os.Symlink(wd+"/ro", wd+"/store/foo/READONLY")
	CheckSuccess(err)

	err = os.Mkdir(wd+"/store/ws2", 0755)
	CheckSuccess(err)
	os.Symlink(wd+"/ro", wd+"/store/ws2/READONLY")
	CheckSuccess(err)

	err = os.Symlink(wd+"/store/foo", wd+"/mnt/config/bar")
	CheckSuccess(err)

	err = os.Symlink(wd+"/store/foo", wd+"/mnt/config/foo")
	code := fuse.ToStatus(err)
	if code != fuse.EBUSY {
		t.Error("Should return EBUSY", err)
	}

	err = os.Symlink(wd+"/store/ws2", wd+"/mnt/config/config")
	code = fuse.ToStatus(err)
	if code != fuse.EINVAL {
		t.Error("Should return EINVAL", err)
	}
}
開發者ID:crazy2be,項目名稱:go-fuse,代碼行數:29,代碼來源:autounion_test.go

示例4: TestExpandToWithBadLink

func (s *BundleSuite) TestExpandToWithBadLink(c *gc.C) {
	charmDir := charmtesting.Charms.ClonedDirPath(c.MkDir(), "dummy")
	badLink := filepath.Join(charmDir, "hooks", "badlink")

	// Symlink targeting a path outside of the charm.
	err := os.Symlink("../../target", badLink)
	c.Assert(err, gc.IsNil)

	bundle := extBundleDir(c, charmDir)
	c.Assert(err, gc.IsNil)

	path := filepath.Join(c.MkDir(), "charm")
	err = bundle.ExpandTo(path)
	c.Assert(err, gc.ErrorMatches, `cannot extract "hooks/badlink": symlink "../../target" leads out of scope`)

	// Symlink targeting an absolute path.
	os.Remove(badLink)
	err = os.Symlink("/target", badLink)
	c.Assert(err, gc.IsNil)

	bundle = extBundleDir(c, charmDir)
	c.Assert(err, gc.IsNil)

	path = filepath.Join(c.MkDir(), "charm")
	err = bundle.ExpandTo(path)
	c.Assert(err, gc.ErrorMatches, `cannot extract "hooks/badlink": symlink "/target" is absolute`)
}
開發者ID:bz2,項目名稱:charm,代碼行數:27,代碼來源:bundle_test.go

示例5: TestCanonicalCircular

func TestCanonicalCircular(t *testing.T) {
	tmp1, err := tmpfile("circular")
	if err != nil {
		t.Fatal(err)
	}
	tmp2, err := tmpfile("circular")
	if err != nil {
		t.Fatal(nonil(err, os.Remove(tmp1)))
	}
	defer removeall(tmp1, tmp2)
	// Symlink tmp1 -> tmp2.
	if err = nonil(os.Remove(tmp1), os.Symlink(tmp2, tmp1)); err != nil {
		t.Fatal(err)
	}
	// Symlnik tmp2 -> tmp1.
	if err = nonil(os.Remove(tmp2), os.Symlink(tmp1, tmp2)); err != nil {
		t.Fatal(err)
	}
	if _, err = canonical(tmp1); err == nil {
		t.Fatalf("want canonical(%q)!=nil", tmp1)
	}
	if _, ok := err.(*os.PathError); !ok {
		t.Fatalf("want canonical(%q)=os.PathError; got %T", tmp1, err)
	}
}
開發者ID:kaocs,項目名稱:notify,代碼行數:25,代碼來源:util_unix_test.go

示例6: TestDetectSymlinkedDirectories

func TestDetectSymlinkedDirectories(t *testing.T) {
	wd, clean := setup(t)
	defer clean()

	err := os.Mkdir(wd+"/backing1", 0755)
	if err != nil {
		t.Fatalf("Mkdir failed: %v", err)
	}

	err = os.Symlink(wd+"/ro", wd+"/backing1/READONLY")
	if err != nil {
		t.Fatalf("Symlink failed: %v", err)
	}

	err = os.Symlink(wd+"/backing1", wd+"/store/backing1")
	if err != nil {
		t.Fatalf("Symlink failed: %v", err)
	}

	scan := wd + "/mnt/config/" + _SCAN_CONFIG
	err = ioutil.WriteFile(scan, []byte("something"), 0644)
	if err != nil {
		t.Error("error writing:", err)
	}

	_, err = os.Lstat(wd + "/mnt/backing1")
	if err != nil {
		t.Fatalf("Lstat failed: %v", err)
	}
}
開發者ID:y-okubo,項目名稱:go-fuse,代碼行數:30,代碼來源:autounion_test.go

示例7: Symlink

func (fs *FS) Symlink(target string, linkName string, context *fuse.Context) (code fuse.Status) {
	cryptfs.Debug.Printf("Symlink(\"%s\", \"%s\")", target, linkName)
	if fs.isFiltered(linkName) {
		return fuse.EPERM
	}
	cPath, err := fs.getBackingPath(linkName)
	if err != nil {
		return fuse.ToStatus(err)
	}
	// Old filesystem: symlinks are encrypted like paths (CBC)
	if !fs.args.DirIV {
		cTarget, err := fs.encryptPath(target)
		if err != nil {
			cryptfs.Warn.Printf("Symlink: BUG: we should not get an error here: %v", err)
			return fuse.ToStatus(err)
		}
		err = os.Symlink(cTarget, cPath)
		return fuse.ToStatus(err)
	}
	// Since gocryptfs v0.5 symlinks are encrypted like file contents (GCM)
	cBinTarget := fs.CryptFS.EncryptBlock([]byte(target), 0, nil)
	cTarget := base64.URLEncoding.EncodeToString(cBinTarget)

	err = os.Symlink(cTarget, cPath)
	cryptfs.Debug.Printf("Symlink: os.Symlink(%s, %s) = %v", cTarget, cPath, err)
	return fuse.ToStatus(err)
}
開發者ID:cautio,項目名稱:gocryptfs,代碼行數:27,代碼來源:fs.go

示例8: makeTestDir

func makeTestDir(t *testing.T) string {
	cwd, err := os.Getwd()
	TestExpectSuccess(t, err)
	AddTestFinalizer(func() {
		TestExpectSuccess(t, os.Chdir(cwd))
	})
	dir := TempDir(t)
	TestExpectSuccess(t, os.Chdir(dir))
	mode := os.FileMode(0755)
	os.Mkdir(cwd, mode) //Don't care about return value.  For some reason CWD is not created by go test on all systems.
	TestExpectSuccess(t, os.Mkdir("a", mode))
	TestExpectSuccess(t, os.Mkdir("a/b", mode))
	TestExpectSuccess(t, os.Mkdir("a/b/c", mode))
	TestExpectSuccess(t, os.Mkdir("a/b/c/d", mode))
	TestExpectSuccess(t, os.Mkdir("a/b/i", mode))
	TestExpectSuccess(t, os.Mkdir("a/b/i/j", mode))
	TestExpectSuccess(t, ioutil.WriteFile("a/b/c/d/e", []byte{}, mode))
	TestExpectSuccess(t, ioutil.WriteFile("a/b/c/f", []byte{}, mode))
	TestExpectSuccess(t, ioutil.WriteFile("a/b/g", []byte{}, mode))
	TestExpectSuccess(t, ioutil.WriteFile("a/b/i/j/k", []byte{}, mode))
	TestExpectSuccess(t, os.Symlink("/bin/bash", "a/b/bash"))
	TestExpectSuccess(t, os.Symlink("../i", "a/b/c/l"))
	TestExpectSuccess(t, os.Symlink("g", "a/b/h"))
	TestExpectSuccess(t, os.Symlink("k", "a/b/i/j/l"))
	TestExpectSuccess(t, os.Symlink("../../g", "a/b/i/j/m"))
	return dir
}
開發者ID:liyangdal,項目名稱:kurma,代碼行數:27,代碼來源:tar_test.go

示例9: Symlink

func Symlink(oldname, newname string, overwrite bool) error {
	err := os.Symlink(oldname, newname)
	if err == nil {
		return nil // Success
	}
	if !os.IsExist(err) {
		return err // Failure
	}
	// Failure, file exists
	symbolic := false
	{
		stat, err := os.Lstat(newname)
		if err != nil {
			return err
		}
		symbolic = stat.Mode()&os.ModeSymlink != 0
	}
	if !symbolic {
		return err
	}
	if !overwrite {
		return nil
	}
	err = os.Remove(newname)
	if err != nil {
		return err
	}
	return os.Symlink(oldname, newname)
}
開發者ID:robertkrimen,項目名稱:dogbag,代碼行數:29,代碼來源:kilt.go

示例10: symlinkDirs

func symlinkDirs() (err error) {
	err = os.RemoveAll(SITE_DIR + "/_posts")
	if err != nil {
		return err
	}
	err = os.RemoveAll(SITE_DIR + "/uploads")
	if err != nil {
		return err
	}

	postsAbs, err := filepath.Abs(POSTS_REPO_DIR + "/posts")
	if err != nil {
		return errors.New("Unable to get absolute path of posts directory")
	}

	uploadsAbs, err := filepath.Abs(POSTS_REPO_DIR + "/uploads")
	if err != nil {
		return errors.New("Unable to get absolute path of uploads directory")
	}
	err = os.Symlink(postsAbs, SITE_DIR+"/_posts")
	if err != nil {
		return err
	}
	err = os.Symlink(uploadsAbs, SITE_DIR+"/uploads")
	if err != nil {
		return err
	}
	return nil
}
開發者ID:davinche,項目名稱:Jekyll-Build-Server,代碼行數:29,代碼來源:main.go

示例11: TestGeneralCase

func TestGeneralCase(t *testing.T) {
	fn := new(WalkFn)
	root := makeEmptyRoot(t)
	defer os.RemoveAll(root)

	ensure.Nil(t, os.MkdirAll(filepath.Join(root, "a", "a1"), 0755))
	createFile(t, filepath.Join(root, "a", "a1", "1"))

	ensure.Nil(t, os.Mkdir(filepath.Join(root, "b"), 0755))
	createFile(t, filepath.Join(root, "b", "2"))

	ensure.Nil(t, os.Symlink(filepath.Join(root, "b"), filepath.Join(root, "a", "a1", "a2")))

	ensure.Nil(t, os.Symlink(filepath.Join(root, "a", "a1", "1"), filepath.Join(root, "top")))

	ensure.Nil(t, Walk(root, fn.walkFn))

	ensure.DeepEqual(t,
		fn.List,
		[]string{
			root,
			filepath.Join(root, "a"),
			filepath.Join(root, "a", "a1"),
			filepath.Join(root, "a", "a1", "1"),
			filepath.Join(root, "a", "a1", "a2"),
			filepath.Join(root, "a", "a1", "a2", "2"),
			filepath.Join(root, "b"),
			filepath.Join(root, "b", "2"),
			filepath.Join(root, "top"),
		},
	)
}
開發者ID:postfix,項目名稱:symwalk,代碼行數:32,代碼來源:walk_test.go

示例12: TestExpandToWithBadLink

func (s *BundleSuite) TestExpandToWithBadLink(c *C) {
	charmDir := testing.Charms.ClonedDirPath(c.MkDir(), "dummy")
	badLink := filepath.Join(charmDir, "hooks", "badlink")

	// Symlink targeting a path outside of the charm.
	err := os.Symlink("../../target", badLink)
	c.Assert(err, IsNil)

	bundle, err := charm.ReadBundle(extBundleDir(c, charmDir))
	c.Assert(err, IsNil)

	path := filepath.Join(c.MkDir(), "charm")
	err = bundle.ExpandTo(path)
	c.Assert(err, ErrorMatches, `symlink "hooks/badlink" links out of charm: "../../target"`)

	// Symlink targeting an absolute path.
	os.Remove(badLink)
	err = os.Symlink("/target", badLink)
	c.Assert(err, IsNil)

	bundle, err = charm.ReadBundle(extBundleDir(c, charmDir))
	c.Assert(err, IsNil)

	path = filepath.Join(c.MkDir(), "charm")
	err = bundle.ExpandTo(path)
	c.Assert(err, ErrorMatches, `symlink "hooks/badlink" is absolute: "/target"`)
}
開發者ID:rif,項目名稱:golang-stuff,代碼行數:27,代碼來源:bundle_test.go

示例13: CopySource

// CopySource copies the source into the HostPath
func (p *Runner) CopySource() error {
	timer := util.NewTimer()
	f := p.formatter

	err := os.MkdirAll(p.options.HostPath(), 0755)
	if err != nil {
		return err
	}

	// Link the path to BuildPath("latest") for easy access
	err = os.RemoveAll(p.options.BuildPath("latest"))
	if err != nil {
		return err
	}
	err = os.Symlink(p.options.HostPath(), p.options.BuildPath("latest"))
	if err != nil {
		return err
	}

	err = os.Symlink(p.ProjectDir(), p.options.HostPath("source"))
	if err != nil {
		return err
	}
	if p.options.Verbose {
		p.logger.Printf(f.Success("Source -> Staging Area", timer.String()))
	}
	return nil
}
開發者ID:sgoings,項目名稱:wercker,代碼行數:29,代碼來源:runner.go

示例14: manageSymlinks

// (Un)install symbolic runlevel links
func (s *sysv) manageSymlinks(confPath string, install bool) error {
	var cmd *exec.Cmd

	if _, err := exec.LookPath("chkconfig"); err == nil {
		if install {
			cmd = exec.Command("chkconfig", "--add", s.Name)
		} else {
			cmd = exec.Command("chkconfig", "--del", s.Name)
		}
	} else if _, err := exec.LookPath("update-rc.d"); err == nil {
		if install {
			cmd = exec.Command("update-rc.d", s.Name, "defaults")
		} else {
			cmd = exec.Command("update-rc.d", "-f", s.Name, "remove")
		}
	}

	if cmd != nil {
		if err := cmd.Run(); err != nil {
			return fmt.Errorf("Failed to run %q: %s", strings.Join(cmd.Args, " "), err)
		}
	} else {
		/* Manually install/remove symlinks */
		var base = "/etc"

		/* Debian/ubuntu use /etc/rc[0-6].d; RedHat uses /etc/rc.d/rc[0-6].d */
		if _, err := os.Stat("/etc/rc.d/"); err == nil {
			base = "/etc/rc.d"
		} else if _, err := os.Stat(base + "/rc0.d"); os.IsNotExist(err) {
			fmt.Fprintf(os.Stderr, "FIXME: no suitable rc.d directory found in /etc")
			os.Exit(1)
		}

		for _, i := range strings.Split(defaultStartLevels, "") {
			path := fmt.Sprintf("%s/rc%s.d/S%s%s", base, i, defaultStartPriority, s.Name)
			if install {
				if err := os.Symlink(confPath, path); err != nil {
					return fmt.Errorf("Failed to create startup link %s: %s", path, err)
				}
			} else {
				if err := os.Remove(path); err != nil {
					return fmt.Errorf("Failed to remove startup link %s: %s", path, err)
				}
			}
		}
		for _, i := range strings.Split(defaultStopLevels, "") {
			path := fmt.Sprintf("%s/rc%s.d/K%s%s", base, i, defaultStopPriority, s.Name)
			if install {
				if err := os.Symlink(confPath, path); err != nil {
					return fmt.Errorf("Failed to create shutdown link %s: %s", path, err)
				}
			} else {
				if err := os.Remove(path); err != nil {
					return fmt.Errorf("Failed to remove shutdown link %s: %s", path, err)
				}
			}
		}
	}
	return nil
}
開發者ID:grrtrr,項目名稱:golang-kardianos-service,代碼行數:61,代碼來源:service_sysv_linux.go

示例15: Add

// Add creates a symlink in the SRCLIBPATH so that the toolchain in dir is
// available at the toolchainPath.
func Add(dir, toolchainPath string, opt *AddOpt) error {
	if opt == nil {
		opt = &AddOpt{}
	}
	if !opt.Force {
		if _, err := Lookup(toolchainPath); !os.IsNotExist(err) {
			return fmt.Errorf("a toolchain already exists at toolchain path %q", toolchainPath)
		}
	}

	absDir, err := filepath.Abs(dir)
	if err != nil {
		return err
	}

	srclibpathEntry := strings.SplitN(srclib.Path, ":", 2)[0]
	targetDir := filepath.Join(srclibpathEntry, toolchainPath)

	if err := os.MkdirAll(filepath.Dir(targetDir), 0700); err != nil {
		return err
	}

	if !opt.Force {
		return os.Symlink(absDir, targetDir)
	}
	// Force install the toolchain by removing the directory if
	// the symlink fails, and then try the symlink again.
	if err := os.Symlink(absDir, targetDir); err != nil {
		if err := os.RemoveAll(targetDir); err != nil {
			return err
		}
		return os.Symlink(absDir, targetDir)
	}
	return nil
}
開發者ID:sombr,項目名稱:ccat,代碼行數:37,代碼來源:add.go


注:本文中的os.Symlink函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。