file - 如何正确复制二进制文件

标签 file unix go file-permissions

我使用以下代码以编程方式构建二进制文件 二进制文件构建成功但现在我想通过代码将其复制到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)

副本是:

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
}

最佳答案

问题出在您提供的权限位掩码上:0644。它不包括可执行权限,这是每个组中的最低位。

所以改用0755,结果文件将被所有人执行:

err = ioutil.WriteFile(dst, data, 0755)

查看 Wikipedia Chmod位掩码的含义。

相关位掩码表:

#    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

关于file - 如何正确复制二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54348353/

相关文章:

c++ - 使用 fork() 创建多个进程,无需使用 exit() 或 wait() 即可与管道通信

linux - Unix - 需要显示目录上次修改时间

go - 运行多文件go程序

algorithm - go 中更好的并发素数筛

python - 打开: invalid mode or filename

Flash ProgressEvent 未显示总大小

android - flutter 在外部打开文件,例如在 ios 上 "open in"

java - List.Add 在 Java 中意外多次添加相同的内容

linux - 当我从 Perl 调用时,为什么我的 awk 单行代码不起作用?

docker - 无法使用 Docker 为 Golang 应用程序创建镜像