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