4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 #include <media-util-user.h>
22 #include "media-svc-debug.h"
23 #include "media-svc-env.h"
24 #include "media-svc-db-utils.h"
25 #include "media-svc-util.h"
26 #include "media-svc-storage.h"
28 int _media_svc_check_storage(sqlite3 *handle, const char *storage_id, char **storage_path, int *validity)
30 int ret = MS_MEDIA_ERR_NONE;
31 sqlite3_stmt *stmt = NULL;
34 media_svc_retvm_if(!storage_id, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
35 media_svc_retvm_if(!storage_path, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
36 media_svc_retvm_if(!validity, MS_MEDIA_ERR_INVALID_PARAMETER, "validity is NULL");
38 q = sqlite3_mprintf("SELECT storage_path, validity FROM %q WHERE storage_id=%Q", DB_TABLE_STORAGE, storage_id);
39 ret = _media_svc_get_result_with_check_record(handle, q, &stmt);
40 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
42 *storage_path = g_strdup((const char *)sqlite3_column_text(stmt, 0));
43 *validity = sqlite3_column_int(stmt, 1);
45 sqlite3_finalize(stmt);
47 return MS_MEDIA_ERR_NONE;
50 int _media_svc_append_storage(const char *storage_id, const char *storage_path, uid_t uid)
52 sql_autoptr q = sqlite3_mprintf("INSERT INTO %q (storage_id, storage_path) values (%Q, %Q);",
53 DB_TABLE_STORAGE, storage_id, storage_path);
55 return _media_svc_sql_query_direct(q, uid);
58 int _media_svc_update_storage_path(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
60 int ret = MS_MEDIA_ERR_NONE;
62 g_autofree gchar *old_storage_path = NULL;
65 media_svc_retvm_if(!storage_id, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
66 media_svc_retvm_if(!path, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
69 ret = _media_svc_check_storage(handle, storage_id, &old_storage_path, &validity);
70 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
72 /*Storage table update*/
73 q = sqlite3_mprintf("UPDATE %q SET storage_path=%Q WHERE storage_id=%Q", DB_TABLE_STORAGE, path, storage_id);
74 ret = _media_svc_sql_query_direct(q, uid);
76 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
78 /*Folder table update*/
79 q = sqlite3_mprintf("UPDATE %q SET folder_path=REPLACE(folder_path, %Q, %Q) WHERE storage_uuid=%Q", DB_TABLE_FOLDER, old_storage_path, path, storage_id);
80 ret = _media_svc_sql_query_direct(q, uid);
82 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
84 /*Media table update*/
85 q = sqlite3_mprintf("UPDATE %q SET media_path=REPLACE(media_path, %Q, %Q) WHERE storage_uuid=%Q", DB_TABLE_MEDIA, old_storage_path, path, storage_id);
86 ret = _media_svc_sql_query_direct(q, uid);
88 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
93 static int __media_svc_delete_thumbnail(sqlite3 *handle)
95 int ret = MS_MEDIA_ERR_NONE;
96 const char *q = "SELECT media_thumbnail_path FROM media WHERE validity=0 AND media_thumbnail_path IS NOT NULL;";
97 sqlite3_stmt *stmt = NULL;
99 ret = _media_svc_get_result(handle, q, &stmt);
100 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
102 while (sqlite3_step(stmt) == SQLITE_ROW)
103 _media_svc_remove_file((const char *)sqlite3_column_text(stmt, 0));
105 sqlite3_finalize(stmt);
110 int _media_svc_delete_invalid_storage(sqlite3 *handle, uid_t uid)
112 int ret = MS_MEDIA_ERR_NONE;
113 sql_autoptr q = NULL;
115 ret = __media_svc_delete_thumbnail(handle);
116 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Fail to remove thumbnail");
118 q = sqlite3_mprintf("DELETE FROM %q WHERE validity=0;DELETE FROM %q WHERE validity=0;DELETE FROM %q WHERE validity=0;",
119 DB_TABLE_MEDIA, DB_TABLE_STORAGE, DB_TABLE_FOLDER);
121 return _media_svc_sql_query_direct(q, uid);
124 int _media_svc_update_storage_validity(const char *storage_id, int validity, uid_t uid)
126 sql_autoptr q = NULL;
129 q = sqlite3_mprintf("UPDATE %q SET validity=%d;UPDATE %q SET validity=%d WHERE storage_uuid IS NOT 'media';UPDATE %q SET validity=%d WHERE storage_uuid IS NOT 'media';", DB_TABLE_STORAGE, validity, DB_TABLE_FOLDER, validity, DB_TABLE_MEDIA, validity);
131 q = sqlite3_mprintf("UPDATE %q SET validity=%d WHERE storage_id=%Q;UPDATE %q SET validity=%d WHERE storage_uuid=%Q;UPDATE %q SET validity=%d WHERE storage_uuid=%Q;", DB_TABLE_STORAGE, validity, storage_id, DB_TABLE_FOLDER, validity, storage_id, DB_TABLE_MEDIA, validity, storage_id);
133 return _media_svc_sql_query_direct(q, uid);
136 int _media_svc_get_storage_uuid(sqlite3 *handle, const char *path, char *storage_id, uid_t uid)
138 int ret = MS_MEDIA_ERR_NONE;
139 sqlite3_stmt *stmt = NULL;
140 sql_autoptr q = NULL;
141 g_autofree gchar *internal_path = NULL;
143 media_svc_retvm_if(!path, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
145 ret = ms_user_get_internal_root_path(uid, &internal_path);
146 media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Fail to get root path");
148 if (STRING_VALID(internal_path) && strncmp(path, internal_path, strlen(internal_path)) == 0) {
149 g_strlcpy(storage_id, DB_TABLE_MEDIA, MEDIA_SVC_UUID_SIZE + 1);
150 return MS_MEDIA_ERR_NONE;
153 q = sqlite3_mprintf("SELECT storage_id FROM %q WHERE validity=1 AND instr(%Q, storage_path)", DB_TABLE_STORAGE, path);
155 ret = _media_svc_get_result_with_check_record(handle, q, &stmt);
156 media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
158 if (STRING_VALID((const char *)sqlite3_column_text(stmt, 0)))
159 g_strlcpy(storage_id, (const char *)sqlite3_column_text(stmt, 0), MEDIA_SVC_UUID_SIZE + 1);
161 sqlite3_finalize(stmt);
163 if (!STRING_VALID(storage_id)) {
164 media_svc_error("Not found valid storage id [%s]", path);
165 return MS_MEDIA_ERR_INVALID_PARAMETER;
168 return MS_MEDIA_ERR_NONE;