Recent

Author Topic: [SOLVED] LibTar - exception reading archived file to TStream  (Read 594 times)

Vodnik

  • Full Member
  • ***
  • Posts: 210
[SOLVED] LibTar - exception reading archived file to TStream
« on: November 20, 2022, 09:50:00 pm »
Hello,

Attached is a simplified project using LibTar package, delivered with Lazarus.
It opens TAR archive, list archived files and tries to read archived file content to TStream for further processing. The last operation causes EStreamError exception with message "Writing to TStream is not supported".

Problem occur when using
PROCEDURE TTarArchive.ReadFile (Stream : TStream);
while using
PROCEDURE TTarArchive.ReadFile (Filename : STRING);
successfully extract archived files.

LibTar package seems to be imported from Delphi, original source code at www.destructor.de

I'm using Lazarus V 2.2.4, 64-bit, Windows 10.
« Last Edit: November 21, 2022, 10:17:23 am by Vodnik »

Vodnik

  • Full Member
  • ***
  • Posts: 210
Re: LibTar - exception reading archived file to TStream
« Reply #1 on: November 21, 2022, 10:16:55 am »
That was my fault.
TStream is abstract class and shouldn't be used here.
Using TStringStream instead solves the problem.

Vodnik

  • Full Member
  • ***
  • Posts: 210
Re: [SOLVED] LibTar - exception reading archived file to TStream
« Reply #2 on: November 22, 2022, 01:21:42 pm »
Well, this was not end of a story...

There is an issue when processing some TARs (supposingly created under Linux) in Windows.
Procedure FindNext causes exception when reading buffer to FStream (see LibTar.pp, line 598):

Code: Pascal  [Select][+][-]
  1.   // --- Scan until next pointer
  2.   IF FBytesToGo > 0 THEN
  3.     FStream.Seek (Records (FBytesToGo) * RECORDSIZE, soCurrent);
  4.  
  5.   // --- EOF reached?
  6.   Result := FALSE;
  7.   CurFilePos := FStream.Position;
  8.   TRY
  9.     FStream.ReadBuffer (Rec, RECORDSIZE);
  10.     if Rec [0] = #0 THEN EXIT;   // EOF reached
  11.   EXCEPT
  12.     EXIT;   // EOF reached, too
  13.     END;
  14.   Result := TRUE;
  15.  

Adding trivial exception processing solves the problem:

Code: Pascal  [Select][+][-]
  1.   EXCEPT
  2.     on E: Exception do
  3.       EXIT;   // EOF reached, too
  4.   END;
  5.  

MarkMLl

  • Hero Member
  • *****
  • Posts: 6881
Re: [SOLVED] LibTar - exception reading archived file to TStream
« Reply #3 on: November 22, 2022, 01:41:45 pm »
Is this LibTar being used directly, or with a fancy class wrapper? I'd hardly call myself the local expert but have previously commented on the otiosity of some of this stuff https://forum.lazarus.freepascal.org/index.php/topic,59296.msg442107.html#msg442107

I think that I'd note however that most if not all Linux systems will be running GNU Tar these days, which is pretty much the standard bearer as far as the format is concerned. I've worked with Sun systems in the past that used a different implementation, but would be surprised if you came across that.

There have, over the years, been minor format changes, including adding user and group names to file specifications rather than simply assuming that every unix system will have the same uid/gid numbers. However I think that things have been fairly stable for the last ten years or so, so again I'd be surprised if you were bitten by that.

A more serious possibility would be is the file had been created using a compression library which hadn't been linked into the tar implementation that you're running... which is a distinct possibility if you're not using a prebuilt port to Windows of GNU Tar.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018