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

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

Creating a relative symbolic link through the os package

问题

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

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

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

  1. package main
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "path/filepath"
  6. )
  7. func main() {
  8. path := "/tmp/rolfl/symexample"
  9. target := filepath.Join(path, "symtarget.txt")
  10. os.MkdirAll(path, 0755)
  11. ioutil.WriteFile(target, []byte("Hello\n"), 0644)
  12. symlink := filepath.Join(path, "symlink")
  13. os.Symlink(target, symlink)
  14. }

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

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

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

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

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

我需要使用os/exec吗?

  1. cmd := exec.Command("ln", "-s", "symtarget.txt", "symlink")
  2. cmd.Dir = "/tmp/rolfl/symexample"
  3. 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:

  1. package main
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "path/filepath"
  6. )
  7. func main() {
  8. path := "/tmp/rolfl/symexample"
  9. target := filepath.Join(path, "symtarget.txt")
  10. os.MkdirAll(path, 0755)
  11. ioutil.WriteFile(target, []byte("Hello\n"), 0644)
  12. symlink := filepath.Join(path, "symlink")
  13. os.Symlink(target, symlink)
  14. }

it creates the following in my filesystem:

  1. $ ls -la /tmp/rolfl/symexample
  2. total 12
  3. drwxr-xr-x 2 rolf rolf 4096 Feb 21 15:21 .
  4. drwxr-xr-x 3 rolf rolf 4096 Feb 21 15:21 ..
  5. lrwxrwxrwx 1 rolf rolf 35 Feb 21 15:21 symlink -> /tmp/rolfl/symexample/symtarget.txt
  6. -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:

  1. $ ln -s symtarget.txt symrelative
  2. $ ls -la
  3. total 12
  4. drwxr-xr-x 2 rolf rolf 4096 Feb 21 15:23 .
  5. drwxr-xr-x 3 rolf rolf 4096 Feb 21 15:21 ..
  6. lrwxrwxrwx 1 rolf rolf 35 Feb 21 15:21 symlink -> /tmp/rolfl/symexample/symtarget.txt
  7. lrwxrwxrwx 1 rolf rolf 13 Feb 21 15:23 symrelative -> symtarget.txt
  8. -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:

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

答案1

得分: 19

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

  1. package main
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "path/filepath"
  6. )
  7. func main() {
  8. path := "/tmp/rolfl/symexample"
  9. target := "symtarget.txt"
  10. os.MkdirAll(path, 0755)
  11. ioutil.WriteFile(filepath.Join(path, "symtarget.txt"), []byte("Hello\n"), 0644)
  12. symlink := filepath.Join(path, "symlink")
  13. os.Symlink(target, symlink)
  14. }
英文:

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

  1. package main
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "path/filepath"
  6. )
  7. func main() {
  8. path := "/tmp/rolfl/symexample"
  9. target := "symtarget.txt"
  10. os.MkdirAll(path, 0755)
  11. ioutil.WriteFile(filepath.Join(path, "symtarget.txt"), []byte("Hello\n"), 0644)
  12. symlink := filepath.Join(path, "symlink")
  13. os.Symlink(target, symlink)
  14. }

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:

确定