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.

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