3

I would like to write a file inside a "d:\test\" unfortunately I don't have a write permission for that folder. How to check whether that folder have a write permission or not.

NOTE: fopen is helpful,But that's creates a new file. I don't want to create exra file.

I am also seen CreateFile(), don't know how to use that for this case.

bala
  • 275
  • 2
  • 3
  • 13
  • 3
    Nearly the only correct test is to attempt to write, and react if it fails. Separating testing from attempting leads almost inevitably to race conditions. – Jerry Coffin Feb 25 '14 at 07:16
  • Without attempt to write a file, no other way is there? @jerry Coffin – bala Feb 25 '14 at 08:22
  • 1
    @bala I'm afraid no. Another example is when you have permission to write, but there is no enough space, so even if you check for rights you won't be able to create a consistent file afterwards. And yes, checking for free disk space would also lead to race conditions. – mike.dld Feb 25 '14 at 15:44

3 Answers3

2

Just open (and write to) the file and analyse the error returned by the system, if any. Checking if you have rights in a separate operation would result in race condition, where first call tells you you could create a file while second one fails because access rights have changed inbetween.

mike.dld
  • 2,929
  • 20
  • 21
  • At least on windows you could have the create file permission without the delete file permission. This would end up leaving a lot of stale test files. – RedX Feb 25 '14 at 08:52
  • it could be smart to check for a LACK of permissions ahead of time, and still gracefully deal with the OS's error/exception as per normal. Its not always a bad idea to check ahead of time, it just depends on whether you're planning to ONLY check ahead of time. – KotoroShinoto Jan 23 '20 at 17:11
2

Alternative 1:

You can use _access or _access_s to check if file is having permission.

More information here

00 Existence only
02 Write-only
04 Read-only
06 Read and write

Alternative 2:

As you already tried CreateFile, use GENERIC_WRITE as your dwDesiredAccess

and then see error, if any.

More details here

PS: If you anyway just need to check (and not write/create file), delete temp file, if already created.

Digital_Reality
  • 4,488
  • 1
  • 29
  • 31
  • @bala Updated answer with _access – Digital_Reality Feb 25 '14 at 08:38
  • Did you tried this? It won't works for me. I used for the folder that folder doesn't have a write access. – bala Feb 25 '14 at 09:25
  • @bala, you can't check permission to folder.. but just append tempfilename to your existing folder and use _access. (anyway file is not going to be created..) then you just check what is return code. Did you try this? – Digital_Reality Feb 25 '14 at 09:27
  • It's working like if temp file name is available then its returning 0. if not returns -1. – bala Feb 25 '14 at 10:24
0

Actually one way of checking if you have permission to write is to use the security access functions. There you can query for all the different rights (delete, create, write, index...)

I have never used them so i can't provide you with an example.

Remember though that between checking your access permissions and creating the file or writing there could be a change in access rights and it could still fail. The race condition mentioned by the others.

If you will end up creating a file anyway, then just use CreateFile as mentioned by the others.

RedX
  • 14,749
  • 1
  • 53
  • 76