]>
Commit | Line | Data |
---|---|---|
25535cbe | 1 | |
9cef6668 | 2 | /* |
5f341392 | 3 | * $Id: store_swapmeta.cc,v 1.17 2001/10/24 08:52:37 hno Exp $ |
9cef6668 | 4 | * |
5 | * DEBUG: section 20 Storage Manager Swapfile Metadata | |
6 | * AUTHOR: Kostas Anagnostakis | |
7 | * | |
2b6662ba | 8 | * SQUID Web Proxy Cache http://www.squid-cache.org/ |
9cef6668 | 9 | * ---------------------------------------------------------- |
10 | * | |
2b6662ba | 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. | |
9cef6668 | 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 | ||
f09f5b26 | 36 | #include "squid.h" |
37 | ||
69c01cd7 | 38 | static tlv ** |
39 | storeSwapTLVAdd(int type, const void *ptr, size_t len, tlv ** tail) | |
f09f5b26 | 40 | { |
58cd5bbd | 41 | tlv *t = memAllocate(MEM_TLV); |
69c01cd7 | 42 | t->type = (char) type; |
43 | t->length = (int) len; | |
44 | t->value = xmalloc(len); | |
45 | xmemcpy(t->value, ptr, len); | |
46 | *tail = t; | |
47 | return &t->next; /* return new tail pointer */ | |
f09f5b26 | 48 | } |
49 | ||
69c01cd7 | 50 | void |
51 | storeSwapTLVFree(tlv * n) | |
f09f5b26 | 52 | { |
69c01cd7 | 53 | tlv *t; |
54 | while ((t = n) != NULL) { | |
55 | n = t->next; | |
56 | xfree(t->value); | |
58cd5bbd | 57 | memFree(t, MEM_TLV); |
f09f5b26 | 58 | } |
f09f5b26 | 59 | } |
60 | ||
69c01cd7 | 61 | /* |
62 | * Build a TLV list for a StoreEntry | |
63 | */ | |
64 | tlv * | |
65 | storeSwapMetaBuild(StoreEntry * e) | |
f09f5b26 | 66 | { |
69c01cd7 | 67 | tlv *TLV = NULL; /* we'll return this */ |
68 | tlv **T = &TLV; | |
69 | const char *url; | |
f66a9ef4 | 70 | const char *vary; |
10602161 | 71 | assert(e->mem_obj != NULL); |
69c01cd7 | 72 | assert(e->swap_status == SWAPOUT_WRITING); |
73 | url = storeUrl(e); | |
74 | debug(20, 3) ("storeSwapMetaBuild: %s\n", url); | |
186477c1 | 75 | T = storeSwapTLVAdd(STORE_META_KEY, e->hash.key, MD5_DIGEST_CHARS, T); |
69c01cd7 | 76 | T = storeSwapTLVAdd(STORE_META_STD, &e->timestamp, STORE_HDR_METASIZE, T); |
5830cdb3 | 77 | T = storeSwapTLVAdd(STORE_META_URL, url, strlen(url) + 1, T); |
f66a9ef4 | 78 | vary = e->mem_obj->vary_headers; |
79 | if (vary) | |
80 | T = storeSwapTLVAdd(STORE_META_VARY_HEADERS, vary, strlen(vary) + 1, T); | |
69c01cd7 | 81 | return TLV; |
f09f5b26 | 82 | } |
83 | ||
69c01cd7 | 84 | char * |
85 | storeSwapMetaPack(tlv * tlv_list, int *length) | |
f09f5b26 | 86 | { |
e3ef2b09 | 87 | int buflen = 0; |
69c01cd7 | 88 | tlv *t; |
89 | off_t j = 0; | |
90 | char *buf; | |
91 | assert(length != NULL); | |
92 | buflen++; /* STORE_META_OK */ | |
93 | buflen += sizeof(int); /* size of header to follow */ | |
94 | for (t = tlv_list; t; t = t->next) | |
e3ef2b09 | 95 | buflen += sizeof(char) + sizeof(int) + t->length; |
69c01cd7 | 96 | buflen++; /* STORE_META_END */ |
97 | buf = xmalloc(buflen); | |
98 | buf[j++] = (char) STORE_META_OK; | |
e3ef2b09 | 99 | xmemcpy(&buf[j], &buflen, sizeof(int)); |
100 | j += sizeof(int); | |
69c01cd7 | 101 | for (t = tlv_list; t; t = t->next) { |
102 | buf[j++] = (char) t->type; | |
103 | xmemcpy(&buf[j], &t->length, sizeof(int)); | |
104 | j += sizeof(int); | |
105 | xmemcpy(&buf[j], t->value, t->length); | |
106 | j += t->length; | |
f09f5b26 | 107 | } |
69c01cd7 | 108 | buf[j++] = (char) STORE_META_END; |
e3ef2b09 | 109 | assert((int) j == buflen); |
110 | *length = buflen; | |
69c01cd7 | 111 | return buf; |
f09f5b26 | 112 | } |
113 | ||
69c01cd7 | 114 | tlv * |
115 | storeSwapMetaUnpack(const char *buf, int *hdr_len) | |
f09f5b26 | 116 | { |
69c01cd7 | 117 | tlv *TLV; /* we'll return this */ |
118 | tlv **T = &TLV; | |
119 | char type; | |
120 | int length; | |
121 | int buflen; | |
122 | off_t j = 0; | |
69c01cd7 | 123 | assert(buf != NULL); |
124 | assert(hdr_len != NULL); | |
125 | if (buf[j++] != (char) STORE_META_OK) | |
126 | return NULL; | |
127 | xmemcpy(&buflen, &buf[j], sizeof(int)); | |
128 | j += sizeof(int); | |
42b51993 | 129 | /* |
130 | * sanity check on 'buflen' value. It should be at least big | |
131 | * enough to hold one type and one length. | |
132 | */ | |
133 | if (buflen <= (sizeof(char) + sizeof(int))) | |
134 | return NULL; | |
69c01cd7 | 135 | while (buflen - j > (sizeof(char) + sizeof(int))) { |
136 | type = buf[j++]; | |
5f341392 | 137 | /* VOID is reserved, but allow some slack for new types.. */ |
138 | if (type <= STORE_META_VOID || type > STORE_META_END + 10) { | |
fff97a60 | 139 | debug(20, 0) ("storeSwapMetaUnpack: bad type (%d)!\n", type); |
140 | break; | |
141 | } | |
69c01cd7 | 142 | xmemcpy(&length, &buf[j], sizeof(int)); |
5ba01c92 | 143 | if (length < 0 || length > (1 << 16)) { |
fff97a60 | 144 | debug(20, 0) ("storeSwapMetaUnpack: insane length (%d)!\n", length); |
145 | break; | |
146 | } | |
69c01cd7 | 147 | j += sizeof(int); |
148 | if (j + length > buflen) { | |
149 | debug(20, 0) ("storeSwapMetaUnpack: overflow!\n"); | |
150 | debug(20, 0) ("\ttype=%d, length=%d, buflen=%d, offset=%d\n", | |
151 | type, length, buflen, (int) j); | |
152 | break; | |
153 | } | |
154 | T = storeSwapTLVAdd(type, &buf[j], (size_t) length, T); | |
155 | j += length; | |
156 | } | |
e3ef2b09 | 157 | *hdr_len = buflen; |
69c01cd7 | 158 | return TLV; |
f09f5b26 | 159 | } |