libblkid-tiny: use separated buffer for each block device read
[project/fstools.git] / libblkid-tiny / libblkid-tiny.c
index f020e231ed83eb1b3611d57f2d0ff9a62b4453bc..05b4b99ef9dc2ac40dc891792b548e90e72cc724 100644 (file)
@@ -13,9 +13,6 @@
 
 int blkid_debug_mask = 0;
 
-static unsigned char *probe_buffer;
-static unsigned int probe_buffer_size = 0;
-
 int get_linux_version (void)
 {
        static int kver = -1;
@@ -81,32 +78,27 @@ int blkid_probe_sprintf_version(blkid_probe pr, const char *fmt, ...)
 unsigned char *blkid_probe_get_buffer(blkid_probe pr,
                                blkid_loff_t off, blkid_loff_t len)
 {
+       struct blkid_bufinfo *bf;
        int ret;
-       unsigned char *buf;
-
-       if (len > probe_buffer_size) {
-               buf = realloc(probe_buffer, len);
-
-               if (!buf) {
-                       fprintf(stderr, "failed to allocate %d byte buffer\n",
-                               (int)len);
-
-                       return NULL;
-               }
 
-               probe_buffer = buf;
-               probe_buffer_size = len;
-       }
-
-       memset(probe_buffer, 0, probe_buffer_size);
+       bf = malloc(sizeof(*bf) + len);
+       if (!bf)
+               return NULL;
+       memset(bf, 0, sizeof(*bf));
+       bf->data = ((unsigned char *)bf) + sizeof(*bf);
 
        lseek(pr->fd, off, SEEK_SET);
-       ret = read(pr->fd, probe_buffer, len);
+       ret = read(pr->fd, bf->data, len);
 
-       if (ret != len)
+       if (ret != len) {
                fprintf(stderr, "faile to read blkid\n");
+               free(bf);
+               return NULL;
+       }
 
-       return probe_buffer;
+       list_add_tail(&bf->bufs, &pr->buffers);
+
+       return bf->data;
 }
 
 int blkid_probe_set_label(blkid_probe pr, unsigned char *label, size_t len)
@@ -121,6 +113,21 @@ int blkid_probe_set_label(blkid_probe pr, unsigned char *label, size_t len)
        return 0;
 }
 
+int blkid_probe_set_utf8label(blkid_probe pr, unsigned char *label,
+                               size_t len, int enc)
+{
+       if (len > (sizeof(pr->label) - 1)) {
+               fprintf(stderr, "label buffer too small %d > %d\n",
+                       (int) len, (int) sizeof(pr->label) - 1);
+               return -1;
+       }
+
+       blkid_encode_to_utf8(enc,(unsigned char*) pr->label, len,
+                       label, len+1);
+
+       return 0;
+}
+
 int blkid_probe_set_uuid_as(blkid_probe pr, unsigned char *uuid, const char *name)
 {
        short unsigned int*u = (short unsigned int*) uuid;