]> git.ipfire.org Git - thirdparty/squid.git/blob - tools/purge/conffile.cc
Merged from trunk (r13356).
[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 #include "conffile.hh"
38
39 #include <cerrno>
40 #include <cstdlib>
41 #include <cstring>
42 #include <fstream>
43 #include <sys/types.h>
44 #include <memory.h>
45
46 int
47 readConfigFile( CacheDirVector& cachedir, const char* fn, FILE* debug )
48 // purpose: read squid.conf file and extract cache_dir entries
49 // paramtr: cachedir (OUT): vector with an entry for each cache_dir found
50 // fn (IN): file name of squid.conf to use
51 // returns: number of entries, or negative to warn of errors
52 {
53 static const char* expression =
54 "^[ \t]*cache_dir([ \t]+([[:alpha:]]+))?[ \t]+([[:graph:]]+)[ \t]+([0-9]+)[ \t]+([0-9]+)[ \t]+([0-9]+)";
55
56 // try to open file
57 if ( debug ) fprintf( debug, "# trying to open %s\n", fn ? fn : "(null)" );
58 std::ifstream cfgin(fn);
59 if (!cfgin) {
60 fprintf( stderr, "fopen %s: %s\n", fn, strerror(errno) );
61 return -1;
62 }
63
64 // prepare regular expression for matching
65 if ( debug ) fprintf( debug, "# trying to compile \"%s\"\n", expression );
66 regex_t rexp;
67 int result = regcomp( &rexp, expression, REG_EXTENDED );
68 if ( result != 0 ) {
69 char buffer[256];
70 regerror( result, &rexp, buffer, sizeof(buffer) );
71 fprintf( stderr, "regular expression \"%s\": %s\n", expression, buffer );
72 return -1;
73 }
74
75 // read line by line
76 if ( debug ) fputs( "# trying to read lines\n", debug );
77
78 regmatch_t subs[8];
79 char *s, line[1024];
80 CacheDir cd;
81 while ( cfgin.getline( line, sizeof(line)) ) {
82 // FIXME: overly long lines
83
84 // terminate line at start of comment
85 if ( (s = (char*) memchr( line, '#', sizeof(line) )) ) *s = '\0';
86
87 // quick skip
88 if ( *line == '\0' || *line == '\n' ) continue;
89
90 // test line
91 if ( (result=regexec( &rexp, line, 7, subs, 0 )) != 0 ) {
92 // error or no match
93 if ( result != REG_NOMATCH ) {
94 char buffer[256];
95 regerror( result, &rexp, buffer, sizeof(buffer) );
96 fprintf( stderr, "while matching \"%s\" against %s%s\n",
97 expression, line, buffer );
98 regfree(&rexp);
99 cfgin.close();
100 return -1;
101 }
102 } else {
103 // match, please record
104 memset( &cd, 0, sizeof(cd) );
105 if ( debug ) fprintf( debug, "# match from %d-%d on line %s",
106 (int)subs[0].rm_so, (int)subs[0].rm_eo,
107 line );
108
109 // terminate line after matched expression
110 line[ subs[0].rm_eo ] = '\0';
111
112 // extract information. If 6th parenthesis is filled, this is
113 // a new squid with disk types, otherwise it is an older version
114 int offset = 2;
115 if ( subs[6].rm_so == -1 ) {
116 // old version, disk type at position 2 is always UFS
117 cd.type = CacheDir::CDT_UFS;
118 } else {
119 // new version, disk type at position 2
120 line[ subs[offset].rm_eo ] = '\0';
121 if ( debug ) fprintf( debug, "# match from %d-%d on \"%s\"\n",
122 (int)subs[offset].rm_so,
123 (int)subs[offset].rm_eo,
124 line+subs[offset].rm_so );
125 if ( strcmp( line + subs[offset].rm_so, "ufs" ) == 0 )
126 cd.type = CacheDir::CDT_UFS;
127 else if ( strcmp( line + subs[offset].rm_so, "asyncufs" ) == 0 )
128 cd.type = CacheDir::CDT_AUFS;
129 else if ( strcmp( line + subs[offset].rm_so, "diskd" ) == 0 )
130 cd.type = CacheDir::CDT_DISKD;
131 else
132 cd.type = CacheDir::CDT_OTHER;
133 ++offset;
134 }
135
136 // extract base directory
137 line[ subs[offset].rm_eo ] = '\0';
138 if ( debug ) fprintf( debug, "# match from %d-%d on \"%s\"\n",
139 (int)subs[offset].rm_so,
140 (int)subs[offset].rm_eo,
141 line+subs[offset].rm_so );
142 cd.base = xstrdup( line+subs[offset].rm_so );
143 ++offset;
144
145 // extract size information
146 line[ subs[offset].rm_eo ] = '\0';
147 if ( debug ) fprintf( debug, "# match from %d-%d on \"%s\"\n",
148 (int)subs[offset].rm_so,
149 (int)subs[offset].rm_eo,
150 line+subs[offset].rm_so );
151 cd.size = strtoul( line+subs[offset].rm_so, 0, 10 );
152 ++offset;
153
154 // extract 1st level directories
155 line[ subs[offset].rm_eo ] = '\0';
156 if ( debug ) fprintf( debug, "# match from %d-%d on \"%s\"\n",
157 (int)subs[offset].rm_so,
158 (int)subs[offset].rm_eo,
159 line+subs[offset].rm_so );
160 cd.level[0] = strtoul( line+subs[offset].rm_so, 0, 10 );
161 ++offset;
162
163 // extract 2nd level directories
164 line[ subs[offset].rm_eo ] = '\0';
165 if ( debug ) fprintf( debug, "# match from %d-%d on \"%s\"\n",
166 (int)subs[offset].rm_so,
167 (int)subs[offset].rm_eo,
168 line+subs[offset].rm_so );
169 cd.level[1] = strtoul( line+subs[offset].rm_so, 0, 10 );
170 ++offset;
171
172 cachedir.push_back( cd );
173 }
174 }
175
176 cfgin.close();
177 regfree(&rexp);
178 return cachedir.size();
179 }