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

Defines

#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_info ? ast_module_info->self : NULL)
 Register support for a specific scheme.

Typedefs

typedef int(* bucket_file_create_cb )(struct ast_bucket_file *file)
 A callback function invoked when creating a file snapshot.
typedef void(* bucket_file_destroy_cb )(struct ast_bucket_file *file)
 A callback function invoked when destroying a file snapshot.

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.
struct ast_bucketast_bucket_alloc (const char *uri)
 Allocate a new bucket.
int ast_bucket_create (struct ast_bucket *bucket)
 Create a new bucket in backend storage.
int ast_bucket_delete (struct ast_bucket *bucket)
 Delete a bucket from backend storage.
struct ast_bucket_fileast_bucket_file_alloc (const char *uri)
 Allocate a new bucket file.
struct ast_bucket_fileast_bucket_file_copy (struct ast_bucket_file *file, const char *uri)
 Copy a bucket file to a new URI.
int ast_bucket_file_create (struct ast_bucket_file *file)
 Create a new bucket file in backend storage.
int ast_bucket_file_delete (struct ast_bucket_file *file)
 Delete a bucket file from backend storage.
struct ast_jsonast_bucket_file_json (const struct ast_bucket_file *file)
 Get a JSON representation of a bucket file.
struct ast_bucket_metadataast_bucket_file_metadata_get (struct ast_bucket_file *file, const char *name)
 Retrieve a metadata attribute from a file.
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.
int ast_bucket_file_metadata_unset (struct ast_bucket_file *file, const char *name)
 Unset a specific metadata attribute on a file.
int ast_bucket_file_observer_add (const struct ast_sorcery_observer *callbacks)
 Add an observer for bucket file creation and deletion operations.
void ast_bucket_file_observer_remove (const struct ast_sorcery_observer *callbacks)
 Remove an observer from bucket file creation and deletion.
struct ast_bucket_fileast_bucket_file_retrieve (const char *uri)
 Retrieve a bucket file.
int ast_bucket_file_temporary_create (struct ast_bucket_file *file)
 Common file snapshot creation callback for creating a temporary file.
void ast_bucket_file_temporary_destroy (struct ast_bucket_file *file)
 Common file snapshot destruction callback for deleting a temporary file.
int ast_bucket_file_update (struct ast_bucket_file *file)
 Update an existing bucket file in backend storage.
int ast_bucket_init (void)
 Initialize bucket support.
struct ast_jsonast_bucket_json (const struct ast_bucket *bucket)
 Get a JSON representation of a bucket.
int ast_bucket_observer_add (const struct ast_sorcery_observer *callbacks)
 Add an observer for bucket creation and deletion operations.
void ast_bucket_observer_remove (const struct ast_sorcery_observer *callbacks)
 Remove an observer from bucket creation and deletion.
struct ast_bucketast_bucket_retrieve (const char *uri)
 Retrieve information about a bucket.


Detailed Description

Bucket File API.

Author:
Joshua Colp <jcolp@digium.com> Bucket File API

Definition in file bucket.h.


Define Documentation

#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_info ? ast_module_info->self : NULL)

Register support for a specific scheme.

Parameters:
name Name of the scheme, used to find based on scheme in URIs
bucket Sorcery wizard used for buckets
file Sorcery wizard used for files
create_cb Required file snapshot creation callback
destroy_cb Optional file snapshot destruction callback
Return values:
0 success
-1 failure
Note:
Once a scheme has been registered it can not be unregistered

Definition at line 137 of file bucket.h.

Referenced by AST_TEST_DEFINE(), and load_module().


Typedef Documentation

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

A callback function invoked when creating a file snapshot.

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

Definition at line 106 of file bucket.h.

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

A callback function invoked when destroying a file snapshot.

Parameters:
file Pointer to the file snapshot

Definition at line 113 of file bucket.h.


Function Documentation

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:
name Name of the scheme, used to find based on scheme in URIs
bucket Sorcery wizard used for buckets
file Sorcery wizard used for files
create_cb Required file snapshot creation callback
destroy_cb Optional file snapshot destruction callback
module The module which implements this scheme
Return values:
0 success
-1 failure
Note:
Once a scheme has been registered it can not be unregistered

Definition at line 252 of file bucket.c.

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, lock, NULL, OBJ_KEY, OBJ_NOLOCK, RAII_VAR, ast_sorcery_wizard::retrieve_id, and SCOPED_AO2WRLOCK.

00255 {
00256    SCOPED_AO2WRLOCK(lock, schemes);
00257    RAII_VAR(struct ast_bucket_scheme *, scheme, NULL, ao2_cleanup);
00258 
00259    if (ast_strlen_zero(name) || !bucket || !file ||
00260        !bucket->create || !bucket->delete || !bucket->retrieve_id ||
00261        !create_cb) {
00262       return -1;
00263    }
00264 
00265    scheme = ao2_find(schemes, name, OBJ_KEY | OBJ_NOLOCK);
00266    if (scheme) {
00267       return -1;
00268    }
00269 
00270    scheme = ao2_alloc(sizeof(*scheme) + strlen(name) + 1, NULL);
00271    if (!scheme) {
00272       return -1;
00273    }
00274 
00275    strcpy(scheme->name, name);
00276    scheme->bucket = bucket;
00277    scheme->file = file;
00278    scheme->create = create_cb;
00279    scheme->destroy = destroy_cb;
00280 
00281    ao2_link_flags(schemes, scheme, OBJ_NOLOCK);
00282 
00283    ast_verb(2, "Registered bucket scheme '%s'\n", name);
00284 
00285    ast_module_shutdown_ref(module);
00286 
00287    return 0;
00288 }

struct ast_bucket* ast_bucket_alloc ( const char *  uri  )  [read]

Allocate a new bucket.

Parameters:
uri Complete URI for the bucket
non-NULL success
NULL failure
Note:
This only creates a local bucket object, to persist in backend storage you must call ast_bucket_create

Definition at line 400 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, len(), NULL, OBJ_KEY, RAII_VAR, ast_bucket::scheme, ast_bucket::scheme_impl, and tmp().

Referenced by AST_TEST_DEFINE(), and bucket_test_wizard_retrieve_id().

00401 {
00402 #ifdef HAVE_URIPARSER
00403    UriParserStateA state;
00404    UriUriA full_uri;
00405    size_t len;
00406 #else
00407    char *tmp = ast_strdupa(uri);
00408 #endif
00409    char *uri_scheme;
00410    RAII_VAR(struct ast_bucket_scheme *, scheme, NULL, ao2_cleanup);
00411    struct ast_bucket *bucket;
00412 
00413    if (ast_strlen_zero(uri)) {
00414       return NULL;
00415    }
00416 
00417 #ifdef HAVE_URIPARSER
00418    state.uri = &full_uri;
00419    if (uriParseUriA(&state, uri) != URI_SUCCESS ||
00420       !full_uri.scheme.first || !full_uri.scheme.afterLast ||
00421       !full_uri.pathTail) {
00422       uriFreeUriMembersA(&full_uri);
00423       return NULL;
00424    }
00425 
00426    len = (full_uri.scheme.afterLast - full_uri.scheme.first) + 1;
00427    uri_scheme = ast_alloca(len);
00428    ast_copy_string(uri_scheme, full_uri.scheme.first, len);
00429 
00430    uriFreeUriMembersA(&full_uri);
00431 #else
00432    uri_scheme = tmp;
00433    if (!(tmp = strchr(uri_scheme, ':'))) {
00434       return NULL;
00435    }
00436    *tmp = '\0';
00437 #endif
00438 
00439    scheme = ao2_find(schemes, uri_scheme, OBJ_KEY);
00440    if (!scheme) {
00441       return NULL;
00442    }
00443 
00444    bucket = ast_sorcery_alloc(bucket_sorcery, "bucket", uri);
00445    if (!bucket) {
00446       return NULL;
00447    }
00448 
00449    ao2_ref(scheme, +1);
00450    bucket->scheme_impl = scheme;
00451 
00452    ast_string_field_set(bucket, scheme, uri_scheme);
00453 
00454    return bucket;
00455 }

int ast_bucket_create ( struct ast_bucket bucket  ) 

Create a new bucket in backend storage.

Parameters:
bucket The bucket
Return values:
0 success
-1 failure

Definition at line 457 of file bucket.c.

References ast_sorcery_create().

Referenced by AST_TEST_DEFINE().

00458 {
00459    return ast_sorcery_create(bucket_sorcery, bucket);
00460 }

int ast_bucket_delete ( struct ast_bucket bucket  ) 

Delete a bucket from backend storage.

Parameters:
bucket The bucket
Return values:
0 success
-1 failure

Definition at line 481 of file bucket.c.

References ast_sorcery_delete().

Referenced by AST_TEST_DEFINE().

00482 {
00483    return ast_sorcery_delete(bucket_sorcery, bucket);
00484 }

struct ast_bucket_file* ast_bucket_file_alloc ( const char *  uri  )  [read]

Allocate a new bucket file.

Parameters:
uri Complete URI for the bucket file
non-NULL success
NULL failure
Note:
This only creates a local bucket file object, to persist in backend storage you must call ast_bucket_file_create

Definition at line 628 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, len(), NULL, OBJ_KEY, RAII_VAR, ast_bucket_file::scheme, ast_bucket_file::scheme_impl, and tmp().

Referenced by ast_bucket_file_copy(), AST_TEST_DEFINE(), and bucket_test_wizard_retrieve_id().

00629 {
00630 #ifdef HAVE_URIPARSER
00631    UriParserStateA state;
00632    UriUriA full_uri;
00633    size_t len;
00634 #else
00635    char *tmp = ast_strdupa(uri);
00636 #endif
00637    char *uri_scheme;
00638    RAII_VAR(struct ast_bucket_scheme *, scheme, NULL, ao2_cleanup);
00639    struct ast_bucket_file *file;
00640 
00641    if (ast_strlen_zero(uri)) {
00642       return NULL;
00643    }
00644 
00645 #ifdef HAVE_URIPARSER
00646    state.uri = &full_uri;
00647    if (uriParseUriA(&state, uri) != URI_SUCCESS ||
00648       !full_uri.scheme.first || !full_uri.scheme.afterLast ||
00649       !full_uri.pathTail) {
00650       uriFreeUriMembersA(&full_uri);
00651       return NULL;
00652    }
00653 
00654    len = (full_uri.scheme.afterLast - full_uri.scheme.first) + 1;
00655    uri_scheme = ast_alloca(len);
00656    ast_copy_string(uri_scheme, full_uri.scheme.first, len);
00657 
00658    uriFreeUriMembersA(&full_uri);
00659 #else
00660    uri_scheme = tmp;
00661    if (!(tmp = strchr(uri_scheme, ':'))) {
00662       return NULL;
00663    }
00664    *tmp = '\0';
00665 #endif
00666 
00667    scheme = ao2_find(schemes, uri_scheme, OBJ_KEY);
00668    if (!scheme) {
00669       return NULL;
00670    }
00671 
00672    file = ast_sorcery_alloc(bucket_sorcery, "file", uri);
00673    if (!file) {
00674       return NULL;
00675    }
00676 
00677    ao2_ref(scheme, +1);
00678    file->scheme_impl = scheme;
00679 
00680    ast_string_field_set(file, scheme, uri_scheme);
00681 
00682    if (scheme->create(file)) {
00683       ao2_ref(file, -1);
00684       return NULL;
00685    }
00686 
00687    return file;
00688 }

struct ast_bucket_file* ast_bucket_file_copy ( struct ast_bucket_file file,
const char *  uri 
) [read]

Copy a bucket file to a new URI.

Parameters:
file The source bucket file
uri The new URI
Return values:
non-NULL success
NULL failure
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 733 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().

00734 {
00735    RAII_VAR(struct ast_bucket_file *, copy, ast_bucket_file_alloc(uri), ao2_cleanup);
00736 
00737    if (!copy) {
00738       return NULL;
00739    }
00740 
00741    ao2_cleanup(copy->metadata);
00742    copy->metadata = ao2_container_clone(file->metadata, 0);
00743    if (!copy->metadata ||
00744       bucket_copy(file->path, copy->path)) {
00745       return NULL;
00746    }
00747 
00748    ao2_ref(copy, +1);
00749    return copy;
00750 }

int ast_bucket_file_create ( struct ast_bucket_file file  ) 

Create a new bucket file in backend storage.

Parameters:
file The bucket file
Return values:
0 success
-1 failure

Definition at line 690 of file bucket.c.

References ast_sorcery_create().

Referenced by AST_TEST_DEFINE().

00691 {
00692    return ast_sorcery_create(bucket_sorcery, file);
00693 }

int ast_bucket_file_delete ( struct ast_bucket_file file  ) 

Delete a bucket file from backend storage.

Parameters:
file The bucket file
Return values:
0 success
-1 failure

Definition at line 776 of file bucket.c.

References ast_sorcery_delete().

Referenced by AST_TEST_DEFINE().

00777 {
00778    return ast_sorcery_delete(bucket_sorcery, file);
00779 }

struct ast_json* ast_bucket_file_json ( const struct ast_bucket_file file  )  [read]

Get a JSON representation of a bucket file.

Parameters:
file The specific bucket file
Return values:
non-NULL success
NULL failure
Note:
The returned ast_json object must be unreferenced using ast_json_unref

Definition at line 781 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, ast_bucket_metadata::value, and value.

Referenced by AST_TEST_DEFINE().

00782 {
00783    RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
00784    struct ast_json *id, *metadata;
00785    struct ao2_iterator i;
00786    struct ast_bucket_metadata *attribute;
00787    int res = 0;
00788 
00789    json = ast_sorcery_objectset_json_create(bucket_sorcery, file);
00790    if (!json) {
00791       return NULL;
00792    }
00793 
00794    id = ast_json_string_create(ast_sorcery_object_get_id(file));
00795    if (!id) {
00796       return NULL;
00797    }
00798 
00799    if (ast_json_object_set(json, "id", id)) {
00800       return NULL;
00801    }
00802 
00803    metadata = ast_json_object_create();
00804    if (!metadata) {
00805       return NULL;
00806    }
00807 
00808    if (ast_json_object_set(json, "metadata", metadata)) {
00809       return NULL;
00810    }
00811 
00812    i = ao2_iterator_init(file->metadata, 0);
00813    for (; (attribute = ao2_iterator_next(&i)); ao2_ref(attribute, -1)) {
00814       struct ast_json *value = ast_json_string_create(attribute->value);
00815 
00816       if (!value || ast_json_object_set(metadata, attribute->name, value)) {
00817          res = -1;
00818          break;
00819       }
00820    }
00821    ao2_iterator_destroy(&i);
00822 
00823    if (res) {
00824       return NULL;
00825    }
00826 
00827    ast_json_ref(json);
00828    return json;
00829 }

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

Retrieve a metadata attribute from a file.

Parameters:
file The bucket file
name Name of the attribute
Return values:
non-NULL if found
NULL if not found
Note:
The object is returned with reference count increased

Definition at line 334 of file bucket.c.

References ao2_find, ast_bucket_file::metadata, and OBJ_KEY.

Referenced by AST_TEST_DEFINE().

00335 {
00336    return ao2_find(file->metadata, name, OBJ_KEY);
00337 }

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:
file The bucket file
name Name of the attribute
value Value of the attribute
Return values:
0 success
-1 failure
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 309 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_TEST_DEFINE().

00310 {
00311    RAII_VAR(struct ast_bucket_metadata *, metadata, bucket_metadata_alloc(name, value), ao2_cleanup);
00312 
00313    if (!metadata) {
00314       return -1;
00315    }
00316 
00317    ao2_find(file->metadata, name, OBJ_NODATA | OBJ_UNLINK | OBJ_KEY);
00318    ao2_link(file->metadata, metadata);
00319 
00320    return 0;
00321 }

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

Unset a specific metadata attribute on a file.

Parameters:
file The bucket file
name Name of the attribute
Return values:
0 success
-1 failure

Definition at line 323 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().

00324 {
00325    RAII_VAR(struct ast_bucket_metadata *, metadata, ao2_find(file->metadata, name, OBJ_UNLINK | OBJ_KEY), ao2_cleanup);
00326 
00327    if (!metadata) {
00328       return -1;
00329    }
00330 
00331    return 0;
00332 }

int ast_bucket_file_observer_add ( const struct ast_sorcery_observer callbacks  ) 

Add an observer for bucket file creation and deletion operations.

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

Definition at line 761 of file bucket.c.

References ast_sorcery_observer_add().

00762 {
00763    return ast_sorcery_observer_add(bucket_sorcery, "file", callbacks);
00764 }

void ast_bucket_file_observer_remove ( const struct ast_sorcery_observer callbacks  ) 

Remove an observer from bucket file creation and deletion.

Parameters:
callbacks Implementation of the sorcery observer interface

Definition at line 766 of file bucket.c.

References ast_sorcery_observer_remove().

00767 {
00768    ast_sorcery_observer_remove(bucket_sorcery, "file", callbacks);
00769 }

struct ast_bucket_file* ast_bucket_file_retrieve ( const char *  uri  )  [read]

Retrieve a bucket file.

Parameters:
uri Complete URI of the bucket file
Return values:
non-NULL if found
NULL if not found
Note:
The object is returned with reference count increased

Definition at line 752 of file bucket.c.

References ast_sorcery_retrieve_by_id(), ast_strlen_zero, and NULL.

Referenced by AST_TEST_DEFINE().

00753 {
00754    if (ast_strlen_zero(uri)) {
00755       return NULL;
00756    }
00757 
00758    return ast_sorcery_retrieve_by_id(bucket_sorcery, "file", uri);
00759 }

int ast_bucket_file_temporary_create ( struct ast_bucket_file file  ) 

Common file snapshot creation callback for creating a temporary file.

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

Definition at line 831 of file bucket.c.

References ast_copy_string(), and ast_bucket_file::path.

Referenced by load_module().

00832 {
00833    int fd;
00834 
00835    ast_copy_string(file->path, "/tmp/bucket-XXXXXX", sizeof(file->path));
00836 
00837    fd = mkstemp(file->path);
00838    if (fd < 0) {
00839       return -1;
00840    }
00841 
00842    close(fd);
00843    return 0;
00844 }

void ast_bucket_file_temporary_destroy ( struct ast_bucket_file file  ) 

Common file snapshot destruction callback for deleting a temporary file.

Parameters:
file Pointer to the file snapshot

Definition at line 846 of file bucket.c.

References ast_strlen_zero, and ast_bucket_file::path.

Referenced by load_module().

00847 {
00848    if (!ast_strlen_zero(file->path)) {
00849       unlink(file->path);
00850    }
00851 }

int ast_bucket_file_update ( struct ast_bucket_file file  ) 

Update an existing bucket file in backend storage.

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

Definition at line 771 of file bucket.c.

References ast_sorcery_update().

Referenced by AST_TEST_DEFINE().

00772 {
00773    return ast_sorcery_update(bucket_sorcery, file);
00774 }

int ast_bucket_init ( void   ) 

Initialize bucket support.

Return values:
0 success
-1 failure

Definition at line 909 of file bucket.c.

References __ast_sorcery_wizard_register(), AO2_ALLOC_OPT_LOCK_RWLOCK, ao2_container_alloc_options, 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_open, bucket_alloc(), bucket_cleanup(), bucket_file_alloc(), bucket_scheme_cmp(), bucket_scheme_hash(), FLDSET, LOG_ERROR, NULL, OPT_STRINGFIELD_T, SCHEME_BUCKETS, STRFLDSET, timeval_str2struct(), and timeval_struct2str().

Referenced by main().

00910 {
00911    ast_register_cleanup(&bucket_cleanup);
00912 
00913    schemes = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_RWLOCK, SCHEME_BUCKETS, bucket_scheme_hash,
00914       bucket_scheme_cmp);
00915    if (!schemes) {
00916       ast_log(LOG_ERROR, "Failed to create container for Bucket schemes\n");
00917       return -1;
00918    }
00919 
00920    if (__ast_sorcery_wizard_register(&bucket_wizard, NULL)) {
00921       ast_log(LOG_ERROR, "Failed to register sorcery wizard for 'bucket' intermediary\n");
00922       return -1;
00923    }
00924 
00925    if (__ast_sorcery_wizard_register(&bucket_file_wizard, NULL)) {
00926       ast_log(LOG_ERROR, "Failed to register sorcery wizard for 'file' intermediary\n");
00927       return -1;
00928    }
00929 
00930    if (!(bucket_sorcery = ast_sorcery_open())) {
00931       ast_log(LOG_ERROR, "Failed to create sorcery instance for Bucket support\n");
00932       return -1;
00933    }
00934 
00935    if (ast_sorcery_apply_default(bucket_sorcery, "bucket", "bucket", NULL) == AST_SORCERY_APPLY_FAIL) {
00936       ast_log(LOG_ERROR, "Failed to apply intermediary for 'bucket' object type in Bucket sorcery\n");
00937       return -1;
00938    }
00939 
00940    if (ast_sorcery_object_register(bucket_sorcery, "bucket", bucket_alloc, NULL, NULL)) {
00941       ast_log(LOG_ERROR, "Failed to register 'bucket' object type in Bucket sorcery\n");
00942       return -1;
00943    }
00944 
00945    ast_sorcery_object_field_register(bucket_sorcery, "bucket", "scheme", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_bucket, scheme));
00946    ast_sorcery_object_field_register_custom(bucket_sorcery, "bucket", "created", "", timeval_str2struct, timeval_struct2str, NULL, 0, FLDSET(struct ast_bucket, created));
00947    ast_sorcery_object_field_register_custom(bucket_sorcery, "bucket", "modified", "", timeval_str2struct, timeval_struct2str, NULL, 0, FLDSET(struct ast_bucket, modified));
00948 
00949    if (ast_sorcery_apply_default(bucket_sorcery, "file", "bucket_file", NULL) == AST_SORCERY_APPLY_FAIL) {
00950       ast_log(LOG_ERROR, "Failed to apply intermediary for 'file' object type in Bucket sorcery\n");
00951       return -1;
00952    }
00953 
00954    if (ast_sorcery_object_register(bucket_sorcery, "file", bucket_file_alloc, NULL, NULL)) {
00955       ast_log(LOG_ERROR, "Failed to register 'file' object type in Bucket sorcery\n");
00956       return -1;
00957    }
00958 
00959    ast_sorcery_object_field_register(bucket_sorcery, "file", "scheme", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_bucket_file, scheme));
00960    ast_sorcery_object_field_register_custom(bucket_sorcery, "file", "created", "", timeval_str2struct, timeval_struct2str, NULL, 0, FLDSET(struct ast_bucket_file, created));
00961    ast_sorcery_object_field_register_custom(bucket_sorcery, "file", "modified", "", timeval_str2struct, timeval_struct2str, NULL, 0, FLDSET(struct ast_bucket_file, modified));
00962 
00963    return 0;
00964 }

struct ast_json* ast_bucket_json ( const struct ast_bucket bucket  )  [read]

Get a JSON representation of a bucket.

Parameters:
bucket The specific bucket
Return values:
non-NULL success
NULL failure
Note:
The returned ast_json object must be unreferenced using ast_json_unref

Definition at line 486 of file bucket.c.

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(), ast_bucket::buckets, ast_bucket::files, id, NULL, and RAII_VAR.

Referenced by AST_TEST_DEFINE().

00487 {
00488    RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
00489    struct ast_json *id, *files, *buckets;
00490    struct ao2_iterator i;
00491    char *uri;
00492    int res = 0;
00493 
00494    json = ast_sorcery_objectset_json_create(bucket_sorcery, bucket);
00495    if (!json) {
00496       return NULL;
00497    }
00498 
00499    id = ast_json_string_create(ast_sorcery_object_get_id(bucket));
00500    if (!id) {
00501       return NULL;
00502    }
00503 
00504    if (ast_json_object_set(json, "id", id)) {
00505       return NULL;
00506    }
00507 
00508    buckets = ast_json_array_create();
00509    if (!buckets) {
00510       return NULL;
00511    }
00512 
00513    if (ast_json_object_set(json, "buckets", buckets)) {
00514       return NULL;
00515    }
00516 
00517    i = ao2_iterator_init(bucket->buckets, 0);
00518    for (; (uri = ao2_iterator_next(&i)); ao2_ref(uri, -1)) {
00519       struct ast_json *bucket_uri = ast_json_string_create(uri);
00520 
00521       if (!bucket_uri || ast_json_array_append(buckets, bucket_uri)) {
00522          res = -1;
00523          ao2_ref(uri, -1);
00524          break;
00525       }
00526    }
00527    ao2_iterator_destroy(&i);
00528 
00529    if (res) {
00530       return NULL;
00531    }
00532 
00533    files = ast_json_array_create();
00534    if (!files) {
00535       return NULL;
00536    }
00537 
00538    if (ast_json_object_set(json, "files", files)) {
00539       return NULL;
00540    }
00541 
00542    i = ao2_iterator_init(bucket->files, 0);
00543    for (; (uri = ao2_iterator_next(&i)); ao2_ref(uri, -1)) {
00544       struct ast_json *file_uri = ast_json_string_create(uri);
00545 
00546       if (!file_uri || ast_json_array_append(files, file_uri)) {
00547          res = -1;
00548          ao2_ref(uri, -1);
00549          break;
00550       }
00551    }
00552    ao2_iterator_destroy(&i);
00553 
00554    if (res) {
00555       return NULL;
00556    }
00557 
00558    ast_json_ref(json);
00559    return json;
00560 }

int ast_bucket_observer_add ( const struct ast_sorcery_observer callbacks  ) 

Add an observer for bucket creation and deletion operations.

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

Definition at line 471 of file bucket.c.

References ast_sorcery_observer_add().

00472 {
00473    return ast_sorcery_observer_add(bucket_sorcery, "bucket", callbacks);
00474 }

void ast_bucket_observer_remove ( const struct ast_sorcery_observer callbacks  ) 

Remove an observer from bucket creation and deletion.

Parameters:
callbacks Implementation of the sorcery observer interface

Definition at line 476 of file bucket.c.

References ast_sorcery_observer_remove().

00477 {
00478    ast_sorcery_observer_remove(bucket_sorcery, "bucket", callbacks);
00479 }

struct ast_bucket* ast_bucket_retrieve ( const char *  uri  )  [read]

Retrieve information about a bucket.

Parameters:
uri Complete URI of the bucket
Return values:
non-NULL if found
NULL if not found
Note:
The object is returned with reference count increased

Definition at line 462 of file bucket.c.

References ast_sorcery_retrieve_by_id(), ast_strlen_zero, and NULL.

Referenced by AST_TEST_DEFINE().

00463 {
00464    if (ast_strlen_zero(uri)) {
00465       return NULL;
00466    }
00467 
00468    return ast_sorcery_retrieve_by_id(bucket_sorcery, "bucket", uri);
00469 }


Generated on Thu Apr 16 06:29:17 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6