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 static int __media_svc_is_root_path(const char *folder_path, bool *is_root, uid_t uid)
37 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
41 char *internal_path = _media_svc_get_path(uid);
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);
50 SAFE_FREE(internal_path);
51 return MS_MEDIA_ERR_NONE;
54 static int __media_svc_parent_is_ext_root_path(const char *folder_path, bool *is_root)
56 char *parent_folder_path = NULL;
58 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
62 parent_folder_path = g_path_get_dirname(folder_path);
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;
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);
75 SAFE_FREE(parent_folder_path);
77 return MS_MEDIA_ERR_NONE;
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)
82 int ret = MS_MEDIA_ERR_NONE;
83 sqlite3_stmt *sql_stmt = NULL;
85 char parent_folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0, };
86 char *temp_parent_uuid = NULL;
87 char *parent_folder_path = NULL;
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);
91 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
93 if (ret != MS_MEDIA_ERR_NONE) {
94 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
95 media_svc_debug("there is no folder.");
97 media_svc_error("error when _media_svc_get_folder_id_by_foldername. err = [%d]", ret);
102 memset(parent_folder_uuid, 0, sizeof(parent_folder_uuid));
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);
108 SQLITE3_FINALIZE(sql_stmt);
110 /*root path can be null*/
111 if (!STRING_VALID(parent_folder_uuid)) {
112 bool is_root = FALSE;
114 ret = __media_svc_is_root_path(folder_name, &is_root, uid);
116 return MS_MEDIA_ERR_NONE;
118 ret = __media_svc_parent_is_ext_root_path(folder_name, &is_root);
120 return MS_MEDIA_ERR_NONE;
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");
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;
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);
138 sql = sqlite3_mprintf("SELECT folder_uuid FROM '%s' WHERE path = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, parent_folder_path);
140 SAFE_FREE(parent_folder_path);
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);
149 SQLITE3_FINALIZE(sql_stmt);
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);
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);
160 media_svc_error("error when get parent folder uuid");
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)
170 int ret = MS_MEDIA_ERR_NONE;
171 char *folder_name = NULL;
172 int folder_modified_date = 0;
174 folder_name = g_path_get_basename(folder_path);
175 folder_modified_date = _media_svc_get_file_time(folder_path);
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);
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);
187 ret = _media_svc_sql_query(sql, uid);
190 _media_svc_sql_query_add(&g_media_svc_insert_folder_query_list, &sql);
193 SAFE_FREE(folder_name);
194 SAFE_FREE(folder_name_pinyin);
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)
201 int ret = MS_MEDIA_ERR_NONE;
202 int modified_time = 0;
204 modified_time = _media_svc_get_file_time(folder_path);
206 char *sql = sqlite3_mprintf("UPDATE %s SET modified_time=%d WHERE folder_uuid=%Q;", MEDIA_SVC_DB_TABLE_FOLDER, modified_time, folder_uuid);
209 ret = _media_svc_sql_query(sql, uid);
212 _media_svc_sql_query_add(&g_media_svc_move_item_query_list, &sql);
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)
220 int ret = MS_MEDIA_ERR_NONE;
221 unsigned int next_pos = 0;
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;
230 memset(parent_folder_uuid, 0, sizeof(parent_folder_uuid));
231 internal_path = _media_svc_get_path(uid);
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);
242 media_svc_error("Invalid 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) && (strcmp(dir_path, MEDIA_ROOT_PATH_EXTERNAL) == 0)) {
262 /*To avoid insert MEDIA_ROOT_PATH_EXTERNAL path*/
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");
272 return MS_MEDIA_ERR_INTERNAL;
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");
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);
283 media_svc_error("EXIST dir path : %s\n", dir_path);
289 if (STRING_VALID(folder_uuid)) {
290 _strncpy_safe(folder_id, folder_uuid, MEDIA_SVC_UUID_SIZE + 1);
292 media_svc_error("Fail to get folder_uuid");
293 return MS_MEDIA_ERR_INTERNAL;
296 return MS_MEDIA_ERR_NONE;
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)
301 int ret = MS_MEDIA_ERR_NONE;
303 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, path, folder_id, uid);
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);
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)
314 char *dir_path = NULL;
315 int ret = MS_MEDIA_ERR_NONE;
317 dir_path = g_path_get_dirname(path);
319 ret = _media_svc_get_and_append_folder(handle, storage_id, dir_path, storage_type, folder_id, uid);
326 int _media_svc_update_folder_table(const char *storage_id, uid_t uid)
328 int ret = MS_MEDIA_ERR_NONE;
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);
334 ret = _media_svc_sql_query(sql, uid);
340 static int __media_svc_count_all_folders(sqlite3 *handle, char *start_path, int *count)
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);
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);
352 *count = sqlite3_column_int(sql_stmt, 0);
354 SQLITE3_FINALIZE(sql_stmt);
356 return MS_MEDIA_ERR_NONE;
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)
361 int ret = MS_MEDIA_ERR_NONE;
363 sqlite3_stmt *sql_stmt = NULL;
366 char **folder_uuid = NULL;
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);
376 sql = sqlite3_mprintf("SELECT path, modified_time, folder_uuid FROM '%s' WHERE path LIKE '%q%%'", MEDIA_SVC_DB_TABLE_FOLDER, start_path);
379 *modified_time_list = NULL;
380 *item_num_list = NULL;
381 return MS_MEDIA_ERR_NONE;
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);
389 if ((*folder_list == NULL) || (*modified_time_list == NULL) || (*item_num_list == NULL) || (folder_uuid == NULL)) {
390 media_svc_error("Out of memory");
393 memset(folder_uuid, 0x0, sizeof(char *) * cnt);
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));
401 media_svc_debug("QEURY OK");
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);
407 /* get the folder's id */
408 folder_uuid[idx] = g_strdup((char *)sqlite3_column_text(sql_stmt, 2));
412 if (sqlite3_step(sql_stmt) != SQLITE_ROW)
415 SQLITE3_FINALIZE(sql_stmt);
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));
427 (*item_num_list)[i] = (int)sqlite3_column_int(sql_stmt, 0);
429 SQLITE3_FINALIZE(sql_stmt);
431 media_svc_error("Invalid Folder Id");
437 media_svc_debug("Get Folder is OK");
439 media_svc_error("Fail to get folder");
440 ret = MS_MEDIA_ERR_INTERNAL;
445 for (i = 0; i < idx; i++) {
446 SAFE_FREE(folder_uuid[i]);
448 SAFE_FREE(folder_uuid);
455 for (i = 0; i < idx; i++) {
456 SAFE_FREE((*folder_list)[i]);
457 SAFE_FREE(folder_uuid[i]);
459 SAFE_FREE(*folder_list);
460 SAFE_FREE(*modified_time_list);
461 SAFE_FREE(*item_num_list);
462 SAFE_FREE(folder_uuid);
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)
471 int ret = MS_MEDIA_ERR_NONE;
472 sqlite3_stmt *sql_stmt = NULL;
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);
476 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
478 if (ret != MS_MEDIA_ERR_NONE) {
479 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
480 media_svc_debug("there is no folder.");
482 media_svc_error("error when _media_svc_get_folder_id_by_foldername. err = [%d]", ret);
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);
490 SQLITE3_FINALIZE(sql_stmt);
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)
497 char *path_name = NULL;
498 int ret = MS_MEDIA_ERR_NONE;
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;
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;
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);
515 char parent_folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0, };
516 memset(parent_folder_uuid, 0x00, sizeof(parent_folder_uuid));
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);
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);
531 media_svc_error("error when _media_svc_get_parent_folder_id_by_foldername. err = [%d]", ret);
534 SAFE_FREE(path_name);
535 SAFE_FREE(parent_path_name);
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;
548 ret = __media_svc_append_folder(storage_id, storage_type, folder_uuid, path_name, parent_folder_uuid, stack_query, uid);
550 _strncpy_safe(folder_id, folder_uuid, MEDIA_SVC_UUID_SIZE+1);
553 sql = sqlite3_mprintf("UPDATE '%s' SET validity=1 WHERE storage_uuid = '%q' AND path = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
556 ret = _media_svc_sql_query(sql, uid);
559 _media_svc_sql_query_add(&g_media_svc_insert_folder_query_list, &sql);
563 SAFE_FREE(path_name);
568 int _media_svc_delete_invalid_folder(const char *storage_id, uid_t uid)
570 int ret = MS_MEDIA_ERR_NONE;
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);
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)
583 int ret = MS_MEDIA_ERR_NONE;
585 char start_path_id[MEDIA_SVC_UUID_SIZE+1] = {0,};
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");
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);
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);
599 ret = _media_svc_sql_query(sql, uid);
606 int _media_svc_delete_folder_by_storage_id(const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
608 int ret = MS_MEDIA_ERR_NONE;
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);
619 GList **_media_svc_get_folder_list_ptr(void)
621 return &g_media_svc_insert_folder_query_list;
624 int _media_svc_get_folder_scan_status(sqlite3 *handle, const char *storage_id, const char *path, int *scan_status)
626 int ret = MS_MEDIA_ERR_NONE;
627 sqlite3_stmt *sql_stmt = NULL;
630 if (!STRING_VALID(path)) {
631 media_svc_error("Not found valid path");
632 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
635 sql = sqlite3_mprintf("SELECT scan_status FROM '%s' WHERE path = '%q' AND storage_uuid = '%q'", MEDIA_SVC_DB_TABLE_FOLDER, path, storage_id);
637 ret = _media_svc_sql_prepare_to_step_simple(handle, sql, &sql_stmt);
639 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
641 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
642 *scan_status = sqlite3_column_int(sql_stmt, 0);
645 SQLITE3_FINALIZE(sql_stmt);
650 int _media_svc_set_folder_scan_status(const char *storage_id, const char *path, int scan_status, uid_t uid)
652 int ret = MS_MEDIA_ERR_NONE;
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);
658 sql = sqlite3_mprintf("UPDATE '%s' SET scan_status=%d WHERE storage_uuid = '%q'", MEDIA_SVC_DB_TABLE_FOLDER, scan_status, storage_id);
661 ret = _media_svc_sql_query(sql, uid);
667 int _media_svc_get_folder_modified_time_by_path(sqlite3 *handle, const char *path, const char *storage_id, time_t *modified_time)
669 int ret = MS_MEDIA_ERR_NONE;
670 sqlite3_stmt *sql_stmt = NULL;
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);
674 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
676 if (ret != MS_MEDIA_ERR_NONE) {
677 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
678 media_svc_debug("there is no folder.");
680 media_svc_error("error when _media_svc_get_folder_modified_time_by_path. err = [%d]", ret);
685 *modified_time = (int)sqlite3_column_int(sql_stmt, 0);
687 SQLITE3_FINALIZE(sql_stmt);
692 int _media_svc_get_null_scan_folder_list(sqlite3 *handle, char *storage_id, char *path, char ***folder_list, int *count)
694 int ret = MS_MEDIA_ERR_NONE;
698 char folder_id[MEDIA_SVC_UUID_SIZE+1] = {0,};
699 sqlite3_stmt *sql_stmt = 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);
705 if (ret != MS_MEDIA_ERR_NONE) {
706 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
707 media_svc_debug("there is no folder.");
709 media_svc_error("error when get folder_id by path. err = [%d]", ret);
718 cnt = sqlite3_column_int(sql_stmt, 0);
719 SQLITE3_FINALIZE(sql_stmt);
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);
726 return MS_MEDIA_ERR_INTERNAL;
729 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
731 if (ret != MS_MEDIA_ERR_NONE) {
732 media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
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);
743 if (ret != MS_MEDIA_ERR_NONE) {
744 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
745 media_svc_debug("there is no folder.");
747 media_svc_error("error when get folder_id by path [%s]. err = [%d]", path, ret);
752 _strncpy_safe(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE+1);
754 SQLITE3_FINALIZE(sql_stmt);
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);
758 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
760 if (ret != MS_MEDIA_ERR_NONE) {
761 media_svc_error("error when _media_svc_sql_prepare_to_step. err = [%d]", ret);
765 cnt = sqlite3_column_int(sql_stmt, 0);
766 SQLITE3_FINALIZE(sql_stmt);
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);
773 return MS_MEDIA_ERR_NONE;
776 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
778 if (ret != MS_MEDIA_ERR_NONE) {
779 media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
788 *folder_list = malloc(sizeof(char *) * cnt);
791 (*folder_list)[idx] = strdup((char *)sqlite3_column_text(sql_stmt, 0));
793 if (sqlite3_step(sql_stmt) != SQLITE_ROW)
798 if (cnt == idx + 1) {
800 media_svc_debug("OK");
804 for (i = 0; i < idx; i++) {
805 SAFE_FREE((*folder_list)[i]);
807 SAFE_FREE(*folder_list);
809 ret = MS_MEDIA_ERR_INTERNAL;
812 SQLITE3_FINALIZE(sql_stmt);
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)
819 int ret = MS_MEDIA_ERR_NONE;
822 sqlite3_stmt *sql_stmt = NULL;
824 if (folder_path == NULL)
825 return MS_MEDIA_ERR_INVALID_PARAMETER;
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);
830 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
831 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
833 del_count = sqlite3_column_int(sql_stmt, 0);
835 SQLITE3_FINALIZE(sql_stmt);
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);
844 *delete_count = del_count;
849 int _media_svc_count_folder_with_path(sqlite3 *handle, const char *storage_id, const char *path, int *count)
851 int ret = MS_MEDIA_ERR_NONE;
852 sqlite3_stmt *sql_stmt = NULL;
854 char *sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE (storage_uuid='%q' AND path='%q')", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
856 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
858 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
860 *count = sqlite3_column_int(sql_stmt, 0);
862 SQLITE3_FINALIZE(sql_stmt);
864 return MS_MEDIA_ERR_NONE;
867 int _media_svc_count_subfolder_with_path(sqlite3 *handle, const char *storage_id, const char *path, int *count)
869 int ret = MS_MEDIA_ERR_NONE;
870 sqlite3_stmt *sql_stmt = NULL;
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);
874 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
876 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
878 *count = sqlite3_column_int(sql_stmt, 0);
880 SQLITE3_FINALIZE(sql_stmt);
882 return MS_MEDIA_ERR_NONE;
885 int _media_svc_get_folder_uuid(sqlite3 *handle, const char *storage_id, const char *path, char *folder_id)
887 int ret = MS_MEDIA_ERR_NONE;
888 sqlite3_stmt *sql_stmt = NULL;
891 sql = sqlite3_mprintf("SELECT folder_uuid FROM '%s' WHERE (storage_uuid='%q' AND path='%q')", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
893 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
895 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
897 _strncpy_safe(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE+1);
899 SQLITE3_FINALIZE(sql_stmt);
901 if (!STRING_VALID(folder_id)) {
902 media_svc_error("Not found valid storage id [%s]", path);
903 ret = MS_MEDIA_ERR_INVALID_PARAMETER;