Asterisk - The Open Source Telephony Project GIT-master-754dea3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
Data Structures | Macros | Typedefs | Functions
bucket.h File Reference

Bucket File API. More...

#include "asterisk/sorcery.h"
Include dependency graph for bucket.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ast_bucket
 Bucket structure, contains other buckets and files. More...
 
struct  ast_bucket_file
 Bucket file structure, contains reference to file and information about it. More...
 
struct  ast_bucket_metadata
 Bucket metadata structure, AO2 key value pair. More...
 

Macros

#define ast_bucket_scheme_register(name, bucket, file, create_cb, destroy_cb)   __ast_bucket_scheme_register(name, bucket, file, create_cb, destroy_cb, AST_MODULE_SELF)
 Register support for a specific scheme. More...
 

Typedefs

typedef int(* bucket_file_create_cb) (struct ast_bucket_file *file)
 A callback function invoked when creating a file snapshot. More...
 
typedef void(* bucket_file_destroy_cb) (struct ast_bucket_file *file)
 A callback function invoked when destroying a file snapshot. 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...
 

Detailed Description

Bucket File API.

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

Definition in file bucket.h.

Macro Definition Documentation

◆ ast_bucket_scheme_register

#define ast_bucket_scheme_register (   name,
  bucket,
  file,
  create_cb,
  destroy_cb 
)    __ast_bucket_scheme_register(name, bucket, file, create_cb, destroy_cb, AST_MODULE_SELF)

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
Return values
0success
-1failure
Note
Once a scheme has been registered it can not be unregistered

Definition at line 137 of file bucket.h.

Typedef Documentation

◆ bucket_file_create_cb

typedef int(* bucket_file_create_cb) (struct ast_bucket_file *file)

A callback function invoked when creating a file snapshot.

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

Definition at line 106 of file bucket.h.

◆ bucket_file_destroy_cb

typedef void(* bucket_file_destroy_cb) (struct ast_bucket_file *file)

A callback function invoked when destroying a file snapshot.

Parameters
filePointer to the file snapshot

Definition at line 113 of file bucket.h.

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 247 of file bucket.c.

250{
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
277
278 ast_verb(5, "Registered bucket scheme '%s'\n", name);
279
281
282 return 0;
283}
ast_mutex_t lock
Definition: app_sla.c:337
#define OBJ_KEY
Definition: astobj2.h:1151
#define ao2_cleanup(obj)
Definition: astobj2.h:1934
#define ao2_link_flags(container, obj, flags)
Add an object to a container.
Definition: astobj2.h:1554
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1736
@ OBJ_NOLOCK
Assume that the ao2_container is already locked.
Definition: astobj2.h:1063
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:409
static struct ao2_container * schemes
Container of registered schemes.
Definition: bucket.c:58
static const char name[]
Definition: format_mp3.c:68
#define ast_verb(level,...)
#define SCOPED_AO2WRLOCK(varname, obj)
scoped lock specialization for ao2 write locks.
Definition: lock.h:618
#define ast_module_shutdown_ref(mod)
Prevent unload of the module before shutdown.
Definition: module.h:478
#define NULL
Definition: resample.c:96
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
Structure for available schemes.
Definition: bucket.c:61
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
int(* create)(const struct ast_sorcery *sorcery, void *data, void *object)
Callback for creating an object.
Definition: sorcery.h:293
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:941

References ao2_alloc, ao2_cleanup, ao2_find, ao2_link_flags, ast_module_shutdown_ref, ast_strlen_zero(), ast_verb, ast_sorcery_wizard::create, ast_sorcery_wizard::delete, make_ari_stubs::file, lock, ast_sorcery_wizard::module, name, NULL, OBJ_KEY, OBJ_NOLOCK, RAII_VAR, ast_sorcery_wizard::retrieve_id, schemes, and SCOPED_AO2WRLOCK.

◆ ast_bucket_alloc()

struct ast_bucket * ast_bucket_alloc ( const char *  uri)

Allocate a new bucket.

Parameters
uriComplete URI for the bucket
Return values
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 401 of file bucket.c.

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}
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:288
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
static struct ast_sorcery * bucket_sorcery
Sorcery instance for all bucket operations.
Definition: bucket.c:55
enum cc_state state
Definition: ccss.c:399
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
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.
Definition: stringfields.h:521
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
Bucket structure, contains other buckets and files.
Definition: bucket.h:57
const ast_string_field scheme
Definition: bucket.h:66
struct ast_bucket_scheme * scheme_impl
Scheme implementation in use.
Definition: bucket.h:61

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

Referenced by AST_TEST_DEFINE(), and bucket_test_wizard_retrieve_id().

◆ 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
bucketThe 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 480 of file bucket.c.

481{
482 return ast_sorcery_copy(bucket_sorcery, bucket);
483}
void * ast_sorcery_copy(const struct ast_sorcery *sorcery, const void *object)
Create a copy of an object.
Definition: sorcery.c:1778

References ast_sorcery_copy(), and bucket_sorcery.

Referenced by AST_TEST_DEFINE().

◆ 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 458 of file bucket.c.

459{
460 return ast_sorcery_create(bucket_sorcery, bucket);
461}
int ast_sorcery_create(const struct ast_sorcery *sorcery, void *object)
Create and potentially persist an object using an available wizard.
Definition: sorcery.c:2062

References ast_sorcery_create(), and bucket_sorcery.

Referenced by AST_TEST_DEFINE().

◆ 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 509 of file bucket.c.

510{
511 return ast_sorcery_delete(bucket_sorcery, bucket);
512}
int ast_sorcery_delete(const struct ast_sorcery *sorcery, void *object)
Delete an object.
Definition: sorcery.c:2238

References ast_sorcery_delete(), and bucket_sorcery.

Referenced by AST_TEST_DEFINE().

◆ 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
Return values
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 633 of file bucket.c.

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}
Bucket file structure, contains reference to file and information about it.
Definition: bucket.h:78
const ast_string_field scheme
Definition: bucket.h:87

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

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().

◆ 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 780 of file bucket.c.

781{
783}

References ast_sorcery_copy(), bucket_sorcery, and make_ari_stubs::file.

Referenced by ast_media_cache_create_or_update(), and AST_TEST_DEFINE().

◆ 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 761 of file bucket.c.

762{
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}
static int copy(char *infile, char *outfile)
Utility function to copy a file.
#define ao2_container_clone(orig, flags)
Create a clone/copy of the given container.
Definition: astobj2.h:1419
struct ast_bucket_file * ast_bucket_file_alloc(const char *uri)
Allocate a new bucket file.
Definition: bucket.c:633
static int bucket_copy(const char *infile, const char *outfile)
Copy a file, shamelessly taken from file.c.
Definition: bucket.c:701

References ao2_cleanup, ao2_container_clone, ao2_ref, ast_bucket_file_alloc(), bucket_copy(), copy(), make_ari_stubs::file, NULL, and RAII_VAR.

Referenced by AST_TEST_DEFINE().

◆ 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 695 of file bucket.c.

696{
698}

References ast_sorcery_create(), bucket_sorcery, and make_ari_stubs::file.

Referenced by ast_media_cache_create_or_update(), and AST_TEST_DEFINE().

◆ 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 814 of file bucket.c.

815{
817}

References ast_sorcery_delete(), bucket_sorcery, and make_ari_stubs::file.

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

◆ 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
fileThe bucket file object to check
Return values
0if file is not stale
1if file is stale

Definition at line 794 of file bucket.c.

795{
797}
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:2288

References ast_sorcery_is_stale(), bucket_sorcery, and make_ari_stubs::file.

Referenced by ast_media_cache_retrieve(), and AST_TEST_DEFINE().

◆ 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 819 of file bucket.c.

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
828 if (!json) {
829 return NULL;
830 }
831
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 }
860
861 if (res) {
862 return NULL;
863 }
864
865 ast_json_ref(json);
866 return json;
867}
enum queue_result id
Definition: app_queue.c:1808
#define ao2_iterator_next(iter)
Definition: astobj2.h:1911
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result
Create an iterator for a container.
void ao2_iterator_destroy(struct ao2_iterator *iter)
Destroy a container iterator.
struct ast_json * ast_json_string_create(const char *value)
Construct a JSON string from value.
Definition: json.c:278
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
struct ast_json * ast_json_object_create(void)
Create a new JSON object.
Definition: json.c:399
struct ast_json * ast_json_ref(struct ast_json *value)
Increase refcount on value.
Definition: json.c:67
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:414
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition: sorcery.c:2317
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
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
Definition: astobj2.h:1821
Bucket metadata structure, AO2 key value pair.
Definition: bucket.h:47
const char * value
Value of the attribute.
Definition: bucket.h:51
const char * name
Name of the attribute.
Definition: bucket.h:49
Abstract JSON element (object, array, string, int, ...).
int value
Definition: syslog.c:37

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(), bucket_sorcery, make_ari_stubs::file, id, ast_bucket_metadata::name, NULL, RAII_VAR, ast_bucket_metadata::value, and value.

Referenced by AST_TEST_DEFINE().

◆ 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 334 of file bucket.c.

335{
336 ao2_callback(file->metadata, 0, cb, arg);
337}
#define ao2_callback(c, flags, cb_fn, arg)
ao2_callback() is a generic function that applies cb_fn() to all objects in a container,...
Definition: astobj2.h:1693

References ao2_callback, and make_ari_stubs::file.

Referenced by media_cache_item_sync_to_astdb().

◆ 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 329 of file bucket.c.

330{
331 return ao2_find(file->metadata, name, OBJ_KEY);
332}

References ao2_find, make_ari_stubs::file, name, and OBJ_KEY.

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

◆ 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 304 of file bucket.c.

305{
307
308 if (!metadata) {
309 return -1;
310 }
311
313 ao2_link(file->metadata, metadata);
314
315 return 0;
316}
#define ao2_link(container, obj)
Add an object to a container.
Definition: astobj2.h:1532
@ OBJ_NODATA
Definition: astobj2.h:1044
@ OBJ_UNLINK
Definition: astobj2.h:1039
static struct ast_bucket_metadata * bucket_metadata_alloc(const char *name, const char *value)
Allocator for metadata attributes.
Definition: bucket.c:286

References ao2_cleanup, ao2_find, ao2_link, bucket_metadata_alloc(), make_ari_stubs::file, name, OBJ_KEY, OBJ_NODATA, OBJ_UNLINK, RAII_VAR, and value.

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

◆ 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 318 of file bucket.c.

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}

References ao2_cleanup, ao2_find, make_ari_stubs::file, name, OBJ_KEY, OBJ_UNLINK, and RAII_VAR.

Referenced by AST_TEST_DEFINE().

◆ 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 799 of file bucket.c.

800{
802}
struct @471 callbacks
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:2391

References ast_sorcery_observer_add(), bucket_sorcery, and callbacks.

◆ 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 804 of file bucket.c.

805{
807}
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:2423

References ast_sorcery_observer_remove(), bucket_sorcery, and callbacks.

◆ 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 785 of file bucket.c.

786{
787 if (ast_strlen_zero(uri)) {
788 return NULL;
789 }
790
791 return ast_sorcery_retrieve_by_id(bucket_sorcery, "file", uri);
792}
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

References ast_sorcery_retrieve_by_id(), ast_strlen_zero(), bucket_sorcery, and NULL.

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

◆ 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 869 of file bucket.c.

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}
const char * ast_config_AST_CACHE_DIR
Definition: options.c:150

References ast_config_AST_CACHE_DIR, and make_ari_stubs::file.

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

◆ 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 884 of file bucket.c.

885{
886 if (!ast_strlen_zero(file->path)) {
887 unlink(file->path);
888 }
889}

References ast_strlen_zero(), and make_ari_stubs::file.

Referenced by load_module().

◆ 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 809 of file bucket.c.

810{
812}
int ast_sorcery_update(const struct ast_sorcery *sorcery, void *object)
Update an object.
Definition: sorcery.c:2150

References ast_sorcery_update(), bucket_sorcery, and make_ari_stubs::file.

Referenced by AST_TEST_DEFINE().

◆ ast_bucket_init()

int ast_bucket_init ( void  )

Initialize bucket support.

Return values
0success
-1failure

Definition at line 924 of file bucket.c.

925{
927
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
936 ast_log(LOG_ERROR, "Failed to register sorcery wizard for 'bucket' intermediary\n");
937 return -1;
938 }
939
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
951 ast_log(LOG_ERROR, "Failed to apply intermediary for 'bucket' object type in Bucket sorcery\n");
952 return -1;
953 }
954
956 ast_log(LOG_ERROR, "Failed to register 'bucket' object type in Bucket sorcery\n");
957 return -1;
958 }
959
964
966 ast_log(LOG_ERROR, "Failed to apply intermediary for 'file' object type in Bucket sorcery\n");
967 return -1;
968 }
969
971 ast_log(LOG_ERROR, "Failed to register 'file' object type in Bucket sorcery\n");
972 return -1;
973 }
974
979
980 return 0;
981}
int ast_register_cleanup(void(*func)(void))
Register a function to be executed before Asterisk gracefully exits.
Definition: clicompat.c:19
#define ast_log
Definition: astobj2.c:42
@ AO2_ALLOC_OPT_LOCK_RWLOCK
Definition: astobj2.h:365
#define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn)
Allocate and initialize a hash container with the desired number of buckets.
Definition: astobj2.h:1303
static void bucket_cleanup(void)
Hashing function for scheme container.
Definition: bucket.c:898
static void * bucket_file_alloc(const char *name)
Allocator for bucket files.
Definition: bucket.c:610
static struct ast_sorcery_wizard bucket_file_wizard
Intermediary file wizard.
Definition: bucket.c:238
static struct ast_sorcery_wizard bucket_wizard
Intermediary bucket wizard.
Definition: bucket.c:148
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:910
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:917
static int bucket_copy_handler(const void *src, void *dst)
Definition: bucket.c:467
static void * bucket_alloc(const char *name)
Allocator for buckets.
Definition: bucket.c:372
static int bucket_file_copy_handler(const void *src, void *dst)
Definition: bucket.c:742
#define SCHEME_BUCKETS
Number of buckets for the container of schemes.
Definition: bucket.c:49
#define STRFLDSET(type,...)
Convert a struct and a list of stringfield fields to an argument list of field offsets.
#define FLDSET(type,...)
Convert a struct and list of fields to an argument list of field offsets.
@ OPT_STRINGFIELD_T
Type for default option handler for stringfields.
#define LOG_ERROR
#define ast_sorcery_object_field_register_nodoc(sorcery, type, name, default_val, opt_type, flags,...)
Register a field within an object without documentation.
Definition: sorcery.h:987
#define ast_sorcery_object_field_register_custom_nodoc(sorcery, type, name, default_val, config_handler, sorcery_handler, multiple_handler, flags,...)
Register a field within an object with custom handlers without documentation.
Definition: sorcery.h:1041
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
int __ast_sorcery_wizard_register(const struct ast_sorcery_wizard *interface, struct ast_module *module)
Register a sorcery wizard.
Definition: sorcery.c:432
#define ast_sorcery_internal_object_register(sorcery, type, alloc, transform, apply)
Register an internal, hidden object type.
Definition: sorcery.h:867
#define ast_sorcery_apply_default(sorcery, type, name, data)
Definition: sorcery.h:476
#define ast_sorcery_open()
Open a new sorcery structure.
Definition: sorcery.h:406
@ AST_SORCERY_APPLY_FAIL
Definition: sorcery.h:425

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_internal_object_register, ast_sorcery_object_field_register_custom_nodoc, ast_sorcery_object_field_register_nodoc, ast_sorcery_object_set_copy_handler(), ast_sorcery_open, bucket_alloc(), bucket_cleanup(), bucket_copy_handler(), bucket_file_alloc(), bucket_file_copy_handler(), bucket_file_wizard, bucket_sorcery, bucket_wizard, FLDSET, LOG_ERROR, NULL, OPT_STRINGFIELD_T, SCHEME_BUCKETS, schemes, STRFLDSET, timeval_str2struct(), and timeval_struct2str().

Referenced by asterisk_daemon().

◆ 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 494 of file bucket.c.

495{
496 return ast_sorcery_is_stale(bucket_sorcery, bucket);
497}

References ast_sorcery_is_stale(), and bucket_sorcery.

Referenced by AST_TEST_DEFINE().

◆ 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 514 of file bucket.c.

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
523 if (!json) {
524 return NULL;
525 }
526
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 }
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 }
581
582 if (res) {
583 return NULL;
584 }
585
586 ast_json_ref(json);
587 return json;
588}
int ast_json_array_append(struct ast_json *array, struct ast_json *value)
Append to an array.
Definition: json.c:378
struct ast_json * ast_json_array_create(void)
Create a empty JSON array.
Definition: json.c:362
struct ao2_container * buckets
Container of string URIs of buckets within this bucket.
Definition: bucket.h:72
struct ao2_container * files
Container of string URIs of files within this bucket.
Definition: bucket.h:74

References ao2_iterator_destroy(), ao2_iterator_init(), ao2_iterator_next, ao2_ref, 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(), bucket_sorcery, ast_bucket::buckets, ast_bucket::files, id, NULL, and RAII_VAR.

Referenced by AST_TEST_DEFINE().

◆ 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 499 of file bucket.c.

500{
502}

References ast_sorcery_observer_add(), bucket_sorcery, and callbacks.

◆ 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 504 of file bucket.c.

References ast_sorcery_observer_remove(), bucket_sorcery, and callbacks.

◆ 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 485 of file bucket.c.

486{
487 if (ast_strlen_zero(uri)) {
488 return NULL;
489 }
490
491 return ast_sorcery_retrieve_by_id(bucket_sorcery, "bucket", uri);
492}

References ast_sorcery_retrieve_by_id(), ast_strlen_zero(), bucket_sorcery, and NULL.

Referenced by AST_TEST_DEFINE().