2 * Bucket -> Object converter
4 * (c) 2004, Robert Spalek <robert@ucw.cz>
8 #include "lib/unaligned.h"
10 #include "lib/fastbuf.h"
11 #include "charset/unicode.h"
12 #include "lib/object.h"
13 #include "lib/bucket.h"
14 #include "lib/lizard.h"
15 #include "lib/buck2obj.h"
21 #define RET_ERR(num) ({ errno = num; return NULL; })
23 #define GBUF_TYPE byte
24 #define GBUF_PREFIX(x) bb_##x
30 struct lizard_buffer *lizard;
35 buck2obj_alloc(struct mempool *mp)
37 struct buck2obj_buf *buf = xmalloc(sizeof(struct buck2obj_buf));
39 buf->lizard = lizard_alloc();
45 buck2obj_free(struct buck2obj_buf *buf)
47 lizard_free(buf->lizard);
53 buck2obj_flush(struct buck2obj_buf *buf)
59 decode_attributes(byte *ptr, byte *end, struct odes *o, uns can_overwrite)
61 if (can_overwrite >= 2)
71 obj_add_attr_ref(o, type, ptr);
75 else if (can_overwrite == 1)
85 obj_add_attr(o, type, ptr);
99 byte *dup = mp_alloc_fast_noalign(o->pool, len+1);
100 memcpy(dup, ptr, len);
102 obj_add_attr_ref(o, type, dup);
110 obj_read_bucket(struct buck2obj_buf *buf, uns buck_type, uns buck_len, struct fastbuf *body, uns *body_start)
112 struct odes *o = obj_new(buf->mp);
114 if (buck_type < BUCKET_TYPE_V33)
116 if (!body_start) // header + body: ignore empty lines, read until EOF
118 obj_read_multi(body, o);
121 else // header only: end on EOF or the first empty line
123 sh_off_t start = btell(body);
125 *body_start = btell(body) - start;
130 /* Read all the bucket into 1 buffer, 0-copy if possible. */
131 int can_overwrite = bconfig(body, BCONFIG_CAN_OVERWRITE, -1);
132 /* FIXME: This could be cached in buck2obj_buf */
133 if (can_overwrite < 0)
137 uns len = bdirect_read_prepare(body, &ptr);
139 || (can_overwrite < 2 && buck_type == BUCKET_TYPE_V33))
141 /* Copy if the original buffer is too small.
142 * If it is write-protected, copy it also if it is uncompressed. */
143 bb_grow(&buf->bb, buck_len);
144 len = bread(body, buf->bb.ptr, buck_len);
150 overwritten = can_overwrite > 1;
154 ptr = decode_attributes(ptr, end, o, can_overwrite);// header
157 *body_start = ptr - start;
160 if (buck_type == BUCKET_TYPE_V33)
162 else if (buck_type == BUCKET_TYPE_V33_LIZARD) // decompression
168 res = lizard_decompress_safe(ptr, buf->lizard, len, &new_ptr);
169 if (res != (int) len)
179 else // unknown bucket type
181 ASSERT(can_overwrite == 2); // because of the policy and decompression
182 ptr = decode_attributes(ptr, end, o, 2); // body
186 /* If (overwritten), bflush(body) might be needed. */
192 obj_attr_to_bucket(byte *buf, uns buck_type, uns attr, byte *val)
198 case BUCKET_TYPE_PLAIN:
199 case BUCKET_TYPE_V30:
200 buf += sprintf(buf, "%c%s\n", attr, val);
202 case BUCKET_TYPE_V33:
203 case BUCKET_TYPE_V33_LIZARD:
212 die("obj_attr_to_bucket called for unknown type %08x", buck_type);
218 obj_attr_to_bucket_num(byte *buf, uns buck_type, uns attr, uns val)
221 sprintf(vbuf, "%d", val);
222 return obj_attr_to_bucket(buf, buck_type, attr, vbuf);