fix prevent bugs
[framework/multimedia/media-server.git] / src / common / media-common-external-storage.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 <locale.h>
22 #include <libintl.h>
23 #include <sys/stat.h>
24 #include <dirent.h>
25 #include <malloc.h>
26 #include <vconf.h>
27 #include <notification.h>
28
29 #include "media-util.h"
30 #include "media-server-ipc.h"
31 #include "media-common-dbg.h"
32 #include "media-common-utils.h"
33 #include "media-common-drm.h"
34 #include "media-common-external-storage.h"
35
36 #define MMC_INFO_SIZE 256
37
38 char default_path[][MS_FILE_NAME_LEN_MAX + 1] = {
39                 {"/opt/storage/sdcard/Images"},
40                 {"/opt/storage/sdcard/Videos"},
41                 {"/opt/storage/sdcard/Sounds"},
42                 {"/opt/storage/sdcard/Downloads"},
43                 {"/opt/storage/sdcard/Camera"}
44 };
45
46 #define DIR_NUM       ((int)(sizeof(default_path)/sizeof(default_path[0])))
47
48 void
49 ms_make_default_path_mmc(void)
50 {
51         int i = 0;
52         int ret = 0;
53         DIR *dp = NULL;
54
55         for (i = 0; i < DIR_NUM; ++i) {
56                 dp = opendir(default_path[i]);
57                 if (dp == NULL) {
58                         ret = mkdir(default_path[i], 0777);
59                         if (ret < 0) {
60                                 MS_DBG_ERR("make fail");
61                         }
62                         /*this fuction for emulator*/
63                         /*at the first time, the directroies are made permission 755*/
64                         ret = chmod(default_path[i], 0777);
65                         if (ret != 0) {
66                                 MS_DBG_ERR("chmod failed [%s]", strerror(errno));
67                         }
68                         ret = chown(default_path[i], 5000, 5000);
69                         if (ret != 0) {
70                                 MS_DBG_ERR("chown failed [%s]", strerror(errno));
71                         }
72                 } else {
73                         closedir(dp);
74                 }
75         }
76 }
77
78 int
79 _ms_update_mmc_info(const char *cid)
80 {
81         bool res;
82
83         if (cid == NULL) {
84                 MS_DBG_ERR("Parameters are invalid");
85                 return MS_MEDIA_ERR_INVALID_PARAMETER;
86         }
87
88         res = ms_config_set_str(MS_MMC_INFO_KEY, cid);
89         if (!res) {
90                 MS_DBG_ERR("fail to get MS_MMC_INFO_KEY");
91                 return MS_MEDIA_ERR_VCONF_SET_FAIL;
92         }
93
94         return MS_MEDIA_ERR_NONE;
95 }
96
97 bool
98 _ms_check_mmc_info(const char *cid)
99 {
100         char pre_mmc_info[MMC_INFO_SIZE] = { 0 };
101         bool res = false;
102
103         if (cid == NULL) {
104                 MS_DBG_ERR("Parameters are invalid");
105                 return false;
106         }
107
108         res = ms_config_get_str(MS_MMC_INFO_KEY, pre_mmc_info);
109         if (!res) {
110                 MS_DBG_ERR("fail to get MS_MMC_INFO_KEY");
111                 return false;
112         }
113
114         MS_DBG("Last MMC info   = %s", pre_mmc_info);
115         MS_DBG("Current MMC info = %s", cid);
116
117         if (strcmp(pre_mmc_info, cid) == 0) {
118                 return true;
119         }
120
121         return false;
122 }
123
124 static int
125 _get_contents(const char *filename, char *buf)
126 {
127         FILE *fp;
128
129         fp = fopen(filename, "rt");
130         if (fp == NULL) {
131                 MS_DBG_ERR("fp is NULL. file name : %s", filename);
132                 return MS_MEDIA_ERR_FILE_OPEN_FAIL;
133         }
134         if (fgets(buf, 255, fp) == NULL)
135                 MS_DBG_ERR("fgets failed");
136
137         fclose(fp);
138
139         return MS_MEDIA_ERR_NONE;
140 }
141
142 /*need optimize*/
143 int
144 _ms_get_mmc_info(char *cid)
145 {
146         int i;
147         int j;
148         int len;
149         int err = -1;
150         bool getdata = false;
151         bool bHasColon = false;
152         char path[MS_FILE_PATH_LEN_MAX] = { 0 };
153         char mmcpath[MS_FILE_PATH_LEN_MAX] = { 0 };
154
155         DIR *dp;
156         struct dirent ent;
157         struct dirent *res = NULL;
158
159         /* mmcblk0 and mmcblk1 is reserved for movinand */
160         for (j = 1; j < 3; j++) {
161                 len = snprintf(mmcpath, MS_FILE_PATH_LEN_MAX, "/sys/class/mmc_host/mmc%d/", j);
162                 if (len < 0) {
163                         MS_DBG_ERR("FAIL : snprintf");
164                         return MS_MEDIA_ERR_INTERNAL;
165                 }
166                 else {
167                         mmcpath[len] = '\0';
168                 }
169
170                 dp = opendir(mmcpath);
171                 if (dp == NULL) {
172                         MS_DBG_ERR("dp is NULL");
173                         return MS_MEDIA_ERR_DIR_OPEN_FAIL;
174                 }
175
176                 while (!readdir_r(dp, &ent, &res)) {
177                          /*end of read dir*/
178                         if (res == NULL)
179                                 break;
180
181                         bHasColon = false;
182                         if (ent.d_name[0] == '.')
183                                 continue;
184
185                         if (ent.d_type == DT_DIR) {
186                                 /*ent->d_name is including ':' */
187                                 for (i = 0; i < strlen(ent.d_name); i++) {
188                                         if (ent.d_name[i] == ':') {
189                                                 bHasColon = true;
190                                                 break;
191                                         }
192                                 }
193
194                                 if (bHasColon) {
195                                         /*check serial */
196                                         err = ms_strappend(path, sizeof(path), "%s%s/cid", mmcpath, ent.d_name);
197                                         if (err < 0) {
198                                                 MS_DBG_ERR("ms_strappend error : %d", err);
199                                                 continue;
200                                         }
201
202                                         if (_get_contents(path, cid) != MS_MEDIA_ERR_NONE)
203                                                 break;
204                                         else
205                                                 getdata = true;
206                                 }
207                         }
208                 }
209                 closedir(dp);
210
211                 if (getdata == true) {
212                         break;
213                 }
214         }
215
216         return MS_MEDIA_ERR_NONE;
217 }
218
219 ms_dir_scan_type_t
220 ms_get_mmc_state(void)
221 {
222         char cid[MMC_INFO_SIZE] = { 0 };
223         ms_dir_scan_type_t ret = MS_SCAN_ALL;
224
225         /*get new info */
226         _ms_get_mmc_info(cid);
227
228         /*check it's same mmc */
229         if (_ms_check_mmc_info(cid)) {
230                 ret = MS_SCAN_PART;
231         }
232
233         return ret;
234 }
235
236 int
237 ms_update_mmc_info(void)
238 {
239         int err;
240         char cid[MMC_INFO_SIZE] = { 0 };
241
242         err = _ms_get_mmc_info(cid);
243
244         err = _ms_update_mmc_info(cid);
245
246         /*Active flush */
247         if (!malloc_trim(0))
248                 MS_DBG_ERR("malloc_trim is failed");
249
250         return err;
251 }
252
253 #define _GETSYSTEMSTR(ID)       dgettext("sys_string", (ID))
254
255 void update_lang(void)
256 {
257         char *lang = NULL;
258         char *r = NULL;
259
260         lang = vconf_get_str(VCONFKEY_LANGSET);
261         if (lang) {
262                 setenv("LANG", lang, 1);
263                 setenv("LC_MESSAGES", lang, 1);
264                 r = setlocale(LC_ALL, "");
265                 if (r == NULL) {
266                         r = setlocale(LC_ALL, vconf_get_str(VCONFKEY_LANGSET));
267                         MS_DBG_ERR("*****appcore setlocale=%s\n", r);
268                 }
269                 free(lang);
270         }
271 }
272
273 int
274 ms_present_mmc_status(ms_sdcard_status_type_t status)
275 {
276         int ret = NOTIFICATION_ERROR_NONE;
277
278         update_lang();
279
280         if (status == MS_SDCARD_INSERTED)
281                 ret = notification_status_message_post(_GETSYSTEMSTR("IDS_COM_BODY_PREPARING_SD_CARD"));
282         else if (status == MS_SDCARD_REMOVED)
283                 ret = notification_status_message_post(_GETSYSTEMSTR("IDS_COM_BODY_SD_CARD_REMOVED"));
284
285         if(ret != NOTIFICATION_ERROR_NONE)
286                 return MS_MEDIA_ERR_INTERNAL;
287
288         return MS_MEDIA_ERR_NONE;
289 }
290