]> git.ipfire.org Git - thirdparty/squid.git/blob - src/StoreMeta.cc
Author: Francesco Chemolli <kinkie@squid-cache.org>
[thirdparty/squid.git] / src / StoreMeta.cc
1
2 /*
3 * $Id: StoreMeta.cc,v 1.8 2007/12/14 20:07:15 hno Exp $
4 *
5 * DEBUG: section 20 Storage Manager Swapfile Metadata
6 * AUTHOR: Kostas Anagnostakis
7 *
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
10 *
11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
19 *
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
33 *
34 */
35
36 #include "squid.h"
37 #include "StoreMeta.h"
38 #include "Store.h"
39 #include "MemObject.h"
40 #include "StoreMetaMD5.h"
41 #include "StoreMetaURL.h"
42 #include "StoreMetaSTD.h"
43 #include "StoreMetaSTDLFS.h"
44 #include "StoreMetaVary.h"
45 #include "StoreMetaObjSize.h"
46
47 bool
48 StoreMeta::validType(char type)
49 {
50 /* VOID is reserved, and new types have to be added as classes */
51 if (type <= STORE_META_VOID || type >= STORE_META_END + 10) {
52 debugs(20, 0, "storeSwapMetaUnpack: bad type (" << type << ")!");
53 return false;
54 }
55
56 /* Not yet implemented */
57 if (type >= STORE_META_END ||
58 type == STORE_META_STOREURL ||
59 type == STORE_META_VARY_ID) {
60 debugs(20, 3, "storeSwapMetaUnpack: Not yet implemented (" << type << ") in disk metadata");
61 return false;
62 }
63
64 /* Unused in any current squid code */
65 if (type == STORE_META_KEY_URL ||
66 type == STORE_META_KEY_SHA ||
67 type == STORE_META_HITMETERING ||
68 type == STORE_META_VALID) {
69 debugs(20, 0, "Obsolete and unused type (" << type << ") in disk metadata");
70 return false;
71 }
72
73 return true;
74 }
75
76 class IntRange
77 {
78
79 public:
80 IntRange (int minimum, int maximum) : _min (minimum), _max (maximum) {
81 if (_min > _max) {
82 int temp = _min;
83 _min = _max;
84 _max = temp;
85 }
86 }
87
88 bool includes (int anInt) const {
89 if (anInt < _min || anInt > _max)
90 return false;
91
92 return true;
93 }
94
95 private:
96 int _min;
97 int _max;
98 };
99
100 const int StoreMeta::MinimumTLVLength = 0;
101 const int StoreMeta::MaximumTLVLength = 1 << 16;
102
103 bool
104 StoreMeta::validLength(int length) const
105 {
106 if (!IntRange (MinimumTLVLength, MaximumTLVLength).includes(length)) {
107 debugs(20, 0, "storeSwapMetaUnpack: insane length (" << length << ")!");
108 return false;
109 }
110
111 return true;
112 }
113
114
115 StoreMeta *
116 StoreMeta::Factory (char type, size_t len, void const *value)
117 {
118 if (!validType(type))
119 return NULL;
120
121 StoreMeta *result;
122
123 switch (type) {
124
125 case STORE_META_KEY:
126 result = new StoreMetaMD5;
127 break;
128
129 case STORE_META_URL:
130 result = new StoreMetaURL;
131 break;
132
133 case STORE_META_STD:
134 result = new StoreMetaSTD;
135 break;
136
137 case STORE_META_STD_LFS:
138 result = new StoreMetaSTDLFS;
139 break;
140
141 case STORE_META_OBJSIZE:
142 result = new StoreMetaObjSize;
143 break;
144
145 case STORE_META_VARY_HEADERS:
146 result = new StoreMetaVary;
147 break;
148
149 default:
150 debugs(20, 0, "Attempt to create unknown concrete StoreMeta");
151 return NULL;
152 }
153
154 if (!result->validLength(len)) {
155 delete result;
156 return NULL;
157 }
158
159 result->length = len;
160 result->value = xmalloc(len);
161 xmemcpy(result->value, value, len);
162 return result;
163 }
164
165 void
166 StoreMeta::FreeList(StoreMeta **head)
167 {
168 StoreMeta *node;
169
170 while ((node = *head) != NULL) {
171 *head = node->next;
172 xfree(node->value);
173 delete node;
174 }
175 }
176
177 StoreMeta **
178 StoreMeta::Add(StoreMeta **tail, StoreMeta *aNode)
179 {
180 assert (*tail == NULL);
181 *tail = aNode;
182 return &aNode->next; /* return new tail pointer */
183 }
184
185 bool
186 StoreMeta::checkConsistency(StoreEntry *e) const
187 {
188 switch (getType()) {
189
190 case STORE_META_KEY:
191
192 case STORE_META_URL:
193
194 case STORE_META_VARY_HEADERS:
195 assert(0);
196 break;
197
198 case STORE_META_STD:
199 break;
200
201 case STORE_META_STD_LFS:
202 break;
203
204 case STORE_META_OBJSIZE:
205 break;
206
207 default:
208 debugs(20, 1, "WARNING: got unused STORE_META type " << getType());
209 break;
210 }
211
212 return true;
213 }