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.

669 lines
20KB

  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 DB_ENV* db_env = NULL;
  24. static int
  25. jack_property_init (const char* server_name)
  26. {
  27. int ret;
  28. char dbpath[PATH_MAX+1];
  29. char server_dir[PATH_MAX+1];
  30. /* idempotent */
  31. if (db_env) {
  32. return 0;
  33. }
  34. if ((ret = db_env_create(&db_env, 0)) != 0) {
  35. jack_error ("cannot initialize DB environment: %s\n", db_strerror(ret));
  36. return -1;
  37. }
  38. if ((ret = db_env->open(db_env, jack_server_dir (server_name, server_dir), DB_CREATE | DB_INIT_LOCK | DB_INIT_MPOOL | DB_THREAD, 0)) != 0) {
  39. jack_error ("cannot open DB environment: %s", db_strerror (ret));
  40. return -1;
  41. }
  42. if ((ret = db_create (&db, db_env, 0)) != 0) {
  43. jack_error ("Cannot initialize metadata DB (%s)", db_strerror (ret));
  44. return -1;
  45. }
  46. snprintf (dbpath, sizeof (dbpath), "%s/%s", jack_server_dir (server_name, server_dir), "metadata.db");
  47. if ((ret = db->open (db, NULL, dbpath, NULL, DB_HASH, DB_CREATE|DB_THREAD, 0666)) != 0) {
  48. jack_error ("Cannot open metadata DB at %s: %s", dbpath, db_strerror (ret));
  49. db->close (db, 0);
  50. db = NULL;
  51. return -1;
  52. }
  53. return 0;
  54. }
  55. static void jack_properties_uninit () __attribute__ ((destructor));
  56. void
  57. jack_properties_uninit ()
  58. {
  59. if (db) {
  60. db->close (db, 0);
  61. db = NULL;
  62. }
  63. if (db_env) {
  64. db_env->close (db_env, 0);
  65. db_env = 0;
  66. }
  67. }
  68. void
  69. jack_free_description(jack_description_t* desc, int free_actual_description_too)
  70. {
  71. uint32_t n;
  72. for (n = 0; n < desc->property_cnt; ++n) {
  73. free ((char*) desc->properties[n].key);
  74. free ((char*) desc->properties[n].data);
  75. if (desc->properties[n].type) {
  76. free ((char*) desc->properties[n].type);
  77. }
  78. }
  79. free (desc->properties);
  80. if (free_actual_description_too) {
  81. free (desc);
  82. }
  83. }
  84. static int
  85. jack_property_change_notify (jack_client_t* client, jack_uuid_t uuid, const char* key, jack_property_change_t change)
  86. {
  87. jack_request_t req;
  88. /* the engine passes in a NULL client when it removes metadata during port or client removal
  89. */
  90. if (client == NULL) {
  91. return 0;
  92. }
  93. req.type = PropertyChangeNotify;
  94. req.x.property.change = change;
  95. jack_uuid_copy (req.x.property.uuid, uuid);
  96. req.x.property.keylen = key ? strlen (key) + 1 : 0;
  97. req.x.property.key = key;
  98. return jack_client_deliver_request (client, &req);
  99. }
  100. static void
  101. make_key_dbt (DBT* dbt, jack_uuid_t subject, const char* key)
  102. {
  103. char ustr[JACK_UUID_STRING_SIZE];
  104. size_t len1, len2;
  105. memset(dbt, 0, sizeof(DBT));
  106. jack_uuid_unparse (subject, ustr);
  107. len1 = JACK_UUID_STRING_SIZE;
  108. len2 = strlen (key) + 1;
  109. dbt->size = len1 + len2;
  110. dbt->data = malloc (dbt->size);
  111. memcpy (dbt->data, ustr, len1); // copy subject+null
  112. memcpy (dbt->data + len1, key, len2); // copy key+null
  113. }
  114. int
  115. jack_set_property (jack_client_t* client,
  116. jack_uuid_t subject,
  117. const char* key,
  118. const char* value,
  119. const char* type)
  120. {
  121. DBT d_key;
  122. DBT data;
  123. int ret;
  124. size_t len1, len2;
  125. jack_property_change_t change;
  126. if (!key || key[0] == '\0') {
  127. jack_error ("empty key string for metadata not allowed");
  128. return -1;
  129. }
  130. if (!value || value[0] == '\0') {
  131. jack_error ("empty value string for metadata not allowed");
  132. return -1;
  133. }
  134. if (jack_property_init (NULL)) {
  135. return -1;
  136. }
  137. /* build a key */
  138. make_key_dbt (&d_key, subject, key);
  139. /* build data */
  140. memset(&data, 0, sizeof(data));
  141. len1 = strlen(value) + 1;
  142. if (type && type[0] != '\0') {
  143. len2 = strlen(type) + 1;
  144. } else {
  145. len2 = 0;
  146. }
  147. data.size = len1 + len2;
  148. data.data = malloc (data.size);
  149. memcpy (data.data, value, len1);
  150. if (len2) {
  151. memcpy (data.data + len1, type, len2);
  152. }
  153. if (db->exists (db, NULL, &d_key, 0) == DB_NOTFOUND) {
  154. change = PropertyCreated;
  155. } else {
  156. change = PropertyChanged;
  157. }
  158. if ((ret = db->put (db, NULL, &d_key, &data, 0)) != 0) {
  159. char ustr[JACK_UUID_STRING_SIZE];
  160. jack_uuid_unparse (subject, ustr);
  161. jack_error ("Cannot store metadata for %s/%s (%s)", ustr, key, db_strerror (ret));
  162. return -1;
  163. }
  164. jack_property_change_notify (client, subject, key, change);
  165. return 0;
  166. }
  167. int
  168. jack_get_property (jack_uuid_t subject,
  169. const char* key,
  170. char** value,
  171. char** type)
  172. {
  173. DBT d_key;
  174. DBT data;
  175. int ret;
  176. size_t len1, len2;
  177. if (key == NULL || key[0] == '\0') {
  178. return -1;
  179. }
  180. if (jack_property_init (NULL)) {
  181. return -1;
  182. }
  183. /* build a key */
  184. make_key_dbt (&d_key, subject, key);
  185. /* setup data DBT */
  186. memset(&data, 0, sizeof(data));
  187. data.flags = DB_DBT_MALLOC;
  188. if ((ret = db->get (db, NULL, &d_key, &data, 0)) != 0) {
  189. if (ret != DB_NOTFOUND) {
  190. char ustr[JACK_UUID_STRING_SIZE];
  191. jack_uuid_unparse (subject, ustr);
  192. jack_error ("Cannot metadata for %s/%s (%s)", ustr, key, db_strerror (ret));
  193. }
  194. return -1;
  195. }
  196. /* result must have at least 2 chars plus 2 nulls to be valid
  197. */
  198. if (data.size < 4) {
  199. if (data.size > 0) {
  200. free (data.data);
  201. }
  202. return -1;
  203. }
  204. len1 = strlen (data.data) + 1;
  205. (*value) = (char *) malloc (len1);
  206. memcpy (*value, data.data, len1);
  207. if (len1 < data.size) {
  208. len2 = strlen (data.data+len1) + 1;
  209. (*type) = (char *) malloc (len2);
  210. memcpy (*type, data.data+len1, len2);
  211. } else {
  212. /* no type specified, assume default */
  213. *type = NULL;
  214. }
  215. if (data.size) {
  216. free (data.data);
  217. }
  218. return 0;
  219. }
  220. int
  221. jack_get_properties (jack_uuid_t subject,
  222. jack_description_t* desc)
  223. {
  224. DBT key;
  225. DBT data;
  226. DBC* cursor;
  227. int ret;
  228. size_t len1, len2;
  229. size_t cnt = 0;
  230. char ustr[JACK_UUID_STRING_SIZE];
  231. size_t props_size = 0;
  232. jack_property_t* prop;
  233. desc->properties = NULL;
  234. desc->property_cnt = 0;
  235. jack_uuid_unparse (subject, ustr);
  236. if (jack_property_init (NULL)) {
  237. return -1;
  238. }
  239. if ((ret = db->cursor (db, NULL, &cursor, 0)) != 0) {
  240. jack_error ("Cannot create cursor for metadata search (%s)", db_strerror (ret));
  241. return -1;
  242. }
  243. memset(&key, 0, sizeof(key));
  244. memset(&data, 0, sizeof(data));
  245. data.flags = DB_DBT_MALLOC;
  246. while ((ret = cursor->get(cursor, &key, &data, DB_NEXT)) == 0) {
  247. /* require 2 extra chars (data+null) for key,
  248. which is composed of UUID str plus a key name
  249. */
  250. if (key.size < JACK_UUID_STRING_SIZE + 2) {
  251. if (data.size > 0) {
  252. free (data.data);
  253. }
  254. continue;
  255. }
  256. if (memcmp (ustr, key.data, JACK_UUID_STRING_SIZE) != 0) {
  257. /* not relevant */
  258. if (data.size > 0) {
  259. free (data.data);
  260. }
  261. continue;
  262. }
  263. /* result must have at least 2 chars plus 2 nulls to be valid
  264. */
  265. if (data.size < 4) {
  266. if (data.size > 0) {
  267. free (data.data);
  268. }
  269. continue;
  270. }
  271. /* realloc array if necessary */
  272. if (cnt == props_size) {
  273. if (props_size == 0) {
  274. props_size = 8; /* a rough guess at a likely upper bound for the number of properties */
  275. } else {
  276. props_size *= 2;
  277. }
  278. desc->properties = (jack_property_t*) realloc (desc->properties, sizeof (jack_property_t) * props_size);
  279. }
  280. prop = &desc->properties[cnt];
  281. /* store UUID/subject */
  282. jack_uuid_copy (desc->subject, subject);
  283. /* copy key (without leading UUID as subject */
  284. len1 = key.size - JACK_UUID_STRING_SIZE;
  285. prop->key = malloc (len1);
  286. memcpy ((char*) prop->key, key.data + JACK_UUID_STRING_SIZE, len1);
  287. /* copy data (which contains 1 or 2 null terminated strings, the value
  288. and optionally a MIME type.
  289. */
  290. len1 = strlen (data.data) + 1;
  291. prop->data = (char *) malloc (len1);
  292. memcpy ((char*) prop->data, data.data, len1);
  293. if (len1 < data.size) {
  294. len2 = strlen (data.data+len1) + 1;
  295. prop->type= (char *) malloc (len2);
  296. memcpy ((char*) prop->type, data.data+len1, len2);
  297. } else {
  298. /* no type specified, assume default */
  299. prop->type = NULL;
  300. }
  301. if (data.size) {
  302. free (data.data);
  303. }
  304. ++cnt;
  305. }
  306. cursor->close (cursor);
  307. desc->property_cnt = cnt;
  308. return cnt;
  309. }
  310. int
  311. jack_get_all_properties (jack_description_t** descriptions)
  312. {
  313. DBT key;
  314. DBT data;
  315. DBC* cursor;
  316. int ret;
  317. size_t dcnt = 0;
  318. size_t dsize = 0;
  319. size_t n = 0;
  320. jack_description_t* desc = NULL;
  321. jack_uuid_t uuid;
  322. jack_description_t* current_desc = NULL;
  323. jack_property_t* current_prop = NULL;
  324. size_t len1, len2;
  325. if (jack_property_init (NULL)) {
  326. return -1;
  327. }
  328. if ((ret = db->cursor (db, NULL, &cursor, 0)) != 0) {
  329. jack_error ("Cannot create cursor for metadata search (%s)", db_strerror (ret));
  330. return -1;
  331. }
  332. memset(&key, 0, sizeof(key));
  333. memset(&data, 0, sizeof(data));
  334. data.flags = DB_DBT_MALLOC;
  335. dsize = 8; /* initial guess at number of descriptions we need */
  336. dcnt = 0;
  337. desc = (jack_description_t*) malloc (dsize * sizeof (jack_description_t));
  338. while ((ret = cursor->get(cursor, &key, &data, DB_NEXT)) == 0) {
  339. /* require 2 extra chars (data+null) for key,
  340. which is composed of UUID str plus a key name
  341. */
  342. if (key.size < JACK_UUID_STRING_SIZE + 2) {
  343. if (data.size > 0) {
  344. free (data.data);
  345. }
  346. continue;
  347. }
  348. if (jack_uuid_parse (key.data, uuid) != 0) {
  349. continue;
  350. }
  351. /* do we have an existing description for this UUID */
  352. for (n = 0; n < dcnt ; ++n) {
  353. if (jack_uuid_compare (uuid, desc[n].subject) == 0) {
  354. break;
  355. }
  356. }
  357. if (n == dcnt) {
  358. /* we do not have an existing description, so grow the array */
  359. if (dcnt == dsize) {
  360. dsize *= 2;
  361. desc = (jack_description_t*) realloc (desc, sizeof (jack_description_t) * dsize);
  362. }
  363. /* initialize */
  364. desc[n].property_size = 0;
  365. desc[n].property_cnt = 0;
  366. desc[n].properties = NULL;
  367. /* set up UUID */
  368. jack_uuid_copy (desc[n].subject, uuid);
  369. dcnt++;
  370. }
  371. current_desc = &desc[n];
  372. /* see if there is room for the new property or if we need to realloc
  373. */
  374. if (current_desc->property_cnt == current_desc->property_size) {
  375. if (current_desc->property_size == 0) {
  376. current_desc->property_size = 8;
  377. } else {
  378. current_desc->property_size *= 2;
  379. }
  380. current_desc->properties = (jack_property_t*) realloc (current_desc->properties, sizeof (jack_property_t) * current_desc->property_size);
  381. }
  382. current_prop = &current_desc->properties[current_desc->property_cnt++];
  383. /* copy key (without leading UUID) */
  384. len1 = key.size - JACK_UUID_STRING_SIZE;
  385. current_prop->key = malloc (len1);
  386. memcpy ((char*) current_prop->key, key.data + JACK_UUID_STRING_SIZE, len1);
  387. /* copy data (which contains 1 or 2 null terminated strings, the value
  388. and optionally a MIME type.
  389. */
  390. len1 = strlen (data.data) + 1;
  391. current_prop->data = (char *) malloc (len1);
  392. memcpy ((char*) current_prop->data, data.data, len1);
  393. if (len1 < data.size) {
  394. len2 = strlen (data.data+len1) + 1;
  395. current_prop->type= (char *) malloc (len2);
  396. memcpy ((char*) current_prop->type, data.data+len1, len2);
  397. } else {
  398. /* no type specified, assume default */
  399. current_prop->type = NULL;
  400. }
  401. if (data.size) {
  402. free (data.data);
  403. }
  404. }
  405. cursor->close (cursor);
  406. (*descriptions) = desc;
  407. return dcnt;
  408. }
  409. int
  410. jack_get_description (jack_uuid_t subject,
  411. jack_description_t* desc)
  412. {
  413. return 0;
  414. }
  415. int
  416. jack_get_all_descriptions (jack_description_t** descs)
  417. {
  418. return 0;
  419. }
  420. int
  421. jack_set_property_change_callback (jack_client_t *client,
  422. JackPropertyChangeCallback callback, void *arg)
  423. {
  424. if (client->control->active) {
  425. jack_error ("You cannot set callbacks on an active client.");
  426. return -1;
  427. }
  428. client->property_cb = callback;
  429. client->property_cb_arg = arg;
  430. client->control->property_cbset = (callback != NULL);
  431. return 0;
  432. }
  433. int
  434. jack_remove_property (jack_client_t* client, jack_uuid_t subject, const char* key)
  435. {
  436. DBT d_key;
  437. int ret;
  438. if (jack_property_init (NULL)) {
  439. return -1;
  440. }
  441. make_key_dbt (&d_key, subject, key);
  442. if ((ret = db->del (db, NULL, &d_key, 0)) != 0) {
  443. jack_error ("Cannot delete key %s (%s)", key, db_strerror (ret));
  444. return -1;
  445. }
  446. jack_property_change_notify (client, subject, key, PropertyDeleted);
  447. return 0;
  448. }
  449. int
  450. jack_remove_properties (jack_client_t* client, jack_uuid_t subject)
  451. {
  452. DBT key;
  453. DBT data;
  454. DBC* cursor;
  455. int ret;
  456. char ustr[JACK_UUID_STRING_SIZE];
  457. int retval = 0;
  458. uint32_t cnt = 0;
  459. jack_uuid_unparse (subject, ustr);
  460. if (jack_property_init (NULL)) {
  461. return -1;
  462. }
  463. if ((ret = db->cursor (db, NULL, &cursor, 0)) != 0) {
  464. jack_error ("Cannot create cursor for metadata search (%s)", db_strerror (ret));
  465. return -1;
  466. }
  467. memset(&key, 0, sizeof(key));
  468. memset(&data, 0, sizeof(data));
  469. data.flags = DB_DBT_MALLOC;
  470. while ((ret = cursor->get(cursor, &key, &data, DB_NEXT)) == 0) {
  471. /* require 2 extra chars (data+null) for key,
  472. which is composed of UUID str plus a key name
  473. */
  474. if (key.size < JACK_UUID_STRING_SIZE + 2) {
  475. if (data.size > 0) {
  476. free (data.data);
  477. }
  478. continue;
  479. }
  480. if (memcmp (ustr, key.data, JACK_UUID_STRING_SIZE) != 0) {
  481. /* not relevant */
  482. if (data.size > 0) {
  483. free (data.data);
  484. }
  485. continue;
  486. }
  487. if ((ret = cursor->del (cursor, 0)) != 0) {
  488. jack_error ("cannot delete property (%s)", db_strerror (ret));
  489. /* don't return -1 here since this would leave things
  490. even more inconsistent. wait till the cursor is finished
  491. */
  492. retval = -1;
  493. }
  494. cnt++;
  495. }
  496. cursor->close (cursor);
  497. if (cnt) {
  498. jack_property_change_notify (client, subject, NULL, PropertyDeleted);
  499. }
  500. if (retval) {
  501. return -1;
  502. }
  503. return cnt;
  504. }
  505. int
  506. jack_remove_all_properties (jack_client_t* client)
  507. {
  508. int ret;
  509. jack_uuid_t empty_uuid;
  510. if (jack_property_init (NULL)) {
  511. return -1;
  512. }
  513. if ((ret = db->truncate (db, NULL, NULL, 0)) != 0) {
  514. jack_error ("Cannot clear properties (%s)", db_strerror (ret));
  515. return -1;
  516. }
  517. jack_uuid_clear (empty_uuid);
  518. jack_property_change_notify (client, empty_uuid, NULL, PropertyDeleted);
  519. return 0;
  520. }