2

I have a scenario where I need to check permissions of files (and folders) in Windows, without attempting to read them.

As a further complication, these files are on a network share.

To do so, I run os.Stat() on the file and then check f.Mode().Perm(). The issue is, even for files that I do not have access, this API call shows that I do indeed have access.

During testing of this error, I logged the permissions, then I tried to read the file (in production this program will not be reading the file). I get an error stating that I don’t have access to read the file, as I expect.

My question is, other than attempting to read/write a file and checking the error, is there a reliable way to check real permissions in Go under Windows?

Thanks for your time!

Zachary Craig
  • 2,192
  • 4
  • 23
  • 34
jssblck
  • 529
  • 5
  • 20
  • 1
    Windows file permissions are complicated and cannot be represented by three bytes as they can in Unix. [See this related question](https://stackoverflow.com/questions/33445727/how-to-control-file-access-in-windows) – Adrian May 09 '18 at 19:42
  • @Adrian, thanks for the link. Understood about the complications. I guess I’m used to how in NodeJS for example I can just ask “do I have access to this folder”; I don’t really care about the whole ACL. I assume based on that post and your response there’s no similarly simple way to do this in Go? – jssblck May 09 '18 at 19:45
  • You'd have to look at the NodeJS source to be sure, but it's either using Windows syscalls to check the ACL, or it's just attempting a read or something and seeing if it succeeds. – Adrian May 09 '18 at 19:49
  • @Adrian, good call. Thanks for your pointers. – jssblck May 09 '18 at 20:36

0 Answers0