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 char *internal_path = _media_svc_get_path(uid);
41 if ((STRING_VALID(internal_path) && (strcmp(folder_path, internal_path) == 0)) ||
42 strcmp(folder_path, MEDIA_ROOT_PATH_SDCARD) == 0 ||
43 (STRING_VALID(MEDIA_ROOT_PATH_CLOUD) && strcmp(folder_path, MEDIA_ROOT_PATH_CLOUD) == 0)) {
44 media_svc_debug("ROOT PATH [%s]", folder_path);
48 SAFE_FREE(internal_path);
49 return MS_MEDIA_ERR_NONE;
52 static int __media_svc_parent_is_ext_root_path(const char *folder_path, bool *is_root)
54 char *parent_folder_path = NULL;
56 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
60 parent_folder_path = g_path_get_dirname(folder_path);
62 if(!STRING_VALID(parent_folder_path)) {
63 media_svc_error("error : g_path_get_dirname falied");
64 SAFE_FREE(parent_folder_path);
65 return MS_MEDIA_ERR_OUT_OF_MEMORY;
68 if (STRING_VALID(MEDIA_ROOT_PATH_EXTERNAL) && (strcmp(parent_folder_path, MEDIA_ROOT_PATH_EXTERNAL) == 0)) {
69 media_svc_debug("parent folder is ROOT PATH [%s]", parent_folder_path);
73 SAFE_FREE(parent_folder_path);
75 return MS_MEDIA_ERR_NONE;
78 int _media_svc_get_folder_id_by_foldername(sqlite3 *handle, const char *storage_id, const char *folder_name, char *folder_id, uid_t uid)
80 int ret = MS_MEDIA_ERR_NONE;
81 sqlite3_stmt *sql_stmt = NULL;
83 char parent_folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0, };
84 char *temp_parent_uuid = NULL;
85 char *parent_folder_path = NULL;
87 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);
89 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
91 if (ret != MS_MEDIA_ERR_NONE) {
92 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
93 media_svc_debug("there is no folder.");
95 media_svc_error("error when _media_svc_get_folder_id_by_foldername. err = [%d]", ret);
100 memset(parent_folder_uuid, 0, sizeof(parent_folder_uuid));
102 _strncpy_safe(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE + 1);
103 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 1))) /*root path can be null*/
104 _strncpy_safe(parent_folder_uuid, (const char *)sqlite3_column_text(sql_stmt, 1), MEDIA_SVC_UUID_SIZE+1);
106 SQLITE3_FINALIZE(sql_stmt);
108 /*root path can be null*/
109 if(!STRING_VALID(parent_folder_uuid)) {
110 bool is_root = FALSE;
112 ret = __media_svc_is_root_path(folder_name, &is_root, uid);
114 return MS_MEDIA_ERR_NONE;
116 ret = __media_svc_parent_is_ext_root_path(folder_name, &is_root);
118 return MS_MEDIA_ERR_NONE;
121 /* Notice : Below code is the code only for inserting parent folder uuid when upgrade the media db version 3 to 4 */
122 /* Check parent folder uuid */
123 if(!STRING_VALID(parent_folder_uuid)) {
124 /* update parent_uuid */
125 media_svc_error("[No-Error] there is no parent folder uuid. PLEASE CHECK IT");
127 parent_folder_path = g_path_get_dirname(folder_name);
128 if(!STRING_VALID(parent_folder_path)) {
129 media_svc_error("error : g_path_get_dirname falied.");
130 return MS_MEDIA_ERR_OUT_OF_MEMORY;
133 if(STRING_VALID(storage_id))
134 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);
136 sql = sqlite3_mprintf("SELECT folder_uuid FROM '%s' WHERE path = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, parent_folder_path);
138 SAFE_FREE(parent_folder_path);
140 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
141 if (ret == MS_MEDIA_ERR_NONE) {
142 temp_parent_uuid = (char *)sqlite3_column_text(sql_stmt, 0);
143 if (temp_parent_uuid != NULL) {
144 _strncpy_safe(parent_folder_uuid, temp_parent_uuid, MEDIA_SVC_UUID_SIZE+1);
147 SQLITE3_FINALIZE(sql_stmt);
150 if(STRING_VALID(parent_folder_uuid)) {
151 if(STRING_VALID(storage_id))
152 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);
154 sql = sqlite3_mprintf("UPDATE %q SET parent_folder_uuid = '%q' WHERE path = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, parent_folder_uuid, folder_name);
155 ret = _media_svc_sql_query(handle, sql, uid);
158 media_svc_error("error when get parent folder uuid");
165 static int __media_svc_append_folder(sqlite3 *handle, const char *storage_id, media_svc_storage_type_e storage_type,
166 const char *folder_id, const char *folder_path, const char *parent_folder_uuid, bool stack_query, uid_t uid)
168 int ret = MS_MEDIA_ERR_NONE;
169 char *folder_name = NULL;
170 int folder_modified_date = 0;
172 folder_name = g_path_get_basename(folder_path);
173 folder_modified_date = _media_svc_get_file_time(folder_path);
175 /*Update Pinyin If Support Pinyin*/
176 char *folder_name_pinyin = NULL;
177 if (_media_svc_check_pinyin_support())
178 _media_svc_get_pinyin_str(folder_name, &folder_name_pinyin);
180 char *sql = sqlite3_mprintf("INSERT INTO %s (folder_uuid, path, name, storage_uuid, storage_type, modified_time, name_pinyin, parent_folder_uuid) \
181 values (%Q, %Q, %Q, %Q, '%d', '%d', %Q, %Q); ",
182 MEDIA_SVC_DB_TABLE_FOLDER, folder_id, folder_path, folder_name, storage_id, storage_type, folder_modified_date, folder_name_pinyin, parent_folder_uuid);
186 ret = _media_svc_sql_query(handle, sql, uid);
191 _media_svc_sql_query_add(&g_media_svc_insert_folder_query_list, &sql);
194 SAFE_FREE(folder_name);
195 SAFE_FREE(folder_name_pinyin);
200 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)
202 int ret = MS_MEDIA_ERR_NONE;
203 int modified_time = 0;
205 modified_time = _media_svc_get_file_time(folder_path);
207 char *sql = sqlite3_mprintf("UPDATE %s SET modified_time=%d WHERE folder_uuid=%Q;", MEDIA_SVC_DB_TABLE_FOLDER, modified_time, folder_uuid);
210 ret = _media_svc_sql_query(handle, sql, uid);
213 _media_svc_sql_query_add(&g_media_svc_move_item_query_list, &sql);
219 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)
221 int ret = MS_MEDIA_ERR_NONE;
222 unsigned int next_pos = 0;
224 char *dir_path = NULL;
225 const char *token = "/";
226 char *folder_uuid = NULL;
227 char parent_folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
228 bool folder_search_end = FALSE;
229 char *internal_path = NULL;
231 memset(parent_folder_uuid, 0, sizeof(parent_folder_uuid));
232 internal_path = _media_svc_get_path(uid);
234 if (STRING_VALID(internal_path) && (strncmp(path, internal_path, strlen(internal_path)) == 0))
235 next_pos = strlen(internal_path);
236 else if (strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0)
237 next_pos = strlen(MEDIA_ROOT_PATH_SDCARD);
238 else if (STRING_VALID(MEDIA_ROOT_PATH_CLOUD) && (strncmp(path, MEDIA_ROOT_PATH_CLOUD, strlen(MEDIA_ROOT_PATH_CLOUD)) == 0))
239 next_pos = strlen(MEDIA_ROOT_PATH_CLOUD);
240 else if (strncmp(path, MEDIA_ROOT_PATH_EXTERNAL, strlen(MEDIA_ROOT_PATH_EXTERNAL)) == 0)
241 next_pos = strlen(MEDIA_ROOT_PATH_EXTERNAL);
243 media_svc_error("Invalid Path");
244 SAFE_FREE(internal_path);
245 return MS_MEDIA_ERR_INTERNAL;
248 SAFE_FREE(internal_path);
250 while (!folder_search_end) {
251 next = strstr(path + next_pos, token);
253 next_pos = (next - path);
254 dir_path = strndup(path, next_pos);
257 dir_path = strndup(path, strlen(path));
258 folder_search_end = TRUE;
259 media_svc_error("[No-Error] End Path [%s]", dir_path);
262 if (STRING_VALID(MEDIA_ROOT_PATH_EXTERNAL) && (strcmp(dir_path, MEDIA_ROOT_PATH_EXTERNAL) == 0)) {
263 /*To avoid insert MEDIA_ROOT_PATH_EXTERNAL path*/
267 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, dir_path, parent_folder_uuid, uid);
268 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
269 folder_uuid = _media_info_generate_uuid();
270 if (folder_uuid == NULL) {
271 media_svc_error("Invalid UUID");
273 return MS_MEDIA_ERR_INTERNAL;
276 ret = __media_svc_append_folder(handle, storage_id, storage_type, folder_uuid, dir_path, parent_folder_uuid, FALSE, uid);
277 if (ret != MS_MEDIA_ERR_NONE) {
278 media_svc_error("__media_svc_append_folder is failed");
281 media_svc_error("[No-Error] New Appended folder path [%s], folder_uuid [%s], parent_folder_uuid [%s]", dir_path, folder_uuid, parent_folder_uuid);
282 _strncpy_safe(parent_folder_uuid, folder_uuid, MEDIA_SVC_UUID_SIZE + 1);
284 media_svc_error("EXIST dir path : %s\n", dir_path);
290 if(STRING_VALID(folder_uuid)) {
291 _strncpy_safe(folder_id, folder_uuid, MEDIA_SVC_UUID_SIZE + 1);
293 media_svc_error("Fail to get folder_uuid");
294 return MS_MEDIA_ERR_INTERNAL;
297 return MS_MEDIA_ERR_NONE;
300 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)
302 int ret = MS_MEDIA_ERR_NONE;
304 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, path, folder_id, uid);
306 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
307 ret = __media_svc_get_and_append_parent_folder(handle, storage_id, path, storage_type, folder_id, uid);
313 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)
315 char *dir_path = NULL;
316 int ret = MS_MEDIA_ERR_NONE;
318 dir_path = g_path_get_dirname(path);
320 ret = _media_svc_get_and_append_folder(handle, storage_id, dir_path, storage_type, folder_id, uid);
327 int _media_svc_update_folder_table(sqlite3 *handle, const char *storage_id, uid_t uid)
329 int ret = MS_MEDIA_ERR_NONE;
332 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'))",
333 MEDIA_SVC_DB_TABLE_FOLDER, MEDIA_SVC_DB_TABLE_FOLDER, storage_id);
335 ret = _media_svc_sql_query(handle, sql, uid);
341 static int __media_svc_count_all_folders(sqlite3 *handle, char *start_path, int *count)
343 int ret = MS_MEDIA_ERR_NONE;
344 sqlite3_stmt *sql_stmt = NULL;
345 char *sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE path LIKE '%q%%'", MEDIA_SVC_DB_TABLE_FOLDER, start_path);
347 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
348 if (ret != MS_MEDIA_ERR_NONE) {
349 media_svc_error("error when _media_svc_sql_prepare_to_step. err = [%d]", ret);
353 *count = sqlite3_column_int(sql_stmt, 0);
355 SQLITE3_FINALIZE(sql_stmt);
357 return MS_MEDIA_ERR_NONE;
360 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)
362 int ret = MS_MEDIA_ERR_NONE;
364 sqlite3_stmt *sql_stmt = NULL;
367 char **folder_uuid = NULL;
370 ret = __media_svc_count_all_folders(handle, start_path, &cnt);
371 if (ret != MS_MEDIA_ERR_NONE) {
372 media_svc_error("error when __media_svc_count_all_folders. err = [%d]", ret);
377 sql = sqlite3_mprintf("SELECT path, modified_time, folder_uuid FROM '%s' WHERE path LIKE '%q%%'", MEDIA_SVC_DB_TABLE_FOLDER, start_path);
380 *modified_time_list = NULL;
381 *item_num_list = NULL;
382 return MS_MEDIA_ERR_NONE;
385 *folder_list = malloc(sizeof(char *) * cnt);
386 *modified_time_list = malloc(sizeof(int) * cnt);
387 *item_num_list = malloc(sizeof(int) * cnt);
388 folder_uuid = malloc(sizeof(char *) * cnt);
390 if ((*folder_list == NULL) || (*modified_time_list == NULL) || (*item_num_list == NULL) || (folder_uuid == NULL)) {
391 media_svc_error("Out of memory");
394 memset(folder_uuid, 0x0, sizeof(char *) * cnt);
396 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
397 if (ret != MS_MEDIA_ERR_NONE) {
398 media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
402 media_svc_debug("QEURY OK");
405 (*folder_list)[idx] = g_strdup((char *)sqlite3_column_text(sql_stmt, 0));
406 (*modified_time_list)[idx] = (int)sqlite3_column_int(sql_stmt, 1);
408 /* get the folder's id */
409 folder_uuid[idx] = g_strdup((char *)sqlite3_column_text(sql_stmt, 2));
413 if (sqlite3_step(sql_stmt) != SQLITE_ROW)
416 SQLITE3_FINALIZE(sql_stmt);
418 /*get the numbder of item in the folder by using folder's id */
419 for (i = 0; i < idx; i++) {
420 if (STRING_VALID(folder_uuid[i])) {
421 sql = sqlite3_mprintf("SELECT COUNT(*) FROM %s WHERE (folder_uuid='%q' AND validity = 1)", MEDIA_SVC_DB_TABLE_MEDIA, folder_uuid[i]);
422 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
423 if (ret != MS_MEDIA_ERR_NONE) {
424 media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
428 (*item_num_list)[i] = (int)sqlite3_column_int(sql_stmt, 0);
430 SQLITE3_FINALIZE(sql_stmt);
432 media_svc_error("Invalid Folder Id");
438 media_svc_debug("Get Folder is OK");
440 media_svc_error("Fail to get folder");
441 ret = MS_MEDIA_ERR_INTERNAL;
446 for (i = 0; i < idx; i++) {
447 SAFE_FREE(folder_uuid[i]);
449 SAFE_FREE(folder_uuid);
456 for (i = 0; i < idx; i++) {
457 SAFE_FREE((*folder_list)[i]);
458 SAFE_FREE(folder_uuid[i]);
460 SAFE_FREE(*folder_list);
461 SAFE_FREE(*modified_time_list);
462 SAFE_FREE(*item_num_list);
463 SAFE_FREE(folder_uuid);
470 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)
472 int ret = MS_MEDIA_ERR_NONE;
473 sqlite3_stmt *sql_stmt = NULL;
475 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);
477 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
479 if (ret != MS_MEDIA_ERR_NONE) {
480 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
481 media_svc_debug("there is no folder.");
483 media_svc_error("error when _media_svc_get_folder_id_by_foldername. err = [%d]", ret);
488 _strncpy_safe(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE + 1);
489 *modified_time = (int)sqlite3_column_int(sql_stmt, 1);
491 SQLITE3_FINALIZE(sql_stmt);
496 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)
498 char *path_name = NULL;
499 int ret = MS_MEDIA_ERR_NONE;
502 path_name = strdup(path);
503 if (path_name == NULL) {
504 media_svc_error("out of memory");
505 return MS_MEDIA_ERR_OUT_OF_MEMORY;
508 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, path_name, folder_id, uid);
509 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
510 bool is_root = FALSE;
511 bool is_parent_root = FALSE;
513 ret = __media_svc_is_root_path(path_name, &is_root, uid);
514 ret = __media_svc_parent_is_ext_root_path(path_name, &is_parent_root);
516 char parent_folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0, };
517 memset(parent_folder_uuid, 0x00, sizeof(parent_folder_uuid));
519 if ((is_root == FALSE) && (is_parent_root == FALSE)) {
520 /*get parent folder id*/
521 char *parent_path_name = g_path_get_dirname(path_name);
523 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, parent_path_name, parent_folder_uuid, uid);
524 if (ret != MS_MEDIA_ERR_NONE) {
525 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
526 /*if No-Root directory scanning start before doing storage scanning, parent_folder_uuid can be NULL.
527 You can remove above logic if use below logic always. but I keep above code for the performance issue.
528 Most case (except No-Root directory scanning start before doing storage scanning) get parent_folder_uuid well*/
529 media_svc_error("[No-Error] There is no proper parent_folder_uuid. so try to make it");
530 ret = _media_svc_get_and_append_folder(handle, storage_id, path, storage_type, folder_id, uid);
532 media_svc_error("error when _media_svc_get_parent_folder_id_by_foldername. err = [%d]", ret);
535 SAFE_FREE(path_name);
536 SAFE_FREE(parent_path_name);
542 char *folder_uuid = _media_info_generate_uuid();
543 if (folder_uuid == NULL) {
544 media_svc_error("Invalid UUID");
545 SAFE_FREE(path_name);
546 return MS_MEDIA_ERR_INTERNAL;
549 ret = __media_svc_append_folder(handle, storage_id, storage_type, folder_uuid, path_name, parent_folder_uuid, stack_query, uid);
551 _strncpy_safe(folder_id, folder_uuid, MEDIA_SVC_UUID_SIZE+1);
554 sql = sqlite3_mprintf("UPDATE '%s' SET validity=1 WHERE storage_uuid = '%q' AND path = '%q'", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
558 ret = _media_svc_sql_query(handle, sql, uid);
563 _media_svc_sql_query_add(&g_media_svc_insert_folder_query_list, &sql);
567 SAFE_FREE(path_name);
572 int _media_svc_delete_invalid_folder(sqlite3 *handle, const char *storage_id, uid_t uid)
574 int ret = MS_MEDIA_ERR_NONE;
577 sql = sqlite3_mprintf("DELETE FROM '%s' WHERE storage_uuid = '%q' AND validity = 0", MEDIA_SVC_DB_TABLE_FOLDER, storage_id);
578 ret = _media_svc_sql_query(handle, sql, uid);
585 int _media_svc_set_folder_validity(sqlite3 *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
587 int ret = MS_MEDIA_ERR_NONE;
589 char start_path_id[MEDIA_SVC_UUID_SIZE+1] = {0,};
592 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, start_path, start_path_id, uid);
593 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_folder_id_by_foldername fail");
594 media_svc_retvm_if(!STRING_VALID(start_path_id), MS_MEDIA_ERR_INVALID_PARAMETER, "start_path_id is NULL");
596 sql = sqlite3_mprintf("UPDATE '%s' SET validity = %d WHERE storage_uuid = '%q' AND (parent_folder_uuid = '%q' OR folder_uuid ='%q')",
597 MEDIA_SVC_DB_TABLE_FOLDER, validity, storage_id, start_path_id, start_path_id);
599 sql = sqlite3_mprintf("UPDATE '%s' SET validity = %d WHERE storage_uuid = '%q' AND path = '%q'",
600 MEDIA_SVC_DB_TABLE_FOLDER, validity, storage_id, start_path);
603 ret = _media_svc_sql_query(handle, sql, uid);
610 int _media_svc_delete_folder_by_storage_id(sqlite3 *handle, const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
612 int ret = MS_MEDIA_ERR_NONE;
615 sql = sqlite3_mprintf("DELETE FROM '%s' WHERE storage_uuid = '%q' AND storage_type = %d", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, storage_type);
616 ret = _media_svc_sql_query(handle, sql, uid);
623 GList ** _media_svc_get_folder_list_ptr(void)
625 return &g_media_svc_insert_folder_query_list;
628 int _media_svc_delete_invalid_folder_by_path(sqlite3 *handle, const char *storage_id, const char *folder_path, uid_t uid, int *delete_count)
630 int ret = MS_MEDIA_ERR_NONE;
633 sqlite3_stmt *sql_stmt = NULL;
635 if (folder_path == NULL)
636 return MS_MEDIA_ERR_INVALID_PARAMETER;
638 /*check the number of the deleted folder*/
639 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);
641 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
642 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
644 del_count = sqlite3_column_int(sql_stmt, 0);
646 SQLITE3_FINALIZE(sql_stmt);
649 /*delete invalid folder*/
650 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);
651 ret = _media_svc_sql_query(handle, sql, uid);
655 *delete_count = del_count;
660 int _media_svc_count_folder_with_path(sqlite3 *handle, const char *storage_id, const char *path, int *count)
662 int ret = MS_MEDIA_ERR_NONE;
663 sqlite3_stmt *sql_stmt = NULL;
665 char *sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE (storage_uuid='%q' AND path='%q')", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
667 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
669 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
671 *count = sqlite3_column_int(sql_stmt, 0);
673 SQLITE3_FINALIZE(sql_stmt);
675 return MS_MEDIA_ERR_NONE;
678 int _media_svc_count_subfolder_with_path(sqlite3 *handle, const char *storage_id, const char *path, int *count)
680 int ret = MS_MEDIA_ERR_NONE;
681 sqlite3_stmt *sql_stmt = NULL;
683 char *sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE (storage_uuid='%q' AND path LIKE'%q/%%')", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
685 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
687 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
689 *count = sqlite3_column_int(sql_stmt, 0);
691 SQLITE3_FINALIZE(sql_stmt);
693 return MS_MEDIA_ERR_NONE;
696 int _media_svc_get_folder_uuid(sqlite3 *handle, const char *storage_id, const char *path, char *folder_id)
698 int ret = MS_MEDIA_ERR_NONE;
699 sqlite3_stmt *sql_stmt = NULL;
702 sql = sqlite3_mprintf("SELECT folder_uuid FROM '%s' WHERE (storage_uuid='%q' AND path='%q')", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
704 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
706 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
708 _strncpy_safe(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE+1);
710 SQLITE3_FINALIZE(sql_stmt);
712 if(!STRING_VALID(folder_id))
714 media_svc_error("Not found valid storage id [%s]", path);
715 ret = MS_MEDIA_ERR_INVALID_PARAMETER;