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