]>
git.ipfire.org Git - thirdparty/squid.git/blob - tools/purge/conffile.cc
2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 // Author: Jens-S. V?ckler <voeckler@rvs.uni-hannover.de>
14 // (c) 2000 Lehrgebiet Rechnernetze und Verteilte Systeme
15 // Universit?t Hannover, Germany
17 // Permission to use, copy, modify, distribute, and sell this software
18 // and its documentation for any purpose is hereby granted without fee,
19 // provided that (i) the above copyright notices and this permission
20 // notice appear in all copies of the software and related documentation,
21 // and (ii) the names of the Lehrgebiet Rechnernetze und Verteilte
22 // Systeme and the University of Hannover may not be used in any
23 // advertising or publicity relating to the software without the
24 // specific, prior written permission of Lehrgebiet Rechnernetze und
25 // Verteilte Systeme and the University of Hannover.
27 // THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
29 // WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
31 // IN NO EVENT SHALL THE LEHRGEBIET RECHNERNETZE UND VERTEILTE SYSTEME OR
32 // THE UNIVERSITY OF HANNOVER BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
33 // INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES
34 // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT
35 // ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY,
36 // ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
39 // Revision 1.1 2000/09/21 09:44:53 voeckler
44 #include "conffile.hh"
50 #include <sys/types.h>
54 readConfigFile( CacheDirVector
& cachedir
, const char* fn
, FILE* debug
)
55 // purpose: read squid.conf file and extract cache_dir entries
56 // paramtr: cachedir (OUT): vector with an entry for each cache_dir found
57 // fn (IN): file name of squid.conf to use
58 // returns: number of entries, or negative to warn of errors
60 static const char* expression
=
61 "^[ \t]*cache_dir([ \t]+([[:alpha:]]+))?[ \t]+([[:graph:]]+)[ \t]+([0-9]+)[ \t]+([0-9]+)[ \t]+([0-9]+)";
64 if ( debug
) fprintf( debug
, "# trying to open %s\n", fn
? fn
: "(null)" );
65 std::ifstream
cfgin(fn
);
67 fprintf( stderr
, "fopen %s: %s\n", fn
, strerror(errno
) );
71 // prepare regular expression for matching
72 if ( debug
) fprintf( debug
, "# trying to compile \"%s\"\n", expression
);
74 int result
= regcomp( &rexp
, expression
, REG_EXTENDED
);
77 regerror( result
, &rexp
, buffer
, sizeof(buffer
) );
78 fprintf( stderr
, "regular expression \"%s\": %s\n", expression
, buffer
);
83 if ( debug
) fputs( "# trying to read lines\n", debug
);
88 while ( cfgin
.getline( line
, sizeof(line
)) ) {
89 // FIXME: overly long lines
91 // terminate line at start of comment
92 if ( (s
= (char*) memchr( line
, '#', sizeof(line
) )) ) *s
= '\0';
95 if ( *line
== '\0' || *line
== '\n' ) continue;
98 if ( (result
=regexec( &rexp
, line
, 7, subs
, 0 )) != 0 ) {
100 if ( result
!= REG_NOMATCH
) {
102 regerror( result
, &rexp
, buffer
, sizeof(buffer
) );
103 fprintf( stderr
, "while matching \"%s\" against %s%s\n",
104 expression
, line
, buffer
);
110 // match, please record
111 memset( &cd
, 0, sizeof(cd
) );
112 if ( debug
) fprintf( debug
, "# match from %d-%d on line %s",
113 (int)subs
[0].rm_so
, (int)subs
[0].rm_eo
,
116 // terminate line after matched expression
117 line
[ subs
[0].rm_eo
] = '\0';
119 // extract information. If 6th parenthesis is filled, this is
120 // a new squid with disk types, otherwise it is an older version
122 if ( subs
[6].rm_so
== -1 ) {
123 // old version, disk type at position 2 is always UFS
124 cd
.type
= CacheDir::CDT_UFS
;
126 // new version, disk type at position 2
127 line
[ subs
[offset
].rm_eo
] = '\0';
128 if ( debug
) fprintf( debug
, "# match from %d-%d on \"%s\"\n",
129 (int)subs
[offset
].rm_so
,
130 (int)subs
[offset
].rm_eo
,
131 line
+subs
[offset
].rm_so
);
132 if ( strcmp( line
+ subs
[offset
].rm_so
, "ufs" ) == 0 )
133 cd
.type
= CacheDir::CDT_UFS
;
134 else if ( strcmp( line
+ subs
[offset
].rm_so
, "asyncufs" ) == 0 )
135 cd
.type
= CacheDir::CDT_AUFS
;
136 else if ( strcmp( line
+ subs
[offset
].rm_so
, "diskd" ) == 0 )
137 cd
.type
= CacheDir::CDT_DISKD
;
139 cd
.type
= CacheDir::CDT_OTHER
;
143 // extract base directory
144 line
[ subs
[offset
].rm_eo
] = '\0';
145 if ( debug
) fprintf( debug
, "# match from %d-%d on \"%s\"\n",
146 (int)subs
[offset
].rm_so
,
147 (int)subs
[offset
].rm_eo
,
148 line
+subs
[offset
].rm_so
);
149 cd
.base
= xstrdup( line
+subs
[offset
].rm_so
);
152 // extract size information
153 line
[ subs
[offset
].rm_eo
] = '\0';
154 if ( debug
) fprintf( debug
, "# match from %d-%d on \"%s\"\n",
155 (int)subs
[offset
].rm_so
,
156 (int)subs
[offset
].rm_eo
,
157 line
+subs
[offset
].rm_so
);
158 cd
.size
= strtoul( line
+subs
[offset
].rm_so
, 0, 10 );
161 // extract 1st level directories
162 line
[ subs
[offset
].rm_eo
] = '\0';
163 if ( debug
) fprintf( debug
, "# match from %d-%d on \"%s\"\n",
164 (int)subs
[offset
].rm_so
,
165 (int)subs
[offset
].rm_eo
,
166 line
+subs
[offset
].rm_so
);
167 cd
.level
[0] = strtoul( line
+subs
[offset
].rm_so
, 0, 10 );
170 // extract 2nd level directories
171 line
[ subs
[offset
].rm_eo
] = '\0';
172 if ( debug
) fprintf( debug
, "# match from %d-%d on \"%s\"\n",
173 (int)subs
[offset
].rm_so
,
174 (int)subs
[offset
].rm_eo
,
175 line
+subs
[offset
].rm_so
);
176 cd
.level
[1] = strtoul( line
+subs
[offset
].rm_so
, 0, 10 );
179 cachedir
.push_back( cd
);
185 return cachedir
.size();