[fpc-pascal] Why opening a file for write fails in fpc 2.6?

Jonas Maebe jonas.maebe at elis.ugent.be
Wed Oct 31 14:45:27 CET 2012


On 31 Oct 2012, at 14:35, Giuliano Colla wrote:

> Jonas Maebe ha scritto:
>>
>>
>> FPC 2.6 added support for file locking on Unix platforms. You  
>> probably should use
>>
>>   USBOut := FileOpen(UsbDev,fmOpenWrite or fmShareDenyNone);
>>
>> Otherwise FileOpen will try to get an exclusive (write) lock on  
>> that device, which may well not be possible.
>>
> Understood, thank you. But shouldn't it be the other way around,  
> i.e. one should explicitly request an exclusive lock?

No, that is how the behaviour of this function is defined. Looking at  
the documentation, is seems that file locking for Unix was already  
implemented in FPC 2.4.0 though, so maybe that won't be the problem  
after all.

>>> Moreover I don't understand why the call returns a -1, meaning  
>>> that the Open failed, without rising an exception.
>>
>> Does FileOpen raise an exception in Delphi when locking fails?
>>
>
> I have no idea,

It's always a good idea to look at the documentation first. At least  
the FPC documentation clearly states that it will return -1 on error: http://www.freepascal.org/docs-html/rtl/sysutils/fileopen.html

> but if it doesn't it's a Delphi bug, I'd say. What are exceptions  
> there for, if not for telling you that what you requested cannot be  
> done?

There are multiple ways to indicate a failure. Exceptions are a very  
expensive way for doing so, and generally should only be used in cases  
that are truly exceptional and/or indicate a fatal error. In other  
cases, using a special return value is generally much more appropriate.


Jonas



More information about the fpc-pascal mailing list