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 static __thread GList *g_media_svc_insert_folder_query_list;
32 int _media_svc_get_folder_id_by_foldername(sqlite3 *handle, const char *storage_id, const char *folder_name, char *folder_id, uid_t uid)
34 int ret = MS_MEDIA_ERR_NONE;
35 sqlite3_stmt *sql_stmt = NULL;
38 sql = sqlite3_mprintf("SELECT folder_uuid FROM %q WHERE storage_uuid = '%q' AND path = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, folder_name);
40 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
41 if (ret != MS_MEDIA_ERR_NONE) {
42 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
43 media_svc_debug("there is no folder.");
45 media_svc_error("error when _media_svc_get_folder_id_by_foldername. err = [%d]", ret);
50 SAFE_STRLCPY(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE + 1);
52 SQLITE3_FINALIZE(sql_stmt);
57 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)
59 int ret = MS_MEDIA_ERR_NONE;
60 sqlite3_stmt *sql_stmt = NULL;
63 sql = sqlite3_mprintf("SELECT folder_uuid, validity FROM %q WHERE storage_uuid = '%q' AND path = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, folder_name);
65 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
66 if (ret != MS_MEDIA_ERR_NONE) {
67 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
68 media_svc_debug("there is no folder.");
70 media_svc_error("error when _media_svc_get_folder_id_by_foldername_with_validity. err = [%d]", ret);
75 SAFE_STRLCPY(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE + 1);
76 *validity = (int)sqlite3_column_int(sql_stmt, 1);
78 SQLITE3_FINALIZE(sql_stmt);
83 static int __media_svc_append_folder(const char *storage_id, ms_user_storage_type_e storage_type,
84 const char *folder_id, const char *folder_path, bool stack_query, uid_t uid)
86 int ret = MS_MEDIA_ERR_NONE;
87 char *folder_name = NULL;
88 int folder_modified_date = 0;
90 folder_name = g_path_get_basename(folder_path);
91 folder_modified_date = _media_svc_get_file_time(folder_path);
93 /*Update Pinyin If Support Pinyin*/
94 char *folder_name_pinyin = NULL;
95 if (_media_svc_check_pinyin_support())
96 _media_svc_get_pinyin_str(folder_name, &folder_name_pinyin);
97 /* Sometime SQLITE3 returns NO_RECORD, so need to consider conflict case.. */
98 media_svc_debug("UNIQUE:path[%s], storage_uuid[%s]", folder_path, storage_id);
99 char *sql = sqlite3_mprintf("INSERT OR IGNORE INTO %q (folder_uuid, path, name, storage_uuid, storage_type, modified_time, name_pinyin) values (%Q, %Q, %Q, %Q, '%d', '%d', %Q); ",
100 MEDIA_SVC_DB_TABLE_FOLDER, folder_id, folder_path, folder_name, storage_id, storage_type, folder_modified_date, folder_name_pinyin);
103 ret = _media_svc_sql_query(sql, uid);
104 SQLITE3_SAFE_FREE(sql);
106 _media_svc_sql_query_add(&g_media_svc_insert_folder_query_list, &sql);
109 SAFE_FREE(folder_name);
110 SAFE_FREE(folder_name_pinyin);
115 int _media_svc_update_folder_modified_time_by_folder_uuid(const char *folder_uuid, const char *folder_path, uid_t uid)
117 int ret = MS_MEDIA_ERR_NONE;
118 int modified_time = 0;
120 modified_time = _media_svc_get_file_time(folder_path);
122 char *sql = sqlite3_mprintf("UPDATE %q SET modified_time=%d WHERE folder_uuid=%Q;", MEDIA_SVC_DB_TABLE_FOLDER, modified_time, folder_uuid);
124 ret = _media_svc_sql_query(sql, uid);
125 SQLITE3_SAFE_FREE(sql);
130 static int __media_svc_get_and_append_parent_folder(sqlite3 *handle, const char *storage_id, const char *path, ms_user_storage_type_e storage_type, char *folder_id, uid_t uid)
132 int ret = MS_MEDIA_ERR_NONE;
133 unsigned int next_pos = 0;
135 char *dir_path = NULL;
136 const char *token = "/";
137 char *folder_uuid = NULL;
138 char tmp_folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
139 bool folder_search_end = FALSE;
140 char *internal_path = NULL;
142 memset(tmp_folder_uuid, 0, sizeof(tmp_folder_uuid));
143 ret = ms_user_get_internal_root_path(uid, &internal_path);
144 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Fail to get root path");
146 if (STRING_VALID(internal_path) && (strncmp(path, internal_path, strlen(internal_path)) == 0))
147 next_pos = strlen(internal_path);
148 else if (STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0)
149 next_pos = strlen(MEDIA_ROOT_PATH_SDCARD);
150 else if (STRING_VALID(MEDIA_ROOT_PATH_EXTERNAL) && strncmp(path, MEDIA_ROOT_PATH_EXTERNAL, strlen(MEDIA_ROOT_PATH_EXTERNAL)) == 0)
151 next_pos = strlen(MEDIA_ROOT_PATH_EXTERNAL);
152 else if (STRING_VALID(MEDIA_ROOT_PATH_DISC) && strncmp(path, MEDIA_ROOT_PATH_DISC, strlen(MEDIA_ROOT_PATH_DISC)) == 0)
153 next_pos = strlen(MEDIA_ROOT_PATH_DISC);
155 media_svc_error("Invalid Path");
156 media_svc_sec_error("Invalid Path [%s], internal_path [%s]", path, internal_path);
157 SAFE_FREE(internal_path);
158 return MS_MEDIA_ERR_INTERNAL;
161 SAFE_FREE(internal_path);
163 while (!folder_search_end) {
164 next = strstr(path + next_pos, token);
166 next_pos = (next - path);
167 dir_path = strndup(path, next_pos);
170 dir_path = strndup(path, strlen(path));
171 folder_search_end = TRUE;
172 media_svc_error("[No-Error] End Path [%s]", dir_path);
175 if (STRING_VALID(MEDIA_ROOT_PATH_EXTERNAL) && (g_strcmp0(dir_path, MEDIA_ROOT_PATH_EXTERNAL) == 0)) {
176 /*To avoid insert MEDIA_ROOT_PATH_EXTERNAL path*/
181 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, dir_path, tmp_folder_uuid, uid);
182 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
183 folder_uuid = _media_info_generate_uuid();
184 if (folder_uuid == NULL) {
185 media_svc_error("Invalid UUID");
187 return MS_MEDIA_ERR_INTERNAL;
190 ret = __media_svc_append_folder(storage_id, storage_type, folder_uuid, dir_path, FALSE, uid);
191 if (ret != MS_MEDIA_ERR_NONE)
192 media_svc_error("__media_svc_append_folder is failed");
194 media_svc_error("[No-Error] New Appended folder path [%s], folder_uuid [%s]", dir_path, folder_uuid);
196 media_svc_error("EXIST dir path : %s", dir_path);
202 if (STRING_VALID(folder_uuid)) {
203 SAFE_STRLCPY(folder_id, folder_uuid, MEDIA_SVC_UUID_SIZE + 1);
205 media_svc_error("Fail to get folder_uuid");
206 return MS_MEDIA_ERR_INTERNAL;
209 return MS_MEDIA_ERR_NONE;
212 static int __media_svc_update_parent_folder_validity(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
214 int ret = MS_MEDIA_ERR_NONE;
215 unsigned int next_pos = 0;
217 char *dir_path = NULL;
218 const char *token = "/";
219 bool folder_search_end = FALSE;
220 char *internal_path = NULL;
222 ret = ms_user_get_internal_root_path(uid, &internal_path);
223 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Fail to get root path");
225 if (STRING_VALID(internal_path) && (strncmp(path, internal_path, strlen(internal_path)) == 0))
226 next_pos = strlen(internal_path);
227 else if (STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0)
228 next_pos = strlen(MEDIA_ROOT_PATH_SDCARD);
229 else if (STRING_VALID(MEDIA_ROOT_PATH_EXTERNAL) && strncmp(path, MEDIA_ROOT_PATH_EXTERNAL, strlen(MEDIA_ROOT_PATH_EXTERNAL)) == 0)
230 next_pos = strlen(MEDIA_ROOT_PATH_EXTERNAL);
231 else if (STRING_VALID(MEDIA_ROOT_PATH_DISC) && strncmp(path, MEDIA_ROOT_PATH_DISC, strlen(MEDIA_ROOT_PATH_DISC)) == 0)
232 next_pos = strlen(MEDIA_ROOT_PATH_DISC);
234 media_svc_error("Invalid Path");
235 media_svc_sec_error("Invalid Path [%s], internal_path [%s]", path, internal_path);
236 SAFE_FREE(internal_path);
237 return MS_MEDIA_ERR_INTERNAL;
240 SAFE_FREE(internal_path);
242 while (!folder_search_end) {
243 next = strstr(path + next_pos, token);
245 next_pos = (next - path);
246 dir_path = strndup(path, next_pos);
249 dir_path = strndup(path, strlen(path));
250 folder_search_end = TRUE;
251 media_svc_error("[No-Error] End Path [%s]", dir_path);
254 if (STRING_VALID(MEDIA_ROOT_PATH_EXTERNAL) && (g_strcmp0(dir_path, MEDIA_ROOT_PATH_EXTERNAL) == 0)) {
255 /*To avoid insert MEDIA_ROOT_PATH_EXTERNAL path*/
260 ret = _media_svc_set_folder_validity(handle, storage_id, dir_path, 1, false, uid);
262 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Fail to set folder validity");
265 return MS_MEDIA_ERR_NONE;
268 int _media_svc_get_and_append_folder(sqlite3 *handle, const char *storage_id, const char *path, ms_user_storage_type_e storage_type, char *folder_id, uid_t uid)
270 int ret = MS_MEDIA_ERR_NONE;
273 ret = _media_svc_get_folder_id_by_foldername_with_validity(handle, storage_id, path, folder_id, uid, &validity);
275 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
276 ret = __media_svc_get_and_append_parent_folder(handle, storage_id, path, storage_type, folder_id, uid);
277 else if (ret == MS_MEDIA_ERR_NONE && validity == 0)
278 ret = __media_svc_update_parent_folder_validity(handle, storage_id, path, uid);
283 int _media_svc_get_and_append_folder_id_by_path(sqlite3 *handle, const char *storage_id, const char *path, ms_user_storage_type_e storage_type, char *folder_id, uid_t uid)
285 char *dir_path = NULL;
286 int ret = MS_MEDIA_ERR_NONE;
288 dir_path = g_path_get_dirname(path);
290 ret = _media_svc_get_and_append_folder(handle, storage_id, dir_path, storage_type, folder_id, uid);
297 int _media_svc_get_and_append_folder_id_by_folder_path(sqlite3 *handle, const char *storage_id, const char *path, ms_user_storage_type_e storage_type, char *folder_id, bool stack_query, uid_t uid)
299 char *path_name = NULL;
300 int ret = MS_MEDIA_ERR_NONE;
303 path_name = strdup(path);
304 if (path_name == NULL) {
305 media_svc_error("out of memory");
306 return MS_MEDIA_ERR_OUT_OF_MEMORY;
309 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, path_name, folder_id, uid);
310 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
311 ret = __media_svc_get_and_append_parent_folder(handle, storage_id, path_name, storage_type, folder_id, uid);
313 sql = sqlite3_mprintf("UPDATE %q SET validity=1 WHERE storage_uuid = '%q' AND path = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
315 ret = _media_svc_sql_query(sql, uid);
316 SQLITE3_SAFE_FREE(sql);
318 _media_svc_sql_query_add(&g_media_svc_insert_folder_query_list, &sql);
322 SAFE_FREE(path_name);
327 int _media_svc_delete_invalid_folder(const char *storage_id, int storage_type, uid_t uid)
329 int ret = MS_MEDIA_ERR_NONE;
332 sql = sqlite3_mprintf("DELETE FROM %q WHERE storage_uuid = '%q' AND storage_type = %d AND validity = 0;", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, storage_type);
333 ret = _media_svc_sql_query(sql, uid);
335 SQLITE3_SAFE_FREE(sql);
340 int _media_svc_set_folder_validity(sqlite3 *handle, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
342 int ret = MS_MEDIA_ERR_NONE;
344 char start_path_id[MEDIA_SVC_UUID_SIZE+1] = {0,};
347 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, start_path, start_path_id, uid);
348 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "_media_svc_get_folder_id_by_foldername fail");
349 media_svc_retvm_if(!STRING_VALID(start_path_id), MS_MEDIA_ERR_INVALID_PARAMETER, "start_path_id is NULL");
351 sql = sqlite3_mprintf("UPDATE %q SET validity = %d WHERE storage_uuid = '%q' AND (path LIKE '%q/%%' OR folder_uuid ='%q');",
352 MEDIA_SVC_DB_TABLE_FOLDER, validity, storage_id, start_path, start_path_id);
354 sql = sqlite3_mprintf("UPDATE %q SET validity = %d WHERE storage_uuid = '%q' AND path = '%q';",
355 MEDIA_SVC_DB_TABLE_FOLDER, validity, storage_id, start_path);
358 ret = _media_svc_sql_query(sql, uid);
360 SQLITE3_SAFE_FREE(sql);
365 GList **_media_svc_get_folder_list_ptr(void)
367 return &g_media_svc_insert_folder_query_list;
370 int _media_svc_count_folder_with_path(sqlite3 *handle, const char *storage_id, const char *path, int *count)
372 int ret = MS_MEDIA_ERR_NONE;
373 sqlite3_stmt *sql_stmt = NULL;
376 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
377 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
379 sql = sqlite3_mprintf("SELECT count(*) FROM %q WHERE (storage_uuid='%q' AND path='%q')", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
381 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
383 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
385 *count = sqlite3_column_int(sql_stmt, 0);
387 SQLITE3_FINALIZE(sql_stmt);
389 return MS_MEDIA_ERR_NONE;
392 int _media_svc_get_folder_uuid(sqlite3 *handle, const char *storage_id, const char *path, char *folder_id)
394 int ret = MS_MEDIA_ERR_NONE;
395 sqlite3_stmt *sql_stmt = NULL;
398 media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
399 media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
401 sql = sqlite3_mprintf("SELECT folder_uuid FROM %q WHERE (storage_uuid='%q' AND path='%q')", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
403 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
405 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
407 SAFE_STRLCPY(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE+1);
409 SQLITE3_FINALIZE(sql_stmt);
411 if (!STRING_VALID(folder_id)) {
412 media_svc_error("Not found valid storage id [%s]", path);
413 ret = MS_MEDIA_ERR_INVALID_PARAMETER;