]> git.imager.perl.org - imager.git/commitdiff
- reading a raw image no longer exits on a short read or read error,
authorTony Cook <tony@develop=help.com>
Wed, 18 Jan 2006 14:06:50 +0000 (14:06 +0000)
committerTony Cook <tony@develop=help.com>
Wed, 18 Jan 2006 14:06:50 +0000 (14:06 +0000)
  and returns an appropriate error message in $im->errstr
- write failures when writing a raw image now return a useful
  message in $im->errstr

Changes
Imager.pm
iolayer.c
raw.c
t/t103raw.t

diff --git a/Changes b/Changes
index c1bdb15d1717db82a8e0c2e197a8be3b5550b017..ba5216704609f9c454a4b7df1ebd9e35ec8a1cc7 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1307,6 +1307,10 @@ Revision history for Perl extension Imager.
 - error messages when writing PNM images were always
   'unable to write pnm image', more useful messages are now reported.
 - convert t/t103raw.t to Test::More
+- reading a raw image no longer exits on a short read or read error,
+  and returns an appropriate error message in $im->errstr
+- write failures when writing a raw image now return a useful
+  message in $im->errstr
 
 =================================================================
 
index 244d14cd6ce0f1cd4fb67e3c7209f407a5a6c032..010244527071eb812f23d8d75cdc782aacb2746c 100644 (file)
--- a/Imager.pm
+++ b/Imager.pm
@@ -1311,7 +1311,7 @@ sub read {
                                   $params{storechannels},
                                   $params{interleave});
     if ( !defined($self->{IMG}) ) {
-      $self->{ERRSTR}='unable to read raw image';
+      $self->{ERRSTR}=$self->_error_as_msg();
       return undef;
     }
     $self->{DEBUG} && print "loading a raw file\n";
@@ -1468,7 +1468,7 @@ sub write {
     $self->_set_opts(\%input, "raw_", $self)
       or return undef;
     if ( !i_writeraw_wiol($self->{IMG},$IO) ) {
-      $self->{ERRSTR}='unable to write raw image';
+      $self->{ERRSTR} = $self->_error_as_msg();
       return undef;
     }
     $self->{DEBUG} && print "writing a raw file\n";
index 1bbda1158300e4818a4c179b781d3fbeaf220b1f..a425bff8a10ca140620e43e8c1662b73345474c4 100644 (file)
--- a/iolayer.c
+++ b/iolayer.c
@@ -1064,11 +1064,19 @@ io_slurp(io_glue *ig, unsigned char **c) {
 =cut
 */
 static ssize_t fd_read(io_glue *ig, void *buf, size_t count) {
+  ssize_t result;
 #ifdef _MSC_VER
-  return _read(ig->source.fdseek.fd, buf, count);
+  result = _read(ig->source.fdseek.fd, buf, count);
 #else
-  return read(ig->source.fdseek.fd, buf, count);
+  result = read(ig->source.fdseek.fd, buf, count);
 #endif
+
+  /* 0 is valid - means EOF */
+  if (result < 0) {
+    i_push_errorf(0, "read() failure: %s (%d)", strerror(errno), errno);
+  }
+
+  return result;
 }
 
 static ssize_t fd_write(io_glue *ig, const void *buf, size_t count) {
diff --git a/raw.c b/raw.c
index 8b363d5a96b2d3c20a780fcccd97674c5c3c5c60..ac799ac4a4979bad98cd0af3d21a8430d6a331de 100644 (file)
--- a/raw.c
+++ b/raw.c
@@ -53,6 +53,8 @@ i_img *
 i_readraw_wiol(io_glue *ig, int x, int y, int datachannels, int storechannels, int intrl) {
   i_img* im;
   int rc,k;
+
+  i_clear_error();
   
   unsigned char *inbuffer;
   unsigned char *ilbuffer;
@@ -83,7 +85,17 @@ i_readraw_wiol(io_glue *ig, int x, int y, int datachannels, int storechannels, i
   k=0;
   while( k<im->ysize ) {
     rc = ig->readcb(ig, inbuffer, inbuflen);
-    if (rc != inbuflen) { fprintf(stderr,"Premature end of file.\n"); exit(2); }
+    if (rc != inbuflen) { 
+      if (rc < 0)
+       i_push_error(0, "error reading file");
+      else
+       i_push_error(0, "premature end of file");
+      i_img_destroy(im);
+      myfree(inbuffer);
+      if (intrl != 0) myfree(ilbuffer);
+      if (datachannels != storechannels) myfree(exbuffer);
+      return NULL;
+    }
     interleave(inbuffer,ilbuffer,im->xsize,datachannels);
     expandchannels(ilbuffer,exbuffer,im->xsize,datachannels,storechannels);
     /* FIXME: Do we ever want to save to a virtual image? */
index b565cbf730e31da7cfb8047ebd083991b399cca1..94996ff3fb928bd25b02cdc640d7d558b18074ff 100644 (file)
@@ -1,7 +1,7 @@
 #!perl -w
 use strict;
 use lib 't';
-use Test::More tests => 17;
+use Test::More tests => 23;
 use Imager qw(:all);
 init_log("testout/t103raw.log",1);
 
@@ -152,6 +152,33 @@ SKIP:
   }
 }
 
+{ # error handling checks
+  # should get an error writing to a open for read file
+  # make a empty file
+  open RAW, "> testout/t103_empty.raw"
+    or die "Cannot create testout/t103_empty.raw: $!";
+  close RAW;
+  open RAW, "< testout/t103_empty.raw"
+    or die "Cannot open testout/t103_empty.raw: $!";
+  my $im = Imager->new(xsize => 50, ysize=>50);
+  ok(!$im->write(fh => \*RAW, type => 'raw'),
+     "write to open for read handle");
+  cmp_ok($im->errstr, '=~', '^Could not write to file: write\(\) failure', 
+        "check error message");
+  close RAW;
+
+  # should get an error reading an empty file
+  ok(!$im->read(file => 'testout/t103_empty.raw', xsize => 50, ysize=>50, type=>'raw'),
+     'read an empty file');
+  is($im->errstr, 'premature end of file', "check message");
+  open RAW, "> testout/t103_empty.raw"
+    or die "Cannot create testout/t103_empty.raw: $!";
+  ok(!$im->read(fh => \*RAW, , xsize => 50, ysize=>50, type=>'raw'),
+     'read a file open for write');
+  cmp_ok($im->errstr, '=~', '^error reading file: read\(\) failure', "check message");
+  
+}
+
 sub read_test {
   my ($in, $xsize, $ysize, $data, $store, $intrl, $base) = @_;
   open FH, $in or die "Cannot open $in: $!";