Browse code

new option CompressLocalDatabase

git-svn: trunk@3455

Tomasz Kojm authored on 2007/12/23 06:30:38
Showing 7 changed files
... ...
@@ -1,3 +1,7 @@
1
+Sat Dec 22 21:52:51 CET 2007 (tk)
2
+---------------------------------
3
+  * freshclam: new option CompressLocalDatabase (default: no)
4
+
1 5
 Sat Dec 22 02:07:55 CET 2007 (tk)
2 6
 ---------------------------------
3 7
   * freshclam/manager.c: improve db check; make new db available already
... ...
@@ -91,6 +91,11 @@ With this option you can control scripted updates. It's highly recommended to ke
91 91
 .br .
92 92
 Default: enabled
93 93
 .TP 
94
+\fBCompressLocalDatabase BOOL\fR
95
+By default freshclam will keep the local databases (.cld) uncompressed to make their handling faster. With this option you can enable the compression; the change will take effect with the next database update.
96
+.br 
97
+Default: no
98
+.TP 
94 99
 \fBHTTPProxyServer STR\fR, \fBHTTPProxyPort NUMBER\fR
95 100
 Use given proxy server and TCP port for database downloads.
96 101
 .TP 
... ...
@@ -78,8 +78,15 @@ DatabaseMirror database.clamav.net
78 78
 
79 79
 # With this option you can control scripted updates. It's highly recommended
80 80
 # to keep it enabled.
81
+# Default: yes
81 82
 #ScriptedUpdates yes
82 83
 
84
+# By default freshclam will keep the local databases (.cld) uncompressed to
85
+# make their handling faster. With this option you can enable the compression;
86
+# the change will take effect with the next database update.
87
+# Default: no
88
+#CompressLocalDatabase no
89
+
83 90
 # Number of database checks per day.
84 91
 # Default: 12 (every two hours)
85 92
 #Checks 24
... ...
@@ -51,6 +51,7 @@
51 51
 #include <sys/stat.h>
52 52
 #include <dirent.h>
53 53
 #include <errno.h>
54
+#include <zlib.h>
54 55
 
55 56
 #include "manager.h"
56 57
 #include "notify.h"
... ...
@@ -820,12 +821,13 @@ static struct cl_cvd *currentdb(const char *dbname, char *localname)
820 820
     return cvd;
821 821
 }
822 822
 
823
-static int buildcld(const char *tmpdir, const char *dbname, const char *newfile)
823
+static int buildcld(const char *tmpdir, const char *dbname, const char *newfile, unsigned int compr)
824 824
 {
825 825
 	DIR *dir;
826 826
 	char cwd[512], info[32], buff[512], *pt;
827 827
 	struct dirent *dent;
828 828
 	int fd;
829
+	gzFile *gzs = NULL;
829 830
 
830 831
 
831 832
     getcwd(cwd, sizeof(cwd));
... ...
@@ -868,15 +870,26 @@ static int buildcld(const char *tmpdir, const char *dbname, const char *newfile)
868 868
 	close(fd);
869 869
 	return -1;
870 870
     }
871
-    close(fd);
872 871
 
873 872
     if((dir = opendir(".")) == NULL) {
874 873
 	logg("!buildcld: Can't open directory %s\n", tmpdir);
875 874
 	chdir(cwd);
876 875
 	unlink(newfile);
876
+	close(fd);
877 877
 	return -1;
878 878
     }
879 879
 
880
+    if(compr) {
881
+	close(fd);
882
+	if(!(gzs = gzopen(newfile, "ab"))) {
883
+	    logg("!buildcld: gzopen() failed for %s\n", newfile);
884
+	    chdir(cwd);
885
+	    unlink(newfile);
886
+	    closedir(dir);
887
+	    return -1;
888
+	}
889
+    }
890
+
880 891
     while((dent = readdir(dir))) {
881 892
 #ifndef C_INTERIX
882 893
 	if(dent->d_ino)
... ...
@@ -885,9 +898,13 @@ static int buildcld(const char *tmpdir, const char *dbname, const char *newfile)
885 885
 	    if(!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
886 886
 		continue;
887 887
 
888
-	    if(tar_addfile(newfile, dent->d_name) == -1) {
888
+	    if(tar_addfile(fd, gzs, dent->d_name) == -1) {
889 889
 		logg("!buildcld: Can't add %s to .cld file\n", dent->d_name);
890 890
 		chdir(cwd);
891
+		if(gzs)
892
+		    gzclose(gzs);
893
+		else
894
+		    close(fd);
891 895
 		unlink(newfile);
892 896
 		closedir(dir);
893 897
 		return -1;
... ...
@@ -896,6 +913,20 @@ static int buildcld(const char *tmpdir, const char *dbname, const char *newfile)
896 896
     }
897 897
     closedir(dir);
898 898
 
899
+    if(gzs) {
900
+	if(gzclose(gzs)) {
901
+	    logg("!buildcld: gzclose() failed for %s\n", newfile);
902
+	    unlink(newfile);
903
+	    return -1;
904
+	}
905
+    } else {
906
+	if(close(fd) == -1) {
907
+	    logg("!buildcld: close() failed for %s\n", newfile);
908
+	    unlink(newfile);
909
+	    return -1;
910
+	}
911
+    }
912
+
899 913
     if(chdir(cwd) == -1) {
900 914
 	logg("!buildcld: Can't return to previous directory %s\n", cwd);
901 915
 	return -1;
... ...
@@ -1086,7 +1117,7 @@ static int updatedb(const char *dbname, const char *hostname, char *ip, int *sig
1086 1086
 	    }
1087 1087
 	    snprintf(localname, sizeof(localname), "%s.cvd", dbname);
1088 1088
 	} else {
1089
-	    if(buildcld(tmpdir, dbname, newfile) == -1) {
1089
+	    if(buildcld(tmpdir, dbname, newfile, cfgopt(copt, "CompressLocalDatabase")->enabled) == -1) {
1090 1090
 		logg("!Can't create local database\n");
1091 1091
 		cli_rmdirs(tmpdir);
1092 1092
 		free(tmpdir);
... ...
@@ -105,6 +105,7 @@ struct cfgoption cfg_options[] = {
105 105
     {"DatabaseMirror", OPT_QUOTESTR, -1, NULL, 1, OPT_FRESHCLAM},
106 106
     {"MaxAttempts", OPT_NUM, 3, NULL, 0, OPT_FRESHCLAM},
107 107
     {"ScriptedUpdates", OPT_BOOL, 1, NULL, 0, OPT_FRESHCLAM},
108
+    {"CompressLocalDatabase", OPT_BOOL, 0, NULL, 0, OPT_FRESHCLAM},
108 109
     {"HTTPProxyServer", OPT_QUOTESTR, -1, NULL, 0, OPT_FRESHCLAM},
109 110
     {"HTTPProxyPort", OPT_NUM, -1, NULL, 0, OPT_FRESHCLAM},
110 111
     {"HTTPProxyUsername", OPT_QUOTESTR, -1, NULL, 0, OPT_FRESHCLAM},
... ...
@@ -29,6 +29,7 @@
29 29
 #ifdef  HAVE_UNISTD_H
30 30
 #include <unistd.h>
31 31
 #endif
32
+#include <zlib.h>
32 33
 
33 34
 #include "tar.h"
34 35
 
... ...
@@ -50,25 +51,21 @@ struct tar_header {
50 50
 };
51 51
 #define TARBLK 512
52 52
 
53
-int tar_addfile(const char *tar, const char *file)
53
+int tar_addfile(int fd, gzFile *gzs, const char *file)
54 54
 {
55
-	int s, d, bytes;
55
+	int s, bytes;
56 56
 	struct tar_header hdr;
57 57
 	struct stat sb;
58 58
 	unsigned char buff[FILEBUFF], *pt;
59 59
 	unsigned int i, chksum = 0;
60 60
 
61 61
 
62
-    if((d = open(tar, O_WRONLY|O_APPEND|O_CREAT|O_BINARY, 0644)) == -1)
63
-	return -1;
64
-
65 62
     if((s = open(file, O_RDONLY|O_BINARY)) == -1) {
66 63
 	close(s);
67 64
 	return -1;
68 65
     }
69 66
 
70 67
     if(fstat(s, &sb) == -1) {
71
-	close(d);
72 68
 	close(s);
73 69
 	return -1;
74 70
     }
... ...
@@ -81,29 +78,43 @@ int tar_addfile(const char *tar, const char *file)
81 81
 	chksum += *pt++;
82 82
     snprintf(hdr.chksum, 8, "%06o", chksum + 256);
83 83
 
84
-    if(write(d, &hdr, TARBLK) != TARBLK) {
85
-	close(d);
86
-	close(s);
87
-	return -1;
84
+    if(gzs) {
85
+	if(!gzwrite(gzs, &hdr, TARBLK)) {
86
+	    close(s);
87
+	    return -1;
88
+	}
89
+    } else {
90
+	if(write(fd, &hdr, TARBLK) != TARBLK) {
91
+	    close(s);
92
+	    return -1;
93
+	}
88 94
     }
89 95
 
90 96
     while((bytes = read(s, buff, FILEBUFF)) > 0) {
91
-	if(write(d, buff, bytes) != bytes) {
92
-	    close(d);
93
-	    close(s);
94
-	    return -1;
97
+	if(gzs) {
98
+	    if(!gzwrite(gzs, buff, bytes)) {
99
+		close(s);
100
+		return -1;
101
+	    }
102
+	} else {
103
+	    if(write(fd, buff, bytes) != bytes) {
104
+		close(s);
105
+		return -1;
106
+	    }
95 107
 	}
96 108
     }
109
+    close(s);
97 110
 
98 111
     if(sb.st_size % TARBLK) {
99 112
 	memset(&hdr, 0, TARBLK);
100
-	if(write(d, &hdr, TARBLK - (sb.st_size % TARBLK)) == -1) {
101
-	    close(d);
102
-	    close(s);
103
-	    return -1;
113
+	if(gzs) {
114
+	    if(!gzwrite(gzs, &hdr, TARBLK - (sb.st_size % TARBLK)))
115
+		return -1;
116
+	} else {
117
+	    if(write(fd, &hdr, TARBLK - (sb.st_size % TARBLK)) == -1)
118
+		return -1;
104 119
 	}
105 120
     }
106 121
 
107
-    close(s);
108
-    return close(d);
122
+    return 0;
109 123
 }
... ...
@@ -21,6 +21,8 @@
21 21
 #ifndef __TAR_H
22 22
 #define __TAR_H
23 23
 
24
-int tar_addfile(const char *tar, const char *file);
24
+#include <zlib.h>
25
+
26
+int tar_addfile(int fd, gzFile *gzs, const char *file);
25 27
 
26 28
 #endif