[fpc-pascal] file locking cross platform

Tomas Hajny XHajT03 at hajny.biz
Sat Apr 4 10:06:04 CEST 2015

On 4 Apr 15, at 8:23, Michael Van Canneyt wrote:
> 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.


More information about the fpc-pascal mailing list