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 #define FOLDER_SCAN_DONE 4
32 extern __thread GList *g_media_svc_move_item_query_list;
33 static __thread GList *g_media_svc_insert_folder_query_list;
35 int _media_svc_get_folder_id_by_foldername(sqlite3 *handle, const char *storage_id, const char *folder_name, char *folder_id, uid_t uid)
37 int ret = MS_MEDIA_ERR_NONE;
38 sqlite3_stmt *sql_stmt = NULL;
41 sql = sqlite3_mprintf("SELECT folder_uuid FROM '%s' WHERE storage_uuid = '%q' AND path = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, folder_name);
43 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
44 if (ret != MS_MEDIA_ERR_NONE) {
45 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
46 media_svc_debug("there is no folder.");
48 media_svc_error("error when _media_svc_get_folder_id_by_foldername. err = [%d]", ret);
53 SAFE_STRLCPY(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE + 1);
55 SQLITE3_FINALIZE(sql_stmt);
60 int _media_svc_get_folder_id_by_foldername_with_validity(sqlite3 *handle, const char *storage_id, const char *folder_name, char *folder_id, uid_t uid, int *validity)
62 int ret = MS_MEDIA_ERR_NONE;
63 sqlite3_stmt *sql_stmt = NULL;
66 sql = sqlite3_mprintf("SELECT folder_uuid, validity FROM '%s' WHERE storage_uuid = '%q' AND path = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, folder_name);
68 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
69 if (ret != MS_MEDIA_ERR_NONE) {
70 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
71 media_svc_debug("there is no folder.");
73 media_svc_error("error when _media_svc_get_folder_id_by_foldername_with_validity. err = [%d]", ret);
78 SAFE_STRLCPY(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE + 1);
79 *validity = (int)sqlite3_column_int(sql_stmt, 1);
81 SQLITE3_FINALIZE(sql_stmt);
86 static int __media_svc_append_folder(const char *storage_id, media_svc_storage_type_e storage_type,
87 const char *folder_id, const char *folder_path, bool stack_query, uid_t uid)
89 int ret = MS_MEDIA_ERR_NONE;
90 char *folder_name = NULL;
91 int folder_modified_date = 0;
93 folder_name = g_path_get_basename(folder_path);
94 folder_modified_date = _media_svc_get_file_time(folder_path);
96 /*Update Pinyin If Support Pinyin*/
97 char *folder_name_pinyin = NULL;
98 if (_media_svc_check_pinyin_support())
99 _media_svc_get_pinyin_str(folder_name, &folder_name_pinyin);
100 /* Sometime SQLITE3 returns NO_RECORD, so need to consider conflict case.. */
101 media_svc_debug("UNIQUE:path[%s], storage_uuid[%s]", folder_path, storage_id);
102 char *sql = sqlite3_mprintf("INSERT OR IGNORE INTO %s (folder_uuid, path, name, storage_uuid, storage_type, modified_time, name_pinyin) values (%Q, %Q, %Q, %Q, '%d', '%d', %Q); ",
103 MEDIA_SVC_DB_TABLE_FOLDER, folder_id, folder_path, folder_name, storage_id, storage_type, folder_modified_date, folder_name_pinyin);
106 ret = _media_svc_sql_query(sql, uid);
107 SQLITE3_SAFE_FREE(sql);
109 _media_svc_sql_query_add(&g_media_svc_insert_folder_query_list, &sql);
112 SAFE_FREE(folder_name);
113 SAFE_FREE(folder_name_pinyin);
118 int _media_svc_update_folder_modified_time_by_folder_uuid(const char *folder_uuid, const char *folder_path, bool stack_query, uid_t uid)
120 int ret = MS_MEDIA_ERR_NONE;
121 int modified_time = 0;
123 modified_time = _media_svc_get_file_time(folder_path);
125 char *sql = sqlite3_mprintf("UPDATE %s SET modified_time=%d WHERE folder_uuid=%Q;", MEDIA_SVC_DB_TABLE_FOLDER, modified_time, folder_uuid);
128 ret = _media_svc_sql_query(sql, uid);
129 SQLITE3_SAFE_FREE(sql);
131 _media_svc_sql_query_add(&g_media_svc_move_item_query_list, &sql);
137 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)
139 int ret = MS_MEDIA_ERR_NONE;
140 unsigned int next_pos = 0;
142 char *dir_path = NULL;
143 const char *token = "/";
144 char *folder_uuid = NULL;
145 char tmp_folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
146 bool folder_search_end = FALSE;
147 char *internal_path = NULL;
149 memset(tmp_folder_uuid, 0, sizeof(tmp_folder_uuid));
150 ret = ms_user_get_internal_root_path(uid, &internal_path);
151 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Fail to get root path");
153 if (STRING_VALID(internal_path) && (strncmp(path, internal_path, strlen(internal_path)) == 0))
154 next_pos = strlen(internal_path);
155 else if (STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0)
156 next_pos = strlen(MEDIA_ROOT_PATH_SDCARD);
157 else if (STRING_VALID(MEDIA_ROOT_PATH_EXTERNAL) && strncmp(path, MEDIA_ROOT_PATH_EXTERNAL, strlen(MEDIA_ROOT_PATH_EXTERNAL)) == 0)
158 next_pos = strlen(MEDIA_ROOT_PATH_EXTERNAL);
159 else if (STRING_VALID(MEDIA_ROOT_PATH_DISC) && strncmp(path, MEDIA_ROOT_PATH_DISC, strlen(MEDIA_ROOT_PATH_DISC)) == 0)
160 next_pos = strlen(MEDIA_ROOT_PATH_DISC);
162 media_svc_error("Invalid Path");
163 media_svc_sec_error("Invalid Path [%s], internal_path [%s]", path, internal_path);
164 SAFE_FREE(internal_path);
165 return MS_MEDIA_ERR_INTERNAL;
168 SAFE_FREE(internal_path);
170 while (!folder_search_end) {
171 next = strstr(path + next_pos, token);
173 next_pos = (next - path);
174 dir_path = strndup(path, next_pos);
177 dir_path = strndup(path, strlen(path));
178 folder_search_end = TRUE;
179 media_svc_error("[No-Error] End Path [%s]", dir_path);
182 if (STRING_VALID(MEDIA_ROOT_PATH_EXTERNAL) && (g_strcmp0(dir_path, MEDIA_ROOT_PATH_EXTERNAL) == 0)) {
183 /*To avoid insert MEDIA_ROOT_PATH_EXTERNAL path*/
188 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, dir_path, tmp_folder_uuid, uid);
189 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
190 folder_uuid = _media_info_generate_uuid();
191 if (folder_uuid == NULL) {
192 media_svc_error("Invalid UUID");
194 return MS_MEDIA_ERR_INTERNAL;
197 ret = __media_svc_append_folder(storage_id, storage_type, folder_uuid, dir_path, FALSE, uid);
198 if (ret != MS_MEDIA_ERR_NONE)
199 media_svc_error("__media_svc_append_folder is failed");
201 media_svc_error("[No-Error] New Appended folder path [%s], folder_uuid [%s]", dir_path, folder_uuid);
203 media_svc_error("EXIST dir path : %s\n", dir_path);
209 if (STRING_VALID(folder_uuid)) {
210 SAFE_STRLCPY(folder_id, folder_uuid, MEDIA_SVC_UUID_SIZE + 1);
212 media_svc_error("Fail to get folder_uuid");
213 return MS_MEDIA_ERR_INTERNAL;
216 return MS_MEDIA_ERR_NONE;
219 static int __media_svc_update_parent_folder_validity(sqlite3 *handle, const char *storage_id, const char *path, 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 bool folder_search_end = FALSE;
227 char *internal_path = NULL;
229 ret = ms_user_get_internal_root_path(uid, &internal_path);
230 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Fail to get root path");
232 if (STRING_VALID(internal_path) && (strncmp(path, internal_path, strlen(internal_path)) == 0))
233 next_pos = strlen(internal_path);
234 else if (STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0)
235 next_pos = strlen(MEDIA_ROOT_PATH_SDCARD);
236 else if (STRING_VALID(MEDIA_ROOT_PATH_EXTERNAL) && strncmp(path, MEDIA_ROOT_PATH_EXTERNAL, strlen(MEDIA_ROOT_PATH_EXTERNAL)) == 0)
237 next_pos = strlen(MEDIA_ROOT_PATH_EXTERNAL);
238 else if (STRING_VALID(MEDIA_ROOT_PATH_DISC) && strncmp(path, MEDIA_ROOT_PATH_DISC, strlen(MEDIA_ROOT_PATH_DISC)) == 0)
239 next_pos = strlen(MEDIA_ROOT_PATH_DISC);
241 media_svc_error("Invalid Path");
242 media_svc_sec_error("Invalid Path [%s], internal_path [%s]", path, internal_path);
243 SAFE_FREE(internal_path);
244 return MS_MEDIA_ERR_INTERNAL;
247 SAFE_FREE(internal_path);
249 while (!folder_search_end) {
250 next = strstr(path + next_pos, token);
252 next_pos = (next - path);
253 dir_path = strndup(path, next_pos);
256 dir_path = strndup(path, strlen(path));
257 folder_search_end = TRUE;
258 media_svc_error("[No-Error] End Path [%s]", dir_path);
261 if (STRING_VALID(MEDIA_ROOT_PATH_EXTERNAL) && (g_strcmp0(dir_path, MEDIA_ROOT_PATH_EXTERNAL) == 0)) {
262 /*To avoid insert MEDIA_ROOT_PATH_EXTERNAL path*/
267 ret = _media_svc_set_folder_validity(handle, storage_id, dir_path, 1, false, uid);
269 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Fail to set folder validity");
272 return MS_MEDIA_ERR_NONE;
275 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)
277 int ret = MS_MEDIA_ERR_NONE;
280 ret = _media_svc_get_folder_id_by_foldername_with_validity(handle, storage_id, path, folder_id, uid, &validity);
282 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
283 ret = __media_svc_get_and_append_parent_folder(handle, storage_id, path, storage_type, folder_id, uid);
284 else if (ret == MS_MEDIA_ERR_NONE && validity == 0)
285 ret = __media_svc_update_parent_folder_validity(handle, storage_id, path, uid);
290 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)
292 char *dir_path = NULL;
293 int ret = MS_MEDIA_ERR_NONE;
295 dir_path = g_path_get_dirname(path);
297 ret = _media_svc_get_and_append_folder(handle, storage_id, dir_path, storage_type, folder_id, uid);
304 int _media_svc_update_folder_table(const char *storage_id, uid_t uid)
306 int ret = MS_MEDIA_ERR_NONE;
309 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'));",
310 MEDIA_SVC_DB_TABLE_FOLDER, MEDIA_SVC_DB_TABLE_FOLDER, storage_id);
312 ret = _media_svc_sql_query(sql, uid);
313 SQLITE3_SAFE_FREE(sql);
318 static int __media_svc_count_all_folders(sqlite3 *handle, char *start_path, int *count)
320 int ret = MS_MEDIA_ERR_NONE;
321 sqlite3_stmt *sql_stmt = NULL;
322 char *sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE path LIKE '%q%%'", MEDIA_SVC_DB_TABLE_FOLDER, start_path);
324 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
325 if (ret != MS_MEDIA_ERR_NONE) {
326 media_svc_error("error when _media_svc_sql_prepare_to_step. err = [%d]", ret);
330 *count = sqlite3_column_int(sql_stmt, 0);
332 SQLITE3_FINALIZE(sql_stmt);
334 return MS_MEDIA_ERR_NONE;
337 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)
339 int ret = MS_MEDIA_ERR_NONE;
340 sqlite3_stmt *sql_stmt = NULL;
342 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);
344 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
346 if (ret != MS_MEDIA_ERR_NONE) {
347 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
348 media_svc_debug("there is no folder.");
350 media_svc_error("error when _media_svc_get_folder_id_by_foldername. err = [%d]", ret);
355 SAFE_STRLCPY(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE + 1);
356 *modified_time = (int)sqlite3_column_int(sql_stmt, 1);
358 SQLITE3_FINALIZE(sql_stmt);
363 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)
365 char *path_name = NULL;
366 int ret = MS_MEDIA_ERR_NONE;
369 path_name = strdup(path);
370 if (path_name == NULL) {
371 media_svc_error("out of memory");
372 return MS_MEDIA_ERR_OUT_OF_MEMORY;
375 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, path_name, folder_id, uid);
376 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
377 ret = __media_svc_get_and_append_parent_folder(handle, storage_id, path_name, storage_type, folder_id, uid);
379 sql = sqlite3_mprintf("UPDATE '%s' SET validity=1 WHERE storage_uuid = '%q' AND path = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
381 ret = _media_svc_sql_query(sql, uid);
382 SQLITE3_SAFE_FREE(sql);
384 _media_svc_sql_query_add(&g_media_svc_insert_folder_query_list, &sql);
388 SAFE_FREE(path_name);
393 int _media_svc_delete_invalid_folder(const char *storage_id, int storage_type, uid_t uid)
395 int ret = MS_MEDIA_ERR_NONE;
398 sql = sqlite3_mprintf("DELETE FROM '%s' WHERE storage_uuid = '%q' AND storage_type = %d AND validity = 0;", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, storage_type);
399 ret = _media_svc_sql_query(sql, uid);
401 SQLITE3_SAFE_FREE(sql);
406 int _media_svc_set_folder_validity(sqlite3 *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
408 int ret = MS_MEDIA_ERR_NONE;
410 char start_path_id[MEDIA_SVC_UUID_SIZE+1] = {0,};
413 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, start_path, start_path_id, uid);
414 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_folder_id_by_foldername fail");
415 media_svc_retvm_if(!STRING_VALID(start_path_id), MS_MEDIA_ERR_INVALID_PARAMETER, "start_path_id is NULL");
417 sql = sqlite3_mprintf("UPDATE '%s' SET validity = %d WHERE storage_uuid = '%q' AND (path LIKE '%q/%%' OR folder_uuid ='%q');",
418 MEDIA_SVC_DB_TABLE_FOLDER, validity, storage_id, start_path, start_path_id);
420 sql = sqlite3_mprintf("UPDATE '%s' SET validity = %d WHERE storage_uuid = '%q' AND path = '%q';",
421 MEDIA_SVC_DB_TABLE_FOLDER, validity, storage_id, start_path);
424 ret = _media_svc_sql_query(sql, uid);
426 SQLITE3_SAFE_FREE(sql);
431 int _media_svc_delete_folder_by_storage_id(const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
433 int ret = MS_MEDIA_ERR_NONE;
436 sql = sqlite3_mprintf("DELETE FROM '%s' WHERE storage_uuid = '%q' AND storage_type = %d;", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, storage_type);
437 ret = _media_svc_sql_query(sql, uid);
439 SQLITE3_SAFE_FREE(sql);
444 GList **_media_svc_get_folder_list_ptr(void)
446 return &g_media_svc_insert_folder_query_list;
449 int _media_svc_get_folder_scan_status(sqlite3 *handle, const char *storage_id, const char *path, int *scan_status)
451 int ret = MS_MEDIA_ERR_NONE;
452 sqlite3_stmt *sql_stmt = NULL;
455 if (!STRING_VALID(path)) {
456 media_svc_error("Not found valid path");
457 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
460 sql = sqlite3_mprintf("SELECT scan_status FROM '%s' WHERE path = '%q' AND storage_uuid = '%q'", MEDIA_SVC_DB_TABLE_FOLDER, path, storage_id);
462 ret = _media_svc_sql_prepare_to_step_simple(handle, sql, &sql_stmt);
464 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
466 while (sqlite3_step(sql_stmt) == SQLITE_ROW)
467 *scan_status = sqlite3_column_int(sql_stmt, 0);
469 SQLITE3_FINALIZE(sql_stmt);
474 int _media_svc_set_folder_scan_status(const char *storage_id, const char *path, int scan_status, uid_t uid)
476 int ret = MS_MEDIA_ERR_NONE;
480 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);
482 sql = sqlite3_mprintf("UPDATE '%s' SET scan_status=%d WHERE storage_uuid = '%q'", MEDIA_SVC_DB_TABLE_FOLDER, scan_status, storage_id);
484 ret = _media_svc_sql_query(sql, uid);
485 SQLITE3_SAFE_FREE(sql);
490 int _media_svc_get_folder_modified_time_by_path(sqlite3 *handle, const char *path, const char *storage_id, time_t *modified_time)
492 int ret = MS_MEDIA_ERR_NONE;
493 sqlite3_stmt *sql_stmt = NULL;
495 char *sql = sqlite3_mprintf("SELECT modified_time FROM '%s' WHERE path = '%q' AND storage_uuid = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, path, storage_id);
497 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
499 if (ret != MS_MEDIA_ERR_NONE) {
500 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
501 media_svc_debug("there is no folder.");
503 media_svc_error("error when _media_svc_get_folder_modified_time_by_path. err = [%d]", ret);
508 *modified_time = (int)sqlite3_column_int(sql_stmt, 0);
510 SQLITE3_FINALIZE(sql_stmt);
515 int _media_svc_get_null_scan_folder_list(sqlite3 *handle, const char *storage_id, const char *path, char ***folder_list, int *count)
517 int ret = MS_MEDIA_ERR_NONE;
521 sqlite3_stmt *sql_stmt = NULL;
524 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);
525 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
527 if (ret != MS_MEDIA_ERR_NONE) {
528 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
529 media_svc_debug("there is no folder.");
531 media_svc_error("error when get folder_id by path. err = [%d]", ret);
539 cnt = sqlite3_column_int(sql_stmt, 0);
540 SQLITE3_FINALIZE(sql_stmt);
542 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);
547 return MS_MEDIA_ERR_INTERNAL;
550 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
552 if (ret != MS_MEDIA_ERR_NONE) {
553 media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
561 sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE path IN (SELECT DISTINCT '%q/'||name from '%s' WHERE path LIKE '%q/%%')",
562 MEDIA_SVC_DB_TABLE_FOLDER, path, MEDIA_SVC_DB_TABLE_FOLDER, path);
563 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
565 if (ret != MS_MEDIA_ERR_NONE) {
566 media_svc_error("error when _media_svc_sql_prepare_to_step. err = [%d]", ret);
570 cnt = sqlite3_column_int(sql_stmt, 0);
571 SQLITE3_FINALIZE(sql_stmt);
573 sql = sqlite3_mprintf("SELECT path FROM '%s' WHERE path IN (SELECT DISTINCT '%q/'||name from '%s' WHERE path LIKE '%q/%%')",
574 MEDIA_SVC_DB_TABLE_FOLDER, path, MEDIA_SVC_DB_TABLE_FOLDER, path);
579 return MS_MEDIA_ERR_NONE;
582 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
584 if (ret != MS_MEDIA_ERR_NONE) {
585 media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
594 *folder_list = malloc(sizeof(char *) * cnt);
595 if (*folder_list == NULL) {
596 media_svc_error("Allocation failed");
601 return MS_MEDIA_ERR_OUT_OF_MEMORY;
605 (*folder_list)[idx] = strdup((char *)sqlite3_column_text(sql_stmt, 0));
607 if (sqlite3_step(sql_stmt) != SQLITE_ROW)
612 if (cnt == idx + 1) {
614 media_svc_debug("OK");
618 for (i = 0; i < idx; i++)
619 SAFE_FREE((*folder_list)[i]);
621 SAFE_FREE(*folder_list);
623 ret = MS_MEDIA_ERR_INTERNAL;
626 SQLITE3_FINALIZE(sql_stmt);
631 int _media_svc_count_folder_with_path(sqlite3 *handle, const char *storage_id, const char *path, int *count)
633 int ret = MS_MEDIA_ERR_NONE;
634 sqlite3_stmt *sql_stmt = NULL;
636 char *sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE (storage_uuid='%q' AND path='%q')", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
638 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
640 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
642 *count = sqlite3_column_int(sql_stmt, 0);
644 SQLITE3_FINALIZE(sql_stmt);
646 return MS_MEDIA_ERR_NONE;
649 int _media_svc_get_folder_uuid(sqlite3 *handle, const char *storage_id, const char *path, char *folder_id)
651 int ret = MS_MEDIA_ERR_NONE;
652 sqlite3_stmt *sql_stmt = NULL;
655 sql = sqlite3_mprintf("SELECT folder_uuid FROM '%s' WHERE (storage_uuid='%q' AND path='%q')", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
657 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
659 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
661 SAFE_STRLCPY(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE+1);
663 SQLITE3_FINALIZE(sql_stmt);
665 if (!STRING_VALID(folder_id)) {
666 media_svc_error("Not found valid storage id [%s]", path);
667 ret = MS_MEDIA_ERR_INVALID_PARAMETER;