]> git.ipfire.org Git - thirdparty/squid.git/blame - tools/purge/conffile.cc
Docs: Copyright updates for 2018 (#114)
[thirdparty/squid.git] / tools / purge / conffile.cc
CommitLineData
5f623035 1/*
5b74111a 2 * Copyright (C) 1996-2018 The Squid Software Foundation and contributors
5f623035
AJ
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 */
75072239 8
0b96a9b3 9// Author: Jens-S. V?ckler <voeckler@rvs.uni-hannover.de>
eb1f6bfa
AJ
10//
11// File: conffile.cc
12// Fri Sep 15 2000
13//
14// (c) 2000 Lehrgebiet Rechnernetze und Verteilte Systeme
0b96a9b3 15// Universit?t Hannover, Germany
eb1f6bfa
AJ
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//
eb1f6bfa
AJ
39// Revision 1.1 2000/09/21 09:44:53 voeckler
40// Initial revision
41//
5f623035
AJ
42
43#include "squid.h"
eb1f6bfa 44#include "conffile.hh"
074d6a40
AJ
45
46#include <cerrno>
47#include <cstdlib>
48#include <cstring>
49#include <fstream>
eb1f6bfa 50#include <sys/types.h>
eb1f6bfa 51#include <memory.h>
eb1f6bfa
AJ
52
53int
54readConfigFile( CacheDirVector& cachedir, const char* fn, FILE* debug )
feec68a0
A
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
eb1f6bfa 59{
c2afddd8
AJ
60 static const char* expression =
61 "^[ \t]*cache_dir([ \t]+([[:alpha:]]+))?[ \t]+([[:graph:]]+)[ \t]+([0-9]+)[ \t]+([0-9]+)[ \t]+([0-9]+)";
62
feec68a0
A
63 // try to open file
64 if ( debug ) fprintf( debug, "# trying to open %s\n", fn ? fn : "(null)" );
446e1d78 65 std::ifstream cfgin(fn);
30f9c749 66 if (!cfgin) {
feec68a0
A
67 fprintf( stderr, "fopen %s: %s\n", fn, strerror(errno) );
68 return -1;
eb1f6bfa 69 }
eb1f6bfa 70
feec68a0
A
71 // prepare regular expression for matching
72 if ( debug ) fprintf( debug, "# trying to compile \"%s\"\n", expression );
c2afddd8
AJ
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 }
feec68a0
A
81
82 // read line by line
83 if ( debug ) fputs( "# trying to read lines\n", debug );
84
c2afddd8 85 regmatch_t subs[8];
feec68a0
A
86 char *s, line[1024];
87 CacheDir cd;
446e1d78 88 while ( cfgin.getline( line, sizeof(line)) ) {
feec68a0
A
89 // FIXME: 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
c2afddd8
AJ
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 }
feec68a0 109 } else {
c2afddd8
AJ
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
feec68a0 124 cd.type = CacheDir::CDT_UFS;
c2afddd8
AJ
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 );
14942edd 150 ++offset;
feec68a0 151
c2afddd8
AJ
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 );
159 ++offset;
feec68a0 160
c2afddd8
AJ
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 );
168 ++offset;
feec68a0 169
c2afddd8
AJ
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 );
177 ++offset;
2056c6a6 178
c2afddd8
AJ
179 cachedir.push_back( cd );
180 }
feec68a0
A
181 }
182
446e1d78 183 cfgin.close();
c2afddd8 184 regfree(&rexp);
feec68a0 185 return cachedir.size();
eb1f6bfa 186}
f53969cc 187