Link to home
Start Free TrialLog in
Avatar of Delphi_developer
Delphi_developer

asked on

Open file readonly without locking it

Hi

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:

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

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.

Thanks!
SOLUTION
Avatar of Paul MacDonald
Paul MacDonald
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Delphi_developer
Delphi_developer

ASKER

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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@sinisav:
"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?
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.
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365430%28v=vs.85%29.aspx
Thank you guys for your help, points are assigned according to how I was able to understand and get value, the solution from them.