OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
BESUncompress3BZ2.cc
Go to the documentation of this file.
1 // BESUncompress3BZ2.cc
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 //
23 // You can contact University Corporation for Atmospheric Research at
24 // 3080 Center Green Drive, Boulder, CO 80301
25 
26 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
27 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
28 //
29 // Authors:
30 // pwest Patrick West <pwest@ucar.edu>
31 // jgarcia Jose Garcia <jgarcia@ucar.edu>
32 
33 
34 #include "config.h"
35 
36 #ifdef HAVE_BZLIB_H
37 #include <bzlib.h>
38 #endif
39 
40 #include <cstring>
41 #include <cerrno>
42 #include <sstream>
43 #include <unistd.h>
44 
45 using std::ostringstream;
46 
47 #include "BESUncompress3BZ2.h"
48 #include "BESInternalError.h"
49 #include "BESDebug.h"
50 
51 #define CHUNK 4096
52 
53 static void bz_internal_error(int errcode)
54 {
55  ostringstream strm;
56  strm << "internal error in bz2 library occurred: " << errcode;
57  throw BESInternalError(strm.str(), __FILE__, __LINE__);
58 }
59 
66 void
67 BESUncompress3BZ2::uncompress( const string &src_name, int fd )
68 {
69 #ifndef HAVE_BZLIB_H
70  string err = "Unable to uncompress bz2 files, feature not built. Check config.h in bes directory for HAVE_BZLIB_H flag set to 1";
71  throw BESInternalError( err, __FILE__, __LINE__ );
72 #else
73  FILE *src = fopen( src_name.c_str(), "rb" );
74  if( !src )
75  {
76  char *serr = strerror( errno );
77  string err = "Unable to open the compressed file "
78  + src_name + ": ";
79  if( serr )
80  {
81  err.append( serr );
82  }
83  else
84  {
85  err.append( "unknown error occurred" );
86  }
87  throw BESInternalError( err, __FILE__, __LINE__ );
88  }
89 
90  int bzerror = 0; // any error flags will be stored here
91  int verbosity = 0; // 0 is silent up to 4 which is very verbose
92  int small = 0; // if non zero then memory management is different
93  void *unused = NULL; // any unused bytes would be stored in here
94  int nunused = 0; // the size of the unused buffer
95  char in[CHUNK]; // input buffer used to read uncompressed data in bzRead
96 
97  BZFILE *bsrc = NULL;
98 
99  bsrc = BZ2_bzReadOpen( &bzerror, src, verbosity, small, NULL, 0 );
100  if( bsrc == NULL )
101  {
102  const char *berr = BZ2_bzerror( bsrc, &bzerror );
103  string err = "bzReadOpen failed on " + src_name + ": ";
104  if( berr )
105  {
106  err.append( berr );
107  }
108  else
109  {
110  err.append( "Unknown error" );
111  }
112  fclose( src );
113 
114  throw BESInternalError( err, __FILE__, __LINE__ );
115  }
116 
117  bool done = false;
118  while( !done )
119  {
120  int bytes_read = BZ2_bzRead( &bzerror, bsrc, in, CHUNK );
121  if( bzerror != BZ_OK && bzerror != BZ_STREAM_END )
122  {
123  const char *berr = BZ2_bzerror( bsrc, &bzerror );
124  string err = "bzRead failed on " + src_name + ": ";
125  if( berr )
126  {
127  err.append( berr );
128  }
129  else
130  {
131  err.append( "Unknown error" );
132  }
133 
134  BZ2_bzReadClose( &bzerror, bsrc );
135  fclose( src );
136 
137  throw BESInternalError( err, __FILE__, __LINE__ );
138  }
139  //if( bytes_read == 0 || bzerror == BZ_STREAM_END )
140  if( bzerror == BZ_STREAM_END )
141  {
142  done = true;
143  }
144  int bytes_written = write(fd, in, bytes_read);
145  if( bytes_written < bytes_read )
146  {
147  ostringstream strm;
148  strm << "Error writing uncompressed data: "
149  << "wrote " << bytes_written
150  << " instead of " << bytes_read;
151 
152  BZ2_bzReadClose( &bzerror, bsrc );
153  fclose( src );
154 
155  throw BESInternalError( strm.str(), __FILE__, __LINE__ );
156  }
157  }
158 
159  BZ2_bzReadClose( &bzerror, bsrc );
160  fclose( src );
161 #endif
162 }
163 
exception thrown if inernal error encountered
void bz_internal_error(int errcode)
static void uncompress(const string &src, int fd)
uncompress a file with the .bz2 file extension
#define CHUNK