Skip site navigation (1) Skip section navigation (2)

Re: lo_read error ???

From: Douglas Thomson <dougt(at)mugc(dot)cc(dot)monash(dot)edu(dot)au>
To: pgsql-interfaces(at)postgresql(dot)org
Cc: andrea(at)ssto-dtcb(dot)interbusiness(dot)it
Subject: Re: lo_read error ???
Date: 1999-08-26 12:14:56
Message-ID: 37C52FC0.3F33DAED@mugc.cc.monash.edu.au (view raw or flat)
Thread:
Lists: pgsql-interfaces
In a message that I found in the archives:
    http://www.PostgreSQL.ORG/mhonarc/pgsql-interfaces/1999-05/msg00230.html
Andrea Partinico writes:
> Hi to all,
> 
> I'm trying to use large objects this way:
> 
> -----------------------
> #!/usr/bin/perl
> $ENV{'PGDATESTYLE'} = "European";
> use CGI;
> use Pg;
> $base = new CGI;
> $fax = $base->url_param('fax');
> $page = $base->url_param('page');
> $conn = Pg::connectdb("dbname=www host=localhost");
> $select = $conn->exec("SELECT faxpage FROM faxric where codfax='$fax'");
> 
> $faxpage = $select->fetchrow();
> $faxpage_fd = $conn->lo_open($faxpage, PGRES_INV_READ);
> if ($faxpage_fd < 0) {
>  print $base->header('text/plain'),
>  "Errore aprendo file su db";
>  exit;
> }
> print $base->header(-type=>'image/gif');
> while(($ret = $conn->lo_read($faxpage_fd, $buf, 1024)) >0 ) {
>  print "$buf";
> }
> $ret = $conn->lo_close($faxpage_fd);
> 
> ------------------------
> 
> and I get only an icon, but not the image
> so I modified the script ...
> 
> ------------------------
> #!/usr/bin/perl
> $ENV{'PGDATESTYLE'} = "European";
> use CGI;
> use Pg;
> $base = new CGI;
> $fax = $base->url_param('fax');
> $page = $base->url_param('page');
> $conn = Pg::connectdb("dbname=www host=localhost");
> $select = $conn->exec("SELECT faxpage FROM faxric where codfax='$fax'");
> 
> $faxpage = $select->fetchrow();
> $faxpage_fd = $conn->lo_open($faxpage, PGRES_INV_READ);
> if ($faxpage_fd < 0) {
>  print $base->header('text/plain'),
>  "Errore aprendo file su db";
>  exit;
> }
> print $base->header(-type=>'text/plain');
> while(($ret = $conn->lo_read($faxpage_fd, $buf, 1024)) >0 ) {
>  $test = length $buf;
>  print "$ret -- $test\n";
> }
> $ret = $conn->lo_close($faxpage_fd);
> ------------------------
> 
> and I get this output:
> 
> 1024 -- 11
> 1024 -- 179
> 1024 -- 172
> 1024 -- 187
> 1024 -- 120
> 1024 -- 232
> 1024 -- 182
> 1024 -- 98
> 1024 -- 15
> 1024 -- 129
> 1024 -- 241
> 1024 -- 315
> 1024 -- 221
> 1024 -- 1002
> 1024 -- 13
> 190 -- 110
> 
> ------------------------
> why $buf do not contain all characters that lo_read() return ???
> 
> system is Linux RedHat 6.0, whit Postgres 6.4.2
> (I've also tryed to compile it myself, but nothing change)
> 
> large objects are gif images
> ------------------------
> 
> Many tanks in advance to all.
> Andrea Partinico
> andrea(at)ssto-dtcb(dot)interbusiness(dot)it

The explanation is probably fairly simple: the BLOB is getting 
truncated at the first NUL character it contains! The reason for 
this is that lo_read in Pg.xs is using the default:
    OUTPUT:
        RETVAL
        buf
which uses C's strlen() to work out the length of the scalar.

The code ought to read something more like:
    OUTPUT:
        RETVAL
        buf sv_setpvn((SV*)ST(2), buf, RETVAL);

I am not sure if this needs to be done on both lo_read methods
in this file, but I changed both and have not since had any
problems with truncated BLOBs.

I will attach the (trivial) patch file in case that is useful to
anyone.

Let us know if this fixes your problem...

Douglas Thomson
Lecturer in Computing
Monash University, Gippsland Campus, AUSTRALIA

P.S. Several months ago I reported this problem, along with my 
     suggested patch, to E(dot)Mergl(at)bawue(dot)de (who as far as I know 
     is the code maintainer) but I received no response (neither 
     e-mail nor any sign of the bug getting fixed) so I suspect 
     my mail did not get through... if anyone can call this
     patch to the attention of anyone who can fix the original
     I would greatly appreciate it!

Attachment: Pg.xs.patch
Description: text/plain (667 bytes)

pgsql-interfaces by date

Next:From: Marten FeldtmannDate: 1999-08-26 13:11:37
Subject: Problems with ODBC connect ...
Previous:From: Moray McConnachieDate: 1999-08-26 09:36:30
Subject: RE: [INTERFACES] Access 2000

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group