]>
Commit | Line | Data |
---|---|---|
528b2c61 | 1 | |
2 | /* | |
3 | * $Id: StoreMeta.cc,v 1.1 2003/01/23 00:37:14 robertc 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 "StoreMetaVary.h" | |
44 | ||
45 | bool | |
46 | StoreMeta::validType(char type) | |
47 | { | |
48 | /* VOID is reserved, and new types have to be added as classes */ | |
49 | if (type <= STORE_META_VOID || type >= STORE_META_END) { | |
50 | debug(20, 0) ("storeSwapMetaUnpack: bad type (%d)!\n", type); | |
51 | return false; | |
52 | } | |
53 | /* Unused in any current squid code */ | |
54 | if (type == STORE_META_KEY_URL || | |
55 | type == STORE_META_KEY_SHA || | |
56 | type == STORE_META_HITMETERING || | |
57 | type == STORE_META_VALID) { | |
58 | debug (20,0)("Obsolete and unused type (%d) in disk metadata\n", type); | |
59 | return false; | |
60 | } | |
61 | return true; | |
62 | } | |
63 | ||
64 | class IntRange{ | |
65 | public: | |
66 | IntRange (int minimum, int maximum) : _min (minimum), _max (maximum) | |
67 | { | |
68 | if (_min > _max) { | |
69 | int temp = _min; | |
70 | _min = _max; | |
71 | _max = temp; | |
72 | } | |
73 | } | |
74 | bool includes (int anInt) const | |
75 | { | |
76 | if (anInt < _min || anInt > _max) | |
77 | return false; | |
78 | return true; | |
79 | } | |
80 | private: | |
81 | int _min; | |
82 | int _max; | |
83 | }; | |
84 | ||
85 | const int StoreMeta::MinimumTLVLength = 0; | |
86 | const int StoreMeta::MaximumTLVLength = 1 << 16; | |
87 | ||
88 | bool | |
89 | StoreMeta::validLength(int length) const | |
90 | { | |
91 | if (!IntRange (MinimumTLVLength, MaximumTLVLength).includes(length)){ | |
92 | debug(20, 0) ("storeSwapMetaUnpack: insane length (%d)!\n", length); | |
93 | return false; | |
94 | } | |
95 | return true; | |
96 | } | |
97 | ||
98 | ||
99 | StoreMeta * | |
100 | StoreMeta::Factory (char type, size_t len, void const *value) | |
101 | { | |
102 | if (!validType(type)) | |
103 | return NULL; | |
104 | StoreMeta *result; | |
105 | switch (type) { | |
106 | case STORE_META_KEY: | |
107 | result = new StoreMetaMD5; | |
108 | break; | |
109 | case STORE_META_URL: | |
110 | result = new StoreMetaURL; | |
111 | break; | |
112 | case STORE_META_STD: | |
113 | result = new StoreMetaSTD; | |
114 | break; | |
115 | case STORE_META_VARY_HEADERS: | |
116 | result = new StoreMetaVary; | |
117 | break; | |
118 | default: | |
119 | debug (20,0)("Attempt to create unknown concrete StoreMeta\n"); | |
120 | return NULL; | |
121 | } | |
122 | if (!result->validLength(len)) { | |
123 | result->deleteSelf(); | |
124 | return NULL; | |
125 | } | |
126 | result->length = len; | |
127 | result->value = xmalloc(len); | |
128 | xmemcpy(result->value, value, len); | |
129 | return result; | |
130 | } | |
131 | ||
132 | void | |
133 | StoreMeta::FreeList(StoreMeta **head) | |
134 | { | |
135 | StoreMeta *node; | |
136 | while ((node = *head) != NULL) { | |
137 | *head = node->next; | |
138 | xfree(node->value); | |
139 | node->deleteSelf(); | |
140 | } | |
141 | } | |
142 | ||
143 | StoreMeta ** | |
144 | StoreMeta::Add(StoreMeta **tail, StoreMeta *aNode) | |
145 | { | |
146 | assert (*tail == NULL); | |
147 | *tail = aNode; | |
148 | return &aNode->next; /* return new tail pointer */ | |
149 | } | |
150 | ||
151 | bool | |
152 | StoreMeta::checkConsistency(StoreEntry *e) const | |
153 | { | |
154 | switch (getType()) | |
155 | { | |
156 | case STORE_META_KEY: | |
157 | case STORE_META_URL: | |
158 | case STORE_META_VARY_HEADERS: | |
159 | assert(0); | |
160 | break; | |
161 | case STORE_META_STD: | |
162 | break; | |
163 | default: | |
164 | debug(20, 1) ("WARNING: got unused STORE_META type %d\n", getType()); | |
165 | break; | |
166 | } | |
167 | return true; | |
168 | } | |
169 | ||
170 | void | |
171 | StoreMeta::deleteSelf() | |
172 | { | |
173 | delete this; | |
174 | } |