1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
/*
* libjingle
* Copyright 2004--2005, Google Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <time.h>
#ifdef WIN32
#include "talk/base/win32.h"
#endif
#include "talk/base/basicdefs.h"
#include "talk/base/common.h"
#include "talk/base/diskcache.h"
#include "talk/base/fileutils.h"
#include "talk/base/pathutils.h"
#include "talk/base/stream.h"
#include "talk/base/stringencode.h"
#include "talk/base/stringutils.h"
#ifdef _DEBUG
#define TRANSPARENT_CACHE_NAMES 1
#else // !_DEBUG
#define TRANSPARENT_CACHE_NAMES 0
#endif // !_DEBUG
namespace talk_base {
class DiskCache;
///////////////////////////////////////////////////////////////////////////////
// DiskCacheAdapter
///////////////////////////////////////////////////////////////////////////////
class DiskCacheAdapter : public StreamAdapterInterface {
public:
DiskCacheAdapter(const DiskCache* cache, const std::string& id, size_t index,
StreamInterface* stream)
: StreamAdapterInterface(stream), cache_(cache), id_(id), index_(index)
{ }
virtual ~DiskCacheAdapter() {
Close();
cache_->ReleaseResource(id_, index_);
}
private:
const DiskCache* cache_;
std::string id_;
size_t index_;
};
///////////////////////////////////////////////////////////////////////////////
// DiskCache
///////////////////////////////////////////////////////////////////////////////
DiskCache::DiskCache() : max_cache_(0), total_size_(0), total_accessors_(0) {
}
DiskCache::~DiskCache() {
ASSERT(0 == total_accessors_);
}
bool DiskCache::Initialize(const std::string& folder, size_t size) {
if (!folder_.empty() || !Filesystem::CreateFolder(folder))
return false;
folder_ = folder;
max_cache_ = size;
ASSERT(0 == total_size_);
if (!InitializeEntries())
return false;
return CheckLimit();
}
bool DiskCache::Purge() {
if (folder_.empty())
return false;
if (total_accessors_ > 0) {
LOG_F(LS_WARNING) << "Cache files open";
return false;
}
if (!PurgeFiles())
return false;
map_.clear();
return true;
}
bool DiskCache::LockResource(const std::string& id) {
Entry* entry = GetOrCreateEntry(id, true);
if (LS_LOCKED == entry->lock_state)
return false;
if ((LS_UNLOCKED == entry->lock_state) && (entry->accessors > 0))
return false;
if ((total_size_ > max_cache_) && !CheckLimit()) {
LOG_F(LS_WARNING) << "Cache overfull";
return false;
}
entry->lock_state = LS_LOCKED;
return true;
}
StreamInterface* DiskCache::WriteResource(const std::string& id, size_t index) {
Entry* entry = GetOrCreateEntry(id, false);
if (LS_LOCKED != entry->lock_state)
return NULL;
size_t previous_size = 0;
std::string filename(IdToFilename(id, index));
FileStream::GetSize(filename, &previous_size);
ASSERT(previous_size <= entry->size);
if (previous_size > entry->size) {
previous_size = entry->size;
}
scoped_ptr<FileStream> file(new FileStream);
if (!file->Open(filename, "wb")) {
LOG_F(LS_ERROR) << "Couldn't create cache file";
return NULL;
}
entry->streams = stdmax(entry->streams, index + 1);
entry->size -= previous_size;
total_size_ -= previous_size;
entry->accessors += 1;
total_accessors_ += 1;
return new DiskCacheAdapter(this, id, index, file.release());
}
bool DiskCache::UnlockResource(const std::string& id) {
Entry* entry = GetOrCreateEntry(id, false);
if (LS_LOCKED != entry->lock_state)
return false;
if (entry->accessors > 0) {
entry->lock_state = LS_UNLOCKING;
} else {
entry->lock_state = LS_UNLOCKED;
entry->last_modified = time(0);
CheckLimit();
}
return true;
}
StreamInterface* DiskCache::ReadResource(const std::string& id,
size_t index) const {
const Entry* entry = GetEntry(id);
if (LS_UNLOCKED != entry->lock_state)
return NULL;
if (index >= entry->streams)
return NULL;
scoped_ptr<FileStream> file(new FileStream);
if (!file->Open(IdToFilename(id, index), "rb"))
return NULL;
entry->accessors += 1;
total_accessors_ += 1;
return new DiskCacheAdapter(this, id, index, file.release());
}
bool DiskCache::HasResource(const std::string& id) const {
const Entry* entry = GetEntry(id);
return (NULL != entry) && (entry->streams > 0);
}
bool DiskCache::HasResourceStream(const std::string& id, size_t index) const {
const Entry* entry = GetEntry(id);
if ((NULL == entry) || (index >= entry->streams))
return false;
std::string filename = IdToFilename(id, index);
return FileExists(filename);
}
bool DiskCache::DeleteResource(const std::string& id) {
Entry* entry = GetOrCreateEntry(id, false);
if (!entry)
return true;
if ((LS_UNLOCKED != entry->lock_state) || (entry->accessors > 0))
return false;
bool success = true;
for (size_t index = 0; index < entry->streams; ++index) {
std::string filename = IdToFilename(id, index);
if (!FileExists(filename))
continue;
if (!DeleteFile(filename)) {
LOG_F(LS_ERROR) << "Couldn't remove cache file: " << filename;
success = false;
}
}
total_size_ -= entry->size;
map_.erase(id);
return success;
}
bool DiskCache::CheckLimit() {
#ifdef _DEBUG
// Temporary check to make sure everything is working correctly.
size_t cache_size = 0;
for (EntryMap::iterator it = map_.begin(); it != map_.end(); ++it) {
cache_size += it->second.size;
}
ASSERT(cache_size == total_size_);
#endif // _DEBUG
// TODO: Replace this with a non-brain-dead algorithm for clearing out the
// oldest resources... something that isn't O(n^2)
while (total_size_ > max_cache_) {
EntryMap::iterator oldest = map_.end();
for (EntryMap::iterator it = map_.begin(); it != map_.end(); ++it) {
if ((LS_UNLOCKED != it->second.lock_state) || (it->second.accessors > 0))
continue;
oldest = it;
break;
}
if (oldest == map_.end()) {
LOG_F(LS_WARNING) << "All resources are locked!";
return false;
}
for (EntryMap::iterator it = oldest++; it != map_.end(); ++it) {
if (it->second.last_modified < oldest->second.last_modified) {
oldest = it;
}
}
if (!DeleteResource(oldest->first)) {
LOG_F(LS_ERROR) << "Couldn't delete from cache!";
return false;
}
}
return true;
}
std::string DiskCache::IdToFilename(const std::string& id, size_t index) const {
#ifdef TRANSPARENT_CACHE_NAMES
// This escapes colons and other filesystem characters, so the user can't open
// special devices (like "COM1:"), or access other directories.
size_t buffer_size = id.length()*3 + 1;
char* buffer = new char[buffer_size];
encode(buffer, buffer_size, id.data(), id.length(),
unsafe_filename_characters(), '%');
// TODO: ASSERT(strlen(buffer) < FileSystem::MaxBasenameLength());
#else // !TRANSPARENT_CACHE_NAMES
// We might want to just use a hash of the filename at some point, both for
// obfuscation, and to avoid both filename length and escaping issues.
ASSERT(false);
#endif // !TRANSPARENT_CACHE_NAMES
char extension[32];
sprintfn(extension, ARRAY_SIZE(extension), ".%u", index);
Pathname pathname;
pathname.SetFolder(folder_);
pathname.SetBasename(buffer);
pathname.SetExtension(extension);
#ifdef TRANSPARENT_CACHE_NAMES
delete [] buffer;
#endif // TRANSPARENT_CACHE_NAMES
return pathname.pathname();
}
bool DiskCache::FilenameToId(const std::string& filename, std::string* id,
size_t* index) const {
Pathname pathname(filename);
if (1 != sscanf(pathname.extension().c_str(), ".%u", (unsigned int*)index))
return false;
size_t buffer_size = pathname.basename().length() + 1;
char* buffer = new char[buffer_size];
decode(buffer, buffer_size, pathname.basename().data(),
pathname.basename().length(), '%');
id->assign(buffer);
delete [] buffer;
return true;
}
DiskCache::Entry* DiskCache::GetOrCreateEntry(const std::string& id,
bool create) {
EntryMap::iterator it = map_.find(id);
if (it != map_.end())
return &it->second;
if (!create)
return NULL;
Entry e;
e.lock_state = LS_UNLOCKED;
e.accessors = 0;
e.size = 0;
e.streams = 0;
e.last_modified = time(0);
it = map_.insert(EntryMap::value_type(id, e)).first;
return &it->second;
}
void DiskCache::ReleaseResource(const std::string& id, size_t index) const {
const Entry* entry = GetEntry(id);
if (!entry) {
LOG_F(LS_WARNING) << "Missing cache entry";
ASSERT(false);
return;
}
entry->accessors -= 1;
total_accessors_ -= 1;
if (LS_UNLOCKED != entry->lock_state) {
// This is safe, because locked resources only issue WriteResource, which
// is non-const. Think about a better way to handle it.
DiskCache* this2 = const_cast<DiskCache*>(this);
Entry* entry2 = this2->GetOrCreateEntry(id, false);
size_t new_size = 0;
std::string filename(IdToFilename(id, index));
FileStream::GetSize(filename, &new_size);
entry2->size += new_size;
this2->total_size_ += new_size;
if ((LS_UNLOCKING == entry->lock_state) && (0 == entry->accessors)) {
entry2->last_modified = time(0);
entry2->lock_state = LS_UNLOCKED;
this2->CheckLimit();
}
}
}
///////////////////////////////////////////////////////////////////////////////
} // namespace talk_base
|