/src/mozilla-central/netwerk/cache/nsDeleteDir.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim:set ts=2 sw=2 sts=2 et cindent: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #include "nsDeleteDir.h" |
8 | | #include "nsIFile.h" |
9 | | #include "nsString.h" |
10 | | #include "mozilla/Telemetry.h" |
11 | | #include "nsITimer.h" |
12 | | #include "nsISimpleEnumerator.h" |
13 | | #include "nsAutoPtr.h" |
14 | | #include "nsThreadUtils.h" |
15 | | #include "nsISupportsPriority.h" |
16 | | #include "nsCacheUtils.h" |
17 | | #include "prtime.h" |
18 | | #include <time.h> |
19 | | |
20 | | using namespace mozilla; |
21 | | |
22 | | class nsBlockOnBackgroundThreadEvent : public Runnable { |
23 | | public: |
24 | | nsBlockOnBackgroundThreadEvent() |
25 | | : mozilla::Runnable("nsBlockOnBackgroundThreadEvent") |
26 | 0 | { |
27 | 0 | } |
28 | | NS_IMETHOD Run() override |
29 | 0 | { |
30 | 0 | MutexAutoLock lock(nsDeleteDir::gInstance->mLock); |
31 | 0 | nsDeleteDir::gInstance->mNotified = true; |
32 | 0 | nsDeleteDir::gInstance->mCondVar.Notify(); |
33 | 0 | return NS_OK; |
34 | 0 | } |
35 | | }; |
36 | | |
37 | | |
38 | | nsDeleteDir * nsDeleteDir::gInstance = nullptr; |
39 | | |
40 | | nsDeleteDir::nsDeleteDir() |
41 | | : mLock("nsDeleteDir.mLock"), |
42 | | mCondVar(mLock, "nsDeleteDir.mCondVar"), |
43 | | mNotified(false), |
44 | | mShutdownPending(false), |
45 | | mStopDeleting(false) |
46 | 0 | { |
47 | 0 | NS_ASSERTION(gInstance==nullptr, "multiple nsCacheService instances!"); |
48 | 0 | } |
49 | | |
50 | | nsDeleteDir::~nsDeleteDir() |
51 | 0 | { |
52 | 0 | gInstance = nullptr; |
53 | 0 | } |
54 | | |
55 | | nsresult |
56 | | nsDeleteDir::Init() |
57 | 0 | { |
58 | 0 | if (gInstance) |
59 | 0 | return NS_ERROR_ALREADY_INITIALIZED; |
60 | 0 | |
61 | 0 | gInstance = new nsDeleteDir(); |
62 | 0 | return NS_OK; |
63 | 0 | } |
64 | | |
65 | | nsresult |
66 | | nsDeleteDir::Shutdown(bool finishDeleting) |
67 | 0 | { |
68 | 0 | if (!gInstance) |
69 | 0 | return NS_ERROR_NOT_INITIALIZED; |
70 | 0 | |
71 | 0 | nsCOMArray<nsIFile> dirsToRemove; |
72 | 0 | nsCOMPtr<nsIThread> thread; |
73 | 0 | { |
74 | 0 | MutexAutoLock lock(gInstance->mLock); |
75 | 0 | NS_ASSERTION(!gInstance->mShutdownPending, |
76 | 0 | "Unexpected state in nsDeleteDir::Shutdown()"); |
77 | 0 | gInstance->mShutdownPending = true; |
78 | 0 |
|
79 | 0 | if (!finishDeleting) |
80 | 0 | gInstance->mStopDeleting = true; |
81 | 0 |
|
82 | 0 | // remove all pending timers |
83 | 0 | for (int32_t i = gInstance->mTimers.Count(); i > 0; i--) { |
84 | 0 | nsCOMPtr<nsITimer> timer = gInstance->mTimers[i-1]; |
85 | 0 | gInstance->mTimers.RemoveObjectAt(i-1); |
86 | 0 |
|
87 | 0 | nsCOMArray<nsIFile> *arg; |
88 | 0 | timer->GetClosure((reinterpret_cast<void**>(&arg))); |
89 | 0 | timer->Cancel(); |
90 | 0 |
|
91 | 0 | if (finishDeleting) |
92 | 0 | dirsToRemove.AppendObjects(*arg); |
93 | 0 |
|
94 | 0 | // delete argument passed to the timer |
95 | 0 | delete arg; |
96 | 0 | } |
97 | 0 |
|
98 | 0 | thread.swap(gInstance->mThread); |
99 | 0 | if (thread) { |
100 | 0 | // dispatch event and wait for it to run and notify us, so we know thread |
101 | 0 | // has completed all work and can be shutdown |
102 | 0 | nsCOMPtr<nsIRunnable> event = new nsBlockOnBackgroundThreadEvent(); |
103 | 0 | nsresult rv = thread->Dispatch(event, NS_DISPATCH_NORMAL); |
104 | 0 | if (NS_FAILED(rv)) { |
105 | 0 | NS_WARNING("Failed dispatching block-event"); |
106 | 0 | return NS_ERROR_UNEXPECTED; |
107 | 0 | } |
108 | 0 |
|
109 | 0 | gInstance->mNotified = false; |
110 | 0 | while (!gInstance->mNotified) { |
111 | 0 | gInstance->mCondVar.Wait(); |
112 | 0 | } |
113 | 0 | nsShutdownThread::BlockingShutdown(thread); |
114 | 0 | } |
115 | 0 | } |
116 | 0 |
|
117 | 0 | delete gInstance; |
118 | 0 |
|
119 | 0 | for (int32_t i = 0; i < dirsToRemove.Count(); i++) |
120 | 0 | dirsToRemove[i]->Remove(true); |
121 | 0 |
|
122 | 0 | return NS_OK; |
123 | 0 | } |
124 | | |
125 | | nsresult |
126 | | nsDeleteDir::InitThread() |
127 | 0 | { |
128 | 0 | if (mThread) |
129 | 0 | return NS_OK; |
130 | 0 | |
131 | 0 | nsresult rv = NS_NewNamedThread("Cache Deleter", getter_AddRefs(mThread)); |
132 | 0 | if (NS_FAILED(rv)) { |
133 | 0 | NS_WARNING("Can't create background thread"); |
134 | 0 | return rv; |
135 | 0 | } |
136 | 0 |
|
137 | 0 | nsCOMPtr<nsISupportsPriority> p = do_QueryInterface(mThread); |
138 | 0 | if (p) { |
139 | 0 | p->SetPriority(nsISupportsPriority::PRIORITY_LOWEST); |
140 | 0 | } |
141 | 0 | return NS_OK; |
142 | 0 | } |
143 | | |
144 | | void |
145 | | nsDeleteDir::DestroyThread() |
146 | 0 | { |
147 | 0 | if (!mThread) |
148 | 0 | return; |
149 | 0 | |
150 | 0 | if (mTimers.Count()) |
151 | 0 | // more work to do, so don't delete thread. |
152 | 0 | return; |
153 | 0 | |
154 | 0 | nsShutdownThread::Shutdown(mThread); |
155 | 0 | mThread = nullptr; |
156 | 0 | } |
157 | | |
158 | | void |
159 | | nsDeleteDir::TimerCallback(nsITimer *aTimer, void *arg) |
160 | 0 | { |
161 | 0 | Telemetry::AutoTimer<Telemetry::NETWORK_DISK_CACHE_DELETEDIR> timer; |
162 | 0 | { |
163 | 0 | MutexAutoLock lock(gInstance->mLock); |
164 | 0 |
|
165 | 0 | int32_t idx = gInstance->mTimers.IndexOf(aTimer); |
166 | 0 | if (idx == -1) { |
167 | 0 | // Timer was canceled and removed during shutdown. |
168 | 0 | return; |
169 | 0 | } |
170 | 0 | |
171 | 0 | gInstance->mTimers.RemoveObjectAt(idx); |
172 | 0 | } |
173 | 0 |
|
174 | 0 | nsAutoPtr<nsCOMArray<nsIFile> > dirList; |
175 | 0 | dirList = static_cast<nsCOMArray<nsIFile> *>(arg); |
176 | 0 |
|
177 | 0 | bool shuttingDown = false; |
178 | 0 |
|
179 | 0 | // Intentional extra braces to control variable sope. |
180 | 0 | { |
181 | 0 | // Low IO priority can only be set when running in the context of the |
182 | 0 | // current thread. So this shouldn't be moved to where we set the priority |
183 | 0 | // of the Cache deleter thread using the nsThread's NSPR priority constants. |
184 | 0 | nsAutoLowPriorityIO autoLowPriority; |
185 | 0 | for (int32_t i = 0; i < dirList->Count() && !shuttingDown; i++) { |
186 | 0 | gInstance->RemoveDir((*dirList)[i], &shuttingDown); |
187 | 0 | } |
188 | 0 | } |
189 | 0 |
|
190 | 0 | { |
191 | 0 | MutexAutoLock lock(gInstance->mLock); |
192 | 0 | gInstance->DestroyThread(); |
193 | 0 | } |
194 | 0 | } |
195 | | |
196 | | nsresult |
197 | | nsDeleteDir::DeleteDir(nsIFile *dirIn, bool moveToTrash, uint32_t delay) |
198 | 0 | { |
199 | 0 | Telemetry::AutoTimer<Telemetry::NETWORK_DISK_CACHE_TRASHRENAME> timer; |
200 | 0 |
|
201 | 0 | if (!gInstance) |
202 | 0 | return NS_ERROR_NOT_INITIALIZED; |
203 | 0 | |
204 | 0 | nsresult rv; |
205 | 0 | nsCOMPtr<nsIFile> trash, dir; |
206 | 0 |
|
207 | 0 | // Need to make a clone of this since we don't want to modify the input |
208 | 0 | // file object. |
209 | 0 | rv = dirIn->Clone(getter_AddRefs(dir)); |
210 | 0 | if (NS_FAILED(rv)) |
211 | 0 | return rv; |
212 | 0 | |
213 | 0 | if (moveToTrash) { |
214 | 0 | rv = GetTrashDir(dir, &trash); |
215 | 0 | if (NS_FAILED(rv)) |
216 | 0 | return rv; |
217 | 0 | nsAutoCString origLeaf; |
218 | 0 | rv = trash->GetNativeLeafName(origLeaf); |
219 | 0 | if (NS_FAILED(rv)) |
220 | 0 | return rv; |
221 | 0 | |
222 | 0 | // Append random number to the trash directory and check if it exists. |
223 | 0 | srand(static_cast<unsigned>(PR_Now())); |
224 | 0 | nsAutoCString leaf; |
225 | 0 | for (int32_t i = 0; i < 10; i++) { |
226 | 0 | leaf = origLeaf; |
227 | 0 | leaf.AppendInt(rand()); |
228 | 0 | rv = trash->SetNativeLeafName(leaf); |
229 | 0 | if (NS_FAILED(rv)) |
230 | 0 | return rv; |
231 | 0 | |
232 | 0 | bool exists; |
233 | 0 | if (NS_SUCCEEDED(trash->Exists(&exists)) && !exists) { |
234 | 0 | break; |
235 | 0 | } |
236 | 0 | |
237 | 0 | leaf.Truncate(); |
238 | 0 | } |
239 | 0 |
|
240 | 0 | // Fail if we didn't find unused trash directory within the limit |
241 | 0 | if (!leaf.Length()) |
242 | 0 | return NS_ERROR_FAILURE; |
243 | 0 | |
244 | | #if defined(MOZ_WIDGET_ANDROID) |
245 | | nsCOMPtr<nsIFile> parent; |
246 | | rv = trash->GetParent(getter_AddRefs(parent)); |
247 | | if (NS_FAILED(rv)) |
248 | | return rv; |
249 | | rv = dir->MoveToNative(parent, leaf); |
250 | | #else |
251 | | // Important: must rename directory w/o changing parent directory: else on |
252 | 0 | // NTFS we'll wait (with cache lock) while nsIFile's ACL reset walks file |
253 | 0 | // tree: was hanging GUI for *minutes* on large cache dirs. |
254 | 0 | rv = dir->MoveToNative(nullptr, leaf); |
255 | 0 | #endif |
256 | 0 | if (NS_FAILED(rv)) |
257 | 0 | return rv; |
258 | 0 | } else { |
259 | 0 | // we want to pass a clone of the original off to the worker thread. |
260 | 0 | trash.swap(dir); |
261 | 0 | } |
262 | 0 |
|
263 | 0 | nsAutoPtr<nsCOMArray<nsIFile> > arg(new nsCOMArray<nsIFile>); |
264 | 0 | arg->AppendObject(trash); |
265 | 0 |
|
266 | 0 | rv = gInstance->PostTimer(arg, delay); |
267 | 0 | if (NS_FAILED(rv)) |
268 | 0 | return rv; |
269 | 0 | |
270 | 0 | arg.forget(); |
271 | 0 | return NS_OK; |
272 | 0 | } |
273 | | |
274 | | nsresult |
275 | | nsDeleteDir::GetTrashDir(nsIFile *target, nsCOMPtr<nsIFile> *result) |
276 | 0 | { |
277 | 0 | nsresult rv; |
278 | | #if defined(MOZ_WIDGET_ANDROID) |
279 | | // Try to use the app cache folder for cache trash on Android |
280 | | char* cachePath = getenv("CACHE_DIRECTORY"); |
281 | | if (cachePath) { |
282 | | rv = NS_NewNativeLocalFile(nsDependentCString(cachePath), |
283 | | true, getter_AddRefs(*result)); |
284 | | if (NS_FAILED(rv)) |
285 | | return rv; |
286 | | |
287 | | // Add a sub folder with the cache folder name |
288 | | nsAutoCString leaf; |
289 | | rv = target->GetNativeLeafName(leaf); |
290 | | (*result)->AppendNative(leaf); |
291 | | } else |
292 | | #endif |
293 | | { |
294 | 0 | rv = target->Clone(getter_AddRefs(*result)); |
295 | 0 | } |
296 | 0 | if (NS_FAILED(rv)) |
297 | 0 | return rv; |
298 | 0 | |
299 | 0 | nsAutoCString leaf; |
300 | 0 | rv = (*result)->GetNativeLeafName(leaf); |
301 | 0 | if (NS_FAILED(rv)) |
302 | 0 | return rv; |
303 | 0 | leaf.AppendLiteral(".Trash"); |
304 | 0 |
|
305 | 0 | return (*result)->SetNativeLeafName(leaf); |
306 | 0 | } |
307 | | |
308 | | nsresult |
309 | | nsDeleteDir::RemoveOldTrashes(nsIFile *cacheDir) |
310 | 0 | { |
311 | 0 | if (!gInstance) |
312 | 0 | return NS_ERROR_NOT_INITIALIZED; |
313 | 0 | |
314 | 0 | nsresult rv; |
315 | 0 |
|
316 | 0 | nsCOMPtr<nsIFile> trash; |
317 | 0 | rv = GetTrashDir(cacheDir, &trash); |
318 | 0 | if (NS_FAILED(rv)) |
319 | 0 | return rv; |
320 | 0 | |
321 | 0 | nsAutoString trashName; |
322 | 0 | rv = trash->GetLeafName(trashName); |
323 | 0 | if (NS_FAILED(rv)) |
324 | 0 | return rv; |
325 | 0 | |
326 | 0 | nsCOMPtr<nsIFile> parent; |
327 | | #if defined(MOZ_WIDGET_ANDROID) |
328 | | rv = trash->GetParent(getter_AddRefs(parent)); |
329 | | #else |
330 | | rv = cacheDir->GetParent(getter_AddRefs(parent)); |
331 | 0 | #endif |
332 | 0 | if (NS_FAILED(rv)) |
333 | 0 | return rv; |
334 | 0 | |
335 | 0 | nsCOMPtr<nsIDirectoryEnumerator> iter; |
336 | 0 | rv = parent->GetDirectoryEntries(getter_AddRefs(iter)); |
337 | 0 | if (NS_FAILED(rv)) |
338 | 0 | return rv; |
339 | 0 | |
340 | 0 | nsAutoPtr<nsCOMArray<nsIFile> > dirList; |
341 | 0 |
|
342 | 0 | nsCOMPtr<nsIFile> file; |
343 | 0 | while (NS_SUCCEEDED(iter->GetNextFile(getter_AddRefs(file))) && file) { |
344 | 0 | nsAutoString leafName; |
345 | 0 | rv = file->GetLeafName(leafName); |
346 | 0 | if (NS_FAILED(rv)) |
347 | 0 | continue; |
348 | 0 | |
349 | 0 | // match all names that begin with the trash name (i.e. "Cache.Trash") |
350 | 0 | if (Substring(leafName, 0, trashName.Length()).Equals(trashName)) { |
351 | 0 | if (!dirList) |
352 | 0 | dirList = new nsCOMArray<nsIFile>; |
353 | 0 | dirList->AppendObject(file); |
354 | 0 | } |
355 | 0 | } |
356 | 0 |
|
357 | 0 | if (dirList) { |
358 | 0 | rv = gInstance->PostTimer(dirList, 90000); |
359 | 0 | if (NS_FAILED(rv)) |
360 | 0 | return rv; |
361 | 0 | |
362 | 0 | dirList.forget(); |
363 | 0 | } |
364 | 0 |
|
365 | 0 | return NS_OK; |
366 | 0 | } |
367 | | |
368 | | nsresult |
369 | | nsDeleteDir::PostTimer(void *arg, uint32_t delay) |
370 | 0 | { |
371 | 0 | nsresult rv; |
372 | 0 |
|
373 | 0 | MutexAutoLock lock(mLock); |
374 | 0 |
|
375 | 0 | rv = InitThread(); |
376 | 0 | if (NS_FAILED(rv)) |
377 | 0 | return rv; |
378 | 0 | |
379 | 0 | nsCOMPtr<nsITimer> timer; |
380 | 0 | rv = NS_NewTimerWithFuncCallback(getter_AddRefs(timer), |
381 | 0 | TimerCallback, |
382 | 0 | arg, |
383 | 0 | delay, |
384 | 0 | nsITimer::TYPE_ONE_SHOT, |
385 | 0 | "nsDeleteDir::PostTimer", |
386 | 0 | mThread); |
387 | 0 | if (NS_FAILED(rv)) |
388 | 0 | return rv; |
389 | 0 | |
390 | 0 | mTimers.AppendObject(timer); |
391 | 0 | return NS_OK; |
392 | 0 | } |
393 | | |
394 | | nsresult |
395 | | nsDeleteDir::RemoveDir(nsIFile *file, bool *stopDeleting) |
396 | 0 | { |
397 | 0 | nsresult rv; |
398 | 0 | bool isLink; |
399 | 0 |
|
400 | 0 | rv = file->IsSymlink(&isLink); |
401 | 0 | if (NS_FAILED(rv) || isLink) |
402 | 0 | return NS_ERROR_UNEXPECTED; |
403 | 0 | |
404 | 0 | bool isDir; |
405 | 0 | rv = file->IsDirectory(&isDir); |
406 | 0 | if (NS_FAILED(rv)) |
407 | 0 | return rv; |
408 | 0 | |
409 | 0 | if (isDir) { |
410 | 0 | nsCOMPtr<nsIDirectoryEnumerator> iter; |
411 | 0 | rv = file->GetDirectoryEntries(getter_AddRefs(iter)); |
412 | 0 | if (NS_FAILED(rv)) |
413 | 0 | return rv; |
414 | 0 | |
415 | 0 | nsCOMPtr<nsIFile> file2; |
416 | 0 | while (NS_SUCCEEDED(iter->GetNextFile(getter_AddRefs(file2))) && file2) { |
417 | 0 | RemoveDir(file2, stopDeleting); |
418 | 0 | // No check for errors to remove as much as possible |
419 | 0 |
|
420 | 0 | if (*stopDeleting) |
421 | 0 | return NS_OK; |
422 | 0 | } |
423 | 0 | } |
424 | 0 |
|
425 | 0 | file->Remove(false); |
426 | 0 | // No check for errors to remove as much as possible |
427 | 0 |
|
428 | 0 | MutexAutoLock lock(mLock); |
429 | 0 | if (mStopDeleting) |
430 | 0 | *stopDeleting = true; |
431 | 0 |
|
432 | 0 | return NS_OK; |
433 | 0 | } |