Tizen 2.1 base
[framework/multimedia/media-server.git] / common / mediadb-update.c
1 /*
2  *  Media Server
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Yong Yeon Kim <yy9875.kim@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 #include <errno.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <dirent.h>
29
30
31 #include <glib.h>
32 #include "media-util.h"
33
34 GMainLoop * mainloop = NULL;
35
36 void callback(media_request_result_s * result, void *user_data)
37 {
38         printf("db updating done\n");
39
40         g_main_loop_quit(mainloop);
41 }
42
43 void print_help()
44 {
45         printf("=======================================================================================\n");
46         printf("\n");
47         printf("mediadb-update [option] <directory path> \n");
48         printf("\n");
49         printf("[option]\n");
50         printf("        -r : [only directory] update all directory recursivly under <directory path>\n");
51         printf("\n");
52         printf("mediadb-update --help for check this messages.\n");
53         printf("\n");
54         printf("A file or directory must exists under /opt/usr/media or /opt/storage/sdcard.\n");
55         printf("Using /opt/storage/sdcard is allowed SD card is mounted.\n");
56         printf("\n");
57         printf("=======================================================================================\n");
58         exit(1);
59 }
60
61 int dir_scan_non_recursive(char *path)
62 {
63         return media_directory_scanning_async(path, FALSE, callback, NULL);
64 }
65
66 int dir_scan_recursive(char *path)
67 {
68         return media_directory_scanning_async(path, TRUE, callback, NULL);
69 }
70
71 typedef enum {
72         DIRECTORY_OK,
73         FILE_OK,
74         NOT_OK,
75 }check_result;
76
77 check_result check_path(char *path)
78 {
79         struct stat buf;
80         DIR *dp = NULL;
81
82         /*check the path directory or file*/
83         if (stat(path, &buf) == 0) {
84                 if (S_ISDIR(buf.st_mode)) {
85                         printf("This is directory\n");
86                         return DIRECTORY_OK;
87                 } else {
88                         dp = opendir(path);
89                         if (dp == NULL) {
90                                 /*if openning directory is failed, check it exists. */
91                                 if (errno == ENOENT) {
92                                         /* this directory is deleted */
93                                         return DIRECTORY_OK;
94                                 }
95                         } else {
96                                 closedir(dp);
97                         }
98                         printf("[%d]invalid path\n", __LINE__);
99                         print_help();
100                 }
101         } else {
102                 printf("stat error : %s\n", strerror(errno));
103         }
104
105         return NOT_OK;
106 }
107
108 int main(int argc, char **argv)
109 {
110         int ret;
111         char *argv1 = NULL;
112         char *argv2 = NULL;
113
114         if (argc > 3 || argc < 2) {
115                 print_help();
116         }
117
118         argv1 = strdup(argv[1]);
119
120         mainloop = g_main_loop_new(NULL, FALSE);
121
122         if (argc == 2) {
123                 if (strcmp(argv1 , "--help") == 0) {
124                         print_help();
125                 }
126
127                 if (check_path(argv1) == DIRECTORY_OK) {
128                         ret = dir_scan_non_recursive(argv1);
129                         if (ret != 0) {
130                                 printf("error : %d\n", ret);
131                                 exit(1);
132                         }
133                 } else {
134                         print_help();
135                 }
136         } else if (argc == 3) {
137                 argv2 = strdup(argv[2]);
138                 if (strcmp(argv1, "-r") == 0) {
139                         if (check_path(argv2) == DIRECTORY_OK) {
140                                 ret = dir_scan_recursive(argv2);
141                                 if (ret != 0) {
142                                         printf("error : %d\n", ret);
143                                         exit(1);
144                                 }
145                         } else {
146                                 print_help();
147                         }
148                 } else {
149                         printf("[%d] invalide option\n", __LINE__);
150                         print_help();
151                 }
152         }
153
154         g_main_loop_run(mainloop);
155
156         exit(0);
157 }