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");
43 char *internal_path = _media_svc_get_path(uid);
45 if (STRING_VALID(internal_path) && (strcmp(folder_path, internal_path) == 0)) {
46 media_svc_debug("ROOT PATH [%s]", folder_path);
49 SAFE_FREE(internal_path);
51 return MS_MEDIA_ERR_NONE;
53 SAFE_FREE(internal_path);
55 if ((STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && strncmp(folder_path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0) ||
56 (STRING_VALID(MEDIA_ROOT_PATH_CLOUD) && strncmp(folder_path, MEDIA_ROOT_PATH_CLOUD, strlen(MEDIA_ROOT_PATH_CLOUD)) == 0)) {
58 start_len = strlen(MEDIA_ROOT_PATH_SDCARD);
59 path_len = strlen(folder_path);
61 for (i = start_len; i < path_len; i++) {
62 if (folder_path[i] == '/')
63 return MS_MEDIA_ERR_NONE;
66 media_svc_debug("ROOT PATH [%s]", folder_path);
70 return MS_MEDIA_ERR_NONE;
73 static int __media_svc_parent_is_ext_root_path(const char *folder_path, bool *is_root)
75 char *parent_folder_path = NULL;
77 media_svc_retvm_if(!STRING_VALID(folder_path), MS_MEDIA_ERR_INVALID_PARAMETER, "folder_path is NULL");
81 parent_folder_path = g_path_get_dirname(folder_path);
83 if (!STRING_VALID(parent_folder_path)) {
84 media_svc_error("error : g_path_get_dirname falied");
85 SAFE_FREE(parent_folder_path);
86 return MS_MEDIA_ERR_OUT_OF_MEMORY;
89 if (STRING_VALID(MEDIA_ROOT_PATH_EXTERNAL) && (strcmp(parent_folder_path, MEDIA_ROOT_PATH_EXTERNAL) == 0)) {
90 media_svc_debug("parent folder is ROOT PATH [%s]", parent_folder_path);
94 SAFE_FREE(parent_folder_path);
96 return MS_MEDIA_ERR_NONE;
99 int _media_svc_get_folder_id_by_foldername(sqlite3 *handle, const char *storage_id, const char *folder_name, char *folder_id, uid_t uid)
101 int ret = MS_MEDIA_ERR_NONE;
102 sqlite3_stmt *sql_stmt = NULL;
104 char parent_folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0, };
105 char *temp_parent_uuid = NULL;
106 char *parent_folder_path = NULL;
108 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);
110 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
112 if (ret != MS_MEDIA_ERR_NONE) {
113 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
114 media_svc_debug("there is no folder.");
116 media_svc_error("error when _media_svc_get_folder_id_by_foldername. err = [%d]", ret);
121 memset(parent_folder_uuid, 0, sizeof(parent_folder_uuid));
123 _strncpy_safe(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE + 1);
124 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 1))) /*root path can be null*/
125 _strncpy_safe(parent_folder_uuid, (const char *)sqlite3_column_text(sql_stmt, 1), MEDIA_SVC_UUID_SIZE+1);
127 SQLITE3_FINALIZE(sql_stmt);
129 /*root path can be null*/
130 if (!STRING_VALID(parent_folder_uuid)) {
131 bool is_root = FALSE;
133 ret = __media_svc_is_root_path(folder_name, &is_root, uid);
135 return MS_MEDIA_ERR_NONE;
137 ret = __media_svc_parent_is_ext_root_path(folder_name, &is_root);
139 return MS_MEDIA_ERR_NONE;
142 /* Notice : Below code is the code only for inserting parent folder uuid when upgrade the media db version 3 to 4 */
143 /* Check parent folder uuid */
144 if (!STRING_VALID(parent_folder_uuid)) {
145 /* update parent_uuid */
146 media_svc_error("[No-Error] there is no parent folder uuid. PLEASE CHECK IT");
148 parent_folder_path = g_path_get_dirname(folder_name);
149 if (!STRING_VALID(parent_folder_path)) {
150 media_svc_error("error : g_path_get_dirname falied.");
151 return MS_MEDIA_ERR_OUT_OF_MEMORY;
154 if (STRING_VALID(storage_id))
155 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);
157 sql = sqlite3_mprintf("SELECT folder_uuid FROM '%s' WHERE path = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, parent_folder_path);
159 SAFE_FREE(parent_folder_path);
161 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
162 if (ret == MS_MEDIA_ERR_NONE) {
163 temp_parent_uuid = (char *)sqlite3_column_text(sql_stmt, 0);
164 if (temp_parent_uuid != NULL) {
165 _strncpy_safe(parent_folder_uuid, temp_parent_uuid, MEDIA_SVC_UUID_SIZE+1);
168 SQLITE3_FINALIZE(sql_stmt);
171 if (STRING_VALID(parent_folder_uuid)) {
172 if (STRING_VALID(storage_id))
173 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);
175 sql = sqlite3_mprintf("UPDATE %q SET parent_folder_uuid = '%q' WHERE path = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, parent_folder_uuid, folder_name);
176 ret = _media_svc_sql_query(sql, uid);
179 media_svc_error("error when get parent folder uuid");
186 static int __media_svc_append_folder(const char *storage_id, media_svc_storage_type_e storage_type,
187 const char *folder_id, const char *folder_path, const char *parent_folder_uuid, bool stack_query, uid_t uid)
189 int ret = MS_MEDIA_ERR_NONE;
190 char *folder_name = NULL;
191 int folder_modified_date = 0;
193 folder_name = g_path_get_basename(folder_path);
194 folder_modified_date = _media_svc_get_file_time(folder_path);
196 /*Update Pinyin If Support Pinyin*/
197 char *folder_name_pinyin = NULL;
198 if (_media_svc_check_pinyin_support())
199 _media_svc_get_pinyin_str(folder_name, &folder_name_pinyin);
201 char *sql = sqlite3_mprintf("INSERT INTO %s (folder_uuid, path, name, storage_uuid, storage_type, modified_time, name_pinyin, parent_folder_uuid) \
202 values (%Q, %Q, %Q, %Q, '%d', '%d', %Q, %Q); ",
203 MEDIA_SVC_DB_TABLE_FOLDER, folder_id, folder_path, folder_name, storage_id, storage_type, folder_modified_date, folder_name_pinyin, parent_folder_uuid);
206 ret = _media_svc_sql_query(sql, uid);
209 _media_svc_sql_query_add(&g_media_svc_insert_folder_query_list, &sql);
212 SAFE_FREE(folder_name);
213 SAFE_FREE(folder_name_pinyin);
218 int _media_svc_update_folder_modified_time_by_folder_uuid(const char *folder_uuid, const char *folder_path, bool stack_query, uid_t uid)
220 int ret = MS_MEDIA_ERR_NONE;
221 int modified_time = 0;
223 modified_time = _media_svc_get_file_time(folder_path);
225 char *sql = sqlite3_mprintf("UPDATE %s SET modified_time=%d WHERE folder_uuid=%Q;", MEDIA_SVC_DB_TABLE_FOLDER, modified_time, folder_uuid);
228 ret = _media_svc_sql_query(sql, uid);
231 _media_svc_sql_query_add(&g_media_svc_move_item_query_list, &sql);
237 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)
239 int ret = MS_MEDIA_ERR_NONE;
240 unsigned int next_pos = 0;
242 char *dir_path = NULL;
243 const char *token = "/";
244 char *folder_uuid = NULL;
245 char parent_folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
246 bool folder_search_end = FALSE;
247 char *internal_path = NULL;
249 memset(parent_folder_uuid, 0, sizeof(parent_folder_uuid));
250 internal_path = _media_svc_get_path(uid);
252 if (STRING_VALID(internal_path) && (strncmp(path, internal_path, strlen(internal_path)) == 0))
253 next_pos = strlen(internal_path);
254 else if (STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0)
255 next_pos = strlen(MEDIA_ROOT_PATH_SDCARD);
256 else if (STRING_VALID(MEDIA_ROOT_PATH_CLOUD) && (strncmp(path, MEDIA_ROOT_PATH_CLOUD, strlen(MEDIA_ROOT_PATH_CLOUD)) == 0))
257 next_pos = strlen(MEDIA_ROOT_PATH_CLOUD);
258 else if (STRING_VALID(MEDIA_ROOT_PATH_EXTERNAL) && strncmp(path, MEDIA_ROOT_PATH_EXTERNAL, strlen(MEDIA_ROOT_PATH_EXTERNAL)) == 0)
259 next_pos = strlen(MEDIA_ROOT_PATH_EXTERNAL);
261 media_svc_error("Invalid Path");
262 SAFE_FREE(internal_path);
263 return MS_MEDIA_ERR_INTERNAL;
266 SAFE_FREE(internal_path);
268 while (!folder_search_end) {
269 next = strstr(path + next_pos, token);
271 next_pos = (next - path);
272 dir_path = strndup(path, next_pos);
275 dir_path = strndup(path, strlen(path));
276 folder_search_end = TRUE;
277 media_svc_error("[No-Error] End Path [%s]", dir_path);
280 if (STRING_VALID(MEDIA_ROOT_PATH_EXTERNAL) && (strcmp(dir_path, MEDIA_ROOT_PATH_EXTERNAL) == 0)) {
281 /*To avoid insert MEDIA_ROOT_PATH_EXTERNAL path*/
285 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, dir_path, parent_folder_uuid, uid);
286 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
287 folder_uuid = _media_info_generate_uuid();
288 if (folder_uuid == NULL) {
289 media_svc_error("Invalid UUID");
291 return MS_MEDIA_ERR_INTERNAL;
294 ret = __media_svc_append_folder(storage_id, storage_type, folder_uuid, dir_path, parent_folder_uuid, FALSE, uid);
295 if (ret != MS_MEDIA_ERR_NONE) {
296 media_svc_error("__media_svc_append_folder is failed");
299 media_svc_error("[No-Error] New Appended folder path [%s], folder_uuid [%s], parent_folder_uuid [%s]", dir_path, folder_uuid, parent_folder_uuid);
300 _strncpy_safe(parent_folder_uuid, folder_uuid, MEDIA_SVC_UUID_SIZE + 1);
302 media_svc_error("EXIST dir path : %s\n", dir_path);
308 if (STRING_VALID(folder_uuid)) {
309 _strncpy_safe(folder_id, folder_uuid, MEDIA_SVC_UUID_SIZE + 1);
311 media_svc_error("Fail to get folder_uuid");
312 return MS_MEDIA_ERR_INTERNAL;
315 return MS_MEDIA_ERR_NONE;
318 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)
320 int ret = MS_MEDIA_ERR_NONE;
322 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, path, folder_id, uid);
324 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
325 ret = __media_svc_get_and_append_parent_folder(handle, storage_id, path, storage_type, folder_id, uid);
331 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)
333 char *dir_path = NULL;
334 int ret = MS_MEDIA_ERR_NONE;
336 dir_path = g_path_get_dirname(path);
338 ret = _media_svc_get_and_append_folder(handle, storage_id, dir_path, storage_type, folder_id, uid);
345 int _media_svc_update_folder_table(const char *storage_id, uid_t uid)
347 int ret = MS_MEDIA_ERR_NONE;
350 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'));",
351 MEDIA_SVC_DB_TABLE_FOLDER, MEDIA_SVC_DB_TABLE_FOLDER, storage_id);
353 ret = _media_svc_sql_query(sql, uid);
359 static int __media_svc_count_all_folders(sqlite3 *handle, char *start_path, int *count)
361 int ret = MS_MEDIA_ERR_NONE;
362 sqlite3_stmt *sql_stmt = NULL;
363 char *sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE path LIKE '%q%%'", MEDIA_SVC_DB_TABLE_FOLDER, start_path);
365 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
366 if (ret != MS_MEDIA_ERR_NONE) {
367 media_svc_error("error when _media_svc_sql_prepare_to_step. err = [%d]", ret);
371 *count = sqlite3_column_int(sql_stmt, 0);
373 SQLITE3_FINALIZE(sql_stmt);
375 return MS_MEDIA_ERR_NONE;
378 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)
380 int ret = MS_MEDIA_ERR_NONE;
382 sqlite3_stmt *sql_stmt = NULL;
385 char **folder_uuid = NULL;
388 ret = __media_svc_count_all_folders(handle, start_path, &cnt);
389 if (ret != MS_MEDIA_ERR_NONE) {
390 media_svc_error("error when __media_svc_count_all_folders. err = [%d]", ret);
395 sql = sqlite3_mprintf("SELECT path, modified_time, folder_uuid FROM '%s' WHERE path LIKE '%q%%'", MEDIA_SVC_DB_TABLE_FOLDER, start_path);
398 *modified_time_list = NULL;
399 *item_num_list = NULL;
400 return MS_MEDIA_ERR_NONE;
403 *folder_list = malloc(sizeof(char *) * cnt);
404 *modified_time_list = malloc(sizeof(int) * cnt);
405 *item_num_list = malloc(sizeof(int) * cnt);
406 folder_uuid = malloc(sizeof(char *) * cnt);
408 if ((*folder_list == NULL) || (*modified_time_list == NULL) || (*item_num_list == NULL) || (folder_uuid == NULL)) {
409 media_svc_error("Out of memory");
412 memset(folder_uuid, 0x0, sizeof(char *) * cnt);
413 memset(*folder_list, 0x0, sizeof(char *) * cnt);
415 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
416 if (ret != MS_MEDIA_ERR_NONE) {
417 media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
421 media_svc_debug("QEURY OK");
424 (*folder_list)[idx] = g_strdup((char *)sqlite3_column_text(sql_stmt, 0));
425 (*modified_time_list)[idx] = (int)sqlite3_column_int(sql_stmt, 1);
427 /* get the folder's id */
428 folder_uuid[idx] = g_strdup((char *)sqlite3_column_text(sql_stmt, 2));
432 if (sqlite3_step(sql_stmt) != SQLITE_ROW)
435 SQLITE3_FINALIZE(sql_stmt);
437 /*get the numbder of item in the folder by using folder's id */
438 for (i = 0; i < idx; i++) {
439 if (STRING_VALID(folder_uuid[i])) {
440 sql = sqlite3_mprintf("SELECT COUNT(*) FROM %s WHERE (folder_uuid='%q' AND validity = 1)", MEDIA_SVC_DB_TABLE_MEDIA, folder_uuid[i]);
441 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
442 if (ret != MS_MEDIA_ERR_NONE) {
443 media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
447 (*item_num_list)[i] = (int)sqlite3_column_int(sql_stmt, 0);
449 SQLITE3_FINALIZE(sql_stmt);
451 media_svc_error("Invalid Folder Id");
457 media_svc_debug("Get Folder is OK");
459 media_svc_error("Fail to get folder");
460 ret = MS_MEDIA_ERR_INTERNAL;
465 for (i = 0; i < idx; i++) {
466 SAFE_FREE(folder_uuid[i]);
468 SAFE_FREE(folder_uuid);
475 for (i = 0; i < idx; i++) {
476 SAFE_FREE((*folder_list)[i]);
477 SAFE_FREE(folder_uuid[i]);
479 SAFE_FREE(*folder_list);
480 SAFE_FREE(*modified_time_list);
481 SAFE_FREE(*item_num_list);
482 SAFE_FREE(folder_uuid);
489 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)
491 int ret = MS_MEDIA_ERR_NONE;
492 sqlite3_stmt *sql_stmt = NULL;
494 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);
496 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
498 if (ret != MS_MEDIA_ERR_NONE) {
499 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
500 media_svc_debug("there is no folder.");
502 media_svc_error("error when _media_svc_get_folder_id_by_foldername. err = [%d]", ret);
507 _strncpy_safe(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE + 1);
508 *modified_time = (int)sqlite3_column_int(sql_stmt, 1);
510 SQLITE3_FINALIZE(sql_stmt);
515 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)
517 char *path_name = NULL;
518 int ret = MS_MEDIA_ERR_NONE;
521 path_name = strdup(path);
522 if (path_name == NULL) {
523 media_svc_error("out of memory");
524 return MS_MEDIA_ERR_OUT_OF_MEMORY;
527 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, path_name, folder_id, uid);
528 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
529 bool is_root = FALSE;
530 bool is_parent_root = FALSE;
532 ret = __media_svc_is_root_path(path_name, &is_root, uid);
533 ret = __media_svc_parent_is_ext_root_path(path_name, &is_parent_root);
535 char parent_folder_uuid[MEDIA_SVC_UUID_SIZE+1] = {0, };
536 memset(parent_folder_uuid, 0x00, sizeof(parent_folder_uuid));
538 if ((is_root == FALSE) && (is_parent_root == FALSE)) {
539 /*get parent folder id*/
540 char *parent_path_name = g_path_get_dirname(path_name);
542 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, parent_path_name, parent_folder_uuid, uid);
543 if (ret != MS_MEDIA_ERR_NONE) {
544 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
545 /*if No-Root directory scanning start before doing storage scanning, parent_folder_uuid can be NULL.
546 You can remove above logic if use below logic always. but I keep above code for the performance issue.
547 Most case (except No-Root directory scanning start before doing storage scanning) get parent_folder_uuid well*/
548 media_svc_error("[No-Error] There is no proper parent_folder_uuid. so try to make it");
549 ret = _media_svc_get_and_append_folder(handle, storage_id, path, storage_type, folder_id, uid);
551 media_svc_error("error when _media_svc_get_parent_folder_id_by_foldername. err = [%d]", ret);
554 SAFE_FREE(path_name);
555 SAFE_FREE(parent_path_name);
561 char *folder_uuid = _media_info_generate_uuid();
562 if (folder_uuid == NULL) {
563 media_svc_error("Invalid UUID");
564 SAFE_FREE(path_name);
565 return MS_MEDIA_ERR_INTERNAL;
568 ret = __media_svc_append_folder(storage_id, storage_type, folder_uuid, path_name, parent_folder_uuid, stack_query, uid);
570 _strncpy_safe(folder_id, folder_uuid, MEDIA_SVC_UUID_SIZE+1);
573 sql = sqlite3_mprintf("UPDATE '%s' SET validity=1 WHERE storage_uuid = '%q' AND path = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
576 ret = _media_svc_sql_query(sql, uid);
579 _media_svc_sql_query_add(&g_media_svc_insert_folder_query_list, &sql);
583 SAFE_FREE(path_name);
588 int _media_svc_delete_invalid_folder(const char *storage_id, uid_t uid)
590 int ret = MS_MEDIA_ERR_NONE;
593 sql = sqlite3_mprintf("DELETE FROM '%s' WHERE storage_uuid = '%q' AND validity = 0;", MEDIA_SVC_DB_TABLE_FOLDER, storage_id);
594 ret = _media_svc_sql_query(sql, uid);
601 int _media_svc_set_folder_validity(sqlite3 *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
603 int ret = MS_MEDIA_ERR_NONE;
605 char start_path_id[MEDIA_SVC_UUID_SIZE+1] = {0,};
608 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, start_path, start_path_id, uid);
609 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_folder_id_by_foldername fail");
610 media_svc_retvm_if(!STRING_VALID(start_path_id), MS_MEDIA_ERR_INVALID_PARAMETER, "start_path_id is NULL");
612 sql = sqlite3_mprintf("UPDATE '%s' SET validity = %d WHERE storage_uuid = '%q' AND (path LIKE '%q/%%' OR folder_uuid ='%q')",
613 MEDIA_SVC_DB_TABLE_FOLDER, validity, storage_id, start_path, start_path_id);
615 sql = sqlite3_mprintf("UPDATE '%s' SET validity = %d WHERE storage_uuid = '%q' AND path = '%q';",
616 MEDIA_SVC_DB_TABLE_FOLDER, validity, storage_id, start_path);
619 ret = _media_svc_sql_query(sql, uid);
626 int _media_svc_delete_folder_by_storage_id(const char *storage_id, media_svc_storage_type_e storage_type, uid_t uid)
628 int ret = MS_MEDIA_ERR_NONE;
631 sql = sqlite3_mprintf("DELETE FROM '%s' WHERE storage_uuid = '%q' AND storage_type = %d;", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, storage_type);
632 ret = _media_svc_sql_query(sql, uid);
639 GList **_media_svc_get_folder_list_ptr(void)
641 return &g_media_svc_insert_folder_query_list;
644 int _media_svc_get_folder_scan_status(sqlite3 *handle, const char *storage_id, const char *path, int *scan_status)
646 int ret = MS_MEDIA_ERR_NONE;
647 sqlite3_stmt *sql_stmt = NULL;
650 if (!STRING_VALID(path)) {
651 media_svc_error("Not found valid path");
652 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
655 sql = sqlite3_mprintf("SELECT scan_status FROM '%s' WHERE path = '%q' AND storage_uuid = '%q'", MEDIA_SVC_DB_TABLE_FOLDER, path, storage_id);
657 ret = _media_svc_sql_prepare_to_step_simple(handle, sql, &sql_stmt);
659 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
661 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
662 *scan_status = sqlite3_column_int(sql_stmt, 0);
665 SQLITE3_FINALIZE(sql_stmt);
670 int _media_svc_set_folder_scan_status(const char *storage_id, const char *path, int scan_status, uid_t uid)
672 int ret = MS_MEDIA_ERR_NONE;
676 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);
678 sql = sqlite3_mprintf("UPDATE '%s' SET scan_status=%d WHERE storage_uuid = '%q'", MEDIA_SVC_DB_TABLE_FOLDER, scan_status, storage_id);
681 ret = _media_svc_sql_query(sql, uid);
687 int _media_svc_get_folder_modified_time_by_path(sqlite3 *handle, const char *path, const char *storage_id, time_t *modified_time)
689 int ret = MS_MEDIA_ERR_NONE;
690 sqlite3_stmt *sql_stmt = NULL;
692 char *sql = sqlite3_mprintf("SELECT modified_time FROM '%s' WHERE path = '%q' AND storage_uuid = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, path, storage_id);
694 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
696 if (ret != MS_MEDIA_ERR_NONE) {
697 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
698 media_svc_debug("there is no folder.");
700 media_svc_error("error when _media_svc_get_folder_modified_time_by_path. err = [%d]", ret);
705 *modified_time = (int)sqlite3_column_int(sql_stmt, 0);
707 SQLITE3_FINALIZE(sql_stmt);
712 int _media_svc_get_null_scan_folder_list(sqlite3 *handle, const char *storage_id, const char *path, char ***folder_list, int *count)
714 int ret = MS_MEDIA_ERR_NONE;
718 char folder_id[MEDIA_SVC_UUID_SIZE+1] = {0,};
719 sqlite3_stmt *sql_stmt = NULL;
722 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);
723 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
725 if (ret != MS_MEDIA_ERR_NONE) {
726 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
727 media_svc_debug("there is no folder.");
729 media_svc_error("error when get folder_id by path. err = [%d]", ret);
738 cnt = sqlite3_column_int(sql_stmt, 0);
739 SQLITE3_FINALIZE(sql_stmt);
741 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);
746 return MS_MEDIA_ERR_INTERNAL;
749 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
751 if (ret != MS_MEDIA_ERR_NONE) {
752 media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
760 sql = sqlite3_mprintf("SELECT folder_uuid FROM '%s' WHERE storage_uuid = '%q' AND path = '%q'", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
761 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
763 if (ret != MS_MEDIA_ERR_NONE) {
764 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
765 media_svc_debug("there is no folder.");
767 media_svc_error("error when get folder_id by path [%s]. err = [%d]", path, ret);
772 _strncpy_safe(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE+1);
774 SQLITE3_FINALIZE(sql_stmt);
776 sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE path LIKE '%q%%' AND parent_folder_uuid = '%q'", MEDIA_SVC_DB_TABLE_FOLDER, path, folder_id);
778 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
780 if (ret != MS_MEDIA_ERR_NONE) {
781 media_svc_error("error when _media_svc_sql_prepare_to_step. err = [%d]", ret);
785 cnt = sqlite3_column_int(sql_stmt, 0);
786 SQLITE3_FINALIZE(sql_stmt);
788 sql = sqlite3_mprintf("SELECT path FROM '%s' WHERE path LIKE '%q%%' AND parent_folder_uuid = '%q'", MEDIA_SVC_DB_TABLE_FOLDER, path, folder_id);
793 return MS_MEDIA_ERR_NONE;
796 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
798 if (ret != MS_MEDIA_ERR_NONE) {
799 media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
808 *folder_list = malloc(sizeof(char *) * cnt);
811 (*folder_list)[idx] = strdup((char *)sqlite3_column_text(sql_stmt, 0));
813 if (sqlite3_step(sql_stmt) != SQLITE_ROW)
818 if (cnt == idx + 1) {
820 media_svc_debug("OK");
824 for (i = 0; i < idx; i++) {
825 SAFE_FREE((*folder_list)[i]);
827 SAFE_FREE(*folder_list);
829 ret = MS_MEDIA_ERR_INTERNAL;
832 SQLITE3_FINALIZE(sql_stmt);
837 int _media_svc_delete_invalid_folder_by_path(sqlite3 *handle, const char *storage_id, const char *folder_path, uid_t uid, int *delete_count)
839 int ret = MS_MEDIA_ERR_NONE;
842 sqlite3_stmt *sql_stmt = NULL;
844 if (folder_path == NULL)
845 return MS_MEDIA_ERR_INVALID_PARAMETER;
847 /*check the number of the deleted folder*/
848 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);
850 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
851 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
853 del_count = sqlite3_column_int(sql_stmt, 0);
855 SQLITE3_FINALIZE(sql_stmt);
858 /*delete invalid folder*/
859 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);
860 ret = _media_svc_sql_query(sql, uid);
864 *delete_count = del_count;
869 int _media_svc_count_folder_with_path(sqlite3 *handle, const char *storage_id, const char *path, int *count)
871 int ret = MS_MEDIA_ERR_NONE;
872 sqlite3_stmt *sql_stmt = NULL;
874 char *sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE (storage_uuid='%q' AND path='%q')", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
876 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
878 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
880 *count = sqlite3_column_int(sql_stmt, 0);
882 SQLITE3_FINALIZE(sql_stmt);
884 return MS_MEDIA_ERR_NONE;
887 int _media_svc_count_subfolder_with_path(sqlite3 *handle, const char *storage_id, const char *path, int *count)
889 int ret = MS_MEDIA_ERR_NONE;
890 sqlite3_stmt *sql_stmt = NULL;
892 char *sql = sqlite3_mprintf("SELECT count(*) FROM '%s' WHERE (storage_uuid='%q' AND path LIKE'%q/%%')", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
894 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
896 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
898 *count = sqlite3_column_int(sql_stmt, 0);
900 SQLITE3_FINALIZE(sql_stmt);
902 return MS_MEDIA_ERR_NONE;
905 int _media_svc_get_folder_uuid(sqlite3 *handle, const char *storage_id, const char *path, char *folder_id)
907 int ret = MS_MEDIA_ERR_NONE;
908 sqlite3_stmt *sql_stmt = NULL;
911 sql = sqlite3_mprintf("SELECT folder_uuid FROM '%s' WHERE (storage_uuid='%q' AND path='%q')", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
913 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
915 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
917 _strncpy_safe(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE+1);
919 SQLITE3_FINALIZE(sql_stmt);
921 if (!STRING_VALID(folder_id)) {
922 media_svc_error("Not found valid storage id [%s]", path);
923 ret = MS_MEDIA_ERR_INVALID_PARAMETER;