| 184 | } |
| 185 | |
| 186 | Result Open(NewParams* params, HCache* cache) |
| 187 | { |
| 188 | const char* path = params->m_Path; |
| 189 | if (dmSys::Exists(path)) |
| 190 | { |
| 191 | if (dmSys::RESULT_OK != dmSys::IsDir(path)) |
| 192 | { |
| 193 | dmLogError("Unable to use '%s' as http cache directory. Path exists and is not a directory.", path); |
| 194 | return RESULT_INVALID_PATH; |
| 195 | } |
| 196 | } |
| 197 | else |
| 198 | { |
| 199 | dmSys::Result r = dmSys::Mkdir(path, 0755); |
| 200 | if (r != dmSys::RESULT_OK) |
| 201 | { |
| 202 | dmLogError("Unable to create directory '%s' (%d)", path, r); |
| 203 | return RESULT_IO_ERROR; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | Cache* c = new Cache(path, params->m_MaxCacheEntryAge * 1000000); |
| 208 | c->m_CacheCreatorsPool.SetCapacity(MAX_CACHE_CREATORS); |
| 209 | c->m_CacheCreators.SetCapacity(MAX_CACHE_CREATORS); |
| 210 | c->m_CacheCreators.SetSize(MAX_CACHE_CREATORS); |
| 211 | |
| 212 | for (uint32_t i = 0; i < MAX_CACHE_CREATORS; ++i) |
| 213 | { |
| 214 | CacheCreator* h = &c->m_CacheCreators[i]; |
| 215 | memset(h, 0, sizeof(*h)); |
| 216 | } |
| 217 | |
| 218 | char cache_file[DMPATH_MAX_PATH]; |
| 219 | dmSnPrintf(cache_file, sizeof(cache_file), "%s/%s", path, "index"); |
| 220 | FILE* f = fopen(cache_file, "rb"); |
| 221 | if (f) |
| 222 | { |
| 223 | fseek(f, 0, SEEK_END); |
| 224 | size_t size = ftell(f); |
| 225 | fseek(f, 0, SEEK_SET); |
| 226 | void* buffer = malloc(size); |
| 227 | (void)fread(buffer, 1, size, f); |
| 228 | IndexHeader* header = (IndexHeader*) buffer; |
| 229 | if (size < (sizeof(IndexHeader)) || !IsValidHeader(header)) |
| 230 | { |
| 231 | dmLogError("Invalid cache index file '%s'. Removing file.", cache_file); |
| 232 | // We remove the file and return RESULT_OK |
| 233 | dmSys::Unlink(cache_file); |
| 234 | } |
| 235 | else |
| 236 | { |
| 237 | uint64_t checksum = dmHashBuffer64((void*) (((uintptr_t) buffer) + sizeof(IndexHeader)), size - sizeof(IndexHeader)); |
| 238 | if (checksum != header->m_Checksum) |
| 239 | { |
| 240 | dmLogError("Corrupt cache index file '%s'. Removing file.", cache_file); |
| 241 | } |
| 242 | else |
| 243 | { |
nothing calls this directly
no test coverage detected