使用golang创建硬链接

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

Create Hardlink with golang

问题

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

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

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

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

cmd := exec.Command("cmd.exe", "mklink.exe", "/H", hardlink_path, file_path)
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".

cmd := exec.Command("mklink.exe", "/H", hardlink_path, file_path)
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:

cmd := exec.Command("cmd.exe", "mklink.exe", "/H", hardlink_path, file_path)
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硬链接的支持。具体来说,这个提交使得以下代码片段可以工作:

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

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

完整的示例代码:

package main

import (
    "log"
    "os"
    "io/ioutil"
)

func main() {    
    err := ioutil.WriteFile("original.txt", []byte("hello world"), 0600)
    if err != nil {
        log.Fatalln(err)
    }    

    err = os.Link("original.txt", "link.txt")
    if err != nil {
        log.Fatalln(err)
    }
}
英文:

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

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:

package main

import (
    "log"
	"os"
    "io/ioutil"
)

func main() {	
    err := ioutil.WriteFile("original.txt", []byte("hello world"), 0600)
	if err != nil {
    	log.Fatalln(err)
	}    

    err = os.Link("original.txt", "link.txt")
	if err != nil {
    	log.Fatalln(err)
	}
}

答案2

得分: 0

例如,

package main

import (
	"fmt"
	"os"
	"os/exec"
)

func main() {
	hardlink_path := `link.hard`
	file_path := `link.go`
	_, err := os.Stat(file_path)
	if err != nil {
		fmt.Println(err)
		return
	}
	os.Remove(hardlink_path)
	cmd := exec.Command("cmd", "/c", "mklink", "/H", hardlink_path, file_path)
	out, err := cmd.CombinedOutput()
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Print(string(out))
}

输出:

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

For example,

package main

import (
	"fmt"
	"os"
	"os/exec"
)

func main() {
	hardlink_path := `link.hard`
	file_path := `link.go`
	_, err := os.Stat(file_path)
	if err != nil {
		fmt.Println(err)
		return
	}
	os.Remove(hardlink_path)
	cmd := exec.Command("cmd", "/c", "mklink", "/H", hardlink_path, file_path)
	out, err := cmd.CombinedOutput()
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Print(string(out))
}

Output:

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:

确定