]>
git.ipfire.org Git - thirdparty/squid.git/blob - tools/purge/conffile.cc
6 // Author: Jens-S. V?ckler <voeckler@rvs.uni-hannover.de>
11 // (c) 2000 Lehrgebiet Rechnernetze und Verteilte Systeme
12 // Universit?t Hannover, Germany
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.
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.
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
36 // Revision 1.1 2000/09/21 09:44:53 voeckler
40 #if defined(__GNUC__) || defined(__GNUG__)
41 #pragma implementation
44 #include "conffile.hh"
45 #include <sys/types.h>
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
59 static const char* expression
=
60 "^[ \t]*cache_dir([ \t]+([[:alpha:]]+))?[ \t]+([[:graph:]]+)[ \t]+([0-9]+)[ \t]+([0-9]+)[ \t]+([0-9]+)";
63 if ( debug
) fprintf( debug
, "# trying to open %s\n", fn
? fn
: "(null)" );
64 FILE* in
= fopen( fn
, "r" );
66 fprintf( stderr
, "fopen %s: %s\n", fn
, strerror(errno
) );
70 // prepare regular expression for matching
71 if ( debug
) fprintf( debug
, "# trying to compile \"%s\"\n", expression
);
73 int result
= regcomp( &rexp
, expression
, REG_EXTENDED
);
76 regerror( result
, &rexp
, buffer
, sizeof(buffer
) );
77 fprintf( stderr
, "regular expression \"%s\": %s\n", expression
, buffer
);
82 if ( debug
) fputs( "# trying to read lines\n", debug
);
87 while ( fgets( line
, sizeof(line
), in
) ) {
88 // FIXME: overly long lines
90 // terminate line at start of comment
91 if ( (s
= (char*) memchr( line
, '#', sizeof(line
) )) ) *s
= '\0';
94 if ( *line
== '\0' || *line
== '\n' ) continue;
97 if ( (result
=regexec( &rexp
, line
, 7, subs
, 0 )) != 0 ) {
99 if ( result
!= REG_NOMATCH
) {
101 regerror( result
, &rexp
, buffer
, sizeof(buffer
) );
102 fprintf( stderr
, "while matching \"%s\" against %s%s\n",
103 expression
, line
, buffer
);
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
,
115 // terminate line after matched expression
116 line
[ subs
[0].rm_eo
] = '\0';
118 // extract information. If 6th parenthesis is filled, this is
119 // a new squid with disk types, otherwise it is an older version
121 if ( subs
[6].rm_so
== -1 ) {
122 // old version, disk type at position 2 is always UFS
123 cd
.type
= CacheDir::CDT_UFS
;
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
;
138 cd
.type
= CacheDir::CDT_OTHER
;
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
);
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 );
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 );
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 );
178 cachedir
.push_back( cd
);
184 return cachedir
.size();