How can I set file permissions to prevent anyone else from taking exclusive lock on my file?

StackOverflow https://stackoverflow.com/questions/12252127

  •  30-06-2021
  •  | 
  •  

Question

I want to open file for append on Windows 7 host using C#/.NET. I want to use usual file output operations for this purpose. I found such solution:

FileStream trace_fd = new FileStream(r"c:\temp\testlog.txt", 
                                     FileMode.Append, 
                                     FileAccess.Write, 
                                     FileShare.ReadWrite)

My tracefile is readable even if my program with line above is up and running. I don't have exception that file is used by another process anymore from proper code like this:

FileStream good_logreader_fd = new FileStream("c:\temp\testlog.txt", 
                                              FileMode.Read, 
                                              FileAccess.Read,
                                              FileShare.ReadWrite)

Then I have another program (buggy log scanner) with such line:

FileStream bad_logreader_fd = new FileStream("c:\temp\testlog.txt", 
                                             FileMode.Read, 
                                             FileAccess.Read, 
                                             FileShare.None)

Such line is obviously a bug for log scanner. If my program is not running then buggy logscaner will get exclusive lock and subsequent runs of my program will fail to get fd for trace file.

My question is what can I do to prevent such horrific scenario from happening. E.g. can I set file permissions for the tracefile to prevent anyone from taking exclusive lock? If yes then how? Anything else which can protect my program from buggy log scanner problem? Note that I have to keep the same trace filename between my program runs.

Was it helpful?

Solution

You can not set permissions to allow any access to a file, but somehow restrict FileShare.None as file sharing mode is not related to access permissions.

Permissions give a process (based on account it runs under) access to a file/resource. If access granted process can open file with whatever share mode it desires. If this share mode does not conflict with existing share modes on the file request succeeds and now file will have this share mode (combined with previous one). See CreateFile- dwShareMode section for details.

In your case log reader needs to have permissions to open file, so it will be able to set ShareMode.None if it the first process to open file. As devshorts says there is not much you can do short of changing offending process OR hacking file access methods (search for "hook CreateFile").

Side note: if your log reader runs under the same account as other processes permissions will not help for one additional reason - as there is no "per process" permissions in Windows and all processes will share the same user's permissions.

OTHER TIPS

As far as I know you can't prevent someone else from trying to open the file in another file access mode without modifying their code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top