Allow readonly mode (demo)
[mediadatabase.git] / cui / main.c
blob1790fcb150ce648e67c9abbf3a3f5fadb04fbc8b
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
56 #define CONF_FILE ".mediadatabase"
57 #define CFL_SECTION_GLOBAL NULL
58 #define CFL_SECTION_CUI "cui"
59 #define CFL_SECTION_MYSQL "MySQL"
60 #define CFL_SECTION_SQLITE "SQLite"
61 #define CFL_VALUE_MYSQL_HOST "host"
62 #define CFL_VALUE_MYSQL_USER "user"
63 #define CFL_VALUE_MYSQL_PASS "pass"
64 #define CFL_VALUE_MYSQL_DB "db"
65 #define CFL_VALUE_MOUNT "mount"
66 #define CFL_VALUE_UNMOUNT "unmount"
67 #define CFL_VALUE_MOUNTDIR "mountdir"
69 //#define PRINT_FILENAMES
71 char strTitle[MAX_TITLE_SIZE+2]; /* + newline + null */
72 char strPass[MAX_PASS_SIZE];
73 char *pszPathBuffer = NULL;
74 size_t nPathBufferSize = 0;
75 size_t nMountPrefixLength;
76 unsigned long nTotalFiles;
77 unsigned long long nTotalSize;
78 mediadb_uint nMediaID = 0ULL;
79 mediadb hDB = MEDIADB_INVALID_VALUE;
80 int blnMediaMounted = 0;
81 int blnExiting = 0;
82 char *pszHost = NULL;
83 char *pszUser = NULL;
84 char *pszPass = NULL;
85 char *pszDB = NULL;
86 char *pszMountMediaCommand = NULL;
87 char *pszUnmountMediaCommand = NULL;
88 char *pszMountDir = NULL;
89 char *pszTitle = NULL;
91 #define MODE_LIST_MEDIA 0
92 #define MODE_ADD_MEDIA 1
93 #define MODE_UPDATE_MEDIA 2
94 unsigned int nMode = MODE_LIST_MEDIA;
96 void
97 ExecuteCommand(const char *pszCommand);
99 void
100 MountMedia()
102 printf("--> Mounting media ...\n");
103 ExecuteCommand(pszMountMediaCommand);
104 printf("--> Media mounted.\n");
105 blnMediaMounted = 1;
108 void
109 UnmountMedia()
111 printf("--> Unmounting media ...\n");
112 ExecuteCommand(pszUnmountMediaCommand);
113 printf("--> Media unmounted.\n");
114 blnMediaMounted = 0;
117 void
118 Exit(int nRet)
120 mediadb_result r;
122 if (blnExiting)
123 return;
125 blnExiting = 1;
127 if (hDB != MEDIADB_INVALID_VALUE)
129 r = mediadb_close(hDB);
132 if (blnMediaMounted)
133 UnmountMedia();
135 if (pszHost != NULL)
136 free(pszHost);
138 if (pszUser != NULL)
139 free(pszUser);
141 if (pszPass != NULL)
142 free(pszPass);
144 if (pszDB != NULL)
145 free(pszDB);
147 if (pszMountMediaCommand != NULL)
148 free(pszMountMediaCommand);
150 if (pszUnmountMediaCommand != NULL)
151 free(pszUnmountMediaCommand);
153 if (pszMountDir != NULL)
154 free(pszMountDir);
156 if (pszTitle != NULL)
157 free(pszTitle);
159 exit(nRet);
162 void
163 ExecuteCommand(const char *pszCommand)
165 int nRet;
167 nRet = system(pszCommand);
168 if (nRet == -1)
170 fprintf(stderr,
171 "Cannot execute command \"%s\". "
172 "system() returned -1, error is %d (%s)\n",
173 pszCommand,
174 errno,
175 strerror(errno));
176 Exit(1);
179 if (WIFEXITED(nRet))
181 if (WEXITSTATUS(nRet) != 0)
183 fprintf(stderr,
184 "Cannot execute command \"%s\". "
185 "Command returned %d\n",
186 pszCommand,
187 (int)WEXITSTATUS(nRet));
188 Exit(1);
191 else if (WIFSIGNALED(nRet))
193 fprintf(stderr,
194 "Cannot execute command \"%s\". "
195 "Cammand was terminated because of signal %d\n",
196 pszCommand,
197 (int)WTERMSIG(nRet));
198 Exit(1);
200 else
202 fprintf(stderr,
203 "Cannot execute command \"%s\". "
204 "system() returned %d\n",
205 pszCommand,
206 nRet);
207 Exit(1);
211 void
212 ReadTitleExitOnEmpty()
214 char *pch;
216 printf("Title: ");
217 if (fgets(strTitle, sizeof(strTitle), stdin) == NULL)
219 printf("\n");
221 if (feof(stdin))
223 Exit(0);
226 fprintf(stderr, "Error occured while reading standard input.\n");
227 Exit(1);
229 else if (strTitle[0] == '\n')
231 Exit(0);
234 if ((pch = strchr(strTitle, '\n')) == NULL)
236 fprintf(stderr, "input line too long.\n");
237 Exit(1);
240 *pch = '\0';
243 void
244 ReadMediaID()
246 char *pch;
247 char strMediaID[100];
249 printf("Media ID: ");
250 if (fgets(strMediaID, sizeof(strMediaID), stdin) == NULL)
252 printf("\n");
254 if (feof(stdin))
256 Exit(0);
259 fprintf(stderr, "Error occured while reading standard input.\n");
260 Exit(1);
262 else if (strMediaID[0] == '\n')
264 Exit(0);
267 if ((pch = strchr(strMediaID, '\n')) == NULL)
269 fprintf(stderr, "input line too long.\n");
270 Exit(1);
273 *pch = '\0';
275 nMediaID = strtoull(strMediaID, NULL, 10);
276 if (nMediaID == 0)
278 Exit(1);
282 void
283 ReadTitleIgnoreOnEmpty()
285 char *pch;
287 printf("Title: ");
288 if (fgets(strTitle, sizeof(strTitle), stdin) == NULL)
290 printf("\n");
292 if (feof(stdin))
294 strcpy(strTitle, pszTitle);
295 return;
298 fprintf(stderr, "Error occured while reading standard input.\n");
299 Exit(1);
301 else if (strTitle[0] == '\n')
303 strcpy(strTitle, pszTitle);
304 return;
307 if ((pch = strchr(strTitle, '\n')) == NULL)
309 fprintf(stderr, "input line too long.\n");
310 Exit(1);
313 *pch = '\0';
316 void
317 AskForPassword()
319 char *pszPass;
321 pszPass = getpass("MySQL password: ");
323 strcpy(strPass, pszPass);
326 void
327 MaybeEnlargeBuffer(char ** ppBuffer,
328 size_t *pnBufferSize,
329 size_t nSizeRequired)
331 mediadb_result r;
333 r = maybe_enlarge_buffer(
334 ppBuffer,
335 pnBufferSize,
336 nSizeRequired);
337 if (MEDIADB_IS_ERROR(r))
339 fprintf(stderr,
340 "Cannot enlarge buffer.\n");
341 Exit(0);
345 void
346 ScanDir(unsigned long nDepth)
348 mediadb_result r;
349 DIR *pDir;
350 struct dirent *pDE;
351 size_t nCurrentPathLength;
352 struct stat st;
353 size_t sizeName;
355 if (nDepth >= MAX_PATH_DEPTH)
357 fprintf(stderr,
358 "Max path depth of reached \"%u\"\n",
359 (unsigned int)MAX_PATH_DEPTH);
360 Exit(1);
363 nCurrentPathLength = strlen(pszPathBuffer);
365 pDir = opendir(pszPathBuffer);
366 if (pDir == NULL)
368 fprintf(stderr, "Cannot open \"%s\"\n", pszPathBuffer);
369 Exit(1);
372 if (pDir)
374 while ((pDE = readdir(pDir)) != NULL)
376 if (strcmp(pDE->d_name, ".") == 0)
377 continue;
379 if (strcmp(pDE->d_name, "..") == 0)
380 continue;
382 #if defined(OPENBSD)
383 sizeName = pDE->d_namlen;
384 #else
385 sizeName = strlen(pDE->d_name);
386 #endif
388 MaybeEnlargeBuffer(&pszPathBuffer,
389 &nPathBufferSize,
390 nCurrentPathLength + sizeName + 1);
392 memcpy(pszPathBuffer + nCurrentPathLength, pDE->d_name, sizeName);
393 pszPathBuffer[nCurrentPathLength + sizeName] = 0;
395 if (lstat(pszPathBuffer, &st) != 0)
397 fprintf(stderr,
398 "Cannot stat() %s. Error is %d (%s)\n",
399 pszPathBuffer,
400 errno,
401 strerror(errno));
404 pszPathBuffer[nCurrentPathLength] = 0;
406 r = mediadb_file_add_new(
407 hDB,
408 nMediaID,
409 (S_ISDIR(st.st_mode))?MEDIADB_FILETYPE_DIR:MEDIADB_FILETYPE_FILE,
410 pszPathBuffer + nMountPrefixLength,
411 pDE->d_name,
412 ((S_ISDIR(st.st_mode))?0:st.st_size),
413 st.st_ctime);
414 if (MEDIADB_IS_ERROR(r))
416 fprintf(stderr,
417 "Failed to add new file to database. Error is %d (%s)\n",
418 (int)r,
419 mediadb_get_error_message(hDB));
422 memcpy(pszPathBuffer + nCurrentPathLength, pDE->d_name, sizeName);
423 pszPathBuffer[nCurrentPathLength + sizeName] = 0;
425 #ifdef PRINT_FILENAMES
426 printf("%s", pszPathBuffer + nMountPrefixLength);
427 #endif
429 nTotalFiles++;
431 if (S_ISDIR(st.st_mode))
433 #ifdef PRINT_FILENAMES
434 printf("/\n");
435 #else
436 printf("/");
437 #endif
438 fflush(stdout);
439 memcpy(pszPathBuffer + nCurrentPathLength + sizeName, "/", 2);
440 ScanDir(nDepth + 1);
442 else
444 nTotalSize += st.st_size;
445 #ifdef PRINT_FILENAMES
446 printf("\n");
447 #else
448 printf(".");
449 #endif
450 fflush(stdout);
454 closedir(pDir);
458 void
459 SetDefaults()
461 if (pszHost == NULL)
463 pszHost = strdup(DEFAULT_MYSQL_HOST);
464 if (pszHost == NULL)
466 fprintf(stderr,
467 "Out of memory.\n");
468 Exit(1);
472 if (pszUser == NULL)
474 pszUser = strdup(DEFAULT_MYSQL_USER);
475 if (pszUser == NULL)
477 fprintf(stderr,
478 "Out of memory.\n");
479 Exit(1);
483 if (pszPass == NULL)
485 pszPass = strdup(DEFAULT_MYSQL_PASS);
486 if (pszPass == NULL)
488 fprintf(stderr,
489 "Out of memory.\n");
490 Exit(1);
494 if (pszDB == NULL)
496 pszDB = strdup(DEFAULT_MYSQL_DB);
497 if (pszDB == NULL)
499 fprintf(stderr,
500 "Out of memory.\n");
501 Exit(1);
505 if (pszMountMediaCommand == NULL)
507 pszMountMediaCommand = strdup(DEFAULT_MOUNT_CMD);
508 if (pszMountMediaCommand == NULL)
510 fprintf(stderr,
511 "Out of memory.\n");
512 Exit(1);
516 if (pszUnmountMediaCommand == NULL)
518 pszUnmountMediaCommand = strdup(DEFAULT_UNMOUNT_CMD);
519 if (pszUnmountMediaCommand == NULL)
521 fprintf(stderr,
522 "Out of memory.\n");
523 Exit(1);
527 if (pszMountDir == NULL)
529 pszMountDir = strdup(DEFAULT_MOUNTDIR);
530 if (pszMountDir == NULL)
532 fprintf(stderr,
533 "Out of memory.\n");
534 Exit(1);
539 void
540 Help(char *arg0)
542 char *pszExecutable;
544 pszExecutable = strrchr(arg0, '/');
546 if (pszExecutable == NULL)
548 pszExecutable = arg0;
550 else
552 pszExecutable++;
555 SetDefaults();
557 printf("==============================\n");
558 printf("Offline media content database\n");
559 printf("==============================\n");
560 printf("Usage:\n");
561 printf("[<path_to>]%s [options]\n", pszExecutable);
562 printf(" Options can be:\n");
563 printf(" -? - This help\n");
564 printf(" -L - List media mode\n");
565 printf(" -A - Add media mode\n");
566 printf(" -U - Update media mode\n");
567 printf(" -m <mount_cmd> - command to mount media, default is \"%s\"\n", pszMountMediaCommand);
568 printf(" -n <unmount_cmd> - command to unmount, default is \"%s\"\n", pszUnmountMediaCommand);
569 printf(" -d <mount_dir> - where media is mounted, default is \"%s\"\n", pszMountDir);
570 printf(" -h <mysql_host> - MySQL server host, default is \"%s\"\n", pszHost);
571 printf(" -u <mysql_user> - MySQL user, default is \"%s\"\n", pszUser);
572 printf(" -p [mysql_pass] - MySQL password, default is \"%s\"\n", pszPass);
573 printf(" -b <mysql_database> - MySQL database, default is \"%s\"\n", pszDB);
574 Exit(0);
577 void
578 AddMedia()
580 mediadb_result r;
581 time_t timeAdded;
583 printf("NOTE: Currently audio media is not supported.\n");
585 Loop:
586 memcpy(pszPathBuffer + nMountPrefixLength, "/", 2);
588 printf("--> Please insert media and enter title.\n");
590 ReadTitleExitOnEmpty();
592 MountMedia();
594 printf("--> Processing media ...\n");
596 r = mediadb_media_add_new(
597 hDB,
598 strTitle,
600 MEDIADB_MT_DATA,
601 &nMediaID);
603 if (MEDIADB_IS_ERROR(r))
605 fprintf(stderr,
606 "Failed to add new media to database. Error is %d (%s)\n",
607 (int)r,
608 mediadb_get_error_message(hDB));
609 Exit(1);
612 ScanDir(0);
614 printf("\n");
616 timeAdded = time(NULL);
618 r = mediadb_media_update_properties(
619 hDB,
620 nMediaID,
621 timeAdded,
622 nTotalFiles,
623 nTotalSize);
625 if (MEDIADB_IS_ERROR(r))
627 fprintf(stderr,
628 "Failed to update new media to database. Error is %d (%s)\n",
629 (int)r,
630 mediadb_get_error_message(hDB));
631 Exit(1);
634 UnmountMedia();
636 printf("--> SUCCESS - Media added.\n");
637 printf("Media title: %s\n", strTitle);
638 printf("Media ID: %u\n", (unsigned int)nMediaID);
639 printf("Added: %s", ctime(&timeAdded));
640 printf("Total files: %u\n", (unsigned int)nTotalFiles);
641 printf("Total size: %u\n", (unsigned int)nTotalSize);
643 goto Loop;
646 void
647 UpdateMedia()
649 mediadb_result r;
650 time_t timeAdded;
651 mediadb_uint nTimeAdded;
652 mediadb_uint nTotalFiles;
653 mediadb_uint nTotalSize;
655 printf("NOTE: Currently audio media is not supported.\n");
657 Loop:
658 printf("--> Please enter Media ID.\n");
660 ReadMediaID();
662 memcpy(pszPathBuffer + nMountPrefixLength, "/", 2);
664 r = mediadb_media_get_properties(
665 hDB,
666 nMediaID,
667 &nTimeAdded,
668 &nTotalFiles,
669 &nTotalSize,
670 &pszTitle);
671 if (MEDIADB_IS_ERROR(r))
673 fprintf(stderr,
674 "Failed to get media properties. Error is %d (%s)\n",
675 (int)r,
676 mediadb_get_error_message(hDB));
677 Exit(1);
680 if (strlen(pszTitle) + 1 > MAX_TITLE_SIZE)
682 fprintf(stderr,
683 "Title too big\n");
684 Exit(1);
687 timeAdded = (time_t)nTimeAdded;
689 printf("Media title: %s\n", pszTitle);
690 printf("Added: %s", ctime(&timeAdded));
691 printf("Total files: %u\n", (unsigned int)nTotalFiles);
692 printf("Total size: %u\n", (unsigned int)nTotalSize);
694 printf("--> Please insert media and enter title (Just press return to keep current title).\n");
696 ReadTitleIgnoreOnEmpty();
698 MountMedia();
700 printf("--> Processing media ...\n");
702 if (strcmp(pszTitle, strTitle) != 0)
704 r = mediadb_media_update_name(
705 hDB,
706 nMediaID,
707 strTitle);
708 if (MEDIADB_IS_ERROR(r))
710 fprintf(stderr,
711 "Cannot update media title. Error is %d (%s)\n",
712 (int)r,
713 mediadb_get_error_message(hDB));
714 Exit(1);
718 r = mediadb_delete_media_files(
719 hDB,
720 nMediaID);
722 if (MEDIADB_IS_ERROR(r))
724 fprintf(stderr,
725 "Failed to delete media files in database. Error is %d (%s)\n",
726 (int)r,
727 mediadb_get_error_message(hDB));
728 Exit(1);
731 ScanDir(0);
733 printf("\n");
735 //timeAdded = time(NULL);
737 r = mediadb_media_update_properties(
738 hDB,
739 nMediaID,
740 timeAdded,
741 nTotalFiles,
742 nTotalSize);
744 if (MEDIADB_IS_ERROR(r))
746 fprintf(stderr,
747 "Failed to update new media to database. Error is %d (%s)\n",
748 (int)r,
749 mediadb_get_error_message(hDB));
750 Exit(1);
753 UnmountMedia();
755 printf("--> SUCCESS - Media added.\n");
756 printf("Media title: %s\n", strTitle);
757 printf("Media ID: %u\n", (unsigned int)nMediaID);
758 printf("Added: %s", ctime(&timeAdded));
759 printf("Total files: %u\n", (unsigned int)nTotalFiles);
760 printf("Total size: %u\n", (unsigned int)nTotalSize);
762 goto Loop;
765 void
766 ListMedia()
768 printf("List media mode not implemented yet.\n");
772 main(
773 int argc,
774 char ** argv
777 int i;
778 mediadb_result r;
779 int nRet;
780 cflh_t hConf;
781 FILE *hFile;
782 const char *pszHOME;
783 char **ppsz;
784 char *pszConf;
785 size_t s;
786 char *pszCFLValue;
787 cfl_value_type_t CFLValueType;
788 char *pszExecutable;
790 pszExecutable = strrchr(argv[0], '/');
792 if (pszExecutable == NULL)
794 pszExecutable = argv[0];
796 else
798 pszExecutable++;
801 if (strcmp(pszExecutable, "mdb_add") == 0)
803 nMode = MODE_ADD_MEDIA;
805 else if (strcmp(pszExecutable, "mdb_update") == 0)
807 nMode = MODE_UPDATE_MEDIA;
809 else
811 nMode = MODE_LIST_MEDIA;
814 /* Find HOME environment variable */
815 pszHOME = NULL;
816 ppsz = environ;
817 while (*ppsz)
819 if (strncasecmp(*ppsz, "HOME=", 5) == 0)
821 pszHOME = *ppsz + 5;
822 break;
824 ppsz++;
827 if (pszHOME == NULL)
829 fprintf(stderr,
830 "Please set the HOME encironment variable.\n");
831 Exit(1);
834 /* Look into configuration file */
836 s = strlen(pszHOME) + 1 + strlen(CONF_FILE) + 1;
837 pszConf = (char *)malloc(s);
838 sprintf(pszConf, "%s/%s", pszHOME, CONF_FILE);
840 hFile = fopen(pszConf, "r");
841 if (hFile != NULL)
843 hConf = cfl_alloc();
844 if (hConf == NULL)
846 fprintf(stderr,
847 "Out of memory.\n");
848 Exit(1);
851 nRet = cfl_init_from_file(
852 hConf,
853 hFile,
854 CFL_FILE_TYPE_DEFAULT);
855 if (nRet != 0)
857 printf("cfl_init_from_file() failed. (%s)\n", cfl_errcode_to_message(nRet));
860 pszCFLValue = cfl_value_get_by_name(
861 hConf,
862 CFL_SECTION_GLOBAL,
863 CFL_VALUE_MOUNT,
864 &CFLValueType);
866 if (pszCFLValue != NULL &&
867 CFLValueType == CFL_TYPE_STRING)
869 pszMountMediaCommand = strdup(pszCFLValue);
870 if (pszMountMediaCommand == NULL)
872 fprintf(stderr,
873 "Out of memory.\n");
874 Exit(1);
878 pszCFLValue = cfl_value_get_by_name(
879 hConf,
880 CFL_SECTION_GLOBAL,
881 CFL_VALUE_UNMOUNT,
882 &CFLValueType);
884 if (pszCFLValue != NULL &&
885 CFLValueType == CFL_TYPE_STRING)
887 pszUnmountMediaCommand = strdup(pszCFLValue);
888 if (pszUnmountMediaCommand == NULL)
890 fprintf(stderr,
891 "Out of memory.\n");
892 Exit(1);
896 pszCFLValue = cfl_value_get_by_name(
897 hConf,
898 CFL_SECTION_GLOBAL,
899 CFL_VALUE_MOUNTDIR,
900 &CFLValueType);
902 if (pszCFLValue != NULL &&
903 CFLValueType == CFL_TYPE_STRING)
905 pszMountDir = strdup(pszCFLValue);
906 if (pszMountDir == NULL)
908 fprintf(stderr,
909 "Out of memory.\n");
910 Exit(1);
914 pszCFLValue = cfl_value_get_by_name(
915 hConf,
916 CFL_SECTION_MYSQL,
917 CFL_VALUE_MYSQL_HOST,
918 &CFLValueType);
920 if (pszCFLValue != NULL &&
921 CFLValueType == CFL_TYPE_STRING)
923 pszHost = strdup(pszCFLValue);
924 if (pszHost == NULL)
926 fprintf(stderr,
927 "Out of memory.\n");
928 Exit(1);
932 pszCFLValue = cfl_value_get_by_name(
933 hConf,
934 CFL_SECTION_MYSQL,
935 CFL_VALUE_MYSQL_DB,
936 &CFLValueType);
938 if (pszCFLValue != NULL &&
939 CFLValueType == CFL_TYPE_STRING)
941 pszDB = strdup(pszCFLValue);
942 if (pszDB == NULL)
944 fprintf(stderr,
945 "Out of memory.\n");
946 Exit(1);
950 pszCFLValue = cfl_value_get_by_name(
951 hConf,
952 CFL_SECTION_MYSQL,
953 CFL_VALUE_MYSQL_USER,
954 &CFLValueType);
956 if (pszCFLValue != NULL &&
957 CFLValueType == CFL_TYPE_STRING)
959 pszUser = strdup(pszCFLValue);
960 if (pszUser == NULL)
962 fprintf(stderr,
963 "Out of memory.\n");
964 Exit(1);
968 pszCFLValue = cfl_value_get_by_name(
969 hConf,
970 CFL_SECTION_MYSQL,
971 CFL_VALUE_MYSQL_PASS,
972 &CFLValueType);
974 if (pszCFLValue != NULL &&
975 CFLValueType == CFL_TYPE_STRING)
977 pszPass = strdup(pszCFLValue);
978 if (pszPass == NULL)
980 fprintf(stderr,
981 "Out of memory.\n");
982 Exit(1);
986 cfl_free(hConf);
988 fclose(hFile);
991 /* Process options from command line */
993 for (i = 1; i < argc ; i++)
995 if (strcmp(argv[i],"-L") == 0)
997 nMode = MODE_LIST_MEDIA;
999 else if (strcmp(argv[i],"-A") == 0)
1001 nMode = MODE_ADD_MEDIA;
1003 else if (strcmp(argv[i],"-U") == 0)
1005 nMode = MODE_UPDATE_MEDIA;
1007 else if (strcmp(argv[i],"-h") == 0)
1009 i++;
1010 if (i < argc)
1012 if (pszHost != NULL)
1013 free(pszHost);
1014 pszHost = strdup(argv[i]);
1015 if (pszHost == NULL)
1017 fprintf(stderr,
1018 "Out of memory.\n");
1019 Exit(1);
1022 else
1024 fprintf(stderr,
1025 "Missing parameter of -h option\n");
1026 Help(argv[0]);
1029 else if (strcmp(argv[i],"-u") == 0)
1031 i++;
1032 if (i < argc)
1034 if (pszUser != NULL)
1035 free(pszUser);
1036 pszUser = strdup(argv[i]);
1037 if (pszUser == NULL)
1039 fprintf(stderr,
1040 "Out of memory.\n");
1041 Exit(1);
1044 else
1046 fprintf(stderr,
1047 "Missing parameter of -u option\n");
1048 Help(argv[0]);
1051 else if (strcmp(argv[i],"-p") == 0)
1053 if (i+1 < argc && argv[i+1][0] != '-')
1055 i++;
1056 if (pszPass != NULL)
1057 free(pszPass);
1058 pszPass = strdup(argv[i]);
1059 if (pszPass == NULL)
1061 fprintf(stderr,
1062 "Out of memory.\n");
1063 Exit(1);
1066 else
1068 AskForPassword();
1069 if (pszPass != NULL)
1070 free(pszPass);
1071 pszPass = strdup(strPass);
1072 if (pszPass == NULL)
1074 fprintf(stderr,
1075 "Out of memory.\n");
1076 Exit(1);
1080 else if (strcmp(argv[i],"-b") == 0)
1082 i++;
1083 if (i < argc)
1085 if (pszDB != NULL)
1086 free(pszDB);
1087 pszDB = strdup(argv[i]);
1088 if (pszDB == NULL)
1090 fprintf(stderr,
1091 "Out of memory.\n");
1092 Exit(1);
1095 else
1097 fprintf(stderr,
1098 "Missing parameter of -b option\n");
1099 Help(argv[0]);
1102 else if (strcmp(argv[i],"-m") == 0)
1104 i++;
1105 if (i < argc)
1107 if (pszMountMediaCommand != NULL)
1108 free(pszMountMediaCommand);
1109 pszMountMediaCommand = strdup(argv[i]);
1110 if (pszMountMediaCommand == NULL)
1112 fprintf(stderr,
1113 "Out of memory.\n");
1114 Exit(1);
1117 else
1119 fprintf(stderr,
1120 "Missing parameter of -m option\n");
1121 Help(argv[0]);
1124 else if (strcmp(argv[i],"-n") == 0)
1126 i++;
1127 if (i < argc)
1129 if (pszUnmountMediaCommand != NULL)
1130 free(pszUnmountMediaCommand);
1131 pszUnmountMediaCommand = strdup(argv[i]);
1132 if (pszUnmountMediaCommand == NULL)
1134 fprintf(stderr,
1135 "Out of memory.\n");
1136 Exit(1);
1139 else
1141 fprintf(stderr,
1142 "Missing parameter of -n option\n");
1143 Help(argv[0]);
1146 else if (strcmp(argv[i],"-d") == 0)
1148 i++;
1149 if (i < argc)
1151 if (pszMountDir != NULL)
1152 free(pszMountDir);
1153 pszMountDir = strdup(argv[i]);
1154 if (pszMountDir == NULL)
1156 fprintf(stderr,
1157 "Out of memory.\n");
1158 Exit(1);
1161 else
1163 fprintf(stderr,
1164 "Missing parameter of -d option\n");
1165 Help(argv[0]);
1168 else if (strcmp(argv[i],"-?") == 0)
1170 Help(argv[0]);
1172 else
1174 fprintf(stderr,
1175 "Unknown option \"%s\"\n",
1176 argv[i]);
1177 Help(argv[0]);
1181 SetDefaults();
1183 r = mediadb_open(
1184 MEDIADB_DBTYPE_MYSQL,
1185 pszHost,
1186 pszUser,
1187 pszPass,
1188 pszDB,
1189 &hDB);
1190 if (MEDIADB_IS_ERROR(r))
1192 fprintf(stderr, "Failed to connect to database.\n");
1193 Exit(1);
1196 nMountPrefixLength = strlen(pszMountDir);
1198 MaybeEnlargeBuffer(&pszPathBuffer,
1199 &nPathBufferSize,
1200 nMountPrefixLength + 2);
1202 memcpy(pszPathBuffer, pszMountDir, nMountPrefixLength);
1204 if (nMode == MODE_ADD_MEDIA)
1206 printf("=========================\n");
1207 printf("Add media mode\n");
1208 printf("=========================\n");
1209 AddMedia();
1211 else if (nMode == MODE_UPDATE_MEDIA)
1213 printf("=========================\n");
1214 printf("Update media mode\n");
1215 printf("=========================\n");
1216 UpdateMedia();
1218 else if (nMode == MODE_LIST_MEDIA)
1220 printf("=========================\n");
1221 printf("List media mode\n");
1222 printf("=========================\n");
1223 ListMedia();
1225 else
1227 assert(0);
1228 return 1;
1231 Exit(0);
1232 return 1;