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