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 MAX_HEADER_SIZE 1024 // extra space for the header not counted in MaxObjSize
22 #define RET_ERR(num) ({ errno = num; return NULL; })
28 struct lizard_buffer *lizard;
33 buck2obj_alloc(uns max_len, struct mempool *mp)
35 struct buck2obj_buf *buf = xmalloc(sizeof(struct buck2obj_buf));
36 buf->raw_len = max_len * LIZARD_MAX_MULTIPLY + LIZARD_MAX_ADD + MAX_HEADER_SIZE;
37 buf->raw = xmalloc(buf->raw_len);
38 buf->lizard = lizard_alloc(max_len);
44 buck2obj_free(struct buck2obj_buf *buf)
46 lizard_free(buf->lizard);
52 decode_attributes(byte *ptr, byte *end, struct odes *o, uns can_overwrite)
54 if (can_overwrite >= 2)
64 obj_add_attr_ref(o, type, ptr);
68 else if (can_overwrite == 1)
78 obj_add_attr(o, type, ptr);
92 byte *dup = mp_alloc_fast_noalign(o->pool, len+1);
93 memcpy(dup, ptr, len);
95 obj_add_attr_ref(o, type, dup);
103 buck2obj_convert(struct buck2obj_buf *buf, uns buck_type, struct fastbuf *body)
106 struct odes *o = obj_new(buf->mp);
108 if (buck_type < BUCKET_TYPE_V33)
109 obj_read_multi(body, o);
112 /* Compute the length of the bucket. We cannot fetch this attribute
113 * directly due to remote indexing. */
114 bseek(body, 0, SEEK_END);
115 sh_off_t buck_len = btell(body);
118 /* Read all the bucket into 1 buffer, 0-copy if possible. */
119 int can_overwrite = bconfig(body, BCONFIG_CAN_OVERWRITE, 0);
120 if (can_overwrite < 0)
124 uns len = bdirect_read_prepare(body, &ptr);
126 || (can_overwrite < 2 && buck_type == BUCKET_TYPE_V33))
128 /* Copy if the original buffer is too small.
129 * If it is write-protected, copy it also if it is uncompressed. */
130 if (buck_len > buf->raw_len)
132 len = bread(body, buf->raw, buck_len);
138 overwritten = can_overwrite > 1;
141 ptr = decode_attributes(ptr, end, o, can_overwrite);// header
142 if (buck_type == BUCKET_TYPE_V33)
144 else if (buck_type == BUCKET_TYPE_V33_LIZARD) // decompression
148 int res = lizard_decompress_safe(ptr, buf->lizard, len);
149 if (res != (int) len)
155 ptr = buf->lizard->ptr;
159 else // unknown bucket type
161 ASSERT(can_overwrite == 2); // because of the policy and decompression
162 ptr = decode_attributes(ptr, end, o, 2); // body
166 /* If (overwritten), bflush(body) might be needed. */