使用golang创建硬链接

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

Create Hardlink with golang

问题

我想使用golang创建一个文件的硬链接。
os.Link()告诉我,不支持Windows。
因此,我尝试使用os.exec来调用"mklink.exe"。

  1. cmd := exec.Command("mklink.exe", "/H", hardlink_path, file_path)
  2. err := cmd.Run()

然而,它告诉我在%PATH%中找不到mklink.exe。
这让我感到困惑,因为我可以使用cmd来调用它。

接下来,我尝试通过cmd间接调用它:

  1. cmd := exec.Command("cmd.exe", "mklink.exe", "/H", hardlink_path, file_path)
  2. err := cmd.Run()

现在它没有返回任何错误,但也没有创建硬链接。
有什么建议吗?

英文:

I want to create a hardlink to a file using golang.
os.Link() tells me, that windows is not supported.
Therefore i tried to use os.exec, to call "mklink.exe".

  1. cmd := exec.Command("mklink.exe", "/H", hardlink_path, file_path)
  2. err := cmd.Run()

However, it tells me, that it can't find mklink.exe in %PATH%.
This baffels me, since i can call it using cmd.

Next i tried to call it indirectly via cmd:

  1. cmd := exec.Command("cmd.exe", "mklink.exe", "/H", hardlink_path, file_path)
  2. err := cmd.Run()

Now it does not return any error, however, it also doesn't create a hardlink.
Any suggestions?

答案1

得分: 15

Golang在Go 1.4中添加了对本机Windows硬链接的支持。具体来说,这个提交使得以下代码片段可以工作:

  1. err := os.Link("original.txt", "link.txt")

请注意,并非所有的Windows文件系统都支持硬链接。目前,NTFS和UDF支持它,但FAT32、exFAT和较新的ReFS不支持

完整的示例代码:

  1. package main
  2. import (
  3. "log"
  4. "os"
  5. "io/ioutil"
  6. )
  7. func main() {
  8. err := ioutil.WriteFile("original.txt", []byte("hello world"), 0600)
  9. if err != nil {
  10. log.Fatalln(err)
  11. }
  12. err = os.Link("original.txt", "link.txt")
  13. if err != nil {
  14. log.Fatalln(err)
  15. }
  16. }
英文:

Golang support for native Windows hard links was added in Go 1.4. Specifically, this commit makes the following snippet work:

  1. err := os.Link("original.txt", "link.txt")

Beware that not all Windows file systems support hard links. Currently NTFS and UDF support it, but FAT32, exFAT and the newer ReFS do not.

Full example code:

  1. package main
  2. import (
  3. "log"
  4. "os"
  5. "io/ioutil"
  6. )
  7. func main() {
  8. err := ioutil.WriteFile("original.txt", []byte("hello world"), 0600)
  9. if err != nil {
  10. log.Fatalln(err)
  11. }
  12. err = os.Link("original.txt", "link.txt")
  13. if err != nil {
  14. log.Fatalln(err)
  15. }
  16. }

答案2

得分: 0

例如,

  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "os/exec"
  6. )
  7. func main() {
  8. hardlink_path := `link.hard`
  9. file_path := `link.go`
  10. _, err := os.Stat(file_path)
  11. if err != nil {
  12. fmt.Println(err)
  13. return
  14. }
  15. os.Remove(hardlink_path)
  16. cmd := exec.Command("cmd", "/c", "mklink", "/H", hardlink_path, file_path)
  17. out, err := cmd.CombinedOutput()
  18. if err != nil {
  19. fmt.Println(err)
  20. return
  21. }
  22. fmt.Print(string(out))
  23. }

输出:

  1. 创建了 link.hard 的硬链接 <<===>> link.go
英文:

For example,

  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "os/exec"
  6. )
  7. func main() {
  8. hardlink_path := `link.hard`
  9. file_path := `link.go`
  10. _, err := os.Stat(file_path)
  11. if err != nil {
  12. fmt.Println(err)
  13. return
  14. }
  15. os.Remove(hardlink_path)
  16. cmd := exec.Command("cmd", "/c", "mklink", "/H", hardlink_path, file_path)
  17. out, err := cmd.CombinedOutput()
  18. if err != nil {
  19. fmt.Println(err)
  20. return
  21. }
  22. fmt.Print(string(out))
  23. }

Output:

  1. Hardlink created for link.hard <<===>> link.go

huangapple
  • 本文由 发表于 2013年5月29日 03:36:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/16800044.html
匿名

发表评论

匿名网友

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

确定