blob: f893ce73d3b2241319ca7b6af2c6edc94c8c5593 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include <time.h>
12
13#if defined(WEBRTC_WIN)
14#include "webrtc/base/win32.h"
15#endif
16
17#include "webrtc/base/common.h"
18#include "webrtc/base/diskcache.h"
19#include "webrtc/base/fileutils.h"
20#include "webrtc/base/pathutils.h"
21#include "webrtc/base/stream.h"
22#include "webrtc/base/stringencode.h"
23#include "webrtc/base/stringutils.h"
24
25#ifdef _DEBUG
26#define TRANSPARENT_CACHE_NAMES 1
27#else // !_DEBUG
28#define TRANSPARENT_CACHE_NAMES 0
29#endif // !_DEBUG
30
31namespace rtc {
32
33class DiskCache;
34
35///////////////////////////////////////////////////////////////////////////////
36// DiskCacheAdapter
37///////////////////////////////////////////////////////////////////////////////
38
39class DiskCacheAdapter : public StreamAdapterInterface {
40public:
41 DiskCacheAdapter(const DiskCache* cache, const std::string& id, size_t index,
42 StreamInterface* stream)
43 : StreamAdapterInterface(stream), cache_(cache), id_(id), index_(index)
44 { }
45 virtual ~DiskCacheAdapter() {
46 Close();
47 cache_->ReleaseResource(id_, index_);
48 }
49
50private:
51 const DiskCache* cache_;
52 std::string id_;
53 size_t index_;
54};
55
56///////////////////////////////////////////////////////////////////////////////
57// DiskCache
58///////////////////////////////////////////////////////////////////////////////
59
60DiskCache::DiskCache() : max_cache_(0), total_size_(0), total_accessors_(0) {
61}
62
63DiskCache::~DiskCache() {
64 ASSERT(0 == total_accessors_);
65}
66
67bool DiskCache::Initialize(const std::string& folder, size_t size) {
68 if (!folder_.empty() || !Filesystem::CreateFolder(folder))
69 return false;
70
71 folder_ = folder;
72 max_cache_ = size;
73 ASSERT(0 == total_size_);
74
75 if (!InitializeEntries())
76 return false;
77
78 return CheckLimit();
79}
80
81bool DiskCache::Purge() {
82 if (folder_.empty())
83 return false;
84
85 if (total_accessors_ > 0) {
86 LOG_F(LS_WARNING) << "Cache files open";
87 return false;
88 }
89
90 if (!PurgeFiles())
91 return false;
92
93 map_.clear();
94 return true;
95}
96
97bool DiskCache::LockResource(const std::string& id) {
98 Entry* entry = GetOrCreateEntry(id, true);
99 if (LS_LOCKED == entry->lock_state)
100 return false;
101 if ((LS_UNLOCKED == entry->lock_state) && (entry->accessors > 0))
102 return false;
103 if ((total_size_ > max_cache_) && !CheckLimit()) {
104 LOG_F(LS_WARNING) << "Cache overfull";
105 return false;
106 }
107 entry->lock_state = LS_LOCKED;
108 return true;
109}
110
111StreamInterface* DiskCache::WriteResource(const std::string& id, size_t index) {
112 Entry* entry = GetOrCreateEntry(id, false);
113 if (LS_LOCKED != entry->lock_state)
114 return NULL;
115
116 size_t previous_size = 0;
117 std::string filename(IdToFilename(id, index));
118 FileStream::GetSize(filename, &previous_size);
119 ASSERT(previous_size <= entry->size);
120 if (previous_size > entry->size) {
121 previous_size = entry->size;
122 }
123
124 scoped_ptr<FileStream> file(new FileStream);
125 if (!file->Open(filename, "wb", NULL)) {
126 LOG_F(LS_ERROR) << "Couldn't create cache file";
127 return NULL;
128 }
129
130 entry->streams = stdmax(entry->streams, index + 1);
131 entry->size -= previous_size;
132 total_size_ -= previous_size;
133
134 entry->accessors += 1;
135 total_accessors_ += 1;
136 return new DiskCacheAdapter(this, id, index, file.release());
137}
138
139bool DiskCache::UnlockResource(const std::string& id) {
140 Entry* entry = GetOrCreateEntry(id, false);
141 if (LS_LOCKED != entry->lock_state)
142 return false;
143
144 if (entry->accessors > 0) {
145 entry->lock_state = LS_UNLOCKING;
146 } else {
147 entry->lock_state = LS_UNLOCKED;
148 entry->last_modified = time(0);
149 CheckLimit();
150 }
151 return true;
152}
153
154StreamInterface* DiskCache::ReadResource(const std::string& id,
155 size_t index) const {
156 const Entry* entry = GetEntry(id);
157 if (LS_UNLOCKED != entry->lock_state)
158 return NULL;
159 if (index >= entry->streams)
160 return NULL;
161
162 scoped_ptr<FileStream> file(new FileStream);
163 if (!file->Open(IdToFilename(id, index), "rb", NULL))
164 return NULL;
165
166 entry->accessors += 1;
167 total_accessors_ += 1;
168 return new DiskCacheAdapter(this, id, index, file.release());
169}
170
171bool DiskCache::HasResource(const std::string& id) const {
172 const Entry* entry = GetEntry(id);
173 return (NULL != entry) && (entry->streams > 0);
174}
175
176bool DiskCache::HasResourceStream(const std::string& id, size_t index) const {
177 const Entry* entry = GetEntry(id);
178 if ((NULL == entry) || (index >= entry->streams))
179 return false;
180
181 std::string filename = IdToFilename(id, index);
182
183 return FileExists(filename);
184}
185
186bool DiskCache::DeleteResource(const std::string& id) {
187 Entry* entry = GetOrCreateEntry(id, false);
188 if (!entry)
189 return true;
190
191 if ((LS_UNLOCKED != entry->lock_state) || (entry->accessors > 0))
192 return false;
193
194 bool success = true;
195 for (size_t index = 0; index < entry->streams; ++index) {
196 std::string filename = IdToFilename(id, index);
197
198 if (!FileExists(filename))
199 continue;
200
201 if (!DeleteFile(filename)) {
202 LOG_F(LS_ERROR) << "Couldn't remove cache file: " << filename;
203 success = false;
204 }
205 }
206
207 total_size_ -= entry->size;
208 map_.erase(id);
209 return success;
210}
211
212bool DiskCache::CheckLimit() {
213#ifdef _DEBUG
214 // Temporary check to make sure everything is working correctly.
215 size_t cache_size = 0;
216 for (EntryMap::iterator it = map_.begin(); it != map_.end(); ++it) {
217 cache_size += it->second.size;
218 }
219 ASSERT(cache_size == total_size_);
220#endif // _DEBUG
221
222 // TODO: Replace this with a non-brain-dead algorithm for clearing out the
223 // oldest resources... something that isn't O(n^2)
224 while (total_size_ > max_cache_) {
225 EntryMap::iterator oldest = map_.end();
226 for (EntryMap::iterator it = map_.begin(); it != map_.end(); ++it) {
227 if ((LS_UNLOCKED != it->second.lock_state) || (it->second.accessors > 0))
228 continue;
229 oldest = it;
230 break;
231 }
232 if (oldest == map_.end()) {
233 LOG_F(LS_WARNING) << "All resources are locked!";
234 return false;
235 }
236 for (EntryMap::iterator it = oldest++; it != map_.end(); ++it) {
237 if (it->second.last_modified < oldest->second.last_modified) {
238 oldest = it;
239 }
240 }
241 if (!DeleteResource(oldest->first)) {
242 LOG_F(LS_ERROR) << "Couldn't delete from cache!";
243 return false;
244 }
245 }
246 return true;
247}
248
249std::string DiskCache::IdToFilename(const std::string& id, size_t index) const {
250#ifdef TRANSPARENT_CACHE_NAMES
251 // This escapes colons and other filesystem characters, so the user can't open
252 // special devices (like "COM1:"), or access other directories.
253 size_t buffer_size = id.length()*3 + 1;
254 char* buffer = new char[buffer_size];
255 encode(buffer, buffer_size, id.data(), id.length(),
256 unsafe_filename_characters(), '%');
257 // TODO: ASSERT(strlen(buffer) < FileSystem::MaxBasenameLength());
258#else // !TRANSPARENT_CACHE_NAMES
259 // We might want to just use a hash of the filename at some point, both for
260 // obfuscation, and to avoid both filename length and escaping issues.
261 ASSERT(false);
262#endif // !TRANSPARENT_CACHE_NAMES
263
264 char extension[32];
265 sprintfn(extension, ARRAY_SIZE(extension), ".%u", index);
266
267 Pathname pathname;
268 pathname.SetFolder(folder_);
269 pathname.SetBasename(buffer);
270 pathname.SetExtension(extension);
271
272#ifdef TRANSPARENT_CACHE_NAMES
273 delete [] buffer;
274#endif // TRANSPARENT_CACHE_NAMES
275
276 return pathname.pathname();
277}
278
279bool DiskCache::FilenameToId(const std::string& filename, std::string* id,
280 size_t* index) const {
281 Pathname pathname(filename);
282 unsigned tempdex;
283 if (1 != sscanf(pathname.extension().c_str(), ".%u", &tempdex))
284 return false;
285
286 *index = static_cast<size_t>(tempdex);
287
288 size_t buffer_size = pathname.basename().length() + 1;
289 char* buffer = new char[buffer_size];
290 decode(buffer, buffer_size, pathname.basename().data(),
291 pathname.basename().length(), '%');
292 id->assign(buffer);
293 delete [] buffer;
294 return true;
295}
296
297DiskCache::Entry* DiskCache::GetOrCreateEntry(const std::string& id,
298 bool create) {
299 EntryMap::iterator it = map_.find(id);
300 if (it != map_.end())
301 return &it->second;
302 if (!create)
303 return NULL;
304 Entry e;
305 e.lock_state = LS_UNLOCKED;
306 e.accessors = 0;
307 e.size = 0;
308 e.streams = 0;
309 e.last_modified = time(0);
310 it = map_.insert(EntryMap::value_type(id, e)).first;
311 return &it->second;
312}
313
314void DiskCache::ReleaseResource(const std::string& id, size_t index) const {
315 const Entry* entry = GetEntry(id);
316 if (!entry) {
317 LOG_F(LS_WARNING) << "Missing cache entry";
318 ASSERT(false);
319 return;
320 }
321
322 entry->accessors -= 1;
323 total_accessors_ -= 1;
324
325 if (LS_UNLOCKED != entry->lock_state) {
326 // This is safe, because locked resources only issue WriteResource, which
327 // is non-const. Think about a better way to handle it.
328 DiskCache* this2 = const_cast<DiskCache*>(this);
329 Entry* entry2 = this2->GetOrCreateEntry(id, false);
330
331 size_t new_size = 0;
332 std::string filename(IdToFilename(id, index));
333 FileStream::GetSize(filename, &new_size);
334 entry2->size += new_size;
335 this2->total_size_ += new_size;
336
337 if ((LS_UNLOCKING == entry->lock_state) && (0 == entry->accessors)) {
338 entry2->last_modified = time(0);
339 entry2->lock_state = LS_UNLOCKED;
340 this2->CheckLimit();
341 }
342 }
343}
344
345///////////////////////////////////////////////////////////////////////////////
346
347} // namespace rtc