|
1 /* |
|
2 * Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 #include <stdlib.h> |
|
19 #include <string> |
|
20 #include <queue> |
|
21 #include <new> |
|
22 |
|
23 #include "e32image.h" |
|
24 |
|
25 #include <filesystem.hpp> |
|
26 #include <thread/thread.hpp> |
|
27 #include <thread/mutex.hpp> |
|
28 #include <thread/condition_variable.hpp> |
|
29 |
|
30 #include "cache/cacheexception.hpp" |
|
31 #include "cache/cacheentry.hpp" |
|
32 #include "cache/cache.hpp" |
|
33 #include "cache/cachegenerator.hpp" |
|
34 #include "cache/cachevalidator.hpp" |
|
35 #include "cache/cacheablelist.hpp" |
|
36 #include "cache/cachemanager.hpp" |
|
37 |
|
38 using namespace std; |
|
39 |
|
40 |
|
41 CacheManager* CacheManager::Only = (CacheManager*)0; |
|
42 boost::mutex CacheManager::creationlock; |
|
43 |
|
44 |
|
45 CacheManager* CacheManager::GetInstance(void) throw (CacheException) |
|
46 { |
|
47 if(! CacheManager::Only) |
|
48 { |
|
49 boost::mutex::scoped_lock lock(CacheManager::creationlock); |
|
50 if(! CacheManager::Only) |
|
51 { |
|
52 CacheManager::Only = new (nothrow) CacheManager(); |
|
53 if(!CacheManager::Only) |
|
54 throw CacheException(CacheException::RESOURCE_ALLOCATION_FAILURE); |
|
55 CacheManager::Only->InitializeCache(); |
|
56 } |
|
57 } |
|
58 |
|
59 return CacheManager::Only; |
|
60 } |
|
61 |
|
62 |
|
63 E32ImageFile* CacheManager::GetE32ImageFile(char* OriginalFilename, int CurrentCompressionID) |
|
64 { |
|
65 this->NormalizeFilename(OriginalFilename); |
|
66 CacheEntry* validatedfile =CacheValidator::GetInstance()->Validate(OriginalFilename, CurrentCompressionID); |
|
67 if(! validatedfile) |
|
68 return (E32ImageFile*)0; |
|
69 |
|
70 printf("%s is validated in cache.\r\n", OriginalFilename); |
|
71 E32ImageFile* cachedimagefile = new (nothrow) E32ImageFile(); |
|
72 if(! cachedimagefile) |
|
73 return (E32ImageFile*)0; |
|
74 boost::filesystem::path cachedfile(validatedfile->GetCachedFilename()); |
|
75 ifstream filecontentreader(cachedfile.file_string().c_str(), ios_base::in | ios_base::binary); |
|
76 if(! filecontentreader.is_open()) |
|
77 return (E32ImageFile*)0; |
|
78 filecontentreader.seekg(0, ios_base::end); |
|
79 int contentlength = filecontentreader.tellg(); |
|
80 cachedimagefile->iData = (char*)malloc(sizeof(char)*contentlength); |
|
81 filecontentreader.seekg(0, ios_base::beg); |
|
82 filecontentreader.read(cachedimagefile->iData, contentlength); |
|
83 cachedimagefile->iHdr->iUncompressedSize = contentlength - cachedimagefile->iHdr->TotalSize(); |
|
84 |
|
85 return cachedimagefile; |
|
86 } |
|
87 |
|
88 |
|
89 CacheEntry* CacheManager::GetE32ImageFileRepresentation(char* OriginalFilename, int CurrentCompressionID) |
|
90 { |
|
91 this->NormalizeFilename(OriginalFilename); |
|
92 |
|
93 return CacheValidator::GetInstance()->Validate(OriginalFilename, CurrentCompressionID); |
|
94 } |
|
95 |
|
96 |
|
97 void CacheManager::Invalidate(char* Filename, CacheEntry* EntryRef) throw (CacheException) |
|
98 { |
|
99 //update cache meta file. |
|
100 this->NormalizeFilename(Filename); |
|
101 Cache::GetInstance()->AddEntry(Filename, EntryRef); |
|
102 |
|
103 //update cache content. |
|
104 CacheableList::GetInstance()->AddCacheable(EntryRef); |
|
105 |
|
106 printf("Caching %s\r\n", Filename); |
|
107 |
|
108 return; |
|
109 } |
|
110 |
|
111 |
|
112 void CacheManager::CleanCache(void) throw (CacheException) |
|
113 { |
|
114 //check if the cache is present in the current system. |
|
115 boost::filesystem::path cacherootdir(this->cacheroot); |
|
116 if(! exists(cacherootdir)) |
|
117 throw CacheException(CacheException::CACHE_NOT_FOUND); |
|
118 |
|
119 //remove files iteratively from cache root directory. |
|
120 if(remove_all(cacherootdir) <= 0) |
|
121 throw CacheException(CacheException::CACHE_IS_EMPTY); |
|
122 |
|
123 return; |
|
124 } |
|
125 |
|
126 |
|
127 const char* CacheManager::GetCacheRoot(void) |
|
128 { |
|
129 return this->cacheroot; |
|
130 } |
|
131 |
|
132 |
|
133 CacheManager::~CacheManager(void) |
|
134 { |
|
135 CacheEntry* newentryref = new (nothrow) CacheEntry(); |
|
136 CacheableList::GetInstance()->AddCacheable(newentryref); |
|
137 Cache::GetInstance()->CloseCache(); |
|
138 CacheGenerator::GetInstance()->join(); |
|
139 |
|
140 delete CacheValidator::GetInstance(); |
|
141 delete Cache::GetInstance(); |
|
142 delete CacheGenerator::GetInstance(); |
|
143 delete CacheableList::GetInstance(); |
|
144 |
|
145 return; |
|
146 } |
|
147 |
|
148 |
|
149 void CacheManager::InitializeCache(void) throw (CacheException) |
|
150 { |
|
151 //assume the root directory is EPOCROOT/epoc32/build/.cache |
|
152 char* epocroot = getenv("EPOCROOT"); |
|
153 if(! epocroot) |
|
154 throw CacheException(CacheException::EPOCROOT_NOT_FOUND); |
|
155 |
|
156 //initialize cacheroot member variable. |
|
157 int cacherootstrlen = sizeof(char)*(strlen(epocroot)+strlen("/epoc32/build/.cache")+1); |
|
158 this->cacheroot = (char*)malloc(cacherootstrlen); |
|
159 if(! this->cacheroot) |
|
160 throw CacheException(CacheException::RESOURCE_ALLOCATION_FAILURE); |
|
161 memset(this->cacheroot, 0, cacherootstrlen); |
|
162 sprintf(this->cacheroot, "%s%s", epocroot, "/epoc32/build/.cache"); |
|
163 |
|
164 //normalize cache root path. |
|
165 this->NormalizeFilename(this->cacheroot); |
|
166 |
|
167 //create cache root directory if it is not present. |
|
168 boost::filesystem::path cacherootdir(this->cacheroot); |
|
169 if(! exists(cacherootdir)) |
|
170 create_directories(cacherootdir); |
|
171 printf("Using %s as cache root directory.\r\n", cacherootdir.file_string().c_str()); |
|
172 |
|
173 //create cache instance. |
|
174 Cache* cacheref = Cache::GetInstance(); |
|
175 if(! cacheref) |
|
176 throw CacheException(CacheException::RESOURCE_ALLOCATION_FAILURE); |
|
177 |
|
178 //create cache validator instance. |
|
179 if(! CacheValidator::GetInstance()) |
|
180 throw CacheException(CacheException::RESOURCE_ALLOCATION_FAILURE); |
|
181 |
|
182 //create cacheable list instance |
|
183 if(! CacheableList::GetInstance()) |
|
184 throw CacheException(CacheException::RESOURCE_ALLOCATION_FAILURE); |
|
185 |
|
186 //initialize cache container. |
|
187 cacheref->Initialize(); |
|
188 |
|
189 //start cache generator. |
|
190 CacheGenerator::GetInstance(); |
|
191 |
|
192 return; |
|
193 } |
|
194 |
|
195 |
|
196 void CacheManager::NormalizeFilename(char* Filename) |
|
197 { |
|
198 if(!Filename) |
|
199 return; |
|
200 |
|
201 //convert back slashes into forward slashes. |
|
202 char* normalizedfilename = Filename; |
|
203 while(*normalizedfilename) |
|
204 { |
|
205 if(*normalizedfilename == '\\') |
|
206 *normalizedfilename = '/'; |
|
207 |
|
208 normalizedfilename++; |
|
209 } |
|
210 |
|
211 //remove redundant slashes. |
|
212 char* redundantfilename = Filename; |
|
213 while(*redundantfilename) |
|
214 { |
|
215 if((*redundantfilename=='/') && (*(redundantfilename+1)=='/')) |
|
216 { |
|
217 redundantfilename++; |
|
218 continue; |
|
219 } |
|
220 *Filename++ = *redundantfilename++; |
|
221 } |
|
222 *Filename = 0; |
|
223 |
|
224 return; |
|
225 } |
|
226 |
|
227 |
|
228 CacheManager::CacheManager(void) |
|
229 { |
|
230 return; |
|
231 } |