• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1161
  • Last Modified:

Open file readonly without locking it


I need to read files from folder that is actually set as subversion repository (or similar name) - files are being managed by subversioning system (TFS, TortoiseSVN...)

This means that the files I need to read, can't be locked as at any time one of the developers might be using it... so, I need to make sure I only try to read files.

If files are locked with denied share, then no problem, I will read later or not.

So, this is how I access file currently:

  F: TFileStream;
  F := TFileStream.Create(AFileName, fmOpenRead or fmShareDenyWrite);

Open in new window

Files can be up to 50MB, on network server, so reading could take a few seconds....

I actually have 3 questions:
1: Why are here 2 flags? Why not just fmOpenRead? What does OR mean, try one and if it doesn't work try the second one?

2: What do I need to set, to access file only read only? I can't lock the files in anyway, so if it's locked I should get a message back (any kind that will tell me I can't read the content)

3: What will happen if another process writes something in a file while I'm accessing the content? Let's say my operation lasts 3 seconds and the other process changed the content in .5s in between?

I hope 500 points is enough for all 3 questions.

3 Solutions
Paul MacDonaldDirector, Information SystemsCommented:
1) The first permission allows/denies you opening the file at all.  The second permits/denies you changing the file

2) You could try setting the "Deny Write" permission.  This should allow people to open/read the file, but not make any changes to it.  Whether or not you can do this in a way that supersedes your version control system, I don't know.

3) If they're not denied write permissions, I'd expect the file to change in the middle of your copy.  I'm not sure if you'd capture the changes or not.
Delphi_developerAuthor Commented:
Thanks for quick reply, but:

1. Not sure I follow, after reading in Help, I thought that fmOpenRead means "I want to open in read mode" and fmShareDenyWrite would be "If I succeed opening a file in read mode, then deny writing to others"... is this correct?

2. Perhaps I was not clear, I need to read the files, but in no way I should be locking for read/write to other processes.

3. I guess I will need to do some testing to know the answer here, as we are both just guessing.
Or performs a bitwise or between the two values.  The combined value is passed to the function.  Normally these values are set up as distinct binary values so ORing them will produce a number that is an integer value that contains bit flags for all of the settings.  In your case it will try to open the file to read it while denying the ability to write to the file to others.

Even if you could read from the file while allowing someone else to write to it the process would invariably end up with you reading a half modified file.  As you are reading the file, someone else is writing to it.  Could you guarantee that you got the whole file read before it was written to?

I hope the explanation helps.  Let me know if you have any questions about what I wrote.

If you still need more help, it would be good to know the reason you are trying to do this... what are you trying to accomplish?
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Delphi_developerAuthor Commented:
Thank you, now I understand about the parameters, OR purpose.

I'm creating an external Diff tool, that SVN software opens. The SVN usually places 2 files into temp folder so I can open the files and show the differences, no problem here, as these files are generated just for me and I can read and access them properly.
But, to properly identify the content of the files, I need content of some 'supporting' files, which are located in SVN and not copied together into temp folder. These files are in 'live' mode, meaning they can be accesses/changed by any developer at any time.
These files are important for my app but not critical, so I need to read them, but the priority is that my app is not locking these files and SVN is behaving without any interruption.
So, I need to read the files and leave them unlocked.

Do you think this would work for my situation, to read and not block:

F := TFileStream.Create(AFileName, fmOpenRead or fmShareDenyNone);

Open in new window


My app is going to be used by some larger systems, where 50+ developers can be using the SVN system, so I got to be very careful here.
Sinisa VukCommented:
Yes, your line
F := TFileStream.Create(AFileName, fmOpenRead or fmShareDenyNone);

Open in new window

is correct because of fmShareDenyNone - deny none to other processes.
But if svn opened file exclusively - then you are in trouble...
Delphi_developerAuthor Commented:
"But if svn opened file exclusively - then you are in trouble... _" - Why? I mean I understand I will not be able to read content of the file.

Can I use
Try except on E: EFOpenError do {nothing} end; 

Open in new window

block and skip the file on error?

If the files are already locked, I have no issue with that - will not read the content; try later or skip it.

Or do you think I could have other issues than just not being able to read the file, which is opened exclusively by another process?
Sinisa VukCommented:
no, its like you said - nothing bad happens except on exception is generated (and I see that you already handle it). Using native win api functions for file opening there is no exception but error code is returned.
Delphi_developerAuthor Commented:
Thank you guys for your help, points are assigned according to how I was able to understand and get value, the solution from them.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now