summaryrefslogtreecommitdiff
path: root/src/cache/conf.c
blob: 68edef5501a72df993032ce3b2c1241a917248c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* ISC license. */

#include <stddef.h>
#include <errno.h>
#include <string.h>

#include <skalibs/uint16.h>
#include <skalibs/uint32.h>
#include <skalibs/uint64.h>
#include <skalibs/cdb.h>

#include "shibari-cache-internal.h"

#include <skalibs/posixishard.h>

int conf_getb (char const *key, size_t keylen, cdb_data *data)
{
  if (keylen > 4096) return (errno = EINVAL, 0) ;
  switch (cdb_find(&g->confdb, data, key, keylen))
  {
    case -1 : return (errno = EILSEQ, 0) ;
    case 0 : return (errno = ENOENT, 0) ;
    default : return 1 ;
  }
}

int conf_get (char const *key, cdb_data *data)
{
  return conf_getb(key, strlen(key), data) ;
}

int conf_get_uint16 (char const *key, uint16_t *value)
{
  cdb_data data ;
  if (!conf_get(key, &data)) return 0 ;
  if (data.len != 2) return (errno = EPROTO, 0) ;
  uint16_unpack_big(data.s, value) ;
  return 1 ;
}

int conf_get_uint32 (char const *key, uint32_t *value)
{
  cdb_data data ;
  if (!conf_get(key, &data)) return 0 ;
  if (data.len != 4) return (errno = EPROTO, 0) ;
  uint32_unpack_big(data.s, value) ;
  return 1 ;
}

int conf_get_uint64 (char const *key, uint64_t *value)
{
  cdb_data data ;
  if (!conf_get(key, &data)) return 0 ;
  if (data.len != 8) return (errno = EPROTO, 0) ;
  uint64_unpack_big(data.s, value) ;
  return 1 ;
}

char const *conf_get_string (char const *key)
{
  cdb_data data ;
  if (!conf_get(key, &data)) return 0 ;
  if (!data.len || data.s[data.len - 1]) return (errno = EPROTO, NULL) ;
  return data.s ;
}