为什么在调用OpenFile时需要设置权限?
I understand what permissions mean for files and dirs stored in filesystem. But why do I need to set permissions when calling os.OpenFile? Does it update file permission on filesystem if opened successfully? If not - what difference does it make to open same file with 0000 or 0777?
https://golang.org/src/os/file.go?s=8454:8520#L272
func OpenFile(name string, flag int, perm FileMode) (*File, error)
...
f, err := os.OpenFile("access.log", os.O_APPEND, 0644)
我了解权限对存储在文件系统中的文件和目录意味着什么。
但是为什么我需要设置权限调用os.OpenFile时 strong>?
如果打开成功,它会更新文件系统上的文件权限吗?
如果不成功-打开带有0000或0777的同一文件有什么区别? p>
https://golang.org/src/os/ file.go?s = 8454:8520#L272 p>
func OpenFile(名称字符串,标志int,perm FileMode)(*文件,错误)
。 ..
f,err:= os.OpenFile(“ access.log”,os.O_APPEND,0644)
code> pre>
div>
As documented (emphasis added):
OpenFile is the generalized open call; most users will use Open or Create instead. It opens the named file with specified flag (O_RDONLY etc.) and perm (before umask), if applicable. If successful, methods on the returned File can be used for I/O. If there is an error, it will be of type *PathError.
So, the perm
value is only used when the file is created--when opening an existing file, it is not applicable, so it is ignored.