[fpc-pascal] TFileStream.SetSize sometimes fails

Michael Van Canneyt michael at freepascal.org
Fri Aug 14 20:34:40 CEST 2009

On Fri, 14 Aug 2009, Martin wrote:

> Michael Van Canneyt wrote:
>> On Thu, 13 Aug 2009, Mattias Gaertner wrote:
>>> I want to replace the content of an existing file (keeping the type and
>>> rights). I used:
>>> fs := TFileStream.Create(Filename, fmOpenWrite or fmShareDenyNone);
>>> fs.Size := 0;
>>> fs.Write(.....);
>>> fs.Free;
>>> But it does not always work. The line fs.Size does not clear the file on
>>> every file system. So the fs.Write only replaces the first part of the
>>> file.
>> If it depends on the filesystem, then you should complain with the linux
>> kernel people ?
>> Size=0 results in a ftruncate(Handle,0) call
>> Part of a strace (ex20 from the baseunix unit docs, fptruncate call)
>> open("test.dat", O_WRONLY|O_LARGEFILE)  = 3
>> ftruncate(3, 60)                        = 0
>> close(3)                                = 0
>> So we can state that FPC's ftruncate does work, it passes the args to the 
>> kernel.
>> If the kernel does not actually truncate the file, there is little FPC can 
>> do.
> Well the only case were it fails (at least were we know that it is truncate 
> that fails) is VBox shared folders. And indeed it seems they do have a bug. 
> http://www.virtualbox.org/ticket/3712
> or the one I can reproduce http://www.virtualbox.org/ticket/4771
> However in c I can specify
>  open(fname, O_WRONLY | O_TRUNC );
> which does work (bypasses the bug)
> I haven't seen an equvivalent to O_TRUNC in fpc ?

Sure there is:

uses baseunix;

   H : integer;

   h:=fpOpen(fname,O_WrOnly or o_trunk)

But that is not relevant:
a) FPC itself should not work around kernel/vbox bugs.
b) It would not help in Mattias' case anyway...


More information about the fpc-pascal mailing list