DropBoxManagerService.java revision 3a8b0c18a49c017ecfde8ebf4eb4bb5181ac6fef
1/* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.server; 18 19import android.content.BroadcastReceiver; 20import android.content.ContentResolver; 21import android.content.Context; 22import android.content.Intent; 23import android.content.IntentFilter; 24import android.content.pm.PackageManager; 25import android.database.ContentObserver; 26import android.net.Uri; 27import android.os.Debug; 28import android.os.DropBoxManager; 29import android.os.Handler; 30import android.os.ParcelFileDescriptor; 31import android.os.StatFs; 32import android.os.SystemClock; 33import android.provider.Settings; 34import android.text.format.Time; 35import android.util.Slog; 36 37import com.android.internal.os.IDropBoxManagerService; 38 39import java.io.File; 40import java.io.FileDescriptor; 41import java.io.FileOutputStream; 42import java.io.IOException; 43import java.io.InputStream; 44import java.io.InputStreamReader; 45import java.io.OutputStream; 46import java.io.OutputStreamWriter; 47import java.io.PrintWriter; 48import java.io.UnsupportedEncodingException; 49import java.util.ArrayList; 50import java.util.Comparator; 51import java.util.HashMap; 52import java.util.Iterator; 53import java.util.Map; 54import java.util.SortedSet; 55import java.util.TreeSet; 56import java.util.zip.GZIPOutputStream; 57 58/** 59 * Implementation of {@link IDropBoxManagerService} using the filesystem. 60 * Clients use {@link DropBoxManager} to access this service. 61 */ 62public final class DropBoxManagerService extends IDropBoxManagerService.Stub { 63 private static final String TAG = "DropBoxManagerService"; 64 private static final int DEFAULT_AGE_SECONDS = 3 * 86400; 65 private static final int DEFAULT_MAX_FILES = 1000; 66 private static final int DEFAULT_QUOTA_KB = 5 * 1024; 67 private static final int DEFAULT_QUOTA_PERCENT = 10; 68 private static final int DEFAULT_RESERVE_PERCENT = 10; 69 private static final int QUOTA_RESCAN_MILLIS = 5000; 70 71 private static final boolean PROFILE_DUMP = false; 72 73 // TODO: This implementation currently uses one file per entry, which is 74 // inefficient for smallish entries -- consider using a single queue file 75 // per tag (or even globally) instead. 76 77 // The cached context and derived objects 78 79 private final Context mContext; 80 private final ContentResolver mContentResolver; 81 private final File mDropBoxDir; 82 83 // Accounting of all currently written log files (set in init()). 84 85 private FileList mAllFiles = null; 86 private HashMap<String, FileList> mFilesByTag = null; 87 88 // Various bits of disk information 89 90 private StatFs mStatFs = null; 91 private int mBlockSize = 0; 92 private int mCachedQuotaBlocks = 0; // Space we can use: computed from free space, etc. 93 private long mCachedQuotaUptimeMillis = 0; 94 95 // Ensure that all log entries have a unique timestamp 96 private long mLastTimestamp = 0; 97 98 /** Receives events that might indicate a need to clean up files. */ 99 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 100 @Override 101 public void onReceive(Context context, Intent intent) { 102 mCachedQuotaUptimeMillis = 0; // Force a re-check of quota size 103 104 // Run the initialization in the background (not this main thread). 105 // The init() and trimToFit() methods are synchronized, so they still 106 // block other users -- but at least the onReceive() call can finish. 107 new Thread() { 108 public void run() { 109 try { 110 init(); 111 trimToFit(); 112 } catch (IOException e) { 113 Slog.e(TAG, "Can't init", e); 114 } 115 } 116 }.start(); 117 } 118 }; 119 120 /** 121 * Creates an instance of managed drop box storage. Normally there is one of these 122 * run by the system, but others can be created for testing and other purposes. 123 * 124 * @param context to use for receiving free space & gservices intents 125 * @param path to store drop box entries in 126 */ 127 public DropBoxManagerService(final Context context, File path) { 128 mDropBoxDir = path; 129 130 // Set up intent receivers 131 mContext = context; 132 mContentResolver = context.getContentResolver(); 133 context.registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_DEVICE_STORAGE_LOW)); 134 135 mContentResolver.registerContentObserver( 136 Settings.Secure.CONTENT_URI, true, 137 new ContentObserver(new Handler()) { 138 public void onChange(boolean selfChange) { 139 mReceiver.onReceive(context, (Intent) null); 140 } 141 }); 142 143 // The real work gets done lazily in init() -- that way service creation always 144 // succeeds, and things like disk problems cause individual method failures. 145 } 146 147 /** Unregisters broadcast receivers and any other hooks -- for test instances */ 148 public void stop() { 149 mContext.unregisterReceiver(mReceiver); 150 } 151 152 public void add(DropBoxManager.Entry entry) { 153 File temp = null; 154 OutputStream output = null; 155 final String tag = entry.getTag(); 156 try { 157 int flags = entry.getFlags(); 158 if ((flags & DropBoxManager.IS_EMPTY) != 0) throw new IllegalArgumentException(); 159 160 init(); 161 if (!isTagEnabled(tag)) return; 162 long max = trimToFit(); 163 long lastTrim = System.currentTimeMillis(); 164 165 byte[] buffer = new byte[mBlockSize]; 166 InputStream input = entry.getInputStream(); 167 168 // First, accumulate up to one block worth of data in memory before 169 // deciding whether to compress the data or not. 170 171 int read = 0; 172 while (read < buffer.length) { 173 int n = input.read(buffer, read, buffer.length - read); 174 if (n <= 0) break; 175 read += n; 176 } 177 178 // If we have at least one block, compress it -- otherwise, just write 179 // the data in uncompressed form. 180 181 temp = new File(mDropBoxDir, "drop" + Thread.currentThread().getId() + ".tmp"); 182 output = new FileOutputStream(temp); 183 if (read == buffer.length && ((flags & DropBoxManager.IS_GZIPPED) == 0)) { 184 output = new GZIPOutputStream(output); 185 flags = flags | DropBoxManager.IS_GZIPPED; 186 } 187 188 do { 189 output.write(buffer, 0, read); 190 191 long now = System.currentTimeMillis(); 192 if (now - lastTrim > 30 * 1000) { 193 max = trimToFit(); // In case data dribbles in slowly 194 lastTrim = now; 195 } 196 197 read = input.read(buffer); 198 if (read <= 0) { 199 output.close(); // Get a final size measurement 200 output = null; 201 } else { 202 output.flush(); // So the size measurement is pseudo-reasonable 203 } 204 205 long len = temp.length(); 206 if (len > max) { 207 Slog.w(TAG, "Dropping: " + tag + " (" + temp.length() + " > " + max + " bytes)"); 208 temp.delete(); 209 temp = null; // Pass temp = null to createEntry() to leave a tombstone 210 break; 211 } 212 } while (read > 0); 213 214 createEntry(temp, tag, flags); 215 temp = null; 216 } catch (IOException e) { 217 Slog.e(TAG, "Can't write: " + tag, e); 218 } finally { 219 try { if (output != null) output.close(); } catch (IOException e) {} 220 entry.close(); 221 if (temp != null) temp.delete(); 222 } 223 } 224 225 public boolean isTagEnabled(String tag) { 226 return !"disabled".equals(Settings.Secure.getString( 227 mContentResolver, Settings.Secure.DROPBOX_TAG_PREFIX + tag)); 228 } 229 230 public synchronized DropBoxManager.Entry getNextEntry(String tag, long millis) { 231 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.READ_LOGS) 232 != PackageManager.PERMISSION_GRANTED) { 233 throw new SecurityException("READ_LOGS permission required"); 234 } 235 236 try { 237 init(); 238 } catch (IOException e) { 239 Slog.e(TAG, "Can't init", e); 240 return null; 241 } 242 243 FileList list = tag == null ? mAllFiles : mFilesByTag.get(tag); 244 if (list == null) return null; 245 246 for (EntryFile entry : list.contents.tailSet(new EntryFile(millis + 1))) { 247 if (entry.tag == null) continue; 248 if ((entry.flags & DropBoxManager.IS_EMPTY) != 0) { 249 return new DropBoxManager.Entry(entry.tag, entry.timestampMillis); 250 } 251 try { 252 return new DropBoxManager.Entry( 253 entry.tag, entry.timestampMillis, entry.file, entry.flags); 254 } catch (IOException e) { 255 Slog.e(TAG, "Can't read: " + entry.file, e); 256 // Continue to next file 257 } 258 } 259 260 return null; 261 } 262 263 public synchronized void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 264 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) 265 != PackageManager.PERMISSION_GRANTED) { 266 pw.println("Permission Denial: Can't dump DropBoxManagerService"); 267 return; 268 } 269 270 try { 271 init(); 272 } catch (IOException e) { 273 pw.println("Can't initialize: " + e); 274 Slog.e(TAG, "Can't init", e); 275 return; 276 } 277 278 if (PROFILE_DUMP) Debug.startMethodTracing("/data/trace/dropbox.dump"); 279 280 StringBuilder out = new StringBuilder(); 281 boolean doPrint = false, doFile = false; 282 ArrayList<String> searchArgs = new ArrayList<String>(); 283 for (int i = 0; args != null && i < args.length; i++) { 284 if (args[i].equals("-p") || args[i].equals("--print")) { 285 doPrint = true; 286 } else if (args[i].equals("-f") || args[i].equals("--file")) { 287 doFile = true; 288 } else if (args[i].startsWith("-")) { 289 out.append("Unknown argument: ").append(args[i]).append("\n"); 290 } else { 291 searchArgs.add(args[i]); 292 } 293 } 294 295 out.append("Drop box contents: ").append(mAllFiles.contents.size()).append(" entries\n"); 296 297 if (!searchArgs.isEmpty()) { 298 out.append("Searching for:"); 299 for (String a : searchArgs) out.append(" ").append(a); 300 out.append("\n"); 301 } 302 303 int numFound = 0, numArgs = searchArgs.size(); 304 Time time = new Time(); 305 out.append("\n"); 306 for (EntryFile entry : mAllFiles.contents) { 307 time.set(entry.timestampMillis); 308 String date = time.format("%Y-%m-%d %H:%M:%S"); 309 boolean match = true; 310 for (int i = 0; i < numArgs && match; i++) { 311 String arg = searchArgs.get(i); 312 match = (date.contains(arg) || arg.equals(entry.tag)); 313 } 314 if (!match) continue; 315 316 numFound++; 317 if (doPrint) out.append("========================================\n"); 318 out.append(date).append(" ").append(entry.tag == null ? "(no tag)" : entry.tag); 319 if (entry.file == null) { 320 out.append(" (no file)\n"); 321 continue; 322 } else if ((entry.flags & DropBoxManager.IS_EMPTY) != 0) { 323 out.append(" (contents lost)\n"); 324 continue; 325 } else { 326 out.append(" ("); 327 if ((entry.flags & DropBoxManager.IS_GZIPPED) != 0) out.append("compressed "); 328 out.append((entry.flags & DropBoxManager.IS_TEXT) != 0 ? "text" : "data"); 329 out.append(", ").append(entry.file.length()).append(" bytes)\n"); 330 } 331 332 if (doFile || (doPrint && (entry.flags & DropBoxManager.IS_TEXT) == 0)) { 333 if (!doPrint) out.append(" "); 334 out.append(entry.file.getPath()).append("\n"); 335 } 336 337 if ((entry.flags & DropBoxManager.IS_TEXT) != 0 && (doPrint || !doFile)) { 338 DropBoxManager.Entry dbe = null; 339 try { 340 dbe = new DropBoxManager.Entry( 341 entry.tag, entry.timestampMillis, entry.file, entry.flags); 342 343 if (doPrint) { 344 InputStreamReader r = new InputStreamReader(dbe.getInputStream()); 345 char[] buf = new char[4096]; 346 boolean newline = false; 347 for (;;) { 348 int n = r.read(buf); 349 if (n <= 0) break; 350 out.append(buf, 0, n); 351 newline = (buf[n - 1] == '\n'); 352 353 // Flush periodically when printing to avoid out-of-memory. 354 if (out.length() > 65536) { 355 pw.write(out.toString()); 356 out.setLength(0); 357 } 358 } 359 if (!newline) out.append("\n"); 360 } else { 361 String text = dbe.getText(70); 362 boolean truncated = (text.length() == 70); 363 out.append(" ").append(text.trim().replace('\n', '/')); 364 if (truncated) out.append(" ..."); 365 out.append("\n"); 366 } 367 } catch (IOException e) { 368 out.append("*** ").append(e.toString()).append("\n"); 369 Slog.e(TAG, "Can't read: " + entry.file, e); 370 } finally { 371 if (dbe != null) dbe.close(); 372 } 373 } 374 375 if (doPrint) out.append("\n"); 376 } 377 378 if (numFound == 0) out.append("(No entries found.)\n"); 379 380 if (args == null || args.length == 0) { 381 if (!doPrint) out.append("\n"); 382 out.append("Usage: dumpsys dropbox [--print|--file] [YYYY-mm-dd] [HH:MM:SS] [tag]\n"); 383 } 384 385 pw.write(out.toString()); 386 if (PROFILE_DUMP) Debug.stopMethodTracing(); 387 } 388 389 /////////////////////////////////////////////////////////////////////////// 390 391 /** Chronologically sorted list of {@link #EntryFile} */ 392 private static final class FileList implements Comparable<FileList> { 393 public int blocks = 0; 394 public final TreeSet<EntryFile> contents = new TreeSet<EntryFile>(); 395 396 /** Sorts bigger FileList instances before smaller ones. */ 397 public final int compareTo(FileList o) { 398 if (blocks != o.blocks) return o.blocks - blocks; 399 if (this == o) return 0; 400 if (hashCode() < o.hashCode()) return -1; 401 if (hashCode() > o.hashCode()) return 1; 402 return 0; 403 } 404 } 405 406 /** Metadata describing an on-disk log file. */ 407 private static final class EntryFile implements Comparable<EntryFile> { 408 public final String tag; 409 public final long timestampMillis; 410 public final int flags; 411 public final File file; 412 public final int blocks; 413 414 /** Sorts earlier EntryFile instances before later ones. */ 415 public final int compareTo(EntryFile o) { 416 if (timestampMillis < o.timestampMillis) return -1; 417 if (timestampMillis > o.timestampMillis) return 1; 418 if (file != null && o.file != null) return file.compareTo(o.file); 419 if (o.file != null) return -1; 420 if (file != null) return 1; 421 if (this == o) return 0; 422 if (hashCode() < o.hashCode()) return -1; 423 if (hashCode() > o.hashCode()) return 1; 424 return 0; 425 } 426 427 /** 428 * Moves an existing temporary file to a new log filename. 429 * @param temp file to rename 430 * @param dir to store file in 431 * @param tag to use for new log file name 432 * @param timestampMillis of log entry 433 * @param flags for the entry data 434 * @param blockSize to use for space accounting 435 * @throws IOException if the file can't be moved 436 */ 437 public EntryFile(File temp, File dir, String tag,long timestampMillis, 438 int flags, int blockSize) throws IOException { 439 if ((flags & DropBoxManager.IS_EMPTY) != 0) throw new IllegalArgumentException(); 440 441 this.tag = tag; 442 this.timestampMillis = timestampMillis; 443 this.flags = flags; 444 this.file = new File(dir, Uri.encode(tag) + "@" + timestampMillis + 445 ((flags & DropBoxManager.IS_TEXT) != 0 ? ".txt" : ".dat") + 446 ((flags & DropBoxManager.IS_GZIPPED) != 0 ? ".gz" : "")); 447 448 if (!temp.renameTo(this.file)) { 449 throw new IOException("Can't rename " + temp + " to " + this.file); 450 } 451 this.blocks = (int) ((this.file.length() + blockSize - 1) / blockSize); 452 } 453 454 /** 455 * Creates a zero-length tombstone for a file whose contents were lost. 456 * @param dir to store file in 457 * @param tag to use for new log file name 458 * @param timestampMillis of log entry 459 * @throws IOException if the file can't be created. 460 */ 461 public EntryFile(File dir, String tag, long timestampMillis) throws IOException { 462 this.tag = tag; 463 this.timestampMillis = timestampMillis; 464 this.flags = DropBoxManager.IS_EMPTY; 465 this.file = new File(dir, Uri.encode(tag) + "@" + timestampMillis + ".lost"); 466 this.blocks = 0; 467 new FileOutputStream(this.file).close(); 468 } 469 470 /** 471 * Extracts metadata from an existing on-disk log filename. 472 * @param file name of existing log file 473 * @param blockSize to use for space accounting 474 */ 475 public EntryFile(File file, int blockSize) { 476 this.file = file; 477 this.blocks = (int) ((this.file.length() + blockSize - 1) / blockSize); 478 479 String name = file.getName(); 480 int at = name.lastIndexOf('@'); 481 if (at < 0) { 482 this.tag = null; 483 this.timestampMillis = 0; 484 this.flags = DropBoxManager.IS_EMPTY; 485 return; 486 } 487 488 int flags = 0; 489 this.tag = Uri.decode(name.substring(0, at)); 490 if (name.endsWith(".gz")) { 491 flags |= DropBoxManager.IS_GZIPPED; 492 name = name.substring(0, name.length() - 3); 493 } 494 if (name.endsWith(".lost")) { 495 flags |= DropBoxManager.IS_EMPTY; 496 name = name.substring(at + 1, name.length() - 5); 497 } else if (name.endsWith(".txt")) { 498 flags |= DropBoxManager.IS_TEXT; 499 name = name.substring(at + 1, name.length() - 4); 500 } else if (name.endsWith(".dat")) { 501 name = name.substring(at + 1, name.length() - 4); 502 } else { 503 this.flags = DropBoxManager.IS_EMPTY; 504 this.timestampMillis = 0; 505 return; 506 } 507 this.flags = flags; 508 509 long millis; 510 try { millis = Long.valueOf(name); } catch (NumberFormatException e) { millis = 0; } 511 this.timestampMillis = millis; 512 } 513 514 /** 515 * Creates a EntryFile object with only a timestamp for comparison purposes. 516 * @param timestampMillis to compare with. 517 */ 518 public EntryFile(long millis) { 519 this.tag = null; 520 this.timestampMillis = millis; 521 this.flags = DropBoxManager.IS_EMPTY; 522 this.file = null; 523 this.blocks = 0; 524 } 525 } 526 527 /////////////////////////////////////////////////////////////////////////// 528 529 /** If never run before, scans disk contents to build in-memory tracking data. */ 530 private synchronized void init() throws IOException { 531 if (mStatFs == null) { 532 if (!mDropBoxDir.isDirectory() && !mDropBoxDir.mkdirs()) { 533 throw new IOException("Can't mkdir: " + mDropBoxDir); 534 } 535 try { 536 mStatFs = new StatFs(mDropBoxDir.getPath()); 537 mBlockSize = mStatFs.getBlockSize(); 538 } catch (IllegalArgumentException e) { // StatFs throws this on error 539 throw new IOException("Can't statfs: " + mDropBoxDir); 540 } 541 } 542 543 if (mAllFiles == null) { 544 File[] files = mDropBoxDir.listFiles(); 545 if (files == null) throw new IOException("Can't list files: " + mDropBoxDir); 546 547 mAllFiles = new FileList(); 548 mFilesByTag = new HashMap<String, FileList>(); 549 550 // Scan pre-existing files. 551 for (File file : files) { 552 if (file.getName().endsWith(".tmp")) { 553 Slog.i(TAG, "Cleaning temp file: " + file); 554 file.delete(); 555 continue; 556 } 557 558 EntryFile entry = new EntryFile(file, mBlockSize); 559 if (entry.tag == null) { 560 Slog.w(TAG, "Unrecognized file: " + file); 561 continue; 562 } else if (entry.timestampMillis == 0) { 563 Slog.w(TAG, "Invalid filename: " + file); 564 file.delete(); 565 continue; 566 } 567 568 enrollEntry(entry); 569 } 570 } 571 } 572 573 /** Adds a disk log file to in-memory tracking for accounting and enumeration. */ 574 private synchronized void enrollEntry(EntryFile entry) { 575 mAllFiles.contents.add(entry); 576 mAllFiles.blocks += entry.blocks; 577 578 // mFilesByTag is used for trimming, so don't list empty files. 579 // (Zero-length/lost files are trimmed by date from mAllFiles.) 580 581 if (entry.tag != null && entry.file != null && entry.blocks > 0) { 582 FileList tagFiles = mFilesByTag.get(entry.tag); 583 if (tagFiles == null) { 584 tagFiles = new FileList(); 585 mFilesByTag.put(entry.tag, tagFiles); 586 } 587 tagFiles.contents.add(entry); 588 tagFiles.blocks += entry.blocks; 589 } 590 } 591 592 /** Moves a temporary file to a final log filename and enrolls it. */ 593 private synchronized void createEntry(File temp, String tag, int flags) throws IOException { 594 long t = System.currentTimeMillis(); 595 596 // Require each entry to have a unique timestamp; if there are entries 597 // >10sec in the future (due to clock skew), drag them back to avoid 598 // keeping them around forever. 599 600 SortedSet<EntryFile> tail = mAllFiles.contents.tailSet(new EntryFile(t + 10000)); 601 EntryFile[] future = null; 602 if (!tail.isEmpty()) { 603 future = tail.toArray(new EntryFile[tail.size()]); 604 tail.clear(); // Remove from mAllFiles 605 } 606 607 if (!mAllFiles.contents.isEmpty()) { 608 t = Math.max(t, mAllFiles.contents.last().timestampMillis + 1); 609 } 610 611 if (future != null) { 612 for (EntryFile late : future) { 613 mAllFiles.blocks -= late.blocks; 614 FileList tagFiles = mFilesByTag.get(late.tag); 615 if (tagFiles != null && tagFiles.contents.remove(late)) { 616 tagFiles.blocks -= late.blocks; 617 } 618 if ((late.flags & DropBoxManager.IS_EMPTY) == 0) { 619 enrollEntry(new EntryFile( 620 late.file, mDropBoxDir, late.tag, t++, late.flags, mBlockSize)); 621 } else { 622 enrollEntry(new EntryFile(mDropBoxDir, late.tag, t++)); 623 } 624 } 625 } 626 627 if (temp == null) { 628 enrollEntry(new EntryFile(mDropBoxDir, tag, t)); 629 } else { 630 enrollEntry(new EntryFile(temp, mDropBoxDir, tag, t, flags, mBlockSize)); 631 } 632 } 633 634 /** 635 * Trims the files on disk to make sure they aren't using too much space. 636 * @return the overall quota for storage (in bytes) 637 */ 638 private synchronized long trimToFit() { 639 // Expunge aged items (including tombstones marking deleted data). 640 641 int ageSeconds = Settings.Secure.getInt(mContentResolver, 642 Settings.Secure.DROPBOX_AGE_SECONDS, DEFAULT_AGE_SECONDS); 643 int maxFiles = Settings.Secure.getInt(mContentResolver, 644 Settings.Secure.DROPBOX_MAX_FILES, DEFAULT_MAX_FILES); 645 long cutoffMillis = System.currentTimeMillis() - ageSeconds * 1000; 646 while (!mAllFiles.contents.isEmpty()) { 647 EntryFile entry = mAllFiles.contents.first(); 648 if (entry.timestampMillis > cutoffMillis && mAllFiles.contents.size() < maxFiles) break; 649 650 FileList tag = mFilesByTag.get(entry.tag); 651 if (tag != null && tag.contents.remove(entry)) tag.blocks -= entry.blocks; 652 if (mAllFiles.contents.remove(entry)) mAllFiles.blocks -= entry.blocks; 653 if (entry.file != null) entry.file.delete(); 654 } 655 656 // Compute overall quota (a fraction of available free space) in blocks. 657 // The quota changes dynamically based on the amount of free space; 658 // that way when lots of data is available we can use it, but we'll get 659 // out of the way if storage starts getting tight. 660 661 long uptimeMillis = SystemClock.uptimeMillis(); 662 if (uptimeMillis > mCachedQuotaUptimeMillis + QUOTA_RESCAN_MILLIS) { 663 int quotaPercent = Settings.Secure.getInt(mContentResolver, 664 Settings.Secure.DROPBOX_QUOTA_PERCENT, DEFAULT_QUOTA_PERCENT); 665 int reservePercent = Settings.Secure.getInt(mContentResolver, 666 Settings.Secure.DROPBOX_RESERVE_PERCENT, DEFAULT_RESERVE_PERCENT); 667 int quotaKb = Settings.Secure.getInt(mContentResolver, 668 Settings.Secure.DROPBOX_QUOTA_KB, DEFAULT_QUOTA_KB); 669 670 mStatFs.restat(mDropBoxDir.getPath()); 671 int available = mStatFs.getAvailableBlocks(); 672 int nonreserved = available - mStatFs.getBlockCount() * reservePercent / 100; 673 int maximum = quotaKb * 1024 / mBlockSize; 674 mCachedQuotaBlocks = Math.min(maximum, Math.max(0, nonreserved * quotaPercent / 100)); 675 mCachedQuotaUptimeMillis = uptimeMillis; 676 } 677 678 // If we're using too much space, delete old items to make room. 679 // 680 // We trim each tag independently (this is why we keep per-tag lists). 681 // Space is "fairly" shared between tags -- they are all squeezed 682 // equally until enough space is reclaimed. 683 // 684 // A single circular buffer (a la logcat) would be simpler, but this 685 // way we can handle fat/bursty data (like 1MB+ bugreports, 300KB+ 686 // kernel crash dumps, and 100KB+ ANR reports) without swamping small, 687 // well-behaved data streams (event statistics, profile data, etc). 688 // 689 // Deleted files are replaced with zero-length tombstones to mark what 690 // was lost. Tombstones are expunged by age (see above). 691 692 if (mAllFiles.blocks > mCachedQuotaBlocks) { 693 Slog.i(TAG, "Usage (" + mAllFiles.blocks + ") > Quota (" + mCachedQuotaBlocks + ")"); 694 695 // Find a fair share amount of space to limit each tag 696 int unsqueezed = mAllFiles.blocks, squeezed = 0; 697 TreeSet<FileList> tags = new TreeSet<FileList>(mFilesByTag.values()); 698 for (FileList tag : tags) { 699 if (squeezed > 0 && tag.blocks <= (mCachedQuotaBlocks - unsqueezed) / squeezed) { 700 break; 701 } 702 unsqueezed -= tag.blocks; 703 squeezed++; 704 } 705 int tagQuota = (mCachedQuotaBlocks - unsqueezed) / squeezed; 706 707 // Remove old items from each tag until it meets the per-tag quota. 708 for (FileList tag : tags) { 709 if (mAllFiles.blocks < mCachedQuotaBlocks) break; 710 while (tag.blocks > tagQuota && !tag.contents.isEmpty()) { 711 EntryFile entry = tag.contents.first(); 712 if (tag.contents.remove(entry)) tag.blocks -= entry.blocks; 713 if (mAllFiles.contents.remove(entry)) mAllFiles.blocks -= entry.blocks; 714 715 try { 716 if (entry.file != null) entry.file.delete(); 717 enrollEntry(new EntryFile(mDropBoxDir, entry.tag, entry.timestampMillis)); 718 } catch (IOException e) { 719 Slog.e(TAG, "Can't write tombstone file", e); 720 } 721 } 722 } 723 } 724 725 return mCachedQuotaBlocks * mBlockSize; 726 } 727} 728