download12749 2015-10-30 22:21
浏览 231
已采纳

如何在Windows中控制文件访问?

Go provides os.Chmod() for setting file and directory permissions. For example, if I want to ensure a file is accessible only to the current user, I can do the following:

os.Chmod("somefile.txt", 0600)

This works great on Linux but does absolutely nothing on Windows. After digging into the Go source code, I came across its implementation. It seems like S_IWRITE is the only attribute supported.

How do I control access to a file or directory on Windows using Go?

  • 写回答

1条回答 默认 最新

  • dqvzfp6468 2015-10-30 22:21
    关注

    Explanation

    Windows does not use traditional Unix permissions. Instead, Windows controls access to files and directories through access control. Each object has an ACL (Access Control List)* which controls access to the object.

    Each ACL is basically a list of ACEs (Access Control Entries) which determine what access a specific trustee (user, group, etc.) is granted. For example, a file may contain an ACE granting a specific user read access (GENERIC_READ) to the file.

    Manipulating ACLs and ACEs is done through the authorization functions in the Windows API.

    * technically each object has two ACLs - a DACL and a SACL

    Solution

    Thankfully, learning all of these functions isn't necessary. I've put together a small Go package named "go-acl" that does all of the heavy-lifting and exposes a function named (what else?) Chmod. Basic usage is as follows:

    import "github.com/hectane/go-acl"
    
    err := acl.Chmod("C:\\path\\to\\file.txt", 0755)
    if err != nil {
        panic(err)
    }
    

    Results

    The Chmod() function creates three ACEs in the file's ACL:

    enter image description here

    • one for the owner (WinCreatorOwnerSid)
    • one for the group (WinCreatorGroupSid)
    • one for everyone else (WinWorldSid)
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测