doubeng1278 2018-12-13 04:26
浏览 127
已采纳

为什么在调用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)
  • 写回答

1条回答 默认 最新

  • doutang1856 2018-12-13 04:29
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部