【发布时间】: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