Use transaction for scanner requests
[platform/core/multimedia/libmedia-service.git] / src / common / media-svc-storage.c
1 /*
2  * libmedia-service
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Hyunjun Ko <zzoon.ko@samsung.com>, Haejeong Kim <backto.kim@samsung.com>
7  *
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  */
21
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"
28
29 int _media_svc_check_storage(sqlite3 *handle, const char *storage_id, char **storage_path, int *validity)
30 {
31         int ret = MS_MEDIA_ERR_NONE;
32         sqlite3_stmt *sql_stmt = NULL;
33         char *sql = NULL;
34
35         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
36         media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
37         media_svc_retvm_if(validity == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "validity is NULL");
38
39         *storage_path = NULL;
40         *validity = 0;
41
42         sql = sqlite3_mprintf("SELECT storage_path, validity FROM %q WHERE storage_id=%Q", MEDIA_SVC_DB_TABLE_STORAGE, storage_id);
43         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
44         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
45
46         *storage_path = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
47         *validity = sqlite3_column_int(sql_stmt, 1);
48
49         SQLITE3_FINALIZE(sql_stmt);
50
51         return MS_MEDIA_ERR_NONE;
52 }
53
54 int _media_svc_append_storage(const char *storage_id, const char *storage_path, ms_user_storage_type_e storage_type, uid_t uid)
55 {
56         int ret = MS_MEDIA_ERR_NONE;
57         char *sql = sqlite3_mprintf("INSERT INTO %q (storage_id, storage_path, storage_type) values (%Q, %Q, %d);",
58                                                 MEDIA_SVC_DB_TABLE_STORAGE, storage_id, storage_path, storage_type);
59
60         ret = _media_svc_sql_query_direct(sql, uid);
61         SQLITE3_SAFE_FREE(sql);
62
63         return ret;
64 }
65
66 int _media_svc_update_storage_path(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
67 {
68         int ret = MS_MEDIA_ERR_NONE;
69         char *sql = NULL;
70         char *old_storage_path = NULL;
71         int validity = 0;
72
73         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
74         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
75
76         /*Get old path*/
77         ret = _media_svc_check_storage(handle, storage_id, &old_storage_path, &validity);
78         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
79
80         /*Storage table update*/
81         sql = sqlite3_mprintf("UPDATE %q SET storage_path=%Q WHERE storage_id=%Q", MEDIA_SVC_DB_TABLE_STORAGE, path, storage_id);
82         ret = _media_svc_sql_query_direct(sql, uid);
83         SQLITE3_SAFE_FREE(sql);
84         if (ret != MS_MEDIA_ERR_NONE) {
85                 g_free(old_storage_path);
86                 return ret;
87         }
88
89         /*Folder table update*/
90         sql = sqlite3_mprintf("UPDATE %q SET folder_path=REPLACE(folder_path, %Q, %Q) WHERE storage_uuid=%Q", MEDIA_SVC_DB_TABLE_FOLDER, old_storage_path, path, storage_id);
91         ret = _media_svc_sql_query_direct(sql, uid);
92         SQLITE3_SAFE_FREE(sql);
93         if (ret != MS_MEDIA_ERR_NONE) {
94                 g_free(old_storage_path);
95                 return ret;
96         }
97
98         /*Media table update*/
99         sql = sqlite3_mprintf("UPDATE %q SET media_path=REPLACE(media_path, %Q, %Q) WHERE storage_uuid=%Q", MEDIA_SVC_DB_TABLE_MEDIA, old_storage_path, path, storage_id);
100         ret = _media_svc_sql_query_direct(sql, uid);
101         SQLITE3_SAFE_FREE(sql);
102         g_free(old_storage_path);
103         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
104
105         return ret;
106 }
107
108 int __media_svc_delete_thumbnail(sqlite3 *handle)
109 {
110         int ret = MS_MEDIA_ERR_NONE;
111         char *sql = NULL;
112         sqlite3_stmt *sql_stmt = NULL;
113
114         sql = sqlite3_mprintf("SELECT media_thumbnail_path FROM %q WHERE validity=0 AND media_thumbnail_path is not null", MEDIA_SVC_DB_TABLE_MEDIA);
115         ret = _media_svc_sql_prepare_to_step_simple(handle, sql, &sql_stmt);
116         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
117
118         while (sqlite3_step(sql_stmt) == SQLITE_ROW)
119                 _media_svc_remove_file((const char *)sqlite3_column_text(sql_stmt, 0));
120
121         SQLITE3_FINALIZE(sql_stmt);
122
123         return ret;
124 }
125
126 int _media_svc_delete_invalid_storage(sqlite3 *handle, uid_t uid)
127 {
128         int ret = MS_MEDIA_ERR_NONE;
129         char *sql = NULL;
130
131         ret = __media_svc_delete_thumbnail(handle);
132         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Fail to remove thumbnail");
133
134         sql = sqlite3_mprintf("DELETE FROM %q WHERE validity=0;DELETE FROM %q WHERE validity=0;DELETE FROM %q WHERE validity=0;",
135                         MEDIA_SVC_DB_TABLE_MEDIA, MEDIA_SVC_DB_TABLE_STORAGE, MEDIA_SVC_DB_TABLE_FOLDER);
136
137         ret = _media_svc_sql_query_direct(sql, uid);
138         SQLITE3_SAFE_FREE(sql);
139
140         return ret;
141 }
142
143 int _media_svc_update_storage_validity(const char *storage_id, int validity, uid_t uid)
144 {
145         int ret = MS_MEDIA_ERR_NONE;
146         char *sql = NULL;
147
148         if (storage_id == NULL)
149                 sql = sqlite3_mprintf("UPDATE %q SET validity=%d", MEDIA_SVC_DB_TABLE_STORAGE, validity);
150         else
151                 sql = sqlite3_mprintf("UPDATE %q SET validity=%d WHERE storage_id=%Q", MEDIA_SVC_DB_TABLE_STORAGE, validity, storage_id);
152
153         ret = _media_svc_sql_query_direct(sql, uid);
154         SQLITE3_SAFE_FREE(sql);
155
156         return ret;
157 }
158
159 int _media_svc_get_storage_uuid(sqlite3 *handle, const char *path, char *storage_id, uid_t uid)
160 {
161         int ret = MS_MEDIA_ERR_NONE;
162         sqlite3_stmt *sql_stmt = NULL;
163         char *sql = NULL;
164         char *storage_path = NULL;
165         char *remain_path = NULL;
166         int remain_len = 0;
167         char *internal_path = NULL;
168
169         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
170
171         ret = ms_user_get_internal_root_path(uid, &internal_path);
172         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Fail to get root path");
173
174         if (STRING_VALID(internal_path) && strncmp(path, internal_path, strlen(internal_path)) == 0) {
175                 SAFE_STRLCPY(storage_id, MEDIA_SVC_DB_TABLE_MEDIA, MEDIA_SVC_UUID_SIZE+1);
176                 SAFE_FREE(internal_path);
177                 return MS_MEDIA_ERR_NONE;
178         }
179
180         SAFE_FREE(internal_path);
181
182         remain_path = strstr(path + (STRING_VALID(MEDIA_ROOT_PATH_USB) ? strlen(MEDIA_ROOT_PATH_USB) : 0) + 1, "/");
183         if (remain_path != NULL)
184                 remain_len = strlen(remain_path);
185
186         storage_path = strndup(path, strlen(path) - remain_len);
187
188         sql = sqlite3_mprintf("SELECT storage_id FROM '%q' WHERE validity=1 AND storage_path = '%q'", MEDIA_SVC_DB_TABLE_STORAGE, storage_path);
189
190         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
191         SAFE_FREE(storage_path);
192         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
193
194         if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 0)))
195                 SAFE_STRLCPY(storage_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE+1);
196
197         SQLITE3_FINALIZE(sql_stmt);
198
199         if (!STRING_VALID(storage_id)) {
200                 media_svc_error("Not found valid storage id [%s]", path);
201                 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
202         }
203
204         return ret;
205 }
206
207 int _media_svc_get_storage_path(sqlite3 *handle, GPtrArray **storage_path)
208 {
209         int ret = MS_MEDIA_ERR_NONE;
210         sqlite3_stmt *sql_stmt = NULL;
211         char *sql = NULL;
212         char *root_path = NULL;
213
214         media_svc_retvm_if(!storage_path, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
215
216         sql = sqlite3_mprintf("SELECT storage_path FROM %q WHERE validity=1", MEDIA_SVC_DB_TABLE_STORAGE);
217
218         ret = _media_svc_sql_prepare_to_step_simple(handle, sql, &sql_stmt);
219         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
220
221         while (sqlite3_step(sql_stmt) == SQLITE_ROW) {
222                 root_path = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
223                 g_ptr_array_add(*storage_path, root_path);
224         }
225
226         SQLITE3_FINALIZE(sql_stmt);
227
228         return ret;
229 }
230