1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/cache/nsDeleteDir.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,456 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim:set ts=2 sw=2 sts=2 et cindent: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#include "nsDeleteDir.h" 1.11 +#include "nsIFile.h" 1.12 +#include "nsString.h" 1.13 +#include "mozilla/Telemetry.h" 1.14 +#include "nsITimer.h" 1.15 +#include "nsISimpleEnumerator.h" 1.16 +#include "nsAutoPtr.h" 1.17 +#include "nsThreadUtils.h" 1.18 +#include "nsISupportsPriority.h" 1.19 +#include "nsCacheUtils.h" 1.20 +#include "prtime.h" 1.21 +#include <time.h> 1.22 + 1.23 +using namespace mozilla; 1.24 + 1.25 +class nsBlockOnBackgroundThreadEvent : public nsRunnable { 1.26 +public: 1.27 + nsBlockOnBackgroundThreadEvent() {} 1.28 + NS_IMETHOD Run() 1.29 + { 1.30 + MutexAutoLock lock(nsDeleteDir::gInstance->mLock); 1.31 + nsDeleteDir::gInstance->mCondVar.Notify(); 1.32 + return NS_OK; 1.33 + } 1.34 +}; 1.35 + 1.36 + 1.37 +nsDeleteDir * nsDeleteDir::gInstance = nullptr; 1.38 + 1.39 +nsDeleteDir::nsDeleteDir() 1.40 + : mLock("nsDeleteDir.mLock"), 1.41 + mCondVar(mLock, "nsDeleteDir.mCondVar"), 1.42 + mShutdownPending(false), 1.43 + mStopDeleting(false) 1.44 +{ 1.45 + NS_ASSERTION(gInstance==nullptr, "multiple nsCacheService instances!"); 1.46 +} 1.47 + 1.48 +nsDeleteDir::~nsDeleteDir() 1.49 +{ 1.50 + gInstance = nullptr; 1.51 +} 1.52 + 1.53 +nsresult 1.54 +nsDeleteDir::Init() 1.55 +{ 1.56 + if (gInstance) 1.57 + return NS_ERROR_ALREADY_INITIALIZED; 1.58 + 1.59 + gInstance = new nsDeleteDir(); 1.60 + return NS_OK; 1.61 +} 1.62 + 1.63 +nsresult 1.64 +nsDeleteDir::Shutdown(bool finishDeleting) 1.65 +{ 1.66 + if (!gInstance) 1.67 + return NS_ERROR_NOT_INITIALIZED; 1.68 + 1.69 + nsCOMArray<nsIFile> dirsToRemove; 1.70 + nsCOMPtr<nsIThread> thread; 1.71 + { 1.72 + MutexAutoLock lock(gInstance->mLock); 1.73 + NS_ASSERTION(!gInstance->mShutdownPending, 1.74 + "Unexpected state in nsDeleteDir::Shutdown()"); 1.75 + gInstance->mShutdownPending = true; 1.76 + 1.77 + if (!finishDeleting) 1.78 + gInstance->mStopDeleting = true; 1.79 + 1.80 + // remove all pending timers 1.81 + for (int32_t i = gInstance->mTimers.Count(); i > 0; i--) { 1.82 + nsCOMPtr<nsITimer> timer = gInstance->mTimers[i-1]; 1.83 + gInstance->mTimers.RemoveObjectAt(i-1); 1.84 + timer->Cancel(); 1.85 + 1.86 + nsCOMArray<nsIFile> *arg; 1.87 + timer->GetClosure((reinterpret_cast<void**>(&arg))); 1.88 + 1.89 + if (finishDeleting) 1.90 + dirsToRemove.AppendObjects(*arg); 1.91 + 1.92 + // delete argument passed to the timer 1.93 + delete arg; 1.94 + } 1.95 + 1.96 + thread.swap(gInstance->mThread); 1.97 + if (thread) { 1.98 + // dispatch event and wait for it to run and notify us, so we know thread 1.99 + // has completed all work and can be shutdown 1.100 + nsCOMPtr<nsIRunnable> event = new nsBlockOnBackgroundThreadEvent(); 1.101 + nsresult rv = thread->Dispatch(event, NS_DISPATCH_NORMAL); 1.102 + if (NS_FAILED(rv)) { 1.103 + NS_WARNING("Failed dispatching block-event"); 1.104 + return NS_ERROR_UNEXPECTED; 1.105 + } 1.106 + 1.107 + rv = gInstance->mCondVar.Wait(); 1.108 + nsShutdownThread::BlockingShutdown(thread); 1.109 + } 1.110 + } 1.111 + 1.112 + delete gInstance; 1.113 + 1.114 + for (int32_t i = 0; i < dirsToRemove.Count(); i++) 1.115 + dirsToRemove[i]->Remove(true); 1.116 + 1.117 + return NS_OK; 1.118 +} 1.119 + 1.120 +nsresult 1.121 +nsDeleteDir::InitThread() 1.122 +{ 1.123 + if (mThread) 1.124 + return NS_OK; 1.125 + 1.126 + nsresult rv = NS_NewNamedThread("Cache Deleter", getter_AddRefs(mThread)); 1.127 + if (NS_FAILED(rv)) { 1.128 + NS_WARNING("Can't create background thread"); 1.129 + return rv; 1.130 + } 1.131 + 1.132 + nsCOMPtr<nsISupportsPriority> p = do_QueryInterface(mThread); 1.133 + if (p) { 1.134 + p->SetPriority(nsISupportsPriority::PRIORITY_LOWEST); 1.135 + } 1.136 + return NS_OK; 1.137 +} 1.138 + 1.139 +void 1.140 +nsDeleteDir::DestroyThread() 1.141 +{ 1.142 + if (!mThread) 1.143 + return; 1.144 + 1.145 + if (mTimers.Count()) 1.146 + // more work to do, so don't delete thread. 1.147 + return; 1.148 + 1.149 + nsShutdownThread::Shutdown(mThread); 1.150 + mThread = nullptr; 1.151 +} 1.152 + 1.153 +void 1.154 +nsDeleteDir::TimerCallback(nsITimer *aTimer, void *arg) 1.155 +{ 1.156 + Telemetry::AutoTimer<Telemetry::NETWORK_DISK_CACHE_DELETEDIR> timer; 1.157 + { 1.158 + MutexAutoLock lock(gInstance->mLock); 1.159 + 1.160 + int32_t idx = gInstance->mTimers.IndexOf(aTimer); 1.161 + if (idx == -1) { 1.162 + // Timer was canceled and removed during shutdown. 1.163 + return; 1.164 + } 1.165 + 1.166 + gInstance->mTimers.RemoveObjectAt(idx); 1.167 + } 1.168 + 1.169 + nsAutoPtr<nsCOMArray<nsIFile> > dirList; 1.170 + dirList = static_cast<nsCOMArray<nsIFile> *>(arg); 1.171 + 1.172 + bool shuttingDown = false; 1.173 + 1.174 + // Intentional extra braces to control variable sope. 1.175 + { 1.176 + // Low IO priority can only be set when running in the context of the 1.177 + // current thread. So this shouldn't be moved to where we set the priority 1.178 + // of the Cache deleter thread using the nsThread's NSPR priority constants. 1.179 + nsAutoLowPriorityIO autoLowPriority; 1.180 + for (int32_t i = 0; i < dirList->Count() && !shuttingDown; i++) { 1.181 + gInstance->RemoveDir((*dirList)[i], &shuttingDown); 1.182 + } 1.183 + } 1.184 + 1.185 + { 1.186 + MutexAutoLock lock(gInstance->mLock); 1.187 + gInstance->DestroyThread(); 1.188 + } 1.189 +} 1.190 + 1.191 +nsresult 1.192 +nsDeleteDir::DeleteDir(nsIFile *dirIn, bool moveToTrash, uint32_t delay) 1.193 +{ 1.194 + Telemetry::AutoTimer<Telemetry::NETWORK_DISK_CACHE_TRASHRENAME> timer; 1.195 + 1.196 + if (!gInstance) 1.197 + return NS_ERROR_NOT_INITIALIZED; 1.198 + 1.199 + nsresult rv; 1.200 + nsCOMPtr<nsIFile> trash, dir; 1.201 + 1.202 + // Need to make a clone of this since we don't want to modify the input 1.203 + // file object. 1.204 + rv = dirIn->Clone(getter_AddRefs(dir)); 1.205 + if (NS_FAILED(rv)) 1.206 + return rv; 1.207 + 1.208 + if (moveToTrash) { 1.209 + rv = GetTrashDir(dir, &trash); 1.210 + if (NS_FAILED(rv)) 1.211 + return rv; 1.212 + nsAutoCString origLeaf; 1.213 + rv = trash->GetNativeLeafName(origLeaf); 1.214 + if (NS_FAILED(rv)) 1.215 + return rv; 1.216 + 1.217 + // Append random number to the trash directory and check if it exists. 1.218 + srand(static_cast<unsigned>(PR_Now())); 1.219 + nsAutoCString leaf; 1.220 + for (int32_t i = 0; i < 10; i++) { 1.221 + leaf = origLeaf; 1.222 + leaf.AppendInt(rand()); 1.223 + rv = trash->SetNativeLeafName(leaf); 1.224 + if (NS_FAILED(rv)) 1.225 + return rv; 1.226 + 1.227 + bool exists; 1.228 + if (NS_SUCCEEDED(trash->Exists(&exists)) && !exists) { 1.229 + break; 1.230 + } 1.231 + 1.232 + leaf.Truncate(); 1.233 + } 1.234 + 1.235 + // Fail if we didn't find unused trash directory within the limit 1.236 + if (!leaf.Length()) 1.237 + return NS_ERROR_FAILURE; 1.238 + 1.239 +#if defined(MOZ_WIDGET_ANDROID) 1.240 + nsCOMPtr<nsIFile> parent; 1.241 + rv = trash->GetParent(getter_AddRefs(parent)); 1.242 + if (NS_FAILED(rv)) 1.243 + return rv; 1.244 + rv = dir->MoveToNative(parent, leaf); 1.245 +#else 1.246 + // Important: must rename directory w/o changing parent directory: else on 1.247 + // NTFS we'll wait (with cache lock) while nsIFile's ACL reset walks file 1.248 + // tree: was hanging GUI for *minutes* on large cache dirs. 1.249 + rv = dir->MoveToNative(nullptr, leaf); 1.250 +#endif 1.251 + if (NS_FAILED(rv)) 1.252 + return rv; 1.253 + } else { 1.254 + // we want to pass a clone of the original off to the worker thread. 1.255 + trash.swap(dir); 1.256 + } 1.257 + 1.258 + nsAutoPtr<nsCOMArray<nsIFile> > arg(new nsCOMArray<nsIFile>); 1.259 + arg->AppendObject(trash); 1.260 + 1.261 + rv = gInstance->PostTimer(arg, delay); 1.262 + if (NS_FAILED(rv)) 1.263 + return rv; 1.264 + 1.265 + arg.forget(); 1.266 + return NS_OK; 1.267 +} 1.268 + 1.269 +nsresult 1.270 +nsDeleteDir::GetTrashDir(nsIFile *target, nsCOMPtr<nsIFile> *result) 1.271 +{ 1.272 + nsresult rv; 1.273 +#if defined(MOZ_WIDGET_ANDROID) 1.274 + // Try to use the app cache folder for cache trash on Android 1.275 + char* cachePath = getenv("CACHE_DIRECTORY"); 1.276 + if (cachePath) { 1.277 + rv = NS_NewNativeLocalFile(nsDependentCString(cachePath), 1.278 + true, getter_AddRefs(*result)); 1.279 + if (NS_FAILED(rv)) 1.280 + return rv; 1.281 + 1.282 + // Add a sub folder with the cache folder name 1.283 + nsAutoCString leaf; 1.284 + rv = target->GetNativeLeafName(leaf); 1.285 + (*result)->AppendNative(leaf); 1.286 + } else 1.287 +#endif 1.288 + { 1.289 + rv = target->Clone(getter_AddRefs(*result)); 1.290 + } 1.291 + if (NS_FAILED(rv)) 1.292 + return rv; 1.293 + 1.294 + nsAutoCString leaf; 1.295 + rv = (*result)->GetNativeLeafName(leaf); 1.296 + if (NS_FAILED(rv)) 1.297 + return rv; 1.298 + leaf.AppendLiteral(".Trash"); 1.299 + 1.300 + return (*result)->SetNativeLeafName(leaf); 1.301 +} 1.302 + 1.303 +nsresult 1.304 +nsDeleteDir::RemoveOldTrashes(nsIFile *cacheDir) 1.305 +{ 1.306 + if (!gInstance) 1.307 + return NS_ERROR_NOT_INITIALIZED; 1.308 + 1.309 + nsresult rv; 1.310 + 1.311 + static bool firstRun = true; 1.312 + 1.313 + if (!firstRun) 1.314 + return NS_OK; 1.315 + 1.316 + firstRun = false; 1.317 + 1.318 + nsCOMPtr<nsIFile> trash; 1.319 + rv = GetTrashDir(cacheDir, &trash); 1.320 + if (NS_FAILED(rv)) 1.321 + return rv; 1.322 + 1.323 + nsAutoString trashName; 1.324 + rv = trash->GetLeafName(trashName); 1.325 + if (NS_FAILED(rv)) 1.326 + return rv; 1.327 + 1.328 + nsCOMPtr<nsIFile> parent; 1.329 +#if defined(MOZ_WIDGET_ANDROID) 1.330 + rv = trash->GetParent(getter_AddRefs(parent)); 1.331 +#else 1.332 + rv = cacheDir->GetParent(getter_AddRefs(parent)); 1.333 +#endif 1.334 + if (NS_FAILED(rv)) 1.335 + return rv; 1.336 + 1.337 + nsCOMPtr<nsISimpleEnumerator> iter; 1.338 + rv = parent->GetDirectoryEntries(getter_AddRefs(iter)); 1.339 + if (NS_FAILED(rv)) 1.340 + return rv; 1.341 + 1.342 + bool more; 1.343 + nsCOMPtr<nsISupports> elem; 1.344 + nsAutoPtr<nsCOMArray<nsIFile> > dirList; 1.345 + 1.346 + while (NS_SUCCEEDED(iter->HasMoreElements(&more)) && more) { 1.347 + rv = iter->GetNext(getter_AddRefs(elem)); 1.348 + if (NS_FAILED(rv)) 1.349 + continue; 1.350 + 1.351 + nsCOMPtr<nsIFile> file = do_QueryInterface(elem); 1.352 + if (!file) 1.353 + continue; 1.354 + 1.355 + nsAutoString leafName; 1.356 + rv = file->GetLeafName(leafName); 1.357 + if (NS_FAILED(rv)) 1.358 + continue; 1.359 + 1.360 + // match all names that begin with the trash name (i.e. "Cache.Trash") 1.361 + if (Substring(leafName, 0, trashName.Length()).Equals(trashName)) { 1.362 + if (!dirList) 1.363 + dirList = new nsCOMArray<nsIFile>; 1.364 + dirList->AppendObject(file); 1.365 + } 1.366 + } 1.367 + 1.368 + if (dirList) { 1.369 + rv = gInstance->PostTimer(dirList, 90000); 1.370 + if (NS_FAILED(rv)) 1.371 + return rv; 1.372 + 1.373 + dirList.forget(); 1.374 + } 1.375 + 1.376 + return NS_OK; 1.377 +} 1.378 + 1.379 +nsresult 1.380 +nsDeleteDir::PostTimer(void *arg, uint32_t delay) 1.381 +{ 1.382 + nsresult rv; 1.383 + 1.384 + nsCOMPtr<nsITimer> timer = do_CreateInstance("@mozilla.org/timer;1", &rv); 1.385 + if (NS_FAILED(rv)) 1.386 + return NS_ERROR_UNEXPECTED; 1.387 + 1.388 + MutexAutoLock lock(mLock); 1.389 + 1.390 + rv = InitThread(); 1.391 + if (NS_FAILED(rv)) 1.392 + return rv; 1.393 + 1.394 + rv = timer->SetTarget(mThread); 1.395 + if (NS_FAILED(rv)) 1.396 + return rv; 1.397 + 1.398 + rv = timer->InitWithFuncCallback(TimerCallback, arg, delay, 1.399 + nsITimer::TYPE_ONE_SHOT); 1.400 + if (NS_FAILED(rv)) 1.401 + return rv; 1.402 + 1.403 + mTimers.AppendObject(timer); 1.404 + return NS_OK; 1.405 +} 1.406 + 1.407 +nsresult 1.408 +nsDeleteDir::RemoveDir(nsIFile *file, bool *stopDeleting) 1.409 +{ 1.410 + nsresult rv; 1.411 + bool isLink; 1.412 + 1.413 + rv = file->IsSymlink(&isLink); 1.414 + if (NS_FAILED(rv) || isLink) 1.415 + return NS_ERROR_UNEXPECTED; 1.416 + 1.417 + bool isDir; 1.418 + rv = file->IsDirectory(&isDir); 1.419 + if (NS_FAILED(rv)) 1.420 + return rv; 1.421 + 1.422 + if (isDir) { 1.423 + nsCOMPtr<nsISimpleEnumerator> iter; 1.424 + rv = file->GetDirectoryEntries(getter_AddRefs(iter)); 1.425 + if (NS_FAILED(rv)) 1.426 + return rv; 1.427 + 1.428 + bool more; 1.429 + nsCOMPtr<nsISupports> elem; 1.430 + while (NS_SUCCEEDED(iter->HasMoreElements(&more)) && more) { 1.431 + rv = iter->GetNext(getter_AddRefs(elem)); 1.432 + if (NS_FAILED(rv)) { 1.433 + NS_WARNING("Unexpected failure in nsDeleteDir::RemoveDir"); 1.434 + continue; 1.435 + } 1.436 + 1.437 + nsCOMPtr<nsIFile> file2 = do_QueryInterface(elem); 1.438 + if (!file2) { 1.439 + NS_WARNING("Unexpected failure in nsDeleteDir::RemoveDir"); 1.440 + continue; 1.441 + } 1.442 + 1.443 + RemoveDir(file2, stopDeleting); 1.444 + // No check for errors to remove as much as possible 1.445 + 1.446 + if (*stopDeleting) 1.447 + return NS_OK; 1.448 + } 1.449 + } 1.450 + 1.451 + file->Remove(false); 1.452 + // No check for errors to remove as much as possible 1.453 + 1.454 + MutexAutoLock lock(mLock); 1.455 + if (mStopDeleting) 1.456 + *stopDeleting = true; 1.457 + 1.458 + return NS_OK; 1.459 +}