Nin*_*a S 3 unix file-permissions file go
我使用以下代码以编程方式构建二进制文件,二进制文件已成功构建,但现在我想通过代码将其复制到go/bin路径,我能够做到,但它复制文件但不是可执行文件.
什么可能是错的?源文件是可执行的
bPath := filepath.FromSlash("./integration/testdata/" + fileName)
cmd := exec.Command("go", "build", "-o", bPath, ".")
cmd.Dir = filepath.FromSlash("../")
err := cmd.Run()
if err != nil {
fmt.Println("binary creation failed: ", err)
}
fmt.Println(os.Getenv("GOPATH"))
dir, _ := os.Getwd()
srcPath := filepath.Join(dir, "testdata", , fileName)
targetPath := filepath.Join(os.Getenv("GOPATH"),"/bin/",fileName)
copy(srcPath, targetPath)
Run Code Online (Sandbox Code Playgroud)
副本是:
func copy(src string, dst string) error {
// Read all content of src to data
data, err := ioutil.ReadFile(src)
if err != nil {
return err
}
// Write data to dst
err = ioutil.WriteFile(dst, data, 0644)
if err != nil {
return err
}
return nil
}
Run Code Online (Sandbox Code Playgroud)
问题在于您提供的权限位掩码:0644.它不包括可执行权限,这是每个组中的最低位.
所以改为使用0755,结果文件将由每个人执行:
err = ioutil.WriteFile(dst, data, 0755)
Run Code Online (Sandbox Code Playgroud)
查看Wikipedia Chmod的bitmask含义.
相关的位掩码表:
# Permission rwx Binary
-------------------------------------------
7 read, write and execute rwx 111
6 read and write rw- 110
5 read and execute r-x 101
4 read only r-- 100
3 write and execute -wx 011
2 write only -w- 010
1 execute only --x 001
0 none --- 000
Run Code Online (Sandbox Code Playgroud)