Add initial source codes for gtest
[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 //LCOV_EXCL_START
37 static void __change_db_entries_num_num(int num)
38 {
39         __STC_LOG_FUNC_ENTER__;
40
41         db_entries += num;
42         if (vconf_set_int(VCONF_KEY_DB_ENTRIES_COUNT, db_entries))
43                 STC_LOGE("Failed to set new db entries number");
44
45         __STC_LOG_FUNC_EXIT__;
46 }
47
48 static void __check_erase_db_oversize(void)
49 {
50         __STC_LOG_FUNC_ENTER__;
51
52         struct stat db_stat = {0};
53         int del_entry = 0;
54
55         if (stat(DATABASE_FULL_PATH, &db_stat)) {
56                 STC_LOGE("Failed to get statistics for %s errno %d",
57                          DATABASE_FULL_PATH, errno);
58                 __STC_LOG_FUNC_EXIT__;
59                 return;
60         }
61
62         if (db_stat.st_size < DB_SIZE_THRESHOLD) {
63                 STC_LOGD("Db truncation isn't required!");
64                 __STC_LOG_FUNC_EXIT__;
65                 return;
66         }
67
68         /* get approximate number of entries for removing */
69         del_entry = (db_stat.st_size - DB_SIZE_THRESHOLD) / ENTRY_SIZE;
70         if (STC_ERROR_NONE !=
71             table_statistics_reset_first_n_entries(del_entry)) {
72                 STC_LOGE("Failed to remove first %d entries", del_entry);
73                 __STC_LOG_FUNC_EXIT__;
74                 return;
75         }
76
77         __change_db_entries_num_num(-del_entry);
78
79         __STC_LOG_FUNC_EXIT__;
80 }
81
82 static void __erase_old_entries(void)
83 {
84         __STC_LOG_FUNC_ENTER__;
85         char buffer[80] = {0, };
86         table_statistics_reset_rule rule = {
87                 .iftype = STC_IFACE_LAST_ELEM,
88         };
89         stc_db_tm_interval_s interval;
90         time_t until = time(0);
91         struct tm result = {0, };
92
93         until -= ERASE_INTERVAL;
94
95         interval.from = 0;
96         interval.to = until;
97         rule.interval = &interval;
98
99         strftime(buffer, 80, "%x - %I:%M%p", localtime_r(&until, &result));
100         STC_LOGD("Reset statistics till %s", buffer);
101
102         if (table_statistics_reset(&rule) != STC_ERROR_NONE)
103                 STC_LOGE("Failed to reset statistics");
104
105         __STC_LOG_FUNC_EXIT__;
106 }
107
108 static gboolean __erase_func_cb(void *user_data)
109 {
110         __STC_LOG_FUNC_ENTER__;
111
112         __check_erase_db_oversize();
113         __erase_old_entries();
114
115         /* we need to continue the timer */
116         __STC_LOG_FUNC_EXIT__;
117         return TRUE;
118 }
119 //LCOV_EXCL_STOP
120
121 stc_error_e stc_init_db_guard(void)
122 {
123         __STC_LOG_FUNC_ENTER__;
124
125         erase_timer = g_timeout_add_seconds(ERASE_TIMER_INTERVAL,
126                                             __erase_func_cb, NULL);
127         if (erase_timer == 0) {
128                 STC_LOGE("Failed to create timer"); //LCOV_EXCL_LINE
129                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
130                 return STC_ERROR_FAIL; //LCOV_EXCL_LINE
131         }
132
133         __STC_LOG_FUNC_EXIT__;
134         return STC_ERROR_NONE;
135 }
136
137 void stc_deinit_db_guard(void)
138 {
139         __STC_LOG_FUNC_ENTER__;
140
141         if (erase_timer > 0) {
142                 g_source_remove(erase_timer);
143                 erase_timer = 0;
144         }
145
146         __STC_LOG_FUNC_EXIT__;
147 }