通过os包创建一个相对符号链接。

huangapple go评论76阅读模式
英文:

Creating a relative symbolic link through the os package

问题

我想使用Go语言的os包创建一个相对符号链接。

os包已经包含了函数os.SymLink(oldname, newname string)(https://golang.org/pkg/os/#Symlink),但它无法创建相对符号链接。

例如,如果我运行以下代码:

package main 

import (
	"io/ioutil"
	"os"
	"path/filepath"
)

func main() {
	path := "/tmp/rolfl/symexample"
	target := filepath.Join(path, "symtarget.txt")
	os.MkdirAll(path, 0755)
	ioutil.WriteFile(target, []byte("Hello\n"), 0644)
	symlink := filepath.Join(path, "symlink")
	os.Symlink(target, symlink)
}

它会在我的文件系统中创建以下内容:

$ ls -la /tmp/rolfl/symexample
total 12
drwxr-xr-x 2 rolf rolf 4096 Feb 21 15:21 .
drwxr-xr-x 3 rolf rolf 4096 Feb 21 15:21 ..
lrwxrwxrwx 1 rolf rolf   35 Feb 21 15:21 symlink -> /tmp/rolfl/symexample/symtarget.txt
-rw-r--r-- 1 rolf rolf    6 Feb 21 15:21 symtarget.txt

我该如何使用Go语言创建类似于以下的相对符号链接:

$ ln -s symtarget.txt symrelative
$ ls -la
total 12
drwxr-xr-x 2 rolf rolf 4096 Feb 21 15:23 .
drwxr-xr-x 3 rolf rolf 4096 Feb 21 15:21 ..
lrwxrwxrwx 1 rolf rolf   35 Feb 21 15:21 symlink -> /tmp/rolfl/symexample/symtarget.txt
lrwxrwxrwx 1 rolf rolf   13 Feb 21 15:23 symrelative -> symtarget.txt
-rw-r--r-- 1 rolf rolf    6 Feb 21 15:21 symtarget.txt

我想要的是像上面的symrelative一样的相对符号链接。

我需要使用os/exec吗?

cmd := exec.Command("ln", "-s", "symtarget.txt", "symlink")
cmd.Dir = "/tmp/rolfl/symexample"
cmd.CombinedOutput()
英文:

I would like to create a relative symbolic link in go using the os package.

os already contains the function: os.SymLink(oldname, newname string), but it cannot create relative symlinks.

For example, if I run the following:

package main 

import (
	"io/ioutil"
	"os"
	"path/filepath"
)

func main() {
	path := "/tmp/rolfl/symexample"
	target := filepath.Join(path, "symtarget.txt")
	os.MkdirAll(path, 0755)
	ioutil.WriteFile(target, []byte("Hello\n"), 0644)
	symlink := filepath.Join(path, "symlink")
	os.Symlink(target, symlink)
}

it creates the following in my filesystem:

$ ls -la /tmp/rolfl/symexample
total 12
drwxr-xr-x 2 rolf rolf 4096 Feb 21 15:21 .
drwxr-xr-x 3 rolf rolf 4096 Feb 21 15:21 ..
lrwxrwxrwx 1 rolf rolf   35 Feb 21 15:21 symlink -> /tmp/rolfl/symexample/symtarget.txt
-rw-r--r-- 1 rolf rolf    6 Feb 21 15:21 symtarget.txt

How can I use golang to create the relative symlink that looks like:

$ ln -s symtarget.txt symrelative
$ ls -la
total 12
drwxr-xr-x 2 rolf rolf 4096 Feb 21 15:23 .
drwxr-xr-x 3 rolf rolf 4096 Feb 21 15:21 ..
lrwxrwxrwx 1 rolf rolf   35 Feb 21 15:21 symlink -> /tmp/rolfl/symexample/symtarget.txt
lrwxrwxrwx 1 rolf rolf   13 Feb 21 15:23 symrelative -> symtarget.txt
-rw-r--r-- 1 rolf rolf    6 Feb 21 15:21 symtarget.txt

I want something that's like the symrelative above.

Do I have to resort to os/exec:

cmd := exec.Command("ln", "-s", "symtarget.txt", "symlink")
cmd.Dir = "/tmp/rolfl/symexample"
cmd.CombinedOutput()

答案1

得分: 19

在调用os.Symlink时,不要包含对symtarget.txt的绝对路径;只在写入文件时使用它:

package main 

import (
    "io/ioutil"
    "os"
    "path/filepath"
)

func main() {
    path := "/tmp/rolfl/symexample"
    target := "symtarget.txt"
    os.MkdirAll(path, 0755)
    ioutil.WriteFile(filepath.Join(path, "symtarget.txt"), []byte("Hello\n"), 0644)
    symlink := filepath.Join(path, "symlink")
    os.Symlink(target, symlink)
}
英文:

Don't include the absolute path to symtarget.txt when calling os.Symlink; only use it when writing to the file:

package main 

import (
    "io/ioutil"
    "os"
    "path/filepath"
)

func main() {
    path := "/tmp/rolfl/symexample"
    target := "symtarget.txt"
    os.MkdirAll(path, 0755)
    ioutil.WriteFile(filepath.Join(path, "symtarget.txt"), []byte("Hello\n"), 0644)
    symlink := filepath.Join(path, "symlink")
    os.Symlink(target, symlink)
}

huangapple
  • 本文由 发表于 2016年2月22日 04:25:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/35541589.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定