/usr/src/openvswitch-1.4.0/ovsdb/column.c is in openvswitch-datapath-dkms 1.4.0-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | /* Copyright (c) 2009, 2010, 2011 Nicira Networks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <config.h>
#include "ovsdb/column.h"
#include <stdlib.h>
#include "column.h"
#include "dynamic-string.h"
#include "json.h"
#include "ovsdb-error.h"
#include "ovsdb-parser.h"
#include "table.h"
#include "util.h"
struct ovsdb_column *
ovsdb_column_create(const char *name,
bool mutable, bool persistent,
const struct ovsdb_type *type)
{
/* Doesn't set the new column's 'index': the caller must do that. */
struct ovsdb_column *column;
column = xzalloc(sizeof *column);
column->name = xstrdup(name);
column->mutable = mutable;
column->persistent = persistent;
ovsdb_type_clone(&column->type, type);
return column;
}
struct ovsdb_column *
ovsdb_column_clone(const struct ovsdb_column *old)
{
/* Doesn't copy the column's 'index': the caller must do that. */
return ovsdb_column_create(old->name,
old->mutable, old->persistent,
&old->type);
}
void
ovsdb_column_destroy(struct ovsdb_column *column)
{
ovsdb_type_destroy(&column->type);
free(column->name);
free(column);
}
struct ovsdb_error *
ovsdb_column_from_json(const struct json *json, const char *name,
struct ovsdb_column **columnp)
{
const struct json *mutable, *ephemeral, *type_json;
struct ovsdb_error *error;
struct ovsdb_type type;
struct ovsdb_parser parser;
bool persistent;
*columnp = NULL;
ovsdb_parser_init(&parser, json, "schema for column %s", name);
mutable = ovsdb_parser_member(&parser, "mutable",
OP_TRUE | OP_FALSE | OP_OPTIONAL);
ephemeral = ovsdb_parser_member(&parser, "ephemeral",
OP_TRUE | OP_FALSE | OP_OPTIONAL);
type_json = ovsdb_parser_member(&parser, "type", OP_STRING | OP_OBJECT);
error = ovsdb_parser_finish(&parser);
if (error) {
return error;
}
error = ovsdb_type_from_json(&type, type_json);
if (error) {
return error;
}
persistent = ephemeral ? !json_boolean(ephemeral) : true;
*columnp = ovsdb_column_create(name,
mutable ? json_boolean(mutable) : true,
persistent, &type);
ovsdb_type_destroy(&type);
return NULL;
}
struct json *
ovsdb_column_to_json(const struct ovsdb_column *column)
{
struct json *json = json_object_create();
if (!column->mutable) {
json_object_put(json, "mutable", json_boolean_create(false));
}
if (!column->persistent) {
json_object_put(json, "ephemeral", json_boolean_create(true));
}
json_object_put(json, "type", ovsdb_type_to_json(&column->type));
return json;
}
void
ovsdb_column_set_init(struct ovsdb_column_set *set)
{
set->columns = NULL;
set->n_columns = set->allocated_columns = 0;
}
void
ovsdb_column_set_destroy(struct ovsdb_column_set *set)
{
free(set->columns);
}
void
ovsdb_column_set_clone(struct ovsdb_column_set *new,
const struct ovsdb_column_set *old)
{
new->columns = xmemdup(old->columns,
old->n_columns * sizeof *old->columns);
new->n_columns = new->allocated_columns = old->n_columns;
}
struct ovsdb_error *
ovsdb_column_set_from_json(const struct json *json,
const struct ovsdb_table_schema *schema,
struct ovsdb_column_set *set)
{
ovsdb_column_set_init(set);
if (!json) {
struct shash_node *node;
SHASH_FOR_EACH (node, &schema->columns) {
const struct ovsdb_column *column = node->data;
ovsdb_column_set_add(set, column);
}
return NULL;
} else {
struct ovsdb_error *error = NULL;
size_t i;
if (json->type != JSON_ARRAY) {
goto error;
}
/* XXX this is O(n**2) */
for (i = 0; i < json->u.array.n; i++) {
const struct ovsdb_column *column;
const char *s;
if (json->u.array.elems[i]->type != JSON_STRING) {
goto error;
}
s = json->u.array.elems[i]->u.string;
column = shash_find_data(&schema->columns, s);
if (!column) {
error = ovsdb_syntax_error(json, NULL, "%s is not a valid "
"column name", s);
goto error;
} else if (ovsdb_column_set_contains(set, column->index)) {
goto error;
}
ovsdb_column_set_add(set, column);
}
return NULL;
error:
ovsdb_column_set_destroy(set);
ovsdb_column_set_init(set);
if (!error) {
error = ovsdb_syntax_error(json, NULL, "array of distinct column "
"names expected");
}
return error;
}
}
struct json *
ovsdb_column_set_to_json(const struct ovsdb_column_set *set)
{
struct json *json;
size_t i;
json = json_array_create_empty();
for (i = 0; i < set->n_columns; i++) {
json_array_add(json, json_string_create(set->columns[i]->name));
}
return json;
}
/* Returns an English string listing the contents of 'set', e.g. "columns
* \"a\", \"b\", and \"c\"". The caller must free the string. */
char *
ovsdb_column_set_to_string(const struct ovsdb_column_set *set)
{
if (!set->n_columns) {
return xstrdup("no columns");
} else {
struct ds s;
size_t i;
ds_init(&s);
ds_put_format(&s, "column%s ", set->n_columns > 1 ? "s" : "");
for (i = 0; i < set->n_columns; i++) {
const char *delimiter = english_list_delimiter(i, set->n_columns);
ds_put_format(&s, "%s\"%s\"", delimiter, set->columns[i]->name);
}
return ds_steal_cstr(&s);
}
}
void
ovsdb_column_set_add(struct ovsdb_column_set *set,
const struct ovsdb_column *column)
{
if (set->n_columns >= set->allocated_columns) {
set->columns = x2nrealloc(set->columns, &set->allocated_columns,
sizeof *set->columns);
}
set->columns[set->n_columns++] = column;
}
void
ovsdb_column_set_add_all(struct ovsdb_column_set *set,
const struct ovsdb_table *table)
{
struct shash_node *node;
SHASH_FOR_EACH (node, &table->schema->columns) {
const struct ovsdb_column *column = node->data;
ovsdb_column_set_add(set, column);
}
}
bool
ovsdb_column_set_contains(const struct ovsdb_column_set *set,
unsigned int column_index)
{
size_t i;
for (i = 0; i < set->n_columns; i++) {
if (set->columns[i]->index == column_index) {
return true;
}
}
return false;
}
/* This comparison is sensitive to ordering of columns within a set, but that's
* good: the only existing caller wants to make sure that hash values are
* comparable, which is only true if column ordering is the same. */
bool
ovsdb_column_set_equals(const struct ovsdb_column_set *a,
const struct ovsdb_column_set *b)
{
size_t i;
if (a->n_columns != b->n_columns) {
return false;
}
for (i = 0; i < a->n_columns; i++) {
if (a->columns[i] != b->columns[i]) {
return false;
}
}
return true;
}
|