douyong1885 2016-07-15 17:47
浏览 36
已采纳

无法获得所有权限位

On my shell, I have a file and I change the permissions of this file with sudo chmod 4755 <file>. Upon calling lstat on this file, I'm seeing correct information about its permissions, specifically that it has 4755 as its permission mode.

In my golang program, is there a reason why I'm not getting the correct permission mode bits? Could it be that I'm just formatting the result I get from FileInfo().Mode().Perm() incorrectly? Could it be that the upper 3 bits are "special"?

Thanks for the help!

  • 写回答

1条回答 默认 最新

  • dtpngq3378499 2016-07-15 18:10
    关注

    Short Answer: The three upper bits ARE special and need to be accessed separately.

    Long Answer: The documentation explains that the 9 (out of usually 12) least significant bits are considered the standard Unix permissions.

    The documentation also defines the behavior of the Perm() function you are calling:

    func (m FileMode) Perm() FileMode
    
    Perm returns the Unix permission bits in m.
    

    This means that perm is not defined to return any of the additional bits you are looking for.

    Furthermore, the source code shows that the Perm() function is masking the value returned from FileMode() with 0777 causing the initial three bits to be disregarded.

    The ModeSetuid, ModeSetgid, and ModeSticky bits (4, 2, and 1 respectively) must each be accessed individually as constants of the FileMode type. Do this by performing your own masks.

    In order to determine whether the sticky bit is set, for example, do (FileInfo().Mode() & ModeSticky) != 0. The same applies for ModeSetuid and ModSetgid.

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

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错