[fpc-pascal] Truncate procedure unable to work under Linux, is this a bug?

Michael Van Canneyt michael at freepascal.org
Mon May 18 09:30:27 CEST 2015

On Sun, 17 May 2015, Géza Kovacs Géza wrote:

> Truncate procedure unable to work under Linux, is this a bug?
> This code is unable to work under linux: the Truncate drops an error
> message if the file is larger than 2 or 3 GB.
> I tested it with Free Pascal 2.6.4 under Debian Jessie 64 bit.
> It produce the same error message under Ubuntu 12.04 32 bit with Free
> Pascal 2.4.4
> Sometimes I did not get any error message but the file (which was 2 or
> 3 GB) will be only some hundred MB after truncate...
> It operates correctly under Windows.
> It produce the following error message:
> An unhandled exception occurred at $<address>:
> EPrivilege : Privileged instruction
> $<address>
> Program File_Trunc;
> {$MODE OBJFPC} {$H+}
> uses dos,sysutils;
> var
> 	f : file of byte;
> 	fs : int64;
> begin
> 	assign(f,paramstr(1));
> 	ReSet(f);
> 	fs := FileSize(f);
> 	WriteLn('Original file size: ',fs);
> 	fs := fs-3456789;
> 	WriteLn('File size after truncate: ',fs);
> 	Seek(f,fs);
> 	Truncate(f);
> 	Close(f);
> end.

Truncate is a leftover from the DOS days. 
It does not work with files >2 Gb, the file position is 32-bit.

There is the Filetruncate function from sysutils:

Function FileTruncate (Handle: THandle; Size: Int64) : boolean;

but even that does not work with files >2Gb on unix, because the underlying system call does not support it.
I don't know what happens on Windows.


More information about the fpc-pascal mailing list