annotate libnumdb/refresh.c @ 171:4f1f3f799295

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