4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.kim@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
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"
30 extern __thread GList *g_media_svc_move_item_query_list;
31 static __thread GList *g_media_svc_insert_folder_query_list;
33 static int __media_svc_is_root_path(const char *folder_path, bool *is_root, uid_t uid)
35 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
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);
46 return MS_MEDIA_ERR_NONE;
49 static int __media_svc_parent_is_ext_root_path(const char *folder_path, bool *is_root)
51 char *parent_folder_path = NULL;
53 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
57 parent_folder_path = g_path_get_dirname(folder_path);
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;
65 if (strcmp(parent_folder_path, MEDIA_ROOT_PATH_EXTERNAL) == 0) {
66 media_svc_debug("parent folder is ROOT PATH [%s]", parent_folder_path);
70 SAFE_FREE(parent_folder_path);
72 return MS_MEDIA_ERR_NONE;
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)
77 int ret = MS_MEDIA_ERR_NONE;
78 sqlite3_stmt *sql_stmt = NULL;
80 char parent_folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0, };
81 char *temp_parent_uuid = NULL;
82 char *parent_folder_path = NULL;
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);
86 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
88 if (ret != MS_MEDIA_ERR_NONE) {
89 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
90 media_svc_debug("there is no folder.");
92 media_svc_error("error when _media_svc_get_folder_id_by_foldername. err = [%d]", ret);
97 memset(parent_folder_uuid, 0, sizeof(parent_folder_uuid));
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);
103 SQLITE3_FINALIZE(sql_stmt);
105 /*root path can be null*/
106 if(!STRING_VALID(parent_folder_uuid)) {
107 bool is_root = FALSE;
109 ret = __media_svc_is_root_path(folder_name, &is_root, uid);
111 return MS_MEDIA_ERR_NONE;
113 ret = __media_svc_parent_is_ext_root_path(folder_name, &is_root);
115 return MS_MEDIA_ERR_NONE;
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");
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;
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);
133 sql = sqlite3_mprintf("SELECT folder_uuid FROM '%s' WHERE path = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, parent_folder_path);
135 SAFE_FREE(parent_folder_path);
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);
144 SQLITE3_FINALIZE(sql_stmt);
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);
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);
155 media_svc_error("error when get parent folder uuid");
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)
165 int ret = MS_MEDIA_ERR_NONE;
166 char *folder_name = NULL;
167 int folder_modified_date = 0;
169 folder_name = g_path_get_basename(folder_path);
170 folder_modified_date = _media_svc_get_file_time(folder_path);
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);
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);
183 ret = _media_svc_sql_query(handle, sql, uid);
188 _media_svc_sql_query_add(&g_media_svc_insert_folder_query_list, &sql);
191 SAFE_FREE(folder_name);
192 SAFE_FREE(folder_name_pinyin);
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)
199 int ret = MS_MEDIA_ERR_NONE;
200 int modified_time = 0;
202 modified_time = _media_svc_get_file_time(folder_path);
204 char *sql = sqlite3_mprintf("UPDATE %s SET modified_time=%d WHERE folder_uuid=%Q;", MEDIA_SVC_DB_TABLE_FOLDER, modified_time, folder_uuid);
207 ret = _media_svc_sql_query(handle, sql, uid);
210 _media_svc_sql_query_add(&g_media_svc_move_item_query_list, &sql);
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)
218 int ret = MS_MEDIA_ERR_NONE;
219 unsigned int next_pos = 0;
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;
227 memset(parent_folder_uuid, 0, sizeof(parent_folder_uuid));
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);
238 media_svc_error("Invalid Path");
239 return MS_MEDIA_ERR_INTERNAL;
242 while (!folder_search_end) {
243 next = strstr(path + next_pos, token);
245 next_pos = (next - path);
246 dir_path = strndup(path, next_pos);
249 dir_path = strndup(path, strlen(path));
250 folder_search_end = TRUE;
251 media_svc_error("[No-Error] End Path [%s]", dir_path);
254 if (strcmp(dir_path, MEDIA_ROOT_PATH_EXTERNAL) == 0) {
255 /*To avoid insert MEDIA_ROOT_PATH_EXTERNAL path*/
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");
265 return MS_MEDIA_ERR_INTERNAL;
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");
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);
276 media_svc_error("EXIST dir path : %s\n", dir_path);
282 if(STRING_VALID(folder_uuid)) {
283 _strncpy_safe(folder_id, folder_uuid, MEDIA_SVC_UUID_SIZE + 1);
285 media_svc_error("Fail to get folder_uuid");
286 return MS_MEDIA_ERR_INTERNAL;
289 return MS_MEDIA_ERR_NONE;
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)
294 int ret = MS_MEDIA_ERR_NONE;
296 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, path, folder_id, uid);
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);
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)
307 char *dir_path = NULL;
308 int ret = MS_MEDIA_ERR_NONE;
310 dir_path = g_path_get_dirname(path);
312 ret = _media_svc_get_and_append_folder(handle, storage_id, dir_path, storage_type, folder_id, uid);
319 int _media_svc_update_folder_table(sqlite3 *handle, const char *storage_id, uid_t uid)
321 int ret = MS_MEDIA_ERR_NONE;
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);
327 ret = _media_svc_sql_query(handle, sql, uid);
333 static int __media_svc_count_all_folders(sqlite3 *handle, char *start_path, int *count)
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);
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);
345 *count = sqlite3_column_int(sql_stmt, 0);
347 SQLITE3_FINALIZE(sql_stmt);
349 return MS_MEDIA_ERR_NONE;
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)
354 int ret = MS_MEDIA_ERR_NONE;
356 sqlite3_stmt *sql_stmt = NULL;
359 char **folder_uuid = NULL;
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);
369 sql = sqlite3_mprintf("SELECT path, modified_time, folder_uuid FROM '%s' WHERE path LIKE '%q%%'", MEDIA_SVC_DB_TABLE_FOLDER, start_path);
372 *modified_time_list = NULL;
373 *item_num_list = NULL;
374 return MS_MEDIA_ERR_NONE;
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);
382 if ((*folder_list == NULL) || (*modified_time_list == NULL) || (*item_num_list == NULL) || (folder_uuid == NULL)) {
383 media_svc_error("Out of memory");
386 memset(folder_uuid, 0x0, sizeof(char *) * cnt);
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));
394 media_svc_debug("QEURY OK");
397 if (STRING_VALID((char *)sqlite3_column_text(sql_stmt, 0)))
398 (*folder_list)[idx] = strdup((char *)sqlite3_column_text(sql_stmt, 0));
400 (*modified_time_list)[idx] = (int)sqlite3_column_int(sql_stmt, 1);
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));
408 if (sqlite3_step(sql_stmt) != SQLITE_ROW)
411 SQLITE3_FINALIZE(sql_stmt);
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));
423 (*item_num_list)[i] = (int)sqlite3_column_int(sql_stmt, 0);
425 SQLITE3_FINALIZE(sql_stmt);
427 media_svc_error("Invalid Folder Id");
433 media_svc_debug("Get Folder is OK");
435 media_svc_error("Fail to get folder");
436 ret = MS_MEDIA_ERR_INTERNAL;
441 for (i = 0; i < idx; i++) {
442 SAFE_FREE(folder_uuid[i]);
444 SAFE_FREE(folder_uuid);
451 for (i = 0; i < idx; i++) {
452 SAFE_FREE((*folder_list)[i]);
453 SAFE_FREE(folder_uuid[i]);
455 SAFE_FREE(*folder_list);
456 SAFE_FREE(*modified_time_list);
457 SAFE_FREE(*item_num_list);
458 SAFE_FREE(folder_uuid);
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)
467 int ret = MS_MEDIA_ERR_NONE;
468 sqlite3_stmt *sql_stmt = NULL;
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);
472 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
474 if (ret != MS_MEDIA_ERR_NONE) {
475 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
476 media_svc_debug("there is no folder.");
478 media_svc_error("error when _media_svc_get_folder_id_by_foldername. err = [%d]", ret);
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);
486 SQLITE3_FINALIZE(sql_stmt);
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)
493 char *path_name = NULL;
494 int ret = MS_MEDIA_ERR_NONE;
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;
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;
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);
511 char parent_folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0, };
512 memset(parent_folder_uuid, 0x00, sizeof(parent_folder_uuid));
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);
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);
527 media_svc_error("error when _media_svc_get_parent_folder_id_by_foldername. err = [%d]", ret);
530 SAFE_FREE(path_name);
531 SAFE_FREE(parent_path_name);
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;
544 ret = __media_svc_append_folder(handle, storage_id, storage_type, folder_uuid, path_name, parent_folder_uuid, stack_query, uid);
546 _strncpy_safe(folder_id, folder_uuid, MEDIA_SVC_UUID_SIZE+1);
549 sql = sqlite3_mprintf("UPDATE '%s' SET validity=1 WHERE storage_uuid = '%q' AND path = '%q'", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
553 ret = _media_svc_sql_query(handle, sql, uid);
558 _media_svc_sql_query_add(&g_media_svc_insert_folder_query_list, &sql);
562 SAFE_FREE(path_name);
567 int _media_svc_delete_invalid_folder(sqlite3 *handle, const char *storage_id, uid_t uid)
569 int ret = MS_MEDIA_ERR_NONE;
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);
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)
582 int ret = MS_MEDIA_ERR_NONE;
584 char start_path_id[MEDIA_SVC_UUID_SIZE+1] = {0,};
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");
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);
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);
598 ret = _media_svc_sql_query(handle, sql, uid);
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)
607 int ret = MS_MEDIA_ERR_NONE;
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);
618 GList ** _media_svc_get_folder_list_ptr(void)
620 return &g_media_svc_insert_folder_query_list;
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)
625 int ret = MS_MEDIA_ERR_NONE;
628 sqlite3_stmt *sql_stmt = NULL;
630 if (folder_path == NULL)
631 return MS_MEDIA_ERR_INVALID_PARAMETER;
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);
636 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
637 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
639 del_count = sqlite3_column_int(sql_stmt, 0);
641 SQLITE3_FINALIZE(sql_stmt);
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);
650 *delete_count = del_count;
655 int _media_svc_count_folder_with_path(sqlite3 *handle, const char *storage_id, const char *path, int *count)
657 int ret = MS_MEDIA_ERR_NONE;
658 sqlite3_stmt *sql_stmt = NULL;
660 char *sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE (storage_uuid='%q' AND path='%q')", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
662 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
664 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
666 *count = sqlite3_column_int(sql_stmt, 0);
668 SQLITE3_FINALIZE(sql_stmt);
670 return MS_MEDIA_ERR_NONE;
673 int _media_svc_count_subfolder_with_path(sqlite3 *handle, const char *storage_id, const char *path, int *count)
675 int ret = MS_MEDIA_ERR_NONE;
676 sqlite3_stmt *sql_stmt = NULL;
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);
680 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
682 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
684 *count = sqlite3_column_int(sql_stmt, 0);
686 SQLITE3_FINALIZE(sql_stmt);
688 return MS_MEDIA_ERR_NONE;
691 int _media_svc_get_folder_uuid(sqlite3 *handle, const char *storage_id, const char *path, char *folder_id)
693 int ret = MS_MEDIA_ERR_NONE;
694 sqlite3_stmt *sql_stmt = NULL;
697 sql = sqlite3_mprintf("SELECT folder_uuid FROM '%s' WHERE (storage_uuid='%q' AND path='%q')", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
699 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
701 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
703 _strncpy_safe(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE+1);
705 SQLITE3_FINALIZE(sql_stmt);
707 if(!STRING_VALID(folder_id))
709 media_svc_error("Not found valid storage id [%s]", path);
710 ret = MS_MEDIA_ERR_INVALID_PARAMETER;