当使用`os.O_CREATE`时,读取默认的文件模式。

huangapple go评论84阅读模式
英文:

Reading default FileMode when using os.O_CREATE

问题

我是你的中文翻译助手,以下是翻译好的内容:

我刚开始学习Go语言,在读取默认文件权限/系统掩码方面遇到了一些问题。当然,我可以指定固定的权限:

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

但是我希望程序能够以用户设置的umask打开文件。我该如何做到这一点?

英文:

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

得分: 10

它已经按照你想要的方式工作。

只需使用"0666",umask将被应用。

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

对于我来说,使用umask 0022,我得到的结果是:

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

如果你希望文件始终无法被"other"读取,无论umask如何,可以使用0660(例如)。

英文:

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.

huangapple
  • 本文由 发表于 2014年5月24日 15:05:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/23842247.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定