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