OLD | NEW |
| (Empty) |
1 // Copyright 2009 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 | |
16 #include "omaha/goopdate/package_cache.h" | |
17 #include <shlwapi.h> | |
18 #include "omaha/base/debug.h" | |
19 #include "omaha/base/error.h" | |
20 #include "omaha/base/file.h" | |
21 #include "omaha/base/logging.h" | |
22 #include "omaha/base/path.h" | |
23 #include "omaha/base/string.h" | |
24 #include "omaha/base/signatures.h" | |
25 #include "omaha/base/utils.h" | |
26 #include "omaha/common/config_manager.h" | |
27 #include "omaha/goopdate/package_cache_internal.h" | |
28 #include "omaha/goopdate/worker_metrics.h" | |
29 | |
30 namespace omaha { | |
31 | |
32 namespace internal { | |
33 | |
34 bool PackageSortByTimePredicate(const PackageInfo& package1, | |
35 const PackageInfo& package2) { | |
36 return ::CompareFileTime(&package1.file_time, &package2.file_time) > 0; | |
37 } | |
38 | |
39 bool IsSpecialDirectoryFindData(const WIN32_FIND_DATA& find_data) { | |
40 return find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY && | |
41 (String_StrNCmp(find_data.cFileName, _T("."), 2, false) == 0 || | |
42 String_StrNCmp(find_data.cFileName, _T(".."), 3, false) == 0); | |
43 } | |
44 | |
45 bool IsSubDirectoryFindData(const WIN32_FIND_DATA& find_data) { | |
46 return find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY && | |
47 !IsSpecialDirectoryFindData(find_data); | |
48 } | |
49 | |
50 bool IsFileFindData(const WIN32_FIND_DATA& find_data) { | |
51 return ((find_data.dwFileAttributes == FILE_ATTRIBUTE_NORMAL) || | |
52 !(find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)); | |
53 } | |
54 | |
55 HRESULT FindAllPackagesInfo(const CString& cache_root, | |
56 std::vector<PackageInfo>* packages_info) { | |
57 ASSERT1(packages_info); | |
58 ASSERT1(packages_info->empty()); | |
59 return FindDirectoryPackagesInfo(cache_root, | |
60 CACHE_DIRECTORY_ROOT, | |
61 packages_info); | |
62 } | |
63 | |
64 HRESULT FindAppPackagesInfo(const CString& app_dir, | |
65 std::vector<PackageInfo>* packages_info) { | |
66 return FindDirectoryPackagesInfo(app_dir, CACHE_DIRECTORY_APP, packages_info); | |
67 } | |
68 | |
69 HRESULT FindVersionPackagesInfo(const CString& version_dir, | |
70 std::vector<PackageInfo>* packages_info) { | |
71 return FindDirectoryPackagesInfo(version_dir, | |
72 CACHE_DIRECTORY_VERSION, | |
73 packages_info); | |
74 } | |
75 | |
76 HRESULT FindFilePackagesInfo(const CString& version_dir, | |
77 const WIN32_FIND_DATA& find_data, | |
78 std::vector<PackageInfo>* packages_info) { | |
79 PackageInfo package_info; | |
80 package_info.file_name = ConcatenatePath(version_dir, find_data.cFileName); | |
81 package_info.file_time = ::CompareFileTime(&find_data.ftCreationTime, | |
82 &find_data.ftLastWriteTime) ? | |
83 find_data.ftCreationTime : | |
84 find_data.ftLastWriteTime; | |
85 package_info.file_size.LowPart = find_data.nFileSizeLow; | |
86 package_info.file_size.HighPart = find_data.nFileSizeHigh; | |
87 packages_info->push_back(package_info); | |
88 | |
89 return S_OK; | |
90 } | |
91 | |
92 HRESULT FindDirectoryPackagesInfo(const CString& dir_path, | |
93 CacheDirectoryType dir_type, | |
94 std::vector<PackageInfo>* packages_info) { | |
95 CORE_LOG(L4, (_T("[FindDirectoryPackagesInfo][%s][%d]"), dir_path, dir_type)); | |
96 | |
97 WIN32_FIND_DATA find_data = {0}; | |
98 scoped_hfind hfind(::FindFirstFile(dir_path + _T("\\*"), &find_data)); | |
99 if (!hfind) { | |
100 HRESULT hr = HRESULTFromLastError(); | |
101 CORE_LOG(L4, (_T("[FindDirectoryPackagesInfo failed][0x%x]"), hr)); | |
102 return hr; | |
103 } | |
104 | |
105 HRESULT hr = S_OK; | |
106 do { | |
107 switch (dir_type) { | |
108 case CACHE_DIRECTORY_ROOT: | |
109 if (IsSubDirectoryFindData(find_data)) { | |
110 CString app_dir = ConcatenatePath(dir_path, find_data.cFileName); | |
111 hr = FindAppPackagesInfo(app_dir, packages_info); | |
112 } | |
113 break; | |
114 case CACHE_DIRECTORY_APP: | |
115 if (IsSubDirectoryFindData(find_data)) { | |
116 CString version_dir = ConcatenatePath(dir_path, find_data.cFileName); | |
117 hr = FindVersionPackagesInfo(version_dir, packages_info); | |
118 } | |
119 break; | |
120 case CACHE_DIRECTORY_VERSION: | |
121 if (IsFileFindData(find_data)) { | |
122 hr = FindFilePackagesInfo(dir_path, find_data, packages_info); | |
123 } | |
124 break; | |
125 } | |
126 | |
127 if (FAILED(hr)) { | |
128 CORE_LOG(L4, (_T("[FindDirectoryPackagesInfo failed][0x%x]"), hr)); | |
129 return hr; | |
130 } | |
131 } while (::FindNextFile(get(hfind), &find_data)); | |
132 | |
133 return S_OK; | |
134 } | |
135 | |
136 void SortPackageInfoByTime(std::vector<PackageInfo>* packages_info) { | |
137 std::sort(packages_info->begin(), | |
138 packages_info->end(), | |
139 PackageSortByTimePredicate); | |
140 } | |
141 | |
142 } // namespace internal | |
143 | |
144 PackageCache::PackageCache() { | |
145 cache_time_limit_days_ = | |
146 ConfigManager::Instance()->GetPackageCacheExpirationTimeDays(); | |
147 | |
148 cache_size_limit_bytes_ = 1024 * 1024 * static_cast<uint64>( | |
149 ConfigManager::Instance()->GetPackageCacheSizeLimitMBytes()); | |
150 } | |
151 | |
152 PackageCache::~PackageCache() { | |
153 } | |
154 | |
155 HRESULT PackageCache::Initialize(const CString& cache_root) { | |
156 CORE_LOG(L3, (_T("[PackageCache::Initialize][%s]"), cache_root)); | |
157 | |
158 __mutexScope(cache_lock_); | |
159 | |
160 if (!IsAbsolutePath(cache_root)) { | |
161 return E_INVALIDARG; | |
162 } | |
163 | |
164 HRESULT hr = CreateDir(cache_root, NULL); | |
165 if (FAILED(hr)) { | |
166 CORE_LOG(LE, (_T("[CreateDir failed][0x%x][%s]"), hr, cache_root)); | |
167 return hr; | |
168 } | |
169 | |
170 cache_root_ = cache_root; | |
171 | |
172 return S_OK; | |
173 } | |
174 | |
175 bool PackageCache::IsCached(const Key& key, const CString& hash) const { | |
176 CORE_LOG(L3, (_T("[PackageCache::IsCached][key '%s'][hash %s]"), | |
177 key.ToString(), hash)); | |
178 | |
179 __mutexScope(cache_lock_); | |
180 | |
181 CString filename; | |
182 HRESULT hr = BuildCacheFileNameForKey(key, &filename); | |
183 if (FAILED(hr)) { | |
184 return false; | |
185 } | |
186 | |
187 return File::Exists(filename) && SUCCEEDED(AuthenticateFile(filename, hash)); | |
188 } | |
189 | |
190 HRESULT PackageCache::Put(const Key& key, | |
191 const CString& source_file, | |
192 const CString& hash) { | |
193 ++metric_worker_package_cache_put_total; | |
194 CORE_LOG(L3, (_T("[PackageCache::Put][key '%s'][source_file '%s'][hash %s]"), | |
195 key.ToString(), source_file, hash)); | |
196 | |
197 __mutexScope(cache_lock_); | |
198 | |
199 if (key.app_id().IsEmpty() || key.version().IsEmpty() || | |
200 key.package_name().IsEmpty() ) { | |
201 return E_INVALIDARG; | |
202 } | |
203 | |
204 CString destination_file; | |
205 HRESULT hr = BuildCacheFileNameForKey(key, &destination_file); | |
206 CORE_LOG(L3, (_T("[destination file '%s']"), destination_file)); | |
207 if (FAILED(hr)) { | |
208 return hr; | |
209 } | |
210 | |
211 hr = CreateDir(GetDirectoryFromPath(destination_file), NULL); | |
212 if (FAILED(hr)) { | |
213 CORE_LOG(LE, (_T("[failed to create cache directory][0x%08x][%s]"), | |
214 hr, destination_file)); | |
215 return hr; | |
216 } | |
217 | |
218 // TODO(omaha): consider not overwriting the file if the file is | |
219 // in the cache and it is valid. | |
220 | |
221 // When not impersonated, File::Copy resets the ownership of the destination | |
222 // file and it inherits ACEs from the new parent directory. | |
223 hr = File::Copy(source_file, destination_file, true); | |
224 if (FAILED(hr)) { | |
225 CORE_LOG(LE, (_T("[failed to copy file to cache][0x%08x][%s]"), | |
226 hr, destination_file)); | |
227 return hr; | |
228 } | |
229 | |
230 hr = AuthenticateFile(destination_file, hash); | |
231 if (FAILED(hr)) { | |
232 CORE_LOG(LE, (_T("[failed to authenticate '%s'][%s]"), | |
233 destination_file, hash)); | |
234 VERIFY1(SUCCEEDED(::DeleteFile(destination_file))); | |
235 return hr; | |
236 } | |
237 | |
238 ++metric_worker_package_cache_put_succeeded; | |
239 return S_OK; | |
240 } | |
241 | |
242 HRESULT PackageCache::Get(const Key& key, | |
243 const CString& destination_file, | |
244 const CString& hash) const { | |
245 CORE_LOG(L3, (_T("[PackageCache::Get][key '%s'][dest file '%s'][hash '%s']"), | |
246 key.ToString(), destination_file, hash)); | |
247 | |
248 __mutexScope(cache_lock_); | |
249 | |
250 if (key.app_id().IsEmpty() || key.version().IsEmpty() || | |
251 key.package_name().IsEmpty() ) { | |
252 return E_INVALIDARG; | |
253 } | |
254 | |
255 CString source_file; | |
256 HRESULT hr = BuildCacheFileNameForKey(key, &source_file); | |
257 CORE_LOG(L3, (_T("[source file '%s']"), source_file)); | |
258 if (FAILED(hr)) { | |
259 return hr; | |
260 } | |
261 | |
262 if (!File::Exists(source_file)) { | |
263 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND); | |
264 } | |
265 | |
266 hr = AuthenticateFile(source_file, hash); | |
267 if (FAILED(hr)) { | |
268 CORE_LOG(LE, (_T("[failed to authenticate '%s']"), source_file)); | |
269 return hr; | |
270 } | |
271 | |
272 return File::Copy(source_file, destination_file, true); | |
273 } | |
274 | |
275 HRESULT PackageCache::Purge(const Key& key) { | |
276 CORE_LOG(L3, (_T("[PackageCache::Purge][key '%s']"), key.ToString())); | |
277 | |
278 __mutexScope(cache_lock_); | |
279 | |
280 return Delete(key.app_id(), key.version(), key.package_name()); | |
281 } | |
282 | |
283 HRESULT PackageCache::PurgeVersion(const CString& app_id, | |
284 const CString& version) { | |
285 CORE_LOG(L3, (_T("[PackageCache::PurgeVersion][app_id '%s'][version '%s']"), | |
286 app_id, version)); | |
287 | |
288 __mutexScope(cache_lock_); | |
289 | |
290 return Delete(app_id, version, _T("")); | |
291 } | |
292 | |
293 HRESULT PackageCache::PurgeApp(const CString& app_id) { | |
294 CORE_LOG(L3, (_T("[PackageCache::PurgeApp][app_id '%s']"), app_id)); | |
295 | |
296 __mutexScope(cache_lock_); | |
297 | |
298 return Delete(app_id, _T(""), _T("")); | |
299 } | |
300 | |
301 HRESULT PackageCache::PurgeAppLowerVersions(const CString& app_id, | |
302 const CString& version) { | |
303 CORE_LOG(L3, (_T("[PackageCache::PurgeAppLowerVersions][%s][%s]"), | |
304 app_id, version)); | |
305 | |
306 __mutexScope(cache_lock_); | |
307 | |
308 ULONGLONG my_version = VersionFromString(version); | |
309 if (!my_version) { | |
310 return E_INVALIDARG; | |
311 } | |
312 | |
313 CString app_id_path; | |
314 HRESULT hr = BuildCacheFileName(app_id, CString(), CString(), &app_id_path); | |
315 if (FAILED(hr)) { | |
316 CORE_LOG(LE, (_T("[BuildCacheFileName fail][%s][0x%x]"), app_id_path, hr)); | |
317 return hr; | |
318 } | |
319 | |
320 WIN32_FIND_DATA find_data = {0}; | |
321 scoped_hfind hfind(::FindFirstFile(app_id_path + _T("\\*"), &find_data)); | |
322 if (!hfind) { | |
323 HRESULT hr = HRESULTFromLastError(); | |
324 CORE_LOG(L4, (_T("[FindFirstFile failed][%s][0x%x]"), app_id_path, hr)); | |
325 return hr; | |
326 } | |
327 | |
328 do { | |
329 if (internal::IsSpecialDirectoryFindData(find_data)) { | |
330 continue; | |
331 } | |
332 ASSERT1(internal::IsSubDirectoryFindData(find_data)); | |
333 | |
334 ULONGLONG found_version = VersionFromString(find_data.cFileName); | |
335 if (!found_version || found_version >= my_version) { | |
336 CORE_LOG(L2, (_T("[Not purging version][%s]"), find_data.cFileName)); | |
337 continue; | |
338 } | |
339 | |
340 CString version_dir = ConcatenatePath(app_id_path, find_data.cFileName); | |
341 hr = DeleteBeforeOrAfterReboot(version_dir); | |
342 CORE_LOG(L3, (_T("[Purge version][%s][0x%x]"), version_dir, hr)); | |
343 } while (::FindNextFile(get(hfind), &find_data)); | |
344 | |
345 return S_OK; | |
346 } | |
347 | |
348 HRESULT PackageCache::PurgeAll() { | |
349 CORE_LOG(L3, (_T("[PackageCache::PurgeAll]"))); | |
350 | |
351 __mutexScope(cache_lock_); | |
352 | |
353 // Deletes the cache root including all the cache entries. | |
354 HRESULT hr = Delete(_T(""), _T(""), _T("")); | |
355 if (FAILED(hr)) { | |
356 return hr; | |
357 } | |
358 | |
359 // Recreate the cache root. | |
360 hr = CreateDir(cache_root_, NULL); | |
361 if (FAILED(hr)) { | |
362 CORE_LOG(LW, (_T("[CreateDir failed][0x%x][%s]"), hr, cache_root_)); | |
363 return hr; | |
364 } | |
365 | |
366 return hr; | |
367 } | |
368 | |
369 FILETIME PackageCache::GetCacheExpirationTime() const { | |
370 FILETIME current_time = {0}; | |
371 ::GetSystemTimeAsFileTime(¤t_time); | |
372 ULARGE_INTEGER now = {0}; | |
373 now.LowPart = current_time.dwLowDateTime; | |
374 now.HighPart = current_time.dwHighDateTime; | |
375 | |
376 const uint64 kNum100NanoSecondsInDay = 1000LL * 1000 * 10 * kSecondsPerDay; | |
377 now.QuadPart -= kNum100NanoSecondsInDay * cache_time_limit_days_; | |
378 | |
379 FILETIME expiration_time = {0}; | |
380 expiration_time.dwLowDateTime = now.LowPart; | |
381 expiration_time.dwHighDateTime = now.HighPart; | |
382 | |
383 return expiration_time; | |
384 } | |
385 | |
386 HRESULT PackageCache::PurgeOldPackagesIfNecessary() const { | |
387 __mutexScope(cache_lock_); | |
388 | |
389 std::vector<internal::PackageInfo> packages_info; | |
390 HRESULT hr = internal::FindAllPackagesInfo(cache_root_, &packages_info); | |
391 | |
392 if (FAILED(hr)) { | |
393 CORE_LOG(LE, (_T("[internal::FindAllPackagesInfo failed][0x%x]"), hr)); | |
394 return hr; | |
395 } | |
396 internal::SortPackageInfoByTime(&packages_info); | |
397 | |
398 FILETIME expiration_time = GetCacheExpirationTime(); | |
399 | |
400 // Delete cached package based on the package info. | |
401 std::vector<internal::PackageInfo>::const_iterator it; | |
402 uint64 total_cache_size = 0; | |
403 for (it = packages_info.begin(); it != packages_info.end(); ++it) { | |
404 total_cache_size += it->file_size.QuadPart; | |
405 if (total_cache_size > cache_size_limit_bytes_) { | |
406 break; // Remaining packages should be deleted as size limit is reached. | |
407 } | |
408 if (::CompareFileTime(&it->file_time, &expiration_time) < 0) { | |
409 break; // Remaining packages should be deleted as they are expired. | |
410 } | |
411 } | |
412 | |
413 for (; it != packages_info.end(); ++it) { | |
414 hr = DeleteBeforeOrAfterReboot(it->file_name); | |
415 } | |
416 | |
417 return hr; | |
418 } | |
419 | |
420 HRESULT PackageCache::Delete(const CString& app_id, | |
421 const CString& version, | |
422 const CString& package_name) { | |
423 CString filename; | |
424 HRESULT hr = BuildCacheFileName(app_id, version, package_name, &filename); | |
425 CORE_LOG(L3, (_T("[PackageCache::Delete '%s']"), filename)); | |
426 if (FAILED(hr)) { | |
427 return hr; | |
428 } | |
429 | |
430 return DeleteBeforeOrAfterReboot(filename); | |
431 } | |
432 | |
433 CString PackageCache::cache_root() const { | |
434 __mutexScope(cache_lock_); | |
435 | |
436 ASSERT1(!cache_root_.IsEmpty()); | |
437 ASSERT1(File::Exists(cache_root_)); | |
438 | |
439 return cache_root_; | |
440 } | |
441 | |
442 uint64 PackageCache::Size() const { | |
443 uint64 result(0); | |
444 return SUCCEEDED(GetDirectorySize(cache_root_, &result)) ? result : 0; | |
445 } | |
446 | |
447 HRESULT PackageCache::BuildCacheFileNameForKey(const Key& key, | |
448 CString* filename) const { | |
449 ASSERT1(filename); | |
450 | |
451 return BuildCacheFileName(key.app_id(), | |
452 key.version(), | |
453 key.package_name(), | |
454 filename); | |
455 } | |
456 | |
457 HRESULT PackageCache::BuildCacheFileName(const CString& app_id, | |
458 const CString& version, | |
459 const CString& package_name, | |
460 CString* filename) const { | |
461 ASSERT1(filename); | |
462 | |
463 // Validate the package name does not contain the "..". | |
464 if (package_name.Find(_T("..")) != -1) { | |
465 return E_INVALIDARG; | |
466 } | |
467 | |
468 CString tmp_filename; | |
469 tmp_filename = ConcatenatePath(cache_root_, app_id); | |
470 tmp_filename = ConcatenatePath(tmp_filename, version); | |
471 tmp_filename = ConcatenatePath(tmp_filename, package_name); | |
472 | |
473 *filename = tmp_filename; | |
474 | |
475 return S_OK; | |
476 } | |
477 | |
478 HRESULT PackageCache::AuthenticateFile(const CString& filename, | |
479 const CString& hash) { | |
480 CORE_LOG(L3, (_T("[PackageCache::AuthenticateFile][%s][%s]"), | |
481 filename, hash)); | |
482 HighresTimer authentication_timer; | |
483 | |
484 std::vector<CString> files; | |
485 files.push_back(filename); | |
486 HRESULT hr = AuthenticateFiles(files, hash); | |
487 CORE_LOG(L3, (_T("[PackageCache::AuthenticateFile completed][0x%08x][%d ms]"), | |
488 hr, authentication_timer.GetElapsedMs())); | |
489 | |
490 return hr; | |
491 } | |
492 | |
493 } // namespace omaha | |
494 | |
OLD | NEW |