|
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 <new> |
|
19 #include <cstring> |
|
20 #include <string> |
|
21 #include <iostream> |
|
22 #include <fstream> |
|
23 #include <queue> |
|
24 #include <map> |
|
25 |
|
26 #include "e32image.h" |
|
27 |
|
28 #include <filesystem.hpp> |
|
29 #include <thread/thread.hpp> |
|
30 #include <thread/mutex.hpp> |
|
31 #include <thread/condition_variable.hpp> |
|
32 |
|
33 #include "cache/cacheexception.hpp" |
|
34 #include "cache/cacheentry.hpp" |
|
35 #include "cache/cacheablelist.hpp" |
|
36 #include "cache/cache.hpp" |
|
37 #include "cache/cachegenerator.hpp" |
|
38 #include "cache/cachevalidator.hpp" |
|
39 #include "cache/cachemanager.hpp" |
|
40 #include <malloc.h> |
|
41 #ifdef __LINUX__ |
|
42 #define _alloca alloca |
|
43 #endif |
|
44 |
|
45 Cache* Cache::Only = (Cache*)0; |
|
46 |
|
47 |
|
48 Cache* Cache::GetInstance(void) throw (CacheException) |
|
49 { |
|
50 if(! Cache::Only) |
|
51 { |
|
52 Cache::Only = new (std::nothrow) Cache(); |
|
53 if(! Cache::Only) |
|
54 throw CacheException(CacheException::RESOURCE_ALLOCATION_FAILURE); |
|
55 } |
|
56 |
|
57 return Cache::Only; |
|
58 } |
|
59 |
|
60 |
|
61 void Cache::Initialize(void) throw (CacheException) |
|
62 { |
|
63 //create and open cache meta data file. |
|
64 |
|
65 |
|
66 metafile = CacheManager::GetInstance()->GetCacheRoot(); |
|
67 metafile += "/.rofsmeta"; |
|
68 boost::filesystem::path metafilepath(metafile.c_str()); |
|
69 if(!exists(metafilepath)) |
|
70 { |
|
71 //create cache root directory if it's not present. |
|
72 boost::filesystem::path createcacheroot(CacheManager::GetInstance()->GetCacheRoot()); |
|
73 create_directory(createcacheroot); |
|
74 |
|
75 //create cache index file. |
|
76 ofstream openmetafile(metafilepath.file_string().c_str(), ios_base::app | ios_base::out); |
|
77 if(! openmetafile.is_open()) |
|
78 throw CacheException(CacheException::CACHE_INVALID); |
|
79 openmetafile.close(); |
|
80 } |
|
81 printf("Loading cache meta file : %s\r\n", metafilepath.file_string().c_str()); |
|
82 ifstream metafileref(metafilepath.file_string().c_str(), ios_base::in); |
|
83 if(! metafileref.is_open()) |
|
84 throw CacheException(CacheException::HARDDRIVE_FAILURE); |
|
85 |
|
86 //read ROFS meta file and construct entry map. |
|
87 string inboundbuffer; |
|
88 while(getline(metafileref, inboundbuffer)) |
|
89 { |
|
90 //validate cache index record. |
|
91 if(! ValidateEntry(inboundbuffer)) |
|
92 throw CacheException(CacheException::CACHE_INVALID); |
|
93 |
|
94 //instantiate a new instance of class CacheEntry. |
|
95 CacheEntry* entryref = new (nothrow) CacheEntry(); |
|
96 if(!entryref) |
|
97 throw CacheException(CacheException::RESOURCE_ALLOCATION_FAILURE); |
|
98 |
|
99 //set entry's attributes. |
|
100 |
|
101 char* attrwalker = (char*)_alloca(inboundbuffer.length() + 1); |
|
102 memcpy(attrwalker,inboundbuffer.c_str(),inboundbuffer.length() + 1); |
|
103 |
|
104 char* start = attrwalker ; |
|
105 while(*start != ';') |
|
106 start++; |
|
107 *start++ = 0; |
|
108 entryref->SetOriginalFilename(attrwalker); |
|
109 attrwalker = start; |
|
110 while(*start != ';') |
|
111 start++; |
|
112 *start++ = 0; |
|
113 entryref->SetOriginalFileCreateTime(attrwalker); |
|
114 attrwalker = start; |
|
115 while(*start != ';') |
|
116 start++; |
|
117 *start++ = 0; |
|
118 entryref->SetOriginalFileCompression(attrwalker); |
|
119 attrwalker = start; |
|
120 while(*start != ';') |
|
121 start++; |
|
122 *start++ = 0; |
|
123 entryref->SetCachedFilename(attrwalker); |
|
124 attrwalker = start; |
|
125 entryref->SetCachedFileCompression(attrwalker); |
|
126 |
|
127 //add newly created entry into entry-map. |
|
128 string newentrystring(entryref->GetOriginalFilename()); |
|
129 CacheEntry* existentryref = entrymap[newentrystring]; |
|
130 if(existentryref) { |
|
131 while(existentryref->GetNextEntry()) |
|
132 existentryref = existentryref->GetNextEntry(); |
|
133 existentryref->AppendEntry(entryref); |
|
134 } |
|
135 else { |
|
136 entrymap[newentrystring] = entryref; |
|
137 } |
|
138 |
|
139 //reinitialize inbound buffer. |
|
140 inboundbuffer.clear(); |
|
141 } |
|
142 |
|
143 return; |
|
144 } |
|
145 |
|
146 |
|
147 CacheEntry* Cache::GetEntryList(const char* OriginalFilename) |
|
148 { |
|
149 //retrieval could be performed concurrently. |
|
150 boost::lock_guard<boost::mutex> lock(cachemutex); |
|
151 string originalfile(OriginalFilename); |
|
152 CacheEntry* resultentries = entrymap[originalfile]; |
|
153 |
|
154 return resultentries; |
|
155 } |
|
156 |
|
157 |
|
158 void Cache::AddEntry(const char* OriginalFilename, CacheEntry* EntryRef) |
|
159 { |
|
160 string originalfile(OriginalFilename); |
|
161 |
|
162 //addtions could be performed concurrently. |
|
163 boost::lock_guard<boost::mutex> lock(cachemutex); |
|
164 |
|
165 entrymap[originalfile] = EntryRef; |
|
166 |
|
167 return; |
|
168 } |
|
169 |
|
170 |
|
171 void Cache::CloseCache(void) throw (CacheException) |
|
172 { |
|
173 //open up the cache meta file. |
|
174 boost::filesystem::path metafilepath(metafile); |
|
175 ofstream metafileref; |
|
176 if(! exists(metafilepath)) |
|
177 metafileref.open(metafilepath.file_string().c_str(), ios_base::out | ios_base::app); |
|
178 else |
|
179 metafileref.open(metafilepath.file_string().c_str(), ios_base::out | ios_base::trunc); |
|
180 if(! metafileref.is_open()) |
|
181 throw CacheException(CacheException::HARDDRIVE_FAILURE); |
|
182 |
|
183 //save cache meta onto hard drive along with changed cache files. |
|
184 char* delimiter = ";"; |
|
185 map<string, CacheEntry*>::iterator mapitem; |
|
186 for(mapitem=entrymap.begin(); mapitem != entrymap.end(); mapitem++) |
|
187 { |
|
188 CacheEntry* concreteentryref = (*mapitem).second; |
|
189 while(concreteentryref) |
|
190 { |
|
191 metafileref.write(concreteentryref->GetOriginalFilename(), strlen(concreteentryref->GetOriginalFilename())); |
|
192 metafileref.write(delimiter, strlen(delimiter)); |
|
193 metafileref.write(concreteentryref->GetOriginalFileCreateTime(), strlen(concreteentryref->GetOriginalFileCreateTime())); |
|
194 metafileref.write(delimiter, strlen(delimiter)); |
|
195 metafileref.write(concreteentryref->GetOriginalFileCompressionID(), strlen(concreteentryref->GetOriginalFileCompressionID())); |
|
196 metafileref.write(delimiter, strlen(delimiter)); |
|
197 metafileref.write(concreteentryref->GetCachedFilename(), strlen(concreteentryref->GetCachedFilename())); |
|
198 metafileref.write(delimiter, strlen(delimiter)); |
|
199 metafileref.write(concreteentryref->GetCachedFileCompressionID(), strlen(concreteentryref->GetCachedFileCompressionID())); |
|
200 metafileref.write("\n", strlen("\n")); |
|
201 |
|
202 // CacheEntry* tobedeletedentryref = concreteentryref; |
|
203 concreteentryref = concreteentryref->GetNextEntry(); |
|
204 // delete tobedeletedentryref; |
|
205 } |
|
206 } |
|
207 |
|
208 //close cache meta file. |
|
209 metafileref.close(); |
|
210 |
|
211 return; |
|
212 } |
|
213 |
|
214 |
|
215 bool Cache::ValidateEntry(string& EntryRawText) |
|
216 { |
|
217 //an entry is formed as original_filename;original_file_create_time;original_file_compression_id;cached_filename;cached_file_compression_id(end of line - '\n') |
|
218 |
|
219 //format validation. |
|
220 int semicolon = 0; |
|
221 size_t semicolonposition = 0; |
|
222 while(1) { |
|
223 semicolonposition = EntryRawText.find(';', semicolonposition); |
|
224 if(semicolonposition != string::npos) { |
|
225 semicolonposition++; |
|
226 semicolon++; |
|
227 } |
|
228 else |
|
229 break; |
|
230 } |
|
231 if(semicolon != 4) |
|
232 return false; |
|
233 |
|
234 return true; |
|
235 } |
|
236 |
|
237 |
|
238 Cache::Cache(void) |
|
239 { |
|
240 return; |
|
241 } |
|
242 |