Initialize smart traffic control manager package
[platform/core/connectivity/stc-manager.git] / src / database / db-guard.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <errno.h>
18 #include <sys/stat.h>
19
20 #include "stc-db.h"
21 #include "table-statistics.h"
22
23 #define VCONF_KEY_DB_ENTRIES_COUNT "db/stc-manager/datausage_timer"
24 #define ENTRY_SIZE 128
25
26 /* one hour */
27 #define ERASE_TIMER_INTERVAL 3600
28 /* 40 days */
29 #define ERASE_INTERVAL 3600 * 24 * 40
30 /* 50 Mb */
31 #define DB_SIZE_THRESHOLD 1048576 * 50
32
33 static guint erase_timer = 0;
34 static int db_entries = 0;
35
36 static void __change_db_entries_num_num(int num)
37 {
38         __STC_LOG_FUNC_ENTER__;
39
40         db_entries += num;
41         if (vconf_set_int(VCONF_KEY_DB_ENTRIES_COUNT, db_entries))
42                 STC_LOGE("Failed to set new db entries number");
43
44         __STC_LOG_FUNC_EXIT__;
45 }
46
47 static void __check_erase_db_oversize(void)
48 {
49         __STC_LOG_FUNC_ENTER__;
50
51         struct stat db_stat = {0};
52         int del_entry = 0;
53
54         if (stat(DATABASE_FULL_PATH, &db_stat)) {
55                 STC_LOGE("Failed to get statistics for %s errno %d",
56                          DATABASE_FULL_PATH, errno);
57                 __STC_LOG_FUNC_EXIT__;
58                 return;
59         }
60
61         if (db_stat.st_size < DB_SIZE_THRESHOLD) {
62                 STC_LOGD("Db truncation isn't required!");
63                 __STC_LOG_FUNC_EXIT__;
64                 return;
65         }
66
67         /* get approximate number of entries for removing */
68         del_entry = (db_stat.st_size - DB_SIZE_THRESHOLD) / ENTRY_SIZE;
69         if (STC_ERROR_NONE !=
70             table_statistics_reset_first_n_entries(del_entry)) {
71                 STC_LOGE("Failed to remove first %d entries", del_entry);
72                 __STC_LOG_FUNC_EXIT__;
73                 return;
74         }
75
76         __change_db_entries_num_num(-del_entry);
77
78         __STC_LOG_FUNC_EXIT__;
79 }
80
81 static void __erase_old_entries(void)
82 {
83         __STC_LOG_FUNC_ENTER__;
84         char buffer[80] = {0, };
85         table_statistics_reset_rule rule = {
86                 .iftype = STC_IFACE_LAST_ELEM,
87         };
88         stc_db_tm_interval_s interval;
89         time_t until = time(0);
90
91         until -= ERASE_INTERVAL;
92
93         interval.from = 0;
94         interval.to = until;
95         rule.interval = &interval;
96
97         strftime(buffer, 80, "%x - %I:%M%p", localtime(&until));
98         STC_LOGD("Reset statistics till %s", buffer);
99
100         if (table_statistics_reset(&rule) != STC_ERROR_NONE)
101                 STC_LOGE("Failed to reset statistics");
102
103         __STC_LOG_FUNC_EXIT__;
104 }
105
106 static gboolean __erase_func_cb(void *user_data)
107 {
108         __STC_LOG_FUNC_ENTER__;
109
110         __check_erase_db_oversize();
111         __erase_old_entries();
112
113         /* we need to continue the timer */
114         __STC_LOG_FUNC_EXIT__;
115         return TRUE;
116 }
117
118 stc_error_e stc_init_db_guard(void)
119 {
120         __STC_LOG_FUNC_ENTER__;
121
122         erase_timer = g_timeout_add_seconds(ERASE_TIMER_INTERVAL,
123                                             __erase_func_cb, NULL);
124         if (erase_timer == 0) {
125                 STC_LOGE("Failed to create timer");
126                 __STC_LOG_FUNC_EXIT__;
127                 return STC_ERROR_FAIL;
128         }
129
130         __STC_LOG_FUNC_EXIT__;
131         return STC_ERROR_NONE;
132 }
133
134 void stc_deinit_db_guard(void)
135 {
136         __STC_LOG_FUNC_ENTER__;
137
138         if (erase_timer > 0) {
139                 g_source_remove(erase_timer);
140                 erase_timer = 0;
141         }
142
143         __STC_LOG_FUNC_EXIT__;
144 }