| 559 | } |
| 560 | |
| 561 | Result End(HCache cache, HCacheCreator cache_creator) |
| 562 | { |
| 563 | dmMutex::ScopedLock lock(cache->m_Mutex); |
| 564 | |
| 565 | assert(cache_creator->m_File && cache_creator->m_Filename); |
| 566 | uint64_t identifier_hash = cache_creator->m_IdentifierHash; |
| 567 | |
| 568 | fclose(cache_creator->m_File); |
| 569 | cache_creator->m_File = 0; |
| 570 | |
| 571 | uint64_t uri_hash = cache_creator->m_UriHash; |
| 572 | Entry* entry = cache->m_CacheTable.Get(uri_hash); |
| 573 | assert(entry); |
| 574 | |
| 575 | if (cache_creator->m_Error) |
| 576 | { |
| 577 | dmSys::Unlink(cache_creator->m_Filename); |
| 578 | FreeCacheCreator(cache, cache_creator); |
| 579 | cache->m_CacheTable.Erase(uri_hash); |
| 580 | return RESULT_IO_ERROR; |
| 581 | } |
| 582 | |
| 583 | char path[DMPATH_MAX_PATH]; |
| 584 | ContentFilePath(cache, identifier_hash, path, sizeof(path)); |
| 585 | if (dmSys::Exists(path)) |
| 586 | { |
| 587 | dmSys::Result r = dmSys::Unlink(path); |
| 588 | if (r != dmSys::RESULT_OK) |
| 589 | { |
| 590 | dmLogError("Unable to remove cache file: %s", path); |
| 591 | FreeCacheCreator(cache, cache_creator); |
| 592 | cache->m_CacheTable.Erase(uri_hash); |
| 593 | return RESULT_IO_ERROR; |
| 594 | } |
| 595 | } |
| 596 | else |
| 597 | { |
| 598 | // Modify path and remove last part of hash temporarily |
| 599 | // ie, .../5d/a7b8fa56d18877 to .../5d |
| 600 | char* last_slash = strrchr(path, '/'); |
| 601 | char save = *last_slash; |
| 602 | *last_slash = '\0'; |
| 603 | if (!dmSys::Exists(path)) |
| 604 | { |
| 605 | dmSys::Result r = dmSys::Mkdir(path, 0755); |
| 606 | if (r != dmSys::RESULT_OK) |
| 607 | { |
| 608 | dmLogError("Unable to create directory '%s'", path); |
| 609 | FreeCacheCreator(cache, cache_creator); |
| 610 | cache->m_CacheTable.Erase(uri_hash); |
| 611 | return RESULT_IO_ERROR; |
| 612 | } |
| 613 | } |
| 614 | *last_slash = save; |
| 615 | } |
| 616 | |
| 617 | assert(entry->m_WriteLock); |
| 618 | assert(entry->m_Info.m_IdentifierHash == identifier_hash); |