【问题标题】:ioutils.WriteFile() not respecting permissionsioutils.WriteFile() 不尊重权限
【发布时间】:2018-05-09 16:23:05
【问题描述】:

我正在尝试使用 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)
    }
}

我从中得到的输出是:

$ 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

这意味着:

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

我做错了什么?

【问题讨论】:

  • 这与 Go 或 ioutil.WriteFile 无关:参见 en.wikipedia.org/wiki/Umask
  • @JimB 如果您将其写为答案,我会将其标记为所选答案。

标签: go file-permissions


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-13
    • 2011-07-13
    • 2019-10-01
    • 2022-01-21
    • 1970-01-01
    • 2017-03-29
    • 2015-08-08
    相关资源
    最近更新 更多