FreeCalypso > hg > themwi-system-sw
comparison libnumdb/refresh.c @ 9:2cc790b66359
libnumdb: implement refresh
| author | Mychaela Falconia <falcon@freecalypso.org> |
|---|---|
| date | Sun, 26 Jun 2022 13:24:47 -0800 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 8:ffd48df829a7 | 9:2cc790b66359 |
|---|---|
| 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 #include "../include/number_db_file.h" | |
| 16 | |
| 17 extern char numdb_pathname[]; | |
| 18 extern struct stat numdb_file_stat; | |
| 19 extern uint64_t *numdb_owned_numbers; | |
| 20 extern struct short_number_map *numdb_short_numbers; | |
| 21 | |
| 22 refresh_number_db() | |
| 23 { | |
| 24 int rc; | |
| 25 struct stat st; | |
| 26 | |
| 27 rc = stat(numdb_pathname, &st); | |
| 28 if (rc < 0) { | |
| 29 syslog(LOG_CRIT, "unable to stat %s for refresh: %m", | |
| 30 numdb_pathname); | |
| 31 return(-1); | |
| 32 } | |
| 33 if (st.st_mtime == numdb_file_stat.st_mtime && | |
| 34 st.st_ctime == numdb_file_stat.st_ctime && | |
| 35 st.st_size == numdb_file_stat.st_size) | |
| 36 return(0); | |
| 37 if (numdb_owned_numbers) { | |
| 38 free(numdb_owned_numbers); | |
| 39 numdb_owned_numbers = 0; | |
| 40 } | |
| 41 if (numdb_short_numbers) { | |
| 42 free(numdb_short_numbers); | |
| 43 numdb_short_numbers = 0; | |
| 44 } | |
| 45 rc = read_number_db(); | |
| 46 if (rc < 0) { | |
| 47 syslog(LOG_CRIT, "error reading %s on refresh!", | |
| 48 numdb_pathname); | |
| 49 exit(1); | |
| 50 } | |
| 51 return(1); | |
| 52 } |
