pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
lo_read error ???
3+ messages / 2 participants
[nested] [flat]

* lo_read error ???
@ 1999-05-20 15:47 root <andrea@ssto-dtcb.interbusiness.it>
  0 siblings, 0 replies; 3+ messages in thread

From: root @ 1999-05-20 15:47 UTC (permalink / raw)
  To: pgsql-interfaces

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





^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* lo_read error ???
@ 1999-05-20 15:55 root <andrea@ssto-dtcb.interbusiness.it>
  0 siblings, 0 replies; 3+ messages in thread

From: root @ 1999-05-20 15:55 UTC (permalink / raw)
  To: pgsql-interfaces

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







^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: lo_read error ???
@ 1999-08-26 12:14 Douglas Thomson <dougt@mugc.cc.monash.edu.au>
  0 siblings, 0 replies; 3+ messages in thread

From: Douglas Thomson @ 1999-08-26 12:14 UTC (permalink / raw)
  To: pgsql-interfaces; +Cc: andrea@ssto-dtcb.interbusiness.it

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!

Attachments:

Pg.xs.patch (text/plain, 666B) download 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
^ permalink raw reply [nested|flat] 3+ messages in thread


end of thread, other threads:[~1999-08-26 12:14 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
1999-05-20 15:47 lo_read error ??? root <andrea@ssto-dtcb.interbusiness.it>
1999-05-20 15:55 lo_read error ??? root <andrea@ssto-dtcb.interbusiness.it>
1999-08-26 12:14 Re: lo_read error ??? Douglas Thomson <dougt@mugc.cc.monash.edu.au>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox