bes  Updated for version 3.20.5
BESUncompress3BZ2.cc
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 #if 0
54 static void bz_internal_error(int errcode)
55 {
56  ostringstream strm;
57  strm << "internal error in bz2 library occurred: " << errcode;
58  throw BESInternalError(strm.str(), __FILE__, __LINE__);
59 }
60 #endif
61 
68 void
69 BESUncompress3BZ2::uncompress( const string &src_name, int fd )
70 {
71 #ifndef HAVE_BZLIB_H
72  string err = "Unable to uncompress bz2 files, feature not built. Check config.h in bes directory for HAVE_BZLIB_H flag set to 1";
73  throw BESInternalError( err, __FILE__, __LINE__ );
74 #else
75  FILE *src = fopen( src_name.c_str(), "rb" );
76  if( !src )
77  {
78  char *serr = strerror( errno );
79  string err = "Unable to open the compressed file "
80  + src_name + ": ";
81  if( serr )
82  {
83  err.append( serr );
84  }
85  else
86  {
87  err.append( "unknown error occurred" );
88  }
89  throw BESInternalError( err, __FILE__, __LINE__ );
90  }
91 
92  int bzerror = 0; // any error flags will be stored here
93  int verbosity = 0; // 0 is silent up to 4 which is very verbose
94  int small = 0; // if non zero then memory management is different
95  // unused void *unused = NULL; // any unused bytes would be stored in here
96  // unused int nunused = 0; // the size of the unused buffer
97  char in[CHUNK]; // input buffer used to read uncompressed data in bzRead
98 
99  BZFILE *bsrc = NULL;
100 
101  bsrc = BZ2_bzReadOpen( &bzerror, src, verbosity, small, NULL, 0 );
102  if( bsrc == NULL )
103  {
104  const char *berr = BZ2_bzerror( bsrc, &bzerror );
105  string err = "bzReadOpen failed on " + src_name + ": ";
106  if( berr )
107  {
108  err.append( berr );
109  }
110  else
111  {
112  err.append( "Unknown error" );
113  }
114  fclose( src );
115 
116  throw BESInternalError( err, __FILE__, __LINE__ );
117  }
118 
119  bool done = false;
120  while( !done )
121  {
122  int bytes_read = BZ2_bzRead( &bzerror, bsrc, in, CHUNK );
123  if( bzerror != BZ_OK && bzerror != BZ_STREAM_END )
124  {
125  const char *berr = BZ2_bzerror( bsrc, &bzerror );
126  string err = "bzRead failed on " + src_name + ": ";
127  if( berr )
128  {
129  err.append( berr );
130  }
131  else
132  {
133  err.append( "Unknown error" );
134  }
135 
136  BZ2_bzReadClose( &bzerror, bsrc );
137  fclose( src );
138 
139  throw BESInternalError( err, __FILE__, __LINE__ );
140  }
141  //if( bytes_read == 0 || bzerror == BZ_STREAM_END )
142  if( bzerror == BZ_STREAM_END )
143  {
144  done = true;
145  }
146  int bytes_written = write(fd, in, bytes_read);
147  if( bytes_written < bytes_read )
148  {
149  ostringstream strm;
150  strm << "Error writing uncompressed data: "
151  << "wrote " << bytes_written
152  << " instead of " << bytes_read;
153 
154  BZ2_bzReadClose( &bzerror, bsrc );
155  fclose( src );
156 
157  throw BESInternalError( strm.str(), __FILE__, __LINE__ );
158  }
159  }
160 
161  BZ2_bzReadClose( &bzerror, bsrc );
162  fclose( src );
163 #endif
164 }
165 
BESInternalError
exception thrown if inernal error encountered
Definition: BESInternalError.h:43
BESUncompress3BZ2::uncompress
static void uncompress(const string &src, int fd)
uncompress a file with the .bz2 file extension
Definition: BESUncompress3BZ2.cc:69