4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Yong Yeon Kim <yy9875.kim@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
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"
34 int ms_reset_mediadb(uid_t uid)
36 int ret = MS_MEDIA_ERR_NONE;
37 GFile *source = g_file_new_for_path(MS_DUMMY_MEDIA_DB_PATH);
38 GFile *destination = NULL;
40 gboolean result = FALSE;
43 MS_DBG_ERR("[MEDIA DB RESET START]");
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);
49 if (source == NULL || destination == NULL) {
50 MS_DBG_ERR("could not open file");
52 ret = MS_MEDIA_ERR_INTERNAL;
54 result = g_file_copy(source, destination, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &error);
56 MS_DBG_ERR("g_file_copy failed: %s", error ? error->message : "none");
57 ret = MS_MEDIA_ERR_INTERNAL;
62 g_object_unref(source);
64 g_object_unref(destination);
68 MS_SAFE_FREE(db_path);
70 MS_DBG_ERR("[MEDIA DB RESET END]");
75 static int __ms_remake_mediadb(uid_t uid)
77 int err = MS_MEDIA_ERR_NONE;
80 MS_DBG_ERR("THE SIZE OF MEDIA DB REACH THE LIMIT. RESET MEDIA DB.");
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);
90 ms_config_set_int(MS_DB_RESET, 1);
92 ms_reset_mediadb(uid);
94 ms_connect_db(&handle, uid);
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");
100 MS_DBG_WARN("END WRITE SCHEMA");
102 /*disconnect form media db*/
103 if (handle) ms_disconnect_db(&handle);
106 ms_unload_functions();
108 #ifdef _USE_TVPD_MODE
109 ms_config_set_int(MS_DB_RESET, 0);
111 return MS_MEDIA_ERR_NONE;
114 int ms_check_mediadb(uid_t uid, bool *is_reset)
116 double db_size = 0.0;
118 ms_check_size_mediadb(uid, &db_size);
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);
128 return MS_MEDIA_ERR_NONE;
131 int ms_check_corrupt_mediadb(void)
133 MediaDBHandle *db_handle = NULL;
134 uid_t uid = MEDIA_DEFAULT_UID;
136 ms_sys_get_uid(&uid);
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;
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;
150 media_db_disconnect(db_handle);
152 return MS_MEDIA_ERR_NONE;