| 51 | } |
| 52 | |
| 53 | static char *fetchMOTDFromCache() |
| 54 | { |
| 55 | struct stat attrib; |
| 56 | if (stat(szMotdCachePath(), &attrib) != 0) |
| 57 | return NULL; |
| 58 | time_t t = attrib.st_mtim.tv_sec; |
| 59 | time_t now = time(NULL); |
| 60 | if ((now - t) < 14400) |
| 61 | { |
| 62 | // If our cache was updated no more than 4 hours ago use it instead of fetching the MOTD |
| 63 | FILE *pf = fopen(szMotdCachePath(), "rb"); |
| 64 | if (pf == NULL) |
| 65 | return NULL; |
| 66 | fseek(pf, 0L, SEEK_END); |
| 67 | long cb = ftell(pf); |
| 68 | fseek(pf, 0L, SEEK_SET); // rewind |
| 69 | sds str = sdsnewlen(NULL, cb); |
| 70 | size_t cbRead = fread(str, 1, cb, pf); |
| 71 | fclose(pf); |
| 72 | if ((long)cbRead != cb) |
| 73 | { |
| 74 | sdsfree(str); |
| 75 | return NULL; |
| 76 | } |
| 77 | return str; |
| 78 | } |
| 79 | return NULL; |
| 80 | } |
| 81 | |
| 82 | static void setMOTDCache(const char *sz) |
| 83 | { |
no test coverage detected