]> mj.ucw.cz Git - libucw.git/blobdiff - lib/buck2obj.c
deleted obj_attr_to_bucket*(). sorry, I like them, however I have already
[libucw.git] / lib / buck2obj.c
index 4270b7a36a7dd8f3028b82cb4115e40001175e7d..c0ee98c66af9486f34338353dfc4e0fec629b9a9 100644 (file)
 #include <errno.h>
 #include <unistd.h>
 
-#define        MAX_HEADER_SIZE 1024            // extra space for the header not counted in MaxObjSize
 #define        RET_ERR(num)    ({ errno = num; return NULL; })
 
+#define        GBUF_TYPE       byte
+#define        GBUF_PREFIX(x)  bb_##x
+#include "lib/gbuf.h"
+
 struct buck2obj_buf
 {
-  byte *raw;
-  uns raw_len;
+  bb_t bb;
   struct lizard_buffer *lizard;
   struct mempool *mp;
 };
 
 struct buck2obj_buf *
-buck2obj_alloc(uns max_len, struct mempool *mp)
+buck2obj_alloc(struct mempool *mp)
 {
   struct buck2obj_buf *buf = xmalloc(sizeof(struct buck2obj_buf));
-  buf->raw_len = max_len * LIZARD_MAX_MULTIPLY + LIZARD_MAX_ADD + MAX_HEADER_SIZE;
-  buf->raw = xmalloc(buf->raw_len);
-  buf->lizard = lizard_alloc(max_len);
+  bb_init(&buf->bb);
+  buf->lizard = lizard_alloc();
   buf->mp = mp;
   return buf;
 }
@@ -44,61 +45,93 @@ void
 buck2obj_free(struct buck2obj_buf *buf)
 {
   lizard_free(buf->lizard);
-  xfree(buf->raw);
+  bb_done(&buf->bb);
   xfree(buf);
 }
 
+void
+buck2obj_flush(struct buck2obj_buf *buf)
+{
+  mp_flush(buf->mp);
+}
+
 static inline byte *
 decode_attributes(byte *ptr, byte *end, struct odes *o, uns can_overwrite)
 {
-  while (ptr < end)
-  {
-    uns len;
-    GET_UTF8(ptr, len);
-    if (!len--)
-      break;
-    byte type = ptr[len];
-    if (can_overwrite == 2)
+  if (can_overwrite >= 2)
+    while (ptr < end)
     {
+      uns len;
+      GET_UTF8(ptr, len);
+      if (!len--)
+       break;
+      byte type = ptr[len];
+
       ptr[len] = 0;
       obj_add_attr_ref(o, type, ptr);
+
+      ptr += len + 1;
     }
-    else if (can_overwrite == 1)
+  else if (can_overwrite == 1)
+    while (ptr < end)
     {
+      uns len;
+      GET_UTF8(ptr, len);
+      if (!len--)
+       break;
+      byte type = ptr[len];
+
       ptr[len] = 0;
       obj_add_attr(o, type, ptr);
       ptr[len] = type;
+
+      ptr += len + 1;
     }
-    else
+  else
+    while (ptr < end)
     {
-      byte *dup = mp_alloc(o->pool, len+1);
+      uns len;
+      GET_UTF8(ptr, len);
+      if (!len--)
+       break;
+      byte type = ptr[len];
+
+      byte *dup = mp_alloc_fast_noalign(o->pool, len+1);
       memcpy(dup, ptr, len);
       dup[len] = 0;
-      obj_add_attr_ref(o, type, ptr);
+      obj_add_attr_ref(o, type, dup);
+
+      ptr += len + 1;
     }
-    ptr += len + 1;
-  }
   return ptr;
 }
 
 struct odes *
-buck2obj_convert(struct buck2obj_buf *buf, uns buck_type, struct fastbuf *body)
+obj_read_bucket(struct buck2obj_buf *buf, uns buck_type, uns buck_len, struct fastbuf *body, uns *body_start)
 {
-  mp_flush(buf->mp);
   struct odes *o = obj_new(buf->mp);
 
   if (buck_type < BUCKET_TYPE_V33)
-    obj_read_multi(body, o);
+  {
+    if (!body_start)                   // header + body: ignore empty lines, read until EOF
+    {
+      obj_read_multi(body, o);
+      bgetc(body);
+    }
+    else                               // header only: end on EOF or the first empty line
+    {
+      sh_off_t start = btell(body);
+      obj_read(body, o);
+      *body_start = btell(body) - start;
+    }
+  }
   else
   {
-    /* Compute the length of the bucket.  We cannot fetch this attribute
-     * directly due to remote indexing.  */
-    bseek(body, 0, SEEK_END);
-    sh_off_t buck_len = btell(body);
-    bsetpos(body, 0);
-
     /* Read all the bucket into 1 buffer, 0-copy if possible.  */
-    int can_overwrite = MAX(bconfig(body, BCONFIG_CAN_OVERWRITE, 0), 0);
+    int can_overwrite = bconfig(body, BCONFIG_CAN_OVERWRITE, -1);
+    /* FIXME: This could be cached in buck2obj_buf */
+    if (can_overwrite < 0)
+      can_overwrite = 0;
     uns overwritten;
     byte *ptr, *end;
     uns len = bdirect_read_prepare(body, &ptr);
@@ -107,10 +140,9 @@ buck2obj_convert(struct buck2obj_buf *buf, uns buck_type, struct fastbuf *body)
     {
       /* Copy if the original buffer is too small.
        * If it is write-protected, copy it also if it is uncompressed.  */
-      if (buck_len > buf->raw_len)
-       RET_ERR(EFBIG);
-      len = bread(body, buf->raw, buck_len);
-      ptr = buf->raw;
+      bb_grow(&buf->bb, buck_len);
+      len = bread(body, buf->bb.ptr, buck_len);
+      ptr = buf->bb.ptr;
       can_overwrite = 2;
       overwritten = 0;
     }
@@ -118,21 +150,29 @@ buck2obj_convert(struct buck2obj_buf *buf, uns buck_type, struct fastbuf *body)
       overwritten = can_overwrite > 1;
     end = ptr + len;
 
+    byte *start = ptr;
     ptr = decode_attributes(ptr, end, o, can_overwrite);// header
+    if (body_start)
+    {
+      *body_start = ptr - start;
+      return o;
+    }
     if (buck_type == BUCKET_TYPE_V33)
       ;
     else if (buck_type == BUCKET_TYPE_V33_LIZARD)      // decompression
     {
       len = GET_U32(ptr);
       ptr += 4;
-      int res = lizard_decompress_safe(ptr, buf->lizard, len);
+      int res;
+      byte *new_ptr;
+      res = lizard_decompress_safe(ptr, buf->lizard, len, &new_ptr);
       if (res != (int) len)
       {
        if (res >= 0)
          errno = EINVAL;
        return NULL;
       }
-      ptr = buf->lizard->ptr;
+      ptr = new_ptr;
       end = ptr + len;
       can_overwrite = 2;
     }
@@ -143,8 +183,7 @@ buck2obj_convert(struct buck2obj_buf *buf, uns buck_type, struct fastbuf *body)
 
     if (ptr != end)
       RET_ERR(EINVAL);
-    if (overwritten)
-      bflush(body);
+    /* If (overwritten), bflush(body) might be needed.  */
   }
   return o;
 }