MySQL realated stuff documented.
[mediadatabase.git] / libfrontend / conf.c
blobdf8cf6bfa061142accc8df72f0219dd8c0cecb76
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*****************************************************************************
4 * $Id: conf.c,v 1.3 2005/02/19 20:58:32 nedko Exp $
6 * DESCRIPTION:
7 * Mediadatabase configuration file handling.
9 * AUTHOR:
10 * Nedko Arnaudov <nedko@users.sourceforge.net>
12 * LICENSE:
13 * GNU GENERAL PUBLIC LICENSE version 2
15 *****************************************************************************/
17 #include <stdlib.h>
18 #include <string.h>
19 #include <cfl.h>
21 #include "../result.h"
22 #include "conf.h"
23 #include "error.h"
24 #include "db.h"
25 #include "disk.h"
27 #define CONF_FILE ".mediadatabase"
28 #define CFL_SECTION_GLOBAL NULL
29 #define CFL_SECTION_CUI "cui"
30 #define CFL_SECTION_MYSQL "MySQL"
31 #define CFL_SECTION_SQLITE "SQLite"
32 #define CFL_VALUE_MYSQL_HOST "host"
33 #define CFL_VALUE_MYSQL_USER "user"
34 #define CFL_VALUE_MYSQL_PASS "pass"
35 #define CFL_VALUE_MYSQL_DB "db"
36 #define CFL_VALUE_MOUNT "mount"
37 #define CFL_VALUE_UNMOUNT "unmount"
38 #define CFL_VALUE_MOUNTDIR "mountdir"
39 #define CFL_VALUE_SQLITE_DB "dbfile"
40 #define CFL_VALUE_BACKEND "backend"
42 static cflh_t g_hConf = NULL;
44 mediadb_result
45 conf_get_full_name(char ** ppszConf)
47 const char *pszHOME;
48 char *pszConf;
49 size_t s;
51 pszHOME = getenv("HOME");
53 if (pszHOME == NULL)
55 mediadb_error_callback(
56 MEDIADB_ERROR_CRITICAL,
57 "HOME environment variable not set.");
58 return MEDIADB_FAIL;
61 s = strlen(pszHOME) + 1 + strlen(CONF_FILE) + 1;
63 pszConf = (char *)malloc(s);
64 if (pszConf == NULL)
66 mediadb_error_callback(
67 MEDIADB_ERROR_CRITICAL,
68 "Out of memory.");
69 return MEDIADB_MEM;
72 sprintf(pszConf, "%s/%s", pszHOME, CONF_FILE);
74 *ppszConf = pszConf;
76 return MEDIADB_OK;
79 mediadb_result
80 conf_parse()
82 mediadb_result r;
83 char *pszConf;
84 int nRet;
85 FILE *hFile;
86 char *pszCFLValue;
87 cfl_value_type_t CFLValueType;
89 hFile = NULL;
90 pszConf = NULL;
92 conf_cleanup();
94 r = conf_get_full_name(&pszConf);
95 if (MEDIADB_IS_ERROR(r))
97 goto Exit;
100 hFile = fopen(pszConf, "r");
101 if (hFile == NULL)
103 mediadb_error_printf(
104 MEDIADB_ERROR_CRITICAL,
105 "Cannot open configuration file \"%s\".",
106 pszConf);
107 r = MEDIADB_FAIL;
108 goto Exit;
111 g_hConf = cfl_alloc();
112 if (g_hConf == NULL)
114 mediadb_error_callback(
115 MEDIADB_ERROR_CRITICAL,
116 "Out of memory.");
117 r = MEDIADB_MEM;
118 goto Exit;
121 nRet = cfl_init_from_file(
122 g_hConf,
123 hFile,
124 CFL_FILE_TYPE_DEFAULT);
125 if (nRet != 0)
127 mediadb_error_printf(
128 MEDIADB_ERROR_CRITICAL,
129 "cfl_init_from_file() failed. (%s)",
130 cfl_errcode_to_message(nRet));
131 goto Exit;
134 pszCFLValue = cfl_value_get_by_name(
135 g_hConf,
136 CFL_SECTION_GLOBAL,
137 CFL_VALUE_MOUNT,
138 &CFLValueType);
140 if (pszCFLValue != NULL &&
141 CFLValueType == CFL_TYPE_STRING)
143 r = disk_set_mount_command(pszCFLValue);
144 if (MEDIADB_IS_ERROR(r))
146 goto Exit;
150 pszCFLValue = cfl_value_get_by_name(
151 g_hConf,
152 CFL_SECTION_GLOBAL,
153 CFL_VALUE_UNMOUNT,
154 &CFLValueType);
156 if (pszCFLValue != NULL &&
157 CFLValueType == CFL_TYPE_STRING)
159 r = disk_set_unmount_command(pszCFLValue);
160 if (MEDIADB_IS_ERROR(r))
162 goto Exit;
166 pszCFLValue = cfl_value_get_by_name(
167 g_hConf,
168 CFL_SECTION_GLOBAL,
169 CFL_VALUE_MOUNTDIR,
170 &CFLValueType);
172 if (pszCFLValue != NULL &&
173 CFLValueType == CFL_TYPE_STRING)
175 r = disk_set_path(pszCFLValue);
176 if (MEDIADB_IS_ERROR(r))
178 goto Exit;
182 pszCFLValue = cfl_value_get_by_name(
183 g_hConf,
184 CFL_SECTION_MYSQL,
185 CFL_VALUE_MYSQL_HOST,
186 &CFLValueType);
188 if (pszCFLValue != NULL &&
189 CFLValueType == CFL_TYPE_STRING)
191 r = db_set_mysql_host(pszCFLValue);
192 if (MEDIADB_IS_ERROR(r))
194 goto Exit;
198 pszCFLValue = cfl_value_get_by_name(
199 g_hConf,
200 CFL_SECTION_MYSQL,
201 CFL_VALUE_MYSQL_DB,
202 &CFLValueType);
204 if (pszCFLValue != NULL &&
205 CFLValueType == CFL_TYPE_STRING)
207 r = db_set_mysql_database(pszCFLValue);
208 if (MEDIADB_IS_ERROR(r))
210 goto Exit;
214 pszCFLValue = cfl_value_get_by_name(
215 g_hConf,
216 CFL_SECTION_MYSQL,
217 CFL_VALUE_MYSQL_USER,
218 &CFLValueType);
220 if (pszCFLValue != NULL &&
221 CFLValueType == CFL_TYPE_STRING)
223 r = db_set_mysql_user(pszCFLValue);
224 if (MEDIADB_IS_ERROR(r))
226 goto Exit;
230 pszCFLValue = cfl_value_get_by_name(
231 g_hConf,
232 CFL_SECTION_MYSQL,
233 CFL_VALUE_MYSQL_PASS,
234 &CFLValueType);
236 if (pszCFLValue != NULL &&
237 CFLValueType == CFL_TYPE_STRING)
239 r = db_set_mysql_pass(pszCFLValue);
240 if (MEDIADB_IS_ERROR(r))
242 goto Exit;
246 pszCFLValue = cfl_value_get_by_name(
247 g_hConf,
248 CFL_SECTION_SQLITE,
249 CFL_VALUE_SQLITE_DB,
250 &CFLValueType);
252 if (pszCFLValue != NULL &&
253 CFLValueType == CFL_TYPE_STRING)
255 r = db_set_sqlite_database(pszCFLValue);
256 if (MEDIADB_IS_ERROR(r))
258 goto Exit;
262 pszCFLValue = cfl_value_get_by_name(
263 g_hConf,
264 CFL_SECTION_GLOBAL,
265 CFL_VALUE_BACKEND,
266 &CFLValueType);
268 if (pszCFLValue != NULL &&
269 CFLValueType == CFL_TYPE_STRING)
271 if (strcmp(pszCFLValue, CFL_SECTION_SQLITE) == 0)
273 db_use_sqlite();
275 else if (strcmp(pszCFLValue, CFL_SECTION_MYSQL) == 0)
277 db_use_mysql();
281 r = MEDIADB_OK;
283 Exit:
284 if (hFile != NULL)
286 fclose(hFile);
289 if (pszConf != NULL)
291 free(pszConf);
294 return r;
297 mediadb_result
298 conf_write()
300 mediadb_result r;
301 char *pszConf;
302 FILE *hFile;
303 int nRet;
304 unsigned int nDBType;
306 hFile = NULL;
307 pszConf = NULL;
309 if (g_hConf == NULL)
311 mediadb_error_printf(
312 MEDIADB_ERROR_CRITICAL,
313 "Cannot write configuration file without first parsing it.");
314 return MEDIADB_UNEXP;
317 r = conf_get_full_name(&pszConf);
318 if (MEDIADB_IS_ERROR(r))
320 goto Exit;
323 hFile = fopen(pszConf, "w");
324 if (hFile == NULL)
326 mediadb_error_printf(
327 MEDIADB_ERROR_CRITICAL,
328 "Cannot open configuration file \"%s\".",
329 pszConf);
330 r = MEDIADB_FAIL;
331 goto Exit;
334 nRet = cfl_value_set_by_name(
335 g_hConf,
336 CFL_SECTION_GLOBAL,
337 CFL_VALUE_MOUNT,
338 CFL_TYPE_STRING,
339 (void *)disk_get_mount_command());
340 if (nRet != 0)
342 mediadb_error_printf(
343 MEDIADB_ERROR_CRITICAL,
344 "cfl_value_set_by_name() failed with %d",
345 nRet);
346 r = MEDIADB_FAIL;
347 goto Exit;
350 nRet = cfl_value_set_by_name(
351 g_hConf,
352 CFL_SECTION_GLOBAL,
353 CFL_VALUE_UNMOUNT,
354 CFL_TYPE_STRING,
355 (void *)disk_get_unmount_command());
356 if (nRet != 0)
358 mediadb_error_printf(
359 MEDIADB_ERROR_CRITICAL,
360 "cfl_value_set_by_name() failed with %d",
361 nRet);
362 r = MEDIADB_FAIL;
363 goto Exit;
366 nRet = cfl_value_set_by_name(
367 g_hConf,
368 CFL_SECTION_GLOBAL,
369 CFL_VALUE_MOUNTDIR,
370 CFL_TYPE_STRING,
371 (void *)disk_get_path());
372 if (nRet != 0)
374 mediadb_error_printf(
375 MEDIADB_ERROR_CRITICAL,
376 "cfl_value_set_by_name() failed with %d",
377 nRet);
378 r = MEDIADB_FAIL;
379 goto Exit;
382 nRet = cfl_value_set_by_name(
383 g_hConf,
384 CFL_SECTION_MYSQL,
385 CFL_VALUE_MYSQL_HOST,
386 CFL_TYPE_STRING,
387 (void *)db_get_mysql_host());
388 if (nRet != 0)
390 mediadb_error_printf(
391 MEDIADB_ERROR_CRITICAL,
392 "cfl_value_set_by_name() failed with %d",
393 nRet);
394 r = MEDIADB_FAIL;
395 goto Exit;
398 nRet = cfl_value_set_by_name(
399 g_hConf,
400 CFL_SECTION_MYSQL,
401 CFL_VALUE_MYSQL_USER,
402 CFL_TYPE_STRING,
403 (void *)db_get_mysql_user());
404 if (nRet != 0)
406 mediadb_error_printf(
407 MEDIADB_ERROR_CRITICAL,
408 "cfl_value_set_by_name() failed with %d",
409 nRet);
410 r = MEDIADB_FAIL;
411 goto Exit;
414 nRet = cfl_value_set_by_name(
415 g_hConf,
416 CFL_SECTION_MYSQL,
417 CFL_VALUE_MYSQL_PASS,
418 CFL_TYPE_STRING,
419 (void *)db_get_mysql_pass());
420 if (nRet != 0)
422 mediadb_error_printf(
423 MEDIADB_ERROR_CRITICAL,
424 "cfl_value_set_by_name() failed with %d",
425 nRet);
426 r = MEDIADB_FAIL;
427 goto Exit;
430 nRet = cfl_value_set_by_name(
431 g_hConf,
432 CFL_SECTION_MYSQL,
433 CFL_VALUE_MYSQL_DB,
434 CFL_TYPE_STRING,
435 (void *)db_get_mysql_database());
436 if (nRet != 0)
438 mediadb_error_printf(
439 MEDIADB_ERROR_CRITICAL,
440 "cfl_value_set_by_name() failed with %d",
441 nRet);
442 r = MEDIADB_FAIL;
443 goto Exit;
446 nRet = cfl_value_set_by_name(
447 g_hConf,
448 CFL_SECTION_SQLITE,
449 CFL_VALUE_SQLITE_DB,
450 CFL_TYPE_STRING,
451 (void *)db_get_sqlite_database());
452 if (nRet != 0)
454 mediadb_error_printf(
455 MEDIADB_ERROR_CRITICAL,
456 "cfl_value_set_by_name() failed with %d",
457 nRet);
458 r = MEDIADB_FAIL;
459 goto Exit;
462 nDBType = db_get_type();
463 if (nDBType == MEDIADB_DBTYPE_MYSQL)
465 nRet = cfl_value_set_by_name(
466 g_hConf,
467 CFL_SECTION_GLOBAL,
468 CFL_VALUE_BACKEND,
469 CFL_TYPE_STRING,
470 (void *)CFL_SECTION_MYSQL);
471 if (nRet != 0)
473 mediadb_error_printf(
474 MEDIADB_ERROR_CRITICAL,
475 "cfl_value_set_by_name() failed with %d",
476 nRet);
477 r = MEDIADB_FAIL;
478 goto Exit;
481 else if (nDBType == MEDIADB_DBTYPE_SQLITE)
483 nRet = cfl_value_set_by_name(
484 g_hConf,
485 CFL_SECTION_GLOBAL,
486 CFL_VALUE_BACKEND,
487 CFL_TYPE_STRING,
488 (void *)CFL_SECTION_SQLITE);
489 if (nRet != 0)
491 mediadb_error_printf(
492 MEDIADB_ERROR_CRITICAL,
493 "cfl_value_set_by_name() failed with %d",
494 nRet);
495 r = MEDIADB_FAIL;
496 goto Exit;
499 else
501 mediadb_error_printf(
502 MEDIADB_ERROR_CRITICAL,
503 "unknown backend type %u",
504 nRet);
505 r = MEDIADB_UNEXP;
506 goto Exit;
509 cfl_write(g_hConf, hFile);
511 r = MEDIADB_OK;
513 Exit:
514 if (hFile != NULL)
516 fclose(hFile);
519 if (pszConf != NULL)
521 free(pszConf);
524 return r;
527 void
528 conf_cleanup()
530 if (g_hConf != NULL)
532 cfl_free(g_hConf);
533 g_hConf = NULL;
537 /*****************************************************************************
539 * Modifications log:
541 * !!! WARNING !!! Following lines are automatically updated by the CVS system.
543 * $Log: conf.c,v $
544 * Revision 1.3 2005/02/19 20:58:32 nedko
545 * Implement conf file writing.
547 * Revision 1.2 2004/05/21 23:42:49 nedko
548 * mediadb_error_callback() now tells if error is critical.
550 * Revision 1.1 2004/05/16 19:01:17 nedko
551 * libfrontend holds code common to frontends but not in libdb.
553 *****************************************************************************/