]>
git.ipfire.org Git - thirdparty/squid.git/blob - tools/purge/conffile.cc
4 // Author: Jens-S. V?ckler <voeckler@rvs.uni-hannover.de>
9 // (c) 2000 Lehrgebiet Rechnernetze und Verteilte Systeme
10 // Universit?t Hannover, Germany
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.
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.
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
34 // Revision 1.1 2000/09/21 09:44:53 voeckler
38 #if defined(__GNUC__) || defined(__GNUG__)
39 #pragma implementation
42 #include "conffile.hh"
43 #include <sys/types.h>
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
58 static const char* expression
=
59 "^[ \t]*cache_dir([ \t]+([[:alpha:]]+))?[ \t]+([[:graph:]]+)[ \t]+([0-9]+)[ \t]+([0-9]+)[ \t]+([0-9]+)";
62 if ( debug
) fprintf( debug
, "# trying to open %s\n", fn
? fn
: "(null)" );
63 FILE* in
= fopen( fn
, "r" );
65 fprintf( stderr
, "fopen %s: %s\n", fn
, strerror(errno
) );
69 // prepare regular expression for matching
70 if ( debug
) fprintf( debug
, "# trying to compile \"%s\"\n", expression
);
72 int result
= regcomp( &rexp
, expression
, REG_EXTENDED
);
75 regerror( result
, &rexp
, buffer
, sizeof(buffer
) );
76 fprintf( stderr
, "regular expression \"%s\": %s\n", expression
, buffer
);
81 if ( debug
) fputs( "# trying to read lines\n", debug
);
86 while ( fgets( line
, sizeof(line
), in
) ) {
87 // FIXME: overly long lines
89 // terminate line at start of comment
90 if ( (s
= (char*) memchr( line
, '#', sizeof(line
) )) ) *s
= '\0';
93 if ( *line
== '\0' || *line
== '\n' ) continue;
96 if ( (result
=regexec( &rexp
, line
, 7, subs
, 0 )) != 0 ) {
98 if ( result
!= REG_NOMATCH
) {
100 regerror( result
, &rexp
, buffer
, sizeof(buffer
) );
101 fprintf( stderr
, "while matching \"%s\" against %s%s\n",
102 expression
, line
, buffer
);
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
,
114 // terminate line after matched expression
115 line
[ subs
[0].rm_eo
] = '\0';
117 // extract information. If 6th parenthesis is filled, this is
118 // a new squid with disk types, otherwise it is an older version
120 if ( subs
[6].rm_so
== -1 ) {
121 // old version, disk type at position 2 is always UFS
122 cd
.type
= CacheDir::CDT_UFS
;
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
;
137 cd
.type
= CacheDir::CDT_OTHER
;
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
);
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 );
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 );
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 );
177 cachedir
.push_back( cd
);
183 return cachedir
.size();