| 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 |
#include "asterisk.h" |
| 32 |
|
| 33 |
#ifdef HAVE_URIPARSER |
| 34 |
#include <uriparser/Uri.h> |
| 35 |
#endif |
| 36 |
|
| 37 |
#include "asterisk/logger.h" |
| 38 |
#include "asterisk/sorcery.h" |
| 39 |
#include "asterisk/bucket.h" |
| 40 |
#include "asterisk/config_options.h" |
| 41 |
#include "asterisk/astobj2.h" |
| 42 |
#include "asterisk/strings.h" |
| 43 |
#include "asterisk/json.h" |
| 44 |
#include "asterisk/file.h" |
| 45 |
#include "asterisk/module.h" |
| 46 |
#include "asterisk/paths.h" |
| 47 |
|
| 48 |
|
| 49 |
#define SCHEME_BUCKETS 53 |
| 50 |
|
| 51 |
|
| 52 |
#define METADATA_BUCKETS 53 |
| 53 |
|
| 54 |
|
| 55 |
static struct ast_sorcery *bucket_sorcery; |
| 56 |
|
| 57 |
|
| 58 |
static struct ao2_container *schemes; |
| 59 |
|
| 60 |
|
| 61 |
struct ast_bucket_scheme { |
| 62 |
|
| 63 |
struct ast_sorcery_wizard *bucket; |
| 64 |
|
| 65 |
struct ast_sorcery_wizard *file; |
| 66 |
|
| 67 |
bucket_file_create_cb create; |
| 68 |
|
| 69 |
bucket_file_destroy_cb destroy; |
| 70 |
|
| 71 |
char name[0]; |
| 72 |
}; |
| 73 |
|
| 74 |
|
| 75 |
static int bucket_wizard_create(const struct ast_sorcery *sorcery, void *data, void *object) |
| 76 |
{ |
| 77 |
struct ast_bucket *bucket = object; |
| 78 |
|
| 79 |
return bucket->scheme_impl->bucket->create(sorcery, data, object); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
static void *bucket_wizard_retrieve(const struct ast_sorcery *sorcery, void *data, const char *type, |
| 84 |
const char *id) |
| 85 |
{ |
| 86 |
#ifdef HAVE_URIPARSER |
| 87 |
UriParserStateA state; |
| 88 |
UriUriA uri; |
| 89 |
size_t len; |
| 90 |
#else |
| 91 |
char *tmp = ast_strdupa(id); |
| 92 |
#endif |
| 93 |
SCOPED_AO2RDLOCK(lock, schemes); |
| 94 |
char *uri_scheme; |
| 95 |
RAII_VAR(struct ast_bucket_scheme *, scheme, NULL, ao2_cleanup); |
| 96 |
|
| 97 |
#ifdef HAVE_URIPARSER |
| 98 |
state.uri = &uri; |
| 99 |
if (uriParseUriA(&state, id) != URI_SUCCESS || |
| 100 |
!uri.scheme.first || !uri.scheme.afterLast) { |
| 101 |
uriFreeUriMembersA(&uri); |
| 102 |
return NULL; |
| 103 |
} |
| 104 |
|
| 105 |
len = (uri.scheme.afterLast - uri.scheme.first) + 1; |
| 106 |
uri_scheme = ast_alloca(len); |
| 107 |
ast_copy_string(uri_scheme, uri.scheme.first, len); |
| 108 |
|
| 109 |
uriFreeUriMembersA(&uri); |
| 110 |
#else |
| 111 |
uri_scheme = tmp; |
| 112 |
if (!(tmp = strchr(uri_scheme, ':'))) { |
| 113 |
return NULL; |
| 114 |
} |
| 115 |
*tmp = '\0'; |
| 116 |
#endif |
| 117 |
|
| 118 |
scheme = ao2_find(schemes, uri_scheme, OBJ_KEY | OBJ_NOLOCK); |
| 119 |
|
| 120 |
if (!scheme) { |
| 121 |
return NULL; |
| 122 |
} |
| 123 |
|
| 124 |
return scheme->bucket->retrieve_id(sorcery, data, type, id); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
static int bucket_wizard_delete(const struct ast_sorcery *sorcery, void *data, void *object) |
| 129 |
{ |
| 130 |
struct ast_bucket *bucket = object; |
| 131 |
|
| 132 |
return bucket->scheme_impl->bucket->delete(sorcery, data, object); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
static int bucket_wizard_is_stale(const struct ast_sorcery *sorcery, void *data, void *object) |
| 137 |
{ |
| 138 |
struct ast_bucket *bucket = object; |
| 139 |
|
| 140 |
if (!bucket->scheme_impl->bucket->is_stale) { |
| 141 |
return 0; |
| 142 |
} |
| 143 |
|
| 144 |
return bucket->scheme_impl->bucket->is_stale(sorcery, data, object); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
static struct ast_sorcery_wizard bucket_wizard = { |
| 149 |
.name = "bucket", |
| 150 |
.create = bucket_wizard_create, |
| 151 |
.retrieve_id = bucket_wizard_retrieve, |
| 152 |
.delete = bucket_wizard_delete, |
| 153 |
.is_stale = bucket_wizard_is_stale, |
| 154 |
}; |
| 155 |
|
| 156 |
|
| 157 |
static int bucket_file_wizard_create(const struct ast_sorcery *sorcery, void *data, void *object) |
| 158 |
{ |
| 159 |
struct ast_bucket_file *file = object; |
| 160 |
|
| 161 |
return file->scheme_impl->file->create(sorcery, data, object); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
static void *bucket_file_wizard_retrieve(const struct ast_sorcery *sorcery, void *data, const char *type, |
| 166 |
const char *id) |
| 167 |
{ |
| 168 |
#ifdef HAVE_URIPARSER |
| 169 |
UriParserStateA state; |
| 170 |
UriUriA uri; |
| 171 |
size_t len; |
| 172 |
#else |
| 173 |
char *tmp = ast_strdupa(id); |
| 174 |
#endif |
| 175 |
char *uri_scheme; |
| 176 |
SCOPED_AO2RDLOCK(lock, schemes); |
| 177 |
RAII_VAR(struct ast_bucket_scheme *, scheme, NULL, ao2_cleanup); |
| 178 |
|
| 179 |
#ifdef HAVE_URIPARSER |
| 180 |
state.uri = &uri; |
| 181 |
if (uriParseUriA(&state, id) != URI_SUCCESS || |
| 182 |
!uri.scheme.first || !uri.scheme.afterLast) { |
| 183 |
uriFreeUriMembersA(&uri); |
| 184 |
return NULL; |
| 185 |
} |
| 186 |
|
| 187 |
len = (uri.scheme.afterLast - uri.scheme.first) + 1; |
| 188 |
uri_scheme = ast_alloca(len); |
| 189 |
ast_copy_string(uri_scheme, uri.scheme.first, len); |
| 190 |
|
| 191 |
uriFreeUriMembersA(&uri); |
| 192 |
#else |
| 193 |
uri_scheme = tmp; |
| 194 |
if (!(tmp = strchr(uri_scheme, ':'))) { |
| 195 |
return NULL; |
| 196 |
} |
| 197 |
*tmp = '\0'; |
| 198 |
#endif |
| 199 |
|
| 200 |
scheme = ao2_find(schemes, uri_scheme, OBJ_KEY | OBJ_NOLOCK); |
| 201 |
|
| 202 |
if (!scheme) { |
| 203 |
return NULL; |
| 204 |
} |
| 205 |
|
| 206 |
return scheme->file->retrieve_id(sorcery, data, type, id); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
static int bucket_file_wizard_update(const struct ast_sorcery *sorcery, void *data, void *object) |
| 211 |
{ |
| 212 |
struct ast_bucket_file *file = object; |
| 213 |
|
| 214 |
return file->scheme_impl->file->update(sorcery, data, object); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
static int bucket_file_wizard_delete(const struct ast_sorcery *sorcery, void *data, void *object) |
| 219 |
{ |
| 220 |
struct ast_bucket_file *file = object; |
| 221 |
|
| 222 |
return file->scheme_impl->file->delete(sorcery, data, object); |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
static int bucket_file_wizard_is_stale(const struct ast_sorcery *sorcery, void *data, void *object) |
| 227 |
{ |
| 228 |
struct ast_bucket_file *file = object; |
| 229 |
|
| 230 |
if (!file->scheme_impl->file->is_stale) { |
| 231 |
return 0; |
| 232 |
} |
| 233 |
|
| 234 |
return file->scheme_impl->file->is_stale(sorcery, data, object); |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
static struct ast_sorcery_wizard bucket_file_wizard = { |
| 239 |
.name = "bucket_file", |
| 240 |
.create = bucket_file_wizard_create, |
| 241 |
.retrieve_id = bucket_file_wizard_retrieve, |
| 242 |
.update = bucket_file_wizard_update, |
| 243 |
.delete = bucket_file_wizard_delete, |
| 244 |
.is_stale = bucket_file_wizard_is_stale, |
| 245 |
}; |
| 246 |
|
| 247 |
int __ast_bucket_scheme_register(const char *name, struct ast_sorcery_wizard *bucket, |
| 248 |
struct ast_sorcery_wizard *file, bucket_file_create_cb create_cb, |
| 249 |
bucket_file_destroy_cb destroy_cb, struct ast_module *module) |
| 250 |
{ |
| 251 |
SCOPED_AO2WRLOCK(lock, schemes); |
| 252 |
RAII_VAR(struct ast_bucket_scheme *, scheme, NULL, ao2_cleanup); |
| 253 |
|
| 254 |
if (ast_strlen_zero(name) || !bucket || !file || |
| 255 |
!bucket->create || !bucket->delete || !bucket->retrieve_id || |
| 256 |
(!bucket->create && !create_cb)) { |
| 257 |
return -1; |
| 258 |
} |
| 259 |
|
| 260 |
scheme = ao2_find(schemes, name, OBJ_KEY | OBJ_NOLOCK); |
| 261 |
if (scheme) { |
| 262 |
return -1; |
| 263 |
} |
| 264 |
|
| 265 |
scheme = ao2_alloc(sizeof(*scheme) + strlen(name) + 1, NULL); |
| 266 |
if (!scheme) { |
| 267 |
return -1; |
| 268 |
} |
| 269 |
|
| 270 |
strcpy(scheme->name, name); |
| 271 |
scheme->bucket = bucket; |
| 272 |
scheme->file = file; |
| 273 |
scheme->create = create_cb; |
| 274 |
scheme->destroy = destroy_cb; |
| 275 |
|
| 276 |
ao2_link_flags(schemes, scheme, OBJ_NOLOCK); |
| 277 |
|
| 278 |
ast_verb(5, "Registered bucket scheme '%s'\n", name); |
| 279 |
|
| 280 |
ast_module_shutdown_ref(module); |
| 281 |
|
| 282 |
return 0; |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
static struct ast_bucket_metadata *bucket_metadata_alloc(const char *name, const char *value) |
| 287 |
{ |
| 288 |
int name_len = strlen(name) + 1, value_len = strlen(value) + 1; |
| 289 |
struct ast_bucket_metadata *metadata = ao2_alloc(sizeof(*metadata) + name_len + value_len, NULL); |
| 290 |
char *dst; |
| 291 |
|
| 292 |
if (!metadata) { |
| 293 |
return NULL; |
| 294 |
} |
| 295 |
|
| 296 |
dst = metadata->data; |
| 297 |
metadata->name = strcpy(dst, name); |
| 298 |
dst += name_len; |
| 299 |
metadata->value = strcpy(dst, value); |
| 300 |
|
| 301 |
return metadata; |
| 302 |
} |
| 303 |
|
| 304 |
int ast_bucket_file_metadata_set(struct ast_bucket_file *file, const char *name, const char *value) |
| 305 |
{ |
| 306 |
RAII_VAR(struct ast_bucket_metadata *, metadata, bucket_metadata_alloc(name, value), ao2_cleanup); |
| 307 |
|
| 308 |
if (!metadata) { |
| 309 |
return -1; |
| 310 |
} |
| 311 |
|
| 312 |
ao2_find(file->metadata, name, OBJ_NODATA | OBJ_UNLINK | OBJ_KEY); |
| 313 |
ao2_link(file->metadata, metadata); |
| 314 |
|
| 315 |
return 0; |
| 316 |
} |
| 317 |
|
| 318 |
int ast_bucket_file_metadata_unset(struct ast_bucket_file *file, const char *name) |
| 319 |
{ |
| 320 |
RAII_VAR(struct ast_bucket_metadata *, metadata, ao2_find(file->metadata, name, OBJ_UNLINK | OBJ_KEY), ao2_cleanup); |
| 321 |
|
| 322 |
if (!metadata) { |
| 323 |
return -1; |
| 324 |
} |
| 325 |
|
| 326 |
return 0; |
| 327 |
} |
| 328 |
|
| 329 |
struct ast_bucket_metadata *ast_bucket_file_metadata_get(struct ast_bucket_file *file, const char *name) |
| 330 |
{ |
| 331 |
return ao2_find(file->metadata, name, OBJ_KEY); |
| 332 |
} |
| 333 |
|
| 334 |
void ast_bucket_file_metadata_callback(struct ast_bucket_file *file, ao2_callback_fn cb, void *arg) |
| 335 |
{ |
| 336 |
ao2_callback(file->metadata, 0, cb, arg); |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
static void bucket_destroy(void *obj) |
| 342 |
{ |
| 343 |
struct ast_bucket *bucket = obj; |
| 344 |
|
| 345 |
ao2_cleanup(bucket->scheme_impl); |
| 346 |
ast_string_field_free_memory(bucket); |
| 347 |
ao2_cleanup(bucket->buckets); |
| 348 |
ao2_cleanup(bucket->files); |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
static int bucket_rbtree_str_sort_cmp(const void *obj_left, const void *obj_right, int flags) |
| 353 |
{ |
| 354 |
const char *str_left = obj_left; |
| 355 |
const char *str_right = obj_right; |
| 356 |
int cmp = 0; |
| 357 |
|
| 358 |
switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) { |
| 359 |
default: |
| 360 |
case OBJ_POINTER: |
| 361 |
case OBJ_KEY: |
| 362 |
cmp = strcmp(str_left, str_right); |
| 363 |
break; |
| 364 |
case OBJ_PARTIAL_KEY: |
| 365 |
cmp = strncmp(str_left, str_right, strlen(str_right)); |
| 366 |
break; |
| 367 |
} |
| 368 |
return cmp; |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
static void *bucket_alloc(const char *name) |
| 373 |
{ |
| 374 |
RAII_VAR(struct ast_bucket *, bucket, NULL, ao2_cleanup); |
| 375 |
|
| 376 |
bucket = ast_sorcery_generic_alloc(sizeof(*bucket), bucket_destroy); |
| 377 |
if (!bucket) { |
| 378 |
return NULL; |
| 379 |
} |
| 380 |
|
| 381 |
if (ast_string_field_init(bucket, 128)) { |
| 382 |
return NULL; |
| 383 |
} |
| 384 |
|
| 385 |
bucket->buckets = ao2_container_alloc_rbtree(AO2_ALLOC_OPT_LOCK_NOLOCK, |
| 386 |
AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT, bucket_rbtree_str_sort_cmp, NULL); |
| 387 |
if (!bucket->buckets) { |
| 388 |
return NULL; |
| 389 |
} |
| 390 |
|
| 391 |
bucket->files = ao2_container_alloc_rbtree(AO2_ALLOC_OPT_LOCK_NOLOCK, |
| 392 |
AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT, bucket_rbtree_str_sort_cmp, NULL); |
| 393 |
if (!bucket->files) { |
| 394 |
return NULL; |
| 395 |
} |
| 396 |
|
| 397 |
ao2_ref(bucket, +1); |
| 398 |
return bucket; |
| 399 |
} |
| 400 |
|
| 401 |
struct ast_bucket *ast_bucket_alloc(const char *uri) |
| 402 |
{ |
| 403 |
#ifdef HAVE_URIPARSER |
| 404 |
UriParserStateA state; |
| 405 |
UriUriA full_uri; |
| 406 |
size_t len; |
| 407 |
#else |
| 408 |
char *tmp = ast_strdupa(uri); |
| 409 |
#endif |
| 410 |
char *uri_scheme; |
| 411 |
RAII_VAR(struct ast_bucket_scheme *, scheme, NULL, ao2_cleanup); |
| 412 |
struct ast_bucket *bucket; |
| 413 |
|
| 414 |
if (ast_strlen_zero(uri)) { |
| 415 |
return NULL; |
| 416 |
} |
| 417 |
|
| 418 |
#ifdef HAVE_URIPARSER |
| 419 |
state.uri = &full_uri; |
| 420 |
if (uriParseUriA(&state, uri) != URI_SUCCESS || |
| 421 |
!full_uri.scheme.first || !full_uri.scheme.afterLast || |
| 422 |
!full_uri.pathTail) { |
| 423 |
uriFreeUriMembersA(&full_uri); |
| 424 |
return NULL; |
| 425 |
} |
| 426 |
|
| 427 |
len = (full_uri.scheme.afterLast - full_uri.scheme.first) + 1; |
| 428 |
uri_scheme = ast_alloca(len); |
| 429 |
ast_copy_string(uri_scheme, full_uri.scheme.first, len); |
| 430 |
|
| 431 |
uriFreeUriMembersA(&full_uri); |
| 432 |
#else |
| 433 |
uri_scheme = tmp; |
| 434 |
if (!(tmp = strchr(uri_scheme, ':'))) { |
| 435 |
return NULL; |
| 436 |
} |
| 437 |
*tmp = '\0'; |
| 438 |
#endif |
| 439 |
|
| 440 |
scheme = ao2_find(schemes, uri_scheme, OBJ_KEY); |
| 441 |
if (!scheme) { |
| 442 |
return NULL; |
| 443 |
} |
| 444 |
|
| 445 |
bucket = ast_sorcery_alloc(bucket_sorcery, "bucket", uri); |
| 446 |
if (!bucket) { |
| 447 |
return NULL; |
| 448 |
} |
| 449 |
|
| 450 |
ao2_ref(scheme, +1); |
| 451 |
bucket->scheme_impl = scheme; |
| 452 |
|
| 453 |
ast_string_field_set(bucket, scheme, uri_scheme); |
| 454 |
|
| 455 |
return bucket; |
| 456 |
} |
| 457 |
|
| 458 |
int ast_bucket_create(struct ast_bucket *bucket) |
| 459 |
{ |
| 460 |
return ast_sorcery_create(bucket_sorcery, bucket); |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
static int bucket_copy_handler(const void *src, void *dst) |
| 468 |
{ |
| 469 |
const struct ast_bucket *src_bucket = src; |
| 470 |
struct ast_bucket *dst_bucket = dst; |
| 471 |
|
| 472 |
dst_bucket->scheme_impl = ao2_bump(src_bucket->scheme_impl); |
| 473 |
ast_string_field_set(dst_bucket, scheme, src_bucket->scheme); |
| 474 |
dst_bucket->created = src_bucket->created; |
| 475 |
dst_bucket->modified = src_bucket->modified; |
| 476 |
|
| 477 |
return 0; |
| 478 |
} |
| 479 |
|
| 480 |
struct ast_bucket *ast_bucket_clone(struct ast_bucket *bucket) |
| 481 |
{ |
| 482 |
return ast_sorcery_copy(bucket_sorcery, bucket); |
| 483 |
} |
| 484 |
|
| 485 |
struct ast_bucket *ast_bucket_retrieve(const char *uri) |
| 486 |
{ |
| 487 |
if (ast_strlen_zero(uri)) { |
| 488 |
return NULL; |
| 489 |
} |
| 490 |
|
| 491 |
return ast_sorcery_retrieve_by_id(bucket_sorcery, "bucket", uri); |
| 492 |
} |
| 493 |
|
| 494 |
int ast_bucket_is_stale(struct ast_bucket *bucket) |
| 495 |
{ |
| 496 |
return ast_sorcery_is_stale(bucket_sorcery, bucket); |
| 497 |
} |
| 498 |
|
| 499 |
int ast_bucket_observer_add(const struct ast_sorcery_observer *callbacks) |
| 500 |
{ |
| 501 |
return ast_sorcery_observer_add(bucket_sorcery, "bucket", callbacks); |
| 502 |
} |
| 503 |
|
| 504 |
void ast_bucket_observer_remove(const struct ast_sorcery_observer *callbacks) |
| 505 |
{ |
| 506 |
ast_sorcery_observer_remove(bucket_sorcery, "bucket", callbacks); |
| 507 |
} |
| 508 |
|
| 509 |
int ast_bucket_delete(struct ast_bucket *bucket) |
| 510 |
{ |
| 511 |
return ast_sorcery_delete(bucket_sorcery, bucket); |
| 512 |
} |
| 513 |
|
| 514 |
struct ast_json *ast_bucket_json(const struct ast_bucket *bucket) |
| 515 |
{ |
| 516 |
RAII_VAR(struct ast_json *, json, NULL, ast_json_unref); |
| 517 |
struct ast_json *id, *files, *buckets; |
| 518 |
struct ao2_iterator i; |
| 519 |
char *uri; |
| 520 |
int res = 0; |
| 521 |
|
| 522 |
json = ast_sorcery_objectset_json_create(bucket_sorcery, bucket); |
| 523 |
if (!json) { |
| 524 |
return NULL; |
| 525 |
} |
| 526 |
|
| 527 |
id = ast_json_string_create(ast_sorcery_object_get_id(bucket)); |
| 528 |
if (!id) { |
| 529 |
return NULL; |
| 530 |
} |
| 531 |
|
| 532 |
if (ast_json_object_set(json, "id", id)) { |
| 533 |
return NULL; |
| 534 |
} |
| 535 |
|
| 536 |
buckets = ast_json_array_create(); |
| 537 |
if (!buckets) { |
| 538 |
return NULL; |
| 539 |
} |
| 540 |
|
| 541 |
if (ast_json_object_set(json, "buckets", buckets)) { |
| 542 |
return NULL; |
| 543 |
} |
| 544 |
|
| 545 |
i = ao2_iterator_init(bucket->buckets, 0); |
| 546 |
for (; (uri = ao2_iterator_next(&i)); ao2_ref(uri, -1)) { |
| 547 |
struct ast_json *bucket_uri = ast_json_string_create(uri); |
| 548 |
|
| 549 |
if (!bucket_uri || ast_json_array_append(buckets, bucket_uri)) { |
| 550 |
res = -1; |
| 551 |
ao2_ref(uri, -1); |
| 552 |
break; |
| 553 |
} |
| 554 |
} |
| 555 |
ao2_iterator_destroy(&i); |
| 556 |
|
| 557 |
if (res) { |
| 558 |
return NULL; |
| 559 |
} |
| 560 |
|
| 561 |
files = ast_json_array_create(); |
| 562 |
if (!files) { |
| 563 |
return NULL; |
| 564 |
} |
| 565 |
|
| 566 |
if (ast_json_object_set(json, "files", files)) { |
| 567 |
return NULL; |
| 568 |
} |
| 569 |
|
| 570 |
i = ao2_iterator_init(bucket->files, 0); |
| 571 |
for (; (uri = ao2_iterator_next(&i)); ao2_ref(uri, -1)) { |
| 572 |
struct ast_json *file_uri = ast_json_string_create(uri); |
| 573 |
|
| 574 |
if (!file_uri || ast_json_array_append(files, file_uri)) { |
| 575 |
res = -1; |
| 576 |
ao2_ref(uri, -1); |
| 577 |
break; |
| 578 |
} |
| 579 |
} |
| 580 |
ao2_iterator_destroy(&i); |
| 581 |
|
| 582 |
if (res) { |
| 583 |
return NULL; |
| 584 |
} |
| 585 |
|
| 586 |
ast_json_ref(json); |
| 587 |
return json; |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
AO2_STRING_FIELD_HASH_FN(ast_bucket_metadata, name) |
| 592 |
|
| 593 |
|
| 594 |
AO2_STRING_FIELD_CMP_FN(ast_bucket_metadata, name) |
| 595 |
|
| 596 |
|
| 597 |
static void bucket_file_destroy(void *obj) |
| 598 |
{ |
| 599 |
struct ast_bucket_file *file = obj; |
| 600 |
|
| 601 |
if (file->scheme_impl->destroy) { |
| 602 |
file->scheme_impl->destroy(file); |
| 603 |
} |
| 604 |
|
| 605 |
ao2_cleanup(file->scheme_impl); |
| 606 |
ao2_cleanup(file->metadata); |
| 607 |
} |
| 608 |
|
| 609 |
|
| 610 |
static void *bucket_file_alloc(const char *name) |
| 611 |
{ |
| 612 |
RAII_VAR(struct ast_bucket_file *, file, NULL, ao2_cleanup); |
| 613 |
|
| 614 |
file = ast_sorcery_generic_alloc(sizeof(*file), bucket_file_destroy); |
| 615 |
if (!file) { |
| 616 |
return NULL; |
| 617 |
} |
| 618 |
|
| 619 |
if (ast_string_field_init(file, 128)) { |
| 620 |
return NULL; |
| 621 |
} |
| 622 |
|
| 623 |
file->metadata = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_NOLOCK, 0, METADATA_BUCKETS, |
| 624 |
ast_bucket_metadata_hash_fn, NULL, ast_bucket_metadata_cmp_fn); |
| 625 |
if (!file->metadata) { |
| 626 |
return NULL; |
| 627 |
} |
| 628 |
|
| 629 |
ao2_ref(file, +1); |
| 630 |
return file; |
| 631 |
} |
| 632 |
|
| 633 |
struct ast_bucket_file *ast_bucket_file_alloc(const char *uri) |
| 634 |
{ |
| 635 |
#ifdef HAVE_URIPARSER |
| 636 |
UriParserStateA state; |
| 637 |
UriUriA full_uri; |
| 638 |
size_t len; |
| 639 |
#else |
| 640 |
char *tmp = ast_strdupa(uri); |
| 641 |
#endif |
| 642 |
char *uri_scheme; |
| 643 |
RAII_VAR(struct ast_bucket_scheme *, scheme, NULL, ao2_cleanup); |
| 644 |
struct ast_bucket_file *file; |
| 645 |
|
| 646 |
if (ast_strlen_zero(uri)) { |
| 647 |
return NULL; |
| 648 |
} |
| 649 |
|
| 650 |
#ifdef HAVE_URIPARSER |
| 651 |
state.uri = &full_uri; |
| 652 |
if (uriParseUriA(&state, uri) != URI_SUCCESS || |
| 653 |
!full_uri.scheme.first || !full_uri.scheme.afterLast || |
| 654 |
!full_uri.pathTail) { |
| 655 |
uriFreeUriMembersA(&full_uri); |
| 656 |
return NULL; |
| 657 |
} |
| 658 |
|
| 659 |
len = (full_uri.scheme.afterLast - full_uri.scheme.first) + 1; |
| 660 |
uri_scheme = ast_alloca(len); |
| 661 |
ast_copy_string(uri_scheme, full_uri.scheme.first, len); |
| 662 |
|
| 663 |
uriFreeUriMembersA(&full_uri); |
| 664 |
#else |
| 665 |
uri_scheme = tmp; |
| 666 |
if (!(tmp = strchr(uri_scheme, ':'))) { |
| 667 |
return NULL; |
| 668 |
} |
| 669 |
*tmp = '\0'; |
| 670 |
#endif |
| 671 |
|
| 672 |
scheme = ao2_find(schemes, uri_scheme, OBJ_KEY); |
| 673 |
if (!scheme) { |
| 674 |
return NULL; |
| 675 |
} |
| 676 |
|
| 677 |
file = ast_sorcery_alloc(bucket_sorcery, "file", uri); |
| 678 |
if (!file) { |
| 679 |
return NULL; |
| 680 |
} |
| 681 |
|
| 682 |
ao2_ref(scheme, +1); |
| 683 |
file->scheme_impl = scheme; |
| 684 |
|
| 685 |
ast_string_field_set(file, scheme, uri_scheme); |
| 686 |
|
| 687 |
if (scheme->create && scheme->create(file)) { |
| 688 |
ao2_ref(file, -1); |
| 689 |
return NULL; |
| 690 |
} |
| 691 |
|
| 692 |
return file; |
| 693 |
} |
| 694 |
|
| 695 |
int ast_bucket_file_create(struct ast_bucket_file *file) |
| 696 |
{ |
| 697 |
return ast_sorcery_create(bucket_sorcery, file); |
| 698 |
} |
| 699 |
|
| 700 |
|
| 701 |
static int bucket_copy(const char *infile, const char *outfile) |
| 702 |
{ |
| 703 |
int ifd, ofd, len; |
| 704 |
char buf[4096]; |
| 705 |
|
| 706 |
if ((ifd = open(infile, O_RDONLY)) < 0) { |
| 707 |
ast_log(LOG_WARNING, "Unable to open %s in read-only mode, error: %s\n", infile, strerror(errno)); |
| 708 |
return -1; |
| 709 |
} |
| 710 |
if ((ofd = open(outfile, O_WRONLY | O_TRUNC | O_CREAT, AST_FILE_MODE)) < 0) { |
| 711 |
ast_log(LOG_WARNING, "Unable to open %s in write-only mode, error: %s\n", outfile, strerror(errno)); |
| 712 |
close(ifd); |
| 713 |
return -1; |
| 714 |
} |
| 715 |
while ( (len = read(ifd, buf, sizeof(buf)) ) ) { |
| 716 |
int res; |
| 717 |
if (len < 0) { |
| 718 |
ast_log(LOG_WARNING, "Read failed on %s: %s\n", infile, strerror(errno)); |
| 719 |
break; |
| 720 |
} |
| 721 |
|
| 722 |
res = write(ofd, buf, len); |
| 723 |
if (res != len) { |
| 724 |
ast_log(LOG_WARNING, "Write failed on %s (%d of %d): %s\n", outfile, res, len, strerror(errno)); |
| 725 |
len = -1; |
| 726 |
break; |
| 727 |
} |
| 728 |
} |
| 729 |
close(ifd); |
| 730 |
close(ofd); |
| 731 |
if (len < 0) { |
| 732 |
unlink(outfile); |
| 733 |
return -1; |
| 734 |
} |
| 735 |
return 0; |
| 736 |
} |
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
static int bucket_file_copy_handler(const void *src, void *dst) |
| 743 |
{ |
| 744 |
const struct ast_bucket_file *src_file = src; |
| 745 |
struct ast_bucket_file *dst_file = dst; |
| 746 |
|
| 747 |
dst_file->scheme_impl = ao2_bump(src_file->scheme_impl); |
| 748 |
ast_string_field_set(dst_file, scheme, src_file->scheme); |
| 749 |
dst_file->created = src_file->created; |
| 750 |
dst_file->modified = src_file->modified; |
| 751 |
strcpy(dst_file->path, src_file->path); |
| 752 |
|
| 753 |
dst_file->metadata = ao2_container_clone(src_file->metadata, 0); |
| 754 |
if (!dst_file->metadata) { |
| 755 |
return -1; |
| 756 |
} |
| 757 |
|
| 758 |
return 0; |
| 759 |
} |
| 760 |
|
| 761 |
struct ast_bucket_file *ast_bucket_file_copy(struct ast_bucket_file *file, const char *uri) |
| 762 |
{ |
| 763 |
RAII_VAR(struct ast_bucket_file *, copy, ast_bucket_file_alloc(uri), ao2_cleanup); |
| 764 |
|
| 765 |
if (!copy) { |
| 766 |
return NULL; |
| 767 |
} |
| 768 |
|
| 769 |
ao2_cleanup(copy->metadata); |
| 770 |
copy->metadata = ao2_container_clone(file->metadata, 0); |
| 771 |
if (!copy->metadata || |
| 772 |
bucket_copy(file->path, copy->path)) { |
| 773 |
return NULL; |
| 774 |
} |
| 775 |
|
| 776 |
ao2_ref(copy, +1); |
| 777 |
return copy; |
| 778 |
} |
| 779 |
|
| 780 |
struct ast_bucket_file *ast_bucket_file_clone(struct ast_bucket_file *file) |
| 781 |
{ |
| 782 |
return ast_sorcery_copy(bucket_sorcery, file); |
| 783 |
} |
| 784 |
|
| 785 |
struct ast_bucket_file *ast_bucket_file_retrieve(const char *uri) |
| 786 |
{ |
| 787 |
if (ast_strlen_zero(uri)) { |
| 788 |
return NULL; |
| 789 |
} |
| 790 |
|
| 791 |
return ast_sorcery_retrieve_by_id(bucket_sorcery, "file", uri); |
| 792 |
} |
| 793 |
|
| 794 |
int ast_bucket_file_is_stale(struct ast_bucket_file *file) |
| 795 |
{ |
| 796 |
return ast_sorcery_is_stale(bucket_sorcery, file); |
| 797 |
} |
| 798 |
|
| 799 |
int ast_bucket_file_observer_add(const struct ast_sorcery_observer *callbacks) |
| 800 |
{ |
| 801 |
return ast_sorcery_observer_add(bucket_sorcery, "file", callbacks); |
| 802 |
} |
| 803 |
|
| 804 |
void ast_bucket_file_observer_remove(const struct ast_sorcery_observer *callbacks) |
| 805 |
{ |
| 806 |
ast_sorcery_observer_remove(bucket_sorcery, "file", callbacks); |
| 807 |
} |
| 808 |
|
| 809 |
int ast_bucket_file_update(struct ast_bucket_file *file) |
| 810 |
{ |
| 811 |
return ast_sorcery_update(bucket_sorcery, file); |
| 812 |
} |
| 813 |
|
| 814 |
int ast_bucket_file_delete(struct ast_bucket_file *file) |
| 815 |
{ |
| 816 |
return ast_sorcery_delete(bucket_sorcery, file); |
| 817 |
} |
| 818 |
|
| 819 |
struct ast_json *ast_bucket_file_json(const struct ast_bucket_file *file) |
| 820 |
{ |
| 821 |
RAII_VAR(struct ast_json *, json, NULL, ast_json_unref); |
| 822 |
struct ast_json *id, *metadata; |
| 823 |
struct ao2_iterator i; |
| 824 |
struct ast_bucket_metadata *attribute; |
| 825 |
int res = 0; |
| 826 |
|
| 827 |
json = ast_sorcery_objectset_json_create(bucket_sorcery, file); |
| 828 |
if (!json) { |
| 829 |
return NULL; |
| 830 |
} |
| 831 |
|
| 832 |
id = ast_json_string_create(ast_sorcery_object_get_id(file)); |
| 833 |
if (!id) { |
| 834 |
return NULL; |
| 835 |
} |
| 836 |
|
| 837 |
if (ast_json_object_set(json, "id", id)) { |
| 838 |
return NULL; |
| 839 |
} |
| 840 |
|
| 841 |
metadata = ast_json_object_create(); |
| 842 |
if (!metadata) { |
| 843 |
return NULL; |
| 844 |
} |
| 845 |
|
| 846 |
if (ast_json_object_set(json, "metadata", metadata)) { |
| 847 |
return NULL; |
| 848 |
} |
| 849 |
|
| 850 |
i = ao2_iterator_init(file->metadata, 0); |
| 851 |
for (; (attribute = ao2_iterator_next(&i)); ao2_ref(attribute, -1)) { |
| 852 |
struct ast_json *value = ast_json_string_create(attribute->value); |
| 853 |
|
| 854 |
if (!value || ast_json_object_set(metadata, attribute->name, value)) { |
| 855 |
res = -1; |
| 856 |
break; |
| 857 |
} |
| 858 |
} |
| 859 |
ao2_iterator_destroy(&i); |
| 860 |
|
| 861 |
if (res) { |
| 862 |
return NULL; |
| 863 |
} |
| 864 |
|
| 865 |
ast_json_ref(json); |
| 866 |
return json; |
| 867 |
} |
| 868 |
|
| 869 |
int ast_bucket_file_temporary_create(struct ast_bucket_file *file) |
| 870 |
{ |
| 871 |
int fd; |
| 872 |
|
| 873 |
snprintf(file->path, sizeof(file->path), "%s/bucket-XXXXXX", ast_config_AST_CACHE_DIR); |
| 874 |
|
| 875 |
fd = mkstemp(file->path); |
| 876 |
if (fd < 0) { |
| 877 |
return -1; |
| 878 |
} |
| 879 |
|
| 880 |
close(fd); |
| 881 |
return 0; |
| 882 |
} |
| 883 |
|
| 884 |
void ast_bucket_file_temporary_destroy(struct ast_bucket_file *file) |
| 885 |
{ |
| 886 |
if (!ast_strlen_zero(file->path)) { |
| 887 |
unlink(file->path); |
| 888 |
} |
| 889 |
} |
| 890 |
|
| 891 |
|
| 892 |
AO2_STRING_FIELD_HASH_FN(ast_bucket_scheme, name) |
| 893 |
|
| 894 |
|
| 895 |
AO2_STRING_FIELD_CMP_FN(ast_bucket_scheme, name) |
| 896 |
|
| 897 |
|
| 898 |
static void bucket_cleanup(void) |
| 899 |
{ |
| 900 |
ast_sorcery_unref(bucket_sorcery); |
| 901 |
bucket_sorcery = NULL; |
| 902 |
|
| 903 |
ast_sorcery_wizard_unregister(&bucket_wizard); |
| 904 |
ast_sorcery_wizard_unregister(&bucket_file_wizard); |
| 905 |
|
| 906 |
ao2_cleanup(schemes); |
| 907 |
} |
| 908 |
|
| 909 |
|
| 910 |
static int timeval_str2struct(const struct aco_option *opt, struct ast_variable *var, void *obj) |
| 911 |
{ |
| 912 |
struct timeval *field = (struct timeval *)(obj + aco_option_get_argument(opt, 0)); |
| 913 |
return ast_get_timeval(var->value, field, ast_tv(0, 0), NULL); |
| 914 |
} |
| 915 |
|
| 916 |
|
| 917 |
static int timeval_struct2str(const void *obj, const intptr_t *args, char **buf) |
| 918 |
{ |
| 919 |
struct timeval *field = (struct timeval *)(obj + args[0]); |
| 920 |
return (ast_asprintf(buf, "%lu.%06lu", (unsigned long)field->tv_sec, (unsigned long)field->tv_usec) < 0) ? -1 : 0; |
| 921 |
} |
| 922 |
|
| 923 |
|
| 924 |
int ast_bucket_init(void) |
| 925 |
{ |
| 926 |
ast_register_cleanup(&bucket_cleanup); |
| 927 |
|
| 928 |
schemes = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_RWLOCK, 0, SCHEME_BUCKETS, |
| 929 |
ast_bucket_scheme_hash_fn, NULL, ast_bucket_scheme_cmp_fn); |
| 930 |
if (!schemes) { |
| 931 |
ast_log(LOG_ERROR, "Failed to create container for Bucket schemes\n"); |
| 932 |
return -1; |
| 933 |
} |
| 934 |
|
| 935 |
if (__ast_sorcery_wizard_register(&bucket_wizard, NULL)) { |
| 936 |
ast_log(LOG_ERROR, "Failed to register sorcery wizard for 'bucket' intermediary\n"); |
| 937 |
return -1; |
| 938 |
} |
| 939 |
|
| 940 |
if (__ast_sorcery_wizard_register(&bucket_file_wizard, NULL)) { |
| 941 |
ast_log(LOG_ERROR, "Failed to register sorcery wizard for 'file' intermediary\n"); |
| 942 |
return -1; |
| 943 |
} |
| 944 |
|
| 945 |
if (!(bucket_sorcery = ast_sorcery_open())) { |
| 946 |
ast_log(LOG_ERROR, "Failed to create sorcery instance for Bucket support\n"); |
| 947 |
return -1; |
| 948 |
} |
| 949 |
|
| 950 |
if (ast_sorcery_apply_default(bucket_sorcery, "bucket", "bucket", NULL) == AST_SORCERY_APPLY_FAIL) { |
| 951 |
ast_log(LOG_ERROR, "Failed to apply intermediary for 'bucket' object type in Bucket sorcery\n"); |
| 952 |
return -1; |
| 953 |
} |
| 954 |
|
| 955 |
if (ast_sorcery_internal_object_register(bucket_sorcery, "bucket", bucket_alloc, NULL, NULL)) { |
| 956 |
ast_log(LOG_ERROR, "Failed to register 'bucket' object type in Bucket sorcery\n"); |
| 957 |
return -1; |
| 958 |
} |
| 959 |
|
| 960 |
ast_sorcery_object_field_register_nodoc(bucket_sorcery, "bucket", "scheme", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_bucket, scheme)); |
| 961 |
ast_sorcery_object_field_register_custom_nodoc(bucket_sorcery, "bucket", "created", "", timeval_str2struct, timeval_struct2str, NULL, 0, FLDSET(struct ast_bucket, created)); |
| 962 |
ast_sorcery_object_field_register_custom_nodoc(bucket_sorcery, "bucket", "modified", "", timeval_str2struct, timeval_struct2str, NULL, 0, FLDSET(struct ast_bucket, modified)); |
| 963 |
ast_sorcery_object_set_copy_handler(bucket_sorcery, "bucket", bucket_copy_handler); |
| 964 |
|
| 965 |
if (ast_sorcery_apply_default(bucket_sorcery, "file", "bucket_file", NULL) == AST_SORCERY_APPLY_FAIL) { |
| 966 |
ast_log(LOG_ERROR, "Failed to apply intermediary for 'file' object type in Bucket sorcery\n"); |
| 967 |
return -1; |
| 968 |
} |
| 969 |
|
| 970 |
if (ast_sorcery_internal_object_register(bucket_sorcery, "file", bucket_file_alloc, NULL, NULL)) { |
| 971 |
ast_log(LOG_ERROR, "Failed to register 'file' object type in Bucket sorcery\n"); |
| 972 |
return -1; |
| 973 |
} |
| 974 |
|
| 975 |
ast_sorcery_object_field_register_nodoc(bucket_sorcery, "file", "scheme", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_bucket_file, scheme)); |
| 976 |
ast_sorcery_object_field_register_custom_nodoc(bucket_sorcery, "file", "created", "", timeval_str2struct, timeval_struct2str, NULL, 0, FLDSET(struct ast_bucket_file, created)); |
| 977 |
ast_sorcery_object_field_register_custom_nodoc(bucket_sorcery, "file", "modified", "", timeval_str2struct, timeval_struct2str, NULL, 0, FLDSET(struct ast_bucket_file, modified)); |
| 978 |
ast_sorcery_object_set_copy_handler(bucket_sorcery, "file", bucket_file_copy_handler); |
| 979 |
|
| 980 |
return 0; |
| 981 |
} |