src/manager/src/untar.c

Go to the documentation of this file.
00001 /*******************************************************************************
00002 This file is part of mDictionary
00003 
00004 mDictionary is free software; you can redistribute it and/or modify
00005 it under the terms of the GNU General Public License as published by
00006 the Free Software Foundation; either version 2 of the License, or
00007 (at your option) any later version.
00008 
00009 mDictionary is distributed in the hope that it will be useful, 
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00012 GNU General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public License 
00015 along with WhiteStork; if not, write to the Free Software
00016 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00017 
00018 Copyright 2006-2008 ComArch S.A.
00019 *******************************************************************************/
00023 
00027 #include <untar.h>
00028 
00029 
00030 
00038 static gint unpack_file_contents( BZFILE *bz2_file,
00039                                   TarHeader *header,
00040                                   gchar *out_name )
00041 {
00042         gchar* buffer = NULL;
00043         guint i = 0;
00044         gint im = 0;
00045         FILE *file_out;
00046         gulong length = 0;
00047         guint result = 0;
00048         gint bzerror = 0;
00049 
00050         file_out = fopen(out_name, "w");
00051 
00052         if (file_out == NULL) return -1;
00053 
00054         sscanf(header->size, "%12lo", &length);
00055         g_debug("File size: %ld\n", length);
00056 
00057         im = length / BLOCK_SIZE;
00058         for (i = 0; i < im; ++i)
00059         {
00060                 buffer = (gchar*) g_try_malloc(BLOCK_SIZE * sizeof(gchar));
00061                 
00062                 result = BZ2_bzRead( &bzerror,
00063                                      bz2_file,
00064                                      buffer,
00065                                      BLOCK_SIZE * sizeof (gchar) );
00066 
00067                 fwrite (buffer, BLOCK_SIZE * sizeof(gchar), 1, file_out);
00068                 g_free (buffer);
00069         }
00070 
00071         i = length % BLOCK_SIZE;
00072         if (i != 0)
00073         {
00074                 buffer = (gchar*) g_try_malloc (BLOCK_SIZE * sizeof(gchar));
00075                 if (NULL == buffer)
00076                 {
00077                         g_debug("Memory not allocated");
00078                         return -100;
00079                 };
00080                 
00081                 result = BZ2_bzRead( &bzerror,
00082                                      bz2_file,
00083                                      buffer,
00084                                      BLOCK_SIZE * sizeof(gchar) );
00085                 if (BZ_OK == bzerror)
00086                 {
00087                         fwrite (buffer, i * sizeof(gchar), 1, file_out);
00088                 }
00089                 else
00090                 {
00091                         g_debug("bzerror = %d", bzerror);
00092                         return -200;
00093                 };
00094                 g_free(buffer);
00095         }
00096 
00097         fclose(file_out);
00098         return 0;
00099 };
00100 
00119 gint decompress_file (gchar *in_file, gchar **out_path)
00120 {
00121         FILE *file;
00122         BZFILE  *bz2_file;
00123         guint result;
00124         gint ret = 0;
00125         TarHeader* header;
00126         gint bzerror;
00127         gchar* dest_dir = NULL;
00128 
00129         header = (TarHeader*) g_try_malloc (BLOCK_SIZE * sizeof(gchar));
00130         if (NULL == header)
00131         {
00132                 g_debug("\nCould not allocate memory\n");
00133                 return -1;
00134         }
00135 
00136         file = fopen (in_file, "rb");
00137         if (NULL == file)
00138         {
00139                 g_debug("There was an error while trying to read archive file");
00140                 g_free(header); header = NULL;
00141                 return -2;
00142         };
00143 
00144         bz2_file = BZ2_bzReadOpen (&bzerror, file, 0, 0, NULL, 0);
00145         if ( BZ_OK != bzerror )
00146         {
00147                 fclose(file);
00148                 g_free(header); header = NULL;
00149                 g_debug("There was an error while reading compressed file\n");
00150                 return -3;
00151         }
00152 
00153         /* read archive and exctract all files in it */
00154         while (TRUE)
00155         {
00156                 /* get info about next file/directory in the archive */
00157                 result = BZ2_bzRead( &bzerror,
00158                                      bz2_file,
00159                                      header,
00160                                      BLOCK_SIZE * sizeof (char) );
00161                 if ( BZ_OK == bzerror )
00162                 {
00163                         if (strlen(header->name) == 0)
00164                         {
00165                                 g_debug("\nFilename length is 0, exitting\n");
00166                                 break;
00167                         };
00168                         gchar *temp = g_strconcat( *out_path,
00169                                                    header->name,
00170                                                    NULL );
00171 
00172                         /* exctract file or create new directory */
00173                         switch (header->typeflag)
00174                         {
00175                                 case File:
00176                                         unpack_file_contents( bz2_file,
00177                                                               header,
00178                                                               temp );
00179                                 break;
00180                                 case Dir:
00181                                         if (0 !=
00182                                                 mkdir( temp,
00183                                                        S_IRUSR|S_IWUSR|S_IXUSR )
00184                                         ) {
00185                                                 g_debug("Couldnt create dir:%s",
00186                                                          temp);
00187                                                 g_free(header);
00188                                                 BZ2_bzReadClose( &bzerror,
00189                                                                  bz2_file );
00190                                                 fclose(file);
00191                                                 return -4;
00192                                         }
00193                                         else {
00194                                                 /* get last created directory */
00195                                                 if(NULL != dest_dir) {
00196                                                         g_free(dest_dir);
00197                                                 }
00198                                                 dest_dir = g_strdup(temp);
00199                                                 g_debug( "dest_dir %s \n",
00200                                                          dest_dir );
00201                                         }
00202                                 break;
00203                                 default:
00204                                         g_debug("Untar:Wrong type of content!");
00205                                 break;
00206                         }
00207                         g_free(temp);
00208                         temp = NULL;
00209                 }
00210                 else if ( bzerror != BZ_STREAM_END )
00211                 {
00212                         g_debug("\nBZ2 READ_CLOSE(stream end) %d\n", bzerror);
00213                         BZ2_bzReadClose ( &bzerror, bz2_file );
00214                         break;
00215                 }
00216                 else
00217                 {
00218                         g_debug("\nExitting, error nr %d\n", bzerror);
00219                         BZ2_bzReadClose ( &bzerror, bz2_file );
00220                         ret = -5;
00221                         break;
00222                 };
00223         };
00224 
00225         /* put newly created directory path into out_path parameter */
00226         if ((0 == ret) && (dest_dir != NULL))
00227         {
00228                 g_free(*out_path); *out_path = NULL;
00229                 *out_path = g_strdup(dest_dir);
00230                 g_free(dest_dir); dest_dir = NULL;
00231         }
00232 
00233         g_free(header); header = NULL;
00234         fclose(file);
00235         return ret;
00236 };
00237 

Generated on Fri Jan 11 14:30:17 2008 for mDictionary Project by  doxygen 1.5.1