Sign in Join

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

Mailing lists / fpc-pascal / 2019-10

Improve THandleStream.Size for VFS on Linux

2 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

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

Alexey Tor.

Ubuntu 18.x x64. FPC 3.3 trunk.
I get handle of '/proc/version' using func below (e.g. handle=22). Now I want to get file size of that file. and read all contents into THandleStream. But problem is: THandleSteam.Size gets 0. I guess THandleStream can override GetSize to get size smarter. Maybe someone knows how?
It will be very good, to access /proc/... by streams.

const
{$IF DEFINED(LINUX)}
   FD_CLOEXEC = 1;
   O_CLOEXEC  = &02000000;
{$ELSEIF DEFINED(FREEBSD)}
   O_CLOEXEC  = &04000000;
{$ELSEIF DEFINED(NETBSD)}
   O_CLOEXEC  = $00400000;
{$ELSE}
   O_CLOEXEC  = 0;
{$ENDIF}
function _CustomFileHandle(const FileName: string; Mode: LongWord): THandle;
const
   AccessModes: array[0..2] of cInt  = (
                 O_RdOnly,
                 O_WrOnly,
                 O_RdWr);
   OpenFlags: array[0..3] of cInt  = (
                 0,
                 O_SYNC,
                 O_DIRECT,
                 O_SYNC or O_DIRECT);
begin
   repeat
     Result:= fpOpen(UTF8ToSys(FileName), AccessModes[Mode and 3] or
                     OpenFlags[(Mode shr 16) and 3] or O_CLOEXEC);
   until (Result  -1) or (fpgeterrno  ESysEINTR);
end;
Regards, Alexey

Reply by email

Michael Van Canneyt
to Alexey Tor.
Alexey Tor. wrote 7 lines
Ubuntu 18.x x64. FPC 3.3 trunk. I get handle of '/proc/version' using func below (e.g. handle=22). Now I want to get file size of that file. and read all contents into THandleStream. But problem is: THandleSteam.Size gets 0. I guess THandleStream can override GetSize to get size smarter. Maybe someone knows how? It will be very good, to access /proc/... by streams.

Don't use size. Just read data till you get no more data.

Many streams do not have a size. Socket or IO streams as prime examples. Some of the encoding/decoding streams also do not have a real size.

Michael.

Reply by email