]> git.ipfire.org Git - thirdparty/squid.git/blob - src/StoreMeta.cc
Author: Christos Tsantilas <chtsanti@users.sourceforge.net>
[thirdparty/squid.git] / src / StoreMeta.cc
1
2 /*
3 * $Id: StoreMeta.cc,v 1.6 2007/09/19 08:54:51 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
52 if (type <= STORE_META_VOID || type >= STORE_META_END) {
53 debugs(20, 0, "storeSwapMetaUnpack: bad type (" << type << ")!");
54 return false;
55 }
56
57 /* Unused in any current squid code */
58 if (type == STORE_META_KEY_URL ||
59 type == STORE_META_KEY_SHA ||
60 type == STORE_META_HITMETERING ||
61 type == STORE_META_VALID) {
62 debugs(20, 0, "Obsolete and unused type (" << type << ") in disk metadata");
63 return false;
64 }
65
66 return true;
67 }
68
69 class IntRange
70 {
71
72 public:
73 IntRange (int minimum, int maximum) : _min (minimum), _max (maximum)
74 {
75 if (_min > _max) {
76 int temp = _min;
77 _min = _max;
78 _max = temp;
79 }
80 }
81
82 bool includes (int anInt) const
83 {
84 if (anInt < _min || anInt > _max)
85 return false;
86
87 return true;
88 }
89
90 private:
91 int _min;
92 int _max;
93 };
94
95 const int StoreMeta::MinimumTLVLength = 0;
96 const int StoreMeta::MaximumTLVLength = 1 << 16;
97
98 bool
99 StoreMeta::validLength(int length) const
100 {
101 if (!IntRange (MinimumTLVLength, MaximumTLVLength).includes(length)) {
102 debugs(20, 0, "storeSwapMetaUnpack: insane length (" << length << ")!");
103 return false;
104 }
105
106 return true;
107 }
108
109
110 StoreMeta *
111 StoreMeta::Factory (char type, size_t len, void const *value)
112 {
113 if (!validType(type))
114 return NULL;
115
116 StoreMeta *result;
117
118 switch (type) {
119
120 case STORE_META_KEY:
121 result = new StoreMetaMD5;
122 break;
123
124 case STORE_META_URL:
125 result = new StoreMetaURL;
126 break;
127
128 case STORE_META_STD:
129 result = new StoreMetaSTD;
130 break;
131
132 case STORE_META_STD_LFS:
133 result = new StoreMetaSTDLFS;
134 break;
135
136 case STORE_META_OBJSIZE:
137 result = new StoreMetaObjSize;
138 break;
139
140 case STORE_META_VARY_HEADERS:
141 result = new StoreMetaVary;
142 break;
143
144 default:
145 debugs(20, 0, "Attempt to create unknown concrete StoreMeta");
146 return NULL;
147 }
148
149 if (!result->validLength(len)) {
150 delete result;
151 return NULL;
152 }
153
154 result->length = len;
155 result->value = xmalloc(len);
156 xmemcpy(result->value, value, len);
157 return result;
158 }
159
160 void
161 StoreMeta::FreeList(StoreMeta **head)
162 {
163 StoreMeta *node;
164
165 while ((node = *head) != NULL) {
166 *head = node->next;
167 xfree(node->value);
168 delete node;
169 }
170 }
171
172 StoreMeta **
173 StoreMeta::Add(StoreMeta **tail, StoreMeta *aNode)
174 {
175 assert (*tail == NULL);
176 *tail = aNode;
177 return &aNode->next; /* return new tail pointer */
178 }
179
180 bool
181 StoreMeta::checkConsistency(StoreEntry *e) const
182 {
183 switch (getType()) {
184
185 case STORE_META_KEY:
186
187 case STORE_META_URL:
188
189 case STORE_META_VARY_HEADERS:
190 assert(0);
191 break;
192
193 case STORE_META_STD:
194 break;
195
196 case STORE_META_STD_LFS:
197 break;
198
199 case STORE_META_OBJSIZE:
200 break;
201
202 default:
203 debugs(20, 1, "WARNING: got unused STORE_META type " << getType());
204 break;
205 }
206
207 return true;
208 }