| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 }; | 59 }; |
| 60 | 60 |
| 61 /////////////////////////////////////////////////////////////////////////////// | 61 /////////////////////////////////////////////////////////////////////////////// |
| 62 // DiskCache | 62 // DiskCache |
| 63 /////////////////////////////////////////////////////////////////////////////// | 63 /////////////////////////////////////////////////////////////////////////////// |
| 64 | 64 |
| 65 DiskCache::DiskCache() : max_cache_(0), total_size_(0), total_accessors_(0) { | 65 DiskCache::DiskCache() : max_cache_(0), total_size_(0), total_accessors_(0) { |
| 66 } | 66 } |
| 67 | 67 |
| 68 DiskCache::~DiskCache() { | 68 DiskCache::~DiskCache() { |
| 69 ASSERT(0 == total_accessors_); | 69 RTC_DCHECK(0 == total_accessors_); |
| 70 } | 70 } |
| 71 | 71 |
| 72 bool DiskCache::Initialize(const std::string& folder, size_t size) { | 72 bool DiskCache::Initialize(const std::string& folder, size_t size) { |
| 73 if (!folder_.empty() || !Filesystem::CreateFolder(folder)) | 73 if (!folder_.empty() || !Filesystem::CreateFolder(folder)) |
| 74 return false; | 74 return false; |
| 75 | 75 |
| 76 folder_ = folder; | 76 folder_ = folder; |
| 77 max_cache_ = size; | 77 max_cache_ = size; |
| 78 ASSERT(0 == total_size_); | 78 RTC_DCHECK(0 == total_size_); |
| 79 | 79 |
| 80 if (!InitializeEntries()) | 80 if (!InitializeEntries()) |
| 81 return false; | 81 return false; |
| 82 | 82 |
| 83 return CheckLimit(); | 83 return CheckLimit(); |
| 84 } | 84 } |
| 85 | 85 |
| 86 bool DiskCache::Purge() { | 86 bool DiskCache::Purge() { |
| 87 if (folder_.empty()) | 87 if (folder_.empty()) |
| 88 return false; | 88 return false; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 114 } | 114 } |
| 115 | 115 |
| 116 StreamInterface* DiskCache::WriteResource(const std::string& id, size_t index) { | 116 StreamInterface* DiskCache::WriteResource(const std::string& id, size_t index) { |
| 117 Entry* entry = GetOrCreateEntry(id, false); | 117 Entry* entry = GetOrCreateEntry(id, false); |
| 118 if (LS_LOCKED != entry->lock_state) | 118 if (LS_LOCKED != entry->lock_state) |
| 119 return NULL; | 119 return NULL; |
| 120 | 120 |
| 121 size_t previous_size = 0; | 121 size_t previous_size = 0; |
| 122 std::string filename(IdToFilename(id, index)); | 122 std::string filename(IdToFilename(id, index)); |
| 123 FileStream::GetSize(filename, &previous_size); | 123 FileStream::GetSize(filename, &previous_size); |
| 124 ASSERT(previous_size <= entry->size); | 124 RTC_DCHECK(previous_size <= entry->size); |
| 125 if (previous_size > entry->size) { | 125 if (previous_size > entry->size) { |
| 126 previous_size = entry->size; | 126 previous_size = entry->size; |
| 127 } | 127 } |
| 128 | 128 |
| 129 std::unique_ptr<FileStream> file(new FileStream); | 129 std::unique_ptr<FileStream> file(new FileStream); |
| 130 if (!file->Open(filename, "wb", NULL)) { | 130 if (!file->Open(filename, "wb", NULL)) { |
| 131 LOG_F(LS_ERROR) << "Couldn't create cache file"; | 131 LOG_F(LS_ERROR) << "Couldn't create cache file"; |
| 132 return NULL; | 132 return NULL; |
| 133 } | 133 } |
| 134 | 134 |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 return success; | 214 return success; |
| 215 } | 215 } |
| 216 | 216 |
| 217 bool DiskCache::CheckLimit() { | 217 bool DiskCache::CheckLimit() { |
| 218 #if !defined(NDEBUG) | 218 #if !defined(NDEBUG) |
| 219 // Temporary check to make sure everything is working correctly. | 219 // Temporary check to make sure everything is working correctly. |
| 220 size_t cache_size = 0; | 220 size_t cache_size = 0; |
| 221 for (EntryMap::iterator it = map_.begin(); it != map_.end(); ++it) { | 221 for (EntryMap::iterator it = map_.begin(); it != map_.end(); ++it) { |
| 222 cache_size += it->second.size; | 222 cache_size += it->second.size; |
| 223 } | 223 } |
| 224 ASSERT(cache_size == total_size_); | 224 RTC_DCHECK(cache_size == total_size_); |
| 225 #endif | 225 #endif |
| 226 | 226 |
| 227 // TODO: Replace this with a non-brain-dead algorithm for clearing out the | 227 // TODO: Replace this with a non-brain-dead algorithm for clearing out the |
| 228 // oldest resources... something that isn't O(n^2) | 228 // oldest resources... something that isn't O(n^2) |
| 229 while (total_size_ > max_cache_) { | 229 while (total_size_ > max_cache_) { |
| 230 EntryMap::iterator oldest = map_.end(); | 230 EntryMap::iterator oldest = map_.end(); |
| 231 for (EntryMap::iterator it = map_.begin(); it != map_.end(); ++it) { | 231 for (EntryMap::iterator it = map_.begin(); it != map_.end(); ++it) { |
| 232 if ((LS_UNLOCKED != it->second.lock_state) || (it->second.accessors > 0)) | 232 if ((LS_UNLOCKED != it->second.lock_state) || (it->second.accessors > 0)) |
| 233 continue; | 233 continue; |
| 234 oldest = it; | 234 oldest = it; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 252 } | 252 } |
| 253 | 253 |
| 254 std::string DiskCache::IdToFilename(const std::string& id, size_t index) const { | 254 std::string DiskCache::IdToFilename(const std::string& id, size_t index) const { |
| 255 #ifdef TRANSPARENT_CACHE_NAMES | 255 #ifdef TRANSPARENT_CACHE_NAMES |
| 256 // This escapes colons and other filesystem characters, so the user can't open | 256 // This escapes colons and other filesystem characters, so the user can't open |
| 257 // special devices (like "COM1:"), or access other directories. | 257 // special devices (like "COM1:"), or access other directories. |
| 258 size_t buffer_size = id.length()*3 + 1; | 258 size_t buffer_size = id.length()*3 + 1; |
| 259 char* buffer = new char[buffer_size]; | 259 char* buffer = new char[buffer_size]; |
| 260 encode(buffer, buffer_size, id.data(), id.length(), | 260 encode(buffer, buffer_size, id.data(), id.length(), |
| 261 unsafe_filename_characters(), '%'); | 261 unsafe_filename_characters(), '%'); |
| 262 // TODO: ASSERT(strlen(buffer) < FileSystem::MaxBasenameLength()); | 262 // TODO(nisse): RTC_DCHECK(strlen(buffer) < FileSystem::MaxBasenameLength()); |
| 263 #else // !TRANSPARENT_CACHE_NAMES | 263 #else // !TRANSPARENT_CACHE_NAMES |
| 264 // We might want to just use a hash of the filename at some point, both for | 264 // We might want to just use a hash of the filename at some point, both for |
| 265 // obfuscation, and to avoid both filename length and escaping issues. | 265 // obfuscation, and to avoid both filename length and escaping issues. |
| 266 RTC_NOTREACHED(); | 266 RTC_NOTREACHED(); |
| 267 #endif // !TRANSPARENT_CACHE_NAMES | 267 #endif // !TRANSPARENT_CACHE_NAMES |
| 268 | 268 |
| 269 char extension[32]; | 269 char extension[32]; |
| 270 sprintfn(extension, arraysize(extension), ".%u", index); | 270 sprintfn(extension, arraysize(extension), ".%u", index); |
| 271 | 271 |
| 272 Pathname pathname; | 272 Pathname pathname; |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 entry2->last_modified = time(0); | 343 entry2->last_modified = time(0); |
| 344 entry2->lock_state = LS_UNLOCKED; | 344 entry2->lock_state = LS_UNLOCKED; |
| 345 this2->CheckLimit(); | 345 this2->CheckLimit(); |
| 346 } | 346 } |
| 347 } | 347 } |
| 348 } | 348 } |
| 349 | 349 |
| 350 /////////////////////////////////////////////////////////////////////////////// | 350 /////////////////////////////////////////////////////////////////////////////// |
| 351 | 351 |
| 352 } // namespace rtc | 352 } // namespace rtc |
| OLD | NEW |