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 "media-util-err.h"
23 #include "media-svc-debug.h"
24 #include "media-svc-env.h"
25 #include "media-svc-db-utils.h"
26 #include "media-svc-util.h"
27 #include "media-svc-storage.h"
29 int _media_svc_check_storage(sqlite3 *handle, const char *storage_id, char **storage_path, int *validity, uid_t uid)
31 int ret = MS_MEDIA_ERR_NONE;
32 sqlite3_stmt *sql_stmt = NULL;
38 sql = sqlite3_mprintf("SELECT storage_path, validity FROM '%q' WHERE storage_uuid=%Q", MEDIA_SVC_DB_TABLE_STORAGE, storage_id);
39 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
40 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
42 *storage_path = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
43 *validity = sqlite3_column_int(sql_stmt, 1);
45 SQLITE3_FINALIZE(sql_stmt);
47 /*check storage media table*/
48 if (STRING_VALID(storage_id)) {
51 /*Select list of storage*/
52 sql = sqlite3_mprintf("SELECT COUNT(*) FROM SQLITE_MASTER WHERE type='table' and name='%q'", storage_id);
53 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
54 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
56 table_cnt = sqlite3_column_int(sql_stmt, 0);
57 SQLITE3_FINALIZE(sql_stmt);
62 media_svc_error("media table not exist for storage [%s]", storage_id);
63 /*make storage media table*/
64 ret = _media_svc_create_media_table_with_id(storage_id, uid);
65 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "create media table failed : %d", ret);
69 return MS_MEDIA_ERR_NONE;
72 int _media_svc_append_storage(const char *storage_id, const char *storage_path, ms_user_storage_type_e storage_type, uid_t uid)
74 int ret = MS_MEDIA_ERR_NONE;
75 char *sql = sqlite3_mprintf("INSERT INTO %q (storage_uuid, storage_path, storage_type) values (%Q, %Q, %d);",
76 MEDIA_SVC_DB_TABLE_STORAGE, storage_id, storage_path, storage_type);
78 ret = _media_svc_sql_query(sql, uid);
79 SQLITE3_SAFE_FREE(sql);
84 int _media_svc_update_storage_path(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
86 int ret = MS_MEDIA_ERR_NONE;
88 char *old_storage_path = NULL;
92 ret = _media_svc_check_storage(handle, storage_id, &old_storage_path, &validity, uid);
93 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
95 /*Storage table update*/
96 sql = sqlite3_mprintf("UPDATE '%q' SET storage_path=%Q WHERE storage_uuid=%Q;", MEDIA_SVC_DB_TABLE_STORAGE, path, storage_id);
97 ret = _media_svc_sql_query(sql, uid);
98 SQLITE3_SAFE_FREE(sql);
99 if (ret != MS_MEDIA_ERR_NONE) {
100 G_SAFE_FREE(old_storage_path);
104 /*Folder table update*/
105 sql = sqlite3_mprintf("UPDATE '%q' SET path=REPLACE(path, %Q, %Q) WHERE storage_uuid=%Q;", MEDIA_SVC_DB_TABLE_FOLDER, old_storage_path, path, storage_id);
106 ret = _media_svc_sql_query(sql, uid);
107 SQLITE3_SAFE_FREE(sql);
108 if (ret != MS_MEDIA_ERR_NONE) {
109 G_SAFE_FREE(old_storage_path);
113 /*Media table update*/
114 sql = sqlite3_mprintf("UPDATE '%q' SET path=REPLACE(path, %Q, %Q);", storage_id, old_storage_path, path);
115 ret = _media_svc_sql_query(sql, uid);
116 SQLITE3_SAFE_FREE(sql);
117 G_SAFE_FREE(old_storage_path);
118 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
123 int __media_svc_delete_thumbnail(sqlite3 *handle, const char *storage_id)
125 int ret = MS_MEDIA_ERR_NONE;
127 sqlite3_stmt *sql_stmt = NULL;
129 sql = sqlite3_mprintf("SELECT thumbnail_path FROM '%q' WHERE thumbnail_path is not null;", storage_id);
130 ret = _media_svc_sql_prepare_to_step_simple(handle, sql, &sql_stmt);
131 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
133 while (sqlite3_step(sql_stmt) == SQLITE_ROW)
134 _media_svc_remove_file((const char *)sqlite3_column_text(sql_stmt, 0));
136 SQLITE3_FINALIZE(sql_stmt);
141 int _media_svc_delete_invalid_storage(sqlite3 *handle, ms_user_storage_type_e storage_type, uid_t uid)
143 int ret = MS_MEDIA_ERR_NONE;
145 char *storage_id = NULL;
146 char *thumb_path = NULL;
147 sqlite3_stmt *sql_stmt = NULL;
148 GArray *storage_list = NULL;
150 /* Get invalid storage_uuid (with storage_type)*/
151 sql = sqlite3_mprintf("SELECT storage_uuid FROM '%q' WHERE validity = 0 AND storage_type=%d;", MEDIA_SVC_DB_TABLE_STORAGE, storage_type);
152 ret = _media_svc_sql_prepare_to_step_simple(handle, sql, &sql_stmt);
153 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
155 storage_list = g_array_new(false, false, sizeof(char *));
157 while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
158 storage_id = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
159 if (storage_id != NULL)
160 g_array_append_val(storage_list, storage_id);
165 SQLITE3_FINALIZE(sql_stmt);
167 while ((storage_list != NULL) && (storage_list->len > 0)) {
168 storage_id = g_array_index(storage_list, char *, 0);
169 storage_list = g_array_remove_index(storage_list, 0);
171 ret = __media_svc_delete_thumbnail(handle, storage_id);
172 if (ret != MS_MEDIA_ERR_NONE) {
173 media_svc_error("Fail to remove thumbnail");
177 /* remove media before drop table (for clear playlist, and tag table)*/
178 sql = sqlite3_mprintf("DELETE FROM '%q';DROP TABLE '%q';", storage_id, storage_id);
179 ret = _media_svc_sql_query(sql, uid);
180 SQLITE3_SAFE_FREE(sql);
181 if (ret != MS_MEDIA_ERR_NONE) {
182 media_svc_error("Fail to drop table[%s]", storage_id);
186 SAFE_FREE(storage_id);
189 /* Update storage, folder table */
190 sql = sqlite3_mprintf("DELETE FROM '%q' WHERE validity=0 AND storage_type=%d;DELETE FROM '%q' WHERE validity=0 AND storage_type=%d;",
191 MEDIA_SVC_DB_TABLE_STORAGE, storage_type, MEDIA_SVC_DB_TABLE_FOLDER, storage_type);
193 ret = _media_svc_sql_query(sql, uid);
194 SQLITE3_SAFE_FREE(sql);
195 if (ret != MS_MEDIA_ERR_NONE) {
196 media_svc_error("Fail to update storage table");
201 SAFE_FREE(storage_id);
202 SAFE_FREE(thumb_path);
204 while ((storage_list != NULL) && (storage_list->len > 0)) {
205 storage_id = g_array_index(storage_list, char *, 0);
206 storage_list = g_array_remove_index(storage_list, 0);
207 SAFE_FREE(storage_id);
210 g_array_free(storage_list, false);
216 int _media_svc_update_storage_validity(const char *storage_id, int validity, uid_t uid)
218 int ret = MS_MEDIA_ERR_NONE;
221 if (storage_id == NULL)
222 sql = sqlite3_mprintf("UPDATE '%q' SET validity=%d WHERE storage_uuid!='media';", MEDIA_SVC_DB_TABLE_STORAGE, validity);
224 sql = sqlite3_mprintf("UPDATE '%q' SET validity=%d WHERE storage_uuid=%Q;", MEDIA_SVC_DB_TABLE_STORAGE, validity, storage_id);
226 ret = _media_svc_sql_query(sql, uid);
227 SQLITE3_SAFE_FREE(sql);
232 int _media_svc_get_storage_uuid(sqlite3 *handle, const char *path, char *storage_id, uid_t uid)
234 int ret = MS_MEDIA_ERR_NONE;
235 sqlite3_stmt *sql_stmt = NULL;
237 char *storage_path = NULL;
238 char *remain_path = NULL;
240 char *internal_path = NULL;
242 ret = ms_user_get_internal_root_path(uid, &internal_path);
243 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Fail to get root path");
245 if (STRING_VALID(internal_path) && strncmp(path, internal_path, strlen(internal_path)) == 0) {
246 SAFE_STRLCPY(storage_id, MEDIA_SVC_DB_TABLE_MEDIA, MEDIA_SVC_UUID_SIZE+1);
247 SAFE_FREE(internal_path);
248 return MS_MEDIA_ERR_NONE;
251 SAFE_FREE(internal_path);
253 remain_path = strstr(path + (STRING_VALID(MEDIA_ROOT_PATH_USB) ? strlen(MEDIA_ROOT_PATH_USB) : 0) + 1, "/");
254 if (remain_path != NULL)
255 remain_len = strlen(remain_path);
257 storage_path = strndup(path, strlen(path) - remain_len);
259 sql = sqlite3_mprintf("SELECT storage_uuid FROM '%q' WHERE validity=1 AND storage_path = '%q'", MEDIA_SVC_DB_TABLE_STORAGE, storage_path);
261 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
262 SAFE_FREE(storage_path);
263 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
265 if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 0)))
266 SAFE_STRLCPY(storage_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE+1);
268 SQLITE3_FINALIZE(sql_stmt);
270 if (!STRING_VALID(storage_id)) {
271 media_svc_error("Not found valid storage id [%s]", path);
272 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
278 static int __media_svc_count_all_storage(sqlite3 *handle, int *count)
280 int ret = MS_MEDIA_ERR_NONE;
281 sqlite3_stmt *sql_stmt = NULL;
282 char *sql = sqlite3_mprintf("SELECT count(*) FROM '%q' WHERE validity = 1", MEDIA_SVC_DB_TABLE_STORAGE);
284 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
285 if (ret != MS_MEDIA_ERR_NONE) {
286 media_svc_error("error when _media_svc_sql_prepare_to_step. err = [%d]", ret);
290 *count = sqlite3_column_int(sql_stmt, 0);
292 SQLITE3_FINALIZE(sql_stmt);
294 return MS_MEDIA_ERR_NONE;
297 int _media_svc_get_all_storage(sqlite3 *handle, char ***storage_list, char ***storage_id_list, int *count)
299 int ret = MS_MEDIA_ERR_NONE;
301 sqlite3_stmt *sql_stmt = NULL;
305 ret = __media_svc_count_all_storage(handle, &cnt);
306 if (ret != MS_MEDIA_ERR_NONE) {
307 media_svc_error("error when __media_svc_count_all_folders. err = [%d]", ret);
312 sql = sqlite3_mprintf("SELECT storage_path, storage_uuid FROM '%q' WHERE validity = 1", MEDIA_SVC_DB_TABLE_STORAGE);
314 *storage_list = NULL;
315 return MS_MEDIA_ERR_NONE;
318 *storage_list = malloc(sizeof(char *) * cnt);
319 *storage_id_list = malloc(sizeof(char *) * cnt);
320 if (*storage_list == NULL || *storage_id_list == NULL) {
321 media_svc_error("Allocation failed");
322 SAFE_FREE(*storage_list);
323 SAFE_FREE(*storage_id_list);
324 SQLITE3_SAFE_FREE(sql);
325 return MS_MEDIA_ERR_OUT_OF_MEMORY;
328 ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
329 if (ret != MS_MEDIA_ERR_NONE) {
330 media_svc_error("prepare error [%s]", sqlite3_errmsg(handle));
331 SAFE_FREE(*storage_list);
332 SAFE_FREE(*storage_id_list);
336 media_svc_debug("QEURY OK");
339 (*storage_list)[idx] = g_strdup((char *)sqlite3_column_text(sql_stmt, 0));
340 (*storage_id_list)[idx] = g_strdup((char *)sqlite3_column_text(sql_stmt, 1));
341 if (sqlite3_step(sql_stmt) != SQLITE_ROW)
346 if (cnt == idx + 1) {
348 media_svc_debug("OK");
352 for (i = 0; i < idx; i++) {
353 SAFE_FREE((*storage_list)[i]);
354 SAFE_FREE((*storage_id_list)[i]);
356 SAFE_FREE(*storage_list);
357 SAFE_FREE(*storage_id_list);
359 ret = MS_MEDIA_ERR_INTERNAL;
362 SQLITE3_FINALIZE(sql_stmt);