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