Update scanner process
[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, uid_t uid)
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_svc_get_path(uid)) == 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 [%s]", 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, uid);
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                         dir_path = strndup(path, strlen(path));
250                         folder_search_end = TRUE;
251                         media_svc_error("[No-Error] End Path [%s]", dir_path);
252                 }
253
254                 if (strcmp(dir_path, MEDIA_ROOT_PATH_EXTERNAL) == 0) {
255                         /*To avoid insert MEDIA_ROOT_PATH_EXTERNAL path*/
256                         continue;
257                 }
258
259                 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, dir_path, parent_folder_uuid, uid);
260                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
261                         folder_uuid = _media_info_generate_uuid();
262                         if (folder_uuid == NULL) {
263                                 media_svc_error("Invalid UUID");
264                                 SAFE_FREE(dir_path);
265                                 return MS_MEDIA_ERR_INTERNAL;
266                         }
267
268                         ret = __media_svc_append_folder(handle, storage_id, storage_type, folder_uuid, dir_path, parent_folder_uuid, FALSE, uid);
269                         if (ret != MS_MEDIA_ERR_NONE) {
270                                 media_svc_error("__media_svc_append_folder is failed");
271                         }
272
273                         media_svc_error("[No-Error] New Appended folder path [%s], folder_uuid [%s], parent_folder_uuid [%s]", dir_path, folder_uuid, parent_folder_uuid);
274                         _strncpy_safe(parent_folder_uuid, folder_uuid, MEDIA_SVC_UUID_SIZE + 1);
275                 } else {
276                         media_svc_error("EXIST dir path : %s\n", dir_path);
277                 }
278
279                 SAFE_FREE(dir_path);
280         }
281
282         if(STRING_VALID(folder_uuid)) {
283                 _strncpy_safe(folder_id, folder_uuid, MEDIA_SVC_UUID_SIZE + 1);
284         } else {
285                 media_svc_error("Fail to get folder_uuid");
286                 return MS_MEDIA_ERR_INTERNAL;
287         }
288
289         return MS_MEDIA_ERR_NONE;
290 }
291
292 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)
293 {
294         int ret = MS_MEDIA_ERR_NONE;
295
296         ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, path, folder_id, uid);
297
298         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
299                 ret = __media_svc_get_and_append_parent_folder(handle, storage_id, path, storage_type, folder_id, uid);
300         }
301
302         return ret;
303 }
304
305 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)
306 {
307         char *dir_path = NULL;
308         int ret = MS_MEDIA_ERR_NONE;
309
310         dir_path = g_path_get_dirname(path);
311
312         ret =  _media_svc_get_and_append_folder(handle, storage_id, dir_path, storage_type, folder_id, uid);
313
314         SAFE_FREE(dir_path);
315
316         return ret;
317 }
318
319 int _media_svc_update_folder_table(sqlite3 *handle, const char *storage_id, uid_t uid)
320 {
321         int ret = MS_MEDIA_ERR_NONE;
322         char *sql = NULL;
323
324         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'))",
325              MEDIA_SVC_DB_TABLE_FOLDER, MEDIA_SVC_DB_TABLE_FOLDER, storage_id);
326
327         ret = _media_svc_sql_query(handle, sql, uid);
328         sqlite3_free(sql);
329
330         return ret;
331 }
332
333 static int __media_svc_count_all_folders(sqlite3 *handle, char *start_path, int *count)
334 {
335         int ret = MS_MEDIA_ERR_NONE;
336         sqlite3_stmt *sql_stmt = NULL;
337         char *sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE path LIKE '%q%%'", MEDIA_SVC_DB_TABLE_FOLDER, start_path);
338
339         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
340         if (ret != MS_MEDIA_ERR_NONE) {
341                 media_svc_error("error when _media_svc_sql_prepare_to_step. err = [%d]", ret);
342                 return ret;
343         }
344
345         *count = sqlite3_column_int(sql_stmt, 0);
346
347         SQLITE3_FINALIZE(sql_stmt);
348
349         return MS_MEDIA_ERR_NONE;
350 }
351
352 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)
353 {
354         int ret = MS_MEDIA_ERR_NONE;
355         int idx = 0;
356         sqlite3_stmt *sql_stmt = NULL;
357         char *sql = NULL;
358         int cnt = 0;
359         char **folder_uuid = NULL;
360         int i = 0;
361
362         ret  = __media_svc_count_all_folders(handle, start_path, &cnt);
363         if (ret != MS_MEDIA_ERR_NONE) {
364                 media_svc_error("error when __media_svc_count_all_folders. err = [%d]", ret);
365                 return ret;
366         }
367
368         if (cnt > 0) {
369                 sql = sqlite3_mprintf("SELECT path, modified_time, folder_uuid FROM '%s' WHERE path LIKE '%q%%'", MEDIA_SVC_DB_TABLE_FOLDER, start_path);
370         } else {
371                 *folder_list = NULL;
372                 *modified_time_list = NULL;
373                 *item_num_list = NULL;
374                 return MS_MEDIA_ERR_NONE;
375         }
376
377         *folder_list = malloc(sizeof(char *) * cnt);
378         *modified_time_list = malloc(sizeof(int) * cnt);
379         *item_num_list = malloc(sizeof(int) * cnt);
380         folder_uuid = malloc(sizeof(char *) * cnt);
381
382         if ((*folder_list == NULL) || (*modified_time_list == NULL) || (*item_num_list == NULL) || (folder_uuid == NULL)) {
383                 media_svc_error("Out of memory");
384                 goto ERROR;
385         }
386         memset(folder_uuid, 0x0, sizeof(char *) * cnt);
387
388         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
389         if (ret != MS_MEDIA_ERR_NONE) {
390                 media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
391                 goto ERROR;
392         }
393
394         media_svc_debug("QEURY OK");
395
396         while (1) {
397                 if (STRING_VALID((char *)sqlite3_column_text(sql_stmt, 0)))
398                         (*folder_list)[idx] = strdup((char *)sqlite3_column_text(sql_stmt, 0));
399
400                 (*modified_time_list)[idx] = (int)sqlite3_column_int(sql_stmt, 1);
401
402                 /* get the folder's id */
403                 if (STRING_VALID((char *)sqlite3_column_text(sql_stmt, 2)))
404                         folder_uuid[idx] = strdup((char *)sqlite3_column_text(sql_stmt, 2));
405
406                 idx++;
407
408                 if (sqlite3_step(sql_stmt) != SQLITE_ROW)
409                         break;
410         }
411         SQLITE3_FINALIZE(sql_stmt);
412
413         /*get the numbder of item in the folder by using folder's id */
414         for (i = 0; i < idx; i++) {
415                 if (STRING_VALID(folder_uuid[i])) {
416                         sql = sqlite3_mprintf("SELECT COUNT(*) FROM %s WHERE (folder_uuid='%q' AND validity = 1)", MEDIA_SVC_DB_TABLE_MEDIA, folder_uuid[i]);
417                         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
418                         if (ret != MS_MEDIA_ERR_NONE) {
419                                 media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
420                                 goto ERROR;
421                         }
422
423                         (*item_num_list)[i] = (int)sqlite3_column_int(sql_stmt, 0);
424
425                         SQLITE3_FINALIZE(sql_stmt);
426                 } else {
427                         media_svc_error("Invalid Folder Id");
428                 }
429         }
430
431         if (cnt == idx) {
432                 *count = cnt;
433                 media_svc_debug("Get Folder is OK");
434         } else {
435                 media_svc_error("Fail to get folder");
436                 ret = MS_MEDIA_ERR_INTERNAL;
437                 goto ERROR;
438         }
439
440         /* free all data */
441         for (i  = 0; i < idx; i++) {
442                 SAFE_FREE(folder_uuid[i]);
443         }
444         SAFE_FREE(folder_uuid);
445
446         return ret;
447
448 ERROR:
449
450         /* free all data */
451         for (i  = 0; i < idx; i++) {
452                 SAFE_FREE((*folder_list)[i]);
453                 SAFE_FREE(folder_uuid[i]);
454         }
455         SAFE_FREE(*folder_list);
456         SAFE_FREE(*modified_time_list);
457         SAFE_FREE(*item_num_list);
458         SAFE_FREE(folder_uuid);
459
460         *count = 0;
461
462         return ret;
463 }
464
465 int _media_svc_get_folder_info_by_foldername(sqlite3 *handle, const char *storage_id, const char *folder_name, char *folder_id, time_t *modified_time)
466 {
467         int ret = MS_MEDIA_ERR_NONE;
468         sqlite3_stmt *sql_stmt = NULL;
469
470         char *sql = sqlite3_mprintf("SELECT folder_uuid, modified_time FROM %s WHERE (storage_uuid = '%q' AND path = '%q');", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, folder_name);
471
472         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
473
474         if (ret != MS_MEDIA_ERR_NONE) {
475                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
476                         media_svc_debug("there is no folder.");
477                 } else {
478                         media_svc_error("error when _media_svc_get_folder_id_by_foldername. err = [%d]", ret);
479                 }
480                 return ret;
481         }
482
483         _strncpy_safe(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE + 1);
484         *modified_time = (int)sqlite3_column_int(sql_stmt, 1);
485
486         SQLITE3_FINALIZE(sql_stmt);
487
488         return ret;
489 }
490
491 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)
492 {
493         char *path_name = NULL;
494         int ret = MS_MEDIA_ERR_NONE;
495         char *sql = NULL;
496
497         path_name = strdup(path);
498         if (path_name == NULL) {
499                 media_svc_error("out of memory");
500                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
501         }
502
503         ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, path_name, folder_id, uid);
504         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
505                 bool is_root = FALSE;
506                 bool is_parent_root = FALSE;
507
508                 ret = __media_svc_is_root_path(path_name, &is_root, uid);
509                 ret = __media_svc_parent_is_ext_root_path(path_name, &is_parent_root);
510
511                 char parent_folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0, };
512                 memset(parent_folder_uuid, 0x00, sizeof(parent_folder_uuid));
513
514                 if ((is_root == FALSE) && (is_parent_root == FALSE)) {
515                         /*get parent folder id*/
516                         char *parent_path_name = g_path_get_dirname(path_name);
517
518                         ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, parent_path_name, parent_folder_uuid, uid);
519                         if (ret != MS_MEDIA_ERR_NONE) {
520                                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
521                                 /*if No-Root directory scanning start before doing storage scanning, parent_folder_uuid can be NULL.
522                                 You can remove above logic if use below logic always. but I keep above code for the performance issue.
523                                 Most case (except No-Root directory scanning start before doing storage scanning) get parent_folder_uuid well*/
524                                 media_svc_error("[No-Error] There is no proper parent_folder_uuid. so try to make it");
525                                 ret = _media_svc_get_and_append_folder(handle, storage_id, path, storage_type, folder_id, uid);
526                                 } else {
527                                         media_svc_error("error when _media_svc_get_parent_folder_id_by_foldername. err = [%d]", ret);
528                                 }
529
530                                 SAFE_FREE(path_name);
531                                 SAFE_FREE(parent_path_name);
532
533                                 return ret;
534                         }
535                 }
536
537                 char *folder_uuid = _media_info_generate_uuid();
538                 if (folder_uuid == NULL) {
539                         media_svc_error("Invalid UUID");
540                         SAFE_FREE(path_name);
541                         return MS_MEDIA_ERR_INTERNAL;
542                 }
543
544                 ret = __media_svc_append_folder(handle, storage_id, storage_type, folder_uuid, path_name, parent_folder_uuid, stack_query, uid);
545
546                 _strncpy_safe(folder_id, folder_uuid, MEDIA_SVC_UUID_SIZE+1);
547
548         } else {
549                 sql = sqlite3_mprintf("UPDATE '%s' SET validity=1 WHERE storage_uuid = '%q' AND path = '%q'", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
550
551                 if(!stack_query)
552                 {
553                         ret = _media_svc_sql_query(handle, sql, uid);
554                         sqlite3_free(sql);
555                 }
556                 else
557                 {
558                         _media_svc_sql_query_add(&g_media_svc_insert_folder_query_list, &sql);
559                 }
560         }
561
562         SAFE_FREE(path_name);
563
564         return ret;
565 }
566
567 int _media_svc_delete_invalid_folder(sqlite3 *handle, const char *storage_id, uid_t uid)
568 {
569         int ret = MS_MEDIA_ERR_NONE;
570         char *sql = NULL;
571
572         sql = sqlite3_mprintf("DELETE FROM '%s' WHERE storage_uuid = '%q' AND validity = 0", MEDIA_SVC_DB_TABLE_FOLDER, storage_id);
573         ret = _media_svc_sql_query(handle, sql, uid);
574
575         sqlite3_free(sql);
576
577         return ret;
578 }
579
580 int _media_svc_set_folder_validity(sqlite3 *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
581 {
582         int ret = MS_MEDIA_ERR_NONE;
583         char *sql = NULL;
584         char start_path_id[MEDIA_SVC_UUID_SIZE+1] = {0,};
585
586         if (is_recursive) {
587                 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, start_path, start_path_id, uid);
588                 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_folder_id_by_foldername fail");
589                 media_svc_retvm_if(!STRING_VALID(start_path_id), MS_MEDIA_ERR_INVALID_PARAMETER, "start_path_id is NULL");
590
591                 sql = sqlite3_mprintf("UPDATE '%s' SET validity = %d WHERE storage_uuid = '%q' AND (parent_folder_uuid = '%q' OR folder_uuid ='%q')",
592                                                 MEDIA_SVC_DB_TABLE_FOLDER, validity, storage_id, start_path_id, start_path_id);
593         } else {
594                 sql = sqlite3_mprintf("UPDATE '%s' SET validity = %d WHERE storage_uuid = '%q' AND path = '%q'",
595                                                 MEDIA_SVC_DB_TABLE_FOLDER, validity, storage_id, start_path);
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
623 int _media_svc_delete_invalid_folder_by_path(sqlite3 *handle, const char *storage_id, const char *folder_path, uid_t uid, int *delete_count)
624 {
625         int ret = MS_MEDIA_ERR_NONE;
626         char *sql = NULL;
627         int del_count = 0;
628         sqlite3_stmt *sql_stmt = NULL;
629
630         if (folder_path == NULL)
631                 return MS_MEDIA_ERR_INVALID_PARAMETER;
632
633         /*check the number of the deleted folder*/
634         sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE (storage_uuid = '%q' AND validity = 0 AND PATH LIKE '%q/%%')", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, folder_path);
635
636         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
637         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
638
639         del_count = sqlite3_column_int(sql_stmt, 0);
640
641         SQLITE3_FINALIZE(sql_stmt);
642         sql = NULL;
643
644         /*delete invalid folder*/
645         sql = sqlite3_mprintf("DELETE FROM '%s' WHERE (storage_uuid = '%q' AND validity = 0 AND PATH LIKE '%q%%')", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, folder_path);
646         ret = _media_svc_sql_query(handle, sql, uid);
647
648         sqlite3_free(sql);
649
650         *delete_count = del_count;
651
652         return ret;
653 }
654
655 int _media_svc_count_folder_with_path(sqlite3 *handle,  const char *storage_id, const char *path, int *count)
656 {
657         int ret = MS_MEDIA_ERR_NONE;
658         sqlite3_stmt *sql_stmt = NULL;
659
660         char *sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE (storage_uuid='%q' AND path='%q')", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
661
662         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
663
664         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
665
666         *count = sqlite3_column_int(sql_stmt, 0);
667
668         SQLITE3_FINALIZE(sql_stmt);
669
670         return MS_MEDIA_ERR_NONE;
671 }
672
673 int _media_svc_count_subfolder_with_path(sqlite3 *handle,  const char *storage_id, const char *path, int *count)
674 {
675         int ret = MS_MEDIA_ERR_NONE;
676         sqlite3_stmt *sql_stmt = NULL;
677
678         char *sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE (storage_uuid='%q' AND path LIKE'%q/%%')", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
679
680         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
681
682         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
683
684         *count = sqlite3_column_int(sql_stmt, 0);
685
686         SQLITE3_FINALIZE(sql_stmt);
687
688         return MS_MEDIA_ERR_NONE;
689 }
690
691 int _media_svc_get_folder_uuid(sqlite3 *handle, const char *storage_id, const char *path, char *folder_id)
692 {
693         int ret = MS_MEDIA_ERR_NONE;
694         sqlite3_stmt *sql_stmt = NULL;
695         char *sql = NULL;
696
697         sql = sqlite3_mprintf("SELECT folder_uuid FROM '%s' WHERE (storage_uuid='%q' AND path='%q')", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
698
699         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
700
701         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
702
703         _strncpy_safe(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE+1);
704
705         SQLITE3_FINALIZE(sql_stmt);
706
707         if(!STRING_VALID(folder_id))
708         {
709                 media_svc_error("Not found valid storage id [%s]", path);
710                 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
711         }
712
713         return ret;
714 }
715