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!
Delphi_developerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
0
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.
0
developmentguruPresidentCommented:
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?
0
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

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.
0
Sinisa VukSoftware architectCommented:
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...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Delphi_developerAuthor Commented:
@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?
0
Sinisa VukSoftware architectCommented:
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
0
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.