bucket.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * Joshua Colp <jcolp@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. * \brief Bucket File API
  20. * \author Joshua Colp <jcolp@digium.com>
  21. * \ref AstBucket
  22. */
  23. /*!
  24. * \page AstBucket Bucket File API
  25. *
  26. * Bucket is an API which provides directory and file access in a generic fashion. It is
  27. * implemented as a thin wrapper over the sorcery data access layer API and is written in
  28. * a pluggable fashion to allow different backend storage mechanisms.
  29. *
  30. */
  31. #ifndef _ASTERISK_BUCKET_H
  32. #define _ASTERISK_BUCKET_H
  33. #if defined(__cplusplus) || defined(c_plusplus)
  34. extern "C" {
  35. #endif
  36. #include "asterisk/sorcery.h"
  37. /*! \brief Opaque structure for internal details about a scheme */
  38. struct ast_bucket_scheme;
  39. /*! \brief Bucket metadata structure, AO2 key value pair */
  40. struct ast_bucket_metadata {
  41. /*! \brief Name of the attribute */
  42. const char *name;
  43. /*! \brief Value of the attribute */
  44. const char *value;
  45. /*! \brief Storage for the above name and value */
  46. char data[0];
  47. };
  48. /*! \brief Bucket structure, contains other buckets and files */
  49. struct ast_bucket {
  50. /*! \brief Sorcery object information */
  51. SORCERY_OBJECT(details);
  52. /*! \brief Scheme implementation in use */
  53. struct ast_bucket_scheme *scheme_impl;
  54. /*! \brief Stringfields */
  55. AST_DECLARE_STRING_FIELDS(
  56. /*! \brief Name of scheme in use */
  57. AST_STRING_FIELD(scheme);
  58. );
  59. /*! \brief When this bucket was created */
  60. struct timeval created;
  61. /*! \brief When this bucket was last modified */
  62. struct timeval modified;
  63. /*! \brief Container of string URIs of buckets within this bucket */
  64. struct ao2_container *buckets;
  65. /*! \brief Container of string URIs of files within this bucket */
  66. struct ao2_container *files;
  67. };
  68. /*! \brief Bucket file structure, contains reference to file and information about it */
  69. struct ast_bucket_file {
  70. /*! \brief Sorcery object information */
  71. SORCERY_OBJECT(details);
  72. /*! \brief Scheme implementation in use */
  73. struct ast_bucket_scheme *scheme_impl;
  74. /*! \brief Stringfields */
  75. AST_DECLARE_STRING_FIELDS(
  76. /*! \brief Name of scheme in use */
  77. AST_STRING_FIELD(scheme);
  78. );
  79. /*! \brief When this file was created */
  80. struct timeval created;
  81. /*! \brief When this file was last modified */
  82. struct timeval modified;
  83. /*! \brief Container of metadata attributes about file */
  84. struct ao2_container *metadata;
  85. /*! \brief Local path to this file */
  86. char path[PATH_MAX];
  87. };
  88. /*!
  89. * \brief A callback function invoked when creating a file snapshot
  90. *
  91. * \param file Pointer to the file snapshot
  92. *
  93. * \retval 0 success
  94. * \retval -1 failure
  95. */
  96. typedef int (*bucket_file_create_cb)(struct ast_bucket_file *file);
  97. /*!
  98. * \brief A callback function invoked when destroying a file snapshot
  99. *
  100. * \param file Pointer to the file snapshot
  101. */
  102. typedef void (*bucket_file_destroy_cb)(struct ast_bucket_file *file);
  103. /*!
  104. * \brief Initialize bucket support
  105. *
  106. * \retval 0 success
  107. * \retval -1 failure
  108. */
  109. int ast_bucket_init(void);
  110. /*!
  111. * \brief Register support for a specific scheme
  112. *
  113. * \param name Name of the scheme, used to find based on scheme in URIs
  114. * \param bucket Sorcery wizard used for buckets
  115. * \param file Sorcery wizard used for files
  116. * \param create_cb Required file snapshot creation callback
  117. * \param destroy_cb Optional file snapshot destruction callback
  118. *
  119. * \retval 0 success
  120. * \retval -1 failure
  121. *
  122. * \note Once a scheme has been registered it can not be unregistered
  123. */
  124. #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)
  125. /*!
  126. * \brief Register support for a specific scheme
  127. *
  128. * \param name Name of the scheme, used to find based on scheme in URIs
  129. * \param bucket Sorcery wizard used for buckets
  130. * \param file Sorcery wizard used for files
  131. * \param create_cb Required file snapshot creation callback
  132. * \param destroy_cb Optional file snapshot destruction callback
  133. * \param module The module which implements this scheme
  134. *
  135. * \retval 0 success
  136. * \retval -1 failure
  137. *
  138. * \note Once a scheme has been registered it can not be unregistered
  139. */
  140. int __ast_bucket_scheme_register(const char *name, struct ast_sorcery_wizard *bucket,
  141. struct ast_sorcery_wizard *file, bucket_file_create_cb create_cb,
  142. bucket_file_destroy_cb destroy_cb, struct ast_module *module);
  143. /*!
  144. * \brief Set a metadata attribute on a file to a specific value
  145. *
  146. * \param file The bucket file
  147. * \param name Name of the attribute
  148. * \param value Value of the attribute
  149. *
  150. * \retval 0 success
  151. * \retval -1 failure
  152. *
  153. * \note This function will overwrite an existing attribute of the same name, unless an error
  154. * occurs. If an error occurs the existing attribute is left alone.
  155. */
  156. int ast_bucket_file_metadata_set(struct ast_bucket_file *file, const char *name, const char *value);
  157. /*!
  158. * \brief Unset a specific metadata attribute on a file
  159. *
  160. * \param file The bucket file
  161. * \param name Name of the attribute
  162. *
  163. * \retval 0 success
  164. * \retval -1 failure
  165. */
  166. int ast_bucket_file_metadata_unset(struct ast_bucket_file *file, const char *name);
  167. /*!
  168. * \brief Retrieve a metadata attribute from a file
  169. *
  170. * \param file The bucket file
  171. * \param name Name of the attribute
  172. *
  173. * \retval non-NULL if found
  174. * \retval NULL if not found
  175. *
  176. * \note The object is returned with reference count increased
  177. */
  178. struct ast_bucket_metadata *ast_bucket_file_metadata_get(struct ast_bucket_file *file, const char *name);
  179. /*!
  180. * \brief Allocate a new bucket
  181. *
  182. * \param uri Complete URI for the bucket
  183. *
  184. * \param non-NULL success
  185. * \param NULL failure
  186. *
  187. * \note This only creates a local bucket object, to persist in backend storage you must call
  188. * ast_bucket_create
  189. */
  190. struct ast_bucket *ast_bucket_alloc(const char *uri);
  191. /*!
  192. * \brief Create a new bucket in backend storage
  193. *
  194. * \param bucket The bucket
  195. *
  196. * \retval 0 success
  197. * \retval -1 failure
  198. */
  199. int ast_bucket_create(struct ast_bucket *bucket);
  200. /*!
  201. * \brief Delete a bucket from backend storage
  202. *
  203. * \param bucket The bucket
  204. *
  205. * \retval 0 success
  206. * \retval -1 failure
  207. */
  208. int ast_bucket_delete(struct ast_bucket *bucket);
  209. /*!
  210. * \brief Retrieve information about a bucket
  211. *
  212. * \param uri Complete URI of the bucket
  213. *
  214. * \retval non-NULL if found
  215. * \retval NULL if not found
  216. *
  217. * \note The object is returned with reference count increased
  218. */
  219. struct ast_bucket *ast_bucket_retrieve(const char *uri);
  220. /*!
  221. * \brief Add an observer for bucket creation and deletion operations
  222. *
  223. * \param callbacks Implementation of the sorcery observer interface
  224. *
  225. * \retval 0 success
  226. * \retval -1 failure
  227. *
  228. * \note You must be ready to accept observer invocations before this function is called
  229. */
  230. int ast_bucket_observer_add(const struct ast_sorcery_observer *callbacks);
  231. /*!
  232. * \brief Remove an observer from bucket creation and deletion
  233. *
  234. * \param callbacks Implementation of the sorcery observer interface
  235. */
  236. void ast_bucket_observer_remove(const struct ast_sorcery_observer *callbacks);
  237. /*!
  238. * \brief Get a JSON representation of a bucket
  239. *
  240. * \param bucket The specific bucket
  241. *
  242. * \retval non-NULL success
  243. * \retval NULL failure
  244. *
  245. * \note The returned ast_json object must be unreferenced using ast_json_unref
  246. */
  247. struct ast_json *ast_bucket_json(const struct ast_bucket *bucket);
  248. /*!
  249. * \brief Allocate a new bucket file
  250. *
  251. * \param uri Complete URI for the bucket file
  252. *
  253. * \param non-NULL success
  254. * \param NULL failure
  255. *
  256. * \note This only creates a local bucket file object, to persist in backend storage you must call
  257. * ast_bucket_file_create
  258. */
  259. struct ast_bucket_file *ast_bucket_file_alloc(const char *uri);
  260. /*!
  261. * \brief Create a new bucket file in backend storage
  262. *
  263. * \param file The bucket file
  264. *
  265. * \retval 0 success
  266. * \retval -1 failure
  267. */
  268. int ast_bucket_file_create(struct ast_bucket_file *file);
  269. /*!
  270. * \brief Copy a bucket file to a new URI
  271. *
  272. * \param file The source bucket file
  273. * \param uri The new URI
  274. *
  275. * \retval non-NULL success
  276. * \retval NULL failure
  277. *
  278. * \note This operation stages things locally, you must call ast_bucket_file_create on the file
  279. * that is returned to commit the copy to backend storage
  280. *
  281. */
  282. struct ast_bucket_file *ast_bucket_file_copy(struct ast_bucket_file *file, const char *uri);
  283. /*!
  284. * \brief Update an existing bucket file in backend storage
  285. *
  286. * \param file The bucket file
  287. *
  288. * \retval 0 success
  289. * \retval -1 failure
  290. *
  291. * \note This operation will update both the actual content of the file and the metadata associated with it
  292. */
  293. int ast_bucket_file_update(struct ast_bucket_file *file);
  294. /*!
  295. * \brief Delete a bucket file from backend storage
  296. *
  297. * \param file The bucket file
  298. *
  299. * \retval 0 success
  300. * \retval -1 failure
  301. */
  302. int ast_bucket_file_delete(struct ast_bucket_file *file);
  303. /*!
  304. * \brief Retrieve a bucket file
  305. *
  306. * \param uri Complete URI of the bucket file
  307. *
  308. * \retval non-NULL if found
  309. * \retval NULL if not found
  310. *
  311. * \note The object is returned with reference count increased
  312. */
  313. struct ast_bucket_file *ast_bucket_file_retrieve(const char *uri);
  314. /*!
  315. * \brief Add an observer for bucket file creation and deletion operations
  316. *
  317. * \param callbacks Implementation of the sorcery observer interface
  318. *
  319. * \retval 0 success
  320. * \retval -1 failure
  321. *
  322. * \note You must be ready to accept observer invocations before this function is called
  323. */
  324. int ast_bucket_file_observer_add(const struct ast_sorcery_observer *callbacks);
  325. /*!
  326. * \brief Remove an observer from bucket file creation and deletion
  327. *
  328. * \param callbacks Implementation of the sorcery observer interface
  329. */
  330. void ast_bucket_file_observer_remove(const struct ast_sorcery_observer *callbacks);
  331. /*!
  332. * \brief Get a JSON representation of a bucket file
  333. *
  334. * \param file The specific bucket file
  335. *
  336. * \retval non-NULL success
  337. * \retval NULL failure
  338. *
  339. * \note The returned ast_json object must be unreferenced using ast_json_unref
  340. */
  341. struct ast_json *ast_bucket_file_json(const struct ast_bucket_file *file);
  342. /*!
  343. * \brief Common file snapshot creation callback for creating a temporary file
  344. *
  345. * \param file Pointer to the file snapshot
  346. *
  347. * \retval 0 success
  348. * \retval -1 failure
  349. */
  350. int ast_bucket_file_temporary_create(struct ast_bucket_file *file);
  351. /*!
  352. * \brief Common file snapshot destruction callback for deleting a temporary file
  353. *
  354. * \param file Pointer to the file snapshot
  355. */
  356. void ast_bucket_file_temporary_destroy(struct ast_bucket_file *file);
  357. #if defined(__cplusplus) || defined(c_plusplus)
  358. }
  359. #endif
  360. #endif /* _ASTERISK_BUCKET_H */