]>
git.ipfire.org Git - thirdparty/squid.git/blob - tools/purge/conffile.cc
2 * Copyright (C) 1996-2023 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"
56 #include <sys/types.h>
59 readConfigFile( CacheDirVector
& cachedir
, const char* fn
, FILE* debug
)
60 // purpose: read squid.conf file and extract cache_dir entries
61 // paramtr: cachedir (OUT): vector with an entry for each cache_dir found
62 // fn (IN): file name of squid.conf to use
63 // returns: number of entries, or negative to warn of errors
65 static const char* expression
=
66 "^[ \t]*cache_dir([ \t]+([[:alpha:]]+))?[ \t]+([[:graph:]]+)[ \t]+([0-9]+)[ \t]+([0-9]+)[ \t]+([0-9]+)";
69 if ( debug
) fprintf( debug
, "# trying to open %s\n", fn
? fn
: "(null)" );
70 std::ifstream
cfgin(fn
);
72 fprintf( stderr
, "fopen %s: %s\n", fn
, strerror(errno
) );
76 // prepare regular expression for matching
77 if ( debug
) fprintf( debug
, "# trying to compile \"%s\"\n", expression
);
79 int result
= regcomp( &rexp
, expression
, REG_EXTENDED
);
82 regerror( result
, &rexp
, buffer
, sizeof(buffer
) );
83 fprintf( stderr
, "regular expression \"%s\": %s\n", expression
, buffer
);
88 if ( debug
) fputs( "# trying to read lines\n", debug
);
93 while ( cfgin
.getline( line
, sizeof(line
)) ) {
94 // TODO: overly long lines
96 // terminate line at start of comment
97 if ( (s
= (char*) memchr( line
, '#', sizeof(line
) )) ) *s
= '\0';
100 if ( *line
== '\0' || *line
== '\n' ) continue;
103 if ( (result
=regexec( &rexp
, line
, 7, subs
, 0 )) != 0 ) {
105 if ( result
!= REG_NOMATCH
) {
107 regerror( result
, &rexp
, buffer
, sizeof(buffer
) );
108 fprintf( stderr
, "while matching \"%s\" against %s%s\n",
109 expression
, line
, buffer
);
115 // match, please record
116 memset( &cd
, 0, sizeof(cd
) );
117 if ( debug
) fprintf( debug
, "# match from %d-%d on line %s",
118 (int)subs
[0].rm_so
, (int)subs
[0].rm_eo
,
121 // terminate line after matched expression
122 line
[ subs
[0].rm_eo
] = '\0';
124 // extract information. If 6th parenthesis is filled, this is
125 // a new squid with disk types, otherwise it is an older version
127 if ( subs
[6].rm_so
== -1 ) {
128 // old version, disk type at position 2 is always UFS
129 cd
.type
= CacheDir::CDT_UFS
;
131 // new version, disk type at position 2
132 line
[ subs
[offset
].rm_eo
] = '\0';
133 if ( debug
) fprintf( debug
, "# match from %d-%d on \"%s\"\n",
134 (int)subs
[offset
].rm_so
,
135 (int)subs
[offset
].rm_eo
,
136 line
+subs
[offset
].rm_so
);
137 if ( strcmp( line
+ subs
[offset
].rm_so
, "ufs" ) == 0 )
138 cd
.type
= CacheDir::CDT_UFS
;
139 else if ( strcmp( line
+ subs
[offset
].rm_so
, "asyncufs" ) == 0 )
140 cd
.type
= CacheDir::CDT_AUFS
;
141 else if ( strcmp( line
+ subs
[offset
].rm_so
, "diskd" ) == 0 )
142 cd
.type
= CacheDir::CDT_DISKD
;
144 cd
.type
= CacheDir::CDT_OTHER
;
148 // extract base directory
149 line
[ subs
[offset
].rm_eo
] = '\0';
150 if ( debug
) fprintf( debug
, "# match from %d-%d on \"%s\"\n",
151 (int)subs
[offset
].rm_so
,
152 (int)subs
[offset
].rm_eo
,
153 line
+subs
[offset
].rm_so
);
154 cd
.base
= xstrdup( line
+subs
[offset
].rm_so
);
157 // extract size information
158 line
[ subs
[offset
].rm_eo
] = '\0';
159 if ( debug
) fprintf( debug
, "# match from %d-%d on \"%s\"\n",
160 (int)subs
[offset
].rm_so
,
161 (int)subs
[offset
].rm_eo
,
162 line
+subs
[offset
].rm_so
);
163 cd
.size
= strtoul( line
+subs
[offset
].rm_so
, nullptr, 10 );
166 // extract 1st level directories
167 line
[ subs
[offset
].rm_eo
] = '\0';
168 if ( debug
) fprintf( debug
, "# match from %d-%d on \"%s\"\n",
169 (int)subs
[offset
].rm_so
,
170 (int)subs
[offset
].rm_eo
,
171 line
+subs
[offset
].rm_so
);
172 cd
.level
[0] = strtoul( line
+subs
[offset
].rm_so
, nullptr, 10 );
175 // extract 2nd level directories
176 line
[ subs
[offset
].rm_eo
] = '\0';
177 if ( debug
) fprintf( debug
, "# match from %d-%d on \"%s\"\n",
178 (int)subs
[offset
].rm_so
,
179 (int)subs
[offset
].rm_eo
,
180 line
+subs
[offset
].rm_so
);
181 cd
.level
[1] = strtoul( line
+subs
[offset
].rm_so
, nullptr, 10 );
184 cachedir
.push_back( cd
);
190 return cachedir
.size();