]> git.ipfire.org Git - thirdparty/squid.git/blob - tools/purge/conffile.cc
merge from trunk r12441
[thirdparty/squid.git] / tools / purge / conffile.cc
1 #include "squid.h"
2
3 // Author: Jens-S. V?ckler <voeckler@rvs.uni-hannover.de>
4 //
5 // File: conffile.cc
6 // Fri Sep 15 2000
7 //
8 // (c) 2000 Lehrgebiet Rechnernetze und Verteilte Systeme
9 // Universit?t Hannover, Germany
10 //
11 // Permission to use, copy, modify, distribute, and sell this software
12 // and its documentation for any purpose is hereby granted without fee,
13 // provided that (i) the above copyright notices and this permission
14 // notice appear in all copies of the software and related documentation,
15 // and (ii) the names of the Lehrgebiet Rechnernetze und Verteilte
16 // Systeme and the University of Hannover may not be used in any
17 // advertising or publicity relating to the software without the
18 // specific, prior written permission of Lehrgebiet Rechnernetze und
19 // Verteilte Systeme and the University of Hannover.
20 //
21 // THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
23 // WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
24 //
25 // IN NO EVENT SHALL THE LEHRGEBIET RECHNERNETZE UND VERTEILTE SYSTEME OR
26 // THE UNIVERSITY OF HANNOVER BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
27 // INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES
28 // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT
29 // ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY,
30 // ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
31 // SOFTWARE.
32 //
33 // Revision 1.1 2000/09/21 09:44:53 voeckler
34 // Initial revision
35 //
36 //
37 #if (defined(__GNUC__) || defined(__GNUG__)) && !defined(__clang__)
38 #pragma implementation
39 #endif
40
41 #include "conffile.hh"
42 #include <sys/types.h>
43 #include <errno.h>
44 #include <memory.h>
45 #include <string.h>
46 #include <stdlib.h>
47 #include <stdio.h>
48 #include <fstream>
49
50 int
51 readConfigFile( CacheDirVector& cachedir, const char* fn, FILE* debug )
52 // purpose: read squid.conf file and extract cache_dir entries
53 // paramtr: cachedir (OUT): vector with an entry for each cache_dir found
54 // fn (IN): file name of squid.conf to use
55 // returns: number of entries, or negative to warn of errors
56 {
57 static const char* expression =
58 "^[ \t]*cache_dir([ \t]+([[:alpha:]]+))?[ \t]+([[:graph:]]+)[ \t]+([0-9]+)[ \t]+([0-9]+)[ \t]+([0-9]+)";
59
60 // try to open file
61 if ( debug ) fprintf( debug, "# trying to open %s\n", fn ? fn : "(null)" );
62 std::ifstream cfgin(fn);
63 if (!cfgin) {
64 fprintf( stderr, "fopen %s: %s\n", fn, strerror(errno) );
65 return -1;
66 }
67
68 // prepare regular expression for matching
69 if ( debug ) fprintf( debug, "# trying to compile \"%s\"\n", expression );
70 regex_t rexp;
71 int result = regcomp( &rexp, expression, REG_EXTENDED );
72 if ( result != 0 ) {
73 char buffer[256];
74 regerror( result, &rexp, buffer, sizeof(buffer) );
75 fprintf( stderr, "regular expression \"%s\": %s\n", expression, buffer );
76 return -1;
77 }
78
79 // read line by line
80 if ( debug ) fputs( "# trying to read lines\n", debug );
81
82 regmatch_t subs[8];
83 char *s, line[1024];
84 CacheDir cd;
85 while ( cfgin.getline( line, sizeof(line)) ) {
86 // FIXME: overly long lines
87
88 // terminate line at start of comment
89 if ( (s = (char*) memchr( line, '#', sizeof(line) )) ) *s = '\0';
90
91 // quick skip
92 if ( *line == '\0' || *line == '\n' ) continue;
93
94 // test line
95 if ( (result=regexec( &rexp, line, 7, subs, 0 )) != 0 ) {
96 // error or no match
97 if ( result != REG_NOMATCH ) {
98 char buffer[256];
99 regerror( result, &rexp, buffer, sizeof(buffer) );
100 fprintf( stderr, "while matching \"%s\" against %s%s\n",
101 expression, line, buffer );
102 regfree(&rexp);
103 cfgin.close();
104 return -1;
105 }
106 } else {
107 // match, please record
108 memset( &cd, 0, sizeof(cd) );
109 if ( debug ) fprintf( debug, "# match from %d-%d on line %s",
110 (int)subs[0].rm_so, (int)subs[0].rm_eo,
111 line );
112
113 // terminate line after matched expression
114 line[ subs[0].rm_eo ] = '\0';
115
116 // extract information. If 6th parenthesis is filled, this is
117 // a new squid with disk types, otherwise it is an older version
118 int offset = 2;
119 if ( subs[6].rm_so == -1 ) {
120 // old version, disk type at position 2 is always UFS
121 cd.type = CacheDir::CDT_UFS;
122 } else {
123 // new version, disk type at position 2
124 line[ subs[offset].rm_eo ] = '\0';
125 if ( debug ) fprintf( debug, "# match from %d-%d on \"%s\"\n",
126 (int)subs[offset].rm_so,
127 (int)subs[offset].rm_eo,
128 line+subs[offset].rm_so );
129 if ( strcmp( line + subs[offset].rm_so, "ufs" ) == 0 )
130 cd.type = CacheDir::CDT_UFS;
131 else if ( strcmp( line + subs[offset].rm_so, "asyncufs" ) == 0 )
132 cd.type = CacheDir::CDT_AUFS;
133 else if ( strcmp( line + subs[offset].rm_so, "diskd" ) == 0 )
134 cd.type = CacheDir::CDT_DISKD;
135 else
136 cd.type = CacheDir::CDT_OTHER;
137 ++offset;
138 }
139
140 // extract base directory
141 line[ subs[offset].rm_eo ] = '\0';
142 if ( debug ) fprintf( debug, "# match from %d-%d on \"%s\"\n",
143 (int)subs[offset].rm_so,
144 (int)subs[offset].rm_eo,
145 line+subs[offset].rm_so );
146 cd.base = strdup( line+subs[offset].rm_so );
147 ++offset;
148
149 // extract size information
150 line[ subs[offset].rm_eo ] = '\0';
151 if ( debug ) fprintf( debug, "# match from %d-%d on \"%s\"\n",
152 (int)subs[offset].rm_so,
153 (int)subs[offset].rm_eo,
154 line+subs[offset].rm_so );
155 cd.size = strtoul( line+subs[offset].rm_so, 0, 10 );
156 ++offset;
157
158 // extract 1st level directories
159 line[ subs[offset].rm_eo ] = '\0';
160 if ( debug ) fprintf( debug, "# match from %d-%d on \"%s\"\n",
161 (int)subs[offset].rm_so,
162 (int)subs[offset].rm_eo,
163 line+subs[offset].rm_so );
164 cd.level[0] = strtoul( line+subs[offset].rm_so, 0, 10 );
165 ++offset;
166
167 // extract 2nd level directories
168 line[ subs[offset].rm_eo ] = '\0';
169 if ( debug ) fprintf( debug, "# match from %d-%d on \"%s\"\n",
170 (int)subs[offset].rm_so,
171 (int)subs[offset].rm_eo,
172 line+subs[offset].rm_so );
173 cd.level[1] = strtoul( line+subs[offset].rm_so, 0, 10 );
174 ++offset;
175
176 cachedir.push_back( cd );
177 }
178 }
179
180 cfgin.close();
181 regfree(&rexp);
182 return cachedir.size();
183 }