doubeng1278 2018-12-13 12: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 12: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.

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

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题