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 int _media_svc_get_folder_id_by_foldername(sqlite3 *handle, const char *storage_id, const char *folder_name, char *folder_id, uid_t uid)
32 int ret = MS_MEDIA_ERR_NONE;
33 sqlite3_stmt *sql_stmt = NULL;
36 sql = sqlite3_mprintf("SELECT folder_id FROM %q WHERE storage_uuid=%Q AND folder_path=%Q", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, folder_name);
38 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
39 if (ret != MS_MEDIA_ERR_NONE) {
40 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
41 media_svc_debug("No record");
43 media_svc_error("error when _media_svc_get_folder_id_by_foldername. err = [%d]", ret);
48 SAFE_STRLCPY(folder_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE + 1);
50 SQLITE3_FINALIZE(sql_stmt);
55 static int __media_svc_append_folder(bool is_direct, const char *storage_id, ms_user_storage_type_e storage_type, const char *folder_id, const char *folder_path, uid_t uid)
57 int ret = MS_MEDIA_ERR_NONE;
58 char *folder_name = NULL;
59 int folder_modified_date = 0;
61 folder_name = g_path_get_basename(folder_path);
62 folder_modified_date = _media_svc_get_file_time(folder_path);
64 /*Update Pinyin If Support Pinyin*/
65 char *folder_name_pinyin = NULL;
66 if (_media_svc_check_pinyin_support())
67 _media_svc_get_pinyin_str(folder_name, &folder_name_pinyin);
68 /* Sometime SQLITE3 returns NO_RECORD, so need to consider conflict case.. */
69 char *sql = sqlite3_mprintf("INSERT OR IGNORE INTO %q (folder_id, folder_path, folder_name, storage_uuid, folder_storage_type, folder_modified_time, folder_name_pinyin) values (%Q, %Q, %Q, %Q, '%d', '%d', %Q); ",
70 MEDIA_SVC_DB_TABLE_FOLDER, folder_id, folder_path, folder_name, storage_id, storage_type, folder_modified_date, folder_name_pinyin);
73 ret = _media_svc_sql_query_direct(sql, uid);
75 ret = _media_svc_sql_query(sql, uid);
77 SQLITE3_SAFE_FREE(sql);
79 SAFE_FREE(folder_name);
80 SAFE_FREE(folder_name_pinyin);
85 int _media_svc_update_folder_modified_time_by_folder_uuid(const char *folder_uuid, const char *folder_path, uid_t uid)
87 int ret = MS_MEDIA_ERR_NONE;
88 int modified_time = 0;
90 modified_time = _media_svc_get_file_time(folder_path);
92 char *sql = sqlite3_mprintf("UPDATE %q SET folder_modified_time=%d WHERE folder_id=%Q;", MEDIA_SVC_DB_TABLE_FOLDER, modified_time, folder_uuid);
94 ret = _media_svc_sql_query(sql, uid);
95 SQLITE3_SAFE_FREE(sql);
100 static int __media_svc_get_and_append_parent_folder(sqlite3 *handle, bool is_direct, const char *storage_id, const char *path, ms_user_storage_type_e storage_type, char *folder_id, uid_t uid)
102 int ret = MS_MEDIA_ERR_NONE;
103 unsigned int next_pos = 0;
105 char *dir_path = NULL;
106 const char *token = "/";
107 char *folder_uuid = NULL;
108 char tmp_folder_uuid[MEDIA_SVC_UUID_SIZE + 1] = {0, };
109 bool folder_search_end = false;
110 char *internal_path = NULL;
112 memset(tmp_folder_uuid, 0, sizeof(tmp_folder_uuid));
113 ret = ms_user_get_internal_root_path(uid, &internal_path);
114 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Fail to get root path");
116 if (STRING_VALID(internal_path) && (strncmp(path, internal_path, strlen(internal_path)) == 0))
117 next_pos = strlen(internal_path);
118 else if (STRING_VALID(MEDIA_ROOT_PATH_SDCARD) && strncmp(path, MEDIA_ROOT_PATH_SDCARD, strlen(MEDIA_ROOT_PATH_SDCARD)) == 0)
119 next_pos = strlen(MEDIA_ROOT_PATH_SDCARD);
120 else if (STRING_VALID(MEDIA_ROOT_PATH_EXTERNAL) && strncmp(path, MEDIA_ROOT_PATH_EXTERNAL, strlen(MEDIA_ROOT_PATH_EXTERNAL)) == 0)
121 next_pos = strlen(MEDIA_ROOT_PATH_EXTERNAL);
123 media_svc_error("Invalid Path");
124 media_svc_sec_error("Invalid Path [%s], internal_path [%s]", path, internal_path);
125 SAFE_FREE(internal_path);
126 return MS_MEDIA_ERR_INTERNAL;
129 SAFE_FREE(internal_path);
131 while (!folder_search_end) {
132 next = strstr(path + next_pos, token);
134 next_pos = (next - path);
135 dir_path = strndup(path, next_pos);
138 dir_path = strndup(path, strlen(path));
139 folder_search_end = true;
140 media_svc_sec_debug("End Path[%s]", dir_path);
143 if (STRING_VALID(MEDIA_ROOT_PATH_EXTERNAL) && (g_strcmp0(dir_path, MEDIA_ROOT_PATH_EXTERNAL) == 0)) {
144 /*To avoid insert MEDIA_ROOT_PATH_EXTERNAL path*/
149 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, dir_path, tmp_folder_uuid, uid);
150 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
151 folder_uuid = _media_info_generate_uuid();
152 if (folder_uuid == NULL) {
153 media_svc_error("Invalid UUID");
155 return MS_MEDIA_ERR_INTERNAL;
158 ret = __media_svc_append_folder(is_direct, storage_id, storage_type, folder_uuid, dir_path, uid);
159 if (ret != MS_MEDIA_ERR_NONE)
160 media_svc_error("__media_svc_append_folder is failed");
162 media_svc_sec_debug("Append new folder path[%s] uuid[%s]", dir_path, folder_uuid);
168 if (STRING_VALID(folder_uuid)) {
169 SAFE_STRLCPY(folder_id, folder_uuid, MEDIA_SVC_UUID_SIZE + 1);
171 media_svc_error("Fail to get folder_uuid");
172 return MS_MEDIA_ERR_INTERNAL;
175 return MS_MEDIA_ERR_NONE;
178 int _media_svc_get_and_append_folder(sqlite3 *handle, bool is_direct, const char *storage_id, const char *path, ms_user_storage_type_e storage_type, char *folder_id, uid_t uid)
180 int ret = MS_MEDIA_ERR_NONE;
182 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, path, folder_id, uid);
183 if (ret == MS_MEDIA_ERR_DB_NO_RECORD)
184 ret = __media_svc_get_and_append_parent_folder(handle, is_direct, storage_id, path, storage_type, folder_id, uid);
189 int _media_svc_get_and_append_folder_id_by_path(sqlite3 *handle, bool is_direct, const char *storage_id, const char *path, ms_user_storage_type_e storage_type, char *folder_id, uid_t uid)
191 char *dir_path = NULL;
192 int ret = MS_MEDIA_ERR_NONE;
194 dir_path = g_path_get_dirname(path);
196 ret = _media_svc_get_and_append_folder(handle, is_direct, storage_id, dir_path, storage_type, folder_id, uid);
203 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, uid_t uid)
205 char *path_name = NULL;
206 int ret = MS_MEDIA_ERR_NONE;
208 char folder_id[MEDIA_SVC_UUID_SIZE + 1] = {0, };
210 path_name = strdup(path);
211 if (path_name == NULL) {
212 media_svc_error("out of memory");
213 return MS_MEDIA_ERR_OUT_OF_MEMORY;
216 ret = _media_svc_get_folder_id_by_foldername(handle, storage_id, path_name, folder_id, uid);
217 if (ret == MS_MEDIA_ERR_DB_NO_RECORD) {
218 ret = __media_svc_get_and_append_parent_folder(handle, true, storage_id, path_name, storage_type, folder_id, uid);
220 sql = sqlite3_mprintf("UPDATE %q SET validity=1 WHERE storage_uuid = '%q' AND folder_path = '%q';", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
221 ret = _media_svc_sql_query_direct(sql, uid);
222 SQLITE3_SAFE_FREE(sql);
225 SAFE_FREE(path_name);
230 int _media_svc_set_folder_validity(sqlite3 *handle, bool is_direct, const char *storage_id, const char *start_path, int validity, bool is_recursive, uid_t uid)
232 int ret = MS_MEDIA_ERR_NONE;
236 sql = sqlite3_mprintf("UPDATE %q SET validity=%d WHERE storage_uuid='%q' AND (folder_path LIKE '%q/%%' OR folder_path='%q');",
237 MEDIA_SVC_DB_TABLE_FOLDER, validity, storage_id, start_path, start_path);
239 sql = sqlite3_mprintf("UPDATE %q SET validity=%d WHERE storage_uuid='%q' AND folder_path='%q';",
240 MEDIA_SVC_DB_TABLE_FOLDER, validity, storage_id, start_path);
244 ret = _media_svc_sql_query_direct(sql, uid);
246 ret = _media_svc_sql_query(sql, uid);
248 SQLITE3_SAFE_FREE(sql);
253 int _media_svc_count_folder_with_path(sqlite3 *handle, const char *storage_id, const char *path, int *count)
255 int ret = MS_MEDIA_ERR_NONE;
256 sqlite3_stmt *sql_stmt = NULL;
259 media_svc_retvm_if(!STRING_VALID(storage_id), MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
260 media_svc_retvm_if(!STRING_VALID(path), MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
262 sql = sqlite3_mprintf("SELECT count(*) FROM %q WHERE (storage_uuid='%q' AND folder_path='%q')", MEDIA_SVC_DB_TABLE_FOLDER, storage_id, path);
264 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
266 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
268 *count = sqlite3_column_int(sql_stmt, 0);
270 SQLITE3_FINALIZE(sql_stmt);
272 return MS_MEDIA_ERR_NONE;