Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2015-04

file locking cross platform

4 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

no reply

There are some major differences between the way Windows and Unix allow locking of files with their api's, but is it possible to make a cross platform LockFile() procedure or function for both unix and windows?

By file locking I of course mean when for example you have to write data to a file but you want to lock it so no one else can write to the file, then UnlockFile() when you are done with it.

If not cross platform, shouldn't one of the unix units have a LockFile function at least?

Lars

no reply
to no reply
There are some major differences between the way Windows and Unix allow locking of files with their api's, but is it possible to make a cross platform LockFile() procedure or function for both unix and windows?

There is of course fpFlock for unix,

http://www.freepascal.org/docs-html/rtl/unix/fpflock.html

However this means implementing file locking differently on unix and windows computers, which makes the application not cross platform.

I was hoping for something like LockFile and UnlockFile for both windows and unix, instead of implementing locking using IFDEF WINDOWS and IFDEF UNIX.

Lars

Michael Van Canneyt
to no reply: There are some major differences between the way Windows and Unix allow locking of files…
no reply wrote 10 lines
There are some major differences between the way Windows and Unix allow locking of files with their api's, but is it possible to make a cross platform LockFile() procedure or function for both unix and windows? By file locking I of course mean when for example you have to write data to a file but you want to lock it so no one else can write to the file, then UnlockFile() when you are done with it. If not cross platform, shouldn't one of the unix units have a LockFile function at least?

To the best of my knowledge, unix only has advisory file locking, as you discovered. It works different from windows.

There is some linux specific mandatory locking, I think, but implementing it so it behaves the same as Windows is IMHO not possible.

The FileOpen/FileCreate (and hence TFileStream) implements the various fmShare* constants, but they will only work as expected if all accessing applications are FPC applications. Obviously we cannot guarantee that other apps "play well".

Michael.

Tomas Hajny
to Michael Van Canneyt
Michael Van Canneyt wrote 22 lines
On Fri, 3 Apr 2015, no reply wrote: > There are some major differences between the way Windows and Unix allow > locking of files with their api's, but is it possible to make a cross > platform LockFile() procedure or function for both unix and windows? > > By file locking I of course mean when for example you have to write data > to a file but you want to lock it so no one else can write to the file, > then UnlockFile() when you are done with it. > > If not cross platform, shouldn't one of the unix units have a LockFile > function at least? To the best of my knowledge, unix only has advisory file locking, as you discovered. It works different from windows. There is some linux specific mandatory locking, I think, but implementing it so it behaves the same as Windows is IMHO not possible. The FileOpen/FileCreate (and hence TFileStream) implements the various fmShare* constants, but they will only work as expected if all accessing applications are FPC applications. Obviously we cannot guarantee that other apps "play well".

As an additional idea, you may want to have a look at fcl- db/src/dbase/dbf-wtil.pas which provides supposed alternatives of LockFile and UnlockFile for Unix and OS/2 targets. The caveat mentioned by Michael above regarding advisory nature of file locking under Unix still holds true, of course.

Tomas