Fix build warning
[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 #define FOLDER_SCAN_DONE 4
31
32 extern __thread GList *g_media_svc_move_item_query_list;
33 static __thread GList *g_media_svc_insert_folder_query_list;
34
35 static int __media_svc_is_root_path(const char *folder_path, bool *is_root, uid_t uid)
36 {
37         media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
38
39         *is_root = FALSE;
40
41         char *internal_path = _media_svc_get_path(uid);
42
43         if ((STRING_VALID(internal_path) && (strcmp(folder_path, internal_path) == 0)) ||
44                 (STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && strcmp(folder_path, MEDIA_ROOT_PATH_SDCARD) == 0) ||
45                 (STRING_VALID(MEDIA_ROOT_PATH_CLOUD) && strcmp(folder_path, MEDIA_ROOT_PATH_CLOUD) == 0)) {
46                 media_svc_debug("ROOT PATH [%s]", folder_path);
47                 *is_root = TRUE;
48         }
49
50         SAFE_FREE(internal_path);
51         return MS_MEDIA_ERR_NONE;
52 }
53
54 static int __media_svc_parent_is_ext_root_path(const char *folder_path, bool *is_root)
55 {
56         char *parent_folder_path = NULL;
57
58         media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
59
60         *is_root = FALSE;
61
62         parent_folder_path = g_path_get_dirname(folder_path);
63
64         if (!STRING_VALID(parent_folder_path)) {
65                 media_svc_error("error : g_path_get_dirname falied");
66                 SAFE_FREE(parent_folder_path);
67                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
68         }
69
70         if (STRING_VALID(MEDIA_ROOT_PATH_EXTERNAL) && (strcmp(parent_folder_path, MEDIA_ROOT_PATH_EXTERNAL) == 0)) {
71                 media_svc_debug("parent folder is ROOT PATH [%s]", parent_folder_path);
72                 *is_root = TRUE;
73         }
74
75         SAFE_FREE(parent_folder_path);
76
77         return MS_MEDIA_ERR_NONE;
78 }
79
80 int _media_svc_get_folder_id_by_foldername(sqlite3 *handle, const char *storage_id, const char *folder_name, char *folder_id, uid_t uid)
81 {
82         int ret = MS_MEDIA_ERR_NONE;
83         sqlite3_stmt *sql_stmt = NULL;
84         char *sql = NULL;
85         char parent_folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0, };
86         char *temp_parent_uuid = NULL;
87         char *parent_folder_path = NULL;
88
89         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);
90
91         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
92
93         if (ret != MS_MEDIA_ERR_NONE) {
94                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
95                         media_svc_debug("there is no folder.");
96                 } else {
97                         media_svc_error("error when _media_svc_get_folder_id_by_foldername. err = [%d]", ret);
98                 }
99                 return ret;
100         }
101
102         memset(parent_folder_uuid, 0, sizeof(parent_folder_uuid));
103
104         _strncpy_safe(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE + 1);
105         if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 1)))       /*root path can be null*/
106                 _strncpy_safe(parent_folder_uuid, (const char *)sqlite3_column_text(sql_stmt, 1), MEDIA_SVC_UUID_SIZE+1);
107
108         SQLITE3_FINALIZE(sql_stmt);
109
110         /*root path can be null*/
111         if (!STRING_VALID(parent_folder_uuid)) {
112                 bool is_root = FALSE;
113
114                 ret = __media_svc_is_root_path(folder_name, &is_root, uid);
115                 if (is_root)
116                         return MS_MEDIA_ERR_NONE;
117
118                 ret = __media_svc_parent_is_ext_root_path(folder_name, &is_root);
119                 if (is_root)
120                         return MS_MEDIA_ERR_NONE;
121         }
122
123         /* Notice : Below code is the code only for inserting parent folder uuid when upgrade the media db version 3 to 4 */
124         /* Check parent folder uuid */
125         if (!STRING_VALID(parent_folder_uuid)) {
126                 /* update parent_uuid */
127                 media_svc_error("[No-Error] there is no parent folder uuid. PLEASE CHECK IT");
128
129                 parent_folder_path = g_path_get_dirname(folder_name);
130                 if (!STRING_VALID(parent_folder_path)) {
131                         media_svc_error("error : g_path_get_dirname falied.");
132                         return MS_MEDIA_ERR_OUT_OF_MEMORY;
133                 }
134
135                 if (STRING_VALID(storage_id))
136                         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);
137                 else
138                         sql = sqlite3_mprintf("SELECT folder_uuid FROM '%s' WHERE path = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, parent_folder_path);
139
140                 SAFE_FREE(parent_folder_path);
141
142                 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
143                 if (ret == MS_MEDIA_ERR_NONE) {
144                         temp_parent_uuid = (char *)sqlite3_column_text(sql_stmt, 0);
145                         if (temp_parent_uuid != NULL) {
146                                 _strncpy_safe(parent_folder_uuid, temp_parent_uuid, MEDIA_SVC_UUID_SIZE+1);
147                         }
148
149                         SQLITE3_FINALIZE(sql_stmt);
150                 }
151
152                 if (STRING_VALID(parent_folder_uuid)) {
153                         if (STRING_VALID(storage_id))
154                                 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);
155                         else
156                                 sql = sqlite3_mprintf("UPDATE %q SET parent_folder_uuid = '%q' WHERE path = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, parent_folder_uuid, folder_name);
157                         ret = _media_svc_sql_query(sql, uid);
158                         sqlite3_free(sql);
159                 } else {
160                         media_svc_error("error when get parent folder uuid");
161                 }
162         }
163
164         return ret;
165 }
166
167 static int __media_svc_append_folder(const char *storage_id, media_svc_storage_type_e storage_type,
168                         const char *folder_id, const char *folder_path, const char *parent_folder_uuid, bool stack_query, uid_t uid)
169 {
170         int ret = MS_MEDIA_ERR_NONE;
171         char *folder_name = NULL;
172         int folder_modified_date = 0;
173
174         folder_name = g_path_get_basename(folder_path);
175         folder_modified_date = _media_svc_get_file_time(folder_path);
176
177         /*Update Pinyin If Support Pinyin*/
178         char *folder_name_pinyin = NULL;
179         if (_media_svc_check_pinyin_support())
180                 _media_svc_get_pinyin_str(folder_name, &folder_name_pinyin);
181
182         char *sql = sqlite3_mprintf("INSERT INTO %s (folder_uuid, path, name, storage_uuid, storage_type, modified_time, name_pinyin, parent_folder_uuid) \
183                                                         values (%Q, %Q, %Q, %Q, '%d', '%d', %Q, %Q); ",
184                                                         MEDIA_SVC_DB_TABLE_FOLDER, folder_id, folder_path, folder_name, storage_id, storage_type, folder_modified_date, folder_name_pinyin, parent_folder_uuid);
185
186         if (!stack_query) {
187                 ret = _media_svc_sql_query(sql, uid);
188                 sqlite3_free(sql);
189         } else {
190                 _media_svc_sql_query_add(&g_media_svc_insert_folder_query_list, &sql);
191         }
192
193         SAFE_FREE(folder_name);
194         SAFE_FREE(folder_name_pinyin);
195
196         return ret;
197 }
198
199 int _media_svc_update_folder_modified_time_by_folder_uuid(const char *folder_uuid, const char *folder_path, bool stack_query, uid_t uid)
200 {
201         int ret = MS_MEDIA_ERR_NONE;
202         int modified_time = 0;
203
204         modified_time = _media_svc_get_file_time(folder_path);
205
206         char *sql = sqlite3_mprintf("UPDATE %s SET modified_time=%d WHERE folder_uuid=%Q;", MEDIA_SVC_DB_TABLE_FOLDER, modified_time, folder_uuid);
207
208         if (!stack_query) {
209                 ret = _media_svc_sql_query(sql, uid);
210                 sqlite3_free(sql);
211         } else {
212                 _media_svc_sql_query_add(&g_media_svc_move_item_query_list, &sql);
213         }
214
215         return ret;
216 }
217
218 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)
219 {
220         int ret = MS_MEDIA_ERR_NONE;
221         unsigned int next_pos = 0;
222         char *next = NULL;
223         char *dir_path = NULL;
224         const char *token = "/";
225         char *folder_uuid = NULL;
226         char parent_folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
227         bool folder_search_end = FALSE;
228         char *internal_path = NULL;
229
230         memset(parent_folder_uuid, 0, sizeof(parent_folder_uuid));
231         internal_path = _media_svc_get_path(uid);
232
233         if (STRING_VALID(internal_path) && (strncmp(path, internal_path, strlen(internal_path)) == 0))
234                 next_pos = strlen(internal_path);
235         else if (STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0)
236                 next_pos = strlen(MEDIA_ROOT_PATH_SDCARD);
237         else if (STRING_VALID(MEDIA_ROOT_PATH_CLOUD) && (strncmp(path, MEDIA_ROOT_PATH_CLOUD, strlen(MEDIA_ROOT_PATH_CLOUD)) == 0))
238                 next_pos = strlen(MEDIA_ROOT_PATH_CLOUD);
239         else if (STRING_VALID(MEDIA_ROOT_PATH_EXTERNAL) && strncmp(path, MEDIA_ROOT_PATH_EXTERNAL, strlen(MEDIA_ROOT_PATH_EXTERNAL)) == 0)
240                 next_pos = strlen(MEDIA_ROOT_PATH_EXTERNAL);
241         else {
242                 media_svc_error("Invalid Path");
243                 SAFE_FREE(internal_path);
244                 return MS_MEDIA_ERR_INTERNAL;
245         }
246
247         SAFE_FREE(internal_path);
248
249         while (!folder_search_end) {
250                 next = strstr(path + next_pos, token);
251                 if (next != NULL) {
252                         next_pos = (next - path);
253                         dir_path = strndup(path, next_pos);
254                         next_pos++;
255                 } else {
256                         dir_path = strndup(path, strlen(path));
257                         folder_search_end = TRUE;
258                         media_svc_error("[No-Error] End Path [%s]", dir_path);
259                 }
260
261                 if (STRING_VALID(MEDIA_ROOT_PATH_EXTERNAL) && (strcmp(dir_path, MEDIA_ROOT_PATH_EXTERNAL) == 0)) {
262                         /*To avoid insert MEDIA_ROOT_PATH_EXTERNAL path*/
263                         continue;
264                 }
265
266                 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, dir_path, parent_folder_uuid, uid);
267                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
268                         folder_uuid = _media_info_generate_uuid();
269                         if (folder_uuid == NULL) {
270                                 media_svc_error("Invalid UUID");
271                                 SAFE_FREE(dir_path);
272                                 return MS_MEDIA_ERR_INTERNAL;
273                         }
274
275                         ret = __media_svc_append_folder(storage_id, storage_type, folder_uuid, dir_path, parent_folder_uuid, FALSE, uid);
276                         if (ret != MS_MEDIA_ERR_NONE) {
277                                 media_svc_error("__media_svc_append_folder is failed");
278                         }
279
280                         media_svc_error("[No-Error] New Appended folder path [%s], folder_uuid [%s], parent_folder_uuid [%s]", dir_path, folder_uuid, parent_folder_uuid);
281                         _strncpy_safe(parent_folder_uuid, folder_uuid, MEDIA_SVC_UUID_SIZE + 1);
282                 } else {
283                         media_svc_error("EXIST dir path : %s\n", dir_path);
284                 }
285
286                 SAFE_FREE(dir_path);
287         }
288
289         if (STRING_VALID(folder_uuid)) {
290                 _strncpy_safe(folder_id, folder_uuid, MEDIA_SVC_UUID_SIZE + 1);
291         } else {
292                 media_svc_error("Fail to get folder_uuid");
293                 return MS_MEDIA_ERR_INTERNAL;
294         }
295
296         return MS_MEDIA_ERR_NONE;
297 }
298
299 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)
300 {
301         int ret = MS_MEDIA_ERR_NONE;
302
303         ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, path, folder_id, uid);
304
305         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
306                 ret = __media_svc_get_and_append_parent_folder(handle, storage_id, path, storage_type, folder_id, uid);
307         }
308
309         return ret;
310 }
311
312 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)
313 {
314         char *dir_path = NULL;
315         int ret = MS_MEDIA_ERR_NONE;
316
317         dir_path = g_path_get_dirname(path);
318
319         ret = _media_svc_get_and_append_folder(handle, storage_id, dir_path, storage_type, folder_id, uid);
320
321         SAFE_FREE(dir_path);
322
323         return ret;
324 }
325
326 int _media_svc_update_folder_table(const char *storage_id, uid_t uid)
327 {
328         int ret = MS_MEDIA_ERR_NONE;
329         char *sql = NULL;
330
331         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'));",
332                 MEDIA_SVC_DB_TABLE_FOLDER, MEDIA_SVC_DB_TABLE_FOLDER, storage_id);
333
334         ret = _media_svc_sql_query(sql, uid);
335         sqlite3_free(sql);
336
337         return ret;
338 }
339
340 static int __media_svc_count_all_folders(sqlite3 *handle, char *start_path, int *count)
341 {
342         int ret = MS_MEDIA_ERR_NONE;
343         sqlite3_stmt *sql_stmt = NULL;
344         char *sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE path LIKE '%q%%'", MEDIA_SVC_DB_TABLE_FOLDER, start_path);
345
346         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
347         if (ret != MS_MEDIA_ERR_NONE) {
348                 media_svc_error("error when _media_svc_sql_prepare_to_step. err = [%d]", ret);
349                 return ret;
350         }
351
352         *count = sqlite3_column_int(sql_stmt, 0);
353
354         SQLITE3_FINALIZE(sql_stmt);
355
356         return MS_MEDIA_ERR_NONE;
357 }
358
359 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)
360 {
361         int ret = MS_MEDIA_ERR_NONE;
362         int idx = 0;
363         sqlite3_stmt *sql_stmt = NULL;
364         char *sql = NULL;
365         int cnt = 0;
366         char **folder_uuid = NULL;
367         int i = 0;
368
369         ret = __media_svc_count_all_folders(handle, start_path, &cnt);
370         if (ret != MS_MEDIA_ERR_NONE) {
371                 media_svc_error("error when __media_svc_count_all_folders. err = [%d]", ret);
372                 return ret;
373         }
374
375         if (cnt > 0) {
376                 sql = sqlite3_mprintf("SELECT path, modified_time, folder_uuid FROM '%s' WHERE path LIKE '%q%%'", MEDIA_SVC_DB_TABLE_FOLDER, start_path);
377         } else {
378                 *folder_list = NULL;
379                 *modified_time_list = NULL;
380                 *item_num_list = NULL;
381                 return MS_MEDIA_ERR_NONE;
382         }
383
384         *folder_list = malloc(sizeof(char *) * cnt);
385         *modified_time_list = malloc(sizeof(int) * cnt);
386         *item_num_list = malloc(sizeof(int) * cnt);
387         folder_uuid = malloc(sizeof(char *) * cnt);
388
389         if ((*folder_list == NULL) || (*modified_time_list == NULL) || (*item_num_list == NULL) || (folder_uuid == NULL)) {
390                 media_svc_error("Out of memory");
391                 goto ERROR;
392         }
393         memset(folder_uuid, 0x0, sizeof(char *) * cnt);
394
395         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
396         if (ret != MS_MEDIA_ERR_NONE) {
397                 media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
398                 goto ERROR;
399         }
400
401         media_svc_debug("QEURY OK");
402
403         while (1) {
404                 (*folder_list)[idx] = g_strdup((char *)sqlite3_column_text(sql_stmt, 0));
405                 (*modified_time_list)[idx] = (int)sqlite3_column_int(sql_stmt, 1);
406
407                 /* get the folder's id */
408                 folder_uuid[idx] = g_strdup((char *)sqlite3_column_text(sql_stmt, 2));
409
410                 idx++;
411
412                 if (sqlite3_step(sql_stmt) != SQLITE_ROW)
413                         break;
414         }
415         SQLITE3_FINALIZE(sql_stmt);
416
417         /*get the numbder of item in the folder by using folder's id */
418         for (i = 0; i < idx; i++) {
419                 if (STRING_VALID(folder_uuid[i])) {
420                         sql = sqlite3_mprintf("SELECT COUNT(*) FROM %s WHERE (folder_uuid='%q' AND validity = 1)", MEDIA_SVC_DB_TABLE_MEDIA, folder_uuid[i]);
421                         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
422                         if (ret != MS_MEDIA_ERR_NONE) {
423                                 media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
424                                 goto ERROR;
425                         }
426
427                         (*item_num_list)[i] = (int)sqlite3_column_int(sql_stmt, 0);
428
429                         SQLITE3_FINALIZE(sql_stmt);
430                 } else {
431                         media_svc_error("Invalid Folder Id");
432                 }
433         }
434
435         if (cnt == idx) {
436                 *count = cnt;
437                 media_svc_debug("Get Folder is OK");
438         } else {
439                 media_svc_error("Fail to get folder");
440                 ret = MS_MEDIA_ERR_INTERNAL;
441                 goto ERROR;
442         }
443
444         /* free all data */
445         for (i = 0; i < idx; i++) {
446                 SAFE_FREE(folder_uuid[i]);
447         }
448         SAFE_FREE(folder_uuid);
449
450         return ret;
451
452 ERROR:
453
454         /* free all data */
455         for (i = 0; i < idx; i++) {
456                 SAFE_FREE((*folder_list)[i]);
457                 SAFE_FREE(folder_uuid[i]);
458         }
459         SAFE_FREE(*folder_list);
460         SAFE_FREE(*modified_time_list);
461         SAFE_FREE(*item_num_list);
462         SAFE_FREE(folder_uuid);
463
464         *count = 0;
465
466         return ret;
467 }
468
469 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)
470 {
471         int ret = MS_MEDIA_ERR_NONE;
472         sqlite3_stmt *sql_stmt = NULL;
473
474         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);
475
476         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
477
478         if (ret != MS_MEDIA_ERR_NONE) {
479                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
480                         media_svc_debug("there is no folder.");
481                 } else {
482                         media_svc_error("error when _media_svc_get_folder_id_by_foldername. err = [%d]", ret);
483                 }
484                 return ret;
485         }
486
487         _strncpy_safe(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE + 1);
488         *modified_time = (int)sqlite3_column_int(sql_stmt, 1);
489
490         SQLITE3_FINALIZE(sql_stmt);
491
492         return ret;
493 }
494
495 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)
496 {
497         char *path_name = NULL;
498         int ret = MS_MEDIA_ERR_NONE;
499         char *sql = NULL;
500
501         path_name = strdup(path);
502         if (path_name == NULL) {
503                 media_svc_error("out of memory");
504                 return MS_MEDIA_ERR_OUT_OF_MEMORY;
505         }
506
507         ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, path_name, folder_id, uid);
508         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
509                 bool is_root = FALSE;
510                 bool is_parent_root = FALSE;
511
512                 ret = __media_svc_is_root_path(path_name, &is_root, uid);
513                 ret = __media_svc_parent_is_ext_root_path(path_name, &is_parent_root);
514
515                 char parent_folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0, };
516                 memset(parent_folder_uuid, 0x00, sizeof(parent_folder_uuid));
517
518                 if ((is_root == FALSE) && (is_parent_root == FALSE)) {
519                         /*get parent folder id*/
520                         char *parent_path_name = g_path_get_dirname(path_name);
521
522                         ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, parent_path_name, parent_folder_uuid, uid);
523                         if (ret != MS_MEDIA_ERR_NONE) {
524                                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
525                                 /*if No-Root directory scanning start before doing storage scanning, parent_folder_uuid can be NULL.
526                                 You can remove above logic if use below logic always. but I keep above code for the performance issue.
527                                 Most case (except No-Root directory scanning start before doing storage scanning) get parent_folder_uuid well*/
528                                 media_svc_error("[No-Error] There is no proper parent_folder_uuid. so try to make it");
529                                 ret = _media_svc_get_and_append_folder(handle, storage_id, path, storage_type, folder_id, uid);
530                                 } else {
531                                         media_svc_error("error when _media_svc_get_parent_folder_id_by_foldername. err = [%d]", ret);
532                                 }
533
534                                 SAFE_FREE(path_name);
535                                 SAFE_FREE(parent_path_name);
536
537                                 return ret;
538                         }
539                 }
540
541                 char *folder_uuid = _media_info_generate_uuid();
542                 if (folder_uuid == NULL) {
543                         media_svc_error("Invalid UUID");
544                         SAFE_FREE(path_name);
545                         return MS_MEDIA_ERR_INTERNAL;
546                 }
547
548                 ret = __media_svc_append_folder(storage_id, storage_type, folder_uuid, path_name, parent_folder_uuid, stack_query, uid);
549
550                 _strncpy_safe(folder_id, folder_uuid, MEDIA_SVC_UUID_SIZE+1);
551
552         } else {
553                 sql = sqlite3_mprintf("UPDATE '%s' SET validity=1 WHERE storage_uuid = '%q' AND path = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
554
555                 if (!stack_query) {
556                         ret = _media_svc_sql_query(sql, uid);
557                         sqlite3_free(sql);
558                 } else {
559                         _media_svc_sql_query_add(&g_media_svc_insert_folder_query_list, &sql);
560                 }
561         }
562
563         SAFE_FREE(path_name);
564
565         return ret;
566 }
567
568 int _media_svc_delete_invalid_folder(const char *storage_id, uid_t uid)
569 {
570         int ret = MS_MEDIA_ERR_NONE;
571         char *sql = NULL;
572
573         sql = sqlite3_mprintf("DELETE FROM '%s' WHERE storage_uuid = '%q' AND validity = 0;", MEDIA_SVC_DB_TABLE_FOLDER, storage_id);
574         ret = _media_svc_sql_query(sql, uid);
575
576         sqlite3_free(sql);
577
578         return ret;
579 }
580
581 int _media_svc_set_folder_validity(sqlite3 *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
582 {
583         int ret = MS_MEDIA_ERR_NONE;
584         char *sql = NULL;
585         char start_path_id[MEDIA_SVC_UUID_SIZE+1] = {0,};
586
587         if (is_recursive) {
588                 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, start_path, start_path_id, uid);
589                 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_folder_id_by_foldername fail");
590                 media_svc_retvm_if(!STRING_VALID(start_path_id), MS_MEDIA_ERR_INVALID_PARAMETER, "start_path_id is NULL");
591
592                 sql = sqlite3_mprintf("UPDATE '%s' SET validity = %d WHERE storage_uuid = '%q' AND (path LIKE '%q/%%' OR folder_uuid ='%q')",
593                                                 MEDIA_SVC_DB_TABLE_FOLDER, validity, storage_id, start_path, start_path_id);
594         } else {
595                 sql = sqlite3_mprintf("UPDATE '%s' SET validity = %d WHERE storage_uuid = '%q' AND path = '%q';",
596                                                 MEDIA_SVC_DB_TABLE_FOLDER, validity, storage_id, start_path);
597         }
598
599         ret = _media_svc_sql_query(sql, uid);
600
601         sqlite3_free(sql);
602
603         return ret;
604 }
605
606 int _media_svc_delete_folder_by_storage_id(const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
607 {
608         int ret = MS_MEDIA_ERR_NONE;
609         char *sql = NULL;
610
611         sql = sqlite3_mprintf("DELETE FROM '%s' WHERE storage_uuid = '%q' AND storage_type = %d;", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, storage_type);
612         ret = _media_svc_sql_query(sql, uid);
613
614         sqlite3_free(sql);
615
616         return ret;
617 }
618
619 GList **_media_svc_get_folder_list_ptr(void)
620 {
621         return &g_media_svc_insert_folder_query_list;
622 }
623
624 int _media_svc_get_folder_scan_status(sqlite3 *handle, const char *storage_id, const char *path, int *scan_status)
625 {
626         int ret = MS_MEDIA_ERR_NONE;
627         sqlite3_stmt *sql_stmt = NULL;
628         char *sql = NULL;
629
630         if (!STRING_VALID(path)) {
631                 media_svc_error("Not found valid path");
632                 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
633         }
634
635         sql = sqlite3_mprintf("SELECT scan_status FROM '%s' WHERE path = '%q' AND storage_uuid = '%q'", MEDIA_SVC_DB_TABLE_FOLDER, path, storage_id);
636
637         ret = _media_svc_sql_prepare_to_step_simple(handle, sql, &sql_stmt);
638
639         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
640
641         while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
642                 *scan_status = sqlite3_column_int(sql_stmt, 0);
643         }
644
645         SQLITE3_FINALIZE(sql_stmt);
646
647         return ret;
648 }
649
650 int _media_svc_set_folder_scan_status(const char *storage_id, const char *path, int scan_status, uid_t uid)
651 {
652         int ret = MS_MEDIA_ERR_NONE;
653         char *sql = NULL;
654
655         if (path) {
656                 sql = sqlite3_mprintf("UPDATE '%s' SET scan_status=%d WHERE path = '%q' AND storage_uuid = '%q'", MEDIA_SVC_DB_TABLE_FOLDER, scan_status, path, storage_id);
657         } else {
658                 sql = sqlite3_mprintf("UPDATE '%s' SET scan_status=%d WHERE storage_uuid = '%q'", MEDIA_SVC_DB_TABLE_FOLDER, scan_status, storage_id);
659         }
660
661         ret = _media_svc_sql_query(sql, uid);
662         sqlite3_free(sql);
663
664         return ret;
665 }
666
667 int _media_svc_get_folder_modified_time_by_path(sqlite3 *handle, const char *path, const char *storage_id, time_t *modified_time)
668 {
669         int ret = MS_MEDIA_ERR_NONE;
670         sqlite3_stmt *sql_stmt = NULL;
671
672         char *sql = sqlite3_mprintf("SELECT modified_time FROM '%s' WHERE path = '%q' AND storage_uuid = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, path, storage_id);
673
674         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
675
676         if (ret != MS_MEDIA_ERR_NONE) {
677                 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
678                         media_svc_debug("there is no folder.");
679                 } else {
680                         media_svc_error("error when _media_svc_get_folder_modified_time_by_path. err = [%d]", ret);
681                 }
682                 return ret;
683         }
684
685         *modified_time = (int)sqlite3_column_int(sql_stmt, 0);
686
687         SQLITE3_FINALIZE(sql_stmt);
688
689         return ret;
690 }
691
692 int _media_svc_get_null_scan_folder_list(sqlite3 *handle, const char *storage_id, const char *path, char ***folder_list, int *count)
693 {
694         int ret = MS_MEDIA_ERR_NONE;
695         int idx = 0;
696         int cnt = 0;
697         char *sql = NULL;
698         char folder_id[MEDIA_SVC_UUID_SIZE+1] = {0,};
699         sqlite3_stmt *sql_stmt = NULL;
700
701         if (path == NULL) {
702                 sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE storage_uuid = '%q' AND scan_status!=%d", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, FOLDER_SCAN_DONE);
703                 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
704
705                 if (ret != MS_MEDIA_ERR_NONE) {
706                         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
707                                 media_svc_debug("there is no folder.");
708                         } else {
709                                 media_svc_error("error when get folder_id by path. err = [%d]", ret);
710                         }
711
712                         *folder_list = NULL;
713                         *count = 0;
714
715                         return ret;
716                 }
717
718                 cnt = sqlite3_column_int(sql_stmt, 0);
719                 SQLITE3_FINALIZE(sql_stmt);
720                 if (cnt > 0) {
721                         sql = sqlite3_mprintf("SELECT path FROM '%s' WHERE storage_uuid = '%q' AND scan_status!=%d", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, FOLDER_SCAN_DONE);
722                 } else {
723                         *folder_list = NULL;
724                         *count = 0;
725
726                         return MS_MEDIA_ERR_INTERNAL;
727                 }
728
729                 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
730
731                 if (ret != MS_MEDIA_ERR_NONE) {
732                         media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
733
734                         *folder_list = NULL;
735                         *count = 0;
736
737                         return ret;
738                 }
739         } else {
740                 sql = sqlite3_mprintf("SELECT folder_uuid FROM '%s' WHERE storage_uuid = '%q' AND path = '%q'", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
741                 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
742
743                 if (ret != MS_MEDIA_ERR_NONE) {
744                         if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
745                                 media_svc_debug("there is no folder.");
746                         } else {
747                                 media_svc_error("error when get folder_id by path [%s]. err = [%d]", path, ret);
748                         }
749                         return ret;
750                 }
751
752                 _strncpy_safe(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE+1);
753
754                 SQLITE3_FINALIZE(sql_stmt);
755
756                 sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE path LIKE '%q%%' AND parent_folder_uuid = '%q'", MEDIA_SVC_DB_TABLE_FOLDER, path, folder_id);
757
758                 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
759
760                 if (ret != MS_MEDIA_ERR_NONE) {
761                         media_svc_error("error when _media_svc_sql_prepare_to_step. err = [%d]", ret);
762                         return ret;
763                 }
764
765                 cnt = sqlite3_column_int(sql_stmt, 0);
766                 SQLITE3_FINALIZE(sql_stmt);
767                 if (cnt > 0) {
768                         sql = sqlite3_mprintf("SELECT path FROM '%s' WHERE path LIKE '%q%%' AND parent_folder_uuid = '%q'", MEDIA_SVC_DB_TABLE_FOLDER, path, folder_id);
769                 } else {
770                         *folder_list = NULL;
771                         *count = 0;
772
773                         return MS_MEDIA_ERR_NONE;
774                 }
775
776                 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
777
778                 if (ret != MS_MEDIA_ERR_NONE) {
779                         media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
780
781                         *folder_list = NULL;
782                         *count = 0;
783
784                         return ret;
785                 }
786         }
787
788         *folder_list = malloc(sizeof(char *) * cnt);
789
790         while (1) {
791                 (*folder_list)[idx] = strdup((char *)sqlite3_column_text(sql_stmt, 0));
792
793                 if (sqlite3_step(sql_stmt) != SQLITE_ROW)
794                         break;
795                 idx++;
796         }
797
798         if (cnt == idx + 1) {
799                 *count = cnt;
800                 media_svc_debug("OK");
801         } else {
802                 /* free all data */
803                 int i = 0;
804                 for (i  = 0; i < idx; i++) {
805                         SAFE_FREE((*folder_list)[i]);
806                 }
807                 SAFE_FREE(*folder_list);
808                 *count = 0;
809                 ret = MS_MEDIA_ERR_INTERNAL;
810         }
811
812         SQLITE3_FINALIZE(sql_stmt);
813
814         return ret;
815 }
816
817 int _media_svc_delete_invalid_folder_by_path(sqlite3 *handle, const char *storage_id, const char *folder_path, uid_t uid, int *delete_count)
818 {
819         int ret = MS_MEDIA_ERR_NONE;
820         char *sql = NULL;
821         int del_count = 0;
822         sqlite3_stmt *sql_stmt = NULL;
823
824         if (folder_path == NULL)
825                 return MS_MEDIA_ERR_INVALID_PARAMETER;
826
827         /*check the number of the deleted folder*/
828         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);
829
830         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
831         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
832
833         del_count = sqlite3_column_int(sql_stmt, 0);
834
835         SQLITE3_FINALIZE(sql_stmt);
836         sql = NULL;
837
838         /*delete invalid folder*/
839         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);
840         ret = _media_svc_sql_query(sql, uid);
841
842         sqlite3_free(sql);
843
844         *delete_count = del_count;
845
846         return ret;
847 }
848
849 int _media_svc_count_folder_with_path(sqlite3 *handle, const char *storage_id, const char *path, int *count)
850 {
851         int ret = MS_MEDIA_ERR_NONE;
852         sqlite3_stmt *sql_stmt = NULL;
853
854         char *sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE (storage_uuid='%q' AND path='%q')", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
855
856         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
857
858         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
859
860         *count = sqlite3_column_int(sql_stmt, 0);
861
862         SQLITE3_FINALIZE(sql_stmt);
863
864         return MS_MEDIA_ERR_NONE;
865 }
866
867 int _media_svc_count_subfolder_with_path(sqlite3 *handle, const char *storage_id, const char *path, int *count)
868 {
869         int ret = MS_MEDIA_ERR_NONE;
870         sqlite3_stmt *sql_stmt = NULL;
871
872         char *sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE (storage_uuid='%q' AND path LIKE'%q/%%')", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
873
874         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
875
876         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
877
878         *count = sqlite3_column_int(sql_stmt, 0);
879
880         SQLITE3_FINALIZE(sql_stmt);
881
882         return MS_MEDIA_ERR_NONE;
883 }
884
885 int _media_svc_get_folder_uuid(sqlite3 *handle, const char *storage_id, const char *path, char *folder_id)
886 {
887         int ret = MS_MEDIA_ERR_NONE;
888         sqlite3_stmt *sql_stmt = NULL;
889         char *sql = NULL;
890
891         sql = sqlite3_mprintf("SELECT folder_uuid FROM '%s' WHERE (storage_uuid='%q' AND path='%q')", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
892
893         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
894
895         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
896
897         _strncpy_safe(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE+1);
898
899         SQLITE3_FINALIZE(sql_stmt);
900
901         if (!STRING_VALID(folder_id)) {
902                 media_svc_error("Not found valid storage id [%s]", path);
903                 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
904         }
905
906         return ret;
907 }
908