]> mj.ucw.cz Git - libucw.git/commitdiff
obj_read_bucket() now gets the bucket length as a parameter, because
authorMartin Mares <mj@ucw.cz>
Sun, 27 Jun 2004 14:10:47 +0000 (14:10 +0000)
committerMartin Mares <mj@ucw.cz>
Sun, 27 Jun 2004 14:10:47 +0000 (14:10 +0000)
in many cases, the fastbuf continues after the end of the bucket.

If a V30 bucket ends with a NUL byte, eat that byte as well.

Also introduced obj_attr_to_bucket{,_num}() -- these functions take
a single attribute and encode it using the specified bucket type
for inclusion at the beginning of the bucket header. These functions
probably belong to obj2buck instead, but currently there is no such
file, so I'm leaving the moving to you.

lib/buck2obj.c
lib/buck2obj.h

index cbcb6f529e8539952177563df1f005b7a6941f98..92f1478b10ac4e926b4a5f16829ba6729a731b18 100644 (file)
@@ -126,26 +126,24 @@ decode_attributes(byte *ptr, byte *end, struct odes *o, uns can_overwrite)
 }
 
 struct odes *
-obj_read_bucket(struct buck2obj_buf *buf, uns buck_type, struct fastbuf *body, uns want_body)
+obj_read_bucket(struct buck2obj_buf *buf, uns buck_type, uns buck_len, struct fastbuf *body, uns want_body)
 {
   struct odes *o = obj_new(buf->mp);
 
   if (buck_type < BUCKET_TYPE_V33)
   {
-    if (want_body)                     // ignore empty lines, read until EOF
-      obj_read_multi(body, o);
+    if (want_body)                     // ignore empty lines, read until NUL or EOF
+      {
+       obj_read_multi(body, o);
+       bgetc(body);
+      }
     else                               // end on EOF or the first empty line
       obj_read(body, o);
   }
   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.  */
+    /* FIXME: This could be cached in buck2obj_buf */
     int can_overwrite = bconfig(body, BCONFIG_CAN_OVERWRITE, 0);
     if (can_overwrite < 0)
       can_overwrite = 0;
@@ -207,3 +205,37 @@ decompress:
   }
   return o;
 }
+
+byte *
+obj_attr_to_bucket(byte *buf, uns buck_type, uns attr, byte *val)
+{
+  uns l;
+
+  switch (buck_type)
+    {
+    case BUCKET_TYPE_PLAIN:
+    case BUCKET_TYPE_V30:
+      buf += sprintf(buf, "%c%s\n", attr, val);
+      break;
+    case BUCKET_TYPE_V33:
+    case BUCKET_TYPE_V33_LIZARD:
+      l = strlen(val) + 1;
+      PUT_UTF8(buf, l);
+      l--;
+      memcpy(buf, val, l);
+      buf += l;
+      *buf++ = attr;
+      break;
+    default:
+      die("obj_attr_to_bucket called for unknown type %08x", buck_type);
+    }
+  return buf;
+}
+
+byte *
+obj_attr_to_bucket_num(byte *buf, uns buck_type, uns attr, uns val)
+{
+  byte vbuf[16];
+  sprintf(vbuf, "%d", val);
+  return obj_attr_to_bucket(buf, buck_type, attr, vbuf);
+}
index f7fad1b80f16dbfa04187f19899e79ee8ed3fa2a..b4046340383205ca62349198b57338e6c52b02c4 100644 (file)
@@ -5,11 +5,16 @@
  */
 
 struct buck2obj_buf;
+struct mempool;
 
 struct buck2obj_buf *buck2obj_alloc(struct mempool *mp);
 void buck2obj_free(struct buck2obj_buf *buf);
-struct odes *obj_read_bucket(struct buck2obj_buf *buf, uns buck_type, struct fastbuf *body, uns want_body);
+struct odes *obj_read_bucket(struct buck2obj_buf *buf, uns buck_type, uns buck_len, struct fastbuf *body, uns want_body);
   /* If BCONFIG_CAN_OVERWRITE(body)==2, then the buffer of body has probably
    * been tampered with (unless the bucket is larger than the buffer).  In such
    * a case, you must call bflush(body) before you do anything else than
    * sequential read.  */
+
+/* FIXME: These functions should be put somewhere else */
+byte *obj_attr_to_bucket(byte *buf, uns buck_type, uns attr, byte *val);
+byte *obj_attr_to_bucket_num(byte *buf, uns buck_type, uns attr, uns val);