]>
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 #include "conffile.hh"
43 #include <sys/types.h>
47 readConfigFile( CacheDirVector
& cachedir
, const char* fn
, FILE* debug
)
48 // purpose: read squid.conf file and extract cache_dir entries
49 // paramtr: cachedir (OUT): vector with an entry for each cache_dir found
50 // fn (IN): file name of squid.conf to use
51 // returns: number of entries, or negative to warn of errors
53 static const char* expression
=
54 "^[ \t]*cache_dir([ \t]+([[:alpha:]]+))?[ \t]+([[:graph:]]+)[ \t]+([0-9]+)[ \t]+([0-9]+)[ \t]+([0-9]+)";
57 if ( debug
) fprintf( debug
, "# trying to open %s\n", fn
? fn
: "(null)" );
58 std::ifstream
cfgin(fn
);
60 fprintf( stderr
, "fopen %s: %s\n", fn
, strerror(errno
) );
64 // prepare regular expression for matching
65 if ( debug
) fprintf( debug
, "# trying to compile \"%s\"\n", expression
);
67 int result
= regcomp( &rexp
, expression
, REG_EXTENDED
);
70 regerror( result
, &rexp
, buffer
, sizeof(buffer
) );
71 fprintf( stderr
, "regular expression \"%s\": %s\n", expression
, buffer
);
76 if ( debug
) fputs( "# trying to read lines\n", debug
);
81 while ( cfgin
.getline( line
, sizeof(line
)) ) {
82 // FIXME: overly long lines
84 // terminate line at start of comment
85 if ( (s
= (char*) memchr( line
, '#', sizeof(line
) )) ) *s
= '\0';
88 if ( *line
== '\0' || *line
== '\n' ) continue;
91 if ( (result
=regexec( &rexp
, line
, 7, subs
, 0 )) != 0 ) {
93 if ( result
!= REG_NOMATCH
) {
95 regerror( result
, &rexp
, buffer
, sizeof(buffer
) );
96 fprintf( stderr
, "while matching \"%s\" against %s%s\n",
97 expression
, line
, buffer
);
103 // match, please record
104 memset( &cd
, 0, sizeof(cd
) );
105 if ( debug
) fprintf( debug
, "# match from %d-%d on line %s",
106 (int)subs
[0].rm_so
, (int)subs
[0].rm_eo
,
109 // terminate line after matched expression
110 line
[ subs
[0].rm_eo
] = '\0';
112 // extract information. If 6th parenthesis is filled, this is
113 // a new squid with disk types, otherwise it is an older version
115 if ( subs
[6].rm_so
== -1 ) {
116 // old version, disk type at position 2 is always UFS
117 cd
.type
= CacheDir::CDT_UFS
;
119 // new version, disk type at position 2
120 line
[ subs
[offset
].rm_eo
] = '\0';
121 if ( debug
) fprintf( debug
, "# match from %d-%d on \"%s\"\n",
122 (int)subs
[offset
].rm_so
,
123 (int)subs
[offset
].rm_eo
,
124 line
+subs
[offset
].rm_so
);
125 if ( strcmp( line
+ subs
[offset
].rm_so
, "ufs" ) == 0 )
126 cd
.type
= CacheDir::CDT_UFS
;
127 else if ( strcmp( line
+ subs
[offset
].rm_so
, "asyncufs" ) == 0 )
128 cd
.type
= CacheDir::CDT_AUFS
;
129 else if ( strcmp( line
+ subs
[offset
].rm_so
, "diskd" ) == 0 )
130 cd
.type
= CacheDir::CDT_DISKD
;
132 cd
.type
= CacheDir::CDT_OTHER
;
136 // extract base directory
137 line
[ subs
[offset
].rm_eo
] = '\0';
138 if ( debug
) fprintf( debug
, "# match from %d-%d on \"%s\"\n",
139 (int)subs
[offset
].rm_so
,
140 (int)subs
[offset
].rm_eo
,
141 line
+subs
[offset
].rm_so
);
142 cd
.base
= xstrdup( line
+subs
[offset
].rm_so
);
145 // extract size information
146 line
[ subs
[offset
].rm_eo
] = '\0';
147 if ( debug
) fprintf( debug
, "# match from %d-%d on \"%s\"\n",
148 (int)subs
[offset
].rm_so
,
149 (int)subs
[offset
].rm_eo
,
150 line
+subs
[offset
].rm_so
);
151 cd
.size
= strtoul( line
+subs
[offset
].rm_so
, 0, 10 );
154 // extract 1st level directories
155 line
[ subs
[offset
].rm_eo
] = '\0';
156 if ( debug
) fprintf( debug
, "# match from %d-%d on \"%s\"\n",
157 (int)subs
[offset
].rm_so
,
158 (int)subs
[offset
].rm_eo
,
159 line
+subs
[offset
].rm_so
);
160 cd
.level
[0] = strtoul( line
+subs
[offset
].rm_so
, 0, 10 );
163 // extract 2nd level directories
164 line
[ subs
[offset
].rm_eo
] = '\0';
165 if ( debug
) fprintf( debug
, "# match from %d-%d on \"%s\"\n",
166 (int)subs
[offset
].rm_so
,
167 (int)subs
[offset
].rm_eo
,
168 line
+subs
[offset
].rm_so
);
169 cd
.level
[1] = strtoul( line
+subs
[offset
].rm_so
, 0, 10 );
172 cachedir
.push_back( cd
);
178 return cachedir
.size();