Reading default FileMode when using os.O_CREATE

早过忘川 提交于 2020-07-08 11:28:05

问题


I'm new to Go, have a bit of a problem with reading default file permissions / system mask. Of course I can specify fixed permissions:

f, err := os.OpenFile(fpath, os.O_CREATE|os.O_WRONLY, 0600)

But I would like the program to behave nicely and open a file with user's account set umask. How can I do that?


回答1:


It already works like you want it.

Just use "0666" and the umask will be applied.

f, err := os.OpenFile(fpath, os.O_CREATE|os.O_WRONLY, 0666)

For me with umask 0022 I get:

$ go run x.go  ; ls -l filename
-rw-r--r--  1 ask  wheel  0 May 24 00:18 filename

Use 0660 (for example) if you always want the file to be unreadable by "other", no matter the umask.



来源:https://stackoverflow.com/questions/23842247/reading-default-filemode-when-using-os-o-create

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!