]>
Commit | Line | Data |
---|---|---|
528b2c61 | 1 | /* |
bbc27441 | 2 | * Copyright (C) 1996-2014 The Squid Software Foundation and contributors |
528b2c61 | 3 | * |
bbc27441 AJ |
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. | |
528b2c61 | 7 | */ |
8 | ||
bbc27441 AJ |
9 | /* DEBUG: section 20 Storage Manager Swapfile Metadata */ |
10 | ||
582c2af2 | 11 | #include "squid.h" |
528b2c61 | 12 | #include "MemObject.h" |
602d9612 A |
13 | #include "Store.h" |
14 | #include "StoreMeta.h" | |
528b2c61 | 15 | #include "StoreMetaMD5.h" |
602d9612 | 16 | #include "StoreMetaObjSize.h" |
528b2c61 | 17 | #include "StoreMetaSTD.h" |
47f6e231 | 18 | #include "StoreMetaSTDLFS.h" |
602d9612 | 19 | #include "StoreMetaURL.h" |
528b2c61 | 20 | #include "StoreMetaVary.h" |
21 | ||
22 | bool | |
23 | StoreMeta::validType(char type) | |
24 | { | |
25 | /* VOID is reserved, and new types have to be added as classes */ | |
b7f7b5a3 | 26 | if (type <= STORE_META_VOID || type >= STORE_META_END + 10) { |
fa84c01d | 27 | debugs(20, DBG_CRITICAL, "storeSwapMetaUnpack: bad type (" << type << ")!"); |
62e76326 | 28 | return false; |
528b2c61 | 29 | } |
62e76326 | 30 | |
b7f7b5a3 | 31 | /* Not yet implemented */ |
02f95cf0 | 32 | if (type >= STORE_META_END || |
26ac0430 AJ |
33 | type == STORE_META_STOREURL || |
34 | type == STORE_META_VARY_ID) { | |
b7f7b5a3 | 35 | debugs(20, 3, "storeSwapMetaUnpack: Not yet implemented (" << type << ") in disk metadata"); |
36 | return false; | |
37 | } | |
38 | ||
528b2c61 | 39 | /* Unused in any current squid code */ |
40 | if (type == STORE_META_KEY_URL || | |
62e76326 | 41 | type == STORE_META_KEY_SHA || |
42 | type == STORE_META_HITMETERING || | |
43 | type == STORE_META_VALID) { | |
fa84c01d | 44 | debugs(20, DBG_CRITICAL, "Obsolete and unused type (" << type << ") in disk metadata"); |
62e76326 | 45 | return false; |
528b2c61 | 46 | } |
62e76326 | 47 | |
528b2c61 | 48 | return true; |
49 | } | |
50 | ||
62e76326 | 51 | class IntRange |
52 | { | |
53 | ||
528b2c61 | 54 | public: |
26ac0430 | 55 | IntRange (int minimum, int maximum) : _min (minimum), _max (maximum) { |
62e76326 | 56 | if (_min > _max) { |
57 | int temp = _min; | |
58 | _min = _max; | |
59 | _max = temp; | |
60 | } | |
61 | } | |
62 | ||
26ac0430 | 63 | bool includes (int anInt) const { |
62e76326 | 64 | if (anInt < _min || anInt > _max) |
65 | return false; | |
66 | ||
67 | return true; | |
68 | } | |
69 | ||
528b2c61 | 70 | private: |
71 | int _min; | |
72 | int _max; | |
73 | }; | |
74 | ||
75 | const int StoreMeta::MinimumTLVLength = 0; | |
76 | const int StoreMeta::MaximumTLVLength = 1 << 16; | |
77 | ||
78 | bool | |
e4ae841b | 79 | StoreMeta::validLength(int aLength) const |
528b2c61 | 80 | { |
e4ae841b | 81 | if (!IntRange (MinimumTLVLength, MaximumTLVLength).includes(aLength)) { |
fa84c01d | 82 | debugs(20, DBG_CRITICAL, "storeSwapMetaUnpack: insane length (" << aLength << ")!"); |
62e76326 | 83 | return false; |
528b2c61 | 84 | } |
62e76326 | 85 | |
528b2c61 | 86 | return true; |
87 | } | |
88 | ||
528b2c61 | 89 | StoreMeta * |
90 | StoreMeta::Factory (char type, size_t len, void const *value) | |
91 | { | |
92 | if (!validType(type)) | |
93 | return NULL; | |
62e76326 | 94 | |
528b2c61 | 95 | StoreMeta *result; |
62e76326 | 96 | |
528b2c61 | 97 | switch (type) { |
62e76326 | 98 | |
99 | case STORE_META_KEY: | |
100 | result = new StoreMetaMD5; | |
101 | break; | |
102 | ||
103 | case STORE_META_URL: | |
104 | result = new StoreMetaURL; | |
105 | break; | |
106 | ||
107 | case STORE_META_STD: | |
108 | result = new StoreMetaSTD; | |
109 | break; | |
110 | ||
47f6e231 | 111 | case STORE_META_STD_LFS: |
112 | result = new StoreMetaSTDLFS; | |
113 | break; | |
114 | ||
26ac0430 AJ |
115 | case STORE_META_OBJSIZE: |
116 | result = new StoreMetaObjSize; | |
9e6e1d99 | 117 | break; |
118 | ||
62e76326 | 119 | case STORE_META_VARY_HEADERS: |
120 | result = new StoreMetaVary; | |
121 | break; | |
122 | ||
123 | default: | |
fa84c01d | 124 | debugs(20, DBG_CRITICAL, "Attempt to create unknown concrete StoreMeta"); |
62e76326 | 125 | return NULL; |
528b2c61 | 126 | } |
62e76326 | 127 | |
528b2c61 | 128 | if (!result->validLength(len)) { |
00d77d6b | 129 | delete result; |
62e76326 | 130 | return NULL; |
528b2c61 | 131 | } |
62e76326 | 132 | |
528b2c61 | 133 | result->length = len; |
134 | result->value = xmalloc(len); | |
41d00cd3 | 135 | memcpy(result->value, value, len); |
528b2c61 | 136 | return result; |
137 | } | |
138 | ||
139 | void | |
140 | StoreMeta::FreeList(StoreMeta **head) | |
141 | { | |
142 | StoreMeta *node; | |
62e76326 | 143 | |
528b2c61 | 144 | while ((node = *head) != NULL) { |
62e76326 | 145 | *head = node->next; |
146 | xfree(node->value); | |
00d77d6b | 147 | delete node; |
528b2c61 | 148 | } |
149 | } | |
150 | ||
151 | StoreMeta ** | |
152 | StoreMeta::Add(StoreMeta **tail, StoreMeta *aNode) | |
153 | { | |
154 | assert (*tail == NULL); | |
155 | *tail = aNode; | |
f53969cc | 156 | return &aNode->next; /* return new tail pointer */ |
528b2c61 | 157 | } |
158 | ||
62e76326 | 159 | bool |
528b2c61 | 160 | StoreMeta::checkConsistency(StoreEntry *e) const |
161 | { | |
62e76326 | 162 | switch (getType()) { |
163 | ||
164 | case STORE_META_KEY: | |
165 | ||
166 | case STORE_META_URL: | |
167 | ||
168 | case STORE_META_VARY_HEADERS: | |
169 | assert(0); | |
170 | break; | |
171 | ||
172 | case STORE_META_STD: | |
173 | break; | |
174 | ||
47f6e231 | 175 | case STORE_META_STD_LFS: |
176 | break; | |
177 | ||
178 | case STORE_META_OBJSIZE: | |
26ac0430 | 179 | break; |
47f6e231 | 180 | |
62e76326 | 181 | default: |
e0236918 | 182 | debugs(20, DBG_IMPORTANT, "WARNING: got unused STORE_META type " << getType()); |
62e76326 | 183 | break; |
184 | } | |
185 | ||
528b2c61 | 186 | return true; |
187 | } | |
f53969cc | 188 |