]>
git.ipfire.org Git - thirdparty/squid.git/blob - tools/purge/conffile.cc
3 // Author: Jens-S. V?ckler <voeckler@rvs.uni-hannover.de>
8 // (c) 2000 Lehrgebiet Rechnernetze und Verteilte Systeme
9 // Universit?t Hannover, Germany
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.
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.
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
33 // Revision 1.1 2000/09/21 09:44:53 voeckler
37 #if (defined(__GNUC__) || defined(__GNUG__)) && !defined(__clang__)
38 #pragma implementation
41 #include "conffile.hh"
42 #include <sys/types.h>
51 readConfigFile( CacheDirVector
& cachedir
, const char* fn
, FILE* debug
)
52 // purpose: read squid.conf file and extract cache_dir entries
53 // paramtr: cachedir (OUT): vector with an entry for each cache_dir found
54 // fn (IN): file name of squid.conf to use
55 // returns: number of entries, or negative to warn of errors
57 static const char* expression
=
58 "^[ \t]*cache_dir([ \t]+([[:alpha:]]+))?[ \t]+([[:graph:]]+)[ \t]+([0-9]+)[ \t]+([0-9]+)[ \t]+([0-9]+)";
61 if ( debug
) fprintf( debug
, "# trying to open %s\n", fn
? fn
: "(null)" );
62 std::ifstream
cfgin(fn
);
64 fprintf( stderr
, "fopen %s: %s\n", fn
, strerror(errno
) );
68 // prepare regular expression for matching
69 if ( debug
) fprintf( debug
, "# trying to compile \"%s\"\n", expression
);
71 int result
= regcomp( &rexp
, expression
, REG_EXTENDED
);
74 regerror( result
, &rexp
, buffer
, sizeof(buffer
) );
75 fprintf( stderr
, "regular expression \"%s\": %s\n", expression
, buffer
);
80 if ( debug
) fputs( "# trying to read lines\n", debug
);
85 while ( cfgin
.getline( line
, sizeof(line
)) ) {
86 // FIXME: overly long lines
88 // terminate line at start of comment
89 if ( (s
= (char*) memchr( line
, '#', sizeof(line
) )) ) *s
= '\0';
92 if ( *line
== '\0' || *line
== '\n' ) continue;
95 if ( (result
=regexec( &rexp
, line
, 7, subs
, 0 )) != 0 ) {
97 if ( result
!= REG_NOMATCH
) {
99 regerror( result
, &rexp
, buffer
, sizeof(buffer
) );
100 fprintf( stderr
, "while matching \"%s\" against %s%s\n",
101 expression
, line
, buffer
);
107 // match, please record
108 memset( &cd
, 0, sizeof(cd
) );
109 if ( debug
) fprintf( debug
, "# match from %d-%d on line %s",
110 (int)subs
[0].rm_so
, (int)subs
[0].rm_eo
,
113 // terminate line after matched expression
114 line
[ subs
[0].rm_eo
] = '\0';
116 // extract information. If 6th parenthesis is filled, this is
117 // a new squid with disk types, otherwise it is an older version
119 if ( subs
[6].rm_so
== -1 ) {
120 // old version, disk type at position 2 is always UFS
121 cd
.type
= CacheDir::CDT_UFS
;
123 // new version, disk type at position 2
124 line
[ subs
[offset
].rm_eo
] = '\0';
125 if ( debug
) fprintf( debug
, "# match from %d-%d on \"%s\"\n",
126 (int)subs
[offset
].rm_so
,
127 (int)subs
[offset
].rm_eo
,
128 line
+subs
[offset
].rm_so
);
129 if ( strcmp( line
+ subs
[offset
].rm_so
, "ufs" ) == 0 )
130 cd
.type
= CacheDir::CDT_UFS
;
131 else if ( strcmp( line
+ subs
[offset
].rm_so
, "asyncufs" ) == 0 )
132 cd
.type
= CacheDir::CDT_AUFS
;
133 else if ( strcmp( line
+ subs
[offset
].rm_so
, "diskd" ) == 0 )
134 cd
.type
= CacheDir::CDT_DISKD
;
136 cd
.type
= CacheDir::CDT_OTHER
;
140 // extract base directory
141 line
[ subs
[offset
].rm_eo
] = '\0';
142 if ( debug
) fprintf( debug
, "# match from %d-%d on \"%s\"\n",
143 (int)subs
[offset
].rm_so
,
144 (int)subs
[offset
].rm_eo
,
145 line
+subs
[offset
].rm_so
);
146 cd
.base
= strdup( line
+subs
[offset
].rm_so
);
149 // extract size information
150 line
[ subs
[offset
].rm_eo
] = '\0';
151 if ( debug
) fprintf( debug
, "# match from %d-%d on \"%s\"\n",
152 (int)subs
[offset
].rm_so
,
153 (int)subs
[offset
].rm_eo
,
154 line
+subs
[offset
].rm_so
);
155 cd
.size
= strtoul( line
+subs
[offset
].rm_so
, 0, 10 );
158 // extract 1st level directories
159 line
[ subs
[offset
].rm_eo
] = '\0';
160 if ( debug
) fprintf( debug
, "# match from %d-%d on \"%s\"\n",
161 (int)subs
[offset
].rm_so
,
162 (int)subs
[offset
].rm_eo
,
163 line
+subs
[offset
].rm_so
);
164 cd
.level
[0] = strtoul( line
+subs
[offset
].rm_so
, 0, 10 );
167 // extract 2nd level directories
168 line
[ subs
[offset
].rm_eo
] = '\0';
169 if ( debug
) fprintf( debug
, "# match from %d-%d on \"%s\"\n",
170 (int)subs
[offset
].rm_so
,
171 (int)subs
[offset
].rm_eo
,
172 line
+subs
[offset
].rm_so
);
173 cd
.level
[1] = strtoul( line
+subs
[offset
].rm_so
, 0, 10 );
176 cachedir
.push_back( cd
);
182 return cachedir
.size();