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