comparison libnumdb/refresh.c @ 1:6534965175dd

libnumdb ported over
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 13 Dec 2023 00:53:04 +0000
parents
children
comparison
equal deleted inserted replaced
0:159dd90eeafe 1:6534965175dd
1 /*
2 * Long-running ThemWi daemon processes need to be able to pick up updates
3 * to the number database without being restarted. Whenever they need to
4 * consult the number db when handling a new call setup or equivalent,
5 * they will call refresh_number_db(), which does a stat on the file,
6 * followed by a re-read if the file has changed.
7 */
8
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <stdio.h>
12 #include <stdint.h>
13 #include <stdlib.h>
14 #include <syslog.h>
15
16 #include <themwi/nanp/number_db_v2.h>
17 #include <themwi/nanp/number_lookup.h>
18
19 extern const char numdb_pathname[];
20 extern struct stat numdb_file_stat;
21 extern struct owned_number_rec *numdb_owned_numbers;
22 extern struct short_number_rec *numdb_short_numbers;
23
24 int refresh_number_db(void)
25 {
26 int rc;
27 struct stat st;
28
29 rc = stat(numdb_pathname, &st);
30 if (rc < 0) {
31 syslog(LOG_CRIT, "unable to stat %s for refresh: %m",
32 numdb_pathname);
33 return(-1);
34 }
35 if (st.st_mtime == numdb_file_stat.st_mtime &&
36 st.st_ctime == numdb_file_stat.st_ctime &&
37 st.st_size == numdb_file_stat.st_size)
38 return(0);
39 if (numdb_owned_numbers) {
40 free(numdb_owned_numbers);
41 numdb_owned_numbers = 0;
42 }
43 if (numdb_short_numbers) {
44 free(numdb_short_numbers);
45 numdb_short_numbers = 0;
46 }
47 rc = read_number_db();
48 if (rc < 0) {
49 syslog(LOG_CRIT, "error reading %s on refresh!",
50 numdb_pathname);
51 exit(1);
52 }
53 return(1);
54 }