Received: from mugca.cc.monash.edu.au (root@mugca.cc.monash.edu.au [130.194.208.7]) by hub.org (8.9.3/8.9.3) with ESMTP id IAA72271 for ; Thu, 26 Aug 1999 08:18:43 -0400 (EDT) (envelope-from dougt@mugc.cc.monash.edu.au) Received: from mugc.cc.monash.edu.au (dougt@pc-205-25.fcit.monash.edu.au [130.194.205.25]) by mugca.cc.monash.edu.au (8.9.3/8.9.3) with ESMTP id WAA00808; Thu, 26 Aug 1999 22:18:22 +1000 (EST) Sender: dougt@mugc.cc.monash.edu.au Message-ID: <37C52FC0.3F33DAED@mugc.cc.monash.edu.au> Date: Thu, 26 Aug 1999 22:14:56 +1000 From: Douglas Thomson Organization: Monash University, Gippsland Campus X-Mailer: Mozilla 4.07 [en] (X11; U; Linux 2.0.36 i686) MIME-Version: 1.0 To: pgsql-interfaces@postgresql.org CC: andrea@ssto-dtcb.interbusiness.it Subject: Re: lo_read error ??? Content-Type: multipart/mixed; boundary="------------09577247BC27B13FD1F68C08" This is a multi-part message in MIME format. --------------09577247BC27B13FD1F68C08 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit 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@ssto-dtcb.interbusiness.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.Mergl@bawue.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! --------------09577247BC27B13FD1F68C08 Content-Type: text/plain; charset=us-ascii; name="Pg.xs.patch" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="Pg.xs.patch" diff -Naur perl5/Pg.xs perl5.fixed/Pg.xs --- perl5/Pg.xs Sat Aug 14 12:46:35 1999 +++ perl5.fixed/Pg.xs Thu Aug 26 21:35:33 1999 @@ -1,6 +1,6 @@ /*------------------------------------------------------- * - * $Id: Pg.xs,v 1.12 1999/02/19 23:27:17 tgl Exp $ + * $Id: Pg.xs,v 1.12 1999/02/19 23:27:17 tgl Exp $ with patch for NULs * * Copyright (c) 1997, 1998 Edmund Mergl * @@ -643,7 +643,7 @@ } OUTPUT: RETVAL - buf + buf sv_setpvn((SV*)ST(2), buf, RETVAL); /* to handle NULs */ int lo_write(conn, fd, buf, len) @@ -1029,7 +1029,7 @@ } OUTPUT: RETVAL - buf + buf sv_setpvn((SV*)ST(2), buf, RETVAL); /* to handle NULs */ int --------------09577247BC27B13FD1F68C08--