Пример #1
0
static int read_int(int fd, int64_t *out, size_t size)
{
    uint8_t tmp8 = 0;
    uint16_t tmp16;
    uint32_t tmp32;
    uint64_t tmp64;

    if (read(fd, &tmp64, size) == -1)
        return -errno;

    (void)tmp8;

    switch (size)
    {
    case 1:
        *out = *((int8_t *)(&tmp64));
        break;
    case 2:
        tmp16 = betoht16(tmp64);
        *out = *((int16_t *)(&tmp16));
        break;
    case 4:
        tmp32 = betoht32(tmp64);
        *out = *((int32_t *)(&tmp32));
        break;
    case 8:
        tmp64 = betoht64(tmp64);
        *out = *((int64_t *)(&tmp64));
        break;
    }
    return 0;
}
Пример #2
0
int libretrodb_open(const char *path, libretrodb_t *db)
{
   libretrodb_header_t header;
   libretrodb_metadata_t md;
   int rv;
   int fd = open(path, O_RDWR);

   if (fd == -1)
      return -errno;

   strcpy(db->path, path);
   db->root = lseek(fd, 0, SEEK_CUR);

   if ((rv = read(fd, &header, sizeof(header))) == -1)
   {
      rv = -errno;
      goto error;
   }

   if (strncmp(header.magic_number, MAGIC_NUMBER, sizeof(MAGIC_NUMBER)) != 0)
   {
      rv = -EINVAL;
      goto error;
   }

   header.metadata_offset = betoht64(header.metadata_offset);
   lseek(fd, header.metadata_offset, SEEK_SET);

   if (libretrodb_read_metadata(fd, &md) < 0)
   {
      rv = -EINVAL;
      goto error;
   }

   db->count = md.count;
   db->first_index_offset = lseek(fd, 0, SEEK_CUR);
   db->fd = fd;
   return 0;
error:
   close(fd);
   return rv;
}
Пример #3
0
static int read_uint(int fd, uint64_t *out, size_t size)
{
    uint64_t tmp;

    if (read(fd, &tmp, size) == -1)
        return -errno;

    switch (size)
    {
    case 1:
        *out = *(uint8_t *)(&tmp);
        break;
    case 2:
        *out = betoht16(tmp);
        break;
    case 4:
        *out = betoht32(tmp);
        break;
    case 8:
        *out = betoht64(tmp);
        break;
    }
    return 0;
}