Add SQLite support.
[mediadatabase.git] / cui / main.c
blobb29be9828b53057684716197395b8ad0c5abb29d
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*****************************************************************************
4 * DESCRIPTION:
5 * Portable frontend to MediaDatabase
7 * COMPILATION:
8 * gcc main.c -lmysqlclient -o mdb_cui [-DOPENBSD]
10 * AUTHOR:
11 * Nedko Arnaudov <nedko@users.sourceforge.net>
13 * LICENSE:
14 * GNU GENERAL PUBLIC LICENSE version 2
16 *****************************************************************************/
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <sys/types.h>
22 #include <dirent.h>
23 #include <sys/stat.h>
24 #include <errno.h>
25 #include <time.h>
26 #include <sys/wait.h>
27 #include <pwd.h>
28 #include <unistd.h>
29 #include <cfl.h>
30 #include <assert.h>
31 extern char **environ;
33 #include "../libdb/libdb.h"
34 #include "../libdb/memory.h"
36 #define MB_T_AUDIO 1
37 #define MB_T_DATA 2
38 #define MB_T_EMPTY 3
40 #define MAX_TITLE_SIZE 128 /* in db */
41 #define MAX_PASS_SIZE 1024
42 #define DEFAULT_MYSQL_USER "mediadatabase"
43 #define DEFAULT_MYSQL_PASS ""
44 #define DEFAULT_MYSQL_HOST "localhost"
45 #define DEFAULT_MYSQL_DB "mediadatabase"
46 #if defined(OPENBSD)
47 #define DEFAULT_MOUNT_CMD "sudo /sbin/mount /cdrom"
48 #define DEFAULT_UNMOUNT_CMD "sudo /sbin/umount /cdrom"
49 #else
50 #define DEFAULT_MOUNT_CMD "/bin/mount /cdrom"
51 #define DEFAULT_UNMOUNT_CMD "/bin/umount /cdrom"
52 #endif
53 #define DEFAULT_MOUNTDIR "/cdrom"
54 #define MAX_PATH_DEPTH 65535
55 #define DEFAULT_SQLITE_DB "mediadatabase.sqlitedb"
56 #define DEFAULT_BACKEND MEDIADB_DBTYPE_SQLITE
58 #define CONF_FILE ".mediadatabase"
59 #define CFL_SECTION_GLOBAL NULL
60 #define CFL_SECTION_CUI "cui"
61 #define CFL_SECTION_MYSQL "MySQL"
62 #define CFL_SECTION_SQLITE "SQLite"
63 #define CFL_VALUE_MYSQL_HOST "host"
64 #define CFL_VALUE_MYSQL_USER "user"
65 #define CFL_VALUE_MYSQL_PASS "pass"
66 #define CFL_VALUE_MYSQL_DB "db"
67 #define CFL_VALUE_MOUNT "mount"
68 #define CFL_VALUE_UNMOUNT "unmount"
69 #define CFL_VALUE_MOUNTDIR "mountdir"
70 #define CFL_VALUE_SQLITE_DB "dbfile"
71 #define CFL_VALUE_BACKEND "backend"
73 //#define PRINT_FILENAMES
75 char strTitle[MAX_TITLE_SIZE+2]; /* + newline + null */
76 char strPass[MAX_PASS_SIZE];
77 char *pszPathBuffer = NULL;
78 size_t nPathBufferSize = 0;
79 size_t nMountPrefixLength;
80 unsigned long nTotalFiles;
81 unsigned long long nTotalSize;
82 mediadb_uint nMediaID = 0ULL;
83 mediadb hDB = MEDIADB_INVALID_VALUE;
84 int blnMediaMounted = 0;
85 int blnExiting = 0;
86 char *pszHost = NULL;
87 char *pszUser = NULL;
88 char *pszPass = NULL;
89 char *pszDB = NULL;
90 char *pszMountMediaCommand = NULL;
91 char *pszUnmountMediaCommand = NULL;
92 char *pszMountDir = NULL;
93 char *pszTitle = NULL;
94 char *pszDBSQLite = NULL;
95 const char *pszHOME;
96 unsigned int nDBType = 0;
98 #define MODE_LIST_MEDIA 0
99 #define MODE_ADD_MEDIA 1
100 #define MODE_UPDATE_MEDIA 2
101 unsigned int nMode = MODE_LIST_MEDIA;
103 void
104 ExecuteCommand(const char *pszCommand);
106 void
107 MountMedia()
109 printf("--> Mounting media ...\n");
110 ExecuteCommand(pszMountMediaCommand);
111 printf("--> Media mounted.\n");
112 blnMediaMounted = 1;
115 void
116 UnmountMedia()
118 printf("--> Unmounting media ...\n");
119 ExecuteCommand(pszUnmountMediaCommand);
120 printf("--> Media unmounted.\n");
121 blnMediaMounted = 0;
124 void
125 Exit(int nRet)
127 mediadb_result r;
129 if (blnExiting)
130 return;
132 blnExiting = 1;
134 if (hDB != MEDIADB_INVALID_VALUE)
136 r = mediadb_close(hDB);
139 if (blnMediaMounted)
140 UnmountMedia();
142 if (pszHost != NULL)
143 free(pszHost);
145 if (pszUser != NULL)
146 free(pszUser);
148 if (pszPass != NULL)
149 free(pszPass);
151 if (pszDB != NULL)
152 free(pszDB);
154 if (pszMountMediaCommand != NULL)
155 free(pszMountMediaCommand);
157 if (pszUnmountMediaCommand != NULL)
158 free(pszUnmountMediaCommand);
160 if (pszMountDir != NULL)
161 free(pszMountDir);
163 if (pszTitle != NULL)
164 free(pszTitle);
166 exit(nRet);
169 void
170 ExecuteCommand(const char *pszCommand)
172 int nRet;
174 nRet = system(pszCommand);
175 if (nRet == -1)
177 fprintf(stderr,
178 "Cannot execute command \"%s\". "
179 "system() returned -1, error is %d (%s)\n",
180 pszCommand,
181 errno,
182 strerror(errno));
183 Exit(1);
186 if (WIFEXITED(nRet))
188 if (WEXITSTATUS(nRet) != 0)
190 fprintf(stderr,
191 "Cannot execute command \"%s\". "
192 "Command returned %d\n",
193 pszCommand,
194 (int)WEXITSTATUS(nRet));
195 Exit(1);
198 else if (WIFSIGNALED(nRet))
200 fprintf(stderr,
201 "Cannot execute command \"%s\". "
202 "Cammand was terminated because of signal %d\n",
203 pszCommand,
204 (int)WTERMSIG(nRet));
205 Exit(1);
207 else
209 fprintf(stderr,
210 "Cannot execute command \"%s\". "
211 "system() returned %d\n",
212 pszCommand,
213 nRet);
214 Exit(1);
218 void
219 ReadTitleExitOnEmpty()
221 char *pch;
223 printf("Title: ");
224 if (fgets(strTitle, sizeof(strTitle), stdin) == NULL)
226 printf("\n");
228 if (feof(stdin))
230 Exit(0);
233 fprintf(stderr, "Error occured while reading standard input.\n");
234 Exit(1);
236 else if (strTitle[0] == '\n')
238 Exit(0);
241 if ((pch = strchr(strTitle, '\n')) == NULL)
243 fprintf(stderr, "input line too long.\n");
244 Exit(1);
247 *pch = '\0';
250 void
251 ReadMediaID()
253 char *pch;
254 char strMediaID[100];
256 printf("Media ID: ");
257 if (fgets(strMediaID, sizeof(strMediaID), stdin) == NULL)
259 printf("\n");
261 if (feof(stdin))
263 Exit(0);
266 fprintf(stderr, "Error occured while reading standard input.\n");
267 Exit(1);
269 else if (strMediaID[0] == '\n')
271 Exit(0);
274 if ((pch = strchr(strMediaID, '\n')) == NULL)
276 fprintf(stderr, "input line too long.\n");
277 Exit(1);
280 *pch = '\0';
282 nMediaID = strtoull(strMediaID, NULL, 10);
283 if (nMediaID == 0)
285 Exit(1);
289 void
290 ReadTitleIgnoreOnEmpty()
292 char *pch;
294 printf("Title: ");
295 if (fgets(strTitle, sizeof(strTitle), stdin) == NULL)
297 printf("\n");
299 if (feof(stdin))
301 strcpy(strTitle, pszTitle);
302 return;
305 fprintf(stderr, "Error occured while reading standard input.\n");
306 Exit(1);
308 else if (strTitle[0] == '\n')
310 strcpy(strTitle, pszTitle);
311 return;
314 if ((pch = strchr(strTitle, '\n')) == NULL)
316 fprintf(stderr, "input line too long.\n");
317 Exit(1);
320 *pch = '\0';
323 void
324 AskForPassword()
326 char *pszPass;
328 pszPass = getpass("MySQL password: ");
330 strcpy(strPass, pszPass);
333 void
334 MaybeEnlargeBuffer(char ** ppBuffer,
335 size_t *pnBufferSize,
336 size_t nSizeRequired)
338 mediadb_result r;
340 r = maybe_enlarge_buffer(
341 ppBuffer,
342 pnBufferSize,
343 nSizeRequired);
344 if (MEDIADB_IS_ERROR(r))
346 fprintf(stderr,
347 "Cannot enlarge buffer.\n");
348 Exit(0);
352 void
353 ScanDir(unsigned long nDepth)
355 mediadb_result r;
356 DIR *pDir;
357 struct dirent *pDE;
358 size_t nCurrentPathLength;
359 struct stat st;
360 size_t sizeName;
362 if (nDepth >= MAX_PATH_DEPTH)
364 fprintf(stderr,
365 "Max path depth of reached \"%u\"\n",
366 (unsigned int)MAX_PATH_DEPTH);
367 Exit(1);
370 nCurrentPathLength = strlen(pszPathBuffer);
372 pDir = opendir(pszPathBuffer);
373 if (pDir == NULL)
375 fprintf(stderr, "Cannot open \"%s\"\n", pszPathBuffer);
376 Exit(1);
379 if (pDir)
381 while ((pDE = readdir(pDir)) != NULL)
383 if (strcmp(pDE->d_name, ".") == 0)
384 continue;
386 if (strcmp(pDE->d_name, "..") == 0)
387 continue;
389 #if defined(OPENBSD)
390 sizeName = pDE->d_namlen;
391 #else
392 sizeName = strlen(pDE->d_name);
393 #endif
395 MaybeEnlargeBuffer(&pszPathBuffer,
396 &nPathBufferSize,
397 nCurrentPathLength + sizeName + 1);
399 memcpy(pszPathBuffer + nCurrentPathLength, pDE->d_name, sizeName);
400 pszPathBuffer[nCurrentPathLength + sizeName] = 0;
402 if (lstat(pszPathBuffer, &st) != 0)
404 fprintf(stderr,
405 "Cannot stat() %s. Error is %d (%s)\n",
406 pszPathBuffer,
407 errno,
408 strerror(errno));
411 pszPathBuffer[nCurrentPathLength] = 0;
413 r = mediadb_file_add_new(
414 hDB,
415 nMediaID,
416 (S_ISDIR(st.st_mode))?MEDIADB_FILETYPE_DIR:MEDIADB_FILETYPE_FILE,
417 pszPathBuffer + nMountPrefixLength,
418 pDE->d_name,
419 ((S_ISDIR(st.st_mode))?0:st.st_size),
420 st.st_ctime);
421 if (MEDIADB_IS_ERROR(r))
423 fprintf(stderr,
424 "Failed to add new file to database. Error is %d (%s)\n",
425 (int)r,
426 mediadb_get_error_message(hDB));
429 memcpy(pszPathBuffer + nCurrentPathLength, pDE->d_name, sizeName);
430 pszPathBuffer[nCurrentPathLength + sizeName] = 0;
432 #ifdef PRINT_FILENAMES
433 printf("%s", pszPathBuffer + nMountPrefixLength);
434 #endif
436 nTotalFiles++;
438 if (S_ISDIR(st.st_mode))
440 #ifdef PRINT_FILENAMES
441 printf("/\n");
442 #else
443 printf("/");
444 #endif
445 fflush(stdout);
446 memcpy(pszPathBuffer + nCurrentPathLength + sizeName, "/", 2);
447 ScanDir(nDepth + 1);
449 else
451 nTotalSize += st.st_size;
452 #ifdef PRINT_FILENAMES
453 printf("\n");
454 #else
455 printf(".");
456 #endif
457 fflush(stdout);
461 closedir(pDir);
465 void
466 SetDefaults()
468 size_t s;
470 if (pszHost == NULL)
472 pszHost = strdup(DEFAULT_MYSQL_HOST);
473 if (pszHost == NULL)
475 fprintf(stderr,
476 "Out of memory.\n");
477 Exit(1);
481 if (pszUser == NULL)
483 pszUser = strdup(DEFAULT_MYSQL_USER);
484 if (pszUser == NULL)
486 fprintf(stderr,
487 "Out of memory.\n");
488 Exit(1);
492 if (pszPass == NULL)
494 pszPass = strdup(DEFAULT_MYSQL_PASS);
495 if (pszPass == NULL)
497 fprintf(stderr,
498 "Out of memory.\n");
499 Exit(1);
503 if (pszDB == NULL)
505 pszDB = strdup(DEFAULT_MYSQL_DB);
506 if (pszDB == NULL)
508 fprintf(stderr,
509 "Out of memory.\n");
510 Exit(1);
514 if (pszMountMediaCommand == NULL)
516 pszMountMediaCommand = strdup(DEFAULT_MOUNT_CMD);
517 if (pszMountMediaCommand == NULL)
519 fprintf(stderr,
520 "Out of memory.\n");
521 Exit(1);
525 if (pszUnmountMediaCommand == NULL)
527 pszUnmountMediaCommand = strdup(DEFAULT_UNMOUNT_CMD);
528 if (pszUnmountMediaCommand == NULL)
530 fprintf(stderr,
531 "Out of memory.\n");
532 Exit(1);
536 if (pszMountDir == NULL)
538 pszMountDir = strdup(DEFAULT_MOUNTDIR);
539 if (pszMountDir == NULL)
541 fprintf(stderr,
542 "Out of memory.\n");
543 Exit(1);
547 if (pszDBSQLite == NULL)
549 s = strlen(pszHOME) + strlen(DEFAULT_SQLITE_DB)+2;
550 pszDBSQLite = malloc(s);
551 if (pszDBSQLite == NULL)
553 fprintf(stderr,
554 "Out of memory.\n");
555 Exit(1);
557 sprintf(pszDBSQLite,
558 "%s/%s",
559 pszHOME,
560 DEFAULT_SQLITE_DB);
563 if (nDBType == 0)
565 nDBType = DEFAULT_BACKEND;
569 void
570 Help(char *arg0)
572 char *pszExecutable;
574 pszExecutable = strrchr(arg0, '/');
576 if (pszExecutable == NULL)
578 pszExecutable = arg0;
580 else
582 pszExecutable++;
585 SetDefaults();
587 printf("==============================\n");
588 printf("Offline media content database\n");
589 printf("==============================\n");
590 printf("Usage:\n");
591 printf("[<path_to>]%s [options]\n", pszExecutable);
592 printf(" Options can be:\n");
593 printf(" -? - This help\n");
594 printf(" -L - List media mode\n");
595 printf(" -A - Add media mode\n");
596 printf(" -U - Update media mode\n");
597 printf(" -m <mount_cmd> - command to mount media, default is \"%s\"\n", pszMountMediaCommand);
598 printf(" -n <unmount_cmd> - command to unmount, default is \"%s\"\n", pszUnmountMediaCommand);
599 printf(" -M - Use MySQL backend%s\n", (nDBType == MEDIADB_DBTYPE_MYSQL)?" (default)":"");
600 printf(" -d <mount_dir> - where media is mounted, default is \"%s\"\n", pszMountDir);
601 printf(" -h <mysql_host> - MySQL server host, default is \"%s\"\n", pszHost);
602 printf(" -u <mysql_user> - MySQL user, default is \"%s\"\n", pszUser);
603 printf(" -p [mysql_pass] - MySQL password, default is \"%s\"\n", pszPass);
604 printf(" -b <mysql_database> - MySQL database, default is \"%s\"\n", pszDB);
605 printf(" -S - Use SQLite backend%s\n", (nDBType == MEDIADB_DBTYPE_SQLITE)?" (default)":"");
606 printf(" -f <sqlite_database_file> - SQLite database file, default is \"%s\"\n", pszDBSQLite);
607 Exit(0);
610 void
611 AddMedia()
613 mediadb_result r;
614 time_t timeAdded;
616 printf("NOTE: Currently audio media is not supported.\n");
618 Loop:
619 memcpy(pszPathBuffer + nMountPrefixLength, "/", 2);
621 printf("--> Please insert media and enter title.\n");
623 ReadTitleExitOnEmpty();
625 MountMedia();
627 printf("--> Processing media ...\n");
629 r = mediadb_media_add_new(
630 hDB,
631 strTitle,
633 MEDIADB_MT_DATA,
634 &nMediaID);
636 if (MEDIADB_IS_ERROR(r))
638 fprintf(stderr,
639 "Failed to add new media to database. Error is %d (%s)\n",
640 (int)r,
641 mediadb_get_error_message(hDB));
642 Exit(1);
645 ScanDir(0);
647 printf("\n");
649 timeAdded = time(NULL);
651 r = mediadb_media_update_properties(
652 hDB,
653 nMediaID,
654 timeAdded,
655 nTotalFiles,
656 nTotalSize);
658 if (MEDIADB_IS_ERROR(r))
660 fprintf(stderr,
661 "Failed to update new media to database. Error is %d (%s)\n",
662 (int)r,
663 mediadb_get_error_message(hDB));
664 Exit(1);
667 UnmountMedia();
669 printf("--> SUCCESS - Media added.\n");
670 printf("Media title: %s\n", strTitle);
671 printf("Media ID: %u\n", (unsigned int)nMediaID);
672 printf("Added: %s", ctime(&timeAdded));
673 printf("Total files: %u\n", (unsigned int)nTotalFiles);
674 printf("Total size: %u\n", (unsigned int)nTotalSize);
676 goto Loop;
679 void
680 UpdateMedia()
682 mediadb_result r;
683 time_t timeAdded;
684 mediadb_uint nTimeAdded;
685 mediadb_uint nTotalFiles;
686 mediadb_uint nTotalSize;
688 printf("NOTE: Currently audio media is not supported.\n");
690 Loop:
691 printf("--> Please enter Media ID.\n");
693 ReadMediaID();
695 memcpy(pszPathBuffer + nMountPrefixLength, "/", 2);
697 r = mediadb_media_get_properties(
698 hDB,
699 nMediaID,
700 &nTimeAdded,
701 &nTotalFiles,
702 &nTotalSize,
703 &pszTitle);
704 if (MEDIADB_IS_ERROR(r))
706 fprintf(stderr,
707 "Failed to get media properties. Error is %d (%s)\n",
708 (int)r,
709 mediadb_get_error_message(hDB));
710 Exit(1);
713 if (strlen(pszTitle) + 1 > MAX_TITLE_SIZE)
715 fprintf(stderr,
716 "Title too big\n");
717 Exit(1);
720 timeAdded = (time_t)nTimeAdded;
722 printf("Media title: %s\n", pszTitle);
723 printf("Added: %s", ctime(&timeAdded));
724 printf("Total files: %u\n", (unsigned int)nTotalFiles);
725 printf("Total size: %u\n", (unsigned int)nTotalSize);
727 printf("--> Please insert media and enter title (Just press return to keep current title).\n");
729 ReadTitleIgnoreOnEmpty();
731 MountMedia();
733 printf("--> Processing media ...\n");
735 if (strcmp(pszTitle, strTitle) != 0)
737 r = mediadb_media_update_name(
738 hDB,
739 nMediaID,
740 strTitle);
741 if (MEDIADB_IS_ERROR(r))
743 fprintf(stderr,
744 "Cannot update media title. Error is %d (%s)\n",
745 (int)r,
746 mediadb_get_error_message(hDB));
747 Exit(1);
751 r = mediadb_delete_media_files(
752 hDB,
753 nMediaID);
755 if (MEDIADB_IS_ERROR(r))
757 fprintf(stderr,
758 "Failed to delete media files in database. Error is %d (%s)\n",
759 (int)r,
760 mediadb_get_error_message(hDB));
761 Exit(1);
764 ScanDir(0);
766 printf("\n");
768 //timeAdded = time(NULL);
770 r = mediadb_media_update_properties(
771 hDB,
772 nMediaID,
773 timeAdded,
774 nTotalFiles,
775 nTotalSize);
777 if (MEDIADB_IS_ERROR(r))
779 fprintf(stderr,
780 "Failed to update new media to database. Error is %d (%s)\n",
781 (int)r,
782 mediadb_get_error_message(hDB));
783 Exit(1);
786 UnmountMedia();
788 printf("--> SUCCESS - Media added.\n");
789 printf("Media title: %s\n", strTitle);
790 printf("Media ID: %u\n", (unsigned int)nMediaID);
791 printf("Added: %s", ctime(&timeAdded));
792 printf("Total files: %u\n", (unsigned int)nTotalFiles);
793 printf("Total size: %u\n", (unsigned int)nTotalSize);
795 goto Loop;
798 void
799 ListMedia()
801 printf("List media mode not implemented yet.\n");
805 main(
806 int argc,
807 char ** argv
810 int i;
811 mediadb_result r;
812 int nRet;
813 cflh_t hConf;
814 FILE *hFile;
815 char *pszConf;
816 size_t s;
817 char *pszCFLValue;
818 cfl_value_type_t CFLValueType;
819 char *pszExecutable;
821 pszExecutable = strrchr(argv[0], '/');
823 if (pszExecutable == NULL)
825 pszExecutable = argv[0];
827 else
829 pszExecutable++;
832 if (strcmp(pszExecutable, "mdb_add") == 0)
834 nMode = MODE_ADD_MEDIA;
836 else if (strcmp(pszExecutable, "mdb_update") == 0)
838 nMode = MODE_UPDATE_MEDIA;
840 else
842 nMode = MODE_LIST_MEDIA;
845 /* Find HOME environment variable */
846 pszHOME = getenv("HOME");
848 if (pszHOME == NULL)
850 fprintf(stderr,
851 "Please set the HOME encironment variable.\n");
852 Exit(1);
855 /* Look into configuration file */
857 s = strlen(pszHOME) + 1 + strlen(CONF_FILE) + 1;
858 pszConf = (char *)malloc(s);
859 sprintf(pszConf, "%s/%s", pszHOME, CONF_FILE);
861 hFile = fopen(pszConf, "r");
862 if (hFile != NULL)
864 hConf = cfl_alloc();
865 if (hConf == NULL)
867 fprintf(stderr,
868 "Out of memory.\n");
869 Exit(1);
872 nRet = cfl_init_from_file(
873 hConf,
874 hFile,
875 CFL_FILE_TYPE_DEFAULT);
876 if (nRet != 0)
878 printf("cfl_init_from_file() failed. (%s)\n", cfl_errcode_to_message(nRet));
881 pszCFLValue = cfl_value_get_by_name(
882 hConf,
883 CFL_SECTION_GLOBAL,
884 CFL_VALUE_MOUNT,
885 &CFLValueType);
887 if (pszCFLValue != NULL &&
888 CFLValueType == CFL_TYPE_STRING)
890 pszMountMediaCommand = strdup(pszCFLValue);
891 if (pszMountMediaCommand == NULL)
893 fprintf(stderr,
894 "Out of memory.\n");
895 Exit(1);
899 pszCFLValue = cfl_value_get_by_name(
900 hConf,
901 CFL_SECTION_GLOBAL,
902 CFL_VALUE_UNMOUNT,
903 &CFLValueType);
905 if (pszCFLValue != NULL &&
906 CFLValueType == CFL_TYPE_STRING)
908 pszUnmountMediaCommand = strdup(pszCFLValue);
909 if (pszUnmountMediaCommand == NULL)
911 fprintf(stderr,
912 "Out of memory.\n");
913 Exit(1);
917 pszCFLValue = cfl_value_get_by_name(
918 hConf,
919 CFL_SECTION_GLOBAL,
920 CFL_VALUE_MOUNTDIR,
921 &CFLValueType);
923 if (pszCFLValue != NULL &&
924 CFLValueType == CFL_TYPE_STRING)
926 pszMountDir = strdup(pszCFLValue);
927 if (pszMountDir == NULL)
929 fprintf(stderr,
930 "Out of memory.\n");
931 Exit(1);
935 pszCFLValue = cfl_value_get_by_name(
936 hConf,
937 CFL_SECTION_MYSQL,
938 CFL_VALUE_MYSQL_HOST,
939 &CFLValueType);
941 if (pszCFLValue != NULL &&
942 CFLValueType == CFL_TYPE_STRING)
944 pszHost = strdup(pszCFLValue);
945 if (pszHost == NULL)
947 fprintf(stderr,
948 "Out of memory.\n");
949 Exit(1);
953 pszCFLValue = cfl_value_get_by_name(
954 hConf,
955 CFL_SECTION_MYSQL,
956 CFL_VALUE_MYSQL_DB,
957 &CFLValueType);
959 if (pszCFLValue != NULL &&
960 CFLValueType == CFL_TYPE_STRING)
962 pszDB = strdup(pszCFLValue);
963 if (pszDB == NULL)
965 fprintf(stderr,
966 "Out of memory.\n");
967 Exit(1);
971 pszCFLValue = cfl_value_get_by_name(
972 hConf,
973 CFL_SECTION_MYSQL,
974 CFL_VALUE_MYSQL_USER,
975 &CFLValueType);
977 if (pszCFLValue != NULL &&
978 CFLValueType == CFL_TYPE_STRING)
980 pszUser = strdup(pszCFLValue);
981 if (pszUser == NULL)
983 fprintf(stderr,
984 "Out of memory.\n");
985 Exit(1);
989 pszCFLValue = cfl_value_get_by_name(
990 hConf,
991 CFL_SECTION_MYSQL,
992 CFL_VALUE_MYSQL_PASS,
993 &CFLValueType);
995 if (pszCFLValue != NULL &&
996 CFLValueType == CFL_TYPE_STRING)
998 pszPass = strdup(pszCFLValue);
999 if (pszPass == NULL)
1001 fprintf(stderr,
1002 "Out of memory.\n");
1003 Exit(1);
1007 pszCFLValue = cfl_value_get_by_name(
1008 hConf,
1009 CFL_SECTION_SQLITE,
1010 CFL_VALUE_SQLITE_DB,
1011 &CFLValueType);
1013 if (pszCFLValue != NULL &&
1014 CFLValueType == CFL_TYPE_STRING)
1016 pszDBSQLite = strdup(pszCFLValue);
1017 if (pszDBSQLite == NULL)
1019 fprintf(stderr,
1020 "Out of memory.\n");
1021 Exit(1);
1025 pszCFLValue = cfl_value_get_by_name(
1026 hConf,
1027 CFL_SECTION_GLOBAL,
1028 CFL_VALUE_BACKEND,
1029 &CFLValueType);
1031 if (pszCFLValue != NULL &&
1032 CFLValueType == CFL_TYPE_STRING)
1034 if (strcmp(pszCFLValue, CFL_SECTION_SQLITE) == 0)
1036 nDBType = MEDIADB_DBTYPE_SQLITE;
1038 else if (strcmp(pszCFLValue, CFL_SECTION_MYSQL) == 0)
1040 nDBType = MEDIADB_DBTYPE_MYSQL;
1044 cfl_free(hConf);
1046 fclose(hFile);
1049 /* Process options from command line */
1051 for (i = 1; i < argc ; i++)
1053 if (strcmp(argv[i],"-L") == 0)
1055 nMode = MODE_LIST_MEDIA;
1057 else if (strcmp(argv[i],"-A") == 0)
1059 nMode = MODE_ADD_MEDIA;
1061 else if (strcmp(argv[i],"-U") == 0)
1063 nMode = MODE_UPDATE_MEDIA;
1065 else if (strcmp(argv[i],"-h") == 0)
1067 i++;
1068 if (i < argc)
1070 if (pszHost != NULL)
1071 free(pszHost);
1072 pszHost = strdup(argv[i]);
1073 if (pszHost == NULL)
1075 fprintf(stderr,
1076 "Out of memory.\n");
1077 Exit(1);
1080 else
1082 fprintf(stderr,
1083 "Missing parameter of -h option\n");
1084 Help(argv[0]);
1087 else if (strcmp(argv[i],"-u") == 0)
1089 i++;
1090 if (i < argc)
1092 if (pszUser != NULL)
1093 free(pszUser);
1094 pszUser = strdup(argv[i]);
1095 if (pszUser == NULL)
1097 fprintf(stderr,
1098 "Out of memory.\n");
1099 Exit(1);
1102 else
1104 fprintf(stderr,
1105 "Missing parameter of -u option\n");
1106 Help(argv[0]);
1109 else if (strcmp(argv[i],"-p") == 0)
1111 if (i+1 < argc && argv[i+1][0] != '-')
1113 i++;
1114 if (pszPass != NULL)
1115 free(pszPass);
1116 pszPass = strdup(argv[i]);
1117 if (pszPass == NULL)
1119 fprintf(stderr,
1120 "Out of memory.\n");
1121 Exit(1);
1124 else
1126 AskForPassword();
1127 if (pszPass != NULL)
1128 free(pszPass);
1129 pszPass = strdup(strPass);
1130 if (pszPass == NULL)
1132 fprintf(stderr,
1133 "Out of memory.\n");
1134 Exit(1);
1138 else if (strcmp(argv[i],"-b") == 0)
1140 i++;
1141 if (i < argc)
1143 if (pszDB != NULL)
1144 free(pszDB);
1145 pszDB = strdup(argv[i]);
1146 if (pszDB == NULL)
1148 fprintf(stderr,
1149 "Out of memory.\n");
1150 Exit(1);
1153 else
1155 fprintf(stderr,
1156 "Missing parameter of -b option\n");
1157 Help(argv[0]);
1160 else if (strcmp(argv[i],"-f") == 0)
1162 i++;
1163 if (i < argc)
1165 if (pszDBSQLite != NULL)
1166 free(pszDBSQLite);
1167 pszDBSQLite = strdup(argv[i]);
1168 if (pszDBSQLite == NULL)
1170 fprintf(stderr,
1171 "Out of memory.\n");
1172 Exit(1);
1175 else
1177 fprintf(stderr,
1178 "Missing parameter of -f option\n");
1179 Help(argv[0]);
1182 else if (strcmp(argv[i],"-m") == 0)
1184 i++;
1185 if (i < argc)
1187 if (pszMountMediaCommand != NULL)
1188 free(pszMountMediaCommand);
1189 pszMountMediaCommand = strdup(argv[i]);
1190 if (pszMountMediaCommand == NULL)
1192 fprintf(stderr,
1193 "Out of memory.\n");
1194 Exit(1);
1197 else
1199 fprintf(stderr,
1200 "Missing parameter of -m option\n");
1201 Help(argv[0]);
1204 else if (strcmp(argv[i],"-n") == 0)
1206 i++;
1207 if (i < argc)
1209 if (pszUnmountMediaCommand != NULL)
1210 free(pszUnmountMediaCommand);
1211 pszUnmountMediaCommand = strdup(argv[i]);
1212 if (pszUnmountMediaCommand == NULL)
1214 fprintf(stderr,
1215 "Out of memory.\n");
1216 Exit(1);
1219 else
1221 fprintf(stderr,
1222 "Missing parameter of -n option\n");
1223 Help(argv[0]);
1226 else if (strcmp(argv[i],"-d") == 0)
1228 i++;
1229 if (i < argc)
1231 if (pszMountDir != NULL)
1232 free(pszMountDir);
1233 pszMountDir = strdup(argv[i]);
1234 if (pszMountDir == NULL)
1236 fprintf(stderr,
1237 "Out of memory.\n");
1238 Exit(1);
1241 else
1243 fprintf(stderr,
1244 "Missing parameter of -d option\n");
1245 Help(argv[0]);
1248 else if (strcmp(argv[i],"-M") == 0)
1250 nDBType = MEDIADB_DBTYPE_MYSQL;
1252 else if (strcmp(argv[i],"-S") == 0)
1254 nDBType = MEDIADB_DBTYPE_SQLITE;
1256 else if (strcmp(argv[i],"-?") == 0)
1258 Help(argv[0]);
1260 else
1262 fprintf(stderr,
1263 "Unknown option \"%s\"\n",
1264 argv[i]);
1265 Help(argv[0]);
1269 SetDefaults();
1271 if (nDBType == MEDIADB_DBTYPE_MYSQL)
1273 r = mediadb_open(
1274 MEDIADB_DBTYPE_MYSQL,
1275 pszHost,
1276 pszUser,
1277 pszPass,
1278 pszDB,
1279 &hDB);
1281 else if (nDBType == MEDIADB_DBTYPE_SQLITE)
1283 r = mediadb_open(
1284 MEDIADB_DBTYPE_SQLITE,
1285 NULL,
1286 NULL,
1287 NULL,
1288 pszDBSQLite,
1289 &hDB);
1291 else
1293 fprintf(stderr,
1294 "Unknown backend type (internal error)\n");
1295 Exit(1);
1298 if (MEDIADB_IS_ERROR(r))
1300 fprintf(stderr,
1301 "Failed to connect to database. Error is %d (%s)\n",
1302 (int)r,
1303 mediadb_get_error_message(NULL));
1304 Exit(1);
1307 nMountPrefixLength = strlen(pszMountDir);
1309 MaybeEnlargeBuffer(&pszPathBuffer,
1310 &nPathBufferSize,
1311 nMountPrefixLength + 2);
1313 memcpy(pszPathBuffer, pszMountDir, nMountPrefixLength);
1315 if (nMode == MODE_ADD_MEDIA)
1317 printf("=========================\n");
1318 printf("Add media mode\n");
1319 printf("=========================\n");
1320 AddMedia();
1322 else if (nMode == MODE_UPDATE_MEDIA)
1324 printf("=========================\n");
1325 printf("Update media mode\n");
1326 printf("=========================\n");
1327 UpdateMedia();
1329 else if (nMode == MODE_LIST_MEDIA)
1331 printf("=========================\n");
1332 printf("List media mode\n");
1333 printf("=========================\n");
1334 ListMedia();
1336 else
1338 assert(0);
1339 return 1;
1342 Exit(0);
1343 return 1;