summaryrefslogtreecommitdiffstats
path: root/utils/capabilities.c
blob: 187b1660ef5d2d126b9270b1d882edf551e0d100 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
  Copyright (c) 2018 Red Hat, Inc. <http://www.redhat.com>
  This file is part of gluster-block.

  This file is licensed to you under your choice of the GNU Lesser
  General Public License, version 3 or any later version (LGPLv3 or
  later), or the GNU General Public License, version 2 (GPLv2), in all
  cases as published by the Free Software Foundation.
*/


# include "capabilities.h"


int
gbCapabilitiesEnumParse(const char *cap)
{
  int i;


  if (!cap) {
    return GB_CAP_MAX;
  }

  for (i = 0; i < GB_CAP_MAX; i++) {
    if (!strcmp(cap, gbCapabilitiesLookup[i])) {
      return i;
    }
  }

  return i;
}


int
gbSetCapabilties(blockResponse **c)
{
  FILE * fp;
  char *line = NULL;
  size_t len = 0;
  int count = 0;
  int ret = 0;
  blockResponse *reply = *c;
  gbCapObj *caps = NULL;
  char *p, *sep;


  fp = fopen(GB_CAPS_FILE, "r");
  if (fp == NULL) {
    return -1;
  }

  if (GB_ALLOC_N(caps, GB_CAP_MAX) < 0) {
    fclose(fp);
    return -1;
  }

  while ((getline(&line, &len, fp)) != -1) {
    if (!line) {
      continue;
    }
    if ((line[0] == '\n') || (line[0] == '#')) {
      GB_FREE(line);
      continue;
    }

    /* Part before ':' */
    p = line;
    sep = strchr(p, ':');
    *sep = '\0';

    ret = gbCapabilitiesEnumParse(p);
    if (ret != GB_CAP_MAX) {
      GB_STRCPYSTATIC(caps[count].cap, gbCapabilitiesLookup[ret]);

      /* Part after ':' and before '\n' */
      p = sep + 1;
      sep = strchr(p, '\n');
      *sep = '\0';

      while(isspace((unsigned char)*p)) p++;
      ret = convertStringToTrillianParse(p);
      if (ret >= 0) {
        caps[count].status = ret;
      } else {
        ret = -1;
        goto out;
      }
      count++;
    }

    GB_FREE(line);
  }

  if (GB_CAP_MAX != count) {
    ret = -1;
    goto out;
  }

  reply->xdata.xdata_len = GB_CAP_MAX * sizeof(gbCapObj);
  reply->xdata.xdata_val = (char *) caps;

  ret = 0;
 out:
  GB_FREE(line);
  fclose(fp);

  return ret;
}