Set NULL after free
[platform/core/multimedia/libmedia-service.git] / src / 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 *stmt = NULL;
32         sql_autoptr q = NULL;
33
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");
37
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);
41
42         *storage_path = g_strdup((const char *)sqlite3_column_text(stmt, 0));
43         *validity = sqlite3_column_int(stmt, 1);
44
45         sqlite3_finalize(stmt);
46
47         return MS_MEDIA_ERR_NONE;
48 }
49
50 int _media_svc_append_storage(const char *storage_id, const char *storage_path, uid_t uid)
51 {
52         sql_autoptr q = sqlite3_mprintf("INSERT INTO %q (storage_id, storage_path) values (%Q, %Q);",
53                                 DB_TABLE_STORAGE, storage_id, storage_path);
54
55         return _media_svc_sql_query_direct(q, uid);
56 }
57
58 int _media_svc_update_storage_path(sqlite3 *handle, const char *storage_id, const char *path, uid_t uid)
59 {
60         int ret = MS_MEDIA_ERR_NONE;
61         char *q = NULL;
62         g_autofree gchar *old_storage_path = NULL;
63         int validity = 0;
64
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");
67
68         /*Get old path*/
69         ret = _media_svc_check_storage(handle, storage_id, &old_storage_path, &validity);
70         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
71
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);
75         sqlite3_free(q);
76         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
77
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);
81         sqlite3_free(q);
82         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
83
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);
87         sqlite3_free(q);
88         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
89
90         return ret;
91 }
92
93 static int __media_svc_delete_thumbnail(sqlite3 *handle)
94 {
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;
98
99         ret = _media_svc_get_result(handle, q, &stmt);
100         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
101
102         while (sqlite3_step(stmt) == SQLITE_ROW)
103                 _media_svc_remove_file((const char *)sqlite3_column_text(stmt, 0));
104
105         sqlite3_finalize(stmt);
106
107         return ret;
108 }
109
110 int _media_svc_delete_invalid_storage(sqlite3 *handle, uid_t uid)
111 {
112         int ret = MS_MEDIA_ERR_NONE;
113         sql_autoptr q = NULL;
114
115         ret = __media_svc_delete_thumbnail(handle);
116         media_svc_retvm_if(ret != MS_MEDIA_ERR_NONE, ret, "Fail to remove thumbnail");
117
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);
120
121         return _media_svc_sql_query_direct(q, uid);
122 }
123
124 int _media_svc_update_storage_validity(const char *storage_id, int validity, uid_t uid)
125 {
126         sql_autoptr q = NULL;
127
128         if (!storage_id)
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);
130         else
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);
132
133         return _media_svc_sql_query_direct(q, uid);
134 }
135
136 int _media_svc_get_storage_uuid(sqlite3 *handle, const char *path, char *storage_id, uid_t uid)
137 {
138         int ret = MS_MEDIA_ERR_NONE;
139         sqlite3_stmt *stmt = NULL;
140         sql_autoptr q = NULL;
141         g_autofree gchar *internal_path = NULL;
142
143         media_svc_retvm_if(!path, MS_MEDIA_ERR_INVALID_PARAMETER, "path is NULL");
144
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");
147
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;
151         }
152
153         q = sqlite3_mprintf("SELECT storage_id FROM %q WHERE validity=1 AND instr(%Q, storage_path)", DB_TABLE_STORAGE, path);
154
155         ret = _media_svc_get_result_with_check_record(handle, q, &stmt);
156         media_svc_retv_if(ret != MS_MEDIA_ERR_NONE, ret);
157
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);
160
161         sqlite3_finalize(stmt);
162
163         if (!STRING_VALID(storage_id)) {
164                 media_svc_error("Not found valid storage id [%s]", path);
165                 return MS_MEDIA_ERR_INVALID_PARAMETER;
166         }
167
168         return MS_MEDIA_ERR_NONE;
169 }