#include <string.h>
#include <errno.h>
#include <zlib.h>
#include "src/mod/module.h"
#include "share.mod/share.h"
#include "compress.h"
#include "tclcompress.c"
Go to the source code of this file.
Defines | |
#define | MODULE_NAME "compress" |
#define | MAKING_COMPRESS |
#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 | uff_comp (int idx, char *filename) |
static int | uff_uncomp (int idx, char *filename) |
static int | uff_ask_compress (int idx) |
static int | compress_expmem (void) |
static int | compress_report (int idx, int details) |
static char * | compress_close () |
EXPORT_SCOPE char * | compress_start () |
char * | compress_start (Function *global_funcs) |
Variables | |
static Function * | global = 0 |
static Function * | share_funcs = 0 |
static unsigned int | compressed_files |
static unsigned int | uncompressed_files |
static unsigned int | share_compressed |
static unsigned int | compress_level |
static uff_table_t | compress_uff_table [] |
static tcl_ints | my_tcl_ints [] |
static Function | compress_table [] |
#define BUFLEN 512 |
Definition at line 48 of file compress.c.
Referenced by compress_to_file(), and uncompress_to_file().
#define MAKING_COMPRESS |
Definition at line 30 of file compress.c.
#define MODULE_NAME "compress" |
Definition at line 29 of file compress.c.
static void adjust_mode_num | ( | int * | mode | ) | [inline, static] |
Definition at line 176 of file compress.c.
Referenced by compress_to_file().
static char* compress_close | ( | ) | [static] |
Definition at line 396 of file compress.c.
References MODULE_NAME, module_undepend, NULL, rem_help_reference, rem_tcl_commands, rem_tcl_ints, and uff_deltable().
00397 { 00398 rem_help_reference("compress.help"); 00399 rem_tcl_commands(my_tcl_cmds); 00400 rem_tcl_ints(my_tcl_ints); 00401 uff_deltable(compress_uff_table); 00402 00403 module_undepend(MODULE_NAME); 00404 return NULL; 00405 }
static int compress_expmem | ( | void | ) | [static] |
Definition at line 376 of file compress.c.
Referenced by compress_report().
static int compress_file | ( | char * | filename, | |
int | mode_num | |||
) | [static] |
Definition at line 286 of file compress.c.
References COMPF_SUCCESS, compress_to_file(), make_rand_str, movefile, nfree, and nmalloc.
Referenced by uff_comp().
00287 { 00288 char *temp_fn, randstr[5]; 00289 int ret; 00290 00291 /* Create temporary filename. */ 00292 temp_fn = nmalloc(strlen(filename) + 5); 00293 make_rand_str(randstr, 4); 00294 strcpy(temp_fn, filename); 00295 strcat(temp_fn, randstr); 00296 00297 /* Compress file. */ 00298 ret = compress_to_file(filename, temp_fn, mode_num); 00299 00300 /* Overwrite old file with compressed version. Only do so 00301 * if the compression routine succeeded. 00302 */ 00303 if (ret == COMPF_SUCCESS) 00304 movefile(temp_fn, filename); 00305 00306 nfree(temp_fn); 00307 return ret; 00308 }
static int compress_report | ( | int | idx, | |
int | details | |||
) | [static] |
Definition at line 381 of file compress.c.
References compress_expmem(), compressed_files, dprintf, and uncompressed_files.
00382 { 00383 if (details) { 00384 int size = compress_expmem(); 00385 00386 dprintf(idx, " %u file%s compressed\n", compressed_files, 00387 (compressed_files != 1) ? "s" : ""); 00388 dprintf(idx, " %u file%s uncompressed\n", uncompressed_files, 00389 (uncompressed_files != 1) ? "s" : ""); 00390 dprintf(idx, " Using %d byte%s of memory\n", size, 00391 (size != 1) ? "s" : ""); 00392 } 00393 return 0; 00394 }
char* compress_start | ( | Function * | global_funcs | ) |
Definition at line 424 of file compress.c.
References add_help_reference, add_tcl_commands, add_tcl_ints, compress_level, compressed_files, global, module_depend, MODULE_NAME, module_register, module_undepend, NULL, share_compressed, share_funcs, uff_addtable(), and uncompressed_files.
00425 { 00426 global = global_funcs; 00427 00428 compressed_files = 0; 00429 uncompressed_files = 0; 00430 share_compressed = 0; 00431 compress_level = 9; 00432 00433 module_register(MODULE_NAME, compress_table, 1, 1); 00434 if (!module_depend(MODULE_NAME, "eggdrop", 106, 0)) { 00435 module_undepend(MODULE_NAME); 00436 return "This module requires Eggdrop 1.6.0 or later."; 00437 } 00438 00439 share_funcs = module_depend(MODULE_NAME, "share", 2, 3); 00440 if (!share_funcs) { 00441 module_undepend(MODULE_NAME); 00442 return "This module requires share module 2.3 or later."; 00443 } 00444 00445 uff_addtable(compress_uff_table); 00446 add_tcl_ints(my_tcl_ints); 00447 add_tcl_commands(my_tcl_cmds); 00448 add_help_reference("compress.help"); 00449 return NULL; 00450 }
EXPORT_SCOPE char* compress_start | ( | ) |
static int compress_to_file | ( | char * | f_src, | |
char * | f_target, | |||
int | mode_num | |||
) | [static] |
Definition at line 219 of file compress.c.
References adjust_mode_num(), BUFLEN, COMPF_ERROR, COMPF_SUCCESS, compressed_files, egg_snprintf, is_file, LOG_MISC, and putlog.
Referenced by compress_file().
00220 { 00221 char buf[BUFLEN], mode[5]; 00222 FILE *fin, *fout; 00223 int len; 00224 00225 adjust_mode_num(&mode_num); 00226 egg_snprintf(mode, sizeof mode, "wb%d", mode_num); 00227 00228 if (!is_file(f_src)) { 00229 putlog(LOG_MISC, "*", "Failed to compress file `%s': not a file.", f_src); 00230 return COMPF_ERROR; 00231 } 00232 fin = fopen(f_src, "rb"); 00233 if (!fin) { 00234 putlog(LOG_MISC, "*", "Failed to compress file `%s': open failed: %s.", 00235 f_src, strerror(errno)); 00236 return COMPF_ERROR; 00237 } 00238 00239 fout = gzopen(f_target, mode); 00240 if (!fout) { 00241 putlog(LOG_MISC, "*", "Failed to compress file `%s': gzopen failed.", 00242 f_src); 00243 return COMPF_ERROR; 00244 } 00245 00246 #ifdef HAVE_MMAP 00247 if (compress_to_file_mmap(fout, fin) == COMPF_SUCCESS) { 00248 compressed_files++; 00249 return COMPF_SUCCESS; 00250 } else { 00251 /* To be on the safe side, close the file before attempting 00252 * to write again. 00253 */ 00254 gzclose(fout); 00255 fout = gzopen(f_target, mode); 00256 } 00257 #endif /* HAVE_MMAP */ 00258 00259 while (1) { 00260 len = fread(buf, 1, sizeof(buf), fin); 00261 if (ferror(fin)) { 00262 putlog(LOG_MISC, "*", "Failed to compress file `%s': fread failed: %s", 00263 f_src, strerror(errno)); 00264 return COMPF_ERROR; 00265 } 00266 if (!len) 00267 break; 00268 if (gzwrite(fout, buf, (unsigned int) len) != len) { 00269 putlog(LOG_MISC, "*", "Failed to compress file `%s': gzwrite failed.", 00270 f_src); 00271 return COMPF_ERROR; 00272 } 00273 } 00274 fclose(fin); 00275 if (gzclose(fout) != Z_OK) { 00276 putlog(LOG_MISC, "*", "Failed to compress file `%s': gzclose failed.", 00277 f_src); 00278 return COMPF_ERROR; 00279 } 00280 compressed_files++; 00281 return COMPF_SUCCESS; 00282 }
static int is_compressedfile | ( | char * | filename | ) | [static] |
Definition at line 72 of file compress.c.
References COMPF_COMPRESSED, COMPF_FAILED, COMPF_UNCOMPRESSED, egg_memset, and is_file.
00073 { 00074 char buf1[50], buf2[50]; 00075 FILE *fin; 00076 register int len1, len2, i; 00077 00078 egg_memset(buf1, 0, 50); 00079 egg_memset(buf2, 0, 50); 00080 if (!is_file(filename)) 00081 return COMPF_FAILED; 00082 00083 /* Read data with zlib routines. 00084 */ 00085 fin = gzopen(filename, "rb"); 00086 if (!fin) 00087 return COMPF_FAILED; 00088 len1 = gzread(fin, buf1, sizeof(buf1)); 00089 if (len1 < 0) 00090 return COMPF_FAILED; 00091 if (gzclose(fin) != Z_OK) 00092 return COMPF_FAILED; 00093 00094 /* Read raw data. 00095 */ 00096 fin = fopen(filename, "rb"); 00097 if (!fin) 00098 return COMPF_FAILED; 00099 len2 = fread(buf2, 1, sizeof(buf2), fin); 00100 if (ferror(fin)) 00101 return COMPF_FAILED; 00102 fclose(fin); 00103 00104 /* Compare what we found. 00105 */ 00106 if (len1 != len2) 00107 return COMPF_COMPRESSED; 00108 for (i = 0; i < sizeof(buf1); i++) 00109 if (buf1[i] != buf2[i]) 00110 return COMPF_COMPRESSED; 00111 return COMPF_UNCOMPRESSED; 00112 }
static int uff_ask_compress | ( | int | idx | ) | [static] |
Definition at line 353 of file compress.c.
References share_compressed.
00354 { 00355 if (share_compressed) 00356 return 1; 00357 else 00358 return 0; 00359 }
static int uff_comp | ( | int | idx, | |
char * | filename | |||
) | [static] |
Definition at line 341 of file compress.c.
References compress_file(), compress_level, dcc, and debug1.
00342 { 00343 debug1("Compressing user file for %s.", dcc[idx].nick); 00344 return compress_file(filename, compress_level); 00345 }
static int uff_uncomp | ( | int | idx, | |
char * | filename | |||
) | [static] |
Definition at line 347 of file compress.c.
References dcc, debug1, and uncompress_file().
00348 { 00349 debug1("Uncompressing user file from %s.", dcc[idx].nick); 00350 return uncompress_file(filename); 00351 }
static int uncompress_file | ( | char * | filename | ) | [static] |
Definition at line 312 of file compress.c.
References COMPF_SUCCESS, make_rand_str, movefile, nfree, nmalloc, and uncompress_to_file().
Referenced by uff_uncomp().
00313 { 00314 char *temp_fn, randstr[5]; 00315 int ret; 00316 00317 /* Create temporary filename. */ 00318 temp_fn = nmalloc(strlen(filename) + 5); 00319 make_rand_str(randstr, 4); 00320 strcpy(temp_fn, filename); 00321 strcat(temp_fn, randstr); 00322 00323 /* Uncompress file. */ 00324 ret = uncompress_to_file(filename, temp_fn); 00325 00326 /* Overwrite old file with uncompressed version. Only do so 00327 * if the uncompression routine succeeded. 00328 */ 00329 if (ret == COMPF_SUCCESS) 00330 movefile(temp_fn, filename); 00331 00332 nfree(temp_fn); 00333 return ret; 00334 }
static int uncompress_to_file | ( | char * | f_src, | |
char * | f_target | |||
) | [static] |
Definition at line 121 of file compress.c.
References BUFLEN, COMPF_ERROR, COMPF_SUCCESS, is_file, LOG_MISC, putlog, and uncompressed_files.
Referenced by uncompress_file().
00122 { 00123 char buf[BUFLEN]; 00124 int len; 00125 FILE *fin, *fout; 00126 00127 if (!is_file(f_src)) { 00128 putlog(LOG_MISC, "*", "Failed to uncompress file `%s': not a file.", f_src); 00129 return COMPF_ERROR; 00130 } 00131 fin = gzopen(f_src, "rb"); 00132 if (!fin) { 00133 putlog(LOG_MISC, "*", "Failed to uncompress file `%s': gzopen failed.", 00134 f_src); 00135 return COMPF_ERROR; 00136 } 00137 00138 fout = fopen(f_target, "wb"); 00139 if (!fout) { 00140 putlog(LOG_MISC, "*", "Failed to uncompress file `%s': open failed: %s.", 00141 f_src, strerror(errno)); 00142 return COMPF_ERROR; 00143 } 00144 00145 while (1) { 00146 len = gzread(fin, buf, sizeof(buf)); 00147 if (len < 0) { 00148 putlog(LOG_MISC, "*", "Failed to uncompress file `%s': gzread failed.", 00149 f_src); 00150 return COMPF_ERROR; 00151 } 00152 if (!len) 00153 break; 00154 if ((int) fwrite(buf, 1, (unsigned int) len, fout) != len) { 00155 putlog(LOG_MISC, "*", "Failed to uncompress file `%s': fwrite " 00156 "failed: %s.", f_src, strerror(errno)); 00157 return COMPF_ERROR; 00158 } 00159 } 00160 if (fclose(fout)) { 00161 putlog(LOG_MISC, "*", "Failed to uncompress file `%s': fclose failed: %s.", 00162 f_src, strerror(errno)); 00163 return COMPF_ERROR; 00164 } 00165 if (gzclose(fin) != Z_OK) { 00166 putlog(LOG_MISC, "*", "Failed to uncompress file `%s': gzclose failed.", 00167 f_src); 00168 return COMPF_ERROR; 00169 } 00170 uncompressed_files++; 00171 return COMPF_SUCCESS; 00172 }
unsigned int compress_level [static] |
Definition at line 56 of file compress.c.
Referenced by compress_start(), and uff_comp().
Function compress_table[] [static] |
{ (Function) compress_start, (Function) compress_close, (Function) compress_expmem, (Function) compress_report, (Function) compress_to_file, (Function) compress_file, (Function) uncompress_to_file, (Function) uncompress_file, (Function) is_compressedfile, }
Definition at line 409 of file compress.c.
uff_table_t compress_uff_table[] [static] |
{ {"compress", UFF_COMPRESS, uff_ask_compress, 100, uff_comp, uff_uncomp}, { 0 , 0, 0 , 0, 0 , 0 } }
Definition at line 361 of file compress.c.
unsigned int compressed_files [static] |
Definition at line 53 of file compress.c.
Referenced by compress_report(), compress_start(), and compress_to_file().
Function* global = 0 [static] |
Definition at line 51 of file compress.c.
tcl_ints my_tcl_ints[] [static] |
{ {"share-compressed", (int *)&share_compressed}, {"compress-level", (int *)&compress_level}, { 0 , 0 } }
Definition at line 370 of file compress.c.
unsigned int share_compressed [static] |
Definition at line 55 of file compress.c.
Referenced by compress_start(), and uff_ask_compress().
Function * share_funcs = 0 [static] |
Definition at line 51 of file compress.c.
Referenced by compress_start().
unsigned int uncompressed_files [static] |
Definition at line 54 of file compress.c.
Referenced by compress_report(), compress_start(), and uncompress_to_file().