summaryrefslogtreecommitdiffstats
path: root/utils/common.c
blob: 751e2b80afb463d4d876d1ba6116fa882e6d45f0 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
  Copyright (c) 2016 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 "common.h"


enum JsonResponseFormat
jsonResponseFormatParse(const char *opt)
{
  int i;


  if (!opt) {
    return GB_JSON_MAX;
  }

  if (strlen (opt) < 2 || opt[0] != '-' || opt[1] != '-') {
    /*json option is not given*/
    return GB_JSON_NONE;
  }

  for (i = 0; i < GB_JSON_MAX; i++) {
    if (!strcmp(opt, JsonResponseFormatLookup[i])) {
      return i;
    }
  }

  return i;
}


ssize_t
glusterBlockParseSize(const char *dom, char *value)
{
  char *postfix;
  char *tmp;
  ssize_t sizef;


  if (!value)
    return -1;

  sizef = strtod(value, &postfix);
  if (sizef < 0) {
    LOG(dom, GB_LOG_ERROR, "%s", "size cannot be negative number");
    return -1;
  }

  tmp = postfix;
  if (*postfix == ' ') {
    tmp = tmp + 1;
  }

  switch (tolower(*tmp)) {
  case 'y':
    sizef *= 1024;
    /* fall through */
  case 'z':
    sizef *= 1024;
    /* fall through */
  case 'e':
    sizef *= 1024;
    /* fall through */
  case 'p':
    sizef *= 1024;
    /* fall through */
  case 't':
    sizef *= 1024;
    /* fall through */
  case 'g':
    sizef *= 1024;
    /* fall through */
  case 'm':
    sizef *= 1024;
    /* fall through */
  case 'k':
    sizef *= 1024;
    /* fall through */
  case 'b':
  case '\0':
    break;
  default:
    goto fail;
  }

  if ((strlen(tmp) > 1) &&
      ((tmp[0] == 'b') || (tmp[0] == 'B') ||
       (strncasecmp(tmp+1, "ib", strlen(tmp+1)) != 0)))
  {
    goto fail;
  }

  /* success */
  return sizef;

fail:
  LOG(dom, GB_LOG_ERROR, "%s",
      "Unknown size unit. "
      "You may use b/B, k/K(iB), m/M(iB), g/G(iB), and t/T(iB) suffixes for "
      "bytes, kibibytes, mebibytes, gibibytes, and tebibytes.");
  return -1;
}


char *
glusterBlockFormatSize(const char *dom, size_t bytes)
{
  char *buf;
  size_t i = 0;
  size_t rem = 0;
  const char* units[] = {"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"};


  while (bytes >= 1024) {
    rem = (bytes % 1024);
    bytes /= 1024;
    i++;
  }

  if (GB_ASPRINTF(&buf, "%.1f %s", (float)bytes + (float)rem / 1024.0, units[i]) < 0) {
    LOG(dom, GB_LOG_ERROR, "%s", "glusterBlockFormatSize() failed");
    buf = NULL;
  }

  return buf;
}


/* Return value and meaning
 *  1  - true/set
 *  0  - false/unset
 * -1  - unknown string
 */
int
convertStringToTrillianParse(const char *opt)
{
  int i;


  if (!opt) {
    return -1;
  }

  for (i = 1; i < GB_BOOL_MAX; i++) {
    if (!strcmp(opt, ConvertStringToTrillianLookup[i])) {
      return i%2;
    }
  }

  return -1;
}


void
blockServerDefFree(blockServerDefPtr blkServers)
{
  size_t i;


  if (!blkServers) {
    return;
  }

  for (i = 0; i < blkServers->nhosts; i++) {
     GB_FREE(blkServers->hosts[i]);
  }
  GB_FREE(blkServers->hosts);
  GB_FREE(blkServers);
}


bool
blockhostIsValid(char *status)
{
  switch (blockMetaStatusEnumParse(status)) {
  case GB_CONFIG_SUCCESS:
  case GB_CLEANUP_INPROGRESS:
  case GB_AUTH_ENFORCEING:
  case GB_AUTH_ENFORCED:
  case GB_AUTH_ENFORCE_FAIL:
  case GB_AUTH_CLEAR_ENFORCED:
  case GB_AUTH_CLEAR_ENFORCEING:
  case GB_AUTH_CLEAR_ENFORCE_FAIL:
  case GB_RP_SUCCESS:
  case GB_RP_FAIL:
  case GB_RP_INPROGRESS:
    return TRUE;
  }

  return FALSE;
}