Imported Upstream version 2.7
[ossec-hids.git] / src / monitord / compress_log.c
1 /* @(#) $Id: ./src/monitord/compress_log.c, 2011/09/08 dcid Exp $
2  */
3
4 /* Copyright (C) 2009 Trend Micro Inc.
5  * All right reserved.
6  *
7  * This program is a free software; you can redistribute it
8  * and/or modify it under the terms of the GNU General Public
9  * License (version 2) as published by the FSF - Free Software
10  * Foundation
11  */
12
13 #include "shared.h"
14 #include "monitord.h"
15 #include "os_zlib/os_zlib.h"
16
17
18 /* gzips a log file */
19 void OS_CompressLog(char *logfile)
20 {
21     FILE *log;
22     gzFile *zlog;
23
24     char logfileGZ[OS_FLSIZE + 1];
25     int len, err;
26
27     char buf[OS_MAXSTR + 1];
28
29
30     /* Do not compress */
31     if(mond.compress == 0)
32         return;
33
34
35     /* Clearing the memory */
36     memset(logfileGZ,'\0',OS_FLSIZE +1);
37     memset(buf, '\0', OS_MAXSTR + 1);
38
39
40     /* Setting the umask */
41     umask(0027);
42
43                 
44     /* Creating the gzip file name */
45     snprintf(logfileGZ, OS_FLSIZE, "%s.gz", logfile);
46
47
48     /* Reading log file */
49     log = fopen(logfile, "r");
50     if(!log)
51     {
52         /* Do not warn in here, since the alert file may not exist. */
53         return;
54     }
55
56     /* Opening compressed file */
57     zlog = gzopen(logfileGZ, "w");
58     if(!zlog)
59     {
60         fclose(log);
61         merror(FOPEN_ERROR, ARGV0, logfileGZ);
62         return;
63     }
64
65     for(;;)
66     {
67         len = fread(buf, 1, OS_MAXSTR, log);
68         if(len <= 0)
69             break;
70         if(gzwrite(zlog, buf, (unsigned)len) != len)
71             merror("%s: Compression error: %s", ARGV0, gzerror(zlog, &err));
72     }
73
74     /* Closing */
75     fclose(log);
76     gzclose(zlog);
77
78     /* Removing uncompressed file */
79     unlink(logfile);
80
81     return;
82 }
83         
84
85 /* EOF */