ioutils.WriteFile() 不尊重权限

Eds*_*ina 6 file-permissions go

我正在尝试使用 ioutils.WriteFile() 但由于某种原因它忽略了我授予它的 0777 权限。

package main

import (
    "io/ioutil"
    "os"
)

func main() {

    // normal permissions
    if err := ioutil.WriteFile("cant-touch-this-0644", []byte{}, 0644); err != nil {
            panic(err)
    }


    // full permissions
    if err := ioutil.WriteFile("cant-touch-this-0777", []byte{}, 0777); err != nil {
            panic(err)
    }


    // normal permissions + chmod to full
    if err := ioutil.WriteFile("cant-touch-this-mixed", []byte{}, 0755); err != nil {
            panic(err)
    }

    if err := os.Chmod("cant-touch-this-mixed", 0777); err != nil {
            panic(err)
    }
}
Run Code Online (Sandbox Code Playgroud)

我从中得到的输出是:

$ ls -l
-rw-r--r--  1 edson edson    0 May  9 17:19  cant-touch-this-0644
-rwxr-xr-x  1 edson edson    0 May  9 17:19  cant-touch-this-0777
-rwxrwxrwx  1 edson edson    0 May  9 17:19  cant-touch-this-mixed
Run Code Online (Sandbox Code Playgroud)

意思是:

  • 第一个场景(0644)有效
  • 第二个(0777)被忽略
  • 我可以获得完整 0777 权限的唯一方法是使用os.Chmod(如第三种情况)

我究竟做错了什么?

Tos*_*ita 3

正如对这个问题的评论所说,这是因为umask起作用了。unmask 控制如何为新创建的文件设置文件权限。当 umask 为 022 时,您要创建的文件为 666 将是 644(从组和其他权限中删除写入权限)。您可以使用命令检查目录的 umask umask