a076418b4d462a12db53967b29e0a659e017cbcd
[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  * 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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19
20 #include "media-util-err.h"
21 #include "media-svc-debug.h"
22 #include "media-svc-env.h"
23 #include "media-svc-db-utils.h"
24 #include "media-svc-util.h"
25 #include "media-svc-storage.h"
26
27 int _media_svc_check_storage(sqlite3 *handle, const char *storage_id, char **storage_path, int *validity)
28 {
29         int ret = MS_MEDIA_ERR_NONE;
30         sqlite3_stmt *sql_stmt = NULL;
31         char *sql = NULL;
32
33         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
34         media_svc_retvm_if(storage_path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_path is NULL");
35         media_svc_retvm_if(validity == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "validity is NULL");
36
37         *storage_path = NULL;
38         *validity = 0;
39
40         sql = sqlite3_mprintf("SELECT storage_path, validity FROM %q WHERE storage_id=%Q", DB_TABLE_STORAGE, storage_id);
41         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
42         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
43
44         *storage_path = g_strdup((const char *)sqlite3_column_text(sql_stmt, 0));
45         *validity = sqlite3_column_int(sql_stmt, 1);
46
47         SQLITE3_FINALIZE(sql_stmt);
48
49         return MS_MEDIA_ERR_NONE;
50 }
51
52 int _media_svc_append_storage(const char *storage_id, const char *storage_path, uid_t uid)
53 {
54         int ret = MS_MEDIA_ERR_NONE;
55         char *sql = sqlite3_mprintf("INSERT INTO %q (storage_id, storage_path) values (%Q, %Q);",
56                                                 DB_TABLE_STORAGE, storage_id, storage_path);
57
58         ret = _media_svc_sql_query_direct(sql, uid);
59         SQLITE3_SAFE_FREE(sql);
60
61         return ret;
62 }
63
64 int _media_svc_update_storage_path(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
65 {
66         int ret = MS_MEDIA_ERR_NONE;
67         char *sql = NULL;
68         char *old_storage_path = NULL;
69         int validity = 0;
70
71         media_svc_retvm_if(storage_id == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "storage_id is NULL");
72         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
73
74         /*Get old path*/
75         ret = _media_svc_check_storage(handle, storage_id, &old_storage_path, &validity);
76         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
77
78         /*Storage table update*/
79         sql = sqlite3_mprintf("UPDATE %q SET storage_path=%Q WHERE storage_id=%Q", DB_TABLE_STORAGE, path, storage_id);
80         ret = _media_svc_sql_query_direct(sql, uid);
81         SQLITE3_SAFE_FREE(sql);
82         if (ret != MS_MEDIA_ERR_NONE) {
83                 g_free(old_storage_path);
84                 return ret;
85         }
86
87         /*Folder table update*/
88         sql = 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);
89         ret = _media_svc_sql_query_direct(sql, uid);
90         SQLITE3_SAFE_FREE(sql);
91         if (ret != MS_MEDIA_ERR_NONE) {
92                 g_free(old_storage_path);
93                 return ret;
94         }
95
96         /*Media table update*/
97         sql = 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);
98         ret = _media_svc_sql_query_direct(sql, uid);
99         SQLITE3_SAFE_FREE(sql);
100         g_free(old_storage_path);
101         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
102
103         return ret;
104 }
105
106 static int __media_svc_delete_thumbnail(sqlite3 *handle)
107 {
108         int ret = MS_MEDIA_ERR_NONE;
109         char *sql = NULL;
110         sqlite3_stmt *sql_stmt = NULL;
111
112         sql = sqlite3_mprintf("SELECT media_thumbnail_path FROM %q WHERE validity=0 AND media_thumbnail_path is not null", DB_TABLE_MEDIA);
113         ret = _media_svc_sql_prepare_to_step_simple(handle, sql, &sql_stmt);
114         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
115
116         while (sqlite3_step(sql_stmt) == SQLITE_ROW)
117                 _media_svc_remove_file((const char *)sqlite3_column_text(sql_stmt, 0));
118
119         SQLITE3_FINALIZE(sql_stmt);
120
121         return ret;
122 }
123
124 int _media_svc_delete_invalid_storage(sqlite3 *handle, uid_t uid)
125 {
126         int ret = MS_MEDIA_ERR_NONE;
127         char *sql = NULL;
128
129         ret = __media_svc_delete_thumbnail(handle);
130         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Fail to remove thumbnail");
131
132         sql = sqlite3_mprintf("DELETE FROM %q WHERE validity=0;DELETE FROM %q WHERE validity=0;DELETE FROM %q WHERE validity=0;",
133                         DB_TABLE_MEDIA, DB_TABLE_STORAGE, DB_TABLE_FOLDER);
134
135         ret = _media_svc_sql_query_direct(sql, uid);
136         SQLITE3_SAFE_FREE(sql);
137
138         return ret;
139 }
140
141 int _media_svc_update_storage_validity(const char *storage_id, int validity, uid_t uid)
142 {
143         int ret = MS_MEDIA_ERR_NONE;
144         char *sql = NULL;
145
146         if (storage_id == NULL) {
147                 sql = 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';",
148                         DB_TABLE_STORAGE, validity, DB_TABLE_FOLDER, validity, DB_TABLE_MEDIA, validity);
149         } else {
150                 sql = 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;",
151                         DB_TABLE_STORAGE, validity, storage_id, DB_TABLE_FOLDER, validity, storage_id, DB_TABLE_MEDIA, 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 *internal_path = NULL;
165
166         media_svc_retvm_if(path == NULL, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
167
168         ret = ms_user_get_internal_root_path(uid, &internal_path);
169         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Fail to get root path");
170
171         if (STRING_VALID(internal_path) && strncmp(path, internal_path, strlen(internal_path)) == 0) {
172                 g_strlcpy(storage_id, DB_TABLE_MEDIA, MEDIA_SVC_UUID_SIZE + 1);
173                 g_free(internal_path);
174                 return MS_MEDIA_ERR_NONE;
175         }
176
177         g_free(internal_path);
178
179         sql = sqlite3_mprintf("SELECT storage_id FROM %q WHERE validity=1 AND instr(%Q, storage_path)", DB_TABLE_STORAGE, path);
180
181         ret = _media_svc_sql_prepare_to_step(handle, sql, &sql_stmt);
182         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
183
184         if (STRING_VALID((const char *)sqlite3_column_text(sql_stmt, 0)))
185                 g_strlcpy(storage_id, (const char *)sqlite3_column_text(sql_stmt, 0), MEDIA_SVC_UUID_SIZE + 1);
186
187         SQLITE3_FINALIZE(sql_stmt);
188
189         if (!STRING_VALID(storage_id)) {
190                 media_svc_error("Not found valid storage id [%s]", path);
191                 ret = MS_MEDIA_ERR_INVALID_PARAMETER;
192         }
193
194         return ret;
195 }