conffile.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2022 The Squid Software Foundation and contributors
3 *
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.
7 */
8
9// Author: Jens-S. V?ckler <voeckler@rvs.uni-hannover.de>
10//
11// File: conffile.cc
12// Fri Sep 15 2000
13//
14// (c) 2000 Lehrgebiet Rechnernetze und Verteilte Systeme
15// Universit?t Hannover, Germany
16//
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.
26//
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.
30//
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
37// SOFTWARE.
38//
39// Revision 1.1 2000/09/21 09:44:53 voeckler
40// Initial revision
41//
42
43#include "squid.h"
44#include "conffile.hh"
45
46#include <cerrno>
47#include <cstdlib>
48#include <cstring>
49#include <fstream>
50#include <sys/types.h>
51#include <memory.h>
52
53int
54readConfigFile( 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
59{
60 static const char* expression =
61 "^[ \t]*cache_dir([ \t]+([[:alpha:]]+))?[ \t]+([[:graph:]]+)[ \t]+([0-9]+)[ \t]+([0-9]+)[ \t]+([0-9]+)";
62
63 // try to open file
64 if ( debug ) fprintf( debug, "# trying to open %s\n", fn ? fn : "(null)" );
65 std::ifstream cfgin(fn);
66 if (!cfgin) {
67 fprintf( stderr, "fopen %s: %s\n", fn, strerror(errno) );
68 return -1;
69 }
70
71 // prepare regular expression for matching
72 if ( debug ) fprintf( debug, "# trying to compile \"%s\"\n", expression );
73 regex_t rexp;
74 int result = regcomp( &rexp, expression, REG_EXTENDED );
75 if ( result != 0 ) {
76 char buffer[256];
77 regerror( result, &rexp, buffer, sizeof(buffer) );
78 fprintf( stderr, "regular expression \"%s\": %s\n", expression, buffer );
79 return -1;
80 }
81
82 // read line by line
83 if ( debug ) fputs( "# trying to read lines\n", debug );
84
85 regmatch_t subs[8];
86 char *s, line[1024];
87 CacheDir cd;
88 while ( cfgin.getline( line, sizeof(line)) ) {
89 // TODO: overly long lines
90
91 // terminate line at start of comment
92 if ( (s = (char*) memchr( line, '#', sizeof(line) )) ) *s = '\0';
93
94 // quick skip
95 if ( *line == '\0' || *line == '\n' ) continue;
96
97 // test line
98 if ( (result=regexec( &rexp, line, 7, subs, 0 )) != 0 ) {
99 // error or no match
100 if ( result != REG_NOMATCH ) {
101 char buffer[256];
102 regerror( result, &rexp, buffer, sizeof(buffer) );
103 fprintf( stderr, "while matching \"%s\" against %s%s\n",
104 expression, line, buffer );
105 regfree(&rexp);
106 cfgin.close();
107 return -1;
108 }
109 } else {
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,
114 line );
115
116 // terminate line after matched expression
117 line[ subs[0].rm_eo ] = '\0';
118
119 // extract information. If 6th parenthesis is filled, this is
120 // a new squid with disk types, otherwise it is an older version
121 int offset = 2;
122 if ( subs[6].rm_so == -1 ) {
123 // old version, disk type at position 2 is always UFS
124 cd.type = CacheDir::CDT_UFS;
125 } else {
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;
138 else
139 cd.type = CacheDir::CDT_OTHER;
140 ++offset;
141 }
142
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 );
150 ++offset;
151
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, nullptr, 10 );
159 ++offset;
160
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, nullptr, 10 );
168 ++offset;
169
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, nullptr, 10 );
177 ++offset;
178
179 cachedir.push_back( cd );
180 }
181 }
182
183 cfgin.close();
184 regfree(&rexp);
185 return cachedir.size();
186}
187
int regexec(regex_t *preg, const char *string, size_t nmatch, pmatch, int eflags) const
Definition: GnuRegex.c:4195
size_t regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
Definition: GnuRegex.c:4252
int regcomp(regex_t *preg, const char *pattern, int cflags)
Definition: GnuRegex.c:4124
void regfree(regex_t *preg)
Definition: GnuRegex.c:4287
@ REG_NOMATCH
Definition: GnuRegex.h:257
#define REG_EXTENDED
Definition: GnuRegex.h:226
int readConfigFile(CacheDirVector &cachedir, const char *fn, FILE *debug)
Definition: conffile.cc:54
#define xstrdup
char * strerror(int ern)
Definition: strerror.c:22
regoff_t rm_eo
Definition: GnuRegex.h:382
static int debug
Definition: tcp-banger3.c:105

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors