annotate libnumdb/refresh.c @ 3:5bf2648e5413

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