3 * Copyright 2004--2005, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "talk/base/win32.h"
34 #include "talk/base/common.h"
35 #include "talk/base/diskcache.h"
36 #include "talk/base/fileutils.h"
37 #include "talk/base/pathutils.h"
38 #include "talk/base/stream.h"
39 #include "talk/base/stringencode.h"
40 #include "talk/base/stringutils.h"
43 #define TRANSPARENT_CACHE_NAMES 1
45 #define TRANSPARENT_CACHE_NAMES 0
52 ///////////////////////////////////////////////////////////////////////////////
54 ///////////////////////////////////////////////////////////////////////////////
56 class DiskCacheAdapter : public StreamAdapterInterface {
58 DiskCacheAdapter(const DiskCache* cache, const std::string& id, size_t index,
59 StreamInterface* stream)
60 : StreamAdapterInterface(stream), cache_(cache), id_(id), index_(index)
62 virtual ~DiskCacheAdapter() {
64 cache_->ReleaseResource(id_, index_);
68 const DiskCache* cache_;
73 ///////////////////////////////////////////////////////////////////////////////
75 ///////////////////////////////////////////////////////////////////////////////
77 DiskCache::DiskCache() : max_cache_(0), total_size_(0), total_accessors_(0) {
80 DiskCache::~DiskCache() {
81 ASSERT(0 == total_accessors_);
84 bool DiskCache::Initialize(const std::string& folder, size_t size) {
85 if (!folder_.empty() || !Filesystem::CreateFolder(folder))
90 ASSERT(0 == total_size_);
92 if (!InitializeEntries())
98 bool DiskCache::Purge() {
102 if (total_accessors_ > 0) {
103 LOG_F(LS_WARNING) << "Cache files open";
114 bool DiskCache::LockResource(const std::string& id) {
115 Entry* entry = GetOrCreateEntry(id, true);
116 if (LS_LOCKED == entry->lock_state)
118 if ((LS_UNLOCKED == entry->lock_state) && (entry->accessors > 0))
120 if ((total_size_ > max_cache_) && !CheckLimit()) {
121 LOG_F(LS_WARNING) << "Cache overfull";
124 entry->lock_state = LS_LOCKED;
128 StreamInterface* DiskCache::WriteResource(const std::string& id, size_t index) {
129 Entry* entry = GetOrCreateEntry(id, false);
130 if (LS_LOCKED != entry->lock_state)
133 size_t previous_size = 0;
134 std::string filename(IdToFilename(id, index));
135 FileStream::GetSize(filename, &previous_size);
136 ASSERT(previous_size <= entry->size);
137 if (previous_size > entry->size) {
138 previous_size = entry->size;
141 scoped_ptr<FileStream> file(new FileStream);
142 if (!file->Open(filename, "wb", NULL)) {
143 LOG_F(LS_ERROR) << "Couldn't create cache file";
147 entry->streams = stdmax(entry->streams, index + 1);
148 entry->size -= previous_size;
149 total_size_ -= previous_size;
151 entry->accessors += 1;
152 total_accessors_ += 1;
153 return new DiskCacheAdapter(this, id, index, file.release());
156 bool DiskCache::UnlockResource(const std::string& id) {
157 Entry* entry = GetOrCreateEntry(id, false);
158 if (LS_LOCKED != entry->lock_state)
161 if (entry->accessors > 0) {
162 entry->lock_state = LS_UNLOCKING;
164 entry->lock_state = LS_UNLOCKED;
165 entry->last_modified = time(0);
171 StreamInterface* DiskCache::ReadResource(const std::string& id,
172 size_t index) const {
173 const Entry* entry = GetEntry(id);
174 if (LS_UNLOCKED != entry->lock_state)
176 if (index >= entry->streams)
179 scoped_ptr<FileStream> file(new FileStream);
180 if (!file->Open(IdToFilename(id, index), "rb", NULL))
183 entry->accessors += 1;
184 total_accessors_ += 1;
185 return new DiskCacheAdapter(this, id, index, file.release());
188 bool DiskCache::HasResource(const std::string& id) const {
189 const Entry* entry = GetEntry(id);
190 return (NULL != entry) && (entry->streams > 0);
193 bool DiskCache::HasResourceStream(const std::string& id, size_t index) const {
194 const Entry* entry = GetEntry(id);
195 if ((NULL == entry) || (index >= entry->streams))
198 std::string filename = IdToFilename(id, index);
200 return FileExists(filename);
203 bool DiskCache::DeleteResource(const std::string& id) {
204 Entry* entry = GetOrCreateEntry(id, false);
208 if ((LS_UNLOCKED != entry->lock_state) || (entry->accessors > 0))
212 for (size_t index = 0; index < entry->streams; ++index) {
213 std::string filename = IdToFilename(id, index);
215 if (!FileExists(filename))
218 if (!DeleteFile(filename)) {
219 LOG_F(LS_ERROR) << "Couldn't remove cache file: " << filename;
224 total_size_ -= entry->size;
229 bool DiskCache::CheckLimit() {
231 // Temporary check to make sure everything is working correctly.
232 size_t cache_size = 0;
233 for (EntryMap::iterator it = map_.begin(); it != map_.end(); ++it) {
234 cache_size += it->second.size;
236 ASSERT(cache_size == total_size_);
239 // TODO: Replace this with a non-brain-dead algorithm for clearing out the
240 // oldest resources... something that isn't O(n^2)
241 while (total_size_ > max_cache_) {
242 EntryMap::iterator oldest = map_.end();
243 for (EntryMap::iterator it = map_.begin(); it != map_.end(); ++it) {
244 if ((LS_UNLOCKED != it->second.lock_state) || (it->second.accessors > 0))
249 if (oldest == map_.end()) {
250 LOG_F(LS_WARNING) << "All resources are locked!";
253 for (EntryMap::iterator it = oldest++; it != map_.end(); ++it) {
254 if (it->second.last_modified < oldest->second.last_modified) {
258 if (!DeleteResource(oldest->first)) {
259 LOG_F(LS_ERROR) << "Couldn't delete from cache!";
266 std::string DiskCache::IdToFilename(const std::string& id, size_t index) const {
267 #ifdef TRANSPARENT_CACHE_NAMES
268 // This escapes colons and other filesystem characters, so the user can't open
269 // special devices (like "COM1:"), or access other directories.
270 size_t buffer_size = id.length()*3 + 1;
271 char* buffer = new char[buffer_size];
272 encode(buffer, buffer_size, id.data(), id.length(),
273 unsafe_filename_characters(), '%');
274 // TODO: ASSERT(strlen(buffer) < FileSystem::MaxBasenameLength());
275 #else // !TRANSPARENT_CACHE_NAMES
276 // We might want to just use a hash of the filename at some point, both for
277 // obfuscation, and to avoid both filename length and escaping issues.
279 #endif // !TRANSPARENT_CACHE_NAMES
282 sprintfn(extension, ARRAY_SIZE(extension), ".%u", index);
285 pathname.SetFolder(folder_);
286 pathname.SetBasename(buffer);
287 pathname.SetExtension(extension);
289 #ifdef TRANSPARENT_CACHE_NAMES
291 #endif // TRANSPARENT_CACHE_NAMES
293 return pathname.pathname();
296 bool DiskCache::FilenameToId(const std::string& filename, std::string* id,
297 size_t* index) const {
298 Pathname pathname(filename);
300 if (1 != sscanf(pathname.extension().c_str(), ".%u", &tempdex))
303 *index = static_cast<size_t>(tempdex);
305 size_t buffer_size = pathname.basename().length() + 1;
306 char* buffer = new char[buffer_size];
307 decode(buffer, buffer_size, pathname.basename().data(),
308 pathname.basename().length(), '%');
314 DiskCache::Entry* DiskCache::GetOrCreateEntry(const std::string& id,
316 EntryMap::iterator it = map_.find(id);
317 if (it != map_.end())
322 e.lock_state = LS_UNLOCKED;
326 e.last_modified = time(0);
327 it = map_.insert(EntryMap::value_type(id, e)).first;
331 void DiskCache::ReleaseResource(const std::string& id, size_t index) const {
332 const Entry* entry = GetEntry(id);
334 LOG_F(LS_WARNING) << "Missing cache entry";
339 entry->accessors -= 1;
340 total_accessors_ -= 1;
342 if (LS_UNLOCKED != entry->lock_state) {
343 // This is safe, because locked resources only issue WriteResource, which
344 // is non-const. Think about a better way to handle it.
345 DiskCache* this2 = const_cast<DiskCache*>(this);
346 Entry* entry2 = this2->GetOrCreateEntry(id, false);
349 std::string filename(IdToFilename(id, index));
350 FileStream::GetSize(filename, &new_size);
351 entry2->size += new_size;
352 this2->total_size_ += new_size;
354 if ((LS_UNLOCKING == entry->lock_state) && (0 == entry->accessors)) {
355 entry2->last_modified = time(0);
356 entry2->lock_state = LS_UNLOCKED;
362 ///////////////////////////////////////////////////////////////////////////////
364 } // namespace talk_base