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