]> git.ipfire.org Git - thirdparty/squid.git/blame - src/store_swapmeta.cc
Convert Vary handling to SBuf
[thirdparty/squid.git] / src / store_swapmeta.cc
CommitLineData
9cef6668 1/*
ef57eb7b 2 * Copyright (C) 1996-2016 The Squid Software Foundation and contributors
9cef6668 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.
9cef6668 7 */
8
bbc27441
AJ
9/* DEBUG: section 20 Storage Manager Swapfile Metadata */
10
582c2af2
FC
11#include "squid.h"
12#include "md5.h"
528b2c61 13#include "MemObject.h"
582c2af2 14#include "Store.h"
528b2c61 15#include "StoreMeta.h"
16#include "StoreMetaUnpacker.h"
f09f5b26 17
582c2af2
FC
18#if HAVE_SYS_WAIT_H
19#include <sys/wait.h>
20#endif
21
69c01cd7 22void
23storeSwapTLVFree(tlv * n)
f09f5b26 24{
69c01cd7 25 tlv *t;
62e76326 26
69c01cd7 27 while ((t = n) != NULL) {
62e76326 28 n = t->next;
29 xfree(t->value);
00d77d6b 30 delete t;
f09f5b26 31 }
f09f5b26 32}
33
69c01cd7 34/*
35 * Build a TLV list for a StoreEntry
36 */
37tlv *
38storeSwapMetaBuild(StoreEntry * e)
f09f5b26 39{
f53969cc 40 tlv *TLV = NULL; /* we'll return this */
69c01cd7 41 tlv **T = &TLV;
10602161 42 assert(e->mem_obj != NULL);
aa1a691e 43 const int64_t objsize = e->mem_obj->expectedReplySize();
a8a0b1c2
EC
44
45 // e->mem_obj->request may be nil in this context
851feda6 46 SBuf url;
a8a0b1c2
EC
47 if (e->mem_obj->request)
48 url = e->mem_obj->request->storeId();
49 else
50 url = e->url();
51
52 debugs(20, 3, "storeSwapMetaBuild URL: " << url);
53
c3031d67 54 tlv *t = StoreMeta::Factory (STORE_META_KEY,SQUID_MD5_DIGEST_LENGTH, e->key);
62e76326 55
528b2c61 56 if (!t) {
62e76326 57 storeSwapTLVFree(TLV);
58 return NULL;
528b2c61 59 }
62e76326 60
528b2c61 61 T = StoreMeta::Add(T, t);
47f6e231 62 t = StoreMeta::Factory(STORE_META_STD_LFS,STORE_HDR_METASIZE,&e->timestamp);
62e76326 63
528b2c61 64 if (!t) {
62e76326 65 storeSwapTLVFree(TLV);
66 return NULL;
528b2c61 67 }
62e76326 68
851feda6 69 // XXX: do TLV without the c_str() termination. check readers first though
528b2c61 70 T = StoreMeta::Add(T, t);
851feda6 71 t = StoreMeta::Factory(STORE_META_URL, url.length()+1, url.c_str());
62e76326 72
528b2c61 73 if (!t) {
62e76326 74 storeSwapTLVFree(TLV);
75 return NULL;
528b2c61 76 }
62e76326 77
ffcd6c5f 78 if (objsize >= 0) {
15f1c218
A
79 T = StoreMeta::Add(T, t);
80 t = StoreMeta::Factory(STORE_META_OBJSIZE, sizeof(objsize), &objsize);
ffcd6c5f 81
15f1c218
A
82 if (!t) {
83 storeSwapTLVFree(TLV);
84 return NULL;
85 }
ffcd6c5f
HN
86 }
87
528b2c61 88 T = StoreMeta::Add(T, t);
90ab8f20 89 SBuf vary(e->mem_obj->vary_headers);
62e76326 90
90ab8f20
AJ
91 if (!vary.isEmpty()) {
92 // TODO: do we still need +1 here? StoreMetaVary::checkConsistency
93 // no longer relies on nul-termination, but other things might.
94 t = StoreMeta::Factory(STORE_META_VARY_HEADERS, vary.length() + 1, vary.c_str());
62e76326 95
96 if (!t) {
97 storeSwapTLVFree(TLV);
98 return NULL;
99 }
100
101 StoreMeta::Add (T, t);
528b2c61 102 }
62e76326 103
69c01cd7 104 return TLV;
f09f5b26 105}
106
69c01cd7 107char *
108storeSwapMetaPack(tlv * tlv_list, int *length)
f09f5b26 109{
e3ef2b09 110 int buflen = 0;
69c01cd7 111 tlv *t;
57d55dfa 112 int j = 0;
69c01cd7 113 char *buf;
114 assert(length != NULL);
f53969cc
SM
115 ++buflen; /* STORE_META_OK */
116 buflen += sizeof(int); /* size of header to follow */
62e76326 117
69c01cd7 118 for (t = tlv_list; t; t = t->next)
62e76326 119 buflen += sizeof(char) + sizeof(int) + t->length;
120
e6ccf245 121 buf = (char *)xmalloc(buflen);
62e76326 122
5db6bf73
FC
123 buf[j] = (char) STORE_META_OK;
124 ++j;
62e76326 125
41d00cd3 126 memcpy(&buf[j], &buflen, sizeof(int));
62e76326 127
e3ef2b09 128 j += sizeof(int);
62e76326 129
69c01cd7 130 for (t = tlv_list; t; t = t->next) {
5db6bf73
FC
131 buf[j] = t->getType();
132 ++j;
41d00cd3 133 memcpy(&buf[j], &t->length, sizeof(int));
62e76326 134 j += sizeof(int);
41d00cd3 135 memcpy(&buf[j], t->value, t->length);
62e76326 136 j += t->length;
f09f5b26 137 }
62e76326 138
e3ef2b09 139 assert((int) j == buflen);
140 *length = buflen;
69c01cd7 141 return buf;
f09f5b26 142}
f53969cc 143