Sign in Join

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

Mailing lists / fpc-pascal / 2015-05

Truncate procedure unable to work under Linux, is this a bug?

8 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

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

Géza Kovacs Géza

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.
Best regards, Geza

Reply by email

Michael Van Canneyt
to Géza Kovacs Géza
Géza Kovacs Géza wrote 32 lines
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.

Michael.

Reply by email

Peter
to Michael Van Canneyt: Truncate is a leftover from the DOS days. It does not work with files >2 Gb, the file…
Michael Van Canneyt wrote 55 lines
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. Michael. _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Hi,
It should work if Truncate64 is used instead of Truncate. I suggest its a bug, albeit probably a wishlist one.

http://man7.org/linux/man-pages/man2/ftruncate.2.html

Regards,
Peter

Reply by email

Peter
to Marco van de Voort
Marco van de Voort wrote 13 lines
In our previous episode, Peter said: > It should work if Truncate64 is used instead of Truncate. > I suggest its a bug, albeit probably a wishlist one. > > http://man7.org/linux/man-pages/man2/ftruncate.2.html ftruncate64 is already used for 32-bit CPUs, ftruncate for 64-bit cpus You can get info on what FPC sends to the kernel using strace. _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Thanks for that. So is it a bug then? Why is 64-bit not using ftruncate64?

Regards,
PB.

Reply by email

Marco van de Voort
to Peter

In our previous episode, Peter said:

> Thanks for that. So is it a bug then? Why is 64-bit not using ftruncate64?

I'm no linux expert, but I assume because it is only for 32-bit. On 64-bit, off_t already is 64-bit, so the posix template matches 1:1, and there is no need for another function.

Most BSDs have off_t at 64-bit, and therefore don't have this problem.

Reply by email

Michael Van Canneyt
to Peter: Thanks for that. So is it a bug then? Why is 64-bit not using ftruncate64? PB.
Peter wrote 16 lines
On 20/05/15 16:36, Marco van de Voort wrote: > In our previous episode, Peter said: > >> It should work if Truncate64 is used instead of Truncate. >> I suggest its a bug, albeit probably a wishlist one. >> >> http://man7.org/linux/man-pages/man2/ftruncate.2.html > ftruncate64 is already used for 32-bit CPUs, ftruncate for 64-bit cpus > > You can get info on what FPC sends to the kernel using strace. > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal > Thanks for that. So is it a bug then? Why is 64-bit not using ftruncate64?

It is a bug in the system unit. I committed a patch in rev. 30892.

Michael.

Reply by email