Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Macros | Functions | Variables
bucket.c File Reference

Bucket File API. More...

#include "asterisk.h"
#include <uriparser/Uri.h>
#include "asterisk/logger.h"
#include "asterisk/sorcery.h"
#include "asterisk/bucket.h"
#include "asterisk/config_options.h"
#include "asterisk/astobj2.h"
#include "asterisk/strings.h"
#include "asterisk/json.h"
#include "asterisk/file.h"
#include "asterisk/module.h"
#include "asterisk/paths.h"
Include dependency graph for bucket.c:

Go to the source code of this file.

Data Structures

struct  ast_bucket_scheme
 Structure for available schemes. More...
 

Macros

#define METADATA_BUCKETS   53
 Number of buckets for the container of metadata in a file. More...
 
#define SCHEME_BUCKETS   53
 Number of buckets for the container of schemes. More...
 

Functions

int __ast_bucket_scheme_register (const char *name, struct ast_sorcery_wizard *bucket, struct ast_sorcery_wizard *file, bucket_file_create_cb create_cb, bucket_file_destroy_cb destroy_cb, struct ast_module *module)
 Register support for a specific scheme. More...
 
struct ast_bucketast_bucket_alloc (const char *uri)
 Allocate a new bucket. More...
 
struct ast_bucketast_bucket_clone (struct ast_bucket *bucket)
 Clone a bucket. More...
 
int ast_bucket_create (struct ast_bucket *bucket)
 Create a new bucket in backend storage. More...
 
int ast_bucket_delete (struct ast_bucket *bucket)
 Delete a bucket from backend storage. More...
 
struct ast_bucket_fileast_bucket_file_alloc (const char *uri)
 Allocate a new bucket file. More...
 
struct ast_bucket_fileast_bucket_file_clone (struct ast_bucket_file *file)
 Clone a bucket file. More...
 
struct ast_bucket_fileast_bucket_file_copy (struct ast_bucket_file *file, const char *uri)
 Copy a bucket file to a new URI. More...
 
int ast_bucket_file_create (struct ast_bucket_file *file)
 Create a new bucket file in backend storage. More...
 
int ast_bucket_file_delete (struct ast_bucket_file *file)
 Delete a bucket file from backend storage. More...
 
int ast_bucket_file_is_stale (struct ast_bucket_file *file)
 Retrieve whether or not the backing datastore views the bucket file as stale. More...
 
struct ast_jsonast_bucket_file_json (const struct ast_bucket_file *file)
 Get a JSON representation of a bucket file. More...
 
void ast_bucket_file_metadata_callback (struct ast_bucket_file *file, ao2_callback_fn cb, void *arg)
 Execute a callback function on the metadata associated with a file. More...
 
struct ast_bucket_metadataast_bucket_file_metadata_get (struct ast_bucket_file *file, const char *name)
 Retrieve a metadata attribute from a file. More...
 
int ast_bucket_file_metadata_set (struct ast_bucket_file *file, const char *name, const char *value)
 Set a metadata attribute on a file to a specific value. More...
 
int ast_bucket_file_metadata_unset (struct ast_bucket_file *file, const char *name)
 Unset a specific metadata attribute on a file. More...
 
int ast_bucket_file_observer_add (const struct ast_sorcery_observer *callbacks)
 Add an observer for bucket file creation and deletion operations. More...
 
void ast_bucket_file_observer_remove (const struct ast_sorcery_observer *callbacks)
 Remove an observer from bucket file creation and deletion. More...
 
struct ast_bucket_fileast_bucket_file_retrieve (const char *uri)
 Retrieve a bucket file. More...
 
int ast_bucket_file_temporary_create (struct ast_bucket_file *file)
 Common file snapshot creation callback for creating a temporary file. More...
 
void ast_bucket_file_temporary_destroy (struct ast_bucket_file *file)
 Common file snapshot destruction callback for deleting a temporary file. More...
 
int ast_bucket_file_update (struct ast_bucket_file *file)
 Update an existing bucket file in backend storage. More...
 
int ast_bucket_init (void)
 Initialize bucket support. More...
 
int ast_bucket_is_stale (struct ast_bucket *bucket)
 Retrieve whether or not the backing datastore views the bucket as stale. More...
 
struct ast_jsonast_bucket_json (const struct ast_bucket *bucket)
 Get a JSON representation of a bucket. More...
 
int ast_bucket_observer_add (const struct ast_sorcery_observer *callbacks)
 Add an observer for bucket creation and deletion operations. More...
 
void ast_bucket_observer_remove (const struct ast_sorcery_observer *callbacks)
 Remove an observer from bucket creation and deletion. More...
 
struct ast_bucketast_bucket_retrieve (const char *uri)
 Retrieve information about a bucket. More...
 
static void * bucket_alloc (const char *name)
 Allocator for buckets. More...
 
static void bucket_cleanup (void)
 Hashing function for scheme container. More...
 
static int bucket_copy (const char *infile, const char *outfile)
 Copy a file, shamelessly taken from file.c. More...
 
static int bucket_copy_handler (const void *src, void *dst)
 
static void bucket_destroy (void *obj)
 Destructor for buckets. More...
 
static void * bucket_file_alloc (const char *name)
 Allocator for bucket files. More...
 
static int bucket_file_copy_handler (const void *src, void *dst)
 
static void bucket_file_destroy (void *obj)
 Hashing function for file metadata. More...
 
static int bucket_file_wizard_create (const struct ast_sorcery *sorcery, void *data, void *object)
 Callback function for creating a bucket file. More...
 
static int bucket_file_wizard_delete (const struct ast_sorcery *sorcery, void *data, void *object)
 Callback function for deleting a bucket file. More...
 
static int bucket_file_wizard_is_stale (const struct ast_sorcery *sorcery, void *data, void *object)
 Callback function for determining if a bucket is stale. More...
 
static void * bucket_file_wizard_retrieve (const struct ast_sorcery *sorcery, void *data, const char *type, const char *id)
 Callback function for retrieving a bucket file. More...
 
static int bucket_file_wizard_update (const struct ast_sorcery *sorcery, void *data, void *object)
 Callback function for updating a bucket file. More...
 
static struct ast_bucket_metadatabucket_metadata_alloc (const char *name, const char *value)
 Allocator for metadata attributes. More...
 
static int bucket_rbtree_str_sort_cmp (const void *obj_left, const void *obj_right, int flags)
 Sorting function for red black tree string container. More...
 
static int bucket_wizard_create (const struct ast_sorcery *sorcery, void *data, void *object)
 Callback function for creating a bucket. More...
 
static int bucket_wizard_delete (const struct ast_sorcery *sorcery, void *data, void *object)
 Callback function for deleting a bucket. More...
 
static int bucket_wizard_is_stale (const struct ast_sorcery *sorcery, void *data, void *object)
 Callback function for determining if a bucket is stale. More...
 
static void * bucket_wizard_retrieve (const struct ast_sorcery *sorcery, void *data, const char *type, const char *id)
 Callback function for retrieving a bucket. More...
 
static int timeval_str2struct (const struct aco_option *opt, struct ast_variable *var, void *obj)
 Custom handler for translating from a string timeval to actual structure. More...
 
static int timeval_struct2str (const void *obj, const intptr_t *args, char **buf)
 Custom handler for translating from an actual structure timeval to string. More...
 

Variables

static struct ast_sorcery_wizard bucket_file_wizard
 Intermediary file wizard. More...
 
static struct ast_sorcerybucket_sorcery
 Sorcery instance for all bucket operations. More...
 
static struct ast_sorcery_wizard bucket_wizard
 Intermediary bucket wizard. More...
 
static struct ao2_containerschemes
 Container of registered schemes. More...
 

Detailed Description

Bucket File API.

Author
Joshua Colp jcolp.nosp@m.@dig.nosp@m.ium.c.nosp@m.om

Definition in file bucket.c.

Macro Definition Documentation

◆ METADATA_BUCKETS

#define METADATA_BUCKETS   53

Number of buckets for the container of metadata in a file.

Definition at line 82 of file bucket.c.

Referenced by bucket_file_alloc().

◆ SCHEME_BUCKETS

#define SCHEME_BUCKETS   53

Number of buckets for the container of schemes.

Definition at line 79 of file bucket.c.

Referenced by ast_bucket_init().

Function Documentation

◆ __ast_bucket_scheme_register()

int __ast_bucket_scheme_register ( const char *  name,
struct ast_sorcery_wizard bucket,
struct ast_sorcery_wizard file,
bucket_file_create_cb  create_cb,
bucket_file_destroy_cb  destroy_cb,
struct ast_module module 
)

Register support for a specific scheme.

Parameters
nameName of the scheme, used to find based on scheme in URIs
bucketSorcery wizard used for buckets
fileSorcery wizard used for files
create_cbRequired file snapshot creation callback
destroy_cbOptional file snapshot destruction callback
moduleThe module which implements this scheme
Return values
0success
-1failure
Note
Once a scheme has been registered it can not be unregistered

Definition at line 277 of file bucket.c.

References ao2_alloc, ao2_cleanup, ao2_find, ao2_link_flags, ast_module_shutdown_ref, ast_strlen_zero, ast_verb, ast_bucket_scheme::bucket, ast_sorcery_wizard::create, ast_sorcery_wizard::delete, ast_bucket_scheme::file, lock, NULL, OBJ_KEY, OBJ_NOLOCK, RAII_VAR, ast_sorcery_wizard::retrieve_id, and SCOPED_AO2WRLOCK.

280 {
282  RAII_VAR(struct ast_bucket_scheme *, scheme, NULL, ao2_cleanup);
283 
284  if (ast_strlen_zero(name) || !bucket || !file ||
285  !bucket->create || !bucket->delete || !bucket->retrieve_id ||
286  (!bucket->create && !create_cb)) {
287  return -1;
288  }
289 
290  scheme = ao2_find(schemes, name, OBJ_KEY | OBJ_NOLOCK);
291  if (scheme) {
292  return -1;
293  }
294 
295  scheme = ao2_alloc(sizeof(*scheme) + strlen(name) + 1, NULL);
296  if (!scheme) {
297  return -1;
298  }
299 
300  strcpy(scheme->name, name);
301  scheme->bucket = bucket;
302  scheme->file = file;
303  scheme->create = create_cb;
304  scheme->destroy = destroy_cb;
305 
307 
308  ast_verb(2, "Registered bucket scheme '%s'\n", name);
309 
310  ast_module_shutdown_ref(module);
311 
312  return 0;
313 }
#define OBJ_KEY
Definition: astobj2.h:1155
Assume that the ao2_container is already locked.
Definition: astobj2.h:1067
#define ao2_link_flags(container, obj, flags)
Definition: astobj2.h:1572
#define NULL
Definition: resample.c:96
int(* create)(const struct ast_sorcery *sorcery, void *data, void *object)
Callback for creating an object.
Definition: sorcery.h:293
#define ast_verb(level,...)
Definition: logger.h:463
#define SCOPED_AO2WRLOCK(varname, obj)
scoped lock specialization for ao2 write locks.
Definition: lock.h:612
#define ast_strlen_zero(foo)
Definition: strings.h:52
int(* delete)(const struct ast_sorcery *sorcery, void *data, void *object)
Callback for deleting an object.
Definition: sorcery.h:319
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
ast_mutex_t lock
Definition: app_meetme.c:1091
#define ast_module_shutdown_ref(mod)
Prevent unload of the module before shutdown.
Definition: module.h:464
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:411
static const char name[]
Definition: cdr_mysql.c:74
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1756
void *(* retrieve_id)(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id)
Callback for retrieving an object using an id.
Definition: sorcery.h:296
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
Structure for available schemes.
Definition: bucket.c:91
static struct ao2_container * schemes
Container of registered schemes.
Definition: bucket.c:88

◆ ast_bucket_alloc()

struct ast_bucket* ast_bucket_alloc ( const char *  uri)

Allocate a new bucket.

Parameters
uriComplete URI for the bucket
non-NULLsuccess
NULLfailure
Note
This only creates a local bucket object, to persist in backend storage you must call ast_bucket_create

Definition at line 431 of file bucket.c.

References ao2_cleanup, ao2_find, ao2_ref, ast_alloca, ast_copy_string(), ast_sorcery_alloc(), ast_strdupa, ast_string_field_set, ast_strlen_zero, ast_bucket_scheme::bucket, len(), NULL, OBJ_KEY, RAII_VAR, ast_bucket::scheme, ast_bucket::scheme_impl, state, and tmp().

Referenced by AST_TEST_DEFINE(), and bucket_test_wizard_retrieve_id().

432 {
433 #ifdef HAVE_URIPARSER
434  UriParserStateA state;
435  UriUriA full_uri;
436  size_t len;
437 #else
438  char *tmp = ast_strdupa(uri);
439 #endif
440  char *uri_scheme;
441  RAII_VAR(struct ast_bucket_scheme *, scheme, NULL, ao2_cleanup);
442  struct ast_bucket *bucket;
443 
444  if (ast_strlen_zero(uri)) {
445  return NULL;
446  }
447 
448 #ifdef HAVE_URIPARSER
449  state.uri = &full_uri;
450  if (uriParseUriA(&state, uri) != URI_SUCCESS ||
451  !full_uri.scheme.first || !full_uri.scheme.afterLast ||
452  !full_uri.pathTail) {
453  uriFreeUriMembersA(&full_uri);
454  return NULL;
455  }
456 
457  len = (full_uri.scheme.afterLast - full_uri.scheme.first) + 1;
458  uri_scheme = ast_alloca(len);
459  ast_copy_string(uri_scheme, full_uri.scheme.first, len);
460 
461  uriFreeUriMembersA(&full_uri);
462 #else
463  uri_scheme = tmp;
464  if (!(tmp = strchr(uri_scheme, ':'))) {
465  return NULL;
466  }
467  *tmp = '\0';
468 #endif
469 
470  scheme = ao2_find(schemes, uri_scheme, OBJ_KEY);
471  if (!scheme) {
472  return NULL;
473  }
474 
475  bucket = ast_sorcery_alloc(bucket_sorcery, "bucket", uri);
476  if (!bucket) {
477  return NULL;
478  }
479 
480  ao2_ref(scheme, +1);
481  bucket->scheme_impl = scheme;
482 
483  ast_string_field_set(bucket, scheme, uri_scheme);
484 
485  return bucket;
486 }
enum sip_cc_notify_state state
Definition: chan_sip.c:959
#define OBJ_KEY
Definition: astobj2.h:1155
Bucket structure, contains other buckets and files.
Definition: bucket.h:57
static int tmp()
Definition: bt_open.c:389
#define NULL
Definition: resample.c:96
#define ast_strlen_zero(foo)
Definition: strings.h:52
static struct ast_sorcery * bucket_sorcery
Sorcery instance for all bucket operations.
Definition: bucket.c:85
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
#define ao2_ref(o, delta)
Definition: astobj2.h:464
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:290
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
void * ast_sorcery_alloc(const struct ast_sorcery *sorcery, const char *type, const char *id)
Allocate an object.
Definition: sorcery.c:1744
struct ast_bucket_scheme * scheme_impl
Scheme implementation in use.
Definition: bucket.h:61
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1756
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:401
Structure for available schemes.
Definition: bucket.c:91
const ast_string_field scheme
Name of scheme in use.
Definition: bucket.h:66
static struct ao2_container * schemes
Container of registered schemes.
Definition: bucket.c:88
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.
Definition: stringfields.h:514

◆ ast_bucket_clone()

struct ast_bucket* ast_bucket_clone ( struct ast_bucket bucket)

Clone a bucket.

This will create a copy of the passed in ast_bucket structure. While all properties of the ast_bucket structure are copied, any metadata in the original structure simply has its reference count increased.

Parameters
fileThe bucket to clone
Return values
non-NULLsuccess
NULLfailure
Note
This operation should be called prior to updating a bucket object, as ast_bucket instances are immutable

Definition at line 510 of file bucket.c.

References ast_sorcery_copy().

Referenced by AST_TEST_DEFINE().

511 {
512  return ast_sorcery_copy(bucket_sorcery, bucket);
513 }
static struct ast_sorcery * bucket_sorcery
Sorcery instance for all bucket operations.
Definition: bucket.c:85
void * ast_sorcery_copy(const struct ast_sorcery *sorcery, const void *object)
Create a copy of an object.
Definition: sorcery.c:1778

◆ ast_bucket_create()

int ast_bucket_create ( struct ast_bucket bucket)

Create a new bucket in backend storage.

Parameters
bucketThe bucket
Return values
0success
-1failure

Definition at line 488 of file bucket.c.

References ast_sorcery_create().

Referenced by AST_TEST_DEFINE().

489 {
490  return ast_sorcery_create(bucket_sorcery, bucket);
491 }
static struct ast_sorcery * bucket_sorcery
Sorcery instance for all bucket operations.
Definition: bucket.c:85
int ast_sorcery_create(const struct ast_sorcery *sorcery, void *object)
Create and potentially persist an object using an available wizard.
Definition: sorcery.c:2057

◆ ast_bucket_delete()

int ast_bucket_delete ( struct ast_bucket bucket)

Delete a bucket from backend storage.

Parameters
bucketThe bucket
Return values
0success
-1failure

Definition at line 539 of file bucket.c.

References ast_sorcery_delete().

Referenced by AST_TEST_DEFINE().

540 {
541  return ast_sorcery_delete(bucket_sorcery, bucket);
542 }
static struct ast_sorcery * bucket_sorcery
Sorcery instance for all bucket operations.
Definition: bucket.c:85
int ast_sorcery_delete(const struct ast_sorcery *sorcery, void *object)
Delete an object.
Definition: sorcery.c:2233

◆ ast_bucket_file_alloc()

struct ast_bucket_file* ast_bucket_file_alloc ( const char *  uri)

Allocate a new bucket file.

Parameters
uriComplete URI for the bucket file
non-NULLsuccess
NULLfailure
Note
This only creates a local bucket file object, to persist in backend storage you must call ast_bucket_file_create

Definition at line 663 of file bucket.c.

References ao2_cleanup, ao2_find, ao2_ref, ast_alloca, ast_copy_string(), ast_sorcery_alloc(), ast_strdupa, ast_string_field_set, ast_strlen_zero, ast_bucket_scheme::file, len(), NULL, OBJ_KEY, RAII_VAR, ast_bucket_file::scheme, ast_bucket_file::scheme_impl, state, and tmp().

Referenced by ast_bucket_file_copy(), ast_media_cache_create_or_update(), AST_TEST_DEFINE(), bucket_http_test_wizard_retrieve_id(), bucket_http_wizard_retrieve_id(), bucket_test_wizard_retrieve_id(), and media_cache_item_populate_from_astdb().

664 {
665 #ifdef HAVE_URIPARSER
666  UriParserStateA state;
667  UriUriA full_uri;
668  size_t len;
669 #else
670  char *tmp = ast_strdupa(uri);
671 #endif
672  char *uri_scheme;
674  struct ast_bucket_file *file;
675 
676  if (ast_strlen_zero(uri)) {
677  return NULL;
678  }
679 
680 #ifdef HAVE_URIPARSER
681  state.uri = &full_uri;
682  if (uriParseUriA(&state, uri) != URI_SUCCESS ||
683  !full_uri.scheme.first || !full_uri.scheme.afterLast ||
684  !full_uri.pathTail) {
685  uriFreeUriMembersA(&full_uri);
686  return NULL;
687  }
688 
689  len = (full_uri.scheme.afterLast - full_uri.scheme.first) + 1;
690  uri_scheme = ast_alloca(len);
691  ast_copy_string(uri_scheme, full_uri.scheme.first, len);
692 
693  uriFreeUriMembersA(&full_uri);
694 #else
695  uri_scheme = tmp;
696  if (!(tmp = strchr(uri_scheme, ':'))) {
697  return NULL;
698  }
699  *tmp = '\0';
700 #endif
701 
702  scheme = ao2_find(schemes, uri_scheme, OBJ_KEY);
703  if (!scheme) {
704  return NULL;
705  }
706 
707  file = ast_sorcery_alloc(bucket_sorcery, "file", uri);
708  if (!file) {
709  return NULL;
710  }
711 
712  ao2_ref(scheme, +1);
713  file->scheme_impl = scheme;
714 
715  ast_string_field_set(file, scheme, uri_scheme);
716 
717  if (scheme->create && scheme->create(file)) {
718  ao2_ref(file, -1);
719  return NULL;
720  }
721 
722  return file;
723 }
enum sip_cc_notify_state state
Definition: chan_sip.c:959
#define OBJ_KEY
Definition: astobj2.h:1155
static int tmp()
Definition: bt_open.c:389
struct ast_bucket_scheme * scheme_impl
Scheme implementation in use.
Definition: bucket.h:82
#define NULL
Definition: resample.c:96
#define ast_strlen_zero(foo)
Definition: strings.h:52
static struct ast_sorcery * bucket_sorcery
Sorcery instance for all bucket operations.
Definition: bucket.c:85
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
#define ao2_ref(o, delta)
Definition: astobj2.h:464
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
Bucket file structure, contains reference to file and information about it.
Definition: bucket.h:78
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:290
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
const ast_string_field scheme
Name of scheme in use.
Definition: bucket.h:87
void * ast_sorcery_alloc(const struct ast_sorcery *sorcery, const char *type, const char *id)
Allocate an object.
Definition: sorcery.c:1744
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1756
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:401
Structure for available schemes.
Definition: bucket.c:91
const ast_string_field scheme
Name of scheme in use.
Definition: bucket.h:66
static struct ao2_container * schemes
Container of registered schemes.
Definition: bucket.c:88
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.
Definition: stringfields.h:514

◆ ast_bucket_file_clone()

struct ast_bucket_file* ast_bucket_file_clone ( struct ast_bucket_file file)

Clone a bucket file.

This will create a copy of the passed in ast_bucket_file structure. While all properties of the ast_bucket_file structure are copied, any metadata in the original structure simply has its reference count increased. Note that this copies the structure, not the underlying file.

Parameters
fileThe bucket file to clone
Return values
non-NULLsuccess
NULLfailure
Note
This operation should be called prior to updating a bucket file object, as ast_bucket_file instances are immutable

Definition at line 810 of file bucket.c.

References ast_sorcery_copy().

Referenced by ast_media_cache_create_or_update(), and AST_TEST_DEFINE().

811 {
812  return ast_sorcery_copy(bucket_sorcery, file);
813 }
static struct ast_sorcery * bucket_sorcery
Sorcery instance for all bucket operations.
Definition: bucket.c:85
void * ast_sorcery_copy(const struct ast_sorcery *sorcery, const void *object)
Create a copy of an object.
Definition: sorcery.c:1778

◆ ast_bucket_file_copy()

struct ast_bucket_file* ast_bucket_file_copy ( struct ast_bucket_file file,
const char *  uri 
)

Copy a bucket file to a new URI.

Parameters
fileThe source bucket file
uriThe new URI
Return values
non-NULLsuccess
NULLfailure
Note
This operation stages things locally, you must call ast_bucket_file_create on the file that is returned to commit the copy to backend storage

Definition at line 791 of file bucket.c.

References ao2_cleanup, ao2_container_clone, ao2_ref, ast_bucket_file_alloc(), bucket_copy(), copy(), ast_bucket_file::metadata, NULL, ast_bucket_file::path, and RAII_VAR.

Referenced by AST_TEST_DEFINE().

792 {
794 
795  if (!copy) {
796  return NULL;
797  }
798 
799  ao2_cleanup(copy->metadata);
800  copy->metadata = ao2_container_clone(file->metadata, 0);
801  if (!copy->metadata ||
802  bucket_copy(file->path, copy->path)) {
803  return NULL;
804  }
805 
806  ao2_ref(copy, +1);
807  return copy;
808 }
#define ao2_container_clone(orig, flags)
Definition: astobj2.h:1430
static int copy(char *infile, char *outfile)
Utility function to copy a file.
#define NULL
Definition: resample.c:96
static int bucket_copy(const char *infile, const char *outfile)
Copy a file, shamelessly taken from file.c.
Definition: bucket.c:731
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
#define ao2_ref(o, delta)
Definition: astobj2.h:464
Bucket file structure, contains reference to file and information about it.
Definition: bucket.h:78
struct ao2_container * metadata
Container of metadata attributes about file.
Definition: bucket.h:93
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
char path[PATH_MAX]
Local path to this file.
Definition: bucket.h:95
struct ast_bucket_file * ast_bucket_file_alloc(const char *uri)
Allocate a new bucket file.
Definition: bucket.c:663

◆ ast_bucket_file_create()

int ast_bucket_file_create ( struct ast_bucket_file file)

Create a new bucket file in backend storage.

Parameters
fileThe bucket file
Return values
0success
-1failure

Definition at line 725 of file bucket.c.

References ast_sorcery_create().

Referenced by ast_media_cache_create_or_update(), and AST_TEST_DEFINE().

726 {
727  return ast_sorcery_create(bucket_sorcery, file);
728 }
static struct ast_sorcery * bucket_sorcery
Sorcery instance for all bucket operations.
Definition: bucket.c:85
int ast_sorcery_create(const struct ast_sorcery *sorcery, void *object)
Create and potentially persist an object using an available wizard.
Definition: sorcery.c:2057

◆ ast_bucket_file_delete()

int ast_bucket_file_delete ( struct ast_bucket_file file)

Delete a bucket file from backend storage.

Parameters
fileThe bucket file
Return values
0success
-1failure

Definition at line 844 of file bucket.c.

References ast_sorcery_delete().

Referenced by ast_media_cache_delete(), ast_media_cache_retrieve(), AST_TEST_DEFINE(), and bucket_file_cleanup().

845 {
846  return ast_sorcery_delete(bucket_sorcery, file);
847 }
static struct ast_sorcery * bucket_sorcery
Sorcery instance for all bucket operations.
Definition: bucket.c:85
int ast_sorcery_delete(const struct ast_sorcery *sorcery, void *object)
Delete an object.
Definition: sorcery.c:2233

◆ ast_bucket_file_is_stale()

int ast_bucket_file_is_stale ( struct ast_bucket_file file)

Retrieve whether or not the backing datastore views the bucket file as stale.

Since
14.0.0

This function will ask whatever data storage backs the bucket file's schema type if the current instance of the object is stale. It will not update the bucket file object itself, as said objects are immutable. If the caller of this function would like to update the object, it should perform a retrieve operation.

Parameters
bucket_fileThe bucket file object to check
Return values
0if bucket_file is not stale
1if bucket_file is stale

Definition at line 824 of file bucket.c.

References ast_sorcery_is_stale().

Referenced by ast_media_cache_retrieve(), and AST_TEST_DEFINE().

825 {
826  return ast_sorcery_is_stale(bucket_sorcery, file);
827 }
int ast_sorcery_is_stale(const struct ast_sorcery *sorcery, void *object)
Determine if a sorcery object is stale with respect to its backing datastore.
Definition: sorcery.c:2283
static struct ast_sorcery * bucket_sorcery
Sorcery instance for all bucket operations.
Definition: bucket.c:85

◆ ast_bucket_file_json()

struct ast_json* ast_bucket_file_json ( const struct ast_bucket_file file)

Get a JSON representation of a bucket file.

Parameters
fileThe specific bucket file
Return values
non-NULLsuccess
NULLfailure
Note
The returned ast_json object must be unreferenced using ast_json_unref

Definition at line 849 of file bucket.c.

References ao2_iterator_destroy(), ao2_iterator_init(), ao2_iterator_next, ao2_ref, ast_json_object_create(), ast_json_object_set(), ast_json_ref(), ast_json_string_create(), ast_json_unref(), ast_sorcery_object_get_id(), ast_sorcery_objectset_json_create(), id, ast_bucket_file::metadata, ast_bucket_metadata::name, NULL, RAII_VAR, value, and ast_bucket_metadata::value.

Referenced by AST_TEST_DEFINE().

850 {
851  RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
852  struct ast_json *id, *metadata;
853  struct ao2_iterator i;
854  struct ast_bucket_metadata *attribute;
855  int res = 0;
856 
858  if (!json) {
859  return NULL;
860  }
861 
863  if (!id) {
864  return NULL;
865  }
866 
867  if (ast_json_object_set(json, "id", id)) {
868  return NULL;
869  }
870 
871  metadata = ast_json_object_create();
872  if (!metadata) {
873  return NULL;
874  }
875 
876  if (ast_json_object_set(json, "metadata", metadata)) {
877  return NULL;
878  }
879 
880  i = ao2_iterator_init(file->metadata, 0);
881  for (; (attribute = ao2_iterator_next(&i)); ao2_ref(attribute, -1)) {
882  struct ast_json *value = ast_json_string_create(attribute->value);
883 
884  if (!value || ast_json_object_set(metadata, attribute->name, value)) {
885  res = -1;
886  break;
887  }
888  }
890 
891  if (res) {
892  return NULL;
893  }
894 
895  ast_json_ref(json);
896  return json;
897 }
struct ast_json * ast_json_ref(struct ast_json *value)
Increase refcount on value.
Definition: json.c:67
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
void ao2_iterator_destroy(struct ao2_iterator *iter)
Destroy a container iterator.
#define NULL
Definition: resample.c:96
int value
Definition: syslog.c:37
int ast_json_object_set(struct ast_json *object, const char *key, struct ast_json *value)
Set a field in a JSON object.
Definition: json.c:404
static struct ast_sorcery * bucket_sorcery
Sorcery instance for all bucket operations.
Definition: bucket.c:85
const char * value
Value of the attribute.
Definition: bucket.h:51
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
struct ast_json * ast_json_string_create(const char *value)
Construct a JSON string from value.
Definition: json.c:268
const char * name
Name of the attribute.
Definition: bucket.h:49
#define ao2_ref(o, delta)
Definition: astobj2.h:464
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition: sorcery.c:2312
#define ao2_iterator_next(iter)
Definition: astobj2.h:1933
struct ao2_container * metadata
Container of metadata attributes about file.
Definition: bucket.h:93
Bucket metadata structure, AO2 key value pair.
Definition: bucket.h:47
struct ast_json * ast_json_object_create(void)
Create a new JSON object.
Definition: json.c:389
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
Definition: astobj2.h:1841
Abstract JSON element (object, array, string, int, ...).
struct ast_json * ast_sorcery_objectset_json_create(const struct ast_sorcery *sorcery, const void *object)
Create an object set in JSON format for an object.
Definition: sorcery.c:1565
enum queue_result id
Definition: app_queue.c:1507
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result
Create an iterator for a container.

◆ ast_bucket_file_metadata_callback()

void ast_bucket_file_metadata_callback ( struct ast_bucket_file file,
ao2_callback_fn  cb,
void *  arg 
)

Execute a callback function on the metadata associated with a file.

Since
14.0.0
Parameters
fileThe bucket file
cbAn ao2 callback function that will be called with each ast_bucket_metadata associated with file
argAn optional argument to pass to cb

Definition at line 364 of file bucket.c.

References ao2_callback, and ast_bucket_file::metadata.

Referenced by media_cache_item_sync_to_astdb().

365 {
366  ao2_callback(file->metadata, 0, cb, arg);
367 }
#define ao2_callback(c, flags, cb_fn, arg)
Definition: astobj2.h:1716
struct ao2_container * metadata
Container of metadata attributes about file.
Definition: bucket.h:93

◆ ast_bucket_file_metadata_get()

struct ast_bucket_metadata* ast_bucket_file_metadata_get ( struct ast_bucket_file file,
const char *  name 
)

Retrieve a metadata attribute from a file.

Parameters
fileThe bucket file
nameName of the attribute
Return values
non-NULLif found
NULLif not found
Note
The object is returned with reference count increased

Definition at line 359 of file bucket.c.

References ao2_find, ast_bucket_file::metadata, and OBJ_KEY.

Referenced by AST_TEST_DEFINE(), bucket_file_always_revalidate(), bucket_file_expired(), bucket_file_set_expiration(), bucket_file_update_path(), and bucket_http_wizard_is_stale().

360 {
361  return ao2_find(file->metadata, name, OBJ_KEY);
362 }
#define OBJ_KEY
Definition: astobj2.h:1155
struct ao2_container * metadata
Container of metadata attributes about file.
Definition: bucket.h:93
static const char name[]
Definition: cdr_mysql.c:74
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1756

◆ ast_bucket_file_metadata_set()

int ast_bucket_file_metadata_set ( struct ast_bucket_file file,
const char *  name,
const char *  value 
)

Set a metadata attribute on a file to a specific value.

Parameters
fileThe bucket file
nameName of the attribute
valueValue of the attribute
Return values
0success
-1failure
Note
This function will overwrite an existing attribute of the same name, unless an error occurs. If an error occurs the existing attribute is left alone.

Definition at line 334 of file bucket.c.

References ao2_cleanup, ao2_find, ao2_link, bucket_metadata_alloc(), ast_bucket_file::metadata, OBJ_KEY, OBJ_NODATA, OBJ_UNLINK, and RAII_VAR.

Referenced by ast_media_cache_create_or_update(), AST_TEST_DEFINE(), bucket_file_set_expiration(), bucket_file_update_path(), curl_header_callback(), and media_cache_item_populate_from_astdb().

335 {
337 
338  if (!metadata) {
339  return -1;
340  }
341 
343  ao2_link(file->metadata, metadata);
344 
345  return 0;
346 }
#define OBJ_KEY
Definition: astobj2.h:1155
int value
Definition: syslog.c:37
static struct ast_bucket_metadata * bucket_metadata_alloc(const char *name, const char *value)
Allocator for metadata attributes.
Definition: bucket.c:316
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
struct ao2_container * metadata
Container of metadata attributes about file.
Definition: bucket.h:93
static const char name[]
Definition: cdr_mysql.c:74
Bucket metadata structure, AO2 key value pair.
Definition: bucket.h:47
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1756
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
#define ao2_link(container, obj)
Definition: astobj2.h:1549

◆ ast_bucket_file_metadata_unset()

int ast_bucket_file_metadata_unset ( struct ast_bucket_file file,
const char *  name 
)

Unset a specific metadata attribute on a file.

Parameters
fileThe bucket file
nameName of the attribute
Return values
0success
-1failure

Definition at line 348 of file bucket.c.

References ao2_cleanup, ao2_find, ast_bucket_file::metadata, OBJ_KEY, OBJ_UNLINK, and RAII_VAR.

Referenced by AST_TEST_DEFINE().

349 {
351 
352  if (!metadata) {
353  return -1;
354  }
355 
356  return 0;
357 }
#define OBJ_KEY
Definition: astobj2.h:1155
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
struct ao2_container * metadata
Container of metadata attributes about file.
Definition: bucket.h:93
static const char name[]
Definition: cdr_mysql.c:74
Bucket metadata structure, AO2 key value pair.
Definition: bucket.h:47
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1756
#define ao2_cleanup(obj)
Definition: astobj2.h:1958

◆ ast_bucket_file_observer_add()

int ast_bucket_file_observer_add ( const struct ast_sorcery_observer callbacks)

Add an observer for bucket file creation and deletion operations.

Parameters
callbacksImplementation of the sorcery observer interface
Return values
0success
-1failure
Note
You must be ready to accept observer invocations before this function is called

Definition at line 829 of file bucket.c.

References ast_sorcery_observer_add().

830 {
831  return ast_sorcery_observer_add(bucket_sorcery, "file", callbacks);
832 }
static struct ast_sorcery * bucket_sorcery
Sorcery instance for all bucket operations.
Definition: bucket.c:85
int ast_sorcery_observer_add(const struct ast_sorcery *sorcery, const char *type, const struct ast_sorcery_observer *callbacks)
Add an observer to a specific object type.
Definition: sorcery.c:2386

◆ ast_bucket_file_observer_remove()

void ast_bucket_file_observer_remove ( const struct ast_sorcery_observer callbacks)

Remove an observer from bucket file creation and deletion.

Parameters
callbacksImplementation of the sorcery observer interface

Definition at line 834 of file bucket.c.

References ast_sorcery_observer_remove().

835 {
836  ast_sorcery_observer_remove(bucket_sorcery, "file", callbacks);
837 }
static struct ast_sorcery * bucket_sorcery
Sorcery instance for all bucket operations.
Definition: bucket.c:85
void ast_sorcery_observer_remove(const struct ast_sorcery *sorcery, const char *type, const struct ast_sorcery_observer *callbacks)
Remove an observer from a specific object type.
Definition: sorcery.c:2418

◆ ast_bucket_file_retrieve()

struct ast_bucket_file* ast_bucket_file_retrieve ( const char *  uri)

Retrieve a bucket file.

Parameters
uriComplete URI of the bucket file
Return values
non-NULLif found
NULLif not found
Note
The object is returned with reference count increased

Definition at line 815 of file bucket.c.

References ast_sorcery_retrieve_by_id(), ast_strlen_zero, and NULL.

Referenced by ast_media_cache_exists(), ast_media_cache_retrieve(), and AST_TEST_DEFINE().

816 {
817  if (ast_strlen_zero(uri)) {
818  return NULL;
819  }
820 
821  return ast_sorcery_retrieve_by_id(bucket_sorcery, "file", uri);
822 }
#define NULL
Definition: resample.c:96
#define ast_strlen_zero(foo)
Definition: strings.h:52
void * ast_sorcery_retrieve_by_id(const struct ast_sorcery *sorcery, const char *type, const char *id)
Retrieve an object using its unique identifier.
Definition: sorcery.c:1853
static struct ast_sorcery * bucket_sorcery
Sorcery instance for all bucket operations.
Definition: bucket.c:85

◆ ast_bucket_file_temporary_create()

int ast_bucket_file_temporary_create ( struct ast_bucket_file file)

Common file snapshot creation callback for creating a temporary file.

Parameters
filePointer to the file snapshot
Return values
0success
-1failure

Definition at line 899 of file bucket.c.

References ast_config_AST_CACHE_DIR, and ast_bucket_file::path.

Referenced by AST_TEST_DEFINE(), bucket_http_test_wizard_retrieve_id(), bucket_http_wizard_retrieve_id(), and load_module().

900 {
901  int fd;
902 
903  snprintf(file->path, sizeof(file->path), "%s/bucket-XXXXXX", ast_config_AST_CACHE_DIR);
904 
905  fd = mkstemp(file->path);
906  if (fd < 0) {
907  return -1;
908  }
909 
910  close(fd);
911  return 0;
912 }
const char * ast_config_AST_CACHE_DIR
Definition: options.c:150
char path[PATH_MAX]
Local path to this file.
Definition: bucket.h:95

◆ ast_bucket_file_temporary_destroy()

void ast_bucket_file_temporary_destroy ( struct ast_bucket_file file)

Common file snapshot destruction callback for deleting a temporary file.

Parameters
filePointer to the file snapshot

Definition at line 914 of file bucket.c.

References AO2_STRING_FIELD_CMP_FN, AO2_STRING_FIELD_HASH_FN, ast_strlen_zero, ast_bucket_scheme::name, and ast_bucket_file::path.

Referenced by load_module().

915 {
916  if (!ast_strlen_zero(file->path)) {
917  unlink(file->path);
918  }
919 }
#define ast_strlen_zero(foo)
Definition: strings.h:52
char path[PATH_MAX]
Local path to this file.
Definition: bucket.h:95

◆ ast_bucket_file_update()

int ast_bucket_file_update ( struct ast_bucket_file file)

Update an existing bucket file in backend storage.

Parameters
fileThe bucket file
Return values
0success
-1failure
Note
This operation will update both the actual content of the file and the metadata associated with it

Definition at line 839 of file bucket.c.

References ast_sorcery_update().

Referenced by AST_TEST_DEFINE().

840 {
841  return ast_sorcery_update(bucket_sorcery, file);
842 }
static struct ast_sorcery * bucket_sorcery
Sorcery instance for all bucket operations.
Definition: bucket.c:85
int ast_sorcery_update(const struct ast_sorcery *sorcery, void *object)
Update an object.
Definition: sorcery.c:2145

◆ ast_bucket_init()

int ast_bucket_init ( void  )

Initialize bucket support.

Return values
0success
-1failure

Definition at line 954 of file bucket.c.

References __ast_sorcery_wizard_register(), AO2_ALLOC_OPT_LOCK_RWLOCK, ao2_container_alloc_hash, ast_log, ast_register_cleanup(), ast_sorcery_apply_default, AST_SORCERY_APPLY_FAIL, ast_sorcery_object_field_register, ast_sorcery_object_field_register_custom, ast_sorcery_object_register, ast_sorcery_object_set_copy_handler(), ast_sorcery_open, bucket_alloc(), bucket_cleanup(), bucket_copy_handler(), bucket_file_alloc(), bucket_file_copy_handler(), FLDSET, LOG_ERROR, NULL, OPT_STRINGFIELD_T, SCHEME_BUCKETS, STRFLDSET, timeval_str2struct(), and timeval_struct2str().

Referenced by asterisk_daemon().

955 {
957 
959  ast_bucket_scheme_hash_fn, NULL, ast_bucket_scheme_cmp_fn);
960  if (!schemes) {
961  ast_log(LOG_ERROR, "Failed to create container for Bucket schemes\n");
962  return -1;
963  }
964 
966  ast_log(LOG_ERROR, "Failed to register sorcery wizard for 'bucket' intermediary\n");
967  return -1;
968  }
969 
971  ast_log(LOG_ERROR, "Failed to register sorcery wizard for 'file' intermediary\n");
972  return -1;
973  }
974 
975  if (!(bucket_sorcery = ast_sorcery_open())) {
976  ast_log(LOG_ERROR, "Failed to create sorcery instance for Bucket support\n");
977  return -1;
978  }
979 
981  ast_log(LOG_ERROR, "Failed to apply intermediary for 'bucket' object type in Bucket sorcery\n");
982  return -1;
983  }
984 
986  ast_log(LOG_ERROR, "Failed to register 'bucket' object type in Bucket sorcery\n");
987  return -1;
988  }
989 
990  ast_sorcery_object_field_register(bucket_sorcery, "bucket", "scheme", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_bucket, scheme));
994 
995  if (ast_sorcery_apply_default(bucket_sorcery, "file", "bucket_file", NULL) == AST_SORCERY_APPLY_FAIL) {
996  ast_log(LOG_ERROR, "Failed to apply intermediary for 'file' object type in Bucket sorcery\n");
997  return -1;
998  }
999 
1001  ast_log(LOG_ERROR, "Failed to register 'file' object type in Bucket sorcery\n");
1002  return -1;
1003  }
1004 
1009 
1010  return 0;
1011 }
#define SCHEME_BUCKETS
Number of buckets for the container of schemes.
Definition: bucket.c:79
int __ast_sorcery_wizard_register(const struct ast_sorcery_wizard *interface, struct ast_module *module)
Register a sorcery wizard.
Definition: sorcery.c:432
Bucket structure, contains other buckets and files.
Definition: bucket.h:57
static struct ast_sorcery_wizard bucket_wizard
Intermediary bucket wizard.
Definition: bucket.c:178
#define NULL
Definition: resample.c:96
static void bucket_cleanup(void)
Hashing function for scheme container.
Definition: bucket.c:928
static int bucket_file_copy_handler(const void *src, void *dst)
Definition: bucket.c:772
static int timeval_str2struct(const struct aco_option *opt, struct ast_variable *var, void *obj)
Custom handler for translating from a string timeval to actual structure.
Definition: bucket.c:940
static struct ast_sorcery * bucket_sorcery
Sorcery instance for all bucket operations.
Definition: bucket.c:85
#define ast_log
Definition: astobj2.c:42
#define ast_sorcery_object_field_register_custom(sorcery, type, name, default_val, config_handler, sorcery_handler, multiple_handler, flags,...)
Register a field within an object with custom handlers.
Definition: sorcery.h:1005
static void * bucket_file_alloc(const char *name)
Allocator for bucket files.
Definition: bucket.c:640
#define FLDSET(type,...)
Convert a struct and list of fields to an argument list of field offsets.
int ast_register_cleanup(void(*func)(void))
Register a function to be executed before Asterisk gracefully exits.
Definition: clicompat.c:19
Bucket file structure, contains reference to file and information about it.
Definition: bucket.h:78
#define ast_sorcery_object_register(sorcery, type, alloc, transform, apply)
Register an object type.
Definition: sorcery.h:838
#define LOG_ERROR
Definition: logger.h:285
#define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn)
Definition: astobj2.h:1310
static struct ast_sorcery_wizard bucket_file_wizard
Intermediary file wizard.
Definition: bucket.c:268
#define ast_sorcery_apply_default(sorcery, type, name, data)
Definition: sorcery.h:477
static int timeval_struct2str(const void *obj, const intptr_t *args, char **buf)
Custom handler for translating from an actual structure timeval to string.
Definition: bucket.c:947
#define STRFLDSET(type,...)
Convert a struct and a list of stringfield fields to an argument list of field offsets.
void ast_sorcery_object_set_copy_handler(struct ast_sorcery *sorcery, const char *type, sorcery_copy_handler copy)
Set the copy handler for an object type.
Definition: sorcery.c:1128
#define ast_sorcery_object_field_register(sorcery, type, name, default_val, opt_type, flags,...)
Register a field within an object.
Definition: sorcery.h:955
static void * bucket_alloc(const char *name)
Allocator for buckets.
Definition: bucket.c:402
static int bucket_copy_handler(const void *src, void *dst)
Definition: bucket.c:497
#define ast_sorcery_open()
Definition: sorcery.h:408
Type for default option handler for stringfields.
static struct ao2_container * schemes
Container of registered schemes.
Definition: bucket.c:88

◆ ast_bucket_is_stale()

int ast_bucket_is_stale ( struct ast_bucket bucket)

Retrieve whether or not the backing datastore views the bucket as stale.

Since
14.0.0

This function will ask whatever data storage backs the bucket's schema type if the current instance of the object is stale. It will not update the bucket object itself, as said objects are immutable. If the caller of this function would like to update the object, it should perform a retrieve operation.

Parameters
bucketThe bucket object to check
Return values
0if bucket is not stale
1if bucket is stale

Definition at line 524 of file bucket.c.

References ast_sorcery_is_stale().

Referenced by AST_TEST_DEFINE().

525 {
526  return ast_sorcery_is_stale(bucket_sorcery, bucket);
527 }
int ast_sorcery_is_stale(const struct ast_sorcery *sorcery, void *object)
Determine if a sorcery object is stale with respect to its backing datastore.
Definition: sorcery.c:2283
static struct ast_sorcery * bucket_sorcery
Sorcery instance for all bucket operations.
Definition: bucket.c:85

◆ ast_bucket_json()

struct ast_json* ast_bucket_json ( const struct ast_bucket bucket)

Get a JSON representation of a bucket.

Parameters
bucketThe specific bucket
Return values
non-NULLsuccess
NULLfailure
Note
The returned ast_json object must be unreferenced using ast_json_unref

Definition at line 544 of file bucket.c.

References ao2_iterator_destroy(), ao2_iterator_init(), ao2_iterator_next, ao2_ref, AO2_STRING_FIELD_CMP_FN, AO2_STRING_FIELD_HASH_FN, ast_json_array_append(), ast_json_array_create(), ast_json_object_set(), ast_json_ref(), ast_json_string_create(), ast_json_unref(), ast_sorcery_object_get_id(), ast_sorcery_objectset_json_create(), ast_bucket::buckets, ast_bucket::files, id, ast_bucket_scheme::name, NULL, and RAII_VAR.

Referenced by AST_TEST_DEFINE().

545 {
546  RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
547  struct ast_json *id, *files, *buckets;
548  struct ao2_iterator i;
549  char *uri;
550  int res = 0;
551 
553  if (!json) {
554  return NULL;
555  }
556 
558  if (!id) {
559  return NULL;
560  }
561 
562  if (ast_json_object_set(json, "id", id)) {
563  return NULL;
564  }
565 
566  buckets = ast_json_array_create();
567  if (!buckets) {
568  return NULL;
569  }
570 
571  if (ast_json_object_set(json, "buckets", buckets)) {
572  return NULL;
573  }
574 
575  i = ao2_iterator_init(bucket->buckets, 0);
576  for (; (uri = ao2_iterator_next(&i)); ao2_ref(uri, -1)) {
577  struct ast_json *bucket_uri = ast_json_string_create(uri);
578 
579  if (!bucket_uri || ast_json_array_append(buckets, bucket_uri)) {
580  res = -1;
581  ao2_ref(uri, -1);
582  break;
583  }
584  }
586 
587  if (res) {
588  return NULL;
589  }
590 
591  files = ast_json_array_create();
592  if (!files) {
593  return NULL;
594  }
595 
596  if (ast_json_object_set(json, "files", files)) {
597  return NULL;
598  }
599 
600  i = ao2_iterator_init(bucket->files, 0);
601  for (; (uri = ao2_iterator_next(&i)); ao2_ref(uri, -1)) {
602  struct ast_json *file_uri = ast_json_string_create(uri);
603 
604  if (!file_uri || ast_json_array_append(files, file_uri)) {
605  res = -1;
606  ao2_ref(uri, -1);
607  break;
608  }
609  }
611 
612  if (res) {
613  return NULL;
614  }
615 
616  ast_json_ref(json);
617  return json;
618 }
struct ast_json * ast_json_ref(struct ast_json *value)
Increase refcount on value.
Definition: json.c:67
struct ao2_container * files
Container of string URIs of files within this bucket.
Definition: bucket.h:74
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
void ao2_iterator_destroy(struct ao2_iterator *iter)
Destroy a container iterator.
#define NULL
Definition: resample.c:96
int ast_json_object_set(struct ast_json *object, const char *key, struct ast_json *value)
Set a field in a JSON object.
Definition: json.c:404
static struct ast_sorcery * bucket_sorcery
Sorcery instance for all bucket operations.
Definition: bucket.c:85
struct ao2_container * buckets
Container of string URIs of buckets within this bucket.
Definition: bucket.h:72
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
struct ast_json * ast_json_string_create(const char *value)
Construct a JSON string from value.
Definition: json.c:268
#define ao2_ref(o, delta)
Definition: astobj2.h:464
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition: sorcery.c:2312
struct ast_json * ast_json_array_create(void)
Create a empty JSON array.
Definition: json.c:352
int ast_json_array_append(struct ast_json *array, struct ast_json *value)
Append to an array.
Definition: json.c:368
#define ao2_iterator_next(iter)
Definition: astobj2.h:1933
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
Definition: astobj2.h:1841
Abstract JSON element (object, array, string, int, ...).
struct ast_json * ast_sorcery_objectset_json_create(const struct ast_sorcery *sorcery, const void *object)
Create an object set in JSON format for an object.
Definition: sorcery.c:1565
enum queue_result id
Definition: app_queue.c:1507
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result
Create an iterator for a container.

◆ ast_bucket_observer_add()

int ast_bucket_observer_add ( const struct ast_sorcery_observer callbacks)

Add an observer for bucket creation and deletion operations.

Parameters
callbacksImplementation of the sorcery observer interface
Return values
0success
-1failure
Note
You must be ready to accept observer invocations before this function is called

Definition at line 529 of file bucket.c.

References ast_sorcery_observer_add().

530 {
531  return ast_sorcery_observer_add(bucket_sorcery, "bucket", callbacks);
532 }
static struct ast_sorcery * bucket_sorcery
Sorcery instance for all bucket operations.
Definition: bucket.c:85
int ast_sorcery_observer_add(const struct ast_sorcery *sorcery, const char *type, const struct ast_sorcery_observer *callbacks)
Add an observer to a specific object type.
Definition: sorcery.c:2386

◆ ast_bucket_observer_remove()

void ast_bucket_observer_remove ( const struct ast_sorcery_observer callbacks)

Remove an observer from bucket creation and deletion.

Parameters
callbacksImplementation of the sorcery observer interface

Definition at line 534 of file bucket.c.

References ast_sorcery_observer_remove().

535 {
536  ast_sorcery_observer_remove(bucket_sorcery, "bucket", callbacks);
537 }
static struct ast_sorcery * bucket_sorcery
Sorcery instance for all bucket operations.
Definition: bucket.c:85
void ast_sorcery_observer_remove(const struct ast_sorcery *sorcery, const char *type, const struct ast_sorcery_observer *callbacks)
Remove an observer from a specific object type.
Definition: sorcery.c:2418

◆ ast_bucket_retrieve()

struct ast_bucket* ast_bucket_retrieve ( const char *  uri)

Retrieve information about a bucket.

Parameters
uriComplete URI of the bucket
Return values
non-NULLif found
NULLif not found
Note
The object is returned with reference count increased

Definition at line 515 of file bucket.c.

References ast_sorcery_retrieve_by_id(), ast_strlen_zero, and NULL.

Referenced by AST_TEST_DEFINE().

516 {
517  if (ast_strlen_zero(uri)) {
518  return NULL;
519  }
520 
521  return ast_sorcery_retrieve_by_id(bucket_sorcery, "bucket", uri);
522 }
#define NULL
Definition: resample.c:96
#define ast_strlen_zero(foo)
Definition: strings.h:52
void * ast_sorcery_retrieve_by_id(const struct ast_sorcery *sorcery, const char *type, const char *id)
Retrieve an object using its unique identifier.
Definition: sorcery.c:1853
static struct ast_sorcery * bucket_sorcery
Sorcery instance for all bucket operations.
Definition: bucket.c:85

◆ bucket_alloc()

static void* bucket_alloc ( const char *  name)
static

Allocator for buckets.

Definition at line 402 of file bucket.c.

References AO2_ALLOC_OPT_LOCK_NOLOCK, ao2_cleanup, AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT, ao2_container_alloc_rbtree, ao2_ref, ast_sorcery_generic_alloc(), ast_string_field_init, ast_bucket_scheme::bucket, bucket_destroy(), bucket_rbtree_str_sort_cmp(), NULL, and RAII_VAR.

Referenced by ast_bucket_init(), load_module(), and unload_module().

403 {
404  RAII_VAR(struct ast_bucket *, bucket, NULL, ao2_cleanup);
405 
406  bucket = ast_sorcery_generic_alloc(sizeof(*bucket), bucket_destroy);
407  if (!bucket) {
408  return NULL;
409  }
410 
411  if (ast_string_field_init(bucket, 128)) {
412  return NULL;
413  }
414 
417  if (!bucket->buckets) {
418  return NULL;
419  }
420 
423  if (!bucket->files) {
424  return NULL;
425  }
426 
427  ao2_ref(bucket, +1);
428  return bucket;
429 }
Bucket structure, contains other buckets and files.
Definition: bucket.h:57
#define NULL
Definition: resample.c:96
static int bucket_rbtree_str_sort_cmp(const void *obj_left, const void *obj_right, int flags)
Sorting function for red black tree string container.
Definition: bucket.c:382
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
#define ast_string_field_init(x, size)
Initialize a field pool and fields.
Definition: stringfields.h:353
static void bucket_destroy(void *obj)
Destructor for buckets.
Definition: bucket.c:371
#define ao2_ref(o, delta)
Definition: astobj2.h:464
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
#define ao2_container_alloc_rbtree(ao2_options, container_options, sort_fn, cmp_fn)
Definition: astobj2.h:1358
Reject objects with duplicate keys in container.
Definition: astobj2.h:1192
void * ast_sorcery_generic_alloc(size_t size, ao2_destructor_fn destructor)
Allocate a generic sorcery capable object.
Definition: sorcery.c:1728

◆ bucket_cleanup()

static void bucket_cleanup ( void  )
static

Hashing function for scheme container.

Comparison function for scheme container

Cleanup function for graceful shutdowns

Definition at line 928 of file bucket.c.

References ao2_cleanup, ast_sorcery_unref, ast_sorcery_wizard_unregister(), and NULL.

Referenced by ast_bucket_init().

929 {
932 
935 
937 }
static struct ast_sorcery_wizard bucket_wizard
Intermediary bucket wizard.
Definition: bucket.c:178
#define NULL
Definition: resample.c:96
#define ast_sorcery_unref(sorcery)
Decrease the reference count of a sorcery structure.
Definition: sorcery.h:1502
static struct ast_sorcery * bucket_sorcery
Sorcery instance for all bucket operations.
Definition: bucket.c:85
static struct ast_sorcery_wizard bucket_file_wizard
Intermediary file wizard.
Definition: bucket.c:268
int ast_sorcery_wizard_unregister(const struct ast_sorcery_wizard *interface)
Unregister a sorcery wizard.
Definition: sorcery.c:474
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
static struct ao2_container * schemes
Container of registered schemes.
Definition: bucket.c:88

◆ bucket_copy()

static int bucket_copy ( const char *  infile,
const char *  outfile 
)
static

Copy a file, shamelessly taken from file.c.

Definition at line 731 of file bucket.c.

References AST_FILE_MODE, ast_log, buf, errno, len(), and LOG_WARNING.

Referenced by ast_bucket_file_copy().

732 {
733  int ifd, ofd, len;
734  char buf[4096]; /* XXX make it lerger. */
735 
736  if ((ifd = open(infile, O_RDONLY)) < 0) {
737  ast_log(LOG_WARNING, "Unable to open %s in read-only mode, error: %s\n", infile, strerror(errno));
738  return -1;
739  }
740  if ((ofd = open(outfile, O_WRONLY | O_TRUNC | O_CREAT, AST_FILE_MODE)) < 0) {
741  ast_log(LOG_WARNING, "Unable to open %s in write-only mode, error: %s\n", outfile, strerror(errno));
742  close(ifd);
743  return -1;
744  }
745  while ( (len = read(ifd, buf, sizeof(buf)) ) ) {
746  int res;
747  if (len < 0) {
748  ast_log(LOG_WARNING, "Read failed on %s: %s\n", infile, strerror(errno));
749  break;
750  }
751  /* XXX handle partial writes */
752  res = write(ofd, buf, len);
753  if (res != len) {
754  ast_log(LOG_WARNING, "Write failed on %s (%d of %d): %s\n", outfile, res, len, strerror(errno));
755  len = -1; /* error marker */
756  break;
757  }
758  }
759  close(ifd);
760  close(ofd);
761  if (len < 0) {
762  unlink(outfile);
763  return -1; /* error */
764  }
765  return 0; /* success */
766 }
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define LOG_WARNING
Definition: logger.h:274
#define AST_FILE_MODE
Definition: asterisk.h:32
#define ast_log
Definition: astobj2.c:42
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
int errno

◆ bucket_copy_handler()

static int bucket_copy_handler ( const void *  src,
void *  dst 
)
static

Definition at line 497 of file bucket.c.

References ao2_bump, ast_string_field_set, ast_bucket::created, ast_bucket::modified, ast_bucket::scheme, and ast_bucket::scheme_impl.

Referenced by ast_bucket_init().

498 {
499  const struct ast_bucket *src_bucket = src;
500  struct ast_bucket *dst_bucket = dst;
501 
502  dst_bucket->scheme_impl = ao2_bump(src_bucket->scheme_impl);
503  ast_string_field_set(dst_bucket, scheme, src_bucket->scheme);
504  dst_bucket->created = src_bucket->created;
505  dst_bucket->modified = src_bucket->modified;
506 
507  return 0;
508 }
Bucket structure, contains other buckets and files.
Definition: bucket.h:57
struct timeval created
When this bucket was created.
Definition: bucket.h:68
#define ao2_bump(obj)
Definition: astobj2.h:491
struct ast_bucket_scheme * scheme_impl
Scheme implementation in use.
Definition: bucket.h:61
const ast_string_field scheme
Name of scheme in use.
Definition: bucket.h:66
struct timeval modified
When this bucket was last modified.
Definition: bucket.h:70
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.
Definition: stringfields.h:514

◆ bucket_destroy()

static void bucket_destroy ( void *  obj)
static

Destructor for buckets.

Definition at line 371 of file bucket.c.

References ao2_cleanup, ast_string_field_free_memory, ast_bucket_scheme::bucket, ast_bucket::buckets, ast_bucket::files, and ast_bucket::scheme_impl.

Referenced by bucket_alloc().

372 {
373  struct ast_bucket *bucket = obj;
374 
375  ao2_cleanup(bucket->scheme_impl);
377  ao2_cleanup(bucket->buckets);
378  ao2_cleanup(bucket->files);
379 }
struct ao2_container * files
Container of string URIs of files within this bucket.
Definition: bucket.h:74
Bucket structure, contains other buckets and files.
Definition: bucket.h:57
struct ao2_container * buckets
Container of string URIs of buckets within this bucket.
Definition: bucket.h:72
struct ast_bucket_scheme * scheme_impl
Scheme implementation in use.
Definition: bucket.h:61
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
#define ast_string_field_free_memory(x)
free all memory - to be called before destroying the object
Definition: stringfields.h:368

◆ bucket_file_alloc()

static void* bucket_file_alloc ( const char *  name)
static

Allocator for bucket files.

Definition at line 640 of file bucket.c.

References AO2_ALLOC_OPT_LOCK_NOLOCK, ao2_cleanup, ao2_container_alloc_hash, ao2_ref, ast_sorcery_generic_alloc(), ast_string_field_init, bucket_file_destroy(), ast_bucket_scheme::file, ast_bucket_file::metadata, METADATA_BUCKETS, NULL, and RAII_VAR.

Referenced by ast_bucket_init(), load_module(), and unload_module().

641 {
643 
645  if (!file) {
646  return NULL;
647  }
648 
649  if (ast_string_field_init(file, 128)) {
650  return NULL;
651  }
652 
654  ast_bucket_metadata_hash_fn, NULL, ast_bucket_metadata_cmp_fn);
655  if (!file->metadata) {
656  return NULL;
657  }
658 
659  ao2_ref(file, +1);
660  return file;
661 }
#define METADATA_BUCKETS
Number of buckets for the container of metadata in a file.
Definition: bucket.c:82
#define NULL
Definition: resample.c:96
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
#define ast_string_field_init(x, size)
Initialize a field pool and fields.
Definition: stringfields.h:353
#define ao2_ref(o, delta)
Definition: astobj2.h:464
Bucket file structure, contains reference to file and information about it.
Definition: bucket.h:78
#define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn)
Definition: astobj2.h:1310
static void bucket_file_destroy(void *obj)
Hashing function for file metadata.
Definition: bucket.c:627
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
void * ast_sorcery_generic_alloc(size_t size, ao2_destructor_fn destructor)
Allocate a generic sorcery capable object.
Definition: sorcery.c:1728

◆ bucket_file_copy_handler()

static int bucket_file_copy_handler ( const void *  src,
void *  dst 
)
static

Definition at line 772 of file bucket.c.

References ao2_bump, ao2_container_clone, ast_string_field_set, ast_bucket_file::created, ast_bucket_file::metadata, ast_bucket_file::modified, ast_bucket_file::path, ast_bucket_file::scheme, and ast_bucket_file::scheme_impl.

Referenced by ast_bucket_init().

773 {
774  const struct ast_bucket_file *src_file = src;
775  struct ast_bucket_file *dst_file = dst;
776 
777  dst_file->scheme_impl = ao2_bump(src_file->scheme_impl);
778  ast_string_field_set(dst_file, scheme, src_file->scheme);
779  dst_file->created = src_file->created;
780  dst_file->modified = src_file->modified;
781  strcpy(dst_file->path, src_file->path); /* safe */
782 
783  dst_file->metadata = ao2_container_clone(src_file->metadata, 0);
784  if (!dst_file->metadata) {
785  return -1;
786  }
787 
788  return 0;
789 }
#define ao2_container_clone(orig, flags)
Definition: astobj2.h:1430
struct ast_bucket_scheme * scheme_impl
Scheme implementation in use.
Definition: bucket.h:82
struct timeval modified
When this file was last modified.
Definition: bucket.h:91
#define ao2_bump(obj)
Definition: astobj2.h:491
struct timeval created
When this file was created.
Definition: bucket.h:89
Bucket file structure, contains reference to file and information about it.
Definition: bucket.h:78
const ast_string_field scheme
Name of scheme in use.
Definition: bucket.h:87
struct ao2_container * metadata
Container of metadata attributes about file.
Definition: bucket.h:93
char path[PATH_MAX]
Local path to this file.
Definition: bucket.h:95
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.
Definition: stringfields.h:514

◆ bucket_file_destroy()

static void bucket_file_destroy ( void *  obj)
static

Hashing function for file metadata.

Comparison function for file metadata

Destructor for bucket files

Definition at line 627 of file bucket.c.

References ao2_cleanup, ast_bucket_scheme::destroy, ast_bucket_scheme::file, ast_bucket_file::metadata, and ast_bucket_file::scheme_impl.

Referenced by bucket_file_alloc().

628 {
629  struct ast_bucket_file *file = obj;
630 
631  if (file->scheme_impl->destroy) {
632  file->scheme_impl->destroy(file);
633  }
634 
635  ao2_cleanup(file->scheme_impl);
636  ao2_cleanup(file->metadata);
637 }
bucket_file_destroy_cb destroy
Pointer to the file snapshot destruction callback.
Definition: bucket.c:99
struct ast_bucket_scheme * scheme_impl
Scheme implementation in use.
Definition: bucket.h:82
Bucket file structure, contains reference to file and information about it.
Definition: bucket.h:78
struct ao2_container * metadata
Container of metadata attributes about file.
Definition: bucket.h:93
#define ao2_cleanup(obj)
Definition: astobj2.h:1958

◆ bucket_file_wizard_create()

static int bucket_file_wizard_create ( const struct ast_sorcery sorcery,
void *  data,
void *  object 
)
static

Callback function for creating a bucket file.

Definition at line 187 of file bucket.c.

References ast_sorcery_wizard::create, ast_bucket_scheme::file, and ast_bucket_file::scheme_impl.

188 {
189  struct ast_bucket_file *file = object;
190 
191  return file->scheme_impl->file->create(sorcery, data, object);
192 }
struct ast_bucket_scheme * scheme_impl
Scheme implementation in use.
Definition: bucket.h:82
int(* create)(const struct ast_sorcery *sorcery, void *data, void *object)
Callback for creating an object.
Definition: sorcery.h:293
struct ast_sorcery_wizard * file
Wizard for files.
Definition: bucket.c:95
Bucket file structure, contains reference to file and information about it.
Definition: bucket.h:78

◆ bucket_file_wizard_delete()

static int bucket_file_wizard_delete ( const struct ast_sorcery sorcery,
void *  data,
void *  object 
)
static

Callback function for deleting a bucket file.

Definition at line 248 of file bucket.c.

References ast_sorcery_wizard::delete, ast_bucket_scheme::file, and ast_bucket_file::scheme_impl.

249 {
250  struct ast_bucket_file *file = object;
251 
252  return file->scheme_impl->file->delete(sorcery, data, object);
253 }
struct ast_bucket_scheme * scheme_impl
Scheme implementation in use.
Definition: bucket.h:82
int(* delete)(const struct ast_sorcery *sorcery, void *data, void *object)
Callback for deleting an object.
Definition: sorcery.h:319
struct ast_sorcery_wizard * file
Wizard for files.
Definition: bucket.c:95
Bucket file structure, contains reference to file and information about it.
Definition: bucket.h:78

◆ bucket_file_wizard_is_stale()

static int bucket_file_wizard_is_stale ( const struct ast_sorcery sorcery,
void *  data,
void *  object 
)
static

Callback function for determining if a bucket is stale.

Definition at line 256 of file bucket.c.

References ast_bucket_scheme::file, ast_sorcery_wizard::is_stale, and ast_bucket_file::scheme_impl.

257 {
258  struct ast_bucket_file *file = object;
259 
260  if (!file->scheme_impl->file->is_stale) {
261  return 0;
262  }
263 
264  return file->scheme_impl->file->is_stale(sorcery, data, object);
265 }
struct ast_bucket_scheme * scheme_impl
Scheme implementation in use.
Definition: bucket.h:82
struct ast_sorcery_wizard * file
Wizard for files.
Definition: bucket.c:95
Bucket file structure, contains reference to file and information about it.
Definition: bucket.h:78
int(* is_stale)(const struct ast_sorcery *sorcery, void *data, void *object)
Definition: sorcery.h:325

◆ bucket_file_wizard_retrieve()

static void* bucket_file_wizard_retrieve ( const struct ast_sorcery sorcery,
void *  data,
const char *  type,
const char *  id 
)
static

Callback function for retrieving a bucket file.

Definition at line 195 of file bucket.c.

References ao2_cleanup, ao2_find, ast_alloca, ast_copy_string(), ast_strdupa, len(), lock, NULL, OBJ_KEY, OBJ_NOLOCK, RAII_VAR, ast_bucket_file::scheme, SCOPED_AO2RDLOCK, state, and tmp().

197 {
198 #ifdef HAVE_URIPARSER
199  UriParserStateA state;
200  UriUriA uri;
201  size_t len;
202 #else
203  char *tmp = ast_strdupa(id);
204 #endif
205  char *uri_scheme;
208 
209 #ifdef HAVE_URIPARSER
210  state.uri = &uri;
211  if (uriParseUriA(&state, id) != URI_SUCCESS ||
212  !uri.scheme.first || !uri.scheme.afterLast) {
213  uriFreeUriMembersA(&uri);
214  return NULL;
215  }
216 
217  len = (uri.scheme.afterLast - uri.scheme.first) + 1;
218  uri_scheme = ast_alloca(len);
219  ast_copy_string(uri_scheme, uri.scheme.first, len);
220 
221  uriFreeUriMembersA(&uri);
222 #else
223  uri_scheme = tmp;
224  if (!(tmp = strchr(uri_scheme, ':'))) {
225  return NULL;
226  }
227  *tmp = '\0';
228 #endif
229 
230  scheme = ao2_find(schemes, uri_scheme, OBJ_KEY | OBJ_NOLOCK);
231 
232  if (!scheme) {
233  return NULL;
234  }
235 
236  return scheme->file->retrieve_id(sorcery, data, type, id);
237 }
static const char type[]
Definition: chan_ooh323.c:109
enum sip_cc_notify_state state
Definition: chan_sip.c:959
#define OBJ_KEY
Definition: astobj2.h:1155
static int tmp()
Definition: bt_open.c:389
Assume that the ao2_container is already locked.
Definition: astobj2.h:1067
#define NULL
Definition: resample.c:96
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
ast_mutex_t lock
Definition: app_meetme.c:1091
#define SCOPED_AO2RDLOCK(varname, obj)
scoped lock specialization for ao2 read locks.
Definition: lock.h:607
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:290
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
const ast_string_field scheme
Name of scheme in use.
Definition: bucket.h:87
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1756
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:401
Structure for available schemes.
Definition: bucket.c:91
static struct ao2_container * schemes
Container of registered schemes.
Definition: bucket.c:88

◆ bucket_file_wizard_update()

static int bucket_file_wizard_update ( const struct ast_sorcery sorcery,
void *  data,
void *  object 
)
static

Callback function for updating a bucket file.

Definition at line 240 of file bucket.c.

References ast_bucket_scheme::file, ast_bucket_file::scheme_impl, and ast_sorcery_wizard::update.

241 {
242  struct ast_bucket_file *file = object;
243 
244  return file->scheme_impl->file->update(sorcery, data, object);
245 }
int(* update)(const struct ast_sorcery *sorcery, void *data, void *object)
Callback for updating an object.
Definition: sorcery.h:316
struct ast_bucket_scheme * scheme_impl
Scheme implementation in use.
Definition: bucket.h:82
struct ast_sorcery_wizard * file
Wizard for files.
Definition: bucket.c:95
Bucket file structure, contains reference to file and information about it.
Definition: bucket.h:78

◆ bucket_metadata_alloc()

static struct ast_bucket_metadata* bucket_metadata_alloc ( const char *  name,
const char *  value 
)
static

Allocator for metadata attributes.

Definition at line 316 of file bucket.c.

References ao2_alloc, ast_bucket_metadata::data, ast_bucket_metadata::name, NULL, and ast_bucket_metadata::value.

Referenced by ast_bucket_file_metadata_set().

317 {
318  int name_len = strlen(name) + 1, value_len = strlen(value) + 1;
319  struct ast_bucket_metadata *metadata = ao2_alloc(sizeof(*metadata) + name_len + value_len, NULL);
320  char *dst;
321 
322  if (!metadata) {
323  return NULL;
324  }
325 
326  dst = metadata->data;
327  metadata->name = strcpy(dst, name);
328  dst += name_len;
329  metadata->value = strcpy(dst, value);
330 
331  return metadata;
332 }
#define NULL
Definition: resample.c:96
int value
Definition: syslog.c:37
char data[0]
Storage for the above name and value.
Definition: bucket.h:53
const char * value
Value of the attribute.
Definition: bucket.h:51
const char * name
Name of the attribute.
Definition: bucket.h:49
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:411
static const char name[]
Definition: cdr_mysql.c:74
Bucket metadata structure, AO2 key value pair.
Definition: bucket.h:47

◆ bucket_rbtree_str_sort_cmp()

static int bucket_rbtree_str_sort_cmp ( const void *  obj_left,
const void *  obj_right,
int  flags 
)
static

Sorting function for red black tree string container.

Definition at line 382 of file bucket.c.

References OBJ_KEY, OBJ_PARTIAL_KEY, and OBJ_POINTER.

Referenced by bucket_alloc().

383 {
384  const char *str_left = obj_left;
385  const char *str_right = obj_right;
386  int cmp = 0;
387 
388  switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
389  default:
390  case OBJ_POINTER:
391  case OBJ_KEY:
392  cmp = strcmp(str_left, str_right);
393  break;
394  case OBJ_PARTIAL_KEY:
395  cmp = strncmp(str_left, str_right, strlen(str_right));
396  break;
397  }
398  return cmp;
399 }
#define OBJ_KEY
Definition: astobj2.h:1155
#define OBJ_POINTER
Definition: astobj2.h:1154
#define OBJ_PARTIAL_KEY
Definition: astobj2.h:1156

◆ bucket_wizard_create()

static int bucket_wizard_create ( const struct ast_sorcery sorcery,
void *  data,
void *  object 
)
static

Callback function for creating a bucket.

Definition at line 105 of file bucket.c.

References ast_bucket_scheme::bucket, ast_sorcery_wizard::create, and ast_bucket::scheme_impl.

106 {
107  struct ast_bucket *bucket = object;
108 
109  return bucket->scheme_impl->bucket->create(sorcery, data, object);
110 }
struct ast_sorcery_wizard * bucket
Wizard for buckets.
Definition: bucket.c:93
Bucket structure, contains other buckets and files.
Definition: bucket.h:57
int(* create)(const struct ast_sorcery *sorcery, void *data, void *object)
Callback for creating an object.
Definition: sorcery.h:293
struct ast_bucket_scheme * scheme_impl
Scheme implementation in use.
Definition: bucket.h:61

◆ bucket_wizard_delete()

static int bucket_wizard_delete ( const struct ast_sorcery sorcery,
void *  data,
void *  object 
)
static

Callback function for deleting a bucket.

Definition at line 158 of file bucket.c.

References ast_bucket_scheme::bucket, ast_sorcery_wizard::delete, and ast_bucket::scheme_impl.

159 {
160  struct ast_bucket *bucket = object;
161 
162  return bucket->scheme_impl->bucket->delete(sorcery, data, object);
163 }
struct ast_sorcery_wizard * bucket
Wizard for buckets.
Definition: bucket.c:93
Bucket structure, contains other buckets and files.
Definition: bucket.h:57
int(* delete)(const struct ast_sorcery *sorcery, void *data, void *object)
Callback for deleting an object.
Definition: sorcery.h:319
struct ast_bucket_scheme * scheme_impl
Scheme implementation in use.
Definition: bucket.h:61

◆ bucket_wizard_is_stale()

static int bucket_wizard_is_stale ( const struct ast_sorcery sorcery,
void *  data,
void *  object 
)
static

Callback function for determining if a bucket is stale.

Definition at line 166 of file bucket.c.

References ast_bucket_scheme::bucket, ast_sorcery_wizard::is_stale, and ast_bucket::scheme_impl.

167 {
168  struct ast_bucket *bucket = object;
169 
170  if (!bucket->scheme_impl->bucket->is_stale) {
171  return 0;
172  }
173 
174  return bucket->scheme_impl->bucket->is_stale(sorcery, data, object);
175 }
struct ast_sorcery_wizard * bucket
Wizard for buckets.
Definition: bucket.c:93
Bucket structure, contains other buckets and files.
Definition: bucket.h:57
struct ast_bucket_scheme * scheme_impl
Scheme implementation in use.
Definition: bucket.h:61
int(* is_stale)(const struct ast_sorcery *sorcery, void *data, void *object)
Definition: sorcery.h:325

◆ bucket_wizard_retrieve()

static void* bucket_wizard_retrieve ( const struct ast_sorcery sorcery,
void *  data,
const char *  type,
const char *  id 
)
static

Callback function for retrieving a bucket.

Definition at line 113 of file bucket.c.

References ao2_cleanup, ao2_find, ast_alloca, ast_copy_string(), ast_strdupa, len(), lock, NULL, OBJ_KEY, OBJ_NOLOCK, RAII_VAR, ast_bucket::scheme, SCOPED_AO2RDLOCK, state, and tmp().

115 {
116 #ifdef HAVE_URIPARSER
117  UriParserStateA state;
118  UriUriA uri;
119  size_t len;
120 #else
121  char *tmp = ast_strdupa(id);
122 #endif
124  char *uri_scheme;
126 
127 #ifdef HAVE_URIPARSER
128  state.uri = &uri;
129  if (uriParseUriA(&state, id) != URI_SUCCESS ||
130  !uri.scheme.first || !uri.scheme.afterLast) {
131  uriFreeUriMembersA(&uri);
132  return NULL;
133  }
134 
135  len = (uri.scheme.afterLast - uri.scheme.first) + 1;
136  uri_scheme = ast_alloca(len);
137  ast_copy_string(uri_scheme, uri.scheme.first, len);
138 
139  uriFreeUriMembersA(&uri);
140 #else
141  uri_scheme = tmp;
142  if (!(tmp = strchr(uri_scheme, ':'))) {
143  return NULL;
144  }
145  *tmp = '\0';
146 #endif
147 
148  scheme = ao2_find(schemes, uri_scheme, OBJ_KEY | OBJ_NOLOCK);
149 
150  if (!scheme) {
151  return NULL;
152  }
153 
154  return scheme->bucket->retrieve_id(sorcery, data, type, id);
155 }
static const char type[]
Definition: chan_ooh323.c:109
enum sip_cc_notify_state state
Definition: chan_sip.c:959
#define OBJ_KEY
Definition: astobj2.h:1155
static int tmp()
Definition: bt_open.c:389
Assume that the ao2_container is already locked.
Definition: astobj2.h:1067
#define NULL
Definition: resample.c:96
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
ast_mutex_t lock
Definition: app_meetme.c:1091
#define SCOPED_AO2RDLOCK(varname, obj)
scoped lock specialization for ao2 read locks.
Definition: lock.h:607
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:290
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1756
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:401
Structure for available schemes.
Definition: bucket.c:91
const ast_string_field scheme
Name of scheme in use.
Definition: bucket.h:66
static struct ao2_container * schemes
Container of registered schemes.
Definition: bucket.c:88

◆ timeval_str2struct()

static int timeval_str2struct ( const struct aco_option opt,
struct ast_variable var,
void *  obj 
)
static

Custom handler for translating from a string timeval to actual structure.

Definition at line 940 of file bucket.c.

References aco_option_get_argument(), ast_get_timeval(), ast_tv(), NULL, and ast_variable::value.

Referenced by ast_bucket_init().

941 {
942  struct timeval *field = (struct timeval *)(obj + aco_option_get_argument(opt, 0));
943  return ast_get_timeval(var->value, field, ast_tv(0, 0), NULL);
944 }
#define NULL
Definition: resample.c:96
int ast_get_timeval(const char *src, struct timeval *tv, struct timeval _default, int *consumed)
get values from config variables.
Definition: main/utils.c:2171
struct timeval ast_tv(ast_time_t sec, ast_suseconds_t usec)
Returns a timeval from sec, usec.
Definition: time.h:226
intptr_t aco_option_get_argument(const struct aco_option *option, unsigned int position)
Get the offset position for an argument within a config option.

◆ timeval_struct2str()

static int timeval_struct2str ( const void *  obj,
const intptr_t *  args,
char **  buf 
)
static

Custom handler for translating from an actual structure timeval to string.

Definition at line 947 of file bucket.c.

References ast_asprintf.

Referenced by ast_bucket_init().

948 {
949  struct timeval *field = (struct timeval *)(obj + args[0]);
950  return (ast_asprintf(buf, "%lu.%06lu", (unsigned long)field->tv_sec, (unsigned long)field->tv_usec) < 0) ? -1 : 0;
951 }
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
const char * args
#define ast_asprintf(ret, fmt,...)
A wrapper for asprintf()
Definition: astmm.h:269

Variable Documentation

◆ bucket_file_wizard

struct ast_sorcery_wizard bucket_file_wizard
static

Intermediary file wizard.

Definition at line 268 of file bucket.c.

◆ bucket_sorcery

struct ast_sorcery* bucket_sorcery
static

Sorcery instance for all bucket operations.

Definition at line 85 of file bucket.c.

◆ bucket_wizard

struct ast_sorcery_wizard bucket_wizard
static

Intermediary bucket wizard.

Definition at line 178 of file bucket.c.

◆ schemes

struct ao2_container* schemes
static

Container of registered schemes.

Definition at line 88 of file bucket.c.