modules/compress/compress.c File Reference

#include <string.h>
#include <errno.h>
#include <zlib.h>
#include "lib/eggdrop/module.h"
#include "compress.h"
#include "tclcompress.c"

Go to the source code of this file.

Defines

#define MODULE_NAME   "compress"
#define MAKING_COMPRESS
#define start   compress_LTX_start
#define BUFLEN   512

Functions

static int uncompress_to_file (char *f_src, char *f_target)
static int compress_to_file (char *f_src, char *f_target, int mode_num)
static int compress_file (char *filename, int mode_num)
static int uncompress_file (char *filename)
static int is_compressedfile (char *filename)
static void adjust_mode_num (int *mode)
static int compress_report (int idx, int details)
static char * compress_close ()
EXPORT_SCOPE char * start ()
char * start (eggdrop_t *eggdrop)

Variables

static const char rcsid [] = "$Id: compress.c,v 1.10 2003-12-11 00:49:10 wcc Exp $"
static eggdrop_t * egg = NULL
static unsigned int compressed_files
static unsigned int uncompressed_files
static unsigned int compress_level
static tcl_ints my_tcl_ints []
static Function compress_table []


Define Documentation

#define BUFLEN   512

Definition at line 50 of file compress.c.

Referenced by compress_to_file(), and uncompress_to_file().

#define MAKING_COMPRESS

Definition at line 33 of file compress.c.

#define MODULE_NAME   "compress"

Definition at line 32 of file compress.c.

#define start   compress_LTX_start

Definition at line 48 of file compress.c.


Function Documentation

static void adjust_mode_num ( int *  mode  )  [inline, static]

Definition at line 178 of file compress.c.

Referenced by compress_to_file().

00179 {
00180   if (*mode > 9)
00181     *mode = 9;
00182   else if (*mode < 0)
00183     *mode = 0;
00184 }

static char* compress_close (  )  [static]

Definition at line 359 of file compress.c.

References MODULE_NAME, my_tcl_cmds, and NULL.

00360 {
00361   rem_help_reference("compress.help");
00362   rem_tcl_commands(my_tcl_cmds);
00363   rem_tcl_ints(my_tcl_ints);
00364   module_undepend(MODULE_NAME);
00365   return NULL;
00366 }

static int compress_file ( char *  filename,
int  mode_num 
) [static]

Definition at line 289 of file compress.c.

References COMPF_SUCCESS, compress_to_file, and movefile().

00290 {
00291   char *temp_fn, randstr[5];
00292   int   ret;
00293 
00294   /* Create temporary filename. */
00295   temp_fn = malloc(strlen(filename) + 5);
00296   make_rand_str(randstr, 4);
00297   strcpy(temp_fn, filename);
00298   strcat(temp_fn, randstr);
00299 
00300   /* Compress file. */
00301   ret = compress_to_file(filename, temp_fn, mode_num);
00302 
00303   /* Overwrite old file with compressed version.  Only do so
00304    * if the compression routine succeeded.
00305    */
00306   if (ret == COMPF_SUCCESS)
00307     movefile(temp_fn, filename);
00308 
00309   free(temp_fn);
00310   return ret;
00311 }

static int compress_report ( int  idx,
int  details 
) [static]

Definition at line 348 of file compress.c.

References compressed_files, P_, and uncompressed_files.

00349 {
00350   if (details) {
00351     dprintf(idx, P_("    Compressed %u file,", "    Compressed %u files,",
00352                     compressed_files), compressed_files);
00353     dprintf(idx, P_(" uncompressed %u file.\n", " uncompressed %u files.\n",
00354                     uncompressed_files), uncompressed_files);
00355   }
00356   return 0;
00357 }

static int compress_to_file ( char *  f_src,
char *  f_target,
int  mode_num 
) [static]

Definition at line 221 of file compress.c.

References _, adjust_mode_num(), BUFLEN, COMPF_ERROR, COMPF_SUCCESS, compressed_files, is_file(), LOG_MISC, and putlog().

00222 {
00223   char  buf[BUFLEN], mode[5];
00224   FILE *fin, *fout;
00225   int   len;
00226 
00227   adjust_mode_num(&mode_num);
00228   snprintf(mode, sizeof mode, "wb%d", mode_num);
00229 
00230   if (!is_file(f_src)) {
00231     putlog(LOG_MISC, "*", _("Failed to compress file `%s': not a file."),
00232      f_src);
00233     return COMPF_ERROR;
00234   }
00235   fin = fopen(f_src, "rb");
00236   if (!fin) {
00237     putlog(LOG_MISC, "*", _("Failed to compress file `%1$s': open failed: %2$s."),
00238      f_src, strerror(errno));
00239     return COMPF_ERROR;
00240   }
00241 
00242   fout = gzopen(f_target, mode);
00243   if (!fout) {
00244     putlog(LOG_MISC, "*", _("Failed to compress file `%s': gzopen failed."),
00245      f_src);
00246     return COMPF_ERROR;
00247   }
00248 
00249 #ifdef HAVE_MMAP
00250   if (compress_to_file_mmap(fout, fin) == COMPF_SUCCESS) {
00251     compressed_files++;
00252     return COMPF_SUCCESS;
00253   } else {
00254     /* To be on the safe side, close the file before attempting
00255      * to write again.
00256      */
00257     gzclose(fout);
00258     fout = gzopen(f_target, mode);
00259   }
00260 #endif /* HAVE_MMAP */
00261 
00262   while (1) {
00263     len = fread(buf, 1, sizeof(buf), fin);
00264     if (ferror(fin)) {
00265       putlog(LOG_MISC, "*", _("Failed to compress file `%1$s': fread failed: %2$s"),
00266        f_src, strerror(errno));
00267       return COMPF_ERROR;
00268     }
00269     if (!len)
00270       break;
00271     if (gzwrite(fout, buf, (unsigned int) len) != len) {
00272       putlog(LOG_MISC, "*", _("Failed to compress file `%s': gzwrite failed."),
00273        f_src);
00274       return COMPF_ERROR;
00275     }
00276   }
00277   fclose(fin);
00278   if (gzclose(fout) != Z_OK) {
00279     putlog(LOG_MISC, "*", _("Failed to compress file `%s': gzclose failed."),
00280      f_src);
00281     return COMPF_ERROR;
00282   }
00283   compressed_files++;
00284   return COMPF_SUCCESS;
00285 }

static int is_compressedfile ( char *  filename  )  [static]

Definition at line 73 of file compress.c.

References COMPF_COMPRESSED, COMPF_FAILED, COMPF_UNCOMPRESSED, and is_file().

00074 {
00075   char      buf1[50], buf2[50];
00076   FILE     *fin;
00077   register int    len1, len2, i;
00078 
00079   memset(buf1, 0, 50);
00080   memset(buf2, 0, 50);
00081   if (!is_file(filename))
00082     return COMPF_FAILED;
00083 
00084   /* Read data with zlib routines.
00085    */
00086   fin = gzopen(filename, "rb");
00087   if (!fin)
00088     return COMPF_FAILED;
00089   len1 = gzread(fin, buf1, sizeof(buf1));
00090   if (len1 < 0)
00091     return COMPF_FAILED;
00092   if (gzclose(fin) != Z_OK)
00093     return COMPF_FAILED;
00094 
00095   /* Read raw data.
00096    */
00097   fin = fopen(filename, "rb");
00098   if (!fin)
00099     return COMPF_FAILED;
00100   len2 = fread(buf2, 1, sizeof(buf2), fin);
00101   if (ferror(fin))
00102     return COMPF_FAILED;
00103   fclose(fin);
00104 
00105   /* Compare what we found.
00106    */
00107   if (len1 != len2)
00108     return COMPF_COMPRESSED;
00109   for (i = 0; i < sizeof(buf1); i++)
00110     if (buf1[i] != buf2[i])
00111       return COMPF_COMPRESSED;
00112   return COMPF_UNCOMPRESSED;
00113 }

char* start ( eggdrop_t *  eggdrop  ) 

Definition at line 386 of file compress.c.

References _, compress_level, compressed_files, egg, MODULE_NAME, my_tcl_cmds, NULL, and uncompressed_files.

00387 {
00388   egg = eggdrop;
00389   compressed_files  = 0;
00390   uncompressed_files  = 0;
00391   compress_level  = 9;
00392 
00393   module_register(MODULE_NAME, compress_table, 1, 1);
00394   if (!module_depend(MODULE_NAME, "eggdrop", 107, 0)) {
00395     module_undepend(MODULE_NAME);
00396     return _("This module needs eggdrop1.7.0 or later");
00397   }
00398   add_tcl_ints(my_tcl_ints);
00399   add_tcl_commands(my_tcl_cmds);
00400   add_help_reference("compress.help");
00401   return NULL;
00402 }

EXPORT_SCOPE char* start (  ) 

static int uncompress_file ( char *  filename  )  [static]

Definition at line 315 of file compress.c.

References COMPF_SUCCESS, movefile(), and uncompress_to_file.

00316 {
00317   char *temp_fn, randstr[5];
00318   int   ret;
00319 
00320   /* Create temporary filename. */
00321   temp_fn = malloc(strlen(filename) + 5);
00322   make_rand_str(randstr, 4);
00323   strcpy(temp_fn, filename);
00324   strcat(temp_fn, randstr);
00325 
00326   /* Uncompress file. */
00327   ret = uncompress_to_file(filename, temp_fn);
00328 
00329   /* Overwrite old file with uncompressed version.  Only do so
00330    * if the uncompression routine succeeded.
00331    */
00332   if (ret == COMPF_SUCCESS)
00333     movefile(temp_fn, filename);
00334 
00335   free(temp_fn);
00336   return ret;
00337 }

static int uncompress_to_file ( char *  f_src,
char *  f_target 
) [static]

Definition at line 122 of file compress.c.

References _, BUFLEN, COMPF_ERROR, COMPF_SUCCESS, is_file(), LOG_MISC, putlog(), and uncompressed_files.

00123 {
00124   char buf[BUFLEN];
00125   int len;
00126   FILE *fin, *fout;
00127 
00128   if (!is_file(f_src)) {
00129     putlog(LOG_MISC, "*", _("Failed to uncompress file `%s': not a file."),
00130      f_src);
00131     return COMPF_ERROR;
00132   }
00133   fin = gzopen(f_src, "rb");
00134   if (!fin) {
00135     putlog(LOG_MISC, "*", _("Failed to uncompress file `%s': gzopen failed."),
00136      f_src);
00137     return COMPF_ERROR;
00138   }
00139 
00140   fout = fopen(f_target, "wb");
00141   if (!fout) {
00142     putlog(LOG_MISC, "*", _("Failed to uncompress file `%1$s': open failed: %2$s."),
00143      f_src, strerror(errno));
00144     return COMPF_ERROR;
00145   }
00146 
00147   while (1) {
00148     len = gzread(fin, buf, sizeof(buf));
00149     if (len < 0) {
00150       putlog(LOG_MISC, "*", _("Failed to uncompress file `%s': gzread failed."),
00151        f_src);
00152       return COMPF_ERROR;
00153     }
00154     if (!len)
00155       break;
00156     if ((int) fwrite(buf, 1, (unsigned int) len, fout) != len) {
00157       putlog(LOG_MISC, "*", _("Failed to uncompress file `%1$s': fwrite failed: %2$s."),
00158        f_src, strerror(errno));
00159       return COMPF_ERROR;
00160     }
00161   }
00162   if (fclose(fout)) {
00163     putlog(LOG_MISC, "*", _("Failed to uncompress file `%1$s': fclose failed: %2$s."),
00164      f_src, strerror(errno));
00165     return COMPF_ERROR;
00166   }
00167   if (gzclose(fin) != Z_OK) {
00168     putlog(LOG_MISC, "*", _("Failed to uncompress file `%s': gzclose failed."),
00169      f_src);
00170     return COMPF_ERROR;
00171   }
00172   uncompressed_files++;
00173   return COMPF_SUCCESS;
00174 }


Variable Documentation

unsigned int compress_level [static]

Definition at line 57 of file compress.c.

Referenced by start(), and tcl_compress_file().

unsigned int compressed_files [static]

Definition at line 55 of file compress.c.

Referenced by compress_report(), compress_to_file(), and start().

eggdrop_t* egg = NULL [static]

Definition at line 53 of file compress.c.

tcl_ints my_tcl_ints[] [static]

Initial value:

 {
  {"compress_level",    &compress_level},
  {NULL,                  NULL}
}

Definition at line 343 of file compress.c.

Referenced by channels_close(), and start().

const char rcsid[] = "$Id: compress.c,v 1.10 2003-12-11 00:49:10 wcc Exp $" [static]

Definition at line 29 of file compress.c.

unsigned int uncompressed_files [static]

Definition at line 56 of file compress.c.

Referenced by compress_report(), start(), and uncompress_to_file().


Generated on Sun Nov 30 18:43:35 2008 for eggdrop1.9 by  doxygen 1.5.6