check integrity in media server
[platform/core/multimedia/media-server.git] / src / server / media-server-db-manage.c
1 /*
2  * Media Server
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Yong Yeon Kim <yy9875.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 <gio/gio.h>
23
24 #include "media-util.h"
25 #include "media-common-db-svc.h"
26 #include "media-common-system.h"
27 #include "media-common-utils.h"
28 #include "media-server-ipc.h"
29 #include "media-server-dbg.h"
30 #include "media-server-scanner.h"
31 #include "media-server-thumb.h"
32 #include "media-server-db-manage.h"
33
34 int ms_reset_mediadb(uid_t uid)
35 {
36         int ret = MS_MEDIA_ERR_NONE;
37         GFile *source = g_file_new_for_path(MS_DUMMY_MEDIA_DB_PATH);
38         GFile *destination = NULL;
39         GError *error = NULL;
40         gboolean result = FALSE;
41         char *db_path = NULL;
42
43         MS_DBG_ERR("[MEDIA DB RESET START]");
44
45         ret = ms_user_get_media_db_path(uid, &db_path);
46         if ((ret == MS_MEDIA_ERR_NONE) && (db_path != NULL))
47                 destination =  g_file_new_for_path(db_path);
48
49         if (source == NULL || destination == NULL) {
50                 MS_DBG_ERR("could not open file");
51
52                 ret = MS_MEDIA_ERR_INTERNAL;
53         } else {
54                 result = g_file_copy(source, destination, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &error);
55                 if (!result) {
56                         MS_DBG_ERR("g_file_copy failed: %s", error ? error->message : "none");
57                         ret = MS_MEDIA_ERR_INTERNAL;
58                 }
59         }
60
61         if (source)
62                 g_object_unref(source);
63         if (destination)
64                 g_object_unref(destination);
65         if (error)
66                 g_error_free(error);
67
68         MS_SAFE_FREE(db_path);
69
70         MS_DBG_ERR("[MEDIA DB RESET END]");
71
72         return ret;
73 }
74
75 static int __ms_remake_mediadb(uid_t uid)
76 {
77         int err = MS_MEDIA_ERR_NONE;
78         void **handle = NULL;
79
80         MS_DBG_ERR("THE SIZE OF MEDIA DB REACH THE LIMIT. RESET MEDIA DB.");
81
82         /*write read schema*/
83         /*load functions from plusin(s)*/
84         err = ms_load_functions();
85         if (err != MS_MEDIA_ERR_NONE) {
86                 MS_DBG_ERR("function load failed [%d]", err);
87                 return err;
88         }
89 #ifdef _USE_TVPD_MODE
90         ms_config_set_int(MS_DB_RESET, 1);
91 #endif
92         ms_reset_mediadb(uid);
93
94         ms_connect_db(&handle, uid);
95
96         MS_DBG_WARN("START WRITE SCHEMA");
97         if (ms_check_db_upgrade(handle, uid) != MS_MEDIA_ERR_NONE)
98                 MS_DBG_ERR("ms_check_db_upgrade fail");
99
100         MS_DBG_WARN("END WRITE SCHEMA");
101
102         /*disconnect form media db*/
103         if (handle) ms_disconnect_db(&handle);
104
105         /*unload functions*/
106         ms_unload_functions();
107
108 #ifdef _USE_TVPD_MODE
109         ms_config_set_int(MS_DB_RESET, 0);
110 #endif
111         return MS_MEDIA_ERR_NONE;
112 }
113
114 int ms_check_mediadb(uid_t uid, bool *is_reset)
115 {
116         double db_size = 0.0;
117
118         ms_check_size_mediadb(uid, &db_size);
119
120         MS_DBG_WARN("[DB SIZE : %lf] [LIMIT1 : %lf] [LIMIT2 : %lf]", db_size, MEDIA_DB_SIZE_LIMIT_1, MEDIA_DB_SIZE_LIMIT_2);
121         if (db_size > MEDIA_DB_SIZE_LIMIT_2) {
122                 MS_DBG_ERR("THE SIZE OF MEDIA DB REACH THE LIMIT. RESET MEDIA DB.");
123                 MS_DBG_ERR("[DB SIZE : %lf] [LIMIT1 : %lf] [LIMIT2 : %lf]", db_size, MEDIA_DB_SIZE_LIMIT_1, MEDIA_DB_SIZE_LIMIT_2);
124                 __ms_remake_mediadb(uid);
125                 *is_reset = TRUE;
126         }
127
128         return MS_MEDIA_ERR_NONE;
129 }
130
131 int ms_check_corrupt_mediadb(void)
132 {
133         MediaDBHandle *db_handle = NULL;
134         uid_t uid = MEDIA_DEFAULT_UID;
135
136         ms_sys_get_uid(&uid);
137
138         if (media_db_connect(&db_handle, uid, TRUE) != MS_MEDIA_ERR_NONE) {
139                 MS_DBG_ERR("Failed to connect DB");
140                 return MS_MEDIA_ERR_DB_CONNECT_FAIL;
141         }
142
143         if (media_db_check_integrity(db_handle) != MS_MEDIA_ERR_NONE) {
144                 MS_DBG_ERR("check interity failed");
145                 media_db_disconnect(db_handle);
146                 return MS_MEDIA_ERR_DB_CORRUPT;
147         }
148
149         /* Disconnect DB*/
150         media_db_disconnect(db_handle);
151
152         return MS_MEDIA_ERR_NONE;
153 }
154