MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / fetchMOTDFromCache

Function fetchMOTDFromCache

src/motd.cpp:53–80  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

51}
52
53static 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
82static void setMOTDCache(const char *sz)
83{

Callers 1

fetchMOTDFunction · 0.85

Calls 4

szMotdCachePathFunction · 0.85
sdsnewlenFunction · 0.85
sdsfreeFunction · 0.85
statClass · 0.70

Tested by

no test coverage detected