Modify external storage scan
[platform/core/multimedia/libmedia-service.git] / src / common / media-svc-media-folder.c
1 /*
2  * libmedia-service
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.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
22 #include <glib/gstdio.h>
23 #include <media-util-err.h>
24 #include "media-svc-media-folder.h"
25 #include "media-svc-debug.h"
26 #include "media-svc-env.h"
27 #include "media-svc-util.h"
28 #include "media-svc-db-utils.h"
29
30 extern __thread GList *g_media_svc_move_item_query_list;
31 static __thread GList *g_media_svc_insert_folder_query_list;
32
33 static int __media_svc_is_root_path(const char *folder_path, bool *is_root)
34 {
35         media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
36
37         *is_root = FALSE;
38
39         if (strcmp(folder_path, MEDIA_ROOT_PATH_INTERNAL) == 0 ||
40                 strcmp(folder_path, MEDIA_ROOT_PATH_SDCARD) == 0 ||
41                 strcmp(folder_path, MEDIA_ROOT_PATH_CLOUD) == 0) {
42                 media_svc_debug("ROOT PATH [%d]", folder_path);
43                 *is_root = TRUE;
44         }
45
46         return MS_MEDIA_ERR_NONE;
47 }
48
49 static int __media_svc_parent_is_ext_root_path(const char *folder_path, bool *is_root)
50 {
51         char *parent_folder_path = NULL;
52
53         media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
54
55         *is_root = FALSE;
56
57         parent_folder_path = g_path_get_dirname(folder_path);
58
59         if(!STRING_VALID(parent_folder_path)) {
60                 media_svc_error("error : g_path_get_dirname falied");
61                 SAFE_FREE(parent_folder_path);
62                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
63         }
64
65         if (strcmp(parent_folder_path, MEDIA_ROOT_PATH_EXTERNAL) == 0) {
66                 media_svc_debug("parent folder is ROOT PATH [%s]", parent_folder_path);
67                 *is_root = TRUE;
68         }
69
70         SAFE_FREE(parent_folder_path);
71
72         return MS_MEDIA_ERR_NONE;
73 }
74
75 int _media_svc_get_folder_id_by_foldername(sqlite3 *handle, const char *storage_id, const char *folder_name, char *folder_id, uid_t uid)
76 {
77         int ret = MS_MEDIA_ERR_NONE;
78         sqlite3_stmt *sql_stmt = NULL;
79         char *sql = NULL;
80         char parent_folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0, };
81         char *temp_parent_uuid = NULL;
82         char *parent_folder_path = NULL;
83
84         sql = sqlite3_mprintf("SELECT folder_uuid, parent_folder_uuid  FROM '%s' WHERE storage_uuid = '%q' AND path = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, folder_name);
85
86         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
87
88         if (ret != MS_MEDIA_ERR_NONE) {
89                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
90                         media_svc_debug("there is no folder.");
91                 } else {
92                         media_svc_error("error when _media_svc_get_folder_id_by_foldername. err = [%d]", ret);
93                 }
94                 return ret;
95         }
96
97         memset(parent_folder_uuid, 0, sizeof(parent_folder_uuid));
98
99         _strncpy_safe(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE + 1);
100         if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 1)))       /*root path can be null*/
101                 _strncpy_safe(parent_folder_uuid, (const char *)sqlite3_column_text(sql_stmt, 1), MEDIA_SVC_UUID_SIZE+1);
102
103         SQLITE3_FINALIZE(sql_stmt);
104
105         /*root path can be null*/
106         if(!STRING_VALID(parent_folder_uuid)) {
107                 bool is_root = FALSE;
108
109                 ret = __media_svc_is_root_path(folder_name, &is_root);
110                 if (is_root)
111                         return MS_MEDIA_ERR_NONE;
112
113                 ret = __media_svc_parent_is_ext_root_path(folder_name, &is_root);
114                 if (is_root)
115                         return MS_MEDIA_ERR_NONE;
116         }
117
118         /* Notice : Below code is the code only for inserting parent folder uuid when upgrade the media db version 3 to 4  */
119         /* Check parent folder uuid */
120         if(!STRING_VALID(parent_folder_uuid)) {
121                 /* update parent_uuid */
122                 media_svc_error("[No-Error] there is no parent folder uuid. PLEASE CHECK IT");
123
124                 parent_folder_path = g_path_get_dirname(folder_name);
125                 if(!STRING_VALID(parent_folder_path)) {
126                         media_svc_error("error : g_path_get_dirname falied.");
127                         return MS_MEDIA_ERR_OUT_OF_MEMORY;
128                 }
129
130                 if(STRING_VALID(storage_id))
131                         sql = sqlite3_mprintf("SELECT folder_uuid  FROM '%s' WHERE storage_uuid = '%q' AND path = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, parent_folder_path);
132                 else
133                         sql = sqlite3_mprintf("SELECT folder_uuid  FROM '%s' WHERE path = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, parent_folder_path);
134
135                 SAFE_FREE(parent_folder_path);
136
137                 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
138                 if (ret == MS_MEDIA_ERR_NONE) {
139                         temp_parent_uuid =  (char *)sqlite3_column_text(sql_stmt, 0);
140                         if (temp_parent_uuid != NULL) {
141                                 _strncpy_safe(parent_folder_uuid, temp_parent_uuid, MEDIA_SVC_UUID_SIZE+1);
142                         }
143
144                         SQLITE3_FINALIZE(sql_stmt);
145                 }
146
147                 if(STRING_VALID(parent_folder_uuid)) {
148                         if(STRING_VALID(storage_id))
149                                 sql = sqlite3_mprintf("UPDATE %q SET parent_folder_uuid  = '%q' WHERE storage_uuid = '%q' AND path = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, parent_folder_uuid, storage_id, folder_name);
150                         else
151                                 sql = sqlite3_mprintf("UPDATE %q SET parent_folder_uuid  = '%q' WHERE path = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, parent_folder_uuid, folder_name);
152                         ret = _media_svc_sql_query(handle, sql, uid);
153                         sqlite3_free(sql);
154                 } else {
155                         media_svc_error("error when get parent folder uuid");
156                 }
157         }
158
159         return ret;
160 }
161
162 static int __media_svc_append_folder(sqlite3 *handle, const char *storage_id, media_svc_storage_type_e storage_type,
163                                     const char *folder_id, const char *folder_path, const char *parent_folder_uuid, bool stack_query, uid_t uid)
164 {
165         int ret = MS_MEDIA_ERR_NONE;
166         char *folder_name = NULL;
167         int folder_modified_date = 0;
168
169         folder_name = g_path_get_basename(folder_path);
170         folder_modified_date = _media_svc_get_file_time(folder_path);
171
172         /*Update Pinyin If Support Pinyin*/
173         char *folder_name_pinyin = NULL;
174         if (_media_svc_check_pinyin_support())
175                 _media_svc_get_pinyin_str(folder_name, &folder_name_pinyin);
176
177         char *sql = sqlite3_mprintf("INSERT INTO %s (folder_uuid, path, name, storage_uuid, storage_type, modified_time, name_pinyin, parent_folder_uuid) \
178                                                         values (%Q, %Q, %Q, %Q, '%d', '%d', %Q, %Q); ",
179                                                      MEDIA_SVC_DB_TABLE_FOLDER, folder_id, folder_path, folder_name, storage_id, storage_type, folder_modified_date, folder_name_pinyin, parent_folder_uuid);
180
181         if(!stack_query)
182         {
183                 ret = _media_svc_sql_query(handle, sql, uid);
184                 sqlite3_free(sql);
185         }
186         else
187         {
188                 _media_svc_sql_query_add(&g_media_svc_insert_folder_query_list, &sql);
189         }
190
191         SAFE_FREE(folder_name);
192         SAFE_FREE(folder_name_pinyin);
193
194         return ret;
195 }
196
197 int _media_svc_update_folder_modified_time_by_folder_uuid(sqlite3 *handle, const char *folder_uuid, const char *folder_path, bool stack_query, uid_t uid)
198 {
199         int ret = MS_MEDIA_ERR_NONE;
200         int modified_time = 0;
201
202         modified_time = _media_svc_get_file_time(folder_path);
203
204         char *sql = sqlite3_mprintf("UPDATE %s SET modified_time=%d WHERE folder_uuid=%Q;", MEDIA_SVC_DB_TABLE_FOLDER, modified_time, folder_uuid);
205
206         if (!stack_query) {
207                 ret = _media_svc_sql_query(handle, sql, uid);
208                 sqlite3_free(sql);
209         } else {
210                 _media_svc_sql_query_add(&g_media_svc_move_item_query_list, &sql);
211         }
212
213         return ret;
214 }
215
216 static int __media_svc_get_and_append_parent_folder(sqlite3 *handle, const char *storage_id, const char *path, media_svc_storage_type_e storage_type, char *folder_id, uid_t uid)
217 {
218         int ret = MS_MEDIA_ERR_NONE;
219         unsigned int next_pos = 0;
220         char *next = NULL;
221         char *dir_path = NULL;
222         const char *token = "/";
223         char *folder_uuid = NULL;
224         char parent_folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
225         bool folder_search_end = FALSE;
226
227         memset(parent_folder_uuid, 0, sizeof(parent_folder_uuid));
228
229         if (strncmp(path, _media_svc_get_path(uid), strlen(_media_svc_get_path(uid))) == 0)
230                 next_pos = strlen(_media_svc_get_path(uid));
231         else if (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0)
232                 next_pos = strlen(MEDIA_ROOT_PATH_SDCARD);
233         else if (strncmp(path, MEDIA_ROOT_PATH_CLOUD, strlen(MEDIA_ROOT_PATH_CLOUD)) == 0)
234                 next_pos = strlen(MEDIA_ROOT_PATH_CLOUD);
235         else if (strncmp(path, MEDIA_ROOT_PATH_EXTERNAL, strlen(MEDIA_ROOT_PATH_EXTERNAL)) == 0)
236                 next_pos = strlen(MEDIA_ROOT_PATH_EXTERNAL);
237         else {
238                 media_svc_error("Invalid Path");
239                 return MS_MEDIA_ERR_INTERNAL;
240         }
241
242         while (!folder_search_end) {
243                 next = strstr(path + next_pos, token);
244                 if (next != NULL) {
245                         next_pos = (next - path);
246                         dir_path = strndup(path, next_pos);
247                         next_pos++;
248                 } else {
249                         media_svc_error("End Path");
250                         dir_path = strndup(path, strlen(path));
251                         folder_search_end = TRUE;
252                         media_svc_error("[No-Error] End Path [%s]", dir_path);
253                 }
254
255                 if (strcmp(dir_path, MEDIA_ROOT_PATH_EXTERNAL) == 0) {
256                         /*To avoid insert MEDIA_ROOT_PATH_EXTERNAL path*/
257                         continue;
258                 }
259
260                 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, dir_path, parent_folder_uuid, uid);
261                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
262                         media_svc_error("NOT EXIST dir path : %s", dir_path);
263
264                         folder_uuid = _media_info_generate_uuid();
265                         if (folder_uuid == NULL) {
266                                 media_svc_error("Invalid UUID");
267                                 SAFE_FREE(dir_path);
268                                 return MS_MEDIA_ERR_INTERNAL;
269                         }
270
271                         ret = __media_svc_append_folder(handle, storage_id, storage_type, folder_uuid, dir_path, parent_folder_uuid, FALSE, uid);
272                         if (ret != MS_MEDIA_ERR_NONE) {
273                                 media_svc_error("__media_svc_append_folder is failed");
274                         }
275
276                         _strncpy_safe(parent_folder_uuid, folder_uuid, MEDIA_SVC_UUID_SIZE + 1);
277                 } else {
278                         media_svc_error("EXIST dir path : %s\n", dir_path);
279                 }
280
281                 SAFE_FREE(dir_path);
282         }
283
284         if(STRING_VALID(folder_uuid)) {
285                 _strncpy_safe(folder_id, folder_uuid, MEDIA_SVC_UUID_SIZE + 1);
286         } else {
287                 media_svc_error("Fail to get folder_uuid");
288                 return MS_MEDIA_ERR_INTERNAL;
289         }
290
291         return MS_MEDIA_ERR_NONE;
292 }
293
294 int _media_svc_get_and_append_folder(sqlite3 *handle, const char *storage_id, const char *path, media_svc_storage_type_e storage_type, char *folder_id, uid_t uid)
295 {
296         int ret = MS_MEDIA_ERR_NONE;
297
298         ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, path, folder_id, uid);
299
300         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
301                 ret = __media_svc_get_and_append_parent_folder(handle, storage_id, path, storage_type, folder_id, uid);
302         }
303
304         return ret;
305 }
306
307 int _media_svc_get_and_append_folder_id_by_path(sqlite3 *handle, const char *storage_id, const char *path, media_svc_storage_type_e storage_type, char *folder_id, uid_t uid)
308 {
309         char *path_name = NULL;
310         int ret = MS_MEDIA_ERR_NONE;
311
312         path_name = g_path_get_dirname(path);
313
314         ret =  _media_svc_get_and_append_folder(handle, storage_id, path_name, storage_type, folder_id, uid);
315
316         SAFE_FREE(path_name);
317
318         return ret;
319 }
320
321 int _media_svc_update_folder_table(sqlite3 *handle, const char *storage_id, uid_t uid)
322 {
323         int ret = MS_MEDIA_ERR_NONE;
324         char *sql = NULL;
325
326         sql = sqlite3_mprintf("DELETE FROM '%s' WHERE folder_uuid IN (SELECT folder_uuid FROM '%s' WHERE folder_uuid NOT IN (SELECT folder_uuid FROM '%s'))",
327              MEDIA_SVC_DB_TABLE_FOLDER, MEDIA_SVC_DB_TABLE_FOLDER, storage_id);
328
329         ret = _media_svc_sql_query(handle, sql, uid);
330         sqlite3_free(sql);
331
332         return ret;
333 }
334
335 static int __media_svc_count_all_folders(sqlite3 *handle, char *start_path, int *count)
336 {
337         int ret = MS_MEDIA_ERR_NONE;
338         sqlite3_stmt *sql_stmt = NULL;
339         char *sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE path LIKE '%q%%'", MEDIA_SVC_DB_TABLE_FOLDER, start_path);
340
341         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
342         if (ret != MS_MEDIA_ERR_NONE) {
343                 media_svc_error("error when _media_svc_sql_prepare_to_step. err = [%d]", ret);
344                 return ret;
345         }
346
347         *count = sqlite3_column_int(sql_stmt, 0);
348
349         SQLITE3_FINALIZE(sql_stmt);
350
351         return MS_MEDIA_ERR_NONE;
352 }
353
354 int _media_svc_get_all_folders(sqlite3 *handle, char *start_path, char ***folder_list, time_t **modified_time_list, int **item_num_list, int *count)
355 {
356         int ret = MS_MEDIA_ERR_NONE;
357         int idx = 0;
358         sqlite3_stmt *sql_stmt = NULL;
359         char *sql = NULL;
360         int cnt = 0;
361         char **folder_uuid = NULL;
362         int i = 0;
363
364         ret  = __media_svc_count_all_folders(handle, start_path, &cnt);
365         if (ret != MS_MEDIA_ERR_NONE) {
366                 media_svc_error("error when __media_svc_count_all_folders. err = [%d]", ret);
367                 return ret;
368         }
369
370         if (cnt > 0) {
371                 sql = sqlite3_mprintf("SELECT path, modified_time, folder_uuid FROM '%s' WHERE path LIKE '%q%%'", MEDIA_SVC_DB_TABLE_FOLDER, start_path);
372         } else {
373                 *folder_list = NULL;
374                 *modified_time_list = NULL;
375                 *item_num_list = NULL;
376                 return MS_MEDIA_ERR_NONE;
377         }
378
379         *folder_list = malloc(sizeof(char *) * cnt);
380         *modified_time_list = malloc(sizeof(int) * cnt);
381         *item_num_list = malloc(sizeof(int) * cnt);
382         folder_uuid = malloc(sizeof(char *) * cnt);
383
384         if ((*folder_list == NULL) || (*modified_time_list == NULL) || (*item_num_list == NULL) || (folder_uuid == NULL)) {
385                 media_svc_error("Out of memory");
386                 goto ERROR;
387         }
388         memset(folder_uuid, 0x0, sizeof(char *) * cnt);
389
390         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
391         if (ret != MS_MEDIA_ERR_NONE) {
392                 media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
393                 goto ERROR;
394         }
395
396         media_svc_debug("QEURY OK");
397
398         while (1) {
399                 if (STRING_VALID((char *)sqlite3_column_text(sql_stmt, 0)))
400                         (*folder_list)[idx] = strdup((char *)sqlite3_column_text(sql_stmt, 0));
401
402                 (*modified_time_list)[idx] = (int)sqlite3_column_int(sql_stmt, 1);
403
404                 /* get the folder's id */
405                 if (STRING_VALID((char *)sqlite3_column_text(sql_stmt, 2)))
406                         folder_uuid[idx] = strdup((char *)sqlite3_column_text(sql_stmt, 2));
407
408                 idx++;
409
410                 if (sqlite3_step(sql_stmt) != SQLITE_ROW)
411                         break;
412         }
413         SQLITE3_FINALIZE(sql_stmt);
414
415         /*get the numbder of item in the folder by using folder's id */
416         for (i = 0; i < idx; i++) {
417                 if (STRING_VALID(folder_uuid[i])) {
418                         sql = sqlite3_mprintf("SELECT COUNT(*) FROM %s WHERE (folder_uuid='%q' AND validity = 1)", MEDIA_SVC_DB_TABLE_MEDIA, folder_uuid[i]);
419                         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
420                         if (ret != MS_MEDIA_ERR_NONE) {
421                                 media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
422                                 goto ERROR;
423                         }
424
425                         (*item_num_list)[i] = (int)sqlite3_column_int(sql_stmt, 0);
426
427                         SQLITE3_FINALIZE(sql_stmt);
428                 } else {
429                         media_svc_error("Invalid Folder Id");
430                 }
431         }
432
433         if (cnt == idx) {
434                 *count = cnt;
435                 media_svc_debug("Get Folder is OK");
436         } else {
437                 media_svc_error("Fail to get folder");
438                 ret = MS_MEDIA_ERR_INTERNAL;
439                 goto ERROR;
440         }
441
442         /* free all data */
443         for (i  = 0; i < idx; i++) {
444                 SAFE_FREE(folder_uuid[i]);
445         }
446         SAFE_FREE(folder_uuid);
447
448         return ret;
449
450 ERROR:
451
452         /* free all data */
453         for (i  = 0; i < idx; i++) {
454                 SAFE_FREE((*folder_list)[i]);
455                 SAFE_FREE(folder_uuid[i]);
456         }
457         SAFE_FREE(*folder_list);
458         SAFE_FREE(*modified_time_list);
459         SAFE_FREE(*item_num_list);
460         SAFE_FREE(folder_uuid);
461
462         *count = 0;
463
464         return ret;
465 }
466
467 int _media_svc_get_folder_info_by_foldername(sqlite3 *handle, const char *folder_name, char *folder_id, time_t *modified_time)
468 {
469         int ret = MS_MEDIA_ERR_NONE;
470         sqlite3_stmt *sql_stmt = NULL;
471
472         char *sql = sqlite3_mprintf("SELECT folder_uuid, modified_time FROM %s WHERE path = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, folder_name);
473
474         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
475
476         if (ret != MS_MEDIA_ERR_NONE) {
477                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
478                         media_svc_debug("there is no folder.");
479                 } else {
480                         media_svc_error("error when _media_svc_get_folder_id_by_foldername. err = [%d]", ret);
481                 }
482                 return ret;
483         }
484
485         _strncpy_safe(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE + 1);
486         *modified_time = (int)sqlite3_column_int(sql_stmt, 1);
487
488         SQLITE3_FINALIZE(sql_stmt);
489
490         return ret;
491 }
492
493 int _media_svc_get_and_append_folder_id_by_folder_path(sqlite3 *handle, const char *storage_id, const char *path, media_svc_storage_type_e storage_type, char *folder_id, bool stack_query, uid_t uid)
494 {
495         char *path_name = NULL;
496         int ret = MS_MEDIA_ERR_NONE;
497         char *sql = NULL;
498
499         path_name = strdup(path);
500         if (path_name == NULL) {
501                 media_svc_error("out of memory");
502                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
503         }
504
505         ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, path_name, folder_id, uid);
506         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
507                 bool is_root = FALSE;
508                 bool is_parent_root = FALSE;
509
510                 ret = __media_svc_is_root_path(path_name, &is_root);
511                 ret = __media_svc_parent_is_ext_root_path(path_name, &is_parent_root);
512
513                 char parent_folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0, };
514                 memset(parent_folder_uuid, 0x00, sizeof(parent_folder_uuid));
515
516                 if ((is_root == FALSE) && (is_parent_root == FALSE)) {
517                         /*get parent folder id*/
518                         char *parent_path_name = g_path_get_dirname(path_name);
519
520                         ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, parent_path_name, parent_folder_uuid, uid);
521                         if (ret != MS_MEDIA_ERR_NONE) {
522                                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
523                                 /*if No-Root directory scanning start before doing storage scanning, parent_folder_uuid can be NULL.
524                                 You can remove above logic if use below logic always. but I keep above code for the performance issue.
525                                 Most case (except No-Root directory scanning start before doing storage scanning) get parent_folder_uuid well*/
526                                 media_svc_error("[No-Error] There is no proper parent_folder_uuid. so try to make it");
527                                 ret = _media_svc_get_and_append_folder(handle, storage_id, path, storage_type, folder_id, uid);
528                                 } else {
529                                         media_svc_error("error when _media_svc_get_parent_folder_id_by_foldername. err = [%d]", ret);
530                                 }
531
532                                 SAFE_FREE(path_name);
533                                 SAFE_FREE(parent_path_name);
534
535                                 return ret;
536                         }
537                 }
538
539                 char *folder_uuid = _media_info_generate_uuid();
540                 if (folder_uuid == NULL) {
541                         media_svc_error("Invalid UUID");
542                         SAFE_FREE(path_name);
543                         return MS_MEDIA_ERR_INTERNAL;
544                 }
545
546                 ret = __media_svc_append_folder(handle, storage_id, storage_type, folder_uuid, path_name, parent_folder_uuid, stack_query, uid);
547
548                 _strncpy_safe(folder_id, folder_uuid, MEDIA_SVC_UUID_SIZE+1);
549
550         } else {
551                 sql = sqlite3_mprintf("UPDATE '%s' SET validity=1 WHERE storage_uuid = '%q' AND path = '%q'", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
552
553                 if(!stack_query)
554                 {
555                         ret = _media_svc_sql_query(handle, sql, uid);
556                         sqlite3_free(sql);
557                 }
558                 else
559                 {
560                         _media_svc_sql_query_add(&g_media_svc_insert_folder_query_list, &sql);
561                 }
562         }
563
564         SAFE_FREE(path_name);
565
566         return ret;
567 }
568
569 int _media_svc_delete_invalid_folder(sqlite3 *handle, const char *storage_id, uid_t uid)
570 {
571         int ret = MS_MEDIA_ERR_NONE;
572         char *sql = NULL;
573
574         sql = sqlite3_mprintf("DELETE FROM '%s' WHERE storage_uuid = '%q' AND validity = 0", MEDIA_SVC_DB_TABLE_FOLDER, storage_id);
575         ret = _media_svc_sql_query(handle, sql, uid);
576
577         sqlite3_free(sql);
578
579         return ret;
580 }
581
582 int _media_svc_set_folder_validity(sqlite3 *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
583 {
584         int ret = MS_MEDIA_ERR_NONE;
585         char start_path_id[MEDIA_SVC_UUID_SIZE+1] = {0,};
586         char *sql = NULL;
587
588         if (is_recursive) {
589                 sql = sqlite3_mprintf("UPDATE '%s' SET validity = %d WHERE storage_uuid = '%q' AND path LIKE '%q%%'", MEDIA_SVC_DB_TABLE_FOLDER, validity, storage_id, start_path);
590         } else {
591                 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, start_path, start_path_id, uid);
592                 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_folder_id_by_foldername fail");
593                 media_svc_retvm_if(!STRING_VALID(start_path_id), MS_MEDIA_ERR_INVALID_PARAMETER, "start_path_id is NULL");
594
595                 sql = sqlite3_mprintf("UPDATE '%s' SET validity = %d WHERE storage_uuid = '%q' AND parent_folder_uuid = '%q'", MEDIA_SVC_DB_TABLE_FOLDER, validity, storage_id, start_path_id);
596         }
597
598         ret = _media_svc_sql_query(handle, sql, uid);
599
600         sqlite3_free(sql);
601
602         return ret;
603 }
604
605 int _media_svc_delete_folder_by_storage_id(sqlite3 *handle, const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
606 {
607         int ret = MS_MEDIA_ERR_NONE;
608         char *sql = NULL;
609
610         sql = sqlite3_mprintf("DELETE FROM '%s' WHERE storage_uuid = '%q' AND storage_type = %d", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, storage_type);
611         ret = _media_svc_sql_query(handle, sql, uid);
612
613         sqlite3_free(sql);
614
615         return ret;
616 }
617
618 GList ** _media_svc_get_folder_list_ptr(void)
619 {
620         return &g_media_svc_insert_folder_query_list;
621 }
622