jack1 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

487 lines
13KB

  1. /*
  2. Copyright (C) 2013 Paul Davis
  3. This program is free software; you can redistribute it and/or modify it
  4. under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 2.1 of the License, or (at
  6. your option) any later version.
  7. This program is distributed in the hope that it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  9. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  10. License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #include <string.h>
  16. #include <db.h>
  17. #include <limits.h>
  18. #include <jack/metadata.h>
  19. #include <jack/uuid.h>
  20. #include "internal.h"
  21. #include "local.h"
  22. static DB* db = NULL;
  23. static int
  24. jack_property_init (const char* server_name)
  25. {
  26. int ret;
  27. char dbpath[PATH_MAX+1];
  28. char server_dir[PATH_MAX+1];
  29. /* idempotent */
  30. if (db) {
  31. return 0;
  32. }
  33. if ((ret = db_create (&db, NULL, 0)) != 0) {
  34. jack_error ("Cannot initialize metadata DB (%s)", db_strerror (ret));
  35. return -1;
  36. }
  37. snprintf (dbpath, sizeof (dbpath), "%s/%s", jack_server_dir (server_name, server_dir), "metadata.db");
  38. if ((ret = db->open (db, NULL, dbpath, NULL, DB_HASH, DB_CREATE|DB_THREAD, 0666)) != 0) {
  39. jack_error ("Cannot open metadata DB at %s: %s", dbpath, db_strerror (ret));
  40. db->close (db, 0);
  41. db = NULL;
  42. return -1;
  43. }
  44. return 0;
  45. }
  46. static void jack_properties_uninit () __attribute__ ((destructor));
  47. void
  48. jack_properties_uninit ()
  49. {
  50. if (db) {
  51. db->close (db, 0);
  52. db = NULL;
  53. }
  54. }
  55. void
  56. jack_free_description(jack_description_t* desc)
  57. {
  58. return;
  59. }
  60. static void
  61. make_key_dbt (DBT* dbt, jack_uuid_t subject, const char* key)
  62. {
  63. char ustr[JACK_UUID_STRING_SIZE];
  64. size_t len1, len2;
  65. memset(dbt, 0, sizeof(DBT));
  66. jack_uuid_unparse (subject, ustr);
  67. len1 = JACK_UUID_STRING_SIZE;
  68. len2 = strlen (key) + 1;
  69. dbt->size = len1 + len2;
  70. dbt->data = malloc (dbt->size);
  71. memcpy (dbt->data, ustr, len1); // copy subject+null
  72. memcpy (dbt->data + len1, key, len2); // copy key+null
  73. }
  74. int
  75. jack_set_property (jack_client_t* client,
  76. jack_uuid_t subject,
  77. const char* key,
  78. const char* value,
  79. const char* type)
  80. {
  81. DBT d_key;
  82. DBT data;
  83. int ret;
  84. size_t len1, len2;
  85. if (jack_property_init (NULL)) {
  86. return -1;
  87. }
  88. /* build a key */
  89. make_key_dbt (&d_key, subject, key);
  90. /* build data */
  91. memset(&data, 0, sizeof(data));
  92. len1 = strlen(value) + 1;
  93. if (type && type[0] != '\0') {
  94. len2 = strlen(type) + 1;
  95. } else {
  96. len2 = 0;
  97. }
  98. data.size = len1 + len2;
  99. data.data = malloc (data.size);
  100. memcpy (data.data, value, len1);
  101. if (len2) {
  102. memcpy (data.data + len1, type, len2);
  103. }
  104. if ((ret = db->put (db, NULL, &d_key, &data, 0)) != 0) {
  105. char ustr[JACK_UUID_STRING_SIZE];
  106. jack_uuid_unparse (subject, ustr);
  107. jack_error ("Cannot store metadata for %s/%s (%s)", ustr, key, db_strerror (ret));
  108. return -1;
  109. }
  110. return 0;
  111. }
  112. int
  113. jack_get_property (jack_uuid_t subject,
  114. const char* key,
  115. char** value,
  116. char** type)
  117. {
  118. DBT d_key;
  119. DBT data;
  120. int ret;
  121. size_t len1, len2;
  122. if (key == NULL || key[0] == '\0') {
  123. return -1;
  124. }
  125. if (jack_property_init (NULL)) {
  126. return -1;
  127. }
  128. /* build a key */
  129. make_key_dbt (&d_key, subject, key);
  130. /* setup data DBT */
  131. memset(&data, 0, sizeof(data));
  132. data.flags = DB_DBT_MALLOC;
  133. if ((ret = db->get (db, NULL, &d_key, &data, 0)) != 0) {
  134. if (ret != DB_NOTFOUND) {
  135. char ustr[JACK_UUID_STRING_SIZE];
  136. jack_uuid_unparse (subject, ustr);
  137. jack_error ("Cannot metadata for %s/%s (%s)", ustr, key, db_strerror (ret));
  138. }
  139. return -1;
  140. }
  141. /* result must have at least 2 chars plus 2 nulls to be valid
  142. */
  143. if (data.size < 4) {
  144. if (data.size > 0) {
  145. free (data.data);
  146. }
  147. return -1;
  148. }
  149. len1 = strlen (data.data) + 1;
  150. (*value) = (char *) malloc (len1);
  151. memcpy (*value, data.data, len1);
  152. if (len1 < data.size) {
  153. len2 = strlen (data.data+len1) + 1;
  154. (*type) = (char *) malloc (len2);
  155. memcpy (*type, data.data+len1, len2);
  156. } else {
  157. /* no type specified, assume default */
  158. *type = NULL;
  159. }
  160. if (data.size) {
  161. free (data.data);
  162. }
  163. return 0;
  164. }
  165. int
  166. jack_get_properties (jack_uuid_t subject,
  167. jack_description_t* desc)
  168. {
  169. DBT key;
  170. DBT data;
  171. DBC* cursor;
  172. int ret;
  173. size_t len1, len2;
  174. size_t cnt = 0;
  175. char ustr[JACK_UUID_STRING_SIZE];
  176. size_t props_size = 0;
  177. jack_property_t* prop;
  178. desc->properties = NULL;
  179. jack_uuid_unparse (subject, ustr);
  180. if (jack_property_init (NULL)) {
  181. return -1;
  182. }
  183. if ((ret = db->cursor (db, NULL, &cursor, 0)) != 0) {
  184. jack_error ("Cannot create cursor for metadata search (%s)", db_strerror (ret));
  185. return -1;
  186. }
  187. memset(&key, 0, sizeof(key));
  188. memset(&data, 0, sizeof(data));
  189. data.flags = DB_DBT_MALLOC;
  190. while ((ret = cursor->get(cursor, &key, &data, DB_NEXT)) == 0) {
  191. /* require 2 extra chars (data+null) for key,
  192. which is composed of UUID str plus a key name
  193. */
  194. if (key.size < JACK_UUID_STRING_SIZE + 2) {
  195. if (data.size > 0) {
  196. free (data.data);
  197. }
  198. continue;
  199. }
  200. if (memcmp (ustr, key.data, JACK_UUID_STRING_SIZE) != 0) {
  201. /* not relevant */
  202. if (data.size > 0) {
  203. free (data.data);
  204. }
  205. continue;
  206. }
  207. /* result must have at least 2 chars plus 2 nulls to be valid
  208. */
  209. if (data.size < 4) {
  210. if (data.size > 0) {
  211. free (data.data);
  212. }
  213. continue;
  214. }
  215. /* realloc array if necessary */
  216. if (cnt == props_size) {
  217. if (props_size == 0) {
  218. props_size = 8;
  219. } else {
  220. props_size *= 2;
  221. }
  222. desc->properties = (jack_property_t*) realloc (desc->properties, sizeof (jack_property_t) * props_size);
  223. }
  224. prop = &desc->properties[cnt];
  225. /* store UUID/subject */
  226. jack_uuid_copy (desc->subject, subject);
  227. /* copy key (without leading UUID as subject */
  228. len1 = key.size - JACK_UUID_STRING_SIZE;
  229. prop->key = malloc (key.size);
  230. memcpy ((char*) prop->key, key.data + JACK_UUID_STRING_SIZE, len1);
  231. /* copy data */
  232. len1 = strlen (data.data) + 1;
  233. prop->data = (char *) malloc (len1);
  234. memcpy ((char*) prop->data, data.data, len1);
  235. if (len1 < data.size) {
  236. len2 = strlen (data.data+len1) + 1;
  237. prop->type= (char *) malloc (len2);
  238. memcpy ((char*) prop->type, data.data+len1, len2);
  239. } else {
  240. /* no type specified, assume default */
  241. prop->type = NULL;
  242. }
  243. if (data.size) {
  244. free (data.data);
  245. }
  246. ++cnt;
  247. }
  248. cursor->close (cursor);
  249. return cnt;
  250. }
  251. int
  252. jack_get_all_properties (jack_description_t** desc)
  253. {
  254. DBT key;
  255. DBT data;
  256. DBC* cursor;
  257. int ret;
  258. size_t cnt = 0;
  259. if (jack_property_init (NULL)) {
  260. return -1;
  261. }
  262. if ((ret = db->cursor (db, NULL, &cursor, 0)) != 0) {
  263. jack_error ("Cannot create cursor for metadata search (%s)", db_strerror (ret));
  264. return -1;
  265. }
  266. memset(&key, 0, sizeof(key));
  267. memset(&data, 0, sizeof(data));
  268. data.flags = DB_DBT_MALLOC;
  269. while ((ret = cursor->get(cursor, &key, &data, DB_NEXT)) == 0) {
  270. if (data.size) {
  271. free (data.data);
  272. }
  273. ++cnt;
  274. }
  275. cursor->close (cursor);
  276. return cnt;
  277. }
  278. int
  279. jack_get_description (jack_uuid_t subject,
  280. jack_description_t* desc)
  281. {
  282. return 0;
  283. }
  284. int
  285. jack_get_all_descriptions (jack_description_t** descs)
  286. {
  287. return 0;
  288. }
  289. int
  290. jack_set_property_change_callback (jack_client_t *client,
  291. JackPropertyChangeCallback callback, void *arg)
  292. {
  293. if (client->control->active) {
  294. jack_error ("You cannot set callbacks on an active client.");
  295. return -1;
  296. }
  297. client->property_cb = callback;
  298. client->property_cb_arg = arg;
  299. client->control->property_cbset = (callback != NULL);
  300. return 0;
  301. }
  302. int
  303. jack_remove_property (jack_client_t* client, jack_uuid_t subject, const char* key)
  304. {
  305. DBT d_key;
  306. int ret;
  307. if (jack_property_init (NULL)) {
  308. return -1;
  309. }
  310. make_key_dbt (&d_key, subject, key);
  311. if ((ret = db->del (db, NULL, &d_key, 0)) != 0) {
  312. jack_error ("Cannot delete key %s (%s)", key, db_strerror (ret));
  313. return -1;
  314. }
  315. return 0;
  316. }
  317. int
  318. jack_remove_properties (jack_client_t* client, jack_uuid_t subject)
  319. {
  320. DBT key;
  321. DBT data;
  322. DBC* cursor;
  323. int ret;
  324. char ustr[JACK_UUID_STRING_SIZE];
  325. int retval = 0;
  326. jack_uuid_unparse (subject, ustr);
  327. if (jack_property_init (NULL)) {
  328. return -1;
  329. }
  330. if ((ret = db->cursor (db, NULL, &cursor, 0)) != 0) {
  331. jack_error ("Cannot create cursor for metadata search (%s)", db_strerror (ret));
  332. return -1;
  333. }
  334. memset(&key, 0, sizeof(key));
  335. memset(&data, 0, sizeof(data));
  336. data.flags = DB_DBT_MALLOC;
  337. while ((ret = cursor->get(cursor, &key, &data, DB_NEXT)) == 0) {
  338. /* require 2 extra chars (data+null) for key,
  339. which is composed of UUID str plus a key name
  340. */
  341. if (key.size < JACK_UUID_STRING_SIZE + 2) {
  342. if (data.size > 0) {
  343. free (data.data);
  344. }
  345. continue;
  346. }
  347. if (memcmp (ustr, key.data, JACK_UUID_STRING_SIZE) != 0) {
  348. /* not relevant */
  349. if (data.size > 0) {
  350. free (data.data);
  351. }
  352. continue;
  353. }
  354. if ((ret = cursor->del (cursor, 0)) != 0) {
  355. jack_error ("cannot delete property (%s)", db_strerror (ret));
  356. /* don't return -1 here since this would leave things
  357. even more inconsistent. wait till the cursor is finished
  358. */
  359. retval = -1;
  360. }
  361. }
  362. cursor->close (cursor);
  363. return retval;
  364. }
  365. int
  366. jack_remove_all_properties (jack_client_t* client)
  367. {
  368. int ret;
  369. if (jack_property_init (NULL)) {
  370. return -1;
  371. }
  372. if ((ret = db->truncate (db, NULL, NULL, 0)) != 0) {
  373. jack_error ("Cannot clear properties (%s)", db_strerror (ret));
  374. return -1;
  375. }
  376. return 0;
  377. }