conffile.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 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 <memory.h>
51
52#if HAVE_REGEX_H
53#include <regex.h>
54#endif
55
56#include <sys/types.h>
57
58int
59readConfigFile( 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
64{
65 static const char* expression =
66 "^[ \t]*cache_dir([ \t]+([[:alpha:]]+))?[ \t]+([[:graph:]]+)[ \t]+([0-9]+)[ \t]+([0-9]+)[ \t]+([0-9]+)";
67
68 // try to open file
69 if ( debug ) fprintf( debug, "# trying to open %s\n", fn ? fn : "(null)" );
70 std::ifstream cfgin(fn);
71 if (!cfgin) {
72 fprintf( stderr, "fopen %s: %s\n", fn, strerror(errno) );
73 return -1;
74 }
75
76 // prepare regular expression for matching
77 if ( debug ) fprintf( debug, "# trying to compile \"%s\"\n", expression );
78 regex_t rexp;
79 int result = regcomp( &rexp, expression, REG_EXTENDED );
80 if ( result != 0 ) {
81 char buffer[256];
82 regerror( result, &rexp, buffer, sizeof(buffer) );
83 fprintf( stderr, "regular expression \"%s\": %s\n", expression, buffer );
84 return -1;
85 }
86
87 // read line by line
88 if ( debug ) fputs( "# trying to read lines\n", debug );
89
90 regmatch_t subs[8];
91 char *s, line[1024];
92 CacheDir cd;
93 while ( cfgin.getline( line, sizeof(line)) ) {
94 // TODO: overly long lines
95
96 // terminate line at start of comment
97 if ( (s = (char*) memchr( line, '#', sizeof(line) )) ) *s = '\0';
98
99 // quick skip
100 if ( *line == '\0' || *line == '\n' ) continue;
101
102 // test line
103 if ( (result=regexec( &rexp, line, 7, subs, 0 )) != 0 ) {
104 // error or no match
105 if ( result != REG_NOMATCH ) {
106 char buffer[256];
107 regerror( result, &rexp, buffer, sizeof(buffer) );
108 fprintf( stderr, "while matching \"%s\" against %s%s\n",
109 expression, line, buffer );
110 regfree(&rexp);
111 cfgin.close();
112 return -1;
113 }
114 } else {
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,
119 line );
120
121 // terminate line after matched expression
122 line[ subs[0].rm_eo ] = '\0';
123
124 // extract information. If 6th parenthesis is filled, this is
125 // a new squid with disk types, otherwise it is an older version
126 int offset = 2;
127 if ( subs[6].rm_so == -1 ) {
128 // old version, disk type at position 2 is always UFS
129 cd.type = CacheDir::CDT_UFS;
130 } else {
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;
143 else
144 cd.type = CacheDir::CDT_OTHER;
145 ++offset;
146 }
147
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 );
155 ++offset;
156
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 );
164 ++offset;
165
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 );
173 ++offset;
174
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 );
182 ++offset;
183
184 cachedir.push_back( cd );
185 }
186 }
187
188 cfgin.close();
189 regfree(&rexp);
190 return cachedir.size();
191}
192
void debug(const char *format,...)
Definition: debug.cc:19
int readConfigFile(CacheDirVector &cachedir, const char *fn, FILE *debug)
Definition: conffile.cc:59
#define xstrdup
char * strerror(int ern)
Definition: strerror.c:22

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors