1

okay, so I'm a bit of a C newbie. How does one test whether a file is read only on windows.

I tried to get something working with the GetFileAttributes function, but to no avail.

Given a file's path, what is the proper way to test if it is read only?

Edit:

So I'm still stuck on this one. I want to check if the user has permission to add and edit files in C:\Program Files... and I can't seem to get any of the advice to work.

when I use !(GetFileAtrributes(path) & FILE_ATTRIBUTES_READONLY), it indicates that the directory is not read only. I then tried to write a file and test if it worked:

strcat(path,"\\testFile000");
FILE *test = 0;
test = fopen(path,"w");
int i = fwrite("ab",1,sizeof("ab"),test);

but when I do this test is non-null, i==3 and no file appears in the OS.

Any ideas?

Ian Fellows
  • 17,228
  • 10
  • 49
  • 63
  • Sorry, misread your question. –  Sep 27 '10 at 00:09
  • possible duplicate of [Effective file permissions tool's api in windows](http://stackoverflow.com/questions/3021698/effective-file-permissions-tools-api-in-windows) – bmargulies Sep 27 '10 at 00:49

2 Answers2

2

GetFileAttributes is the correct Windows call - see MSDN example for use

The example tests read only and the test is check dwAttrs & FILE_ATTRIBUTE_READONLY and that will be non zero if the file is read only.

Graeme Perrow
  • 56,086
  • 21
  • 82
  • 121
mmmmmm
  • 32,227
  • 27
  • 88
  • 117
2

You are forgetting about the ACL. A file can have it's read-only bit clear but still have a restrictive ACL. You can call CreateFile and check the return code. Or call `AuthzAccessCheck'.

bmargulies
  • 97,814
  • 39
  • 186
  • 310